Get rid of a bunch of unnecessary indirections
[nasm.git] / output / outelf64.c
blob5b4eee86d3656cbe9098368a61eedfcded431b48
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 * outelf64.c output routines for the Netwide Assembler to produce
36 * ELF64 (x86_64 of course) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELF64
63 #define SECT_DELTA 32
64 static struct elf_section **sects;
65 static int nsects, sectlen;
67 #define SHSTR_DELTA 256
68 static char *shstrtab;
69 static int shstrtablen, shstrtabsize;
71 static struct SAA *syms;
72 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
74 static int32_t def_seg;
76 static struct RAA *bsym;
78 static struct SAA *strs;
79 static uint32_t strslen;
81 static struct elf_symbol *fwds;
83 static char elf_module[FILENAME_MAX];
85 extern struct ofmt of_elf64;
87 static struct ELF_SECTDATA {
88 void *data;
89 int64_t len;
90 bool is_saa;
91 } *elf_sects;
92 static int elf_nsect, nsections;
93 static int64_t elf_foffs;
95 static void elf_write(void);
96 static void elf_sect_write(struct elf_section *, const void *, size_t);
97 static void elf_sect_writeaddr(struct elf_section *, int64_t, size_t);
98 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
99 int, int);
100 static void elf_write_sections(void);
101 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
102 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
103 static void add_sectname(char *, char *);
105 struct erel {
106 int offset, info;
109 struct symlininfo {
110 int offset;
111 int section; /* index into sects[] */
112 int segto; /* internal section number */
113 char *name; /* shallow-copied pointer of section name */
116 struct linelist {
117 struct linelist *next;
118 struct linelist *last;
119 struct symlininfo info;
120 char *filename;
121 int line;
124 struct sectlist {
125 struct SAA *psaa;
126 int section;
127 int line;
128 int offset;
129 int file;
130 struct sectlist *next;
131 struct sectlist *last;
134 /* common debug variables */
135 static int currentline = 1;
136 static int debug_immcall = 0;
138 /* stabs debug variables */
139 static struct linelist *stabslines = 0;
140 static int numlinestabs = 0;
141 static char *stabs_filename = 0;
142 static int symtabsection;
143 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
144 static int stablen, stabstrlen, stabrellen;
146 /* dwarf debug variables */
147 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
148 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
149 static int dwarf_numfiles = 0, dwarf_nsections;
150 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
151 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
152 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
153 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
154 abbrevlen, linelen, linerellen, framelen, loclen;
155 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
158 static struct dfmt df_dwarf;
159 static struct dfmt df_stabs;
160 static struct elf_symbol *lastsym;
162 /* common debugging routines */
163 static void debug64_typevalue(int32_t);
164 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
165 static void debug64_directive(const char *, const char *);
167 /* stabs debugging routines */
168 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
169 static void stabs64_output(int, void *);
170 static void stabs64_generate(void);
171 static void stabs64_cleanup(void);
173 /* dwarf debugging routines */
174 static void dwarf64_init(void);
175 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
176 static void dwarf64_output(int, void *);
177 static void dwarf64_generate(void);
178 static void dwarf64_cleanup(void);
179 static void dwarf64_findfile(const char *);
180 static void dwarf64_findsect(const int);
183 * Special section numbers which are used to define ELF special
184 * symbols, which can be used with WRT to provide PIC relocation
185 * types.
187 static int32_t elf_gotpc_sect, elf_gotoff_sect;
188 static int32_t elf_got_sect, elf_plt_sect;
189 static int32_t elf_sym_sect;
190 static int32_t elf_gottpoff_sect;
192 static void elf_init(void)
194 sects = NULL;
195 nsects = sectlen = 0;
196 syms = saa_init((int32_t)sizeof(struct elf_symbol));
197 nlocals = nglobs = ndebugs = 0;
198 bsym = raa_init();
199 strs = saa_init(1L);
200 saa_wbytes(strs, "\0", 1L);
201 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
202 strslen = 2 + strlen(elf_module);
203 shstrtab = NULL;
204 shstrtablen = shstrtabsize = 0;;
205 add_sectname("", "");
207 fwds = NULL;
209 elf_gotpc_sect = seg_alloc();
210 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
211 elf_gotoff_sect = seg_alloc();
212 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
213 elf_got_sect = seg_alloc();
214 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
215 elf_plt_sect = seg_alloc();
216 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
217 elf_sym_sect = seg_alloc();
218 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
219 elf_gottpoff_sect = seg_alloc();
220 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
222 def_seg = seg_alloc();
226 static void elf_cleanup(int debuginfo)
228 struct elf_reloc *r;
229 int i;
231 (void)debuginfo;
233 elf_write();
234 for (i = 0; i < nsects; i++) {
235 if (sects[i]->type != SHT_NOBITS)
236 saa_free(sects[i]->data);
237 if (sects[i]->head)
238 saa_free(sects[i]->rel);
239 while (sects[i]->head) {
240 r = sects[i]->head;
241 sects[i]->head = sects[i]->head->next;
242 nasm_free(r);
245 nasm_free(sects);
246 saa_free(syms);
247 raa_free(bsym);
248 saa_free(strs);
249 if (of_elf64.current_dfmt) {
250 of_elf64.current_dfmt->cleanup();
254 /* add entry to the elf .shstrtab section */
255 static void add_sectname(char *firsthalf, char *secondhalf)
257 int len = strlen(firsthalf) + strlen(secondhalf);
258 while (shstrtablen + len + 1 > shstrtabsize)
259 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
260 strcpy(shstrtab + shstrtablen, firsthalf);
261 strcat(shstrtab + shstrtablen, secondhalf);
262 shstrtablen += len + 1;
265 static int elf_make_section(char *name, int type, int flags, int align)
267 struct elf_section *s;
269 s = nasm_zalloc(sizeof(*s));
271 if (type != SHT_NOBITS)
272 s->data = saa_init(1L);
273 s->tail = &s->head;
274 if (!strcmp(name, ".text"))
275 s->index = def_seg;
276 else
277 s->index = seg_alloc();
278 add_sectname("", name);
280 s->name = nasm_strdup(name);
281 s->type = type;
282 s->flags = flags;
283 s->align = align;
285 if (nsects >= sectlen)
286 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
287 sects[nsects++] = s;
289 return nsects - 1;
292 static int32_t elf_section_names(char *name, int pass, int *bits)
294 char *p;
295 uint32_t flags, flags_and, flags_or;
296 uint64_t align;
297 int type, i;
300 * Default is 64 bits.
302 if (!name) {
303 *bits = 64;
304 return def_seg;
307 p = nasm_skip_word(name);
308 if (*p)
309 *p++ = '\0';
310 flags_and = flags_or = type = align = 0;
312 elf_section_attrib(name, p, pass, &flags_and,
313 &flags_or, &align, &type);
315 if (!strcmp(name, ".shstrtab") ||
316 !strcmp(name, ".symtab") ||
317 !strcmp(name, ".strtab")) {
318 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
319 "name `%s'", name);
320 return NO_SEG;
323 for (i = 0; i < nsects; i++)
324 if (!strcmp(name, sects[i]->name))
325 break;
326 if (i == nsects) {
327 const struct elf_known_section *ks = elf_known_sections;
329 while (ks->name) {
330 if (!strcmp(name, ks->name))
331 break;
332 ks++;
335 type = type ? type : ks->type;
336 align = align ? align : ks->align;
337 flags = (ks->flags & ~flags_and) | flags_or;
339 i = elf_make_section(name, type, flags, align);
340 } else if (pass == 1) {
341 if ((type && sects[i]->type != type)
342 || (align && sects[i]->align != align)
343 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
344 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
345 " redeclaration of section `%s'", name);
348 return sects[i]->index;
351 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
352 int is_global, char *special)
354 int pos = strslen;
355 struct elf_symbol *sym;
356 bool special_used = false;
358 #if defined(DEBUG) && DEBUG>2
359 nasm_error(ERR_DEBUG,
360 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
361 name, segment, offset, is_global, special);
362 #endif
363 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
365 * This is a NASM special symbol. We never allow it into
366 * the ELF symbol table, even if it's a valid one. If it
367 * _isn't_ a valid one, we should barf immediately.
369 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
370 strcmp(name, "..got") && strcmp(name, "..plt") &&
371 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
372 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
373 return;
376 if (is_global == 3) {
377 struct elf_symbol **s;
379 * Fix up a forward-reference symbol size from the first
380 * pass.
382 for (s = &fwds; *s; s = &(*s)->nextfwd)
383 if (!strcmp((*s)->name, name)) {
384 struct tokenval tokval;
385 expr *e;
386 char *p = nasm_skip_spaces(nasm_skip_word(special));
388 stdscan_reset();
389 stdscan_set(p);
390 tokval.t_type = TOKEN_INVALID;
391 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
392 if (e) {
393 if (!is_simple(e))
394 nasm_error(ERR_NONFATAL, "cannot use relocatable"
395 " expression as symbol size");
396 else
397 (*s)->size = reloc_value(e);
401 * Remove it from the list of unresolved sizes.
403 nasm_free((*s)->name);
404 *s = (*s)->nextfwd;
405 return;
407 return; /* it wasn't an important one */
410 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
411 strslen += 1 + strlen(name);
413 lastsym = sym = saa_wstruct(syms);
415 memset(&sym->symv, 0, sizeof(struct rbtree));
417 sym->strpos = pos;
418 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
419 sym->other = STV_DEFAULT;
420 sym->size = 0;
421 if (segment == NO_SEG)
422 sym->section = SHN_ABS;
423 else {
424 int i;
425 sym->section = SHN_UNDEF;
426 if (segment == def_seg) {
427 /* we have to be sure at least text section is there */
428 int tempint;
429 if (segment != elf_section_names(".text", 2, &tempint))
430 nasm_panic(0, "strange segment conditions in ELF driver");
432 for (i = 0; i < nsects; i++) {
433 if (segment == sects[i]->index) {
434 sym->section = i + 1;
435 break;
440 if (is_global == 2) {
441 sym->size = offset;
442 sym->symv.key = 0;
443 sym->section = SHN_COMMON;
445 * We have a common variable. Check the special text to see
446 * if it's a valid number and power of two; if so, store it
447 * as the alignment for the common variable.
449 if (special) {
450 bool err;
451 sym->symv.key = readnum(special, &err);
452 if (err)
453 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
454 " valid number", special);
455 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
456 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
457 " power of two", special);
459 special_used = true;
460 } else
461 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
463 if (sym->type == SYM_GLOBAL) {
465 * If sym->section == SHN_ABS, then the first line of the
466 * else section would cause a core dump, because its a reference
467 * beyond the end of the section array.
468 * This behaviour is exhibited by this code:
469 * GLOBAL crash_nasm
470 * crash_nasm equ 0
471 * To avoid such a crash, such requests are silently discarded.
472 * This may not be the best solution.
474 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
475 bsym = raa_write(bsym, segment, nglobs);
476 } else if (sym->section != SHN_ABS) {
478 * This is a global symbol; so we must add it to the rbtree
479 * of global symbols in its section.
481 * In addition, we check the special text for symbol
482 * type and size information.
484 sects[sym->section-1]->gsyms =
485 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
487 if (special) {
488 int n = strcspn(special, " \t");
490 if (!nasm_strnicmp(special, "function", n))
491 sym->type |= STT_FUNC;
492 else if (!nasm_strnicmp(special, "data", n) ||
493 !nasm_strnicmp(special, "object", n))
494 sym->type |= STT_OBJECT;
495 else if (!nasm_strnicmp(special, "notype", n))
496 sym->type |= STT_NOTYPE;
497 else
498 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
499 n, special);
500 special += n;
502 special = nasm_skip_spaces(special);
503 if (*special) {
504 n = strcspn(special, " \t");
505 if (!nasm_strnicmp(special, "default", n))
506 sym->other = STV_DEFAULT;
507 else if (!nasm_strnicmp(special, "internal", n))
508 sym->other = STV_INTERNAL;
509 else if (!nasm_strnicmp(special, "hidden", n))
510 sym->other = STV_HIDDEN;
511 else if (!nasm_strnicmp(special, "protected", n))
512 sym->other = STV_PROTECTED;
513 else
514 n = 0;
515 special += n;
518 if (*special) {
519 struct tokenval tokval;
520 expr *e;
521 int fwd = 0;
522 char *saveme = stdscan_get();
524 while (special[n] && nasm_isspace(special[n]))
525 n++;
527 * We have a size expression; attempt to
528 * evaluate it.
530 stdscan_reset();
531 stdscan_set(special + n);
532 tokval.t_type = TOKEN_INVALID;
533 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
534 if (fwd) {
535 sym->nextfwd = fwds;
536 fwds = sym;
537 sym->name = nasm_strdup(name);
538 } else if (e) {
539 if (!is_simple(e))
540 nasm_error(ERR_NONFATAL, "cannot use relocatable"
541 " expression as symbol size");
542 else
543 sym->size = reloc_value(e);
545 stdscan_set(saveme);
547 special_used = true;
550 * If TLS segment, mark symbol accordingly.
552 if (sects[sym->section - 1]->flags & SHF_TLS) {
553 sym->type &= 0xf0;
554 sym->type |= STT_TLS;
557 sym->globnum = nglobs;
558 nglobs++;
559 } else
560 nlocals++;
562 if (special && !special_used)
563 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
566 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
567 int64_t offset, int type)
569 struct elf_reloc *r;
571 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
572 sect->tail = &r->next;
574 r->address = sect->len;
575 r->offset = offset;
577 if (segment != NO_SEG) {
578 int i;
579 for (i = 0; i < nsects; i++)
580 if (segment == sects[i]->index)
581 r->symbol = i + 2;
582 if (!r->symbol)
583 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
585 r->type = type;
587 sect->nrelocs++;
591 * This routine deals with ..got and ..sym relocations: the more
592 * complicated kinds. In shared-library writing, some relocations
593 * with respect to global symbols must refer to the precise symbol
594 * rather than referring to an offset from the base of the section
595 * _containing_ the symbol. Such relocations call to this routine,
596 * which searches the symbol list for the symbol in question.
598 * R_386_GOT32 references require the _exact_ symbol address to be
599 * used; R_386_32 references can be at an offset from the symbol.
600 * The boolean argument `exact' tells us this.
602 * Return value is the adjusted value of `addr', having become an
603 * offset from the symbol rather than the section. Should always be
604 * zero when returning from an exact call.
606 * Limitation: if you define two symbols at the same place,
607 * confusion will occur.
609 * Inefficiency: we search, currently, using a linked list which
610 * isn't even necessarily sorted.
612 static void elf_add_gsym_reloc(struct elf_section *sect,
613 int32_t segment, uint64_t offset, int64_t pcrel,
614 int type, bool exact)
616 struct elf_reloc *r;
617 struct elf_section *s;
618 struct elf_symbol *sym;
619 struct rbtree *srb;
620 int i;
623 * First look up the segment/offset pair and find a global
624 * symbol corresponding to it. If it's not one of our segments,
625 * then it must be an external symbol, in which case we're fine
626 * doing a normal elf_add_reloc after first sanity-checking
627 * that the offset from the symbol is zero.
629 s = NULL;
630 for (i = 0; i < nsects; i++)
631 if (segment == sects[i]->index) {
632 s = sects[i];
633 break;
636 if (!s) {
637 if (exact && offset)
638 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
639 else
640 elf_add_reloc(sect, segment, offset - pcrel, type);
641 return;
644 srb = rb_search(s->gsyms, offset);
645 if (!srb || (exact && srb->key != offset)) {
646 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
647 " for this reference");
648 return;
650 sym = container_of(srb, struct elf_symbol, symv);
652 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
653 sect->tail = &r->next;
654 r->next = NULL;
656 r->address = sect->len;
657 r->offset = offset - pcrel - sym->symv.key;
658 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
659 r->type = type;
661 sect->nrelocs++;
664 static void elf_out(int32_t segto, const void *data,
665 enum out_type type, uint64_t size,
666 int32_t segment, int32_t wrt)
668 struct elf_section *s;
669 int64_t addr;
670 int reltype, bytes;
671 int i;
672 static struct symlininfo sinfo;
674 #if defined(DEBUG) && DEBUG>2
675 if (data)
676 nasm_error(ERR_DEBUG,
677 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
678 currentline, type, segment, segto, size, *(int64_t *)data);
679 else
680 nasm_error(ERR_DEBUG,
681 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
682 currentline, type, segment, segto, size);
683 #endif
686 * handle absolute-assembly (structure definitions)
688 if (segto == NO_SEG) {
689 if (type != OUT_RESERVE)
690 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
691 " space");
692 return;
695 s = NULL;
696 for (i = 0; i < nsects; i++)
697 if (segto == sects[i]->index) {
698 s = sects[i];
699 break;
701 if (!s) {
702 int tempint; /* ignored */
703 if (segto != elf_section_names(".text", 2, &tempint))
704 nasm_panic(0, "strange segment conditions in ELF driver");
705 else {
706 s = sects[nsects - 1];
707 i = nsects - 1;
711 /* again some stabs debugging stuff */
712 if (of_elf64.current_dfmt) {
713 sinfo.offset = s->len;
714 sinfo.section = i;
715 sinfo.segto = segto;
716 sinfo.name = s->name;
717 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
719 /* end of debugging stuff */
721 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
722 nasm_error(ERR_WARNING, "attempt to initialize memory in"
723 " BSS section `%s': ignored", s->name);
724 s->len += realsize(type, size);
725 return;
728 switch (type) {
729 case OUT_RESERVE:
730 if (s->type == SHT_PROGBITS) {
731 nasm_error(ERR_WARNING, "uninitialized space declared in"
732 " non-BSS section `%s': zeroing", s->name);
733 elf_sect_write(s, NULL, size);
734 } else
735 s->len += size;
736 break;
738 case OUT_RAWDATA:
739 if (segment != NO_SEG)
740 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
741 elf_sect_write(s, data, size);
742 break;
744 case OUT_ADDRESS:
746 int isize = (int)size;
747 int asize = abs((int)size);
749 addr = *(int64_t *)data;
750 if (segment == NO_SEG) {
751 /* Do nothing */
752 } else if (segment % 2) {
753 nasm_error(ERR_NONFATAL, "ELF format does not support"
754 " segment base references");
755 } else {
756 if (wrt == NO_SEG) {
757 switch (isize) {
758 case 1:
759 case -1:
760 elf_add_reloc(s, segment, addr, R_X86_64_8);
761 break;
762 case 2:
763 case -2:
764 elf_add_reloc(s, segment, addr, R_X86_64_16);
765 break;
766 case 4:
767 elf_add_reloc(s, segment, addr, R_X86_64_32);
768 break;
769 case -4:
770 elf_add_reloc(s, segment, addr, R_X86_64_32S);
771 break;
772 case 8:
773 case -8:
774 elf_add_reloc(s, segment, addr, R_X86_64_64);
775 break;
776 default:
777 nasm_panic(0, "internal error elf64-hpa-871");
778 break;
780 addr = 0;
781 } else if (wrt == elf_gotpc_sect + 1) {
783 * The user will supply GOT relative to $$. ELF
784 * will let us have GOT relative to $. So we
785 * need to fix up the data item by $-$$.
787 addr += s->len;
788 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
789 addr = 0;
790 } else if (wrt == elf_gotoff_sect + 1) {
791 if (asize != 8) {
792 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
793 "references to be qword");
794 } else {
795 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
796 addr = 0;
798 } else if (wrt == elf_got_sect + 1) {
799 switch (asize) {
800 case 4:
801 elf_add_gsym_reloc(s, segment, addr, 0,
802 R_X86_64_GOT32, true);
803 addr = 0;
804 break;
805 case 8:
806 elf_add_gsym_reloc(s, segment, addr, 0,
807 R_X86_64_GOT64, true);
808 addr = 0;
809 break;
810 default:
811 nasm_error(ERR_NONFATAL, "invalid ..got reference");
812 break;
814 } else if (wrt == elf_sym_sect + 1) {
815 switch (isize) {
816 case 1:
817 case -1:
818 elf_add_gsym_reloc(s, segment, addr, 0,
819 R_X86_64_8, false);
820 addr = 0;
821 break;
822 case 2:
823 case -2:
824 elf_add_gsym_reloc(s, segment, addr, 0,
825 R_X86_64_16, false);
826 addr = 0;
827 break;
828 case 4:
829 elf_add_gsym_reloc(s, segment, addr, 0,
830 R_X86_64_32, false);
831 addr = 0;
832 break;
833 case -4:
834 elf_add_gsym_reloc(s, segment, addr, 0,
835 R_X86_64_32S, false);
836 addr = 0;
837 break;
838 case 8:
839 case -8:
840 elf_add_gsym_reloc(s, segment, addr, 0,
841 R_X86_64_64, false);
842 addr = 0;
843 break;
844 default:
845 nasm_panic(0, "internal error elf64-hpa-903");
846 break;
848 } else if (wrt == elf_plt_sect + 1) {
849 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
850 "relative PLT references");
851 } else {
852 nasm_error(ERR_NONFATAL, "ELF format does not support this"
853 " use of WRT");
856 elf_sect_writeaddr(s, addr, asize);
857 break;
860 case OUT_REL1ADR:
861 reltype = R_X86_64_PC8;
862 bytes = 1;
863 goto rel12adr;
865 case OUT_REL2ADR:
866 reltype = R_X86_64_PC16;
867 bytes = 2;
868 goto rel12adr;
870 rel12adr:
871 addr = *(int64_t *)data - size;
872 if (segment == segto)
873 nasm_panic(0, "intra-segment OUT_REL1ADR");
874 if (segment == NO_SEG) {
875 /* Do nothing */
876 } else if (segment % 2) {
877 nasm_error(ERR_NONFATAL, "ELF format does not support"
878 " segment base references");
879 } else {
880 if (wrt == NO_SEG) {
881 elf_add_reloc(s, segment, addr, reltype);
882 addr = 0;
883 } else {
884 nasm_error(ERR_NONFATAL,
885 "Unsupported non-32-bit ELF relocation");
888 elf_sect_writeaddr(s, addr, bytes);
889 break;
891 case OUT_REL4ADR:
892 addr = *(int64_t *)data - size;
893 if (segment == segto)
894 nasm_panic(0, "intra-segment OUT_REL4ADR");
895 if (segment == NO_SEG) {
896 /* Do nothing */
897 } else if (segment % 2) {
898 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
899 " segment base references");
900 } else {
901 if (wrt == NO_SEG) {
902 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
903 addr = 0;
904 } else if (wrt == elf_plt_sect + 1) {
905 elf_add_gsym_reloc(s, segment, addr+size, size,
906 R_X86_64_PLT32, true);
907 addr = 0;
908 } else if (wrt == elf_gotpc_sect + 1 ||
909 wrt == elf_got_sect + 1) {
910 elf_add_gsym_reloc(s, segment, addr+size, size,
911 R_X86_64_GOTPCREL, true);
912 addr = 0;
913 } else if (wrt == elf_gotoff_sect + 1 ||
914 wrt == elf_got_sect + 1) {
915 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
916 "qword absolute");
917 } else if (wrt == elf_gottpoff_sect + 1) {
918 elf_add_gsym_reloc(s, segment, addr+size, size,
919 R_X86_64_GOTTPOFF, true);
920 addr = 0;
921 } else {
922 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
923 " use of WRT");
926 elf_sect_writeaddr(s, addr, 4);
927 break;
929 case OUT_REL8ADR:
930 addr = *(int64_t *)data - size;
931 if (segment == segto)
932 nasm_panic(0, "intra-segment OUT_REL8ADR");
933 if (segment == NO_SEG) {
934 /* Do nothing */
935 } else if (segment % 2) {
936 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
937 " segment base references");
938 } else {
939 if (wrt == NO_SEG) {
940 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
941 addr = 0;
942 } else if (wrt == elf_gotpc_sect + 1 ||
943 wrt == elf_got_sect + 1) {
944 elf_add_gsym_reloc(s, segment, addr+size, size,
945 R_X86_64_GOTPCREL64, true);
946 addr = 0;
947 } else if (wrt == elf_gotoff_sect + 1 ||
948 wrt == elf_got_sect + 1) {
949 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
950 "absolute");
951 } else if (wrt == elf_gottpoff_sect + 1) {
952 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
953 "dword");
954 } else {
955 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
956 " use of WRT");
959 elf_sect_writeaddr(s, addr, 8);
960 break;
964 static void elf_write(void)
966 int align;
967 char *p;
968 int i;
970 struct SAA *symtab;
971 int32_t symtablen, symtablocal;
974 * Work out how many sections we will have. We have SHN_UNDEF,
975 * then the flexible user sections, then the fixed sections
976 * `.shstrtab', `.symtab' and `.strtab', then optionally
977 * relocation sections for the user sections.
979 nsections = sec_numspecial + 1;
980 if (of_elf64.current_dfmt == &df_stabs)
981 nsections += 3;
982 else if (of_elf64.current_dfmt == &df_dwarf)
983 nsections += 10;
985 add_sectname("", ".shstrtab");
986 add_sectname("", ".symtab");
987 add_sectname("", ".strtab");
988 for (i = 0; i < nsects; i++) {
989 nsections++; /* for the section itself */
990 if (sects[i]->head) {
991 nsections++; /* for its relocations */
992 add_sectname(".rela", sects[i]->name);
996 if (of_elf64.current_dfmt == &df_stabs) {
997 /* in case the debug information is wanted, just add these three sections... */
998 add_sectname("", ".stab");
999 add_sectname("", ".stabstr");
1000 add_sectname(".rel", ".stab");
1003 else if (of_elf64.current_dfmt == &df_dwarf) {
1004 /* the dwarf debug standard specifies the following ten sections,
1005 not all of which are currently implemented,
1006 although all of them are defined. */
1007 #define debug_aranges (int64_t) (nsections-10)
1008 #define debug_info (int64_t) (nsections-7)
1009 #define debug_abbrev (int64_t) (nsections-5)
1010 #define debug_line (int64_t) (nsections-4)
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 nasm_write("\177ELF\2\1\1", 7, 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 fwriteint64_t(0L, ofile); /* no entry point */
1034 fwriteint64_t(0L, ofile); /* no program header table */
1035 fwriteint64_t(0x40L, ofile); /* section headers straight after
1036 * ELF header plus alignment */
1037 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1038 fwriteint16_t(0x40, 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(Elf64_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 */
1047 * Build the symbol table and relocation tables.
1049 symtab = elf_build_symtab(&symtablen, &symtablocal);
1050 for (i = 0; i < nsects; i++)
1051 if (sects[i]->head)
1052 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1053 sects[i]->head);
1056 * Now output the section header table.
1059 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1060 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1061 elf_foffs += align;
1062 elf_nsect = 0;
1063 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1065 /* SHN_UNDEF */
1066 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1067 p = shstrtab + 1;
1069 /* The normal sections */
1070 for (i = 0; i < nsects; i++) {
1071 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1072 (sects[i]->type == SHT_PROGBITS ?
1073 sects[i]->data : NULL), true,
1074 sects[i]->len, 0, 0, sects[i]->align, 0);
1075 p += strlen(p) + 1;
1078 /* .shstrtab */
1079 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1080 shstrtablen, 0, 0, 1, 0);
1081 p += strlen(p) + 1;
1083 /* .symtab */
1084 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1085 symtablen, sec_strtab, symtablocal, 8, 24);
1086 p += strlen(p) + 1;
1088 /* .strtab */
1089 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1090 strslen, 0, 0, 1, 0);
1091 p += strlen(p) + 1;
1093 /* The relocation sections */
1094 for (i = 0; i < nsects; i++)
1095 if (sects[i]->head) {
1096 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1097 sects[i]->rellen, sec_symtab, i + 1, 8, 24);
1098 p += strlen(p) + 1;
1101 if (of_elf64.current_dfmt == &df_stabs) {
1102 /* for debugging information, create the last three sections
1103 which are the .stab , .stabstr and .rel.stab sections respectively */
1105 /* this function call creates the stab sections in memory */
1106 stabs64_generate();
1108 if (stabbuf && stabstrbuf && stabrelbuf) {
1109 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1110 stablen, sec_stabstr, 0, 4, 12);
1111 p += strlen(p) + 1;
1113 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1114 stabstrlen, 0, 0, 4, 0);
1115 p += strlen(p) + 1;
1117 /* link -> symtable info -> section to refer to */
1118 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1119 stabrellen, symtabsection, sec_stab, 4, 16);
1120 p += strlen(p) + 1;
1122 } else if (of_elf64.current_dfmt == &df_dwarf) {
1123 /* for dwarf debugging information, create the ten dwarf sections */
1125 /* this function call creates the dwarf sections in memory */
1126 if (dwarf_fsect)
1127 dwarf64_generate();
1129 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1130 arangeslen, 0, 0, 1, 0);
1131 p += strlen(p) + 1;
1133 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1134 arangesrellen, symtabsection, debug_aranges, 1, 24);
1135 p += strlen(p) + 1;
1137 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1138 pubnameslen, 0, 0, 1, 0);
1139 p += strlen(p) + 1;
1141 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1142 infolen, 0, 0, 1, 0);
1143 p += strlen(p) + 1;
1145 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1146 inforellen, symtabsection, debug_info, 1, 24);
1147 p += strlen(p) + 1;
1149 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1150 abbrevlen, 0, 0, 1, 0);
1151 p += strlen(p) + 1;
1153 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1154 linelen, 0, 0, 1, 0);
1155 p += strlen(p) + 1;
1157 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1158 linerellen, symtabsection, debug_line, 1, 24);
1159 p += strlen(p) + 1;
1161 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1162 framelen, 0, 0, 8, 0);
1163 p += strlen(p) + 1;
1165 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1166 loclen, 0, 0, 1, 0);
1167 p += strlen(p) + 1;
1169 fwritezero(align, ofile);
1172 * Now output the sections.
1174 elf_write_sections();
1176 nasm_free(elf_sects);
1177 saa_free(symtab);
1180 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1182 struct SAA *s = saa_init(1L);
1183 struct elf_symbol *sym;
1184 uint8_t entry[24], *p;
1185 int i;
1187 *len = *local = 0;
1190 * First, an all-zeros entry, required by the ELF spec.
1192 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1193 *len += 24;
1194 (*local)++;
1197 * Next, an entry for the file name.
1199 p = entry;
1200 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1201 WRITESHORT(p, STT_FILE); /* type FILE */
1202 WRITESHORT(p, SHN_ABS);
1203 WRITEDLONG(p, (uint64_t) 0); /* no value */
1204 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1205 saa_wbytes(s, entry, 24L);
1206 *len += 24;
1207 (*local)++;
1210 * Now some standard symbols defining the segments, for relocation
1211 * purposes.
1213 for (i = 1; i <= nsects; i++) {
1214 p = entry;
1215 WRITELONG(p, 0); /* no symbol name */
1216 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1217 WRITESHORT(p, i); /* section id */
1218 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1219 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1220 saa_wbytes(s, entry, 24L);
1221 *len += 24;
1222 (*local)++;
1227 * Now the other local symbols.
1229 saa_rewind(syms);
1230 while ((sym = saa_rstruct(syms))) {
1231 if (sym->type & SYM_GLOBAL)
1232 continue;
1233 p = entry;
1234 WRITELONG(p, sym->strpos); /* index into symbol string table */
1235 WRITECHAR(p, sym->type); /* type and binding */
1236 WRITECHAR(p, sym->other); /* visibility */
1237 WRITESHORT(p, sym->section); /* index into section header table */
1238 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1239 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1240 saa_wbytes(s, entry, 24L);
1241 *len += 24;
1242 (*local)++;
1245 * dwarf needs symbols for debug sections
1246 * which are relocation targets.
1248 if (of_elf64.current_dfmt == &df_dwarf) {
1249 dwarf_infosym = *local;
1250 p = entry;
1251 WRITELONG(p, 0); /* no symbol name */
1252 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1253 WRITESHORT(p, debug_info); /* section id */
1254 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1255 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1256 saa_wbytes(s, entry, 24L);
1257 *len += 24;
1258 (*local)++;
1259 dwarf_abbrevsym = *local;
1260 p = entry;
1261 WRITELONG(p, 0); /* no symbol name */
1262 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1263 WRITESHORT(p, debug_abbrev); /* section id */
1264 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1265 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1266 saa_wbytes(s, entry, 24L);
1267 *len += 24;
1268 (*local)++;
1269 dwarf_linesym = *local;
1270 p = entry;
1271 WRITELONG(p, 0); /* no symbol name */
1272 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1273 WRITESHORT(p, debug_line); /* section id */
1274 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1275 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1276 saa_wbytes(s, entry, 24L);
1277 *len += 24;
1278 (*local)++;
1282 * Now the global symbols.
1284 saa_rewind(syms);
1285 while ((sym = saa_rstruct(syms))) {
1286 if (!(sym->type & SYM_GLOBAL))
1287 continue;
1288 p = entry;
1289 WRITELONG(p, sym->strpos);
1290 WRITECHAR(p, sym->type); /* type and binding */
1291 WRITECHAR(p, sym->other); /* visibility */
1292 WRITESHORT(p, sym->section);
1293 WRITEDLONG(p, (int64_t)sym->symv.key);
1294 WRITEDLONG(p, (int64_t)sym->size);
1295 saa_wbytes(s, entry, 24L);
1296 *len += 24;
1299 return s;
1302 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1304 struct SAA *s;
1305 uint8_t *p, entry[24];
1306 int32_t global_offset;
1308 if (!r)
1309 return NULL;
1311 s = saa_init(1L);
1312 *len = 0;
1315 * How to onvert from a global placeholder to a real symbol index;
1316 * the +2 refers to the two special entries, the null entry and
1317 * the filename entry.
1319 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1321 while (r) {
1322 int32_t sym = r->symbol;
1324 if (sym >= GLOBAL_TEMP_BASE)
1325 sym += global_offset;
1327 p = entry;
1328 WRITEDLONG(p, r->address);
1329 WRITELONG(p, r->type);
1330 WRITELONG(p, sym);
1331 WRITEDLONG(p, r->offset);
1332 saa_wbytes(s, entry, 24L);
1333 *len += 24;
1335 r = r->next;
1338 return s;
1341 static void elf_section_header(int name, int type, uint64_t flags,
1342 void *data, bool is_saa, uint64_t datalen,
1343 int link, int info, int align, int eltsize)
1345 elf_sects[elf_nsect].data = data;
1346 elf_sects[elf_nsect].len = datalen;
1347 elf_sects[elf_nsect].is_saa = is_saa;
1348 elf_nsect++;
1350 fwriteint32_t((int32_t)name, ofile);
1351 fwriteint32_t((int32_t)type, ofile);
1352 fwriteint64_t((int64_t)flags, ofile);
1353 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1354 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1355 fwriteint64_t(datalen, ofile);
1356 if (data)
1357 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1358 fwriteint32_t((int32_t)link, ofile);
1359 fwriteint32_t((int32_t)info, ofile);
1360 fwriteint64_t((int64_t)align, ofile);
1361 fwriteint64_t((int64_t)eltsize, ofile);
1364 static void elf_write_sections(void)
1366 int i;
1367 for (i = 0; i < elf_nsect; i++)
1368 if (elf_sects[i].data) {
1369 int32_t len = elf_sects[i].len;
1370 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1371 int32_t align = reallen - len;
1372 if (elf_sects[i].is_saa)
1373 saa_fpwrite(elf_sects[i].data, ofile);
1374 else
1375 nasm_write(elf_sects[i].data, len, ofile);
1376 fwritezero(align, ofile);
1380 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1382 saa_wbytes(sect->data, data, len);
1383 sect->len += len;
1386 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
1388 saa_writeaddr(sect->data, data, len);
1389 sect->len += len;
1392 static void elf_sectalign(int32_t seg, unsigned int value)
1394 struct elf_section *s = NULL;
1395 int i;
1397 for (i = 0; i < nsects; i++) {
1398 if (sects[i]->index == seg) {
1399 s = sects[i];
1400 break;
1403 if (!s || !is_power2(value))
1404 return;
1406 if (value > s->align)
1407 s->align = value;
1410 static int32_t elf_segbase(int32_t segment)
1412 return segment;
1415 static void elf_filename(char *inname, char *outname)
1417 strcpy(elf_module, inname);
1418 standard_extension(inname, outname, ".o");
1421 extern macros_t elf_stdmac[];
1423 static int elf_set_info(enum geninfo type, char **val)
1425 (void)type;
1426 (void)val;
1427 return 0;
1429 static struct dfmt df_dwarf = {
1430 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1431 "dwarf",
1432 dwarf64_init,
1433 dwarf64_linenum,
1434 debug64_deflabel,
1435 debug64_directive,
1436 debug64_typevalue,
1437 dwarf64_output,
1438 dwarf64_cleanup
1440 static struct dfmt df_stabs = {
1441 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1442 "stabs",
1443 null_debug_init,
1444 stabs64_linenum,
1445 debug64_deflabel,
1446 debug64_directive,
1447 debug64_typevalue,
1448 stabs64_output,
1449 stabs64_cleanup
1452 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1454 struct ofmt of_elf64 = {
1455 "ELF64 (x86_64) object files (e.g. Linux)",
1456 "elf64",
1459 elf64_debugs_arr,
1460 &df_stabs,
1461 elf_stdmac,
1462 elf_init,
1463 elf_set_info,
1464 elf_out,
1465 elf_deflabel,
1466 elf_section_names,
1467 elf_sectalign,
1468 elf_segbase,
1469 elf_directive,
1470 elf_filename,
1471 elf_cleanup
1474 /* common debugging routines */
1475 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1476 int is_global, char *special)
1478 (void)name;
1479 (void)segment;
1480 (void)offset;
1481 (void)is_global;
1482 (void)special;
1485 static void debug64_directive(const char *directive, const char *params)
1487 (void)directive;
1488 (void)params;
1491 static void debug64_typevalue(int32_t type)
1493 int32_t stype, ssize;
1494 switch (TYM_TYPE(type)) {
1495 case TY_LABEL:
1496 ssize = 0;
1497 stype = STT_NOTYPE;
1498 break;
1499 case TY_BYTE:
1500 ssize = 1;
1501 stype = STT_OBJECT;
1502 break;
1503 case TY_WORD:
1504 ssize = 2;
1505 stype = STT_OBJECT;
1506 break;
1507 case TY_DWORD:
1508 ssize = 4;
1509 stype = STT_OBJECT;
1510 break;
1511 case TY_FLOAT:
1512 ssize = 4;
1513 stype = STT_OBJECT;
1514 break;
1515 case TY_QWORD:
1516 ssize = 8;
1517 stype = STT_OBJECT;
1518 break;
1519 case TY_TBYTE:
1520 ssize = 10;
1521 stype = STT_OBJECT;
1522 break;
1523 case TY_OWORD:
1524 ssize = 16;
1525 stype = STT_OBJECT;
1526 break;
1527 case TY_YWORD:
1528 ssize = 32;
1529 stype = STT_OBJECT;
1530 break;
1531 case TY_COMMON:
1532 ssize = 0;
1533 stype = STT_COMMON;
1534 break;
1535 case TY_SEG:
1536 ssize = 0;
1537 stype = STT_SECTION;
1538 break;
1539 case TY_EXTERN:
1540 ssize = 0;
1541 stype = STT_NOTYPE;
1542 break;
1543 case TY_EQU:
1544 ssize = 0;
1545 stype = STT_NOTYPE;
1546 break;
1547 default:
1548 ssize = 0;
1549 stype = STT_NOTYPE;
1550 break;
1552 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1553 lastsym->size = ssize;
1554 lastsym->type = stype;
1558 /* stabs debugging routines */
1560 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1562 (void)segto;
1563 if (!stabs_filename) {
1564 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1565 strcpy(stabs_filename, filename);
1566 } else {
1567 if (strcmp(stabs_filename, filename)) {
1568 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1569 in fact, this leak comes in quite handy to maintain a list of files
1570 encountered so far in the symbol lines... */
1572 /* why not nasm_free(stabs_filename); we're done with the old one */
1574 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1575 strcpy(stabs_filename, filename);
1578 debug_immcall = 1;
1579 currentline = linenumber;
1583 static void stabs64_output(int type, void *param)
1585 struct symlininfo *s;
1586 struct linelist *el;
1587 if (type == TY_DEBUGSYMLIN) {
1588 if (debug_immcall) {
1589 s = (struct symlininfo *)param;
1590 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1591 return; /* line info is only collected for executable sections */
1592 numlinestabs++;
1593 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1594 el->info.offset = s->offset;
1595 el->info.section = s->section;
1596 el->info.name = s->name;
1597 el->line = currentline;
1598 el->filename = stabs_filename;
1599 el->next = 0;
1600 if (stabslines) {
1601 stabslines->last->next = el;
1602 stabslines->last = el;
1603 } else {
1604 stabslines = el;
1605 stabslines->last = el;
1609 debug_immcall = 0;
1612 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1614 static void stabs64_generate(void)
1616 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1617 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1618 char **allfiles;
1619 int *fileidx;
1621 struct linelist *ptr;
1623 ptr = stabslines;
1625 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1626 numfiles = 0;
1627 while (ptr) {
1628 if (numfiles == 0) {
1629 allfiles[0] = ptr->filename;
1630 numfiles++;
1631 } else {
1632 for (i = 0; i < numfiles; i++) {
1633 if (!strcmp(allfiles[i], ptr->filename))
1634 break;
1636 if (i >= numfiles) {
1637 allfiles[i] = ptr->filename;
1638 numfiles++;
1641 ptr = ptr->next;
1643 strsize = 1;
1644 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1645 for (i = 0; i < numfiles; i++) {
1646 fileidx[i] = strsize;
1647 strsize += strlen(allfiles[i]) + 1;
1649 mainfileindex = 0;
1650 for (i = 0; i < numfiles; i++) {
1651 if (!strcmp(allfiles[i], elf_module)) {
1652 mainfileindex = i;
1653 break;
1658 * worst case size of the stab buffer would be:
1659 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1660 * plus one "ending" entry
1662 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1663 sizeof(struct stabentry));
1664 ssbuf = (uint8_t *)nasm_malloc(strsize);
1665 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1666 rptr = rbuf;
1668 for (i = 0; i < numfiles; i++)
1669 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1670 ssbuf[0] = 0;
1672 stabstrlen = strsize; /* set global variable for length of stab strings */
1674 sptr = sbuf;
1675 ptr = stabslines;
1676 numstabs = 0;
1678 if (ptr) {
1680 * this is the first stab, its strx points to the filename of the
1681 * the source-file, the n_desc field should be set to the number
1682 * of remaining stabs
1684 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1686 /* this is the stab for the main source file */
1687 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1689 /* relocation table entry */
1692 * Since the symbol table has two entries before
1693 * the section symbols, the index in the info.section
1694 * member must be adjusted by adding 2
1697 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1698 WRITELONG(rptr, R_X86_64_32);
1699 WRITELONG(rptr, ptr->info.section + 2);
1701 numstabs++;
1702 currfile = mainfileindex;
1705 while (ptr) {
1706 if (strcmp(allfiles[currfile], ptr->filename)) {
1707 /* oops file has changed... */
1708 for (i = 0; i < numfiles; i++)
1709 if (!strcmp(allfiles[i], ptr->filename))
1710 break;
1711 currfile = i;
1712 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1713 ptr->info.offset);
1714 numstabs++;
1716 /* relocation table entry */
1718 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1719 WRITELONG(rptr, R_X86_64_32);
1720 WRITELONG(rptr, ptr->info.section + 2);
1723 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1724 numstabs++;
1726 /* relocation table entry */
1728 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1729 WRITELONG(rptr, R_X86_64_32);
1730 WRITELONG(rptr, ptr->info.section + 2);
1732 ptr = ptr->next;
1736 /* this is an "ending" token */
1737 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1738 numstabs++;
1740 ((struct stabentry *)sbuf)->n_desc = numstabs;
1742 nasm_free(allfiles);
1743 nasm_free(fileidx);
1745 stablen = (sptr - sbuf);
1746 stabrellen = (rptr - rbuf);
1747 stabrelbuf = rbuf;
1748 stabbuf = sbuf;
1749 stabstrbuf = ssbuf;
1752 static void stabs64_cleanup(void)
1754 struct linelist *ptr, *del;
1755 if (!stabslines)
1756 return;
1758 ptr = stabslines;
1759 while (ptr) {
1760 del = ptr;
1761 ptr = ptr->next;
1762 nasm_free(del);
1765 nasm_free(stabbuf);
1766 nasm_free(stabrelbuf);
1767 nasm_free(stabstrbuf);
1770 /* dwarf routines */
1772 static void dwarf64_init(void)
1774 ndebugs = 3; /* 3 debug symbols */
1777 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1778 int32_t segto)
1780 (void)segto;
1781 dwarf64_findfile(filename);
1782 debug_immcall = 1;
1783 currentline = linenumber;
1786 /* called from elf_out with type == TY_DEBUGSYMLIN */
1787 static void dwarf64_output(int type, void *param)
1789 int ln, aa, inx, maxln, soc;
1790 struct symlininfo *s;
1791 struct SAA *plinep;
1793 (void)type;
1795 s = (struct symlininfo *)param;
1797 /* line number info is only gathered for executable sections */
1798 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1799 return;
1801 /* Check if section index has changed */
1802 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1803 dwarf64_findsect(s->section);
1805 /* do nothing unless line or file has changed */
1806 if (!debug_immcall)
1807 return;
1809 ln = currentline - dwarf_csect->line;
1810 aa = s->offset - dwarf_csect->offset;
1811 inx = dwarf_clist->line;
1812 plinep = dwarf_csect->psaa;
1813 /* check for file change */
1814 if (!(inx == dwarf_csect->file)) {
1815 saa_write8(plinep,DW_LNS_set_file);
1816 saa_write8(plinep,inx);
1817 dwarf_csect->file = inx;
1819 /* check for line change */
1820 if (ln) {
1821 /* test if in range of special op code */
1822 maxln = line_base + line_range;
1823 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1824 if (ln >= line_base && ln < maxln && soc < 256) {
1825 saa_write8(plinep,soc);
1826 } else {
1827 saa_write8(plinep,DW_LNS_advance_line);
1828 saa_wleb128s(plinep,ln);
1829 if (aa) {
1830 saa_write8(plinep,DW_LNS_advance_pc);
1831 saa_wleb128u(plinep,aa);
1834 dwarf_csect->line = currentline;
1835 dwarf_csect->offset = s->offset;
1838 /* show change handled */
1839 debug_immcall = 0;
1843 static void dwarf64_generate(void)
1845 uint8_t *pbuf;
1846 int indx;
1847 struct linelist *ftentry;
1848 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1849 struct SAA *parangesrel, *plinesrel, *pinforel;
1850 struct sectlist *psect;
1851 size_t saalen, linepoff, totlen, highaddr;
1853 /* write epilogues for each line program range */
1854 /* and build aranges section */
1855 paranges = saa_init(1L);
1856 parangesrel = saa_init(1L);
1857 saa_write16(paranges,3); /* dwarf version */
1858 saa_write64(parangesrel, paranges->datalen+4);
1859 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1860 saa_write64(parangesrel, 0);
1861 saa_write32(paranges,0); /* offset into info */
1862 saa_write8(paranges,8); /* pointer size */
1863 saa_write8(paranges,0); /* not segmented */
1864 saa_write32(paranges,0); /* padding */
1865 /* iterate though sectlist entries */
1866 psect = dwarf_fsect;
1867 totlen = 0;
1868 highaddr = 0;
1869 for (indx = 0; indx < dwarf_nsections; indx++)
1871 plinep = psect->psaa;
1872 /* Line Number Program Epilogue */
1873 saa_write8(plinep,2); /* std op 2 */
1874 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1875 saa_write8(plinep,DW_LNS_extended_op);
1876 saa_write8(plinep,1); /* operand length */
1877 saa_write8(plinep,DW_LNE_end_sequence);
1878 totlen += plinep->datalen;
1879 /* range table relocation entry */
1880 saa_write64(parangesrel, paranges->datalen + 4);
1881 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1882 saa_write64(parangesrel, (uint64_t) 0);
1883 /* range table entry */
1884 saa_write64(paranges,0x0000); /* range start */
1885 saa_write64(paranges,sects[psect->section]->len); /* range length */
1886 highaddr += sects[psect->section]->len;
1887 /* done with this entry */
1888 psect = psect->next;
1890 saa_write64(paranges,0); /* null address */
1891 saa_write64(paranges,0); /* null length */
1892 saalen = paranges->datalen;
1893 arangeslen = saalen + 4;
1894 arangesbuf = pbuf = nasm_malloc(arangeslen);
1895 WRITELONG(pbuf,saalen); /* initial length */
1896 saa_rnbytes(paranges, pbuf, saalen);
1897 saa_free(paranges);
1899 /* build rela.aranges section */
1900 arangesrellen = saalen = parangesrel->datalen;
1901 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1902 saa_rnbytes(parangesrel, pbuf, saalen);
1903 saa_free(parangesrel);
1905 /* build pubnames section */
1906 ppubnames = saa_init(1L);
1907 saa_write16(ppubnames,3); /* dwarf version */
1908 saa_write32(ppubnames,0); /* offset into info */
1909 saa_write32(ppubnames,0); /* space used in info */
1910 saa_write32(ppubnames,0); /* end of list */
1911 saalen = ppubnames->datalen;
1912 pubnameslen = saalen + 4;
1913 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1914 WRITELONG(pbuf,saalen); /* initial length */
1915 saa_rnbytes(ppubnames, pbuf, saalen);
1916 saa_free(ppubnames);
1918 /* build info section */
1919 pinfo = saa_init(1L);
1920 pinforel = saa_init(1L);
1921 saa_write16(pinfo,3); /* dwarf version */
1922 saa_write64(pinforel, pinfo->datalen + 4);
1923 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
1924 saa_write64(pinforel, 0);
1925 saa_write32(pinfo,0); /* offset into abbrev */
1926 saa_write8(pinfo,8); /* pointer size */
1927 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1928 saa_write64(pinforel, pinfo->datalen + 4);
1929 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1930 saa_write64(pinforel, 0);
1931 saa_write64(pinfo,0); /* DW_AT_low_pc */
1932 saa_write64(pinforel, pinfo->datalen + 4);
1933 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1934 saa_write64(pinforel, 0);
1935 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
1936 saa_write64(pinforel, pinfo->datalen + 4);
1937 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
1938 saa_write64(pinforel, 0);
1939 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1940 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1941 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1942 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1943 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1944 saa_write64(pinforel, pinfo->datalen + 4);
1945 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1946 saa_write64(pinforel, 0);
1947 saa_write64(pinfo,0); /* DW_AT_low_pc */
1948 saa_write64(pinfo,0); /* DW_AT_frame_base */
1949 saa_write8(pinfo,0); /* end of entries */
1950 saalen = pinfo->datalen;
1951 infolen = saalen + 4;
1952 infobuf = pbuf = nasm_malloc(infolen);
1953 WRITELONG(pbuf,saalen); /* initial length */
1954 saa_rnbytes(pinfo, pbuf, saalen);
1955 saa_free(pinfo);
1957 /* build rela.info section */
1958 inforellen = saalen = pinforel->datalen;
1959 inforelbuf = pbuf = nasm_malloc(inforellen);
1960 saa_rnbytes(pinforel, pbuf, saalen);
1961 saa_free(pinforel);
1963 /* build abbrev section */
1964 pabbrev = saa_init(1L);
1965 saa_write8(pabbrev,1); /* entry number LEB128u */
1966 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1967 saa_write8(pabbrev,1); /* has children */
1968 /* the following attributes and forms are all LEB128u values */
1969 saa_write8(pabbrev,DW_AT_low_pc);
1970 saa_write8(pabbrev,DW_FORM_addr);
1971 saa_write8(pabbrev,DW_AT_high_pc);
1972 saa_write8(pabbrev,DW_FORM_addr);
1973 saa_write8(pabbrev,DW_AT_stmt_list);
1974 saa_write8(pabbrev,DW_FORM_data4);
1975 saa_write8(pabbrev,DW_AT_name);
1976 saa_write8(pabbrev,DW_FORM_string);
1977 saa_write8(pabbrev,DW_AT_producer);
1978 saa_write8(pabbrev,DW_FORM_string);
1979 saa_write8(pabbrev,DW_AT_language);
1980 saa_write8(pabbrev,DW_FORM_data2);
1981 saa_write16(pabbrev,0); /* end of entry */
1982 /* LEB128u usage same as above */
1983 saa_write8(pabbrev,2); /* entry number */
1984 saa_write8(pabbrev,DW_TAG_subprogram);
1985 saa_write8(pabbrev,0); /* no children */
1986 saa_write8(pabbrev,DW_AT_low_pc);
1987 saa_write8(pabbrev,DW_FORM_addr);
1988 saa_write8(pabbrev,DW_AT_frame_base);
1989 saa_write8(pabbrev,DW_FORM_data4);
1990 saa_write16(pabbrev,0); /* end of entry */
1991 abbrevlen = saalen = pabbrev->datalen;
1992 abbrevbuf = pbuf = nasm_malloc(saalen);
1993 saa_rnbytes(pabbrev, pbuf, saalen);
1994 saa_free(pabbrev);
1996 /* build line section */
1997 /* prolog */
1998 plines = saa_init(1L);
1999 saa_write8(plines,1); /* Minimum Instruction Length */
2000 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2001 saa_write8(plines,line_base); /* Line Base */
2002 saa_write8(plines,line_range); /* Line Range */
2003 saa_write8(plines,opcode_base); /* Opcode Base */
2004 /* standard opcode lengths (# of LEB128u operands) */
2005 saa_write8(plines,0); /* Std opcode 1 length */
2006 saa_write8(plines,1); /* Std opcode 2 length */
2007 saa_write8(plines,1); /* Std opcode 3 length */
2008 saa_write8(plines,1); /* Std opcode 4 length */
2009 saa_write8(plines,1); /* Std opcode 5 length */
2010 saa_write8(plines,0); /* Std opcode 6 length */
2011 saa_write8(plines,0); /* Std opcode 7 length */
2012 saa_write8(plines,0); /* Std opcode 8 length */
2013 saa_write8(plines,1); /* Std opcode 9 length */
2014 saa_write8(plines,0); /* Std opcode 10 length */
2015 saa_write8(plines,0); /* Std opcode 11 length */
2016 saa_write8(plines,1); /* Std opcode 12 length */
2017 /* Directory Table */
2018 saa_write8(plines,0); /* End of table */
2019 /* File Name Table */
2020 ftentry = dwarf_flist;
2021 for (indx = 0;indx<dwarf_numfiles;indx++)
2023 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2024 saa_write8(plines,0); /* directory LEB128u */
2025 saa_write8(plines,0); /* time LEB128u */
2026 saa_write8(plines,0); /* size LEB128u */
2027 ftentry = ftentry->next;
2029 saa_write8(plines,0); /* End of table */
2030 linepoff = plines->datalen;
2031 linelen = linepoff + totlen + 10;
2032 linebuf = pbuf = nasm_malloc(linelen);
2033 WRITELONG(pbuf,linelen-4); /* initial length */
2034 WRITESHORT(pbuf,3); /* dwarf version */
2035 WRITELONG(pbuf,linepoff); /* offset to line number program */
2036 /* write line header */
2037 saalen = linepoff;
2038 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2039 pbuf += linepoff;
2040 saa_free(plines);
2041 /* concatonate line program ranges */
2042 linepoff += 13;
2043 plinesrel = saa_init(1L);
2044 psect = dwarf_fsect;
2045 for (indx = 0; indx < dwarf_nsections; indx++) {
2046 saa_write64(plinesrel, linepoff);
2047 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2048 saa_write64(plinesrel, (uint64_t) 0);
2049 plinep = psect->psaa;
2050 saalen = plinep->datalen;
2051 saa_rnbytes(plinep, pbuf, saalen);
2052 pbuf += saalen;
2053 linepoff += saalen;
2054 saa_free(plinep);
2055 /* done with this entry */
2056 psect = psect->next;
2060 /* build rela.lines section */
2061 linerellen =saalen = plinesrel->datalen;
2062 linerelbuf = pbuf = nasm_malloc(linerellen);
2063 saa_rnbytes(plinesrel, pbuf, saalen);
2064 saa_free(plinesrel);
2066 /* build frame section */
2067 framelen = 4;
2068 framebuf = pbuf = nasm_malloc(framelen);
2069 WRITELONG(pbuf,framelen-4); /* initial length */
2071 /* build loc section */
2072 loclen = 16;
2073 locbuf = pbuf = nasm_malloc(loclen);
2074 WRITEDLONG(pbuf,0); /* null beginning offset */
2075 WRITEDLONG(pbuf,0); /* null ending offset */
2078 static void dwarf64_cleanup(void)
2080 nasm_free(arangesbuf);
2081 nasm_free(arangesrelbuf);
2082 nasm_free(pubnamesbuf);
2083 nasm_free(infobuf);
2084 nasm_free(inforelbuf);
2085 nasm_free(abbrevbuf);
2086 nasm_free(linebuf);
2087 nasm_free(linerelbuf);
2088 nasm_free(framebuf);
2089 nasm_free(locbuf);
2092 static void dwarf64_findfile(const char * fname)
2094 int finx;
2095 struct linelist *match;
2097 /* return if fname is current file name */
2098 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2099 return;
2101 /* search for match */
2102 match = 0;
2103 if (dwarf_flist) {
2104 match = dwarf_flist;
2105 for (finx = 0; finx < dwarf_numfiles; finx++) {
2106 if (!(strcmp(fname, match->filename))) {
2107 dwarf_clist = match;
2108 return;
2113 /* add file name to end of list */
2114 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2115 dwarf_numfiles++;
2116 dwarf_clist->line = dwarf_numfiles;
2117 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2118 strcpy(dwarf_clist->filename,fname);
2119 dwarf_clist->next = 0;
2120 if (!dwarf_flist) { /* if first entry */
2121 dwarf_flist = dwarf_elist = dwarf_clist;
2122 dwarf_clist->last = 0;
2123 } else { /* chain to previous entry */
2124 dwarf_elist->next = dwarf_clist;
2125 dwarf_elist = dwarf_clist;
2129 static void dwarf64_findsect(const int index)
2131 int sinx;
2132 struct sectlist *match;
2133 struct SAA *plinep;
2135 /* return if index is current section index */
2136 if (dwarf_csect && (dwarf_csect->section == index))
2137 return;
2139 /* search for match */
2140 match = 0;
2141 if (dwarf_fsect) {
2142 match = dwarf_fsect;
2143 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2144 if (match->section == index) {
2145 dwarf_csect = match;
2146 return;
2148 match = match->next;
2152 /* add entry to end of list */
2153 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2154 dwarf_nsections++;
2155 dwarf_csect->psaa = plinep = saa_init(1L);
2156 dwarf_csect->line = 1;
2157 dwarf_csect->offset = 0;
2158 dwarf_csect->file = 1;
2159 dwarf_csect->section = index;
2160 dwarf_csect->next = 0;
2161 /* set relocatable address at start of line program */
2162 saa_write8(plinep,DW_LNS_extended_op);
2163 saa_write8(plinep,9); /* operand length */
2164 saa_write8(plinep,DW_LNE_set_address);
2165 saa_write64(plinep,0); /* Start Address */
2167 if (!dwarf_fsect) { /* if first entry */
2168 dwarf_fsect = dwarf_esect = dwarf_csect;
2169 dwarf_csect->last = 0;
2170 } else { /* chain to previous entry */
2171 dwarf_esect->next = dwarf_csect;
2172 dwarf_esect = dwarf_csect;
2176 #endif /* OF_ELF */