NASM 2.08rc7
[nasm/avx512.git] / output / outelf32.c
blob0ce21135818304beb842a2822880dca86315aeea
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 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
129 static struct ELF_SECTDATA {
130 void *data;
131 int32_t len;
132 bool is_saa;
133 } *elf_sects;
134 static int elf_nsect, nsections;
135 static int32_t elf_foffs;
137 static void elf_write(void);
138 static void elf_sect_write(struct Section *, const uint8_t *,
139 uint32_t);
140 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
141 int, int);
142 static void elf_write_sections(void);
143 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
144 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
145 static void add_sectname(char *, char *);
147 struct stabentry {
148 uint32_t n_strx;
149 uint8_t n_type;
150 uint8_t n_other;
151 uint16_t n_desc;
152 uint32_t n_value;
155 struct erel {
156 int offset, info;
159 struct symlininfo {
160 int offset;
161 int section; /* section index */
162 char *name; /* shallow-copied pointer of section name */
165 struct linelist {
166 struct symlininfo info;
167 int line;
168 char *filename;
169 struct linelist *next;
170 struct linelist *last;
173 struct sectlist {
174 struct SAA *psaa;
175 int section;
176 int line;
177 int offset;
178 int file;
179 struct sectlist *next;
180 struct sectlist *last;
183 /* common debug variables */
184 static int currentline = 1;
185 static int debug_immcall = 0;
187 /* stabs debug variables */
188 static struct linelist *stabslines = 0;
189 static int numlinestabs = 0;
190 static char *stabs_filename = 0;
191 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
192 static int stablen, stabstrlen, stabrellen;
194 /* dwarf debug variables */
195 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
196 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
197 static int dwarf_numfiles = 0, dwarf_nsections;
198 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
199 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
200 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
201 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
202 abbrevlen, linelen, linerellen, framelen, loclen;
203 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
205 static struct dfmt df_dwarf;
206 static struct dfmt df_stabs;
207 static struct Symbol *lastsym;
209 /* common debugging routines */
210 static void debug32_typevalue(int32_t);
211 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
212 static void debug32_directive(const char *, const char *);
214 /* stabs debugging routines */
215 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
216 static void stabs32_output(int, void *);
217 static void stabs32_generate(void);
218 static void stabs32_cleanup(void);
220 /* dwarf debugging routines */
221 static void dwarf32_init(void);
222 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
223 static void dwarf32_output(int, void *);
224 static void dwarf32_generate(void);
225 static void dwarf32_cleanup(void);
226 static void dwarf32_findfile(const char *);
227 static void dwarf32_findsect(const int);
230 * Special NASM section numbers which are used to define ELF special
231 * symbols, which can be used with WRT to provide PIC and TLS
232 * relocation types.
234 static int32_t elf_gotpc_sect, elf_gotoff_sect;
235 static int32_t elf_got_sect, elf_plt_sect;
236 static int32_t elf_sym_sect, elf_tlsie_sect;
238 static void elf_init(void)
240 sects = NULL;
241 nsects = sectlen = 0;
242 syms = saa_init((int32_t)sizeof(struct Symbol));
243 nlocals = nglobs = ndebugs = 0;
244 bsym = raa_init();
245 strs = saa_init(1L);
246 saa_wbytes(strs, "\0", 1L);
247 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
248 strslen = 2 + strlen(elf_module);
249 shstrtab = NULL;
250 shstrtablen = shstrtabsize = 0;;
251 add_sectname("", "");
253 fwds = NULL;
255 elf_gotpc_sect = seg_alloc();
256 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
257 elf_gotoff_sect = seg_alloc();
258 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
259 elf_got_sect = seg_alloc();
260 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
261 elf_plt_sect = seg_alloc();
262 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
263 elf_sym_sect = seg_alloc();
264 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
265 elf_tlsie_sect = seg_alloc();
266 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
268 def_seg = seg_alloc();
271 static void elf_init_hack(void)
273 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
274 elf_init();
277 static void elf_cleanup(int debuginfo)
279 struct Reloc *r;
280 int i;
282 (void)debuginfo;
284 elf_write();
285 for (i = 0; i < nsects; i++) {
286 if (sects[i]->type != SHT_NOBITS)
287 saa_free(sects[i]->data);
288 if (sects[i]->head)
289 saa_free(sects[i]->rel);
290 while (sects[i]->head) {
291 r = sects[i]->head;
292 sects[i]->head = sects[i]->head->next;
293 nasm_free(r);
296 nasm_free(sects);
297 saa_free(syms);
298 raa_free(bsym);
299 saa_free(strs);
300 if (of_elf32.current_dfmt) {
301 of_elf32.current_dfmt->cleanup();
305 static void add_sectname(char *firsthalf, char *secondhalf)
307 int len = strlen(firsthalf) + strlen(secondhalf);
308 while (shstrtablen + len + 1 > shstrtabsize)
309 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
310 strcpy(shstrtab + shstrtablen, firsthalf);
311 strcat(shstrtab + shstrtablen, secondhalf);
312 shstrtablen += len + 1;
315 static int elf_make_section(char *name, int type, int flags, int align)
317 struct Section *s;
319 s = nasm_malloc(sizeof(*s));
321 if (type != SHT_NOBITS)
322 s->data = saa_init(1L);
323 s->head = NULL;
324 s->tail = &s->head;
325 s->len = s->size = 0;
326 s->nrelocs = 0;
327 if (!strcmp(name, ".text"))
328 s->index = def_seg;
329 else
330 s->index = seg_alloc();
331 add_sectname("", name);
332 s->name = nasm_malloc(1 + strlen(name));
333 strcpy(s->name, name);
334 s->type = type;
335 s->flags = flags;
336 s->align = align;
337 s->gsyms = NULL;
339 if (nsects >= sectlen)
340 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
341 sects[nsects++] = s;
343 return nsects - 1;
347 static int32_t elf_section_names(char *name, int pass, int *bits)
349 char *p;
350 uint32_t flags, flags_and, flags_or;
351 uint32_t align;
352 int type, i;
355 * Default is 32 bits.
357 if (!name) {
358 *bits = 32;
359 return def_seg;
362 p = nasm_skip_word(name);
363 if (*p)
364 *p++ = '\0';
365 flags_and = flags_or = type = align = 0;
367 p = nasm_skip_spaces(p);
368 while (*p) {
369 char *q = p;
370 p = nasm_skip_word(p);
371 if (*p)
372 *p++ = '\0';
373 p = nasm_skip_spaces(p);
375 if (!nasm_strnicmp(q, "align=", 6)) {
376 align = atoi(q + 6);
377 if (align == 0)
378 align = 1;
379 if ((align - 1) & align) { /* means it's not a power of two */
380 nasm_error(ERR_NONFATAL, "section alignment %d is not"
381 " a power of two", align);
382 align = 1;
384 } else if (!nasm_stricmp(q, "alloc")) {
385 flags_and |= SHF_ALLOC;
386 flags_or |= SHF_ALLOC;
387 } else if (!nasm_stricmp(q, "noalloc")) {
388 flags_and |= SHF_ALLOC;
389 flags_or &= ~SHF_ALLOC;
390 } else if (!nasm_stricmp(q, "exec")) {
391 flags_and |= SHF_EXECINSTR;
392 flags_or |= SHF_EXECINSTR;
393 } else if (!nasm_stricmp(q, "noexec")) {
394 flags_and |= SHF_EXECINSTR;
395 flags_or &= ~SHF_EXECINSTR;
396 } else if (!nasm_stricmp(q, "write")) {
397 flags_and |= SHF_WRITE;
398 flags_or |= SHF_WRITE;
399 } else if (!nasm_stricmp(q, "tls")) {
400 flags_and |= SHF_TLS;
401 flags_or |= SHF_TLS;
402 } else if (!nasm_stricmp(q, "nowrite")) {
403 flags_and |= SHF_WRITE;
404 flags_or &= ~SHF_WRITE;
405 } else if (!nasm_stricmp(q, "progbits")) {
406 type = SHT_PROGBITS;
407 } else if (!nasm_stricmp(q, "nobits")) {
408 type = SHT_NOBITS;
409 } else if (pass == 1) {
410 nasm_error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
411 " declaration of section `%s'", q, name);
415 if (!strcmp(name, ".shstrtab") ||
416 !strcmp(name, ".symtab") ||
417 !strcmp(name, ".strtab")) {
418 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
419 "name `%s'", name);
420 return NO_SEG;
423 for (i = 0; i < nsects; i++)
424 if (!strcmp(name, sects[i]->name))
425 break;
426 if (i == nsects) {
427 const struct elf_known_section *ks = elf_known_sections;
429 while (ks->name) {
430 if (!strcmp(name, ks->name))
431 break;
432 ks++;
435 type = type ? type : ks->type;
436 align = align ? align : ks->align;
437 flags = (ks->flags & ~flags_and) | flags_or;
439 i = elf_make_section(name, type, flags, align);
440 } else if (pass == 1) {
441 if ((type && sects[i]->type != type)
442 || (align && sects[i]->align != align)
443 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
444 nasm_error(ERR_WARNING, "section attributes ignored on"
445 " redeclaration of section `%s'", name);
448 return sects[i]->index;
451 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
452 int is_global, char *special)
454 int pos = strslen;
455 struct Symbol *sym;
456 bool special_used = false;
458 #if defined(DEBUG) && DEBUG>2
459 nasm_error(ERR_DEBUG,
460 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
461 name, segment, offset, is_global, special);
462 #endif
463 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
465 * This is a NASM special symbol. We never allow it into
466 * the ELF symbol table, even if it's a valid one. If it
467 * _isn't_ a valid one, we should barf immediately.
469 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
470 strcmp(name, "..got") && strcmp(name, "..plt") &&
471 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
472 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
473 return;
476 if (is_global == 3) {
477 struct Symbol **s;
479 * Fix up a forward-reference symbol size from the first
480 * pass.
482 for (s = &fwds; *s; s = &(*s)->nextfwd)
483 if (!strcmp((*s)->name, name)) {
484 struct tokenval tokval;
485 expr *e;
486 char *p = nasm_skip_spaces(nasm_skip_word(special));
488 stdscan_reset();
489 stdscan_set(p);
490 tokval.t_type = TOKEN_INVALID;
491 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
492 if (e) {
493 if (!is_simple(e))
494 nasm_error(ERR_NONFATAL, "cannot use relocatable"
495 " expression as symbol size");
496 else
497 (*s)->size = reloc_value(e);
501 * Remove it from the list of unresolved sizes.
503 nasm_free((*s)->name);
504 *s = (*s)->nextfwd;
505 return;
507 return; /* it wasn't an important one */
510 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
511 strslen += 1 + strlen(name);
513 lastsym = sym = saa_wstruct(syms);
515 memset(&sym->symv, 0, sizeof(struct rbtree));
517 sym->strpos = pos;
518 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
519 sym->other = STV_DEFAULT;
520 sym->size = 0;
521 if (segment == NO_SEG)
522 sym->section = SHN_ABS;
523 else {
524 int i;
525 sym->section = SHN_UNDEF;
526 if (segment == def_seg) {
527 /* we have to be sure at least text section is there */
528 int tempint;
529 if (segment != elf_section_names(".text", 2, &tempint))
530 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
532 for (i = 0; i < nsects; i++) {
533 if (segment == sects[i]->index) {
534 sym->section = i + 1;
535 break;
540 if (is_global == 2) {
541 sym->size = offset;
542 sym->symv.key = 0;
543 sym->section = SHN_COMMON;
545 * We have a common variable. Check the special text to see
546 * if it's a valid number and power of two; if so, store it
547 * as the alignment for the common variable.
549 if (special) {
550 bool err;
551 sym->symv.key = readnum(special, &err);
552 if (err)
553 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
554 " valid number", special);
555 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
556 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
557 " power of two", special);
559 special_used = true;
560 } else
561 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
563 if (sym->type == SYM_GLOBAL) {
565 * If sym->section == SHN_ABS, then the first line of the
566 * else section would cause a core dump, because its a reference
567 * beyond the end of the section array.
568 * This behaviour is exhibited by this code:
569 * GLOBAL crash_nasm
570 * crash_nasm equ 0
571 * To avoid such a crash, such requests are silently discarded.
572 * This may not be the best solution.
574 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
575 bsym = raa_write(bsym, segment, nglobs);
576 } else if (sym->section != SHN_ABS) {
578 * This is a global symbol; so we must add it to the rbtree
579 * of global symbols in its section.
581 * In addition, we check the special text for symbol
582 * type and size information.
584 sects[sym->section-1]->gsyms =
585 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
587 if (special) {
588 int n = strcspn(special, " \t");
590 if (!nasm_strnicmp(special, "function", n))
591 sym->type |= STT_FUNC;
592 else if (!nasm_strnicmp(special, "data", n) ||
593 !nasm_strnicmp(special, "object", n))
594 sym->type |= STT_OBJECT;
595 else if (!nasm_strnicmp(special, "notype", n))
596 sym->type |= STT_NOTYPE;
597 else
598 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
599 n, special);
600 special += n;
602 special = nasm_skip_spaces(special);
603 if (*special) {
604 n = strcspn(special, " \t");
605 if (!nasm_strnicmp(special, "default", n))
606 sym->other = STV_DEFAULT;
607 else if (!nasm_strnicmp(special, "internal", n))
608 sym->other = STV_INTERNAL;
609 else if (!nasm_strnicmp(special, "hidden", n))
610 sym->other = STV_HIDDEN;
611 else if (!nasm_strnicmp(special, "protected", n))
612 sym->other = STV_PROTECTED;
613 else
614 n = 0;
615 special += n;
618 if (*special) {
619 struct tokenval tokval;
620 expr *e;
621 int fwd = 0;
622 char *saveme = stdscan_get();
624 while (special[n] && nasm_isspace(special[n]))
625 n++;
627 * We have a size expression; attempt to
628 * evaluate it.
630 stdscan_reset();
631 stdscan_set(special + n);
632 tokval.t_type = TOKEN_INVALID;
633 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
634 NULL);
635 if (fwd) {
636 sym->nextfwd = fwds;
637 fwds = sym;
638 sym->name = nasm_strdup(name);
639 } else if (e) {
640 if (!is_simple(e))
641 nasm_error(ERR_NONFATAL, "cannot use relocatable"
642 " expression as symbol size");
643 else
644 sym->size = reloc_value(e);
646 stdscan_set(saveme);
648 special_used = true;
651 * If TLS segment, mark symbol accordingly.
653 if (sects[sym->section - 1]->flags & SHF_TLS) {
654 sym->type &= 0xf0;
655 sym->type |= STT_TLS;
658 sym->globnum = nglobs;
659 nglobs++;
660 } else
661 nlocals++;
663 if (special && !special_used)
664 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
667 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
669 struct Reloc *r;
671 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
672 sect->tail = &r->next;
673 r->next = NULL;
675 r->address = sect->len;
676 if (segment == NO_SEG)
677 r->symbol = 0;
678 else {
679 int i;
680 r->symbol = 0;
681 for (i = 0; i < nsects; i++)
682 if (segment == sects[i]->index)
683 r->symbol = i + 2;
684 if (!r->symbol)
685 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
687 r->type = type;
689 sect->nrelocs++;
693 * This routine deals with ..got and ..sym relocations: the more
694 * complicated kinds. In shared-library writing, some relocations
695 * with respect to global symbols must refer to the precise symbol
696 * rather than referring to an offset from the base of the section
697 * _containing_ the symbol. Such relocations call to this routine,
698 * which searches the symbol list for the symbol in question.
700 * R_386_GOT32 references require the _exact_ symbol address to be
701 * used; R_386_32 references can be at an offset from the symbol.
702 * The boolean argument `exact' tells us this.
704 * Return value is the adjusted value of `addr', having become an
705 * offset from the symbol rather than the section. Should always be
706 * zero when returning from an exact call.
708 * Limitation: if you define two symbols at the same place,
709 * confusion will occur.
711 * Inefficiency: we search, currently, using a linked list which
712 * isn't even necessarily sorted.
714 static int32_t elf_add_gsym_reloc(struct Section *sect,
715 int32_t segment, uint32_t offset,
716 int type, bool exact)
718 struct Reloc *r;
719 struct Section *s;
720 struct Symbol *sym;
721 struct rbtree *srb;
722 int i;
725 * First look up the segment/offset pair and find a global
726 * symbol corresponding to it. If it's not one of our segments,
727 * then it must be an external symbol, in which case we're fine
728 * doing a normal elf_add_reloc after first sanity-checking
729 * that the offset from the symbol is zero.
731 s = NULL;
732 for (i = 0; i < nsects; i++)
733 if (segment == sects[i]->index) {
734 s = sects[i];
735 break;
737 if (!s) {
738 if (exact && offset != 0)
739 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
740 " for this reference");
741 else
742 elf_add_reloc(sect, segment, type);
743 return offset;
746 srb = rb_search(s->gsyms, offset);
747 if (!srb || (exact && srb->key != offset)) {
748 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
749 " for this reference");
750 return 0;
752 sym = container_of(srb, struct Symbol, symv);
754 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
755 sect->tail = &r->next;
756 r->next = NULL;
758 r->address = sect->len;
759 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
760 r->type = type;
762 sect->nrelocs++;
764 return offset - sym->symv.key;
767 static void elf_out(int32_t segto, const void *data,
768 enum out_type type, uint64_t size,
769 int32_t segment, int32_t wrt)
771 struct Section *s;
772 int32_t addr;
773 uint8_t mydata[4], *p;
774 int i;
775 static struct symlininfo sinfo;
778 * handle absolute-assembly (structure definitions)
780 if (segto == NO_SEG) {
781 if (type != OUT_RESERVE)
782 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
783 " space");
784 return;
787 s = NULL;
788 for (i = 0; i < nsects; i++)
789 if (segto == sects[i]->index) {
790 s = sects[i];
791 break;
793 if (!s) {
794 int tempint; /* ignored */
795 if (segto != elf_section_names(".text", 2, &tempint))
796 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
797 else {
798 s = sects[nsects - 1];
799 i = nsects - 1;
803 /* again some stabs debugging stuff */
804 if (of_elf32.current_dfmt) {
805 sinfo.offset = s->len;
806 sinfo.section = i;
807 sinfo.name = s->name;
808 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
810 /* end of debugging stuff */
812 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
813 nasm_error(ERR_WARNING, "attempt to initialize memory in"
814 " BSS section `%s': ignored", s->name);
815 s->len += realsize(type, size);
816 return;
819 if (type == OUT_RESERVE) {
820 if (s->type == SHT_PROGBITS) {
821 nasm_error(ERR_WARNING, "uninitialized space declared in"
822 " non-BSS section `%s': zeroing", s->name);
823 elf_sect_write(s, NULL, size);
824 } else
825 s->len += size;
826 } else if (type == OUT_RAWDATA) {
827 if (segment != NO_SEG)
828 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
829 elf_sect_write(s, data, size);
830 } else if (type == OUT_ADDRESS) {
831 bool gnu16 = false;
832 addr = *(int64_t *)data;
833 if (segment != NO_SEG) {
834 if (segment % 2) {
835 nasm_error(ERR_NONFATAL, "ELF format does not support"
836 " segment base references");
837 } else {
838 if (wrt == NO_SEG) {
839 if (size == 2) {
840 gnu16 = true;
841 elf_add_reloc(s, segment, R_386_16);
842 } else {
843 elf_add_reloc(s, segment, R_386_32);
845 } else if (wrt == elf_gotpc_sect + 1) {
847 * The user will supply GOT relative to $$. ELF
848 * will let us have GOT relative to $. So we
849 * need to fix up the data item by $-$$.
851 addr += s->len;
852 elf_add_reloc(s, segment, R_386_GOTPC);
853 } else if (wrt == elf_gotoff_sect + 1) {
854 elf_add_reloc(s, segment, R_386_GOTOFF);
855 } else if (wrt == elf_tlsie_sect + 1) {
856 addr = elf_add_gsym_reloc(s, segment, addr,
857 R_386_TLS_IE, true);
858 } else if (wrt == elf_got_sect + 1) {
859 addr = elf_add_gsym_reloc(s, segment, addr,
860 R_386_GOT32, true);
861 } else if (wrt == elf_sym_sect + 1) {
862 if (size == 2) {
863 gnu16 = true;
864 addr = elf_add_gsym_reloc(s, segment, addr,
865 R_386_16, false);
866 } else {
867 addr = elf_add_gsym_reloc(s, segment, addr,
868 R_386_32, false);
870 } else if (wrt == elf_plt_sect + 1) {
871 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
872 "relative PLT 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 */
880 p = mydata;
881 if (gnu16) {
882 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
883 "16-bit relocations in ELF is a GNU extension");
884 WRITESHORT(p, addr);
885 } else {
886 if (size != 4 && segment != NO_SEG) {
887 nasm_error(ERR_NONFATAL,
888 "Unsupported non-32-bit ELF relocation");
890 WRITELONG(p, addr);
892 elf_sect_write(s, mydata, size);
893 } else if (type == OUT_REL2ADR) {
894 if (segment == segto)
895 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
896 if (segment != NO_SEG && segment % 2) {
897 nasm_error(ERR_NONFATAL, "ELF format does not support"
898 " segment base references");
899 } else {
900 if (wrt == NO_SEG) {
901 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
902 "16-bit relocations in ELF is a GNU extension");
903 elf_add_reloc(s, segment, R_386_PC16);
904 } else {
905 nasm_error(ERR_NONFATAL,
906 "Unsupported non-32-bit ELF relocation");
909 p = mydata;
910 WRITESHORT(p, *(int64_t *)data - size);
911 elf_sect_write(s, mydata, 2L);
912 } else if (type == OUT_REL4ADR) {
913 if (segment == segto)
914 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
915 if (segment != NO_SEG && segment % 2) {
916 nasm_error(ERR_NONFATAL, "ELF format does not support"
917 " segment base references");
918 } else {
919 if (wrt == NO_SEG) {
920 elf_add_reloc(s, segment, R_386_PC32);
921 } else if (wrt == elf_plt_sect + 1) {
922 elf_add_reloc(s, segment, R_386_PLT32);
923 } else if (wrt == elf_gotpc_sect + 1 ||
924 wrt == elf_gotoff_sect + 1 ||
925 wrt == elf_got_sect + 1) {
926 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
927 "relative GOT references");
928 } else {
929 nasm_error(ERR_NONFATAL, "ELF format does not support this"
930 " use of WRT");
931 wrt = NO_SEG; /* we can at least _try_ to continue */
934 p = mydata;
935 WRITELONG(p, *(int64_t *)data - size);
936 elf_sect_write(s, mydata, 4L);
940 static void elf_write(void)
942 int align;
943 char *p;
944 int i;
946 struct SAA *symtab;
947 int32_t symtablen, symtablocal;
950 * Work out how many sections we will have. We have SHN_UNDEF,
951 * then the flexible user sections, then the fixed sections
952 * `.shstrtab', `.symtab' and `.strtab', then optionally
953 * relocation sections for the user sections.
955 nsections = sec_numspecial + 1;
956 if (of_elf32.current_dfmt == &df_stabs)
957 nsections += 3;
958 else if (of_elf32.current_dfmt == &df_dwarf)
959 nsections += 10;
961 add_sectname("", ".shstrtab");
962 add_sectname("", ".symtab");
963 add_sectname("", ".strtab");
964 for (i = 0; i < nsects; i++) {
965 nsections++; /* for the section itself */
966 if (sects[i]->head) {
967 nsections++; /* for its relocations */
968 add_sectname(".rel", sects[i]->name);
972 if (of_elf32.current_dfmt == &df_stabs) {
973 /* in case the debug information is wanted, just add these three sections... */
974 add_sectname("", ".stab");
975 add_sectname("", ".stabstr");
976 add_sectname(".rel", ".stab");
977 } else if (of_elf32.current_dfmt == &df_dwarf) {
978 /* the dwarf debug standard specifies the following ten sections,
979 not all of which are currently implemented,
980 although all of them are defined. */
981 add_sectname("", ".debug_aranges");
982 add_sectname(".rela", ".debug_aranges");
983 add_sectname("", ".debug_pubnames");
984 add_sectname("", ".debug_info");
985 add_sectname(".rela", ".debug_info");
986 add_sectname("", ".debug_abbrev");
987 add_sectname("", ".debug_line");
988 add_sectname(".rela", ".debug_line");
989 add_sectname("", ".debug_frame");
990 add_sectname("", ".debug_loc");
994 * Output the ELF header.
996 fwrite("\177ELF\1\1\1", 7, 1, ofile);
997 fputc(elf_osabi, ofile);
998 fputc(elf_abiver, ofile);
999 fwritezero(7, ofile);
1000 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
1001 fwriteint16_t(3, ofile); /* EM_386 processor ID */
1002 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1003 fwriteint32_t(0L, ofile); /* no entry point */
1004 fwriteint32_t(0L, ofile); /* no program header table */
1005 fwriteint32_t(0x40L, ofile); /* section headers straight after
1006 * ELF header plus alignment */
1007 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1008 fwriteint16_t(0x34, ofile); /* size of ELF header */
1009 fwriteint16_t(0, ofile); /* no program header table, again */
1010 fwriteint16_t(0, ofile); /* still no program header table */
1011 fwriteint16_t(0x28, ofile); /* size of section header */
1012 fwriteint16_t(nsections, ofile); /* number of sections */
1013 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1014 * section header table */
1015 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1016 fwriteint32_t(0L, ofile);
1017 fwriteint32_t(0L, ofile);
1020 * Build the symbol table and relocation tables.
1022 symtab = elf_build_symtab(&symtablen, &symtablocal);
1023 for (i = 0; i < nsects; i++)
1024 if (sects[i]->head)
1025 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1026 sects[i]->head);
1029 * Now output the section header table.
1032 elf_foffs = 0x40 + 0x28 * nsections;
1033 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1034 elf_foffs += align;
1035 elf_nsect = 0;
1036 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1038 /* SHN_UNDEF */
1039 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1040 p = shstrtab + 1;
1042 /* The normal sections */
1043 for (i = 0; i < nsects; i++) {
1044 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1045 (sects[i]->type == SHT_PROGBITS ?
1046 sects[i]->data : NULL), true,
1047 sects[i]->len, 0, 0, sects[i]->align, 0);
1048 p += strlen(p) + 1;
1051 /* .shstrtab */
1052 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1053 shstrtablen, 0, 0, 1, 0);
1054 p += strlen(p) + 1;
1056 /* .symtab */
1057 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1058 symtablen, sec_strtab, symtablocal, 4, 16);
1059 p += strlen(p) + 1;
1061 /* .strtab */
1062 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1063 strslen, 0, 0, 1, 0);
1064 p += strlen(p) + 1;
1066 /* The relocation sections */
1067 for (i = 0; i < nsects; i++)
1068 if (sects[i]->head) {
1069 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1070 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1071 p += strlen(p) + 1;
1074 if (of_elf32.current_dfmt == &df_stabs) {
1075 /* for debugging information, create the last three sections
1076 which are the .stab , .stabstr and .rel.stab sections respectively */
1078 /* this function call creates the stab sections in memory */
1079 stabs32_generate();
1081 if (stabbuf && stabstrbuf && stabrelbuf) {
1082 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1083 stablen, sec_stabstr, 0, 4, 12);
1084 p += strlen(p) + 1;
1086 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1087 stabstrlen, 0, 0, 4, 0);
1088 p += strlen(p) + 1;
1090 /* link -> symtable info -> section to refer to */
1091 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1092 stabrellen, sec_symtab, sec_stab, 4, 8);
1093 p += strlen(p) + 1;
1095 } else if (of_elf32.current_dfmt == &df_dwarf) {
1096 /* for dwarf debugging information, create the ten dwarf sections */
1098 /* this function call creates the dwarf sections in memory */
1099 if (dwarf_fsect)
1100 dwarf32_generate();
1102 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1103 arangeslen, 0, 0, 1, 0);
1104 p += strlen(p) + 1;
1106 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1107 arangesrellen, sec_symtab, sec_debug_aranges,
1108 1, 12);
1109 p += strlen(p) + 1;
1111 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1112 false, pubnameslen, 0, 0, 1, 0);
1113 p += strlen(p) + 1;
1115 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1116 infolen, 0, 0, 1, 0);
1117 p += strlen(p) + 1;
1119 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1120 inforellen, sec_symtab, sec_debug_info, 1, 12);
1121 p += strlen(p) + 1;
1123 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1124 abbrevlen, 0, 0, 1, 0);
1125 p += strlen(p) + 1;
1127 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1128 linelen, 0, 0, 1, 0);
1129 p += strlen(p) + 1;
1131 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1132 linerellen, sec_symtab, sec_debug_line, 1, 12);
1133 p += strlen(p) + 1;
1135 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1136 framelen, 0, 0, 8, 0);
1137 p += strlen(p) + 1;
1139 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1140 loclen, 0, 0, 1, 0);
1141 p += strlen(p) + 1;
1143 fwritezero(align, ofile);
1146 * Now output the sections.
1148 elf_write_sections();
1150 nasm_free(elf_sects);
1151 saa_free(symtab);
1154 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1156 struct SAA *s = saa_init(1L);
1157 struct Symbol *sym;
1158 uint8_t entry[16], *p;
1159 int i;
1161 *len = *local = 0;
1164 * First, an all-zeros entry, required by the ELF spec.
1166 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1167 *len += 16;
1168 (*local)++;
1171 * Next, an entry for the file name.
1173 p = entry;
1174 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1175 WRITELONG(p, 0); /* no value */
1176 WRITELONG(p, 0); /* no size either */
1177 WRITESHORT(p, STT_FILE); /* type FILE */
1178 WRITESHORT(p, SHN_ABS);
1179 saa_wbytes(s, entry, 16L);
1180 *len += 16;
1181 (*local)++;
1184 * Now some standard symbols defining the segments, for relocation
1185 * purposes.
1187 for (i = 1; i <= nsects; i++) {
1188 p = entry;
1189 WRITELONG(p, 0); /* no symbol name */
1190 WRITELONG(p, 0); /* offset zero */
1191 WRITELONG(p, 0); /* size zero */
1192 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1193 WRITESHORT(p, i); /* section id */
1194 saa_wbytes(s, entry, 16L);
1195 *len += 16;
1196 (*local)++;
1200 * Now the other local symbols.
1202 saa_rewind(syms);
1203 while ((sym = saa_rstruct(syms))) {
1204 if (sym->type & SYM_GLOBAL)
1205 continue;
1206 p = entry;
1207 WRITELONG(p, sym->strpos);
1208 WRITELONG(p, sym->symv.key);
1209 WRITELONG(p, sym->size);
1210 WRITECHAR(p, sym->type); /* type and binding */
1211 WRITECHAR(p, sym->other); /* visibility */
1212 WRITESHORT(p, sym->section);
1213 saa_wbytes(s, entry, 16L);
1214 *len += 16;
1215 (*local)++;
1218 * dwarf needs symbols for debug sections
1219 * which are relocation targets.
1221 //*** fix for 32 bit
1222 if (of_elf32.current_dfmt == &df_dwarf) {
1223 dwarf_infosym = *local;
1224 p = entry;
1225 WRITELONG(p, 0); /* no symbol name */
1226 WRITELONG(p, (uint32_t) 0); /* offset zero */
1227 WRITELONG(p, (uint32_t) 0); /* size zero */
1228 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1229 WRITESHORT(p, sec_debug_info); /* section id */
1230 saa_wbytes(s, entry, 16L);
1231 *len += 16;
1232 (*local)++;
1233 dwarf_abbrevsym = *local;
1234 p = entry;
1235 WRITELONG(p, 0); /* no symbol name */
1236 WRITELONG(p, (uint32_t) 0); /* offset zero */
1237 WRITELONG(p, (uint32_t) 0); /* size zero */
1238 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1239 WRITESHORT(p, sec_debug_abbrev); /* section id */
1240 saa_wbytes(s, entry, 16L);
1241 *len += 16;
1242 (*local)++;
1243 dwarf_linesym = *local;
1244 p = entry;
1245 WRITELONG(p, 0); /* no symbol name */
1246 WRITELONG(p, (uint32_t) 0); /* offset zero */
1247 WRITELONG(p, (uint32_t) 0); /* size zero */
1248 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1249 WRITESHORT(p, sec_debug_line); /* section id */
1250 saa_wbytes(s, entry, 16L);
1251 *len += 16;
1252 (*local)++;
1256 * Now the global symbols.
1258 saa_rewind(syms);
1259 while ((sym = saa_rstruct(syms))) {
1260 if (!(sym->type & SYM_GLOBAL))
1261 continue;
1262 p = entry;
1263 WRITELONG(p, sym->strpos);
1264 WRITELONG(p, sym->symv.key);
1265 WRITELONG(p, sym->size);
1266 WRITECHAR(p, sym->type); /* type and binding */
1267 WRITECHAR(p, sym->other); /* visibility */
1268 WRITESHORT(p, sym->section);
1269 saa_wbytes(s, entry, 16L);
1270 *len += 16;
1273 return s;
1276 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1278 struct SAA *s;
1279 uint8_t *p, entry[8];
1280 int32_t global_offset;
1282 if (!r)
1283 return NULL;
1285 s = saa_init(1L);
1286 *len = 0;
1289 * How to onvert from a global placeholder to a real symbol index;
1290 * the +2 refers to the two special entries, the null entry and
1291 * the filename entry.
1293 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1295 while (r) {
1296 int32_t sym = r->symbol;
1299 * Create a real symbol index; the +2 refers to the two special
1300 * entries, the null entry and the filename entry.
1302 if (sym >= GLOBAL_TEMP_BASE)
1303 sym += global_offset;
1305 p = entry;
1306 WRITELONG(p, r->address);
1307 WRITELONG(p, (sym << 8) + r->type);
1308 saa_wbytes(s, entry, 8L);
1309 *len += 8;
1311 r = r->next;
1314 return s;
1317 static void elf_section_header(int name, int type, int flags,
1318 void *data, bool is_saa, int32_t datalen,
1319 int link, int info, int align, int eltsize)
1321 elf_sects[elf_nsect].data = data;
1322 elf_sects[elf_nsect].len = datalen;
1323 elf_sects[elf_nsect].is_saa = is_saa;
1324 elf_nsect++;
1326 fwriteint32_t((int32_t)name, ofile);
1327 fwriteint32_t((int32_t)type, ofile);
1328 fwriteint32_t((int32_t)flags, ofile);
1329 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1330 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1331 fwriteint32_t(datalen, ofile);
1332 if (data)
1333 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1334 fwriteint32_t((int32_t)link, ofile);
1335 fwriteint32_t((int32_t)info, ofile);
1336 fwriteint32_t((int32_t)align, ofile);
1337 fwriteint32_t((int32_t)eltsize, ofile);
1340 static void elf_write_sections(void)
1342 int i;
1343 for (i = 0; i < elf_nsect; i++)
1344 if (elf_sects[i].data) {
1345 int32_t len = elf_sects[i].len;
1346 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1347 int32_t align = reallen - len;
1348 if (elf_sects[i].is_saa)
1349 saa_fpwrite(elf_sects[i].data, ofile);
1350 else
1351 fwrite(elf_sects[i].data, len, 1, ofile);
1352 fwritezero(align, ofile);
1356 static void elf_sect_write(struct Section *sect,
1357 const uint8_t *data, uint32_t len)
1359 saa_wbytes(sect->data, data, len);
1360 sect->len += len;
1363 static int32_t elf_segbase(int32_t segment)
1365 return segment;
1368 static int elf_directive(enum directives directive, char *value, int pass)
1370 bool err;
1371 int64_t n;
1372 char *p;
1374 switch (directive) {
1375 case D_OSABI:
1376 if (pass == 2)
1377 return 1; /* ignore in pass 2 */
1379 n = readnum(value, &err);
1380 if (err) {
1381 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1382 return 1;
1384 if (n < 0 || n > 255) {
1385 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1386 return 1;
1388 elf_osabi = n;
1389 elf_abiver = 0;
1391 if ((p = strchr(value,',')) == NULL)
1392 return 1;
1394 n = readnum(p+1, &err);
1395 if (err || n < 0 || n > 255) {
1396 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1397 return 1;
1400 elf_abiver = n;
1401 return 1;
1403 default:
1404 return 0;
1408 static void elf_filename(char *inname, char *outname)
1410 strcpy(elf_module, inname);
1411 standard_extension(inname, outname, ".o");
1414 extern macros_t elf_stdmac[];
1416 static int elf_set_info(enum geninfo type, char **val)
1418 (void)type;
1419 (void)val;
1420 return 0;
1422 static struct dfmt df_dwarf = {
1423 "ELF32 (i386) dwarf debug format for Linux/Unix",
1424 "dwarf",
1425 dwarf32_init,
1426 dwarf32_linenum,
1427 debug32_deflabel,
1428 debug32_directive,
1429 debug32_typevalue,
1430 dwarf32_output,
1431 dwarf32_cleanup
1433 static struct dfmt df_stabs = {
1434 "ELF32 (i386) stabs debug format for Linux/Unix",
1435 "stabs",
1436 null_debug_init,
1437 stabs32_linenum,
1438 debug32_deflabel,
1439 debug32_directive,
1440 debug32_typevalue,
1441 stabs32_output,
1442 stabs32_cleanup
1445 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1447 struct ofmt of_elf32 = {
1448 "ELF32 (i386) object files (e.g. Linux)",
1449 "elf32",
1451 elf32_debugs_arr,
1452 &df_stabs,
1453 elf_stdmac,
1454 elf_init,
1455 elf_set_info,
1456 elf_out,
1457 elf_deflabel,
1458 elf_section_names,
1459 elf_segbase,
1460 elf_directive,
1461 elf_filename,
1462 elf_cleanup
1465 struct ofmt of_elf = {
1466 "ELF (short name for ELF32) ",
1467 "elf",
1469 elf32_debugs_arr,
1470 &df_stabs,
1471 elf_stdmac,
1472 elf_init_hack,
1473 elf_set_info,
1474 elf_out,
1475 elf_deflabel,
1476 elf_section_names,
1477 elf_segbase,
1478 elf_directive,
1479 elf_filename,
1480 elf_cleanup
1482 /* again, the stabs debugging stuff (code) */
1484 static void stabs32_linenum(const char *filename, int32_t linenumber,
1485 int32_t segto)
1487 (void)segto;
1489 if (!stabs_filename) {
1490 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1491 strcpy(stabs_filename, filename);
1492 } else {
1493 if (strcmp(stabs_filename, filename)) {
1495 * yep, a memory leak...this program is one-shot anyway, so who cares...
1496 * in fact, this leak comes in quite handy to maintain a list of files
1497 * encountered so far in the symbol lines...
1500 /* why not nasm_free(stabs_filename); we're done with the old one */
1502 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1503 strcpy(stabs_filename, filename);
1506 debug_immcall = 1;
1507 currentline = linenumber;
1510 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1511 char *special)
1513 (void)name;
1514 (void)segment;
1515 (void)offset;
1516 (void)is_global;
1517 (void)special;
1520 static void debug32_directive(const char *directive, const char *params)
1522 (void)directive;
1523 (void)params;
1526 static void debug32_typevalue(int32_t type)
1528 int32_t stype, ssize;
1529 switch (TYM_TYPE(type)) {
1530 case TY_LABEL:
1531 ssize = 0;
1532 stype = STT_NOTYPE;
1533 break;
1534 case TY_BYTE:
1535 ssize = 1;
1536 stype = STT_OBJECT;
1537 break;
1538 case TY_WORD:
1539 ssize = 2;
1540 stype = STT_OBJECT;
1541 break;
1542 case TY_DWORD:
1543 ssize = 4;
1544 stype = STT_OBJECT;
1545 break;
1546 case TY_FLOAT:
1547 ssize = 4;
1548 stype = STT_OBJECT;
1549 break;
1550 case TY_QWORD:
1551 ssize = 8;
1552 stype = STT_OBJECT;
1553 break;
1554 case TY_TBYTE:
1555 ssize = 10;
1556 stype = STT_OBJECT;
1557 break;
1558 case TY_OWORD:
1559 ssize = 16;
1560 stype = STT_OBJECT;
1561 break;
1562 case TY_YWORD:
1563 ssize = 32;
1564 stype = STT_OBJECT;
1565 break;
1566 case TY_COMMON:
1567 ssize = 0;
1568 stype = STT_COMMON;
1569 break;
1570 case TY_SEG:
1571 ssize = 0;
1572 stype = STT_SECTION;
1573 break;
1574 case TY_EXTERN:
1575 ssize = 0;
1576 stype = STT_NOTYPE;
1577 break;
1578 case TY_EQU:
1579 ssize = 0;
1580 stype = STT_NOTYPE;
1581 break;
1582 default:
1583 ssize = 0;
1584 stype = STT_NOTYPE;
1585 break;
1587 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1588 lastsym->size = ssize;
1589 lastsym->type = stype;
1593 static void stabs32_output(int type, void *param)
1595 struct symlininfo *s;
1596 struct linelist *el;
1597 if (type == TY_STABSSYMLIN) {
1598 if (debug_immcall) {
1599 s = (struct symlininfo *)param;
1600 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1601 return; /* we are only interested in the text stuff */
1602 numlinestabs++;
1603 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1604 el->info.offset = s->offset;
1605 el->info.section = s->section;
1606 el->info.name = s->name;
1607 el->line = currentline;
1608 el->filename = stabs_filename;
1609 el->next = 0;
1610 if (stabslines) {
1611 stabslines->last->next = el;
1612 stabslines->last = el;
1613 } else {
1614 stabslines = el;
1615 stabslines->last = el;
1619 debug_immcall = 0;
1622 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1623 do { \
1624 WRITELONG(p,n_strx); \
1625 WRITECHAR(p,n_type); \
1626 WRITECHAR(p,n_other); \
1627 WRITESHORT(p,n_desc); \
1628 WRITELONG(p,n_value); \
1629 } while (0)
1631 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1633 static void stabs32_generate(void)
1635 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1636 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1637 char **allfiles;
1638 int *fileidx;
1640 struct linelist *ptr;
1642 ptr = stabslines;
1644 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1645 for (i = 0; i < numlinestabs; i++)
1646 allfiles[i] = 0;
1647 numfiles = 0;
1648 while (ptr) {
1649 if (numfiles == 0) {
1650 allfiles[0] = ptr->filename;
1651 numfiles++;
1652 } else {
1653 for (i = 0; i < numfiles; i++) {
1654 if (!strcmp(allfiles[i], ptr->filename))
1655 break;
1657 if (i >= numfiles) {
1658 allfiles[i] = ptr->filename;
1659 numfiles++;
1662 ptr = ptr->next;
1664 strsize = 1;
1665 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1666 for (i = 0; i < numfiles; i++) {
1667 fileidx[i] = strsize;
1668 strsize += strlen(allfiles[i]) + 1;
1670 mainfileindex = 0;
1671 for (i = 0; i < numfiles; i++) {
1672 if (!strcmp(allfiles[i], elf_module)) {
1673 mainfileindex = i;
1674 break;
1679 * worst case size of the stab buffer would be:
1680 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1681 * plus one "ending" entry
1683 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1684 sizeof(struct stabentry));
1685 ssbuf = (uint8_t *)nasm_malloc(strsize);
1686 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1687 rptr = rbuf;
1689 for (i = 0; i < numfiles; i++)
1690 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1691 ssbuf[0] = 0;
1693 stabstrlen = strsize; /* set global variable for length of stab strings */
1695 sptr = sbuf;
1696 ptr = stabslines;
1697 numstabs = 0;
1699 if (ptr) {
1701 * this is the first stab, its strx points to the filename of the
1702 * the source-file, the n_desc field should be set to the number
1703 * of remaining stabs
1705 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1707 /* this is the stab for the main source file */
1708 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1710 /* relocation table entry */
1713 * Since the symbol table has two entries before
1714 * the section symbols, the index in the info.section
1715 * member must be adjusted by adding 2
1718 WRITELONG(rptr, (sptr - sbuf) - 4);
1719 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1721 numstabs++;
1722 currfile = mainfileindex;
1725 while (ptr) {
1726 if (strcmp(allfiles[currfile], ptr->filename)) {
1727 /* oops file has changed... */
1728 for (i = 0; i < numfiles; i++)
1729 if (!strcmp(allfiles[i], ptr->filename))
1730 break;
1731 currfile = i;
1732 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1733 ptr->info.offset);
1734 numstabs++;
1736 /* relocation table entry */
1737 WRITELONG(rptr, (sptr - sbuf) - 4);
1738 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1741 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1742 numstabs++;
1744 /* relocation table entry */
1746 WRITELONG(rptr, (sptr - sbuf) - 4);
1747 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1749 ptr = ptr->next;
1753 /* this is an "ending" token */
1754 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1755 numstabs++;
1757 ((struct stabentry *)sbuf)->n_desc = numstabs;
1759 nasm_free(allfiles);
1760 nasm_free(fileidx);
1762 stablen = (sptr - sbuf);
1763 stabrellen = (rptr - rbuf);
1764 stabrelbuf = rbuf;
1765 stabbuf = sbuf;
1766 stabstrbuf = ssbuf;
1769 static void stabs32_cleanup(void)
1771 struct linelist *ptr, *del;
1772 if (!stabslines)
1773 return;
1775 ptr = stabslines;
1776 while (ptr) {
1777 del = ptr;
1778 ptr = ptr->next;
1779 nasm_free(del);
1782 nasm_free(stabbuf);
1783 nasm_free(stabrelbuf);
1784 nasm_free(stabstrbuf);
1787 /* dwarf routines */
1789 static void dwarf32_init(void)
1791 ndebugs = 3; /* 3 debug symbols */
1794 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1795 int32_t segto)
1797 (void)segto;
1798 dwarf32_findfile(filename);
1799 debug_immcall = 1;
1800 currentline = linenumber;
1803 /* called from elf_out with type == TY_DEBUGSYMLIN */
1804 static void dwarf32_output(int type, void *param)
1806 int ln, aa, inx, maxln, soc;
1807 struct symlininfo *s;
1808 struct SAA *plinep;
1810 (void)type;
1812 s = (struct symlininfo *)param;
1814 /* line number info is only gathered for executable sections */
1815 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1816 return;
1818 /* Check if section index has changed */
1819 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1820 dwarf32_findsect(s->section);
1822 /* do nothing unless line or file has changed */
1823 if (!debug_immcall)
1824 return;
1826 ln = currentline - dwarf_csect->line;
1827 aa = s->offset - dwarf_csect->offset;
1828 inx = dwarf_clist->line;
1829 plinep = dwarf_csect->psaa;
1830 /* check for file change */
1831 if (!(inx == dwarf_csect->file)) {
1832 saa_write8(plinep,DW_LNS_set_file);
1833 saa_write8(plinep,inx);
1834 dwarf_csect->file = inx;
1836 /* check for line change */
1837 if (ln) {
1838 /* test if in range of special op code */
1839 maxln = line_base + line_range;
1840 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1841 if (ln >= line_base && ln < maxln && soc < 256) {
1842 saa_write8(plinep,soc);
1843 } else {
1844 saa_write8(plinep,DW_LNS_advance_line);
1845 saa_wleb128s(plinep,ln);
1846 if (aa) {
1847 saa_write8(plinep,DW_LNS_advance_pc);
1848 saa_wleb128u(plinep,aa);
1851 dwarf_csect->line = currentline;
1852 dwarf_csect->offset = s->offset;
1855 /* show change handled */
1856 debug_immcall = 0;
1860 static void dwarf32_generate(void)
1862 uint8_t *pbuf;
1863 int indx;
1864 struct linelist *ftentry;
1865 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1866 struct SAA *parangesrel, *plinesrel, *pinforel;
1867 struct sectlist *psect;
1868 size_t saalen, linepoff, totlen, highaddr;
1870 /* write epilogues for each line program range */
1871 /* and build aranges section */
1872 paranges = saa_init(1L);
1873 parangesrel = saa_init(1L);
1874 saa_write16(paranges,2); /* dwarf version */
1875 saa_write32(parangesrel, paranges->datalen+4);
1876 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1877 saa_write32(parangesrel, 0);
1878 saa_write32(paranges,0); /* offset into info */
1879 saa_write8(paranges,4); /* pointer size */
1880 saa_write8(paranges,0); /* not segmented */
1881 saa_write32(paranges,0); /* padding */
1882 /* iterate though sectlist entries */
1883 psect = dwarf_fsect;
1884 totlen = 0;
1885 highaddr = 0;
1886 for (indx = 0; indx < dwarf_nsections; indx++) {
1887 plinep = psect->psaa;
1888 /* Line Number Program Epilogue */
1889 saa_write8(plinep,2); /* std op 2 */
1890 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1891 saa_write8(plinep,DW_LNS_extended_op);
1892 saa_write8(plinep,1); /* operand length */
1893 saa_write8(plinep,DW_LNE_end_sequence);
1894 totlen += plinep->datalen;
1895 /* range table relocation entry */
1896 saa_write32(parangesrel, paranges->datalen + 4);
1897 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1898 saa_write32(parangesrel, (uint32_t) 0);
1899 /* range table entry */
1900 saa_write32(paranges,0x0000); /* range start */
1901 saa_write32(paranges,sects[psect->section]->len); /* range length */
1902 highaddr += sects[psect->section]->len;
1903 /* done with this entry */
1904 psect = psect->next;
1906 saa_write32(paranges,0); /* null address */
1907 saa_write32(paranges,0); /* null length */
1908 saalen = paranges->datalen;
1909 arangeslen = saalen + 4;
1910 arangesbuf = pbuf = nasm_malloc(arangeslen);
1911 WRITELONG(pbuf,saalen); /* initial length */
1912 saa_rnbytes(paranges, pbuf, saalen);
1913 saa_free(paranges);
1915 /* build rela.aranges section */
1916 arangesrellen = saalen = parangesrel->datalen;
1917 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1918 saa_rnbytes(parangesrel, pbuf, saalen);
1919 saa_free(parangesrel);
1921 /* build pubnames section */
1922 ppubnames = saa_init(1L);
1923 saa_write16(ppubnames,3); /* dwarf version */
1924 saa_write32(ppubnames,0); /* offset into info */
1925 saa_write32(ppubnames,0); /* space used in info */
1926 saa_write32(ppubnames,0); /* end of list */
1927 saalen = ppubnames->datalen;
1928 pubnameslen = saalen + 4;
1929 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1930 WRITELONG(pbuf,saalen); /* initial length */
1931 saa_rnbytes(ppubnames, pbuf, saalen);
1932 saa_free(ppubnames);
1934 /* build info section */
1935 pinfo = saa_init(1L);
1936 pinforel = saa_init(1L);
1937 saa_write16(pinfo,2); /* dwarf version */
1938 saa_write32(pinforel, pinfo->datalen + 4);
1939 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1940 saa_write32(pinforel, 0);
1941 saa_write32(pinfo,0); /* offset into abbrev */
1942 saa_write8(pinfo,4); /* pointer size */
1943 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1944 saa_write32(pinforel, pinfo->datalen + 4);
1945 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1946 saa_write32(pinforel, 0);
1947 saa_write32(pinfo,0); /* DW_AT_low_pc */
1948 saa_write32(pinforel, pinfo->datalen + 4);
1949 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1950 saa_write32(pinforel, 0);
1951 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1952 saa_write32(pinforel, pinfo->datalen + 4);
1953 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1954 saa_write32(pinforel, 0);
1955 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1956 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1957 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1958 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1959 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1960 saa_write32(pinforel, pinfo->datalen + 4);
1961 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1962 saa_write32(pinforel, 0);
1963 saa_write32(pinfo,0); /* DW_AT_low_pc */
1964 saa_write32(pinfo,0); /* DW_AT_frame_base */
1965 saa_write8(pinfo,0); /* end of entries */
1966 saalen = pinfo->datalen;
1967 infolen = saalen + 4;
1968 infobuf = pbuf = nasm_malloc(infolen);
1969 WRITELONG(pbuf,saalen); /* initial length */
1970 saa_rnbytes(pinfo, pbuf, saalen);
1971 saa_free(pinfo);
1973 /* build rela.info section */
1974 inforellen = saalen = pinforel->datalen;
1975 inforelbuf = pbuf = nasm_malloc(inforellen);
1976 saa_rnbytes(pinforel, pbuf, saalen);
1977 saa_free(pinforel);
1979 /* build abbrev section */
1980 pabbrev = saa_init(1L);
1981 saa_write8(pabbrev,1); /* entry number LEB128u */
1982 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1983 saa_write8(pabbrev,1); /* has children */
1984 /* the following attributes and forms are all LEB128u values */
1985 saa_write8(pabbrev,DW_AT_low_pc);
1986 saa_write8(pabbrev,DW_FORM_addr);
1987 saa_write8(pabbrev,DW_AT_high_pc);
1988 saa_write8(pabbrev,DW_FORM_addr);
1989 saa_write8(pabbrev,DW_AT_stmt_list);
1990 saa_write8(pabbrev,DW_FORM_data4);
1991 saa_write8(pabbrev,DW_AT_name);
1992 saa_write8(pabbrev,DW_FORM_string);
1993 saa_write8(pabbrev,DW_AT_producer);
1994 saa_write8(pabbrev,DW_FORM_string);
1995 saa_write8(pabbrev,DW_AT_language);
1996 saa_write8(pabbrev,DW_FORM_data2);
1997 saa_write16(pabbrev,0); /* end of entry */
1998 /* LEB128u usage same as above */
1999 saa_write8(pabbrev,2); /* entry number */
2000 saa_write8(pabbrev,DW_TAG_subprogram);
2001 saa_write8(pabbrev,0); /* no children */
2002 saa_write8(pabbrev,DW_AT_low_pc);
2003 saa_write8(pabbrev,DW_FORM_addr);
2004 saa_write8(pabbrev,DW_AT_frame_base);
2005 saa_write8(pabbrev,DW_FORM_data4);
2006 saa_write16(pabbrev,0); /* end of entry */
2007 abbrevlen = saalen = pabbrev->datalen;
2008 abbrevbuf = pbuf = nasm_malloc(saalen);
2009 saa_rnbytes(pabbrev, pbuf, saalen);
2010 saa_free(pabbrev);
2012 /* build line section */
2013 /* prolog */
2014 plines = saa_init(1L);
2015 saa_write8(plines,1); /* Minimum Instruction Length */
2016 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2017 saa_write8(plines,line_base); /* Line Base */
2018 saa_write8(plines,line_range); /* Line Range */
2019 saa_write8(plines,opcode_base); /* Opcode Base */
2020 /* standard opcode lengths (# of LEB128u operands) */
2021 saa_write8(plines,0); /* Std opcode 1 length */
2022 saa_write8(plines,1); /* Std opcode 2 length */
2023 saa_write8(plines,1); /* Std opcode 3 length */
2024 saa_write8(plines,1); /* Std opcode 4 length */
2025 saa_write8(plines,1); /* Std opcode 5 length */
2026 saa_write8(plines,0); /* Std opcode 6 length */
2027 saa_write8(plines,0); /* Std opcode 7 length */
2028 saa_write8(plines,0); /* Std opcode 8 length */
2029 saa_write8(plines,1); /* Std opcode 9 length */
2030 saa_write8(plines,0); /* Std opcode 10 length */
2031 saa_write8(plines,0); /* Std opcode 11 length */
2032 saa_write8(plines,1); /* Std opcode 12 length */
2033 /* Directory Table */
2034 saa_write8(plines,0); /* End of table */
2035 /* File Name Table */
2036 ftentry = dwarf_flist;
2037 for (indx = 0; indx < dwarf_numfiles; indx++) {
2038 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2039 saa_write8(plines,0); /* directory LEB128u */
2040 saa_write8(plines,0); /* time LEB128u */
2041 saa_write8(plines,0); /* size LEB128u */
2042 ftentry = ftentry->next;
2044 saa_write8(plines,0); /* End of table */
2045 linepoff = plines->datalen;
2046 linelen = linepoff + totlen + 10;
2047 linebuf = pbuf = nasm_malloc(linelen);
2048 WRITELONG(pbuf,linelen-4); /* initial length */
2049 WRITESHORT(pbuf,3); /* dwarf version */
2050 WRITELONG(pbuf,linepoff); /* offset to line number program */
2051 /* write line header */
2052 saalen = linepoff;
2053 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2054 pbuf += linepoff;
2055 saa_free(plines);
2056 /* concatonate line program ranges */
2057 linepoff += 13;
2058 plinesrel = saa_init(1L);
2059 psect = dwarf_fsect;
2060 for (indx = 0; indx < dwarf_nsections; indx++) {
2061 saa_write32(plinesrel, linepoff);
2062 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2063 saa_write32(plinesrel, (uint32_t) 0);
2064 plinep = psect->psaa;
2065 saalen = plinep->datalen;
2066 saa_rnbytes(plinep, pbuf, saalen);
2067 pbuf += saalen;
2068 linepoff += saalen;
2069 saa_free(plinep);
2070 /* done with this entry */
2071 psect = psect->next;
2075 /* build rela.lines section */
2076 linerellen =saalen = plinesrel->datalen;
2077 linerelbuf = pbuf = nasm_malloc(linerellen);
2078 saa_rnbytes(plinesrel, pbuf, saalen);
2079 saa_free(plinesrel);
2081 /* build frame section */
2082 framelen = 4;
2083 framebuf = pbuf = nasm_malloc(framelen);
2084 WRITELONG(pbuf,framelen-4); /* initial length */
2086 /* build loc section */
2087 loclen = 16;
2088 locbuf = pbuf = nasm_malloc(loclen);
2089 WRITELONG(pbuf,0); /* null beginning offset */
2090 WRITELONG(pbuf,0); /* null ending offset */
2093 static void dwarf32_cleanup(void)
2095 nasm_free(arangesbuf);
2096 nasm_free(arangesrelbuf);
2097 nasm_free(pubnamesbuf);
2098 nasm_free(infobuf);
2099 nasm_free(inforelbuf);
2100 nasm_free(abbrevbuf);
2101 nasm_free(linebuf);
2102 nasm_free(linerelbuf);
2103 nasm_free(framebuf);
2104 nasm_free(locbuf);
2107 static void dwarf32_findfile(const char * fname)
2109 int finx;
2110 struct linelist *match;
2112 /* return if fname is current file name */
2113 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2114 return;
2116 /* search for match */
2117 match = 0;
2118 if (dwarf_flist) {
2119 match = dwarf_flist;
2120 for (finx = 0; finx < dwarf_numfiles; finx++) {
2121 if (!(strcmp(fname, match->filename))) {
2122 dwarf_clist = match;
2123 return;
2128 /* add file name to end of list */
2129 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2130 dwarf_numfiles++;
2131 dwarf_clist->line = dwarf_numfiles;
2132 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2133 strcpy(dwarf_clist->filename,fname);
2134 dwarf_clist->next = 0;
2135 if (!dwarf_flist) { /* if first entry */
2136 dwarf_flist = dwarf_elist = dwarf_clist;
2137 dwarf_clist->last = 0;
2138 } else { /* chain to previous entry */
2139 dwarf_elist->next = dwarf_clist;
2140 dwarf_elist = dwarf_clist;
2144 static void dwarf32_findsect(const int index)
2146 int sinx;
2147 struct sectlist *match;
2148 struct SAA *plinep;
2150 /* return if index is current section index */
2151 if (dwarf_csect && (dwarf_csect->section == index))
2152 return;
2154 /* search for match */
2155 match = 0;
2156 if (dwarf_fsect) {
2157 match = dwarf_fsect;
2158 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2159 if ((match->section == index)) {
2160 dwarf_csect = match;
2161 return;
2163 match = match->next;
2167 /* add entry to end of list */
2168 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2169 dwarf_nsections++;
2170 dwarf_csect->psaa = plinep = saa_init(1L);
2171 dwarf_csect->line = 1;
2172 dwarf_csect->offset = 0;
2173 dwarf_csect->file = 1;
2174 dwarf_csect->section = index;
2175 dwarf_csect->next = 0;
2176 /* set relocatable address at start of line program */
2177 saa_write8(plinep,DW_LNS_extended_op);
2178 saa_write8(plinep,5); /* operand length */
2179 saa_write8(plinep,DW_LNE_set_address);
2180 saa_write32(plinep,0); /* Start Address */
2182 if (!dwarf_fsect) { /* if first entry */
2183 dwarf_fsect = dwarf_esect = dwarf_csect;
2184 dwarf_csect->last = 0;
2185 } else { /* chain to previous entry */
2186 dwarf_esect->next = dwarf_csect;
2187 dwarf_esect = dwarf_csect;
2191 #endif /* OF_ELF */