outaout.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2013 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. * outaout.c output routines for the Netwide Assembler to produce
  35. * Linux a.out object files
  36. */
  37. #include "compiler.h"
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include "nasm.h"
  43. #include "nasmlib.h"
  44. #include "error.h"
  45. #include "saa.h"
  46. #include "raa.h"
  47. #include "stdscan.h"
  48. #include "eval.h"
  49. #include "outform.h"
  50. #include "outlib.h"
  51. #if defined OF_AOUT || defined OF_AOUTB
  52. #define RELTYPE_ABSOLUTE 0x00
  53. #define RELTYPE_RELATIVE 0x01
  54. #define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
  55. #define RELTYPE_GOTOFF 0x10
  56. #define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
  57. #define RELTYPE_PLT 0x21
  58. #define RELTYPE_SYMFLAG 0x08
  59. struct Reloc {
  60. struct Reloc *next;
  61. int32_t address; /* relative to _start_ of section */
  62. int32_t symbol; /* symbol number or -ve section id */
  63. int bytes; /* 2 or 4 */
  64. int reltype; /* see above */
  65. };
  66. struct Symbol {
  67. int32_t strpos; /* string table position of name */
  68. int type; /* symbol type - see flags below */
  69. int32_t value; /* address, or COMMON variable size */
  70. int32_t size; /* size for data or function exports */
  71. int32_t segment; /* back-reference used by gsym_reloc */
  72. struct Symbol *next; /* list of globals in each section */
  73. struct Symbol *nextfwd; /* list of unresolved-size symbols */
  74. char *name; /* for unresolved-size symbols */
  75. int32_t symnum; /* index into symbol table */
  76. };
  77. /*
  78. * Section IDs - used in Reloc.symbol when negative, and in
  79. * Symbol.type when positive.
  80. */
  81. #define SECT_ABS 2 /* absolute value */
  82. #define SECT_TEXT 4 /* text section */
  83. #define SECT_DATA 6 /* data section */
  84. #define SECT_BSS 8 /* bss section */
  85. #define SECT_MASK 0xE /* mask out any of the above */
  86. /*
  87. * More flags used in Symbol.type.
  88. */
  89. #define SYM_GLOBAL 1 /* it's a global symbol */
  90. #define SYM_DATA 0x100 /* used for shared libs */
  91. #define SYM_FUNCTION 0x200 /* used for shared libs */
  92. #define SYM_WITH_SIZE 0x4000 /* not output; internal only */
  93. /*
  94. * Bit more explanation of symbol types: SECT_xxx denotes a local
  95. * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
  96. * this module. Just SYM_GLOBAL, with zero value, denotes an
  97. * external symbol referenced in this module. And just SYM_GLOBAL,
  98. * but with a non-zero value, declares a C `common' variable, of
  99. * size `value'.
  100. */
  101. struct Section {
  102. struct SAA *data;
  103. uint32_t len, size, nrelocs;
  104. int32_t index;
  105. struct Reloc *head, **tail;
  106. struct Symbol *gsyms, *asym;
  107. };
  108. static struct Section stext, sdata, sbss;
  109. static struct SAA *syms;
  110. static uint32_t nsyms;
  111. static struct RAA *bsym;
  112. static struct SAA *strs;
  113. static uint32_t strslen;
  114. static struct Symbol *fwds;
  115. static int bsd;
  116. static int is_pic;
  117. static void aout_write(void);
  118. static void aout_write_relocs(struct Reloc *);
  119. static void aout_write_syms(void);
  120. static void aout_sect_write(struct Section *, const uint8_t *,
  121. uint32_t);
  122. static void aout_pad_sections(void);
  123. static void aout_fixup_relocs(struct Section *);
  124. /*
  125. * Special section numbers which are used to define special
  126. * symbols, which can be used with WRT to provide PIC relocation
  127. * types.
  128. */
  129. static int32_t aout_gotpc_sect, aout_gotoff_sect;
  130. static int32_t aout_got_sect, aout_plt_sect;
  131. static int32_t aout_sym_sect;
  132. static void aoutg_init(void)
  133. {
  134. stext.data = saa_init(1L);
  135. stext.head = NULL;
  136. stext.tail = &stext.head;
  137. sdata.data = saa_init(1L);
  138. sdata.head = NULL;
  139. sdata.tail = &sdata.head;
  140. stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
  141. stext.nrelocs = sdata.nrelocs = 0;
  142. stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
  143. stext.index = seg_alloc();
  144. sdata.index = seg_alloc();
  145. sbss.index = seg_alloc();
  146. stext.asym = sdata.asym = sbss.asym = NULL;
  147. syms = saa_init((int32_t)sizeof(struct Symbol));
  148. nsyms = 0;
  149. bsym = raa_init();
  150. strs = saa_init(1L);
  151. strslen = 0;
  152. fwds = NULL;
  153. }
  154. #ifdef OF_AOUT
  155. static void aout_init(void)
  156. {
  157. bsd = false;
  158. aoutg_init();
  159. aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
  160. aout_plt_sect = aout_sym_sect = NO_SEG;
  161. }
  162. #endif
  163. #ifdef OF_AOUTB
  164. extern const struct ofmt of_aoutb;
  165. static void aoutb_init(void)
  166. {
  167. bsd = true;
  168. aoutg_init();
  169. is_pic = 0x00; /* may become 0x40 */
  170. aout_gotpc_sect = seg_alloc();
  171. backend_label("..gotpc", aout_gotpc_sect + 1, 0L);
  172. aout_gotoff_sect = seg_alloc();
  173. backend_label("..gotoff", aout_gotoff_sect + 1, 0L);
  174. aout_got_sect = seg_alloc();
  175. backend_label("..got", aout_got_sect + 1, 0L);
  176. aout_plt_sect = seg_alloc();
  177. backend_label("..plt", aout_plt_sect + 1, 0L);
  178. aout_sym_sect = seg_alloc();
  179. backend_label("..sym", aout_sym_sect + 1, 0L);
  180. }
  181. #endif
  182. static void aout_cleanup(void)
  183. {
  184. struct Reloc *r;
  185. aout_pad_sections();
  186. aout_fixup_relocs(&stext);
  187. aout_fixup_relocs(&sdata);
  188. aout_write();
  189. saa_free(stext.data);
  190. while (stext.head) {
  191. r = stext.head;
  192. stext.head = stext.head->next;
  193. nasm_free(r);
  194. }
  195. saa_free(sdata.data);
  196. while (sdata.head) {
  197. r = sdata.head;
  198. sdata.head = sdata.head->next;
  199. nasm_free(r);
  200. }
  201. saa_free(syms);
  202. raa_free(bsym);
  203. saa_free(strs);
  204. }
  205. static int32_t aout_section_names(char *name, int pass, int *bits)
  206. {
  207. (void)pass;
  208. /*
  209. * Default to 32 bits.
  210. */
  211. if (!name) {
  212. *bits = 32;
  213. return stext.index;
  214. }
  215. if (!strcmp(name, ".text"))
  216. return stext.index;
  217. else if (!strcmp(name, ".data"))
  218. return sdata.index;
  219. else if (!strcmp(name, ".bss"))
  220. return sbss.index;
  221. else
  222. return NO_SEG;
  223. }
  224. static void aout_deflabel(char *name, int32_t segment, int64_t offset,
  225. int is_global, char *special)
  226. {
  227. int pos = strslen + 4;
  228. struct Symbol *sym;
  229. int special_used = false;
  230. if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
  231. /*
  232. * This is a NASM special symbol. We never allow it into
  233. * the a.out symbol table, even if it's a valid one. If it
  234. * _isn't_ a valid one, we should barf immediately.
  235. */
  236. if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
  237. strcmp(name, "..got") && strcmp(name, "..plt") &&
  238. strcmp(name, "..sym"))
  239. nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
  240. return;
  241. }
  242. if (is_global == 3) {
  243. struct Symbol **s;
  244. /*
  245. * Fix up a forward-reference symbol size from the first
  246. * pass.
  247. */
  248. for (s = &fwds; *s; s = &(*s)->nextfwd)
  249. if (!strcmp((*s)->name, name)) {
  250. struct tokenval tokval;
  251. expr *e;
  252. char *p = special;
  253. p = nasm_skip_spaces(nasm_skip_word(p));
  254. stdscan_reset();
  255. stdscan_set(p);
  256. tokval.t_type = TOKEN_INVALID;
  257. e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
  258. if (e) {
  259. if (!is_simple(e))
  260. nasm_error(ERR_NONFATAL, "cannot use relocatable"
  261. " expression as symbol size");
  262. else
  263. (*s)->size = reloc_value(e);
  264. }
  265. /*
  266. * Remove it from the list of unresolved sizes.
  267. */
  268. nasm_free((*s)->name);
  269. *s = (*s)->nextfwd;
  270. return;
  271. }
  272. return; /* it wasn't an important one */
  273. }
  274. saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
  275. strslen += 1 + strlen(name);
  276. sym = saa_wstruct(syms);
  277. sym->strpos = pos;
  278. sym->type = is_global ? SYM_GLOBAL : 0;
  279. sym->segment = segment;
  280. if (segment == NO_SEG)
  281. sym->type |= SECT_ABS;
  282. else if (segment == stext.index) {
  283. sym->type |= SECT_TEXT;
  284. if (is_global) {
  285. sym->next = stext.gsyms;
  286. stext.gsyms = sym;
  287. } else if (!stext.asym)
  288. stext.asym = sym;
  289. } else if (segment == sdata.index) {
  290. sym->type |= SECT_DATA;
  291. if (is_global) {
  292. sym->next = sdata.gsyms;
  293. sdata.gsyms = sym;
  294. } else if (!sdata.asym)
  295. sdata.asym = sym;
  296. } else if (segment == sbss.index) {
  297. sym->type |= SECT_BSS;
  298. if (is_global) {
  299. sym->next = sbss.gsyms;
  300. sbss.gsyms = sym;
  301. } else if (!sbss.asym)
  302. sbss.asym = sym;
  303. } else
  304. sym->type = SYM_GLOBAL;
  305. if (is_global == 2)
  306. sym->value = offset;
  307. else
  308. sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
  309. if (is_global && sym->type != SYM_GLOBAL) {
  310. /*
  311. * Global symbol exported _from_ this module. We must check
  312. * the special text for type information.
  313. */
  314. if (special) {
  315. int n = strcspn(special, " ");
  316. if (!nasm_strnicmp(special, "function", n))
  317. sym->type |= SYM_FUNCTION;
  318. else if (!nasm_strnicmp(special, "data", n) ||
  319. !nasm_strnicmp(special, "object", n))
  320. sym->type |= SYM_DATA;
  321. else
  322. nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
  323. n, special);
  324. if (special[n]) {
  325. struct tokenval tokval;
  326. expr *e;
  327. int fwd = false;
  328. char *saveme = stdscan_get();
  329. if (!bsd) {
  330. nasm_error(ERR_NONFATAL, "Linux a.out does not support"
  331. " symbol size information");
  332. } else {
  333. while (special[n] && nasm_isspace(special[n]))
  334. n++;
  335. /*
  336. * We have a size expression; attempt to
  337. * evaluate it.
  338. */
  339. sym->type |= SYM_WITH_SIZE;
  340. stdscan_reset();
  341. stdscan_set(special + n);
  342. tokval.t_type = TOKEN_INVALID;
  343. e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
  344. if (fwd) {
  345. sym->nextfwd = fwds;
  346. fwds = sym;
  347. sym->name = nasm_strdup(name);
  348. } else if (e) {
  349. if (!is_simple(e))
  350. nasm_error(ERR_NONFATAL, "cannot use relocatable"
  351. " expression as symbol size");
  352. else
  353. sym->size = reloc_value(e);
  354. }
  355. }
  356. stdscan_set(saveme);
  357. }
  358. special_used = true;
  359. }
  360. }
  361. /*
  362. * define the references from external-symbol segment numbers
  363. * to these symbol records.
  364. */
  365. if (segment != NO_SEG && segment != stext.index &&
  366. segment != sdata.index && segment != sbss.index)
  367. bsym = raa_write(bsym, segment, nsyms);
  368. sym->symnum = nsyms;
  369. nsyms++;
  370. if (sym->type & SYM_WITH_SIZE)
  371. nsyms++; /* and another for the size */
  372. if (special && !special_used)
  373. nasm_error(ERR_NONFATAL, "no special symbol features supported here");
  374. }
  375. static void aout_add_reloc(struct Section *sect, int32_t segment,
  376. int reltype, int bytes)
  377. {
  378. struct Reloc *r;
  379. r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
  380. sect->tail = &r->next;
  381. r->next = NULL;
  382. r->address = sect->len;
  383. r->symbol = (segment == NO_SEG ? -SECT_ABS :
  384. segment == stext.index ? -SECT_TEXT :
  385. segment == sdata.index ? -SECT_DATA :
  386. segment == sbss.index ? -SECT_BSS :
  387. raa_read(bsym, segment));
  388. r->reltype = reltype;
  389. if (r->symbol >= 0)
  390. r->reltype |= RELTYPE_SYMFLAG;
  391. r->bytes = bytes;
  392. sect->nrelocs++;
  393. }
  394. /*
  395. * This routine deals with ..got and ..sym relocations: the more
  396. * complicated kinds. In shared-library writing, some relocations
  397. * with respect to global symbols must refer to the precise symbol
  398. * rather than referring to an offset from the base of the section
  399. * _containing_ the symbol. Such relocations call to this routine,
  400. * which searches the symbol list for the symbol in question.
  401. *
  402. * RELTYPE_GOT references require the _exact_ symbol address to be
  403. * used; RELTYPE_ABSOLUTE references can be at an offset from the
  404. * symbol. The boolean argument `exact' tells us this.
  405. *
  406. * Return value is the adjusted value of `addr', having become an
  407. * offset from the symbol rather than the section. Should always be
  408. * zero when returning from an exact call.
  409. *
  410. * Limitation: if you define two symbols at the same place,
  411. * confusion will occur.
  412. *
  413. * Inefficiency: we search, currently, using a linked list which
  414. * isn't even necessarily sorted.
  415. */
  416. static int32_t aout_add_gsym_reloc(struct Section *sect,
  417. int32_t segment, int32_t offset,
  418. int type, int bytes, int exact)
  419. {
  420. struct Symbol *sym, *sm, *shead;
  421. struct Reloc *r;
  422. /*
  423. * First look up the segment to find whether it's text, data,
  424. * bss or an external symbol.
  425. */
  426. shead = NULL;
  427. if (segment == stext.index)
  428. shead = stext.gsyms;
  429. else if (segment == sdata.index)
  430. shead = sdata.gsyms;
  431. else if (segment == sbss.index)
  432. shead = sbss.gsyms;
  433. if (!shead) {
  434. if (exact && offset != 0)
  435. nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
  436. " for this reference");
  437. else
  438. aout_add_reloc(sect, segment, type, bytes);
  439. return offset;
  440. }
  441. if (exact) {
  442. /*
  443. * Find a symbol pointing _exactly_ at this one.
  444. */
  445. list_for_each(sym, shead)
  446. if (sym->value == offset)
  447. break;
  448. } else {
  449. /*
  450. * Find the nearest symbol below this one.
  451. */
  452. sym = NULL;
  453. list_for_each(sm, shead)
  454. if (sm->value <= offset && (!sym || sm->value > sym->value))
  455. sym = sm;
  456. }
  457. if (!sym && exact) {
  458. nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
  459. " for this reference");
  460. return 0;
  461. }
  462. r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
  463. sect->tail = &r->next;
  464. r->next = NULL;
  465. r->address = sect->len;
  466. r->symbol = sym->symnum;
  467. r->reltype = type | RELTYPE_SYMFLAG;
  468. r->bytes = bytes;
  469. sect->nrelocs++;
  470. return offset - sym->value;
  471. }
  472. /*
  473. * This routine deals with ..gotoff relocations. These _must_ refer
  474. * to a symbol, due to a perversity of *BSD's PIC implementation,
  475. * and it must be a non-global one as well; so we store `asym', the
  476. * first nonglobal symbol defined in each section, and always work
  477. * from that. Relocation type is always RELTYPE_GOTOFF.
  478. *
  479. * Return value is the adjusted value of `addr', having become an
  480. * offset from the `asym' symbol rather than the section.
  481. */
  482. static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
  483. int32_t offset, int bytes)
  484. {
  485. struct Reloc *r;
  486. struct Symbol *asym;
  487. /*
  488. * First look up the segment to find whether it's text, data,
  489. * bss or an external symbol.
  490. */
  491. asym = NULL;
  492. if (segment == stext.index)
  493. asym = stext.asym;
  494. else if (segment == sdata.index)
  495. asym = sdata.asym;
  496. else if (segment == sbss.index)
  497. asym = sbss.asym;
  498. if (!asym)
  499. nasm_error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
  500. " symbol in the section");
  501. r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
  502. sect->tail = &r->next;
  503. r->next = NULL;
  504. r->address = sect->len;
  505. r->symbol = asym->symnum;
  506. r->reltype = RELTYPE_GOTOFF;
  507. r->bytes = bytes;
  508. sect->nrelocs++;
  509. return offset - asym->value;
  510. }
  511. static void aout_out(int32_t segto, const void *data,
  512. enum out_type type, uint64_t size,
  513. int32_t segment, int32_t wrt)
  514. {
  515. struct Section *s;
  516. int32_t addr;
  517. uint8_t mydata[4], *p;
  518. /*
  519. * handle absolute-assembly (structure definitions)
  520. */
  521. if (segto == NO_SEG) {
  522. if (type != OUT_RESERVE)
  523. nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
  524. " space");
  525. return;
  526. }
  527. if (segto == stext.index)
  528. s = &stext;
  529. else if (segto == sdata.index)
  530. s = &sdata;
  531. else if (segto == sbss.index)
  532. s = NULL;
  533. else {
  534. nasm_error(ERR_WARNING, "attempt to assemble code in"
  535. " segment %d: defaulting to `.text'", segto);
  536. s = &stext;
  537. }
  538. if (!s && type != OUT_RESERVE) {
  539. nasm_error(ERR_WARNING, "attempt to initialize memory in the"
  540. " BSS section: ignored");
  541. sbss.len += realsize(type, size);
  542. return;
  543. }
  544. memset(mydata, 0, sizeof(mydata));
  545. if (type == OUT_RESERVE) {
  546. if (s) {
  547. nasm_error(ERR_WARNING, "uninitialized space declared in"
  548. " %s section: zeroing",
  549. (segto == stext.index ? "code" : "data"));
  550. aout_sect_write(s, NULL, size);
  551. } else
  552. sbss.len += size;
  553. } else if (type == OUT_RAWDATA) {
  554. if (segment != NO_SEG)
  555. nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
  556. aout_sect_write(s, data, size);
  557. } else if (type == OUT_ADDRESS) {
  558. int asize = abs((int)size);
  559. addr = *(int64_t *)data;
  560. if (segment != NO_SEG) {
  561. if (segment % 2) {
  562. nasm_error(ERR_NONFATAL, "a.out format does not support"
  563. " segment base references");
  564. } else {
  565. if (wrt == NO_SEG) {
  566. aout_add_reloc(s, segment, RELTYPE_ABSOLUTE, asize);
  567. } else if (!bsd) {
  568. nasm_error(ERR_NONFATAL,
  569. "Linux a.out format does not support"
  570. " any use of WRT");
  571. wrt = NO_SEG; /* we can at least _try_ to continue */
  572. } else if (wrt == aout_gotpc_sect + 1) {
  573. is_pic = 0x40;
  574. aout_add_reloc(s, segment, RELTYPE_GOTPC, asize);
  575. } else if (wrt == aout_gotoff_sect + 1) {
  576. is_pic = 0x40;
  577. addr = aout_add_gotoff_reloc(s, segment, addr, asize);
  578. } else if (wrt == aout_got_sect + 1) {
  579. is_pic = 0x40;
  580. addr = aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
  581. asize, true);
  582. } else if (wrt == aout_sym_sect + 1) {
  583. addr = aout_add_gsym_reloc(s, segment, addr,
  584. RELTYPE_ABSOLUTE, asize,
  585. false);
  586. } else if (wrt == aout_plt_sect + 1) {
  587. is_pic = 0x40;
  588. nasm_error(ERR_NONFATAL,
  589. "a.out format cannot produce non-PC-"
  590. "relative PLT references");
  591. } else {
  592. nasm_error(ERR_NONFATAL,
  593. "a.out format does not support this"
  594. " use of WRT");
  595. wrt = NO_SEG; /* we can at least _try_ to continue */
  596. }
  597. }
  598. }
  599. p = mydata;
  600. if (asize == 2)
  601. WRITESHORT(p, addr);
  602. else
  603. WRITELONG(p, addr);
  604. aout_sect_write(s, mydata, asize);
  605. } else if (type == OUT_REL2ADR) {
  606. if (segment == segto)
  607. nasm_panic(0, "intra-segment OUT_REL2ADR");
  608. if (segment != NO_SEG && segment % 2) {
  609. nasm_error(ERR_NONFATAL, "a.out format does not support"
  610. " segment base references");
  611. } else {
  612. if (wrt == NO_SEG) {
  613. aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
  614. } else if (!bsd) {
  615. nasm_error(ERR_NONFATAL, "Linux a.out format does not support"
  616. " any use of WRT");
  617. wrt = NO_SEG; /* we can at least _try_ to continue */
  618. } else if (wrt == aout_plt_sect + 1) {
  619. is_pic = 0x40;
  620. aout_add_reloc(s, segment, RELTYPE_PLT, 2);
  621. } else if (wrt == aout_gotpc_sect + 1 ||
  622. wrt == aout_gotoff_sect + 1 ||
  623. wrt == aout_got_sect + 1) {
  624. nasm_error(ERR_NONFATAL, "a.out format cannot produce PC-"
  625. "relative GOT references");
  626. } else {
  627. nasm_error(ERR_NONFATAL, "a.out format does not support this"
  628. " use of WRT");
  629. wrt = NO_SEG; /* we can at least _try_ to continue */
  630. }
  631. }
  632. p = mydata;
  633. WRITESHORT(p, *(int64_t *)data - (size + s->len));
  634. aout_sect_write(s, mydata, 2L);
  635. } else if (type == OUT_REL4ADR) {
  636. if (segment == segto)
  637. nasm_panic(0, "intra-segment OUT_REL4ADR");
  638. if (segment != NO_SEG && segment % 2) {
  639. nasm_error(ERR_NONFATAL, "a.out format does not support"
  640. " segment base references");
  641. } else {
  642. if (wrt == NO_SEG) {
  643. aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
  644. } else if (!bsd) {
  645. nasm_error(ERR_NONFATAL, "Linux a.out format does not support"
  646. " any use of WRT");
  647. wrt = NO_SEG; /* we can at least _try_ to continue */
  648. } else if (wrt == aout_plt_sect + 1) {
  649. is_pic = 0x40;
  650. aout_add_reloc(s, segment, RELTYPE_PLT, 4);
  651. } else if (wrt == aout_gotpc_sect + 1 ||
  652. wrt == aout_gotoff_sect + 1 ||
  653. wrt == aout_got_sect + 1) {
  654. nasm_error(ERR_NONFATAL, "a.out format cannot produce PC-"
  655. "relative GOT references");
  656. } else {
  657. nasm_error(ERR_NONFATAL, "a.out format does not support this"
  658. " use of WRT");
  659. wrt = NO_SEG; /* we can at least _try_ to continue */
  660. }
  661. }
  662. p = mydata;
  663. WRITELONG(p, *(int64_t *)data - (size + s->len));
  664. aout_sect_write(s, mydata, 4L);
  665. }
  666. }
  667. static void aout_pad_sections(void)
  668. {
  669. static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
  670. /*
  671. * Pad each of the text and data sections with NOPs until their
  672. * length is a multiple of four. (NOP == 0x90.) Also increase
  673. * the length of the BSS section similarly.
  674. */
  675. aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
  676. aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
  677. sbss.len = ALIGN(sbss.len, 4);
  678. }
  679. /*
  680. * a.out files have the curious property that all references to
  681. * things in the data or bss sections are done by addresses which
  682. * are actually relative to the start of the _text_ section, in the
  683. * _file_. (No relation to what happens after linking. No idea why
  684. * this should be so. It's very strange.) So we have to go through
  685. * the relocation table, _after_ the final size of each section is
  686. * known, and fix up the relocations pointed to.
  687. */
  688. static void aout_fixup_relocs(struct Section *sect)
  689. {
  690. struct Reloc *r;
  691. saa_rewind(sect->data);
  692. list_for_each(r, sect->head) {
  693. uint8_t *p, *q, blk[4];
  694. int32_t l;
  695. saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
  696. p = q = blk;
  697. l = *p++;
  698. if (r->bytes > 1) {
  699. l += ((int32_t)*p++) << 8;
  700. if (r->bytes == 4) {
  701. l += ((int32_t)*p++) << 16;
  702. l += ((int32_t)*p++) << 24;
  703. }
  704. }
  705. if (r->symbol == -SECT_DATA)
  706. l += stext.len;
  707. else if (r->symbol == -SECT_BSS)
  708. l += stext.len + sdata.len;
  709. if (r->bytes == 4)
  710. WRITELONG(q, l);
  711. else if (r->bytes == 2)
  712. WRITESHORT(q, l);
  713. else
  714. *q++ = l & 0xFF;
  715. saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
  716. }
  717. }
  718. static void aout_write(void)
  719. {
  720. /*
  721. * Emit the a.out header.
  722. */
  723. /* OMAGIC, M_386 or MID_I386, no flags */
  724. fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, ofile);
  725. fwriteint32_t(stext.len, ofile);
  726. fwriteint32_t(sdata.len, ofile);
  727. fwriteint32_t(sbss.len, ofile);
  728. fwriteint32_t(nsyms * 12, ofile); /* length of symbol table */
  729. fwriteint32_t(0L, ofile); /* object files have no entry point */
  730. fwriteint32_t(stext.nrelocs * 8, ofile); /* size of text relocs */
  731. fwriteint32_t(sdata.nrelocs * 8, ofile); /* size of data relocs */
  732. /*
  733. * Write out the code section and the data section.
  734. */
  735. saa_fpwrite(stext.data, ofile);
  736. saa_fpwrite(sdata.data, ofile);
  737. /*
  738. * Write out the relocations.
  739. */
  740. aout_write_relocs(stext.head);
  741. aout_write_relocs(sdata.head);
  742. /*
  743. * Write the symbol table.
  744. */
  745. aout_write_syms();
  746. /*
  747. * And the string table.
  748. */
  749. fwriteint32_t(strslen + 4, ofile); /* length includes length count */
  750. saa_fpwrite(strs, ofile);
  751. }
  752. static void aout_write_relocs(struct Reloc *r)
  753. {
  754. list_for_each(r, r) {
  755. uint32_t word2;
  756. fwriteint32_t(r->address, ofile);
  757. if (r->symbol >= 0)
  758. word2 = r->symbol;
  759. else
  760. word2 = -r->symbol;
  761. word2 |= r->reltype << 24;
  762. word2 |= (r->bytes == 1 ? 0 :
  763. r->bytes == 2 ? 0x2000000L : 0x4000000L);
  764. fwriteint32_t(word2, ofile);
  765. }
  766. }
  767. static void aout_write_syms(void)
  768. {
  769. uint32_t i;
  770. saa_rewind(syms);
  771. for (i = 0; i < nsyms; i++) {
  772. struct Symbol *sym = saa_rstruct(syms);
  773. fwriteint32_t(sym->strpos, ofile);
  774. fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, ofile);
  775. /*
  776. * Fix up the symbol value now we know the final section
  777. * sizes.
  778. */
  779. if ((sym->type & SECT_MASK) == SECT_DATA)
  780. sym->value += stext.len;
  781. if ((sym->type & SECT_MASK) == SECT_BSS)
  782. sym->value += stext.len + sdata.len;
  783. fwriteint32_t(sym->value, ofile);
  784. /*
  785. * Output a size record if necessary.
  786. */
  787. if (sym->type & SYM_WITH_SIZE) {
  788. fwriteint32_t(sym->strpos, ofile);
  789. fwriteint32_t(0x0DL, ofile); /* special value: means size */
  790. fwriteint32_t(sym->size, ofile);
  791. i++; /* use up another of `nsyms' */
  792. }
  793. }
  794. }
  795. static void aout_sect_write(struct Section *sect,
  796. const uint8_t *data, uint32_t len)
  797. {
  798. saa_wbytes(sect->data, data, len);
  799. sect->len += len;
  800. }
  801. static int32_t aout_segbase(int32_t segment)
  802. {
  803. return segment;
  804. }
  805. extern macros_t aout_stdmac[];
  806. #endif /* OF_AOUT || OF_AOUTB */
  807. #ifdef OF_AOUT
  808. const struct ofmt of_aout = {
  809. "Linux a.out object files",
  810. "aout",
  811. ".o",
  812. 0,
  813. 32,
  814. null_debug_arr,
  815. &null_debug_form,
  816. aout_stdmac,
  817. aout_init,
  818. null_reset,
  819. nasm_do_legacy_output,
  820. aout_out,
  821. aout_deflabel,
  822. aout_section_names,
  823. NULL,
  824. null_sectalign,
  825. aout_segbase,
  826. null_directive,
  827. aout_cleanup,
  828. NULL /* pragma list */
  829. };
  830. #endif
  831. #ifdef OF_AOUTB
  832. const struct ofmt of_aoutb = {
  833. "NetBSD/FreeBSD a.out object files",
  834. "aoutb",
  835. ".o",
  836. 0,
  837. 32,
  838. null_debug_arr,
  839. &null_debug_form,
  840. aout_stdmac,
  841. aoutb_init,
  842. null_reset,
  843. nasm_do_legacy_output,
  844. aout_out,
  845. aout_deflabel,
  846. aout_section_names,
  847. NULL,
  848. null_sectalign,
  849. aout_segbase,
  850. null_directive,
  851. aout_cleanup,
  852. NULL /* pragma list */
  853. };
  854. #endif