preproc: Convert tabs to spaces
[nasm/externdefs.git] / output / outelfx32.c
blobbbedc482f4d7ede408f4a654296a9079470b5422
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 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:
787 int isize = (int)size;
788 int asize = abs(size);
790 addr = *(int64_t *)data;
791 if (segment == NO_SEG) {
792 /* Do nothing */
793 } else if (segment % 2) {
794 nasm_error(ERR_NONFATAL, "ELF format does not support"
795 " segment base references");
796 } else {
797 if (wrt == NO_SEG) {
798 switch (isize) {
799 case 1:
800 case -1:
801 elf_add_reloc(s, segment, addr, R_X86_64_8);
802 break;
803 case 2:
804 case -2:
805 elf_add_reloc(s, segment, addr, R_X86_64_16);
806 break;
807 case 4:
808 elf_add_reloc(s, segment, addr, R_X86_64_32);
809 break;
810 case -4:
811 elf_add_reloc(s, segment, addr, R_X86_64_32S);
812 break;
813 case 8:
814 case -8:
815 elf_add_reloc(s, segment, addr, R_X86_64_64);
816 break;
817 default:
818 nasm_error(ERR_PANIC, "internal error elfx32-hpa-871");
819 break;
821 addr = 0;
822 } else if (wrt == elf_gotpc_sect + 1) {
824 * The user will supply GOT relative to $$. ELF
825 * will let us have GOT relative to $. So we
826 * need to fix up the data item by $-$$.
828 addr += s->len;
829 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
830 addr = 0;
831 } else if (wrt == elf_gotoff_sect + 1) {
832 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
833 "R_X86_64_GOTOFF64");
834 } else if (wrt == elf_got_sect + 1) {
835 switch (asize) {
836 case 4:
837 elf_add_gsym_reloc(s, segment, addr, 0,
838 R_X86_64_GOT32, true);
839 addr = 0;
840 break;
841 default:
842 nasm_error(ERR_NONFATAL, "invalid ..got reference");
843 break;
845 } else if (wrt == elf_sym_sect + 1) {
846 switch (isize) {
847 case 1:
848 case -1:
849 elf_add_gsym_reloc(s, segment, addr, 0,
850 R_X86_64_8, false);
851 addr = 0;
852 break;
853 case 2:
854 case -2:
855 elf_add_gsym_reloc(s, segment, addr, 0,
856 R_X86_64_16, false);
857 addr = 0;
858 break;
859 case 4:
860 elf_add_gsym_reloc(s, segment, addr, 0,
861 R_X86_64_32, false);
862 addr = 0;
863 break;
864 case -4:
865 elf_add_gsym_reloc(s, segment, addr, 0,
866 R_X86_64_32S, false);
867 addr = 0;
868 break;
869 case 8:
870 case -8:
871 elf_add_gsym_reloc(s, segment, addr, 0,
872 R_X86_64_64, false);
873 addr = 0;
874 break;
875 default:
876 nasm_error(ERR_PANIC, "internal error elfx32-hpa-903");
877 break;
879 } else if (wrt == elf_plt_sect + 1) {
880 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
881 "relative PLT references");
882 } else {
883 nasm_error(ERR_NONFATAL, "ELF format does not support this"
884 " use of WRT");
887 elf_sect_writeaddr(s, addr, asize);
888 break;
891 case OUT_REL1ADR:
892 reltype = R_X86_64_PC8;
893 bytes = 1;
894 goto rel12adr;
896 case OUT_REL2ADR:
897 reltype = R_X86_64_PC16;
898 bytes = 2;
899 goto rel12adr;
901 rel12adr:
902 addr = *(int64_t *)data - size;
903 if (segment == segto)
904 nasm_error(ERR_PANIC, "intra-segment OUT_REL1ADR");
905 if (segment == NO_SEG) {
906 /* Do nothing */
907 } else if (segment % 2) {
908 nasm_error(ERR_NONFATAL, "ELF format does not support"
909 " segment base references");
910 } else {
911 if (wrt == NO_SEG) {
912 elf_add_reloc(s, segment, addr, reltype);
913 addr = 0;
914 } else {
915 nasm_error(ERR_NONFATAL,
916 "Unsupported non-32-bit ELF relocation");
919 elf_sect_writeaddr(s, addr, bytes);
920 break;
922 case OUT_REL4ADR:
923 addr = *(int64_t *)data - size;
924 if (segment == segto)
925 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
926 if (segment == NO_SEG) {
927 /* Do nothing */
928 } else if (segment % 2) {
929 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
930 " segment base references");
931 } else {
932 if (wrt == NO_SEG) {
933 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
934 addr = 0;
935 } else if (wrt == elf_plt_sect + 1) {
936 elf_add_gsym_reloc(s, segment, addr+size, size,
937 R_X86_64_PLT32, true);
938 addr = 0;
939 } else if (wrt == elf_gotpc_sect + 1 ||
940 wrt == elf_got_sect + 1) {
941 elf_add_gsym_reloc(s, segment, addr+size, size,
942 R_X86_64_GOTPCREL, true);
943 addr = 0;
944 } else if (wrt == elf_gotoff_sect + 1 ||
945 wrt == elf_got_sect + 1) {
946 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
947 } else if (wrt == elf_gottpoff_sect + 1) {
948 elf_add_gsym_reloc(s, segment, addr+size, size,
949 R_X86_64_GOTTPOFF, true);
950 addr = 0;
951 } else {
952 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
953 " use of WRT");
956 elf_sect_writeaddr(s, addr, 4);
957 break;
959 case OUT_REL8ADR:
960 nasm_error(ERR_NONFATAL,
961 "32-bit ELF format does not support 64-bit relocations");
962 addr = 0;
963 elf_sect_writeaddr(s, addr, 8);
964 break;
968 static void elf_write(void)
970 int align;
971 char *p;
972 int i;
974 struct SAA *symtab;
975 int32_t symtablen, symtablocal;
978 * Work out how many sections we will have. We have SHN_UNDEF,
979 * then the flexible user sections, then the fixed sections
980 * `.shstrtab', `.symtab' and `.strtab', then optionally
981 * relocation sections for the user sections.
983 nsections = sec_numspecial + 1;
984 if (of_elfx32.current_dfmt == &df_stabs)
985 nsections += 3;
986 else if (of_elfx32.current_dfmt == &df_dwarf)
987 nsections += 10;
989 add_sectname("", ".shstrtab");
990 add_sectname("", ".symtab");
991 add_sectname("", ".strtab");
992 for (i = 0; i < nsects; i++) {
993 nsections++; /* for the section itself */
994 if (sects[i]->head) {
995 nsections++; /* for its relocations */
996 add_sectname(".rela", sects[i]->name);
1000 if (of_elfx32.current_dfmt == &df_stabs) {
1001 /* in case the debug information is wanted, just add these three sections... */
1002 add_sectname("", ".stab");
1003 add_sectname("", ".stabstr");
1004 add_sectname(".rel", ".stab");
1007 else if (of_elfx32.current_dfmt == &df_dwarf) {
1008 /* the dwarf debug standard specifies the following ten sections,
1009 not all of which are currently implemented,
1010 although all of them are defined. */
1011 add_sectname("", ".debug_aranges");
1012 add_sectname(".rela", ".debug_aranges");
1013 add_sectname("", ".debug_pubnames");
1014 add_sectname("", ".debug_info");
1015 add_sectname(".rela", ".debug_info");
1016 add_sectname("", ".debug_abbrev");
1017 add_sectname("", ".debug_line");
1018 add_sectname(".rela", ".debug_line");
1019 add_sectname("", ".debug_frame");
1020 add_sectname("", ".debug_loc");
1024 * Output the ELF header.
1026 fwrite("\177ELF\1\1\1", 7, 1, ofile);
1027 fputc(elf_osabi, ofile);
1028 fputc(elf_abiver, ofile);
1029 fwritezero(7, ofile);
1030 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1031 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1032 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1033 fwriteint32_t(0L, ofile); /* no entry point */
1034 fwriteint32_t(0L, ofile); /* no program header table */
1035 fwriteint32_t(0x40L, ofile); /* section headers straight after
1036 * ELF header plus alignment */
1037 fwriteint32_t(0L, ofile); /* X86_64 defines no special flags */
1038 fwriteint16_t(0x34, ofile); /* size of ELF header */
1039 fwriteint16_t(0, ofile); /* no program header table, again */
1040 fwriteint16_t(0, ofile); /* still no program header table */
1041 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
1042 fwriteint16_t(nsections, ofile); /* number of sections */
1043 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1044 * section header table */
1045 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1046 fwriteint32_t(0L, ofile);
1047 fwriteint32_t(0L, ofile);
1050 * Build the symbol table and relocation tables.
1052 symtab = elf_build_symtab(&symtablen, &symtablocal);
1053 for (i = 0; i < nsects; i++)
1054 if (sects[i]->head)
1055 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1056 sects[i]->head);
1059 * Now output the section header table.
1062 elf_foffs = 0x40 + sizeof(Elf32_Shdr) * nsections;
1063 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1064 elf_foffs += align;
1065 elf_nsect = 0;
1066 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1068 /* SHN_UNDEF */
1069 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1070 p = shstrtab + 1;
1072 /* The normal sections */
1073 for (i = 0; i < nsects; i++) {
1074 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1075 (sects[i]->type == SHT_PROGBITS ?
1076 sects[i]->data : NULL), true,
1077 sects[i]->len, 0, 0, sects[i]->align, 0);
1078 p += strlen(p) + 1;
1081 /* .shstrtab */
1082 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1083 shstrtablen, 0, 0, 1, 0);
1084 p += strlen(p) + 1;
1086 /* .symtab */
1087 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1088 symtablen, sec_strtab, symtablocal, 4, 16);
1089 p += strlen(p) + 1;
1091 /* .strtab */
1092 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1093 strslen, 0, 0, 1, 0);
1094 p += strlen(p) + 1;
1096 /* The relocation sections */
1097 for (i = 0; i < nsects; i++)
1098 if (sects[i]->head) {
1099 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1100 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1101 p += strlen(p) + 1;
1104 if (of_elfx32.current_dfmt == &df_stabs) {
1105 /* for debugging information, create the last three sections
1106 which are the .stab , .stabstr and .rel.stab sections respectively */
1108 /* this function call creates the stab sections in memory */
1109 stabsx32_generate();
1111 if (stabbuf && stabstrbuf && stabrelbuf) {
1112 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1113 stablen, sec_stabstr, 0, 4, 12);
1114 p += strlen(p) + 1;
1116 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1117 stabstrlen, 0, 0, 4, 0);
1118 p += strlen(p) + 1;
1120 /* link -> symtable info -> section to refer to */
1121 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1122 stabrellen, sec_symtab, sec_stab, 4, 8);
1123 p += strlen(p) + 1;
1125 } else if (of_elfx32.current_dfmt == &df_dwarf) {
1126 /* for dwarf debugging information, create the ten dwarf sections */
1128 /* this function call creates the dwarf sections in memory */
1129 if (dwarf_fsect)
1130 dwarfx32_generate();
1132 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1133 arangeslen, 0, 0, 1, 0);
1134 p += strlen(p) + 1;
1136 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1137 arangesrellen, sec_symtab, sec_debug_aranges, 1, 12);
1138 p += strlen(p) + 1;
1140 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1141 pubnameslen, 0, 0, 1, 0);
1142 p += strlen(p) + 1;
1144 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1145 infolen, 0, 0, 1, 0);
1146 p += strlen(p) + 1;
1148 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1149 inforellen, sec_symtab, sec_debug_info, 1, 12);
1150 p += strlen(p) + 1;
1152 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1153 abbrevlen, 0, 0, 1, 0);
1154 p += strlen(p) + 1;
1156 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1157 linelen, 0, 0, 1, 0);
1158 p += strlen(p) + 1;
1160 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1161 linerellen, sec_symtab, sec_debug_line, 1, 12);
1162 p += strlen(p) + 1;
1164 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1165 framelen, 0, 0, 8, 0);
1166 p += strlen(p) + 1;
1168 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1169 loclen, 0, 0, 1, 0);
1170 p += strlen(p) + 1;
1172 fwritezero(align, ofile);
1175 * Now output the sections.
1177 elf_write_sections();
1179 nasm_free(elf_sects);
1180 saa_free(symtab);
1183 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1185 struct SAA *s = saa_init(1L);
1186 struct Symbol *sym;
1187 uint8_t entry[24], *p;
1188 int i;
1190 *len = *local = 0;
1193 * First, an all-zeros entry, required by the ELF spec.
1195 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1196 *len += 16;
1197 (*local)++;
1200 * Next, an entry for the file name.
1202 p = entry;
1203 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1204 WRITELONG(p, 0); /* no value */
1205 WRITELONG(p, 0); /* no size either */
1206 WRITESHORT(p, STT_FILE); /* type FILE */
1207 WRITESHORT(p, SHN_ABS);
1208 saa_wbytes(s, entry, 16L);
1209 *len += 16;
1210 (*local)++;
1213 * Now some standard symbols defining the segments, for relocation
1214 * purposes.
1216 for (i = 1; i <= nsects; i++) {
1217 p = entry;
1218 WRITELONG(p, 0); /* no symbol name */
1219 WRITELONG(p, 0); /* offset zero */
1220 WRITELONG(p, 0); /* size zero */
1221 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1222 WRITESHORT(p, i); /* section id */
1223 saa_wbytes(s, entry, 16L);
1224 *len += 16;
1225 (*local)++;
1230 * Now the other local symbols.
1232 saa_rewind(syms);
1233 while ((sym = saa_rstruct(syms))) {
1234 if (sym->type & SYM_GLOBAL)
1235 continue;
1236 p = entry;
1237 WRITELONG(p, sym->strpos); /* index into symbol string table */
1238 WRITELONG(p, sym->symv.key); /* value of symbol */
1239 WRITELONG(p, sym->size); /* size of symbol */
1240 WRITECHAR(p, sym->type); /* type and binding */
1241 WRITECHAR(p, sym->other); /* visibility */
1242 WRITESHORT(p, sym->section); /* index into section header table */
1243 saa_wbytes(s, entry, 16L);
1244 *len += 16;
1245 (*local)++;
1248 * dwarf needs symbols for debug sections
1249 * which are relocation targets.
1251 if (of_elfx32.current_dfmt == &df_dwarf) {
1252 dwarf_infosym = *local;
1253 p = entry;
1254 WRITELONG(p, 0); /* no symbol name */
1255 WRITELONG(p, 0); /* offset zero */
1256 WRITELONG(p, 0); /* size zero */
1257 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1258 WRITESHORT(p, sec_debug_info); /* section id */
1259 saa_wbytes(s, entry, 16L);
1260 *len += 16;
1261 (*local)++;
1262 dwarf_abbrevsym = *local;
1263 p = entry;
1264 WRITELONG(p, 0); /* no symbol name */
1265 WRITELONG(p, 0); /* offset zero */
1266 WRITELONG(p, 0); /* size zero */
1267 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1268 WRITESHORT(p, sec_debug_abbrev); /* section id */
1269 saa_wbytes(s, entry, 16L);
1270 *len += 16;
1271 (*local)++;
1272 dwarf_linesym = *local;
1273 p = entry;
1274 WRITELONG(p, 0); /* no symbol name */
1275 WRITELONG(p, 0); /* offset zero */
1276 WRITELONG(p, 0); /* size zero */
1277 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1278 WRITESHORT(p, sec_debug_line); /* section id */
1279 saa_wbytes(s, entry, 16L);
1280 *len += 16;
1281 (*local)++;
1285 * Now the global symbols.
1287 saa_rewind(syms);
1288 while ((sym = saa_rstruct(syms))) {
1289 if (!(sym->type & SYM_GLOBAL))
1290 continue;
1291 p = entry;
1292 WRITELONG(p, sym->strpos);
1293 WRITELONG(p, sym->symv.key);
1294 WRITELONG(p, sym->size);
1295 WRITECHAR(p, sym->type); /* type and binding */
1296 WRITECHAR(p, sym->other); /* visibility */
1297 WRITESHORT(p, sym->section);
1298 saa_wbytes(s, entry, 16L);
1299 *len += 16;
1302 return s;
1305 static struct SAA *elf_build_reltab(uint32_t *len, struct Reloc *r)
1307 struct SAA *s;
1308 uint8_t *p, entry[12];
1309 int32_t global_offset;
1311 if (!r)
1312 return NULL;
1314 s = saa_init(1L);
1315 *len = 0;
1318 * How to onvert from a global placeholder to a real symbol index;
1319 * the +2 refers to the two special entries, the null entry and
1320 * the filename entry.
1322 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1324 while (r) {
1325 int32_t sym = r->symbol;
1327 if (sym >= GLOBAL_TEMP_BASE)
1328 sym += global_offset;
1330 p = entry;
1331 WRITELONG(p, r->address);
1332 WRITELONG(p, (sym << 8) + r->type);
1333 WRITELONG(p, r->offset);
1334 saa_wbytes(s, entry, 12L);
1335 *len += 12;
1337 r = r->next;
1340 return s;
1343 static void elf_section_header(int name, int type, uint32_t flags,
1344 void *data, bool is_saa, uint32_t datalen,
1345 int link, int info, int align, int eltsize)
1347 elf_sects[elf_nsect].data = data;
1348 elf_sects[elf_nsect].len = datalen;
1349 elf_sects[elf_nsect].is_saa = is_saa;
1350 elf_nsect++;
1352 fwriteint32_t((int32_t)name, ofile);
1353 fwriteint32_t((int32_t)type, ofile);
1354 fwriteint32_t((int32_t)flags, ofile);
1355 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1356 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1357 fwriteint32_t(datalen, ofile);
1358 if (data)
1359 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1360 fwriteint32_t((int32_t)link, ofile);
1361 fwriteint32_t((int32_t)info, ofile);
1362 fwriteint32_t((int32_t)align, ofile);
1363 fwriteint32_t((int32_t)eltsize, ofile);
1366 static void elf_write_sections(void)
1368 int i;
1369 for (i = 0; i < elf_nsect; i++)
1370 if (elf_sects[i].data) {
1371 int32_t len = elf_sects[i].len;
1372 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1373 int32_t align = reallen - len;
1374 if (elf_sects[i].is_saa)
1375 saa_fpwrite(elf_sects[i].data, ofile);
1376 else
1377 fwrite(elf_sects[i].data, len, 1, ofile);
1378 fwritezero(align, ofile);
1382 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1384 saa_wbytes(sect->data, data, len);
1385 sect->len += len;
1387 static void elf_sect_writeaddr(struct Section *sect, int32_t data, size_t len)
1389 saa_writeaddr(sect->data, data, len);
1390 sect->len += len;
1393 static void elf_sectalign(int32_t seg, unsigned int value)
1395 struct Section *s = NULL;
1396 int i;
1398 for (i = 0; i < nsects; i++) {
1399 if (sects[i]->index == seg) {
1400 s = sects[i];
1401 break;
1404 if (!s || !is_power2(value))
1405 return;
1407 if (value > s->align)
1408 s->align = value;
1411 static int32_t elf_segbase(int32_t segment)
1413 return segment;
1416 static int elf_directive(enum directives directive, char *value, int pass)
1418 bool err;
1419 int64_t n;
1420 char *p;
1422 switch (directive) {
1423 case D_OSABI:
1424 if (pass == 2)
1425 return 1; /* ignore in pass 2 */
1427 n = readnum(value, &err);
1428 if (err) {
1429 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1430 return 1;
1432 if (n < 0 || n > 255) {
1433 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1434 return 1;
1436 elf_osabi = n;
1437 elf_abiver = 0;
1439 if ((p = strchr(value,',')) == NULL)
1440 return 1;
1442 n = readnum(p+1, &err);
1443 if (err || n < 0 || n > 255) {
1444 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1445 return 1;
1448 elf_abiver = n;
1449 return 1;
1451 default:
1452 return 0;
1456 static void elf_filename(char *inname, char *outname)
1458 strcpy(elf_module, inname);
1459 standard_extension(inname, outname, ".o");
1462 extern macros_t elf_stdmac[];
1464 static int elf_set_info(enum geninfo type, char **val)
1466 (void)type;
1467 (void)val;
1468 return 0;
1470 static struct dfmt df_dwarf = {
1471 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1472 "dwarf",
1473 dwarfx32_init,
1474 dwarfx32_linenum,
1475 debugx32_deflabel,
1476 debugx32_directive,
1477 debugx32_typevalue,
1478 dwarfx32_output,
1479 dwarfx32_cleanup
1481 static struct dfmt df_stabs = {
1482 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1483 "stabs",
1484 null_debug_init,
1485 stabsx32_linenum,
1486 debugx32_deflabel,
1487 debugx32_directive,
1488 debugx32_typevalue,
1489 stabsx32_output,
1490 stabsx32_cleanup
1493 struct dfmt *elfx32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1495 struct ofmt of_elfx32 = {
1496 "ELFX32 (x86_64) object files (e.g. Linux)",
1497 "elfx32",
1499 elfx32_debugs_arr,
1500 &df_stabs,
1501 elf_stdmac,
1502 elf_init,
1503 elf_set_info,
1504 elf_out,
1505 elf_deflabel,
1506 elf_section_names,
1507 elf_sectalign,
1508 elf_segbase,
1509 elf_directive,
1510 elf_filename,
1511 elf_cleanup
1514 /* common debugging routines */
1515 static void debugx32_deflabel(char *name, int32_t segment, int64_t offset,
1516 int is_global, char *special)
1518 (void)name;
1519 (void)segment;
1520 (void)offset;
1521 (void)is_global;
1522 (void)special;
1525 static void debugx32_directive(const char *directive, const char *params)
1527 (void)directive;
1528 (void)params;
1531 static void debugx32_typevalue(int32_t type)
1533 int32_t stype, ssize;
1534 switch (TYM_TYPE(type)) {
1535 case TY_LABEL:
1536 ssize = 0;
1537 stype = STT_NOTYPE;
1538 break;
1539 case TY_BYTE:
1540 ssize = 1;
1541 stype = STT_OBJECT;
1542 break;
1543 case TY_WORD:
1544 ssize = 2;
1545 stype = STT_OBJECT;
1546 break;
1547 case TY_DWORD:
1548 ssize = 4;
1549 stype = STT_OBJECT;
1550 break;
1551 case TY_FLOAT:
1552 ssize = 4;
1553 stype = STT_OBJECT;
1554 break;
1555 case TY_QWORD:
1556 ssize = 8;
1557 stype = STT_OBJECT;
1558 break;
1559 case TY_TBYTE:
1560 ssize = 10;
1561 stype = STT_OBJECT;
1562 break;
1563 case TY_OWORD:
1564 ssize = 16;
1565 stype = STT_OBJECT;
1566 break;
1567 case TY_YWORD:
1568 ssize = 32;
1569 stype = STT_OBJECT;
1570 break;
1571 case TY_COMMON:
1572 ssize = 0;
1573 stype = STT_COMMON;
1574 break;
1575 case TY_SEG:
1576 ssize = 0;
1577 stype = STT_SECTION;
1578 break;
1579 case TY_EXTERN:
1580 ssize = 0;
1581 stype = STT_NOTYPE;
1582 break;
1583 case TY_EQU:
1584 ssize = 0;
1585 stype = STT_NOTYPE;
1586 break;
1587 default:
1588 ssize = 0;
1589 stype = STT_NOTYPE;
1590 break;
1592 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1593 lastsym->size = ssize;
1594 lastsym->type = stype;
1598 /* stabs debugging routines */
1600 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1602 (void)segto;
1603 if (!stabs_filename) {
1604 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1605 strcpy(stabs_filename, filename);
1606 } else {
1607 if (strcmp(stabs_filename, filename)) {
1608 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1609 in fact, this leak comes in quite handy to maintain a list of files
1610 encountered so far in the symbol lines... */
1612 /* why not nasm_free(stabs_filename); we're done with the old one */
1614 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1615 strcpy(stabs_filename, filename);
1618 debug_immcall = 1;
1619 currentline = linenumber;
1623 static void stabsx32_output(int type, void *param)
1625 struct symlininfo *s;
1626 struct linelist *el;
1627 if (type == TY_DEBUGSYMLIN) {
1628 if (debug_immcall) {
1629 s = (struct symlininfo *)param;
1630 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1631 return; /* line info is only collected for executable sections */
1632 numlinestabs++;
1633 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1634 el->info.offset = s->offset;
1635 el->info.section = s->section;
1636 el->info.name = s->name;
1637 el->line = currentline;
1638 el->filename = stabs_filename;
1639 el->next = 0;
1640 if (stabslines) {
1641 stabslines->last->next = el;
1642 stabslines->last = el;
1643 } else {
1644 stabslines = el;
1645 stabslines->last = el;
1649 debug_immcall = 0;
1652 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1654 static void stabsx32_generate(void)
1656 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1657 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1658 char **allfiles;
1659 int *fileidx;
1661 struct linelist *ptr;
1663 ptr = stabslines;
1665 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1666 numfiles = 0;
1667 while (ptr) {
1668 if (numfiles == 0) {
1669 allfiles[0] = ptr->filename;
1670 numfiles++;
1671 } else {
1672 for (i = 0; i < numfiles; i++) {
1673 if (!strcmp(allfiles[i], ptr->filename))
1674 break;
1676 if (i >= numfiles) {
1677 allfiles[i] = ptr->filename;
1678 numfiles++;
1681 ptr = ptr->next;
1683 strsize = 1;
1684 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1685 for (i = 0; i < numfiles; i++) {
1686 fileidx[i] = strsize;
1687 strsize += strlen(allfiles[i]) + 1;
1689 mainfileindex = 0;
1690 for (i = 0; i < numfiles; i++) {
1691 if (!strcmp(allfiles[i], elf_module)) {
1692 mainfileindex = i;
1693 break;
1698 * worst case size of the stab buffer would be:
1699 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1700 * plus one "ending" entry
1702 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1703 sizeof(struct stabentry));
1704 ssbuf = (uint8_t *)nasm_malloc(strsize);
1705 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1706 rptr = rbuf;
1708 for (i = 0; i < numfiles; i++)
1709 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1710 ssbuf[0] = 0;
1712 stabstrlen = strsize; /* set global variable for length of stab strings */
1714 sptr = sbuf;
1715 ptr = stabslines;
1716 numstabs = 0;
1718 if (ptr) {
1720 * this is the first stab, its strx points to the filename of the
1721 * the source-file, the n_desc field should be set to the number
1722 * of remaining stabs
1724 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1726 /* this is the stab for the main source file */
1727 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1729 /* relocation table entry */
1732 * Since the symbol table has two entries before
1733 * the section symbols, the index in the info.section
1734 * member must be adjusted by adding 2
1737 WRITELONG(rptr, (sptr - sbuf) - 4);
1738 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1740 numstabs++;
1741 currfile = mainfileindex;
1744 while (ptr) {
1745 if (strcmp(allfiles[currfile], ptr->filename)) {
1746 /* oops file has changed... */
1747 for (i = 0; i < numfiles; i++)
1748 if (!strcmp(allfiles[i], ptr->filename))
1749 break;
1750 currfile = i;
1751 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1752 ptr->info.offset);
1753 numstabs++;
1755 /* relocation table entry */
1757 WRITELONG(rptr, (sptr - sbuf) - 4);
1758 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1761 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1762 numstabs++;
1764 /* relocation table entry */
1766 WRITELONG(rptr, (sptr - sbuf) - 4);
1767 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1769 ptr = ptr->next;
1773 /* this is an "ending" token */
1774 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1775 numstabs++;
1777 ((struct stabentry *)sbuf)->n_desc = numstabs;
1779 nasm_free(allfiles);
1780 nasm_free(fileidx);
1782 stablen = (sptr - sbuf);
1783 stabrellen = (rptr - rbuf);
1784 stabrelbuf = rbuf;
1785 stabbuf = sbuf;
1786 stabstrbuf = ssbuf;
1789 static void stabsx32_cleanup(void)
1791 struct linelist *ptr, *del;
1792 if (!stabslines)
1793 return;
1795 ptr = stabslines;
1796 while (ptr) {
1797 del = ptr;
1798 ptr = ptr->next;
1799 nasm_free(del);
1802 nasm_free(stabbuf);
1803 nasm_free(stabrelbuf);
1804 nasm_free(stabstrbuf);
1807 /* dwarf routines */
1809 static void dwarfx32_init(void)
1811 ndebugs = 3; /* 3 debug symbols */
1814 static void dwarfx32_linenum(const char *filename, int32_t linenumber,
1815 int32_t segto)
1817 (void)segto;
1818 dwarfx32_findfile(filename);
1819 debug_immcall = 1;
1820 currentline = linenumber;
1823 /* called from elf_out with type == TY_DEBUGSYMLIN */
1824 static void dwarfx32_output(int type, void *param)
1826 int ln, aa, inx, maxln, soc;
1827 struct symlininfo *s;
1828 struct SAA *plinep;
1830 (void)type;
1832 s = (struct symlininfo *)param;
1834 /* line number info is only gathered for executable sections */
1835 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1836 return;
1838 /* Check if section index has changed */
1839 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1840 dwarfx32_findsect(s->section);
1842 /* do nothing unless line or file has changed */
1843 if (!debug_immcall)
1844 return;
1846 ln = currentline - dwarf_csect->line;
1847 aa = s->offset - dwarf_csect->offset;
1848 inx = dwarf_clist->line;
1849 plinep = dwarf_csect->psaa;
1850 /* check for file change */
1851 if (!(inx == dwarf_csect->file)) {
1852 saa_write8(plinep,DW_LNS_set_file);
1853 saa_write8(plinep,inx);
1854 dwarf_csect->file = inx;
1856 /* check for line change */
1857 if (ln) {
1858 /* test if in range of special op code */
1859 maxln = line_base + line_range;
1860 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1861 if (ln >= line_base && ln < maxln && soc < 256) {
1862 saa_write8(plinep,soc);
1863 } else {
1864 saa_write8(plinep,DW_LNS_advance_line);
1865 saa_wleb128s(plinep,ln);
1866 if (aa) {
1867 saa_write8(plinep,DW_LNS_advance_pc);
1868 saa_wleb128u(plinep,aa);
1871 dwarf_csect->line = currentline;
1872 dwarf_csect->offset = s->offset;
1875 /* show change handled */
1876 debug_immcall = 0;
1880 static void dwarfx32_generate(void)
1882 uint8_t *pbuf;
1883 int indx;
1884 struct linelist *ftentry;
1885 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1886 struct SAA *parangesrel, *plinesrel, *pinforel;
1887 struct sectlist *psect;
1888 size_t saalen, linepoff, totlen, highaddr;
1890 /* write epilogues for each line program range */
1891 /* and build aranges section */
1892 paranges = saa_init(1L);
1893 parangesrel = saa_init(1L);
1894 saa_write16(paranges,3); /* dwarf version */
1895 saa_write32(parangesrel, paranges->datalen+4);
1896 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
1897 saa_write32(parangesrel, 0);
1898 saa_write32(paranges,0); /* offset into info */
1899 saa_write8(paranges,4); /* pointer size */
1900 saa_write8(paranges,0); /* not segmented */
1901 saa_write32(paranges,0); /* padding */
1902 /* iterate though sectlist entries */
1903 psect = dwarf_fsect;
1904 totlen = 0;
1905 highaddr = 0;
1906 for (indx = 0; indx < dwarf_nsections; indx++)
1908 plinep = psect->psaa;
1909 /* Line Number Program Epilogue */
1910 saa_write8(plinep,2); /* std op 2 */
1911 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1912 saa_write8(plinep,DW_LNS_extended_op);
1913 saa_write8(plinep,1); /* operand length */
1914 saa_write8(plinep,DW_LNE_end_sequence);
1915 totlen += plinep->datalen;
1916 /* range table relocation entry */
1917 saa_write32(parangesrel, paranges->datalen + 4);
1918 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
1919 saa_write32(parangesrel, (uint32_t) 0);
1920 /* range table entry */
1921 saa_write32(paranges,0x0000); /* range start */
1922 saa_write32(paranges,sects[psect->section]->len); /* range length */
1923 highaddr += sects[psect->section]->len;
1924 /* done with this entry */
1925 psect = psect->next;
1927 saa_write32(paranges,0); /* null address */
1928 saa_write32(paranges,0); /* null length */
1929 saalen = paranges->datalen;
1930 arangeslen = saalen + 4;
1931 arangesbuf = pbuf = nasm_malloc(arangeslen);
1932 WRITELONG(pbuf,saalen); /* initial length */
1933 saa_rnbytes(paranges, pbuf, saalen);
1934 saa_free(paranges);
1936 /* build rela.aranges section */
1937 arangesrellen = saalen = parangesrel->datalen;
1938 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1939 saa_rnbytes(parangesrel, pbuf, saalen);
1940 saa_free(parangesrel);
1942 /* build pubnames section */
1943 ppubnames = saa_init(1L);
1944 saa_write16(ppubnames,3); /* dwarf version */
1945 saa_write32(ppubnames,0); /* offset into info */
1946 saa_write32(ppubnames,0); /* space used in info */
1947 saa_write32(ppubnames,0); /* end of list */
1948 saalen = ppubnames->datalen;
1949 pubnameslen = saalen + 4;
1950 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1951 WRITELONG(pbuf,saalen); /* initial length */
1952 saa_rnbytes(ppubnames, pbuf, saalen);
1953 saa_free(ppubnames);
1955 /* build info section */
1956 pinfo = saa_init(1L);
1957 pinforel = saa_init(1L);
1958 saa_write16(pinfo,3); /* dwarf version */
1959 saa_write32(pinforel, pinfo->datalen + 4);
1960 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
1961 saa_write32(pinforel, 0);
1962 saa_write32(pinfo,0); /* offset into abbrev */
1963 saa_write8(pinfo,4); /* pointer size */
1964 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1965 saa_write32(pinforel, pinfo->datalen + 4);
1966 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1967 saa_write32(pinforel, 0);
1968 saa_write32(pinfo,0); /* DW_AT_low_pc */
1969 saa_write32(pinforel, pinfo->datalen + 4);
1970 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1971 saa_write32(pinforel, 0);
1972 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1973 saa_write32(pinforel, pinfo->datalen + 4);
1974 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
1975 saa_write32(pinforel, 0);
1976 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1977 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1978 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1979 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1980 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1981 saa_write32(pinforel, pinfo->datalen + 4);
1982 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1983 saa_write32(pinforel, 0);
1984 saa_write32(pinfo,0); /* DW_AT_low_pc */
1985 saa_write32(pinfo,0); /* DW_AT_frame_base */
1986 saa_write8(pinfo,0); /* end of entries */
1987 saalen = pinfo->datalen;
1988 infolen = saalen + 4;
1989 infobuf = pbuf = nasm_malloc(infolen);
1990 WRITELONG(pbuf,saalen); /* initial length */
1991 saa_rnbytes(pinfo, pbuf, saalen);
1992 saa_free(pinfo);
1994 /* build rela.info section */
1995 inforellen = saalen = pinforel->datalen;
1996 inforelbuf = pbuf = nasm_malloc(inforellen);
1997 saa_rnbytes(pinforel, pbuf, saalen);
1998 saa_free(pinforel);
2000 /* build abbrev section */
2001 pabbrev = saa_init(1L);
2002 saa_write8(pabbrev,1); /* entry number LEB128u */
2003 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2004 saa_write8(pabbrev,1); /* has children */
2005 /* the following attributes and forms are all LEB128u values */
2006 saa_write8(pabbrev,DW_AT_low_pc);
2007 saa_write8(pabbrev,DW_FORM_addr);
2008 saa_write8(pabbrev,DW_AT_high_pc);
2009 saa_write8(pabbrev,DW_FORM_addr);
2010 saa_write8(pabbrev,DW_AT_stmt_list);
2011 saa_write8(pabbrev,DW_FORM_data4);
2012 saa_write8(pabbrev,DW_AT_name);
2013 saa_write8(pabbrev,DW_FORM_string);
2014 saa_write8(pabbrev,DW_AT_producer);
2015 saa_write8(pabbrev,DW_FORM_string);
2016 saa_write8(pabbrev,DW_AT_language);
2017 saa_write8(pabbrev,DW_FORM_data2);
2018 saa_write16(pabbrev,0); /* end of entry */
2019 /* LEB128u usage same as above */
2020 saa_write8(pabbrev,2); /* entry number */
2021 saa_write8(pabbrev,DW_TAG_subprogram);
2022 saa_write8(pabbrev,0); /* no children */
2023 saa_write8(pabbrev,DW_AT_low_pc);
2024 saa_write8(pabbrev,DW_FORM_addr);
2025 saa_write8(pabbrev,DW_AT_frame_base);
2026 saa_write8(pabbrev,DW_FORM_data4);
2027 saa_write16(pabbrev,0); /* end of entry */
2028 abbrevlen = saalen = pabbrev->datalen;
2029 abbrevbuf = pbuf = nasm_malloc(saalen);
2030 saa_rnbytes(pabbrev, pbuf, saalen);
2031 saa_free(pabbrev);
2033 /* build line section */
2034 /* prolog */
2035 plines = saa_init(1L);
2036 saa_write8(plines,1); /* Minimum Instruction Length */
2037 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2038 saa_write8(plines,line_base); /* Line Base */
2039 saa_write8(plines,line_range); /* Line Range */
2040 saa_write8(plines,opcode_base); /* Opcode Base */
2041 /* standard opcode lengths (# of LEB128u operands) */
2042 saa_write8(plines,0); /* Std opcode 1 length */
2043 saa_write8(plines,1); /* Std opcode 2 length */
2044 saa_write8(plines,1); /* Std opcode 3 length */
2045 saa_write8(plines,1); /* Std opcode 4 length */
2046 saa_write8(plines,1); /* Std opcode 5 length */
2047 saa_write8(plines,0); /* Std opcode 6 length */
2048 saa_write8(plines,0); /* Std opcode 7 length */
2049 saa_write8(plines,0); /* Std opcode 8 length */
2050 saa_write8(plines,1); /* Std opcode 9 length */
2051 saa_write8(plines,0); /* Std opcode 10 length */
2052 saa_write8(plines,0); /* Std opcode 11 length */
2053 saa_write8(plines,1); /* Std opcode 12 length */
2054 /* Directory Table */
2055 saa_write8(plines,0); /* End of table */
2056 /* File Name Table */
2057 ftentry = dwarf_flist;
2058 for (indx = 0;indx<dwarf_numfiles;indx++)
2060 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2061 saa_write8(plines,0); /* directory LEB128u */
2062 saa_write8(plines,0); /* time LEB128u */
2063 saa_write8(plines,0); /* size LEB128u */
2064 ftentry = ftentry->next;
2066 saa_write8(plines,0); /* End of table */
2067 linepoff = plines->datalen;
2068 linelen = linepoff + totlen + 10;
2069 linebuf = pbuf = nasm_malloc(linelen);
2070 WRITELONG(pbuf,linelen-4); /* initial length */
2071 WRITESHORT(pbuf,3); /* dwarf version */
2072 WRITELONG(pbuf,linepoff); /* offset to line number program */
2073 /* write line header */
2074 saalen = linepoff;
2075 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2076 pbuf += linepoff;
2077 saa_free(plines);
2078 /* concatonate line program ranges */
2079 linepoff += 13;
2080 plinesrel = saa_init(1L);
2081 psect = dwarf_fsect;
2082 for (indx = 0; indx < dwarf_nsections; indx++) {
2083 saa_write32(plinesrel, linepoff);
2084 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
2085 saa_write32(plinesrel, 0);
2086 plinep = psect->psaa;
2087 saalen = plinep->datalen;
2088 saa_rnbytes(plinep, pbuf, saalen);
2089 pbuf += saalen;
2090 linepoff += saalen;
2091 saa_free(plinep);
2092 /* done with this entry */
2093 psect = psect->next;
2097 /* build rela.lines section */
2098 linerellen =saalen = plinesrel->datalen;
2099 linerelbuf = pbuf = nasm_malloc(linerellen);
2100 saa_rnbytes(plinesrel, pbuf, saalen);
2101 saa_free(plinesrel);
2103 /* build frame section */
2104 framelen = 4;
2105 framebuf = pbuf = nasm_malloc(framelen);
2106 WRITELONG(pbuf,framelen-4); /* initial length */
2108 /* build loc section */
2109 loclen = 16;
2110 locbuf = pbuf = nasm_malloc(loclen);
2111 WRITELONG(pbuf,0); /* null beginning offset */
2112 WRITELONG(pbuf,0); /* null ending offset */
2115 static void dwarfx32_cleanup(void)
2117 nasm_free(arangesbuf);
2118 nasm_free(arangesrelbuf);
2119 nasm_free(pubnamesbuf);
2120 nasm_free(infobuf);
2121 nasm_free(inforelbuf);
2122 nasm_free(abbrevbuf);
2123 nasm_free(linebuf);
2124 nasm_free(linerelbuf);
2125 nasm_free(framebuf);
2126 nasm_free(locbuf);
2129 static void dwarfx32_findfile(const char * fname)
2131 int finx;
2132 struct linelist *match;
2134 /* return if fname is current file name */
2135 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2136 return;
2138 /* search for match */
2139 match = 0;
2140 if (dwarf_flist) {
2141 match = dwarf_flist;
2142 for (finx = 0; finx < dwarf_numfiles; finx++) {
2143 if (!(strcmp(fname, match->filename))) {
2144 dwarf_clist = match;
2145 return;
2150 /* add file name to end of list */
2151 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2152 dwarf_numfiles++;
2153 dwarf_clist->line = dwarf_numfiles;
2154 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2155 strcpy(dwarf_clist->filename,fname);
2156 dwarf_clist->next = 0;
2157 if (!dwarf_flist) { /* if first entry */
2158 dwarf_flist = dwarf_elist = dwarf_clist;
2159 dwarf_clist->last = 0;
2160 } else { /* chain to previous entry */
2161 dwarf_elist->next = dwarf_clist;
2162 dwarf_elist = dwarf_clist;
2166 static void dwarfx32_findsect(const int index)
2168 int sinx;
2169 struct sectlist *match;
2170 struct SAA *plinep;
2172 /* return if index is current section index */
2173 if (dwarf_csect && (dwarf_csect->section == index))
2174 return;
2176 /* search for match */
2177 match = 0;
2178 if (dwarf_fsect) {
2179 match = dwarf_fsect;
2180 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2181 if (match->section == index) {
2182 dwarf_csect = match;
2183 return;
2185 match = match->next;
2189 /* add entry to end of list */
2190 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2191 dwarf_nsections++;
2192 dwarf_csect->psaa = plinep = saa_init(1L);
2193 dwarf_csect->line = 1;
2194 dwarf_csect->offset = 0;
2195 dwarf_csect->file = 1;
2196 dwarf_csect->section = index;
2197 dwarf_csect->next = 0;
2198 /* set relocatable address at start of line program */
2199 saa_write8(plinep,DW_LNS_extended_op);
2200 saa_write8(plinep,5); /* operand length */
2201 saa_write8(plinep,DW_LNE_set_address);
2202 saa_write32(plinep,0); /* Start Address */
2204 if (!dwarf_fsect) { /* if first entry */
2205 dwarf_fsect = dwarf_esect = dwarf_csect;
2206 dwarf_csect->last = 0;
2207 } else { /* chain to previous entry */
2208 dwarf_esect->next = dwarf_csect;
2209 dwarf_esect = dwarf_csect;
2213 #endif /* OF_ELFX32 */