1 /*
2 * Extended Dynamic Object format
3 *
4 * Copyright (C) 2004-2007 Peter Johnson
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <util.h>
28
29 #include <libyasm.h>
30
31
32 #define REGULAR_OUTBUF_SIZE 1024
33
34 #define XDF_MAGIC 0x87654322
35
36 #define XDF_SYM_EXTERN 1
37 #define XDF_SYM_GLOBAL 2
38 #define XDF_SYM_EQU 4
39
40 typedef struct xdf_reloc {
41 yasm_reloc reloc;
42 /*@null@*/ yasm_symrec *base; /* base symbol (for WRT) */
43 enum {
44 XDF_RELOC_REL = 1, /* relative to segment */
45 XDF_RELOC_WRT = 2, /* relative to symbol */
46 XDF_RELOC_RIP = 4, /* RIP-relative */
47 XDF_RELOC_SEG = 8 /* segment containing symbol */
48 } type; /* type of relocation */
49 enum {
50 XDF_RELOC_8 = 1,
51 XDF_RELOC_16 = 2,
52 XDF_RELOC_32 = 4,
53 XDF_RELOC_64 = 8
54 } size; /* size of relocation */
55 unsigned int shift; /* relocation shift (0,4,8,16,24,32) */
56 } xdf_reloc;
57
58 typedef struct xdf_section_data {
59 /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */
60 yasm_intnum *addr; /* starting memory address */
61 yasm_intnum *vaddr; /* starting virtual address */
62 long scnum; /* section number (0=first section) */
63 enum {
64 XDF_SECT_ABSOLUTE = 0x01,
65 XDF_SECT_FLAT = 0x02,
66 XDF_SECT_BSS = 0x04,
67 XDF_SECT_EQU = 0x08,
68 XDF_SECT_USE_16 = 0x10,
69 XDF_SECT_USE_32 = 0x20,
70 XDF_SECT_USE_64 = 0x40
71 } flags; /* section flags */
72 unsigned long scnptr; /* file ptr to raw data */
73 unsigned long size; /* size of raw data (section data) in bytes */
74 unsigned long relptr; /* file ptr to relocation */
75 unsigned long nreloc; /* number of relocation entries >64k -> error */
76 } xdf_section_data;
77
78 typedef struct xdf_symrec_data {
79 unsigned long index; /* assigned XDF symbol table index */
80 } xdf_symrec_data;
81
82 typedef struct yasm_objfmt_xdf {
83 yasm_objfmt_base objfmt; /* base structure */
84
85 long parse_scnum; /* sect numbering in parser */
86 } yasm_objfmt_xdf;
87
88 typedef struct xdf_objfmt_output_info {
89 yasm_object *object;
90 yasm_objfmt_xdf *objfmt_xdf;
91 yasm_errwarns *errwarns;
92 /*@dependent@*/ FILE *f;
93 /*@only@*/ unsigned char *buf;
94 yasm_section *sect;
95 /*@dependent@*/ xdf_section_data *xsd;
96
97 unsigned long indx; /* current symbol index */
98 int all_syms; /* outputting all symbols? */
99 unsigned long strtab_offset; /* current string table offset */
100 } xdf_objfmt_output_info;
101
102 static void xdf_section_data_destroy(/*@only@*/ void *d);
103 static void xdf_section_data_print(void *data, FILE *f, int indent_level);
104
105 static const yasm_assoc_data_callback xdf_section_data_cb = {
106 xdf_section_data_destroy,
107 xdf_section_data_print
108 };
109
110 static void xdf_symrec_data_destroy(/*@only@*/ void *d);
111 static void xdf_symrec_data_print(void *data, FILE *f, int indent_level);
112
113 static const yasm_assoc_data_callback xdf_symrec_data_cb = {
114 xdf_symrec_data_destroy,
115 xdf_symrec_data_print
116 };
117
118 yasm_objfmt_module yasm_xdf_LTX_objfmt;
119
120
121 static yasm_objfmt *
xdf_objfmt_create(yasm_object * object)122 xdf_objfmt_create(yasm_object *object)
123 {
124 yasm_objfmt_xdf *objfmt_xdf = yasm_xmalloc(sizeof(yasm_objfmt_xdf));
125
126 /* Only support x86 arch */
127 if (yasm__strcasecmp(yasm_arch_keyword(object->arch), "x86") != 0) {
128 yasm_xfree(objfmt_xdf);
129 return NULL;
130 }
131
132 /* Support x86 and amd64 machines of x86 arch */
133 if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), "x86") &&
134 yasm__strcasecmp(yasm_arch_get_machine(object->arch), "amd64")) {
135 yasm_xfree(objfmt_xdf);
136 return NULL;
137 }
138
139 objfmt_xdf->parse_scnum = 0; /* section numbering starts at 0 */
140
141 objfmt_xdf->objfmt.module = &yasm_xdf_LTX_objfmt;
142
143 return (yasm_objfmt *)objfmt_xdf;
144 }
145
146 static int
xdf_objfmt_output_value(yasm_value * value,unsigned char * buf,unsigned int destsize,unsigned long offset,yasm_bytecode * bc,int warn,void * d)147 xdf_objfmt_output_value(yasm_value *value, unsigned char *buf,
148 unsigned int destsize, unsigned long offset,
149 yasm_bytecode *bc, int warn, /*@null@*/ void *d)
150 {
151 /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d;
152 /*@dependent@*/ /*@null@*/ yasm_intnum *intn;
153 unsigned long intn_minus;
154 int retval;
155 unsigned int valsize = value->size;
156
157 assert(info != NULL);
158
159 if (value->abs)
160 value->abs = yasm_expr_simplify(value->abs, 1);
161
162 /* Try to output constant and PC-relative section-local first.
163 * Note this does NOT output any value with a SEG, WRT, external,
164 * cross-section, or non-PC-relative reference (those are handled below).
165 */
166 switch (yasm_value_output_basic(value, buf, destsize, bc, warn,
167 info->object->arch)) {
168 case -1:
169 return 1;
170 case 0:
171 break;
172 default:
173 return 0;
174 }
175
176 if (value->section_rel) {
177 yasm_error_set(YASM_ERROR_TOO_COMPLEX,
178 N_("xdf: relocation too complex"));
179 return 1;
180 }
181
182 intn_minus = 0;
183 if (value->rel) {
184 xdf_reloc *reloc;
185
186 reloc = yasm_xmalloc(sizeof(xdf_reloc));
187 reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset);
188 reloc->reloc.sym = value->rel;
189 reloc->base = NULL;
190 reloc->size = valsize/8;
191 reloc->shift = value->rshift;
192
193 if (value->seg_of)
194 reloc->type = XDF_RELOC_SEG;
195 else if (value->wrt) {
196 reloc->base = value->wrt;
197 reloc->type = XDF_RELOC_WRT;
198 } else if (value->curpos_rel) {
199 reloc->type = XDF_RELOC_RIP;
200 /* Adjust to start of section, so subtract out the bytecode
201 * offset.
202 */
203 intn_minus = bc->offset;
204 } else
205 reloc->type = XDF_RELOC_REL;
206 info->xsd->nreloc++;
207 yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree);
208 }
209
210 if (intn_minus > 0) {
211 intn = yasm_intnum_create_uint(intn_minus);
212 yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL);
213 } else
214 intn = yasm_intnum_create_uint(0);
215
216 if (value->abs) {
217 yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0);
218 if (!intn2) {
219 yasm_error_set(YASM_ERROR_TOO_COMPLEX,
220 N_("xdf: relocation too complex"));
221 yasm_intnum_destroy(intn);
222 return 1;
223 }
224 yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2);
225 }
226
227 retval = yasm_arch_intnum_tobytes(info->object->arch, intn, buf, destsize,
228 valsize, 0, bc, warn);
229 yasm_intnum_destroy(intn);
230 return retval;
231 }
232
233 static int
xdf_objfmt_output_bytecode(yasm_bytecode * bc,void * d)234 xdf_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d)
235 {
236 /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d;
237 /*@null@*/ /*@only@*/ unsigned char *bigbuf;
238 unsigned long size = REGULAR_OUTBUF_SIZE;
239 int gap;
240
241 assert(info != NULL);
242
243 bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info,
244 xdf_objfmt_output_value, NULL);
245
246 /* Don't bother doing anything else if size ended up being 0. */
247 if (size == 0) {
248 if (bigbuf)
249 yasm_xfree(bigbuf);
250 return 0;
251 }
252
253 info->xsd->size += size;
254
255 /* Warn that gaps are converted to 0 and write out the 0's. */
256 if (gap) {
257 unsigned long left;
258 yasm_warn_set(YASM_WARN_UNINIT_CONTENTS,
259 N_("uninitialized space: zeroing"));
260 /* Write out in chunks */
261 memset(info->buf, 0, REGULAR_OUTBUF_SIZE);
262 left = size;
263 while (left > REGULAR_OUTBUF_SIZE) {
264 fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f);
265 left -= REGULAR_OUTBUF_SIZE;
266 }
267 fwrite(info->buf, left, 1, info->f);
268 } else {
269 /* Output buf (or bigbuf if non-NULL) to file */
270 fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f);
271 }
272
273 /* If bigbuf was allocated, free it */
274 if (bigbuf)
275 yasm_xfree(bigbuf);
276
277 return 0;
278 }
279
280 static int
xdf_objfmt_output_section(yasm_section * sect,void * d)281 xdf_objfmt_output_section(yasm_section *sect, /*@null@*/ void *d)
282 {
283 /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d;
284 /*@dependent@*/ /*@null@*/ xdf_section_data *xsd;
285 long pos;
286 xdf_reloc *reloc;
287
288 assert(info != NULL);
289 xsd = yasm_section_get_data(sect, &xdf_section_data_cb);
290 assert(xsd != NULL);
291
292 if (xsd->flags & XDF_SECT_BSS) {
293 /* Don't output BSS sections.
294 * TODO: Check for non-reserve bytecodes?
295 */
296 pos = 0; /* position = 0 because it's not in the file */
297 xsd->size = yasm_bc_next_offset(yasm_section_bcs_last(sect));
298 } else {
299 pos = ftell(info->f);
300 if (pos == -1) {
301 yasm__fatal(N_("could not get file position on output file"));
302 /*@notreached@*/
303 return 1;
304 }
305
306 info->sect = sect;
307 info->xsd = xsd;
308 yasm_section_bcs_traverse(sect, info->errwarns, info,
309 xdf_objfmt_output_bytecode);
310
311 /* Sanity check final section size */
312 if (xsd->size != yasm_bc_next_offset(yasm_section_bcs_last(sect)))
313 yasm_internal_error(
314 N_("xdf: section computed size did not match actual size"));
315 }
316
317 /* Empty? Go on to next section */
318 if (xsd->size == 0)
319 return 0;
320
321 xsd->scnptr = (unsigned long)pos;
322
323 /* No relocations to output? Go on to next section */
324 if (xsd->nreloc == 0)
325 return 0;
326
327 pos = ftell(info->f);
328 if (pos == -1) {
329 yasm__fatal(N_("could not get file position on output file"));
330 /*@notreached@*/
331 return 1;
332 }
333 xsd->relptr = (unsigned long)pos;
334
335 reloc = (xdf_reloc *)yasm_section_relocs_first(sect);
336 while (reloc) {
337 unsigned char *localbuf = info->buf;
338 /*@null@*/ xdf_symrec_data *xsymd;
339
340 xsymd = yasm_symrec_get_data(reloc->reloc.sym, &xdf_symrec_data_cb);
341 if (!xsymd)
342 yasm_internal_error(
343 N_("xdf: no symbol data for relocated symbol"));
344
345 yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0);
346 localbuf += 4; /* address of relocation */
347 YASM_WRITE_32_L(localbuf, xsymd->index); /* relocated symbol */
348 if (reloc->base) {
349 xsymd = yasm_symrec_get_data(reloc->base, &xdf_symrec_data_cb);
350 if (!xsymd)
351 yasm_internal_error(
352 N_("xdf: no symbol data for relocated base symbol"));
353 YASM_WRITE_32_L(localbuf, xsymd->index); /* base symbol */
354 } else {
355 if (reloc->type == XDF_RELOC_WRT)
356 yasm_internal_error(
357 N_("xdf: no base symbol for WRT relocation"));
358 YASM_WRITE_32_L(localbuf, 0); /* no base symbol */
359 }
360 YASM_WRITE_8(localbuf, reloc->type); /* type of relocation */
361 YASM_WRITE_8(localbuf, reloc->size); /* size of relocation */
362 YASM_WRITE_8(localbuf, reloc->shift); /* relocation shift */
363 YASM_WRITE_8(localbuf, 0); /* flags */
364 fwrite(info->buf, 16, 1, info->f);
365
366 reloc = (xdf_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc);
367 }
368
369 return 0;
370 }
371
372 static int
xdf_objfmt_output_secthead(yasm_section * sect,void * d)373 xdf_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d)
374 {
375 /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d;
376 /*@dependent@*/ /*@null@*/ xdf_section_data *xsd;
377 /*@null@*/ xdf_symrec_data *xsymd;
378 unsigned char *localbuf;
379
380 assert(info != NULL);
381 xsd = yasm_section_get_data(sect, &xdf_section_data_cb);
382 assert(xsd != NULL);
383
384 localbuf = info->buf;
385 xsymd = yasm_symrec_get_data(xsd->sym, &xdf_symrec_data_cb);
386 assert(xsymd != NULL);
387
388 YASM_WRITE_32_L(localbuf, xsymd->index); /* section name symbol */
389 if (xsd->addr) {
390 yasm_intnum_get_sized(xsd->addr, localbuf, 8, 64, 0, 0, 0);
391 localbuf += 8; /* physical address */
392 } else {
393 YASM_WRITE_32_L(localbuf, 0);
394 YASM_WRITE_32_L(localbuf, 0);
395 }
396 if (xsd->vaddr) {
397 yasm_intnum_get_sized(xsd->vaddr, localbuf, 8, 64, 0, 0, 0);
398 localbuf += 8; /* virtual address */
399 } else if (xsd->addr) {
400 yasm_intnum_get_sized(xsd->addr, localbuf, 8, 64, 0, 0, 0);
401 localbuf += 8; /* VA=PA */
402 } else {
403 YASM_WRITE_32_L(localbuf, 0);
404 YASM_WRITE_32_L(localbuf, 0);
405 }
406 YASM_WRITE_16_L(localbuf, yasm_section_get_align(sect)); /* alignment */
407 YASM_WRITE_16_L(localbuf, xsd->flags); /* flags */
408 YASM_WRITE_32_L(localbuf, xsd->scnptr); /* file ptr to data */
409 YASM_WRITE_32_L(localbuf, xsd->size); /* section size */
410 YASM_WRITE_32_L(localbuf, xsd->relptr); /* file ptr to relocs */
411 YASM_WRITE_32_L(localbuf, xsd->nreloc); /* num of relocation entries */
412 fwrite(info->buf, 40, 1, info->f);
413
414 return 0;
415 }
416
417 static int
xdf_objfmt_count_sym(yasm_symrec * sym,void * d)418 xdf_objfmt_count_sym(yasm_symrec *sym, /*@null@*/ void *d)
419 {
420 /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d;
421 yasm_sym_vis vis = yasm_symrec_get_visibility(sym);
422 assert(info != NULL);
423 if (vis & YASM_SYM_COMMON) {
424 yasm_error_set(YASM_ERROR_GENERAL,
425 N_("XDF object format does not support common variables"));
426 yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym));
427 return 0;
428 }
429 if (info->all_syms ||
430 (vis != YASM_SYM_LOCAL && !(vis & YASM_SYM_DLOCAL))) {
431 /* Save index in symrec data */
432 xdf_symrec_data *sym_data = yasm_xmalloc(sizeof(xdf_symrec_data));
433 sym_data->index = info->indx;
434 yasm_symrec_add_data(sym, &xdf_symrec_data_cb, sym_data);
435
436 info->indx++;
437 }
438 return 0;
439 }
440
441 static int
xdf_objfmt_output_sym(yasm_symrec * sym,void * d)442 xdf_objfmt_output_sym(yasm_symrec *sym, /*@null@*/ void *d)
443 {
444 /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d;
445 yasm_sym_vis vis = yasm_symrec_get_visibility(sym);
446
447 assert(info != NULL);
448
449 if (info->all_syms || vis != YASM_SYM_LOCAL) {
450 /*@only@*/ char *name = yasm_symrec_get_global_name(sym, info->object);
451 const yasm_expr *equ_val;
452 const yasm_intnum *intn;
453 size_t len = strlen(name);
454 unsigned long value = 0;
455 long scnum = -3; /* -3 = debugging symbol */
456 /*@dependent@*/ /*@null@*/ yasm_section *sect;
457 /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc;
458 unsigned long flags = 0;
459 unsigned char *localbuf;
460
461 if (vis & YASM_SYM_GLOBAL)
462 flags = XDF_SYM_GLOBAL;
463
464 /* Look at symrec for value/scnum/etc. */
465 if (yasm_symrec_get_label(sym, &precbc)) {
466 if (precbc)
467 sect = yasm_bc_get_section(precbc);
468 else
469 sect = NULL;
470 /* it's a label: get value and offset.
471 * If there is not a section, leave as debugging symbol.
472 */
473 if (sect) {
474 /*@dependent@*/ /*@null@*/ xdf_section_data *csectd;
475 csectd = yasm_section_get_data(sect, &xdf_section_data_cb);
476 if (csectd)
477 scnum = csectd->scnum;
478 else
479 yasm_internal_error(N_("didn't understand section"));
480 if (precbc)
481 value += yasm_bc_next_offset(precbc);
482 }
483 } else if ((equ_val = yasm_symrec_get_equ(sym))) {
484 yasm_expr *equ_val_copy = yasm_expr_copy(equ_val);
485 intn = yasm_expr_get_intnum(&equ_val_copy, 1);
486 if (!intn) {
487 if (vis & YASM_SYM_GLOBAL) {
488 yasm_error_set(YASM_ERROR_NOT_CONSTANT,
489 N_("global EQU value not an integer expression"));
490 yasm_errwarn_propagate(info->errwarns, equ_val->line);
491 }
492 } else
493 value = yasm_intnum_get_uint(intn);
494 yasm_expr_destroy(equ_val_copy);
495
496 flags |= XDF_SYM_EQU;
497 scnum = -2; /* -2 = absolute symbol */
498 } else {
499 if (vis & YASM_SYM_EXTERN) {
500 flags = XDF_SYM_EXTERN;
501 scnum = -1;
502 }
503 }
504
505 localbuf = info->buf;
506 YASM_WRITE_32_L(localbuf, scnum); /* section number */
507 YASM_WRITE_32_L(localbuf, value); /* value */
508 YASM_WRITE_32_L(localbuf, info->strtab_offset);
509 info->strtab_offset += (unsigned long)(len+1);
510 YASM_WRITE_32_L(localbuf, flags); /* flags */
511 fwrite(info->buf, 16, 1, info->f);
512 yasm_xfree(name);
513 }
514 return 0;
515 }
516
517 static int
xdf_objfmt_output_str(yasm_symrec * sym,void * d)518 xdf_objfmt_output_str(yasm_symrec *sym, /*@null@*/ void *d)
519 {
520 /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d;
521 yasm_sym_vis vis = yasm_symrec_get_visibility(sym);
522
523 assert(info != NULL);
524
525 if (info->all_syms || vis != YASM_SYM_LOCAL) {
526 /*@only@*/ char *name = yasm_symrec_get_global_name(sym, info->object);
527 size_t len = strlen(name);
528 fwrite(name, len+1, 1, info->f);
529 yasm_xfree(name);
530 }
531 return 0;
532 }
533
534 static void
xdf_objfmt_output(yasm_object * object,FILE * f,int all_syms,yasm_errwarns * errwarns)535 xdf_objfmt_output(yasm_object *object, FILE *f, int all_syms,
536 yasm_errwarns *errwarns)
537 {
538 yasm_objfmt_xdf *objfmt_xdf = (yasm_objfmt_xdf *)object->objfmt;
539 xdf_objfmt_output_info info;
540 unsigned char *localbuf;
541 unsigned long symtab_count = 0;
542
543 info.object = object;
544 info.objfmt_xdf = objfmt_xdf;
545 info.errwarns = errwarns;
546 info.f = f;
547 info.buf = yasm_xmalloc(REGULAR_OUTBUF_SIZE);
548
549 /* Allocate space for headers by seeking forward */
550 if (fseek(f, (long)(16+40*(objfmt_xdf->parse_scnum)), SEEK_SET) < 0) {
551 yasm__fatal(N_("could not seek on output file"));
552 /*@notreached@*/
553 return;
554 }
555
556 /* Get number of symbols */
557 info.indx = 0;
558 info.all_syms = 1; /* force all syms into symbol table */
559 yasm_symtab_traverse(object->symtab, &info, xdf_objfmt_count_sym);
560 symtab_count = info.indx;
561
562 /* Get file offset of start of string table */
563 info.strtab_offset = 16+40*(objfmt_xdf->parse_scnum)+16*symtab_count;
564
565 /* Output symbol table */
566 yasm_symtab_traverse(object->symtab, &info, xdf_objfmt_output_sym);
567
568 /* Output string table */
569 yasm_symtab_traverse(object->symtab, &info, xdf_objfmt_output_str);
570
571 /* Section data/relocs */
572 if (yasm_object_sections_traverse(object, &info,
573 xdf_objfmt_output_section))
574 return;
575
576 /* Write headers */
577 if (fseek(f, 0, SEEK_SET) < 0) {
578 yasm__fatal(N_("could not seek on output file"));
579 /*@notreached@*/
580 return;
581 }
582
583 localbuf = info.buf;
584 YASM_WRITE_32_L(localbuf, XDF_MAGIC); /* magic number */
585 YASM_WRITE_32_L(localbuf, objfmt_xdf->parse_scnum); /* number of sects */
586 YASM_WRITE_32_L(localbuf, symtab_count); /* number of symtabs */
587 /* size of sect headers + symbol table + strings */
588 YASM_WRITE_32_L(localbuf, info.strtab_offset-16);
589 fwrite(info.buf, 16, 1, f);
590
591 yasm_object_sections_traverse(object, &info, xdf_objfmt_output_secthead);
592
593 yasm_xfree(info.buf);
594 }
595
596 static void
xdf_objfmt_destroy(yasm_objfmt * objfmt)597 xdf_objfmt_destroy(yasm_objfmt *objfmt)
598 {
599 yasm_xfree(objfmt);
600 }
601
602 static void
xdf_objfmt_init_new_section(yasm_section * sect,unsigned long line)603 xdf_objfmt_init_new_section(yasm_section *sect, unsigned long line)
604 {
605 yasm_object *object = yasm_section_get_object(sect);
606 const char *sectname = yasm_section_get_name(sect);
607 yasm_objfmt_xdf *objfmt_xdf = (yasm_objfmt_xdf *)object->objfmt;
608 xdf_section_data *data;
609 yasm_symrec *sym;
610
611 data = yasm_xmalloc(sizeof(xdf_section_data));
612 data->scnum = objfmt_xdf->parse_scnum++;
613 data->flags = 0;
614 data->addr = NULL;
615 data->vaddr = NULL;
616 data->scnptr = 0;
617 data->size = 0;
618 data->relptr = 0;
619 data->nreloc = 0;
620 yasm_section_add_data(sect, &xdf_section_data_cb, data);
621
622 sym = yasm_symtab_define_label(object->symtab, sectname,
623 yasm_section_bcs_first(sect), 1, line);
624 data->sym = sym;
625 }
626
627 static yasm_section *
xdf_objfmt_add_default_section(yasm_object * object)628 xdf_objfmt_add_default_section(yasm_object *object)
629 {
630 yasm_section *retval;
631 int isnew;
632
633 retval = yasm_object_get_general(object, ".text", 0, 1, 0, &isnew, 0);
634 if (isnew)
635 yasm_section_set_default(retval, 1);
636 return retval;
637 }
638
639 static int
xdf_helper_use(void * obj,yasm_valparam * vp,unsigned long line,void * d,uintptr_t bits)640 xdf_helper_use(void *obj, yasm_valparam *vp, unsigned long line, void *d,
641 uintptr_t bits)
642 {
643 yasm_object *object = (yasm_object *)obj;
644 unsigned long *flags = (unsigned long *)d;
645 *flags &= ~(XDF_SECT_USE_16|XDF_SECT_USE_32|XDF_SECT_USE_64);
646 switch (bits) {
647 case 16: *flags |= XDF_SECT_USE_16; break;
648 case 32: *flags |= XDF_SECT_USE_32; break;
649 case 64: *flags |= XDF_SECT_USE_64; break;
650 };
651 yasm_arch_set_var(object->arch, "mode_bits", bits);
652 return 0;
653 }
654
655 static /*@observer@*/ /*@null@*/ yasm_section *
xdf_objfmt_section_switch(yasm_object * object,yasm_valparamhead * valparams,yasm_valparamhead * objext_valparams,unsigned long line)656 xdf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams,
657 /*@unused@*/ /*@null@*/
658 yasm_valparamhead *objext_valparams,
659 unsigned long line)
660 {
661 yasm_valparam *vp;
662 yasm_section *retval;
663 int isnew;
664 int flags_override = 0;
665 const char *sectname;
666 int resonly = 0;
667 xdf_section_data *xsd;
668 unsigned long align = 0;
669
670 struct xdf_section_switch_data {
671 /*@only@*/ /*@null@*/ yasm_intnum *absaddr;
672 /*@only@*/ /*@null@*/ yasm_intnum *vaddr;
673 /*@only@*/ /*@null@*/ yasm_intnum *align_intn;
674 unsigned long flags;
675 } data;
676
677 static const yasm_dir_help help[] = {
678 { "use16", 0, xdf_helper_use,
679 offsetof(struct xdf_section_switch_data, flags), 16 },
680 { "use32", 0, xdf_helper_use,
681 offsetof(struct xdf_section_switch_data, flags), 32 },
682 { "use64", 0, xdf_helper_use,
683 offsetof(struct xdf_section_switch_data, flags), 64 },
684 { "bss", 0, yasm_dir_helper_flag_or,
685 offsetof(struct xdf_section_switch_data, flags), XDF_SECT_BSS },
686 { "flat", 0, yasm_dir_helper_flag_or,
687 offsetof(struct xdf_section_switch_data, flags), XDF_SECT_FLAT },
688 { "absolute", 1, yasm_dir_helper_intn,
689 offsetof(struct xdf_section_switch_data, absaddr), 0 },
690 { "virtual", 1, yasm_dir_helper_intn,
691 offsetof(struct xdf_section_switch_data, vaddr), 0 },
692 { "align", 1, yasm_dir_helper_intn,
693 offsetof(struct xdf_section_switch_data, align_intn), 0 }
694 };
695
696 data.absaddr = NULL;
697 data.vaddr = NULL;
698 data.align_intn = NULL;
699 data.flags = 0;
700
701 vp = yasm_vps_first(valparams);
702 sectname = yasm_vp_string(vp);
703 if (!sectname)
704 return NULL;
705 vp = yasm_vps_next(vp);
706
707 flags_override = yasm_dir_helper(object, vp, line, help, NELEMS(help),
708 &data, yasm_dir_helper_valparam_warn);
709 if (flags_override < 0)
710 return NULL; /* error occurred */
711
712 if (data.absaddr)
713 data.flags |= XDF_SECT_ABSOLUTE;
714 if (data.align_intn) {
715 align = yasm_intnum_get_uint(data.align_intn);
716 yasm_intnum_destroy(data.align_intn);
717
718 /* Alignments must be a power of two. */
719 if (!is_exp2(align)) {
720 yasm_error_set(YASM_ERROR_VALUE,
721 N_("argument to `%s' is not a power of two"),
722 "align");
723 if (data.vaddr)
724 yasm_intnum_destroy(data.vaddr);
725 if (data.absaddr)
726 yasm_intnum_destroy(data.absaddr);
727 return NULL;
728 }
729
730 /* Check to see if alignment is supported size */
731 if (align > 4096) {
732 yasm_error_set(YASM_ERROR_VALUE,
733 N_("XDF does not support alignments > 4096"));
734 if (data.vaddr)
735 yasm_intnum_destroy(data.vaddr);
736 if (data.absaddr)
737 yasm_intnum_destroy(data.absaddr);
738 return NULL;
739 }
740 }
741
742 retval = yasm_object_get_general(object, sectname, align, 1, resonly,
743 &isnew, line);
744
745 xsd = yasm_section_get_data(retval, &xdf_section_data_cb);
746
747 if (isnew || yasm_section_is_default(retval)) {
748 yasm_section_set_default(retval, 0);
749 xsd->flags = data.flags;
750 if (data.absaddr) {
751 if (xsd->addr)
752 yasm_intnum_destroy(xsd->addr);
753 xsd->addr = data.absaddr;
754 }
755 if (data.vaddr) {
756 if (xsd->vaddr)
757 yasm_intnum_destroy(xsd->vaddr);
758 xsd->vaddr = data.vaddr;
759 }
760 yasm_section_set_align(retval, align, line);
761 } else if (flags_override)
762 yasm_warn_set(YASM_WARN_GENERAL,
763 N_("section flags ignored on section redeclaration"));
764 return retval;
765 }
766
767 static /*@observer@*/ /*@null@*/ yasm_symrec *
xdf_objfmt_get_special_sym(yasm_object * object,const char * name,const char * parser)768 xdf_objfmt_get_special_sym(yasm_object *object, const char *name,
769 const char *parser)
770 {
771 return NULL;
772 }
773
774 static void
xdf_section_data_destroy(void * data)775 xdf_section_data_destroy(void *data)
776 {
777 xdf_section_data *xsd = (xdf_section_data *)data;
778 if (xsd->addr)
779 yasm_intnum_destroy(xsd->addr);
780 if (xsd->vaddr)
781 yasm_intnum_destroy(xsd->vaddr);
782 yasm_xfree(data);
783 }
784
785 static void
xdf_section_data_print(void * data,FILE * f,int indent_level)786 xdf_section_data_print(void *data, FILE *f, int indent_level)
787 {
788 xdf_section_data *xsd = (xdf_section_data *)data;
789
790 fprintf(f, "%*ssym=\n", indent_level, "");
791 yasm_symrec_print(xsd->sym, f, indent_level+1);
792 fprintf(f, "%*sscnum=%ld\n", indent_level, "", xsd->scnum);
793 fprintf(f, "%*sflags=0x%x\n", indent_level, "", xsd->flags);
794 fprintf(f, "%*saddr=", indent_level, "");
795 yasm_intnum_print(xsd->addr, f);
796 fprintf(f, "%*svaddr=", indent_level, "");
797 yasm_intnum_print(xsd->vaddr, f);
798 fprintf(f, "%*sscnptr=0x%lx\n", indent_level, "", xsd->scnptr);
799 fprintf(f, "%*ssize=%ld\n", indent_level, "", xsd->size);
800 fprintf(f, "%*srelptr=0x%lx\n", indent_level, "", xsd->relptr);
801 fprintf(f, "%*snreloc=%ld\n", indent_level, "", xsd->nreloc);
802 }
803
804 static void
xdf_symrec_data_destroy(void * data)805 xdf_symrec_data_destroy(void *data)
806 {
807 yasm_xfree(data);
808 }
809
810 static void
xdf_symrec_data_print(void * data,FILE * f,int indent_level)811 xdf_symrec_data_print(void *data, FILE *f, int indent_level)
812 {
813 xdf_symrec_data *xsd = (xdf_symrec_data *)data;
814
815 fprintf(f, "%*ssymtab index=%lu\n", indent_level, "", xsd->index);
816 }
817
818 /* Define valid debug formats to use with this object format */
819 static const char *xdf_objfmt_dbgfmt_keywords[] = {
820 "null",
821 NULL
822 };
823
824 /* Define objfmt structure -- see objfmt.h for details */
825 yasm_objfmt_module yasm_xdf_LTX_objfmt = {
826 "Extended Dynamic Object",
827 "xdf",
828 "xdf",
829 32,
830 0,
831 xdf_objfmt_dbgfmt_keywords,
832 "null",
833 NULL, /* no directives */
834 NULL, /* no standard macros */
835 xdf_objfmt_create,
836 xdf_objfmt_output,
837 xdf_objfmt_destroy,
838 xdf_objfmt_add_default_section,
839 xdf_objfmt_init_new_section,
840 xdf_objfmt_section_switch,
841 xdf_objfmt_get_special_sym
842 };
843