BR3385573: insns: Fix VPMOVSXBW
[nasm/sigaren-mirror.git] / output / outelfx32.c
blob914a83e861fa663de49b34156dfc37e0e07fd2ac
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2012 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 * outelfx32.c output routines for the Netwide Assembler to produce
36 * ELF32 (x86_64) 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_ELFX32
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 int32_t offset; /* symbol addend */
71 int type; /* type of relocation */
74 struct Symbol {
75 struct rbtree symv; /* symbol value and rbtree of globals */
76 int32_t strpos; /* string table position of name */
77 int32_t section; /* section ID of the symbol */
78 int type; /* symbol type */
79 int other; /* symbol visibility */
80 int32_t size; /* size of symbol */
81 int32_t globnum; /* symbol table offset if global */
82 struct Symbol *nextfwd; /* list of unresolved-size symbols */
83 char *name; /* used temporarily if in above list */
86 struct Section {
87 struct SAA *data;
88 uint32_t len, size, nrelocs;
89 int32_t index; /* index into sects array */
90 int type; /* SHT_PROGBITS or SHT_NOBITS */
91 uint32_t align; /* alignment: power of two */
92 uint32_t flags; /* section flags */
93 char *name;
94 struct SAA *rel;
95 uint32_t rellen;
96 struct Reloc *head, **tail;
97 struct rbtree *gsyms; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section **sects;
102 static int nsects, sectlen;
104 #define SHSTR_DELTA 256
105 static char *shstrtab;
106 static int shstrtablen, shstrtabsize;
108 static struct SAA *syms;
109 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
111 static int32_t def_seg;
113 static struct RAA *bsym;
115 static struct SAA *strs;
116 static uint32_t strslen;
118 static struct Symbol *fwds;
120 static char elf_module[FILENAME_MAX];
122 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
123 static uint8_t elf_abiver = 0; /* Current ABI version */
125 extern struct ofmt of_elfx32;
127 static struct ELF_SECTDATA {
128 void *data;
129 int32_t len;
130 bool is_saa;
131 } *elf_sects;
132 static int elf_nsect, nsections;
133 static int32_t elf_foffs;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section *, const void *, size_t);
137 static void elf_sect_writeaddr(struct Section *, int32_t, size_t);
138 static void elf_section_header(int, int, uint32_t, void *, bool, uint32_t, int, int,
139 int, int);
140 static void elf_write_sections(void);
141 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA *elf_build_reltab(uint32_t *, struct Reloc *);
143 static void add_sectname(char *, char *);
145 struct erel {
146 int offset, info;
149 struct symlininfo {
150 int offset;
151 int section; /* index into sects[] */
152 int segto; /* internal section number */
153 char *name; /* shallow-copied pointer of section name */
156 struct linelist {
157 struct linelist *next;
158 struct linelist *last;
159 struct symlininfo info;
160 char *filename;
161 int line;
164 struct sectlist {
165 struct SAA *psaa;
166 int section;
167 int line;
168 int offset;
169 int file;
170 struct sectlist *next;
171 struct sectlist *last;
174 /* common debug variables */
175 static int currentline = 1;
176 static int debug_immcall = 0;
178 /* stabs debug variables */
179 static struct linelist *stabslines = 0;
180 static int numlinestabs = 0;
181 static char *stabs_filename = 0;
182 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
183 static int stablen, stabstrlen, stabrellen;
185 /* dwarf debug variables */
186 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
187 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
188 static int dwarf_numfiles = 0, dwarf_nsections;
189 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
190 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
191 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
192 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
193 abbrevlen, linelen, linerellen, framelen, loclen;
194 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
197 static struct dfmt df_dwarf;
198 static struct dfmt df_stabs;
199 static struct Symbol *lastsym;
201 /* common debugging routines */
202 static void debugx32_typevalue(int32_t);
203 static void debugx32_deflabel(char *, int32_t, int64_t, int, char *);
204 static void debugx32_directive(const char *, const char *);
206 /* stabs debugging routines */
207 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t);
208 static void stabsx32_output(int, void *);
209 static void stabsx32_generate(void);
210 static void stabsx32_cleanup(void);
212 /* dwarf debugging routines */
213 static void dwarfx32_init(void);
214 static void dwarfx32_linenum(const char *filename, int32_t linenumber, int32_t);
215 static void dwarfx32_output(int, void *);
216 static void dwarfx32_generate(void);
217 static void dwarfx32_cleanup(void);
218 static void dwarfx32_findfile(const char *);
219 static void dwarfx32_findsect(const int);
222 * Special section numbers which are used to define ELF special
223 * symbols, which can be used with WRT to provide PIC relocation
224 * types.
226 static int32_t elf_gotpc_sect, elf_gotoff_sect;
227 static int32_t elf_got_sect, elf_plt_sect;
228 static int32_t elf_sym_sect;
229 static int32_t elf_gottpoff_sect;
231 static void elf_init(void)
233 maxbits = 64;
234 sects = NULL;
235 nsects = sectlen = 0;
236 syms = saa_init((int32_t)sizeof(struct Symbol));
237 nlocals = nglobs = ndebugs = 0;
238 bsym = raa_init();
239 strs = saa_init(1L);
240 saa_wbytes(strs, "\0", 1L);
241 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
242 strslen = 2 + strlen(elf_module);
243 shstrtab = NULL;
244 shstrtablen = shstrtabsize = 0;;
245 add_sectname("", "");
247 fwds = NULL;
249 elf_gotpc_sect = seg_alloc();
250 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
251 elf_gotoff_sect = seg_alloc();
252 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
253 elf_got_sect = seg_alloc();
254 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
255 elf_plt_sect = seg_alloc();
256 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
257 elf_sym_sect = seg_alloc();
258 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
259 elf_gottpoff_sect = seg_alloc();
260 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
262 def_seg = seg_alloc();
266 static void elf_cleanup(int debuginfo)
268 struct Reloc *r;
269 int i;
271 (void)debuginfo;
273 elf_write();
274 for (i = 0; i < nsects; i++) {
275 if (sects[i]->type != SHT_NOBITS)
276 saa_free(sects[i]->data);
277 if (sects[i]->head)
278 saa_free(sects[i]->rel);
279 while (sects[i]->head) {
280 r = sects[i]->head;
281 sects[i]->head = sects[i]->head->next;
282 nasm_free(r);
285 nasm_free(sects);
286 saa_free(syms);
287 raa_free(bsym);
288 saa_free(strs);
289 if (of_elfx32.current_dfmt) {
290 of_elfx32.current_dfmt->cleanup();
294 /* add entry to the elf .shstrtab section */
295 static void add_sectname(char *firsthalf, char *secondhalf)
297 int len = strlen(firsthalf) + strlen(secondhalf);
298 while (shstrtablen + len + 1 > shstrtabsize)
299 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
300 strcpy(shstrtab + shstrtablen, firsthalf);
301 strcat(shstrtab + shstrtablen, secondhalf);
302 shstrtablen += len + 1;
305 static int elf_make_section(char *name, int type, int flags, int align)
307 struct Section *s;
309 s = nasm_zalloc(sizeof(*s));
311 if (type != SHT_NOBITS)
312 s->data = saa_init(1L);
313 s->tail = &s->head;
314 if (!strcmp(name, ".text"))
315 s->index = def_seg;
316 else
317 s->index = seg_alloc();
318 add_sectname("", name);
320 s->name = nasm_strdup(name);
321 s->type = type;
322 s->flags = flags;
323 s->align = align;
325 if (nsects >= sectlen)
326 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
327 sects[nsects++] = s;
329 return nsects - 1;
332 static int32_t elf_section_names(char *name, int pass, int *bits)
334 char *p;
335 uint32_t flags, flags_and, flags_or;
336 uint64_t align;
337 int type, i;
340 * Default is 64 bits.
342 if (!name) {
343 *bits = 64;
344 return def_seg;
347 p = nasm_skip_word(name);
348 if (*p)
349 *p++ = '\0';
350 flags_and = flags_or = type = align = 0;
352 section_attrib(name, p, pass, &flags_and,
353 &flags_or, &align, &type);
355 if (!strcmp(name, ".shstrtab") ||
356 !strcmp(name, ".symtab") ||
357 !strcmp(name, ".strtab")) {
358 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
359 "name `%s'", name);
360 return NO_SEG;
363 for (i = 0; i < nsects; i++)
364 if (!strcmp(name, sects[i]->name))
365 break;
366 if (i == nsects) {
367 const struct elf_known_section *ks = elf_known_sections;
369 while (ks->name) {
370 if (!strcmp(name, ks->name))
371 break;
372 ks++;
375 type = type ? type : ks->type;
376 align = align ? align : ks->align;
377 flags = (ks->flags & ~flags_and) | flags_or;
379 i = elf_make_section(name, type, flags, align);
380 } else if (pass == 1) {
381 if ((type && sects[i]->type != type)
382 || (align && sects[i]->align != align)
383 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
384 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
385 " redeclaration of section `%s'", name);
388 return sects[i]->index;
391 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
392 int is_global, char *special)
394 int pos = strslen;
395 struct Symbol *sym;
396 bool special_used = false;
398 #if defined(DEBUG) && DEBUG>2
399 nasm_error(ERR_DEBUG,
400 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
401 name, segment, offset, is_global, special);
402 #endif
403 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
405 * This is a NASM special symbol. We never allow it into
406 * the ELF symbol table, even if it's a valid one. If it
407 * _isn't_ a valid one, we should barf immediately.
409 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
410 strcmp(name, "..got") && strcmp(name, "..plt") &&
411 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
412 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
413 return;
416 if (is_global == 3) {
417 struct Symbol **s;
419 * Fix up a forward-reference symbol size from the first
420 * pass.
422 for (s = &fwds; *s; s = &(*s)->nextfwd)
423 if (!strcmp((*s)->name, name)) {
424 struct tokenval tokval;
425 expr *e;
426 char *p = nasm_skip_spaces(nasm_skip_word(special));
428 stdscan_reset();
429 stdscan_set(p);
430 tokval.t_type = TOKEN_INVALID;
431 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
432 if (e) {
433 if (!is_simple(e))
434 nasm_error(ERR_NONFATAL, "cannot use relocatable"
435 " expression as symbol size");
436 else
437 (*s)->size = reloc_value(e);
441 * Remove it from the list of unresolved sizes.
443 nasm_free((*s)->name);
444 *s = (*s)->nextfwd;
445 return;
447 return; /* it wasn't an important one */
450 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
451 strslen += 1 + strlen(name);
453 lastsym = sym = saa_wstruct(syms);
455 memset(&sym->symv, 0, sizeof(struct rbtree));
457 sym->strpos = pos;
458 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
459 sym->other = STV_DEFAULT;
460 sym->size = 0;
461 if (segment == NO_SEG)
462 sym->section = SHN_ABS;
463 else {
464 int i;
465 sym->section = SHN_UNDEF;
466 if (segment == def_seg) {
467 /* we have to be sure at least text section is there */
468 int tempint;
469 if (segment != elf_section_names(".text", 2, &tempint))
470 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
472 for (i = 0; i < nsects; i++) {
473 if (segment == sects[i]->index) {
474 sym->section = i + 1;
475 break;
480 if (is_global == 2) {
481 sym->size = offset;
482 sym->symv.key = 0;
483 sym->section = SHN_COMMON;
485 * We have a common variable. Check the special text to see
486 * if it's a valid number and power of two; if so, store it
487 * as the alignment for the common variable.
489 if (special) {
490 bool err;
491 sym->symv.key = readnum(special, &err);
492 if (err)
493 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
494 " valid number", special);
495 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
496 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
497 " power of two", special);
499 special_used = true;
500 } else
501 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
503 if (sym->type == SYM_GLOBAL) {
505 * If sym->section == SHN_ABS, then the first line of the
506 * else section would cause a core dump, because its a reference
507 * beyond the end of the section array.
508 * This behaviour is exhibited by this code:
509 * GLOBAL crash_nasm
510 * crash_nasm equ 0
511 * To avoid such a crash, such requests are silently discarded.
512 * This may not be the best solution.
514 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
515 bsym = raa_write(bsym, segment, nglobs);
516 } else if (sym->section != SHN_ABS) {
518 * This is a global symbol; so we must add it to the rbtree
519 * of global symbols in its section.
521 * In addition, we check the special text for symbol
522 * type and size information.
524 sects[sym->section-1]->gsyms =
525 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
527 if (special) {
528 int n = strcspn(special, " \t");
530 if (!nasm_strnicmp(special, "function", n))
531 sym->type |= STT_FUNC;
532 else if (!nasm_strnicmp(special, "data", n) ||
533 !nasm_strnicmp(special, "object", n))
534 sym->type |= STT_OBJECT;
535 else if (!nasm_strnicmp(special, "notype", n))
536 sym->type |= STT_NOTYPE;
537 else
538 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
539 n, special);
540 special += n;
542 special = nasm_skip_spaces(special);
543 if (*special) {
544 n = strcspn(special, " \t");
545 if (!nasm_strnicmp(special, "default", n))
546 sym->other = STV_DEFAULT;
547 else if (!nasm_strnicmp(special, "internal", n))
548 sym->other = STV_INTERNAL;
549 else if (!nasm_strnicmp(special, "hidden", n))
550 sym->other = STV_HIDDEN;
551 else if (!nasm_strnicmp(special, "protected", n))
552 sym->other = STV_PROTECTED;
553 else
554 n = 0;
555 special += n;
558 if (*special) {
559 struct tokenval tokval;
560 expr *e;
561 int fwd = 0;
562 char *saveme = stdscan_get();
564 while (special[n] && nasm_isspace(special[n]))
565 n++;
567 * We have a size expression; attempt to
568 * evaluate it.
570 stdscan_reset();
571 stdscan_set(special + n);
572 tokval.t_type = TOKEN_INVALID;
573 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
574 NULL);
575 if (fwd) {
576 sym->nextfwd = fwds;
577 fwds = sym;
578 sym->name = nasm_strdup(name);
579 } else if (e) {
580 if (!is_simple(e))
581 nasm_error(ERR_NONFATAL, "cannot use relocatable"
582 " expression as symbol size");
583 else
584 sym->size = reloc_value(e);
586 stdscan_set(saveme);
588 special_used = true;
591 * If TLS segment, mark symbol accordingly.
593 if (sects[sym->section - 1]->flags & SHF_TLS) {
594 sym->type &= 0xf0;
595 sym->type |= STT_TLS;
598 sym->globnum = nglobs;
599 nglobs++;
600 } else
601 nlocals++;
603 if (special && !special_used)
604 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
607 static void elf_add_reloc(struct Section *sect, int32_t segment,
608 int32_t offset, int type)
610 struct Reloc *r;
612 r = *sect->tail = nasm_zalloc(sizeof(struct Reloc));
613 sect->tail = &r->next;
615 r->address = sect->len;
616 r->offset = offset;
618 if (segment != NO_SEG) {
619 int i;
620 for (i = 0; i < nsects; i++)
621 if (segment == sects[i]->index)
622 r->symbol = i + 2;
623 if (!r->symbol)
624 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
626 r->type = type;
628 sect->nrelocs++;
632 * This routine deals with ..got and ..sym relocations: the more
633 * complicated kinds. In shared-library writing, some relocations
634 * with respect to global symbols must refer to the precise symbol
635 * rather than referring to an offset from the base of the section
636 * _containing_ the symbol. Such relocations call to this routine,
637 * which searches the symbol list for the symbol in question.
639 * R_X86_64_GOT32 references require the _exact_ symbol address to be
640 * used; R_X86_64_32 references can be at an offset from the symbol.
641 * The boolean argument `exact' tells us this.
643 * Return value is the adjusted value of `addr', having become an
644 * offset from the symbol rather than the section. Should always be
645 * zero when returning from an exact call.
647 * Limitation: if you define two symbols at the same place,
648 * confusion will occur.
650 * Inefficiency: we search, currently, using a linked list which
651 * isn't even necessarily sorted.
653 static void elf_add_gsym_reloc(struct Section *sect,
654 int32_t segment, uint32_t offset, int32_t pcrel,
655 int type, bool exact)
657 struct Reloc *r;
658 struct Section *s;
659 struct Symbol *sym;
660 struct rbtree *srb;
661 int i;
664 * First look up the segment/offset pair and find a global
665 * symbol corresponding to it. If it's not one of our segments,
666 * then it must be an external symbol, in which case we're fine
667 * doing a normal elf_add_reloc after first sanity-checking
668 * that the offset from the symbol is zero.
670 s = NULL;
671 for (i = 0; i < nsects; i++)
672 if (segment == sects[i]->index) {
673 s = sects[i];
674 break;
677 if (!s) {
678 if (exact && offset)
679 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
680 else
681 elf_add_reloc(sect, segment, offset - pcrel, type);
682 return;
685 srb = rb_search(s->gsyms, offset);
686 if (!srb || (exact && srb->key != offset)) {
687 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
688 " for this reference");
689 return;
691 sym = container_of(srb, struct Symbol, symv);
693 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
694 sect->tail = &r->next;
695 r->next = NULL;
697 r->address = sect->len;
698 r->offset = offset - pcrel - sym->symv.key;
699 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
700 r->type = type;
702 sect->nrelocs++;
705 static void elf_out(int32_t segto, const void *data,
706 enum out_type type, uint64_t size,
707 int32_t segment, int32_t wrt)
709 struct Section *s;
710 int32_t addr;
711 int reltype, bytes;
712 int i;
713 static struct symlininfo sinfo;
715 #if defined(DEBUG) && DEBUG>2
716 if (data)
717 nasm_error(ERR_DEBUG,
718 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
719 currentline, type, segment, segto, size, *(int64_t *)data);
720 else
721 nasm_error(ERR_DEBUG,
722 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
723 currentline, type, segment, segto, size);
724 #endif
727 * handle absolute-assembly (structure definitions)
729 if (segto == NO_SEG) {
730 if (type != OUT_RESERVE)
731 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
732 " space");
733 return;
736 s = NULL;
737 for (i = 0; i < nsects; i++)
738 if (segto == sects[i]->index) {
739 s = sects[i];
740 break;
742 if (!s) {
743 int tempint; /* ignored */
744 if (segto != elf_section_names(".text", 2, &tempint))
745 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
746 else {
747 s = sects[nsects - 1];
748 i = nsects - 1;
752 /* again some stabs debugging stuff */
753 if (of_elfx32.current_dfmt) {
754 sinfo.offset = s->len;
755 sinfo.section = i;
756 sinfo.segto = segto;
757 sinfo.name = s->name;
758 of_elfx32.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
760 /* end of debugging stuff */
762 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
763 nasm_error(ERR_WARNING, "attempt to initialize memory in"
764 " BSS section `%s': ignored", s->name);
765 s->len += realsize(type, size);
766 return;
769 switch (type) {
770 case OUT_RESERVE:
771 if (s->type == SHT_PROGBITS) {
772 nasm_error(ERR_WARNING, "uninitialized space declared in"
773 " non-BSS section `%s': zeroing", s->name);
774 elf_sect_write(s, NULL, size);
775 } else
776 s->len += size;
777 break;
779 case OUT_RAWDATA:
780 if (segment != NO_SEG)
781 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
782 elf_sect_write(s, data, size);
783 break;
785 case OUT_ADDRESS:
786 addr = *(int64_t *)data;
787 if (segment == NO_SEG) {
788 /* Do nothing */
789 } else if (segment % 2) {
790 nasm_error(ERR_NONFATAL, "ELF format does not support"
791 " segment base references");
792 } else {
793 if (wrt == NO_SEG) {
794 switch ((int)size) {
795 case 1:
796 elf_add_reloc(s, segment, addr, R_X86_64_8);
797 break;
798 case 2:
799 elf_add_reloc(s, segment, addr, R_X86_64_16);
800 break;
801 case 4:
802 elf_add_reloc(s, segment, addr, R_X86_64_32);
803 break;
804 case 8:
805 elf_add_reloc(s, segment, addr, R_X86_64_64);
806 break;
807 default:
808 nasm_error(ERR_PANIC, "internal error elfx32-hpa-871");
809 break;
811 addr = 0;
812 } else if (wrt == elf_gotpc_sect + 1) {
814 * The user will supply GOT relative to $$. ELF
815 * will let us have GOT relative to $. So we
816 * need to fix up the data item by $-$$.
818 addr += s->len;
819 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
820 addr = 0;
821 } else if (wrt == elf_gotoff_sect + 1) {
822 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
823 "R_X86_64_GOTOFF64");
824 } else if (wrt == elf_got_sect + 1) {
825 switch ((int)size) {
826 case 4:
827 elf_add_gsym_reloc(s, segment, addr, 0,
828 R_X86_64_GOT32, true);
829 addr = 0;
830 break;
831 default:
832 nasm_error(ERR_NONFATAL, "invalid ..got reference");
833 break;
835 } else if (wrt == elf_sym_sect + 1) {
836 switch ((int)size) {
837 case 1:
838 elf_add_gsym_reloc(s, segment, addr, 0,
839 R_X86_64_8, false);
840 addr = 0;
841 break;
842 case 2:
843 elf_add_gsym_reloc(s, segment, addr, 0,
844 R_X86_64_16, false);
845 addr = 0;
846 break;
847 case 4:
848 elf_add_gsym_reloc(s, segment, addr, 0,
849 R_X86_64_32, false);
850 addr = 0;
851 break;
852 case 8:
853 elf_add_gsym_reloc(s, segment, addr, 0,
854 R_X86_64_64, false);
855 addr = 0;
856 break;
857 default:
858 nasm_error(ERR_PANIC, "internal error elfx32-hpa-903");
859 break;
861 } else if (wrt == elf_plt_sect + 1) {
862 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
863 "relative PLT references");
864 } else {
865 nasm_error(ERR_NONFATAL, "ELF format does not support this"
866 " use of WRT");
869 elf_sect_writeaddr(s, addr, size);
870 break;
872 case OUT_REL1ADR:
873 reltype = R_X86_64_PC8;
874 bytes = 1;
875 goto rel12adr;
877 case OUT_REL2ADR:
878 reltype = R_X86_64_PC16;
879 bytes = 2;
880 goto rel12adr;
882 rel12adr:
883 addr = *(int64_t *)data - size;
884 if (segment == segto)
885 nasm_error(ERR_PANIC, "intra-segment OUT_REL1ADR");
886 if (segment == NO_SEG) {
887 /* Do nothing */
888 } else if (segment % 2) {
889 nasm_error(ERR_NONFATAL, "ELF format does not support"
890 " segment base references");
891 } else {
892 if (wrt == NO_SEG) {
893 elf_add_reloc(s, segment, addr, reltype);
894 addr = 0;
895 } else {
896 nasm_error(ERR_NONFATAL,
897 "Unsupported non-32-bit ELF relocation");
900 elf_sect_writeaddr(s, addr, bytes);
901 break;
903 case OUT_REL4ADR:
904 addr = *(int64_t *)data - size;
905 if (segment == segto)
906 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
907 if (segment == NO_SEG) {
908 /* Do nothing */
909 } else if (segment % 2) {
910 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
911 " segment base references");
912 } else {
913 if (wrt == NO_SEG) {
914 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
915 addr = 0;
916 } else if (wrt == elf_plt_sect + 1) {
917 elf_add_gsym_reloc(s, segment, addr+size, size,
918 R_X86_64_PLT32, true);
919 addr = 0;
920 } else if (wrt == elf_gotpc_sect + 1 ||
921 wrt == elf_got_sect + 1) {
922 elf_add_gsym_reloc(s, segment, addr+size, size,
923 R_X86_64_GOTPCREL, true);
924 addr = 0;
925 } else if (wrt == elf_gotoff_sect + 1 ||
926 wrt == elf_got_sect + 1) {
927 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
928 } else if (wrt == elf_gottpoff_sect + 1) {
929 elf_add_gsym_reloc(s, segment, addr+size, size,
930 R_X86_64_GOTTPOFF, true);
931 addr = 0;
932 } else {
933 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
934 " use of WRT");
937 elf_sect_writeaddr(s, addr, 4);
938 break;
940 case OUT_REL8ADR:
941 nasm_error(ERR_NONFATAL,
942 "32-bit ELF format does not support 64-bit relocations");
943 addr = 0;
944 elf_sect_writeaddr(s, addr, 8);
945 break;
949 static void elf_write(void)
951 int align;
952 char *p;
953 int i;
955 struct SAA *symtab;
956 int32_t symtablen, symtablocal;
959 * Work out how many sections we will have. We have SHN_UNDEF,
960 * then the flexible user sections, then the fixed sections
961 * `.shstrtab', `.symtab' and `.strtab', then optionally
962 * relocation sections for the user sections.
964 nsections = sec_numspecial + 1;
965 if (of_elfx32.current_dfmt == &df_stabs)
966 nsections += 3;
967 else if (of_elfx32.current_dfmt == &df_dwarf)
968 nsections += 10;
970 add_sectname("", ".shstrtab");
971 add_sectname("", ".symtab");
972 add_sectname("", ".strtab");
973 for (i = 0; i < nsects; i++) {
974 nsections++; /* for the section itself */
975 if (sects[i]->head) {
976 nsections++; /* for its relocations */
977 add_sectname(".rela", sects[i]->name);
981 if (of_elfx32.current_dfmt == &df_stabs) {
982 /* in case the debug information is wanted, just add these three sections... */
983 add_sectname("", ".stab");
984 add_sectname("", ".stabstr");
985 add_sectname(".rel", ".stab");
988 else if (of_elfx32.current_dfmt == &df_dwarf) {
989 /* the dwarf debug standard specifies the following ten sections,
990 not all of which are currently implemented,
991 although all of them are defined. */
992 add_sectname("", ".debug_aranges");
993 add_sectname(".rela", ".debug_aranges");
994 add_sectname("", ".debug_pubnames");
995 add_sectname("", ".debug_info");
996 add_sectname(".rela", ".debug_info");
997 add_sectname("", ".debug_abbrev");
998 add_sectname("", ".debug_line");
999 add_sectname(".rela", ".debug_line");
1000 add_sectname("", ".debug_frame");
1001 add_sectname("", ".debug_loc");
1005 * Output the ELF header.
1007 fwrite("\177ELF\1\1\1", 7, 1, ofile);
1008 fputc(elf_osabi, ofile);
1009 fputc(elf_abiver, ofile);
1010 fwritezero(7, ofile);
1011 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1012 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1013 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1014 fwriteint32_t(0L, ofile); /* no entry point */
1015 fwriteint32_t(0L, ofile); /* no program header table */
1016 fwriteint32_t(0x40L, ofile); /* section headers straight after
1017 * ELF header plus alignment */
1018 fwriteint32_t(0L, ofile); /* X86_64 defines no special flags */
1019 fwriteint16_t(0x34, ofile); /* size of ELF header */
1020 fwriteint16_t(0, ofile); /* no program header table, again */
1021 fwriteint16_t(0, ofile); /* still no program header table */
1022 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
1023 fwriteint16_t(nsections, ofile); /* number of sections */
1024 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1025 * section header table */
1026 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1027 fwriteint32_t(0L, ofile);
1028 fwriteint32_t(0L, ofile);
1031 * Build the symbol table and relocation tables.
1033 symtab = elf_build_symtab(&symtablen, &symtablocal);
1034 for (i = 0; i < nsects; i++)
1035 if (sects[i]->head)
1036 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1037 sects[i]->head);
1040 * Now output the section header table.
1043 elf_foffs = 0x40 + sizeof(Elf32_Shdr) * nsections;
1044 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1045 elf_foffs += align;
1046 elf_nsect = 0;
1047 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1049 /* SHN_UNDEF */
1050 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1051 p = shstrtab + 1;
1053 /* The normal sections */
1054 for (i = 0; i < nsects; i++) {
1055 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1056 (sects[i]->type == SHT_PROGBITS ?
1057 sects[i]->data : NULL), true,
1058 sects[i]->len, 0, 0, sects[i]->align, 0);
1059 p += strlen(p) + 1;
1062 /* .shstrtab */
1063 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1064 shstrtablen, 0, 0, 1, 0);
1065 p += strlen(p) + 1;
1067 /* .symtab */
1068 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1069 symtablen, sec_strtab, symtablocal, 4, 16);
1070 p += strlen(p) + 1;
1072 /* .strtab */
1073 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1074 strslen, 0, 0, 1, 0);
1075 p += strlen(p) + 1;
1077 /* The relocation sections */
1078 for (i = 0; i < nsects; i++)
1079 if (sects[i]->head) {
1080 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1081 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1082 p += strlen(p) + 1;
1085 if (of_elfx32.current_dfmt == &df_stabs) {
1086 /* for debugging information, create the last three sections
1087 which are the .stab , .stabstr and .rel.stab sections respectively */
1089 /* this function call creates the stab sections in memory */
1090 stabsx32_generate();
1092 if (stabbuf && stabstrbuf && stabrelbuf) {
1093 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1094 stablen, sec_stabstr, 0, 4, 12);
1095 p += strlen(p) + 1;
1097 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1098 stabstrlen, 0, 0, 4, 0);
1099 p += strlen(p) + 1;
1101 /* link -> symtable info -> section to refer to */
1102 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1103 stabrellen, sec_symtab, sec_stab, 4, 8);
1104 p += strlen(p) + 1;
1106 } else if (of_elfx32.current_dfmt == &df_dwarf) {
1107 /* for dwarf debugging information, create the ten dwarf sections */
1109 /* this function call creates the dwarf sections in memory */
1110 if (dwarf_fsect)
1111 dwarfx32_generate();
1113 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1114 arangeslen, 0, 0, 1, 0);
1115 p += strlen(p) + 1;
1117 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1118 arangesrellen, sec_symtab, sec_debug_aranges, 1, 12);
1119 p += strlen(p) + 1;
1121 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1122 pubnameslen, 0, 0, 1, 0);
1123 p += strlen(p) + 1;
1125 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1126 infolen, 0, 0, 1, 0);
1127 p += strlen(p) + 1;
1129 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1130 inforellen, sec_symtab, sec_debug_info, 1, 12);
1131 p += strlen(p) + 1;
1133 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1134 abbrevlen, 0, 0, 1, 0);
1135 p += strlen(p) + 1;
1137 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1138 linelen, 0, 0, 1, 0);
1139 p += strlen(p) + 1;
1141 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1142 linerellen, sec_symtab, sec_debug_line, 1, 12);
1143 p += strlen(p) + 1;
1145 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1146 framelen, 0, 0, 8, 0);
1147 p += strlen(p) + 1;
1149 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1150 loclen, 0, 0, 1, 0);
1151 p += strlen(p) + 1;
1153 fwritezero(align, ofile);
1156 * Now output the sections.
1158 elf_write_sections();
1160 nasm_free(elf_sects);
1161 saa_free(symtab);
1164 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1166 struct SAA *s = saa_init(1L);
1167 struct Symbol *sym;
1168 uint8_t entry[24], *p;
1169 int i;
1171 *len = *local = 0;
1174 * First, an all-zeros entry, required by the ELF spec.
1176 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1177 *len += 16;
1178 (*local)++;
1181 * Next, an entry for the file name.
1183 p = entry;
1184 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1185 WRITELONG(p, 0); /* no value */
1186 WRITELONG(p, 0); /* no size either */
1187 WRITESHORT(p, STT_FILE); /* type FILE */
1188 WRITESHORT(p, SHN_ABS);
1189 saa_wbytes(s, entry, 16L);
1190 *len += 16;
1191 (*local)++;
1194 * Now some standard symbols defining the segments, for relocation
1195 * purposes.
1197 for (i = 1; i <= nsects; i++) {
1198 p = entry;
1199 WRITELONG(p, 0); /* no symbol name */
1200 WRITELONG(p, 0); /* offset zero */
1201 WRITELONG(p, 0); /* size zero */
1202 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1203 WRITESHORT(p, i); /* section id */
1204 saa_wbytes(s, entry, 16L);
1205 *len += 16;
1206 (*local)++;
1211 * Now the other local symbols.
1213 saa_rewind(syms);
1214 while ((sym = saa_rstruct(syms))) {
1215 if (sym->type & SYM_GLOBAL)
1216 continue;
1217 p = entry;
1218 WRITELONG(p, sym->strpos); /* index into symbol string table */
1219 WRITELONG(p, sym->symv.key); /* value of symbol */
1220 WRITELONG(p, sym->size); /* size of symbol */
1221 WRITECHAR(p, sym->type); /* type and binding */
1222 WRITECHAR(p, sym->other); /* visibility */
1223 WRITESHORT(p, sym->section); /* index into section header table */
1224 saa_wbytes(s, entry, 16L);
1225 *len += 16;
1226 (*local)++;
1229 * dwarf needs symbols for debug sections
1230 * which are relocation targets.
1232 if (of_elfx32.current_dfmt == &df_dwarf) {
1233 dwarf_infosym = *local;
1234 p = entry;
1235 WRITELONG(p, 0); /* no symbol name */
1236 WRITELONG(p, 0); /* offset zero */
1237 WRITELONG(p, 0); /* size zero */
1238 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1239 WRITESHORT(p, sec_debug_info); /* section id */
1240 saa_wbytes(s, entry, 16L);
1241 *len += 16;
1242 (*local)++;
1243 dwarf_abbrevsym = *local;
1244 p = entry;
1245 WRITELONG(p, 0); /* no symbol name */
1246 WRITELONG(p, 0); /* offset zero */
1247 WRITELONG(p, 0); /* size zero */
1248 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1249 WRITESHORT(p, sec_debug_abbrev); /* section id */
1250 saa_wbytes(s, entry, 16L);
1251 *len += 16;
1252 (*local)++;
1253 dwarf_linesym = *local;
1254 p = entry;
1255 WRITELONG(p, 0); /* no symbol name */
1256 WRITELONG(p, 0); /* offset zero */
1257 WRITELONG(p, 0); /* size zero */
1258 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1259 WRITESHORT(p, sec_debug_line); /* section id */
1260 saa_wbytes(s, entry, 16L);
1261 *len += 16;
1262 (*local)++;
1266 * Now the global symbols.
1268 saa_rewind(syms);
1269 while ((sym = saa_rstruct(syms))) {
1270 if (!(sym->type & SYM_GLOBAL))
1271 continue;
1272 p = entry;
1273 WRITELONG(p, sym->strpos);
1274 WRITELONG(p, sym->symv.key);
1275 WRITELONG(p, sym->size);
1276 WRITECHAR(p, sym->type); /* type and binding */
1277 WRITECHAR(p, sym->other); /* visibility */
1278 WRITESHORT(p, sym->section);
1279 saa_wbytes(s, entry, 16L);
1280 *len += 16;
1283 return s;
1286 static struct SAA *elf_build_reltab(uint32_t *len, struct Reloc *r)
1288 struct SAA *s;
1289 uint8_t *p, entry[12];
1290 int32_t global_offset;
1292 if (!r)
1293 return NULL;
1295 s = saa_init(1L);
1296 *len = 0;
1299 * How to onvert from a global placeholder to a real symbol index;
1300 * the +2 refers to the two special entries, the null entry and
1301 * the filename entry.
1303 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1305 while (r) {
1306 int32_t sym = r->symbol;
1308 if (sym >= GLOBAL_TEMP_BASE)
1309 sym += global_offset;
1311 p = entry;
1312 WRITELONG(p, r->address);
1313 WRITELONG(p, (sym << 8) + r->type);
1314 WRITELONG(p, r->offset);
1315 saa_wbytes(s, entry, 12L);
1316 *len += 12;
1318 r = r->next;
1321 return s;
1324 static void elf_section_header(int name, int type, uint32_t flags,
1325 void *data, bool is_saa, uint32_t datalen,
1326 int link, int info, int align, int eltsize)
1328 elf_sects[elf_nsect].data = data;
1329 elf_sects[elf_nsect].len = datalen;
1330 elf_sects[elf_nsect].is_saa = is_saa;
1331 elf_nsect++;
1333 fwriteint32_t((int32_t)name, ofile);
1334 fwriteint32_t((int32_t)type, ofile);
1335 fwriteint32_t((int32_t)flags, ofile);
1336 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1337 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1338 fwriteint32_t(datalen, ofile);
1339 if (data)
1340 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1341 fwriteint32_t((int32_t)link, ofile);
1342 fwriteint32_t((int32_t)info, ofile);
1343 fwriteint32_t((int32_t)align, ofile);
1344 fwriteint32_t((int32_t)eltsize, ofile);
1347 static void elf_write_sections(void)
1349 int i;
1350 for (i = 0; i < elf_nsect; i++)
1351 if (elf_sects[i].data) {
1352 int32_t len = elf_sects[i].len;
1353 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1354 int32_t align = reallen - len;
1355 if (elf_sects[i].is_saa)
1356 saa_fpwrite(elf_sects[i].data, ofile);
1357 else
1358 fwrite(elf_sects[i].data, len, 1, ofile);
1359 fwritezero(align, ofile);
1363 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1365 saa_wbytes(sect->data, data, len);
1366 sect->len += len;
1368 static void elf_sect_writeaddr(struct Section *sect, int32_t data, size_t len)
1370 saa_writeaddr(sect->data, data, len);
1371 sect->len += len;
1374 static void elf_sectalign(int32_t seg, unsigned int value)
1376 struct Section *s = NULL;
1377 int i;
1379 for (i = 0; i < nsects; i++) {
1380 if (sects[i]->index == seg) {
1381 s = sects[i];
1382 break;
1385 if (!s || !is_power2(value))
1386 return;
1388 if (value > s->align)
1389 s->align = value;
1392 static int32_t elf_segbase(int32_t segment)
1394 return segment;
1397 static int elf_directive(enum directives directive, char *value, int pass)
1399 bool err;
1400 int64_t n;
1401 char *p;
1403 switch (directive) {
1404 case D_OSABI:
1405 if (pass == 2)
1406 return 1; /* ignore in pass 2 */
1408 n = readnum(value, &err);
1409 if (err) {
1410 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1411 return 1;
1413 if (n < 0 || n > 255) {
1414 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1415 return 1;
1417 elf_osabi = n;
1418 elf_abiver = 0;
1420 if ((p = strchr(value,',')) == NULL)
1421 return 1;
1423 n = readnum(p+1, &err);
1424 if (err || n < 0 || n > 255) {
1425 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1426 return 1;
1429 elf_abiver = n;
1430 return 1;
1432 default:
1433 return 0;
1437 static void elf_filename(char *inname, char *outname)
1439 strcpy(elf_module, inname);
1440 standard_extension(inname, outname, ".o");
1443 extern macros_t elf_stdmac[];
1445 static int elf_set_info(enum geninfo type, char **val)
1447 (void)type;
1448 (void)val;
1449 return 0;
1451 static struct dfmt df_dwarf = {
1452 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1453 "dwarf",
1454 dwarfx32_init,
1455 dwarfx32_linenum,
1456 debugx32_deflabel,
1457 debugx32_directive,
1458 debugx32_typevalue,
1459 dwarfx32_output,
1460 dwarfx32_cleanup
1462 static struct dfmt df_stabs = {
1463 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1464 "stabs",
1465 null_debug_init,
1466 stabsx32_linenum,
1467 debugx32_deflabel,
1468 debugx32_directive,
1469 debugx32_typevalue,
1470 stabsx32_output,
1471 stabsx32_cleanup
1474 struct dfmt *elfx32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1476 struct ofmt of_elfx32 = {
1477 "ELFX32 (x86_64) object files (e.g. Linux)",
1478 "elfx32",
1480 elfx32_debugs_arr,
1481 &df_stabs,
1482 elf_stdmac,
1483 elf_init,
1484 elf_set_info,
1485 elf_out,
1486 elf_deflabel,
1487 elf_section_names,
1488 elf_sectalign,
1489 elf_segbase,
1490 elf_directive,
1491 elf_filename,
1492 elf_cleanup
1495 /* common debugging routines */
1496 static void debugx32_deflabel(char *name, int32_t segment, int64_t offset,
1497 int is_global, char *special)
1499 (void)name;
1500 (void)segment;
1501 (void)offset;
1502 (void)is_global;
1503 (void)special;
1506 static void debugx32_directive(const char *directive, const char *params)
1508 (void)directive;
1509 (void)params;
1512 static void debugx32_typevalue(int32_t type)
1514 int32_t stype, ssize;
1515 switch (TYM_TYPE(type)) {
1516 case TY_LABEL:
1517 ssize = 0;
1518 stype = STT_NOTYPE;
1519 break;
1520 case TY_BYTE:
1521 ssize = 1;
1522 stype = STT_OBJECT;
1523 break;
1524 case TY_WORD:
1525 ssize = 2;
1526 stype = STT_OBJECT;
1527 break;
1528 case TY_DWORD:
1529 ssize = 4;
1530 stype = STT_OBJECT;
1531 break;
1532 case TY_FLOAT:
1533 ssize = 4;
1534 stype = STT_OBJECT;
1535 break;
1536 case TY_QWORD:
1537 ssize = 8;
1538 stype = STT_OBJECT;
1539 break;
1540 case TY_TBYTE:
1541 ssize = 10;
1542 stype = STT_OBJECT;
1543 break;
1544 case TY_OWORD:
1545 ssize = 16;
1546 stype = STT_OBJECT;
1547 break;
1548 case TY_YWORD:
1549 ssize = 32;
1550 stype = STT_OBJECT;
1551 break;
1552 case TY_COMMON:
1553 ssize = 0;
1554 stype = STT_COMMON;
1555 break;
1556 case TY_SEG:
1557 ssize = 0;
1558 stype = STT_SECTION;
1559 break;
1560 case TY_EXTERN:
1561 ssize = 0;
1562 stype = STT_NOTYPE;
1563 break;
1564 case TY_EQU:
1565 ssize = 0;
1566 stype = STT_NOTYPE;
1567 break;
1568 default:
1569 ssize = 0;
1570 stype = STT_NOTYPE;
1571 break;
1573 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1574 lastsym->size = ssize;
1575 lastsym->type = stype;
1579 /* stabs debugging routines */
1581 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1583 (void)segto;
1584 if (!stabs_filename) {
1585 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1586 strcpy(stabs_filename, filename);
1587 } else {
1588 if (strcmp(stabs_filename, filename)) {
1589 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1590 in fact, this leak comes in quite handy to maintain a list of files
1591 encountered so far in the symbol lines... */
1593 /* why not nasm_free(stabs_filename); we're done with the old one */
1595 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1596 strcpy(stabs_filename, filename);
1599 debug_immcall = 1;
1600 currentline = linenumber;
1604 static void stabsx32_output(int type, void *param)
1606 struct symlininfo *s;
1607 struct linelist *el;
1608 if (type == TY_DEBUGSYMLIN) {
1609 if (debug_immcall) {
1610 s = (struct symlininfo *)param;
1611 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1612 return; /* line info is only collected for executable sections */
1613 numlinestabs++;
1614 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1615 el->info.offset = s->offset;
1616 el->info.section = s->section;
1617 el->info.name = s->name;
1618 el->line = currentline;
1619 el->filename = stabs_filename;
1620 el->next = 0;
1621 if (stabslines) {
1622 stabslines->last->next = el;
1623 stabslines->last = el;
1624 } else {
1625 stabslines = el;
1626 stabslines->last = el;
1630 debug_immcall = 0;
1633 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1635 static void stabsx32_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_zalloc(numlinestabs * sizeof(char *));
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_X86_64_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 */
1738 WRITELONG(rptr, (sptr - sbuf) - 4);
1739 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1742 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1743 numstabs++;
1745 /* relocation table entry */
1747 WRITELONG(rptr, (sptr - sbuf) - 4);
1748 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1750 ptr = ptr->next;
1754 /* this is an "ending" token */
1755 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1756 numstabs++;
1758 ((struct stabentry *)sbuf)->n_desc = numstabs;
1760 nasm_free(allfiles);
1761 nasm_free(fileidx);
1763 stablen = (sptr - sbuf);
1764 stabrellen = (rptr - rbuf);
1765 stabrelbuf = rbuf;
1766 stabbuf = sbuf;
1767 stabstrbuf = ssbuf;
1770 static void stabsx32_cleanup(void)
1772 struct linelist *ptr, *del;
1773 if (!stabslines)
1774 return;
1776 ptr = stabslines;
1777 while (ptr) {
1778 del = ptr;
1779 ptr = ptr->next;
1780 nasm_free(del);
1783 nasm_free(stabbuf);
1784 nasm_free(stabrelbuf);
1785 nasm_free(stabstrbuf);
1788 /* dwarf routines */
1790 static void dwarfx32_init(void)
1792 ndebugs = 3; /* 3 debug symbols */
1795 static void dwarfx32_linenum(const char *filename, int32_t linenumber,
1796 int32_t segto)
1798 (void)segto;
1799 dwarfx32_findfile(filename);
1800 debug_immcall = 1;
1801 currentline = linenumber;
1804 /* called from elf_out with type == TY_DEBUGSYMLIN */
1805 static void dwarfx32_output(int type, void *param)
1807 int ln, aa, inx, maxln, soc;
1808 struct symlininfo *s;
1809 struct SAA *plinep;
1811 (void)type;
1813 s = (struct symlininfo *)param;
1815 /* line number info is only gathered for executable sections */
1816 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1817 return;
1819 /* Check if section index has changed */
1820 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1821 dwarfx32_findsect(s->section);
1823 /* do nothing unless line or file has changed */
1824 if (!debug_immcall)
1825 return;
1827 ln = currentline - dwarf_csect->line;
1828 aa = s->offset - dwarf_csect->offset;
1829 inx = dwarf_clist->line;
1830 plinep = dwarf_csect->psaa;
1831 /* check for file change */
1832 if (!(inx == dwarf_csect->file)) {
1833 saa_write8(plinep,DW_LNS_set_file);
1834 saa_write8(plinep,inx);
1835 dwarf_csect->file = inx;
1837 /* check for line change */
1838 if (ln) {
1839 /* test if in range of special op code */
1840 maxln = line_base + line_range;
1841 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1842 if (ln >= line_base && ln < maxln && soc < 256) {
1843 saa_write8(plinep,soc);
1844 } else {
1845 saa_write8(plinep,DW_LNS_advance_line);
1846 saa_wleb128s(plinep,ln);
1847 if (aa) {
1848 saa_write8(plinep,DW_LNS_advance_pc);
1849 saa_wleb128u(plinep,aa);
1852 dwarf_csect->line = currentline;
1853 dwarf_csect->offset = s->offset;
1856 /* show change handled */
1857 debug_immcall = 0;
1861 static void dwarfx32_generate(void)
1863 uint8_t *pbuf;
1864 int indx;
1865 struct linelist *ftentry;
1866 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1867 struct SAA *parangesrel, *plinesrel, *pinforel;
1868 struct sectlist *psect;
1869 size_t saalen, linepoff, totlen, highaddr;
1871 /* write epilogues for each line program range */
1872 /* and build aranges section */
1873 paranges = saa_init(1L);
1874 parangesrel = saa_init(1L);
1875 saa_write16(paranges,3); /* dwarf version */
1876 saa_write32(parangesrel, paranges->datalen+4);
1877 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
1878 saa_write32(parangesrel, 0);
1879 saa_write32(paranges,0); /* offset into info */
1880 saa_write8(paranges,4); /* pointer size */
1881 saa_write8(paranges,0); /* not segmented */
1882 saa_write32(paranges,0); /* padding */
1883 /* iterate though sectlist entries */
1884 psect = dwarf_fsect;
1885 totlen = 0;
1886 highaddr = 0;
1887 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_X86_64_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,3); /* dwarf version */
1940 saa_write32(pinforel, pinfo->datalen + 4);
1941 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_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_X86_64_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_X86_64_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_X86_64_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_X86_64_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++)
2041 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2042 saa_write8(plines,0); /* directory LEB128u */
2043 saa_write8(plines,0); /* time LEB128u */
2044 saa_write8(plines,0); /* size LEB128u */
2045 ftentry = ftentry->next;
2047 saa_write8(plines,0); /* End of table */
2048 linepoff = plines->datalen;
2049 linelen = linepoff + totlen + 10;
2050 linebuf = pbuf = nasm_malloc(linelen);
2051 WRITELONG(pbuf,linelen-4); /* initial length */
2052 WRITESHORT(pbuf,3); /* dwarf version */
2053 WRITELONG(pbuf,linepoff); /* offset to line number program */
2054 /* write line header */
2055 saalen = linepoff;
2056 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2057 pbuf += linepoff;
2058 saa_free(plines);
2059 /* concatonate line program ranges */
2060 linepoff += 13;
2061 plinesrel = saa_init(1L);
2062 psect = dwarf_fsect;
2063 for (indx = 0; indx < dwarf_nsections; indx++) {
2064 saa_write32(plinesrel, linepoff);
2065 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
2066 saa_write32(plinesrel, 0);
2067 plinep = psect->psaa;
2068 saalen = plinep->datalen;
2069 saa_rnbytes(plinep, pbuf, saalen);
2070 pbuf += saalen;
2071 linepoff += saalen;
2072 saa_free(plinep);
2073 /* done with this entry */
2074 psect = psect->next;
2078 /* build rela.lines section */
2079 linerellen =saalen = plinesrel->datalen;
2080 linerelbuf = pbuf = nasm_malloc(linerellen);
2081 saa_rnbytes(plinesrel, pbuf, saalen);
2082 saa_free(plinesrel);
2084 /* build frame section */
2085 framelen = 4;
2086 framebuf = pbuf = nasm_malloc(framelen);
2087 WRITELONG(pbuf,framelen-4); /* initial length */
2089 /* build loc section */
2090 loclen = 16;
2091 locbuf = pbuf = nasm_malloc(loclen);
2092 WRITELONG(pbuf,0); /* null beginning offset */
2093 WRITELONG(pbuf,0); /* null ending offset */
2096 static void dwarfx32_cleanup(void)
2098 nasm_free(arangesbuf);
2099 nasm_free(arangesrelbuf);
2100 nasm_free(pubnamesbuf);
2101 nasm_free(infobuf);
2102 nasm_free(inforelbuf);
2103 nasm_free(abbrevbuf);
2104 nasm_free(linebuf);
2105 nasm_free(linerelbuf);
2106 nasm_free(framebuf);
2107 nasm_free(locbuf);
2110 static void dwarfx32_findfile(const char * fname)
2112 int finx;
2113 struct linelist *match;
2115 /* return if fname is current file name */
2116 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2117 return;
2119 /* search for match */
2120 match = 0;
2121 if (dwarf_flist) {
2122 match = dwarf_flist;
2123 for (finx = 0; finx < dwarf_numfiles; finx++) {
2124 if (!(strcmp(fname, match->filename))) {
2125 dwarf_clist = match;
2126 return;
2131 /* add file name to end of list */
2132 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2133 dwarf_numfiles++;
2134 dwarf_clist->line = dwarf_numfiles;
2135 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2136 strcpy(dwarf_clist->filename,fname);
2137 dwarf_clist->next = 0;
2138 if (!dwarf_flist) { /* if first entry */
2139 dwarf_flist = dwarf_elist = dwarf_clist;
2140 dwarf_clist->last = 0;
2141 } else { /* chain to previous entry */
2142 dwarf_elist->next = dwarf_clist;
2143 dwarf_elist = dwarf_clist;
2147 static void dwarfx32_findsect(const int index)
2149 int sinx;
2150 struct sectlist *match;
2151 struct SAA *plinep;
2153 /* return if index is current section index */
2154 if (dwarf_csect && (dwarf_csect->section == index))
2155 return;
2157 /* search for match */
2158 match = 0;
2159 if (dwarf_fsect) {
2160 match = dwarf_fsect;
2161 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2162 if ((match->section == index)) {
2163 dwarf_csect = match;
2164 return;
2166 match = match->next;
2170 /* add entry to end of list */
2171 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2172 dwarf_nsections++;
2173 dwarf_csect->psaa = plinep = saa_init(1L);
2174 dwarf_csect->line = 1;
2175 dwarf_csect->offset = 0;
2176 dwarf_csect->file = 1;
2177 dwarf_csect->section = index;
2178 dwarf_csect->next = 0;
2179 /* set relocatable address at start of line program */
2180 saa_write8(plinep,DW_LNS_extended_op);
2181 saa_write8(plinep,5); /* operand length */
2182 saa_write8(plinep,DW_LNE_set_address);
2183 saa_write32(plinep,0); /* Start Address */
2185 if (!dwarf_fsect) { /* if first entry */
2186 dwarf_fsect = dwarf_esect = dwarf_csect;
2187 dwarf_csect->last = 0;
2188 } else { /* chain to previous entry */
2189 dwarf_esect->next = dwarf_csect;
2190 dwarf_esect = dwarf_csect;
2194 #endif /* OF_ELFX32 */