rdf-objfmt.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * Relocatable Dynamic Object File Format (RDOFF) version 2 format
  3. *
  4. * Copyright (C) 2006-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 RDF_MAGIC "RDOFF2"
  31. /* Maximum size of an import/export label (including trailing zero) */
  32. #define EXIM_LABEL_MAX 64
  33. /* Maximum size of library or module name (including trailing zero) */
  34. #define MODLIB_NAME_MAX 128
  35. /* Maximum number of segments that we can handle in one file */
  36. #define RDF_MAXSEGS 64
  37. /* Record types that may present the RDOFF header */
  38. #define RDFREC_GENERIC 0
  39. #define RDFREC_RELOC 1
  40. #define RDFREC_IMPORT 2
  41. #define RDFREC_GLOBAL 3
  42. #define RDFREC_DLL 4
  43. #define RDFREC_BSS 5
  44. #define RDFREC_SEGRELOC 6
  45. #define RDFREC_FARIMPORT 7
  46. #define RDFREC_MODNAME 8
  47. #define RDFREC_COMMON 10
  48. /* Flags for ExportRec/ImportRec */
  49. #define SYM_DATA 1
  50. #define SYM_FUNCTION 2
  51. /* Flags for ExportRec */
  52. #define SYM_GLOBAL 4
  53. /* Flags for ImportRec */
  54. #define SYM_IMPORT 8
  55. #define SYM_FAR 16
  56. typedef struct rdf_reloc {
  57. yasm_reloc reloc;
  58. enum {
  59. RDF_RELOC_NORM, /* normal */
  60. RDF_RELOC_REL, /* relative to current position */
  61. RDF_RELOC_SEG /* segment containing symbol */
  62. } type; /* type of relocation */
  63. unsigned int size;
  64. unsigned int refseg;
  65. } rdf_reloc;
  66. typedef struct rdf_section_data {
  67. /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */
  68. long scnum; /* section number (0=first section) */
  69. enum {
  70. RDF_SECT_BSS = 0,
  71. RDF_SECT_CODE = 1,
  72. RDF_SECT_DATA = 2,
  73. RDF_SECT_COMMENT = 3,
  74. RDF_SECT_LCOMMENT = 4,
  75. RDF_SECT_PCOMMENT = 5,
  76. RDF_SECT_SYMDEBUG = 6,
  77. RDF_SECT_LINEDEBUG = 7
  78. } type; /* section type */
  79. unsigned int reserved; /* reserved data */
  80. unsigned long size; /* size of raw data (section data) in bytes */
  81. unsigned char *raw_data; /* raw section data, only used during output */
  82. } rdf_section_data;
  83. typedef struct rdf_symrec_data {
  84. unsigned int segment; /* assigned RDF "segment" index */
  85. } rdf_symrec_data;
  86. typedef STAILQ_HEAD(xdf_str_head, xdf_str) xdf_str_head;
  87. typedef struct xdf_str {
  88. STAILQ_ENTRY(xdf_str) link;
  89. /*@owned@*/ char *str;
  90. } xdf_str;
  91. typedef struct yasm_objfmt_rdf {
  92. yasm_objfmt_base objfmt; /* base structure */
  93. long parse_scnum; /* sect numbering in parser */
  94. /*@owned@*/ xdf_str_head module_names;
  95. /*@owned@*/ xdf_str_head library_names;
  96. } yasm_objfmt_rdf;
  97. typedef struct rdf_objfmt_output_info {
  98. yasm_object *object;
  99. yasm_objfmt_rdf *objfmt_rdf;
  100. yasm_errwarns *errwarns;
  101. /*@dependent@*/ FILE *f;
  102. /*@only@*/ unsigned char *buf;
  103. yasm_section *sect;
  104. /*@dependent@*/ rdf_section_data *rsd;
  105. unsigned long indx; /* symbol "segment" (extern/common only) */
  106. unsigned long bss_size; /* total BSS size */
  107. } rdf_objfmt_output_info;
  108. static void rdf_section_data_destroy(/*@only@*/ void *d);
  109. static void rdf_section_data_print(void *data, FILE *f, int indent_level);
  110. static const yasm_assoc_data_callback rdf_section_data_cb = {
  111. rdf_section_data_destroy,
  112. rdf_section_data_print
  113. };
  114. static void rdf_symrec_data_destroy(/*@only@*/ void *d);
  115. static void rdf_symrec_data_print(void *data, FILE *f, int indent_level);
  116. static const yasm_assoc_data_callback rdf_symrec_data_cb = {
  117. rdf_symrec_data_destroy,
  118. rdf_symrec_data_print
  119. };
  120. yasm_objfmt_module yasm_rdf_LTX_objfmt;
  121. static /*@dependent@*/ rdf_symrec_data *
  122. rdf_objfmt_sym_set_data(yasm_symrec *sym, unsigned int segment)
  123. {
  124. rdf_symrec_data *rsymd = yasm_xmalloc(sizeof(rdf_symrec_data));
  125. rsymd->segment = segment;
  126. yasm_symrec_add_data(sym, &rdf_symrec_data_cb, rsymd);
  127. return rsymd;
  128. }
  129. static yasm_objfmt *
  130. rdf_objfmt_create(yasm_object *object)
  131. {
  132. yasm_objfmt_rdf *objfmt_rdf = yasm_xmalloc(sizeof(yasm_objfmt_rdf));
  133. /* We theoretically support all arches, so don't check.
  134. * Really we only support byte-addressable ones.
  135. */
  136. objfmt_rdf->parse_scnum = 0; /* section numbering starts at 0 */
  137. STAILQ_INIT(&objfmt_rdf->module_names);
  138. STAILQ_INIT(&objfmt_rdf->library_names);
  139. objfmt_rdf->objfmt.module = &yasm_rdf_LTX_objfmt;
  140. return (yasm_objfmt *)objfmt_rdf;
  141. }
  142. static int
  143. rdf_objfmt_output_value(yasm_value *value, unsigned char *buf,
  144. unsigned int destsize, unsigned long offset,
  145. yasm_bytecode *bc, int warn, /*@null@*/ void *d)
  146. {
  147. /*@null@*/ rdf_objfmt_output_info *info = (rdf_objfmt_output_info *)d;
  148. /*@dependent@*/ /*@null@*/ yasm_intnum *intn;
  149. unsigned long intn_minus;
  150. unsigned long intn_plus;
  151. int retval;
  152. unsigned int valsize = value->size;
  153. assert(info != NULL);
  154. if (value->abs)
  155. value->abs = yasm_expr_simplify(value->abs, 1);
  156. /* Try to output constant and PC-relative section-local first.
  157. * Note this does NOT output any value with a SEG, WRT, external,
  158. * cross-section, or non-PC-relative reference (those are handled below).
  159. */
  160. switch (yasm_value_output_basic(value, buf, destsize, bc, warn,
  161. info->object->arch)) {
  162. case -1:
  163. return 1;
  164. case 0:
  165. break;
  166. default:
  167. return 0;
  168. }
  169. if (value->section_rel) {
  170. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  171. N_("rdf: relocation too complex"));
  172. return 1;
  173. }
  174. if (value->rel && value->wrt) {
  175. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  176. N_("rdf: WRT not supported"));
  177. return 1;
  178. }
  179. intn_minus = 0;
  180. intn_plus = 0;
  181. if (value->rel) {
  182. rdf_reloc *reloc;
  183. /*@null@*/ rdf_symrec_data *rsymd;
  184. /*@dependent@*/ yasm_bytecode *precbc;
  185. reloc = yasm_xmalloc(sizeof(rdf_reloc));
  186. reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset);
  187. reloc->reloc.sym = value->rel;
  188. reloc->size = valsize/8;
  189. if (value->seg_of)
  190. reloc->type = RDF_RELOC_SEG;
  191. else if (value->curpos_rel) {
  192. reloc->type = RDF_RELOC_REL;
  193. /* Adjust to start of section, so subtract out the bytecode
  194. * offset.
  195. */
  196. intn_minus = bc->offset;
  197. } else
  198. reloc->type = RDF_RELOC_NORM;
  199. if (yasm_symrec_get_label(value->rel, &precbc)) {
  200. /* local, set the value to be the offset, and the refseg to the
  201. * segment number.
  202. */
  203. /*@dependent@*/ /*@null@*/ rdf_section_data *csectd;
  204. /*@dependent@*/ yasm_section *sect;
  205. sect = yasm_bc_get_section(precbc);
  206. csectd = yasm_section_get_data(sect, &rdf_section_data_cb);
  207. if (!csectd)
  208. yasm_internal_error(N_("didn't understand section"));
  209. reloc->refseg = csectd->scnum;
  210. intn_plus = yasm_bc_next_offset(precbc);
  211. } else {
  212. /* must be common/external */
  213. rsymd = yasm_symrec_get_data(reloc->reloc.sym,
  214. &rdf_symrec_data_cb);
  215. if (!rsymd)
  216. yasm_internal_error(
  217. N_("rdf: no symbol data for relocated symbol"));
  218. reloc->refseg = rsymd->segment;
  219. }
  220. yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree);
  221. }
  222. if (intn_minus > 0) {
  223. intn = yasm_intnum_create_uint(intn_minus);
  224. yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL);
  225. } else
  226. intn = yasm_intnum_create_uint(intn_plus);
  227. if (value->abs) {
  228. yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0);
  229. if (!intn2) {
  230. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  231. N_("rdf: relocation too complex"));
  232. yasm_intnum_destroy(intn);
  233. return 1;
  234. }
  235. yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2);
  236. }
  237. retval = yasm_arch_intnum_tobytes(info->object->arch, intn, buf, destsize,
  238. valsize, 0, bc, warn);
  239. yasm_intnum_destroy(intn);
  240. return retval;
  241. }
  242. static int
  243. rdf_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d)
  244. {
  245. /*@null@*/ rdf_objfmt_output_info *info = (rdf_objfmt_output_info *)d;
  246. /*@null@*/ /*@only@*/ unsigned char *bigbuf;
  247. unsigned long size = REGULAR_OUTBUF_SIZE;
  248. int gap;
  249. assert(info != NULL);
  250. bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info,
  251. rdf_objfmt_output_value, NULL);
  252. /* Don't bother doing anything else if size ended up being 0. */
  253. if (size == 0) {
  254. if (bigbuf)
  255. yasm_xfree(bigbuf);
  256. return 0;
  257. }
  258. /* Warn that gaps are converted to 0 and write out the 0's. */
  259. if (gap) {
  260. yasm_warn_set(YASM_WARN_UNINIT_CONTENTS,
  261. N_("uninitialized space: zeroing"));
  262. /* Write out in chunks */
  263. memset(&info->rsd->raw_data[info->rsd->size], 0, size);
  264. } else {
  265. /* Output buf (or bigbuf if non-NULL) to file */
  266. memcpy(&info->rsd->raw_data[info->rsd->size],
  267. bigbuf ? bigbuf : info->buf, (size_t)size);
  268. }
  269. info->rsd->size += size;
  270. /* If bigbuf was allocated, free it */
  271. if (bigbuf)
  272. yasm_xfree(bigbuf);
  273. return 0;
  274. }
  275. static int
  276. rdf_objfmt_output_section_mem(yasm_section *sect, /*@null@*/ void *d)
  277. {
  278. /*@null@*/ rdf_objfmt_output_info *info = (rdf_objfmt_output_info *)d;
  279. /*@dependent@*/ /*@null@*/ rdf_section_data *rsd;
  280. unsigned long size;
  281. assert(info != NULL);
  282. rsd = yasm_section_get_data(sect, &rdf_section_data_cb);
  283. assert(rsd != NULL);
  284. size = yasm_bc_next_offset(yasm_section_bcs_last(sect));
  285. if (rsd->type == RDF_SECT_BSS) {
  286. /* Don't output BSS sections, but remember length
  287. * TODO: Check for non-reserve bytecodes?
  288. */
  289. info->bss_size += size;
  290. return 0;
  291. }
  292. /* Empty? Go on to next section */
  293. if (size == 0)
  294. return 0;
  295. /* See UGH comment in output() for why we're doing this */
  296. rsd->raw_data = yasm_xmalloc(size);
  297. rsd->size = 0;
  298. info->sect = sect;
  299. info->rsd = rsd;
  300. yasm_section_bcs_traverse(sect, info->errwarns, info,
  301. rdf_objfmt_output_bytecode);
  302. /* Sanity check final section size */
  303. if (rsd->size != size)
  304. yasm_internal_error(
  305. N_("rdf: section computed size did not match actual size"));
  306. return 0;
  307. }
  308. static int
  309. rdf_objfmt_output_section_reloc(yasm_section *sect, /*@null@*/ void *d)
  310. {
  311. /*@null@*/ rdf_objfmt_output_info *info = (rdf_objfmt_output_info *)d;
  312. /*@dependent@*/ /*@null@*/ rdf_section_data *rsd;
  313. rdf_reloc *reloc;
  314. assert(info != NULL);
  315. rsd = yasm_section_get_data(sect, &rdf_section_data_cb);
  316. assert(rsd != NULL);
  317. if (rsd->type == RDF_SECT_BSS) {
  318. /* Don't output BSS sections. */
  319. return 0;
  320. }
  321. /* Empty? Go on to next section */
  322. if (rsd->size == 0)
  323. return 0;
  324. reloc = (rdf_reloc *)yasm_section_relocs_first(sect);
  325. while (reloc) {
  326. unsigned char *localbuf = info->buf;
  327. if (reloc->type == RDF_RELOC_SEG)
  328. YASM_WRITE_8(localbuf, RDFREC_SEGRELOC);
  329. else
  330. YASM_WRITE_8(localbuf, RDFREC_RELOC);
  331. YASM_WRITE_8(localbuf, 8); /* record length */
  332. /* Section number, +0x40 if relative reloc */
  333. YASM_WRITE_8(localbuf, rsd->scnum +
  334. (reloc->type == RDF_RELOC_REL ? 0x40 : 0));
  335. yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0);
  336. localbuf += 4; /* offset of relocation */
  337. YASM_WRITE_8(localbuf, reloc->size); /* size of relocation */
  338. YASM_WRITE_16_L(localbuf, reloc->refseg); /* relocated symbol */
  339. fwrite(info->buf, 10, 1, info->f);
  340. reloc = (rdf_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc);
  341. }
  342. return 0;
  343. }
  344. static int
  345. rdf_objfmt_output_section_file(yasm_section *sect, /*@null@*/ void *d)
  346. {
  347. /*@null@*/ rdf_objfmt_output_info *info = (rdf_objfmt_output_info *)d;
  348. /*@dependent@*/ /*@null@*/ rdf_section_data *rsd;
  349. unsigned char *localbuf;
  350. assert(info != NULL);
  351. rsd = yasm_section_get_data(sect, &rdf_section_data_cb);
  352. assert(rsd != NULL);
  353. if (rsd->type == RDF_SECT_BSS) {
  354. /* Don't output BSS sections. */
  355. return 0;
  356. }
  357. /* Empty? Go on to next section */
  358. if (rsd->size == 0)
  359. return 0;
  360. /* Section header */
  361. localbuf = info->buf;
  362. YASM_WRITE_16_L(localbuf, rsd->type); /* type */
  363. YASM_WRITE_16_L(localbuf, rsd->scnum); /* number */
  364. YASM_WRITE_16_L(localbuf, rsd->reserved); /* reserved */
  365. YASM_WRITE_32_L(localbuf, rsd->size); /* length */
  366. fwrite(info->buf, 10, 1, info->f);
  367. /* Section data */
  368. fwrite(rsd->raw_data, rsd->size, 1, info->f);
  369. /* Free section data */
  370. yasm_xfree(rsd->raw_data);
  371. rsd->raw_data = NULL;
  372. return 0;
  373. }
  374. #define FLAG_EXT 0x1000
  375. #define FLAG_GLOB 0x2000
  376. #define FLAG_SET 0x4000
  377. #define FLAG_CLR 0x8000
  378. #define FLAG_MASK 0x0fff
  379. static int
  380. rdf_helper_flag(void *obj, yasm_valparam *vp, unsigned long line, void *d,
  381. uintptr_t flag)
  382. {
  383. yasm_symrec *sym = (yasm_symrec *)obj;
  384. yasm_sym_vis vis = yasm_symrec_get_visibility(sym);
  385. unsigned int *flags = (unsigned int *)d;
  386. if (((vis & YASM_SYM_GLOBAL) && (flag & FLAG_GLOB)) ||
  387. ((vis & YASM_SYM_EXTERN) && (flag & FLAG_EXT))) {
  388. if (flag & FLAG_SET)
  389. *flags |= flag & FLAG_MASK;
  390. else if (flag & FLAG_CLR)
  391. *flags &= ~(flag & FLAG_MASK);
  392. }
  393. return 0;
  394. }
  395. static unsigned int
  396. rdf_parse_flags(yasm_symrec *sym)
  397. {
  398. /*@dependent@*/ /*@null@*/ yasm_valparamhead *objext_valparams =
  399. yasm_symrec_get_objext_valparams(sym);
  400. unsigned int flags = 0;
  401. static const yasm_dir_help help[] = {
  402. { "data", 0, rdf_helper_flag, 0,
  403. FLAG_EXT|FLAG_GLOB|FLAG_SET|SYM_DATA },
  404. { "object", 0, rdf_helper_flag, 0,
  405. FLAG_EXT|FLAG_GLOB|FLAG_SET|SYM_DATA },
  406. { "proc", 0, rdf_helper_flag, 0,
  407. FLAG_EXT|FLAG_GLOB|FLAG_SET|SYM_FUNCTION },
  408. { "function", 0, rdf_helper_flag, 0,
  409. FLAG_EXT|FLAG_GLOB|FLAG_SET|SYM_FUNCTION },
  410. { "import", 0, rdf_helper_flag, 0, FLAG_EXT|FLAG_SET|SYM_IMPORT },
  411. { "export", 0, rdf_helper_flag, 0, FLAG_GLOB|FLAG_SET|SYM_GLOBAL },
  412. { "far", 0, rdf_helper_flag, 0, FLAG_EXT|FLAG_SET|SYM_FAR },
  413. { "near", 0, rdf_helper_flag, 0, FLAG_EXT|FLAG_CLR|SYM_FAR }
  414. };
  415. if (!objext_valparams)
  416. return 0;
  417. yasm_dir_helper(sym, yasm_vps_first(objext_valparams), 0, help,
  418. NELEMS(help), &flags, yasm_dir_helper_valparam_warn);
  419. return flags;
  420. }
  421. static int
  422. rdf_objfmt_output_sym(yasm_symrec *sym, /*@null@*/ void *d)
  423. {
  424. /*@null@*/ rdf_objfmt_output_info *info = (rdf_objfmt_output_info *)d;
  425. yasm_sym_vis vis = yasm_symrec_get_visibility(sym);
  426. /*@only@*/ char *name;
  427. size_t len;
  428. unsigned long value = 0;
  429. unsigned int scnum = 0;
  430. /*@dependent@*/ /*@null@*/ yasm_section *sect;
  431. /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc;
  432. unsigned char *localbuf;
  433. assert(info != NULL);
  434. if (vis == YASM_SYM_LOCAL || vis == YASM_SYM_DLOCAL)
  435. return 0; /* skip local syms */
  436. /* Look at symrec for value/scnum/etc. */
  437. if (yasm_symrec_get_label(sym, &precbc)) {
  438. /*@dependent@*/ /*@null@*/ rdf_section_data *csectd;
  439. if (precbc)
  440. sect = yasm_bc_get_section(precbc);
  441. else
  442. sect = NULL;
  443. if (!sect)
  444. return 0;
  445. /* it's a label: get value and offset. */
  446. csectd = yasm_section_get_data(sect, &rdf_section_data_cb);
  447. if (csectd)
  448. scnum = csectd->scnum;
  449. else
  450. yasm_internal_error(N_("didn't understand section"));
  451. value = yasm_bc_next_offset(precbc);
  452. } else if (yasm_symrec_get_equ(sym)) {
  453. yasm_warn_set(YASM_WARN_GENERAL,
  454. N_("rdf does not support exporting EQU/absolute values"));
  455. yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym));
  456. return 0;
  457. }
  458. name = yasm_symrec_get_global_name(sym, info->object);
  459. len = strlen(name);
  460. if (len > EXIM_LABEL_MAX-1) {
  461. yasm_warn_set(YASM_WARN_GENERAL,
  462. N_("label name too long, truncating to %d bytes"),
  463. EXIM_LABEL_MAX);
  464. len = EXIM_LABEL_MAX-1;
  465. }
  466. localbuf = info->buf;
  467. if (vis & YASM_SYM_GLOBAL) {
  468. YASM_WRITE_8(localbuf, RDFREC_GLOBAL);
  469. YASM_WRITE_8(localbuf, 6+len+1); /* record length */
  470. YASM_WRITE_8(localbuf, rdf_parse_flags(sym)); /* flags */
  471. YASM_WRITE_8(localbuf, scnum); /* segment referred to */
  472. YASM_WRITE_32_L(localbuf, value); /* offset */
  473. } else {
  474. /* Save symbol segment in symrec data (for later reloc gen) */
  475. scnum = info->indx++;
  476. rdf_objfmt_sym_set_data(sym, scnum);
  477. if (vis & YASM_SYM_COMMON) {
  478. /*@dependent@*/ /*@null@*/ yasm_expr **csize_expr;
  479. const yasm_intnum *intn;
  480. /*@dependent@*/ /*@null@*/ yasm_valparamhead *objext_valparams =
  481. yasm_symrec_get_objext_valparams(sym);
  482. unsigned long addralign = 0;
  483. YASM_WRITE_8(localbuf, RDFREC_COMMON);
  484. YASM_WRITE_8(localbuf, 8+len+1); /* record length */
  485. YASM_WRITE_16_L(localbuf, scnum); /* segment allocated */
  486. /* size */
  487. csize_expr = yasm_symrec_get_common_size(sym);
  488. assert(csize_expr != NULL);
  489. intn = yasm_expr_get_intnum(csize_expr, 1);
  490. if (!intn) {
  491. yasm_error_set(YASM_ERROR_NOT_CONSTANT,
  492. N_("COMMON data size not an integer expression"));
  493. } else
  494. value = yasm_intnum_get_uint(intn);
  495. YASM_WRITE_32_L(localbuf, value);
  496. /* alignment */
  497. if (objext_valparams) {
  498. yasm_valparam *vp = yasm_vps_first(objext_valparams);
  499. for (; vp; vp = yasm_vps_next(vp)) {
  500. if (!vp->val) {
  501. /*@only@*/ /*@null@*/ yasm_expr *align_expr;
  502. /*@dependent@*/ /*@null@*/
  503. const yasm_intnum *align_intn;
  504. if (!(align_expr = yasm_vp_expr(vp,
  505. info->object->symtab,
  506. yasm_symrec_get_decl_line(sym))) ||
  507. !(align_intn = yasm_expr_get_intnum(&align_expr,
  508. 0))) {
  509. yasm_error_set(YASM_ERROR_VALUE,
  510. N_("argument to `%s' is not an integer"),
  511. vp->val);
  512. if (align_expr)
  513. yasm_expr_destroy(align_expr);
  514. continue;
  515. }
  516. addralign = yasm_intnum_get_uint(align_intn);
  517. yasm_expr_destroy(align_expr);
  518. /* Alignments must be a power of two. */
  519. if (!is_exp2(addralign)) {
  520. yasm_error_set(YASM_ERROR_VALUE,
  521. N_("alignment constraint is not a power of two"));
  522. continue;
  523. }
  524. } else
  525. yasm_warn_set(YASM_WARN_GENERAL,
  526. N_("Unrecognized qualifier `%s'"), vp->val);
  527. }
  528. }
  529. YASM_WRITE_16_L(localbuf, addralign);
  530. } else if (vis & YASM_SYM_EXTERN) {
  531. unsigned int flags = rdf_parse_flags(sym);
  532. if (flags & SYM_FAR) {
  533. YASM_WRITE_8(localbuf, RDFREC_FARIMPORT);
  534. flags &= ~SYM_FAR;
  535. } else
  536. YASM_WRITE_8(localbuf, RDFREC_IMPORT);
  537. YASM_WRITE_8(localbuf, 3+len+1); /* record length */
  538. YASM_WRITE_8(localbuf, flags); /* flags */
  539. YASM_WRITE_16_L(localbuf, scnum); /* segment allocated */
  540. }
  541. }
  542. /* Symbol name */
  543. memcpy(localbuf, name, len);
  544. localbuf += len;
  545. YASM_WRITE_8(localbuf, 0); /* 0-terminated name */
  546. yasm_xfree(name);
  547. fwrite(info->buf, (unsigned long)(localbuf-info->buf), 1, info->f);
  548. yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym));
  549. return 0;
  550. }
  551. static void
  552. rdf_objfmt_output(yasm_object *object, FILE *f, int all_syms,
  553. yasm_errwarns *errwarns)
  554. {
  555. yasm_objfmt_rdf *objfmt_rdf = (yasm_objfmt_rdf *)object->objfmt;
  556. rdf_objfmt_output_info info;
  557. unsigned char *localbuf;
  558. long headerlen, filelen;
  559. xdf_str *cur;
  560. size_t len;
  561. info.object = object;
  562. info.objfmt_rdf = objfmt_rdf;
  563. info.errwarns = errwarns;
  564. info.f = f;
  565. info.buf = yasm_xmalloc(REGULAR_OUTBUF_SIZE);
  566. info.bss_size = 0;
  567. /* Allocate space for file header by seeking forward */
  568. if (fseek(f, (long)strlen(RDF_MAGIC)+8, SEEK_SET) < 0) {
  569. yasm__fatal(N_("could not seek on output file"));
  570. /*@notreached@*/
  571. return;
  572. }
  573. /* Output custom header records (library and module, etc) */
  574. cur = STAILQ_FIRST(&objfmt_rdf->module_names);
  575. while (cur) {
  576. len = strlen(cur->str)+1;
  577. localbuf = info.buf;
  578. YASM_WRITE_8(localbuf, RDFREC_MODNAME); /* record type */
  579. YASM_WRITE_8(localbuf, len); /* record length */
  580. fwrite(info.buf, 2, 1, f);
  581. fwrite(cur->str, len, 1, f);
  582. cur = STAILQ_NEXT(cur, link);
  583. }
  584. cur = STAILQ_FIRST(&objfmt_rdf->library_names);
  585. while (cur) {
  586. len = strlen(cur->str)+1;
  587. localbuf = info.buf;
  588. YASM_WRITE_8(localbuf, RDFREC_DLL); /* record type */
  589. YASM_WRITE_8(localbuf, len); /* record length */
  590. fwrite(info.buf, 2, 1, f);
  591. fwrite(cur->str, len, 1, f);
  592. cur = STAILQ_NEXT(cur, link);
  593. }
  594. /* Output symbol table */
  595. info.indx = objfmt_rdf->parse_scnum;
  596. yasm_symtab_traverse(object->symtab, &info, rdf_objfmt_output_sym);
  597. /* UGH! Due to the fact the relocs go at the beginning of the file, and
  598. * we only know if we have relocs when we output the sections, we have
  599. * to output the section data before we have output the relocs. But
  600. * we also don't know how much space to preallocate for relocs, so....
  601. * we output into memory buffers first (thus the UGH).
  602. *
  603. * Stupid object format design, if you ask me (basically all other
  604. * object formats put the relocs *after* the section data to avoid this
  605. * exact problem).
  606. *
  607. * We also calculate the total size of all BSS sections here.
  608. */
  609. if (yasm_object_sections_traverse(object, &info,
  610. rdf_objfmt_output_section_mem))
  611. return;
  612. /* Output all relocs */
  613. if (yasm_object_sections_traverse(object, &info,
  614. rdf_objfmt_output_section_reloc))
  615. return;
  616. /* Output BSS record */
  617. if (info.bss_size > 0) {
  618. localbuf = info.buf;
  619. YASM_WRITE_8(localbuf, RDFREC_BSS); /* record type */
  620. YASM_WRITE_8(localbuf, 4); /* record length */
  621. YASM_WRITE_32_L(localbuf, info.bss_size); /* total BSS size */
  622. fwrite(info.buf, 6, 1, f);
  623. }
  624. /* Determine header length */
  625. headerlen = ftell(f);
  626. if (headerlen == -1) {
  627. yasm__fatal(N_("could not get file position on output file"));
  628. /*@notreached@*/
  629. return;
  630. }
  631. /* Section data (to file) */
  632. if (yasm_object_sections_traverse(object, &info,
  633. rdf_objfmt_output_section_file))
  634. return;
  635. /* NULL section to end file */
  636. memset(info.buf, 0, 10);
  637. fwrite(info.buf, 10, 1, f);
  638. /* Determine object length */
  639. filelen = ftell(f);
  640. if (filelen == -1) {
  641. yasm__fatal(N_("could not get file position on output file"));
  642. /*@notreached@*/
  643. return;
  644. }
  645. /* Write file header */
  646. if (fseek(f, 0, SEEK_SET) < 0) {
  647. yasm__fatal(N_("could not seek on output file"));
  648. /*@notreached@*/
  649. return;
  650. }
  651. fwrite(RDF_MAGIC, strlen(RDF_MAGIC), 1, f);
  652. localbuf = info.buf;
  653. YASM_WRITE_32_L(localbuf, filelen-10); /* object size */
  654. YASM_WRITE_32_L(localbuf, headerlen-14); /* header size */
  655. fwrite(info.buf, 8, 1, f);
  656. yasm_xfree(info.buf);
  657. }
  658. static void
  659. rdf_objfmt_destroy(yasm_objfmt *objfmt)
  660. {
  661. yasm_objfmt_rdf *objfmt_rdf = (yasm_objfmt_rdf *)objfmt;
  662. xdf_str *cur, *next;
  663. cur = STAILQ_FIRST(&objfmt_rdf->module_names);
  664. while (cur) {
  665. next = STAILQ_NEXT(cur, link);
  666. yasm_xfree(cur->str);
  667. yasm_xfree(cur);
  668. cur = next;
  669. }
  670. cur = STAILQ_FIRST(&objfmt_rdf->library_names);
  671. while (cur) {
  672. next = STAILQ_NEXT(cur, link);
  673. yasm_xfree(cur->str);
  674. yasm_xfree(cur);
  675. cur = next;
  676. }
  677. yasm_xfree(objfmt);
  678. }
  679. static void
  680. rdf_objfmt_init_new_section(yasm_section *sect, unsigned long line)
  681. {
  682. yasm_object *object = yasm_section_get_object(sect);
  683. const char *sectname = yasm_section_get_name(sect);
  684. yasm_objfmt_rdf *objfmt_rdf = (yasm_objfmt_rdf *)object->objfmt;
  685. rdf_section_data *data;
  686. yasm_symrec *sym;
  687. data = yasm_xmalloc(sizeof(rdf_section_data));
  688. data->scnum = objfmt_rdf->parse_scnum++;
  689. data->type = 0;
  690. data->reserved = 0;
  691. data->size = 0;
  692. data->raw_data = NULL;
  693. yasm_section_add_data(sect, &rdf_section_data_cb, data);
  694. sym = yasm_symtab_define_label(object->symtab, sectname,
  695. yasm_section_bcs_first(sect), 1, line);
  696. data->sym = sym;
  697. }
  698. static yasm_section *
  699. rdf_objfmt_add_default_section(yasm_object *object)
  700. {
  701. yasm_section *retval;
  702. rdf_section_data *rsd;
  703. int isnew;
  704. retval = yasm_object_get_general(object, ".text", 0, 1, 0, &isnew, 0);
  705. if (isnew) {
  706. rsd = yasm_section_get_data(retval, &rdf_section_data_cb);
  707. rsd->type = RDF_SECT_CODE;
  708. rsd->reserved = 0;
  709. yasm_section_set_default(retval, 1);
  710. }
  711. return retval;
  712. }
  713. static int
  714. rdf_helper_set_type(void *obj, yasm_valparam *vp, unsigned long line,
  715. void *d, uintptr_t newtype)
  716. {
  717. unsigned int *type = (unsigned int *)d;
  718. *type = newtype;
  719. return 0;
  720. }
  721. struct rdf_section_switch_data {
  722. /*@only@*/ /*@null@*/ yasm_intnum *reserved_intn;
  723. unsigned int type;
  724. };
  725. static int
  726. rdf_helper_set_reserved(void *obj, yasm_valparam *vp, unsigned long line,
  727. void *d)
  728. {
  729. struct rdf_section_switch_data *data = (struct rdf_section_switch_data *)d;
  730. if (!vp->val && vp->type == YASM_PARAM_EXPR)
  731. return yasm_dir_helper_intn(obj, vp, line, &data->reserved_intn, 0);
  732. else
  733. return yasm_dir_helper_valparam_warn(obj, vp, line, d);
  734. }
  735. static /*@observer@*/ /*@null@*/ yasm_section *
  736. rdf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams,
  737. /*@unused@*/ /*@null@*/
  738. yasm_valparamhead *objext_valparams,
  739. unsigned long line)
  740. {
  741. yasm_valparam *vp = yasm_vps_first(valparams);
  742. yasm_section *retval;
  743. int isnew;
  744. unsigned int reserved = 0;
  745. int flags_override = 0;
  746. const char *sectname;
  747. rdf_section_data *rsd;
  748. struct rdf_section_switch_data data;
  749. static const yasm_dir_help help[] = {
  750. { "bss", 0, rdf_helper_set_type,
  751. offsetof(struct rdf_section_switch_data, type), RDF_SECT_BSS },
  752. { "code", 0, rdf_helper_set_type,
  753. offsetof(struct rdf_section_switch_data, type), RDF_SECT_CODE },
  754. { "text", 0, rdf_helper_set_type,
  755. offsetof(struct rdf_section_switch_data, type), RDF_SECT_CODE },
  756. { "data", 0, rdf_helper_set_type,
  757. offsetof(struct rdf_section_switch_data, type), RDF_SECT_DATA },
  758. { "comment", 0, rdf_helper_set_type,
  759. offsetof(struct rdf_section_switch_data, type), RDF_SECT_COMMENT },
  760. { "lcomment", 0, rdf_helper_set_type,
  761. offsetof(struct rdf_section_switch_data, type), RDF_SECT_LCOMMENT },
  762. { "pcomment", 0, rdf_helper_set_type,
  763. offsetof(struct rdf_section_switch_data, type), RDF_SECT_PCOMMENT },
  764. { "symdebug", 0, rdf_helper_set_type,
  765. offsetof(struct rdf_section_switch_data, type), RDF_SECT_SYMDEBUG },
  766. { "linedebug", 0, rdf_helper_set_type,
  767. offsetof(struct rdf_section_switch_data, type), RDF_SECT_LINEDEBUG },
  768. { "reserved", 1, yasm_dir_helper_intn,
  769. offsetof(struct rdf_section_switch_data, reserved_intn), 0 }
  770. };
  771. data.reserved_intn = NULL;
  772. data.type = 0xffff;
  773. vp = yasm_vps_first(valparams);
  774. sectname = yasm_vp_string(vp);
  775. if (!sectname)
  776. return NULL;
  777. vp = yasm_vps_next(vp);
  778. if (strcmp(sectname, ".text") == 0)
  779. data.type = RDF_SECT_CODE;
  780. else if (strcmp(sectname, ".data") == 0)
  781. data.type = RDF_SECT_DATA;
  782. else if (strcmp(sectname, ".bss") == 0)
  783. data.type = RDF_SECT_BSS;
  784. flags_override = yasm_dir_helper(object, vp, line, help, NELEMS(help),
  785. &data, rdf_helper_set_reserved);
  786. if (flags_override < 0)
  787. return NULL; /* error occurred */
  788. if (data.type == 0xffff) {
  789. yasm_error_set(YASM_ERROR_VALUE,
  790. N_("new segment declared without type code"));
  791. data.type = RDF_SECT_DATA;
  792. }
  793. if (data.reserved_intn) {
  794. reserved = yasm_intnum_get_uint(data.reserved_intn);
  795. yasm_intnum_destroy(data.reserved_intn);
  796. }
  797. retval = yasm_object_get_general(object, sectname, 0, 1,
  798. data.type == RDF_SECT_BSS, &isnew, line);
  799. rsd = yasm_section_get_data(retval, &rdf_section_data_cb);
  800. if (isnew || yasm_section_is_default(retval)) {
  801. yasm_section_set_default(retval, 0);
  802. rsd->type = data.type;
  803. rsd->reserved = reserved;
  804. } else if (flags_override)
  805. yasm_warn_set(YASM_WARN_GENERAL,
  806. N_("section flags ignored on section redeclaration"));
  807. return retval;
  808. }
  809. static /*@observer@*/ /*@null@*/ yasm_symrec *
  810. rdf_objfmt_get_special_sym(yasm_object *object, const char *name,
  811. const char *parser)
  812. {
  813. return NULL;
  814. }
  815. static void
  816. rdf_section_data_destroy(void *data)
  817. {
  818. rdf_section_data *rsd = (rdf_section_data *)data;
  819. if (rsd->raw_data)
  820. yasm_xfree(rsd->raw_data);
  821. yasm_xfree(data);
  822. }
  823. static void
  824. rdf_section_data_print(void *data, FILE *f, int indent_level)
  825. {
  826. rdf_section_data *rsd = (rdf_section_data *)data;
  827. fprintf(f, "%*ssym=\n", indent_level, "");
  828. yasm_symrec_print(rsd->sym, f, indent_level+1);
  829. fprintf(f, "%*sscnum=%ld\n", indent_level, "", rsd->scnum);
  830. fprintf(f, "%*stype=0x%x\n", indent_level, "", rsd->type);
  831. fprintf(f, "%*sreserved=0x%x\n", indent_level, "", rsd->reserved);
  832. fprintf(f, "%*ssize=%ld\n", indent_level, "", rsd->size);
  833. }
  834. static void
  835. rdf_symrec_data_destroy(void *data)
  836. {
  837. yasm_xfree(data);
  838. }
  839. static void
  840. rdf_symrec_data_print(void *data, FILE *f, int indent_level)
  841. {
  842. rdf_symrec_data *rsymd = (rdf_symrec_data *)data;
  843. fprintf(f, "%*ssymtab segment=%u\n", indent_level, "", rsymd->segment);
  844. }
  845. static void
  846. rdf_objfmt_add_libmodule(yasm_object *object, char *name, int lib)
  847. {
  848. yasm_objfmt_rdf *objfmt_rdf = (yasm_objfmt_rdf *)object->objfmt;
  849. xdf_str *str;
  850. /* Add to list */
  851. str = yasm_xmalloc(sizeof(xdf_str));
  852. str->str = name;
  853. if (lib)
  854. STAILQ_INSERT_TAIL(&objfmt_rdf->library_names, str, link);
  855. else
  856. STAILQ_INSERT_TAIL(&objfmt_rdf->module_names, str, link);
  857. if (strlen(str->str) > MODLIB_NAME_MAX-1) {
  858. yasm_warn_set(YASM_WARN_GENERAL,
  859. N_("name too long, truncating to %d bytes"),
  860. MODLIB_NAME_MAX);
  861. str->str[MODLIB_NAME_MAX-1] = '\0';
  862. }
  863. }
  864. static void
  865. dir_library(yasm_object *object, yasm_valparamhead *valparams,
  866. yasm_valparamhead *objext_valparams, unsigned long line)
  867. {
  868. yasm_valparam *vp = yasm_vps_first(valparams);
  869. rdf_objfmt_add_libmodule(object, yasm__xstrdup(yasm_vp_string(vp)), 1);
  870. }
  871. static void
  872. dir_module(yasm_object *object, yasm_valparamhead *valparams,
  873. yasm_valparamhead *objext_valparams, unsigned long line)
  874. {
  875. yasm_valparam *vp = yasm_vps_first(valparams);
  876. rdf_objfmt_add_libmodule(object, yasm__xstrdup(yasm_vp_string(vp)), 0);
  877. }
  878. /* Define valid debug formats to use with this object format */
  879. static const char *rdf_objfmt_dbgfmt_keywords[] = {
  880. "null",
  881. NULL
  882. };
  883. static const yasm_directive rdf_objfmt_directives[] = {
  884. { "library", "nasm", dir_library, YASM_DIR_ARG_REQUIRED },
  885. { "module", "nasm", dir_module, YASM_DIR_ARG_REQUIRED },
  886. { NULL, NULL, NULL, 0 }
  887. };
  888. static const char *rdf_nasm_stdmac[] = {
  889. "%imacro library 1+.nolist",
  890. "[library %1]",
  891. "%endmacro",
  892. "%imacro module 1+.nolist",
  893. "[module %1]",
  894. "%endmacro",
  895. NULL
  896. };
  897. static const yasm_stdmac rdf_objfmt_stdmacs[] = {
  898. { "nasm", "nasm", rdf_nasm_stdmac },
  899. { NULL, NULL, NULL }
  900. };
  901. /* Define objfmt structure -- see objfmt.h for details */
  902. yasm_objfmt_module yasm_rdf_LTX_objfmt = {
  903. "Relocatable Dynamic Object File Format (RDOFF) v2.0",
  904. "rdf",
  905. "rdf",
  906. 32,
  907. 0,
  908. rdf_objfmt_dbgfmt_keywords,
  909. "null",
  910. rdf_objfmt_directives,
  911. rdf_objfmt_stdmacs,
  912. rdf_objfmt_create,
  913. rdf_objfmt_output,
  914. rdf_objfmt_destroy,
  915. rdf_objfmt_add_default_section,
  916. rdf_objfmt_init_new_section,
  917. rdf_objfmt_section_switch,
  918. rdf_objfmt_get_special_sym
  919. };