test: Add br3385573 testcase
[nasm/avx512.git] / output / outelf32.c
blob9ef245773daf124e10fd4ba545ae1850632534bc
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 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
65 * Relocation types.
67 struct Reloc {
68 struct Reloc *next;
69 int32_t address; /* relative to _start_ of section */
70 int32_t symbol; /* symbol index */
71 int type; /* type of relocation */
74 struct Symbol {
75 struct rbtree symv; /* symbol value and symbol rbtree */
76 int32_t strpos; /* string table position of name */
77 int32_t section; /* section ID of the symbol */
78 int type; /* symbol type */
79 int other; /* symbol visibility */
80 int32_t size; /* size of symbol */
81 int32_t globnum; /* symbol table offset if global */
82 struct Symbol *nextfwd; /* list of unresolved-size symbols */
83 char *name; /* used temporarily if in above list */
86 struct Section {
87 struct SAA *data;
88 uint32_t len, size, nrelocs;
89 int32_t index;
90 int type; /* SHT_PROGBITS or SHT_NOBITS */
91 uint32_t align; /* alignment: power of two */
92 uint32_t flags; /* section flags */
93 char *name;
94 struct SAA *rel;
95 int32_t rellen;
96 struct Reloc *head, **tail;
97 struct rbtree *gsyms; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section **sects;
102 static int nsects, sectlen;
104 #define SHSTR_DELTA 256
105 static char *shstrtab;
106 static int shstrtablen, shstrtabsize;
108 static struct SAA *syms;
109 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
111 static int32_t def_seg;
113 static struct RAA *bsym;
115 static struct SAA *strs;
116 static uint32_t strslen;
118 static struct Symbol *fwds;
120 static char elf_module[FILENAME_MAX];
122 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
123 static uint8_t elf_abiver = 0; /* Current ABI version */
125 extern struct ofmt of_elf32;
127 static struct ELF_SECTDATA {
128 void *data;
129 int32_t len;
130 bool is_saa;
131 } *elf_sects;
132 static int elf_nsect, nsections;
133 static int32_t elf_foffs;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section *, const uint8_t *,
137 uint32_t);
138 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
139 int, int);
140 static void elf_write_sections(void);
141 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
143 static void add_sectname(char *, char *);
145 struct erel {
146 int offset, info;
149 struct symlininfo {
150 int offset;
151 int section; /* section index */
152 char *name; /* shallow-copied pointer of section name */
155 struct linelist {
156 struct linelist *next;
157 struct linelist *last;
158 struct symlininfo info;
159 char *filename;
160 int line;
163 struct sectlist {
164 struct SAA *psaa;
165 int section;
166 int line;
167 int offset;
168 int file;
169 struct sectlist *next;
170 struct sectlist *last;
173 /* common debug variables */
174 static int currentline = 1;
175 static int debug_immcall = 0;
177 /* stabs debug variables */
178 static struct linelist *stabslines = 0;
179 static int numlinestabs = 0;
180 static char *stabs_filename = 0;
181 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
182 static int stablen, stabstrlen, stabrellen;
184 /* dwarf debug variables */
185 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
186 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
187 static int dwarf_numfiles = 0, dwarf_nsections;
188 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
189 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
190 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
191 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
192 abbrevlen, linelen, linerellen, framelen, loclen;
193 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
195 static struct dfmt df_dwarf;
196 static struct dfmt df_stabs;
197 static struct Symbol *lastsym;
199 /* common debugging routines */
200 static void debug32_typevalue(int32_t);
201 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
202 static void debug32_directive(const char *, const char *);
204 /* stabs debugging routines */
205 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
206 static void stabs32_output(int, void *);
207 static void stabs32_generate(void);
208 static void stabs32_cleanup(void);
210 /* dwarf debugging routines */
211 static void dwarf32_init(void);
212 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
213 static void dwarf32_output(int, void *);
214 static void dwarf32_generate(void);
215 static void dwarf32_cleanup(void);
216 static void dwarf32_findfile(const char *);
217 static void dwarf32_findsect(const int);
220 * Special NASM section numbers which are used to define ELF special
221 * symbols, which can be used with WRT to provide PIC and TLS
222 * relocation types.
224 static int32_t elf_gotpc_sect, elf_gotoff_sect;
225 static int32_t elf_got_sect, elf_plt_sect;
226 static int32_t elf_sym_sect, elf_tlsie_sect;
228 static void elf_init(void)
230 sects = NULL;
231 nsects = sectlen = 0;
232 syms = saa_init((int32_t)sizeof(struct Symbol));
233 nlocals = nglobs = ndebugs = 0;
234 bsym = raa_init();
235 strs = saa_init(1L);
236 saa_wbytes(strs, "\0", 1L);
237 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
238 strslen = 2 + strlen(elf_module);
239 shstrtab = NULL;
240 shstrtablen = shstrtabsize = 0;;
241 add_sectname("", "");
243 fwds = NULL;
245 elf_gotpc_sect = seg_alloc();
246 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
247 elf_gotoff_sect = seg_alloc();
248 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
249 elf_got_sect = seg_alloc();
250 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
251 elf_plt_sect = seg_alloc();
252 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
253 elf_sym_sect = seg_alloc();
254 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
255 elf_tlsie_sect = seg_alloc();
256 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
258 def_seg = seg_alloc();
261 static void elf_cleanup(int debuginfo)
263 struct Reloc *r;
264 int i;
266 (void)debuginfo;
268 elf_write();
269 for (i = 0; i < nsects; i++) {
270 if (sects[i]->type != SHT_NOBITS)
271 saa_free(sects[i]->data);
272 if (sects[i]->head)
273 saa_free(sects[i]->rel);
274 while (sects[i]->head) {
275 r = sects[i]->head;
276 sects[i]->head = sects[i]->head->next;
277 nasm_free(r);
280 nasm_free(sects);
281 saa_free(syms);
282 raa_free(bsym);
283 saa_free(strs);
284 if (of_elf32.current_dfmt) {
285 of_elf32.current_dfmt->cleanup();
289 static void add_sectname(char *firsthalf, char *secondhalf)
291 int len = strlen(firsthalf) + strlen(secondhalf);
292 while (shstrtablen + len + 1 > shstrtabsize)
293 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
294 strcpy(shstrtab + shstrtablen, firsthalf);
295 strcat(shstrtab + shstrtablen, secondhalf);
296 shstrtablen += len + 1;
299 static int elf_make_section(char *name, int type, int flags, int align)
301 struct Section *s;
303 s = nasm_zalloc(sizeof(*s));
305 if (type != SHT_NOBITS)
306 s->data = saa_init(1L);
307 s->tail = &s->head;
308 if (!strcmp(name, ".text"))
309 s->index = def_seg;
310 else
311 s->index = seg_alloc();
312 add_sectname("", name);
314 s->name = nasm_strdup(name);
315 s->type = type;
316 s->flags = flags;
317 s->align = align;
319 if (nsects >= sectlen)
320 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
321 sects[nsects++] = s;
323 return nsects - 1;
326 static int32_t elf_section_names(char *name, int pass, int *bits)
328 char *p;
329 uint32_t flags, flags_and, flags_or;
330 uint64_t align;
331 int type, i;
334 * Default is 32 bits.
336 if (!name) {
337 *bits = 32;
338 return def_seg;
341 p = nasm_skip_word(name);
342 if (*p)
343 *p++ = '\0';
344 flags_and = flags_or = type = align = 0;
346 section_attrib(name, p, pass, &flags_and,
347 &flags_or, &align, &type);
349 if (!strcmp(name, ".shstrtab") ||
350 !strcmp(name, ".symtab") ||
351 !strcmp(name, ".strtab")) {
352 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
353 "name `%s'", name);
354 return NO_SEG;
357 for (i = 0; i < nsects; i++)
358 if (!strcmp(name, sects[i]->name))
359 break;
360 if (i == nsects) {
361 const struct elf_known_section *ks = elf_known_sections;
363 while (ks->name) {
364 if (!strcmp(name, ks->name))
365 break;
366 ks++;
369 type = type ? type : ks->type;
370 align = align ? align : ks->align;
371 flags = (ks->flags & ~flags_and) | flags_or;
373 i = elf_make_section(name, type, flags, align);
374 } else if (pass == 1) {
375 if ((type && sects[i]->type != type)
376 || (align && sects[i]->align != align)
377 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
378 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
379 " redeclaration of section `%s'", name);
382 return sects[i]->index;
385 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
386 int is_global, char *special)
388 int pos = strslen;
389 struct Symbol *sym;
390 bool special_used = false;
392 #if defined(DEBUG) && DEBUG>2
393 nasm_error(ERR_DEBUG,
394 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
395 name, segment, offset, is_global, special);
396 #endif
397 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
399 * This is a NASM special symbol. We never allow it into
400 * the ELF symbol table, even if it's a valid one. If it
401 * _isn't_ a valid one, we should barf immediately.
403 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
404 strcmp(name, "..got") && strcmp(name, "..plt") &&
405 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
406 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
407 return;
410 if (is_global == 3) {
411 struct Symbol **s;
413 * Fix up a forward-reference symbol size from the first
414 * pass.
416 for (s = &fwds; *s; s = &(*s)->nextfwd)
417 if (!strcmp((*s)->name, name)) {
418 struct tokenval tokval;
419 expr *e;
420 char *p = nasm_skip_spaces(nasm_skip_word(special));
422 stdscan_reset();
423 stdscan_set(p);
424 tokval.t_type = TOKEN_INVALID;
425 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
426 if (e) {
427 if (!is_simple(e))
428 nasm_error(ERR_NONFATAL, "cannot use relocatable"
429 " expression as symbol size");
430 else
431 (*s)->size = reloc_value(e);
435 * Remove it from the list of unresolved sizes.
437 nasm_free((*s)->name);
438 *s = (*s)->nextfwd;
439 return;
441 return; /* it wasn't an important one */
444 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
445 strslen += 1 + strlen(name);
447 lastsym = sym = saa_wstruct(syms);
449 memset(&sym->symv, 0, sizeof(struct rbtree));
451 sym->strpos = pos;
452 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
453 sym->other = STV_DEFAULT;
454 sym->size = 0;
455 if (segment == NO_SEG)
456 sym->section = SHN_ABS;
457 else {
458 int i;
459 sym->section = SHN_UNDEF;
460 if (segment == def_seg) {
461 /* we have to be sure at least text section is there */
462 int tempint;
463 if (segment != elf_section_names(".text", 2, &tempint))
464 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
466 for (i = 0; i < nsects; i++) {
467 if (segment == sects[i]->index) {
468 sym->section = i + 1;
469 break;
474 if (is_global == 2) {
475 sym->size = offset;
476 sym->symv.key = 0;
477 sym->section = SHN_COMMON;
479 * We have a common variable. Check the special text to see
480 * if it's a valid number and power of two; if so, store it
481 * as the alignment for the common variable.
483 if (special) {
484 bool err;
485 sym->symv.key = readnum(special, &err);
486 if (err)
487 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
488 " valid number", special);
489 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
490 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
491 " power of two", special);
493 special_used = true;
494 } else
495 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
497 if (sym->type == SYM_GLOBAL) {
499 * If sym->section == SHN_ABS, then the first line of the
500 * else section would cause a core dump, because its a reference
501 * beyond the end of the section array.
502 * This behaviour is exhibited by this code:
503 * GLOBAL crash_nasm
504 * crash_nasm equ 0
505 * To avoid such a crash, such requests are silently discarded.
506 * This may not be the best solution.
508 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
509 bsym = raa_write(bsym, segment, nglobs);
510 } else if (sym->section != SHN_ABS) {
512 * This is a global symbol; so we must add it to the rbtree
513 * of global symbols in its section.
515 * In addition, we check the special text for symbol
516 * type and size information.
518 sects[sym->section-1]->gsyms =
519 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
521 if (special) {
522 int n = strcspn(special, " \t");
524 if (!nasm_strnicmp(special, "function", n))
525 sym->type |= STT_FUNC;
526 else if (!nasm_strnicmp(special, "data", n) ||
527 !nasm_strnicmp(special, "object", n))
528 sym->type |= STT_OBJECT;
529 else if (!nasm_strnicmp(special, "notype", n))
530 sym->type |= STT_NOTYPE;
531 else
532 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
533 n, special);
534 special += n;
536 special = nasm_skip_spaces(special);
537 if (*special) {
538 n = strcspn(special, " \t");
539 if (!nasm_strnicmp(special, "default", n))
540 sym->other = STV_DEFAULT;
541 else if (!nasm_strnicmp(special, "internal", n))
542 sym->other = STV_INTERNAL;
543 else if (!nasm_strnicmp(special, "hidden", n))
544 sym->other = STV_HIDDEN;
545 else if (!nasm_strnicmp(special, "protected", n))
546 sym->other = STV_PROTECTED;
547 else
548 n = 0;
549 special += n;
552 if (*special) {
553 struct tokenval tokval;
554 expr *e;
555 int fwd = 0;
556 char *saveme = stdscan_get();
558 while (special[n] && nasm_isspace(special[n]))
559 n++;
561 * We have a size expression; attempt to
562 * evaluate it.
564 stdscan_reset();
565 stdscan_set(special + n);
566 tokval.t_type = TOKEN_INVALID;
567 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
568 NULL);
569 if (fwd) {
570 sym->nextfwd = fwds;
571 fwds = sym;
572 sym->name = nasm_strdup(name);
573 } else if (e) {
574 if (!is_simple(e))
575 nasm_error(ERR_NONFATAL, "cannot use relocatable"
576 " expression as symbol size");
577 else
578 sym->size = reloc_value(e);
580 stdscan_set(saveme);
582 special_used = true;
585 * If TLS segment, mark symbol accordingly.
587 if (sects[sym->section - 1]->flags & SHF_TLS) {
588 sym->type &= 0xf0;
589 sym->type |= STT_TLS;
592 sym->globnum = nglobs;
593 nglobs++;
594 } else
595 nlocals++;
597 if (special && !special_used)
598 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
601 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
603 struct Reloc *r;
605 r = *sect->tail = nasm_zalloc(sizeof(struct Reloc));
606 sect->tail = &r->next;
608 r->address = sect->len;
609 if (segment != NO_SEG) {
610 int i;
611 for (i = 0; i < nsects; i++)
612 if (segment == sects[i]->index)
613 r->symbol = i + 2;
614 if (!r->symbol)
615 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
617 r->type = type;
619 sect->nrelocs++;
623 * This routine deals with ..got and ..sym relocations: the more
624 * complicated kinds. In shared-library writing, some relocations
625 * with respect to global symbols must refer to the precise symbol
626 * rather than referring to an offset from the base of the section
627 * _containing_ the symbol. Such relocations call to this routine,
628 * which searches the symbol list for the symbol in question.
630 * R_386_GOT32 references require the _exact_ symbol address to be
631 * used; R_386_32 references can be at an offset from the symbol.
632 * The boolean argument `exact' tells us this.
634 * Return value is the adjusted value of `addr', having become an
635 * offset from the symbol rather than the section. Should always be
636 * zero when returning from an exact call.
638 * Limitation: if you define two symbols at the same place,
639 * confusion will occur.
641 * Inefficiency: we search, currently, using a linked list which
642 * isn't even necessarily sorted.
644 static int32_t elf_add_gsym_reloc(struct Section *sect,
645 int32_t segment, uint32_t offset,
646 int type, bool exact)
648 struct Reloc *r;
649 struct Section *s;
650 struct Symbol *sym;
651 struct rbtree *srb;
652 int i;
655 * First look up the segment/offset pair and find a global
656 * symbol corresponding to it. If it's not one of our segments,
657 * then it must be an external symbol, in which case we're fine
658 * doing a normal elf_add_reloc after first sanity-checking
659 * that the offset from the symbol is zero.
661 s = NULL;
662 for (i = 0; i < nsects; i++)
663 if (segment == sects[i]->index) {
664 s = sects[i];
665 break;
667 if (!s) {
668 if (exact && offset != 0)
669 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
670 " for this reference");
671 else
672 elf_add_reloc(sect, segment, type);
673 return offset;
676 srb = rb_search(s->gsyms, offset);
677 if (!srb || (exact && srb->key != offset)) {
678 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
679 " for this reference");
680 return 0;
682 sym = container_of(srb, struct Symbol, symv);
684 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
685 sect->tail = &r->next;
687 r->next = NULL;
688 r->address = sect->len;
689 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
690 r->type = type;
692 sect->nrelocs++;
694 return offset - sym->symv.key;
697 static void elf_out(int32_t segto, const void *data,
698 enum out_type type, uint64_t size,
699 int32_t segment, int32_t wrt)
701 struct Section *s;
702 int32_t addr;
703 uint8_t mydata[8], *p;
704 int reltype, bytes;
705 int i;
706 static struct symlininfo sinfo;
709 * handle absolute-assembly (structure definitions)
711 if (segto == NO_SEG) {
712 if (type != OUT_RESERVE)
713 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
714 " space");
715 return;
718 s = NULL;
719 for (i = 0; i < nsects; i++)
720 if (segto == sects[i]->index) {
721 s = sects[i];
722 break;
724 if (!s) {
725 int tempint; /* ignored */
726 if (segto != elf_section_names(".text", 2, &tempint))
727 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
728 else {
729 s = sects[nsects - 1];
730 i = nsects - 1;
734 /* again some stabs debugging stuff */
735 if (of_elf32.current_dfmt) {
736 sinfo.offset = s->len;
737 sinfo.section = i;
738 sinfo.name = s->name;
739 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
741 /* end of debugging stuff */
743 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
744 nasm_error(ERR_WARNING, "attempt to initialize memory in"
745 " BSS section `%s': ignored", s->name);
746 s->len += realsize(type, size);
747 return;
750 switch (type) {
751 case OUT_RESERVE:
752 if (s->type == SHT_PROGBITS) {
753 nasm_error(ERR_WARNING, "uninitialized space declared in"
754 " non-BSS section `%s': zeroing", s->name);
755 elf_sect_write(s, NULL, size);
756 } else
757 s->len += size;
758 break;
760 case OUT_RAWDATA:
761 if (segment != NO_SEG)
762 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
763 elf_sect_write(s, data, size);
764 break;
766 case OUT_ADDRESS:
768 bool gnu16 = false;
769 addr = *(int64_t *)data;
770 if (segment != NO_SEG) {
771 if (segment % 2) {
772 nasm_error(ERR_NONFATAL, "ELF format does not support"
773 " segment base references");
774 } else {
775 if (wrt == NO_SEG) {
777 * The if() is a hack to deal with compilers which
778 * don't handle switch() statements with 64-bit
779 * expressions.
781 if (size < UINT_MAX) {
782 switch ((unsigned int)size) {
783 case 1:
784 gnu16 = true;
785 elf_add_reloc(s, segment, R_386_8);
786 break;
787 case 2:
788 gnu16 = true;
789 elf_add_reloc(s, segment, R_386_16);
790 break;
791 case 4:
792 elf_add_reloc(s, segment, R_386_32);
793 break;
794 default: /* Error issued further down */
795 break;
798 } else if (wrt == elf_gotpc_sect + 1) {
800 * The user will supply GOT relative to $$. ELF
801 * will let us have GOT relative to $. So we
802 * need to fix up the data item by $-$$.
804 addr += s->len;
805 elf_add_reloc(s, segment, R_386_GOTPC);
806 } else if (wrt == elf_gotoff_sect + 1) {
807 elf_add_reloc(s, segment, R_386_GOTOFF);
808 } else if (wrt == elf_tlsie_sect + 1) {
809 addr = elf_add_gsym_reloc(s, segment, addr,
810 R_386_TLS_IE, true);
811 } else if (wrt == elf_got_sect + 1) {
812 addr = elf_add_gsym_reloc(s, segment, addr,
813 R_386_GOT32, true);
814 } else if (wrt == elf_sym_sect + 1) {
815 if (size == 2) {
816 gnu16 = true;
817 addr = elf_add_gsym_reloc(s, segment, addr,
818 R_386_16, false);
819 } else {
820 addr = elf_add_gsym_reloc(s, segment, addr,
821 R_386_32, false);
823 } else if (wrt == elf_plt_sect + 1) {
824 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
825 "relative PLT references");
826 } else {
827 nasm_error(ERR_NONFATAL, "ELF format does not support this"
828 " use of WRT");
829 wrt = NO_SEG; /* we can at least _try_ to continue */
833 p = mydata;
834 if (gnu16) {
835 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
836 "8- or 16-bit relocations in ELF32 is a GNU extension");
837 } else if (size != 4 && segment != NO_SEG) {
838 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
840 WRITEADDR(p, addr, size);
841 elf_sect_write(s, mydata, size);
842 break;
845 case OUT_REL1ADR:
846 bytes = 1;
847 reltype = R_386_PC8;
848 goto rel12adr;
849 case OUT_REL2ADR:
850 bytes = 2;
851 reltype = R_386_PC16;
852 goto rel12adr;
854 rel12adr:
855 nasm_assert(segment != segto);
856 if (segment != NO_SEG && segment % 2) {
857 nasm_error(ERR_NONFATAL, "ELF format does not support"
858 " segment base references");
859 } else {
860 if (wrt == NO_SEG) {
861 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
862 "8- or 16-bit relocations in ELF is a GNU extension");
863 elf_add_reloc(s, segment, reltype);
864 } else {
865 nasm_error(ERR_NONFATAL,
866 "Unsupported non-32-bit ELF relocation");
869 p = mydata;
870 WRITESHORT(p, *(int64_t *)data - size);
871 elf_sect_write(s, mydata, bytes);
872 break;
874 case OUT_REL4ADR:
875 if (segment == segto)
876 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
877 if (segment != NO_SEG && segment % 2) {
878 nasm_error(ERR_NONFATAL, "ELF format does not support"
879 " segment base references");
880 } else {
881 if (wrt == NO_SEG) {
882 elf_add_reloc(s, segment, R_386_PC32);
883 } else if (wrt == elf_plt_sect + 1) {
884 elf_add_reloc(s, segment, R_386_PLT32);
885 } else if (wrt == elf_gotpc_sect + 1 ||
886 wrt == elf_gotoff_sect + 1 ||
887 wrt == elf_got_sect + 1) {
888 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
889 "relative GOT references");
890 } else {
891 nasm_error(ERR_NONFATAL, "ELF format does not support this"
892 " use of WRT");
893 wrt = NO_SEG; /* we can at least _try_ to continue */
896 p = mydata;
897 WRITELONG(p, *(int64_t *)data - size);
898 elf_sect_write(s, mydata, 4L);
899 break;
901 case OUT_REL8ADR:
902 nasm_error(ERR_NONFATAL,
903 "32-bit ELF format does not support 64-bit relocations");
904 p = mydata;
905 WRITEDLONG(p, 0);
906 elf_sect_write(s, mydata, 8L);
907 break;
911 static void elf_write(void)
913 int align;
914 char *p;
915 int i;
917 struct SAA *symtab;
918 int32_t symtablen, symtablocal;
921 * Work out how many sections we will have. We have SHN_UNDEF,
922 * then the flexible user sections, then the fixed sections
923 * `.shstrtab', `.symtab' and `.strtab', then optionally
924 * relocation sections for the user sections.
926 nsections = sec_numspecial + 1;
927 if (of_elf32.current_dfmt == &df_stabs)
928 nsections += 3;
929 else if (of_elf32.current_dfmt == &df_dwarf)
930 nsections += 10;
932 add_sectname("", ".shstrtab");
933 add_sectname("", ".symtab");
934 add_sectname("", ".strtab");
935 for (i = 0; i < nsects; i++) {
936 nsections++; /* for the section itself */
937 if (sects[i]->head) {
938 nsections++; /* for its relocations */
939 add_sectname(".rel", sects[i]->name);
943 if (of_elf32.current_dfmt == &df_stabs) {
944 /* in case the debug information is wanted, just add these three sections... */
945 add_sectname("", ".stab");
946 add_sectname("", ".stabstr");
947 add_sectname(".rel", ".stab");
948 } else if (of_elf32.current_dfmt == &df_dwarf) {
949 /* the dwarf debug standard specifies the following ten sections,
950 not all of which are currently implemented,
951 although all of them are defined. */
952 add_sectname("", ".debug_aranges");
953 add_sectname(".rela", ".debug_aranges");
954 add_sectname("", ".debug_pubnames");
955 add_sectname("", ".debug_info");
956 add_sectname(".rela", ".debug_info");
957 add_sectname("", ".debug_abbrev");
958 add_sectname("", ".debug_line");
959 add_sectname(".rela", ".debug_line");
960 add_sectname("", ".debug_frame");
961 add_sectname("", ".debug_loc");
965 * Output the ELF header.
967 fwrite("\177ELF\1\1\1", 7, 1, ofile);
968 fputc(elf_osabi, ofile);
969 fputc(elf_abiver, ofile);
970 fwritezero(7, ofile);
971 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
972 fwriteint16_t(3, ofile); /* EM_386 processor ID */
973 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
974 fwriteint32_t(0L, ofile); /* no entry point */
975 fwriteint32_t(0L, ofile); /* no program header table */
976 fwriteint32_t(0x40L, ofile); /* section headers straight after
977 * ELF header plus alignment */
978 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
979 fwriteint16_t(0x34, ofile); /* size of ELF header */
980 fwriteint16_t(0, ofile); /* no program header table, again */
981 fwriteint16_t(0, ofile); /* still no program header table */
982 fwriteint16_t(0x28, ofile); /* size of section header */
983 fwriteint16_t(nsections, ofile); /* number of sections */
984 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
985 * section header table */
986 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
987 fwriteint32_t(0L, ofile);
988 fwriteint32_t(0L, ofile);
991 * Build the symbol table and relocation tables.
993 symtab = elf_build_symtab(&symtablen, &symtablocal);
994 for (i = 0; i < nsects; i++)
995 if (sects[i]->head)
996 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
997 sects[i]->head);
1000 * Now output the section header table.
1003 elf_foffs = 0x40 + 0x28 * nsections;
1004 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1005 elf_foffs += align;
1006 elf_nsect = 0;
1007 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1009 /* SHN_UNDEF */
1010 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1011 p = shstrtab + 1;
1013 /* The normal sections */
1014 for (i = 0; i < nsects; i++) {
1015 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1016 (sects[i]->type == SHT_PROGBITS ?
1017 sects[i]->data : NULL), true,
1018 sects[i]->len, 0, 0, sects[i]->align, 0);
1019 p += strlen(p) + 1;
1022 /* .shstrtab */
1023 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1024 shstrtablen, 0, 0, 1, 0);
1025 p += strlen(p) + 1;
1027 /* .symtab */
1028 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1029 symtablen, sec_strtab, symtablocal, 4, 16);
1030 p += strlen(p) + 1;
1032 /* .strtab */
1033 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1034 strslen, 0, 0, 1, 0);
1035 p += strlen(p) + 1;
1037 /* The relocation sections */
1038 for (i = 0; i < nsects; i++)
1039 if (sects[i]->head) {
1040 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1041 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1042 p += strlen(p) + 1;
1045 if (of_elf32.current_dfmt == &df_stabs) {
1046 /* for debugging information, create the last three sections
1047 which are the .stab , .stabstr and .rel.stab sections respectively */
1049 /* this function call creates the stab sections in memory */
1050 stabs32_generate();
1052 if (stabbuf && stabstrbuf && stabrelbuf) {
1053 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1054 stablen, sec_stabstr, 0, 4, 12);
1055 p += strlen(p) + 1;
1057 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1058 stabstrlen, 0, 0, 4, 0);
1059 p += strlen(p) + 1;
1061 /* link -> symtable info -> section to refer to */
1062 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1063 stabrellen, sec_symtab, sec_stab, 4, 8);
1064 p += strlen(p) + 1;
1066 } else if (of_elf32.current_dfmt == &df_dwarf) {
1067 /* for dwarf debugging information, create the ten dwarf sections */
1069 /* this function call creates the dwarf sections in memory */
1070 if (dwarf_fsect)
1071 dwarf32_generate();
1073 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1074 arangeslen, 0, 0, 1, 0);
1075 p += strlen(p) + 1;
1077 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1078 arangesrellen, sec_symtab, sec_debug_aranges,
1079 1, 12);
1080 p += strlen(p) + 1;
1082 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1083 false, pubnameslen, 0, 0, 1, 0);
1084 p += strlen(p) + 1;
1086 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1087 infolen, 0, 0, 1, 0);
1088 p += strlen(p) + 1;
1090 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1091 inforellen, sec_symtab, sec_debug_info, 1, 12);
1092 p += strlen(p) + 1;
1094 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1095 abbrevlen, 0, 0, 1, 0);
1096 p += strlen(p) + 1;
1098 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1099 linelen, 0, 0, 1, 0);
1100 p += strlen(p) + 1;
1102 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1103 linerellen, sec_symtab, sec_debug_line, 1, 12);
1104 p += strlen(p) + 1;
1106 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1107 framelen, 0, 0, 8, 0);
1108 p += strlen(p) + 1;
1110 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1111 loclen, 0, 0, 1, 0);
1112 p += strlen(p) + 1;
1114 fwritezero(align, ofile);
1117 * Now output the sections.
1119 elf_write_sections();
1121 nasm_free(elf_sects);
1122 saa_free(symtab);
1125 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1127 struct SAA *s = saa_init(1L);
1128 struct Symbol *sym;
1129 uint8_t entry[16], *p;
1130 int i;
1132 *len = *local = 0;
1135 * First, an all-zeros entry, required by the ELF spec.
1137 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1138 *len += 16;
1139 (*local)++;
1142 * Next, an entry for the file name.
1144 p = entry;
1145 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1146 WRITELONG(p, 0); /* no value */
1147 WRITELONG(p, 0); /* no size either */
1148 WRITESHORT(p, STT_FILE); /* type FILE */
1149 WRITESHORT(p, SHN_ABS);
1150 saa_wbytes(s, entry, 16L);
1151 *len += 16;
1152 (*local)++;
1155 * Now some standard symbols defining the segments, for relocation
1156 * purposes.
1158 for (i = 1; i <= nsects; i++) {
1159 p = entry;
1160 WRITELONG(p, 0); /* no symbol name */
1161 WRITELONG(p, 0); /* offset zero */
1162 WRITELONG(p, 0); /* size zero */
1163 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1164 WRITESHORT(p, i); /* section id */
1165 saa_wbytes(s, entry, 16L);
1166 *len += 16;
1167 (*local)++;
1171 * Now the other local symbols.
1173 saa_rewind(syms);
1174 while ((sym = saa_rstruct(syms))) {
1175 if (sym->type & SYM_GLOBAL)
1176 continue;
1177 p = entry;
1178 WRITELONG(p, sym->strpos);
1179 WRITELONG(p, sym->symv.key);
1180 WRITELONG(p, sym->size);
1181 WRITECHAR(p, sym->type); /* type and binding */
1182 WRITECHAR(p, sym->other); /* visibility */
1183 WRITESHORT(p, sym->section);
1184 saa_wbytes(s, entry, 16L);
1185 *len += 16;
1186 (*local)++;
1189 * dwarf needs symbols for debug sections
1190 * which are relocation targets.
1192 //*** fix for 32 bit
1193 if (of_elf32.current_dfmt == &df_dwarf) {
1194 dwarf_infosym = *local;
1195 p = entry;
1196 WRITELONG(p, 0); /* no symbol name */
1197 WRITELONG(p, (uint32_t) 0); /* offset zero */
1198 WRITELONG(p, (uint32_t) 0); /* size zero */
1199 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1200 WRITESHORT(p, sec_debug_info); /* section id */
1201 saa_wbytes(s, entry, 16L);
1202 *len += 16;
1203 (*local)++;
1204 dwarf_abbrevsym = *local;
1205 p = entry;
1206 WRITELONG(p, 0); /* no symbol name */
1207 WRITELONG(p, (uint32_t) 0); /* offset zero */
1208 WRITELONG(p, (uint32_t) 0); /* size zero */
1209 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1210 WRITESHORT(p, sec_debug_abbrev); /* section id */
1211 saa_wbytes(s, entry, 16L);
1212 *len += 16;
1213 (*local)++;
1214 dwarf_linesym = *local;
1215 p = entry;
1216 WRITELONG(p, 0); /* no symbol name */
1217 WRITELONG(p, (uint32_t) 0); /* offset zero */
1218 WRITELONG(p, (uint32_t) 0); /* size zero */
1219 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1220 WRITESHORT(p, sec_debug_line); /* section id */
1221 saa_wbytes(s, entry, 16L);
1222 *len += 16;
1223 (*local)++;
1227 * Now the global symbols.
1229 saa_rewind(syms);
1230 while ((sym = saa_rstruct(syms))) {
1231 if (!(sym->type & SYM_GLOBAL))
1232 continue;
1233 p = entry;
1234 WRITELONG(p, sym->strpos);
1235 WRITELONG(p, sym->symv.key);
1236 WRITELONG(p, sym->size);
1237 WRITECHAR(p, sym->type); /* type and binding */
1238 WRITECHAR(p, sym->other); /* visibility */
1239 WRITESHORT(p, sym->section);
1240 saa_wbytes(s, entry, 16L);
1241 *len += 16;
1244 return s;
1247 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1249 struct SAA *s;
1250 uint8_t *p, entry[8];
1251 int32_t global_offset;
1253 if (!r)
1254 return NULL;
1256 s = saa_init(1L);
1257 *len = 0;
1260 * How to onvert from a global placeholder to a real symbol index;
1261 * the +2 refers to the two special entries, the null entry and
1262 * the filename entry.
1264 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1266 while (r) {
1267 int32_t sym = r->symbol;
1270 * Create a real symbol index; the +2 refers to the two special
1271 * entries, the null entry and the filename entry.
1273 if (sym >= GLOBAL_TEMP_BASE)
1274 sym += global_offset;
1276 p = entry;
1277 WRITELONG(p, r->address);
1278 WRITELONG(p, (sym << 8) + r->type);
1279 saa_wbytes(s, entry, 8L);
1280 *len += 8;
1282 r = r->next;
1285 return s;
1288 static void elf_section_header(int name, int type, int flags,
1289 void *data, bool is_saa, int32_t datalen,
1290 int link, int info, int align, int eltsize)
1292 elf_sects[elf_nsect].data = data;
1293 elf_sects[elf_nsect].len = datalen;
1294 elf_sects[elf_nsect].is_saa = is_saa;
1295 elf_nsect++;
1297 fwriteint32_t((int32_t)name, ofile);
1298 fwriteint32_t((int32_t)type, ofile);
1299 fwriteint32_t((int32_t)flags, ofile);
1300 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1301 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1302 fwriteint32_t(datalen, ofile);
1303 if (data)
1304 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1305 fwriteint32_t((int32_t)link, ofile);
1306 fwriteint32_t((int32_t)info, ofile);
1307 fwriteint32_t((int32_t)align, ofile);
1308 fwriteint32_t((int32_t)eltsize, ofile);
1311 static void elf_write_sections(void)
1313 int i;
1314 for (i = 0; i < elf_nsect; i++)
1315 if (elf_sects[i].data) {
1316 int32_t len = elf_sects[i].len;
1317 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1318 int32_t align = reallen - len;
1319 if (elf_sects[i].is_saa)
1320 saa_fpwrite(elf_sects[i].data, ofile);
1321 else
1322 fwrite(elf_sects[i].data, len, 1, ofile);
1323 fwritezero(align, ofile);
1327 static void elf_sect_write(struct Section *sect,
1328 const uint8_t *data, uint32_t len)
1330 saa_wbytes(sect->data, data, len);
1331 sect->len += len;
1334 static void elf_sectalign(int32_t seg, unsigned int value)
1336 struct Section *s = NULL;
1337 int i;
1339 for (i = 0; i < nsects; i++) {
1340 if (sects[i]->index == seg) {
1341 s = sects[i];
1342 break;
1345 if (!s || !is_power2(value))
1346 return;
1348 if (value > s->align)
1349 s->align = value;
1352 static int32_t elf_segbase(int32_t segment)
1354 return segment;
1357 static int elf_directive(enum directives directive, char *value, int pass)
1359 bool err;
1360 int64_t n;
1361 char *p;
1363 switch (directive) {
1364 case D_OSABI:
1365 if (pass == 2)
1366 return 1; /* ignore in pass 2 */
1368 n = readnum(value, &err);
1369 if (err) {
1370 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1371 return 1;
1373 if (n < 0 || n > 255) {
1374 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1375 return 1;
1377 elf_osabi = n;
1378 elf_abiver = 0;
1380 if ((p = strchr(value,',')) == NULL)
1381 return 1;
1383 n = readnum(p+1, &err);
1384 if (err || n < 0 || n > 255) {
1385 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1386 return 1;
1389 elf_abiver = n;
1390 return 1;
1392 default:
1393 return 0;
1397 static void elf_filename(char *inname, char *outname)
1399 strcpy(elf_module, inname);
1400 standard_extension(inname, outname, ".o");
1403 extern macros_t elf_stdmac[];
1405 static int elf_set_info(enum geninfo type, char **val)
1407 (void)type;
1408 (void)val;
1409 return 0;
1411 static struct dfmt df_dwarf = {
1412 "ELF32 (i386) dwarf debug format for Linux/Unix",
1413 "dwarf",
1414 dwarf32_init,
1415 dwarf32_linenum,
1416 debug32_deflabel,
1417 debug32_directive,
1418 debug32_typevalue,
1419 dwarf32_output,
1420 dwarf32_cleanup
1422 static struct dfmt df_stabs = {
1423 "ELF32 (i386) stabs debug format for Linux/Unix",
1424 "stabs",
1425 null_debug_init,
1426 stabs32_linenum,
1427 debug32_deflabel,
1428 debug32_directive,
1429 debug32_typevalue,
1430 stabs32_output,
1431 stabs32_cleanup
1434 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1436 struct ofmt of_elf32 = {
1437 "ELF32 (i386) object files (e.g. Linux)",
1438 "elf32",
1440 elf32_debugs_arr,
1441 &df_stabs,
1442 elf_stdmac,
1443 elf_init,
1444 elf_set_info,
1445 elf_out,
1446 elf_deflabel,
1447 elf_section_names,
1448 elf_sectalign,
1449 elf_segbase,
1450 elf_directive,
1451 elf_filename,
1452 elf_cleanup
1455 /* again, the stabs debugging stuff (code) */
1457 static void stabs32_linenum(const char *filename, int32_t linenumber,
1458 int32_t segto)
1460 (void)segto;
1462 if (!stabs_filename) {
1463 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1464 strcpy(stabs_filename, filename);
1465 } else {
1466 if (strcmp(stabs_filename, filename)) {
1468 * yep, a memory leak...this program is one-shot anyway, so who cares...
1469 * in fact, this leak comes in quite handy to maintain a list of files
1470 * encountered so far in the symbol lines...
1473 /* why not nasm_free(stabs_filename); we're done with the old one */
1475 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1476 strcpy(stabs_filename, filename);
1479 debug_immcall = 1;
1480 currentline = linenumber;
1483 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1484 char *special)
1486 (void)name;
1487 (void)segment;
1488 (void)offset;
1489 (void)is_global;
1490 (void)special;
1493 static void debug32_directive(const char *directive, const char *params)
1495 (void)directive;
1496 (void)params;
1499 static void debug32_typevalue(int32_t type)
1501 int32_t stype, ssize;
1502 switch (TYM_TYPE(type)) {
1503 case TY_LABEL:
1504 ssize = 0;
1505 stype = STT_NOTYPE;
1506 break;
1507 case TY_BYTE:
1508 ssize = 1;
1509 stype = STT_OBJECT;
1510 break;
1511 case TY_WORD:
1512 ssize = 2;
1513 stype = STT_OBJECT;
1514 break;
1515 case TY_DWORD:
1516 ssize = 4;
1517 stype = STT_OBJECT;
1518 break;
1519 case TY_FLOAT:
1520 ssize = 4;
1521 stype = STT_OBJECT;
1522 break;
1523 case TY_QWORD:
1524 ssize = 8;
1525 stype = STT_OBJECT;
1526 break;
1527 case TY_TBYTE:
1528 ssize = 10;
1529 stype = STT_OBJECT;
1530 break;
1531 case TY_OWORD:
1532 ssize = 16;
1533 stype = STT_OBJECT;
1534 break;
1535 case TY_YWORD:
1536 ssize = 32;
1537 stype = STT_OBJECT;
1538 break;
1539 case TY_COMMON:
1540 ssize = 0;
1541 stype = STT_COMMON;
1542 break;
1543 case TY_SEG:
1544 ssize = 0;
1545 stype = STT_SECTION;
1546 break;
1547 case TY_EXTERN:
1548 ssize = 0;
1549 stype = STT_NOTYPE;
1550 break;
1551 case TY_EQU:
1552 ssize = 0;
1553 stype = STT_NOTYPE;
1554 break;
1555 default:
1556 ssize = 0;
1557 stype = STT_NOTYPE;
1558 break;
1560 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1561 lastsym->size = ssize;
1562 lastsym->type = stype;
1566 static void stabs32_output(int type, void *param)
1568 struct symlininfo *s;
1569 struct linelist *el;
1570 if (type == TY_STABSSYMLIN) {
1571 if (debug_immcall) {
1572 s = (struct symlininfo *)param;
1573 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1574 return; /* line info is only collected for executable sections */
1575 numlinestabs++;
1576 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1577 el->info.offset = s->offset;
1578 el->info.section = s->section;
1579 el->info.name = s->name;
1580 el->line = currentline;
1581 el->filename = stabs_filename;
1582 el->next = 0;
1583 if (stabslines) {
1584 stabslines->last->next = el;
1585 stabslines->last = el;
1586 } else {
1587 stabslines = el;
1588 stabslines->last = el;
1592 debug_immcall = 0;
1595 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1597 static void stabs32_generate(void)
1599 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1600 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1601 char **allfiles;
1602 int *fileidx;
1604 struct linelist *ptr;
1606 ptr = stabslines;
1608 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1609 for (i = 0; i < numlinestabs; i++)
1610 allfiles[i] = 0;
1611 numfiles = 0;
1612 while (ptr) {
1613 if (numfiles == 0) {
1614 allfiles[0] = ptr->filename;
1615 numfiles++;
1616 } else {
1617 for (i = 0; i < numfiles; i++) {
1618 if (!strcmp(allfiles[i], ptr->filename))
1619 break;
1621 if (i >= numfiles) {
1622 allfiles[i] = ptr->filename;
1623 numfiles++;
1626 ptr = ptr->next;
1628 strsize = 1;
1629 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1630 for (i = 0; i < numfiles; i++) {
1631 fileidx[i] = strsize;
1632 strsize += strlen(allfiles[i]) + 1;
1634 mainfileindex = 0;
1635 for (i = 0; i < numfiles; i++) {
1636 if (!strcmp(allfiles[i], elf_module)) {
1637 mainfileindex = i;
1638 break;
1643 * worst case size of the stab buffer would be:
1644 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1645 * plus one "ending" entry
1647 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1648 sizeof(struct stabentry));
1649 ssbuf = (uint8_t *)nasm_malloc(strsize);
1650 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1651 rptr = rbuf;
1653 for (i = 0; i < numfiles; i++)
1654 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1655 ssbuf[0] = 0;
1657 stabstrlen = strsize; /* set global variable for length of stab strings */
1659 sptr = sbuf;
1660 ptr = stabslines;
1661 numstabs = 0;
1663 if (ptr) {
1665 * this is the first stab, its strx points to the filename of the
1666 * the source-file, the n_desc field should be set to the number
1667 * of remaining stabs
1669 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1671 /* this is the stab for the main source file */
1672 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1674 /* relocation table entry */
1677 * Since the symbol table has two entries before
1678 * the section symbols, the index in the info.section
1679 * member must be adjusted by adding 2
1682 WRITELONG(rptr, (sptr - sbuf) - 4);
1683 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1685 numstabs++;
1686 currfile = mainfileindex;
1689 while (ptr) {
1690 if (strcmp(allfiles[currfile], ptr->filename)) {
1691 /* oops file has changed... */
1692 for (i = 0; i < numfiles; i++)
1693 if (!strcmp(allfiles[i], ptr->filename))
1694 break;
1695 currfile = i;
1696 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1697 ptr->info.offset);
1698 numstabs++;
1700 /* relocation table entry */
1701 WRITELONG(rptr, (sptr - sbuf) - 4);
1702 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1705 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1706 numstabs++;
1708 /* relocation table entry */
1710 WRITELONG(rptr, (sptr - sbuf) - 4);
1711 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1713 ptr = ptr->next;
1717 /* this is an "ending" token */
1718 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1719 numstabs++;
1721 ((struct stabentry *)sbuf)->n_desc = numstabs;
1723 nasm_free(allfiles);
1724 nasm_free(fileidx);
1726 stablen = (sptr - sbuf);
1727 stabrellen = (rptr - rbuf);
1728 stabrelbuf = rbuf;
1729 stabbuf = sbuf;
1730 stabstrbuf = ssbuf;
1733 static void stabs32_cleanup(void)
1735 struct linelist *ptr, *del;
1736 if (!stabslines)
1737 return;
1739 ptr = stabslines;
1740 while (ptr) {
1741 del = ptr;
1742 ptr = ptr->next;
1743 nasm_free(del);
1746 nasm_free(stabbuf);
1747 nasm_free(stabrelbuf);
1748 nasm_free(stabstrbuf);
1751 /* dwarf routines */
1753 static void dwarf32_init(void)
1755 ndebugs = 3; /* 3 debug symbols */
1758 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1759 int32_t segto)
1761 (void)segto;
1762 dwarf32_findfile(filename);
1763 debug_immcall = 1;
1764 currentline = linenumber;
1767 /* called from elf_out with type == TY_DEBUGSYMLIN */
1768 static void dwarf32_output(int type, void *param)
1770 int ln, aa, inx, maxln, soc;
1771 struct symlininfo *s;
1772 struct SAA *plinep;
1774 (void)type;
1776 s = (struct symlininfo *)param;
1778 /* line number info is only gathered for executable sections */
1779 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1780 return;
1782 /* Check if section index has changed */
1783 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1784 dwarf32_findsect(s->section);
1786 /* do nothing unless line or file has changed */
1787 if (!debug_immcall)
1788 return;
1790 ln = currentline - dwarf_csect->line;
1791 aa = s->offset - dwarf_csect->offset;
1792 inx = dwarf_clist->line;
1793 plinep = dwarf_csect->psaa;
1794 /* check for file change */
1795 if (!(inx == dwarf_csect->file)) {
1796 saa_write8(plinep,DW_LNS_set_file);
1797 saa_write8(plinep,inx);
1798 dwarf_csect->file = inx;
1800 /* check for line change */
1801 if (ln) {
1802 /* test if in range of special op code */
1803 maxln = line_base + line_range;
1804 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1805 if (ln >= line_base && ln < maxln && soc < 256) {
1806 saa_write8(plinep,soc);
1807 } else {
1808 saa_write8(plinep,DW_LNS_advance_line);
1809 saa_wleb128s(plinep,ln);
1810 if (aa) {
1811 saa_write8(plinep,DW_LNS_advance_pc);
1812 saa_wleb128u(plinep,aa);
1815 dwarf_csect->line = currentline;
1816 dwarf_csect->offset = s->offset;
1819 /* show change handled */
1820 debug_immcall = 0;
1824 static void dwarf32_generate(void)
1826 uint8_t *pbuf;
1827 int indx;
1828 struct linelist *ftentry;
1829 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1830 struct SAA *parangesrel, *plinesrel, *pinforel;
1831 struct sectlist *psect;
1832 size_t saalen, linepoff, totlen, highaddr;
1834 /* write epilogues for each line program range */
1835 /* and build aranges section */
1836 paranges = saa_init(1L);
1837 parangesrel = saa_init(1L);
1838 saa_write16(paranges,2); /* dwarf version */
1839 saa_write32(parangesrel, paranges->datalen+4);
1840 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1841 saa_write32(parangesrel, 0);
1842 saa_write32(paranges,0); /* offset into info */
1843 saa_write8(paranges,4); /* pointer size */
1844 saa_write8(paranges,0); /* not segmented */
1845 saa_write32(paranges,0); /* padding */
1846 /* iterate though sectlist entries */
1847 psect = dwarf_fsect;
1848 totlen = 0;
1849 highaddr = 0;
1850 for (indx = 0; indx < dwarf_nsections; indx++) {
1851 plinep = psect->psaa;
1852 /* Line Number Program Epilogue */
1853 saa_write8(plinep,2); /* std op 2 */
1854 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1855 saa_write8(plinep,DW_LNS_extended_op);
1856 saa_write8(plinep,1); /* operand length */
1857 saa_write8(plinep,DW_LNE_end_sequence);
1858 totlen += plinep->datalen;
1859 /* range table relocation entry */
1860 saa_write32(parangesrel, paranges->datalen + 4);
1861 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1862 saa_write32(parangesrel, (uint32_t) 0);
1863 /* range table entry */
1864 saa_write32(paranges,0x0000); /* range start */
1865 saa_write32(paranges,sects[psect->section]->len); /* range length */
1866 highaddr += sects[psect->section]->len;
1867 /* done with this entry */
1868 psect = psect->next;
1870 saa_write32(paranges,0); /* null address */
1871 saa_write32(paranges,0); /* null length */
1872 saalen = paranges->datalen;
1873 arangeslen = saalen + 4;
1874 arangesbuf = pbuf = nasm_malloc(arangeslen);
1875 WRITELONG(pbuf,saalen); /* initial length */
1876 saa_rnbytes(paranges, pbuf, saalen);
1877 saa_free(paranges);
1879 /* build rela.aranges section */
1880 arangesrellen = saalen = parangesrel->datalen;
1881 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1882 saa_rnbytes(parangesrel, pbuf, saalen);
1883 saa_free(parangesrel);
1885 /* build pubnames section */
1886 ppubnames = saa_init(1L);
1887 saa_write16(ppubnames,3); /* dwarf version */
1888 saa_write32(ppubnames,0); /* offset into info */
1889 saa_write32(ppubnames,0); /* space used in info */
1890 saa_write32(ppubnames,0); /* end of list */
1891 saalen = ppubnames->datalen;
1892 pubnameslen = saalen + 4;
1893 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1894 WRITELONG(pbuf,saalen); /* initial length */
1895 saa_rnbytes(ppubnames, pbuf, saalen);
1896 saa_free(ppubnames);
1898 /* build info section */
1899 pinfo = saa_init(1L);
1900 pinforel = saa_init(1L);
1901 saa_write16(pinfo,2); /* dwarf version */
1902 saa_write32(pinforel, pinfo->datalen + 4);
1903 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1904 saa_write32(pinforel, 0);
1905 saa_write32(pinfo,0); /* offset into abbrev */
1906 saa_write8(pinfo,4); /* pointer size */
1907 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1908 saa_write32(pinforel, pinfo->datalen + 4);
1909 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1910 saa_write32(pinforel, 0);
1911 saa_write32(pinfo,0); /* DW_AT_low_pc */
1912 saa_write32(pinforel, pinfo->datalen + 4);
1913 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1914 saa_write32(pinforel, 0);
1915 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1916 saa_write32(pinforel, pinfo->datalen + 4);
1917 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1918 saa_write32(pinforel, 0);
1919 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1920 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1921 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1922 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1923 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1924 saa_write32(pinforel, pinfo->datalen + 4);
1925 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1926 saa_write32(pinforel, 0);
1927 saa_write32(pinfo,0); /* DW_AT_low_pc */
1928 saa_write32(pinfo,0); /* DW_AT_frame_base */
1929 saa_write8(pinfo,0); /* end of entries */
1930 saalen = pinfo->datalen;
1931 infolen = saalen + 4;
1932 infobuf = pbuf = nasm_malloc(infolen);
1933 WRITELONG(pbuf,saalen); /* initial length */
1934 saa_rnbytes(pinfo, pbuf, saalen);
1935 saa_free(pinfo);
1937 /* build rela.info section */
1938 inforellen = saalen = pinforel->datalen;
1939 inforelbuf = pbuf = nasm_malloc(inforellen);
1940 saa_rnbytes(pinforel, pbuf, saalen);
1941 saa_free(pinforel);
1943 /* build abbrev section */
1944 pabbrev = saa_init(1L);
1945 saa_write8(pabbrev,1); /* entry number LEB128u */
1946 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1947 saa_write8(pabbrev,1); /* has children */
1948 /* the following attributes and forms are all LEB128u values */
1949 saa_write8(pabbrev,DW_AT_low_pc);
1950 saa_write8(pabbrev,DW_FORM_addr);
1951 saa_write8(pabbrev,DW_AT_high_pc);
1952 saa_write8(pabbrev,DW_FORM_addr);
1953 saa_write8(pabbrev,DW_AT_stmt_list);
1954 saa_write8(pabbrev,DW_FORM_data4);
1955 saa_write8(pabbrev,DW_AT_name);
1956 saa_write8(pabbrev,DW_FORM_string);
1957 saa_write8(pabbrev,DW_AT_producer);
1958 saa_write8(pabbrev,DW_FORM_string);
1959 saa_write8(pabbrev,DW_AT_language);
1960 saa_write8(pabbrev,DW_FORM_data2);
1961 saa_write16(pabbrev,0); /* end of entry */
1962 /* LEB128u usage same as above */
1963 saa_write8(pabbrev,2); /* entry number */
1964 saa_write8(pabbrev,DW_TAG_subprogram);
1965 saa_write8(pabbrev,0); /* no children */
1966 saa_write8(pabbrev,DW_AT_low_pc);
1967 saa_write8(pabbrev,DW_FORM_addr);
1968 saa_write8(pabbrev,DW_AT_frame_base);
1969 saa_write8(pabbrev,DW_FORM_data4);
1970 saa_write16(pabbrev,0); /* end of entry */
1971 abbrevlen = saalen = pabbrev->datalen;
1972 abbrevbuf = pbuf = nasm_malloc(saalen);
1973 saa_rnbytes(pabbrev, pbuf, saalen);
1974 saa_free(pabbrev);
1976 /* build line section */
1977 /* prolog */
1978 plines = saa_init(1L);
1979 saa_write8(plines,1); /* Minimum Instruction Length */
1980 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1981 saa_write8(plines,line_base); /* Line Base */
1982 saa_write8(plines,line_range); /* Line Range */
1983 saa_write8(plines,opcode_base); /* Opcode Base */
1984 /* standard opcode lengths (# of LEB128u operands) */
1985 saa_write8(plines,0); /* Std opcode 1 length */
1986 saa_write8(plines,1); /* Std opcode 2 length */
1987 saa_write8(plines,1); /* Std opcode 3 length */
1988 saa_write8(plines,1); /* Std opcode 4 length */
1989 saa_write8(plines,1); /* Std opcode 5 length */
1990 saa_write8(plines,0); /* Std opcode 6 length */
1991 saa_write8(plines,0); /* Std opcode 7 length */
1992 saa_write8(plines,0); /* Std opcode 8 length */
1993 saa_write8(plines,1); /* Std opcode 9 length */
1994 saa_write8(plines,0); /* Std opcode 10 length */
1995 saa_write8(plines,0); /* Std opcode 11 length */
1996 saa_write8(plines,1); /* Std opcode 12 length */
1997 /* Directory Table */
1998 saa_write8(plines,0); /* End of table */
1999 /* File Name Table */
2000 ftentry = dwarf_flist;
2001 for (indx = 0; indx < dwarf_numfiles; indx++) {
2002 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2003 saa_write8(plines,0); /* directory LEB128u */
2004 saa_write8(plines,0); /* time LEB128u */
2005 saa_write8(plines,0); /* size LEB128u */
2006 ftentry = ftentry->next;
2008 saa_write8(plines,0); /* End of table */
2009 linepoff = plines->datalen;
2010 linelen = linepoff + totlen + 10;
2011 linebuf = pbuf = nasm_malloc(linelen);
2012 WRITELONG(pbuf,linelen-4); /* initial length */
2013 WRITESHORT(pbuf,3); /* dwarf version */
2014 WRITELONG(pbuf,linepoff); /* offset to line number program */
2015 /* write line header */
2016 saalen = linepoff;
2017 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2018 pbuf += linepoff;
2019 saa_free(plines);
2020 /* concatonate line program ranges */
2021 linepoff += 13;
2022 plinesrel = saa_init(1L);
2023 psect = dwarf_fsect;
2024 for (indx = 0; indx < dwarf_nsections; indx++) {
2025 saa_write32(plinesrel, linepoff);
2026 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2027 saa_write32(plinesrel, (uint32_t) 0);
2028 plinep = psect->psaa;
2029 saalen = plinep->datalen;
2030 saa_rnbytes(plinep, pbuf, saalen);
2031 pbuf += saalen;
2032 linepoff += saalen;
2033 saa_free(plinep);
2034 /* done with this entry */
2035 psect = psect->next;
2039 /* build rela.lines section */
2040 linerellen =saalen = plinesrel->datalen;
2041 linerelbuf = pbuf = nasm_malloc(linerellen);
2042 saa_rnbytes(plinesrel, pbuf, saalen);
2043 saa_free(plinesrel);
2045 /* build frame section */
2046 framelen = 4;
2047 framebuf = pbuf = nasm_malloc(framelen);
2048 WRITELONG(pbuf,framelen-4); /* initial length */
2050 /* build loc section */
2051 loclen = 16;
2052 locbuf = pbuf = nasm_malloc(loclen);
2053 WRITELONG(pbuf,0); /* null beginning offset */
2054 WRITELONG(pbuf,0); /* null ending offset */
2057 static void dwarf32_cleanup(void)
2059 nasm_free(arangesbuf);
2060 nasm_free(arangesrelbuf);
2061 nasm_free(pubnamesbuf);
2062 nasm_free(infobuf);
2063 nasm_free(inforelbuf);
2064 nasm_free(abbrevbuf);
2065 nasm_free(linebuf);
2066 nasm_free(linerelbuf);
2067 nasm_free(framebuf);
2068 nasm_free(locbuf);
2071 static void dwarf32_findfile(const char * fname)
2073 int finx;
2074 struct linelist *match;
2076 /* return if fname is current file name */
2077 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2078 return;
2080 /* search for match */
2081 match = 0;
2082 if (dwarf_flist) {
2083 match = dwarf_flist;
2084 for (finx = 0; finx < dwarf_numfiles; finx++) {
2085 if (!(strcmp(fname, match->filename))) {
2086 dwarf_clist = match;
2087 return;
2092 /* add file name to end of list */
2093 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2094 dwarf_numfiles++;
2095 dwarf_clist->line = dwarf_numfiles;
2096 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2097 strcpy(dwarf_clist->filename,fname);
2098 dwarf_clist->next = 0;
2099 if (!dwarf_flist) { /* if first entry */
2100 dwarf_flist = dwarf_elist = dwarf_clist;
2101 dwarf_clist->last = 0;
2102 } else { /* chain to previous entry */
2103 dwarf_elist->next = dwarf_clist;
2104 dwarf_elist = dwarf_clist;
2108 static void dwarf32_findsect(const int index)
2110 int sinx;
2111 struct sectlist *match;
2112 struct SAA *plinep;
2114 /* return if index is current section index */
2115 if (dwarf_csect && (dwarf_csect->section == index))
2116 return;
2118 /* search for match */
2119 match = 0;
2120 if (dwarf_fsect) {
2121 match = dwarf_fsect;
2122 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2123 if ((match->section == index)) {
2124 dwarf_csect = match;
2125 return;
2127 match = match->next;
2131 /* add entry to end of list */
2132 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2133 dwarf_nsections++;
2134 dwarf_csect->psaa = plinep = saa_init(1L);
2135 dwarf_csect->line = 1;
2136 dwarf_csect->offset = 0;
2137 dwarf_csect->file = 1;
2138 dwarf_csect->section = index;
2139 dwarf_csect->next = 0;
2140 /* set relocatable address at start of line program */
2141 saa_write8(plinep,DW_LNS_extended_op);
2142 saa_write8(plinep,5); /* operand length */
2143 saa_write8(plinep,DW_LNE_set_address);
2144 saa_write32(plinep,0); /* Start Address */
2146 if (!dwarf_fsect) { /* if first entry */
2147 dwarf_fsect = dwarf_esect = dwarf_csect;
2148 dwarf_csect->last = 0;
2149 } else { /* chain to previous entry */
2150 dwarf_esect->next = dwarf_csect;
2151 dwarf_esect = dwarf_csect;
2155 #endif /* OF_ELF */