insns: add OBSOLETE flag for instructions removed from architecture
[nasm.git] / output / outelf32.c
blobb1596c9056f9068e9abc0d7989b5ed1845422b44
1 /* ----------------------------------------------------------------------- *
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.
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);
163 /* stabs debugging routines */
164 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
165 static void stabs32_output(int, void *);
166 static void stabs32_generate(void);
167 static void stabs32_cleanup(void);
169 /* dwarf debugging routines */
170 static void dwarf32_init(void);
171 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
172 static void dwarf32_output(int, void *);
173 static void dwarf32_generate(void);
174 static void dwarf32_cleanup(void);
175 static void dwarf32_findfile(const char *);
176 static void dwarf32_findsect(const int);
179 * Special NASM section numbers which are used to define ELF special
180 * symbols, which can be used with WRT to provide PIC and TLS
181 * relocation types.
183 static int32_t elf_gotpc_sect, elf_gotoff_sect;
184 static int32_t elf_got_sect, elf_plt_sect;
185 static int32_t elf_sym_sect, elf_tlsie_sect;
187 static void elf_init(void)
189 sects = NULL;
190 nsects = sectlen = 0;
191 syms = saa_init((int32_t)sizeof(struct elf_symbol));
192 nlocals = nglobs = ndebugs = 0;
193 bsym = raa_init();
194 strs = saa_init(1L);
195 saa_wbytes(strs, "\0", 1L);
196 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
197 strslen = 2 + strlen(elf_module);
198 shstrtab = NULL;
199 shstrtablen = shstrtabsize = 0;;
200 add_sectname("", "");
202 fwds = NULL;
204 elf_gotpc_sect = seg_alloc();
205 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
206 elf_gotoff_sect = seg_alloc();
207 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
208 elf_got_sect = seg_alloc();
209 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
210 elf_plt_sect = seg_alloc();
211 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
212 elf_sym_sect = seg_alloc();
213 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
214 elf_tlsie_sect = seg_alloc();
215 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
217 def_seg = seg_alloc();
220 static void elf_cleanup(void)
222 struct elf_reloc *r;
223 int i;
225 elf_write();
226 for (i = 0; i < nsects; i++) {
227 if (sects[i]->type != SHT_NOBITS)
228 saa_free(sects[i]->data);
229 if (sects[i]->head)
230 saa_free(sects[i]->rel);
231 while (sects[i]->head) {
232 r = sects[i]->head;
233 sects[i]->head = sects[i]->head->next;
234 nasm_free(r);
237 nasm_free(sects);
238 saa_free(syms);
239 raa_free(bsym);
240 saa_free(strs);
241 dfmt->cleanup();
244 static void add_sectname(char *firsthalf, char *secondhalf)
246 int len = strlen(firsthalf) + strlen(secondhalf);
247 while (shstrtablen + len + 1 > shstrtabsize)
248 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
249 strcpy(shstrtab + shstrtablen, firsthalf);
250 strcat(shstrtab + shstrtablen, secondhalf);
251 shstrtablen += len + 1;
254 static int elf_make_section(char *name, int type, int flags, int align)
256 struct elf_section *s;
258 s = nasm_zalloc(sizeof(*s));
260 if (type != SHT_NOBITS)
261 s->data = saa_init(1L);
262 s->tail = &s->head;
263 if (!strcmp(name, ".text"))
264 s->index = def_seg;
265 else
266 s->index = seg_alloc();
267 add_sectname("", name);
269 s->name = nasm_strdup(name);
270 s->type = type;
271 s->flags = flags;
272 s->align = align;
274 if (nsects >= sectlen)
275 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
276 sects[nsects++] = s;
278 return nsects - 1;
281 static int32_t elf_section_names(char *name, int pass, int *bits)
283 char *p;
284 uint32_t flags, flags_and, flags_or;
285 uint64_t align;
286 int type, i;
289 * Default is 32 bits.
291 if (!name) {
292 *bits = 32;
293 return def_seg;
296 p = nasm_skip_word(name);
297 if (*p)
298 *p++ = '\0';
299 flags_and = flags_or = type = align = 0;
301 elf_section_attrib(name, p, pass, &flags_and,
302 &flags_or, &align, &type);
304 if (!strcmp(name, ".shstrtab") ||
305 !strcmp(name, ".symtab") ||
306 !strcmp(name, ".strtab")) {
307 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
308 "name `%s'", name);
309 return NO_SEG;
312 for (i = 0; i < nsects; i++)
313 if (!strcmp(name, sects[i]->name))
314 break;
315 if (i == nsects) {
316 const struct elf_known_section *ks = elf_known_sections;
318 while (ks->name) {
319 if (!strcmp(name, ks->name))
320 break;
321 ks++;
324 type = type ? type : ks->type;
325 align = align ? align : ks->align;
326 flags = (ks->flags & ~flags_and) | flags_or;
328 i = elf_make_section(name, type, flags, align);
329 } else if (pass == 1) {
330 if ((type && sects[i]->type != type)
331 || (align && sects[i]->align != align)
332 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
333 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
334 " redeclaration of section `%s'", name);
337 return sects[i]->index;
340 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
341 int is_global, char *special)
343 int pos = strslen;
344 struct elf_symbol *sym;
345 bool special_used = false;
347 #if defined(DEBUG) && DEBUG>2
348 nasm_error(ERR_DEBUG,
349 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
350 name, segment, offset, is_global, special);
351 #endif
352 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
354 * This is a NASM special symbol. We never allow it into
355 * the ELF symbol table, even if it's a valid one. If it
356 * _isn't_ a valid one, we should barf immediately.
358 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
359 strcmp(name, "..got") && strcmp(name, "..plt") &&
360 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
361 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
362 return;
365 if (is_global == 3) {
366 struct elf_symbol **s;
368 * Fix up a forward-reference symbol size from the first
369 * pass.
371 for (s = &fwds; *s; s = &(*s)->nextfwd)
372 if (!strcmp((*s)->name, name)) {
373 struct tokenval tokval;
374 expr *e;
375 char *p = nasm_skip_spaces(nasm_skip_word(special));
377 stdscan_reset();
378 stdscan_set(p);
379 tokval.t_type = TOKEN_INVALID;
380 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
381 if (e) {
382 if (!is_simple(e))
383 nasm_error(ERR_NONFATAL, "cannot use relocatable"
384 " expression as symbol size");
385 else
386 (*s)->size = reloc_value(e);
390 * Remove it from the list of unresolved sizes.
392 nasm_free((*s)->name);
393 *s = (*s)->nextfwd;
394 return;
396 return; /* it wasn't an important one */
399 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
400 strslen += 1 + strlen(name);
402 lastsym = sym = saa_wstruct(syms);
404 memset(&sym->symv, 0, sizeof(struct rbtree));
406 sym->strpos = pos;
407 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
408 sym->other = STV_DEFAULT;
409 sym->size = 0;
410 if (segment == NO_SEG)
411 sym->section = SHN_ABS;
412 else {
413 int i;
414 sym->section = SHN_UNDEF;
415 if (segment == def_seg) {
416 /* we have to be sure at least text section is there */
417 int tempint;
418 if (segment != elf_section_names(".text", 2, &tempint))
419 nasm_panic(0, "strange segment conditions in ELF driver");
421 for (i = 0; i < nsects; i++) {
422 if (segment == sects[i]->index) {
423 sym->section = i + 1;
424 break;
429 if (is_global == 2) {
430 sym->size = offset;
431 sym->symv.key = 0;
432 sym->section = SHN_COMMON;
434 * We have a common variable. Check the special text to see
435 * if it's a valid number and power of two; if so, store it
436 * as the alignment for the common variable.
438 if (special) {
439 bool err;
440 sym->symv.key = readnum(special, &err);
441 if (err)
442 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
443 " valid number", special);
444 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
445 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
446 " power of two", special);
448 special_used = true;
449 } else
450 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
452 if (sym->type == SYM_GLOBAL) {
454 * If sym->section == SHN_ABS, then the first line of the
455 * else section would cause a core dump, because its a reference
456 * beyond the end of the section array.
457 * This behaviour is exhibited by this code:
458 * GLOBAL crash_nasm
459 * crash_nasm equ 0
460 * To avoid such a crash, such requests are silently discarded.
461 * This may not be the best solution.
463 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
464 bsym = raa_write(bsym, segment, nglobs);
465 } else if (sym->section != SHN_ABS) {
467 * This is a global symbol; so we must add it to the rbtree
468 * of global symbols in its section.
470 * In addition, we check the special text for symbol
471 * type and size information.
473 sects[sym->section-1]->gsyms =
474 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
476 if (special) {
477 int n = strcspn(special, " \t");
479 if (!nasm_strnicmp(special, "function", n))
480 sym->type |= STT_FUNC;
481 else if (!nasm_strnicmp(special, "data", n) ||
482 !nasm_strnicmp(special, "object", n))
483 sym->type |= STT_OBJECT;
484 else if (!nasm_strnicmp(special, "notype", n))
485 sym->type |= STT_NOTYPE;
486 else
487 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
488 n, special);
489 special += n;
491 special = nasm_skip_spaces(special);
492 if (*special) {
493 n = strcspn(special, " \t");
494 if (!nasm_strnicmp(special, "default", n))
495 sym->other = STV_DEFAULT;
496 else if (!nasm_strnicmp(special, "internal", n))
497 sym->other = STV_INTERNAL;
498 else if (!nasm_strnicmp(special, "hidden", n))
499 sym->other = STV_HIDDEN;
500 else if (!nasm_strnicmp(special, "protected", n))
501 sym->other = STV_PROTECTED;
502 else
503 n = 0;
504 special += n;
507 if (*special) {
508 struct tokenval tokval;
509 expr *e;
510 int fwd = 0;
511 char *saveme = stdscan_get();
513 while (special[n] && nasm_isspace(special[n]))
514 n++;
516 * We have a size expression; attempt to
517 * evaluate it.
519 stdscan_reset();
520 stdscan_set(special + n);
521 tokval.t_type = TOKEN_INVALID;
522 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
523 if (fwd) {
524 sym->nextfwd = fwds;
525 fwds = sym;
526 sym->name = nasm_strdup(name);
527 } else if (e) {
528 if (!is_simple(e))
529 nasm_error(ERR_NONFATAL, "cannot use relocatable"
530 " expression as symbol size");
531 else
532 sym->size = reloc_value(e);
534 stdscan_set(saveme);
536 special_used = true;
539 * If TLS segment, mark symbol accordingly.
541 if (sects[sym->section - 1]->flags & SHF_TLS) {
542 sym->type &= 0xf0;
543 sym->type |= STT_TLS;
546 sym->globnum = nglobs;
547 nglobs++;
548 } else
549 nlocals++;
551 if (special && !special_used)
552 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
555 static void elf_add_reloc(struct elf_section *sect, int32_t segment, int type)
557 struct elf_reloc *r;
559 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
560 sect->tail = &r->next;
562 r->address = sect->len;
563 if (segment != NO_SEG) {
564 int i;
565 for (i = 0; i < nsects; i++)
566 if (segment == sects[i]->index)
567 r->symbol = i + 2;
568 if (!r->symbol)
569 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
571 r->type = type;
573 sect->nrelocs++;
577 * This routine deals with ..got and ..sym relocations: the more
578 * complicated kinds. In shared-library writing, some relocations
579 * with respect to global symbols must refer to the precise symbol
580 * rather than referring to an offset from the base of the section
581 * _containing_ the symbol. Such relocations call to this routine,
582 * which searches the symbol list for the symbol in question.
584 * R_386_GOT32 references require the _exact_ symbol address to be
585 * used; R_386_32 references can be at an offset from the symbol.
586 * The boolean argument `exact' tells us this.
588 * Return value is the adjusted value of `addr', having become an
589 * offset from the symbol rather than the section. Should always be
590 * zero when returning from an exact call.
592 * Limitation: if you define two symbols at the same place,
593 * confusion will occur.
595 * Inefficiency: we search, currently, using a linked list which
596 * isn't even necessarily sorted.
598 static int32_t elf_add_gsym_reloc(struct elf_section *sect,
599 int32_t segment, uint32_t offset,
600 int type, bool exact)
602 struct elf_reloc *r;
603 struct elf_section *s;
604 struct elf_symbol *sym;
605 struct rbtree *srb;
606 int i;
609 * First look up the segment/offset pair and find a global
610 * symbol corresponding to it. If it's not one of our segments,
611 * then it must be an external symbol, in which case we're fine
612 * doing a normal elf_add_reloc after first sanity-checking
613 * that the offset from the symbol is zero.
615 s = NULL;
616 for (i = 0; i < nsects; i++)
617 if (segment == sects[i]->index) {
618 s = sects[i];
619 break;
622 if (!s) {
623 if (exact && offset)
624 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
625 " for this reference");
626 else
627 elf_add_reloc(sect, segment, type);
628 return offset;
631 srb = rb_search(s->gsyms, offset);
632 if (!srb || (exact && srb->key != offset)) {
633 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
634 " for this reference");
635 return 0;
637 sym = container_of(srb, struct elf_symbol, symv);
639 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
640 sect->tail = &r->next;
642 r->next = NULL;
643 r->address = sect->len;
644 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
645 r->type = type;
647 sect->nrelocs++;
649 return offset - sym->symv.key;
652 static void elf_out(int32_t segto, const void *data,
653 enum out_type type, uint64_t size,
654 int32_t segment, int32_t wrt)
656 struct elf_section *s;
657 int32_t addr;
658 uint8_t mydata[8], *p;
659 int reltype, bytes;
660 int i;
661 static struct symlininfo sinfo;
664 * handle absolute-assembly (structure definitions)
666 if (segto == NO_SEG) {
667 if (type != OUT_RESERVE)
668 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
669 " space");
670 return;
673 s = NULL;
674 for (i = 0; i < nsects; i++)
675 if (segto == sects[i]->index) {
676 s = sects[i];
677 break;
679 if (!s) {
680 int tempint; /* ignored */
681 if (segto != elf_section_names(".text", 2, &tempint))
682 nasm_panic(0, "strange segment conditions in ELF driver");
683 else {
684 s = sects[nsects - 1];
685 i = nsects - 1;
689 /* again some stabs debugging stuff */
690 sinfo.offset = s->len;
691 sinfo.section = i;
692 sinfo.name = s->name;
693 dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
694 /* end of debugging stuff */
696 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
697 nasm_error(ERR_WARNING, "attempt to initialize memory in"
698 " BSS section `%s': ignored", s->name);
699 s->len += realsize(type, size);
700 return;
703 memset(mydata, 0, sizeof(mydata));
705 switch (type) {
706 case OUT_RESERVE:
707 if (s->type == SHT_PROGBITS) {
708 nasm_error(ERR_WARNING, "uninitialized space declared in"
709 " non-BSS section `%s': zeroing", s->name);
710 elf_sect_write(s, NULL, size);
711 } else
712 s->len += size;
713 break;
715 case OUT_RAWDATA:
716 if (segment != NO_SEG)
717 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
718 elf_sect_write(s, data, size);
719 break;
721 case OUT_ADDRESS:
723 bool gnu16 = false;
724 int asize = abs((int)size);
725 addr = *(int64_t *)data;
726 if (segment != NO_SEG) {
727 if (segment % 2) {
728 nasm_error(ERR_NONFATAL, "ELF format does not support"
729 " segment base references");
730 } else {
731 if (wrt == NO_SEG) {
733 * The if() is a hack to deal with compilers which
734 * don't handle switch() statements with 64-bit
735 * expressions.
737 switch (asize) {
738 case 1:
739 gnu16 = true;
740 elf_add_reloc(s, segment, R_386_8);
741 break;
742 case 2:
743 gnu16 = true;
744 elf_add_reloc(s, segment, R_386_16);
745 break;
746 case 4:
747 elf_add_reloc(s, segment, R_386_32);
748 break;
749 default: /* Error issued further down */
750 break;
752 } else if (wrt == elf_gotpc_sect + 1) {
754 * The user will supply GOT relative to $$. ELF
755 * will let us have GOT relative to $. So we
756 * need to fix up the data item by $-$$.
758 addr += s->len;
759 elf_add_reloc(s, segment, R_386_GOTPC);
760 } else if (wrt == elf_gotoff_sect + 1) {
761 elf_add_reloc(s, segment, R_386_GOTOFF);
762 } else if (wrt == elf_tlsie_sect + 1) {
763 addr = elf_add_gsym_reloc(s, segment, addr,
764 R_386_TLS_IE, true);
765 } else if (wrt == elf_got_sect + 1) {
766 addr = elf_add_gsym_reloc(s, segment, addr,
767 R_386_GOT32, true);
768 } else if (wrt == elf_sym_sect + 1) {
769 switch (asize) {
770 case 1:
771 gnu16 = true;
772 addr = elf_add_gsym_reloc(s, segment, addr,
773 R_386_8, false);
774 break;
775 case 2:
776 gnu16 = true;
777 addr = elf_add_gsym_reloc(s, segment, addr,
778 R_386_16, false);
779 break;
780 case 4:
781 addr = elf_add_gsym_reloc(s, segment, addr,
782 R_386_32, false);
783 break;
784 default:
785 break;
787 } else if (wrt == elf_plt_sect + 1) {
788 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
789 "relative PLT references");
790 } else {
791 nasm_error(ERR_NONFATAL, "ELF format does not support this"
792 " use of WRT");
793 wrt = NO_SEG; /* we can at least _try_ to continue */
797 p = mydata;
798 if (gnu16) {
799 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
800 "8- or 16-bit relocations in ELF32 is a GNU extension");
801 } else if (asize != 4 && segment != NO_SEG) {
802 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
804 WRITEADDR(p, addr, asize);
805 elf_sect_write(s, mydata, asize);
806 break;
809 case OUT_REL1ADR:
810 bytes = 1;
811 reltype = R_386_PC8;
812 goto rel12adr;
813 case OUT_REL2ADR:
814 bytes = 2;
815 reltype = R_386_PC16;
816 goto rel12adr;
818 rel12adr:
819 nasm_assert(segment != segto);
820 if (segment != NO_SEG && segment % 2) {
821 nasm_error(ERR_NONFATAL, "ELF format does not support"
822 " segment base references");
823 } else {
824 if (wrt == NO_SEG) {
825 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
826 "8- or 16-bit relocations in ELF is a GNU extension");
827 elf_add_reloc(s, segment, reltype);
828 } else {
829 nasm_error(ERR_NONFATAL,
830 "Unsupported non-32-bit ELF relocation");
833 p = mydata;
834 WRITESHORT(p, *(int64_t *)data - size);
835 elf_sect_write(s, mydata, bytes);
836 break;
838 case OUT_REL4ADR:
839 if (segment == segto)
840 nasm_panic(0, "intra-segment OUT_REL4ADR");
841 if (segment != NO_SEG && segment % 2) {
842 nasm_error(ERR_NONFATAL, "ELF format does not support"
843 " segment base references");
844 } else {
845 if (wrt == NO_SEG) {
846 elf_add_reloc(s, segment, R_386_PC32);
847 } else if (wrt == elf_plt_sect + 1) {
848 elf_add_reloc(s, segment, R_386_PLT32);
849 } else if (wrt == elf_gotpc_sect + 1 ||
850 wrt == elf_gotoff_sect + 1 ||
851 wrt == elf_got_sect + 1) {
852 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
853 "relative GOT references");
854 } else {
855 nasm_error(ERR_NONFATAL, "ELF format does not support this"
856 " use of WRT");
857 wrt = NO_SEG; /* we can at least _try_ to continue */
860 p = mydata;
861 WRITELONG(p, *(int64_t *)data - size);
862 elf_sect_write(s, mydata, 4L);
863 break;
865 case OUT_REL8ADR:
866 nasm_error(ERR_NONFATAL,
867 "32-bit ELF format does not support 64-bit relocations");
868 p = mydata;
869 WRITEDLONG(p, 0);
870 elf_sect_write(s, mydata, 8L);
871 break;
875 static void elf_write(void)
877 int align;
878 char *p;
879 int i;
881 struct SAA *symtab;
882 int32_t symtablen, symtablocal;
885 * Work out how many sections we will have. We have SHN_UNDEF,
886 * then the flexible user sections, then the fixed sections
887 * `.shstrtab', `.symtab' and `.strtab', then optionally
888 * relocation sections for the user sections.
890 nsections = sec_numspecial + 1;
891 if (dfmt == &df_stabs)
892 nsections += 3;
893 else if (dfmt == &df_dwarf)
894 nsections += 10;
896 add_sectname("", ".shstrtab");
897 add_sectname("", ".symtab");
898 add_sectname("", ".strtab");
899 for (i = 0; i < nsects; i++) {
900 nsections++; /* for the section itself */
901 if (sects[i]->head) {
902 nsections++; /* for its relocations */
903 add_sectname(".rel", sects[i]->name);
907 if (dfmt == &df_stabs) {
908 /* in case the debug information is wanted, just add these three sections... */
909 add_sectname("", ".stab");
910 add_sectname("", ".stabstr");
911 add_sectname(".rel", ".stab");
912 } else if (dfmt == &df_dwarf) {
913 /* the dwarf debug standard specifies the following ten sections,
914 not all of which are currently implemented,
915 although all of them are defined. */
916 add_sectname("", ".debug_aranges");
917 add_sectname(".rela", ".debug_aranges");
918 add_sectname("", ".debug_pubnames");
919 add_sectname("", ".debug_info");
920 add_sectname(".rela", ".debug_info");
921 add_sectname("", ".debug_abbrev");
922 add_sectname("", ".debug_line");
923 add_sectname(".rela", ".debug_line");
924 add_sectname("", ".debug_frame");
925 add_sectname("", ".debug_loc");
929 * Output the ELF header.
931 nasm_write("\177ELF\1\1\1", 7, ofile);
932 fputc(elf_osabi, ofile);
933 fputc(elf_abiver, ofile);
934 fwritezero(7, ofile);
935 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
936 fwriteint16_t(3, ofile); /* EM_386 processor ID */
937 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
938 fwriteint32_t(0L, ofile); /* no entry point */
939 fwriteint32_t(0L, ofile); /* no program header table */
940 fwriteint32_t(0x40L, ofile); /* section headers straight after
941 * ELF header plus alignment */
942 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
943 fwriteint16_t(0x34, ofile); /* size of ELF header */
944 fwriteint16_t(0, ofile); /* no program header table, again */
945 fwriteint16_t(0, ofile); /* still no program header table */
946 fwriteint16_t(0x28, ofile); /* size of section header */
947 fwriteint16_t(nsections, ofile); /* number of sections */
948 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
949 * section header table */
950 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
951 fwriteint32_t(0L, ofile);
952 fwriteint32_t(0L, ofile);
955 * Build the symbol table and relocation tables.
957 symtab = elf_build_symtab(&symtablen, &symtablocal);
958 for (i = 0; i < nsects; i++)
959 if (sects[i]->head)
960 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
961 sects[i]->head);
964 * Now output the section header table.
967 elf_foffs = 0x40 + 0x28 * nsections;
968 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
969 elf_foffs += align;
970 elf_nsect = 0;
971 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
973 /* SHN_UNDEF */
974 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
975 p = shstrtab + 1;
977 /* The normal sections */
978 for (i = 0; i < nsects; i++) {
979 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
980 (sects[i]->type == SHT_PROGBITS ?
981 sects[i]->data : NULL), true,
982 sects[i]->len, 0, 0, sects[i]->align, 0);
983 p += strlen(p) + 1;
986 /* .shstrtab */
987 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
988 shstrtablen, 0, 0, 1, 0);
989 p += strlen(p) + 1;
991 /* .symtab */
992 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
993 symtablen, sec_strtab, symtablocal, 4, 16);
994 p += strlen(p) + 1;
996 /* .strtab */
997 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
998 strslen, 0, 0, 1, 0);
999 p += strlen(p) + 1;
1001 /* The relocation sections */
1002 for (i = 0; i < nsects; i++)
1003 if (sects[i]->head) {
1004 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1005 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1006 p += strlen(p) + 1;
1009 if (dfmt == &df_stabs) {
1010 /* for debugging information, create the last three sections
1011 which are the .stab , .stabstr and .rel.stab sections respectively */
1013 /* this function call creates the stab sections in memory */
1014 stabs32_generate();
1016 if (stabbuf && stabstrbuf && stabrelbuf) {
1017 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1018 stablen, sec_stabstr, 0, 4, 12);
1019 p += strlen(p) + 1;
1021 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1022 stabstrlen, 0, 0, 4, 0);
1023 p += strlen(p) + 1;
1025 /* link -> symtable info -> section to refer to */
1026 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1027 stabrellen, sec_symtab, sec_stab, 4, 8);
1028 p += strlen(p) + 1;
1030 } else if (dfmt == &df_dwarf) {
1031 /* for dwarf debugging information, create the ten dwarf sections */
1033 /* this function call creates the dwarf sections in memory */
1034 if (dwarf_fsect)
1035 dwarf32_generate();
1037 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1038 arangeslen, 0, 0, 1, 0);
1039 p += strlen(p) + 1;
1041 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1042 arangesrellen, sec_symtab, sec_debug_aranges,
1043 1, 12);
1044 p += strlen(p) + 1;
1046 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1047 false, pubnameslen, 0, 0, 1, 0);
1048 p += strlen(p) + 1;
1050 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1051 infolen, 0, 0, 1, 0);
1052 p += strlen(p) + 1;
1054 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1055 inforellen, sec_symtab, sec_debug_info, 1, 12);
1056 p += strlen(p) + 1;
1058 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1059 abbrevlen, 0, 0, 1, 0);
1060 p += strlen(p) + 1;
1062 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1063 linelen, 0, 0, 1, 0);
1064 p += strlen(p) + 1;
1066 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1067 linerellen, sec_symtab, sec_debug_line, 1, 12);
1068 p += strlen(p) + 1;
1070 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1071 framelen, 0, 0, 8, 0);
1072 p += strlen(p) + 1;
1074 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1075 loclen, 0, 0, 1, 0);
1076 p += strlen(p) + 1;
1078 fwritezero(align, ofile);
1081 * Now output the sections.
1083 elf_write_sections();
1085 nasm_free(elf_sects);
1086 saa_free(symtab);
1089 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1091 struct SAA *s = saa_init(1L);
1092 struct elf_symbol *sym;
1093 uint8_t entry[16], *p;
1094 int i;
1096 *len = *local = 0;
1099 * First, an all-zeros entry, required by the ELF spec.
1101 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1102 *len += 16;
1103 (*local)++;
1106 * Next, an entry for the file name.
1108 p = entry;
1109 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1110 WRITELONG(p, 0); /* no value */
1111 WRITELONG(p, 0); /* no size either */
1112 WRITESHORT(p, STT_FILE); /* type FILE */
1113 WRITESHORT(p, SHN_ABS);
1114 saa_wbytes(s, entry, 16L);
1115 *len += 16;
1116 (*local)++;
1119 * Now some standard symbols defining the segments, for relocation
1120 * purposes.
1122 for (i = 1; i <= nsects; i++) {
1123 p = entry;
1124 WRITELONG(p, 0); /* no symbol name */
1125 WRITELONG(p, 0); /* offset zero */
1126 WRITELONG(p, 0); /* size zero */
1127 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1128 WRITESHORT(p, i); /* section id */
1129 saa_wbytes(s, entry, 16L);
1130 *len += 16;
1131 (*local)++;
1135 * Now the other local symbols.
1137 saa_rewind(syms);
1138 while ((sym = saa_rstruct(syms))) {
1139 if (sym->type & SYM_GLOBAL)
1140 continue;
1141 p = entry;
1142 WRITELONG(p, sym->strpos);
1143 WRITELONG(p, sym->symv.key);
1144 WRITELONG(p, sym->size);
1145 WRITECHAR(p, sym->type); /* type and binding */
1146 WRITECHAR(p, sym->other); /* visibility */
1147 WRITESHORT(p, sym->section);
1148 saa_wbytes(s, entry, 16L);
1149 *len += 16;
1150 (*local)++;
1153 * dwarf needs symbols for debug sections
1154 * which are relocation targets.
1156 /*** fix for 32 bit ***/
1157 if (dfmt == &df_dwarf) {
1158 dwarf_infosym = *local;
1159 p = entry;
1160 WRITELONG(p, 0); /* no symbol name */
1161 WRITELONG(p, (uint32_t) 0); /* offset zero */
1162 WRITELONG(p, (uint32_t) 0); /* size zero */
1163 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1164 WRITESHORT(p, sec_debug_info); /* section id */
1165 saa_wbytes(s, entry, 16L);
1166 *len += 16;
1167 (*local)++;
1168 dwarf_abbrevsym = *local;
1169 p = entry;
1170 WRITELONG(p, 0); /* no symbol name */
1171 WRITELONG(p, (uint32_t) 0); /* offset zero */
1172 WRITELONG(p, (uint32_t) 0); /* size zero */
1173 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1174 WRITESHORT(p, sec_debug_abbrev); /* section id */
1175 saa_wbytes(s, entry, 16L);
1176 *len += 16;
1177 (*local)++;
1178 dwarf_linesym = *local;
1179 p = entry;
1180 WRITELONG(p, 0); /* no symbol name */
1181 WRITELONG(p, (uint32_t) 0); /* offset zero */
1182 WRITELONG(p, (uint32_t) 0); /* size zero */
1183 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1184 WRITESHORT(p, sec_debug_line); /* section id */
1185 saa_wbytes(s, entry, 16L);
1186 *len += 16;
1187 (*local)++;
1191 * Now the global symbols.
1193 saa_rewind(syms);
1194 while ((sym = saa_rstruct(syms))) {
1195 if (!(sym->type & SYM_GLOBAL))
1196 continue;
1197 p = entry;
1198 WRITELONG(p, sym->strpos);
1199 WRITELONG(p, sym->symv.key);
1200 WRITELONG(p, sym->size);
1201 WRITECHAR(p, sym->type); /* type and binding */
1202 WRITECHAR(p, sym->other); /* visibility */
1203 WRITESHORT(p, sym->section);
1204 saa_wbytes(s, entry, 16L);
1205 *len += 16;
1208 return s;
1211 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1213 struct SAA *s;
1214 uint8_t *p, entry[8];
1215 int32_t global_offset;
1217 if (!r)
1218 return NULL;
1220 s = saa_init(1L);
1221 *len = 0;
1224 * How to onvert from a global placeholder to a real symbol index;
1225 * the +2 refers to the two special entries, the null entry and
1226 * the filename entry.
1228 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1230 while (r) {
1231 int32_t sym = r->symbol;
1233 if (sym >= GLOBAL_TEMP_BASE)
1234 sym += global_offset;
1236 p = entry;
1237 WRITELONG(p, r->address);
1238 WRITELONG(p, (sym << 8) + r->type);
1239 saa_wbytes(s, entry, 8L);
1240 *len += 8;
1242 r = r->next;
1245 return s;
1248 static void elf_section_header(int name, int type, int flags,
1249 void *data, bool is_saa, int32_t datalen,
1250 int link, int info, int align, int eltsize)
1252 elf_sects[elf_nsect].data = data;
1253 elf_sects[elf_nsect].len = datalen;
1254 elf_sects[elf_nsect].is_saa = is_saa;
1255 elf_nsect++;
1257 fwriteint32_t((int32_t)name, ofile);
1258 fwriteint32_t((int32_t)type, ofile);
1259 fwriteint32_t((int32_t)flags, ofile);
1260 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1261 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1262 fwriteint32_t(datalen, ofile);
1263 if (data)
1264 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1265 fwriteint32_t((int32_t)link, ofile);
1266 fwriteint32_t((int32_t)info, ofile);
1267 fwriteint32_t((int32_t)align, ofile);
1268 fwriteint32_t((int32_t)eltsize, ofile);
1271 static void elf_write_sections(void)
1273 int i;
1274 for (i = 0; i < elf_nsect; i++)
1275 if (elf_sects[i].data) {
1276 int32_t len = elf_sects[i].len;
1277 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1278 int32_t align = reallen - len;
1279 if (elf_sects[i].is_saa)
1280 saa_fpwrite(elf_sects[i].data, ofile);
1281 else
1282 nasm_write(elf_sects[i].data, len, ofile);
1283 fwritezero(align, ofile);
1287 static void elf_sect_write(struct elf_section *sect,
1288 const uint8_t *data, uint32_t len)
1290 saa_wbytes(sect->data, data, len);
1291 sect->len += len;
1294 static void elf_sectalign(int32_t seg, unsigned int value)
1296 struct elf_section *s = NULL;
1297 int i;
1299 for (i = 0; i < nsects; i++) {
1300 if (sects[i]->index == seg) {
1301 s = sects[i];
1302 break;
1305 if (!s || !is_power2(value))
1306 return;
1308 if (value > s->align)
1309 s->align = value;
1312 static int32_t elf_segbase(int32_t segment)
1314 return segment;
1317 static void elf_filename(char *inname, char *outname)
1319 strcpy(elf_module, inname);
1320 standard_extension(inname, outname, ".o");
1323 extern macros_t elf_stdmac[];
1325 static int elf_set_info(enum geninfo type, char **val)
1327 (void)type;
1328 (void)val;
1329 return 0;
1331 static struct dfmt df_dwarf = {
1332 "ELF32 (i386) dwarf debug format for Linux/Unix",
1333 "dwarf",
1334 dwarf32_init,
1335 dwarf32_linenum,
1336 null_debug_deflabel,
1337 null_debug_directive,
1338 debug32_typevalue,
1339 dwarf32_output,
1340 dwarf32_cleanup
1342 static struct dfmt df_stabs = {
1343 "ELF32 (i386) stabs debug format for Linux/Unix",
1344 "stabs",
1345 null_debug_init,
1346 stabs32_linenum,
1347 null_debug_deflabel,
1348 null_debug_directive,
1349 debug32_typevalue,
1350 stabs32_output,
1351 stabs32_cleanup
1354 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1356 struct ofmt of_elf32 = {
1357 "ELF32 (i386) object files (e.g. Linux)",
1358 "elf32",
1361 elf32_debugs_arr,
1362 &df_stabs,
1363 elf_stdmac,
1364 elf_init,
1365 elf_set_info,
1366 elf_out,
1367 elf_deflabel,
1368 elf_section_names,
1369 elf_sectalign,
1370 elf_segbase,
1371 elf_directive,
1372 elf_filename,
1373 elf_cleanup
1376 /* again, the stabs debugging stuff (code) */
1378 static void stabs32_linenum(const char *filename, int32_t linenumber,
1379 int32_t segto)
1381 (void)segto;
1383 if (!stabs_filename) {
1384 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1385 strcpy(stabs_filename, filename);
1386 } else {
1387 if (strcmp(stabs_filename, filename)) {
1389 * yep, a memory leak...this program is one-shot anyway, so who cares...
1390 * in fact, this leak comes in quite handy to maintain a list of files
1391 * encountered so far in the symbol lines...
1394 /* why not nasm_free(stabs_filename); we're done with the old one */
1396 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1397 strcpy(stabs_filename, filename);
1400 debug_immcall = 1;
1401 currentline = linenumber;
1404 static void debug32_typevalue(int32_t type)
1406 int32_t stype, ssize;
1407 switch (TYM_TYPE(type)) {
1408 case TY_LABEL:
1409 ssize = 0;
1410 stype = STT_NOTYPE;
1411 break;
1412 case TY_BYTE:
1413 ssize = 1;
1414 stype = STT_OBJECT;
1415 break;
1416 case TY_WORD:
1417 ssize = 2;
1418 stype = STT_OBJECT;
1419 break;
1420 case TY_DWORD:
1421 ssize = 4;
1422 stype = STT_OBJECT;
1423 break;
1424 case TY_FLOAT:
1425 ssize = 4;
1426 stype = STT_OBJECT;
1427 break;
1428 case TY_QWORD:
1429 ssize = 8;
1430 stype = STT_OBJECT;
1431 break;
1432 case TY_TBYTE:
1433 ssize = 10;
1434 stype = STT_OBJECT;
1435 break;
1436 case TY_OWORD:
1437 ssize = 16;
1438 stype = STT_OBJECT;
1439 break;
1440 case TY_YWORD:
1441 ssize = 32;
1442 stype = STT_OBJECT;
1443 break;
1444 case TY_COMMON:
1445 ssize = 0;
1446 stype = STT_COMMON;
1447 break;
1448 case TY_SEG:
1449 ssize = 0;
1450 stype = STT_SECTION;
1451 break;
1452 case TY_EXTERN:
1453 ssize = 0;
1454 stype = STT_NOTYPE;
1455 break;
1456 case TY_EQU:
1457 ssize = 0;
1458 stype = STT_NOTYPE;
1459 break;
1460 default:
1461 ssize = 0;
1462 stype = STT_NOTYPE;
1463 break;
1465 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1466 lastsym->size = ssize;
1467 lastsym->type = stype;
1471 static void stabs32_output(int type, void *param)
1473 struct symlininfo *s;
1474 struct linelist *el;
1475 if (type == TY_STABSSYMLIN) {
1476 if (debug_immcall) {
1477 s = (struct symlininfo *)param;
1478 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1479 return; /* line info is only collected for executable sections */
1480 numlinestabs++;
1481 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1482 el->info.offset = s->offset;
1483 el->info.section = s->section;
1484 el->info.name = s->name;
1485 el->line = currentline;
1486 el->filename = stabs_filename;
1487 el->next = 0;
1488 if (stabslines) {
1489 stabslines->last->next = el;
1490 stabslines->last = el;
1491 } else {
1492 stabslines = el;
1493 stabslines->last = el;
1497 debug_immcall = 0;
1500 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1502 static void stabs32_generate(void)
1504 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1505 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1506 char **allfiles;
1507 int *fileidx;
1509 struct linelist *ptr;
1511 ptr = stabslines;
1513 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1514 for (i = 0; i < numlinestabs; i++)
1515 allfiles[i] = 0;
1516 numfiles = 0;
1517 while (ptr) {
1518 if (numfiles == 0) {
1519 allfiles[0] = ptr->filename;
1520 numfiles++;
1521 } else {
1522 for (i = 0; i < numfiles; i++) {
1523 if (!strcmp(allfiles[i], ptr->filename))
1524 break;
1526 if (i >= numfiles) {
1527 allfiles[i] = ptr->filename;
1528 numfiles++;
1531 ptr = ptr->next;
1533 strsize = 1;
1534 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1535 for (i = 0; i < numfiles; i++) {
1536 fileidx[i] = strsize;
1537 strsize += strlen(allfiles[i]) + 1;
1539 mainfileindex = 0;
1540 for (i = 0; i < numfiles; i++) {
1541 if (!strcmp(allfiles[i], elf_module)) {
1542 mainfileindex = i;
1543 break;
1548 * worst case size of the stab buffer would be:
1549 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1550 * plus one "ending" entry
1552 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1553 sizeof(struct stabentry));
1554 ssbuf = (uint8_t *)nasm_malloc(strsize);
1555 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1556 rptr = rbuf;
1558 for (i = 0; i < numfiles; i++)
1559 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1560 ssbuf[0] = 0;
1562 stabstrlen = strsize; /* set global variable for length of stab strings */
1564 sptr = sbuf;
1565 ptr = stabslines;
1566 numstabs = 0;
1568 if (ptr) {
1570 * this is the first stab, its strx points to the filename of the
1571 * the source-file, the n_desc field should be set to the number
1572 * of remaining stabs
1574 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1576 /* this is the stab for the main source file */
1577 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1579 /* relocation table entry */
1582 * Since the symbol table has two entries before
1583 * the section symbols, the index in the info.section
1584 * member must be adjusted by adding 2
1587 WRITELONG(rptr, (sptr - sbuf) - 4);
1588 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1590 numstabs++;
1591 currfile = mainfileindex;
1594 while (ptr) {
1595 if (strcmp(allfiles[currfile], ptr->filename)) {
1596 /* oops file has changed... */
1597 for (i = 0; i < numfiles; i++)
1598 if (!strcmp(allfiles[i], ptr->filename))
1599 break;
1600 currfile = i;
1601 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1602 ptr->info.offset);
1603 numstabs++;
1605 /* relocation table entry */
1606 WRITELONG(rptr, (sptr - sbuf) - 4);
1607 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1610 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1611 numstabs++;
1613 /* relocation table entry */
1615 WRITELONG(rptr, (sptr - sbuf) - 4);
1616 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1618 ptr = ptr->next;
1622 /* this is an "ending" token */
1623 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1624 numstabs++;
1626 ((struct stabentry *)sbuf)->n_desc = numstabs;
1628 nasm_free(allfiles);
1629 nasm_free(fileidx);
1631 stablen = (sptr - sbuf);
1632 stabrellen = (rptr - rbuf);
1633 stabrelbuf = rbuf;
1634 stabbuf = sbuf;
1635 stabstrbuf = ssbuf;
1638 static void stabs32_cleanup(void)
1640 struct linelist *ptr, *del;
1641 if (!stabslines)
1642 return;
1644 ptr = stabslines;
1645 while (ptr) {
1646 del = ptr;
1647 ptr = ptr->next;
1648 nasm_free(del);
1651 nasm_free(stabbuf);
1652 nasm_free(stabrelbuf);
1653 nasm_free(stabstrbuf);
1656 /* dwarf routines */
1658 static void dwarf32_init(void)
1660 ndebugs = 3; /* 3 debug symbols */
1663 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1664 int32_t segto)
1666 (void)segto;
1667 dwarf32_findfile(filename);
1668 debug_immcall = 1;
1669 currentline = linenumber;
1672 /* called from elf_out with type == TY_DEBUGSYMLIN */
1673 static void dwarf32_output(int type, void *param)
1675 int ln, aa, inx, maxln, soc;
1676 struct symlininfo *s;
1677 struct SAA *plinep;
1679 (void)type;
1681 s = (struct symlininfo *)param;
1683 /* line number info is only gathered for executable sections */
1684 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1685 return;
1687 /* Check if section index has changed */
1688 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1689 dwarf32_findsect(s->section);
1691 /* do nothing unless line or file has changed */
1692 if (!debug_immcall)
1693 return;
1695 ln = currentline - dwarf_csect->line;
1696 aa = s->offset - dwarf_csect->offset;
1697 inx = dwarf_clist->line;
1698 plinep = dwarf_csect->psaa;
1699 /* check for file change */
1700 if (!(inx == dwarf_csect->file)) {
1701 saa_write8(plinep,DW_LNS_set_file);
1702 saa_write8(plinep,inx);
1703 dwarf_csect->file = inx;
1705 /* check for line change */
1706 if (ln) {
1707 /* test if in range of special op code */
1708 maxln = line_base + line_range;
1709 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1710 if (ln >= line_base && ln < maxln && soc < 256) {
1711 saa_write8(plinep,soc);
1712 } else {
1713 saa_write8(plinep,DW_LNS_advance_line);
1714 saa_wleb128s(plinep,ln);
1715 if (aa) {
1716 saa_write8(plinep,DW_LNS_advance_pc);
1717 saa_wleb128u(plinep,aa);
1720 dwarf_csect->line = currentline;
1721 dwarf_csect->offset = s->offset;
1724 /* show change handled */
1725 debug_immcall = 0;
1729 static void dwarf32_generate(void)
1731 uint8_t *pbuf;
1732 int indx;
1733 struct linelist *ftentry;
1734 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1735 struct SAA *parangesrel, *plinesrel, *pinforel;
1736 struct sectlist *psect;
1737 size_t saalen, linepoff, totlen, highaddr;
1739 /* write epilogues for each line program range */
1740 /* and build aranges section */
1741 paranges = saa_init(1L);
1742 parangesrel = saa_init(1L);
1743 saa_write16(paranges,2); /* dwarf version */
1744 saa_write32(parangesrel, paranges->datalen+4);
1745 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1746 saa_write32(parangesrel, 0);
1747 saa_write32(paranges,0); /* offset into info */
1748 saa_write8(paranges,4); /* pointer size */
1749 saa_write8(paranges,0); /* not segmented */
1750 saa_write32(paranges,0); /* padding */
1751 /* iterate though sectlist entries */
1752 psect = dwarf_fsect;
1753 totlen = 0;
1754 highaddr = 0;
1755 for (indx = 0; indx < dwarf_nsections; indx++) {
1756 plinep = psect->psaa;
1757 /* Line Number Program Epilogue */
1758 saa_write8(plinep,2); /* std op 2 */
1759 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1760 saa_write8(plinep,DW_LNS_extended_op);
1761 saa_write8(plinep,1); /* operand length */
1762 saa_write8(plinep,DW_LNE_end_sequence);
1763 totlen += plinep->datalen;
1764 /* range table relocation entry */
1765 saa_write32(parangesrel, paranges->datalen + 4);
1766 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1767 saa_write32(parangesrel, (uint32_t) 0);
1768 /* range table entry */
1769 saa_write32(paranges,0x0000); /* range start */
1770 saa_write32(paranges,sects[psect->section]->len); /* range length */
1771 highaddr += sects[psect->section]->len;
1772 /* done with this entry */
1773 psect = psect->next;
1775 saa_write32(paranges,0); /* null address */
1776 saa_write32(paranges,0); /* null length */
1777 saalen = paranges->datalen;
1778 arangeslen = saalen + 4;
1779 arangesbuf = pbuf = nasm_malloc(arangeslen);
1780 WRITELONG(pbuf,saalen); /* initial length */
1781 saa_rnbytes(paranges, pbuf, saalen);
1782 saa_free(paranges);
1784 /* build rela.aranges section */
1785 arangesrellen = saalen = parangesrel->datalen;
1786 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1787 saa_rnbytes(parangesrel, pbuf, saalen);
1788 saa_free(parangesrel);
1790 /* build pubnames section */
1791 ppubnames = saa_init(1L);
1792 saa_write16(ppubnames,3); /* dwarf version */
1793 saa_write32(ppubnames,0); /* offset into info */
1794 saa_write32(ppubnames,0); /* space used in info */
1795 saa_write32(ppubnames,0); /* end of list */
1796 saalen = ppubnames->datalen;
1797 pubnameslen = saalen + 4;
1798 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1799 WRITELONG(pbuf,saalen); /* initial length */
1800 saa_rnbytes(ppubnames, pbuf, saalen);
1801 saa_free(ppubnames);
1803 /* build info section */
1804 pinfo = saa_init(1L);
1805 pinforel = saa_init(1L);
1806 saa_write16(pinfo,2); /* dwarf version */
1807 saa_write32(pinforel, pinfo->datalen + 4);
1808 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1809 saa_write32(pinforel, 0);
1810 saa_write32(pinfo,0); /* offset into abbrev */
1811 saa_write8(pinfo,4); /* pointer size */
1812 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1813 saa_write32(pinforel, pinfo->datalen + 4);
1814 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1815 saa_write32(pinforel, 0);
1816 saa_write32(pinfo,0); /* DW_AT_low_pc */
1817 saa_write32(pinforel, pinfo->datalen + 4);
1818 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1819 saa_write32(pinforel, 0);
1820 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1821 saa_write32(pinforel, pinfo->datalen + 4);
1822 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1823 saa_write32(pinforel, 0);
1824 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1825 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1826 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1827 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1828 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1829 saa_write32(pinforel, pinfo->datalen + 4);
1830 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1831 saa_write32(pinforel, 0);
1832 saa_write32(pinfo,0); /* DW_AT_low_pc */
1833 saa_write32(pinfo,0); /* DW_AT_frame_base */
1834 saa_write8(pinfo,0); /* end of entries */
1835 saalen = pinfo->datalen;
1836 infolen = saalen + 4;
1837 infobuf = pbuf = nasm_malloc(infolen);
1838 WRITELONG(pbuf,saalen); /* initial length */
1839 saa_rnbytes(pinfo, pbuf, saalen);
1840 saa_free(pinfo);
1842 /* build rela.info section */
1843 inforellen = saalen = pinforel->datalen;
1844 inforelbuf = pbuf = nasm_malloc(inforellen);
1845 saa_rnbytes(pinforel, pbuf, saalen);
1846 saa_free(pinforel);
1848 /* build abbrev section */
1849 pabbrev = saa_init(1L);
1850 saa_write8(pabbrev,1); /* entry number LEB128u */
1851 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1852 saa_write8(pabbrev,1); /* has children */
1853 /* the following attributes and forms are all LEB128u values */
1854 saa_write8(pabbrev,DW_AT_low_pc);
1855 saa_write8(pabbrev,DW_FORM_addr);
1856 saa_write8(pabbrev,DW_AT_high_pc);
1857 saa_write8(pabbrev,DW_FORM_addr);
1858 saa_write8(pabbrev,DW_AT_stmt_list);
1859 saa_write8(pabbrev,DW_FORM_data4);
1860 saa_write8(pabbrev,DW_AT_name);
1861 saa_write8(pabbrev,DW_FORM_string);
1862 saa_write8(pabbrev,DW_AT_producer);
1863 saa_write8(pabbrev,DW_FORM_string);
1864 saa_write8(pabbrev,DW_AT_language);
1865 saa_write8(pabbrev,DW_FORM_data2);
1866 saa_write16(pabbrev,0); /* end of entry */
1867 /* LEB128u usage same as above */
1868 saa_write8(pabbrev,2); /* entry number */
1869 saa_write8(pabbrev,DW_TAG_subprogram);
1870 saa_write8(pabbrev,0); /* no children */
1871 saa_write8(pabbrev,DW_AT_low_pc);
1872 saa_write8(pabbrev,DW_FORM_addr);
1873 saa_write8(pabbrev,DW_AT_frame_base);
1874 saa_write8(pabbrev,DW_FORM_data4);
1875 saa_write16(pabbrev,0); /* end of entry */
1876 abbrevlen = saalen = pabbrev->datalen;
1877 abbrevbuf = pbuf = nasm_malloc(saalen);
1878 saa_rnbytes(pabbrev, pbuf, saalen);
1879 saa_free(pabbrev);
1881 /* build line section */
1882 /* prolog */
1883 plines = saa_init(1L);
1884 saa_write8(plines,1); /* Minimum Instruction Length */
1885 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1886 saa_write8(plines,line_base); /* Line Base */
1887 saa_write8(plines,line_range); /* Line Range */
1888 saa_write8(plines,opcode_base); /* Opcode Base */
1889 /* standard opcode lengths (# of LEB128u operands) */
1890 saa_write8(plines,0); /* Std opcode 1 length */
1891 saa_write8(plines,1); /* Std opcode 2 length */
1892 saa_write8(plines,1); /* Std opcode 3 length */
1893 saa_write8(plines,1); /* Std opcode 4 length */
1894 saa_write8(plines,1); /* Std opcode 5 length */
1895 saa_write8(plines,0); /* Std opcode 6 length */
1896 saa_write8(plines,0); /* Std opcode 7 length */
1897 saa_write8(plines,0); /* Std opcode 8 length */
1898 saa_write8(plines,1); /* Std opcode 9 length */
1899 saa_write8(plines,0); /* Std opcode 10 length */
1900 saa_write8(plines,0); /* Std opcode 11 length */
1901 saa_write8(plines,1); /* Std opcode 12 length */
1902 /* Directory Table */
1903 saa_write8(plines,0); /* End of table */
1904 /* File Name Table */
1905 ftentry = dwarf_flist;
1906 for (indx = 0; indx < dwarf_numfiles; indx++) {
1907 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1908 saa_write8(plines,0); /* directory LEB128u */
1909 saa_write8(plines,0); /* time LEB128u */
1910 saa_write8(plines,0); /* size LEB128u */
1911 ftentry = ftentry->next;
1913 saa_write8(plines,0); /* End of table */
1914 linepoff = plines->datalen;
1915 linelen = linepoff + totlen + 10;
1916 linebuf = pbuf = nasm_malloc(linelen);
1917 WRITELONG(pbuf,linelen-4); /* initial length */
1918 WRITESHORT(pbuf,3); /* dwarf version */
1919 WRITELONG(pbuf,linepoff); /* offset to line number program */
1920 /* write line header */
1921 saalen = linepoff;
1922 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1923 pbuf += linepoff;
1924 saa_free(plines);
1925 /* concatonate line program ranges */
1926 linepoff += 13;
1927 plinesrel = saa_init(1L);
1928 psect = dwarf_fsect;
1929 for (indx = 0; indx < dwarf_nsections; indx++) {
1930 saa_write32(plinesrel, linepoff);
1931 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1932 saa_write32(plinesrel, (uint32_t) 0);
1933 plinep = psect->psaa;
1934 saalen = plinep->datalen;
1935 saa_rnbytes(plinep, pbuf, saalen);
1936 pbuf += saalen;
1937 linepoff += saalen;
1938 saa_free(plinep);
1939 /* done with this entry */
1940 psect = psect->next;
1944 /* build rela.lines section */
1945 linerellen =saalen = plinesrel->datalen;
1946 linerelbuf = pbuf = nasm_malloc(linerellen);
1947 saa_rnbytes(plinesrel, pbuf, saalen);
1948 saa_free(plinesrel);
1950 /* build frame section */
1951 framelen = 4;
1952 framebuf = pbuf = nasm_malloc(framelen);
1953 WRITELONG(pbuf,framelen-4); /* initial length */
1955 /* build loc section */
1956 loclen = 16;
1957 locbuf = pbuf = nasm_malloc(loclen);
1958 WRITELONG(pbuf,0); /* null beginning offset */
1959 WRITELONG(pbuf,0); /* null ending offset */
1962 static void dwarf32_cleanup(void)
1964 nasm_free(arangesbuf);
1965 nasm_free(arangesrelbuf);
1966 nasm_free(pubnamesbuf);
1967 nasm_free(infobuf);
1968 nasm_free(inforelbuf);
1969 nasm_free(abbrevbuf);
1970 nasm_free(linebuf);
1971 nasm_free(linerelbuf);
1972 nasm_free(framebuf);
1973 nasm_free(locbuf);
1976 static void dwarf32_findfile(const char * fname)
1978 int finx;
1979 struct linelist *match;
1981 /* return if fname is current file name */
1982 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
1983 return;
1985 /* search for match */
1986 match = 0;
1987 if (dwarf_flist) {
1988 match = dwarf_flist;
1989 for (finx = 0; finx < dwarf_numfiles; finx++) {
1990 if (!(strcmp(fname, match->filename))) {
1991 dwarf_clist = match;
1992 return;
1997 /* add file name to end of list */
1998 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1999 dwarf_numfiles++;
2000 dwarf_clist->line = dwarf_numfiles;
2001 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2002 strcpy(dwarf_clist->filename,fname);
2003 dwarf_clist->next = 0;
2004 if (!dwarf_flist) { /* if first entry */
2005 dwarf_flist = dwarf_elist = dwarf_clist;
2006 dwarf_clist->last = 0;
2007 } else { /* chain to previous entry */
2008 dwarf_elist->next = dwarf_clist;
2009 dwarf_elist = dwarf_clist;
2013 static void dwarf32_findsect(const int index)
2015 int sinx;
2016 struct sectlist *match;
2017 struct SAA *plinep;
2019 /* return if index is current section index */
2020 if (dwarf_csect && (dwarf_csect->section == index))
2021 return;
2023 /* search for match */
2024 match = 0;
2025 if (dwarf_fsect) {
2026 match = dwarf_fsect;
2027 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2028 if (match->section == index) {
2029 dwarf_csect = match;
2030 return;
2032 match = match->next;
2036 /* add entry to end of list */
2037 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2038 dwarf_nsections++;
2039 dwarf_csect->psaa = plinep = saa_init(1L);
2040 dwarf_csect->line = 1;
2041 dwarf_csect->offset = 0;
2042 dwarf_csect->file = 1;
2043 dwarf_csect->section = index;
2044 dwarf_csect->next = 0;
2045 /* set relocatable address at start of line program */
2046 saa_write8(plinep,DW_LNS_extended_op);
2047 saa_write8(plinep,5); /* operand length */
2048 saa_write8(plinep,DW_LNE_set_address);
2049 saa_write32(plinep,0); /* Start Address */
2051 if (!dwarf_fsect) { /* if first entry */
2052 dwarf_fsect = dwarf_esect = dwarf_csect;
2053 dwarf_csect->last = 0;
2054 } else { /* chain to previous entry */
2055 dwarf_esect->next = dwarf_csect;
2056 dwarf_esect = dwarf_csect;
2060 #endif /* OF_ELF */