BR3041451: Implement upper bound for %rep counter
[nasm.git] / output / outelf32.c
bloba85acf045503779c1084514d4fd70413ebce6c78
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 symlininfo info;
157 int line;
158 char *filename;
159 struct linelist *next;
160 struct linelist *last;
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_malloc(sizeof(*s));
305 if (type != SHT_NOBITS)
306 s->data = saa_init(1L);
307 s->head = NULL;
308 s->tail = &s->head;
309 s->len = s->size = 0;
310 s->nrelocs = 0;
311 if (!strcmp(name, ".text"))
312 s->index = def_seg;
313 else
314 s->index = seg_alloc();
315 add_sectname("", name);
316 s->name = nasm_malloc(1 + strlen(name));
317 strcpy(s->name, name);
318 s->type = type;
319 s->flags = flags;
320 s->align = align;
321 s->gsyms = NULL;
323 if (nsects >= sectlen)
324 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
325 sects[nsects++] = s;
327 return nsects - 1;
330 static int32_t elf_section_names(char *name, int pass, int *bits)
332 char *p;
333 uint32_t flags, flags_and, flags_or;
334 uint64_t align;
335 int type, i;
338 * Default is 32 bits.
340 if (!name) {
341 *bits = 32;
342 return def_seg;
345 p = nasm_skip_word(name);
346 if (*p)
347 *p++ = '\0';
348 flags_and = flags_or = type = align = 0;
350 section_attrib(name, p, pass, &flags_and,
351 &flags_or, &align, &type);
353 if (!strcmp(name, ".shstrtab") ||
354 !strcmp(name, ".symtab") ||
355 !strcmp(name, ".strtab")) {
356 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
357 "name `%s'", name);
358 return NO_SEG;
361 for (i = 0; i < nsects; i++)
362 if (!strcmp(name, sects[i]->name))
363 break;
364 if (i == nsects) {
365 const struct elf_known_section *ks = elf_known_sections;
367 while (ks->name) {
368 if (!strcmp(name, ks->name))
369 break;
370 ks++;
373 type = type ? type : ks->type;
374 align = align ? align : ks->align;
375 flags = (ks->flags & ~flags_and) | flags_or;
377 i = elf_make_section(name, type, flags, align);
378 } else if (pass == 1) {
379 if ((type && sects[i]->type != type)
380 || (align && sects[i]->align != align)
381 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
382 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
383 " redeclaration of section `%s'", name);
386 return sects[i]->index;
389 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
390 int is_global, char *special)
392 int pos = strslen;
393 struct Symbol *sym;
394 bool special_used = false;
396 #if defined(DEBUG) && DEBUG>2
397 nasm_error(ERR_DEBUG,
398 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
399 name, segment, offset, is_global, special);
400 #endif
401 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
403 * This is a NASM special symbol. We never allow it into
404 * the ELF symbol table, even if it's a valid one. If it
405 * _isn't_ a valid one, we should barf immediately.
407 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
408 strcmp(name, "..got") && strcmp(name, "..plt") &&
409 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
410 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
411 return;
414 if (is_global == 3) {
415 struct Symbol **s;
417 * Fix up a forward-reference symbol size from the first
418 * pass.
420 for (s = &fwds; *s; s = &(*s)->nextfwd)
421 if (!strcmp((*s)->name, name)) {
422 struct tokenval tokval;
423 expr *e;
424 char *p = nasm_skip_spaces(nasm_skip_word(special));
426 stdscan_reset();
427 stdscan_set(p);
428 tokval.t_type = TOKEN_INVALID;
429 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
430 if (e) {
431 if (!is_simple(e))
432 nasm_error(ERR_NONFATAL, "cannot use relocatable"
433 " expression as symbol size");
434 else
435 (*s)->size = reloc_value(e);
439 * Remove it from the list of unresolved sizes.
441 nasm_free((*s)->name);
442 *s = (*s)->nextfwd;
443 return;
445 return; /* it wasn't an important one */
448 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
449 strslen += 1 + strlen(name);
451 lastsym = sym = saa_wstruct(syms);
453 memset(&sym->symv, 0, sizeof(struct rbtree));
455 sym->strpos = pos;
456 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
457 sym->other = STV_DEFAULT;
458 sym->size = 0;
459 if (segment == NO_SEG)
460 sym->section = SHN_ABS;
461 else {
462 int i;
463 sym->section = SHN_UNDEF;
464 if (segment == def_seg) {
465 /* we have to be sure at least text section is there */
466 int tempint;
467 if (segment != elf_section_names(".text", 2, &tempint))
468 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
470 for (i = 0; i < nsects; i++) {
471 if (segment == sects[i]->index) {
472 sym->section = i + 1;
473 break;
478 if (is_global == 2) {
479 sym->size = offset;
480 sym->symv.key = 0;
481 sym->section = SHN_COMMON;
483 * We have a common variable. Check the special text to see
484 * if it's a valid number and power of two; if so, store it
485 * as the alignment for the common variable.
487 if (special) {
488 bool err;
489 sym->symv.key = readnum(special, &err);
490 if (err)
491 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
492 " valid number", special);
493 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
494 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
495 " power of two", special);
497 special_used = true;
498 } else
499 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
501 if (sym->type == SYM_GLOBAL) {
503 * If sym->section == SHN_ABS, then the first line of the
504 * else section would cause a core dump, because its a reference
505 * beyond the end of the section array.
506 * This behaviour is exhibited by this code:
507 * GLOBAL crash_nasm
508 * crash_nasm equ 0
509 * To avoid such a crash, such requests are silently discarded.
510 * This may not be the best solution.
512 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
513 bsym = raa_write(bsym, segment, nglobs);
514 } else if (sym->section != SHN_ABS) {
516 * This is a global symbol; so we must add it to the rbtree
517 * of global symbols in its section.
519 * In addition, we check the special text for symbol
520 * type and size information.
522 sects[sym->section-1]->gsyms =
523 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
525 if (special) {
526 int n = strcspn(special, " \t");
528 if (!nasm_strnicmp(special, "function", n))
529 sym->type |= STT_FUNC;
530 else if (!nasm_strnicmp(special, "data", n) ||
531 !nasm_strnicmp(special, "object", n))
532 sym->type |= STT_OBJECT;
533 else if (!nasm_strnicmp(special, "notype", n))
534 sym->type |= STT_NOTYPE;
535 else
536 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
537 n, special);
538 special += n;
540 special = nasm_skip_spaces(special);
541 if (*special) {
542 n = strcspn(special, " \t");
543 if (!nasm_strnicmp(special, "default", n))
544 sym->other = STV_DEFAULT;
545 else if (!nasm_strnicmp(special, "internal", n))
546 sym->other = STV_INTERNAL;
547 else if (!nasm_strnicmp(special, "hidden", n))
548 sym->other = STV_HIDDEN;
549 else if (!nasm_strnicmp(special, "protected", n))
550 sym->other = STV_PROTECTED;
551 else
552 n = 0;
553 special += n;
556 if (*special) {
557 struct tokenval tokval;
558 expr *e;
559 int fwd = 0;
560 char *saveme = stdscan_get();
562 while (special[n] && nasm_isspace(special[n]))
563 n++;
565 * We have a size expression; attempt to
566 * evaluate it.
568 stdscan_reset();
569 stdscan_set(special + n);
570 tokval.t_type = TOKEN_INVALID;
571 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
572 NULL);
573 if (fwd) {
574 sym->nextfwd = fwds;
575 fwds = sym;
576 sym->name = nasm_strdup(name);
577 } else if (e) {
578 if (!is_simple(e))
579 nasm_error(ERR_NONFATAL, "cannot use relocatable"
580 " expression as symbol size");
581 else
582 sym->size = reloc_value(e);
584 stdscan_set(saveme);
586 special_used = true;
589 * If TLS segment, mark symbol accordingly.
591 if (sects[sym->section - 1]->flags & SHF_TLS) {
592 sym->type &= 0xf0;
593 sym->type |= STT_TLS;
596 sym->globnum = nglobs;
597 nglobs++;
598 } else
599 nlocals++;
601 if (special && !special_used)
602 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
605 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
607 struct Reloc *r;
609 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
610 sect->tail = &r->next;
611 r->next = NULL;
613 r->address = sect->len;
614 if (segment == NO_SEG)
615 r->symbol = 0;
616 else {
617 int i;
618 r->symbol = 0;
619 for (i = 0; i < nsects; i++)
620 if (segment == sects[i]->index)
621 r->symbol = i + 2;
622 if (!r->symbol)
623 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
625 r->type = type;
627 sect->nrelocs++;
631 * This routine deals with ..got and ..sym relocations: the more
632 * complicated kinds. In shared-library writing, some relocations
633 * with respect to global symbols must refer to the precise symbol
634 * rather than referring to an offset from the base of the section
635 * _containing_ the symbol. Such relocations call to this routine,
636 * which searches the symbol list for the symbol in question.
638 * R_386_GOT32 references require the _exact_ symbol address to be
639 * used; R_386_32 references can be at an offset from the symbol.
640 * The boolean argument `exact' tells us this.
642 * Return value is the adjusted value of `addr', having become an
643 * offset from the symbol rather than the section. Should always be
644 * zero when returning from an exact call.
646 * Limitation: if you define two symbols at the same place,
647 * confusion will occur.
649 * Inefficiency: we search, currently, using a linked list which
650 * isn't even necessarily sorted.
652 static int32_t elf_add_gsym_reloc(struct Section *sect,
653 int32_t segment, uint32_t offset,
654 int type, bool exact)
656 struct Reloc *r;
657 struct Section *s;
658 struct Symbol *sym;
659 struct rbtree *srb;
660 int i;
663 * First look up the segment/offset pair and find a global
664 * symbol corresponding to it. If it's not one of our segments,
665 * then it must be an external symbol, in which case we're fine
666 * doing a normal elf_add_reloc after first sanity-checking
667 * that the offset from the symbol is zero.
669 s = NULL;
670 for (i = 0; i < nsects; i++)
671 if (segment == sects[i]->index) {
672 s = sects[i];
673 break;
675 if (!s) {
676 if (exact && offset != 0)
677 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
678 " for this reference");
679 else
680 elf_add_reloc(sect, segment, type);
681 return offset;
684 srb = rb_search(s->gsyms, offset);
685 if (!srb || (exact && srb->key != offset)) {
686 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
687 " for this reference");
688 return 0;
690 sym = container_of(srb, struct Symbol, symv);
692 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
693 sect->tail = &r->next;
694 r->next = NULL;
696 r->address = sect->len;
697 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
698 r->type = type;
700 sect->nrelocs++;
702 return offset - sym->symv.key;
705 static void elf_out(int32_t segto, const void *data,
706 enum out_type type, uint64_t size,
707 int32_t segment, int32_t wrt)
709 struct Section *s;
710 int32_t addr;
711 uint8_t mydata[8], *p;
712 int reltype, bytes;
713 int i;
714 static struct symlininfo sinfo;
717 * handle absolute-assembly (structure definitions)
719 if (segto == NO_SEG) {
720 if (type != OUT_RESERVE)
721 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
722 " space");
723 return;
726 s = NULL;
727 for (i = 0; i < nsects; i++)
728 if (segto == sects[i]->index) {
729 s = sects[i];
730 break;
732 if (!s) {
733 int tempint; /* ignored */
734 if (segto != elf_section_names(".text", 2, &tempint))
735 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
736 else {
737 s = sects[nsects - 1];
738 i = nsects - 1;
742 /* again some stabs debugging stuff */
743 if (of_elf32.current_dfmt) {
744 sinfo.offset = s->len;
745 sinfo.section = i;
746 sinfo.name = s->name;
747 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
749 /* end of debugging stuff */
751 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
752 nasm_error(ERR_WARNING, "attempt to initialize memory in"
753 " BSS section `%s': ignored", s->name);
754 s->len += realsize(type, size);
755 return;
758 switch (type) {
759 case OUT_RESERVE:
760 if (s->type == SHT_PROGBITS) {
761 nasm_error(ERR_WARNING, "uninitialized space declared in"
762 " non-BSS section `%s': zeroing", s->name);
763 elf_sect_write(s, NULL, size);
764 } else
765 s->len += size;
766 break;
768 case OUT_RAWDATA:
769 if (segment != NO_SEG)
770 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
771 elf_sect_write(s, data, size);
772 break;
774 case OUT_ADDRESS:
776 bool gnu16 = false;
777 addr = *(int64_t *)data;
778 if (segment != NO_SEG) {
779 if (segment % 2) {
780 nasm_error(ERR_NONFATAL, "ELF format does not support"
781 " segment base references");
782 } else {
783 if (wrt == NO_SEG) {
785 * The if() is a hack to deal with compilers which
786 * don't handle switch() statements with 64-bit
787 * expressions.
789 if (size < UINT_MAX) {
790 switch ((unsigned int)size) {
791 case 1:
792 gnu16 = true;
793 elf_add_reloc(s, segment, R_386_8);
794 break;
795 case 2:
796 gnu16 = true;
797 elf_add_reloc(s, segment, R_386_16);
798 break;
799 case 4:
800 elf_add_reloc(s, segment, R_386_32);
801 break;
802 default: /* Error issued further down */
803 break;
806 } else if (wrt == elf_gotpc_sect + 1) {
808 * The user will supply GOT relative to $$. ELF
809 * will let us have GOT relative to $. So we
810 * need to fix up the data item by $-$$.
812 addr += s->len;
813 elf_add_reloc(s, segment, R_386_GOTPC);
814 } else if (wrt == elf_gotoff_sect + 1) {
815 elf_add_reloc(s, segment, R_386_GOTOFF);
816 } else if (wrt == elf_tlsie_sect + 1) {
817 addr = elf_add_gsym_reloc(s, segment, addr,
818 R_386_TLS_IE, true);
819 } else if (wrt == elf_got_sect + 1) {
820 addr = elf_add_gsym_reloc(s, segment, addr,
821 R_386_GOT32, true);
822 } else if (wrt == elf_sym_sect + 1) {
823 if (size == 2) {
824 gnu16 = true;
825 addr = elf_add_gsym_reloc(s, segment, addr,
826 R_386_16, false);
827 } else {
828 addr = elf_add_gsym_reloc(s, segment, addr,
829 R_386_32, false);
831 } else if (wrt == elf_plt_sect + 1) {
832 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
833 "relative PLT references");
834 } else {
835 nasm_error(ERR_NONFATAL, "ELF format does not support this"
836 " use of WRT");
837 wrt = NO_SEG; /* we can at least _try_ to continue */
841 p = mydata;
842 if (gnu16) {
843 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
844 "8- or 16-bit relocations in ELF32 is a GNU extension");
845 } else if (size != 4 && segment != NO_SEG) {
846 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
848 WRITEADDR(p, addr, size);
849 elf_sect_write(s, mydata, size);
850 break;
853 case OUT_REL1ADR:
854 bytes = 1;
855 reltype = R_386_PC8;
856 goto rel12adr;
857 case OUT_REL2ADR:
858 bytes = 2;
859 reltype = R_386_PC16;
860 goto rel12adr;
862 rel12adr:
863 nasm_assert(segment != segto);
864 if (segment != NO_SEG && segment % 2) {
865 nasm_error(ERR_NONFATAL, "ELF format does not support"
866 " segment base references");
867 } else {
868 if (wrt == NO_SEG) {
869 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
870 "8- or 16-bit relocations in ELF is a GNU extension");
871 elf_add_reloc(s, segment, reltype);
872 } else {
873 nasm_error(ERR_NONFATAL,
874 "Unsupported non-32-bit ELF relocation");
877 p = mydata;
878 WRITESHORT(p, *(int64_t *)data - size);
879 elf_sect_write(s, mydata, bytes);
880 break;
882 case OUT_REL4ADR:
883 if (segment == segto)
884 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
885 if (segment != NO_SEG && segment % 2) {
886 nasm_error(ERR_NONFATAL, "ELF format does not support"
887 " segment base references");
888 } else {
889 if (wrt == NO_SEG) {
890 elf_add_reloc(s, segment, R_386_PC32);
891 } else if (wrt == elf_plt_sect + 1) {
892 elf_add_reloc(s, segment, R_386_PLT32);
893 } else if (wrt == elf_gotpc_sect + 1 ||
894 wrt == elf_gotoff_sect + 1 ||
895 wrt == elf_got_sect + 1) {
896 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
897 "relative GOT references");
898 } else {
899 nasm_error(ERR_NONFATAL, "ELF format does not support this"
900 " use of WRT");
901 wrt = NO_SEG; /* we can at least _try_ to continue */
904 p = mydata;
905 WRITELONG(p, *(int64_t *)data - size);
906 elf_sect_write(s, mydata, 4L);
907 break;
909 case OUT_REL8ADR:
910 nasm_error(ERR_NONFATAL,
911 "32-bit ELF format does not support 64-bit relocations");
912 p = mydata;
913 WRITEDLONG(p, 0);
914 elf_sect_write(s, mydata, 8L);
915 break;
919 static void elf_write(void)
921 int align;
922 char *p;
923 int i;
925 struct SAA *symtab;
926 int32_t symtablen, symtablocal;
929 * Work out how many sections we will have. We have SHN_UNDEF,
930 * then the flexible user sections, then the fixed sections
931 * `.shstrtab', `.symtab' and `.strtab', then optionally
932 * relocation sections for the user sections.
934 nsections = sec_numspecial + 1;
935 if (of_elf32.current_dfmt == &df_stabs)
936 nsections += 3;
937 else if (of_elf32.current_dfmt == &df_dwarf)
938 nsections += 10;
940 add_sectname("", ".shstrtab");
941 add_sectname("", ".symtab");
942 add_sectname("", ".strtab");
943 for (i = 0; i < nsects; i++) {
944 nsections++; /* for the section itself */
945 if (sects[i]->head) {
946 nsections++; /* for its relocations */
947 add_sectname(".rel", sects[i]->name);
951 if (of_elf32.current_dfmt == &df_stabs) {
952 /* in case the debug information is wanted, just add these three sections... */
953 add_sectname("", ".stab");
954 add_sectname("", ".stabstr");
955 add_sectname(".rel", ".stab");
956 } else if (of_elf32.current_dfmt == &df_dwarf) {
957 /* the dwarf debug standard specifies the following ten sections,
958 not all of which are currently implemented,
959 although all of them are defined. */
960 add_sectname("", ".debug_aranges");
961 add_sectname(".rela", ".debug_aranges");
962 add_sectname("", ".debug_pubnames");
963 add_sectname("", ".debug_info");
964 add_sectname(".rela", ".debug_info");
965 add_sectname("", ".debug_abbrev");
966 add_sectname("", ".debug_line");
967 add_sectname(".rela", ".debug_line");
968 add_sectname("", ".debug_frame");
969 add_sectname("", ".debug_loc");
973 * Output the ELF header.
975 fwrite("\177ELF\1\1\1", 7, 1, ofile);
976 fputc(elf_osabi, ofile);
977 fputc(elf_abiver, ofile);
978 fwritezero(7, ofile);
979 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
980 fwriteint16_t(3, ofile); /* EM_386 processor ID */
981 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
982 fwriteint32_t(0L, ofile); /* no entry point */
983 fwriteint32_t(0L, ofile); /* no program header table */
984 fwriteint32_t(0x40L, ofile); /* section headers straight after
985 * ELF header plus alignment */
986 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
987 fwriteint16_t(0x34, ofile); /* size of ELF header */
988 fwriteint16_t(0, ofile); /* no program header table, again */
989 fwriteint16_t(0, ofile); /* still no program header table */
990 fwriteint16_t(0x28, ofile); /* size of section header */
991 fwriteint16_t(nsections, ofile); /* number of sections */
992 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
993 * section header table */
994 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
995 fwriteint32_t(0L, ofile);
996 fwriteint32_t(0L, ofile);
999 * Build the symbol table and relocation tables.
1001 symtab = elf_build_symtab(&symtablen, &symtablocal);
1002 for (i = 0; i < nsects; i++)
1003 if (sects[i]->head)
1004 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1005 sects[i]->head);
1008 * Now output the section header table.
1011 elf_foffs = 0x40 + 0x28 * nsections;
1012 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1013 elf_foffs += align;
1014 elf_nsect = 0;
1015 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1017 /* SHN_UNDEF */
1018 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1019 p = shstrtab + 1;
1021 /* The normal sections */
1022 for (i = 0; i < nsects; i++) {
1023 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1024 (sects[i]->type == SHT_PROGBITS ?
1025 sects[i]->data : NULL), true,
1026 sects[i]->len, 0, 0, sects[i]->align, 0);
1027 p += strlen(p) + 1;
1030 /* .shstrtab */
1031 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1032 shstrtablen, 0, 0, 1, 0);
1033 p += strlen(p) + 1;
1035 /* .symtab */
1036 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1037 symtablen, sec_strtab, symtablocal, 4, 16);
1038 p += strlen(p) + 1;
1040 /* .strtab */
1041 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1042 strslen, 0, 0, 1, 0);
1043 p += strlen(p) + 1;
1045 /* The relocation sections */
1046 for (i = 0; i < nsects; i++)
1047 if (sects[i]->head) {
1048 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1049 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1050 p += strlen(p) + 1;
1053 if (of_elf32.current_dfmt == &df_stabs) {
1054 /* for debugging information, create the last three sections
1055 which are the .stab , .stabstr and .rel.stab sections respectively */
1057 /* this function call creates the stab sections in memory */
1058 stabs32_generate();
1060 if (stabbuf && stabstrbuf && stabrelbuf) {
1061 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1062 stablen, sec_stabstr, 0, 4, 12);
1063 p += strlen(p) + 1;
1065 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1066 stabstrlen, 0, 0, 4, 0);
1067 p += strlen(p) + 1;
1069 /* link -> symtable info -> section to refer to */
1070 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1071 stabrellen, sec_symtab, sec_stab, 4, 8);
1072 p += strlen(p) + 1;
1074 } else if (of_elf32.current_dfmt == &df_dwarf) {
1075 /* for dwarf debugging information, create the ten dwarf sections */
1077 /* this function call creates the dwarf sections in memory */
1078 if (dwarf_fsect)
1079 dwarf32_generate();
1081 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1082 arangeslen, 0, 0, 1, 0);
1083 p += strlen(p) + 1;
1085 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1086 arangesrellen, sec_symtab, sec_debug_aranges,
1087 1, 12);
1088 p += strlen(p) + 1;
1090 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1091 false, pubnameslen, 0, 0, 1, 0);
1092 p += strlen(p) + 1;
1094 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1095 infolen, 0, 0, 1, 0);
1096 p += strlen(p) + 1;
1098 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1099 inforellen, sec_symtab, sec_debug_info, 1, 12);
1100 p += strlen(p) + 1;
1102 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1103 abbrevlen, 0, 0, 1, 0);
1104 p += strlen(p) + 1;
1106 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1107 linelen, 0, 0, 1, 0);
1108 p += strlen(p) + 1;
1110 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1111 linerellen, sec_symtab, sec_debug_line, 1, 12);
1112 p += strlen(p) + 1;
1114 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1115 framelen, 0, 0, 8, 0);
1116 p += strlen(p) + 1;
1118 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1119 loclen, 0, 0, 1, 0);
1120 p += strlen(p) + 1;
1122 fwritezero(align, ofile);
1125 * Now output the sections.
1127 elf_write_sections();
1129 nasm_free(elf_sects);
1130 saa_free(symtab);
1133 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1135 struct SAA *s = saa_init(1L);
1136 struct Symbol *sym;
1137 uint8_t entry[16], *p;
1138 int i;
1140 *len = *local = 0;
1143 * First, an all-zeros entry, required by the ELF spec.
1145 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1146 *len += 16;
1147 (*local)++;
1150 * Next, an entry for the file name.
1152 p = entry;
1153 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1154 WRITELONG(p, 0); /* no value */
1155 WRITELONG(p, 0); /* no size either */
1156 WRITESHORT(p, STT_FILE); /* type FILE */
1157 WRITESHORT(p, SHN_ABS);
1158 saa_wbytes(s, entry, 16L);
1159 *len += 16;
1160 (*local)++;
1163 * Now some standard symbols defining the segments, for relocation
1164 * purposes.
1166 for (i = 1; i <= nsects; i++) {
1167 p = entry;
1168 WRITELONG(p, 0); /* no symbol name */
1169 WRITELONG(p, 0); /* offset zero */
1170 WRITELONG(p, 0); /* size zero */
1171 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1172 WRITESHORT(p, i); /* section id */
1173 saa_wbytes(s, entry, 16L);
1174 *len += 16;
1175 (*local)++;
1179 * Now the other local symbols.
1181 saa_rewind(syms);
1182 while ((sym = saa_rstruct(syms))) {
1183 if (sym->type & SYM_GLOBAL)
1184 continue;
1185 p = entry;
1186 WRITELONG(p, sym->strpos);
1187 WRITELONG(p, sym->symv.key);
1188 WRITELONG(p, sym->size);
1189 WRITECHAR(p, sym->type); /* type and binding */
1190 WRITECHAR(p, sym->other); /* visibility */
1191 WRITESHORT(p, sym->section);
1192 saa_wbytes(s, entry, 16L);
1193 *len += 16;
1194 (*local)++;
1197 * dwarf needs symbols for debug sections
1198 * which are relocation targets.
1200 //*** fix for 32 bit
1201 if (of_elf32.current_dfmt == &df_dwarf) {
1202 dwarf_infosym = *local;
1203 p = entry;
1204 WRITELONG(p, 0); /* no symbol name */
1205 WRITELONG(p, (uint32_t) 0); /* offset zero */
1206 WRITELONG(p, (uint32_t) 0); /* size zero */
1207 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1208 WRITESHORT(p, sec_debug_info); /* section id */
1209 saa_wbytes(s, entry, 16L);
1210 *len += 16;
1211 (*local)++;
1212 dwarf_abbrevsym = *local;
1213 p = entry;
1214 WRITELONG(p, 0); /* no symbol name */
1215 WRITELONG(p, (uint32_t) 0); /* offset zero */
1216 WRITELONG(p, (uint32_t) 0); /* size zero */
1217 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1218 WRITESHORT(p, sec_debug_abbrev); /* section id */
1219 saa_wbytes(s, entry, 16L);
1220 *len += 16;
1221 (*local)++;
1222 dwarf_linesym = *local;
1223 p = entry;
1224 WRITELONG(p, 0); /* no symbol name */
1225 WRITELONG(p, (uint32_t) 0); /* offset zero */
1226 WRITELONG(p, (uint32_t) 0); /* size zero */
1227 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1228 WRITESHORT(p, sec_debug_line); /* section id */
1229 saa_wbytes(s, entry, 16L);
1230 *len += 16;
1231 (*local)++;
1235 * Now the global symbols.
1237 saa_rewind(syms);
1238 while ((sym = saa_rstruct(syms))) {
1239 if (!(sym->type & SYM_GLOBAL))
1240 continue;
1241 p = entry;
1242 WRITELONG(p, sym->strpos);
1243 WRITELONG(p, sym->symv.key);
1244 WRITELONG(p, sym->size);
1245 WRITECHAR(p, sym->type); /* type and binding */
1246 WRITECHAR(p, sym->other); /* visibility */
1247 WRITESHORT(p, sym->section);
1248 saa_wbytes(s, entry, 16L);
1249 *len += 16;
1252 return s;
1255 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1257 struct SAA *s;
1258 uint8_t *p, entry[8];
1259 int32_t global_offset;
1261 if (!r)
1262 return NULL;
1264 s = saa_init(1L);
1265 *len = 0;
1268 * How to onvert from a global placeholder to a real symbol index;
1269 * the +2 refers to the two special entries, the null entry and
1270 * the filename entry.
1272 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1274 while (r) {
1275 int32_t sym = r->symbol;
1278 * Create a real symbol index; the +2 refers to the two special
1279 * entries, the null entry and the filename entry.
1281 if (sym >= GLOBAL_TEMP_BASE)
1282 sym += global_offset;
1284 p = entry;
1285 WRITELONG(p, r->address);
1286 WRITELONG(p, (sym << 8) + r->type);
1287 saa_wbytes(s, entry, 8L);
1288 *len += 8;
1290 r = r->next;
1293 return s;
1296 static void elf_section_header(int name, int type, int flags,
1297 void *data, bool is_saa, int32_t datalen,
1298 int link, int info, int align, int eltsize)
1300 elf_sects[elf_nsect].data = data;
1301 elf_sects[elf_nsect].len = datalen;
1302 elf_sects[elf_nsect].is_saa = is_saa;
1303 elf_nsect++;
1305 fwriteint32_t((int32_t)name, ofile);
1306 fwriteint32_t((int32_t)type, ofile);
1307 fwriteint32_t((int32_t)flags, ofile);
1308 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1309 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1310 fwriteint32_t(datalen, ofile);
1311 if (data)
1312 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1313 fwriteint32_t((int32_t)link, ofile);
1314 fwriteint32_t((int32_t)info, ofile);
1315 fwriteint32_t((int32_t)align, ofile);
1316 fwriteint32_t((int32_t)eltsize, ofile);
1319 static void elf_write_sections(void)
1321 int i;
1322 for (i = 0; i < elf_nsect; i++)
1323 if (elf_sects[i].data) {
1324 int32_t len = elf_sects[i].len;
1325 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1326 int32_t align = reallen - len;
1327 if (elf_sects[i].is_saa)
1328 saa_fpwrite(elf_sects[i].data, ofile);
1329 else
1330 fwrite(elf_sects[i].data, len, 1, ofile);
1331 fwritezero(align, ofile);
1335 static void elf_sect_write(struct Section *sect,
1336 const uint8_t *data, uint32_t len)
1338 saa_wbytes(sect->data, data, len);
1339 sect->len += len;
1342 static void elf_sectalign(int32_t seg, unsigned int value)
1344 struct Section *s = NULL;
1345 int i;
1347 for (i = 0; i < nsects; i++) {
1348 if (sects[i]->index == seg) {
1349 s = sects[i];
1350 break;
1353 if (!s || !is_power2(value))
1354 return;
1356 if (value > s->align)
1357 s->align = value;
1360 static int32_t elf_segbase(int32_t segment)
1362 return segment;
1365 static int elf_directive(enum directives directive, char *value, int pass)
1367 bool err;
1368 int64_t n;
1369 char *p;
1371 switch (directive) {
1372 case D_OSABI:
1373 if (pass == 2)
1374 return 1; /* ignore in pass 2 */
1376 n = readnum(value, &err);
1377 if (err) {
1378 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1379 return 1;
1381 if (n < 0 || n > 255) {
1382 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1383 return 1;
1385 elf_osabi = n;
1386 elf_abiver = 0;
1388 if ((p = strchr(value,',')) == NULL)
1389 return 1;
1391 n = readnum(p+1, &err);
1392 if (err || n < 0 || n > 255) {
1393 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1394 return 1;
1397 elf_abiver = n;
1398 return 1;
1400 default:
1401 return 0;
1405 static void elf_filename(char *inname, char *outname)
1407 strcpy(elf_module, inname);
1408 standard_extension(inname, outname, ".o");
1411 extern macros_t elf_stdmac[];
1413 static int elf_set_info(enum geninfo type, char **val)
1415 (void)type;
1416 (void)val;
1417 return 0;
1419 static struct dfmt df_dwarf = {
1420 "ELF32 (i386) dwarf debug format for Linux/Unix",
1421 "dwarf",
1422 dwarf32_init,
1423 dwarf32_linenum,
1424 debug32_deflabel,
1425 debug32_directive,
1426 debug32_typevalue,
1427 dwarf32_output,
1428 dwarf32_cleanup
1430 static struct dfmt df_stabs = {
1431 "ELF32 (i386) stabs debug format for Linux/Unix",
1432 "stabs",
1433 null_debug_init,
1434 stabs32_linenum,
1435 debug32_deflabel,
1436 debug32_directive,
1437 debug32_typevalue,
1438 stabs32_output,
1439 stabs32_cleanup
1442 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1444 struct ofmt of_elf32 = {
1445 "ELF32 (i386) object files (e.g. Linux)",
1446 "elf32",
1448 elf32_debugs_arr,
1449 &df_stabs,
1450 elf_stdmac,
1451 elf_init,
1452 elf_set_info,
1453 elf_out,
1454 elf_deflabel,
1455 elf_section_names,
1456 elf_sectalign,
1457 elf_segbase,
1458 elf_directive,
1459 elf_filename,
1460 elf_cleanup
1463 /* again, the stabs debugging stuff (code) */
1465 static void stabs32_linenum(const char *filename, int32_t linenumber,
1466 int32_t segto)
1468 (void)segto;
1470 if (!stabs_filename) {
1471 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1472 strcpy(stabs_filename, filename);
1473 } else {
1474 if (strcmp(stabs_filename, filename)) {
1476 * yep, a memory leak...this program is one-shot anyway, so who cares...
1477 * in fact, this leak comes in quite handy to maintain a list of files
1478 * encountered so far in the symbol lines...
1481 /* why not nasm_free(stabs_filename); we're done with the old one */
1483 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1484 strcpy(stabs_filename, filename);
1487 debug_immcall = 1;
1488 currentline = linenumber;
1491 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1492 char *special)
1494 (void)name;
1495 (void)segment;
1496 (void)offset;
1497 (void)is_global;
1498 (void)special;
1501 static void debug32_directive(const char *directive, const char *params)
1503 (void)directive;
1504 (void)params;
1507 static void debug32_typevalue(int32_t type)
1509 int32_t stype, ssize;
1510 switch (TYM_TYPE(type)) {
1511 case TY_LABEL:
1512 ssize = 0;
1513 stype = STT_NOTYPE;
1514 break;
1515 case TY_BYTE:
1516 ssize = 1;
1517 stype = STT_OBJECT;
1518 break;
1519 case TY_WORD:
1520 ssize = 2;
1521 stype = STT_OBJECT;
1522 break;
1523 case TY_DWORD:
1524 ssize = 4;
1525 stype = STT_OBJECT;
1526 break;
1527 case TY_FLOAT:
1528 ssize = 4;
1529 stype = STT_OBJECT;
1530 break;
1531 case TY_QWORD:
1532 ssize = 8;
1533 stype = STT_OBJECT;
1534 break;
1535 case TY_TBYTE:
1536 ssize = 10;
1537 stype = STT_OBJECT;
1538 break;
1539 case TY_OWORD:
1540 ssize = 16;
1541 stype = STT_OBJECT;
1542 break;
1543 case TY_YWORD:
1544 ssize = 32;
1545 stype = STT_OBJECT;
1546 break;
1547 case TY_COMMON:
1548 ssize = 0;
1549 stype = STT_COMMON;
1550 break;
1551 case TY_SEG:
1552 ssize = 0;
1553 stype = STT_SECTION;
1554 break;
1555 case TY_EXTERN:
1556 ssize = 0;
1557 stype = STT_NOTYPE;
1558 break;
1559 case TY_EQU:
1560 ssize = 0;
1561 stype = STT_NOTYPE;
1562 break;
1563 default:
1564 ssize = 0;
1565 stype = STT_NOTYPE;
1566 break;
1568 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1569 lastsym->size = ssize;
1570 lastsym->type = stype;
1574 static void stabs32_output(int type, void *param)
1576 struct symlininfo *s;
1577 struct linelist *el;
1578 if (type == TY_STABSSYMLIN) {
1579 if (debug_immcall) {
1580 s = (struct symlininfo *)param;
1581 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1582 return; /* line info is only collected for executable sections */
1583 numlinestabs++;
1584 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1585 el->info.offset = s->offset;
1586 el->info.section = s->section;
1587 el->info.name = s->name;
1588 el->line = currentline;
1589 el->filename = stabs_filename;
1590 el->next = 0;
1591 if (stabslines) {
1592 stabslines->last->next = el;
1593 stabslines->last = el;
1594 } else {
1595 stabslines = el;
1596 stabslines->last = el;
1600 debug_immcall = 0;
1603 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1605 static void stabs32_generate(void)
1607 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1608 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1609 char **allfiles;
1610 int *fileidx;
1612 struct linelist *ptr;
1614 ptr = stabslines;
1616 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1617 for (i = 0; i < numlinestabs; i++)
1618 allfiles[i] = 0;
1619 numfiles = 0;
1620 while (ptr) {
1621 if (numfiles == 0) {
1622 allfiles[0] = ptr->filename;
1623 numfiles++;
1624 } else {
1625 for (i = 0; i < numfiles; i++) {
1626 if (!strcmp(allfiles[i], ptr->filename))
1627 break;
1629 if (i >= numfiles) {
1630 allfiles[i] = ptr->filename;
1631 numfiles++;
1634 ptr = ptr->next;
1636 strsize = 1;
1637 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1638 for (i = 0; i < numfiles; i++) {
1639 fileidx[i] = strsize;
1640 strsize += strlen(allfiles[i]) + 1;
1642 mainfileindex = 0;
1643 for (i = 0; i < numfiles; i++) {
1644 if (!strcmp(allfiles[i], elf_module)) {
1645 mainfileindex = i;
1646 break;
1651 * worst case size of the stab buffer would be:
1652 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1653 * plus one "ending" entry
1655 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1656 sizeof(struct stabentry));
1657 ssbuf = (uint8_t *)nasm_malloc(strsize);
1658 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1659 rptr = rbuf;
1661 for (i = 0; i < numfiles; i++)
1662 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1663 ssbuf[0] = 0;
1665 stabstrlen = strsize; /* set global variable for length of stab strings */
1667 sptr = sbuf;
1668 ptr = stabslines;
1669 numstabs = 0;
1671 if (ptr) {
1673 * this is the first stab, its strx points to the filename of the
1674 * the source-file, the n_desc field should be set to the number
1675 * of remaining stabs
1677 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1679 /* this is the stab for the main source file */
1680 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1682 /* relocation table entry */
1685 * Since the symbol table has two entries before
1686 * the section symbols, the index in the info.section
1687 * member must be adjusted by adding 2
1690 WRITELONG(rptr, (sptr - sbuf) - 4);
1691 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1693 numstabs++;
1694 currfile = mainfileindex;
1697 while (ptr) {
1698 if (strcmp(allfiles[currfile], ptr->filename)) {
1699 /* oops file has changed... */
1700 for (i = 0; i < numfiles; i++)
1701 if (!strcmp(allfiles[i], ptr->filename))
1702 break;
1703 currfile = i;
1704 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1705 ptr->info.offset);
1706 numstabs++;
1708 /* relocation table entry */
1709 WRITELONG(rptr, (sptr - sbuf) - 4);
1710 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1713 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1714 numstabs++;
1716 /* relocation table entry */
1718 WRITELONG(rptr, (sptr - sbuf) - 4);
1719 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1721 ptr = ptr->next;
1725 /* this is an "ending" token */
1726 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1727 numstabs++;
1729 ((struct stabentry *)sbuf)->n_desc = numstabs;
1731 nasm_free(allfiles);
1732 nasm_free(fileidx);
1734 stablen = (sptr - sbuf);
1735 stabrellen = (rptr - rbuf);
1736 stabrelbuf = rbuf;
1737 stabbuf = sbuf;
1738 stabstrbuf = ssbuf;
1741 static void stabs32_cleanup(void)
1743 struct linelist *ptr, *del;
1744 if (!stabslines)
1745 return;
1747 ptr = stabslines;
1748 while (ptr) {
1749 del = ptr;
1750 ptr = ptr->next;
1751 nasm_free(del);
1754 nasm_free(stabbuf);
1755 nasm_free(stabrelbuf);
1756 nasm_free(stabstrbuf);
1759 /* dwarf routines */
1761 static void dwarf32_init(void)
1763 ndebugs = 3; /* 3 debug symbols */
1766 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1767 int32_t segto)
1769 (void)segto;
1770 dwarf32_findfile(filename);
1771 debug_immcall = 1;
1772 currentline = linenumber;
1775 /* called from elf_out with type == TY_DEBUGSYMLIN */
1776 static void dwarf32_output(int type, void *param)
1778 int ln, aa, inx, maxln, soc;
1779 struct symlininfo *s;
1780 struct SAA *plinep;
1782 (void)type;
1784 s = (struct symlininfo *)param;
1786 /* line number info is only gathered for executable sections */
1787 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1788 return;
1790 /* Check if section index has changed */
1791 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1792 dwarf32_findsect(s->section);
1794 /* do nothing unless line or file has changed */
1795 if (!debug_immcall)
1796 return;
1798 ln = currentline - dwarf_csect->line;
1799 aa = s->offset - dwarf_csect->offset;
1800 inx = dwarf_clist->line;
1801 plinep = dwarf_csect->psaa;
1802 /* check for file change */
1803 if (!(inx == dwarf_csect->file)) {
1804 saa_write8(plinep,DW_LNS_set_file);
1805 saa_write8(plinep,inx);
1806 dwarf_csect->file = inx;
1808 /* check for line change */
1809 if (ln) {
1810 /* test if in range of special op code */
1811 maxln = line_base + line_range;
1812 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1813 if (ln >= line_base && ln < maxln && soc < 256) {
1814 saa_write8(plinep,soc);
1815 } else {
1816 saa_write8(plinep,DW_LNS_advance_line);
1817 saa_wleb128s(plinep,ln);
1818 if (aa) {
1819 saa_write8(plinep,DW_LNS_advance_pc);
1820 saa_wleb128u(plinep,aa);
1823 dwarf_csect->line = currentline;
1824 dwarf_csect->offset = s->offset;
1827 /* show change handled */
1828 debug_immcall = 0;
1832 static void dwarf32_generate(void)
1834 uint8_t *pbuf;
1835 int indx;
1836 struct linelist *ftentry;
1837 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1838 struct SAA *parangesrel, *plinesrel, *pinforel;
1839 struct sectlist *psect;
1840 size_t saalen, linepoff, totlen, highaddr;
1842 /* write epilogues for each line program range */
1843 /* and build aranges section */
1844 paranges = saa_init(1L);
1845 parangesrel = saa_init(1L);
1846 saa_write16(paranges,2); /* dwarf version */
1847 saa_write32(parangesrel, paranges->datalen+4);
1848 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1849 saa_write32(parangesrel, 0);
1850 saa_write32(paranges,0); /* offset into info */
1851 saa_write8(paranges,4); /* pointer size */
1852 saa_write8(paranges,0); /* not segmented */
1853 saa_write32(paranges,0); /* padding */
1854 /* iterate though sectlist entries */
1855 psect = dwarf_fsect;
1856 totlen = 0;
1857 highaddr = 0;
1858 for (indx = 0; indx < dwarf_nsections; indx++) {
1859 plinep = psect->psaa;
1860 /* Line Number Program Epilogue */
1861 saa_write8(plinep,2); /* std op 2 */
1862 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1863 saa_write8(plinep,DW_LNS_extended_op);
1864 saa_write8(plinep,1); /* operand length */
1865 saa_write8(plinep,DW_LNE_end_sequence);
1866 totlen += plinep->datalen;
1867 /* range table relocation entry */
1868 saa_write32(parangesrel, paranges->datalen + 4);
1869 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1870 saa_write32(parangesrel, (uint32_t) 0);
1871 /* range table entry */
1872 saa_write32(paranges,0x0000); /* range start */
1873 saa_write32(paranges,sects[psect->section]->len); /* range length */
1874 highaddr += sects[psect->section]->len;
1875 /* done with this entry */
1876 psect = psect->next;
1878 saa_write32(paranges,0); /* null address */
1879 saa_write32(paranges,0); /* null length */
1880 saalen = paranges->datalen;
1881 arangeslen = saalen + 4;
1882 arangesbuf = pbuf = nasm_malloc(arangeslen);
1883 WRITELONG(pbuf,saalen); /* initial length */
1884 saa_rnbytes(paranges, pbuf, saalen);
1885 saa_free(paranges);
1887 /* build rela.aranges section */
1888 arangesrellen = saalen = parangesrel->datalen;
1889 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1890 saa_rnbytes(parangesrel, pbuf, saalen);
1891 saa_free(parangesrel);
1893 /* build pubnames section */
1894 ppubnames = saa_init(1L);
1895 saa_write16(ppubnames,3); /* dwarf version */
1896 saa_write32(ppubnames,0); /* offset into info */
1897 saa_write32(ppubnames,0); /* space used in info */
1898 saa_write32(ppubnames,0); /* end of list */
1899 saalen = ppubnames->datalen;
1900 pubnameslen = saalen + 4;
1901 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1902 WRITELONG(pbuf,saalen); /* initial length */
1903 saa_rnbytes(ppubnames, pbuf, saalen);
1904 saa_free(ppubnames);
1906 /* build info section */
1907 pinfo = saa_init(1L);
1908 pinforel = saa_init(1L);
1909 saa_write16(pinfo,2); /* dwarf version */
1910 saa_write32(pinforel, pinfo->datalen + 4);
1911 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1912 saa_write32(pinforel, 0);
1913 saa_write32(pinfo,0); /* offset into abbrev */
1914 saa_write8(pinfo,4); /* pointer size */
1915 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1916 saa_write32(pinforel, pinfo->datalen + 4);
1917 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1918 saa_write32(pinforel, 0);
1919 saa_write32(pinfo,0); /* DW_AT_low_pc */
1920 saa_write32(pinforel, pinfo->datalen + 4);
1921 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1922 saa_write32(pinforel, 0);
1923 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1924 saa_write32(pinforel, pinfo->datalen + 4);
1925 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1926 saa_write32(pinforel, 0);
1927 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1928 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1929 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1930 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1931 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1932 saa_write32(pinforel, pinfo->datalen + 4);
1933 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1934 saa_write32(pinforel, 0);
1935 saa_write32(pinfo,0); /* DW_AT_low_pc */
1936 saa_write32(pinfo,0); /* DW_AT_frame_base */
1937 saa_write8(pinfo,0); /* end of entries */
1938 saalen = pinfo->datalen;
1939 infolen = saalen + 4;
1940 infobuf = pbuf = nasm_malloc(infolen);
1941 WRITELONG(pbuf,saalen); /* initial length */
1942 saa_rnbytes(pinfo, pbuf, saalen);
1943 saa_free(pinfo);
1945 /* build rela.info section */
1946 inforellen = saalen = pinforel->datalen;
1947 inforelbuf = pbuf = nasm_malloc(inforellen);
1948 saa_rnbytes(pinforel, pbuf, saalen);
1949 saa_free(pinforel);
1951 /* build abbrev section */
1952 pabbrev = saa_init(1L);
1953 saa_write8(pabbrev,1); /* entry number LEB128u */
1954 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1955 saa_write8(pabbrev,1); /* has children */
1956 /* the following attributes and forms are all LEB128u values */
1957 saa_write8(pabbrev,DW_AT_low_pc);
1958 saa_write8(pabbrev,DW_FORM_addr);
1959 saa_write8(pabbrev,DW_AT_high_pc);
1960 saa_write8(pabbrev,DW_FORM_addr);
1961 saa_write8(pabbrev,DW_AT_stmt_list);
1962 saa_write8(pabbrev,DW_FORM_data4);
1963 saa_write8(pabbrev,DW_AT_name);
1964 saa_write8(pabbrev,DW_FORM_string);
1965 saa_write8(pabbrev,DW_AT_producer);
1966 saa_write8(pabbrev,DW_FORM_string);
1967 saa_write8(pabbrev,DW_AT_language);
1968 saa_write8(pabbrev,DW_FORM_data2);
1969 saa_write16(pabbrev,0); /* end of entry */
1970 /* LEB128u usage same as above */
1971 saa_write8(pabbrev,2); /* entry number */
1972 saa_write8(pabbrev,DW_TAG_subprogram);
1973 saa_write8(pabbrev,0); /* no children */
1974 saa_write8(pabbrev,DW_AT_low_pc);
1975 saa_write8(pabbrev,DW_FORM_addr);
1976 saa_write8(pabbrev,DW_AT_frame_base);
1977 saa_write8(pabbrev,DW_FORM_data4);
1978 saa_write16(pabbrev,0); /* end of entry */
1979 abbrevlen = saalen = pabbrev->datalen;
1980 abbrevbuf = pbuf = nasm_malloc(saalen);
1981 saa_rnbytes(pabbrev, pbuf, saalen);
1982 saa_free(pabbrev);
1984 /* build line section */
1985 /* prolog */
1986 plines = saa_init(1L);
1987 saa_write8(plines,1); /* Minimum Instruction Length */
1988 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1989 saa_write8(plines,line_base); /* Line Base */
1990 saa_write8(plines,line_range); /* Line Range */
1991 saa_write8(plines,opcode_base); /* Opcode Base */
1992 /* standard opcode lengths (# of LEB128u operands) */
1993 saa_write8(plines,0); /* Std opcode 1 length */
1994 saa_write8(plines,1); /* Std opcode 2 length */
1995 saa_write8(plines,1); /* Std opcode 3 length */
1996 saa_write8(plines,1); /* Std opcode 4 length */
1997 saa_write8(plines,1); /* Std opcode 5 length */
1998 saa_write8(plines,0); /* Std opcode 6 length */
1999 saa_write8(plines,0); /* Std opcode 7 length */
2000 saa_write8(plines,0); /* Std opcode 8 length */
2001 saa_write8(plines,1); /* Std opcode 9 length */
2002 saa_write8(plines,0); /* Std opcode 10 length */
2003 saa_write8(plines,0); /* Std opcode 11 length */
2004 saa_write8(plines,1); /* Std opcode 12 length */
2005 /* Directory Table */
2006 saa_write8(plines,0); /* End of table */
2007 /* File Name Table */
2008 ftentry = dwarf_flist;
2009 for (indx = 0; indx < dwarf_numfiles; indx++) {
2010 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2011 saa_write8(plines,0); /* directory LEB128u */
2012 saa_write8(plines,0); /* time LEB128u */
2013 saa_write8(plines,0); /* size LEB128u */
2014 ftentry = ftentry->next;
2016 saa_write8(plines,0); /* End of table */
2017 linepoff = plines->datalen;
2018 linelen = linepoff + totlen + 10;
2019 linebuf = pbuf = nasm_malloc(linelen);
2020 WRITELONG(pbuf,linelen-4); /* initial length */
2021 WRITESHORT(pbuf,3); /* dwarf version */
2022 WRITELONG(pbuf,linepoff); /* offset to line number program */
2023 /* write line header */
2024 saalen = linepoff;
2025 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2026 pbuf += linepoff;
2027 saa_free(plines);
2028 /* concatonate line program ranges */
2029 linepoff += 13;
2030 plinesrel = saa_init(1L);
2031 psect = dwarf_fsect;
2032 for (indx = 0; indx < dwarf_nsections; indx++) {
2033 saa_write32(plinesrel, linepoff);
2034 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2035 saa_write32(plinesrel, (uint32_t) 0);
2036 plinep = psect->psaa;
2037 saalen = plinep->datalen;
2038 saa_rnbytes(plinep, pbuf, saalen);
2039 pbuf += saalen;
2040 linepoff += saalen;
2041 saa_free(plinep);
2042 /* done with this entry */
2043 psect = psect->next;
2047 /* build rela.lines section */
2048 linerellen =saalen = plinesrel->datalen;
2049 linerelbuf = pbuf = nasm_malloc(linerellen);
2050 saa_rnbytes(plinesrel, pbuf, saalen);
2051 saa_free(plinesrel);
2053 /* build frame section */
2054 framelen = 4;
2055 framebuf = pbuf = nasm_malloc(framelen);
2056 WRITELONG(pbuf,framelen-4); /* initial length */
2058 /* build loc section */
2059 loclen = 16;
2060 locbuf = pbuf = nasm_malloc(loclen);
2061 WRITELONG(pbuf,0); /* null beginning offset */
2062 WRITELONG(pbuf,0); /* null ending offset */
2065 static void dwarf32_cleanup(void)
2067 nasm_free(arangesbuf);
2068 nasm_free(arangesrelbuf);
2069 nasm_free(pubnamesbuf);
2070 nasm_free(infobuf);
2071 nasm_free(inforelbuf);
2072 nasm_free(abbrevbuf);
2073 nasm_free(linebuf);
2074 nasm_free(linerelbuf);
2075 nasm_free(framebuf);
2076 nasm_free(locbuf);
2079 static void dwarf32_findfile(const char * fname)
2081 int finx;
2082 struct linelist *match;
2084 /* return if fname is current file name */
2085 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2086 return;
2088 /* search for match */
2089 match = 0;
2090 if (dwarf_flist) {
2091 match = dwarf_flist;
2092 for (finx = 0; finx < dwarf_numfiles; finx++) {
2093 if (!(strcmp(fname, match->filename))) {
2094 dwarf_clist = match;
2095 return;
2100 /* add file name to end of list */
2101 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2102 dwarf_numfiles++;
2103 dwarf_clist->line = dwarf_numfiles;
2104 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2105 strcpy(dwarf_clist->filename,fname);
2106 dwarf_clist->next = 0;
2107 if (!dwarf_flist) { /* if first entry */
2108 dwarf_flist = dwarf_elist = dwarf_clist;
2109 dwarf_clist->last = 0;
2110 } else { /* chain to previous entry */
2111 dwarf_elist->next = dwarf_clist;
2112 dwarf_elist = dwarf_clist;
2116 static void dwarf32_findsect(const int index)
2118 int sinx;
2119 struct sectlist *match;
2120 struct SAA *plinep;
2122 /* return if index is current section index */
2123 if (dwarf_csect && (dwarf_csect->section == index))
2124 return;
2126 /* search for match */
2127 match = 0;
2128 if (dwarf_fsect) {
2129 match = dwarf_fsect;
2130 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2131 if ((match->section == index)) {
2132 dwarf_csect = match;
2133 return;
2135 match = match->next;
2139 /* add entry to end of list */
2140 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2141 dwarf_nsections++;
2142 dwarf_csect->psaa = plinep = saa_init(1L);
2143 dwarf_csect->line = 1;
2144 dwarf_csect->offset = 0;
2145 dwarf_csect->file = 1;
2146 dwarf_csect->section = index;
2147 dwarf_csect->next = 0;
2148 /* set relocatable address at start of line program */
2149 saa_write8(plinep,DW_LNS_extended_op);
2150 saa_write8(plinep,5); /* operand length */
2151 saa_write8(plinep,DW_LNE_set_address);
2152 saa_write32(plinep,0); /* Start Address */
2154 if (!dwarf_fsect) { /* if first entry */
2155 dwarf_fsect = dwarf_esect = dwarf_csect;
2156 dwarf_csect->last = 0;
2157 } else { /* chain to previous entry */
2158 dwarf_esect->next = dwarf_csect;
2159 dwarf_esect = dwarf_csect;
2163 #endif /* OF_ELF */