Makefile: clean up the lib directory on make clean
[nasm.git] / output / outelf32.c
blob4d238a7238e9c1c8a9eace36cb5ed9b6513cfff5
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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[4], *p;
719 int i;
720 static struct symlininfo sinfo;
723 * handle absolute-assembly (structure definitions)
725 if (segto == NO_SEG) {
726 if (type != OUT_RESERVE)
727 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
728 " space");
729 return;
732 s = NULL;
733 for (i = 0; i < nsects; i++)
734 if (segto == sects[i]->index) {
735 s = sects[i];
736 break;
738 if (!s) {
739 int tempint; /* ignored */
740 if (segto != elf_section_names(".text", 2, &tempint))
741 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
742 else {
743 s = sects[nsects - 1];
744 i = nsects - 1;
748 /* again some stabs debugging stuff */
749 if (of_elf32.current_dfmt) {
750 sinfo.offset = s->len;
751 sinfo.section = i;
752 sinfo.name = s->name;
753 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
755 /* end of debugging stuff */
757 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
758 nasm_error(ERR_WARNING, "attempt to initialize memory in"
759 " BSS section `%s': ignored", s->name);
760 s->len += realsize(type, size);
761 return;
764 if (type == OUT_RESERVE) {
765 if (s->type == SHT_PROGBITS) {
766 nasm_error(ERR_WARNING, "uninitialized space declared in"
767 " non-BSS section `%s': zeroing", s->name);
768 elf_sect_write(s, NULL, size);
769 } else
770 s->len += size;
771 } else if (type == OUT_RAWDATA) {
772 if (segment != NO_SEG)
773 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
774 elf_sect_write(s, data, size);
775 } else if (type == 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) {
784 if (size == 2) {
785 gnu16 = true;
786 elf_add_reloc(s, segment, R_386_16);
787 } else {
788 elf_add_reloc(s, segment, R_386_32);
790 } else if (wrt == elf_gotpc_sect + 1) {
792 * The user will supply GOT relative to $$. ELF
793 * will let us have GOT relative to $. So we
794 * need to fix up the data item by $-$$.
796 addr += s->len;
797 elf_add_reloc(s, segment, R_386_GOTPC);
798 } else if (wrt == elf_gotoff_sect + 1) {
799 elf_add_reloc(s, segment, R_386_GOTOFF);
800 } else if (wrt == elf_tlsie_sect + 1) {
801 addr = elf_add_gsym_reloc(s, segment, addr,
802 R_386_TLS_IE, true);
803 } else if (wrt == elf_got_sect + 1) {
804 addr = elf_add_gsym_reloc(s, segment, addr,
805 R_386_GOT32, true);
806 } else if (wrt == elf_sym_sect + 1) {
807 if (size == 2) {
808 gnu16 = true;
809 addr = elf_add_gsym_reloc(s, segment, addr,
810 R_386_16, false);
811 } else {
812 addr = elf_add_gsym_reloc(s, segment, addr,
813 R_386_32, false);
815 } else if (wrt == elf_plt_sect + 1) {
816 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
817 "relative PLT references");
818 } else {
819 nasm_error(ERR_NONFATAL, "ELF format does not support this"
820 " use of WRT");
821 wrt = NO_SEG; /* we can at least _try_ to continue */
825 p = mydata;
826 if (gnu16) {
827 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
828 "16-bit relocations in ELF is a GNU extension");
829 WRITESHORT(p, addr);
830 } else {
831 if (size != 4 && segment != NO_SEG) {
832 nasm_error(ERR_NONFATAL,
833 "Unsupported non-32-bit ELF relocation");
835 WRITELONG(p, addr);
837 elf_sect_write(s, mydata, size);
838 } else if (type == OUT_REL2ADR) {
839 if (segment == segto)
840 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
841 if (segment != NO_SEG && segment % 2) {
842 nasm_error(ERR_NONFATAL, "ELF format does not support"
843 " segment base references");
844 } else {
845 if (wrt == NO_SEG) {
846 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
847 "16-bit relocations in ELF is a GNU extension");
848 elf_add_reloc(s, segment, R_386_PC16);
849 } else {
850 nasm_error(ERR_NONFATAL,
851 "Unsupported non-32-bit ELF relocation");
854 p = mydata;
855 WRITESHORT(p, *(int64_t *)data - size);
856 elf_sect_write(s, mydata, 2L);
857 } else if (type == OUT_REL4ADR) {
858 if (segment == segto)
859 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
860 if (segment != NO_SEG && segment % 2) {
861 nasm_error(ERR_NONFATAL, "ELF format does not support"
862 " segment base references");
863 } else {
864 if (wrt == NO_SEG) {
865 elf_add_reloc(s, segment, R_386_PC32);
866 } else if (wrt == elf_plt_sect + 1) {
867 elf_add_reloc(s, segment, R_386_PLT32);
868 } else if (wrt == elf_gotpc_sect + 1 ||
869 wrt == elf_gotoff_sect + 1 ||
870 wrt == elf_got_sect + 1) {
871 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
872 "relative GOT references");
873 } else {
874 nasm_error(ERR_NONFATAL, "ELF format does not support this"
875 " use of WRT");
876 wrt = NO_SEG; /* we can at least _try_ to continue */
879 p = mydata;
880 WRITELONG(p, *(int64_t *)data - size);
881 elf_sect_write(s, mydata, 4L);
885 static void elf_write(void)
887 int align;
888 char *p;
889 int i;
891 struct SAA *symtab;
892 int32_t symtablen, symtablocal;
895 * Work out how many sections we will have. We have SHN_UNDEF,
896 * then the flexible user sections, then the fixed sections
897 * `.shstrtab', `.symtab' and `.strtab', then optionally
898 * relocation sections for the user sections.
900 nsections = sec_numspecial + 1;
901 if (of_elf32.current_dfmt == &df_stabs)
902 nsections += 3;
903 else if (of_elf32.current_dfmt == &df_dwarf)
904 nsections += 10;
906 add_sectname("", ".shstrtab");
907 add_sectname("", ".symtab");
908 add_sectname("", ".strtab");
909 for (i = 0; i < nsects; i++) {
910 nsections++; /* for the section itself */
911 if (sects[i]->head) {
912 nsections++; /* for its relocations */
913 add_sectname(".rel", sects[i]->name);
917 if (of_elf32.current_dfmt == &df_stabs) {
918 /* in case the debug information is wanted, just add these three sections... */
919 add_sectname("", ".stab");
920 add_sectname("", ".stabstr");
921 add_sectname(".rel", ".stab");
922 } else if (of_elf32.current_dfmt == &df_dwarf) {
923 /* the dwarf debug standard specifies the following ten sections,
924 not all of which are currently implemented,
925 although all of them are defined. */
926 add_sectname("", ".debug_aranges");
927 add_sectname(".rela", ".debug_aranges");
928 add_sectname("", ".debug_pubnames");
929 add_sectname("", ".debug_info");
930 add_sectname(".rela", ".debug_info");
931 add_sectname("", ".debug_abbrev");
932 add_sectname("", ".debug_line");
933 add_sectname(".rela", ".debug_line");
934 add_sectname("", ".debug_frame");
935 add_sectname("", ".debug_loc");
939 * Output the ELF header.
941 fwrite("\177ELF\1\1\1", 7, 1, ofile);
942 fputc(elf_osabi, ofile);
943 fputc(elf_abiver, ofile);
944 fwritezero(7, ofile);
945 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
946 fwriteint16_t(3, ofile); /* EM_386 processor ID */
947 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
948 fwriteint32_t(0L, ofile); /* no entry point */
949 fwriteint32_t(0L, ofile); /* no program header table */
950 fwriteint32_t(0x40L, ofile); /* section headers straight after
951 * ELF header plus alignment */
952 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
953 fwriteint16_t(0x34, ofile); /* size of ELF header */
954 fwriteint16_t(0, ofile); /* no program header table, again */
955 fwriteint16_t(0, ofile); /* still no program header table */
956 fwriteint16_t(0x28, ofile); /* size of section header */
957 fwriteint16_t(nsections, ofile); /* number of sections */
958 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
959 * section header table */
960 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
961 fwriteint32_t(0L, ofile);
962 fwriteint32_t(0L, ofile);
965 * Build the symbol table and relocation tables.
967 symtab = elf_build_symtab(&symtablen, &symtablocal);
968 for (i = 0; i < nsects; i++)
969 if (sects[i]->head)
970 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
971 sects[i]->head);
974 * Now output the section header table.
977 elf_foffs = 0x40 + 0x28 * nsections;
978 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
979 elf_foffs += align;
980 elf_nsect = 0;
981 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
983 /* SHN_UNDEF */
984 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
985 p = shstrtab + 1;
987 /* The normal sections */
988 for (i = 0; i < nsects; i++) {
989 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
990 (sects[i]->type == SHT_PROGBITS ?
991 sects[i]->data : NULL), true,
992 sects[i]->len, 0, 0, sects[i]->align, 0);
993 p += strlen(p) + 1;
996 /* .shstrtab */
997 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
998 shstrtablen, 0, 0, 1, 0);
999 p += strlen(p) + 1;
1001 /* .symtab */
1002 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1003 symtablen, sec_strtab, symtablocal, 4, 16);
1004 p += strlen(p) + 1;
1006 /* .strtab */
1007 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1008 strslen, 0, 0, 1, 0);
1009 p += strlen(p) + 1;
1011 /* The relocation sections */
1012 for (i = 0; i < nsects; i++)
1013 if (sects[i]->head) {
1014 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1015 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1016 p += strlen(p) + 1;
1019 if (of_elf32.current_dfmt == &df_stabs) {
1020 /* for debugging information, create the last three sections
1021 which are the .stab , .stabstr and .rel.stab sections respectively */
1023 /* this function call creates the stab sections in memory */
1024 stabs32_generate();
1026 if (stabbuf && stabstrbuf && stabrelbuf) {
1027 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1028 stablen, sec_stabstr, 0, 4, 12);
1029 p += strlen(p) + 1;
1031 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1032 stabstrlen, 0, 0, 4, 0);
1033 p += strlen(p) + 1;
1035 /* link -> symtable info -> section to refer to */
1036 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1037 stabrellen, sec_symtab, sec_stab, 4, 8);
1038 p += strlen(p) + 1;
1040 } else if (of_elf32.current_dfmt == &df_dwarf) {
1041 /* for dwarf debugging information, create the ten dwarf sections */
1043 /* this function call creates the dwarf sections in memory */
1044 if (dwarf_fsect)
1045 dwarf32_generate();
1047 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1048 arangeslen, 0, 0, 1, 0);
1049 p += strlen(p) + 1;
1051 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1052 arangesrellen, sec_symtab, sec_debug_aranges,
1053 1, 12);
1054 p += strlen(p) + 1;
1056 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1057 false, pubnameslen, 0, 0, 1, 0);
1058 p += strlen(p) + 1;
1060 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1061 infolen, 0, 0, 1, 0);
1062 p += strlen(p) + 1;
1064 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1065 inforellen, sec_symtab, sec_debug_info, 1, 12);
1066 p += strlen(p) + 1;
1068 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1069 abbrevlen, 0, 0, 1, 0);
1070 p += strlen(p) + 1;
1072 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1073 linelen, 0, 0, 1, 0);
1074 p += strlen(p) + 1;
1076 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1077 linerellen, sec_symtab, sec_debug_line, 1, 12);
1078 p += strlen(p) + 1;
1080 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1081 framelen, 0, 0, 8, 0);
1082 p += strlen(p) + 1;
1084 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1085 loclen, 0, 0, 1, 0);
1086 p += strlen(p) + 1;
1088 fwritezero(align, ofile);
1091 * Now output the sections.
1093 elf_write_sections();
1095 nasm_free(elf_sects);
1096 saa_free(symtab);
1099 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1101 struct SAA *s = saa_init(1L);
1102 struct Symbol *sym;
1103 uint8_t entry[16], *p;
1104 int i;
1106 *len = *local = 0;
1109 * First, an all-zeros entry, required by the ELF spec.
1111 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1112 *len += 16;
1113 (*local)++;
1116 * Next, an entry for the file name.
1118 p = entry;
1119 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1120 WRITELONG(p, 0); /* no value */
1121 WRITELONG(p, 0); /* no size either */
1122 WRITESHORT(p, STT_FILE); /* type FILE */
1123 WRITESHORT(p, SHN_ABS);
1124 saa_wbytes(s, entry, 16L);
1125 *len += 16;
1126 (*local)++;
1129 * Now some standard symbols defining the segments, for relocation
1130 * purposes.
1132 for (i = 1; i <= nsects; i++) {
1133 p = entry;
1134 WRITELONG(p, 0); /* no symbol name */
1135 WRITELONG(p, 0); /* offset zero */
1136 WRITELONG(p, 0); /* size zero */
1137 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1138 WRITESHORT(p, i); /* section id */
1139 saa_wbytes(s, entry, 16L);
1140 *len += 16;
1141 (*local)++;
1145 * Now the other local symbols.
1147 saa_rewind(syms);
1148 while ((sym = saa_rstruct(syms))) {
1149 if (sym->type & SYM_GLOBAL)
1150 continue;
1151 p = entry;
1152 WRITELONG(p, sym->strpos);
1153 WRITELONG(p, sym->symv.key);
1154 WRITELONG(p, sym->size);
1155 WRITECHAR(p, sym->type); /* type and binding */
1156 WRITECHAR(p, sym->other); /* visibility */
1157 WRITESHORT(p, sym->section);
1158 saa_wbytes(s, entry, 16L);
1159 *len += 16;
1160 (*local)++;
1163 * dwarf needs symbols for debug sections
1164 * which are relocation targets.
1166 //*** fix for 32 bit
1167 if (of_elf32.current_dfmt == &df_dwarf) {
1168 dwarf_infosym = *local;
1169 p = entry;
1170 WRITELONG(p, 0); /* no symbol name */
1171 WRITELONG(p, (uint32_t) 0); /* offset zero */
1172 WRITELONG(p, (uint32_t) 0); /* size zero */
1173 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1174 WRITESHORT(p, sec_debug_info); /* section id */
1175 saa_wbytes(s, entry, 16L);
1176 *len += 16;
1177 (*local)++;
1178 dwarf_abbrevsym = *local;
1179 p = entry;
1180 WRITELONG(p, 0); /* no symbol name */
1181 WRITELONG(p, (uint32_t) 0); /* offset zero */
1182 WRITELONG(p, (uint32_t) 0); /* size zero */
1183 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1184 WRITESHORT(p, sec_debug_abbrev); /* section id */
1185 saa_wbytes(s, entry, 16L);
1186 *len += 16;
1187 (*local)++;
1188 dwarf_linesym = *local;
1189 p = entry;
1190 WRITELONG(p, 0); /* no symbol name */
1191 WRITELONG(p, (uint32_t) 0); /* offset zero */
1192 WRITELONG(p, (uint32_t) 0); /* size zero */
1193 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1194 WRITESHORT(p, sec_debug_line); /* section id */
1195 saa_wbytes(s, entry, 16L);
1196 *len += 16;
1197 (*local)++;
1201 * Now the global symbols.
1203 saa_rewind(syms);
1204 while ((sym = saa_rstruct(syms))) {
1205 if (!(sym->type & SYM_GLOBAL))
1206 continue;
1207 p = entry;
1208 WRITELONG(p, sym->strpos);
1209 WRITELONG(p, sym->symv.key);
1210 WRITELONG(p, sym->size);
1211 WRITECHAR(p, sym->type); /* type and binding */
1212 WRITECHAR(p, sym->other); /* visibility */
1213 WRITESHORT(p, sym->section);
1214 saa_wbytes(s, entry, 16L);
1215 *len += 16;
1218 return s;
1221 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1223 struct SAA *s;
1224 uint8_t *p, entry[8];
1225 int32_t global_offset;
1227 if (!r)
1228 return NULL;
1230 s = saa_init(1L);
1231 *len = 0;
1234 * How to onvert from a global placeholder to a real symbol index;
1235 * the +2 refers to the two special entries, the null entry and
1236 * the filename entry.
1238 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1240 while (r) {
1241 int32_t sym = r->symbol;
1244 * Create a real symbol index; the +2 refers to the two special
1245 * entries, the null entry and the filename entry.
1247 if (sym >= GLOBAL_TEMP_BASE)
1248 sym += global_offset;
1250 p = entry;
1251 WRITELONG(p, r->address);
1252 WRITELONG(p, (sym << 8) + r->type);
1253 saa_wbytes(s, entry, 8L);
1254 *len += 8;
1256 r = r->next;
1259 return s;
1262 static void elf_section_header(int name, int type, int flags,
1263 void *data, bool is_saa, int32_t datalen,
1264 int link, int info, int align, int eltsize)
1266 elf_sects[elf_nsect].data = data;
1267 elf_sects[elf_nsect].len = datalen;
1268 elf_sects[elf_nsect].is_saa = is_saa;
1269 elf_nsect++;
1271 fwriteint32_t((int32_t)name, ofile);
1272 fwriteint32_t((int32_t)type, ofile);
1273 fwriteint32_t((int32_t)flags, ofile);
1274 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1275 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1276 fwriteint32_t(datalen, ofile);
1277 if (data)
1278 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1279 fwriteint32_t((int32_t)link, ofile);
1280 fwriteint32_t((int32_t)info, ofile);
1281 fwriteint32_t((int32_t)align, ofile);
1282 fwriteint32_t((int32_t)eltsize, ofile);
1285 static void elf_write_sections(void)
1287 int i;
1288 for (i = 0; i < elf_nsect; i++)
1289 if (elf_sects[i].data) {
1290 int32_t len = elf_sects[i].len;
1291 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1292 int32_t align = reallen - len;
1293 if (elf_sects[i].is_saa)
1294 saa_fpwrite(elf_sects[i].data, ofile);
1295 else
1296 fwrite(elf_sects[i].data, len, 1, ofile);
1297 fwritezero(align, ofile);
1301 static void elf_sect_write(struct Section *sect,
1302 const uint8_t *data, uint32_t len)
1304 saa_wbytes(sect->data, data, len);
1305 sect->len += len;
1308 static void elf_sectalign(int32_t seg, unsigned int value)
1310 struct Section *s = NULL;
1311 int i;
1313 for (i = 0; i < nsects; i++) {
1314 if (sects[i]->index == seg) {
1315 s = sects[i];
1316 break;
1319 if (!s || !is_power2(value))
1320 return;
1322 if (value > s->align)
1323 s->align = value;
1326 static int32_t elf_segbase(int32_t segment)
1328 return segment;
1331 static int elf_directive(enum directives directive, char *value, int pass)
1333 bool err;
1334 int64_t n;
1335 char *p;
1337 switch (directive) {
1338 case D_OSABI:
1339 if (pass == 2)
1340 return 1; /* ignore in pass 2 */
1342 n = readnum(value, &err);
1343 if (err) {
1344 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1345 return 1;
1347 if (n < 0 || n > 255) {
1348 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1349 return 1;
1351 elf_osabi = n;
1352 elf_abiver = 0;
1354 if ((p = strchr(value,',')) == NULL)
1355 return 1;
1357 n = readnum(p+1, &err);
1358 if (err || n < 0 || n > 255) {
1359 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1360 return 1;
1363 elf_abiver = n;
1364 return 1;
1366 default:
1367 return 0;
1371 static void elf_filename(char *inname, char *outname)
1373 strcpy(elf_module, inname);
1374 standard_extension(inname, outname, ".o");
1377 extern macros_t elf_stdmac[];
1379 static int elf_set_info(enum geninfo type, char **val)
1381 (void)type;
1382 (void)val;
1383 return 0;
1385 static struct dfmt df_dwarf = {
1386 "ELF32 (i386) dwarf debug format for Linux/Unix",
1387 "dwarf",
1388 dwarf32_init,
1389 dwarf32_linenum,
1390 debug32_deflabel,
1391 debug32_directive,
1392 debug32_typevalue,
1393 dwarf32_output,
1394 dwarf32_cleanup
1396 static struct dfmt df_stabs = {
1397 "ELF32 (i386) stabs debug format for Linux/Unix",
1398 "stabs",
1399 null_debug_init,
1400 stabs32_linenum,
1401 debug32_deflabel,
1402 debug32_directive,
1403 debug32_typevalue,
1404 stabs32_output,
1405 stabs32_cleanup
1408 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1410 struct ofmt of_elf32 = {
1411 "ELF32 (i386) object files (e.g. Linux)",
1412 "elf32",
1414 elf32_debugs_arr,
1415 &df_stabs,
1416 elf_stdmac,
1417 elf_init,
1418 elf_set_info,
1419 elf_out,
1420 elf_deflabel,
1421 elf_section_names,
1422 elf_sectalign,
1423 elf_segbase,
1424 elf_directive,
1425 elf_filename,
1426 elf_cleanup
1429 /* again, the stabs debugging stuff (code) */
1431 static void stabs32_linenum(const char *filename, int32_t linenumber,
1432 int32_t segto)
1434 (void)segto;
1436 if (!stabs_filename) {
1437 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1438 strcpy(stabs_filename, filename);
1439 } else {
1440 if (strcmp(stabs_filename, filename)) {
1442 * yep, a memory leak...this program is one-shot anyway, so who cares...
1443 * in fact, this leak comes in quite handy to maintain a list of files
1444 * encountered so far in the symbol lines...
1447 /* why not nasm_free(stabs_filename); we're done with the old one */
1449 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1450 strcpy(stabs_filename, filename);
1453 debug_immcall = 1;
1454 currentline = linenumber;
1457 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1458 char *special)
1460 (void)name;
1461 (void)segment;
1462 (void)offset;
1463 (void)is_global;
1464 (void)special;
1467 static void debug32_directive(const char *directive, const char *params)
1469 (void)directive;
1470 (void)params;
1473 static void debug32_typevalue(int32_t type)
1475 int32_t stype, ssize;
1476 switch (TYM_TYPE(type)) {
1477 case TY_LABEL:
1478 ssize = 0;
1479 stype = STT_NOTYPE;
1480 break;
1481 case TY_BYTE:
1482 ssize = 1;
1483 stype = STT_OBJECT;
1484 break;
1485 case TY_WORD:
1486 ssize = 2;
1487 stype = STT_OBJECT;
1488 break;
1489 case TY_DWORD:
1490 ssize = 4;
1491 stype = STT_OBJECT;
1492 break;
1493 case TY_FLOAT:
1494 ssize = 4;
1495 stype = STT_OBJECT;
1496 break;
1497 case TY_QWORD:
1498 ssize = 8;
1499 stype = STT_OBJECT;
1500 break;
1501 case TY_TBYTE:
1502 ssize = 10;
1503 stype = STT_OBJECT;
1504 break;
1505 case TY_OWORD:
1506 ssize = 16;
1507 stype = STT_OBJECT;
1508 break;
1509 case TY_YWORD:
1510 ssize = 32;
1511 stype = STT_OBJECT;
1512 break;
1513 case TY_COMMON:
1514 ssize = 0;
1515 stype = STT_COMMON;
1516 break;
1517 case TY_SEG:
1518 ssize = 0;
1519 stype = STT_SECTION;
1520 break;
1521 case TY_EXTERN:
1522 ssize = 0;
1523 stype = STT_NOTYPE;
1524 break;
1525 case TY_EQU:
1526 ssize = 0;
1527 stype = STT_NOTYPE;
1528 break;
1529 default:
1530 ssize = 0;
1531 stype = STT_NOTYPE;
1532 break;
1534 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1535 lastsym->size = ssize;
1536 lastsym->type = stype;
1540 static void stabs32_output(int type, void *param)
1542 struct symlininfo *s;
1543 struct linelist *el;
1544 if (type == TY_STABSSYMLIN) {
1545 if (debug_immcall) {
1546 s = (struct symlininfo *)param;
1547 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1548 return; /* line info is only collected for executable sections */
1549 numlinestabs++;
1550 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1551 el->info.offset = s->offset;
1552 el->info.section = s->section;
1553 el->info.name = s->name;
1554 el->line = currentline;
1555 el->filename = stabs_filename;
1556 el->next = 0;
1557 if (stabslines) {
1558 stabslines->last->next = el;
1559 stabslines->last = el;
1560 } else {
1561 stabslines = el;
1562 stabslines->last = el;
1566 debug_immcall = 0;
1569 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1571 static void stabs32_generate(void)
1573 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1574 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1575 char **allfiles;
1576 int *fileidx;
1578 struct linelist *ptr;
1580 ptr = stabslines;
1582 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1583 for (i = 0; i < numlinestabs; i++)
1584 allfiles[i] = 0;
1585 numfiles = 0;
1586 while (ptr) {
1587 if (numfiles == 0) {
1588 allfiles[0] = ptr->filename;
1589 numfiles++;
1590 } else {
1591 for (i = 0; i < numfiles; i++) {
1592 if (!strcmp(allfiles[i], ptr->filename))
1593 break;
1595 if (i >= numfiles) {
1596 allfiles[i] = ptr->filename;
1597 numfiles++;
1600 ptr = ptr->next;
1602 strsize = 1;
1603 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1604 for (i = 0; i < numfiles; i++) {
1605 fileidx[i] = strsize;
1606 strsize += strlen(allfiles[i]) + 1;
1608 mainfileindex = 0;
1609 for (i = 0; i < numfiles; i++) {
1610 if (!strcmp(allfiles[i], elf_module)) {
1611 mainfileindex = i;
1612 break;
1617 * worst case size of the stab buffer would be:
1618 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1619 * plus one "ending" entry
1621 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1622 sizeof(struct stabentry));
1623 ssbuf = (uint8_t *)nasm_malloc(strsize);
1624 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1625 rptr = rbuf;
1627 for (i = 0; i < numfiles; i++)
1628 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1629 ssbuf[0] = 0;
1631 stabstrlen = strsize; /* set global variable for length of stab strings */
1633 sptr = sbuf;
1634 ptr = stabslines;
1635 numstabs = 0;
1637 if (ptr) {
1639 * this is the first stab, its strx points to the filename of the
1640 * the source-file, the n_desc field should be set to the number
1641 * of remaining stabs
1643 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1645 /* this is the stab for the main source file */
1646 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1648 /* relocation table entry */
1651 * Since the symbol table has two entries before
1652 * the section symbols, the index in the info.section
1653 * member must be adjusted by adding 2
1656 WRITELONG(rptr, (sptr - sbuf) - 4);
1657 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1659 numstabs++;
1660 currfile = mainfileindex;
1663 while (ptr) {
1664 if (strcmp(allfiles[currfile], ptr->filename)) {
1665 /* oops file has changed... */
1666 for (i = 0; i < numfiles; i++)
1667 if (!strcmp(allfiles[i], ptr->filename))
1668 break;
1669 currfile = i;
1670 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1671 ptr->info.offset);
1672 numstabs++;
1674 /* relocation table entry */
1675 WRITELONG(rptr, (sptr - sbuf) - 4);
1676 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1679 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1680 numstabs++;
1682 /* relocation table entry */
1684 WRITELONG(rptr, (sptr - sbuf) - 4);
1685 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1687 ptr = ptr->next;
1691 /* this is an "ending" token */
1692 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1693 numstabs++;
1695 ((struct stabentry *)sbuf)->n_desc = numstabs;
1697 nasm_free(allfiles);
1698 nasm_free(fileidx);
1700 stablen = (sptr - sbuf);
1701 stabrellen = (rptr - rbuf);
1702 stabrelbuf = rbuf;
1703 stabbuf = sbuf;
1704 stabstrbuf = ssbuf;
1707 static void stabs32_cleanup(void)
1709 struct linelist *ptr, *del;
1710 if (!stabslines)
1711 return;
1713 ptr = stabslines;
1714 while (ptr) {
1715 del = ptr;
1716 ptr = ptr->next;
1717 nasm_free(del);
1720 nasm_free(stabbuf);
1721 nasm_free(stabrelbuf);
1722 nasm_free(stabstrbuf);
1725 /* dwarf routines */
1727 static void dwarf32_init(void)
1729 ndebugs = 3; /* 3 debug symbols */
1732 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1733 int32_t segto)
1735 (void)segto;
1736 dwarf32_findfile(filename);
1737 debug_immcall = 1;
1738 currentline = linenumber;
1741 /* called from elf_out with type == TY_DEBUGSYMLIN */
1742 static void dwarf32_output(int type, void *param)
1744 int ln, aa, inx, maxln, soc;
1745 struct symlininfo *s;
1746 struct SAA *plinep;
1748 (void)type;
1750 s = (struct symlininfo *)param;
1752 /* line number info is only gathered for executable sections */
1753 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1754 return;
1756 /* Check if section index has changed */
1757 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1758 dwarf32_findsect(s->section);
1760 /* do nothing unless line or file has changed */
1761 if (!debug_immcall)
1762 return;
1764 ln = currentline - dwarf_csect->line;
1765 aa = s->offset - dwarf_csect->offset;
1766 inx = dwarf_clist->line;
1767 plinep = dwarf_csect->psaa;
1768 /* check for file change */
1769 if (!(inx == dwarf_csect->file)) {
1770 saa_write8(plinep,DW_LNS_set_file);
1771 saa_write8(plinep,inx);
1772 dwarf_csect->file = inx;
1774 /* check for line change */
1775 if (ln) {
1776 /* test if in range of special op code */
1777 maxln = line_base + line_range;
1778 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1779 if (ln >= line_base && ln < maxln && soc < 256) {
1780 saa_write8(plinep,soc);
1781 } else {
1782 saa_write8(plinep,DW_LNS_advance_line);
1783 saa_wleb128s(plinep,ln);
1784 if (aa) {
1785 saa_write8(plinep,DW_LNS_advance_pc);
1786 saa_wleb128u(plinep,aa);
1789 dwarf_csect->line = currentline;
1790 dwarf_csect->offset = s->offset;
1793 /* show change handled */
1794 debug_immcall = 0;
1798 static void dwarf32_generate(void)
1800 uint8_t *pbuf;
1801 int indx;
1802 struct linelist *ftentry;
1803 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1804 struct SAA *parangesrel, *plinesrel, *pinforel;
1805 struct sectlist *psect;
1806 size_t saalen, linepoff, totlen, highaddr;
1808 /* write epilogues for each line program range */
1809 /* and build aranges section */
1810 paranges = saa_init(1L);
1811 parangesrel = saa_init(1L);
1812 saa_write16(paranges,2); /* dwarf version */
1813 saa_write32(parangesrel, paranges->datalen+4);
1814 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1815 saa_write32(parangesrel, 0);
1816 saa_write32(paranges,0); /* offset into info */
1817 saa_write8(paranges,4); /* pointer size */
1818 saa_write8(paranges,0); /* not segmented */
1819 saa_write32(paranges,0); /* padding */
1820 /* iterate though sectlist entries */
1821 psect = dwarf_fsect;
1822 totlen = 0;
1823 highaddr = 0;
1824 for (indx = 0; indx < dwarf_nsections; indx++) {
1825 plinep = psect->psaa;
1826 /* Line Number Program Epilogue */
1827 saa_write8(plinep,2); /* std op 2 */
1828 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1829 saa_write8(plinep,DW_LNS_extended_op);
1830 saa_write8(plinep,1); /* operand length */
1831 saa_write8(plinep,DW_LNE_end_sequence);
1832 totlen += plinep->datalen;
1833 /* range table relocation entry */
1834 saa_write32(parangesrel, paranges->datalen + 4);
1835 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1836 saa_write32(parangesrel, (uint32_t) 0);
1837 /* range table entry */
1838 saa_write32(paranges,0x0000); /* range start */
1839 saa_write32(paranges,sects[psect->section]->len); /* range length */
1840 highaddr += sects[psect->section]->len;
1841 /* done with this entry */
1842 psect = psect->next;
1844 saa_write32(paranges,0); /* null address */
1845 saa_write32(paranges,0); /* null length */
1846 saalen = paranges->datalen;
1847 arangeslen = saalen + 4;
1848 arangesbuf = pbuf = nasm_malloc(arangeslen);
1849 WRITELONG(pbuf,saalen); /* initial length */
1850 saa_rnbytes(paranges, pbuf, saalen);
1851 saa_free(paranges);
1853 /* build rela.aranges section */
1854 arangesrellen = saalen = parangesrel->datalen;
1855 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1856 saa_rnbytes(parangesrel, pbuf, saalen);
1857 saa_free(parangesrel);
1859 /* build pubnames section */
1860 ppubnames = saa_init(1L);
1861 saa_write16(ppubnames,3); /* dwarf version */
1862 saa_write32(ppubnames,0); /* offset into info */
1863 saa_write32(ppubnames,0); /* space used in info */
1864 saa_write32(ppubnames,0); /* end of list */
1865 saalen = ppubnames->datalen;
1866 pubnameslen = saalen + 4;
1867 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1868 WRITELONG(pbuf,saalen); /* initial length */
1869 saa_rnbytes(ppubnames, pbuf, saalen);
1870 saa_free(ppubnames);
1872 /* build info section */
1873 pinfo = saa_init(1L);
1874 pinforel = saa_init(1L);
1875 saa_write16(pinfo,2); /* dwarf version */
1876 saa_write32(pinforel, pinfo->datalen + 4);
1877 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1878 saa_write32(pinforel, 0);
1879 saa_write32(pinfo,0); /* offset into abbrev */
1880 saa_write8(pinfo,4); /* pointer size */
1881 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1882 saa_write32(pinforel, pinfo->datalen + 4);
1883 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1884 saa_write32(pinforel, 0);
1885 saa_write32(pinfo,0); /* DW_AT_low_pc */
1886 saa_write32(pinforel, pinfo->datalen + 4);
1887 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1888 saa_write32(pinforel, 0);
1889 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1890 saa_write32(pinforel, pinfo->datalen + 4);
1891 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1892 saa_write32(pinforel, 0);
1893 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1894 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1895 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1896 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1897 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1898 saa_write32(pinforel, pinfo->datalen + 4);
1899 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1900 saa_write32(pinforel, 0);
1901 saa_write32(pinfo,0); /* DW_AT_low_pc */
1902 saa_write32(pinfo,0); /* DW_AT_frame_base */
1903 saa_write8(pinfo,0); /* end of entries */
1904 saalen = pinfo->datalen;
1905 infolen = saalen + 4;
1906 infobuf = pbuf = nasm_malloc(infolen);
1907 WRITELONG(pbuf,saalen); /* initial length */
1908 saa_rnbytes(pinfo, pbuf, saalen);
1909 saa_free(pinfo);
1911 /* build rela.info section */
1912 inforellen = saalen = pinforel->datalen;
1913 inforelbuf = pbuf = nasm_malloc(inforellen);
1914 saa_rnbytes(pinforel, pbuf, saalen);
1915 saa_free(pinforel);
1917 /* build abbrev section */
1918 pabbrev = saa_init(1L);
1919 saa_write8(pabbrev,1); /* entry number LEB128u */
1920 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1921 saa_write8(pabbrev,1); /* has children */
1922 /* the following attributes and forms are all LEB128u values */
1923 saa_write8(pabbrev,DW_AT_low_pc);
1924 saa_write8(pabbrev,DW_FORM_addr);
1925 saa_write8(pabbrev,DW_AT_high_pc);
1926 saa_write8(pabbrev,DW_FORM_addr);
1927 saa_write8(pabbrev,DW_AT_stmt_list);
1928 saa_write8(pabbrev,DW_FORM_data4);
1929 saa_write8(pabbrev,DW_AT_name);
1930 saa_write8(pabbrev,DW_FORM_string);
1931 saa_write8(pabbrev,DW_AT_producer);
1932 saa_write8(pabbrev,DW_FORM_string);
1933 saa_write8(pabbrev,DW_AT_language);
1934 saa_write8(pabbrev,DW_FORM_data2);
1935 saa_write16(pabbrev,0); /* end of entry */
1936 /* LEB128u usage same as above */
1937 saa_write8(pabbrev,2); /* entry number */
1938 saa_write8(pabbrev,DW_TAG_subprogram);
1939 saa_write8(pabbrev,0); /* no children */
1940 saa_write8(pabbrev,DW_AT_low_pc);
1941 saa_write8(pabbrev,DW_FORM_addr);
1942 saa_write8(pabbrev,DW_AT_frame_base);
1943 saa_write8(pabbrev,DW_FORM_data4);
1944 saa_write16(pabbrev,0); /* end of entry */
1945 abbrevlen = saalen = pabbrev->datalen;
1946 abbrevbuf = pbuf = nasm_malloc(saalen);
1947 saa_rnbytes(pabbrev, pbuf, saalen);
1948 saa_free(pabbrev);
1950 /* build line section */
1951 /* prolog */
1952 plines = saa_init(1L);
1953 saa_write8(plines,1); /* Minimum Instruction Length */
1954 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1955 saa_write8(plines,line_base); /* Line Base */
1956 saa_write8(plines,line_range); /* Line Range */
1957 saa_write8(plines,opcode_base); /* Opcode Base */
1958 /* standard opcode lengths (# of LEB128u operands) */
1959 saa_write8(plines,0); /* Std opcode 1 length */
1960 saa_write8(plines,1); /* Std opcode 2 length */
1961 saa_write8(plines,1); /* Std opcode 3 length */
1962 saa_write8(plines,1); /* Std opcode 4 length */
1963 saa_write8(plines,1); /* Std opcode 5 length */
1964 saa_write8(plines,0); /* Std opcode 6 length */
1965 saa_write8(plines,0); /* Std opcode 7 length */
1966 saa_write8(plines,0); /* Std opcode 8 length */
1967 saa_write8(plines,1); /* Std opcode 9 length */
1968 saa_write8(plines,0); /* Std opcode 10 length */
1969 saa_write8(plines,0); /* Std opcode 11 length */
1970 saa_write8(plines,1); /* Std opcode 12 length */
1971 /* Directory Table */
1972 saa_write8(plines,0); /* End of table */
1973 /* File Name Table */
1974 ftentry = dwarf_flist;
1975 for (indx = 0; indx < dwarf_numfiles; indx++) {
1976 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1977 saa_write8(plines,0); /* directory LEB128u */
1978 saa_write8(plines,0); /* time LEB128u */
1979 saa_write8(plines,0); /* size LEB128u */
1980 ftentry = ftentry->next;
1982 saa_write8(plines,0); /* End of table */
1983 linepoff = plines->datalen;
1984 linelen = linepoff + totlen + 10;
1985 linebuf = pbuf = nasm_malloc(linelen);
1986 WRITELONG(pbuf,linelen-4); /* initial length */
1987 WRITESHORT(pbuf,3); /* dwarf version */
1988 WRITELONG(pbuf,linepoff); /* offset to line number program */
1989 /* write line header */
1990 saalen = linepoff;
1991 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1992 pbuf += linepoff;
1993 saa_free(plines);
1994 /* concatonate line program ranges */
1995 linepoff += 13;
1996 plinesrel = saa_init(1L);
1997 psect = dwarf_fsect;
1998 for (indx = 0; indx < dwarf_nsections; indx++) {
1999 saa_write32(plinesrel, linepoff);
2000 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2001 saa_write32(plinesrel, (uint32_t) 0);
2002 plinep = psect->psaa;
2003 saalen = plinep->datalen;
2004 saa_rnbytes(plinep, pbuf, saalen);
2005 pbuf += saalen;
2006 linepoff += saalen;
2007 saa_free(plinep);
2008 /* done with this entry */
2009 psect = psect->next;
2013 /* build rela.lines section */
2014 linerellen =saalen = plinesrel->datalen;
2015 linerelbuf = pbuf = nasm_malloc(linerellen);
2016 saa_rnbytes(plinesrel, pbuf, saalen);
2017 saa_free(plinesrel);
2019 /* build frame section */
2020 framelen = 4;
2021 framebuf = pbuf = nasm_malloc(framelen);
2022 WRITELONG(pbuf,framelen-4); /* initial length */
2024 /* build loc section */
2025 loclen = 16;
2026 locbuf = pbuf = nasm_malloc(loclen);
2027 WRITELONG(pbuf,0); /* null beginning offset */
2028 WRITELONG(pbuf,0); /* null ending offset */
2031 static void dwarf32_cleanup(void)
2033 nasm_free(arangesbuf);
2034 nasm_free(arangesrelbuf);
2035 nasm_free(pubnamesbuf);
2036 nasm_free(infobuf);
2037 nasm_free(inforelbuf);
2038 nasm_free(abbrevbuf);
2039 nasm_free(linebuf);
2040 nasm_free(linerelbuf);
2041 nasm_free(framebuf);
2042 nasm_free(locbuf);
2045 static void dwarf32_findfile(const char * fname)
2047 int finx;
2048 struct linelist *match;
2050 /* return if fname is current file name */
2051 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2052 return;
2054 /* search for match */
2055 match = 0;
2056 if (dwarf_flist) {
2057 match = dwarf_flist;
2058 for (finx = 0; finx < dwarf_numfiles; finx++) {
2059 if (!(strcmp(fname, match->filename))) {
2060 dwarf_clist = match;
2061 return;
2066 /* add file name to end of list */
2067 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2068 dwarf_numfiles++;
2069 dwarf_clist->line = dwarf_numfiles;
2070 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2071 strcpy(dwarf_clist->filename,fname);
2072 dwarf_clist->next = 0;
2073 if (!dwarf_flist) { /* if first entry */
2074 dwarf_flist = dwarf_elist = dwarf_clist;
2075 dwarf_clist->last = 0;
2076 } else { /* chain to previous entry */
2077 dwarf_elist->next = dwarf_clist;
2078 dwarf_elist = dwarf_clist;
2082 static void dwarf32_findsect(const int index)
2084 int sinx;
2085 struct sectlist *match;
2086 struct SAA *plinep;
2088 /* return if index is current section index */
2089 if (dwarf_csect && (dwarf_csect->section == index))
2090 return;
2092 /* search for match */
2093 match = 0;
2094 if (dwarf_fsect) {
2095 match = dwarf_fsect;
2096 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2097 if ((match->section == index)) {
2098 dwarf_csect = match;
2099 return;
2101 match = match->next;
2105 /* add entry to end of list */
2106 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2107 dwarf_nsections++;
2108 dwarf_csect->psaa = plinep = saa_init(1L);
2109 dwarf_csect->line = 1;
2110 dwarf_csect->offset = 0;
2111 dwarf_csect->file = 1;
2112 dwarf_csect->section = index;
2113 dwarf_csect->next = 0;
2114 /* set relocatable address at start of line program */
2115 saa_write8(plinep,DW_LNS_extended_op);
2116 saa_write8(plinep,5); /* operand length */
2117 saa_write8(plinep,DW_LNE_set_address);
2118 saa_write32(plinep,0); /* Start Address */
2120 if (!dwarf_fsect) { /* if first entry */
2121 dwarf_fsect = dwarf_esect = dwarf_csect;
2122 dwarf_csect->last = 0;
2123 } else { /* chain to previous entry */
2124 dwarf_esect->next = dwarf_csect;
2125 dwarf_esect = dwarf_csect;
2129 #endif /* OF_ELF */