insns.dat: Restore default size of memory operands
[nasm/avx512.git] / output / outelf32.c
blob1625fb7d6ee074c028e1e40d09d9356a91e3122a
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/elf.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 : 0;
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 elf_section_names(".text", 2, &tempint);
531 sym->section = nsects;
532 for (i = 0; i < nsects; i++) {
533 if (segment == sects[i]->index) {
534 sym->section = i + 1;
535 break;
538 if (nsects && i == nsects)
539 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
542 if (is_global == 2) {
543 sym->size = offset;
544 sym->symv.key = 0;
545 sym->section = SHN_COMMON;
547 * We have a common variable. Check the special text to see
548 * if it's a valid number and power of two; if so, store it
549 * as the alignment for the common variable.
551 if (special) {
552 bool err;
553 sym->symv.key = readnum(special, &err);
554 if (err)
555 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
556 " valid number", special);
557 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
558 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
559 " power of two", special);
561 special_used = true;
562 } else
563 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
565 if (sym->type == SYM_GLOBAL) {
567 * If sym->section == SHN_ABS, then the first line of the
568 * else section would cause a core dump, because its a reference
569 * beyond the end of the section array.
570 * This behaviour is exhibited by this code:
571 * GLOBAL crash_nasm
572 * crash_nasm equ 0
573 * To avoid such a crash, such requests are silently discarded.
574 * This may not be the best solution.
576 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
577 bsym = raa_write(bsym, segment, nglobs);
578 } else if (sym->section != SHN_ABS) {
580 * This is a global symbol; so we must add it to the rbtree
581 * of global symbols in its section.
583 * In addition, we check the special text for symbol
584 * type and size information.
586 sects[sym->section-1]->gsyms =
587 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
589 if (special) {
590 int n = strcspn(special, " \t");
592 if (!nasm_strnicmp(special, "function", n))
593 sym->type |= STT_FUNC;
594 else if (!nasm_strnicmp(special, "data", n) ||
595 !nasm_strnicmp(special, "object", n))
596 sym->type |= STT_OBJECT;
597 else if (!nasm_strnicmp(special, "notype", n))
598 sym->type |= STT_NOTYPE;
599 else
600 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
601 n, special);
602 special += n;
604 special = nasm_skip_spaces(special);
605 if (*special) {
606 n = strcspn(special, " \t");
607 if (!nasm_strnicmp(special, "default", n))
608 sym->other = STV_DEFAULT;
609 else if (!nasm_strnicmp(special, "internal", n))
610 sym->other = STV_INTERNAL;
611 else if (!nasm_strnicmp(special, "hidden", n))
612 sym->other = STV_HIDDEN;
613 else if (!nasm_strnicmp(special, "protected", n))
614 sym->other = STV_PROTECTED;
615 else
616 n = 0;
617 special += n;
620 if (*special) {
621 struct tokenval tokval;
622 expr *e;
623 int fwd = 0;
624 char *saveme = stdscan_get();
626 while (special[n] && nasm_isspace(special[n]))
627 n++;
629 * We have a size expression; attempt to
630 * evaluate it.
632 stdscan_reset();
633 stdscan_set(special + n);
634 tokval.t_type = TOKEN_INVALID;
635 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
636 NULL);
637 if (fwd) {
638 sym->nextfwd = fwds;
639 fwds = sym;
640 sym->name = nasm_strdup(name);
641 } else if (e) {
642 if (!is_simple(e))
643 nasm_error(ERR_NONFATAL, "cannot use relocatable"
644 " expression as symbol size");
645 else
646 sym->size = reloc_value(e);
648 stdscan_set(saveme);
650 special_used = true;
653 * If TLS segment, mark symbol accordingly.
655 if (sects[sym->section - 1]->flags & SHF_TLS) {
656 sym->type &= 0xf0;
657 sym->type |= STT_TLS;
660 sym->globnum = nglobs;
661 nglobs++;
662 } else
663 nlocals++;
665 if (special && !special_used)
666 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
669 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
671 struct Reloc *r;
673 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
674 sect->tail = &r->next;
675 r->next = NULL;
677 r->address = sect->len;
678 if (segment == NO_SEG)
679 r->symbol = 0;
680 else {
681 int i;
682 r->symbol = 0;
683 for (i = 0; i < nsects; i++)
684 if (segment == sects[i]->index)
685 r->symbol = i + 2;
686 if (!r->symbol)
687 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
689 r->type = type;
691 sect->nrelocs++;
695 * This routine deals with ..got and ..sym relocations: the more
696 * complicated kinds. In shared-library writing, some relocations
697 * with respect to global symbols must refer to the precise symbol
698 * rather than referring to an offset from the base of the section
699 * _containing_ the symbol. Such relocations call to this routine,
700 * which searches the symbol list for the symbol in question.
702 * R_386_GOT32 references require the _exact_ symbol address to be
703 * used; R_386_32 references can be at an offset from the symbol.
704 * The boolean argument `exact' tells us this.
706 * Return value is the adjusted value of `addr', having become an
707 * offset from the symbol rather than the section. Should always be
708 * zero when returning from an exact call.
710 * Limitation: if you define two symbols at the same place,
711 * confusion will occur.
713 * Inefficiency: we search, currently, using a linked list which
714 * isn't even necessarily sorted.
716 static int32_t elf_add_gsym_reloc(struct Section *sect,
717 int32_t segment, uint32_t offset,
718 int type, bool exact)
720 struct Reloc *r;
721 struct Section *s;
722 struct Symbol *sym;
723 struct rbtree *srb;
724 int i;
727 * First look up the segment/offset pair and find a global
728 * symbol corresponding to it. If it's not one of our segments,
729 * then it must be an external symbol, in which case we're fine
730 * doing a normal elf_add_reloc after first sanity-checking
731 * that the offset from the symbol is zero.
733 s = NULL;
734 for (i = 0; i < nsects; i++)
735 if (segment == sects[i]->index) {
736 s = sects[i];
737 break;
739 if (!s) {
740 if (exact && offset != 0)
741 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
742 " for this reference");
743 else
744 elf_add_reloc(sect, segment, type);
745 return offset;
748 srb = rb_search(s->gsyms, offset);
749 if (!srb || (exact && srb->key != offset)) {
750 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
751 " for this reference");
752 return 0;
754 sym = container_of(srb, struct Symbol, symv);
756 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
757 sect->tail = &r->next;
758 r->next = NULL;
760 r->address = sect->len;
761 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
762 r->type = type;
764 sect->nrelocs++;
766 return offset - sym->symv.key;
769 static void elf_out(int32_t segto, const void *data,
770 enum out_type type, uint64_t size,
771 int32_t segment, int32_t wrt)
773 struct Section *s;
774 int32_t addr;
775 uint8_t mydata[4], *p;
776 int i;
777 static struct symlininfo sinfo;
780 * handle absolute-assembly (structure definitions)
782 if (segto == NO_SEG) {
783 if (type != OUT_RESERVE)
784 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
785 " space");
786 return;
789 s = NULL;
790 for (i = 0; i < nsects; i++)
791 if (segto == sects[i]->index) {
792 s = sects[i];
793 break;
795 if (!s) {
796 int tempint; /* ignored */
797 if (segto != elf_section_names(".text", 2, &tempint))
798 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
799 else {
800 s = sects[nsects - 1];
801 i = nsects - 1;
805 /* again some stabs debugging stuff */
806 if (of_elf32.current_dfmt) {
807 sinfo.offset = s->len;
808 sinfo.section = i;
809 sinfo.name = s->name;
810 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
812 /* end of debugging stuff */
814 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
815 nasm_error(ERR_WARNING, "attempt to initialize memory in"
816 " BSS section `%s': ignored", s->name);
817 s->len += realsize(type, size);
818 return;
821 if (type == OUT_RESERVE) {
822 if (s->type == SHT_PROGBITS) {
823 nasm_error(ERR_WARNING, "uninitialized space declared in"
824 " non-BSS section `%s': zeroing", s->name);
825 elf_sect_write(s, NULL, size);
826 } else
827 s->len += size;
828 } else if (type == OUT_RAWDATA) {
829 if (segment != NO_SEG)
830 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
831 elf_sect_write(s, data, size);
832 } else if (type == OUT_ADDRESS) {
833 bool gnu16 = false;
834 addr = *(int64_t *)data;
835 if (segment != NO_SEG) {
836 if (segment % 2) {
837 nasm_error(ERR_NONFATAL, "ELF format does not support"
838 " segment base references");
839 } else {
840 if (wrt == NO_SEG) {
841 if (size == 2) {
842 gnu16 = true;
843 elf_add_reloc(s, segment, R_386_16);
844 } else {
845 elf_add_reloc(s, segment, R_386_32);
847 } else if (wrt == elf_gotpc_sect + 1) {
849 * The user will supply GOT relative to $$. ELF
850 * will let us have GOT relative to $. So we
851 * need to fix up the data item by $-$$.
853 addr += s->len;
854 elf_add_reloc(s, segment, R_386_GOTPC);
855 } else if (wrt == elf_gotoff_sect + 1) {
856 elf_add_reloc(s, segment, R_386_GOTOFF);
857 } else if (wrt == elf_tlsie_sect + 1) {
858 addr = elf_add_gsym_reloc(s, segment, addr,
859 R_386_TLS_IE, true);
860 } else if (wrt == elf_got_sect + 1) {
861 addr = elf_add_gsym_reloc(s, segment, addr,
862 R_386_GOT32, true);
863 } else if (wrt == elf_sym_sect + 1) {
864 if (size == 2) {
865 gnu16 = true;
866 addr = elf_add_gsym_reloc(s, segment, addr,
867 R_386_16, false);
868 } else {
869 addr = elf_add_gsym_reloc(s, segment, addr,
870 R_386_32, false);
872 } else if (wrt == elf_plt_sect + 1) {
873 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
874 "relative PLT references");
875 } else {
876 nasm_error(ERR_NONFATAL, "ELF format does not support this"
877 " use of WRT");
878 wrt = NO_SEG; /* we can at least _try_ to continue */
882 p = mydata;
883 if (gnu16) {
884 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
885 "16-bit relocations in ELF is a GNU extension");
886 WRITESHORT(p, addr);
887 } else {
888 if (size != 4 && segment != NO_SEG) {
889 nasm_error(ERR_NONFATAL,
890 "Unsupported non-32-bit ELF relocation");
892 WRITELONG(p, addr);
894 elf_sect_write(s, mydata, size);
895 } else if (type == OUT_REL2ADR) {
896 if (segment == segto)
897 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
898 if (segment != NO_SEG && segment % 2) {
899 nasm_error(ERR_NONFATAL, "ELF format does not support"
900 " segment base references");
901 } else {
902 if (wrt == NO_SEG) {
903 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
904 "16-bit relocations in ELF is a GNU extension");
905 elf_add_reloc(s, segment, R_386_PC16);
906 } else {
907 nasm_error(ERR_NONFATAL,
908 "Unsupported non-32-bit ELF relocation");
911 p = mydata;
912 WRITESHORT(p, *(int64_t *)data - size);
913 elf_sect_write(s, mydata, 2L);
914 } else if (type == OUT_REL4ADR) {
915 if (segment == segto)
916 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
917 if (segment != NO_SEG && segment % 2) {
918 nasm_error(ERR_NONFATAL, "ELF format does not support"
919 " segment base references");
920 } else {
921 if (wrt == NO_SEG) {
922 elf_add_reloc(s, segment, R_386_PC32);
923 } else if (wrt == elf_plt_sect + 1) {
924 elf_add_reloc(s, segment, R_386_PLT32);
925 } else if (wrt == elf_gotpc_sect + 1 ||
926 wrt == elf_gotoff_sect + 1 ||
927 wrt == elf_got_sect + 1) {
928 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
929 "relative GOT references");
930 } else {
931 nasm_error(ERR_NONFATAL, "ELF format does not support this"
932 " use of WRT");
933 wrt = NO_SEG; /* we can at least _try_ to continue */
936 p = mydata;
937 WRITELONG(p, *(int64_t *)data - size);
938 elf_sect_write(s, mydata, 4L);
942 static void elf_write(void)
944 int align;
945 char *p;
946 int i;
948 struct SAA *symtab;
949 int32_t symtablen, symtablocal;
952 * Work out how many sections we will have. We have SHN_UNDEF,
953 * then the flexible user sections, then the fixed sections
954 * `.shstrtab', `.symtab' and `.strtab', then optionally
955 * relocation sections for the user sections.
957 nsections = sec_numspecial + 1;
958 if (of_elf32.current_dfmt == &df_stabs)
959 nsections += 3;
960 else if (of_elf32.current_dfmt == &df_dwarf)
961 nsections += 10;
963 add_sectname("", ".shstrtab");
964 add_sectname("", ".symtab");
965 add_sectname("", ".strtab");
966 for (i = 0; i < nsects; i++) {
967 nsections++; /* for the section itself */
968 if (sects[i]->head) {
969 nsections++; /* for its relocations */
970 add_sectname(".rel", sects[i]->name);
974 if (of_elf32.current_dfmt == &df_stabs) {
975 /* in case the debug information is wanted, just add these three sections... */
976 add_sectname("", ".stab");
977 add_sectname("", ".stabstr");
978 add_sectname(".rel", ".stab");
979 } else if (of_elf32.current_dfmt == &df_dwarf) {
980 /* the dwarf debug standard specifies the following ten sections,
981 not all of which are currently implemented,
982 although all of them are defined. */
983 add_sectname("", ".debug_aranges");
984 add_sectname(".rela", ".debug_aranges");
985 add_sectname("", ".debug_pubnames");
986 add_sectname("", ".debug_info");
987 add_sectname(".rela", ".debug_info");
988 add_sectname("", ".debug_abbrev");
989 add_sectname("", ".debug_line");
990 add_sectname(".rela", ".debug_line");
991 add_sectname("", ".debug_frame");
992 add_sectname("", ".debug_loc");
996 * Output the ELF header.
998 fwrite("\177ELF\1\1\1", 7, 1, ofile);
999 fputc(elf_osabi, ofile);
1000 fputc(elf_abiver, ofile);
1001 fwritezero(7, ofile);
1002 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
1003 fwriteint16_t(3, ofile); /* EM_386 processor ID */
1004 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1005 fwriteint32_t(0L, ofile); /* no entry point */
1006 fwriteint32_t(0L, ofile); /* no program header table */
1007 fwriteint32_t(0x40L, ofile); /* section headers straight after
1008 * ELF header plus alignment */
1009 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1010 fwriteint16_t(0x34, ofile); /* size of ELF header */
1011 fwriteint16_t(0, ofile); /* no program header table, again */
1012 fwriteint16_t(0, ofile); /* still no program header table */
1013 fwriteint16_t(0x28, ofile); /* size of section header */
1014 fwriteint16_t(nsections, ofile); /* number of sections */
1015 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1016 * section header table */
1017 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1018 fwriteint32_t(0L, ofile);
1019 fwriteint32_t(0L, ofile);
1022 * Build the symbol table and relocation tables.
1024 symtab = elf_build_symtab(&symtablen, &symtablocal);
1025 for (i = 0; i < nsects; i++)
1026 if (sects[i]->head)
1027 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1028 sects[i]->head);
1031 * Now output the section header table.
1034 elf_foffs = 0x40 + 0x28 * nsections;
1035 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1036 elf_foffs += align;
1037 elf_nsect = 0;
1038 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1040 /* SHN_UNDEF */
1041 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1042 p = shstrtab + 1;
1044 /* The normal sections */
1045 for (i = 0; i < nsects; i++) {
1046 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1047 (sects[i]->type == SHT_PROGBITS ?
1048 sects[i]->data : NULL), true,
1049 sects[i]->len, 0, 0, sects[i]->align, 0);
1050 p += strlen(p) + 1;
1053 /* .shstrtab */
1054 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1055 shstrtablen, 0, 0, 1, 0);
1056 p += strlen(p) + 1;
1058 /* .symtab */
1059 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1060 symtablen, sec_strtab, symtablocal, 4, 16);
1061 p += strlen(p) + 1;
1063 /* .strtab */
1064 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1065 strslen, 0, 0, 1, 0);
1066 p += strlen(p) + 1;
1068 /* The relocation sections */
1069 for (i = 0; i < nsects; i++)
1070 if (sects[i]->head) {
1071 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1072 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1073 p += strlen(p) + 1;
1076 if (of_elf32.current_dfmt == &df_stabs) {
1077 /* for debugging information, create the last three sections
1078 which are the .stab , .stabstr and .rel.stab sections respectively */
1080 /* this function call creates the stab sections in memory */
1081 stabs32_generate();
1083 if (stabbuf && stabstrbuf && stabrelbuf) {
1084 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1085 stablen, sec_stabstr, 0, 4, 12);
1086 p += strlen(p) + 1;
1088 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1089 stabstrlen, 0, 0, 4, 0);
1090 p += strlen(p) + 1;
1092 /* link -> symtable info -> section to refer to */
1093 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1094 stabrellen, sec_symtab, sec_stab, 4, 8);
1095 p += strlen(p) + 1;
1097 } else if (of_elf32.current_dfmt == &df_dwarf) {
1098 /* for dwarf debugging information, create the ten dwarf sections */
1100 /* this function call creates the dwarf sections in memory */
1101 if (dwarf_fsect)
1102 dwarf32_generate();
1104 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1105 arangeslen, 0, 0, 1, 0);
1106 p += strlen(p) + 1;
1108 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1109 arangesrellen, sec_symtab, sec_debug_aranges,
1110 1, 12);
1111 p += strlen(p) + 1;
1113 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1114 false, pubnameslen, 0, 0, 1, 0);
1115 p += strlen(p) + 1;
1117 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1118 infolen, 0, 0, 1, 0);
1119 p += strlen(p) + 1;
1121 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1122 inforellen, sec_symtab, sec_debug_info, 1, 12);
1123 p += strlen(p) + 1;
1125 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1126 abbrevlen, 0, 0, 1, 0);
1127 p += strlen(p) + 1;
1129 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1130 linelen, 0, 0, 1, 0);
1131 p += strlen(p) + 1;
1133 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1134 linerellen, sec_symtab, sec_debug_line, 1, 12);
1135 p += strlen(p) + 1;
1137 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1138 framelen, 0, 0, 8, 0);
1139 p += strlen(p) + 1;
1141 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1142 loclen, 0, 0, 1, 0);
1143 p += strlen(p) + 1;
1145 fwritezero(align, ofile);
1148 * Now output the sections.
1150 elf_write_sections();
1152 nasm_free(elf_sects);
1153 saa_free(symtab);
1156 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1158 struct SAA *s = saa_init(1L);
1159 struct Symbol *sym;
1160 uint8_t entry[16], *p;
1161 int i;
1163 *len = *local = 0;
1166 * First, an all-zeros entry, required by the ELF spec.
1168 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1169 *len += 16;
1170 (*local)++;
1173 * Next, an entry for the file name.
1175 p = entry;
1176 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1177 WRITELONG(p, 0); /* no value */
1178 WRITELONG(p, 0); /* no size either */
1179 WRITESHORT(p, STT_FILE); /* type FILE */
1180 WRITESHORT(p, SHN_ABS);
1181 saa_wbytes(s, entry, 16L);
1182 *len += 16;
1183 (*local)++;
1186 * Now some standard symbols defining the segments, for relocation
1187 * purposes.
1189 for (i = 1; i <= nsects; i++) {
1190 p = entry;
1191 WRITELONG(p, 0); /* no symbol name */
1192 WRITELONG(p, 0); /* offset zero */
1193 WRITELONG(p, 0); /* size zero */
1194 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1195 WRITESHORT(p, i); /* section id */
1196 saa_wbytes(s, entry, 16L);
1197 *len += 16;
1198 (*local)++;
1202 * Now the other local symbols.
1204 saa_rewind(syms);
1205 while ((sym = saa_rstruct(syms))) {
1206 if (sym->type & SYM_GLOBAL)
1207 continue;
1208 p = entry;
1209 WRITELONG(p, sym->strpos);
1210 WRITELONG(p, sym->symv.key);
1211 WRITELONG(p, sym->size);
1212 WRITECHAR(p, sym->type); /* type and binding */
1213 WRITECHAR(p, sym->other); /* visibility */
1214 WRITESHORT(p, sym->section);
1215 saa_wbytes(s, entry, 16L);
1216 *len += 16;
1217 (*local)++;
1220 * dwarf needs symbols for debug sections
1221 * which are relocation targets.
1223 //*** fix for 32 bit
1224 if (of_elf32.current_dfmt == &df_dwarf) {
1225 dwarf_infosym = *local;
1226 p = entry;
1227 WRITELONG(p, 0); /* no symbol name */
1228 WRITELONG(p, (uint32_t) 0); /* offset zero */
1229 WRITELONG(p, (uint32_t) 0); /* size zero */
1230 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1231 WRITESHORT(p, sec_debug_info); /* section id */
1232 saa_wbytes(s, entry, 16L);
1233 *len += 16;
1234 (*local)++;
1235 dwarf_abbrevsym = *local;
1236 p = entry;
1237 WRITELONG(p, 0); /* no symbol name */
1238 WRITELONG(p, (uint32_t) 0); /* offset zero */
1239 WRITELONG(p, (uint32_t) 0); /* size zero */
1240 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1241 WRITESHORT(p, sec_debug_abbrev); /* section id */
1242 saa_wbytes(s, entry, 16L);
1243 *len += 16;
1244 (*local)++;
1245 dwarf_linesym = *local;
1246 p = entry;
1247 WRITELONG(p, 0); /* no symbol name */
1248 WRITELONG(p, (uint32_t) 0); /* offset zero */
1249 WRITELONG(p, (uint32_t) 0); /* size zero */
1250 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1251 WRITESHORT(p, sec_debug_line); /* section id */
1252 saa_wbytes(s, entry, 16L);
1253 *len += 16;
1254 (*local)++;
1258 * Now the global symbols.
1260 saa_rewind(syms);
1261 while ((sym = saa_rstruct(syms))) {
1262 if (!(sym->type & SYM_GLOBAL))
1263 continue;
1264 p = entry;
1265 WRITELONG(p, sym->strpos);
1266 WRITELONG(p, sym->symv.key);
1267 WRITELONG(p, sym->size);
1268 WRITECHAR(p, sym->type); /* type and binding */
1269 WRITECHAR(p, sym->other); /* visibility */
1270 WRITESHORT(p, sym->section);
1271 saa_wbytes(s, entry, 16L);
1272 *len += 16;
1275 return s;
1278 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1280 struct SAA *s;
1281 uint8_t *p, entry[8];
1282 int32_t global_offset;
1284 if (!r)
1285 return NULL;
1287 s = saa_init(1L);
1288 *len = 0;
1291 * How to onvert from a global placeholder to a real symbol index;
1292 * the +2 refers to the two special entries, the null entry and
1293 * the filename entry.
1295 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1297 while (r) {
1298 int32_t sym = r->symbol;
1301 * Create a real symbol index; the +2 refers to the two special
1302 * entries, the null entry and the filename entry.
1304 if (sym >= GLOBAL_TEMP_BASE)
1305 sym += global_offset;
1307 p = entry;
1308 WRITELONG(p, r->address);
1309 WRITELONG(p, (sym << 8) + r->type);
1310 saa_wbytes(s, entry, 8L);
1311 *len += 8;
1313 r = r->next;
1316 return s;
1319 static void elf_section_header(int name, int type, int flags,
1320 void *data, bool is_saa, int32_t datalen,
1321 int link, int info, int align, int eltsize)
1323 elf_sects[elf_nsect].data = data;
1324 elf_sects[elf_nsect].len = datalen;
1325 elf_sects[elf_nsect].is_saa = is_saa;
1326 elf_nsect++;
1328 fwriteint32_t((int32_t)name, ofile);
1329 fwriteint32_t((int32_t)type, ofile);
1330 fwriteint32_t((int32_t)flags, ofile);
1331 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1332 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1333 fwriteint32_t(datalen, ofile);
1334 if (data)
1335 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1336 fwriteint32_t((int32_t)link, ofile);
1337 fwriteint32_t((int32_t)info, ofile);
1338 fwriteint32_t((int32_t)align, ofile);
1339 fwriteint32_t((int32_t)eltsize, ofile);
1342 static void elf_write_sections(void)
1344 int i;
1345 for (i = 0; i < elf_nsect; i++)
1346 if (elf_sects[i].data) {
1347 int32_t len = elf_sects[i].len;
1348 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1349 int32_t align = reallen - len;
1350 if (elf_sects[i].is_saa)
1351 saa_fpwrite(elf_sects[i].data, ofile);
1352 else
1353 fwrite(elf_sects[i].data, len, 1, ofile);
1354 fwritezero(align, ofile);
1358 static void elf_sect_write(struct Section *sect,
1359 const uint8_t *data, uint32_t len)
1361 saa_wbytes(sect->data, data, len);
1362 sect->len += len;
1365 static int32_t elf_segbase(int32_t segment)
1367 return segment;
1370 static int elf_directive(enum directives directive, char *value, int pass)
1372 bool err;
1373 int64_t n;
1374 char *p;
1376 switch (directive) {
1377 case D_OSABI:
1378 if (pass == 2)
1379 return 1; /* ignore in pass 2 */
1381 n = readnum(value, &err);
1382 if (err) {
1383 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1384 return 1;
1386 if (n < 0 || n > 255) {
1387 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1388 return 1;
1390 elf_osabi = n;
1391 elf_abiver = 0;
1393 if ((p = strchr(value,',')) == NULL)
1394 return 1;
1396 n = readnum(p+1, &err);
1397 if (err || n < 0 || n > 255) {
1398 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1399 return 1;
1402 elf_abiver = n;
1403 return 1;
1405 default:
1406 return 0;
1410 static void elf_filename(char *inname, char *outname)
1412 strcpy(elf_module, inname);
1413 standard_extension(inname, outname, ".o");
1416 extern macros_t elf_stdmac[];
1418 static int elf_set_info(enum geninfo type, char **val)
1420 (void)type;
1421 (void)val;
1422 return 0;
1424 static struct dfmt df_dwarf = {
1425 "ELF32 (i386) dwarf debug format for Linux/Unix",
1426 "dwarf",
1427 dwarf32_init,
1428 dwarf32_linenum,
1429 debug32_deflabel,
1430 debug32_directive,
1431 debug32_typevalue,
1432 dwarf32_output,
1433 dwarf32_cleanup
1435 static struct dfmt df_stabs = {
1436 "ELF32 (i386) stabs debug format for Linux/Unix",
1437 "stabs",
1438 null_debug_init,
1439 stabs32_linenum,
1440 debug32_deflabel,
1441 debug32_directive,
1442 debug32_typevalue,
1443 stabs32_output,
1444 stabs32_cleanup
1447 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1449 struct ofmt of_elf32 = {
1450 "ELF32 (i386) object files (e.g. Linux)",
1451 "elf32",
1453 elf32_debugs_arr,
1454 &df_stabs,
1455 elf_stdmac,
1456 elf_init,
1457 elf_set_info,
1458 elf_out,
1459 elf_deflabel,
1460 elf_section_names,
1461 elf_segbase,
1462 elf_directive,
1463 elf_filename,
1464 elf_cleanup
1467 struct ofmt of_elf = {
1468 "ELF (short name for ELF32) ",
1469 "elf",
1471 elf32_debugs_arr,
1472 &df_stabs,
1473 elf_stdmac,
1474 elf_init_hack,
1475 elf_set_info,
1476 elf_out,
1477 elf_deflabel,
1478 elf_section_names,
1479 elf_segbase,
1480 elf_directive,
1481 elf_filename,
1482 elf_cleanup
1484 /* again, the stabs debugging stuff (code) */
1486 static void stabs32_linenum(const char *filename, int32_t linenumber,
1487 int32_t segto)
1489 (void)segto;
1491 if (!stabs_filename) {
1492 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1493 strcpy(stabs_filename, filename);
1494 } else {
1495 if (strcmp(stabs_filename, filename)) {
1497 * yep, a memory leak...this program is one-shot anyway, so who cares...
1498 * in fact, this leak comes in quite handy to maintain a list of files
1499 * encountered so far in the symbol lines...
1502 /* why not nasm_free(stabs_filename); we're done with the old one */
1504 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1505 strcpy(stabs_filename, filename);
1508 debug_immcall = 1;
1509 currentline = linenumber;
1512 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1513 char *special)
1515 (void)name;
1516 (void)segment;
1517 (void)offset;
1518 (void)is_global;
1519 (void)special;
1522 static void debug32_directive(const char *directive, const char *params)
1524 (void)directive;
1525 (void)params;
1528 static void debug32_typevalue(int32_t type)
1530 int32_t stype, ssize;
1531 switch (TYM_TYPE(type)) {
1532 case TY_LABEL:
1533 ssize = 0;
1534 stype = STT_NOTYPE;
1535 break;
1536 case TY_BYTE:
1537 ssize = 1;
1538 stype = STT_OBJECT;
1539 break;
1540 case TY_WORD:
1541 ssize = 2;
1542 stype = STT_OBJECT;
1543 break;
1544 case TY_DWORD:
1545 ssize = 4;
1546 stype = STT_OBJECT;
1547 break;
1548 case TY_FLOAT:
1549 ssize = 4;
1550 stype = STT_OBJECT;
1551 break;
1552 case TY_QWORD:
1553 ssize = 8;
1554 stype = STT_OBJECT;
1555 break;
1556 case TY_TBYTE:
1557 ssize = 10;
1558 stype = STT_OBJECT;
1559 break;
1560 case TY_OWORD:
1561 ssize = 16;
1562 stype = STT_OBJECT;
1563 break;
1564 case TY_YWORD:
1565 ssize = 32;
1566 stype = STT_OBJECT;
1567 break;
1568 case TY_COMMON:
1569 ssize = 0;
1570 stype = STT_COMMON;
1571 break;
1572 case TY_SEG:
1573 ssize = 0;
1574 stype = STT_SECTION;
1575 break;
1576 case TY_EXTERN:
1577 ssize = 0;
1578 stype = STT_NOTYPE;
1579 break;
1580 case TY_EQU:
1581 ssize = 0;
1582 stype = STT_NOTYPE;
1583 break;
1584 default:
1585 ssize = 0;
1586 stype = STT_NOTYPE;
1587 break;
1589 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1590 lastsym->size = ssize;
1591 lastsym->type = stype;
1595 static void stabs32_output(int type, void *param)
1597 struct symlininfo *s;
1598 struct linelist *el;
1599 if (type == TY_STABSSYMLIN) {
1600 if (debug_immcall) {
1601 s = (struct symlininfo *)param;
1602 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1603 return; /* we are only interested in the text stuff */
1604 numlinestabs++;
1605 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1606 el->info.offset = s->offset;
1607 el->info.section = s->section;
1608 el->info.name = s->name;
1609 el->line = currentline;
1610 el->filename = stabs_filename;
1611 el->next = 0;
1612 if (stabslines) {
1613 stabslines->last->next = el;
1614 stabslines->last = el;
1615 } else {
1616 stabslines = el;
1617 stabslines->last = el;
1621 debug_immcall = 0;
1624 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1625 do { \
1626 WRITELONG(p,n_strx); \
1627 WRITECHAR(p,n_type); \
1628 WRITECHAR(p,n_other); \
1629 WRITESHORT(p,n_desc); \
1630 WRITELONG(p,n_value); \
1631 } while (0)
1633 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1635 static void stabs32_generate(void)
1637 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1638 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1639 char **allfiles;
1640 int *fileidx;
1642 struct linelist *ptr;
1644 ptr = stabslines;
1646 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1647 for (i = 0; i < numlinestabs; i++)
1648 allfiles[i] = 0;
1649 numfiles = 0;
1650 while (ptr) {
1651 if (numfiles == 0) {
1652 allfiles[0] = ptr->filename;
1653 numfiles++;
1654 } else {
1655 for (i = 0; i < numfiles; i++) {
1656 if (!strcmp(allfiles[i], ptr->filename))
1657 break;
1659 if (i >= numfiles) {
1660 allfiles[i] = ptr->filename;
1661 numfiles++;
1664 ptr = ptr->next;
1666 strsize = 1;
1667 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1668 for (i = 0; i < numfiles; i++) {
1669 fileidx[i] = strsize;
1670 strsize += strlen(allfiles[i]) + 1;
1672 mainfileindex = 0;
1673 for (i = 0; i < numfiles; i++) {
1674 if (!strcmp(allfiles[i], elf_module)) {
1675 mainfileindex = i;
1676 break;
1681 * worst case size of the stab buffer would be:
1682 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1683 * plus one "ending" entry
1685 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1686 sizeof(struct stabentry));
1687 ssbuf = (uint8_t *)nasm_malloc(strsize);
1688 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1689 rptr = rbuf;
1691 for (i = 0; i < numfiles; i++)
1692 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1693 ssbuf[0] = 0;
1695 stabstrlen = strsize; /* set global variable for length of stab strings */
1697 sptr = sbuf;
1698 ptr = stabslines;
1699 numstabs = 0;
1701 if (ptr) {
1703 * this is the first stab, its strx points to the filename of the
1704 * the source-file, the n_desc field should be set to the number
1705 * of remaining stabs
1707 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1709 /* this is the stab for the main source file */
1710 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1712 /* relocation table entry */
1715 * Since the symbol table has two entries before
1716 * the section symbols, the index in the info.section
1717 * member must be adjusted by adding 2
1720 WRITELONG(rptr, (sptr - sbuf) - 4);
1721 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1723 numstabs++;
1724 currfile = mainfileindex;
1727 while (ptr) {
1728 if (strcmp(allfiles[currfile], ptr->filename)) {
1729 /* oops file has changed... */
1730 for (i = 0; i < numfiles; i++)
1731 if (!strcmp(allfiles[i], ptr->filename))
1732 break;
1733 currfile = i;
1734 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1735 ptr->info.offset);
1736 numstabs++;
1738 /* relocation table entry */
1739 WRITELONG(rptr, (sptr - sbuf) - 4);
1740 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1743 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1744 numstabs++;
1746 /* relocation table entry */
1748 WRITELONG(rptr, (sptr - sbuf) - 4);
1749 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1751 ptr = ptr->next;
1755 /* this is an "ending" token */
1756 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1757 numstabs++;
1759 ((struct stabentry *)sbuf)->n_desc = numstabs;
1761 nasm_free(allfiles);
1762 nasm_free(fileidx);
1764 stablen = (sptr - sbuf);
1765 stabrellen = (rptr - rbuf);
1766 stabrelbuf = rbuf;
1767 stabbuf = sbuf;
1768 stabstrbuf = ssbuf;
1771 static void stabs32_cleanup(void)
1773 struct linelist *ptr, *del;
1774 if (!stabslines)
1775 return;
1777 ptr = stabslines;
1778 while (ptr) {
1779 del = ptr;
1780 ptr = ptr->next;
1781 nasm_free(del);
1784 nasm_free(stabbuf);
1785 nasm_free(stabrelbuf);
1786 nasm_free(stabstrbuf);
1789 /* dwarf routines */
1791 static void dwarf32_init(void)
1793 ndebugs = 3; /* 3 debug symbols */
1796 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1797 int32_t segto)
1799 (void)segto;
1800 dwarf32_findfile(filename);
1801 debug_immcall = 1;
1802 currentline = linenumber;
1805 /* called from elf_out with type == TY_DEBUGSYMLIN */
1806 static void dwarf32_output(int type, void *param)
1808 int ln, aa, inx, maxln, soc;
1809 struct symlininfo *s;
1810 struct SAA *plinep;
1812 (void)type;
1814 s = (struct symlininfo *)param;
1816 /* line number info is only gathered for executable sections */
1817 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1818 return;
1820 /* Check if section index has changed */
1821 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1822 dwarf32_findsect(s->section);
1824 /* do nothing unless line or file has changed */
1825 if (!debug_immcall)
1826 return;
1828 ln = currentline - dwarf_csect->line;
1829 aa = s->offset - dwarf_csect->offset;
1830 inx = dwarf_clist->line;
1831 plinep = dwarf_csect->psaa;
1832 /* check for file change */
1833 if (!(inx == dwarf_csect->file)) {
1834 saa_write8(plinep,DW_LNS_set_file);
1835 saa_write8(plinep,inx);
1836 dwarf_csect->file = inx;
1838 /* check for line change */
1839 if (ln) {
1840 /* test if in range of special op code */
1841 maxln = line_base + line_range;
1842 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1843 if (ln >= line_base && ln < maxln && soc < 256) {
1844 saa_write8(plinep,soc);
1845 } else {
1846 saa_write8(plinep,DW_LNS_advance_line);
1847 saa_wleb128s(plinep,ln);
1848 if (aa) {
1849 saa_write8(plinep,DW_LNS_advance_pc);
1850 saa_wleb128u(plinep,aa);
1853 dwarf_csect->line = currentline;
1854 dwarf_csect->offset = s->offset;
1857 /* show change handled */
1858 debug_immcall = 0;
1862 static void dwarf32_generate(void)
1864 uint8_t *pbuf;
1865 int indx;
1866 struct linelist *ftentry;
1867 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1868 struct SAA *parangesrel, *plinesrel, *pinforel;
1869 struct sectlist *psect;
1870 size_t saalen, linepoff, totlen, highaddr;
1872 /* write epilogues for each line program range */
1873 /* and build aranges section */
1874 paranges = saa_init(1L);
1875 parangesrel = saa_init(1L);
1876 saa_write16(paranges,2); /* dwarf version */
1877 saa_write32(parangesrel, paranges->datalen+4);
1878 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1879 saa_write32(parangesrel, 0);
1880 saa_write32(paranges,0); /* offset into info */
1881 saa_write8(paranges,4); /* pointer size */
1882 saa_write8(paranges,0); /* not segmented */
1883 saa_write32(paranges,0); /* padding */
1884 /* iterate though sectlist entries */
1885 psect = dwarf_fsect;
1886 totlen = 0;
1887 highaddr = 0;
1888 for (indx = 0; indx < dwarf_nsections; indx++) {
1889 plinep = psect->psaa;
1890 /* Line Number Program Epilogue */
1891 saa_write8(plinep,2); /* std op 2 */
1892 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1893 saa_write8(plinep,DW_LNS_extended_op);
1894 saa_write8(plinep,1); /* operand length */
1895 saa_write8(plinep,DW_LNE_end_sequence);
1896 totlen += plinep->datalen;
1897 /* range table relocation entry */
1898 saa_write32(parangesrel, paranges->datalen + 4);
1899 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1900 saa_write32(parangesrel, (uint32_t) 0);
1901 /* range table entry */
1902 saa_write32(paranges,0x0000); /* range start */
1903 saa_write32(paranges,sects[psect->section]->len); /* range length */
1904 highaddr += sects[psect->section]->len;
1905 /* done with this entry */
1906 psect = psect->next;
1908 saa_write32(paranges,0); /* null address */
1909 saa_write32(paranges,0); /* null length */
1910 saalen = paranges->datalen;
1911 arangeslen = saalen + 4;
1912 arangesbuf = pbuf = nasm_malloc(arangeslen);
1913 WRITELONG(pbuf,saalen); /* initial length */
1914 saa_rnbytes(paranges, pbuf, saalen);
1915 saa_free(paranges);
1917 /* build rela.aranges section */
1918 arangesrellen = saalen = parangesrel->datalen;
1919 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1920 saa_rnbytes(parangesrel, pbuf, saalen);
1921 saa_free(parangesrel);
1923 /* build pubnames section */
1924 ppubnames = saa_init(1L);
1925 saa_write16(ppubnames,3); /* dwarf version */
1926 saa_write32(ppubnames,0); /* offset into info */
1927 saa_write32(ppubnames,0); /* space used in info */
1928 saa_write32(ppubnames,0); /* end of list */
1929 saalen = ppubnames->datalen;
1930 pubnameslen = saalen + 4;
1931 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1932 WRITELONG(pbuf,saalen); /* initial length */
1933 saa_rnbytes(ppubnames, pbuf, saalen);
1934 saa_free(ppubnames);
1936 /* build info section */
1937 pinfo = saa_init(1L);
1938 pinforel = saa_init(1L);
1939 saa_write16(pinfo,2); /* dwarf version */
1940 saa_write32(pinforel, pinfo->datalen + 4);
1941 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1942 saa_write32(pinforel, 0);
1943 saa_write32(pinfo,0); /* offset into abbrev */
1944 saa_write8(pinfo,4); /* pointer size */
1945 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1946 saa_write32(pinforel, pinfo->datalen + 4);
1947 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1948 saa_write32(pinforel, 0);
1949 saa_write32(pinfo,0); /* DW_AT_low_pc */
1950 saa_write32(pinforel, pinfo->datalen + 4);
1951 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1952 saa_write32(pinforel, 0);
1953 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1954 saa_write32(pinforel, pinfo->datalen + 4);
1955 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1956 saa_write32(pinforel, 0);
1957 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1958 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1959 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1960 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1961 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1962 saa_write32(pinforel, pinfo->datalen + 4);
1963 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1964 saa_write32(pinforel, 0);
1965 saa_write32(pinfo,0); /* DW_AT_low_pc */
1966 saa_write32(pinfo,0); /* DW_AT_frame_base */
1967 saa_write8(pinfo,0); /* end of entries */
1968 saalen = pinfo->datalen;
1969 infolen = saalen + 4;
1970 infobuf = pbuf = nasm_malloc(infolen);
1971 WRITELONG(pbuf,saalen); /* initial length */
1972 saa_rnbytes(pinfo, pbuf, saalen);
1973 saa_free(pinfo);
1975 /* build rela.info section */
1976 inforellen = saalen = pinforel->datalen;
1977 inforelbuf = pbuf = nasm_malloc(inforellen);
1978 saa_rnbytes(pinforel, pbuf, saalen);
1979 saa_free(pinforel);
1981 /* build abbrev section */
1982 pabbrev = saa_init(1L);
1983 saa_write8(pabbrev,1); /* entry number LEB128u */
1984 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1985 saa_write8(pabbrev,1); /* has children */
1986 /* the following attributes and forms are all LEB128u values */
1987 saa_write8(pabbrev,DW_AT_low_pc);
1988 saa_write8(pabbrev,DW_FORM_addr);
1989 saa_write8(pabbrev,DW_AT_high_pc);
1990 saa_write8(pabbrev,DW_FORM_addr);
1991 saa_write8(pabbrev,DW_AT_stmt_list);
1992 saa_write8(pabbrev,DW_FORM_data4);
1993 saa_write8(pabbrev,DW_AT_name);
1994 saa_write8(pabbrev,DW_FORM_string);
1995 saa_write8(pabbrev,DW_AT_producer);
1996 saa_write8(pabbrev,DW_FORM_string);
1997 saa_write8(pabbrev,DW_AT_language);
1998 saa_write8(pabbrev,DW_FORM_data2);
1999 saa_write16(pabbrev,0); /* end of entry */
2000 /* LEB128u usage same as above */
2001 saa_write8(pabbrev,2); /* entry number */
2002 saa_write8(pabbrev,DW_TAG_subprogram);
2003 saa_write8(pabbrev,0); /* no children */
2004 saa_write8(pabbrev,DW_AT_low_pc);
2005 saa_write8(pabbrev,DW_FORM_addr);
2006 saa_write8(pabbrev,DW_AT_frame_base);
2007 saa_write8(pabbrev,DW_FORM_data4);
2008 saa_write16(pabbrev,0); /* end of entry */
2009 abbrevlen = saalen = pabbrev->datalen;
2010 abbrevbuf = pbuf = nasm_malloc(saalen);
2011 saa_rnbytes(pabbrev, pbuf, saalen);
2012 saa_free(pabbrev);
2014 /* build line section */
2015 /* prolog */
2016 plines = saa_init(1L);
2017 saa_write8(plines,1); /* Minimum Instruction Length */
2018 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2019 saa_write8(plines,line_base); /* Line Base */
2020 saa_write8(plines,line_range); /* Line Range */
2021 saa_write8(plines,opcode_base); /* Opcode Base */
2022 /* standard opcode lengths (# of LEB128u operands) */
2023 saa_write8(plines,0); /* Std opcode 1 length */
2024 saa_write8(plines,1); /* Std opcode 2 length */
2025 saa_write8(plines,1); /* Std opcode 3 length */
2026 saa_write8(plines,1); /* Std opcode 4 length */
2027 saa_write8(plines,1); /* Std opcode 5 length */
2028 saa_write8(plines,0); /* Std opcode 6 length */
2029 saa_write8(plines,0); /* Std opcode 7 length */
2030 saa_write8(plines,0); /* Std opcode 8 length */
2031 saa_write8(plines,1); /* Std opcode 9 length */
2032 saa_write8(plines,0); /* Std opcode 10 length */
2033 saa_write8(plines,0); /* Std opcode 11 length */
2034 saa_write8(plines,1); /* Std opcode 12 length */
2035 /* Directory Table */
2036 saa_write8(plines,0); /* End of table */
2037 /* File Name Table */
2038 ftentry = dwarf_flist;
2039 for (indx = 0;indx<dwarf_numfiles;indx++) {
2040 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2041 saa_write8(plines,0); /* directory LEB128u */
2042 saa_write8(plines,0); /* time LEB128u */
2043 saa_write8(plines,0); /* size LEB128u */
2044 ftentry = ftentry->next;
2046 saa_write8(plines,0); /* End of table */
2047 linepoff = plines->datalen;
2048 linelen = linepoff + totlen + 10;
2049 linebuf = pbuf = nasm_malloc(linelen);
2050 WRITELONG(pbuf,linelen-4); /* initial length */
2051 WRITESHORT(pbuf,3); /* dwarf version */
2052 WRITELONG(pbuf,linepoff); /* offset to line number program */
2053 /* write line header */
2054 saalen = linepoff;
2055 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2056 pbuf += linepoff;
2057 saa_free(plines);
2058 /* concatonate line program ranges */
2059 linepoff += 13;
2060 plinesrel = saa_init(1L);
2061 psect = dwarf_fsect;
2062 for (indx = 0; indx < dwarf_nsections; indx++) {
2063 saa_write32(plinesrel, linepoff);
2064 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2065 saa_write32(plinesrel, (uint32_t) 0);
2066 plinep = psect->psaa;
2067 saalen = plinep->datalen;
2068 saa_rnbytes(plinep, pbuf, saalen);
2069 pbuf += saalen;
2070 linepoff += saalen;
2071 saa_free(plinep);
2072 /* done with this entry */
2073 psect = psect->next;
2077 /* build rela.lines section */
2078 linerellen =saalen = plinesrel->datalen;
2079 linerelbuf = pbuf = nasm_malloc(linerellen);
2080 saa_rnbytes(plinesrel, pbuf, saalen);
2081 saa_free(plinesrel);
2083 /* build frame section */
2084 framelen = 4;
2085 framebuf = pbuf = nasm_malloc(framelen);
2086 WRITELONG(pbuf,framelen-4); /* initial length */
2088 /* build loc section */
2089 loclen = 16;
2090 locbuf = pbuf = nasm_malloc(loclen);
2091 WRITELONG(pbuf,0); /* null beginning offset */
2092 WRITELONG(pbuf,0); /* null ending offset */
2095 static void dwarf32_cleanup(void)
2097 nasm_free(arangesbuf);
2098 nasm_free(arangesrelbuf);
2099 nasm_free(pubnamesbuf);
2100 nasm_free(infobuf);
2101 nasm_free(inforelbuf);
2102 nasm_free(abbrevbuf);
2103 nasm_free(linebuf);
2104 nasm_free(linerelbuf);
2105 nasm_free(framebuf);
2106 nasm_free(locbuf);
2109 static void dwarf32_findfile(const char * fname)
2111 int finx;
2112 struct linelist *match;
2114 /* return if fname is current file name */
2115 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2116 return;
2118 /* search for match */
2119 match = 0;
2120 if (dwarf_flist) {
2121 match = dwarf_flist;
2122 for (finx = 0; finx < dwarf_numfiles; finx++) {
2123 if (!(strcmp(fname, match->filename))) {
2124 dwarf_clist = match;
2125 return;
2130 /* add file name to end of list */
2131 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2132 dwarf_numfiles++;
2133 dwarf_clist->line = dwarf_numfiles;
2134 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2135 strcpy(dwarf_clist->filename,fname);
2136 dwarf_clist->next = 0;
2137 if (!dwarf_flist) { /* if first entry */
2138 dwarf_flist = dwarf_elist = dwarf_clist;
2139 dwarf_clist->last = 0;
2140 } else { /* chain to previous entry */
2141 dwarf_elist->next = dwarf_clist;
2142 dwarf_elist = dwarf_clist;
2146 static void dwarf32_findsect(const int index)
2148 int sinx;
2149 struct sectlist *match;
2150 struct SAA *plinep;
2152 /* return if index is current section index */
2153 if (dwarf_csect && (dwarf_csect->section == index))
2154 return;
2156 /* search for match */
2157 match = 0;
2158 if (dwarf_fsect) {
2159 match = dwarf_fsect;
2160 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2161 if ((match->section == index)) {
2162 dwarf_csect = match;
2163 return;
2165 match = match->next;
2169 /* add entry to end of list */
2170 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2171 dwarf_nsections++;
2172 dwarf_csect->psaa = plinep = saa_init(1L);
2173 dwarf_csect->line = 1;
2174 dwarf_csect->offset = 0;
2175 dwarf_csect->file = 1;
2176 dwarf_csect->section = index;
2177 dwarf_csect->next = 0;
2178 /* set relocatable address at start of line program */
2179 saa_write8(plinep,DW_LNS_extended_op);
2180 saa_write8(plinep,5); /* operand length */
2181 saa_write8(plinep,DW_LNE_set_address);
2182 saa_write32(plinep,0); /* Start Address */
2184 if (!dwarf_fsect) { /* if first entry */
2185 dwarf_fsect = dwarf_esect = dwarf_csect;
2186 dwarf_csect->last = 0;
2187 } else { /* chain to previous entry */
2188 dwarf_esect->next = dwarf_csect;
2189 dwarf_esect = dwarf_csect;
2193 #endif /* OF_ELF */