NASM 2.09rc4
[nasm/avx512.git] / output / outelf32.c
blob14df371f16529e88139dbaf80dc2993cf6568fbe
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 stabentry {
146 uint32_t n_strx;
147 uint8_t n_type;
148 uint8_t n_other;
149 uint16_t n_desc;
150 uint32_t n_value;
153 struct erel {
154 int offset, info;
157 struct symlininfo {
158 int offset;
159 int section; /* section index */
160 char *name; /* shallow-copied pointer of section name */
163 struct linelist {
164 struct symlininfo info;
165 int line;
166 char *filename;
167 struct linelist *next;
168 struct linelist *last;
171 struct sectlist {
172 struct SAA *psaa;
173 int section;
174 int line;
175 int offset;
176 int file;
177 struct sectlist *next;
178 struct sectlist *last;
181 /* common debug variables */
182 static int currentline = 1;
183 static int debug_immcall = 0;
185 /* stabs debug variables */
186 static struct linelist *stabslines = 0;
187 static int numlinestabs = 0;
188 static char *stabs_filename = 0;
189 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
190 static int stablen, stabstrlen, stabrellen;
192 /* dwarf debug variables */
193 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
194 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
195 static int dwarf_numfiles = 0, dwarf_nsections;
196 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
197 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
198 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
199 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
200 abbrevlen, linelen, linerellen, framelen, loclen;
201 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
203 static struct dfmt df_dwarf;
204 static struct dfmt df_stabs;
205 static struct Symbol *lastsym;
207 /* common debugging routines */
208 static void debug32_typevalue(int32_t);
209 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
210 static void debug32_directive(const char *, const char *);
212 /* stabs debugging routines */
213 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
214 static void stabs32_output(int, void *);
215 static void stabs32_generate(void);
216 static void stabs32_cleanup(void);
218 /* dwarf debugging routines */
219 static void dwarf32_init(void);
220 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
221 static void dwarf32_output(int, void *);
222 static void dwarf32_generate(void);
223 static void dwarf32_cleanup(void);
224 static void dwarf32_findfile(const char *);
225 static void dwarf32_findsect(const int);
228 * Special NASM section numbers which are used to define ELF special
229 * symbols, which can be used with WRT to provide PIC and TLS
230 * relocation types.
232 static int32_t elf_gotpc_sect, elf_gotoff_sect;
233 static int32_t elf_got_sect, elf_plt_sect;
234 static int32_t elf_sym_sect, elf_tlsie_sect;
236 static void elf_init(void)
238 sects = NULL;
239 nsects = sectlen = 0;
240 syms = saa_init((int32_t)sizeof(struct Symbol));
241 nlocals = nglobs = ndebugs = 0;
242 bsym = raa_init();
243 strs = saa_init(1L);
244 saa_wbytes(strs, "\0", 1L);
245 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
246 strslen = 2 + strlen(elf_module);
247 shstrtab = NULL;
248 shstrtablen = shstrtabsize = 0;;
249 add_sectname("", "");
251 fwds = NULL;
253 elf_gotpc_sect = seg_alloc();
254 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
255 elf_gotoff_sect = seg_alloc();
256 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
257 elf_got_sect = seg_alloc();
258 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
259 elf_plt_sect = seg_alloc();
260 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
261 elf_sym_sect = seg_alloc();
262 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
263 elf_tlsie_sect = seg_alloc();
264 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
266 def_seg = seg_alloc();
269 static void elf_cleanup(int debuginfo)
271 struct Reloc *r;
272 int i;
274 (void)debuginfo;
276 elf_write();
277 for (i = 0; i < nsects; i++) {
278 if (sects[i]->type != SHT_NOBITS)
279 saa_free(sects[i]->data);
280 if (sects[i]->head)
281 saa_free(sects[i]->rel);
282 while (sects[i]->head) {
283 r = sects[i]->head;
284 sects[i]->head = sects[i]->head->next;
285 nasm_free(r);
288 nasm_free(sects);
289 saa_free(syms);
290 raa_free(bsym);
291 saa_free(strs);
292 if (of_elf32.current_dfmt) {
293 of_elf32.current_dfmt->cleanup();
297 static void add_sectname(char *firsthalf, char *secondhalf)
299 int len = strlen(firsthalf) + strlen(secondhalf);
300 while (shstrtablen + len + 1 > shstrtabsize)
301 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
302 strcpy(shstrtab + shstrtablen, firsthalf);
303 strcat(shstrtab + shstrtablen, secondhalf);
304 shstrtablen += len + 1;
307 static int elf_make_section(char *name, int type, int flags, int align)
309 struct Section *s;
311 s = nasm_malloc(sizeof(*s));
313 if (type != SHT_NOBITS)
314 s->data = saa_init(1L);
315 s->head = NULL;
316 s->tail = &s->head;
317 s->len = s->size = 0;
318 s->nrelocs = 0;
319 if (!strcmp(name, ".text"))
320 s->index = def_seg;
321 else
322 s->index = seg_alloc();
323 add_sectname("", name);
324 s->name = nasm_malloc(1 + strlen(name));
325 strcpy(s->name, name);
326 s->type = type;
327 s->flags = flags;
328 s->align = align;
329 s->gsyms = NULL;
331 if (nsects >= sectlen)
332 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
333 sects[nsects++] = s;
335 return nsects - 1;
338 static int32_t elf_section_names(char *name, int pass, int *bits)
340 char *p;
341 uint32_t flags, flags_and, flags_or;
342 uint64_t align;
343 int type, i;
346 * Default is 32 bits.
348 if (!name) {
349 *bits = 32;
350 return def_seg;
353 p = nasm_skip_word(name);
354 if (*p)
355 *p++ = '\0';
356 flags_and = flags_or = type = align = 0;
358 section_attrib(name, p, pass, &flags_and,
359 &flags_or, &align, &type);
361 if (!strcmp(name, ".shstrtab") ||
362 !strcmp(name, ".symtab") ||
363 !strcmp(name, ".strtab")) {
364 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
365 "name `%s'", name);
366 return NO_SEG;
369 for (i = 0; i < nsects; i++)
370 if (!strcmp(name, sects[i]->name))
371 break;
372 if (i == nsects) {
373 const struct elf_known_section *ks = elf_known_sections;
375 while (ks->name) {
376 if (!strcmp(name, ks->name))
377 break;
378 ks++;
381 type = type ? type : ks->type;
382 align = align ? align : ks->align;
383 flags = (ks->flags & ~flags_and) | flags_or;
385 i = elf_make_section(name, type, flags, align);
386 } else if (pass == 1) {
387 if ((type && sects[i]->type != type)
388 || (align && sects[i]->align != align)
389 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
390 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
391 " redeclaration of section `%s'", name);
394 return sects[i]->index;
397 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
398 int is_global, char *special)
400 int pos = strslen;
401 struct Symbol *sym;
402 bool special_used = false;
404 #if defined(DEBUG) && DEBUG>2
405 nasm_error(ERR_DEBUG,
406 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
407 name, segment, offset, is_global, special);
408 #endif
409 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
411 * This is a NASM special symbol. We never allow it into
412 * the ELF symbol table, even if it's a valid one. If it
413 * _isn't_ a valid one, we should barf immediately.
415 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
416 strcmp(name, "..got") && strcmp(name, "..plt") &&
417 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
418 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
419 return;
422 if (is_global == 3) {
423 struct Symbol **s;
425 * Fix up a forward-reference symbol size from the first
426 * pass.
428 for (s = &fwds; *s; s = &(*s)->nextfwd)
429 if (!strcmp((*s)->name, name)) {
430 struct tokenval tokval;
431 expr *e;
432 char *p = nasm_skip_spaces(nasm_skip_word(special));
434 stdscan_reset();
435 stdscan_set(p);
436 tokval.t_type = TOKEN_INVALID;
437 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
438 if (e) {
439 if (!is_simple(e))
440 nasm_error(ERR_NONFATAL, "cannot use relocatable"
441 " expression as symbol size");
442 else
443 (*s)->size = reloc_value(e);
447 * Remove it from the list of unresolved sizes.
449 nasm_free((*s)->name);
450 *s = (*s)->nextfwd;
451 return;
453 return; /* it wasn't an important one */
456 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
457 strslen += 1 + strlen(name);
459 lastsym = sym = saa_wstruct(syms);
461 memset(&sym->symv, 0, sizeof(struct rbtree));
463 sym->strpos = pos;
464 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
465 sym->other = STV_DEFAULT;
466 sym->size = 0;
467 if (segment == NO_SEG)
468 sym->section = SHN_ABS;
469 else {
470 int i;
471 sym->section = SHN_UNDEF;
472 if (segment == def_seg) {
473 /* we have to be sure at least text section is there */
474 int tempint;
475 if (segment != elf_section_names(".text", 2, &tempint))
476 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
478 for (i = 0; i < nsects; i++) {
479 if (segment == sects[i]->index) {
480 sym->section = i + 1;
481 break;
486 if (is_global == 2) {
487 sym->size = offset;
488 sym->symv.key = 0;
489 sym->section = SHN_COMMON;
491 * We have a common variable. Check the special text to see
492 * if it's a valid number and power of two; if so, store it
493 * as the alignment for the common variable.
495 if (special) {
496 bool err;
497 sym->symv.key = readnum(special, &err);
498 if (err)
499 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
500 " valid number", special);
501 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
502 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
503 " power of two", special);
505 special_used = true;
506 } else
507 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
509 if (sym->type == SYM_GLOBAL) {
511 * If sym->section == SHN_ABS, then the first line of the
512 * else section would cause a core dump, because its a reference
513 * beyond the end of the section array.
514 * This behaviour is exhibited by this code:
515 * GLOBAL crash_nasm
516 * crash_nasm equ 0
517 * To avoid such a crash, such requests are silently discarded.
518 * This may not be the best solution.
520 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
521 bsym = raa_write(bsym, segment, nglobs);
522 } else if (sym->section != SHN_ABS) {
524 * This is a global symbol; so we must add it to the rbtree
525 * of global symbols in its section.
527 * In addition, we check the special text for symbol
528 * type and size information.
530 sects[sym->section-1]->gsyms =
531 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
533 if (special) {
534 int n = strcspn(special, " \t");
536 if (!nasm_strnicmp(special, "function", n))
537 sym->type |= STT_FUNC;
538 else if (!nasm_strnicmp(special, "data", n) ||
539 !nasm_strnicmp(special, "object", n))
540 sym->type |= STT_OBJECT;
541 else if (!nasm_strnicmp(special, "notype", n))
542 sym->type |= STT_NOTYPE;
543 else
544 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
545 n, special);
546 special += n;
548 special = nasm_skip_spaces(special);
549 if (*special) {
550 n = strcspn(special, " \t");
551 if (!nasm_strnicmp(special, "default", n))
552 sym->other = STV_DEFAULT;
553 else if (!nasm_strnicmp(special, "internal", n))
554 sym->other = STV_INTERNAL;
555 else if (!nasm_strnicmp(special, "hidden", n))
556 sym->other = STV_HIDDEN;
557 else if (!nasm_strnicmp(special, "protected", n))
558 sym->other = STV_PROTECTED;
559 else
560 n = 0;
561 special += n;
564 if (*special) {
565 struct tokenval tokval;
566 expr *e;
567 int fwd = 0;
568 char *saveme = stdscan_get();
570 while (special[n] && nasm_isspace(special[n]))
571 n++;
573 * We have a size expression; attempt to
574 * evaluate it.
576 stdscan_reset();
577 stdscan_set(special + n);
578 tokval.t_type = TOKEN_INVALID;
579 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
580 NULL);
581 if (fwd) {
582 sym->nextfwd = fwds;
583 fwds = sym;
584 sym->name = nasm_strdup(name);
585 } else if (e) {
586 if (!is_simple(e))
587 nasm_error(ERR_NONFATAL, "cannot use relocatable"
588 " expression as symbol size");
589 else
590 sym->size = reloc_value(e);
592 stdscan_set(saveme);
594 special_used = true;
597 * If TLS segment, mark symbol accordingly.
599 if (sects[sym->section - 1]->flags & SHF_TLS) {
600 sym->type &= 0xf0;
601 sym->type |= STT_TLS;
604 sym->globnum = nglobs;
605 nglobs++;
606 } else
607 nlocals++;
609 if (special && !special_used)
610 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
613 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
615 struct Reloc *r;
617 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
618 sect->tail = &r->next;
619 r->next = NULL;
621 r->address = sect->len;
622 if (segment == NO_SEG)
623 r->symbol = 0;
624 else {
625 int i;
626 r->symbol = 0;
627 for (i = 0; i < nsects; i++)
628 if (segment == sects[i]->index)
629 r->symbol = i + 2;
630 if (!r->symbol)
631 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
633 r->type = type;
635 sect->nrelocs++;
639 * This routine deals with ..got and ..sym relocations: the more
640 * complicated kinds. In shared-library writing, some relocations
641 * with respect to global symbols must refer to the precise symbol
642 * rather than referring to an offset from the base of the section
643 * _containing_ the symbol. Such relocations call to this routine,
644 * which searches the symbol list for the symbol in question.
646 * R_386_GOT32 references require the _exact_ symbol address to be
647 * used; R_386_32 references can be at an offset from the symbol.
648 * The boolean argument `exact' tells us this.
650 * Return value is the adjusted value of `addr', having become an
651 * offset from the symbol rather than the section. Should always be
652 * zero when returning from an exact call.
654 * Limitation: if you define two symbols at the same place,
655 * confusion will occur.
657 * Inefficiency: we search, currently, using a linked list which
658 * isn't even necessarily sorted.
660 static int32_t elf_add_gsym_reloc(struct Section *sect,
661 int32_t segment, uint32_t offset,
662 int type, bool exact)
664 struct Reloc *r;
665 struct Section *s;
666 struct Symbol *sym;
667 struct rbtree *srb;
668 int i;
671 * First look up the segment/offset pair and find a global
672 * symbol corresponding to it. If it's not one of our segments,
673 * then it must be an external symbol, in which case we're fine
674 * doing a normal elf_add_reloc after first sanity-checking
675 * that the offset from the symbol is zero.
677 s = NULL;
678 for (i = 0; i < nsects; i++)
679 if (segment == sects[i]->index) {
680 s = sects[i];
681 break;
683 if (!s) {
684 if (exact && offset != 0)
685 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
686 " for this reference");
687 else
688 elf_add_reloc(sect, segment, type);
689 return offset;
692 srb = rb_search(s->gsyms, offset);
693 if (!srb || (exact && srb->key != offset)) {
694 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
695 " for this reference");
696 return 0;
698 sym = container_of(srb, struct Symbol, symv);
700 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
701 sect->tail = &r->next;
702 r->next = NULL;
704 r->address = sect->len;
705 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
706 r->type = type;
708 sect->nrelocs++;
710 return offset - sym->symv.key;
713 static void elf_out(int32_t segto, const void *data,
714 enum out_type type, uint64_t size,
715 int32_t segment, int32_t wrt)
717 struct Section *s;
718 int32_t addr;
719 uint8_t mydata[8], *p;
720 int reltype, bytes;
721 int i;
722 static struct symlininfo sinfo;
725 * handle absolute-assembly (structure definitions)
727 if (segto == NO_SEG) {
728 if (type != OUT_RESERVE)
729 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
730 " space");
731 return;
734 s = NULL;
735 for (i = 0; i < nsects; i++)
736 if (segto == sects[i]->index) {
737 s = sects[i];
738 break;
740 if (!s) {
741 int tempint; /* ignored */
742 if (segto != elf_section_names(".text", 2, &tempint))
743 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
744 else {
745 s = sects[nsects - 1];
746 i = nsects - 1;
750 /* again some stabs debugging stuff */
751 if (of_elf32.current_dfmt) {
752 sinfo.offset = s->len;
753 sinfo.section = i;
754 sinfo.name = s->name;
755 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
757 /* end of debugging stuff */
759 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
760 nasm_error(ERR_WARNING, "attempt to initialize memory in"
761 " BSS section `%s': ignored", s->name);
762 s->len += realsize(type, size);
763 return;
766 switch (type) {
767 case OUT_RESERVE:
768 if (s->type == SHT_PROGBITS) {
769 nasm_error(ERR_WARNING, "uninitialized space declared in"
770 " non-BSS section `%s': zeroing", s->name);
771 elf_sect_write(s, NULL, size);
772 } else
773 s->len += size;
774 break;
776 case OUT_RAWDATA:
777 if (segment != NO_SEG)
778 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
779 elf_sect_write(s, data, size);
780 break;
782 case OUT_ADDRESS:
784 bool gnu16 = false;
785 addr = *(int64_t *)data;
786 if (segment != NO_SEG) {
787 if (segment % 2) {
788 nasm_error(ERR_NONFATAL, "ELF format does not support"
789 " segment base references");
790 } else {
791 if (wrt == NO_SEG) {
793 * The if() is a hack to deal with compilers which
794 * don't handle switch() statements with 64-bit
795 * expressions.
797 if (size < UINT_MAX) {
798 switch ((unsigned int)size) {
799 case 1:
800 gnu16 = true;
801 elf_add_reloc(s, segment, R_386_8);
802 break;
803 case 2:
804 gnu16 = true;
805 elf_add_reloc(s, segment, R_386_16);
806 break;
807 case 4:
808 elf_add_reloc(s, segment, R_386_32);
809 break;
810 default: /* Error issued further down */
811 break;
814 } else if (wrt == elf_gotpc_sect + 1) {
816 * The user will supply GOT relative to $$. ELF
817 * will let us have GOT relative to $. So we
818 * need to fix up the data item by $-$$.
820 addr += s->len;
821 elf_add_reloc(s, segment, R_386_GOTPC);
822 } else if (wrt == elf_gotoff_sect + 1) {
823 elf_add_reloc(s, segment, R_386_GOTOFF);
824 } else if (wrt == elf_tlsie_sect + 1) {
825 addr = elf_add_gsym_reloc(s, segment, addr,
826 R_386_TLS_IE, true);
827 } else if (wrt == elf_got_sect + 1) {
828 addr = elf_add_gsym_reloc(s, segment, addr,
829 R_386_GOT32, true);
830 } else if (wrt == elf_sym_sect + 1) {
831 if (size == 2) {
832 gnu16 = true;
833 addr = elf_add_gsym_reloc(s, segment, addr,
834 R_386_16, false);
835 } else {
836 addr = elf_add_gsym_reloc(s, segment, addr,
837 R_386_32, false);
839 } else if (wrt == elf_plt_sect + 1) {
840 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
841 "relative PLT references");
842 } else {
843 nasm_error(ERR_NONFATAL, "ELF format does not support this"
844 " use of WRT");
845 wrt = NO_SEG; /* we can at least _try_ to continue */
849 p = mydata;
850 if (gnu16) {
851 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
852 "8- or 16-bit relocations in ELF32 is a GNU extension");
853 } else if (size != 4 && segment != NO_SEG) {
854 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
856 WRITEADDR(p, addr, size);
857 elf_sect_write(s, mydata, size);
858 break;
861 case OUT_REL1ADR:
862 bytes = 1;
863 reltype = R_386_PC8;
864 goto rel12adr;
865 case OUT_REL2ADR:
866 bytes = 2;
867 reltype = R_386_PC16;
868 goto rel12adr;
870 rel12adr:
871 nasm_assert(segment != segto);
872 if (segment != NO_SEG && segment % 2) {
873 nasm_error(ERR_NONFATAL, "ELF format does not support"
874 " segment base references");
875 } else {
876 if (wrt == NO_SEG) {
877 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
878 "8- or 16-bit relocations in ELF is a GNU extension");
879 elf_add_reloc(s, segment, reltype);
880 } else {
881 nasm_error(ERR_NONFATAL,
882 "Unsupported non-32-bit ELF relocation");
885 p = mydata;
886 WRITESHORT(p, *(int64_t *)data - size);
887 elf_sect_write(s, mydata, bytes);
888 break;
890 case OUT_REL4ADR:
891 if (segment == segto)
892 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
893 if (segment != NO_SEG && segment % 2) {
894 nasm_error(ERR_NONFATAL, "ELF format does not support"
895 " segment base references");
896 } else {
897 if (wrt == NO_SEG) {
898 elf_add_reloc(s, segment, R_386_PC32);
899 } else if (wrt == elf_plt_sect + 1) {
900 elf_add_reloc(s, segment, R_386_PLT32);
901 } else if (wrt == elf_gotpc_sect + 1 ||
902 wrt == elf_gotoff_sect + 1 ||
903 wrt == elf_got_sect + 1) {
904 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
905 "relative GOT references");
906 } else {
907 nasm_error(ERR_NONFATAL, "ELF format does not support this"
908 " use of WRT");
909 wrt = NO_SEG; /* we can at least _try_ to continue */
912 p = mydata;
913 WRITELONG(p, *(int64_t *)data - size);
914 elf_sect_write(s, mydata, 4L);
915 break;
917 case OUT_REL8ADR:
918 nasm_error(ERR_NONFATAL,
919 "32-bit ELF format does not support 64-bit relocations");
920 p = mydata;
921 WRITEDLONG(p, 0);
922 elf_sect_write(s, mydata, 8L);
923 break;
927 static void elf_write(void)
929 int align;
930 char *p;
931 int i;
933 struct SAA *symtab;
934 int32_t symtablen, symtablocal;
937 * Work out how many sections we will have. We have SHN_UNDEF,
938 * then the flexible user sections, then the fixed sections
939 * `.shstrtab', `.symtab' and `.strtab', then optionally
940 * relocation sections for the user sections.
942 nsections = sec_numspecial + 1;
943 if (of_elf32.current_dfmt == &df_stabs)
944 nsections += 3;
945 else if (of_elf32.current_dfmt == &df_dwarf)
946 nsections += 10;
948 add_sectname("", ".shstrtab");
949 add_sectname("", ".symtab");
950 add_sectname("", ".strtab");
951 for (i = 0; i < nsects; i++) {
952 nsections++; /* for the section itself */
953 if (sects[i]->head) {
954 nsections++; /* for its relocations */
955 add_sectname(".rel", sects[i]->name);
959 if (of_elf32.current_dfmt == &df_stabs) {
960 /* in case the debug information is wanted, just add these three sections... */
961 add_sectname("", ".stab");
962 add_sectname("", ".stabstr");
963 add_sectname(".rel", ".stab");
964 } else if (of_elf32.current_dfmt == &df_dwarf) {
965 /* the dwarf debug standard specifies the following ten sections,
966 not all of which are currently implemented,
967 although all of them are defined. */
968 add_sectname("", ".debug_aranges");
969 add_sectname(".rela", ".debug_aranges");
970 add_sectname("", ".debug_pubnames");
971 add_sectname("", ".debug_info");
972 add_sectname(".rela", ".debug_info");
973 add_sectname("", ".debug_abbrev");
974 add_sectname("", ".debug_line");
975 add_sectname(".rela", ".debug_line");
976 add_sectname("", ".debug_frame");
977 add_sectname("", ".debug_loc");
981 * Output the ELF header.
983 fwrite("\177ELF\1\1\1", 7, 1, ofile);
984 fputc(elf_osabi, ofile);
985 fputc(elf_abiver, ofile);
986 fwritezero(7, ofile);
987 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
988 fwriteint16_t(3, ofile); /* EM_386 processor ID */
989 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
990 fwriteint32_t(0L, ofile); /* no entry point */
991 fwriteint32_t(0L, ofile); /* no program header table */
992 fwriteint32_t(0x40L, ofile); /* section headers straight after
993 * ELF header plus alignment */
994 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
995 fwriteint16_t(0x34, ofile); /* size of ELF header */
996 fwriteint16_t(0, ofile); /* no program header table, again */
997 fwriteint16_t(0, ofile); /* still no program header table */
998 fwriteint16_t(0x28, ofile); /* size of section header */
999 fwriteint16_t(nsections, ofile); /* number of sections */
1000 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1001 * section header table */
1002 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1003 fwriteint32_t(0L, ofile);
1004 fwriteint32_t(0L, ofile);
1007 * Build the symbol table and relocation tables.
1009 symtab = elf_build_symtab(&symtablen, &symtablocal);
1010 for (i = 0; i < nsects; i++)
1011 if (sects[i]->head)
1012 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1013 sects[i]->head);
1016 * Now output the section header table.
1019 elf_foffs = 0x40 + 0x28 * nsections;
1020 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1021 elf_foffs += align;
1022 elf_nsect = 0;
1023 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1025 /* SHN_UNDEF */
1026 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1027 p = shstrtab + 1;
1029 /* The normal sections */
1030 for (i = 0; i < nsects; i++) {
1031 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1032 (sects[i]->type == SHT_PROGBITS ?
1033 sects[i]->data : NULL), true,
1034 sects[i]->len, 0, 0, sects[i]->align, 0);
1035 p += strlen(p) + 1;
1038 /* .shstrtab */
1039 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1040 shstrtablen, 0, 0, 1, 0);
1041 p += strlen(p) + 1;
1043 /* .symtab */
1044 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1045 symtablen, sec_strtab, symtablocal, 4, 16);
1046 p += strlen(p) + 1;
1048 /* .strtab */
1049 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1050 strslen, 0, 0, 1, 0);
1051 p += strlen(p) + 1;
1053 /* The relocation sections */
1054 for (i = 0; i < nsects; i++)
1055 if (sects[i]->head) {
1056 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1057 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1058 p += strlen(p) + 1;
1061 if (of_elf32.current_dfmt == &df_stabs) {
1062 /* for debugging information, create the last three sections
1063 which are the .stab , .stabstr and .rel.stab sections respectively */
1065 /* this function call creates the stab sections in memory */
1066 stabs32_generate();
1068 if (stabbuf && stabstrbuf && stabrelbuf) {
1069 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1070 stablen, sec_stabstr, 0, 4, 12);
1071 p += strlen(p) + 1;
1073 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1074 stabstrlen, 0, 0, 4, 0);
1075 p += strlen(p) + 1;
1077 /* link -> symtable info -> section to refer to */
1078 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1079 stabrellen, sec_symtab, sec_stab, 4, 8);
1080 p += strlen(p) + 1;
1082 } else if (of_elf32.current_dfmt == &df_dwarf) {
1083 /* for dwarf debugging information, create the ten dwarf sections */
1085 /* this function call creates the dwarf sections in memory */
1086 if (dwarf_fsect)
1087 dwarf32_generate();
1089 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1090 arangeslen, 0, 0, 1, 0);
1091 p += strlen(p) + 1;
1093 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1094 arangesrellen, sec_symtab, sec_debug_aranges,
1095 1, 12);
1096 p += strlen(p) + 1;
1098 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1099 false, pubnameslen, 0, 0, 1, 0);
1100 p += strlen(p) + 1;
1102 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1103 infolen, 0, 0, 1, 0);
1104 p += strlen(p) + 1;
1106 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1107 inforellen, sec_symtab, sec_debug_info, 1, 12);
1108 p += strlen(p) + 1;
1110 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1111 abbrevlen, 0, 0, 1, 0);
1112 p += strlen(p) + 1;
1114 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1115 linelen, 0, 0, 1, 0);
1116 p += strlen(p) + 1;
1118 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1119 linerellen, sec_symtab, sec_debug_line, 1, 12);
1120 p += strlen(p) + 1;
1122 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1123 framelen, 0, 0, 8, 0);
1124 p += strlen(p) + 1;
1126 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1127 loclen, 0, 0, 1, 0);
1128 p += strlen(p) + 1;
1130 fwritezero(align, ofile);
1133 * Now output the sections.
1135 elf_write_sections();
1137 nasm_free(elf_sects);
1138 saa_free(symtab);
1141 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1143 struct SAA *s = saa_init(1L);
1144 struct Symbol *sym;
1145 uint8_t entry[16], *p;
1146 int i;
1148 *len = *local = 0;
1151 * First, an all-zeros entry, required by the ELF spec.
1153 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1154 *len += 16;
1155 (*local)++;
1158 * Next, an entry for the file name.
1160 p = entry;
1161 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1162 WRITELONG(p, 0); /* no value */
1163 WRITELONG(p, 0); /* no size either */
1164 WRITESHORT(p, STT_FILE); /* type FILE */
1165 WRITESHORT(p, SHN_ABS);
1166 saa_wbytes(s, entry, 16L);
1167 *len += 16;
1168 (*local)++;
1171 * Now some standard symbols defining the segments, for relocation
1172 * purposes.
1174 for (i = 1; i <= nsects; i++) {
1175 p = entry;
1176 WRITELONG(p, 0); /* no symbol name */
1177 WRITELONG(p, 0); /* offset zero */
1178 WRITELONG(p, 0); /* size zero */
1179 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1180 WRITESHORT(p, i); /* section id */
1181 saa_wbytes(s, entry, 16L);
1182 *len += 16;
1183 (*local)++;
1187 * Now the other local symbols.
1189 saa_rewind(syms);
1190 while ((sym = saa_rstruct(syms))) {
1191 if (sym->type & SYM_GLOBAL)
1192 continue;
1193 p = entry;
1194 WRITELONG(p, sym->strpos);
1195 WRITELONG(p, sym->symv.key);
1196 WRITELONG(p, sym->size);
1197 WRITECHAR(p, sym->type); /* type and binding */
1198 WRITECHAR(p, sym->other); /* visibility */
1199 WRITESHORT(p, sym->section);
1200 saa_wbytes(s, entry, 16L);
1201 *len += 16;
1202 (*local)++;
1205 * dwarf needs symbols for debug sections
1206 * which are relocation targets.
1208 //*** fix for 32 bit
1209 if (of_elf32.current_dfmt == &df_dwarf) {
1210 dwarf_infosym = *local;
1211 p = entry;
1212 WRITELONG(p, 0); /* no symbol name */
1213 WRITELONG(p, (uint32_t) 0); /* offset zero */
1214 WRITELONG(p, (uint32_t) 0); /* size zero */
1215 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1216 WRITESHORT(p, sec_debug_info); /* section id */
1217 saa_wbytes(s, entry, 16L);
1218 *len += 16;
1219 (*local)++;
1220 dwarf_abbrevsym = *local;
1221 p = entry;
1222 WRITELONG(p, 0); /* no symbol name */
1223 WRITELONG(p, (uint32_t) 0); /* offset zero */
1224 WRITELONG(p, (uint32_t) 0); /* size zero */
1225 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1226 WRITESHORT(p, sec_debug_abbrev); /* section id */
1227 saa_wbytes(s, entry, 16L);
1228 *len += 16;
1229 (*local)++;
1230 dwarf_linesym = *local;
1231 p = entry;
1232 WRITELONG(p, 0); /* no symbol name */
1233 WRITELONG(p, (uint32_t) 0); /* offset zero */
1234 WRITELONG(p, (uint32_t) 0); /* size zero */
1235 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1236 WRITESHORT(p, sec_debug_line); /* section id */
1237 saa_wbytes(s, entry, 16L);
1238 *len += 16;
1239 (*local)++;
1243 * Now the global symbols.
1245 saa_rewind(syms);
1246 while ((sym = saa_rstruct(syms))) {
1247 if (!(sym->type & SYM_GLOBAL))
1248 continue;
1249 p = entry;
1250 WRITELONG(p, sym->strpos);
1251 WRITELONG(p, sym->symv.key);
1252 WRITELONG(p, sym->size);
1253 WRITECHAR(p, sym->type); /* type and binding */
1254 WRITECHAR(p, sym->other); /* visibility */
1255 WRITESHORT(p, sym->section);
1256 saa_wbytes(s, entry, 16L);
1257 *len += 16;
1260 return s;
1263 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1265 struct SAA *s;
1266 uint8_t *p, entry[8];
1267 int32_t global_offset;
1269 if (!r)
1270 return NULL;
1272 s = saa_init(1L);
1273 *len = 0;
1276 * How to onvert from a global placeholder to a real symbol index;
1277 * the +2 refers to the two special entries, the null entry and
1278 * the filename entry.
1280 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1282 while (r) {
1283 int32_t sym = r->symbol;
1286 * Create a real symbol index; the +2 refers to the two special
1287 * entries, the null entry and the filename entry.
1289 if (sym >= GLOBAL_TEMP_BASE)
1290 sym += global_offset;
1292 p = entry;
1293 WRITELONG(p, r->address);
1294 WRITELONG(p, (sym << 8) + r->type);
1295 saa_wbytes(s, entry, 8L);
1296 *len += 8;
1298 r = r->next;
1301 return s;
1304 static void elf_section_header(int name, int type, int flags,
1305 void *data, bool is_saa, int32_t datalen,
1306 int link, int info, int align, int eltsize)
1308 elf_sects[elf_nsect].data = data;
1309 elf_sects[elf_nsect].len = datalen;
1310 elf_sects[elf_nsect].is_saa = is_saa;
1311 elf_nsect++;
1313 fwriteint32_t((int32_t)name, ofile);
1314 fwriteint32_t((int32_t)type, ofile);
1315 fwriteint32_t((int32_t)flags, ofile);
1316 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1317 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1318 fwriteint32_t(datalen, ofile);
1319 if (data)
1320 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1321 fwriteint32_t((int32_t)link, ofile);
1322 fwriteint32_t((int32_t)info, ofile);
1323 fwriteint32_t((int32_t)align, ofile);
1324 fwriteint32_t((int32_t)eltsize, ofile);
1327 static void elf_write_sections(void)
1329 int i;
1330 for (i = 0; i < elf_nsect; i++)
1331 if (elf_sects[i].data) {
1332 int32_t len = elf_sects[i].len;
1333 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1334 int32_t align = reallen - len;
1335 if (elf_sects[i].is_saa)
1336 saa_fpwrite(elf_sects[i].data, ofile);
1337 else
1338 fwrite(elf_sects[i].data, len, 1, ofile);
1339 fwritezero(align, ofile);
1343 static void elf_sect_write(struct Section *sect,
1344 const uint8_t *data, uint32_t len)
1346 saa_wbytes(sect->data, data, len);
1347 sect->len += len;
1350 static void elf_sectalign(int32_t seg, unsigned int value)
1352 struct Section *s = NULL;
1353 int i;
1355 for (i = 0; i < nsects; i++) {
1356 if (sects[i]->index == seg) {
1357 s = sects[i];
1358 break;
1361 if (!s || !is_power2(value))
1362 return;
1364 if (value > s->align)
1365 s->align = value;
1368 static int32_t elf_segbase(int32_t segment)
1370 return segment;
1373 static int elf_directive(enum directives directive, char *value, int pass)
1375 bool err;
1376 int64_t n;
1377 char *p;
1379 switch (directive) {
1380 case D_OSABI:
1381 if (pass == 2)
1382 return 1; /* ignore in pass 2 */
1384 n = readnum(value, &err);
1385 if (err) {
1386 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1387 return 1;
1389 if (n < 0 || n > 255) {
1390 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1391 return 1;
1393 elf_osabi = n;
1394 elf_abiver = 0;
1396 if ((p = strchr(value,',')) == NULL)
1397 return 1;
1399 n = readnum(p+1, &err);
1400 if (err || n < 0 || n > 255) {
1401 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1402 return 1;
1405 elf_abiver = n;
1406 return 1;
1408 default:
1409 return 0;
1413 static void elf_filename(char *inname, char *outname)
1415 strcpy(elf_module, inname);
1416 standard_extension(inname, outname, ".o");
1419 extern macros_t elf_stdmac[];
1421 static int elf_set_info(enum geninfo type, char **val)
1423 (void)type;
1424 (void)val;
1425 return 0;
1427 static struct dfmt df_dwarf = {
1428 "ELF32 (i386) dwarf debug format for Linux/Unix",
1429 "dwarf",
1430 dwarf32_init,
1431 dwarf32_linenum,
1432 debug32_deflabel,
1433 debug32_directive,
1434 debug32_typevalue,
1435 dwarf32_output,
1436 dwarf32_cleanup
1438 static struct dfmt df_stabs = {
1439 "ELF32 (i386) stabs debug format for Linux/Unix",
1440 "stabs",
1441 null_debug_init,
1442 stabs32_linenum,
1443 debug32_deflabel,
1444 debug32_directive,
1445 debug32_typevalue,
1446 stabs32_output,
1447 stabs32_cleanup
1450 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1452 struct ofmt of_elf32 = {
1453 "ELF32 (i386) object files (e.g. Linux)",
1454 "elf32",
1456 elf32_debugs_arr,
1457 &df_stabs,
1458 elf_stdmac,
1459 elf_init,
1460 elf_set_info,
1461 elf_out,
1462 elf_deflabel,
1463 elf_section_names,
1464 elf_sectalign,
1465 elf_segbase,
1466 elf_directive,
1467 elf_filename,
1468 elf_cleanup
1471 /* again, the stabs debugging stuff (code) */
1473 static void stabs32_linenum(const char *filename, int32_t linenumber,
1474 int32_t segto)
1476 (void)segto;
1478 if (!stabs_filename) {
1479 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1480 strcpy(stabs_filename, filename);
1481 } else {
1482 if (strcmp(stabs_filename, filename)) {
1484 * yep, a memory leak...this program is one-shot anyway, so who cares...
1485 * in fact, this leak comes in quite handy to maintain a list of files
1486 * encountered so far in the symbol lines...
1489 /* why not nasm_free(stabs_filename); we're done with the old one */
1491 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1492 strcpy(stabs_filename, filename);
1495 debug_immcall = 1;
1496 currentline = linenumber;
1499 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1500 char *special)
1502 (void)name;
1503 (void)segment;
1504 (void)offset;
1505 (void)is_global;
1506 (void)special;
1509 static void debug32_directive(const char *directive, const char *params)
1511 (void)directive;
1512 (void)params;
1515 static void debug32_typevalue(int32_t type)
1517 int32_t stype, ssize;
1518 switch (TYM_TYPE(type)) {
1519 case TY_LABEL:
1520 ssize = 0;
1521 stype = STT_NOTYPE;
1522 break;
1523 case TY_BYTE:
1524 ssize = 1;
1525 stype = STT_OBJECT;
1526 break;
1527 case TY_WORD:
1528 ssize = 2;
1529 stype = STT_OBJECT;
1530 break;
1531 case TY_DWORD:
1532 ssize = 4;
1533 stype = STT_OBJECT;
1534 break;
1535 case TY_FLOAT:
1536 ssize = 4;
1537 stype = STT_OBJECT;
1538 break;
1539 case TY_QWORD:
1540 ssize = 8;
1541 stype = STT_OBJECT;
1542 break;
1543 case TY_TBYTE:
1544 ssize = 10;
1545 stype = STT_OBJECT;
1546 break;
1547 case TY_OWORD:
1548 ssize = 16;
1549 stype = STT_OBJECT;
1550 break;
1551 case TY_YWORD:
1552 ssize = 32;
1553 stype = STT_OBJECT;
1554 break;
1555 case TY_COMMON:
1556 ssize = 0;
1557 stype = STT_COMMON;
1558 break;
1559 case TY_SEG:
1560 ssize = 0;
1561 stype = STT_SECTION;
1562 break;
1563 case TY_EXTERN:
1564 ssize = 0;
1565 stype = STT_NOTYPE;
1566 break;
1567 case TY_EQU:
1568 ssize = 0;
1569 stype = STT_NOTYPE;
1570 break;
1571 default:
1572 ssize = 0;
1573 stype = STT_NOTYPE;
1574 break;
1576 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1577 lastsym->size = ssize;
1578 lastsym->type = stype;
1582 static void stabs32_output(int type, void *param)
1584 struct symlininfo *s;
1585 struct linelist *el;
1586 if (type == TY_STABSSYMLIN) {
1587 if (debug_immcall) {
1588 s = (struct symlininfo *)param;
1589 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1590 return; /* line info is only collected for executable sections */
1591 numlinestabs++;
1592 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1593 el->info.offset = s->offset;
1594 el->info.section = s->section;
1595 el->info.name = s->name;
1596 el->line = currentline;
1597 el->filename = stabs_filename;
1598 el->next = 0;
1599 if (stabslines) {
1600 stabslines->last->next = el;
1601 stabslines->last = el;
1602 } else {
1603 stabslines = el;
1604 stabslines->last = el;
1608 debug_immcall = 0;
1611 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1613 static void stabs32_generate(void)
1615 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1616 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1617 char **allfiles;
1618 int *fileidx;
1620 struct linelist *ptr;
1622 ptr = stabslines;
1624 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1625 for (i = 0; i < numlinestabs; i++)
1626 allfiles[i] = 0;
1627 numfiles = 0;
1628 while (ptr) {
1629 if (numfiles == 0) {
1630 allfiles[0] = ptr->filename;
1631 numfiles++;
1632 } else {
1633 for (i = 0; i < numfiles; i++) {
1634 if (!strcmp(allfiles[i], ptr->filename))
1635 break;
1637 if (i >= numfiles) {
1638 allfiles[i] = ptr->filename;
1639 numfiles++;
1642 ptr = ptr->next;
1644 strsize = 1;
1645 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1646 for (i = 0; i < numfiles; i++) {
1647 fileidx[i] = strsize;
1648 strsize += strlen(allfiles[i]) + 1;
1650 mainfileindex = 0;
1651 for (i = 0; i < numfiles; i++) {
1652 if (!strcmp(allfiles[i], elf_module)) {
1653 mainfileindex = i;
1654 break;
1659 * worst case size of the stab buffer would be:
1660 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1661 * plus one "ending" entry
1663 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1664 sizeof(struct stabentry));
1665 ssbuf = (uint8_t *)nasm_malloc(strsize);
1666 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1667 rptr = rbuf;
1669 for (i = 0; i < numfiles; i++)
1670 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1671 ssbuf[0] = 0;
1673 stabstrlen = strsize; /* set global variable for length of stab strings */
1675 sptr = sbuf;
1676 ptr = stabslines;
1677 numstabs = 0;
1679 if (ptr) {
1681 * this is the first stab, its strx points to the filename of the
1682 * the source-file, the n_desc field should be set to the number
1683 * of remaining stabs
1685 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1687 /* this is the stab for the main source file */
1688 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1690 /* relocation table entry */
1693 * Since the symbol table has two entries before
1694 * the section symbols, the index in the info.section
1695 * member must be adjusted by adding 2
1698 WRITELONG(rptr, (sptr - sbuf) - 4);
1699 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1701 numstabs++;
1702 currfile = mainfileindex;
1705 while (ptr) {
1706 if (strcmp(allfiles[currfile], ptr->filename)) {
1707 /* oops file has changed... */
1708 for (i = 0; i < numfiles; i++)
1709 if (!strcmp(allfiles[i], ptr->filename))
1710 break;
1711 currfile = i;
1712 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1713 ptr->info.offset);
1714 numstabs++;
1716 /* relocation table entry */
1717 WRITELONG(rptr, (sptr - sbuf) - 4);
1718 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1721 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1722 numstabs++;
1724 /* relocation table entry */
1726 WRITELONG(rptr, (sptr - sbuf) - 4);
1727 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1729 ptr = ptr->next;
1733 /* this is an "ending" token */
1734 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1735 numstabs++;
1737 ((struct stabentry *)sbuf)->n_desc = numstabs;
1739 nasm_free(allfiles);
1740 nasm_free(fileidx);
1742 stablen = (sptr - sbuf);
1743 stabrellen = (rptr - rbuf);
1744 stabrelbuf = rbuf;
1745 stabbuf = sbuf;
1746 stabstrbuf = ssbuf;
1749 static void stabs32_cleanup(void)
1751 struct linelist *ptr, *del;
1752 if (!stabslines)
1753 return;
1755 ptr = stabslines;
1756 while (ptr) {
1757 del = ptr;
1758 ptr = ptr->next;
1759 nasm_free(del);
1762 nasm_free(stabbuf);
1763 nasm_free(stabrelbuf);
1764 nasm_free(stabstrbuf);
1767 /* dwarf routines */
1769 static void dwarf32_init(void)
1771 ndebugs = 3; /* 3 debug symbols */
1774 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1775 int32_t segto)
1777 (void)segto;
1778 dwarf32_findfile(filename);
1779 debug_immcall = 1;
1780 currentline = linenumber;
1783 /* called from elf_out with type == TY_DEBUGSYMLIN */
1784 static void dwarf32_output(int type, void *param)
1786 int ln, aa, inx, maxln, soc;
1787 struct symlininfo *s;
1788 struct SAA *plinep;
1790 (void)type;
1792 s = (struct symlininfo *)param;
1794 /* line number info is only gathered for executable sections */
1795 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1796 return;
1798 /* Check if section index has changed */
1799 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1800 dwarf32_findsect(s->section);
1802 /* do nothing unless line or file has changed */
1803 if (!debug_immcall)
1804 return;
1806 ln = currentline - dwarf_csect->line;
1807 aa = s->offset - dwarf_csect->offset;
1808 inx = dwarf_clist->line;
1809 plinep = dwarf_csect->psaa;
1810 /* check for file change */
1811 if (!(inx == dwarf_csect->file)) {
1812 saa_write8(plinep,DW_LNS_set_file);
1813 saa_write8(plinep,inx);
1814 dwarf_csect->file = inx;
1816 /* check for line change */
1817 if (ln) {
1818 /* test if in range of special op code */
1819 maxln = line_base + line_range;
1820 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1821 if (ln >= line_base && ln < maxln && soc < 256) {
1822 saa_write8(plinep,soc);
1823 } else {
1824 saa_write8(plinep,DW_LNS_advance_line);
1825 saa_wleb128s(plinep,ln);
1826 if (aa) {
1827 saa_write8(plinep,DW_LNS_advance_pc);
1828 saa_wleb128u(plinep,aa);
1831 dwarf_csect->line = currentline;
1832 dwarf_csect->offset = s->offset;
1835 /* show change handled */
1836 debug_immcall = 0;
1840 static void dwarf32_generate(void)
1842 uint8_t *pbuf;
1843 int indx;
1844 struct linelist *ftentry;
1845 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1846 struct SAA *parangesrel, *plinesrel, *pinforel;
1847 struct sectlist *psect;
1848 size_t saalen, linepoff, totlen, highaddr;
1850 /* write epilogues for each line program range */
1851 /* and build aranges section */
1852 paranges = saa_init(1L);
1853 parangesrel = saa_init(1L);
1854 saa_write16(paranges,2); /* dwarf version */
1855 saa_write32(parangesrel, paranges->datalen+4);
1856 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1857 saa_write32(parangesrel, 0);
1858 saa_write32(paranges,0); /* offset into info */
1859 saa_write8(paranges,4); /* pointer size */
1860 saa_write8(paranges,0); /* not segmented */
1861 saa_write32(paranges,0); /* padding */
1862 /* iterate though sectlist entries */
1863 psect = dwarf_fsect;
1864 totlen = 0;
1865 highaddr = 0;
1866 for (indx = 0; indx < dwarf_nsections; indx++) {
1867 plinep = psect->psaa;
1868 /* Line Number Program Epilogue */
1869 saa_write8(plinep,2); /* std op 2 */
1870 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1871 saa_write8(plinep,DW_LNS_extended_op);
1872 saa_write8(plinep,1); /* operand length */
1873 saa_write8(plinep,DW_LNE_end_sequence);
1874 totlen += plinep->datalen;
1875 /* range table relocation entry */
1876 saa_write32(parangesrel, paranges->datalen + 4);
1877 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1878 saa_write32(parangesrel, (uint32_t) 0);
1879 /* range table entry */
1880 saa_write32(paranges,0x0000); /* range start */
1881 saa_write32(paranges,sects[psect->section]->len); /* range length */
1882 highaddr += sects[psect->section]->len;
1883 /* done with this entry */
1884 psect = psect->next;
1886 saa_write32(paranges,0); /* null address */
1887 saa_write32(paranges,0); /* null length */
1888 saalen = paranges->datalen;
1889 arangeslen = saalen + 4;
1890 arangesbuf = pbuf = nasm_malloc(arangeslen);
1891 WRITELONG(pbuf,saalen); /* initial length */
1892 saa_rnbytes(paranges, pbuf, saalen);
1893 saa_free(paranges);
1895 /* build rela.aranges section */
1896 arangesrellen = saalen = parangesrel->datalen;
1897 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1898 saa_rnbytes(parangesrel, pbuf, saalen);
1899 saa_free(parangesrel);
1901 /* build pubnames section */
1902 ppubnames = saa_init(1L);
1903 saa_write16(ppubnames,3); /* dwarf version */
1904 saa_write32(ppubnames,0); /* offset into info */
1905 saa_write32(ppubnames,0); /* space used in info */
1906 saa_write32(ppubnames,0); /* end of list */
1907 saalen = ppubnames->datalen;
1908 pubnameslen = saalen + 4;
1909 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1910 WRITELONG(pbuf,saalen); /* initial length */
1911 saa_rnbytes(ppubnames, pbuf, saalen);
1912 saa_free(ppubnames);
1914 /* build info section */
1915 pinfo = saa_init(1L);
1916 pinforel = saa_init(1L);
1917 saa_write16(pinfo,2); /* dwarf version */
1918 saa_write32(pinforel, pinfo->datalen + 4);
1919 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1920 saa_write32(pinforel, 0);
1921 saa_write32(pinfo,0); /* offset into abbrev */
1922 saa_write8(pinfo,4); /* pointer size */
1923 saa_write8(pinfo,1); /* 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(pinforel, pinfo->datalen + 4);
1929 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1930 saa_write32(pinforel, 0);
1931 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1932 saa_write32(pinforel, pinfo->datalen + 4);
1933 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1934 saa_write32(pinforel, 0);
1935 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1936 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1937 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1938 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1939 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1940 saa_write32(pinforel, pinfo->datalen + 4);
1941 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1942 saa_write32(pinforel, 0);
1943 saa_write32(pinfo,0); /* DW_AT_low_pc */
1944 saa_write32(pinfo,0); /* DW_AT_frame_base */
1945 saa_write8(pinfo,0); /* end of entries */
1946 saalen = pinfo->datalen;
1947 infolen = saalen + 4;
1948 infobuf = pbuf = nasm_malloc(infolen);
1949 WRITELONG(pbuf,saalen); /* initial length */
1950 saa_rnbytes(pinfo, pbuf, saalen);
1951 saa_free(pinfo);
1953 /* build rela.info section */
1954 inforellen = saalen = pinforel->datalen;
1955 inforelbuf = pbuf = nasm_malloc(inforellen);
1956 saa_rnbytes(pinforel, pbuf, saalen);
1957 saa_free(pinforel);
1959 /* build abbrev section */
1960 pabbrev = saa_init(1L);
1961 saa_write8(pabbrev,1); /* entry number LEB128u */
1962 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1963 saa_write8(pabbrev,1); /* has children */
1964 /* the following attributes and forms are all LEB128u values */
1965 saa_write8(pabbrev,DW_AT_low_pc);
1966 saa_write8(pabbrev,DW_FORM_addr);
1967 saa_write8(pabbrev,DW_AT_high_pc);
1968 saa_write8(pabbrev,DW_FORM_addr);
1969 saa_write8(pabbrev,DW_AT_stmt_list);
1970 saa_write8(pabbrev,DW_FORM_data4);
1971 saa_write8(pabbrev,DW_AT_name);
1972 saa_write8(pabbrev,DW_FORM_string);
1973 saa_write8(pabbrev,DW_AT_producer);
1974 saa_write8(pabbrev,DW_FORM_string);
1975 saa_write8(pabbrev,DW_AT_language);
1976 saa_write8(pabbrev,DW_FORM_data2);
1977 saa_write16(pabbrev,0); /* end of entry */
1978 /* LEB128u usage same as above */
1979 saa_write8(pabbrev,2); /* entry number */
1980 saa_write8(pabbrev,DW_TAG_subprogram);
1981 saa_write8(pabbrev,0); /* no children */
1982 saa_write8(pabbrev,DW_AT_low_pc);
1983 saa_write8(pabbrev,DW_FORM_addr);
1984 saa_write8(pabbrev,DW_AT_frame_base);
1985 saa_write8(pabbrev,DW_FORM_data4);
1986 saa_write16(pabbrev,0); /* end of entry */
1987 abbrevlen = saalen = pabbrev->datalen;
1988 abbrevbuf = pbuf = nasm_malloc(saalen);
1989 saa_rnbytes(pabbrev, pbuf, saalen);
1990 saa_free(pabbrev);
1992 /* build line section */
1993 /* prolog */
1994 plines = saa_init(1L);
1995 saa_write8(plines,1); /* Minimum Instruction Length */
1996 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1997 saa_write8(plines,line_base); /* Line Base */
1998 saa_write8(plines,line_range); /* Line Range */
1999 saa_write8(plines,opcode_base); /* Opcode Base */
2000 /* standard opcode lengths (# of LEB128u operands) */
2001 saa_write8(plines,0); /* Std opcode 1 length */
2002 saa_write8(plines,1); /* Std opcode 2 length */
2003 saa_write8(plines,1); /* Std opcode 3 length */
2004 saa_write8(plines,1); /* Std opcode 4 length */
2005 saa_write8(plines,1); /* Std opcode 5 length */
2006 saa_write8(plines,0); /* Std opcode 6 length */
2007 saa_write8(plines,0); /* Std opcode 7 length */
2008 saa_write8(plines,0); /* Std opcode 8 length */
2009 saa_write8(plines,1); /* Std opcode 9 length */
2010 saa_write8(plines,0); /* Std opcode 10 length */
2011 saa_write8(plines,0); /* Std opcode 11 length */
2012 saa_write8(plines,1); /* Std opcode 12 length */
2013 /* Directory Table */
2014 saa_write8(plines,0); /* End of table */
2015 /* File Name Table */
2016 ftentry = dwarf_flist;
2017 for (indx = 0; indx < dwarf_numfiles; indx++) {
2018 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2019 saa_write8(plines,0); /* directory LEB128u */
2020 saa_write8(plines,0); /* time LEB128u */
2021 saa_write8(plines,0); /* size LEB128u */
2022 ftentry = ftentry->next;
2024 saa_write8(plines,0); /* End of table */
2025 linepoff = plines->datalen;
2026 linelen = linepoff + totlen + 10;
2027 linebuf = pbuf = nasm_malloc(linelen);
2028 WRITELONG(pbuf,linelen-4); /* initial length */
2029 WRITESHORT(pbuf,3); /* dwarf version */
2030 WRITELONG(pbuf,linepoff); /* offset to line number program */
2031 /* write line header */
2032 saalen = linepoff;
2033 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2034 pbuf += linepoff;
2035 saa_free(plines);
2036 /* concatonate line program ranges */
2037 linepoff += 13;
2038 plinesrel = saa_init(1L);
2039 psect = dwarf_fsect;
2040 for (indx = 0; indx < dwarf_nsections; indx++) {
2041 saa_write32(plinesrel, linepoff);
2042 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2043 saa_write32(plinesrel, (uint32_t) 0);
2044 plinep = psect->psaa;
2045 saalen = plinep->datalen;
2046 saa_rnbytes(plinep, pbuf, saalen);
2047 pbuf += saalen;
2048 linepoff += saalen;
2049 saa_free(plinep);
2050 /* done with this entry */
2051 psect = psect->next;
2055 /* build rela.lines section */
2056 linerellen =saalen = plinesrel->datalen;
2057 linerelbuf = pbuf = nasm_malloc(linerellen);
2058 saa_rnbytes(plinesrel, pbuf, saalen);
2059 saa_free(plinesrel);
2061 /* build frame section */
2062 framelen = 4;
2063 framebuf = pbuf = nasm_malloc(framelen);
2064 WRITELONG(pbuf,framelen-4); /* initial length */
2066 /* build loc section */
2067 loclen = 16;
2068 locbuf = pbuf = nasm_malloc(loclen);
2069 WRITELONG(pbuf,0); /* null beginning offset */
2070 WRITELONG(pbuf,0); /* null ending offset */
2073 static void dwarf32_cleanup(void)
2075 nasm_free(arangesbuf);
2076 nasm_free(arangesrelbuf);
2077 nasm_free(pubnamesbuf);
2078 nasm_free(infobuf);
2079 nasm_free(inforelbuf);
2080 nasm_free(abbrevbuf);
2081 nasm_free(linebuf);
2082 nasm_free(linerelbuf);
2083 nasm_free(framebuf);
2084 nasm_free(locbuf);
2087 static void dwarf32_findfile(const char * fname)
2089 int finx;
2090 struct linelist *match;
2092 /* return if fname is current file name */
2093 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2094 return;
2096 /* search for match */
2097 match = 0;
2098 if (dwarf_flist) {
2099 match = dwarf_flist;
2100 for (finx = 0; finx < dwarf_numfiles; finx++) {
2101 if (!(strcmp(fname, match->filename))) {
2102 dwarf_clist = match;
2103 return;
2108 /* add file name to end of list */
2109 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2110 dwarf_numfiles++;
2111 dwarf_clist->line = dwarf_numfiles;
2112 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2113 strcpy(dwarf_clist->filename,fname);
2114 dwarf_clist->next = 0;
2115 if (!dwarf_flist) { /* if first entry */
2116 dwarf_flist = dwarf_elist = dwarf_clist;
2117 dwarf_clist->last = 0;
2118 } else { /* chain to previous entry */
2119 dwarf_elist->next = dwarf_clist;
2120 dwarf_elist = dwarf_clist;
2124 static void dwarf32_findsect(const int index)
2126 int sinx;
2127 struct sectlist *match;
2128 struct SAA *plinep;
2130 /* return if index is current section index */
2131 if (dwarf_csect && (dwarf_csect->section == index))
2132 return;
2134 /* search for match */
2135 match = 0;
2136 if (dwarf_fsect) {
2137 match = dwarf_fsect;
2138 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2139 if ((match->section == index)) {
2140 dwarf_csect = match;
2141 return;
2143 match = match->next;
2147 /* add entry to end of list */
2148 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2149 dwarf_nsections++;
2150 dwarf_csect->psaa = plinep = saa_init(1L);
2151 dwarf_csect->line = 1;
2152 dwarf_csect->offset = 0;
2153 dwarf_csect->file = 1;
2154 dwarf_csect->section = index;
2155 dwarf_csect->next = 0;
2156 /* set relocatable address at start of line program */
2157 saa_write8(plinep,DW_LNS_extended_op);
2158 saa_write8(plinep,5); /* operand length */
2159 saa_write8(plinep,DW_LNE_set_address);
2160 saa_write32(plinep,0); /* Start Address */
2162 if (!dwarf_fsect) { /* if first entry */
2163 dwarf_fsect = dwarf_esect = dwarf_csect;
2164 dwarf_csect->last = 0;
2165 } else { /* chain to previous entry */
2166 dwarf_esect->next = dwarf_csect;
2167 dwarf_esect = dwarf_csect;
2171 #endif /* OF_ELF */