NASM 2.09rc1
[nasm/avx512.git] / output / outelf32.c
blob8a14af459a87e0999f5c8b1ff48836ce15e49439
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>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELF32
64 * Relocation types.
66 struct Reloc {
67 struct Reloc *next;
68 int32_t address; /* relative to _start_ of section */
69 int32_t symbol; /* symbol index */
70 int type; /* type of relocation */
73 struct Symbol {
74 struct rbtree symv; /* symbol value and symbol rbtree */
75 int32_t strpos; /* string table position of name */
76 int32_t section; /* section ID of the symbol */
77 int type; /* symbol type */
78 int other; /* symbol visibility */
79 int32_t size; /* size of symbol */
80 int32_t globnum; /* symbol table offset if global */
81 struct Symbol *nextfwd; /* list of unresolved-size symbols */
82 char *name; /* used temporarily if in above list */
85 struct Section {
86 struct SAA *data;
87 uint32_t len, size, nrelocs;
88 int32_t index;
89 int type; /* SHT_PROGBITS or SHT_NOBITS */
90 uint32_t align; /* alignment: power of two */
91 uint32_t flags; /* section flags */
92 char *name;
93 struct SAA *rel;
94 int32_t rellen;
95 struct Reloc *head, **tail;
96 struct rbtree *gsyms; /* global symbols in section */
99 #define SECT_DELTA 32
100 static struct Section **sects;
101 static int nsects, sectlen;
103 #define SHSTR_DELTA 256
104 static char *shstrtab;
105 static int shstrtablen, shstrtabsize;
107 static struct SAA *syms;
108 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
110 static int32_t def_seg;
112 static struct RAA *bsym;
114 static struct SAA *strs;
115 static uint32_t strslen;
117 static struct Symbol *fwds;
119 static char elf_module[FILENAME_MAX];
121 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
122 static uint8_t elf_abiver = 0; /* Current ABI version */
124 extern struct ofmt of_elf32;
126 static struct ELF_SECTDATA {
127 void *data;
128 int32_t len;
129 bool is_saa;
130 } *elf_sects;
131 static int elf_nsect, nsections;
132 static int32_t elf_foffs;
134 static void elf_write(void);
135 static void elf_sect_write(struct Section *, const uint8_t *,
136 uint32_t);
137 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
138 int, int);
139 static void elf_write_sections(void);
140 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
141 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
142 static void add_sectname(char *, char *);
144 struct stabentry {
145 uint32_t n_strx;
146 uint8_t n_type;
147 uint8_t n_other;
148 uint16_t n_desc;
149 uint32_t n_value;
152 struct erel {
153 int offset, info;
156 struct symlininfo {
157 int offset;
158 int section; /* section index */
159 char *name; /* shallow-copied pointer of section name */
162 struct linelist {
163 struct symlininfo info;
164 int line;
165 char *filename;
166 struct linelist *next;
167 struct linelist *last;
170 struct sectlist {
171 struct SAA *psaa;
172 int section;
173 int line;
174 int offset;
175 int file;
176 struct sectlist *next;
177 struct sectlist *last;
180 /* common debug variables */
181 static int currentline = 1;
182 static int debug_immcall = 0;
184 /* stabs debug variables */
185 static struct linelist *stabslines = 0;
186 static int numlinestabs = 0;
187 static char *stabs_filename = 0;
188 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
189 static int stablen, stabstrlen, stabrellen;
191 /* dwarf debug variables */
192 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
193 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
194 static int dwarf_numfiles = 0, dwarf_nsections;
195 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
196 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
197 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
198 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
199 abbrevlen, linelen, linerellen, framelen, loclen;
200 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
202 static struct dfmt df_dwarf;
203 static struct dfmt df_stabs;
204 static struct Symbol *lastsym;
206 /* common debugging routines */
207 static void debug32_typevalue(int32_t);
208 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
209 static void debug32_directive(const char *, const char *);
211 /* stabs debugging routines */
212 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
213 static void stabs32_output(int, void *);
214 static void stabs32_generate(void);
215 static void stabs32_cleanup(void);
217 /* dwarf debugging routines */
218 static void dwarf32_init(void);
219 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
220 static void dwarf32_output(int, void *);
221 static void dwarf32_generate(void);
222 static void dwarf32_cleanup(void);
223 static void dwarf32_findfile(const char *);
224 static void dwarf32_findsect(const int);
227 * Special NASM section numbers which are used to define ELF special
228 * symbols, which can be used with WRT to provide PIC and TLS
229 * relocation types.
231 static int32_t elf_gotpc_sect, elf_gotoff_sect;
232 static int32_t elf_got_sect, elf_plt_sect;
233 static int32_t elf_sym_sect, elf_tlsie_sect;
235 static void elf_init(void)
237 sects = NULL;
238 nsects = sectlen = 0;
239 syms = saa_init((int32_t)sizeof(struct Symbol));
240 nlocals = nglobs = ndebugs = 0;
241 bsym = raa_init();
242 strs = saa_init(1L);
243 saa_wbytes(strs, "\0", 1L);
244 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
245 strslen = 2 + strlen(elf_module);
246 shstrtab = NULL;
247 shstrtablen = shstrtabsize = 0;;
248 add_sectname("", "");
250 fwds = NULL;
252 elf_gotpc_sect = seg_alloc();
253 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
254 elf_gotoff_sect = seg_alloc();
255 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
256 elf_got_sect = seg_alloc();
257 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
258 elf_plt_sect = seg_alloc();
259 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
260 elf_sym_sect = seg_alloc();
261 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
262 elf_tlsie_sect = seg_alloc();
263 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
265 def_seg = seg_alloc();
268 static void elf_cleanup(int debuginfo)
270 struct Reloc *r;
271 int i;
273 (void)debuginfo;
275 elf_write();
276 for (i = 0; i < nsects; i++) {
277 if (sects[i]->type != SHT_NOBITS)
278 saa_free(sects[i]->data);
279 if (sects[i]->head)
280 saa_free(sects[i]->rel);
281 while (sects[i]->head) {
282 r = sects[i]->head;
283 sects[i]->head = sects[i]->head->next;
284 nasm_free(r);
287 nasm_free(sects);
288 saa_free(syms);
289 raa_free(bsym);
290 saa_free(strs);
291 if (of_elf32.current_dfmt) {
292 of_elf32.current_dfmt->cleanup();
296 static void add_sectname(char *firsthalf, char *secondhalf)
298 int len = strlen(firsthalf) + strlen(secondhalf);
299 while (shstrtablen + len + 1 > shstrtabsize)
300 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
301 strcpy(shstrtab + shstrtablen, firsthalf);
302 strcat(shstrtab + shstrtablen, secondhalf);
303 shstrtablen += len + 1;
306 static int elf_make_section(char *name, int type, int flags, int align)
308 struct Section *s;
310 s = nasm_malloc(sizeof(*s));
312 if (type != SHT_NOBITS)
313 s->data = saa_init(1L);
314 s->head = NULL;
315 s->tail = &s->head;
316 s->len = s->size = 0;
317 s->nrelocs = 0;
318 if (!strcmp(name, ".text"))
319 s->index = def_seg;
320 else
321 s->index = seg_alloc();
322 add_sectname("", name);
323 s->name = nasm_malloc(1 + strlen(name));
324 strcpy(s->name, name);
325 s->type = type;
326 s->flags = flags;
327 s->align = align;
328 s->gsyms = NULL;
330 if (nsects >= sectlen)
331 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
332 sects[nsects++] = s;
334 return nsects - 1;
337 static int32_t elf_section_names(char *name, int pass, int *bits)
339 char *p;
340 uint32_t flags, flags_and, flags_or;
341 uint64_t align;
342 int type, i;
345 * Default is 32 bits.
347 if (!name) {
348 *bits = 32;
349 return def_seg;
352 p = nasm_skip_word(name);
353 if (*p)
354 *p++ = '\0';
355 flags_and = flags_or = type = align = 0;
357 section_attrib(name, p, pass, &flags_and,
358 &flags_or, &align, &type);
360 if (!strcmp(name, ".shstrtab") ||
361 !strcmp(name, ".symtab") ||
362 !strcmp(name, ".strtab")) {
363 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
364 "name `%s'", name);
365 return NO_SEG;
368 for (i = 0; i < nsects; i++)
369 if (!strcmp(name, sects[i]->name))
370 break;
371 if (i == nsects) {
372 const struct elf_known_section *ks = elf_known_sections;
374 while (ks->name) {
375 if (!strcmp(name, ks->name))
376 break;
377 ks++;
380 type = type ? type : ks->type;
381 align = align ? align : ks->align;
382 flags = (ks->flags & ~flags_and) | flags_or;
384 i = elf_make_section(name, type, flags, align);
385 } else if (pass == 1) {
386 if ((type && sects[i]->type != type)
387 || (align && sects[i]->align != align)
388 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
389 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
390 " redeclaration of section `%s'", name);
393 return sects[i]->index;
396 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
397 int is_global, char *special)
399 int pos = strslen;
400 struct Symbol *sym;
401 bool special_used = false;
403 #if defined(DEBUG) && DEBUG>2
404 nasm_error(ERR_DEBUG,
405 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
406 name, segment, offset, is_global, special);
407 #endif
408 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
410 * This is a NASM special symbol. We never allow it into
411 * the ELF symbol table, even if it's a valid one. If it
412 * _isn't_ a valid one, we should barf immediately.
414 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
415 strcmp(name, "..got") && strcmp(name, "..plt") &&
416 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
417 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
418 return;
421 if (is_global == 3) {
422 struct Symbol **s;
424 * Fix up a forward-reference symbol size from the first
425 * pass.
427 for (s = &fwds; *s; s = &(*s)->nextfwd)
428 if (!strcmp((*s)->name, name)) {
429 struct tokenval tokval;
430 expr *e;
431 char *p = nasm_skip_spaces(nasm_skip_word(special));
433 stdscan_reset();
434 stdscan_set(p);
435 tokval.t_type = TOKEN_INVALID;
436 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
437 if (e) {
438 if (!is_simple(e))
439 nasm_error(ERR_NONFATAL, "cannot use relocatable"
440 " expression as symbol size");
441 else
442 (*s)->size = reloc_value(e);
446 * Remove it from the list of unresolved sizes.
448 nasm_free((*s)->name);
449 *s = (*s)->nextfwd;
450 return;
452 return; /* it wasn't an important one */
455 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
456 strslen += 1 + strlen(name);
458 lastsym = sym = saa_wstruct(syms);
460 memset(&sym->symv, 0, sizeof(struct rbtree));
462 sym->strpos = pos;
463 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
464 sym->other = STV_DEFAULT;
465 sym->size = 0;
466 if (segment == NO_SEG)
467 sym->section = SHN_ABS;
468 else {
469 int i;
470 sym->section = SHN_UNDEF;
471 if (segment == def_seg) {
472 /* we have to be sure at least text section is there */
473 int tempint;
474 if (segment != elf_section_names(".text", 2, &tempint))
475 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
477 for (i = 0; i < nsects; i++) {
478 if (segment == sects[i]->index) {
479 sym->section = i + 1;
480 break;
485 if (is_global == 2) {
486 sym->size = offset;
487 sym->symv.key = 0;
488 sym->section = SHN_COMMON;
490 * We have a common variable. Check the special text to see
491 * if it's a valid number and power of two; if so, store it
492 * as the alignment for the common variable.
494 if (special) {
495 bool err;
496 sym->symv.key = readnum(special, &err);
497 if (err)
498 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
499 " valid number", special);
500 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
501 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
502 " power of two", special);
504 special_used = true;
505 } else
506 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
508 if (sym->type == SYM_GLOBAL) {
510 * If sym->section == SHN_ABS, then the first line of the
511 * else section would cause a core dump, because its a reference
512 * beyond the end of the section array.
513 * This behaviour is exhibited by this code:
514 * GLOBAL crash_nasm
515 * crash_nasm equ 0
516 * To avoid such a crash, such requests are silently discarded.
517 * This may not be the best solution.
519 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
520 bsym = raa_write(bsym, segment, nglobs);
521 } else if (sym->section != SHN_ABS) {
523 * This is a global symbol; so we must add it to the rbtree
524 * of global symbols in its section.
526 * In addition, we check the special text for symbol
527 * type and size information.
529 sects[sym->section-1]->gsyms =
530 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
532 if (special) {
533 int n = strcspn(special, " \t");
535 if (!nasm_strnicmp(special, "function", n))
536 sym->type |= STT_FUNC;
537 else if (!nasm_strnicmp(special, "data", n) ||
538 !nasm_strnicmp(special, "object", n))
539 sym->type |= STT_OBJECT;
540 else if (!nasm_strnicmp(special, "notype", n))
541 sym->type |= STT_NOTYPE;
542 else
543 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
544 n, special);
545 special += n;
547 special = nasm_skip_spaces(special);
548 if (*special) {
549 n = strcspn(special, " \t");
550 if (!nasm_strnicmp(special, "default", n))
551 sym->other = STV_DEFAULT;
552 else if (!nasm_strnicmp(special, "internal", n))
553 sym->other = STV_INTERNAL;
554 else if (!nasm_strnicmp(special, "hidden", n))
555 sym->other = STV_HIDDEN;
556 else if (!nasm_strnicmp(special, "protected", n))
557 sym->other = STV_PROTECTED;
558 else
559 n = 0;
560 special += n;
563 if (*special) {
564 struct tokenval tokval;
565 expr *e;
566 int fwd = 0;
567 char *saveme = stdscan_get();
569 while (special[n] && nasm_isspace(special[n]))
570 n++;
572 * We have a size expression; attempt to
573 * evaluate it.
575 stdscan_reset();
576 stdscan_set(special + n);
577 tokval.t_type = TOKEN_INVALID;
578 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
579 NULL);
580 if (fwd) {
581 sym->nextfwd = fwds;
582 fwds = sym;
583 sym->name = nasm_strdup(name);
584 } else if (e) {
585 if (!is_simple(e))
586 nasm_error(ERR_NONFATAL, "cannot use relocatable"
587 " expression as symbol size");
588 else
589 sym->size = reloc_value(e);
591 stdscan_set(saveme);
593 special_used = true;
596 * If TLS segment, mark symbol accordingly.
598 if (sects[sym->section - 1]->flags & SHF_TLS) {
599 sym->type &= 0xf0;
600 sym->type |= STT_TLS;
603 sym->globnum = nglobs;
604 nglobs++;
605 } else
606 nlocals++;
608 if (special && !special_used)
609 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
612 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
614 struct Reloc *r;
616 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
617 sect->tail = &r->next;
618 r->next = NULL;
620 r->address = sect->len;
621 if (segment == NO_SEG)
622 r->symbol = 0;
623 else {
624 int i;
625 r->symbol = 0;
626 for (i = 0; i < nsects; i++)
627 if (segment == sects[i]->index)
628 r->symbol = i + 2;
629 if (!r->symbol)
630 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
632 r->type = type;
634 sect->nrelocs++;
638 * This routine deals with ..got and ..sym relocations: the more
639 * complicated kinds. In shared-library writing, some relocations
640 * with respect to global symbols must refer to the precise symbol
641 * rather than referring to an offset from the base of the section
642 * _containing_ the symbol. Such relocations call to this routine,
643 * which searches the symbol list for the symbol in question.
645 * R_386_GOT32 references require the _exact_ symbol address to be
646 * used; R_386_32 references can be at an offset from the symbol.
647 * The boolean argument `exact' tells us this.
649 * Return value is the adjusted value of `addr', having become an
650 * offset from the symbol rather than the section. Should always be
651 * zero when returning from an exact call.
653 * Limitation: if you define two symbols at the same place,
654 * confusion will occur.
656 * Inefficiency: we search, currently, using a linked list which
657 * isn't even necessarily sorted.
659 static int32_t elf_add_gsym_reloc(struct Section *sect,
660 int32_t segment, uint32_t offset,
661 int type, bool exact)
663 struct Reloc *r;
664 struct Section *s;
665 struct Symbol *sym;
666 struct rbtree *srb;
667 int i;
670 * First look up the segment/offset pair and find a global
671 * symbol corresponding to it. If it's not one of our segments,
672 * then it must be an external symbol, in which case we're fine
673 * doing a normal elf_add_reloc after first sanity-checking
674 * that the offset from the symbol is zero.
676 s = NULL;
677 for (i = 0; i < nsects; i++)
678 if (segment == sects[i]->index) {
679 s = sects[i];
680 break;
682 if (!s) {
683 if (exact && offset != 0)
684 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
685 " for this reference");
686 else
687 elf_add_reloc(sect, segment, type);
688 return offset;
691 srb = rb_search(s->gsyms, offset);
692 if (!srb || (exact && srb->key != offset)) {
693 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
694 " for this reference");
695 return 0;
697 sym = container_of(srb, struct Symbol, symv);
699 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
700 sect->tail = &r->next;
701 r->next = NULL;
703 r->address = sect->len;
704 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
705 r->type = type;
707 sect->nrelocs++;
709 return offset - sym->symv.key;
712 static void elf_out(int32_t segto, const void *data,
713 enum out_type type, uint64_t size,
714 int32_t segment, int32_t wrt)
716 struct Section *s;
717 int32_t addr;
718 uint8_t mydata[8], *p;
719 int reltype, bytes;
720 int i;
721 static struct symlininfo sinfo;
724 * handle absolute-assembly (structure definitions)
726 if (segto == NO_SEG) {
727 if (type != OUT_RESERVE)
728 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
729 " space");
730 return;
733 s = NULL;
734 for (i = 0; i < nsects; i++)
735 if (segto == sects[i]->index) {
736 s = sects[i];
737 break;
739 if (!s) {
740 int tempint; /* ignored */
741 if (segto != elf_section_names(".text", 2, &tempint))
742 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
743 else {
744 s = sects[nsects - 1];
745 i = nsects - 1;
749 /* again some stabs debugging stuff */
750 if (of_elf32.current_dfmt) {
751 sinfo.offset = s->len;
752 sinfo.section = i;
753 sinfo.name = s->name;
754 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
756 /* end of debugging stuff */
758 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
759 nasm_error(ERR_WARNING, "attempt to initialize memory in"
760 " BSS section `%s': ignored", s->name);
761 s->len += realsize(type, size);
762 return;
765 switch (type) {
766 case OUT_RESERVE:
767 if (s->type == SHT_PROGBITS) {
768 nasm_error(ERR_WARNING, "uninitialized space declared in"
769 " non-BSS section `%s': zeroing", s->name);
770 elf_sect_write(s, NULL, size);
771 } else
772 s->len += size;
773 break;
775 case OUT_RAWDATA:
776 if (segment != NO_SEG)
777 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
778 elf_sect_write(s, data, size);
779 break;
781 case OUT_ADDRESS:
783 bool gnu16 = false;
784 addr = *(int64_t *)data;
785 if (segment != NO_SEG) {
786 if (segment % 2) {
787 nasm_error(ERR_NONFATAL, "ELF format does not support"
788 " segment base references");
789 } else {
790 if (wrt == NO_SEG) {
791 switch (size) {
792 case 1:
793 gnu16 = true;
794 elf_add_reloc(s, segment, R_386_8);
795 break;
796 case 2:
797 gnu16 = true;
798 elf_add_reloc(s, segment, R_386_16);
799 break;
800 case 4:
801 elf_add_reloc(s, segment, R_386_32);
802 break;
803 default: /* Error issued further down */
804 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 */