NASM 2.09
[nasm/avx512.git] / output / outelf64.c
blob9807c2a2f8a3b83b5c231095772a38f23f63ea9b
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 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 symlininfo info;
159 int line;
160 char *filename;
161 struct linelist *next;
162 struct linelist *last;
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_malloc(sizeof(*s));
313 if (type != SHT_NOBITS)
314 s->data = saa_init(1L);
315 s->head = NULL;
316 s->tail = &s->head;
317 s->len = s->size = 0;
318 s->nrelocs = 0;
319 if (!strcmp(name, ".text"))
320 s->index = def_seg;
321 else
322 s->index = seg_alloc();
323 add_sectname("", name);
324 s->name = nasm_malloc(1 + strlen(name));
325 strcpy(s->name, name);
326 s->type = type;
327 s->flags = flags;
328 s->align = align;
329 s->gsyms = NULL;
331 if (nsects >= sectlen)
332 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
333 sects[nsects++] = s;
335 return nsects - 1;
338 static int32_t elf_section_names(char *name, int pass, int *bits)
340 char *p;
341 uint32_t flags, flags_and, flags_or;
342 uint64_t align;
343 int type, i;
346 * Default is 64 bits.
348 if (!name) {
349 *bits = 64;
350 return def_seg;
353 p = nasm_skip_word(name);
354 if (*p)
355 *p++ = '\0';
356 flags_and = flags_or = type = align = 0;
358 section_attrib(name, p, pass, &flags_and,
359 &flags_or, &align, &type);
361 if (!strcmp(name, ".shstrtab") ||
362 !strcmp(name, ".symtab") ||
363 !strcmp(name, ".strtab")) {
364 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
365 "name `%s'", name);
366 return NO_SEG;
369 for (i = 0; i < nsects; i++)
370 if (!strcmp(name, sects[i]->name))
371 break;
372 if (i == nsects) {
373 const struct elf_known_section *ks = elf_known_sections;
375 while (ks->name) {
376 if (!strcmp(name, ks->name))
377 break;
378 ks++;
381 type = type ? type : ks->type;
382 align = align ? align : ks->align;
383 flags = (ks->flags & ~flags_and) | flags_or;
385 i = elf_make_section(name, type, flags, align);
386 } else if (pass == 1) {
387 if ((type && sects[i]->type != type)
388 || (align && sects[i]->align != align)
389 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
390 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
391 " redeclaration of section `%s'", name);
394 return sects[i]->index;
397 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
398 int is_global, char *special)
400 int pos = strslen;
401 struct Symbol *sym;
402 bool special_used = false;
404 #if defined(DEBUG) && DEBUG>2
405 nasm_error(ERR_DEBUG,
406 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
407 name, segment, offset, is_global, special);
408 #endif
409 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
411 * This is a NASM special symbol. We never allow it into
412 * the ELF symbol table, even if it's a valid one. If it
413 * _isn't_ a valid one, we should barf immediately.
415 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
416 strcmp(name, "..got") && strcmp(name, "..plt") &&
417 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
418 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
419 return;
422 if (is_global == 3) {
423 struct Symbol **s;
425 * Fix up a forward-reference symbol size from the first
426 * pass.
428 for (s = &fwds; *s; s = &(*s)->nextfwd)
429 if (!strcmp((*s)->name, name)) {
430 struct tokenval tokval;
431 expr *e;
432 char *p = nasm_skip_spaces(nasm_skip_word(special));
434 stdscan_reset();
435 stdscan_set(p);
436 tokval.t_type = TOKEN_INVALID;
437 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
438 if (e) {
439 if (!is_simple(e))
440 nasm_error(ERR_NONFATAL, "cannot use relocatable"
441 " expression as symbol size");
442 else
443 (*s)->size = reloc_value(e);
447 * Remove it from the list of unresolved sizes.
449 nasm_free((*s)->name);
450 *s = (*s)->nextfwd;
451 return;
453 return; /* it wasn't an important one */
456 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
457 strslen += 1 + strlen(name);
459 lastsym = sym = saa_wstruct(syms);
461 memset(&sym->symv, 0, sizeof(struct rbtree));
463 sym->strpos = pos;
464 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
465 sym->other = STV_DEFAULT;
466 sym->size = 0;
467 if (segment == NO_SEG)
468 sym->section = SHN_ABS;
469 else {
470 int i;
471 sym->section = SHN_UNDEF;
472 if (segment == def_seg) {
473 /* we have to be sure at least text section is there */
474 int tempint;
475 if (segment != elf_section_names(".text", 2, &tempint))
476 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
478 for (i = 0; i < nsects; i++) {
479 if (segment == sects[i]->index) {
480 sym->section = i + 1;
481 break;
486 if (is_global == 2) {
487 sym->size = offset;
488 sym->symv.key = 0;
489 sym->section = SHN_COMMON;
491 * We have a common variable. Check the special text to see
492 * if it's a valid number and power of two; if so, store it
493 * as the alignment for the common variable.
495 if (special) {
496 bool err;
497 sym->symv.key = readnum(special, &err);
498 if (err)
499 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
500 " valid number", special);
501 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
502 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
503 " power of two", special);
505 special_used = true;
506 } else
507 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
509 if (sym->type == SYM_GLOBAL) {
511 * If sym->section == SHN_ABS, then the first line of the
512 * else section would cause a core dump, because its a reference
513 * beyond the end of the section array.
514 * This behaviour is exhibited by this code:
515 * GLOBAL crash_nasm
516 * crash_nasm equ 0
517 * To avoid such a crash, such requests are silently discarded.
518 * This may not be the best solution.
520 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
521 bsym = raa_write(bsym, segment, nglobs);
522 } else if (sym->section != SHN_ABS) {
524 * This is a global symbol; so we must add it to the rbtree
525 * of global symbols in its section.
527 * In addition, we check the special text for symbol
528 * type and size information.
530 sects[sym->section-1]->gsyms =
531 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
533 if (special) {
534 int n = strcspn(special, " \t");
536 if (!nasm_strnicmp(special, "function", n))
537 sym->type |= STT_FUNC;
538 else if (!nasm_strnicmp(special, "data", n) ||
539 !nasm_strnicmp(special, "object", n))
540 sym->type |= STT_OBJECT;
541 else if (!nasm_strnicmp(special, "notype", n))
542 sym->type |= STT_NOTYPE;
543 else
544 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
545 n, special);
546 special += n;
548 special = nasm_skip_spaces(special);
549 if (*special) {
550 n = strcspn(special, " \t");
551 if (!nasm_strnicmp(special, "default", n))
552 sym->other = STV_DEFAULT;
553 else if (!nasm_strnicmp(special, "internal", n))
554 sym->other = STV_INTERNAL;
555 else if (!nasm_strnicmp(special, "hidden", n))
556 sym->other = STV_HIDDEN;
557 else if (!nasm_strnicmp(special, "protected", n))
558 sym->other = STV_PROTECTED;
559 else
560 n = 0;
561 special += n;
564 if (*special) {
565 struct tokenval tokval;
566 expr *e;
567 int fwd = 0;
568 char *saveme = stdscan_get();
570 while (special[n] && nasm_isspace(special[n]))
571 n++;
573 * We have a size expression; attempt to
574 * evaluate it.
576 stdscan_reset();
577 stdscan_set(special + n);
578 tokval.t_type = TOKEN_INVALID;
579 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
580 NULL);
581 if (fwd) {
582 sym->nextfwd = fwds;
583 fwds = sym;
584 sym->name = nasm_strdup(name);
585 } else if (e) {
586 if (!is_simple(e))
587 nasm_error(ERR_NONFATAL, "cannot use relocatable"
588 " expression as symbol size");
589 else
590 sym->size = reloc_value(e);
592 stdscan_set(saveme);
594 special_used = true;
597 * If TLS segment, mark symbol accordingly.
599 if (sects[sym->section - 1]->flags & SHF_TLS) {
600 sym->type &= 0xf0;
601 sym->type |= STT_TLS;
604 sym->globnum = nglobs;
605 nglobs++;
606 } else
607 nlocals++;
609 if (special && !special_used)
610 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
613 static void elf_add_reloc(struct Section *sect, int32_t segment,
614 int64_t offset, int type)
616 struct Reloc *r;
617 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
618 sect->tail = &r->next;
619 r->next = NULL;
621 r->address = sect->len;
622 r->offset = offset;
623 if (segment == NO_SEG)
624 r->symbol = 0;
625 else {
626 int i;
627 r->symbol = 0;
628 for (i = 0; i < nsects; i++)
629 if (segment == sects[i]->index)
630 r->symbol = i + 2;
631 if (!r->symbol)
632 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
634 r->type = type;
636 sect->nrelocs++;
640 * This routine deals with ..got and ..sym relocations: the more
641 * complicated kinds. In shared-library writing, some relocations
642 * with respect to global symbols must refer to the precise symbol
643 * rather than referring to an offset from the base of the section
644 * _containing_ the symbol. Such relocations call to this routine,
645 * which searches the symbol list for the symbol in question.
647 * R_386_GOT32 references require the _exact_ symbol address to be
648 * used; R_386_32 references can be at an offset from the symbol.
649 * The boolean argument `exact' tells us this.
651 * Return value is the adjusted value of `addr', having become an
652 * offset from the symbol rather than the section. Should always be
653 * zero when returning from an exact call.
655 * Limitation: if you define two symbols at the same place,
656 * confusion will occur.
658 * Inefficiency: we search, currently, using a linked list which
659 * isn't even necessarily sorted.
661 static void elf_add_gsym_reloc(struct Section *sect,
662 int32_t segment, uint64_t offset, int64_t pcrel,
663 int type, bool exact)
665 struct Reloc *r;
666 struct Section *s;
667 struct Symbol *sym;
668 struct rbtree *srb;
669 int i;
672 * First look up the segment/offset pair and find a global
673 * symbol corresponding to it. If it's not one of our segments,
674 * then it must be an external symbol, in which case we're fine
675 * doing a normal elf_add_reloc after first sanity-checking
676 * that the offset from the symbol is zero.
678 s = NULL;
679 for (i = 0; i < nsects; i++)
680 if (segment == sects[i]->index) {
681 s = sects[i];
682 break;
685 if (!s) {
686 if (exact && offset)
687 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
688 else
689 elf_add_reloc(sect, segment, offset - pcrel, type);
690 return;
693 srb = rb_search(s->gsyms, offset);
694 if (!srb || (exact && srb->key != offset)) {
695 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
696 " for this reference");
697 return;
699 sym = container_of(srb, struct Symbol, symv);
701 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
702 sect->tail = &r->next;
703 r->next = NULL;
705 r->address = sect->len;
706 r->offset = offset - pcrel - sym->symv.key;
707 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
708 r->type = type;
710 sect->nrelocs++;
713 static void elf_out(int32_t segto, const void *data,
714 enum out_type type, uint64_t size,
715 int32_t segment, int32_t wrt)
717 struct Section *s;
718 int64_t addr, zero;
719 int reltype, bytes;
720 int i;
721 static struct symlininfo sinfo;
723 zero = 0;
725 #if defined(DEBUG) && DEBUG>2
726 if (data)
727 nasm_error(ERR_DEBUG,
728 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
729 currentline, type, segment, segto, size, *(int64_t *)data);
730 else
731 nasm_error(ERR_DEBUG,
732 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
733 currentline, type, segment, segto, size);
734 #endif
737 * handle absolute-assembly (structure definitions)
739 if (segto == NO_SEG) {
740 if (type != OUT_RESERVE)
741 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
742 " space");
743 return;
746 s = NULL;
747 for (i = 0; i < nsects; i++)
748 if (segto == sects[i]->index) {
749 s = sects[i];
750 break;
752 if (!s) {
753 int tempint; /* ignored */
754 if (segto != elf_section_names(".text", 2, &tempint))
755 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
756 else {
757 s = sects[nsects - 1];
758 i = nsects - 1;
762 /* again some stabs debugging stuff */
763 if (of_elf64.current_dfmt) {
764 sinfo.offset = s->len;
765 sinfo.section = i;
766 sinfo.segto = segto;
767 sinfo.name = s->name;
768 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
770 /* end of debugging stuff */
772 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
773 nasm_error(ERR_WARNING, "attempt to initialize memory in"
774 " BSS section `%s': ignored", s->name);
775 s->len += realsize(type, size);
776 return;
779 switch (type) {
780 case OUT_RESERVE:
781 if (s->type == SHT_PROGBITS) {
782 nasm_error(ERR_WARNING, "uninitialized space declared in"
783 " non-BSS section `%s': zeroing", s->name);
784 elf_sect_write(s, NULL, size);
785 } else
786 s->len += size;
787 break;
789 case OUT_RAWDATA:
790 if (segment != NO_SEG)
791 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
792 elf_sect_write(s, data, size);
793 break;
795 case OUT_ADDRESS:
796 addr = *(int64_t *)data;
797 if (segment == NO_SEG) {
798 /* Do nothing */
799 } else if (segment % 2) {
800 nasm_error(ERR_NONFATAL, "ELF format does not support"
801 " segment base references");
802 } else {
803 if (wrt == NO_SEG) {
804 switch ((int)size) {
805 case 1:
806 elf_add_reloc(s, segment, addr, R_X86_64_8);
807 break;
808 case 2:
809 elf_add_reloc(s, segment, addr, R_X86_64_16);
810 break;
811 case 4:
812 elf_add_reloc(s, segment, addr, R_X86_64_32);
813 break;
814 case 8:
815 elf_add_reloc(s, segment, addr, R_X86_64_64);
816 break;
817 default:
818 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
819 break;
821 addr = 0;
822 } else if (wrt == elf_gotpc_sect + 1) {
824 * The user will supply GOT relative to $$. ELF
825 * will let us have GOT relative to $. So we
826 * need to fix up the data item by $-$$.
828 addr += s->len;
829 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
830 addr = 0;
831 } else if (wrt == elf_gotoff_sect + 1) {
832 if (size != 8) {
833 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
834 "references to be qword");
835 } else {
836 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
837 addr = 0;
839 } else if (wrt == elf_got_sect + 1) {
840 switch ((int)size) {
841 case 4:
842 elf_add_gsym_reloc(s, segment, addr, 0,
843 R_X86_64_GOT32, true);
844 addr = 0;
845 break;
846 case 8:
847 elf_add_gsym_reloc(s, segment, addr, 0,
848 R_X86_64_GOT64, true);
849 addr = 0;
850 break;
851 default:
852 nasm_error(ERR_NONFATAL, "invalid ..got reference");
853 break;
855 } else if (wrt == elf_sym_sect + 1) {
856 switch ((int)size) {
857 case 1:
858 elf_add_gsym_reloc(s, segment, addr, 0,
859 R_X86_64_8, false);
860 addr = 0;
861 break;
862 case 2:
863 elf_add_gsym_reloc(s, segment, addr, 0,
864 R_X86_64_16, false);
865 addr = 0;
866 break;
867 case 4:
868 elf_add_gsym_reloc(s, segment, addr, 0,
869 R_X86_64_32, false);
870 addr = 0;
871 break;
872 case 8:
873 elf_add_gsym_reloc(s, segment, addr, 0,
874 R_X86_64_64, false);
875 addr = 0;
876 break;
877 default:
878 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
879 break;
881 } else if (wrt == elf_plt_sect + 1) {
882 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
883 "relative PLT references");
884 } else {
885 nasm_error(ERR_NONFATAL, "ELF format does not support this"
886 " use of WRT");
889 elf_sect_writeaddr(s, addr, size);
890 break;
892 case OUT_REL1ADR:
893 reltype = R_X86_64_PC8;
894 bytes = 1;
895 goto rel12adr;
897 case OUT_REL2ADR:
898 reltype = R_X86_64_PC16;
899 bytes = 2;
900 goto rel12adr;
902 rel12adr:
903 addr = *(int64_t *)data - size;
904 if (segment == segto)
905 nasm_error(ERR_PANIC, "intra-segment OUT_REL1ADR");
906 if (segment == NO_SEG) {
907 /* Do nothing */
908 } else if (segment % 2) {
909 nasm_error(ERR_NONFATAL, "ELF format does not support"
910 " segment base references");
911 } else {
912 if (wrt == NO_SEG) {
913 elf_add_reloc(s, segment, addr, reltype);
914 addr = 0;
915 } else {
916 nasm_error(ERR_NONFATAL,
917 "Unsupported non-32-bit ELF relocation");
920 elf_sect_writeaddr(s, addr, bytes);
921 break;
923 case OUT_REL4ADR:
924 addr = *(int64_t *)data - size;
925 if (segment == segto)
926 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
927 if (segment == NO_SEG) {
928 /* Do nothing */
929 } else if (segment % 2) {
930 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
931 " segment base references");
932 } else {
933 if (wrt == NO_SEG) {
934 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
935 addr = 0;
936 } else if (wrt == elf_plt_sect + 1) {
937 elf_add_gsym_reloc(s, segment, addr+size, size,
938 R_X86_64_PLT32, true);
939 addr = 0;
940 } else if (wrt == elf_gotpc_sect + 1 ||
941 wrt == elf_got_sect + 1) {
942 elf_add_gsym_reloc(s, segment, addr+size, size,
943 R_X86_64_GOTPCREL, true);
944 addr = 0;
945 } else if (wrt == elf_gotoff_sect + 1 ||
946 wrt == elf_got_sect + 1) {
947 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
948 "qword absolute");
949 } else if (wrt == elf_gottpoff_sect + 1) {
950 elf_add_gsym_reloc(s, segment, addr+size, size,
951 R_X86_64_GOTTPOFF, true);
952 addr = 0;
953 } else {
954 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
955 " use of WRT");
958 elf_sect_writeaddr(s, addr, 4);
959 break;
961 case OUT_REL8ADR:
962 addr = *(int64_t *)data - size;
963 if (segment == segto)
964 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
965 if (segment == NO_SEG) {
966 /* Do nothing */
967 } else if (segment % 2) {
968 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
969 " segment base references");
970 } else {
971 if (wrt == NO_SEG) {
972 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
973 addr = 0;
974 } else if (wrt == elf_gotpc_sect + 1 ||
975 wrt == elf_got_sect + 1) {
976 elf_add_gsym_reloc(s, segment, addr+size, size,
977 R_X86_64_GOTPCREL64, true);
978 addr = 0;
979 } else if (wrt == elf_gotoff_sect + 1 ||
980 wrt == elf_got_sect + 1) {
981 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
982 "absolute");
983 } else if (wrt == elf_gottpoff_sect + 1) {
984 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
985 "dword");
986 } else {
987 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
988 " use of WRT");
991 elf_sect_writeaddr(s, addr, 8);
992 break;
996 static void elf_write(void)
998 int align;
999 char *p;
1000 int i;
1002 struct SAA *symtab;
1003 int32_t symtablen, symtablocal;
1006 * Work out how many sections we will have. We have SHN_UNDEF,
1007 * then the flexible user sections, then the fixed sections
1008 * `.shstrtab', `.symtab' and `.strtab', then optionally
1009 * relocation sections for the user sections.
1011 nsections = sec_numspecial + 1;
1012 if (of_elf64.current_dfmt == &df_stabs)
1013 nsections += 3;
1014 else if (of_elf64.current_dfmt == &df_dwarf)
1015 nsections += 10;
1017 add_sectname("", ".shstrtab");
1018 add_sectname("", ".symtab");
1019 add_sectname("", ".strtab");
1020 for (i = 0; i < nsects; i++) {
1021 nsections++; /* for the section itself */
1022 if (sects[i]->head) {
1023 nsections++; /* for its relocations */
1024 add_sectname(".rela", sects[i]->name);
1028 if (of_elf64.current_dfmt == &df_stabs) {
1029 /* in case the debug information is wanted, just add these three sections... */
1030 add_sectname("", ".stab");
1031 add_sectname("", ".stabstr");
1032 add_sectname(".rel", ".stab");
1035 else if (of_elf64.current_dfmt == &df_dwarf) {
1036 /* the dwarf debug standard specifies the following ten sections,
1037 not all of which are currently implemented,
1038 although all of them are defined. */
1039 #define debug_aranges (int64_t) (nsections-10)
1040 #define debug_info (int64_t) (nsections-7)
1041 #define debug_abbrev (int64_t) (nsections-5)
1042 #define debug_line (int64_t) (nsections-4)
1043 add_sectname("", ".debug_aranges");
1044 add_sectname(".rela", ".debug_aranges");
1045 add_sectname("", ".debug_pubnames");
1046 add_sectname("", ".debug_info");
1047 add_sectname(".rela", ".debug_info");
1048 add_sectname("", ".debug_abbrev");
1049 add_sectname("", ".debug_line");
1050 add_sectname(".rela", ".debug_line");
1051 add_sectname("", ".debug_frame");
1052 add_sectname("", ".debug_loc");
1056 * Output the ELF header.
1058 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1059 fputc(elf_osabi, ofile);
1060 fputc(elf_abiver, ofile);
1061 fwritezero(7, ofile);
1062 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1063 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1064 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1065 fwriteint64_t(0L, ofile); /* no entry point */
1066 fwriteint64_t(0L, ofile); /* no program header table */
1067 fwriteint64_t(0x40L, ofile); /* section headers straight after
1068 * ELF header plus alignment */
1069 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1070 fwriteint16_t(0x40, ofile); /* size of ELF header */
1071 fwriteint16_t(0, ofile); /* no program header table, again */
1072 fwriteint16_t(0, ofile); /* still no program header table */
1073 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1074 fwriteint16_t(nsections, ofile); /* number of sections */
1075 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1076 * section header table */
1079 * Build the symbol table and relocation tables.
1081 symtab = elf_build_symtab(&symtablen, &symtablocal);
1082 for (i = 0; i < nsects; i++)
1083 if (sects[i]->head)
1084 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1085 sects[i]->head);
1088 * Now output the section header table.
1091 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1092 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1093 elf_foffs += align;
1094 elf_nsect = 0;
1095 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1097 /* SHN_UNDEF */
1098 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1099 p = shstrtab + 1;
1101 /* The normal sections */
1102 for (i = 0; i < nsects; i++) {
1103 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1104 (sects[i]->type == SHT_PROGBITS ?
1105 sects[i]->data : NULL), true,
1106 sects[i]->len, 0, 0, sects[i]->align, 0);
1107 p += strlen(p) + 1;
1110 /* .shstrtab */
1111 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1112 shstrtablen, 0, 0, 1, 0);
1113 p += strlen(p) + 1;
1115 /* .symtab */
1116 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1117 symtablen, sec_strtab, symtablocal, 4, 24);
1118 p += strlen(p) + 1;
1120 /* .strtab */
1121 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1122 strslen, 0, 0, 1, 0);
1123 p += strlen(p) + 1;
1125 /* The relocation sections */
1126 for (i = 0; i < nsects; i++)
1127 if (sects[i]->head) {
1128 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1129 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1130 p += strlen(p) + 1;
1133 if (of_elf64.current_dfmt == &df_stabs) {
1134 /* for debugging information, create the last three sections
1135 which are the .stab , .stabstr and .rel.stab sections respectively */
1137 /* this function call creates the stab sections in memory */
1138 stabs64_generate();
1140 if (stabbuf && stabstrbuf && stabrelbuf) {
1141 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1142 stablen, sec_stabstr, 0, 4, 12);
1143 p += strlen(p) + 1;
1145 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1146 stabstrlen, 0, 0, 4, 0);
1147 p += strlen(p) + 1;
1149 /* link -> symtable info -> section to refer to */
1150 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1151 stabrellen, symtabsection, sec_stab, 4, 16);
1152 p += strlen(p) + 1;
1154 } else if (of_elf64.current_dfmt == &df_dwarf) {
1155 /* for dwarf debugging information, create the ten dwarf sections */
1157 /* this function call creates the dwarf sections in memory */
1158 if (dwarf_fsect)
1159 dwarf64_generate();
1161 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1162 arangeslen, 0, 0, 1, 0);
1163 p += strlen(p) + 1;
1165 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1166 arangesrellen, symtabsection, debug_aranges, 1, 24);
1167 p += strlen(p) + 1;
1169 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1170 pubnameslen, 0, 0, 1, 0);
1171 p += strlen(p) + 1;
1173 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1174 infolen, 0, 0, 1, 0);
1175 p += strlen(p) + 1;
1177 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1178 inforellen, symtabsection, debug_info, 1, 24);
1179 p += strlen(p) + 1;
1181 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1182 abbrevlen, 0, 0, 1, 0);
1183 p += strlen(p) + 1;
1185 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1186 linelen, 0, 0, 1, 0);
1187 p += strlen(p) + 1;
1189 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1190 linerellen, symtabsection, debug_line, 1, 24);
1191 p += strlen(p) + 1;
1193 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1194 framelen, 0, 0, 8, 0);
1195 p += strlen(p) + 1;
1197 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1198 loclen, 0, 0, 1, 0);
1199 p += strlen(p) + 1;
1201 fwritezero(align, ofile);
1204 * Now output the sections.
1206 elf_write_sections();
1208 nasm_free(elf_sects);
1209 saa_free(symtab);
1212 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1214 struct SAA *s = saa_init(1L);
1215 struct Symbol *sym;
1216 uint8_t entry[24], *p;
1217 int i;
1219 *len = *local = 0;
1222 * First, an all-zeros entry, required by the ELF spec.
1224 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1225 *len += 24;
1226 (*local)++;
1229 * Next, an entry for the file name.
1231 p = entry;
1232 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1233 WRITESHORT(p, STT_FILE); /* type FILE */
1234 WRITESHORT(p, SHN_ABS);
1235 WRITEDLONG(p, (uint64_t) 0); /* no value */
1236 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1237 saa_wbytes(s, entry, 24L);
1238 *len += 24;
1239 (*local)++;
1242 * Now some standard symbols defining the segments, for relocation
1243 * purposes.
1245 for (i = 1; i <= nsects; i++) {
1246 p = entry;
1247 WRITELONG(p, 0); /* no symbol name */
1248 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1249 WRITESHORT(p, i); /* section id */
1250 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1251 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1252 saa_wbytes(s, entry, 24L);
1253 *len += 24;
1254 (*local)++;
1259 * Now the other local symbols.
1261 saa_rewind(syms);
1262 while ((sym = saa_rstruct(syms))) {
1263 if (sym->type & SYM_GLOBAL)
1264 continue;
1265 p = entry;
1266 WRITELONG(p, sym->strpos); /* index into symbol string table */
1267 WRITECHAR(p, sym->type); /* type and binding */
1268 WRITECHAR(p, sym->other); /* visibility */
1269 WRITESHORT(p, sym->section); /* index into section header table */
1270 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1271 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1272 saa_wbytes(s, entry, 24L);
1273 *len += 24;
1274 (*local)++;
1277 * dwarf needs symbols for debug sections
1278 * which are relocation targets.
1280 if (of_elf64.current_dfmt == &df_dwarf) {
1281 dwarf_infosym = *local;
1282 p = entry;
1283 WRITELONG(p, 0); /* no symbol name */
1284 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1285 WRITESHORT(p, debug_info); /* section id */
1286 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1287 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1288 saa_wbytes(s, entry, 24L);
1289 *len += 24;
1290 (*local)++;
1291 dwarf_abbrevsym = *local;
1292 p = entry;
1293 WRITELONG(p, 0); /* no symbol name */
1294 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1295 WRITESHORT(p, debug_abbrev); /* section id */
1296 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1297 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1298 saa_wbytes(s, entry, 24L);
1299 *len += 24;
1300 (*local)++;
1301 dwarf_linesym = *local;
1302 p = entry;
1303 WRITELONG(p, 0); /* no symbol name */
1304 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1305 WRITESHORT(p, debug_line); /* section id */
1306 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1307 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1308 saa_wbytes(s, entry, 24L);
1309 *len += 24;
1310 (*local)++;
1314 * Now the global symbols.
1316 saa_rewind(syms);
1317 while ((sym = saa_rstruct(syms))) {
1318 if (!(sym->type & SYM_GLOBAL))
1319 continue;
1320 p = entry;
1321 WRITELONG(p, sym->strpos);
1322 WRITECHAR(p, sym->type); /* type and binding */
1323 WRITECHAR(p, sym->other); /* visibility */
1324 WRITESHORT(p, sym->section);
1325 WRITEDLONG(p, (int64_t)sym->symv.key);
1326 WRITEDLONG(p, (int64_t)sym->size);
1327 saa_wbytes(s, entry, 24L);
1328 *len += 24;
1331 return s;
1334 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1336 struct SAA *s;
1337 uint8_t *p, entry[24];
1338 int32_t global_offset;
1340 if (!r)
1341 return NULL;
1343 s = saa_init(1L);
1344 *len = 0;
1347 * How to onvert from a global placeholder to a real symbol index;
1348 * the +2 refers to the two special entries, the null entry and
1349 * the filename entry.
1351 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1353 while (r) {
1354 int32_t sym = r->symbol;
1356 if (sym >= GLOBAL_TEMP_BASE)
1357 sym += global_offset;
1359 p = entry;
1360 WRITEDLONG(p, r->address);
1361 WRITELONG(p, r->type);
1362 WRITELONG(p, sym);
1363 WRITEDLONG(p, r->offset);
1364 saa_wbytes(s, entry, 24L);
1365 *len += 24;
1367 r = r->next;
1370 return s;
1373 static void elf_section_header(int name, int type, uint64_t flags,
1374 void *data, bool is_saa, uint64_t datalen,
1375 int link, int info, int align, int eltsize)
1377 elf_sects[elf_nsect].data = data;
1378 elf_sects[elf_nsect].len = datalen;
1379 elf_sects[elf_nsect].is_saa = is_saa;
1380 elf_nsect++;
1382 fwriteint32_t((int32_t)name, ofile);
1383 fwriteint32_t((int32_t)type, ofile);
1384 fwriteint64_t((int64_t)flags, ofile);
1385 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1386 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1387 fwriteint64_t(datalen, ofile);
1388 if (data)
1389 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1390 fwriteint32_t((int32_t)link, ofile);
1391 fwriteint32_t((int32_t)info, ofile);
1392 fwriteint64_t((int64_t)align, ofile);
1393 fwriteint64_t((int64_t)eltsize, ofile);
1396 static void elf_write_sections(void)
1398 int i;
1399 for (i = 0; i < elf_nsect; i++)
1400 if (elf_sects[i].data) {
1401 int32_t len = elf_sects[i].len;
1402 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1403 int32_t align = reallen - len;
1404 if (elf_sects[i].is_saa)
1405 saa_fpwrite(elf_sects[i].data, ofile);
1406 else
1407 fwrite(elf_sects[i].data, len, 1, ofile);
1408 fwritezero(align, ofile);
1412 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1414 saa_wbytes(sect->data, data, len);
1415 sect->len += len;
1417 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1419 saa_writeaddr(sect->data, data, len);
1420 sect->len += len;
1423 static void elf_sectalign(int32_t seg, unsigned int value)
1425 struct Section *s = NULL;
1426 int i;
1428 for (i = 0; i < nsects; i++) {
1429 if (sects[i]->index == seg) {
1430 s = sects[i];
1431 break;
1434 if (!s || !is_power2(value))
1435 return;
1437 if (value > s->align)
1438 s->align = value;
1441 static int32_t elf_segbase(int32_t segment)
1443 return segment;
1446 static int elf_directive(enum directives directive, char *value, int pass)
1448 bool err;
1449 int64_t n;
1450 char *p;
1452 switch (directive) {
1453 case D_OSABI:
1454 if (pass == 2)
1455 return 1; /* ignore in pass 2 */
1457 n = readnum(value, &err);
1458 if (err) {
1459 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1460 return 1;
1462 if (n < 0 || n > 255) {
1463 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1464 return 1;
1466 elf_osabi = n;
1467 elf_abiver = 0;
1469 if ((p = strchr(value,',')) == NULL)
1470 return 1;
1472 n = readnum(p+1, &err);
1473 if (err || n < 0 || n > 255) {
1474 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1475 return 1;
1478 elf_abiver = n;
1479 return 1;
1481 default:
1482 return 0;
1486 static void elf_filename(char *inname, char *outname)
1488 strcpy(elf_module, inname);
1489 standard_extension(inname, outname, ".o");
1492 extern macros_t elf_stdmac[];
1494 static int elf_set_info(enum geninfo type, char **val)
1496 (void)type;
1497 (void)val;
1498 return 0;
1500 static struct dfmt df_dwarf = {
1501 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1502 "dwarf",
1503 dwarf64_init,
1504 dwarf64_linenum,
1505 debug64_deflabel,
1506 debug64_directive,
1507 debug64_typevalue,
1508 dwarf64_output,
1509 dwarf64_cleanup
1511 static struct dfmt df_stabs = {
1512 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1513 "stabs",
1514 null_debug_init,
1515 stabs64_linenum,
1516 debug64_deflabel,
1517 debug64_directive,
1518 debug64_typevalue,
1519 stabs64_output,
1520 stabs64_cleanup
1523 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1525 struct ofmt of_elf64 = {
1526 "ELF64 (x86_64) object files (e.g. Linux)",
1527 "elf64",
1529 elf64_debugs_arr,
1530 &df_stabs,
1531 elf_stdmac,
1532 elf_init,
1533 elf_set_info,
1534 elf_out,
1535 elf_deflabel,
1536 elf_section_names,
1537 elf_sectalign,
1538 elf_segbase,
1539 elf_directive,
1540 elf_filename,
1541 elf_cleanup
1544 /* common debugging routines */
1545 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1546 int is_global, char *special)
1548 (void)name;
1549 (void)segment;
1550 (void)offset;
1551 (void)is_global;
1552 (void)special;
1555 static void debug64_directive(const char *directive, const char *params)
1557 (void)directive;
1558 (void)params;
1561 static void debug64_typevalue(int32_t type)
1563 int32_t stype, ssize;
1564 switch (TYM_TYPE(type)) {
1565 case TY_LABEL:
1566 ssize = 0;
1567 stype = STT_NOTYPE;
1568 break;
1569 case TY_BYTE:
1570 ssize = 1;
1571 stype = STT_OBJECT;
1572 break;
1573 case TY_WORD:
1574 ssize = 2;
1575 stype = STT_OBJECT;
1576 break;
1577 case TY_DWORD:
1578 ssize = 4;
1579 stype = STT_OBJECT;
1580 break;
1581 case TY_FLOAT:
1582 ssize = 4;
1583 stype = STT_OBJECT;
1584 break;
1585 case TY_QWORD:
1586 ssize = 8;
1587 stype = STT_OBJECT;
1588 break;
1589 case TY_TBYTE:
1590 ssize = 10;
1591 stype = STT_OBJECT;
1592 break;
1593 case TY_OWORD:
1594 ssize = 16;
1595 stype = STT_OBJECT;
1596 break;
1597 case TY_YWORD:
1598 ssize = 32;
1599 stype = STT_OBJECT;
1600 break;
1601 case TY_COMMON:
1602 ssize = 0;
1603 stype = STT_COMMON;
1604 break;
1605 case TY_SEG:
1606 ssize = 0;
1607 stype = STT_SECTION;
1608 break;
1609 case TY_EXTERN:
1610 ssize = 0;
1611 stype = STT_NOTYPE;
1612 break;
1613 case TY_EQU:
1614 ssize = 0;
1615 stype = STT_NOTYPE;
1616 break;
1617 default:
1618 ssize = 0;
1619 stype = STT_NOTYPE;
1620 break;
1622 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1623 lastsym->size = ssize;
1624 lastsym->type = stype;
1628 /* stabs debugging routines */
1630 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1632 (void)segto;
1633 if (!stabs_filename) {
1634 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1635 strcpy(stabs_filename, filename);
1636 } else {
1637 if (strcmp(stabs_filename, filename)) {
1638 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1639 in fact, this leak comes in quite handy to maintain a list of files
1640 encountered so far in the symbol lines... */
1642 /* why not nasm_free(stabs_filename); we're done with the old one */
1644 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1645 strcpy(stabs_filename, filename);
1648 debug_immcall = 1;
1649 currentline = linenumber;
1653 static void stabs64_output(int type, void *param)
1655 struct symlininfo *s;
1656 struct linelist *el;
1657 if (type == TY_DEBUGSYMLIN) {
1658 if (debug_immcall) {
1659 s = (struct symlininfo *)param;
1660 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1661 return; /* line info is only collected for executable sections */
1662 numlinestabs++;
1663 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1664 el->info.offset = s->offset;
1665 el->info.section = s->section;
1666 el->info.name = s->name;
1667 el->line = currentline;
1668 el->filename = stabs_filename;
1669 el->next = 0;
1670 if (stabslines) {
1671 stabslines->last->next = el;
1672 stabslines->last = el;
1673 } else {
1674 stabslines = el;
1675 stabslines->last = el;
1679 debug_immcall = 0;
1682 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1684 static void stabs64_generate(void)
1686 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1687 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1688 char **allfiles;
1689 int *fileidx;
1691 struct linelist *ptr;
1693 ptr = stabslines;
1695 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1696 for (i = 0; i < numlinestabs; i++)
1697 allfiles[i] = 0;
1698 numfiles = 0;
1699 while (ptr) {
1700 if (numfiles == 0) {
1701 allfiles[0] = ptr->filename;
1702 numfiles++;
1703 } else {
1704 for (i = 0; i < numfiles; i++) {
1705 if (!strcmp(allfiles[i], ptr->filename))
1706 break;
1708 if (i >= numfiles) {
1709 allfiles[i] = ptr->filename;
1710 numfiles++;
1713 ptr = ptr->next;
1715 strsize = 1;
1716 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1717 for (i = 0; i < numfiles; i++) {
1718 fileidx[i] = strsize;
1719 strsize += strlen(allfiles[i]) + 1;
1721 mainfileindex = 0;
1722 for (i = 0; i < numfiles; i++) {
1723 if (!strcmp(allfiles[i], elf_module)) {
1724 mainfileindex = i;
1725 break;
1730 * worst case size of the stab buffer would be:
1731 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1732 * plus one "ending" entry
1734 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1735 sizeof(struct stabentry));
1736 ssbuf = (uint8_t *)nasm_malloc(strsize);
1737 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1738 rptr = rbuf;
1740 for (i = 0; i < numfiles; i++)
1741 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1742 ssbuf[0] = 0;
1744 stabstrlen = strsize; /* set global variable for length of stab strings */
1746 sptr = sbuf;
1747 ptr = stabslines;
1748 numstabs = 0;
1750 if (ptr) {
1752 * this is the first stab, its strx points to the filename of the
1753 * the source-file, the n_desc field should be set to the number
1754 * of remaining stabs
1756 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1758 /* this is the stab for the main source file */
1759 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1761 /* relocation table entry */
1764 * Since the symbol table has two entries before
1765 * the section symbols, the index in the info.section
1766 * member must be adjusted by adding 2
1769 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1770 WRITELONG(rptr, R_X86_64_32);
1771 WRITELONG(rptr, ptr->info.section + 2);
1773 numstabs++;
1774 currfile = mainfileindex;
1777 while (ptr) {
1778 if (strcmp(allfiles[currfile], ptr->filename)) {
1779 /* oops file has changed... */
1780 for (i = 0; i < numfiles; i++)
1781 if (!strcmp(allfiles[i], ptr->filename))
1782 break;
1783 currfile = i;
1784 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1785 ptr->info.offset);
1786 numstabs++;
1788 /* relocation table entry */
1790 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1791 WRITELONG(rptr, R_X86_64_32);
1792 WRITELONG(rptr, ptr->info.section + 2);
1795 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, 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);
1804 ptr = ptr->next;
1808 /* this is an "ending" token */
1809 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1810 numstabs++;
1812 ((struct stabentry *)sbuf)->n_desc = numstabs;
1814 nasm_free(allfiles);
1815 nasm_free(fileidx);
1817 stablen = (sptr - sbuf);
1818 stabrellen = (rptr - rbuf);
1819 stabrelbuf = rbuf;
1820 stabbuf = sbuf;
1821 stabstrbuf = ssbuf;
1824 static void stabs64_cleanup(void)
1826 struct linelist *ptr, *del;
1827 if (!stabslines)
1828 return;
1830 ptr = stabslines;
1831 while (ptr) {
1832 del = ptr;
1833 ptr = ptr->next;
1834 nasm_free(del);
1837 nasm_free(stabbuf);
1838 nasm_free(stabrelbuf);
1839 nasm_free(stabstrbuf);
1842 /* dwarf routines */
1844 static void dwarf64_init(void)
1846 ndebugs = 3; /* 3 debug symbols */
1849 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1850 int32_t segto)
1852 (void)segto;
1853 dwarf64_findfile(filename);
1854 debug_immcall = 1;
1855 currentline = linenumber;
1858 /* called from elf_out with type == TY_DEBUGSYMLIN */
1859 static void dwarf64_output(int type, void *param)
1861 int ln, aa, inx, maxln, soc;
1862 struct symlininfo *s;
1863 struct SAA *plinep;
1865 (void)type;
1867 s = (struct symlininfo *)param;
1869 /* line number info is only gathered for executable sections */
1870 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1871 return;
1873 /* Check if section index has changed */
1874 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1875 dwarf64_findsect(s->section);
1877 /* do nothing unless line or file has changed */
1878 if (!debug_immcall)
1879 return;
1881 ln = currentline - dwarf_csect->line;
1882 aa = s->offset - dwarf_csect->offset;
1883 inx = dwarf_clist->line;
1884 plinep = dwarf_csect->psaa;
1885 /* check for file change */
1886 if (!(inx == dwarf_csect->file)) {
1887 saa_write8(plinep,DW_LNS_set_file);
1888 saa_write8(plinep,inx);
1889 dwarf_csect->file = inx;
1891 /* check for line change */
1892 if (ln) {
1893 /* test if in range of special op code */
1894 maxln = line_base + line_range;
1895 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1896 if (ln >= line_base && ln < maxln && soc < 256) {
1897 saa_write8(plinep,soc);
1898 } else {
1899 saa_write8(plinep,DW_LNS_advance_line);
1900 saa_wleb128s(plinep,ln);
1901 if (aa) {
1902 saa_write8(plinep,DW_LNS_advance_pc);
1903 saa_wleb128u(plinep,aa);
1906 dwarf_csect->line = currentline;
1907 dwarf_csect->offset = s->offset;
1910 /* show change handled */
1911 debug_immcall = 0;
1915 static void dwarf64_generate(void)
1917 uint8_t *pbuf;
1918 int indx;
1919 struct linelist *ftentry;
1920 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1921 struct SAA *parangesrel, *plinesrel, *pinforel;
1922 struct sectlist *psect;
1923 size_t saalen, linepoff, totlen, highaddr;
1925 /* write epilogues for each line program range */
1926 /* and build aranges section */
1927 paranges = saa_init(1L);
1928 parangesrel = saa_init(1L);
1929 saa_write16(paranges,3); /* dwarf version */
1930 saa_write64(parangesrel, paranges->datalen+4);
1931 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1932 saa_write64(parangesrel, 0);
1933 saa_write32(paranges,0); /* offset into info */
1934 saa_write8(paranges,8); /* pointer size */
1935 saa_write8(paranges,0); /* not segmented */
1936 saa_write32(paranges,0); /* padding */
1937 /* iterate though sectlist entries */
1938 psect = dwarf_fsect;
1939 totlen = 0;
1940 highaddr = 0;
1941 for (indx = 0; indx < dwarf_nsections; indx++)
1943 plinep = psect->psaa;
1944 /* Line Number Program Epilogue */
1945 saa_write8(plinep,2); /* std op 2 */
1946 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1947 saa_write8(plinep,DW_LNS_extended_op);
1948 saa_write8(plinep,1); /* operand length */
1949 saa_write8(plinep,DW_LNE_end_sequence);
1950 totlen += plinep->datalen;
1951 /* range table relocation entry */
1952 saa_write64(parangesrel, paranges->datalen + 4);
1953 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1954 saa_write64(parangesrel, (uint64_t) 0);
1955 /* range table entry */
1956 saa_write64(paranges,0x0000); /* range start */
1957 saa_write64(paranges,sects[psect->section]->len); /* range length */
1958 highaddr += sects[psect->section]->len;
1959 /* done with this entry */
1960 psect = psect->next;
1962 saa_write64(paranges,0); /* null address */
1963 saa_write64(paranges,0); /* null length */
1964 saalen = paranges->datalen;
1965 arangeslen = saalen + 4;
1966 arangesbuf = pbuf = nasm_malloc(arangeslen);
1967 WRITELONG(pbuf,saalen); /* initial length */
1968 saa_rnbytes(paranges, pbuf, saalen);
1969 saa_free(paranges);
1971 /* build rela.aranges section */
1972 arangesrellen = saalen = parangesrel->datalen;
1973 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1974 saa_rnbytes(parangesrel, pbuf, saalen);
1975 saa_free(parangesrel);
1977 /* build pubnames section */
1978 ppubnames = saa_init(1L);
1979 saa_write16(ppubnames,3); /* dwarf version */
1980 saa_write32(ppubnames,0); /* offset into info */
1981 saa_write32(ppubnames,0); /* space used in info */
1982 saa_write32(ppubnames,0); /* end of list */
1983 saalen = ppubnames->datalen;
1984 pubnameslen = saalen + 4;
1985 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1986 WRITELONG(pbuf,saalen); /* initial length */
1987 saa_rnbytes(ppubnames, pbuf, saalen);
1988 saa_free(ppubnames);
1990 /* build info section */
1991 pinfo = saa_init(1L);
1992 pinforel = saa_init(1L);
1993 saa_write16(pinfo,3); /* dwarf version */
1994 saa_write64(pinforel, pinfo->datalen + 4);
1995 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
1996 saa_write64(pinforel, 0);
1997 saa_write32(pinfo,0); /* offset into abbrev */
1998 saa_write8(pinfo,8); /* pointer size */
1999 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2000 saa_write64(pinforel, pinfo->datalen + 4);
2001 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2002 saa_write64(pinforel, 0);
2003 saa_write64(pinfo,0); /* DW_AT_low_pc */
2004 saa_write64(pinforel, pinfo->datalen + 4);
2005 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2006 saa_write64(pinforel, 0);
2007 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2008 saa_write64(pinforel, pinfo->datalen + 4);
2009 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2010 saa_write64(pinforel, 0);
2011 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2012 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2013 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2014 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2015 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2016 saa_write64(pinforel, pinfo->datalen + 4);
2017 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2018 saa_write64(pinforel, 0);
2019 saa_write64(pinfo,0); /* DW_AT_low_pc */
2020 saa_write64(pinfo,0); /* DW_AT_frame_base */
2021 saa_write8(pinfo,0); /* end of entries */
2022 saalen = pinfo->datalen;
2023 infolen = saalen + 4;
2024 infobuf = pbuf = nasm_malloc(infolen);
2025 WRITELONG(pbuf,saalen); /* initial length */
2026 saa_rnbytes(pinfo, pbuf, saalen);
2027 saa_free(pinfo);
2029 /* build rela.info section */
2030 inforellen = saalen = pinforel->datalen;
2031 inforelbuf = pbuf = nasm_malloc(inforellen);
2032 saa_rnbytes(pinforel, pbuf, saalen);
2033 saa_free(pinforel);
2035 /* build abbrev section */
2036 pabbrev = saa_init(1L);
2037 saa_write8(pabbrev,1); /* entry number LEB128u */
2038 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2039 saa_write8(pabbrev,1); /* has children */
2040 /* the following attributes and forms are all LEB128u values */
2041 saa_write8(pabbrev,DW_AT_low_pc);
2042 saa_write8(pabbrev,DW_FORM_addr);
2043 saa_write8(pabbrev,DW_AT_high_pc);
2044 saa_write8(pabbrev,DW_FORM_addr);
2045 saa_write8(pabbrev,DW_AT_stmt_list);
2046 saa_write8(pabbrev,DW_FORM_data4);
2047 saa_write8(pabbrev,DW_AT_name);
2048 saa_write8(pabbrev,DW_FORM_string);
2049 saa_write8(pabbrev,DW_AT_producer);
2050 saa_write8(pabbrev,DW_FORM_string);
2051 saa_write8(pabbrev,DW_AT_language);
2052 saa_write8(pabbrev,DW_FORM_data2);
2053 saa_write16(pabbrev,0); /* end of entry */
2054 /* LEB128u usage same as above */
2055 saa_write8(pabbrev,2); /* entry number */
2056 saa_write8(pabbrev,DW_TAG_subprogram);
2057 saa_write8(pabbrev,0); /* no children */
2058 saa_write8(pabbrev,DW_AT_low_pc);
2059 saa_write8(pabbrev,DW_FORM_addr);
2060 saa_write8(pabbrev,DW_AT_frame_base);
2061 saa_write8(pabbrev,DW_FORM_data4);
2062 saa_write16(pabbrev,0); /* end of entry */
2063 abbrevlen = saalen = pabbrev->datalen;
2064 abbrevbuf = pbuf = nasm_malloc(saalen);
2065 saa_rnbytes(pabbrev, pbuf, saalen);
2066 saa_free(pabbrev);
2068 /* build line section */
2069 /* prolog */
2070 plines = saa_init(1L);
2071 saa_write8(plines,1); /* Minimum Instruction Length */
2072 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2073 saa_write8(plines,line_base); /* Line Base */
2074 saa_write8(plines,line_range); /* Line Range */
2075 saa_write8(plines,opcode_base); /* Opcode Base */
2076 /* standard opcode lengths (# of LEB128u operands) */
2077 saa_write8(plines,0); /* Std opcode 1 length */
2078 saa_write8(plines,1); /* Std opcode 2 length */
2079 saa_write8(plines,1); /* Std opcode 3 length */
2080 saa_write8(plines,1); /* Std opcode 4 length */
2081 saa_write8(plines,1); /* Std opcode 5 length */
2082 saa_write8(plines,0); /* Std opcode 6 length */
2083 saa_write8(plines,0); /* Std opcode 7 length */
2084 saa_write8(plines,0); /* Std opcode 8 length */
2085 saa_write8(plines,1); /* Std opcode 9 length */
2086 saa_write8(plines,0); /* Std opcode 10 length */
2087 saa_write8(plines,0); /* Std opcode 11 length */
2088 saa_write8(plines,1); /* Std opcode 12 length */
2089 /* Directory Table */
2090 saa_write8(plines,0); /* End of table */
2091 /* File Name Table */
2092 ftentry = dwarf_flist;
2093 for (indx = 0;indx<dwarf_numfiles;indx++)
2095 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2096 saa_write8(plines,0); /* directory LEB128u */
2097 saa_write8(plines,0); /* time LEB128u */
2098 saa_write8(plines,0); /* size LEB128u */
2099 ftentry = ftentry->next;
2101 saa_write8(plines,0); /* End of table */
2102 linepoff = plines->datalen;
2103 linelen = linepoff + totlen + 10;
2104 linebuf = pbuf = nasm_malloc(linelen);
2105 WRITELONG(pbuf,linelen-4); /* initial length */
2106 WRITESHORT(pbuf,3); /* dwarf version */
2107 WRITELONG(pbuf,linepoff); /* offset to line number program */
2108 /* write line header */
2109 saalen = linepoff;
2110 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2111 pbuf += linepoff;
2112 saa_free(plines);
2113 /* concatonate line program ranges */
2114 linepoff += 13;
2115 plinesrel = saa_init(1L);
2116 psect = dwarf_fsect;
2117 for (indx = 0; indx < dwarf_nsections; indx++) {
2118 saa_write64(plinesrel, linepoff);
2119 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2120 saa_write64(plinesrel, (uint64_t) 0);
2121 plinep = psect->psaa;
2122 saalen = plinep->datalen;
2123 saa_rnbytes(plinep, pbuf, saalen);
2124 pbuf += saalen;
2125 linepoff += saalen;
2126 saa_free(plinep);
2127 /* done with this entry */
2128 psect = psect->next;
2132 /* build rela.lines section */
2133 linerellen =saalen = plinesrel->datalen;
2134 linerelbuf = pbuf = nasm_malloc(linerellen);
2135 saa_rnbytes(plinesrel, pbuf, saalen);
2136 saa_free(plinesrel);
2138 /* build frame section */
2139 framelen = 4;
2140 framebuf = pbuf = nasm_malloc(framelen);
2141 WRITELONG(pbuf,framelen-4); /* initial length */
2143 /* build loc section */
2144 loclen = 16;
2145 locbuf = pbuf = nasm_malloc(loclen);
2146 WRITEDLONG(pbuf,0); /* null beginning offset */
2147 WRITEDLONG(pbuf,0); /* null ending offset */
2150 static void dwarf64_cleanup(void)
2152 nasm_free(arangesbuf);
2153 nasm_free(arangesrelbuf);
2154 nasm_free(pubnamesbuf);
2155 nasm_free(infobuf);
2156 nasm_free(inforelbuf);
2157 nasm_free(abbrevbuf);
2158 nasm_free(linebuf);
2159 nasm_free(linerelbuf);
2160 nasm_free(framebuf);
2161 nasm_free(locbuf);
2164 static void dwarf64_findfile(const char * fname)
2166 int finx;
2167 struct linelist *match;
2169 /* return if fname is current file name */
2170 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2171 return;
2173 /* search for match */
2174 match = 0;
2175 if (dwarf_flist) {
2176 match = dwarf_flist;
2177 for (finx = 0; finx < dwarf_numfiles; finx++) {
2178 if (!(strcmp(fname, match->filename))) {
2179 dwarf_clist = match;
2180 return;
2185 /* add file name to end of list */
2186 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2187 dwarf_numfiles++;
2188 dwarf_clist->line = dwarf_numfiles;
2189 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2190 strcpy(dwarf_clist->filename,fname);
2191 dwarf_clist->next = 0;
2192 if (!dwarf_flist) { /* if first entry */
2193 dwarf_flist = dwarf_elist = dwarf_clist;
2194 dwarf_clist->last = 0;
2195 } else { /* chain to previous entry */
2196 dwarf_elist->next = dwarf_clist;
2197 dwarf_elist = dwarf_clist;
2201 static void dwarf64_findsect(const int index)
2203 int sinx;
2204 struct sectlist *match;
2205 struct SAA *plinep;
2207 /* return if index is current section index */
2208 if (dwarf_csect && (dwarf_csect->section == index))
2209 return;
2211 /* search for match */
2212 match = 0;
2213 if (dwarf_fsect) {
2214 match = dwarf_fsect;
2215 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2216 if ((match->section == index)) {
2217 dwarf_csect = match;
2218 return;
2220 match = match->next;
2224 /* add entry to end of list */
2225 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2226 dwarf_nsections++;
2227 dwarf_csect->psaa = plinep = saa_init(1L);
2228 dwarf_csect->line = 1;
2229 dwarf_csect->offset = 0;
2230 dwarf_csect->file = 1;
2231 dwarf_csect->section = index;
2232 dwarf_csect->next = 0;
2233 /* set relocatable address at start of line program */
2234 saa_write8(plinep,DW_LNS_extended_op);
2235 saa_write8(plinep,9); /* operand length */
2236 saa_write8(plinep,DW_LNE_set_address);
2237 saa_write64(plinep,0); /* Start Address */
2239 if (!dwarf_fsect) { /* if first entry */
2240 dwarf_fsect = dwarf_esect = dwarf_csect;
2241 dwarf_csect->last = 0;
2242 } else { /* chain to previous entry */
2243 dwarf_esect->next = dwarf_csect;
2244 dwarf_esect = dwarf_csect;
2248 #endif /* OF_ELF */