Get rid of a bunch of unnecessary indirections
[nasm.git] / output / outelf32.c
blob0af2420b69f024f3195dcfe8a86c10594e494edb
1 /* ----------------------------------------------------------------------- *
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
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.
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.
32 * ----------------------------------------------------------------------- */
35 * outelf32.c output routines for the Netwide Assembler to produce
36 * ELF32 (i386 of course) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
46 #include <limits.h>
48 #include "nasm.h"
49 #include "nasmlib.h"
50 #include "saa.h"
51 #include "raa.h"
52 #include "stdscan.h"
53 #include "eval.h"
54 #include "output/outform.h"
55 #include "output/outlib.h"
56 #include "rbtree.h"
58 #include "output/dwarf.h"
59 #include "output/stabs.h"
60 #include "output/outelf.h"
62 #ifdef OF_ELF32
64 #define SECT_DELTA 32
65 static struct elf_section **sects;
66 static int nsects, sectlen;
68 #define SHSTR_DELTA 256
69 static char *shstrtab;
70 static int shstrtablen, shstrtabsize;
72 static struct SAA *syms;
73 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
75 static int32_t def_seg;
77 static struct RAA *bsym;
79 static struct SAA *strs;
80 static uint32_t strslen;
82 static struct elf_symbol *fwds;
84 static char elf_module[FILENAME_MAX];
86 extern struct ofmt of_elf32;
88 static struct ELF_SECTDATA {
89 void *data;
90 int32_t len;
91 bool is_saa;
92 } *elf_sects;
93 static int elf_nsect, nsections;
94 static int32_t elf_foffs;
96 static void elf_write(void);
97 static void elf_sect_write(struct elf_section *, const uint8_t *,
98 uint32_t);
99 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
100 int, int);
101 static void elf_write_sections(void);
102 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
103 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
104 static void add_sectname(char *, char *);
106 struct erel {
107 int offset, info;
110 struct symlininfo {
111 int offset;
112 int section; /* section index */
113 char *name; /* shallow-copied pointer of section name */
116 struct linelist {
117 struct linelist *next;
118 struct linelist *last;
119 struct symlininfo info;
120 char *filename;
121 int line;
124 struct sectlist {
125 struct SAA *psaa;
126 int section;
127 int line;
128 int offset;
129 int file;
130 struct sectlist *next;
131 struct sectlist *last;
134 /* common debug variables */
135 static int currentline = 1;
136 static int debug_immcall = 0;
138 /* stabs debug variables */
139 static struct linelist *stabslines = 0;
140 static int numlinestabs = 0;
141 static char *stabs_filename = 0;
142 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
143 static int stablen, stabstrlen, stabrellen;
145 /* dwarf debug variables */
146 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
147 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
148 static int dwarf_numfiles = 0, dwarf_nsections;
149 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
150 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
151 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
152 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
153 abbrevlen, linelen, linerellen, framelen, loclen;
154 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
156 static struct dfmt df_dwarf;
157 static struct dfmt df_stabs;
158 static struct elf_symbol *lastsym;
160 /* common debugging routines */
161 static void debug32_typevalue(int32_t);
162 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
163 static void debug32_directive(const char *, const char *);
165 /* stabs debugging routines */
166 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
167 static void stabs32_output(int, void *);
168 static void stabs32_generate(void);
169 static void stabs32_cleanup(void);
171 /* dwarf debugging routines */
172 static void dwarf32_init(void);
173 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
174 static void dwarf32_output(int, void *);
175 static void dwarf32_generate(void);
176 static void dwarf32_cleanup(void);
177 static void dwarf32_findfile(const char *);
178 static void dwarf32_findsect(const int);
181 * Special NASM section numbers which are used to define ELF special
182 * symbols, which can be used with WRT to provide PIC and TLS
183 * relocation types.
185 static int32_t elf_gotpc_sect, elf_gotoff_sect;
186 static int32_t elf_got_sect, elf_plt_sect;
187 static int32_t elf_sym_sect, elf_tlsie_sect;
189 static void elf_init(void)
191 sects = NULL;
192 nsects = sectlen = 0;
193 syms = saa_init((int32_t)sizeof(struct elf_symbol));
194 nlocals = nglobs = ndebugs = 0;
195 bsym = raa_init();
196 strs = saa_init(1L);
197 saa_wbytes(strs, "\0", 1L);
198 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
199 strslen = 2 + strlen(elf_module);
200 shstrtab = NULL;
201 shstrtablen = shstrtabsize = 0;;
202 add_sectname("", "");
204 fwds = NULL;
206 elf_gotpc_sect = seg_alloc();
207 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
208 elf_gotoff_sect = seg_alloc();
209 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
210 elf_got_sect = seg_alloc();
211 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
212 elf_plt_sect = seg_alloc();
213 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
214 elf_sym_sect = seg_alloc();
215 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
216 elf_tlsie_sect = seg_alloc();
217 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
219 def_seg = seg_alloc();
222 static void elf_cleanup(int debuginfo)
224 struct elf_reloc *r;
225 int i;
227 (void)debuginfo;
229 elf_write();
230 for (i = 0; i < nsects; i++) {
231 if (sects[i]->type != SHT_NOBITS)
232 saa_free(sects[i]->data);
233 if (sects[i]->head)
234 saa_free(sects[i]->rel);
235 while (sects[i]->head) {
236 r = sects[i]->head;
237 sects[i]->head = sects[i]->head->next;
238 nasm_free(r);
241 nasm_free(sects);
242 saa_free(syms);
243 raa_free(bsym);
244 saa_free(strs);
245 if (of_elf32.current_dfmt) {
246 of_elf32.current_dfmt->cleanup();
250 static void add_sectname(char *firsthalf, char *secondhalf)
252 int len = strlen(firsthalf) + strlen(secondhalf);
253 while (shstrtablen + len + 1 > shstrtabsize)
254 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
255 strcpy(shstrtab + shstrtablen, firsthalf);
256 strcat(shstrtab + shstrtablen, secondhalf);
257 shstrtablen += len + 1;
260 static int elf_make_section(char *name, int type, int flags, int align)
262 struct elf_section *s;
264 s = nasm_zalloc(sizeof(*s));
266 if (type != SHT_NOBITS)
267 s->data = saa_init(1L);
268 s->tail = &s->head;
269 if (!strcmp(name, ".text"))
270 s->index = def_seg;
271 else
272 s->index = seg_alloc();
273 add_sectname("", name);
275 s->name = nasm_strdup(name);
276 s->type = type;
277 s->flags = flags;
278 s->align = align;
280 if (nsects >= sectlen)
281 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
282 sects[nsects++] = s;
284 return nsects - 1;
287 static int32_t elf_section_names(char *name, int pass, int *bits)
289 char *p;
290 uint32_t flags, flags_and, flags_or;
291 uint64_t align;
292 int type, i;
295 * Default is 32 bits.
297 if (!name) {
298 *bits = 32;
299 return def_seg;
302 p = nasm_skip_word(name);
303 if (*p)
304 *p++ = '\0';
305 flags_and = flags_or = type = align = 0;
307 elf_section_attrib(name, p, pass, &flags_and,
308 &flags_or, &align, &type);
310 if (!strcmp(name, ".shstrtab") ||
311 !strcmp(name, ".symtab") ||
312 !strcmp(name, ".strtab")) {
313 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
314 "name `%s'", name);
315 return NO_SEG;
318 for (i = 0; i < nsects; i++)
319 if (!strcmp(name, sects[i]->name))
320 break;
321 if (i == nsects) {
322 const struct elf_known_section *ks = elf_known_sections;
324 while (ks->name) {
325 if (!strcmp(name, ks->name))
326 break;
327 ks++;
330 type = type ? type : ks->type;
331 align = align ? align : ks->align;
332 flags = (ks->flags & ~flags_and) | flags_or;
334 i = elf_make_section(name, type, flags, align);
335 } else if (pass == 1) {
336 if ((type && sects[i]->type != type)
337 || (align && sects[i]->align != align)
338 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
339 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
340 " redeclaration of section `%s'", name);
343 return sects[i]->index;
346 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
347 int is_global, char *special)
349 int pos = strslen;
350 struct elf_symbol *sym;
351 bool special_used = false;
353 #if defined(DEBUG) && DEBUG>2
354 nasm_error(ERR_DEBUG,
355 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
356 name, segment, offset, is_global, special);
357 #endif
358 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
360 * This is a NASM special symbol. We never allow it into
361 * the ELF symbol table, even if it's a valid one. If it
362 * _isn't_ a valid one, we should barf immediately.
364 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
365 strcmp(name, "..got") && strcmp(name, "..plt") &&
366 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
367 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
368 return;
371 if (is_global == 3) {
372 struct elf_symbol **s;
374 * Fix up a forward-reference symbol size from the first
375 * pass.
377 for (s = &fwds; *s; s = &(*s)->nextfwd)
378 if (!strcmp((*s)->name, name)) {
379 struct tokenval tokval;
380 expr *e;
381 char *p = nasm_skip_spaces(nasm_skip_word(special));
383 stdscan_reset();
384 stdscan_set(p);
385 tokval.t_type = TOKEN_INVALID;
386 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
387 if (e) {
388 if (!is_simple(e))
389 nasm_error(ERR_NONFATAL, "cannot use relocatable"
390 " expression as symbol size");
391 else
392 (*s)->size = reloc_value(e);
396 * Remove it from the list of unresolved sizes.
398 nasm_free((*s)->name);
399 *s = (*s)->nextfwd;
400 return;
402 return; /* it wasn't an important one */
405 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
406 strslen += 1 + strlen(name);
408 lastsym = sym = saa_wstruct(syms);
410 memset(&sym->symv, 0, sizeof(struct rbtree));
412 sym->strpos = pos;
413 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
414 sym->other = STV_DEFAULT;
415 sym->size = 0;
416 if (segment == NO_SEG)
417 sym->section = SHN_ABS;
418 else {
419 int i;
420 sym->section = SHN_UNDEF;
421 if (segment == def_seg) {
422 /* we have to be sure at least text section is there */
423 int tempint;
424 if (segment != elf_section_names(".text", 2, &tempint))
425 nasm_panic(0, "strange segment conditions in ELF driver");
427 for (i = 0; i < nsects; i++) {
428 if (segment == sects[i]->index) {
429 sym->section = i + 1;
430 break;
435 if (is_global == 2) {
436 sym->size = offset;
437 sym->symv.key = 0;
438 sym->section = SHN_COMMON;
440 * We have a common variable. Check the special text to see
441 * if it's a valid number and power of two; if so, store it
442 * as the alignment for the common variable.
444 if (special) {
445 bool err;
446 sym->symv.key = readnum(special, &err);
447 if (err)
448 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
449 " valid number", special);
450 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
451 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
452 " power of two", special);
454 special_used = true;
455 } else
456 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
458 if (sym->type == SYM_GLOBAL) {
460 * If sym->section == SHN_ABS, then the first line of the
461 * else section would cause a core dump, because its a reference
462 * beyond the end of the section array.
463 * This behaviour is exhibited by this code:
464 * GLOBAL crash_nasm
465 * crash_nasm equ 0
466 * To avoid such a crash, such requests are silently discarded.
467 * This may not be the best solution.
469 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
470 bsym = raa_write(bsym, segment, nglobs);
471 } else if (sym->section != SHN_ABS) {
473 * This is a global symbol; so we must add it to the rbtree
474 * of global symbols in its section.
476 * In addition, we check the special text for symbol
477 * type and size information.
479 sects[sym->section-1]->gsyms =
480 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
482 if (special) {
483 int n = strcspn(special, " \t");
485 if (!nasm_strnicmp(special, "function", n))
486 sym->type |= STT_FUNC;
487 else if (!nasm_strnicmp(special, "data", n) ||
488 !nasm_strnicmp(special, "object", n))
489 sym->type |= STT_OBJECT;
490 else if (!nasm_strnicmp(special, "notype", n))
491 sym->type |= STT_NOTYPE;
492 else
493 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
494 n, special);
495 special += n;
497 special = nasm_skip_spaces(special);
498 if (*special) {
499 n = strcspn(special, " \t");
500 if (!nasm_strnicmp(special, "default", n))
501 sym->other = STV_DEFAULT;
502 else if (!nasm_strnicmp(special, "internal", n))
503 sym->other = STV_INTERNAL;
504 else if (!nasm_strnicmp(special, "hidden", n))
505 sym->other = STV_HIDDEN;
506 else if (!nasm_strnicmp(special, "protected", n))
507 sym->other = STV_PROTECTED;
508 else
509 n = 0;
510 special += n;
513 if (*special) {
514 struct tokenval tokval;
515 expr *e;
516 int fwd = 0;
517 char *saveme = stdscan_get();
519 while (special[n] && nasm_isspace(special[n]))
520 n++;
522 * We have a size expression; attempt to
523 * evaluate it.
525 stdscan_reset();
526 stdscan_set(special + n);
527 tokval.t_type = TOKEN_INVALID;
528 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
529 if (fwd) {
530 sym->nextfwd = fwds;
531 fwds = sym;
532 sym->name = nasm_strdup(name);
533 } else if (e) {
534 if (!is_simple(e))
535 nasm_error(ERR_NONFATAL, "cannot use relocatable"
536 " expression as symbol size");
537 else
538 sym->size = reloc_value(e);
540 stdscan_set(saveme);
542 special_used = true;
545 * If TLS segment, mark symbol accordingly.
547 if (sects[sym->section - 1]->flags & SHF_TLS) {
548 sym->type &= 0xf0;
549 sym->type |= STT_TLS;
552 sym->globnum = nglobs;
553 nglobs++;
554 } else
555 nlocals++;
557 if (special && !special_used)
558 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
561 static void elf_add_reloc(struct elf_section *sect, int32_t segment, int type)
563 struct elf_reloc *r;
565 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
566 sect->tail = &r->next;
568 r->address = sect->len;
569 if (segment != NO_SEG) {
570 int i;
571 for (i = 0; i < nsects; i++)
572 if (segment == sects[i]->index)
573 r->symbol = i + 2;
574 if (!r->symbol)
575 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
577 r->type = type;
579 sect->nrelocs++;
583 * This routine deals with ..got and ..sym relocations: the more
584 * complicated kinds. In shared-library writing, some relocations
585 * with respect to global symbols must refer to the precise symbol
586 * rather than referring to an offset from the base of the section
587 * _containing_ the symbol. Such relocations call to this routine,
588 * which searches the symbol list for the symbol in question.
590 * R_386_GOT32 references require the _exact_ symbol address to be
591 * used; R_386_32 references can be at an offset from the symbol.
592 * The boolean argument `exact' tells us this.
594 * Return value is the adjusted value of `addr', having become an
595 * offset from the symbol rather than the section. Should always be
596 * zero when returning from an exact call.
598 * Limitation: if you define two symbols at the same place,
599 * confusion will occur.
601 * Inefficiency: we search, currently, using a linked list which
602 * isn't even necessarily sorted.
604 static int32_t elf_add_gsym_reloc(struct elf_section *sect,
605 int32_t segment, uint32_t offset,
606 int type, bool exact)
608 struct elf_reloc *r;
609 struct elf_section *s;
610 struct elf_symbol *sym;
611 struct rbtree *srb;
612 int i;
615 * First look up the segment/offset pair and find a global
616 * symbol corresponding to it. If it's not one of our segments,
617 * then it must be an external symbol, in which case we're fine
618 * doing a normal elf_add_reloc after first sanity-checking
619 * that the offset from the symbol is zero.
621 s = NULL;
622 for (i = 0; i < nsects; i++)
623 if (segment == sects[i]->index) {
624 s = sects[i];
625 break;
628 if (!s) {
629 if (exact && offset)
630 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
631 " for this reference");
632 else
633 elf_add_reloc(sect, segment, type);
634 return offset;
637 srb = rb_search(s->gsyms, offset);
638 if (!srb || (exact && srb->key != offset)) {
639 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
640 " for this reference");
641 return 0;
643 sym = container_of(srb, struct elf_symbol, symv);
645 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
646 sect->tail = &r->next;
648 r->next = NULL;
649 r->address = sect->len;
650 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
651 r->type = type;
653 sect->nrelocs++;
655 return offset - sym->symv.key;
658 static void elf_out(int32_t segto, const void *data,
659 enum out_type type, uint64_t size,
660 int32_t segment, int32_t wrt)
662 struct elf_section *s;
663 int32_t addr;
664 uint8_t mydata[8], *p;
665 int reltype, bytes;
666 int i;
667 static struct symlininfo sinfo;
670 * handle absolute-assembly (structure definitions)
672 if (segto == NO_SEG) {
673 if (type != OUT_RESERVE)
674 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
675 " space");
676 return;
679 s = NULL;
680 for (i = 0; i < nsects; i++)
681 if (segto == sects[i]->index) {
682 s = sects[i];
683 break;
685 if (!s) {
686 int tempint; /* ignored */
687 if (segto != elf_section_names(".text", 2, &tempint))
688 nasm_panic(0, "strange segment conditions in ELF driver");
689 else {
690 s = sects[nsects - 1];
691 i = nsects - 1;
695 /* again some stabs debugging stuff */
696 if (of_elf32.current_dfmt) {
697 sinfo.offset = s->len;
698 sinfo.section = i;
699 sinfo.name = s->name;
700 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
702 /* end of debugging stuff */
704 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
705 nasm_error(ERR_WARNING, "attempt to initialize memory in"
706 " BSS section `%s': ignored", s->name);
707 s->len += realsize(type, size);
708 return;
711 memset(mydata, 0, sizeof(mydata));
713 switch (type) {
714 case OUT_RESERVE:
715 if (s->type == SHT_PROGBITS) {
716 nasm_error(ERR_WARNING, "uninitialized space declared in"
717 " non-BSS section `%s': zeroing", s->name);
718 elf_sect_write(s, NULL, size);
719 } else
720 s->len += size;
721 break;
723 case OUT_RAWDATA:
724 if (segment != NO_SEG)
725 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
726 elf_sect_write(s, data, size);
727 break;
729 case OUT_ADDRESS:
731 bool gnu16 = false;
732 int asize = abs((int)size);
733 addr = *(int64_t *)data;
734 if (segment != NO_SEG) {
735 if (segment % 2) {
736 nasm_error(ERR_NONFATAL, "ELF format does not support"
737 " segment base references");
738 } else {
739 if (wrt == NO_SEG) {
741 * The if() is a hack to deal with compilers which
742 * don't handle switch() statements with 64-bit
743 * expressions.
745 switch (asize) {
746 case 1:
747 gnu16 = true;
748 elf_add_reloc(s, segment, R_386_8);
749 break;
750 case 2:
751 gnu16 = true;
752 elf_add_reloc(s, segment, R_386_16);
753 break;
754 case 4:
755 elf_add_reloc(s, segment, R_386_32);
756 break;
757 default: /* Error issued further down */
758 break;
760 } else if (wrt == elf_gotpc_sect + 1) {
762 * The user will supply GOT relative to $$. ELF
763 * will let us have GOT relative to $. So we
764 * need to fix up the data item by $-$$.
766 addr += s->len;
767 elf_add_reloc(s, segment, R_386_GOTPC);
768 } else if (wrt == elf_gotoff_sect + 1) {
769 elf_add_reloc(s, segment, R_386_GOTOFF);
770 } else if (wrt == elf_tlsie_sect + 1) {
771 addr = elf_add_gsym_reloc(s, segment, addr,
772 R_386_TLS_IE, true);
773 } else if (wrt == elf_got_sect + 1) {
774 addr = elf_add_gsym_reloc(s, segment, addr,
775 R_386_GOT32, true);
776 } else if (wrt == elf_sym_sect + 1) {
777 switch (asize) {
778 case 1:
779 gnu16 = true;
780 addr = elf_add_gsym_reloc(s, segment, addr,
781 R_386_8, false);
782 break;
783 case 2:
784 gnu16 = true;
785 addr = elf_add_gsym_reloc(s, segment, addr,
786 R_386_16, false);
787 break;
788 case 4:
789 addr = elf_add_gsym_reloc(s, segment, addr,
790 R_386_32, false);
791 break;
792 default:
793 break;
795 } else if (wrt == elf_plt_sect + 1) {
796 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
797 "relative PLT references");
798 } else {
799 nasm_error(ERR_NONFATAL, "ELF format does not support this"
800 " use of WRT");
801 wrt = NO_SEG; /* we can at least _try_ to continue */
805 p = mydata;
806 if (gnu16) {
807 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
808 "8- or 16-bit relocations in ELF32 is a GNU extension");
809 } else if (asize != 4 && segment != NO_SEG) {
810 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
812 WRITEADDR(p, addr, asize);
813 elf_sect_write(s, mydata, asize);
814 break;
817 case OUT_REL1ADR:
818 bytes = 1;
819 reltype = R_386_PC8;
820 goto rel12adr;
821 case OUT_REL2ADR:
822 bytes = 2;
823 reltype = R_386_PC16;
824 goto rel12adr;
826 rel12adr:
827 nasm_assert(segment != segto);
828 if (segment != NO_SEG && segment % 2) {
829 nasm_error(ERR_NONFATAL, "ELF format does not support"
830 " segment base references");
831 } else {
832 if (wrt == NO_SEG) {
833 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
834 "8- or 16-bit relocations in ELF is a GNU extension");
835 elf_add_reloc(s, segment, reltype);
836 } else {
837 nasm_error(ERR_NONFATAL,
838 "Unsupported non-32-bit ELF relocation");
841 p = mydata;
842 WRITESHORT(p, *(int64_t *)data - size);
843 elf_sect_write(s, mydata, bytes);
844 break;
846 case OUT_REL4ADR:
847 if (segment == segto)
848 nasm_panic(0, "intra-segment OUT_REL4ADR");
849 if (segment != NO_SEG && segment % 2) {
850 nasm_error(ERR_NONFATAL, "ELF format does not support"
851 " segment base references");
852 } else {
853 if (wrt == NO_SEG) {
854 elf_add_reloc(s, segment, R_386_PC32);
855 } else if (wrt == elf_plt_sect + 1) {
856 elf_add_reloc(s, segment, R_386_PLT32);
857 } else if (wrt == elf_gotpc_sect + 1 ||
858 wrt == elf_gotoff_sect + 1 ||
859 wrt == elf_got_sect + 1) {
860 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
861 "relative GOT references");
862 } else {
863 nasm_error(ERR_NONFATAL, "ELF format does not support this"
864 " use of WRT");
865 wrt = NO_SEG; /* we can at least _try_ to continue */
868 p = mydata;
869 WRITELONG(p, *(int64_t *)data - size);
870 elf_sect_write(s, mydata, 4L);
871 break;
873 case OUT_REL8ADR:
874 nasm_error(ERR_NONFATAL,
875 "32-bit ELF format does not support 64-bit relocations");
876 p = mydata;
877 WRITEDLONG(p, 0);
878 elf_sect_write(s, mydata, 8L);
879 break;
883 static void elf_write(void)
885 int align;
886 char *p;
887 int i;
889 struct SAA *symtab;
890 int32_t symtablen, symtablocal;
893 * Work out how many sections we will have. We have SHN_UNDEF,
894 * then the flexible user sections, then the fixed sections
895 * `.shstrtab', `.symtab' and `.strtab', then optionally
896 * relocation sections for the user sections.
898 nsections = sec_numspecial + 1;
899 if (of_elf32.current_dfmt == &df_stabs)
900 nsections += 3;
901 else if (of_elf32.current_dfmt == &df_dwarf)
902 nsections += 10;
904 add_sectname("", ".shstrtab");
905 add_sectname("", ".symtab");
906 add_sectname("", ".strtab");
907 for (i = 0; i < nsects; i++) {
908 nsections++; /* for the section itself */
909 if (sects[i]->head) {
910 nsections++; /* for its relocations */
911 add_sectname(".rel", sects[i]->name);
915 if (of_elf32.current_dfmt == &df_stabs) {
916 /* in case the debug information is wanted, just add these three sections... */
917 add_sectname("", ".stab");
918 add_sectname("", ".stabstr");
919 add_sectname(".rel", ".stab");
920 } else if (of_elf32.current_dfmt == &df_dwarf) {
921 /* the dwarf debug standard specifies the following ten sections,
922 not all of which are currently implemented,
923 although all of them are defined. */
924 add_sectname("", ".debug_aranges");
925 add_sectname(".rela", ".debug_aranges");
926 add_sectname("", ".debug_pubnames");
927 add_sectname("", ".debug_info");
928 add_sectname(".rela", ".debug_info");
929 add_sectname("", ".debug_abbrev");
930 add_sectname("", ".debug_line");
931 add_sectname(".rela", ".debug_line");
932 add_sectname("", ".debug_frame");
933 add_sectname("", ".debug_loc");
937 * Output the ELF header.
939 nasm_write("\177ELF\1\1\1", 7, ofile);
940 fputc(elf_osabi, ofile);
941 fputc(elf_abiver, ofile);
942 fwritezero(7, ofile);
943 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
944 fwriteint16_t(3, ofile); /* EM_386 processor ID */
945 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
946 fwriteint32_t(0L, ofile); /* no entry point */
947 fwriteint32_t(0L, ofile); /* no program header table */
948 fwriteint32_t(0x40L, ofile); /* section headers straight after
949 * ELF header plus alignment */
950 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
951 fwriteint16_t(0x34, ofile); /* size of ELF header */
952 fwriteint16_t(0, ofile); /* no program header table, again */
953 fwriteint16_t(0, ofile); /* still no program header table */
954 fwriteint16_t(0x28, ofile); /* size of section header */
955 fwriteint16_t(nsections, ofile); /* number of sections */
956 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
957 * section header table */
958 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
959 fwriteint32_t(0L, ofile);
960 fwriteint32_t(0L, ofile);
963 * Build the symbol table and relocation tables.
965 symtab = elf_build_symtab(&symtablen, &symtablocal);
966 for (i = 0; i < nsects; i++)
967 if (sects[i]->head)
968 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
969 sects[i]->head);
972 * Now output the section header table.
975 elf_foffs = 0x40 + 0x28 * nsections;
976 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
977 elf_foffs += align;
978 elf_nsect = 0;
979 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
981 /* SHN_UNDEF */
982 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
983 p = shstrtab + 1;
985 /* The normal sections */
986 for (i = 0; i < nsects; i++) {
987 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
988 (sects[i]->type == SHT_PROGBITS ?
989 sects[i]->data : NULL), true,
990 sects[i]->len, 0, 0, sects[i]->align, 0);
991 p += strlen(p) + 1;
994 /* .shstrtab */
995 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
996 shstrtablen, 0, 0, 1, 0);
997 p += strlen(p) + 1;
999 /* .symtab */
1000 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1001 symtablen, sec_strtab, symtablocal, 4, 16);
1002 p += strlen(p) + 1;
1004 /* .strtab */
1005 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1006 strslen, 0, 0, 1, 0);
1007 p += strlen(p) + 1;
1009 /* The relocation sections */
1010 for (i = 0; i < nsects; i++)
1011 if (sects[i]->head) {
1012 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1013 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1014 p += strlen(p) + 1;
1017 if (of_elf32.current_dfmt == &df_stabs) {
1018 /* for debugging information, create the last three sections
1019 which are the .stab , .stabstr and .rel.stab sections respectively */
1021 /* this function call creates the stab sections in memory */
1022 stabs32_generate();
1024 if (stabbuf && stabstrbuf && stabrelbuf) {
1025 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1026 stablen, sec_stabstr, 0, 4, 12);
1027 p += strlen(p) + 1;
1029 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1030 stabstrlen, 0, 0, 4, 0);
1031 p += strlen(p) + 1;
1033 /* link -> symtable info -> section to refer to */
1034 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1035 stabrellen, sec_symtab, sec_stab, 4, 8);
1036 p += strlen(p) + 1;
1038 } else if (of_elf32.current_dfmt == &df_dwarf) {
1039 /* for dwarf debugging information, create the ten dwarf sections */
1041 /* this function call creates the dwarf sections in memory */
1042 if (dwarf_fsect)
1043 dwarf32_generate();
1045 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1046 arangeslen, 0, 0, 1, 0);
1047 p += strlen(p) + 1;
1049 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1050 arangesrellen, sec_symtab, sec_debug_aranges,
1051 1, 12);
1052 p += strlen(p) + 1;
1054 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1055 false, pubnameslen, 0, 0, 1, 0);
1056 p += strlen(p) + 1;
1058 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1059 infolen, 0, 0, 1, 0);
1060 p += strlen(p) + 1;
1062 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1063 inforellen, sec_symtab, sec_debug_info, 1, 12);
1064 p += strlen(p) + 1;
1066 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1067 abbrevlen, 0, 0, 1, 0);
1068 p += strlen(p) + 1;
1070 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1071 linelen, 0, 0, 1, 0);
1072 p += strlen(p) + 1;
1074 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1075 linerellen, sec_symtab, sec_debug_line, 1, 12);
1076 p += strlen(p) + 1;
1078 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1079 framelen, 0, 0, 8, 0);
1080 p += strlen(p) + 1;
1082 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1083 loclen, 0, 0, 1, 0);
1084 p += strlen(p) + 1;
1086 fwritezero(align, ofile);
1089 * Now output the sections.
1091 elf_write_sections();
1093 nasm_free(elf_sects);
1094 saa_free(symtab);
1097 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1099 struct SAA *s = saa_init(1L);
1100 struct elf_symbol *sym;
1101 uint8_t entry[16], *p;
1102 int i;
1104 *len = *local = 0;
1107 * First, an all-zeros entry, required by the ELF spec.
1109 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1110 *len += 16;
1111 (*local)++;
1114 * Next, an entry for the file name.
1116 p = entry;
1117 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1118 WRITELONG(p, 0); /* no value */
1119 WRITELONG(p, 0); /* no size either */
1120 WRITESHORT(p, STT_FILE); /* type FILE */
1121 WRITESHORT(p, SHN_ABS);
1122 saa_wbytes(s, entry, 16L);
1123 *len += 16;
1124 (*local)++;
1127 * Now some standard symbols defining the segments, for relocation
1128 * purposes.
1130 for (i = 1; i <= nsects; i++) {
1131 p = entry;
1132 WRITELONG(p, 0); /* no symbol name */
1133 WRITELONG(p, 0); /* offset zero */
1134 WRITELONG(p, 0); /* size zero */
1135 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1136 WRITESHORT(p, i); /* section id */
1137 saa_wbytes(s, entry, 16L);
1138 *len += 16;
1139 (*local)++;
1143 * Now the other local symbols.
1145 saa_rewind(syms);
1146 while ((sym = saa_rstruct(syms))) {
1147 if (sym->type & SYM_GLOBAL)
1148 continue;
1149 p = entry;
1150 WRITELONG(p, sym->strpos);
1151 WRITELONG(p, sym->symv.key);
1152 WRITELONG(p, sym->size);
1153 WRITECHAR(p, sym->type); /* type and binding */
1154 WRITECHAR(p, sym->other); /* visibility */
1155 WRITESHORT(p, sym->section);
1156 saa_wbytes(s, entry, 16L);
1157 *len += 16;
1158 (*local)++;
1161 * dwarf needs symbols for debug sections
1162 * which are relocation targets.
1164 /*** fix for 32 bit ***/
1165 if (of_elf32.current_dfmt == &df_dwarf) {
1166 dwarf_infosym = *local;
1167 p = entry;
1168 WRITELONG(p, 0); /* no symbol name */
1169 WRITELONG(p, (uint32_t) 0); /* offset zero */
1170 WRITELONG(p, (uint32_t) 0); /* size zero */
1171 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1172 WRITESHORT(p, sec_debug_info); /* section id */
1173 saa_wbytes(s, entry, 16L);
1174 *len += 16;
1175 (*local)++;
1176 dwarf_abbrevsym = *local;
1177 p = entry;
1178 WRITELONG(p, 0); /* no symbol name */
1179 WRITELONG(p, (uint32_t) 0); /* offset zero */
1180 WRITELONG(p, (uint32_t) 0); /* size zero */
1181 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1182 WRITESHORT(p, sec_debug_abbrev); /* section id */
1183 saa_wbytes(s, entry, 16L);
1184 *len += 16;
1185 (*local)++;
1186 dwarf_linesym = *local;
1187 p = entry;
1188 WRITELONG(p, 0); /* no symbol name */
1189 WRITELONG(p, (uint32_t) 0); /* offset zero */
1190 WRITELONG(p, (uint32_t) 0); /* size zero */
1191 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1192 WRITESHORT(p, sec_debug_line); /* section id */
1193 saa_wbytes(s, entry, 16L);
1194 *len += 16;
1195 (*local)++;
1199 * Now the global symbols.
1201 saa_rewind(syms);
1202 while ((sym = saa_rstruct(syms))) {
1203 if (!(sym->type & SYM_GLOBAL))
1204 continue;
1205 p = entry;
1206 WRITELONG(p, sym->strpos);
1207 WRITELONG(p, sym->symv.key);
1208 WRITELONG(p, sym->size);
1209 WRITECHAR(p, sym->type); /* type and binding */
1210 WRITECHAR(p, sym->other); /* visibility */
1211 WRITESHORT(p, sym->section);
1212 saa_wbytes(s, entry, 16L);
1213 *len += 16;
1216 return s;
1219 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1221 struct SAA *s;
1222 uint8_t *p, entry[8];
1223 int32_t global_offset;
1225 if (!r)
1226 return NULL;
1228 s = saa_init(1L);
1229 *len = 0;
1232 * How to onvert from a global placeholder to a real symbol index;
1233 * the +2 refers to the two special entries, the null entry and
1234 * the filename entry.
1236 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1238 while (r) {
1239 int32_t sym = r->symbol;
1241 if (sym >= GLOBAL_TEMP_BASE)
1242 sym += global_offset;
1244 p = entry;
1245 WRITELONG(p, r->address);
1246 WRITELONG(p, (sym << 8) + r->type);
1247 saa_wbytes(s, entry, 8L);
1248 *len += 8;
1250 r = r->next;
1253 return s;
1256 static void elf_section_header(int name, int type, int flags,
1257 void *data, bool is_saa, int32_t datalen,
1258 int link, int info, int align, int eltsize)
1260 elf_sects[elf_nsect].data = data;
1261 elf_sects[elf_nsect].len = datalen;
1262 elf_sects[elf_nsect].is_saa = is_saa;
1263 elf_nsect++;
1265 fwriteint32_t((int32_t)name, ofile);
1266 fwriteint32_t((int32_t)type, ofile);
1267 fwriteint32_t((int32_t)flags, ofile);
1268 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1269 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1270 fwriteint32_t(datalen, ofile);
1271 if (data)
1272 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1273 fwriteint32_t((int32_t)link, ofile);
1274 fwriteint32_t((int32_t)info, ofile);
1275 fwriteint32_t((int32_t)align, ofile);
1276 fwriteint32_t((int32_t)eltsize, ofile);
1279 static void elf_write_sections(void)
1281 int i;
1282 for (i = 0; i < elf_nsect; i++)
1283 if (elf_sects[i].data) {
1284 int32_t len = elf_sects[i].len;
1285 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1286 int32_t align = reallen - len;
1287 if (elf_sects[i].is_saa)
1288 saa_fpwrite(elf_sects[i].data, ofile);
1289 else
1290 nasm_write(elf_sects[i].data, len, ofile);
1291 fwritezero(align, ofile);
1295 static void elf_sect_write(struct elf_section *sect,
1296 const uint8_t *data, uint32_t len)
1298 saa_wbytes(sect->data, data, len);
1299 sect->len += len;
1302 static void elf_sectalign(int32_t seg, unsigned int value)
1304 struct elf_section *s = NULL;
1305 int i;
1307 for (i = 0; i < nsects; i++) {
1308 if (sects[i]->index == seg) {
1309 s = sects[i];
1310 break;
1313 if (!s || !is_power2(value))
1314 return;
1316 if (value > s->align)
1317 s->align = value;
1320 static int32_t elf_segbase(int32_t segment)
1322 return segment;
1325 static void elf_filename(char *inname, char *outname)
1327 strcpy(elf_module, inname);
1328 standard_extension(inname, outname, ".o");
1331 extern macros_t elf_stdmac[];
1333 static int elf_set_info(enum geninfo type, char **val)
1335 (void)type;
1336 (void)val;
1337 return 0;
1339 static struct dfmt df_dwarf = {
1340 "ELF32 (i386) dwarf debug format for Linux/Unix",
1341 "dwarf",
1342 dwarf32_init,
1343 dwarf32_linenum,
1344 debug32_deflabel,
1345 debug32_directive,
1346 debug32_typevalue,
1347 dwarf32_output,
1348 dwarf32_cleanup
1350 static struct dfmt df_stabs = {
1351 "ELF32 (i386) stabs debug format for Linux/Unix",
1352 "stabs",
1353 null_debug_init,
1354 stabs32_linenum,
1355 debug32_deflabel,
1356 debug32_directive,
1357 debug32_typevalue,
1358 stabs32_output,
1359 stabs32_cleanup
1362 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1364 struct ofmt of_elf32 = {
1365 "ELF32 (i386) object files (e.g. Linux)",
1366 "elf32",
1369 elf32_debugs_arr,
1370 &df_stabs,
1371 elf_stdmac,
1372 elf_init,
1373 elf_set_info,
1374 elf_out,
1375 elf_deflabel,
1376 elf_section_names,
1377 elf_sectalign,
1378 elf_segbase,
1379 elf_directive,
1380 elf_filename,
1381 elf_cleanup
1384 /* again, the stabs debugging stuff (code) */
1386 static void stabs32_linenum(const char *filename, int32_t linenumber,
1387 int32_t segto)
1389 (void)segto;
1391 if (!stabs_filename) {
1392 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1393 strcpy(stabs_filename, filename);
1394 } else {
1395 if (strcmp(stabs_filename, filename)) {
1397 * yep, a memory leak...this program is one-shot anyway, so who cares...
1398 * in fact, this leak comes in quite handy to maintain a list of files
1399 * encountered so far in the symbol lines...
1402 /* why not nasm_free(stabs_filename); we're done with the old one */
1404 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1405 strcpy(stabs_filename, filename);
1408 debug_immcall = 1;
1409 currentline = linenumber;
1412 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1413 char *special)
1415 (void)name;
1416 (void)segment;
1417 (void)offset;
1418 (void)is_global;
1419 (void)special;
1422 static void debug32_directive(const char *directive, const char *params)
1424 (void)directive;
1425 (void)params;
1428 static void debug32_typevalue(int32_t type)
1430 int32_t stype, ssize;
1431 switch (TYM_TYPE(type)) {
1432 case TY_LABEL:
1433 ssize = 0;
1434 stype = STT_NOTYPE;
1435 break;
1436 case TY_BYTE:
1437 ssize = 1;
1438 stype = STT_OBJECT;
1439 break;
1440 case TY_WORD:
1441 ssize = 2;
1442 stype = STT_OBJECT;
1443 break;
1444 case TY_DWORD:
1445 ssize = 4;
1446 stype = STT_OBJECT;
1447 break;
1448 case TY_FLOAT:
1449 ssize = 4;
1450 stype = STT_OBJECT;
1451 break;
1452 case TY_QWORD:
1453 ssize = 8;
1454 stype = STT_OBJECT;
1455 break;
1456 case TY_TBYTE:
1457 ssize = 10;
1458 stype = STT_OBJECT;
1459 break;
1460 case TY_OWORD:
1461 ssize = 16;
1462 stype = STT_OBJECT;
1463 break;
1464 case TY_YWORD:
1465 ssize = 32;
1466 stype = STT_OBJECT;
1467 break;
1468 case TY_COMMON:
1469 ssize = 0;
1470 stype = STT_COMMON;
1471 break;
1472 case TY_SEG:
1473 ssize = 0;
1474 stype = STT_SECTION;
1475 break;
1476 case TY_EXTERN:
1477 ssize = 0;
1478 stype = STT_NOTYPE;
1479 break;
1480 case TY_EQU:
1481 ssize = 0;
1482 stype = STT_NOTYPE;
1483 break;
1484 default:
1485 ssize = 0;
1486 stype = STT_NOTYPE;
1487 break;
1489 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1490 lastsym->size = ssize;
1491 lastsym->type = stype;
1495 static void stabs32_output(int type, void *param)
1497 struct symlininfo *s;
1498 struct linelist *el;
1499 if (type == TY_STABSSYMLIN) {
1500 if (debug_immcall) {
1501 s = (struct symlininfo *)param;
1502 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1503 return; /* line info is only collected for executable sections */
1504 numlinestabs++;
1505 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1506 el->info.offset = s->offset;
1507 el->info.section = s->section;
1508 el->info.name = s->name;
1509 el->line = currentline;
1510 el->filename = stabs_filename;
1511 el->next = 0;
1512 if (stabslines) {
1513 stabslines->last->next = el;
1514 stabslines->last = el;
1515 } else {
1516 stabslines = el;
1517 stabslines->last = el;
1521 debug_immcall = 0;
1524 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1526 static void stabs32_generate(void)
1528 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1529 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1530 char **allfiles;
1531 int *fileidx;
1533 struct linelist *ptr;
1535 ptr = stabslines;
1537 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1538 for (i = 0; i < numlinestabs; i++)
1539 allfiles[i] = 0;
1540 numfiles = 0;
1541 while (ptr) {
1542 if (numfiles == 0) {
1543 allfiles[0] = ptr->filename;
1544 numfiles++;
1545 } else {
1546 for (i = 0; i < numfiles; i++) {
1547 if (!strcmp(allfiles[i], ptr->filename))
1548 break;
1550 if (i >= numfiles) {
1551 allfiles[i] = ptr->filename;
1552 numfiles++;
1555 ptr = ptr->next;
1557 strsize = 1;
1558 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1559 for (i = 0; i < numfiles; i++) {
1560 fileidx[i] = strsize;
1561 strsize += strlen(allfiles[i]) + 1;
1563 mainfileindex = 0;
1564 for (i = 0; i < numfiles; i++) {
1565 if (!strcmp(allfiles[i], elf_module)) {
1566 mainfileindex = i;
1567 break;
1572 * worst case size of the stab buffer would be:
1573 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1574 * plus one "ending" entry
1576 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1577 sizeof(struct stabentry));
1578 ssbuf = (uint8_t *)nasm_malloc(strsize);
1579 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1580 rptr = rbuf;
1582 for (i = 0; i < numfiles; i++)
1583 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1584 ssbuf[0] = 0;
1586 stabstrlen = strsize; /* set global variable for length of stab strings */
1588 sptr = sbuf;
1589 ptr = stabslines;
1590 numstabs = 0;
1592 if (ptr) {
1594 * this is the first stab, its strx points to the filename of the
1595 * the source-file, the n_desc field should be set to the number
1596 * of remaining stabs
1598 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1600 /* this is the stab for the main source file */
1601 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1603 /* relocation table entry */
1606 * Since the symbol table has two entries before
1607 * the section symbols, the index in the info.section
1608 * member must be adjusted by adding 2
1611 WRITELONG(rptr, (sptr - sbuf) - 4);
1612 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1614 numstabs++;
1615 currfile = mainfileindex;
1618 while (ptr) {
1619 if (strcmp(allfiles[currfile], ptr->filename)) {
1620 /* oops file has changed... */
1621 for (i = 0; i < numfiles; i++)
1622 if (!strcmp(allfiles[i], ptr->filename))
1623 break;
1624 currfile = i;
1625 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1626 ptr->info.offset);
1627 numstabs++;
1629 /* relocation table entry */
1630 WRITELONG(rptr, (sptr - sbuf) - 4);
1631 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1634 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1635 numstabs++;
1637 /* relocation table entry */
1639 WRITELONG(rptr, (sptr - sbuf) - 4);
1640 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1642 ptr = ptr->next;
1646 /* this is an "ending" token */
1647 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1648 numstabs++;
1650 ((struct stabentry *)sbuf)->n_desc = numstabs;
1652 nasm_free(allfiles);
1653 nasm_free(fileidx);
1655 stablen = (sptr - sbuf);
1656 stabrellen = (rptr - rbuf);
1657 stabrelbuf = rbuf;
1658 stabbuf = sbuf;
1659 stabstrbuf = ssbuf;
1662 static void stabs32_cleanup(void)
1664 struct linelist *ptr, *del;
1665 if (!stabslines)
1666 return;
1668 ptr = stabslines;
1669 while (ptr) {
1670 del = ptr;
1671 ptr = ptr->next;
1672 nasm_free(del);
1675 nasm_free(stabbuf);
1676 nasm_free(stabrelbuf);
1677 nasm_free(stabstrbuf);
1680 /* dwarf routines */
1682 static void dwarf32_init(void)
1684 ndebugs = 3; /* 3 debug symbols */
1687 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1688 int32_t segto)
1690 (void)segto;
1691 dwarf32_findfile(filename);
1692 debug_immcall = 1;
1693 currentline = linenumber;
1696 /* called from elf_out with type == TY_DEBUGSYMLIN */
1697 static void dwarf32_output(int type, void *param)
1699 int ln, aa, inx, maxln, soc;
1700 struct symlininfo *s;
1701 struct SAA *plinep;
1703 (void)type;
1705 s = (struct symlininfo *)param;
1707 /* line number info is only gathered for executable sections */
1708 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1709 return;
1711 /* Check if section index has changed */
1712 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1713 dwarf32_findsect(s->section);
1715 /* do nothing unless line or file has changed */
1716 if (!debug_immcall)
1717 return;
1719 ln = currentline - dwarf_csect->line;
1720 aa = s->offset - dwarf_csect->offset;
1721 inx = dwarf_clist->line;
1722 plinep = dwarf_csect->psaa;
1723 /* check for file change */
1724 if (!(inx == dwarf_csect->file)) {
1725 saa_write8(plinep,DW_LNS_set_file);
1726 saa_write8(plinep,inx);
1727 dwarf_csect->file = inx;
1729 /* check for line change */
1730 if (ln) {
1731 /* test if in range of special op code */
1732 maxln = line_base + line_range;
1733 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1734 if (ln >= line_base && ln < maxln && soc < 256) {
1735 saa_write8(plinep,soc);
1736 } else {
1737 saa_write8(plinep,DW_LNS_advance_line);
1738 saa_wleb128s(plinep,ln);
1739 if (aa) {
1740 saa_write8(plinep,DW_LNS_advance_pc);
1741 saa_wleb128u(plinep,aa);
1744 dwarf_csect->line = currentline;
1745 dwarf_csect->offset = s->offset;
1748 /* show change handled */
1749 debug_immcall = 0;
1753 static void dwarf32_generate(void)
1755 uint8_t *pbuf;
1756 int indx;
1757 struct linelist *ftentry;
1758 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1759 struct SAA *parangesrel, *plinesrel, *pinforel;
1760 struct sectlist *psect;
1761 size_t saalen, linepoff, totlen, highaddr;
1763 /* write epilogues for each line program range */
1764 /* and build aranges section */
1765 paranges = saa_init(1L);
1766 parangesrel = saa_init(1L);
1767 saa_write16(paranges,2); /* dwarf version */
1768 saa_write32(parangesrel, paranges->datalen+4);
1769 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1770 saa_write32(parangesrel, 0);
1771 saa_write32(paranges,0); /* offset into info */
1772 saa_write8(paranges,4); /* pointer size */
1773 saa_write8(paranges,0); /* not segmented */
1774 saa_write32(paranges,0); /* padding */
1775 /* iterate though sectlist entries */
1776 psect = dwarf_fsect;
1777 totlen = 0;
1778 highaddr = 0;
1779 for (indx = 0; indx < dwarf_nsections; indx++) {
1780 plinep = psect->psaa;
1781 /* Line Number Program Epilogue */
1782 saa_write8(plinep,2); /* std op 2 */
1783 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1784 saa_write8(plinep,DW_LNS_extended_op);
1785 saa_write8(plinep,1); /* operand length */
1786 saa_write8(plinep,DW_LNE_end_sequence);
1787 totlen += plinep->datalen;
1788 /* range table relocation entry */
1789 saa_write32(parangesrel, paranges->datalen + 4);
1790 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1791 saa_write32(parangesrel, (uint32_t) 0);
1792 /* range table entry */
1793 saa_write32(paranges,0x0000); /* range start */
1794 saa_write32(paranges,sects[psect->section]->len); /* range length */
1795 highaddr += sects[psect->section]->len;
1796 /* done with this entry */
1797 psect = psect->next;
1799 saa_write32(paranges,0); /* null address */
1800 saa_write32(paranges,0); /* null length */
1801 saalen = paranges->datalen;
1802 arangeslen = saalen + 4;
1803 arangesbuf = pbuf = nasm_malloc(arangeslen);
1804 WRITELONG(pbuf,saalen); /* initial length */
1805 saa_rnbytes(paranges, pbuf, saalen);
1806 saa_free(paranges);
1808 /* build rela.aranges section */
1809 arangesrellen = saalen = parangesrel->datalen;
1810 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1811 saa_rnbytes(parangesrel, pbuf, saalen);
1812 saa_free(parangesrel);
1814 /* build pubnames section */
1815 ppubnames = saa_init(1L);
1816 saa_write16(ppubnames,3); /* dwarf version */
1817 saa_write32(ppubnames,0); /* offset into info */
1818 saa_write32(ppubnames,0); /* space used in info */
1819 saa_write32(ppubnames,0); /* end of list */
1820 saalen = ppubnames->datalen;
1821 pubnameslen = saalen + 4;
1822 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1823 WRITELONG(pbuf,saalen); /* initial length */
1824 saa_rnbytes(ppubnames, pbuf, saalen);
1825 saa_free(ppubnames);
1827 /* build info section */
1828 pinfo = saa_init(1L);
1829 pinforel = saa_init(1L);
1830 saa_write16(pinfo,2); /* dwarf version */
1831 saa_write32(pinforel, pinfo->datalen + 4);
1832 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1833 saa_write32(pinforel, 0);
1834 saa_write32(pinfo,0); /* offset into abbrev */
1835 saa_write8(pinfo,4); /* pointer size */
1836 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1837 saa_write32(pinforel, pinfo->datalen + 4);
1838 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1839 saa_write32(pinforel, 0);
1840 saa_write32(pinfo,0); /* DW_AT_low_pc */
1841 saa_write32(pinforel, pinfo->datalen + 4);
1842 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1843 saa_write32(pinforel, 0);
1844 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1845 saa_write32(pinforel, pinfo->datalen + 4);
1846 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1847 saa_write32(pinforel, 0);
1848 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1849 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1850 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1851 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1852 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1853 saa_write32(pinforel, pinfo->datalen + 4);
1854 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1855 saa_write32(pinforel, 0);
1856 saa_write32(pinfo,0); /* DW_AT_low_pc */
1857 saa_write32(pinfo,0); /* DW_AT_frame_base */
1858 saa_write8(pinfo,0); /* end of entries */
1859 saalen = pinfo->datalen;
1860 infolen = saalen + 4;
1861 infobuf = pbuf = nasm_malloc(infolen);
1862 WRITELONG(pbuf,saalen); /* initial length */
1863 saa_rnbytes(pinfo, pbuf, saalen);
1864 saa_free(pinfo);
1866 /* build rela.info section */
1867 inforellen = saalen = pinforel->datalen;
1868 inforelbuf = pbuf = nasm_malloc(inforellen);
1869 saa_rnbytes(pinforel, pbuf, saalen);
1870 saa_free(pinforel);
1872 /* build abbrev section */
1873 pabbrev = saa_init(1L);
1874 saa_write8(pabbrev,1); /* entry number LEB128u */
1875 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1876 saa_write8(pabbrev,1); /* has children */
1877 /* the following attributes and forms are all LEB128u values */
1878 saa_write8(pabbrev,DW_AT_low_pc);
1879 saa_write8(pabbrev,DW_FORM_addr);
1880 saa_write8(pabbrev,DW_AT_high_pc);
1881 saa_write8(pabbrev,DW_FORM_addr);
1882 saa_write8(pabbrev,DW_AT_stmt_list);
1883 saa_write8(pabbrev,DW_FORM_data4);
1884 saa_write8(pabbrev,DW_AT_name);
1885 saa_write8(pabbrev,DW_FORM_string);
1886 saa_write8(pabbrev,DW_AT_producer);
1887 saa_write8(pabbrev,DW_FORM_string);
1888 saa_write8(pabbrev,DW_AT_language);
1889 saa_write8(pabbrev,DW_FORM_data2);
1890 saa_write16(pabbrev,0); /* end of entry */
1891 /* LEB128u usage same as above */
1892 saa_write8(pabbrev,2); /* entry number */
1893 saa_write8(pabbrev,DW_TAG_subprogram);
1894 saa_write8(pabbrev,0); /* no children */
1895 saa_write8(pabbrev,DW_AT_low_pc);
1896 saa_write8(pabbrev,DW_FORM_addr);
1897 saa_write8(pabbrev,DW_AT_frame_base);
1898 saa_write8(pabbrev,DW_FORM_data4);
1899 saa_write16(pabbrev,0); /* end of entry */
1900 abbrevlen = saalen = pabbrev->datalen;
1901 abbrevbuf = pbuf = nasm_malloc(saalen);
1902 saa_rnbytes(pabbrev, pbuf, saalen);
1903 saa_free(pabbrev);
1905 /* build line section */
1906 /* prolog */
1907 plines = saa_init(1L);
1908 saa_write8(plines,1); /* Minimum Instruction Length */
1909 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1910 saa_write8(plines,line_base); /* Line Base */
1911 saa_write8(plines,line_range); /* Line Range */
1912 saa_write8(plines,opcode_base); /* Opcode Base */
1913 /* standard opcode lengths (# of LEB128u operands) */
1914 saa_write8(plines,0); /* Std opcode 1 length */
1915 saa_write8(plines,1); /* Std opcode 2 length */
1916 saa_write8(plines,1); /* Std opcode 3 length */
1917 saa_write8(plines,1); /* Std opcode 4 length */
1918 saa_write8(plines,1); /* Std opcode 5 length */
1919 saa_write8(plines,0); /* Std opcode 6 length */
1920 saa_write8(plines,0); /* Std opcode 7 length */
1921 saa_write8(plines,0); /* Std opcode 8 length */
1922 saa_write8(plines,1); /* Std opcode 9 length */
1923 saa_write8(plines,0); /* Std opcode 10 length */
1924 saa_write8(plines,0); /* Std opcode 11 length */
1925 saa_write8(plines,1); /* Std opcode 12 length */
1926 /* Directory Table */
1927 saa_write8(plines,0); /* End of table */
1928 /* File Name Table */
1929 ftentry = dwarf_flist;
1930 for (indx = 0; indx < dwarf_numfiles; indx++) {
1931 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1932 saa_write8(plines,0); /* directory LEB128u */
1933 saa_write8(plines,0); /* time LEB128u */
1934 saa_write8(plines,0); /* size LEB128u */
1935 ftentry = ftentry->next;
1937 saa_write8(plines,0); /* End of table */
1938 linepoff = plines->datalen;
1939 linelen = linepoff + totlen + 10;
1940 linebuf = pbuf = nasm_malloc(linelen);
1941 WRITELONG(pbuf,linelen-4); /* initial length */
1942 WRITESHORT(pbuf,3); /* dwarf version */
1943 WRITELONG(pbuf,linepoff); /* offset to line number program */
1944 /* write line header */
1945 saalen = linepoff;
1946 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1947 pbuf += linepoff;
1948 saa_free(plines);
1949 /* concatonate line program ranges */
1950 linepoff += 13;
1951 plinesrel = saa_init(1L);
1952 psect = dwarf_fsect;
1953 for (indx = 0; indx < dwarf_nsections; indx++) {
1954 saa_write32(plinesrel, linepoff);
1955 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1956 saa_write32(plinesrel, (uint32_t) 0);
1957 plinep = psect->psaa;
1958 saalen = plinep->datalen;
1959 saa_rnbytes(plinep, pbuf, saalen);
1960 pbuf += saalen;
1961 linepoff += saalen;
1962 saa_free(plinep);
1963 /* done with this entry */
1964 psect = psect->next;
1968 /* build rela.lines section */
1969 linerellen =saalen = plinesrel->datalen;
1970 linerelbuf = pbuf = nasm_malloc(linerellen);
1971 saa_rnbytes(plinesrel, pbuf, saalen);
1972 saa_free(plinesrel);
1974 /* build frame section */
1975 framelen = 4;
1976 framebuf = pbuf = nasm_malloc(framelen);
1977 WRITELONG(pbuf,framelen-4); /* initial length */
1979 /* build loc section */
1980 loclen = 16;
1981 locbuf = pbuf = nasm_malloc(loclen);
1982 WRITELONG(pbuf,0); /* null beginning offset */
1983 WRITELONG(pbuf,0); /* null ending offset */
1986 static void dwarf32_cleanup(void)
1988 nasm_free(arangesbuf);
1989 nasm_free(arangesrelbuf);
1990 nasm_free(pubnamesbuf);
1991 nasm_free(infobuf);
1992 nasm_free(inforelbuf);
1993 nasm_free(abbrevbuf);
1994 nasm_free(linebuf);
1995 nasm_free(linerelbuf);
1996 nasm_free(framebuf);
1997 nasm_free(locbuf);
2000 static void dwarf32_findfile(const char * fname)
2002 int finx;
2003 struct linelist *match;
2005 /* return if fname is current file name */
2006 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2007 return;
2009 /* search for match */
2010 match = 0;
2011 if (dwarf_flist) {
2012 match = dwarf_flist;
2013 for (finx = 0; finx < dwarf_numfiles; finx++) {
2014 if (!(strcmp(fname, match->filename))) {
2015 dwarf_clist = match;
2016 return;
2021 /* add file name to end of list */
2022 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2023 dwarf_numfiles++;
2024 dwarf_clist->line = dwarf_numfiles;
2025 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2026 strcpy(dwarf_clist->filename,fname);
2027 dwarf_clist->next = 0;
2028 if (!dwarf_flist) { /* if first entry */
2029 dwarf_flist = dwarf_elist = dwarf_clist;
2030 dwarf_clist->last = 0;
2031 } else { /* chain to previous entry */
2032 dwarf_elist->next = dwarf_clist;
2033 dwarf_elist = dwarf_clist;
2037 static void dwarf32_findsect(const int index)
2039 int sinx;
2040 struct sectlist *match;
2041 struct SAA *plinep;
2043 /* return if index is current section index */
2044 if (dwarf_csect && (dwarf_csect->section == index))
2045 return;
2047 /* search for match */
2048 match = 0;
2049 if (dwarf_fsect) {
2050 match = dwarf_fsect;
2051 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2052 if (match->section == index) {
2053 dwarf_csect = match;
2054 return;
2056 match = match->next;
2060 /* add entry to end of list */
2061 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2062 dwarf_nsections++;
2063 dwarf_csect->psaa = plinep = saa_init(1L);
2064 dwarf_csect->line = 1;
2065 dwarf_csect->offset = 0;
2066 dwarf_csect->file = 1;
2067 dwarf_csect->section = index;
2068 dwarf_csect->next = 0;
2069 /* set relocatable address at start of line program */
2070 saa_write8(plinep,DW_LNS_extended_op);
2071 saa_write8(plinep,5); /* operand length */
2072 saa_write8(plinep,DW_LNE_set_address);
2073 saa_write32(plinep,0); /* Start Address */
2075 if (!dwarf_fsect) { /* if first entry */
2076 dwarf_fsect = dwarf_esect = dwarf_csect;
2077 dwarf_csect->last = 0;
2078 } else { /* chain to previous entry */
2079 dwarf_esect->next = dwarf_csect;
2080 dwarf_esect = dwarf_csect;
2084 #endif /* OF_ELF */