preproc: Convert tabs to spaces
[nasm/externdefs.git] / output / outelf64.c
blob9f93fc82f8007073d3dc1938f4e3e3bbba1823ec
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
64 * Relocation types.
66 struct Reloc {
67 struct Reloc *next;
68 int64_t address; /* relative to _start_ of section */
69 int64_t symbol; /* symbol index */
70 int64_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 uint64_t len, size;
89 uint32_t nrelocs;
90 int32_t index; /* index into sects array */
91 int type; /* SHT_PROGBITS or SHT_NOBITS */
92 uint64_t align; /* alignment: power of two */
93 uint64_t flags; /* section flags */
94 char *name;
95 struct SAA *rel;
96 uint64_t rellen;
97 struct Reloc *head, **tail;
98 struct rbtree *gsyms; /* global symbols in section */
101 #define SECT_DELTA 32
102 static struct Section **sects;
103 static int nsects, sectlen;
105 #define SHSTR_DELTA 256
106 static char *shstrtab;
107 static int shstrtablen, shstrtabsize;
109 static struct SAA *syms;
110 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
112 static int32_t def_seg;
114 static struct RAA *bsym;
116 static struct SAA *strs;
117 static uint32_t strslen;
119 static struct Symbol *fwds;
121 static char elf_module[FILENAME_MAX];
123 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
124 static uint8_t elf_abiver = 0; /* Current ABI version */
126 extern struct ofmt of_elf64;
128 static struct ELF_SECTDATA {
129 void *data;
130 int64_t len;
131 bool is_saa;
132 } *elf_sects;
133 static int elf_nsect, nsections;
134 static int64_t elf_foffs;
136 static void elf_write(void);
137 static void elf_sect_write(struct Section *, const void *, size_t);
138 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
139 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
140 int, int);
141 static void elf_write_sections(void);
142 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
143 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
144 static void add_sectname(char *, char *);
146 struct erel {
147 int offset, info;
150 struct symlininfo {
151 int offset;
152 int section; /* index into sects[] */
153 int segto; /* internal section number */
154 char *name; /* shallow-copied pointer of section name */
157 struct linelist {
158 struct linelist *next;
159 struct linelist *last;
160 struct symlininfo info;
161 char *filename;
162 int line;
165 struct sectlist {
166 struct SAA *psaa;
167 int section;
168 int line;
169 int offset;
170 int file;
171 struct sectlist *next;
172 struct sectlist *last;
175 /* common debug variables */
176 static int currentline = 1;
177 static int debug_immcall = 0;
179 /* stabs debug variables */
180 static struct linelist *stabslines = 0;
181 static int numlinestabs = 0;
182 static char *stabs_filename = 0;
183 static int symtabsection;
184 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
185 static int stablen, stabstrlen, stabrellen;
187 /* dwarf debug variables */
188 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
189 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
190 static int dwarf_numfiles = 0, dwarf_nsections;
191 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
192 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
193 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
194 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
195 abbrevlen, linelen, linerellen, framelen, loclen;
196 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
199 static struct dfmt df_dwarf;
200 static struct dfmt df_stabs;
201 static struct Symbol *lastsym;
203 /* common debugging routines */
204 static void debug64_typevalue(int32_t);
205 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
206 static void debug64_directive(const char *, const char *);
208 /* stabs debugging routines */
209 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
210 static void stabs64_output(int, void *);
211 static void stabs64_generate(void);
212 static void stabs64_cleanup(void);
214 /* dwarf debugging routines */
215 static void dwarf64_init(void);
216 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
217 static void dwarf64_output(int, void *);
218 static void dwarf64_generate(void);
219 static void dwarf64_cleanup(void);
220 static void dwarf64_findfile(const char *);
221 static void dwarf64_findsect(const int);
224 * Special section numbers which are used to define ELF special
225 * symbols, which can be used with WRT to provide PIC relocation
226 * types.
228 static int32_t elf_gotpc_sect, elf_gotoff_sect;
229 static int32_t elf_got_sect, elf_plt_sect;
230 static int32_t elf_sym_sect;
231 static int32_t elf_gottpoff_sect;
233 static void elf_init(void)
235 maxbits = 64;
236 sects = NULL;
237 nsects = sectlen = 0;
238 syms = saa_init((int32_t)sizeof(struct Symbol));
239 nlocals = nglobs = ndebugs = 0;
240 bsym = raa_init();
241 strs = saa_init(1L);
242 saa_wbytes(strs, "\0", 1L);
243 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
244 strslen = 2 + strlen(elf_module);
245 shstrtab = NULL;
246 shstrtablen = shstrtabsize = 0;;
247 add_sectname("", "");
249 fwds = NULL;
251 elf_gotpc_sect = seg_alloc();
252 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
253 elf_gotoff_sect = seg_alloc();
254 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
255 elf_got_sect = seg_alloc();
256 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
257 elf_plt_sect = seg_alloc();
258 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
259 elf_sym_sect = seg_alloc();
260 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
261 elf_gottpoff_sect = seg_alloc();
262 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
264 def_seg = seg_alloc();
268 static void elf_cleanup(int debuginfo)
270 struct Reloc *r;
271 int i;
273 (void)debuginfo;
275 elf_write();
276 for (i = 0; i < nsects; i++) {
277 if (sects[i]->type != SHT_NOBITS)
278 saa_free(sects[i]->data);
279 if (sects[i]->head)
280 saa_free(sects[i]->rel);
281 while (sects[i]->head) {
282 r = sects[i]->head;
283 sects[i]->head = sects[i]->head->next;
284 nasm_free(r);
287 nasm_free(sects);
288 saa_free(syms);
289 raa_free(bsym);
290 saa_free(strs);
291 if (of_elf64.current_dfmt) {
292 of_elf64.current_dfmt->cleanup();
296 /* add entry to the elf .shstrtab section */
297 static void add_sectname(char *firsthalf, char *secondhalf)
299 int len = strlen(firsthalf) + strlen(secondhalf);
300 while (shstrtablen + len + 1 > shstrtabsize)
301 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
302 strcpy(shstrtab + shstrtablen, firsthalf);
303 strcat(shstrtab + shstrtablen, secondhalf);
304 shstrtablen += len + 1;
307 static int elf_make_section(char *name, int type, int flags, int align)
309 struct Section *s;
311 s = nasm_zalloc(sizeof(*s));
313 if (type != SHT_NOBITS)
314 s->data = saa_init(1L);
315 s->tail = &s->head;
316 if (!strcmp(name, ".text"))
317 s->index = def_seg;
318 else
319 s->index = seg_alloc();
320 add_sectname("", name);
322 s->name = nasm_strdup(name);
323 s->type = type;
324 s->flags = flags;
325 s->align = align;
327 if (nsects >= sectlen)
328 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
329 sects[nsects++] = s;
331 return nsects - 1;
334 static int32_t elf_section_names(char *name, int pass, int *bits)
336 char *p;
337 uint32_t flags, flags_and, flags_or;
338 uint64_t align;
339 int type, i;
342 * Default is 64 bits.
344 if (!name) {
345 *bits = 64;
346 return def_seg;
349 p = nasm_skip_word(name);
350 if (*p)
351 *p++ = '\0';
352 flags_and = flags_or = type = align = 0;
354 section_attrib(name, p, pass, &flags_and,
355 &flags_or, &align, &type);
357 if (!strcmp(name, ".shstrtab") ||
358 !strcmp(name, ".symtab") ||
359 !strcmp(name, ".strtab")) {
360 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
361 "name `%s'", name);
362 return NO_SEG;
365 for (i = 0; i < nsects; i++)
366 if (!strcmp(name, sects[i]->name))
367 break;
368 if (i == nsects) {
369 const struct elf_known_section *ks = elf_known_sections;
371 while (ks->name) {
372 if (!strcmp(name, ks->name))
373 break;
374 ks++;
377 type = type ? type : ks->type;
378 align = align ? align : ks->align;
379 flags = (ks->flags & ~flags_and) | flags_or;
381 i = elf_make_section(name, type, flags, align);
382 } else if (pass == 1) {
383 if ((type && sects[i]->type != type)
384 || (align && sects[i]->align != align)
385 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
386 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
387 " redeclaration of section `%s'", name);
390 return sects[i]->index;
393 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
394 int is_global, char *special)
396 int pos = strslen;
397 struct Symbol *sym;
398 bool special_used = false;
400 #if defined(DEBUG) && DEBUG>2
401 nasm_error(ERR_DEBUG,
402 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
403 name, segment, offset, is_global, special);
404 #endif
405 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
407 * This is a NASM special symbol. We never allow it into
408 * the ELF symbol table, even if it's a valid one. If it
409 * _isn't_ a valid one, we should barf immediately.
411 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
412 strcmp(name, "..got") && strcmp(name, "..plt") &&
413 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
414 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
415 return;
418 if (is_global == 3) {
419 struct Symbol **s;
421 * Fix up a forward-reference symbol size from the first
422 * pass.
424 for (s = &fwds; *s; s = &(*s)->nextfwd)
425 if (!strcmp((*s)->name, name)) {
426 struct tokenval tokval;
427 expr *e;
428 char *p = nasm_skip_spaces(nasm_skip_word(special));
430 stdscan_reset();
431 stdscan_set(p);
432 tokval.t_type = TOKEN_INVALID;
433 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
434 if (e) {
435 if (!is_simple(e))
436 nasm_error(ERR_NONFATAL, "cannot use relocatable"
437 " expression as symbol size");
438 else
439 (*s)->size = reloc_value(e);
443 * Remove it from the list of unresolved sizes.
445 nasm_free((*s)->name);
446 *s = (*s)->nextfwd;
447 return;
449 return; /* it wasn't an important one */
452 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
453 strslen += 1 + strlen(name);
455 lastsym = sym = saa_wstruct(syms);
457 memset(&sym->symv, 0, sizeof(struct rbtree));
459 sym->strpos = pos;
460 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
461 sym->other = STV_DEFAULT;
462 sym->size = 0;
463 if (segment == NO_SEG)
464 sym->section = SHN_ABS;
465 else {
466 int i;
467 sym->section = SHN_UNDEF;
468 if (segment == def_seg) {
469 /* we have to be sure at least text section is there */
470 int tempint;
471 if (segment != elf_section_names(".text", 2, &tempint))
472 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
474 for (i = 0; i < nsects; i++) {
475 if (segment == sects[i]->index) {
476 sym->section = i + 1;
477 break;
482 if (is_global == 2) {
483 sym->size = offset;
484 sym->symv.key = 0;
485 sym->section = SHN_COMMON;
487 * We have a common variable. Check the special text to see
488 * if it's a valid number and power of two; if so, store it
489 * as the alignment for the common variable.
491 if (special) {
492 bool err;
493 sym->symv.key = readnum(special, &err);
494 if (err)
495 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
496 " valid number", special);
497 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
498 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
499 " power of two", special);
501 special_used = true;
502 } else
503 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
505 if (sym->type == SYM_GLOBAL) {
507 * If sym->section == SHN_ABS, then the first line of the
508 * else section would cause a core dump, because its a reference
509 * beyond the end of the section array.
510 * This behaviour is exhibited by this code:
511 * GLOBAL crash_nasm
512 * crash_nasm equ 0
513 * To avoid such a crash, such requests are silently discarded.
514 * This may not be the best solution.
516 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
517 bsym = raa_write(bsym, segment, nglobs);
518 } else if (sym->section != SHN_ABS) {
520 * This is a global symbol; so we must add it to the rbtree
521 * of global symbols in its section.
523 * In addition, we check the special text for symbol
524 * type and size information.
526 sects[sym->section-1]->gsyms =
527 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
529 if (special) {
530 int n = strcspn(special, " \t");
532 if (!nasm_strnicmp(special, "function", n))
533 sym->type |= STT_FUNC;
534 else if (!nasm_strnicmp(special, "data", n) ||
535 !nasm_strnicmp(special, "object", n))
536 sym->type |= STT_OBJECT;
537 else if (!nasm_strnicmp(special, "notype", n))
538 sym->type |= STT_NOTYPE;
539 else
540 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
541 n, special);
542 special += n;
544 special = nasm_skip_spaces(special);
545 if (*special) {
546 n = strcspn(special, " \t");
547 if (!nasm_strnicmp(special, "default", n))
548 sym->other = STV_DEFAULT;
549 else if (!nasm_strnicmp(special, "internal", n))
550 sym->other = STV_INTERNAL;
551 else if (!nasm_strnicmp(special, "hidden", n))
552 sym->other = STV_HIDDEN;
553 else if (!nasm_strnicmp(special, "protected", n))
554 sym->other = STV_PROTECTED;
555 else
556 n = 0;
557 special += n;
560 if (*special) {
561 struct tokenval tokval;
562 expr *e;
563 int fwd = 0;
564 char *saveme = stdscan_get();
566 while (special[n] && nasm_isspace(special[n]))
567 n++;
569 * We have a size expression; attempt to
570 * evaluate it.
572 stdscan_reset();
573 stdscan_set(special + n);
574 tokval.t_type = TOKEN_INVALID;
575 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
576 NULL);
577 if (fwd) {
578 sym->nextfwd = fwds;
579 fwds = sym;
580 sym->name = nasm_strdup(name);
581 } else if (e) {
582 if (!is_simple(e))
583 nasm_error(ERR_NONFATAL, "cannot use relocatable"
584 " expression as symbol size");
585 else
586 sym->size = reloc_value(e);
588 stdscan_set(saveme);
590 special_used = true;
593 * If TLS segment, mark symbol accordingly.
595 if (sects[sym->section - 1]->flags & SHF_TLS) {
596 sym->type &= 0xf0;
597 sym->type |= STT_TLS;
600 sym->globnum = nglobs;
601 nglobs++;
602 } else
603 nlocals++;
605 if (special && !special_used)
606 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
609 static void elf_add_reloc(struct Section *sect, int32_t segment,
610 int64_t offset, int type)
612 struct Reloc *r;
614 r = *sect->tail = nasm_zalloc(sizeof(struct Reloc));
615 sect->tail = &r->next;
617 r->address = sect->len;
618 r->offset = offset;
620 if (segment != NO_SEG) {
621 int i;
622 for (i = 0; i < nsects; i++)
623 if (segment == sects[i]->index)
624 r->symbol = i + 2;
625 if (!r->symbol)
626 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
628 r->type = type;
630 sect->nrelocs++;
634 * This routine deals with ..got and ..sym relocations: the more
635 * complicated kinds. In shared-library writing, some relocations
636 * with respect to global symbols must refer to the precise symbol
637 * rather than referring to an offset from the base of the section
638 * _containing_ the symbol. Such relocations call to this routine,
639 * which searches the symbol list for the symbol in question.
641 * R_386_GOT32 references require the _exact_ symbol address to be
642 * used; R_386_32 references can be at an offset from the symbol.
643 * The boolean argument `exact' tells us this.
645 * Return value is the adjusted value of `addr', having become an
646 * offset from the symbol rather than the section. Should always be
647 * zero when returning from an exact call.
649 * Limitation: if you define two symbols at the same place,
650 * confusion will occur.
652 * Inefficiency: we search, currently, using a linked list which
653 * isn't even necessarily sorted.
655 static void elf_add_gsym_reloc(struct Section *sect,
656 int32_t segment, uint64_t offset, int64_t pcrel,
657 int type, bool exact)
659 struct Reloc *r;
660 struct Section *s;
661 struct Symbol *sym;
662 struct rbtree *srb;
663 int i;
666 * First look up the segment/offset pair and find a global
667 * symbol corresponding to it. If it's not one of our segments,
668 * then it must be an external symbol, in which case we're fine
669 * doing a normal elf_add_reloc after first sanity-checking
670 * that the offset from the symbol is zero.
672 s = NULL;
673 for (i = 0; i < nsects; i++)
674 if (segment == sects[i]->index) {
675 s = sects[i];
676 break;
679 if (!s) {
680 if (exact && offset)
681 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
682 else
683 elf_add_reloc(sect, segment, offset - pcrel, type);
684 return;
687 srb = rb_search(s->gsyms, offset);
688 if (!srb || (exact && srb->key != offset)) {
689 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
690 " for this reference");
691 return;
693 sym = container_of(srb, struct Symbol, symv);
695 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
696 sect->tail = &r->next;
697 r->next = NULL;
699 r->address = sect->len;
700 r->offset = offset - pcrel - sym->symv.key;
701 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
702 r->type = type;
704 sect->nrelocs++;
707 static void elf_out(int32_t segto, const void *data,
708 enum out_type type, uint64_t size,
709 int32_t segment, int32_t wrt)
711 struct Section *s;
712 int64_t addr;
713 int reltype, bytes;
714 int i;
715 static struct symlininfo sinfo;
717 #if defined(DEBUG) && DEBUG>2
718 if (data)
719 nasm_error(ERR_DEBUG,
720 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
721 currentline, type, segment, segto, size, *(int64_t *)data);
722 else
723 nasm_error(ERR_DEBUG,
724 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
725 currentline, type, segment, segto, size);
726 #endif
729 * handle absolute-assembly (structure definitions)
731 if (segto == NO_SEG) {
732 if (type != OUT_RESERVE)
733 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
734 " space");
735 return;
738 s = NULL;
739 for (i = 0; i < nsects; i++)
740 if (segto == sects[i]->index) {
741 s = sects[i];
742 break;
744 if (!s) {
745 int tempint; /* ignored */
746 if (segto != elf_section_names(".text", 2, &tempint))
747 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
748 else {
749 s = sects[nsects - 1];
750 i = nsects - 1;
754 /* again some stabs debugging stuff */
755 if (of_elf64.current_dfmt) {
756 sinfo.offset = s->len;
757 sinfo.section = i;
758 sinfo.segto = segto;
759 sinfo.name = s->name;
760 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
762 /* end of debugging stuff */
764 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
765 nasm_error(ERR_WARNING, "attempt to initialize memory in"
766 " BSS section `%s': ignored", s->name);
767 s->len += realsize(type, size);
768 return;
771 switch (type) {
772 case OUT_RESERVE:
773 if (s->type == SHT_PROGBITS) {
774 nasm_error(ERR_WARNING, "uninitialized space declared in"
775 " non-BSS section `%s': zeroing", s->name);
776 elf_sect_write(s, NULL, size);
777 } else
778 s->len += size;
779 break;
781 case OUT_RAWDATA:
782 if (segment != NO_SEG)
783 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
784 elf_sect_write(s, data, size);
785 break;
787 case OUT_ADDRESS:
789 int isize = (int)size;
790 int asize = abs(size);
792 addr = *(int64_t *)data;
793 if (segment == NO_SEG) {
794 /* Do nothing */
795 } else if (segment % 2) {
796 nasm_error(ERR_NONFATAL, "ELF format does not support"
797 " segment base references");
798 } else {
799 if (wrt == NO_SEG) {
800 switch (isize) {
801 case 1:
802 case -1:
803 elf_add_reloc(s, segment, addr, R_X86_64_8);
804 break;
805 case 2:
806 case -2:
807 elf_add_reloc(s, segment, addr, R_X86_64_16);
808 break;
809 case 4:
810 elf_add_reloc(s, segment, addr, R_X86_64_32);
811 break;
812 case -4:
813 elf_add_reloc(s, segment, addr, R_X86_64_32S);
814 break;
815 case 8:
816 case -8:
817 elf_add_reloc(s, segment, addr, R_X86_64_64);
818 break;
819 default:
820 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
821 break;
823 addr = 0;
824 } else if (wrt == elf_gotpc_sect + 1) {
826 * The user will supply GOT relative to $$. ELF
827 * will let us have GOT relative to $. So we
828 * need to fix up the data item by $-$$.
830 addr += s->len;
831 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
832 addr = 0;
833 } else if (wrt == elf_gotoff_sect + 1) {
834 if (asize != 8) {
835 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
836 "references to be qword");
837 } else {
838 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
839 addr = 0;
841 } else if (wrt == elf_got_sect + 1) {
842 switch (asize) {
843 case 4:
844 elf_add_gsym_reloc(s, segment, addr, 0,
845 R_X86_64_GOT32, true);
846 addr = 0;
847 break;
848 case 8:
849 elf_add_gsym_reloc(s, segment, addr, 0,
850 R_X86_64_GOT64, true);
851 addr = 0;
852 break;
853 default:
854 nasm_error(ERR_NONFATAL, "invalid ..got reference");
855 break;
857 } else if (wrt == elf_sym_sect + 1) {
858 switch (isize) {
859 case 1:
860 case -1:
861 elf_add_gsym_reloc(s, segment, addr, 0,
862 R_X86_64_8, false);
863 addr = 0;
864 break;
865 case 2:
866 case -2:
867 elf_add_gsym_reloc(s, segment, addr, 0,
868 R_X86_64_16, false);
869 addr = 0;
870 break;
871 case 4:
872 elf_add_gsym_reloc(s, segment, addr, 0,
873 R_X86_64_32, false);
874 addr = 0;
875 break;
876 case -4:
877 elf_add_gsym_reloc(s, segment, addr, 0,
878 R_X86_64_32S, false);
879 addr = 0;
880 break;
881 case 8:
882 case -8:
883 elf_add_gsym_reloc(s, segment, addr, 0,
884 R_X86_64_64, false);
885 addr = 0;
886 break;
887 default:
888 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
889 break;
891 } else if (wrt == elf_plt_sect + 1) {
892 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
893 "relative PLT references");
894 } else {
895 nasm_error(ERR_NONFATAL, "ELF format does not support this"
896 " use of WRT");
899 elf_sect_writeaddr(s, addr, asize);
900 break;
903 case OUT_REL1ADR:
904 reltype = R_X86_64_PC8;
905 bytes = 1;
906 goto rel12adr;
908 case OUT_REL2ADR:
909 reltype = R_X86_64_PC16;
910 bytes = 2;
911 goto rel12adr;
913 rel12adr:
914 addr = *(int64_t *)data - size;
915 if (segment == segto)
916 nasm_error(ERR_PANIC, "intra-segment OUT_REL1ADR");
917 if (segment == NO_SEG) {
918 /* Do nothing */
919 } else if (segment % 2) {
920 nasm_error(ERR_NONFATAL, "ELF format does not support"
921 " segment base references");
922 } else {
923 if (wrt == NO_SEG) {
924 elf_add_reloc(s, segment, addr, reltype);
925 addr = 0;
926 } else {
927 nasm_error(ERR_NONFATAL,
928 "Unsupported non-32-bit ELF relocation");
931 elf_sect_writeaddr(s, addr, bytes);
932 break;
934 case OUT_REL4ADR:
935 addr = *(int64_t *)data - size;
936 if (segment == segto)
937 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
938 if (segment == NO_SEG) {
939 /* Do nothing */
940 } else if (segment % 2) {
941 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
942 " segment base references");
943 } else {
944 if (wrt == NO_SEG) {
945 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
946 addr = 0;
947 } else if (wrt == elf_plt_sect + 1) {
948 elf_add_gsym_reloc(s, segment, addr+size, size,
949 R_X86_64_PLT32, true);
950 addr = 0;
951 } else if (wrt == elf_gotpc_sect + 1 ||
952 wrt == elf_got_sect + 1) {
953 elf_add_gsym_reloc(s, segment, addr+size, size,
954 R_X86_64_GOTPCREL, true);
955 addr = 0;
956 } else if (wrt == elf_gotoff_sect + 1 ||
957 wrt == elf_got_sect + 1) {
958 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
959 "qword absolute");
960 } else if (wrt == elf_gottpoff_sect + 1) {
961 elf_add_gsym_reloc(s, segment, addr+size, size,
962 R_X86_64_GOTTPOFF, true);
963 addr = 0;
964 } else {
965 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
966 " use of WRT");
969 elf_sect_writeaddr(s, addr, 4);
970 break;
972 case OUT_REL8ADR:
973 addr = *(int64_t *)data - size;
974 if (segment == segto)
975 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
976 if (segment == NO_SEG) {
977 /* Do nothing */
978 } else if (segment % 2) {
979 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
980 " segment base references");
981 } else {
982 if (wrt == NO_SEG) {
983 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
984 addr = 0;
985 } else if (wrt == elf_gotpc_sect + 1 ||
986 wrt == elf_got_sect + 1) {
987 elf_add_gsym_reloc(s, segment, addr+size, size,
988 R_X86_64_GOTPCREL64, true);
989 addr = 0;
990 } else if (wrt == elf_gotoff_sect + 1 ||
991 wrt == elf_got_sect + 1) {
992 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
993 "absolute");
994 } else if (wrt == elf_gottpoff_sect + 1) {
995 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
996 "dword");
997 } else {
998 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
999 " use of WRT");
1002 elf_sect_writeaddr(s, addr, 8);
1003 break;
1007 static void elf_write(void)
1009 int align;
1010 char *p;
1011 int i;
1013 struct SAA *symtab;
1014 int32_t symtablen, symtablocal;
1017 * Work out how many sections we will have. We have SHN_UNDEF,
1018 * then the flexible user sections, then the fixed sections
1019 * `.shstrtab', `.symtab' and `.strtab', then optionally
1020 * relocation sections for the user sections.
1022 nsections = sec_numspecial + 1;
1023 if (of_elf64.current_dfmt == &df_stabs)
1024 nsections += 3;
1025 else if (of_elf64.current_dfmt == &df_dwarf)
1026 nsections += 10;
1028 add_sectname("", ".shstrtab");
1029 add_sectname("", ".symtab");
1030 add_sectname("", ".strtab");
1031 for (i = 0; i < nsects; i++) {
1032 nsections++; /* for the section itself */
1033 if (sects[i]->head) {
1034 nsections++; /* for its relocations */
1035 add_sectname(".rela", sects[i]->name);
1039 if (of_elf64.current_dfmt == &df_stabs) {
1040 /* in case the debug information is wanted, just add these three sections... */
1041 add_sectname("", ".stab");
1042 add_sectname("", ".stabstr");
1043 add_sectname(".rel", ".stab");
1046 else if (of_elf64.current_dfmt == &df_dwarf) {
1047 /* the dwarf debug standard specifies the following ten sections,
1048 not all of which are currently implemented,
1049 although all of them are defined. */
1050 #define debug_aranges (int64_t) (nsections-10)
1051 #define debug_info (int64_t) (nsections-7)
1052 #define debug_abbrev (int64_t) (nsections-5)
1053 #define debug_line (int64_t) (nsections-4)
1054 add_sectname("", ".debug_aranges");
1055 add_sectname(".rela", ".debug_aranges");
1056 add_sectname("", ".debug_pubnames");
1057 add_sectname("", ".debug_info");
1058 add_sectname(".rela", ".debug_info");
1059 add_sectname("", ".debug_abbrev");
1060 add_sectname("", ".debug_line");
1061 add_sectname(".rela", ".debug_line");
1062 add_sectname("", ".debug_frame");
1063 add_sectname("", ".debug_loc");
1067 * Output the ELF header.
1069 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1070 fputc(elf_osabi, ofile);
1071 fputc(elf_abiver, ofile);
1072 fwritezero(7, ofile);
1073 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1074 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1075 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1076 fwriteint64_t(0L, ofile); /* no entry point */
1077 fwriteint64_t(0L, ofile); /* no program header table */
1078 fwriteint64_t(0x40L, ofile); /* section headers straight after
1079 * ELF header plus alignment */
1080 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1081 fwriteint16_t(0x40, ofile); /* size of ELF header */
1082 fwriteint16_t(0, ofile); /* no program header table, again */
1083 fwriteint16_t(0, ofile); /* still no program header table */
1084 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1085 fwriteint16_t(nsections, ofile); /* number of sections */
1086 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1087 * section header table */
1090 * Build the symbol table and relocation tables.
1092 symtab = elf_build_symtab(&symtablen, &symtablocal);
1093 for (i = 0; i < nsects; i++)
1094 if (sects[i]->head)
1095 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1096 sects[i]->head);
1099 * Now output the section header table.
1102 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1103 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1104 elf_foffs += align;
1105 elf_nsect = 0;
1106 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1108 /* SHN_UNDEF */
1109 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1110 p = shstrtab + 1;
1112 /* The normal sections */
1113 for (i = 0; i < nsects; i++) {
1114 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1115 (sects[i]->type == SHT_PROGBITS ?
1116 sects[i]->data : NULL), true,
1117 sects[i]->len, 0, 0, sects[i]->align, 0);
1118 p += strlen(p) + 1;
1121 /* .shstrtab */
1122 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1123 shstrtablen, 0, 0, 1, 0);
1124 p += strlen(p) + 1;
1126 /* .symtab */
1127 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1128 symtablen, sec_strtab, symtablocal, 4, 24);
1129 p += strlen(p) + 1;
1131 /* .strtab */
1132 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1133 strslen, 0, 0, 1, 0);
1134 p += strlen(p) + 1;
1136 /* The relocation sections */
1137 for (i = 0; i < nsects; i++)
1138 if (sects[i]->head) {
1139 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1140 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1141 p += strlen(p) + 1;
1144 if (of_elf64.current_dfmt == &df_stabs) {
1145 /* for debugging information, create the last three sections
1146 which are the .stab , .stabstr and .rel.stab sections respectively */
1148 /* this function call creates the stab sections in memory */
1149 stabs64_generate();
1151 if (stabbuf && stabstrbuf && stabrelbuf) {
1152 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1153 stablen, sec_stabstr, 0, 4, 12);
1154 p += strlen(p) + 1;
1156 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1157 stabstrlen, 0, 0, 4, 0);
1158 p += strlen(p) + 1;
1160 /* link -> symtable info -> section to refer to */
1161 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1162 stabrellen, symtabsection, sec_stab, 4, 16);
1163 p += strlen(p) + 1;
1165 } else if (of_elf64.current_dfmt == &df_dwarf) {
1166 /* for dwarf debugging information, create the ten dwarf sections */
1168 /* this function call creates the dwarf sections in memory */
1169 if (dwarf_fsect)
1170 dwarf64_generate();
1172 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1173 arangeslen, 0, 0, 1, 0);
1174 p += strlen(p) + 1;
1176 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1177 arangesrellen, symtabsection, debug_aranges, 1, 24);
1178 p += strlen(p) + 1;
1180 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1181 pubnameslen, 0, 0, 1, 0);
1182 p += strlen(p) + 1;
1184 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1185 infolen, 0, 0, 1, 0);
1186 p += strlen(p) + 1;
1188 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1189 inforellen, symtabsection, debug_info, 1, 24);
1190 p += strlen(p) + 1;
1192 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1193 abbrevlen, 0, 0, 1, 0);
1194 p += strlen(p) + 1;
1196 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1197 linelen, 0, 0, 1, 0);
1198 p += strlen(p) + 1;
1200 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1201 linerellen, symtabsection, debug_line, 1, 24);
1202 p += strlen(p) + 1;
1204 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1205 framelen, 0, 0, 8, 0);
1206 p += strlen(p) + 1;
1208 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1209 loclen, 0, 0, 1, 0);
1210 p += strlen(p) + 1;
1212 fwritezero(align, ofile);
1215 * Now output the sections.
1217 elf_write_sections();
1219 nasm_free(elf_sects);
1220 saa_free(symtab);
1223 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1225 struct SAA *s = saa_init(1L);
1226 struct Symbol *sym;
1227 uint8_t entry[24], *p;
1228 int i;
1230 *len = *local = 0;
1233 * First, an all-zeros entry, required by the ELF spec.
1235 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1236 *len += 24;
1237 (*local)++;
1240 * Next, an entry for the file name.
1242 p = entry;
1243 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1244 WRITESHORT(p, STT_FILE); /* type FILE */
1245 WRITESHORT(p, SHN_ABS);
1246 WRITEDLONG(p, (uint64_t) 0); /* no value */
1247 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1248 saa_wbytes(s, entry, 24L);
1249 *len += 24;
1250 (*local)++;
1253 * Now some standard symbols defining the segments, for relocation
1254 * purposes.
1256 for (i = 1; i <= nsects; i++) {
1257 p = entry;
1258 WRITELONG(p, 0); /* no symbol name */
1259 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1260 WRITESHORT(p, i); /* section id */
1261 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1262 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1263 saa_wbytes(s, entry, 24L);
1264 *len += 24;
1265 (*local)++;
1270 * Now the other local symbols.
1272 saa_rewind(syms);
1273 while ((sym = saa_rstruct(syms))) {
1274 if (sym->type & SYM_GLOBAL)
1275 continue;
1276 p = entry;
1277 WRITELONG(p, sym->strpos); /* index into symbol string table */
1278 WRITECHAR(p, sym->type); /* type and binding */
1279 WRITECHAR(p, sym->other); /* visibility */
1280 WRITESHORT(p, sym->section); /* index into section header table */
1281 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1282 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1283 saa_wbytes(s, entry, 24L);
1284 *len += 24;
1285 (*local)++;
1288 * dwarf needs symbols for debug sections
1289 * which are relocation targets.
1291 if (of_elf64.current_dfmt == &df_dwarf) {
1292 dwarf_infosym = *local;
1293 p = entry;
1294 WRITELONG(p, 0); /* no symbol name */
1295 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1296 WRITESHORT(p, debug_info); /* section id */
1297 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1298 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1299 saa_wbytes(s, entry, 24L);
1300 *len += 24;
1301 (*local)++;
1302 dwarf_abbrevsym = *local;
1303 p = entry;
1304 WRITELONG(p, 0); /* no symbol name */
1305 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1306 WRITESHORT(p, debug_abbrev); /* section id */
1307 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1308 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1309 saa_wbytes(s, entry, 24L);
1310 *len += 24;
1311 (*local)++;
1312 dwarf_linesym = *local;
1313 p = entry;
1314 WRITELONG(p, 0); /* no symbol name */
1315 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1316 WRITESHORT(p, debug_line); /* section id */
1317 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1318 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1319 saa_wbytes(s, entry, 24L);
1320 *len += 24;
1321 (*local)++;
1325 * Now the global symbols.
1327 saa_rewind(syms);
1328 while ((sym = saa_rstruct(syms))) {
1329 if (!(sym->type & SYM_GLOBAL))
1330 continue;
1331 p = entry;
1332 WRITELONG(p, sym->strpos);
1333 WRITECHAR(p, sym->type); /* type and binding */
1334 WRITECHAR(p, sym->other); /* visibility */
1335 WRITESHORT(p, sym->section);
1336 WRITEDLONG(p, (int64_t)sym->symv.key);
1337 WRITEDLONG(p, (int64_t)sym->size);
1338 saa_wbytes(s, entry, 24L);
1339 *len += 24;
1342 return s;
1345 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1347 struct SAA *s;
1348 uint8_t *p, entry[24];
1349 int32_t global_offset;
1351 if (!r)
1352 return NULL;
1354 s = saa_init(1L);
1355 *len = 0;
1358 * How to onvert from a global placeholder to a real symbol index;
1359 * the +2 refers to the two special entries, the null entry and
1360 * the filename entry.
1362 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1364 while (r) {
1365 int32_t sym = r->symbol;
1367 if (sym >= GLOBAL_TEMP_BASE)
1368 sym += global_offset;
1370 p = entry;
1371 WRITEDLONG(p, r->address);
1372 WRITELONG(p, r->type);
1373 WRITELONG(p, sym);
1374 WRITEDLONG(p, r->offset);
1375 saa_wbytes(s, entry, 24L);
1376 *len += 24;
1378 r = r->next;
1381 return s;
1384 static void elf_section_header(int name, int type, uint64_t flags,
1385 void *data, bool is_saa, uint64_t datalen,
1386 int link, int info, int align, int eltsize)
1388 elf_sects[elf_nsect].data = data;
1389 elf_sects[elf_nsect].len = datalen;
1390 elf_sects[elf_nsect].is_saa = is_saa;
1391 elf_nsect++;
1393 fwriteint32_t((int32_t)name, ofile);
1394 fwriteint32_t((int32_t)type, ofile);
1395 fwriteint64_t((int64_t)flags, ofile);
1396 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1397 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1398 fwriteint64_t(datalen, ofile);
1399 if (data)
1400 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1401 fwriteint32_t((int32_t)link, ofile);
1402 fwriteint32_t((int32_t)info, ofile);
1403 fwriteint64_t((int64_t)align, ofile);
1404 fwriteint64_t((int64_t)eltsize, ofile);
1407 static void elf_write_sections(void)
1409 int i;
1410 for (i = 0; i < elf_nsect; i++)
1411 if (elf_sects[i].data) {
1412 int32_t len = elf_sects[i].len;
1413 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1414 int32_t align = reallen - len;
1415 if (elf_sects[i].is_saa)
1416 saa_fpwrite(elf_sects[i].data, ofile);
1417 else
1418 fwrite(elf_sects[i].data, len, 1, ofile);
1419 fwritezero(align, ofile);
1423 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1425 saa_wbytes(sect->data, data, len);
1426 sect->len += len;
1429 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1431 saa_writeaddr(sect->data, data, len);
1432 sect->len += len;
1435 static void elf_sectalign(int32_t seg, unsigned int value)
1437 struct Section *s = NULL;
1438 int i;
1440 for (i = 0; i < nsects; i++) {
1441 if (sects[i]->index == seg) {
1442 s = sects[i];
1443 break;
1446 if (!s || !is_power2(value))
1447 return;
1449 if (value > s->align)
1450 s->align = value;
1453 static int32_t elf_segbase(int32_t segment)
1455 return segment;
1458 static int elf_directive(enum directives directive, char *value, int pass)
1460 bool err;
1461 int64_t n;
1462 char *p;
1464 switch (directive) {
1465 case D_OSABI:
1466 if (pass == 2)
1467 return 1; /* ignore in pass 2 */
1469 n = readnum(value, &err);
1470 if (err) {
1471 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1472 return 1;
1474 if (n < 0 || n > 255) {
1475 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1476 return 1;
1478 elf_osabi = n;
1479 elf_abiver = 0;
1481 if ((p = strchr(value,',')) == NULL)
1482 return 1;
1484 n = readnum(p+1, &err);
1485 if (err || n < 0 || n > 255) {
1486 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1487 return 1;
1490 elf_abiver = n;
1491 return 1;
1493 default:
1494 return 0;
1498 static void elf_filename(char *inname, char *outname)
1500 strcpy(elf_module, inname);
1501 standard_extension(inname, outname, ".o");
1504 extern macros_t elf_stdmac[];
1506 static int elf_set_info(enum geninfo type, char **val)
1508 (void)type;
1509 (void)val;
1510 return 0;
1512 static struct dfmt df_dwarf = {
1513 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1514 "dwarf",
1515 dwarf64_init,
1516 dwarf64_linenum,
1517 debug64_deflabel,
1518 debug64_directive,
1519 debug64_typevalue,
1520 dwarf64_output,
1521 dwarf64_cleanup
1523 static struct dfmt df_stabs = {
1524 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1525 "stabs",
1526 null_debug_init,
1527 stabs64_linenum,
1528 debug64_deflabel,
1529 debug64_directive,
1530 debug64_typevalue,
1531 stabs64_output,
1532 stabs64_cleanup
1535 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1537 struct ofmt of_elf64 = {
1538 "ELF64 (x86_64) object files (e.g. Linux)",
1539 "elf64",
1541 elf64_debugs_arr,
1542 &df_stabs,
1543 elf_stdmac,
1544 elf_init,
1545 elf_set_info,
1546 elf_out,
1547 elf_deflabel,
1548 elf_section_names,
1549 elf_sectalign,
1550 elf_segbase,
1551 elf_directive,
1552 elf_filename,
1553 elf_cleanup
1556 /* common debugging routines */
1557 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1558 int is_global, char *special)
1560 (void)name;
1561 (void)segment;
1562 (void)offset;
1563 (void)is_global;
1564 (void)special;
1567 static void debug64_directive(const char *directive, const char *params)
1569 (void)directive;
1570 (void)params;
1573 static void debug64_typevalue(int32_t type)
1575 int32_t stype, ssize;
1576 switch (TYM_TYPE(type)) {
1577 case TY_LABEL:
1578 ssize = 0;
1579 stype = STT_NOTYPE;
1580 break;
1581 case TY_BYTE:
1582 ssize = 1;
1583 stype = STT_OBJECT;
1584 break;
1585 case TY_WORD:
1586 ssize = 2;
1587 stype = STT_OBJECT;
1588 break;
1589 case TY_DWORD:
1590 ssize = 4;
1591 stype = STT_OBJECT;
1592 break;
1593 case TY_FLOAT:
1594 ssize = 4;
1595 stype = STT_OBJECT;
1596 break;
1597 case TY_QWORD:
1598 ssize = 8;
1599 stype = STT_OBJECT;
1600 break;
1601 case TY_TBYTE:
1602 ssize = 10;
1603 stype = STT_OBJECT;
1604 break;
1605 case TY_OWORD:
1606 ssize = 16;
1607 stype = STT_OBJECT;
1608 break;
1609 case TY_YWORD:
1610 ssize = 32;
1611 stype = STT_OBJECT;
1612 break;
1613 case TY_COMMON:
1614 ssize = 0;
1615 stype = STT_COMMON;
1616 break;
1617 case TY_SEG:
1618 ssize = 0;
1619 stype = STT_SECTION;
1620 break;
1621 case TY_EXTERN:
1622 ssize = 0;
1623 stype = STT_NOTYPE;
1624 break;
1625 case TY_EQU:
1626 ssize = 0;
1627 stype = STT_NOTYPE;
1628 break;
1629 default:
1630 ssize = 0;
1631 stype = STT_NOTYPE;
1632 break;
1634 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1635 lastsym->size = ssize;
1636 lastsym->type = stype;
1640 /* stabs debugging routines */
1642 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1644 (void)segto;
1645 if (!stabs_filename) {
1646 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1647 strcpy(stabs_filename, filename);
1648 } else {
1649 if (strcmp(stabs_filename, filename)) {
1650 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1651 in fact, this leak comes in quite handy to maintain a list of files
1652 encountered so far in the symbol lines... */
1654 /* why not nasm_free(stabs_filename); we're done with the old one */
1656 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1657 strcpy(stabs_filename, filename);
1660 debug_immcall = 1;
1661 currentline = linenumber;
1665 static void stabs64_output(int type, void *param)
1667 struct symlininfo *s;
1668 struct linelist *el;
1669 if (type == TY_DEBUGSYMLIN) {
1670 if (debug_immcall) {
1671 s = (struct symlininfo *)param;
1672 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1673 return; /* line info is only collected for executable sections */
1674 numlinestabs++;
1675 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1676 el->info.offset = s->offset;
1677 el->info.section = s->section;
1678 el->info.name = s->name;
1679 el->line = currentline;
1680 el->filename = stabs_filename;
1681 el->next = 0;
1682 if (stabslines) {
1683 stabslines->last->next = el;
1684 stabslines->last = el;
1685 } else {
1686 stabslines = el;
1687 stabslines->last = el;
1691 debug_immcall = 0;
1694 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1696 static void stabs64_generate(void)
1698 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1699 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1700 char **allfiles;
1701 int *fileidx;
1703 struct linelist *ptr;
1705 ptr = stabslines;
1707 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1708 numfiles = 0;
1709 while (ptr) {
1710 if (numfiles == 0) {
1711 allfiles[0] = ptr->filename;
1712 numfiles++;
1713 } else {
1714 for (i = 0; i < numfiles; i++) {
1715 if (!strcmp(allfiles[i], ptr->filename))
1716 break;
1718 if (i >= numfiles) {
1719 allfiles[i] = ptr->filename;
1720 numfiles++;
1723 ptr = ptr->next;
1725 strsize = 1;
1726 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1727 for (i = 0; i < numfiles; i++) {
1728 fileidx[i] = strsize;
1729 strsize += strlen(allfiles[i]) + 1;
1731 mainfileindex = 0;
1732 for (i = 0; i < numfiles; i++) {
1733 if (!strcmp(allfiles[i], elf_module)) {
1734 mainfileindex = i;
1735 break;
1740 * worst case size of the stab buffer would be:
1741 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1742 * plus one "ending" entry
1744 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1745 sizeof(struct stabentry));
1746 ssbuf = (uint8_t *)nasm_malloc(strsize);
1747 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1748 rptr = rbuf;
1750 for (i = 0; i < numfiles; i++)
1751 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1752 ssbuf[0] = 0;
1754 stabstrlen = strsize; /* set global variable for length of stab strings */
1756 sptr = sbuf;
1757 ptr = stabslines;
1758 numstabs = 0;
1760 if (ptr) {
1762 * this is the first stab, its strx points to the filename of the
1763 * the source-file, the n_desc field should be set to the number
1764 * of remaining stabs
1766 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1768 /* this is the stab for the main source file */
1769 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1771 /* relocation table entry */
1774 * Since the symbol table has two entries before
1775 * the section symbols, the index in the info.section
1776 * member must be adjusted by adding 2
1779 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1780 WRITELONG(rptr, R_X86_64_32);
1781 WRITELONG(rptr, ptr->info.section + 2);
1783 numstabs++;
1784 currfile = mainfileindex;
1787 while (ptr) {
1788 if (strcmp(allfiles[currfile], ptr->filename)) {
1789 /* oops file has changed... */
1790 for (i = 0; i < numfiles; i++)
1791 if (!strcmp(allfiles[i], ptr->filename))
1792 break;
1793 currfile = i;
1794 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1795 ptr->info.offset);
1796 numstabs++;
1798 /* relocation table entry */
1800 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1801 WRITELONG(rptr, R_X86_64_32);
1802 WRITELONG(rptr, ptr->info.section + 2);
1805 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1806 numstabs++;
1808 /* relocation table entry */
1810 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1811 WRITELONG(rptr, R_X86_64_32);
1812 WRITELONG(rptr, ptr->info.section + 2);
1814 ptr = ptr->next;
1818 /* this is an "ending" token */
1819 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1820 numstabs++;
1822 ((struct stabentry *)sbuf)->n_desc = numstabs;
1824 nasm_free(allfiles);
1825 nasm_free(fileidx);
1827 stablen = (sptr - sbuf);
1828 stabrellen = (rptr - rbuf);
1829 stabrelbuf = rbuf;
1830 stabbuf = sbuf;
1831 stabstrbuf = ssbuf;
1834 static void stabs64_cleanup(void)
1836 struct linelist *ptr, *del;
1837 if (!stabslines)
1838 return;
1840 ptr = stabslines;
1841 while (ptr) {
1842 del = ptr;
1843 ptr = ptr->next;
1844 nasm_free(del);
1847 nasm_free(stabbuf);
1848 nasm_free(stabrelbuf);
1849 nasm_free(stabstrbuf);
1852 /* dwarf routines */
1854 static void dwarf64_init(void)
1856 ndebugs = 3; /* 3 debug symbols */
1859 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1860 int32_t segto)
1862 (void)segto;
1863 dwarf64_findfile(filename);
1864 debug_immcall = 1;
1865 currentline = linenumber;
1868 /* called from elf_out with type == TY_DEBUGSYMLIN */
1869 static void dwarf64_output(int type, void *param)
1871 int ln, aa, inx, maxln, soc;
1872 struct symlininfo *s;
1873 struct SAA *plinep;
1875 (void)type;
1877 s = (struct symlininfo *)param;
1879 /* line number info is only gathered for executable sections */
1880 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1881 return;
1883 /* Check if section index has changed */
1884 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1885 dwarf64_findsect(s->section);
1887 /* do nothing unless line or file has changed */
1888 if (!debug_immcall)
1889 return;
1891 ln = currentline - dwarf_csect->line;
1892 aa = s->offset - dwarf_csect->offset;
1893 inx = dwarf_clist->line;
1894 plinep = dwarf_csect->psaa;
1895 /* check for file change */
1896 if (!(inx == dwarf_csect->file)) {
1897 saa_write8(plinep,DW_LNS_set_file);
1898 saa_write8(plinep,inx);
1899 dwarf_csect->file = inx;
1901 /* check for line change */
1902 if (ln) {
1903 /* test if in range of special op code */
1904 maxln = line_base + line_range;
1905 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1906 if (ln >= line_base && ln < maxln && soc < 256) {
1907 saa_write8(plinep,soc);
1908 } else {
1909 saa_write8(plinep,DW_LNS_advance_line);
1910 saa_wleb128s(plinep,ln);
1911 if (aa) {
1912 saa_write8(plinep,DW_LNS_advance_pc);
1913 saa_wleb128u(plinep,aa);
1916 dwarf_csect->line = currentline;
1917 dwarf_csect->offset = s->offset;
1920 /* show change handled */
1921 debug_immcall = 0;
1925 static void dwarf64_generate(void)
1927 uint8_t *pbuf;
1928 int indx;
1929 struct linelist *ftentry;
1930 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1931 struct SAA *parangesrel, *plinesrel, *pinforel;
1932 struct sectlist *psect;
1933 size_t saalen, linepoff, totlen, highaddr;
1935 /* write epilogues for each line program range */
1936 /* and build aranges section */
1937 paranges = saa_init(1L);
1938 parangesrel = saa_init(1L);
1939 saa_write16(paranges,3); /* dwarf version */
1940 saa_write64(parangesrel, paranges->datalen+4);
1941 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1942 saa_write64(parangesrel, 0);
1943 saa_write32(paranges,0); /* offset into info */
1944 saa_write8(paranges,8); /* pointer size */
1945 saa_write8(paranges,0); /* not segmented */
1946 saa_write32(paranges,0); /* padding */
1947 /* iterate though sectlist entries */
1948 psect = dwarf_fsect;
1949 totlen = 0;
1950 highaddr = 0;
1951 for (indx = 0; indx < dwarf_nsections; indx++)
1953 plinep = psect->psaa;
1954 /* Line Number Program Epilogue */
1955 saa_write8(plinep,2); /* std op 2 */
1956 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1957 saa_write8(plinep,DW_LNS_extended_op);
1958 saa_write8(plinep,1); /* operand length */
1959 saa_write8(plinep,DW_LNE_end_sequence);
1960 totlen += plinep->datalen;
1961 /* range table relocation entry */
1962 saa_write64(parangesrel, paranges->datalen + 4);
1963 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1964 saa_write64(parangesrel, (uint64_t) 0);
1965 /* range table entry */
1966 saa_write64(paranges,0x0000); /* range start */
1967 saa_write64(paranges,sects[psect->section]->len); /* range length */
1968 highaddr += sects[psect->section]->len;
1969 /* done with this entry */
1970 psect = psect->next;
1972 saa_write64(paranges,0); /* null address */
1973 saa_write64(paranges,0); /* null length */
1974 saalen = paranges->datalen;
1975 arangeslen = saalen + 4;
1976 arangesbuf = pbuf = nasm_malloc(arangeslen);
1977 WRITELONG(pbuf,saalen); /* initial length */
1978 saa_rnbytes(paranges, pbuf, saalen);
1979 saa_free(paranges);
1981 /* build rela.aranges section */
1982 arangesrellen = saalen = parangesrel->datalen;
1983 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1984 saa_rnbytes(parangesrel, pbuf, saalen);
1985 saa_free(parangesrel);
1987 /* build pubnames section */
1988 ppubnames = saa_init(1L);
1989 saa_write16(ppubnames,3); /* dwarf version */
1990 saa_write32(ppubnames,0); /* offset into info */
1991 saa_write32(ppubnames,0); /* space used in info */
1992 saa_write32(ppubnames,0); /* end of list */
1993 saalen = ppubnames->datalen;
1994 pubnameslen = saalen + 4;
1995 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1996 WRITELONG(pbuf,saalen); /* initial length */
1997 saa_rnbytes(ppubnames, pbuf, saalen);
1998 saa_free(ppubnames);
2000 /* build info section */
2001 pinfo = saa_init(1L);
2002 pinforel = saa_init(1L);
2003 saa_write16(pinfo,3); /* dwarf version */
2004 saa_write64(pinforel, pinfo->datalen + 4);
2005 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2006 saa_write64(pinforel, 0);
2007 saa_write32(pinfo,0); /* offset into abbrev */
2008 saa_write8(pinfo,8); /* pointer size */
2009 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2010 saa_write64(pinforel, pinfo->datalen + 4);
2011 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2012 saa_write64(pinforel, 0);
2013 saa_write64(pinfo,0); /* DW_AT_low_pc */
2014 saa_write64(pinforel, pinfo->datalen + 4);
2015 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2016 saa_write64(pinforel, 0);
2017 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2018 saa_write64(pinforel, pinfo->datalen + 4);
2019 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2020 saa_write64(pinforel, 0);
2021 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2022 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2023 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2024 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2025 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2026 saa_write64(pinforel, pinfo->datalen + 4);
2027 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2028 saa_write64(pinforel, 0);
2029 saa_write64(pinfo,0); /* DW_AT_low_pc */
2030 saa_write64(pinfo,0); /* DW_AT_frame_base */
2031 saa_write8(pinfo,0); /* end of entries */
2032 saalen = pinfo->datalen;
2033 infolen = saalen + 4;
2034 infobuf = pbuf = nasm_malloc(infolen);
2035 WRITELONG(pbuf,saalen); /* initial length */
2036 saa_rnbytes(pinfo, pbuf, saalen);
2037 saa_free(pinfo);
2039 /* build rela.info section */
2040 inforellen = saalen = pinforel->datalen;
2041 inforelbuf = pbuf = nasm_malloc(inforellen);
2042 saa_rnbytes(pinforel, pbuf, saalen);
2043 saa_free(pinforel);
2045 /* build abbrev section */
2046 pabbrev = saa_init(1L);
2047 saa_write8(pabbrev,1); /* entry number LEB128u */
2048 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2049 saa_write8(pabbrev,1); /* has children */
2050 /* the following attributes and forms are all LEB128u values */
2051 saa_write8(pabbrev,DW_AT_low_pc);
2052 saa_write8(pabbrev,DW_FORM_addr);
2053 saa_write8(pabbrev,DW_AT_high_pc);
2054 saa_write8(pabbrev,DW_FORM_addr);
2055 saa_write8(pabbrev,DW_AT_stmt_list);
2056 saa_write8(pabbrev,DW_FORM_data4);
2057 saa_write8(pabbrev,DW_AT_name);
2058 saa_write8(pabbrev,DW_FORM_string);
2059 saa_write8(pabbrev,DW_AT_producer);
2060 saa_write8(pabbrev,DW_FORM_string);
2061 saa_write8(pabbrev,DW_AT_language);
2062 saa_write8(pabbrev,DW_FORM_data2);
2063 saa_write16(pabbrev,0); /* end of entry */
2064 /* LEB128u usage same as above */
2065 saa_write8(pabbrev,2); /* entry number */
2066 saa_write8(pabbrev,DW_TAG_subprogram);
2067 saa_write8(pabbrev,0); /* no children */
2068 saa_write8(pabbrev,DW_AT_low_pc);
2069 saa_write8(pabbrev,DW_FORM_addr);
2070 saa_write8(pabbrev,DW_AT_frame_base);
2071 saa_write8(pabbrev,DW_FORM_data4);
2072 saa_write16(pabbrev,0); /* end of entry */
2073 abbrevlen = saalen = pabbrev->datalen;
2074 abbrevbuf = pbuf = nasm_malloc(saalen);
2075 saa_rnbytes(pabbrev, pbuf, saalen);
2076 saa_free(pabbrev);
2078 /* build line section */
2079 /* prolog */
2080 plines = saa_init(1L);
2081 saa_write8(plines,1); /* Minimum Instruction Length */
2082 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2083 saa_write8(plines,line_base); /* Line Base */
2084 saa_write8(plines,line_range); /* Line Range */
2085 saa_write8(plines,opcode_base); /* Opcode Base */
2086 /* standard opcode lengths (# of LEB128u operands) */
2087 saa_write8(plines,0); /* Std opcode 1 length */
2088 saa_write8(plines,1); /* Std opcode 2 length */
2089 saa_write8(plines,1); /* Std opcode 3 length */
2090 saa_write8(plines,1); /* Std opcode 4 length */
2091 saa_write8(plines,1); /* Std opcode 5 length */
2092 saa_write8(plines,0); /* Std opcode 6 length */
2093 saa_write8(plines,0); /* Std opcode 7 length */
2094 saa_write8(plines,0); /* Std opcode 8 length */
2095 saa_write8(plines,1); /* Std opcode 9 length */
2096 saa_write8(plines,0); /* Std opcode 10 length */
2097 saa_write8(plines,0); /* Std opcode 11 length */
2098 saa_write8(plines,1); /* Std opcode 12 length */
2099 /* Directory Table */
2100 saa_write8(plines,0); /* End of table */
2101 /* File Name Table */
2102 ftentry = dwarf_flist;
2103 for (indx = 0;indx<dwarf_numfiles;indx++)
2105 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2106 saa_write8(plines,0); /* directory LEB128u */
2107 saa_write8(plines,0); /* time LEB128u */
2108 saa_write8(plines,0); /* size LEB128u */
2109 ftentry = ftentry->next;
2111 saa_write8(plines,0); /* End of table */
2112 linepoff = plines->datalen;
2113 linelen = linepoff + totlen + 10;
2114 linebuf = pbuf = nasm_malloc(linelen);
2115 WRITELONG(pbuf,linelen-4); /* initial length */
2116 WRITESHORT(pbuf,3); /* dwarf version */
2117 WRITELONG(pbuf,linepoff); /* offset to line number program */
2118 /* write line header */
2119 saalen = linepoff;
2120 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2121 pbuf += linepoff;
2122 saa_free(plines);
2123 /* concatonate line program ranges */
2124 linepoff += 13;
2125 plinesrel = saa_init(1L);
2126 psect = dwarf_fsect;
2127 for (indx = 0; indx < dwarf_nsections; indx++) {
2128 saa_write64(plinesrel, linepoff);
2129 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2130 saa_write64(plinesrel, (uint64_t) 0);
2131 plinep = psect->psaa;
2132 saalen = plinep->datalen;
2133 saa_rnbytes(plinep, pbuf, saalen);
2134 pbuf += saalen;
2135 linepoff += saalen;
2136 saa_free(plinep);
2137 /* done with this entry */
2138 psect = psect->next;
2142 /* build rela.lines section */
2143 linerellen =saalen = plinesrel->datalen;
2144 linerelbuf = pbuf = nasm_malloc(linerellen);
2145 saa_rnbytes(plinesrel, pbuf, saalen);
2146 saa_free(plinesrel);
2148 /* build frame section */
2149 framelen = 4;
2150 framebuf = pbuf = nasm_malloc(framelen);
2151 WRITELONG(pbuf,framelen-4); /* initial length */
2153 /* build loc section */
2154 loclen = 16;
2155 locbuf = pbuf = nasm_malloc(loclen);
2156 WRITEDLONG(pbuf,0); /* null beginning offset */
2157 WRITEDLONG(pbuf,0); /* null ending offset */
2160 static void dwarf64_cleanup(void)
2162 nasm_free(arangesbuf);
2163 nasm_free(arangesrelbuf);
2164 nasm_free(pubnamesbuf);
2165 nasm_free(infobuf);
2166 nasm_free(inforelbuf);
2167 nasm_free(abbrevbuf);
2168 nasm_free(linebuf);
2169 nasm_free(linerelbuf);
2170 nasm_free(framebuf);
2171 nasm_free(locbuf);
2174 static void dwarf64_findfile(const char * fname)
2176 int finx;
2177 struct linelist *match;
2179 /* return if fname is current file name */
2180 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2181 return;
2183 /* search for match */
2184 match = 0;
2185 if (dwarf_flist) {
2186 match = dwarf_flist;
2187 for (finx = 0; finx < dwarf_numfiles; finx++) {
2188 if (!(strcmp(fname, match->filename))) {
2189 dwarf_clist = match;
2190 return;
2195 /* add file name to end of list */
2196 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2197 dwarf_numfiles++;
2198 dwarf_clist->line = dwarf_numfiles;
2199 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2200 strcpy(dwarf_clist->filename,fname);
2201 dwarf_clist->next = 0;
2202 if (!dwarf_flist) { /* if first entry */
2203 dwarf_flist = dwarf_elist = dwarf_clist;
2204 dwarf_clist->last = 0;
2205 } else { /* chain to previous entry */
2206 dwarf_elist->next = dwarf_clist;
2207 dwarf_elist = dwarf_clist;
2211 static void dwarf64_findsect(const int index)
2213 int sinx;
2214 struct sectlist *match;
2215 struct SAA *plinep;
2217 /* return if index is current section index */
2218 if (dwarf_csect && (dwarf_csect->section == index))
2219 return;
2221 /* search for match */
2222 match = 0;
2223 if (dwarf_fsect) {
2224 match = dwarf_fsect;
2225 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2226 if (match->section == index) {
2227 dwarf_csect = match;
2228 return;
2230 match = match->next;
2234 /* add entry to end of list */
2235 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2236 dwarf_nsections++;
2237 dwarf_csect->psaa = plinep = saa_init(1L);
2238 dwarf_csect->line = 1;
2239 dwarf_csect->offset = 0;
2240 dwarf_csect->file = 1;
2241 dwarf_csect->section = index;
2242 dwarf_csect->next = 0;
2243 /* set relocatable address at start of line program */
2244 saa_write8(plinep,DW_LNS_extended_op);
2245 saa_write8(plinep,9); /* operand length */
2246 saa_write8(plinep,DW_LNE_set_address);
2247 saa_write64(plinep,0); /* Start Address */
2249 if (!dwarf_fsect) { /* if first entry */
2250 dwarf_fsect = dwarf_esect = dwarf_csect;
2251 dwarf_csect->last = 0;
2252 } else { /* chain to previous entry */
2253 dwarf_esect->next = dwarf_csect;
2254 dwarf_esect = dwarf_csect;
2258 #endif /* OF_ELF */