outrdf2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2016 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * outrdf2.c output routines for the Netwide Assembler to produce
  35. * RDOFF version 2 format object files, which Julian originally
  36. * planned to use it in his MOSCOW operating system.
  37. */
  38. #include "compiler.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <assert.h>
  44. #include "nasm.h"
  45. #include "nasmlib.h"
  46. #include "error.h"
  47. #include "saa.h"
  48. #include "outform.h"
  49. #include "outlib.h"
  50. /* VERBOSE_WARNINGS: define this to add some extra warnings... */
  51. #define VERBOSE_WARNINGS
  52. #ifdef OF_RDF2
  53. #include "rdoff.h"
  54. /* This signature is written to start of RDOFF files */
  55. static const char *RDOFF2Id = RDOFF2_SIGNATURE;
  56. /* Note that whenever a segment is referred to in the RDOFF file, its number
  57. * is always half of the segment number that NASM uses to refer to it; this
  58. * is because NASM only allocates even numbered segments, so as to not
  59. * waste any of the 16 bits of segment number written to the file - this
  60. * allows up to 65533 external labels to be defined; otherwise it would be
  61. * 32764. */
  62. #define COUNT_SEGTYPES 9
  63. static char *segmenttypes[COUNT_SEGTYPES] = {
  64. "null", "text", "code", "data",
  65. "comment", "lcomment", "pcomment",
  66. "symdebug", "linedebug"
  67. };
  68. static int segmenttypenumbers[COUNT_SEGTYPES] = {
  69. 0, 1, 1, 2, 3, 4, 5, 6, 7
  70. };
  71. /* code for managing buffers needed to separate code and data into individual
  72. * sections until they are ready to be written to the file.
  73. * We'd better hope that it all fits in memory else we're buggered... */
  74. #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
  75. * on 80x86 machines for efficiency */
  76. /***********************************************************************
  77. * Actual code to deal with RDOFF2 ouput format begins here...
  78. */
  79. /* global variables set during the initialisation phase */
  80. static struct SAA *seg[RDF_MAXSEGS]; /* seg 0 = code, seg 1 = data */
  81. static struct SAA *header; /* relocation/import/export records */
  82. static struct seginfo {
  83. char *segname;
  84. int segnumber;
  85. uint16_t segtype;
  86. uint16_t segreserved;
  87. int32_t seglength;
  88. } segments[RDF_MAXSEGS];
  89. static int nsegments;
  90. static int32_t bsslength;
  91. static int32_t headerlength;
  92. static void rdf2_init(void)
  93. {
  94. int segtext, segdata, segbss;
  95. /* set up the initial segments */
  96. segments[0].segname = ".text";
  97. segments[0].segnumber = 0;
  98. segments[0].segtype = 1;
  99. segments[0].segreserved = 0;
  100. segments[0].seglength = 0;
  101. segments[1].segname = ".data";
  102. segments[1].segnumber = 1;
  103. segments[1].segtype = 2;
  104. segments[1].segreserved = 0;
  105. segments[1].seglength = 0;
  106. segments[2].segname = ".bss";
  107. segments[2].segnumber = 2;
  108. segments[2].segtype = 0xFFFF; /* reserved - should never be produced */
  109. segments[2].segreserved = 0;
  110. segments[2].seglength = 0;
  111. nsegments = 3;
  112. seg[0] = saa_init(1L);
  113. seg[1] = saa_init(1L);
  114. seg[2] = NULL; /* special case! */
  115. header = saa_init(1L);
  116. segtext = seg_alloc();
  117. segdata = seg_alloc();
  118. segbss = seg_alloc();
  119. if (segtext != 0 || segdata != 2 || segbss != 4)
  120. nasm_panic(0,
  121. "rdf segment numbers not allocated as expected (%d,%d,%d)",
  122. segtext, segdata, segbss);
  123. bsslength = 0;
  124. headerlength = 0;
  125. }
  126. static int32_t rdf2_section_names(char *name, int pass, int *bits)
  127. {
  128. int i;
  129. bool err;
  130. char *p, *q;
  131. int code = -1;
  132. int reserved = 0;
  133. (void)pass;
  134. /*
  135. * Default is 32 bits, in the text segment.
  136. */
  137. if (!name) {
  138. *bits = 32;
  139. return 0;
  140. }
  141. /* look for segment type code following segment name */
  142. p = name;
  143. while (*p && !nasm_isspace(*p))
  144. p++;
  145. if (*p) { /* we're now in whitespace */
  146. *p++ = '\0';
  147. while (*p && nasm_isspace(80))
  148. *p++ = '\0';
  149. }
  150. if (*p) { /* we're now in an attribute value */
  151. /*
  152. * see if we have an optional ',number' following the type code
  153. */
  154. if ((q = strchr(p, ','))) {
  155. *q++ = '\0';
  156. reserved = readnum(q, &err);
  157. if (err) {
  158. nasm_error(ERR_NONFATAL,
  159. "value following comma must be numeric");
  160. reserved = 0;
  161. }
  162. }
  163. /*
  164. * check it against the text strings in segmenttypes
  165. */
  166. for (i = 0; i < COUNT_SEGTYPES; i++)
  167. if (!nasm_stricmp(p, segmenttypes[i])) {
  168. code = segmenttypenumbers[i];
  169. break;
  170. }
  171. if (code == -1) { /* didn't find anything */
  172. code = readnum(p, &err);
  173. if (err) {
  174. nasm_error(ERR_NONFATAL, "unrecognised RDF segment type (%s)",
  175. p);
  176. code = 3;
  177. }
  178. }
  179. }
  180. for (i = 0; i < nsegments; i++) {
  181. if (!strcmp(name, segments[i].segname)) {
  182. if (code != -1 || reserved != 0)
  183. nasm_error(ERR_NONFATAL, "segment attributes specified on"
  184. " redeclaration of segment");
  185. return segments[i].segnumber * 2;
  186. }
  187. }
  188. /* declaring a new segment! */
  189. if (code == -1) {
  190. nasm_error(ERR_NONFATAL, "new segment declared without type code");
  191. code = 3;
  192. }
  193. if (nsegments == RDF_MAXSEGS) {
  194. nasm_fatal(0, "reached compiled-in maximum segment limit (%d)",
  195. RDF_MAXSEGS);
  196. return NO_SEG;
  197. }
  198. segments[nsegments].segname = nasm_strdup(name);
  199. i = seg_alloc();
  200. if (i % 2 != 0)
  201. nasm_panic(0, "seg_alloc() returned odd number");
  202. segments[nsegments].segnumber = i >> 1;
  203. segments[nsegments].segtype = code;
  204. segments[nsegments].segreserved = reserved;
  205. segments[nsegments].seglength = 0;
  206. seg[nsegments] = saa_init(1L);
  207. return i;
  208. }
  209. /*
  210. * Write relocation record
  211. */
  212. static void write_reloc_rec(struct RelocRec *r)
  213. {
  214. char buf[4], *b;
  215. if (r->refseg != (uint16_t) NO_SEG && (r->refseg & 1)) /* segment base ref */
  216. r->type = RDFREC_SEGRELOC;
  217. r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
  218. saa_wbytes(header, &r->type, 1);
  219. saa_wbytes(header, &r->reclen, 1);
  220. saa_wbytes(header, &r->segment, 1);
  221. b = buf;
  222. WRITELONG(b, r->offset);
  223. saa_wbytes(header, buf, 4);
  224. saa_wbytes(header, &r->length, 1);
  225. b = buf;
  226. WRITESHORT(b, r->refseg);
  227. saa_wbytes(header, buf, 2);
  228. headerlength += r->reclen + 2;
  229. }
  230. /*
  231. * Write export record
  232. */
  233. static void write_export_rec(struct ExportRec *r)
  234. {
  235. char buf[4], *b;
  236. r->segment >>= 1;
  237. saa_wbytes(header, &r->type, 1);
  238. saa_wbytes(header, &r->reclen, 1);
  239. saa_wbytes(header, &r->flags, 1);
  240. saa_wbytes(header, &r->segment, 1);
  241. b = buf;
  242. WRITELONG(b, r->offset);
  243. saa_wbytes(header, buf, 4);
  244. saa_wbytes(header, r->label, strlen(r->label) + 1);
  245. headerlength += r->reclen + 2;
  246. }
  247. static void write_import_rec(struct ImportRec *r)
  248. {
  249. char buf[4], *b;
  250. r->segment >>= 1;
  251. saa_wbytes(header, &r->type, 1);
  252. saa_wbytes(header, &r->reclen, 1);
  253. saa_wbytes(header, &r->flags, 1);
  254. b = buf;
  255. WRITESHORT(b, r->segment);
  256. saa_wbytes(header, buf, 2);
  257. saa_wbytes(header, r->label, strlen(r->label) + 1);
  258. headerlength += r->reclen + 2;
  259. }
  260. /*
  261. * Write BSS record
  262. */
  263. static void write_bss_rec(struct BSSRec *r)
  264. {
  265. char buf[4], *b;
  266. saa_wbytes(header, &r->type, 1);
  267. saa_wbytes(header, &r->reclen, 1);
  268. b = buf;
  269. WRITELONG(b, r->amount);
  270. saa_wbytes(header, buf, 4);
  271. headerlength += r->reclen + 2;
  272. }
  273. /*
  274. * Write common variable record
  275. */
  276. static void write_common_rec(struct CommonRec *r)
  277. {
  278. char buf[4], *b;
  279. r->segment >>= 1;
  280. saa_wbytes(header, &r->type, 1);
  281. saa_wbytes(header, &r->reclen, 1);
  282. b = buf;
  283. WRITESHORT(b, r->segment);
  284. saa_wbytes(header, buf, 2);
  285. b = buf;
  286. WRITELONG(b, r->size);
  287. saa_wbytes(header, buf, 4);
  288. b = buf;
  289. WRITESHORT(b, r->align);
  290. saa_wbytes(header, buf, 2);
  291. saa_wbytes(header, r->label, strlen(r->label) + 1);
  292. headerlength += r->reclen + 2;
  293. }
  294. /*
  295. * Write library record
  296. */
  297. static void write_dll_rec(struct DLLRec *r)
  298. {
  299. saa_wbytes(header, &r->type, 1);
  300. saa_wbytes(header, &r->reclen, 1);
  301. saa_wbytes(header, r->libname, strlen(r->libname) + 1);
  302. headerlength += r->reclen + 2;
  303. }
  304. /*
  305. * Write module name record
  306. */
  307. static void write_modname_rec(struct ModRec *r)
  308. {
  309. saa_wbytes(header, &r->type, 1);
  310. saa_wbytes(header, &r->reclen, 1);
  311. saa_wbytes(header, r->modname, strlen(r->modname) + 1);
  312. headerlength += r->reclen + 2;
  313. }
  314. /*
  315. * Handle export, import and common records.
  316. */
  317. static void rdf2_deflabel(char *name, int32_t segment, int64_t offset,
  318. int is_global, char *special)
  319. {
  320. struct ExportRec r;
  321. struct ImportRec ri;
  322. struct CommonRec ci;
  323. static int farsym = 0;
  324. static int i;
  325. char symflags = 0;
  326. int len;
  327. /* Check if the label length is OK */
  328. if ((len = strlen(name)) >= EXIM_LABEL_MAX) {
  329. nasm_error(ERR_NONFATAL, "label size exceeds %d bytes", EXIM_LABEL_MAX);
  330. return;
  331. }
  332. if (!len) {
  333. nasm_error(ERR_NONFATAL, "zero-length label");
  334. return;
  335. }
  336. if (is_global == 2) {
  337. /* Common variable */
  338. ci.type = RDFREC_COMMON;
  339. ci.size = offset;
  340. ci.segment = segment;
  341. strcpy(ci.label, name);
  342. ci.reclen = 9 + len;
  343. ci.align = 0;
  344. /*
  345. * Check the special text to see if it's a valid number and power
  346. * of two; if so, store it as the alignment for the common variable.
  347. */
  348. if (special) {
  349. bool err;
  350. ci.align = readnum(special, &err);
  351. if (err)
  352. nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
  353. " valid number", special);
  354. else if ((ci.align | (ci.align - 1)) != 2 * ci.align - 1)
  355. nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
  356. " power of two", special);
  357. }
  358. write_common_rec(&ci);
  359. }
  360. /* We don't care about local labels or fix-up hints */
  361. if (is_global != 1)
  362. return;
  363. if (special) {
  364. while (*special == ' ' || *special == '\t')
  365. special++;
  366. if (!nasm_strnicmp(special, "export", 6)) {
  367. special += 6;
  368. symflags |= SYM_GLOBAL;
  369. } else if (!nasm_strnicmp(special, "import", 6)) {
  370. special += 6;
  371. symflags |= SYM_IMPORT;
  372. }
  373. if (*special) {
  374. while (nasm_isspace(*special))
  375. special++;
  376. if (!nasm_stricmp(special, "far")) {
  377. farsym = 1;
  378. } else if (!nasm_stricmp(special, "near")) {
  379. farsym = 0;
  380. } else if (!nasm_stricmp(special, "proc") ||
  381. !nasm_stricmp(special, "function")) {
  382. symflags |= SYM_FUNCTION;
  383. } else if (!nasm_stricmp(special, "data") ||
  384. !nasm_stricmp(special, "object")) {
  385. symflags |= SYM_DATA;
  386. } else
  387. nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'",
  388. special);
  389. }
  390. }
  391. if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
  392. nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
  393. return;
  394. }
  395. for (i = 0; i < nsegments; i++) {
  396. if (segments[i].segnumber == segment >> 1)
  397. break;
  398. }
  399. if (i >= nsegments) { /* EXTERN declaration */
  400. ri.type = farsym ? RDFREC_FARIMPORT : RDFREC_IMPORT;
  401. if (symflags & SYM_GLOBAL)
  402. nasm_error(ERR_NONFATAL,
  403. "symbol type conflict - EXTERN cannot be EXPORT");
  404. ri.flags = symflags;
  405. ri.segment = segment;
  406. strcpy(ri.label, name);
  407. ri.reclen = 4 + len;
  408. write_import_rec(&ri);
  409. } else if (is_global) {
  410. r.type = RDFREC_GLOBAL; /* GLOBAL declaration */
  411. if (symflags & SYM_IMPORT)
  412. nasm_error(ERR_NONFATAL,
  413. "symbol type conflict - GLOBAL cannot be IMPORT");
  414. r.flags = symflags;
  415. r.segment = segment;
  416. r.offset = offset;
  417. strcpy(r.label, name);
  418. r.reclen = 7 + len;
  419. write_export_rec(&r);
  420. }
  421. }
  422. static void membufwrite(int segment, const void *data, int bytes)
  423. {
  424. int i;
  425. char buf[4], *b;
  426. for (i = 0; i < nsegments; i++) {
  427. if (segments[i].segnumber == segment)
  428. break;
  429. }
  430. if (i == nsegments)
  431. nasm_panic(0, "can't find segment %d", segment);
  432. if (bytes < 0) {
  433. b = buf;
  434. if (bytes == -2)
  435. WRITESHORT(b, *(int16_t *)data);
  436. else
  437. WRITELONG(b, *(int32_t *)data);
  438. data = buf;
  439. bytes = -bytes;
  440. }
  441. segments[i].seglength += bytes;
  442. saa_wbytes(seg[i], data, bytes);
  443. }
  444. static int getsegmentlength(int segment)
  445. {
  446. int i;
  447. for (i = 0; i < nsegments; i++) {
  448. if (segments[i].segnumber == segment)
  449. break;
  450. }
  451. if (i == nsegments)
  452. nasm_panic(0, "can't find segment %d", segment);
  453. return segments[i].seglength;
  454. }
  455. static void rdf2_out(int32_t segto, const void *data,
  456. enum out_type type, uint64_t size,
  457. int32_t segment, int32_t wrt)
  458. {
  459. struct RelocRec rr;
  460. uint8_t databuf[8], *pd;
  461. int seg;
  462. if (segto == NO_SEG) {
  463. if (type != OUT_RESERVE)
  464. nasm_error(ERR_NONFATAL,
  465. "attempt to assemble code in ABSOLUTE space");
  466. return;
  467. }
  468. segto >>= 1; /* convert NASM segment no to RDF number */
  469. for (seg = 0; seg < nsegments; seg++) {
  470. if (segments[seg].segnumber == segto)
  471. break;
  472. }
  473. if (seg >= nsegments) {
  474. nasm_error(ERR_NONFATAL,
  475. "specified segment not supported by rdf output format");
  476. return;
  477. }
  478. if (wrt != NO_SEG) {
  479. wrt = NO_SEG; /* continue to do _something_ */
  480. nasm_error(ERR_NONFATAL, "WRT not supported by rdf output format");
  481. }
  482. if (segto == 2 && type != OUT_RESERVE) {
  483. nasm_error(ERR_NONFATAL, "BSS segments may not be initialized");
  484. /* just reserve the space for now... */
  485. if (type == OUT_REL2ADR)
  486. size = 2;
  487. else
  488. size = 4;
  489. type = OUT_RESERVE;
  490. }
  491. if (type == OUT_RESERVE) {
  492. if (segto == 2) /* BSS segment space reserverd */
  493. bsslength += size;
  494. else
  495. while (size--)
  496. membufwrite(segto, databuf, 1);
  497. } else if (type == OUT_RAWDATA) {
  498. if (segment != NO_SEG)
  499. nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
  500. membufwrite(segto, data, size);
  501. } else if (type == OUT_ADDRESS) {
  502. int asize = abs((int)size);
  503. /* if segment == NO_SEG then we are writing an address of an
  504. object within the same segment - do not produce reloc rec. */
  505. /* FIXME - is this behaviour sane? at first glance it doesn't
  506. appear to be. Must test this thoroughly...! */
  507. if (segment != NO_SEG) {
  508. /* it's an address, so we must write a relocation record */
  509. rr.type = RDFREC_RELOC; /* type signature */
  510. rr.reclen = 8;
  511. rr.segment = segto; /* segment we're currently in */
  512. rr.offset = getsegmentlength(segto); /* current offset */
  513. rr.length = asize; /* length of reference */
  514. rr.refseg = segment; /* segment referred to */
  515. write_reloc_rec(&rr);
  516. }
  517. pd = databuf; /* convert address to little-endian */
  518. WRITEADDR(pd, *(int64_t *)data, asize);
  519. membufwrite(segto, databuf, asize);
  520. } else if (type == OUT_REL2ADR) {
  521. if (segment == segto)
  522. nasm_panic(0, "intra-segment OUT_REL2ADR");
  523. rr.reclen = 8;
  524. rr.offset = getsegmentlength(segto); /* current offset */
  525. rr.length = 2; /* length of reference */
  526. rr.refseg = segment; /* segment referred to (will be >>1'd) */
  527. if (segment != NO_SEG && segment % 2) {
  528. rr.type = RDFREC_SEGRELOC;
  529. rr.segment = segto; /* memory base refs *aren't ever* relative! */
  530. write_reloc_rec(&rr);
  531. /* what do we put in the code? Simply the data. This should almost
  532. * always be zero, unless someone's doing segment arithmetic...
  533. */
  534. rr.offset = *(int64_t *)data;
  535. } else {
  536. rr.type = RDFREC_RELOC; /* type signature */
  537. rr.segment = segto + 64; /* segment we're currently in + rel flag */
  538. write_reloc_rec(&rr);
  539. /* work out what to put in the code: offset of the end of this operand,
  540. * subtracted from any data specified, so that loader can just add
  541. * address of imported symbol onto it to get address relative to end of
  542. * instruction: import_address + data(offset) - end_of_instrn */
  543. rr.offset = *(int32_t *)data - (rr.offset + size);
  544. }
  545. membufwrite(segto, &rr.offset, -2);
  546. } else if (type == OUT_REL4ADR) {
  547. if ((segment == segto) && (globalbits != 64))
  548. nasm_panic(0, "intra-segment OUT_REL4ADR");
  549. if (segment != NO_SEG && segment % 2) {
  550. nasm_panic(0, "erm... 4 byte segment base ref?");
  551. }
  552. rr.type = RDFREC_RELOC; /* type signature */
  553. rr.segment = segto + 64; /* segment we're currently in + rel tag */
  554. rr.offset = getsegmentlength(segto); /* current offset */
  555. rr.length = 4; /* length of reference */
  556. rr.refseg = segment; /* segment referred to */
  557. rr.reclen = 8;
  558. write_reloc_rec(&rr);
  559. rr.offset = *(int64_t *)data - (rr.offset + size);
  560. membufwrite(segto, &rr.offset, -4);
  561. }
  562. }
  563. static void rdf2_cleanup(void)
  564. {
  565. int32_t l;
  566. struct BSSRec bs;
  567. int i;
  568. /* should write imported & exported symbol declarations to header here */
  569. /* generate the output file... */
  570. nasm_write(RDOFF2Id, 6, ofile); /* file type magic number */
  571. if (bsslength != 0) { /* reserve BSS */
  572. bs.type = RDFREC_BSS;
  573. bs.amount = bsslength;
  574. bs.reclen = 4;
  575. write_bss_rec(&bs);
  576. }
  577. /*
  578. * calculate overall length of the output object
  579. */
  580. l = headerlength + 4;
  581. for (i = 0; i < nsegments; i++) {
  582. if (i == 2)
  583. continue; /* skip BSS segment */
  584. l += 10 + segments[i].seglength;
  585. }
  586. l += 10; /* null segment */
  587. fwriteint32_t(l, ofile);
  588. fwriteint32_t(headerlength, ofile);
  589. saa_fpwrite(header, ofile); /* dump header */
  590. saa_free(header);
  591. for (i = 0; i < nsegments; i++) {
  592. if (i == 2)
  593. continue;
  594. fwriteint16_t(segments[i].segtype, ofile);
  595. fwriteint16_t(segments[i].segnumber, ofile);
  596. fwriteint16_t(segments[i].segreserved, ofile);
  597. fwriteint32_t(segments[i].seglength, ofile);
  598. saa_fpwrite(seg[i], ofile);
  599. saa_free(seg[i]);
  600. }
  601. /* null segment - write 10 bytes of zero */
  602. fwriteint32_t(0, ofile);
  603. fwriteint32_t(0, ofile);
  604. fwriteint16_t(0, ofile);
  605. }
  606. static int32_t rdf2_segbase(int32_t segment)
  607. {
  608. return segment;
  609. }
  610. /*
  611. * Handle RDOFF2 specific directives
  612. */
  613. static enum directive_result
  614. rdf2_directive(enum directive directive, char *value, int pass)
  615. {
  616. size_t n;
  617. switch (directive) {
  618. case D_LIBRARY:
  619. n = strlen(value);
  620. if (n >= MODLIB_NAME_MAX) {
  621. nasm_error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
  622. return DIRR_ERROR;
  623. }
  624. if (pass == 1) {
  625. struct DLLRec r;
  626. r.type = RDFREC_DLL;
  627. r.reclen = n + 1;
  628. strcpy(r.libname, value);
  629. write_dll_rec(&r);
  630. }
  631. return DIRR_OK;
  632. case D_MODULE:
  633. if ((n = strlen(value)) >= MODLIB_NAME_MAX) {
  634. nasm_error(ERR_NONFATAL, "name size exceeds %d bytes", MODLIB_NAME_MAX);
  635. return DIRR_ERROR;
  636. }
  637. if (pass == 1) {
  638. struct ModRec r;
  639. r.type = RDFREC_MODNAME;
  640. r.reclen = n + 1;
  641. strcpy(r.modname, value);
  642. write_modname_rec(&r);
  643. }
  644. return DIRR_OK;
  645. default:
  646. return DIRR_UNKNOWN;
  647. }
  648. }
  649. extern macros_t rdf2_stdmac[];
  650. const struct ofmt of_rdf2 = {
  651. "Relocatable Dynamic Object File Format v2.0",
  652. "rdf",
  653. ".rdf",
  654. 0,
  655. 64,
  656. null_debug_arr,
  657. &null_debug_form,
  658. rdf2_stdmac,
  659. rdf2_init,
  660. null_reset,
  661. nasm_do_legacy_output,
  662. rdf2_out,
  663. rdf2_deflabel,
  664. rdf2_section_names,
  665. NULL,
  666. null_sectalign,
  667. rdf2_segbase,
  668. rdf2_directive,
  669. rdf2_cleanup,
  670. NULL /* pragma list */
  671. };
  672. #endif /* OF_RDF2 */