Elf: Some unification snippets for 32/64 bit versions
[nasm.git] / output / outelf32.c
blob4a99ec973f82a78afc1a353603aa7b3f55f92a03
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;
125 extern struct ofmt of_elf;
127 static struct ELF_SECTDATA {
128 void *data;
129 int32_t len;
130 bool is_saa;
131 } *elf_sects;
132 static int elf_nsect, nsections;
133 static int32_t elf_foffs;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section *, const uint8_t *,
137 uint32_t);
138 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
139 int, int);
140 static void elf_write_sections(void);
141 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
143 static void add_sectname(char *, char *);
145 struct stabentry {
146 uint32_t n_strx;
147 uint8_t n_type;
148 uint8_t n_other;
149 uint16_t n_desc;
150 uint32_t n_value;
153 struct erel {
154 int offset, info;
157 struct symlininfo {
158 int offset;
159 int section; /* section index */
160 char *name; /* shallow-copied pointer of section name */
163 struct linelist {
164 struct symlininfo info;
165 int line;
166 char *filename;
167 struct linelist *next;
168 struct linelist *last;
171 struct sectlist {
172 struct SAA *psaa;
173 int section;
174 int line;
175 int offset;
176 int file;
177 struct sectlist *next;
178 struct sectlist *last;
181 /* common debug variables */
182 static int currentline = 1;
183 static int debug_immcall = 0;
185 /* stabs debug variables */
186 static struct linelist *stabslines = 0;
187 static int numlinestabs = 0;
188 static char *stabs_filename = 0;
189 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
190 static int stablen, stabstrlen, stabrellen;
192 /* dwarf debug variables */
193 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
194 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
195 static int dwarf_numfiles = 0, dwarf_nsections;
196 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
197 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
198 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
199 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
200 abbrevlen, linelen, linerellen, framelen, loclen;
201 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
203 static struct dfmt df_dwarf;
204 static struct dfmt df_stabs;
205 static struct Symbol *lastsym;
207 /* common debugging routines */
208 static void debug32_typevalue(int32_t);
209 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
210 static void debug32_directive(const char *, const char *);
212 /* stabs debugging routines */
213 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
214 static void stabs32_output(int, void *);
215 static void stabs32_generate(void);
216 static void stabs32_cleanup(void);
218 /* dwarf debugging routines */
219 static void dwarf32_init(void);
220 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
221 static void dwarf32_output(int, void *);
222 static void dwarf32_generate(void);
223 static void dwarf32_cleanup(void);
224 static void dwarf32_findfile(const char *);
225 static void dwarf32_findsect(const int);
228 * Special NASM section numbers which are used to define ELF special
229 * symbols, which can be used with WRT to provide PIC and TLS
230 * relocation types.
232 static int32_t elf_gotpc_sect, elf_gotoff_sect;
233 static int32_t elf_got_sect, elf_plt_sect;
234 static int32_t elf_sym_sect, elf_tlsie_sect;
236 static void elf_init(void)
238 sects = NULL;
239 nsects = sectlen = 0;
240 syms = saa_init((int32_t)sizeof(struct Symbol));
241 nlocals = nglobs = ndebugs = 0;
242 bsym = raa_init();
243 strs = saa_init(1L);
244 saa_wbytes(strs, "\0", 1L);
245 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
246 strslen = 2 + strlen(elf_module);
247 shstrtab = NULL;
248 shstrtablen = shstrtabsize = 0;;
249 add_sectname("", "");
251 fwds = NULL;
253 elf_gotpc_sect = seg_alloc();
254 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
255 elf_gotoff_sect = seg_alloc();
256 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
257 elf_got_sect = seg_alloc();
258 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
259 elf_plt_sect = seg_alloc();
260 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
261 elf_sym_sect = seg_alloc();
262 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
263 elf_tlsie_sect = seg_alloc();
264 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
266 def_seg = seg_alloc();
269 static void elf_init_hack(void)
271 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
272 elf_init();
275 static void elf_cleanup(int debuginfo)
277 struct Reloc *r;
278 int i;
280 (void)debuginfo;
282 elf_write();
283 for (i = 0; i < nsects; i++) {
284 if (sects[i]->type != SHT_NOBITS)
285 saa_free(sects[i]->data);
286 if (sects[i]->head)
287 saa_free(sects[i]->rel);
288 while (sects[i]->head) {
289 r = sects[i]->head;
290 sects[i]->head = sects[i]->head->next;
291 nasm_free(r);
294 nasm_free(sects);
295 saa_free(syms);
296 raa_free(bsym);
297 saa_free(strs);
298 if (of_elf32.current_dfmt) {
299 of_elf32.current_dfmt->cleanup();
303 static void add_sectname(char *firsthalf, char *secondhalf)
305 int len = strlen(firsthalf) + strlen(secondhalf);
306 while (shstrtablen + len + 1 > shstrtabsize)
307 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
308 strcpy(shstrtab + shstrtablen, firsthalf);
309 strcat(shstrtab + shstrtablen, secondhalf);
310 shstrtablen += len + 1;
313 static int elf_make_section(char *name, int type, int flags, int align)
315 struct Section *s;
317 s = nasm_malloc(sizeof(*s));
319 if (type != SHT_NOBITS)
320 s->data = saa_init(1L);
321 s->head = NULL;
322 s->tail = &s->head;
323 s->len = s->size = 0;
324 s->nrelocs = 0;
325 if (!strcmp(name, ".text"))
326 s->index = def_seg;
327 else
328 s->index = seg_alloc();
329 add_sectname("", name);
330 s->name = nasm_malloc(1 + strlen(name));
331 strcpy(s->name, name);
332 s->type = type;
333 s->flags = flags;
334 s->align = align;
335 s->gsyms = NULL;
337 if (nsects >= sectlen)
338 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
339 sects[nsects++] = s;
341 return nsects - 1;
344 static int32_t elf_section_names(char *name, int pass, int *bits)
346 char *p;
347 uint32_t flags, flags_and, flags_or;
348 uint64_t align;
349 int type, i;
352 * Default is 32 bits.
354 if (!name) {
355 *bits = 32;
356 return def_seg;
359 p = nasm_skip_word(name);
360 if (*p)
361 *p++ = '\0';
362 flags_and = flags_or = type = align = 0;
364 section_attrib(name, p, pass, &flags_and,
365 &flags_or, &align, &type);
367 if (!strcmp(name, ".shstrtab") ||
368 !strcmp(name, ".symtab") ||
369 !strcmp(name, ".strtab")) {
370 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
371 "name `%s'", name);
372 return NO_SEG;
375 for (i = 0; i < nsects; i++)
376 if (!strcmp(name, sects[i]->name))
377 break;
378 if (i == nsects) {
379 const struct elf_known_section *ks = elf_known_sections;
381 while (ks->name) {
382 if (!strcmp(name, ks->name))
383 break;
384 ks++;
387 type = type ? type : ks->type;
388 align = align ? align : ks->align;
389 flags = (ks->flags & ~flags_and) | flags_or;
391 i = elf_make_section(name, type, flags, align);
392 } else if (pass == 1) {
393 if ((type && sects[i]->type != type)
394 || (align && sects[i]->align != align)
395 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
396 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
397 " redeclaration of section `%s'", name);
400 return sects[i]->index;
403 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
404 int is_global, char *special)
406 int pos = strslen;
407 struct Symbol *sym;
408 bool special_used = false;
410 #if defined(DEBUG) && DEBUG>2
411 nasm_error(ERR_DEBUG,
412 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
413 name, segment, offset, is_global, special);
414 #endif
415 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
417 * This is a NASM special symbol. We never allow it into
418 * the ELF symbol table, even if it's a valid one. If it
419 * _isn't_ a valid one, we should barf immediately.
421 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
422 strcmp(name, "..got") && strcmp(name, "..plt") &&
423 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
424 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
425 return;
428 if (is_global == 3) {
429 struct Symbol **s;
431 * Fix up a forward-reference symbol size from the first
432 * pass.
434 for (s = &fwds; *s; s = &(*s)->nextfwd)
435 if (!strcmp((*s)->name, name)) {
436 struct tokenval tokval;
437 expr *e;
438 char *p = nasm_skip_spaces(nasm_skip_word(special));
440 stdscan_reset();
441 stdscan_set(p);
442 tokval.t_type = TOKEN_INVALID;
443 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
444 if (e) {
445 if (!is_simple(e))
446 nasm_error(ERR_NONFATAL, "cannot use relocatable"
447 " expression as symbol size");
448 else
449 (*s)->size = reloc_value(e);
453 * Remove it from the list of unresolved sizes.
455 nasm_free((*s)->name);
456 *s = (*s)->nextfwd;
457 return;
459 return; /* it wasn't an important one */
462 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
463 strslen += 1 + strlen(name);
465 lastsym = sym = saa_wstruct(syms);
467 memset(&sym->symv, 0, sizeof(struct rbtree));
469 sym->strpos = pos;
470 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
471 sym->other = STV_DEFAULT;
472 sym->size = 0;
473 if (segment == NO_SEG)
474 sym->section = SHN_ABS;
475 else {
476 int i;
477 sym->section = SHN_UNDEF;
478 if (segment == def_seg) {
479 /* we have to be sure at least text section is there */
480 int tempint;
481 if (segment != elf_section_names(".text", 2, &tempint))
482 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
484 for (i = 0; i < nsects; i++) {
485 if (segment == sects[i]->index) {
486 sym->section = i + 1;
487 break;
492 if (is_global == 2) {
493 sym->size = offset;
494 sym->symv.key = 0;
495 sym->section = SHN_COMMON;
497 * We have a common variable. Check the special text to see
498 * if it's a valid number and power of two; if so, store it
499 * as the alignment for the common variable.
501 if (special) {
502 bool err;
503 sym->symv.key = readnum(special, &err);
504 if (err)
505 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
506 " valid number", special);
507 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
508 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
509 " power of two", special);
511 special_used = true;
512 } else
513 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
515 if (sym->type == SYM_GLOBAL) {
517 * If sym->section == SHN_ABS, then the first line of the
518 * else section would cause a core dump, because its a reference
519 * beyond the end of the section array.
520 * This behaviour is exhibited by this code:
521 * GLOBAL crash_nasm
522 * crash_nasm equ 0
523 * To avoid such a crash, such requests are silently discarded.
524 * This may not be the best solution.
526 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
527 bsym = raa_write(bsym, segment, nglobs);
528 } else if (sym->section != SHN_ABS) {
530 * This is a global symbol; so we must add it to the rbtree
531 * of global symbols in its section.
533 * In addition, we check the special text for symbol
534 * type and size information.
536 sects[sym->section-1]->gsyms =
537 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
539 if (special) {
540 int n = strcspn(special, " \t");
542 if (!nasm_strnicmp(special, "function", n))
543 sym->type |= STT_FUNC;
544 else if (!nasm_strnicmp(special, "data", n) ||
545 !nasm_strnicmp(special, "object", n))
546 sym->type |= STT_OBJECT;
547 else if (!nasm_strnicmp(special, "notype", n))
548 sym->type |= STT_NOTYPE;
549 else
550 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
551 n, special);
552 special += n;
554 special = nasm_skip_spaces(special);
555 if (*special) {
556 n = strcspn(special, " \t");
557 if (!nasm_strnicmp(special, "default", n))
558 sym->other = STV_DEFAULT;
559 else if (!nasm_strnicmp(special, "internal", n))
560 sym->other = STV_INTERNAL;
561 else if (!nasm_strnicmp(special, "hidden", n))
562 sym->other = STV_HIDDEN;
563 else if (!nasm_strnicmp(special, "protected", n))
564 sym->other = STV_PROTECTED;
565 else
566 n = 0;
567 special += n;
570 if (*special) {
571 struct tokenval tokval;
572 expr *e;
573 int fwd = 0;
574 char *saveme = stdscan_get();
576 while (special[n] && nasm_isspace(special[n]))
577 n++;
579 * We have a size expression; attempt to
580 * evaluate it.
582 stdscan_reset();
583 stdscan_set(special + n);
584 tokval.t_type = TOKEN_INVALID;
585 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
586 NULL);
587 if (fwd) {
588 sym->nextfwd = fwds;
589 fwds = sym;
590 sym->name = nasm_strdup(name);
591 } else if (e) {
592 if (!is_simple(e))
593 nasm_error(ERR_NONFATAL, "cannot use relocatable"
594 " expression as symbol size");
595 else
596 sym->size = reloc_value(e);
598 stdscan_set(saveme);
600 special_used = true;
603 * If TLS segment, mark symbol accordingly.
605 if (sects[sym->section - 1]->flags & SHF_TLS) {
606 sym->type &= 0xf0;
607 sym->type |= STT_TLS;
610 sym->globnum = nglobs;
611 nglobs++;
612 } else
613 nlocals++;
615 if (special && !special_used)
616 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
619 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
621 struct Reloc *r;
623 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
624 sect->tail = &r->next;
625 r->next = NULL;
627 r->address = sect->len;
628 if (segment == NO_SEG)
629 r->symbol = 0;
630 else {
631 int i;
632 r->symbol = 0;
633 for (i = 0; i < nsects; i++)
634 if (segment == sects[i]->index)
635 r->symbol = i + 2;
636 if (!r->symbol)
637 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
639 r->type = type;
641 sect->nrelocs++;
645 * This routine deals with ..got and ..sym relocations: the more
646 * complicated kinds. In shared-library writing, some relocations
647 * with respect to global symbols must refer to the precise symbol
648 * rather than referring to an offset from the base of the section
649 * _containing_ the symbol. Such relocations call to this routine,
650 * which searches the symbol list for the symbol in question.
652 * R_386_GOT32 references require the _exact_ symbol address to be
653 * used; R_386_32 references can be at an offset from the symbol.
654 * The boolean argument `exact' tells us this.
656 * Return value is the adjusted value of `addr', having become an
657 * offset from the symbol rather than the section. Should always be
658 * zero when returning from an exact call.
660 * Limitation: if you define two symbols at the same place,
661 * confusion will occur.
663 * Inefficiency: we search, currently, using a linked list which
664 * isn't even necessarily sorted.
666 static int32_t elf_add_gsym_reloc(struct Section *sect,
667 int32_t segment, uint32_t offset,
668 int type, bool exact)
670 struct Reloc *r;
671 struct Section *s;
672 struct Symbol *sym;
673 struct rbtree *srb;
674 int i;
677 * First look up the segment/offset pair and find a global
678 * symbol corresponding to it. If it's not one of our segments,
679 * then it must be an external symbol, in which case we're fine
680 * doing a normal elf_add_reloc after first sanity-checking
681 * that the offset from the symbol is zero.
683 s = NULL;
684 for (i = 0; i < nsects; i++)
685 if (segment == sects[i]->index) {
686 s = sects[i];
687 break;
689 if (!s) {
690 if (exact && offset != 0)
691 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
692 " for this reference");
693 else
694 elf_add_reloc(sect, segment, type);
695 return offset;
698 srb = rb_search(s->gsyms, offset);
699 if (!srb || (exact && srb->key != offset)) {
700 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
701 " for this reference");
702 return 0;
704 sym = container_of(srb, struct Symbol, symv);
706 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
707 sect->tail = &r->next;
708 r->next = NULL;
710 r->address = sect->len;
711 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
712 r->type = type;
714 sect->nrelocs++;
716 return offset - sym->symv.key;
719 static void elf_out(int32_t segto, const void *data,
720 enum out_type type, uint64_t size,
721 int32_t segment, int32_t wrt)
723 struct Section *s;
724 int32_t addr;
725 uint8_t mydata[4], *p;
726 int i;
727 static struct symlininfo sinfo;
730 * handle absolute-assembly (structure definitions)
732 if (segto == NO_SEG) {
733 if (type != OUT_RESERVE)
734 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
735 " space");
736 return;
739 s = NULL;
740 for (i = 0; i < nsects; i++)
741 if (segto == sects[i]->index) {
742 s = sects[i];
743 break;
745 if (!s) {
746 int tempint; /* ignored */
747 if (segto != elf_section_names(".text", 2, &tempint))
748 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
749 else {
750 s = sects[nsects - 1];
751 i = nsects - 1;
755 /* again some stabs debugging stuff */
756 if (of_elf32.current_dfmt) {
757 sinfo.offset = s->len;
758 sinfo.section = i;
759 sinfo.name = s->name;
760 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
762 /* end of debugging stuff */
764 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
765 nasm_error(ERR_WARNING, "attempt to initialize memory in"
766 " BSS section `%s': ignored", s->name);
767 s->len += realsize(type, size);
768 return;
771 if (type == OUT_RESERVE) {
772 if (s->type == SHT_PROGBITS) {
773 nasm_error(ERR_WARNING, "uninitialized space declared in"
774 " non-BSS section `%s': zeroing", s->name);
775 elf_sect_write(s, NULL, size);
776 } else
777 s->len += size;
778 } else if (type == OUT_RAWDATA) {
779 if (segment != NO_SEG)
780 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
781 elf_sect_write(s, data, size);
782 } else if (type == 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 if (size == 2) {
792 gnu16 = true;
793 elf_add_reloc(s, segment, R_386_16);
794 } else {
795 elf_add_reloc(s, segment, R_386_32);
797 } else if (wrt == elf_gotpc_sect + 1) {
799 * The user will supply GOT relative to $$. ELF
800 * will let us have GOT relative to $. So we
801 * need to fix up the data item by $-$$.
803 addr += s->len;
804 elf_add_reloc(s, segment, R_386_GOTPC);
805 } else if (wrt == elf_gotoff_sect + 1) {
806 elf_add_reloc(s, segment, R_386_GOTOFF);
807 } else if (wrt == elf_tlsie_sect + 1) {
808 addr = elf_add_gsym_reloc(s, segment, addr,
809 R_386_TLS_IE, true);
810 } else if (wrt == elf_got_sect + 1) {
811 addr = elf_add_gsym_reloc(s, segment, addr,
812 R_386_GOT32, true);
813 } else if (wrt == elf_sym_sect + 1) {
814 if (size == 2) {
815 gnu16 = true;
816 addr = elf_add_gsym_reloc(s, segment, addr,
817 R_386_16, false);
818 } else {
819 addr = elf_add_gsym_reloc(s, segment, addr,
820 R_386_32, false);
822 } else if (wrt == elf_plt_sect + 1) {
823 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
824 "relative PLT references");
825 } else {
826 nasm_error(ERR_NONFATAL, "ELF format does not support this"
827 " use of WRT");
828 wrt = NO_SEG; /* we can at least _try_ to continue */
832 p = mydata;
833 if (gnu16) {
834 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
835 "16-bit relocations in ELF is a GNU extension");
836 WRITESHORT(p, addr);
837 } else {
838 if (size != 4 && segment != NO_SEG) {
839 nasm_error(ERR_NONFATAL,
840 "Unsupported non-32-bit ELF relocation");
842 WRITELONG(p, addr);
844 elf_sect_write(s, mydata, size);
845 } else if (type == OUT_REL2ADR) {
846 if (segment == segto)
847 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
848 if (segment != NO_SEG && segment % 2) {
849 nasm_error(ERR_NONFATAL, "ELF format does not support"
850 " segment base references");
851 } else {
852 if (wrt == NO_SEG) {
853 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
854 "16-bit relocations in ELF is a GNU extension");
855 elf_add_reloc(s, segment, R_386_PC16);
856 } else {
857 nasm_error(ERR_NONFATAL,
858 "Unsupported non-32-bit ELF relocation");
861 p = mydata;
862 WRITESHORT(p, *(int64_t *)data - size);
863 elf_sect_write(s, mydata, 2L);
864 } else if (type == OUT_REL4ADR) {
865 if (segment == segto)
866 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
867 if (segment != NO_SEG && segment % 2) {
868 nasm_error(ERR_NONFATAL, "ELF format does not support"
869 " segment base references");
870 } else {
871 if (wrt == NO_SEG) {
872 elf_add_reloc(s, segment, R_386_PC32);
873 } else if (wrt == elf_plt_sect + 1) {
874 elf_add_reloc(s, segment, R_386_PLT32);
875 } else if (wrt == elf_gotpc_sect + 1 ||
876 wrt == elf_gotoff_sect + 1 ||
877 wrt == elf_got_sect + 1) {
878 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
879 "relative GOT references");
880 } else {
881 nasm_error(ERR_NONFATAL, "ELF format does not support this"
882 " use of WRT");
883 wrt = NO_SEG; /* we can at least _try_ to continue */
886 p = mydata;
887 WRITELONG(p, *(int64_t *)data - size);
888 elf_sect_write(s, mydata, 4L);
892 static void elf_write(void)
894 int align;
895 char *p;
896 int i;
898 struct SAA *symtab;
899 int32_t symtablen, symtablocal;
902 * Work out how many sections we will have. We have SHN_UNDEF,
903 * then the flexible user sections, then the fixed sections
904 * `.shstrtab', `.symtab' and `.strtab', then optionally
905 * relocation sections for the user sections.
907 nsections = sec_numspecial + 1;
908 if (of_elf32.current_dfmt == &df_stabs)
909 nsections += 3;
910 else if (of_elf32.current_dfmt == &df_dwarf)
911 nsections += 10;
913 add_sectname("", ".shstrtab");
914 add_sectname("", ".symtab");
915 add_sectname("", ".strtab");
916 for (i = 0; i < nsects; i++) {
917 nsections++; /* for the section itself */
918 if (sects[i]->head) {
919 nsections++; /* for its relocations */
920 add_sectname(".rel", sects[i]->name);
924 if (of_elf32.current_dfmt == &df_stabs) {
925 /* in case the debug information is wanted, just add these three sections... */
926 add_sectname("", ".stab");
927 add_sectname("", ".stabstr");
928 add_sectname(".rel", ".stab");
929 } else if (of_elf32.current_dfmt == &df_dwarf) {
930 /* the dwarf debug standard specifies the following ten sections,
931 not all of which are currently implemented,
932 although all of them are defined. */
933 add_sectname("", ".debug_aranges");
934 add_sectname(".rela", ".debug_aranges");
935 add_sectname("", ".debug_pubnames");
936 add_sectname("", ".debug_info");
937 add_sectname(".rela", ".debug_info");
938 add_sectname("", ".debug_abbrev");
939 add_sectname("", ".debug_line");
940 add_sectname(".rela", ".debug_line");
941 add_sectname("", ".debug_frame");
942 add_sectname("", ".debug_loc");
946 * Output the ELF header.
948 fwrite("\177ELF\1\1\1", 7, 1, ofile);
949 fputc(elf_osabi, ofile);
950 fputc(elf_abiver, ofile);
951 fwritezero(7, ofile);
952 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
953 fwriteint16_t(3, ofile); /* EM_386 processor ID */
954 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
955 fwriteint32_t(0L, ofile); /* no entry point */
956 fwriteint32_t(0L, ofile); /* no program header table */
957 fwriteint32_t(0x40L, ofile); /* section headers straight after
958 * ELF header plus alignment */
959 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
960 fwriteint16_t(0x34, ofile); /* size of ELF header */
961 fwriteint16_t(0, ofile); /* no program header table, again */
962 fwriteint16_t(0, ofile); /* still no program header table */
963 fwriteint16_t(0x28, ofile); /* size of section header */
964 fwriteint16_t(nsections, ofile); /* number of sections */
965 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
966 * section header table */
967 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
968 fwriteint32_t(0L, ofile);
969 fwriteint32_t(0L, ofile);
972 * Build the symbol table and relocation tables.
974 symtab = elf_build_symtab(&symtablen, &symtablocal);
975 for (i = 0; i < nsects; i++)
976 if (sects[i]->head)
977 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
978 sects[i]->head);
981 * Now output the section header table.
984 elf_foffs = 0x40 + 0x28 * nsections;
985 align = ALIGN(elf_foffs, SEG_ALIGN) - elf_foffs;
986 elf_foffs += align;
987 elf_nsect = 0;
988 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
990 /* SHN_UNDEF */
991 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
992 p = shstrtab + 1;
994 /* The normal sections */
995 for (i = 0; i < nsects; i++) {
996 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
997 (sects[i]->type == SHT_PROGBITS ?
998 sects[i]->data : NULL), true,
999 sects[i]->len, 0, 0, sects[i]->align, 0);
1000 p += strlen(p) + 1;
1003 /* .shstrtab */
1004 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1005 shstrtablen, 0, 0, 1, 0);
1006 p += strlen(p) + 1;
1008 /* .symtab */
1009 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1010 symtablen, sec_strtab, symtablocal, 4, 16);
1011 p += strlen(p) + 1;
1013 /* .strtab */
1014 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1015 strslen, 0, 0, 1, 0);
1016 p += strlen(p) + 1;
1018 /* The relocation sections */
1019 for (i = 0; i < nsects; i++)
1020 if (sects[i]->head) {
1021 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1022 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1023 p += strlen(p) + 1;
1026 if (of_elf32.current_dfmt == &df_stabs) {
1027 /* for debugging information, create the last three sections
1028 which are the .stab , .stabstr and .rel.stab sections respectively */
1030 /* this function call creates the stab sections in memory */
1031 stabs32_generate();
1033 if (stabbuf && stabstrbuf && stabrelbuf) {
1034 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1035 stablen, sec_stabstr, 0, 4, 12);
1036 p += strlen(p) + 1;
1038 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1039 stabstrlen, 0, 0, 4, 0);
1040 p += strlen(p) + 1;
1042 /* link -> symtable info -> section to refer to */
1043 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1044 stabrellen, sec_symtab, sec_stab, 4, 8);
1045 p += strlen(p) + 1;
1047 } else if (of_elf32.current_dfmt == &df_dwarf) {
1048 /* for dwarf debugging information, create the ten dwarf sections */
1050 /* this function call creates the dwarf sections in memory */
1051 if (dwarf_fsect)
1052 dwarf32_generate();
1054 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1055 arangeslen, 0, 0, 1, 0);
1056 p += strlen(p) + 1;
1058 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1059 arangesrellen, sec_symtab, sec_debug_aranges,
1060 1, 12);
1061 p += strlen(p) + 1;
1063 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1064 false, pubnameslen, 0, 0, 1, 0);
1065 p += strlen(p) + 1;
1067 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1068 infolen, 0, 0, 1, 0);
1069 p += strlen(p) + 1;
1071 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1072 inforellen, sec_symtab, sec_debug_info, 1, 12);
1073 p += strlen(p) + 1;
1075 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1076 abbrevlen, 0, 0, 1, 0);
1077 p += strlen(p) + 1;
1079 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1080 linelen, 0, 0, 1, 0);
1081 p += strlen(p) + 1;
1083 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1084 linerellen, sec_symtab, sec_debug_line, 1, 12);
1085 p += strlen(p) + 1;
1087 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1088 framelen, 0, 0, 8, 0);
1089 p += strlen(p) + 1;
1091 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1092 loclen, 0, 0, 1, 0);
1093 p += strlen(p) + 1;
1095 fwritezero(align, ofile);
1098 * Now output the sections.
1100 elf_write_sections();
1102 nasm_free(elf_sects);
1103 saa_free(symtab);
1106 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1108 struct SAA *s = saa_init(1L);
1109 struct Symbol *sym;
1110 uint8_t entry[16], *p;
1111 int i;
1113 *len = *local = 0;
1116 * First, an all-zeros entry, required by the ELF spec.
1118 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1119 *len += 16;
1120 (*local)++;
1123 * Next, an entry for the file name.
1125 p = entry;
1126 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1127 WRITELONG(p, 0); /* no value */
1128 WRITELONG(p, 0); /* no size either */
1129 WRITESHORT(p, STT_FILE); /* type FILE */
1130 WRITESHORT(p, SHN_ABS);
1131 saa_wbytes(s, entry, 16L);
1132 *len += 16;
1133 (*local)++;
1136 * Now some standard symbols defining the segments, for relocation
1137 * purposes.
1139 for (i = 1; i <= nsects; i++) {
1140 p = entry;
1141 WRITELONG(p, 0); /* no symbol name */
1142 WRITELONG(p, 0); /* offset zero */
1143 WRITELONG(p, 0); /* size zero */
1144 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1145 WRITESHORT(p, i); /* section id */
1146 saa_wbytes(s, entry, 16L);
1147 *len += 16;
1148 (*local)++;
1152 * Now the other local symbols.
1154 saa_rewind(syms);
1155 while ((sym = saa_rstruct(syms))) {
1156 if (sym->type & SYM_GLOBAL)
1157 continue;
1158 p = entry;
1159 WRITELONG(p, sym->strpos);
1160 WRITELONG(p, sym->symv.key);
1161 WRITELONG(p, sym->size);
1162 WRITECHAR(p, sym->type); /* type and binding */
1163 WRITECHAR(p, sym->other); /* visibility */
1164 WRITESHORT(p, sym->section);
1165 saa_wbytes(s, entry, 16L);
1166 *len += 16;
1167 (*local)++;
1170 * dwarf needs symbols for debug sections
1171 * which are relocation targets.
1173 //*** fix for 32 bit
1174 if (of_elf32.current_dfmt == &df_dwarf) {
1175 dwarf_infosym = *local;
1176 p = entry;
1177 WRITELONG(p, 0); /* no symbol name */
1178 WRITELONG(p, (uint32_t) 0); /* offset zero */
1179 WRITELONG(p, (uint32_t) 0); /* size zero */
1180 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1181 WRITESHORT(p, sec_debug_info); /* section id */
1182 saa_wbytes(s, entry, 16L);
1183 *len += 16;
1184 (*local)++;
1185 dwarf_abbrevsym = *local;
1186 p = entry;
1187 WRITELONG(p, 0); /* no symbol name */
1188 WRITELONG(p, (uint32_t) 0); /* offset zero */
1189 WRITELONG(p, (uint32_t) 0); /* size zero */
1190 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1191 WRITESHORT(p, sec_debug_abbrev); /* section id */
1192 saa_wbytes(s, entry, 16L);
1193 *len += 16;
1194 (*local)++;
1195 dwarf_linesym = *local;
1196 p = entry;
1197 WRITELONG(p, 0); /* no symbol name */
1198 WRITELONG(p, (uint32_t) 0); /* offset zero */
1199 WRITELONG(p, (uint32_t) 0); /* size zero */
1200 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1201 WRITESHORT(p, sec_debug_line); /* section id */
1202 saa_wbytes(s, entry, 16L);
1203 *len += 16;
1204 (*local)++;
1208 * Now the global symbols.
1210 saa_rewind(syms);
1211 while ((sym = saa_rstruct(syms))) {
1212 if (!(sym->type & SYM_GLOBAL))
1213 continue;
1214 p = entry;
1215 WRITELONG(p, sym->strpos);
1216 WRITELONG(p, sym->symv.key);
1217 WRITELONG(p, sym->size);
1218 WRITECHAR(p, sym->type); /* type and binding */
1219 WRITECHAR(p, sym->other); /* visibility */
1220 WRITESHORT(p, sym->section);
1221 saa_wbytes(s, entry, 16L);
1222 *len += 16;
1225 return s;
1228 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1230 struct SAA *s;
1231 uint8_t *p, entry[8];
1232 int32_t global_offset;
1234 if (!r)
1235 return NULL;
1237 s = saa_init(1L);
1238 *len = 0;
1241 * How to onvert from a global placeholder to a real symbol index;
1242 * the +2 refers to the two special entries, the null entry and
1243 * the filename entry.
1245 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1247 while (r) {
1248 int32_t sym = r->symbol;
1251 * Create a real symbol index; the +2 refers to the two special
1252 * entries, the null entry and the filename entry.
1254 if (sym >= GLOBAL_TEMP_BASE)
1255 sym += global_offset;
1257 p = entry;
1258 WRITELONG(p, r->address);
1259 WRITELONG(p, (sym << 8) + r->type);
1260 saa_wbytes(s, entry, 8L);
1261 *len += 8;
1263 r = r->next;
1266 return s;
1269 static void elf_section_header(int name, int type, int flags,
1270 void *data, bool is_saa, int32_t datalen,
1271 int link, int info, int align, int eltsize)
1273 elf_sects[elf_nsect].data = data;
1274 elf_sects[elf_nsect].len = datalen;
1275 elf_sects[elf_nsect].is_saa = is_saa;
1276 elf_nsect++;
1278 fwriteint32_t((int32_t)name, ofile);
1279 fwriteint32_t((int32_t)type, ofile);
1280 fwriteint32_t((int32_t)flags, ofile);
1281 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1282 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1283 fwriteint32_t(datalen, ofile);
1284 if (data)
1285 elf_foffs += ALIGN(datalen, SEG_ALIGN);
1286 fwriteint32_t((int32_t)link, ofile);
1287 fwriteint32_t((int32_t)info, ofile);
1288 fwriteint32_t((int32_t)align, ofile);
1289 fwriteint32_t((int32_t)eltsize, ofile);
1292 static void elf_write_sections(void)
1294 int i;
1295 for (i = 0; i < elf_nsect; i++)
1296 if (elf_sects[i].data) {
1297 int32_t len = elf_sects[i].len;
1298 int32_t reallen = ALIGN(len, SEG_ALIGN);
1299 int32_t align = reallen - len;
1300 if (elf_sects[i].is_saa)
1301 saa_fpwrite(elf_sects[i].data, ofile);
1302 else
1303 fwrite(elf_sects[i].data, len, 1, ofile);
1304 fwritezero(align, ofile);
1308 static void elf_sect_write(struct Section *sect,
1309 const uint8_t *data, uint32_t len)
1311 saa_wbytes(sect->data, data, len);
1312 sect->len += len;
1315 static int32_t elf_segbase(int32_t segment)
1317 return segment;
1320 static int elf_directive(enum directives directive, char *value, int pass)
1322 bool err;
1323 int64_t n;
1324 char *p;
1326 switch (directive) {
1327 case D_OSABI:
1328 if (pass == 2)
1329 return 1; /* ignore in pass 2 */
1331 n = readnum(value, &err);
1332 if (err) {
1333 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1334 return 1;
1336 if (n < 0 || n > 255) {
1337 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1338 return 1;
1340 elf_osabi = n;
1341 elf_abiver = 0;
1343 if ((p = strchr(value,',')) == NULL)
1344 return 1;
1346 n = readnum(p+1, &err);
1347 if (err || n < 0 || n > 255) {
1348 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1349 return 1;
1352 elf_abiver = n;
1353 return 1;
1355 default:
1356 return 0;
1360 static void elf_filename(char *inname, char *outname)
1362 strcpy(elf_module, inname);
1363 standard_extension(inname, outname, ".o");
1366 extern macros_t elf_stdmac[];
1368 static int elf_set_info(enum geninfo type, char **val)
1370 (void)type;
1371 (void)val;
1372 return 0;
1374 static struct dfmt df_dwarf = {
1375 "ELF32 (i386) dwarf debug format for Linux/Unix",
1376 "dwarf",
1377 dwarf32_init,
1378 dwarf32_linenum,
1379 debug32_deflabel,
1380 debug32_directive,
1381 debug32_typevalue,
1382 dwarf32_output,
1383 dwarf32_cleanup
1385 static struct dfmt df_stabs = {
1386 "ELF32 (i386) stabs debug format for Linux/Unix",
1387 "stabs",
1388 null_debug_init,
1389 stabs32_linenum,
1390 debug32_deflabel,
1391 debug32_directive,
1392 debug32_typevalue,
1393 stabs32_output,
1394 stabs32_cleanup
1397 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1399 struct ofmt of_elf32 = {
1400 "ELF32 (i386) object files (e.g. Linux)",
1401 "elf32",
1403 elf32_debugs_arr,
1404 &df_stabs,
1405 elf_stdmac,
1406 elf_init,
1407 elf_set_info,
1408 elf_out,
1409 elf_deflabel,
1410 elf_section_names,
1411 elf_segbase,
1412 elf_directive,
1413 elf_filename,
1414 elf_cleanup
1417 struct ofmt of_elf = {
1418 "ELF (short name for ELF32) ",
1419 "elf",
1421 elf32_debugs_arr,
1422 &df_stabs,
1423 elf_stdmac,
1424 elf_init_hack,
1425 elf_set_info,
1426 elf_out,
1427 elf_deflabel,
1428 elf_section_names,
1429 elf_segbase,
1430 elf_directive,
1431 elf_filename,
1432 elf_cleanup
1434 /* again, the stabs debugging stuff (code) */
1436 static void stabs32_linenum(const char *filename, int32_t linenumber,
1437 int32_t segto)
1439 (void)segto;
1441 if (!stabs_filename) {
1442 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1443 strcpy(stabs_filename, filename);
1444 } else {
1445 if (strcmp(stabs_filename, filename)) {
1447 * yep, a memory leak...this program is one-shot anyway, so who cares...
1448 * in fact, this leak comes in quite handy to maintain a list of files
1449 * encountered so far in the symbol lines...
1452 /* why not nasm_free(stabs_filename); we're done with the old one */
1454 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1455 strcpy(stabs_filename, filename);
1458 debug_immcall = 1;
1459 currentline = linenumber;
1462 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1463 char *special)
1465 (void)name;
1466 (void)segment;
1467 (void)offset;
1468 (void)is_global;
1469 (void)special;
1472 static void debug32_directive(const char *directive, const char *params)
1474 (void)directive;
1475 (void)params;
1478 static void debug32_typevalue(int32_t type)
1480 int32_t stype, ssize;
1481 switch (TYM_TYPE(type)) {
1482 case TY_LABEL:
1483 ssize = 0;
1484 stype = STT_NOTYPE;
1485 break;
1486 case TY_BYTE:
1487 ssize = 1;
1488 stype = STT_OBJECT;
1489 break;
1490 case TY_WORD:
1491 ssize = 2;
1492 stype = STT_OBJECT;
1493 break;
1494 case TY_DWORD:
1495 ssize = 4;
1496 stype = STT_OBJECT;
1497 break;
1498 case TY_FLOAT:
1499 ssize = 4;
1500 stype = STT_OBJECT;
1501 break;
1502 case TY_QWORD:
1503 ssize = 8;
1504 stype = STT_OBJECT;
1505 break;
1506 case TY_TBYTE:
1507 ssize = 10;
1508 stype = STT_OBJECT;
1509 break;
1510 case TY_OWORD:
1511 ssize = 16;
1512 stype = STT_OBJECT;
1513 break;
1514 case TY_YWORD:
1515 ssize = 32;
1516 stype = STT_OBJECT;
1517 break;
1518 case TY_COMMON:
1519 ssize = 0;
1520 stype = STT_COMMON;
1521 break;
1522 case TY_SEG:
1523 ssize = 0;
1524 stype = STT_SECTION;
1525 break;
1526 case TY_EXTERN:
1527 ssize = 0;
1528 stype = STT_NOTYPE;
1529 break;
1530 case TY_EQU:
1531 ssize = 0;
1532 stype = STT_NOTYPE;
1533 break;
1534 default:
1535 ssize = 0;
1536 stype = STT_NOTYPE;
1537 break;
1539 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1540 lastsym->size = ssize;
1541 lastsym->type = stype;
1545 static void stabs32_output(int type, void *param)
1547 struct symlininfo *s;
1548 struct linelist *el;
1549 if (type == TY_STABSSYMLIN) {
1550 if (debug_immcall) {
1551 s = (struct symlininfo *)param;
1552 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1553 return; /* line info is only collected for executable sections */
1554 numlinestabs++;
1555 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1556 el->info.offset = s->offset;
1557 el->info.section = s->section;
1558 el->info.name = s->name;
1559 el->line = currentline;
1560 el->filename = stabs_filename;
1561 el->next = 0;
1562 if (stabslines) {
1563 stabslines->last->next = el;
1564 stabslines->last = el;
1565 } else {
1566 stabslines = el;
1567 stabslines->last = el;
1571 debug_immcall = 0;
1574 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1576 static void stabs32_generate(void)
1578 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1579 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1580 char **allfiles;
1581 int *fileidx;
1583 struct linelist *ptr;
1585 ptr = stabslines;
1587 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1588 for (i = 0; i < numlinestabs; i++)
1589 allfiles[i] = 0;
1590 numfiles = 0;
1591 while (ptr) {
1592 if (numfiles == 0) {
1593 allfiles[0] = ptr->filename;
1594 numfiles++;
1595 } else {
1596 for (i = 0; i < numfiles; i++) {
1597 if (!strcmp(allfiles[i], ptr->filename))
1598 break;
1600 if (i >= numfiles) {
1601 allfiles[i] = ptr->filename;
1602 numfiles++;
1605 ptr = ptr->next;
1607 strsize = 1;
1608 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1609 for (i = 0; i < numfiles; i++) {
1610 fileidx[i] = strsize;
1611 strsize += strlen(allfiles[i]) + 1;
1613 mainfileindex = 0;
1614 for (i = 0; i < numfiles; i++) {
1615 if (!strcmp(allfiles[i], elf_module)) {
1616 mainfileindex = i;
1617 break;
1622 * worst case size of the stab buffer would be:
1623 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1624 * plus one "ending" entry
1626 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1627 sizeof(struct stabentry));
1628 ssbuf = (uint8_t *)nasm_malloc(strsize);
1629 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1630 rptr = rbuf;
1632 for (i = 0; i < numfiles; i++)
1633 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1634 ssbuf[0] = 0;
1636 stabstrlen = strsize; /* set global variable for length of stab strings */
1638 sptr = sbuf;
1639 ptr = stabslines;
1640 numstabs = 0;
1642 if (ptr) {
1644 * this is the first stab, its strx points to the filename of the
1645 * the source-file, the n_desc field should be set to the number
1646 * of remaining stabs
1648 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1650 /* this is the stab for the main source file */
1651 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1653 /* relocation table entry */
1656 * Since the symbol table has two entries before
1657 * the section symbols, the index in the info.section
1658 * member must be adjusted by adding 2
1661 WRITELONG(rptr, (sptr - sbuf) - 4);
1662 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1664 numstabs++;
1665 currfile = mainfileindex;
1668 while (ptr) {
1669 if (strcmp(allfiles[currfile], ptr->filename)) {
1670 /* oops file has changed... */
1671 for (i = 0; i < numfiles; i++)
1672 if (!strcmp(allfiles[i], ptr->filename))
1673 break;
1674 currfile = i;
1675 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1676 ptr->info.offset);
1677 numstabs++;
1679 /* relocation table entry */
1680 WRITELONG(rptr, (sptr - sbuf) - 4);
1681 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1684 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1685 numstabs++;
1687 /* relocation table entry */
1689 WRITELONG(rptr, (sptr - sbuf) - 4);
1690 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1692 ptr = ptr->next;
1696 /* this is an "ending" token */
1697 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1698 numstabs++;
1700 ((struct stabentry *)sbuf)->n_desc = numstabs;
1702 nasm_free(allfiles);
1703 nasm_free(fileidx);
1705 stablen = (sptr - sbuf);
1706 stabrellen = (rptr - rbuf);
1707 stabrelbuf = rbuf;
1708 stabbuf = sbuf;
1709 stabstrbuf = ssbuf;
1712 static void stabs32_cleanup(void)
1714 struct linelist *ptr, *del;
1715 if (!stabslines)
1716 return;
1718 ptr = stabslines;
1719 while (ptr) {
1720 del = ptr;
1721 ptr = ptr->next;
1722 nasm_free(del);
1725 nasm_free(stabbuf);
1726 nasm_free(stabrelbuf);
1727 nasm_free(stabstrbuf);
1730 /* dwarf routines */
1732 static void dwarf32_init(void)
1734 ndebugs = 3; /* 3 debug symbols */
1737 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1738 int32_t segto)
1740 (void)segto;
1741 dwarf32_findfile(filename);
1742 debug_immcall = 1;
1743 currentline = linenumber;
1746 /* called from elf_out with type == TY_DEBUGSYMLIN */
1747 static void dwarf32_output(int type, void *param)
1749 int ln, aa, inx, maxln, soc;
1750 struct symlininfo *s;
1751 struct SAA *plinep;
1753 (void)type;
1755 s = (struct symlininfo *)param;
1757 /* line number info is only gathered for executable sections */
1758 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1759 return;
1761 /* Check if section index has changed */
1762 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1763 dwarf32_findsect(s->section);
1765 /* do nothing unless line or file has changed */
1766 if (!debug_immcall)
1767 return;
1769 ln = currentline - dwarf_csect->line;
1770 aa = s->offset - dwarf_csect->offset;
1771 inx = dwarf_clist->line;
1772 plinep = dwarf_csect->psaa;
1773 /* check for file change */
1774 if (!(inx == dwarf_csect->file)) {
1775 saa_write8(plinep,DW_LNS_set_file);
1776 saa_write8(plinep,inx);
1777 dwarf_csect->file = inx;
1779 /* check for line change */
1780 if (ln) {
1781 /* test if in range of special op code */
1782 maxln = line_base + line_range;
1783 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1784 if (ln >= line_base && ln < maxln && soc < 256) {
1785 saa_write8(plinep,soc);
1786 } else {
1787 saa_write8(plinep,DW_LNS_advance_line);
1788 saa_wleb128s(plinep,ln);
1789 if (aa) {
1790 saa_write8(plinep,DW_LNS_advance_pc);
1791 saa_wleb128u(plinep,aa);
1794 dwarf_csect->line = currentline;
1795 dwarf_csect->offset = s->offset;
1798 /* show change handled */
1799 debug_immcall = 0;
1803 static void dwarf32_generate(void)
1805 uint8_t *pbuf;
1806 int indx;
1807 struct linelist *ftentry;
1808 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1809 struct SAA *parangesrel, *plinesrel, *pinforel;
1810 struct sectlist *psect;
1811 size_t saalen, linepoff, totlen, highaddr;
1813 /* write epilogues for each line program range */
1814 /* and build aranges section */
1815 paranges = saa_init(1L);
1816 parangesrel = saa_init(1L);
1817 saa_write16(paranges,2); /* dwarf version */
1818 saa_write32(parangesrel, paranges->datalen+4);
1819 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1820 saa_write32(parangesrel, 0);
1821 saa_write32(paranges,0); /* offset into info */
1822 saa_write8(paranges,4); /* pointer size */
1823 saa_write8(paranges,0); /* not segmented */
1824 saa_write32(paranges,0); /* padding */
1825 /* iterate though sectlist entries */
1826 psect = dwarf_fsect;
1827 totlen = 0;
1828 highaddr = 0;
1829 for (indx = 0; indx < dwarf_nsections; indx++) {
1830 plinep = psect->psaa;
1831 /* Line Number Program Epilogue */
1832 saa_write8(plinep,2); /* std op 2 */
1833 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1834 saa_write8(plinep,DW_LNS_extended_op);
1835 saa_write8(plinep,1); /* operand length */
1836 saa_write8(plinep,DW_LNE_end_sequence);
1837 totlen += plinep->datalen;
1838 /* range table relocation entry */
1839 saa_write32(parangesrel, paranges->datalen + 4);
1840 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1841 saa_write32(parangesrel, (uint32_t) 0);
1842 /* range table entry */
1843 saa_write32(paranges,0x0000); /* range start */
1844 saa_write32(paranges,sects[psect->section]->len); /* range length */
1845 highaddr += sects[psect->section]->len;
1846 /* done with this entry */
1847 psect = psect->next;
1849 saa_write32(paranges,0); /* null address */
1850 saa_write32(paranges,0); /* null length */
1851 saalen = paranges->datalen;
1852 arangeslen = saalen + 4;
1853 arangesbuf = pbuf = nasm_malloc(arangeslen);
1854 WRITELONG(pbuf,saalen); /* initial length */
1855 saa_rnbytes(paranges, pbuf, saalen);
1856 saa_free(paranges);
1858 /* build rela.aranges section */
1859 arangesrellen = saalen = parangesrel->datalen;
1860 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1861 saa_rnbytes(parangesrel, pbuf, saalen);
1862 saa_free(parangesrel);
1864 /* build pubnames section */
1865 ppubnames = saa_init(1L);
1866 saa_write16(ppubnames,3); /* dwarf version */
1867 saa_write32(ppubnames,0); /* offset into info */
1868 saa_write32(ppubnames,0); /* space used in info */
1869 saa_write32(ppubnames,0); /* end of list */
1870 saalen = ppubnames->datalen;
1871 pubnameslen = saalen + 4;
1872 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1873 WRITELONG(pbuf,saalen); /* initial length */
1874 saa_rnbytes(ppubnames, pbuf, saalen);
1875 saa_free(ppubnames);
1877 /* build info section */
1878 pinfo = saa_init(1L);
1879 pinforel = saa_init(1L);
1880 saa_write16(pinfo,2); /* dwarf version */
1881 saa_write32(pinforel, pinfo->datalen + 4);
1882 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1883 saa_write32(pinforel, 0);
1884 saa_write32(pinfo,0); /* offset into abbrev */
1885 saa_write8(pinfo,4); /* pointer size */
1886 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1887 saa_write32(pinforel, pinfo->datalen + 4);
1888 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1889 saa_write32(pinforel, 0);
1890 saa_write32(pinfo,0); /* DW_AT_low_pc */
1891 saa_write32(pinforel, pinfo->datalen + 4);
1892 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1893 saa_write32(pinforel, 0);
1894 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1895 saa_write32(pinforel, pinfo->datalen + 4);
1896 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1897 saa_write32(pinforel, 0);
1898 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1899 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1900 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1901 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1902 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1903 saa_write32(pinforel, pinfo->datalen + 4);
1904 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1905 saa_write32(pinforel, 0);
1906 saa_write32(pinfo,0); /* DW_AT_low_pc */
1907 saa_write32(pinfo,0); /* DW_AT_frame_base */
1908 saa_write8(pinfo,0); /* end of entries */
1909 saalen = pinfo->datalen;
1910 infolen = saalen + 4;
1911 infobuf = pbuf = nasm_malloc(infolen);
1912 WRITELONG(pbuf,saalen); /* initial length */
1913 saa_rnbytes(pinfo, pbuf, saalen);
1914 saa_free(pinfo);
1916 /* build rela.info section */
1917 inforellen = saalen = pinforel->datalen;
1918 inforelbuf = pbuf = nasm_malloc(inforellen);
1919 saa_rnbytes(pinforel, pbuf, saalen);
1920 saa_free(pinforel);
1922 /* build abbrev section */
1923 pabbrev = saa_init(1L);
1924 saa_write8(pabbrev,1); /* entry number LEB128u */
1925 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1926 saa_write8(pabbrev,1); /* has children */
1927 /* the following attributes and forms are all LEB128u values */
1928 saa_write8(pabbrev,DW_AT_low_pc);
1929 saa_write8(pabbrev,DW_FORM_addr);
1930 saa_write8(pabbrev,DW_AT_high_pc);
1931 saa_write8(pabbrev,DW_FORM_addr);
1932 saa_write8(pabbrev,DW_AT_stmt_list);
1933 saa_write8(pabbrev,DW_FORM_data4);
1934 saa_write8(pabbrev,DW_AT_name);
1935 saa_write8(pabbrev,DW_FORM_string);
1936 saa_write8(pabbrev,DW_AT_producer);
1937 saa_write8(pabbrev,DW_FORM_string);
1938 saa_write8(pabbrev,DW_AT_language);
1939 saa_write8(pabbrev,DW_FORM_data2);
1940 saa_write16(pabbrev,0); /* end of entry */
1941 /* LEB128u usage same as above */
1942 saa_write8(pabbrev,2); /* entry number */
1943 saa_write8(pabbrev,DW_TAG_subprogram);
1944 saa_write8(pabbrev,0); /* no children */
1945 saa_write8(pabbrev,DW_AT_low_pc);
1946 saa_write8(pabbrev,DW_FORM_addr);
1947 saa_write8(pabbrev,DW_AT_frame_base);
1948 saa_write8(pabbrev,DW_FORM_data4);
1949 saa_write16(pabbrev,0); /* end of entry */
1950 abbrevlen = saalen = pabbrev->datalen;
1951 abbrevbuf = pbuf = nasm_malloc(saalen);
1952 saa_rnbytes(pabbrev, pbuf, saalen);
1953 saa_free(pabbrev);
1955 /* build line section */
1956 /* prolog */
1957 plines = saa_init(1L);
1958 saa_write8(plines,1); /* Minimum Instruction Length */
1959 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1960 saa_write8(plines,line_base); /* Line Base */
1961 saa_write8(plines,line_range); /* Line Range */
1962 saa_write8(plines,opcode_base); /* Opcode Base */
1963 /* standard opcode lengths (# of LEB128u operands) */
1964 saa_write8(plines,0); /* Std opcode 1 length */
1965 saa_write8(plines,1); /* Std opcode 2 length */
1966 saa_write8(plines,1); /* Std opcode 3 length */
1967 saa_write8(plines,1); /* Std opcode 4 length */
1968 saa_write8(plines,1); /* Std opcode 5 length */
1969 saa_write8(plines,0); /* Std opcode 6 length */
1970 saa_write8(plines,0); /* Std opcode 7 length */
1971 saa_write8(plines,0); /* Std opcode 8 length */
1972 saa_write8(plines,1); /* Std opcode 9 length */
1973 saa_write8(plines,0); /* Std opcode 10 length */
1974 saa_write8(plines,0); /* Std opcode 11 length */
1975 saa_write8(plines,1); /* Std opcode 12 length */
1976 /* Directory Table */
1977 saa_write8(plines,0); /* End of table */
1978 /* File Name Table */
1979 ftentry = dwarf_flist;
1980 for (indx = 0; indx < dwarf_numfiles; indx++) {
1981 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1982 saa_write8(plines,0); /* directory LEB128u */
1983 saa_write8(plines,0); /* time LEB128u */
1984 saa_write8(plines,0); /* size LEB128u */
1985 ftentry = ftentry->next;
1987 saa_write8(plines,0); /* End of table */
1988 linepoff = plines->datalen;
1989 linelen = linepoff + totlen + 10;
1990 linebuf = pbuf = nasm_malloc(linelen);
1991 WRITELONG(pbuf,linelen-4); /* initial length */
1992 WRITESHORT(pbuf,3); /* dwarf version */
1993 WRITELONG(pbuf,linepoff); /* offset to line number program */
1994 /* write line header */
1995 saalen = linepoff;
1996 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1997 pbuf += linepoff;
1998 saa_free(plines);
1999 /* concatonate line program ranges */
2000 linepoff += 13;
2001 plinesrel = saa_init(1L);
2002 psect = dwarf_fsect;
2003 for (indx = 0; indx < dwarf_nsections; indx++) {
2004 saa_write32(plinesrel, linepoff);
2005 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2006 saa_write32(plinesrel, (uint32_t) 0);
2007 plinep = psect->psaa;
2008 saalen = plinep->datalen;
2009 saa_rnbytes(plinep, pbuf, saalen);
2010 pbuf += saalen;
2011 linepoff += saalen;
2012 saa_free(plinep);
2013 /* done with this entry */
2014 psect = psect->next;
2018 /* build rela.lines section */
2019 linerellen =saalen = plinesrel->datalen;
2020 linerelbuf = pbuf = nasm_malloc(linerellen);
2021 saa_rnbytes(plinesrel, pbuf, saalen);
2022 saa_free(plinesrel);
2024 /* build frame section */
2025 framelen = 4;
2026 framebuf = pbuf = nasm_malloc(framelen);
2027 WRITELONG(pbuf,framelen-4); /* initial length */
2029 /* build loc section */
2030 loclen = 16;
2031 locbuf = pbuf = nasm_malloc(loclen);
2032 WRITELONG(pbuf,0); /* null beginning offset */
2033 WRITELONG(pbuf,0); /* null ending offset */
2036 static void dwarf32_cleanup(void)
2038 nasm_free(arangesbuf);
2039 nasm_free(arangesrelbuf);
2040 nasm_free(pubnamesbuf);
2041 nasm_free(infobuf);
2042 nasm_free(inforelbuf);
2043 nasm_free(abbrevbuf);
2044 nasm_free(linebuf);
2045 nasm_free(linerelbuf);
2046 nasm_free(framebuf);
2047 nasm_free(locbuf);
2050 static void dwarf32_findfile(const char * fname)
2052 int finx;
2053 struct linelist *match;
2055 /* return if fname is current file name */
2056 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2057 return;
2059 /* search for match */
2060 match = 0;
2061 if (dwarf_flist) {
2062 match = dwarf_flist;
2063 for (finx = 0; finx < dwarf_numfiles; finx++) {
2064 if (!(strcmp(fname, match->filename))) {
2065 dwarf_clist = match;
2066 return;
2071 /* add file name to end of list */
2072 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2073 dwarf_numfiles++;
2074 dwarf_clist->line = dwarf_numfiles;
2075 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2076 strcpy(dwarf_clist->filename,fname);
2077 dwarf_clist->next = 0;
2078 if (!dwarf_flist) { /* if first entry */
2079 dwarf_flist = dwarf_elist = dwarf_clist;
2080 dwarf_clist->last = 0;
2081 } else { /* chain to previous entry */
2082 dwarf_elist->next = dwarf_clist;
2083 dwarf_elist = dwarf_clist;
2087 static void dwarf32_findsect(const int index)
2089 int sinx;
2090 struct sectlist *match;
2091 struct SAA *plinep;
2093 /* return if index is current section index */
2094 if (dwarf_csect && (dwarf_csect->section == index))
2095 return;
2097 /* search for match */
2098 match = 0;
2099 if (dwarf_fsect) {
2100 match = dwarf_fsect;
2101 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2102 if ((match->section == index)) {
2103 dwarf_csect = match;
2104 return;
2106 match = match->next;
2110 /* add entry to end of list */
2111 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2112 dwarf_nsections++;
2113 dwarf_csect->psaa = plinep = saa_init(1L);
2114 dwarf_csect->line = 1;
2115 dwarf_csect->offset = 0;
2116 dwarf_csect->file = 1;
2117 dwarf_csect->section = index;
2118 dwarf_csect->next = 0;
2119 /* set relocatable address at start of line program */
2120 saa_write8(plinep,DW_LNS_extended_op);
2121 saa_write8(plinep,5); /* operand length */
2122 saa_write8(plinep,DW_LNE_set_address);
2123 saa_write32(plinep,0); /* Start Address */
2125 if (!dwarf_fsect) { /* if first entry */
2126 dwarf_fsect = dwarf_esect = dwarf_csect;
2127 dwarf_csect->last = 0;
2128 } else { /* chain to previous entry */
2129 dwarf_esect->next = dwarf_csect;
2130 dwarf_esect = dwarf_csect;
2134 #endif /* OF_ELF */