xdf-objfmt.c 28 KB

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