Elf: Rename SEG_ALIGN to SEC_FILEALIGN
[nasm/nasm.git] / output / outelf64.c
blob47eba5b98a27836d2045c753c68592fe7e7d071d
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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 stabentry {
147 uint32_t n_strx;
148 uint8_t n_type;
149 uint8_t n_other;
150 uint16_t n_desc;
151 uint32_t n_value;
154 struct erel {
155 int offset, info;
158 struct symlininfo {
159 int offset;
160 int section; /* index into sects[] */
161 int segto; /* internal section number */
162 char *name; /* shallow-copied pointer of section name */
165 struct linelist {
166 struct symlininfo info;
167 int line;
168 char *filename;
169 struct linelist *next;
170 struct linelist *last;
173 struct sectlist {
174 struct SAA *psaa;
175 int section;
176 int line;
177 int offset;
178 int file;
179 struct sectlist *next;
180 struct sectlist *last;
183 /* common debug variables */
184 static int currentline = 1;
185 static int debug_immcall = 0;
187 /* stabs debug variables */
188 static struct linelist *stabslines = 0;
189 static int numlinestabs = 0;
190 static char *stabs_filename = 0;
191 static int symtabsection;
192 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
193 static int stablen, stabstrlen, stabrellen;
195 /* dwarf debug variables */
196 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
197 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
198 static int dwarf_numfiles = 0, dwarf_nsections;
199 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
200 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
201 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
202 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
203 abbrevlen, linelen, linerellen, framelen, loclen;
204 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
207 static struct dfmt df_dwarf;
208 static struct dfmt df_stabs;
209 static struct Symbol *lastsym;
211 /* common debugging routines */
212 static void debug64_typevalue(int32_t);
213 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
214 static void debug64_directive(const char *, const char *);
216 /* stabs debugging routines */
217 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
218 static void stabs64_output(int, void *);
219 static void stabs64_generate(void);
220 static void stabs64_cleanup(void);
222 /* dwarf debugging routines */
223 static void dwarf64_init(void);
224 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
225 static void dwarf64_output(int, void *);
226 static void dwarf64_generate(void);
227 static void dwarf64_cleanup(void);
228 static void dwarf64_findfile(const char *);
229 static void dwarf64_findsect(const int);
232 * Special section numbers which are used to define ELF special
233 * symbols, which can be used with WRT to provide PIC relocation
234 * types.
236 static int32_t elf_gotpc_sect, elf_gotoff_sect;
237 static int32_t elf_got_sect, elf_plt_sect;
238 static int32_t elf_sym_sect;
239 static int32_t elf_gottpoff_sect;
241 static void elf_init(void)
243 maxbits = 64;
244 sects = NULL;
245 nsects = sectlen = 0;
246 syms = saa_init((int32_t)sizeof(struct Symbol));
247 nlocals = nglobs = ndebugs = 0;
248 bsym = raa_init();
249 strs = saa_init(1L);
250 saa_wbytes(strs, "\0", 1L);
251 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
252 strslen = 2 + strlen(elf_module);
253 shstrtab = NULL;
254 shstrtablen = shstrtabsize = 0;;
255 add_sectname("", "");
257 fwds = NULL;
259 elf_gotpc_sect = seg_alloc();
260 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
261 elf_gotoff_sect = seg_alloc();
262 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
263 elf_got_sect = seg_alloc();
264 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
265 elf_plt_sect = seg_alloc();
266 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
267 elf_sym_sect = seg_alloc();
268 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
269 elf_gottpoff_sect = seg_alloc();
270 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
272 def_seg = seg_alloc();
276 static void elf_cleanup(int debuginfo)
278 struct Reloc *r;
279 int i;
281 (void)debuginfo;
283 elf_write();
284 for (i = 0; i < nsects; i++) {
285 if (sects[i]->type != SHT_NOBITS)
286 saa_free(sects[i]->data);
287 if (sects[i]->head)
288 saa_free(sects[i]->rel);
289 while (sects[i]->head) {
290 r = sects[i]->head;
291 sects[i]->head = sects[i]->head->next;
292 nasm_free(r);
295 nasm_free(sects);
296 saa_free(syms);
297 raa_free(bsym);
298 saa_free(strs);
299 if (of_elf64.current_dfmt) {
300 of_elf64.current_dfmt->cleanup();
304 /* add entry to the elf .shstrtab section */
305 static void add_sectname(char *firsthalf, char *secondhalf)
307 int len = strlen(firsthalf) + strlen(secondhalf);
308 while (shstrtablen + len + 1 > shstrtabsize)
309 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
310 strcpy(shstrtab + shstrtablen, firsthalf);
311 strcat(shstrtab + shstrtablen, secondhalf);
312 shstrtablen += len + 1;
315 static int elf_make_section(char *name, int type, int flags, int align)
317 struct Section *s;
319 s = nasm_malloc(sizeof(*s));
321 if (type != SHT_NOBITS)
322 s->data = saa_init(1L);
323 s->head = NULL;
324 s->tail = &s->head;
325 s->len = s->size = 0;
326 s->nrelocs = 0;
327 if (!strcmp(name, ".text"))
328 s->index = def_seg;
329 else
330 s->index = seg_alloc();
331 add_sectname("", name);
332 s->name = nasm_malloc(1 + strlen(name));
333 strcpy(s->name, name);
334 s->type = type;
335 s->flags = flags;
336 s->align = align;
337 s->gsyms = NULL;
339 if (nsects >= sectlen)
340 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
341 sects[nsects++] = s;
343 return nsects - 1;
346 static int32_t elf_section_names(char *name, int pass, int *bits)
348 char *p;
349 uint32_t flags, flags_and, flags_or;
350 uint64_t align;
351 int type, i;
354 * Default is 64 bits.
356 if (!name) {
357 *bits = 64;
358 return def_seg;
361 p = nasm_skip_word(name);
362 if (*p)
363 *p++ = '\0';
364 flags_and = flags_or = type = align = 0;
366 section_attrib(name, p, pass, &flags_and,
367 &flags_or, &align, &type);
369 if (!strcmp(name, ".shstrtab") ||
370 !strcmp(name, ".symtab") ||
371 !strcmp(name, ".strtab")) {
372 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
373 "name `%s'", name);
374 return NO_SEG;
377 for (i = 0; i < nsects; i++)
378 if (!strcmp(name, sects[i]->name))
379 break;
380 if (i == nsects) {
381 const struct elf_known_section *ks = elf_known_sections;
383 while (ks->name) {
384 if (!strcmp(name, ks->name))
385 break;
386 ks++;
389 type = type ? type : ks->type;
390 align = align ? align : ks->align;
391 flags = (ks->flags & ~flags_and) | flags_or;
393 i = elf_make_section(name, type, flags, align);
394 } else if (pass == 1) {
395 if ((type && sects[i]->type != type)
396 || (align && sects[i]->align != align)
397 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
398 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
399 " redeclaration of section `%s'", name);
402 return sects[i]->index;
405 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
406 int is_global, char *special)
408 int pos = strslen;
409 struct Symbol *sym;
410 bool special_used = false;
412 #if defined(DEBUG) && DEBUG>2
413 nasm_error(ERR_DEBUG,
414 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
415 name, segment, offset, is_global, special);
416 #endif
417 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
419 * This is a NASM special symbol. We never allow it into
420 * the ELF symbol table, even if it's a valid one. If it
421 * _isn't_ a valid one, we should barf immediately.
423 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
424 strcmp(name, "..got") && strcmp(name, "..plt") &&
425 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
426 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
427 return;
430 if (is_global == 3) {
431 struct Symbol **s;
433 * Fix up a forward-reference symbol size from the first
434 * pass.
436 for (s = &fwds; *s; s = &(*s)->nextfwd)
437 if (!strcmp((*s)->name, name)) {
438 struct tokenval tokval;
439 expr *e;
440 char *p = nasm_skip_spaces(nasm_skip_word(special));
442 stdscan_reset();
443 stdscan_set(p);
444 tokval.t_type = TOKEN_INVALID;
445 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
446 if (e) {
447 if (!is_simple(e))
448 nasm_error(ERR_NONFATAL, "cannot use relocatable"
449 " expression as symbol size");
450 else
451 (*s)->size = reloc_value(e);
455 * Remove it from the list of unresolved sizes.
457 nasm_free((*s)->name);
458 *s = (*s)->nextfwd;
459 return;
461 return; /* it wasn't an important one */
464 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
465 strslen += 1 + strlen(name);
467 lastsym = sym = saa_wstruct(syms);
469 memset(&sym->symv, 0, sizeof(struct rbtree));
471 sym->strpos = pos;
472 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
473 sym->other = STV_DEFAULT;
474 sym->size = 0;
475 if (segment == NO_SEG)
476 sym->section = SHN_ABS;
477 else {
478 int i;
479 sym->section = SHN_UNDEF;
480 if (segment == def_seg) {
481 /* we have to be sure at least text section is there */
482 int tempint;
483 if (segment != elf_section_names(".text", 2, &tempint))
484 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
486 for (i = 0; i < nsects; i++) {
487 if (segment == sects[i]->index) {
488 sym->section = i + 1;
489 break;
494 if (is_global == 2) {
495 sym->size = offset;
496 sym->symv.key = 0;
497 sym->section = SHN_COMMON;
499 * We have a common variable. Check the special text to see
500 * if it's a valid number and power of two; if so, store it
501 * as the alignment for the common variable.
503 if (special) {
504 bool err;
505 sym->symv.key = readnum(special, &err);
506 if (err)
507 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
508 " valid number", special);
509 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
510 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
511 " power of two", special);
513 special_used = true;
514 } else
515 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
517 if (sym->type == SYM_GLOBAL) {
519 * If sym->section == SHN_ABS, then the first line of the
520 * else section would cause a core dump, because its a reference
521 * beyond the end of the section array.
522 * This behaviour is exhibited by this code:
523 * GLOBAL crash_nasm
524 * crash_nasm equ 0
525 * To avoid such a crash, such requests are silently discarded.
526 * This may not be the best solution.
528 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
529 bsym = raa_write(bsym, segment, nglobs);
530 } else if (sym->section != SHN_ABS) {
532 * This is a global symbol; so we must add it to the rbtree
533 * of global symbols in its section.
535 * In addition, we check the special text for symbol
536 * type and size information.
538 sects[sym->section-1]->gsyms =
539 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
541 if (special) {
542 int n = strcspn(special, " \t");
544 if (!nasm_strnicmp(special, "function", n))
545 sym->type |= STT_FUNC;
546 else if (!nasm_strnicmp(special, "data", n) ||
547 !nasm_strnicmp(special, "object", n))
548 sym->type |= STT_OBJECT;
549 else if (!nasm_strnicmp(special, "notype", n))
550 sym->type |= STT_NOTYPE;
551 else
552 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
553 n, special);
554 special += n;
556 special = nasm_skip_spaces(special);
557 if (*special) {
558 n = strcspn(special, " \t");
559 if (!nasm_strnicmp(special, "default", n))
560 sym->other = STV_DEFAULT;
561 else if (!nasm_strnicmp(special, "internal", n))
562 sym->other = STV_INTERNAL;
563 else if (!nasm_strnicmp(special, "hidden", n))
564 sym->other = STV_HIDDEN;
565 else if (!nasm_strnicmp(special, "protected", n))
566 sym->other = STV_PROTECTED;
567 else
568 n = 0;
569 special += n;
572 if (*special) {
573 struct tokenval tokval;
574 expr *e;
575 int fwd = 0;
576 char *saveme = stdscan_get();
578 while (special[n] && nasm_isspace(special[n]))
579 n++;
581 * We have a size expression; attempt to
582 * evaluate it.
584 stdscan_reset();
585 stdscan_set(special + n);
586 tokval.t_type = TOKEN_INVALID;
587 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
588 NULL);
589 if (fwd) {
590 sym->nextfwd = fwds;
591 fwds = sym;
592 sym->name = nasm_strdup(name);
593 } else if (e) {
594 if (!is_simple(e))
595 nasm_error(ERR_NONFATAL, "cannot use relocatable"
596 " expression as symbol size");
597 else
598 sym->size = reloc_value(e);
600 stdscan_set(saveme);
602 special_used = true;
605 * If TLS segment, mark symbol accordingly.
607 if (sects[sym->section - 1]->flags & SHF_TLS) {
608 sym->type &= 0xf0;
609 sym->type |= STT_TLS;
612 sym->globnum = nglobs;
613 nglobs++;
614 } else
615 nlocals++;
617 if (special && !special_used)
618 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
621 static void elf_add_reloc(struct Section *sect, int32_t segment,
622 int64_t offset, int type)
624 struct Reloc *r;
625 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
626 sect->tail = &r->next;
627 r->next = NULL;
629 r->address = sect->len;
630 r->offset = offset;
631 if (segment == NO_SEG)
632 r->symbol = 0;
633 else {
634 int i;
635 r->symbol = 0;
636 for (i = 0; i < nsects; i++)
637 if (segment == sects[i]->index)
638 r->symbol = i + 2;
639 if (!r->symbol)
640 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
642 r->type = type;
644 sect->nrelocs++;
648 * This routine deals with ..got and ..sym relocations: the more
649 * complicated kinds. In shared-library writing, some relocations
650 * with respect to global symbols must refer to the precise symbol
651 * rather than referring to an offset from the base of the section
652 * _containing_ the symbol. Such relocations call to this routine,
653 * which searches the symbol list for the symbol in question.
655 * R_386_GOT32 references require the _exact_ symbol address to be
656 * used; R_386_32 references can be at an offset from the symbol.
657 * The boolean argument `exact' tells us this.
659 * Return value is the adjusted value of `addr', having become an
660 * offset from the symbol rather than the section. Should always be
661 * zero when returning from an exact call.
663 * Limitation: if you define two symbols at the same place,
664 * confusion will occur.
666 * Inefficiency: we search, currently, using a linked list which
667 * isn't even necessarily sorted.
669 static void elf_add_gsym_reloc(struct Section *sect,
670 int32_t segment, uint64_t offset, int64_t pcrel,
671 int type, bool exact)
673 struct Reloc *r;
674 struct Section *s;
675 struct Symbol *sym;
676 struct rbtree *srb;
677 int i;
680 * First look up the segment/offset pair and find a global
681 * symbol corresponding to it. If it's not one of our segments,
682 * then it must be an external symbol, in which case we're fine
683 * doing a normal elf_add_reloc after first sanity-checking
684 * that the offset from the symbol is zero.
686 s = NULL;
687 for (i = 0; i < nsects; i++)
688 if (segment == sects[i]->index) {
689 s = sects[i];
690 break;
693 if (!s) {
694 if (exact && offset)
695 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
696 else
697 elf_add_reloc(sect, segment, offset - pcrel, type);
698 return;
701 srb = rb_search(s->gsyms, offset);
702 if (!srb || (exact && srb->key != offset)) {
703 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
704 " for this reference");
705 return;
707 sym = container_of(srb, struct Symbol, symv);
709 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
710 sect->tail = &r->next;
711 r->next = NULL;
713 r->address = sect->len;
714 r->offset = offset - pcrel - sym->symv.key;
715 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
716 r->type = type;
718 sect->nrelocs++;
721 static void elf_out(int32_t segto, const void *data,
722 enum out_type type, uint64_t size,
723 int32_t segment, int32_t wrt)
725 struct Section *s;
726 int64_t addr, zero;
727 int i;
728 static struct symlininfo sinfo;
730 zero = 0;
732 #if defined(DEBUG) && DEBUG>2
733 if (data)
734 nasm_error(ERR_DEBUG,
735 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
736 currentline, type, segment, segto, size, *(int64_t *)data);
737 else
738 nasm_error(ERR_DEBUG,
739 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
740 currentline, type, segment, segto, size);
741 #endif
744 * handle absolute-assembly (structure definitions)
746 if (segto == NO_SEG) {
747 if (type != OUT_RESERVE)
748 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
749 " space");
750 return;
753 s = NULL;
754 for (i = 0; i < nsects; i++)
755 if (segto == sects[i]->index) {
756 s = sects[i];
757 break;
759 if (!s) {
760 int tempint; /* ignored */
761 if (segto != elf_section_names(".text", 2, &tempint))
762 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
763 else {
764 s = sects[nsects - 1];
765 i = nsects - 1;
769 /* again some stabs debugging stuff */
770 if (of_elf64.current_dfmt) {
771 sinfo.offset = s->len;
772 sinfo.section = i;
773 sinfo.segto = segto;
774 sinfo.name = s->name;
775 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
777 /* end of debugging stuff */
779 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
780 nasm_error(ERR_WARNING, "attempt to initialize memory in"
781 " BSS section `%s': ignored", s->name);
782 s->len += realsize(type, size);
783 return;
786 if (type == OUT_RESERVE) {
787 if (s->type == SHT_PROGBITS) {
788 nasm_error(ERR_WARNING, "uninitialized space declared in"
789 " non-BSS section `%s': zeroing", s->name);
790 elf_sect_write(s, NULL, size);
791 } else
792 s->len += size;
793 } else if (type == OUT_RAWDATA) {
794 if (segment != NO_SEG)
795 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
796 elf_sect_write(s, data, size);
797 } else if (type == OUT_ADDRESS) {
798 addr = *(int64_t *)data;
799 if (segment == NO_SEG) {
800 /* Do nothing */
801 } else if (segment % 2) {
802 nasm_error(ERR_NONFATAL, "ELF format does not support"
803 " segment base references");
804 } else {
805 if (wrt == NO_SEG) {
806 switch ((int)size) {
807 case 1:
808 elf_add_reloc(s, segment, addr, R_X86_64_8);
809 break;
810 case 2:
811 elf_add_reloc(s, segment, addr, R_X86_64_16);
812 break;
813 case 4:
814 elf_add_reloc(s, segment, addr, R_X86_64_32);
815 break;
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 (size != 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 ((int)size) {
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 ((int)size) {
859 case 1:
860 elf_add_gsym_reloc(s, segment, addr, 0,
861 R_X86_64_8, false);
862 addr = 0;
863 break;
864 case 2:
865 elf_add_gsym_reloc(s, segment, addr, 0,
866 R_X86_64_16, false);
867 addr = 0;
868 break;
869 case 4:
870 elf_add_gsym_reloc(s, segment, addr, 0,
871 R_X86_64_32, false);
872 addr = 0;
873 break;
874 case 8:
875 elf_add_gsym_reloc(s, segment, addr, 0,
876 R_X86_64_64, false);
877 addr = 0;
878 break;
879 default:
880 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
881 break;
883 } else if (wrt == elf_plt_sect + 1) {
884 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
885 "relative PLT references");
886 } else {
887 nasm_error(ERR_NONFATAL, "ELF format does not support this"
888 " use of WRT");
891 elf_sect_writeaddr(s, addr, size);
892 } else if (type == OUT_REL2ADR) {
893 addr = *(int64_t *)data - size;
894 if (segment == segto)
895 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
896 if (segment == NO_SEG) {
897 /* Do nothing */
898 } else if (segment % 2) {
899 nasm_error(ERR_NONFATAL, "ELF format does not support"
900 " segment base references");
901 } else {
902 if (wrt == NO_SEG) {
903 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
904 addr = 0;
905 } else {
906 nasm_error(ERR_NONFATAL,
907 "Unsupported non-32-bit ELF relocation [2]");
910 elf_sect_writeaddr(s, addr, 2);
911 } else if (type == OUT_REL4ADR) {
912 addr = *(int64_t *)data - size;
913 if (segment == segto)
914 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
915 if (segment == NO_SEG) {
916 /* Do nothing */
917 } else if (segment % 2) {
918 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
919 " segment base references");
920 } else {
921 if (wrt == NO_SEG) {
922 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
923 addr = 0;
924 } else if (wrt == elf_plt_sect + 1) {
925 elf_add_gsym_reloc(s, segment, addr+size, size,
926 R_X86_64_PLT32, true);
927 addr = 0;
928 } else if (wrt == elf_gotpc_sect + 1 ||
929 wrt == elf_got_sect + 1) {
930 elf_add_gsym_reloc(s, segment, addr+size, size,
931 R_X86_64_GOTPCREL, true);
932 addr = 0;
933 } else if (wrt == elf_gotoff_sect + 1 ||
934 wrt == elf_got_sect + 1) {
935 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
936 "qword absolute");
937 } else if (wrt == elf_gottpoff_sect + 1) {
938 elf_add_gsym_reloc(s, segment, addr+size, size,
939 R_X86_64_GOTTPOFF, true);
940 addr = 0;
941 } else {
942 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
943 " use of WRT");
946 elf_sect_writeaddr(s, addr, 4);
947 } else if (type == OUT_REL8ADR) {
948 addr = *(int64_t *)data - size;
949 if (segment == segto)
950 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
951 if (segment == NO_SEG) {
952 /* Do nothing */
953 } else if (segment % 2) {
954 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
955 " segment base references");
956 } else {
957 if (wrt == NO_SEG) {
958 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
959 addr = 0;
960 } else if (wrt == elf_gotpc_sect + 1 ||
961 wrt == elf_got_sect + 1) {
962 elf_add_gsym_reloc(s, segment, addr+size, size,
963 R_X86_64_GOTPCREL64, true);
964 addr = 0;
965 } else if (wrt == elf_gotoff_sect + 1 ||
966 wrt == elf_got_sect + 1) {
967 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
968 "absolute");
969 } else if (wrt == elf_gottpoff_sect + 1) {
970 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
971 "dword");
972 } else {
973 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
974 " use of WRT");
977 elf_sect_writeaddr(s, addr, 8);
981 static void elf_write(void)
983 int align;
984 char *p;
985 int i;
987 struct SAA *symtab;
988 int32_t symtablen, symtablocal;
991 * Work out how many sections we will have. We have SHN_UNDEF,
992 * then the flexible user sections, then the fixed sections
993 * `.shstrtab', `.symtab' and `.strtab', then optionally
994 * relocation sections for the user sections.
996 nsections = sec_numspecial + 1;
997 if (of_elf64.current_dfmt == &df_stabs)
998 nsections += 3;
999 else if (of_elf64.current_dfmt == &df_dwarf)
1000 nsections += 10;
1002 add_sectname("", ".shstrtab");
1003 add_sectname("", ".symtab");
1004 add_sectname("", ".strtab");
1005 for (i = 0; i < nsects; i++) {
1006 nsections++; /* for the section itself */
1007 if (sects[i]->head) {
1008 nsections++; /* for its relocations */
1009 add_sectname(".rela", sects[i]->name);
1013 if (of_elf64.current_dfmt == &df_stabs) {
1014 /* in case the debug information is wanted, just add these three sections... */
1015 add_sectname("", ".stab");
1016 add_sectname("", ".stabstr");
1017 add_sectname(".rel", ".stab");
1020 else if (of_elf64.current_dfmt == &df_dwarf) {
1021 /* the dwarf debug standard specifies the following ten sections,
1022 not all of which are currently implemented,
1023 although all of them are defined. */
1024 #define debug_aranges (int64_t) (nsections-10)
1025 #define debug_info (int64_t) (nsections-7)
1026 #define debug_abbrev (int64_t) (nsections-5)
1027 #define debug_line (int64_t) (nsections-4)
1028 add_sectname("", ".debug_aranges");
1029 add_sectname(".rela", ".debug_aranges");
1030 add_sectname("", ".debug_pubnames");
1031 add_sectname("", ".debug_info");
1032 add_sectname(".rela", ".debug_info");
1033 add_sectname("", ".debug_abbrev");
1034 add_sectname("", ".debug_line");
1035 add_sectname(".rela", ".debug_line");
1036 add_sectname("", ".debug_frame");
1037 add_sectname("", ".debug_loc");
1041 * Output the ELF header.
1043 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1044 fputc(elf_osabi, ofile);
1045 fputc(elf_abiver, ofile);
1046 fwritezero(7, ofile);
1047 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1048 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1049 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1050 fwriteint64_t(0L, ofile); /* no entry point */
1051 fwriteint64_t(0L, ofile); /* no program header table */
1052 fwriteint64_t(0x40L, ofile); /* section headers straight after
1053 * ELF header plus alignment */
1054 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1055 fwriteint16_t(0x40, ofile); /* size of ELF header */
1056 fwriteint16_t(0, ofile); /* no program header table, again */
1057 fwriteint16_t(0, ofile); /* still no program header table */
1058 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1059 fwriteint16_t(nsections, ofile); /* number of sections */
1060 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1061 * section header table */
1064 * Build the symbol table and relocation tables.
1066 symtab = elf_build_symtab(&symtablen, &symtablocal);
1067 for (i = 0; i < nsects; i++)
1068 if (sects[i]->head)
1069 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1070 sects[i]->head);
1073 * Now output the section header table.
1076 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1077 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1078 elf_foffs += align;
1079 elf_nsect = 0;
1080 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1082 /* SHN_UNDEF */
1083 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1084 p = shstrtab + 1;
1086 /* The normal sections */
1087 for (i = 0; i < nsects; i++) {
1088 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1089 (sects[i]->type == SHT_PROGBITS ?
1090 sects[i]->data : NULL), true,
1091 sects[i]->len, 0, 0, sects[i]->align, 0);
1092 p += strlen(p) + 1;
1095 /* .shstrtab */
1096 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1097 shstrtablen, 0, 0, 1, 0);
1098 p += strlen(p) + 1;
1100 /* .symtab */
1101 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1102 symtablen, sec_strtab, symtablocal, 4, 24);
1103 p += strlen(p) + 1;
1105 /* .strtab */
1106 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1107 strslen, 0, 0, 1, 0);
1108 p += strlen(p) + 1;
1110 /* The relocation sections */
1111 for (i = 0; i < nsects; i++)
1112 if (sects[i]->head) {
1113 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1114 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1115 p += strlen(p) + 1;
1118 if (of_elf64.current_dfmt == &df_stabs) {
1119 /* for debugging information, create the last three sections
1120 which are the .stab , .stabstr and .rel.stab sections respectively */
1122 /* this function call creates the stab sections in memory */
1123 stabs64_generate();
1125 if (stabbuf && stabstrbuf && stabrelbuf) {
1126 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1127 stablen, sec_stabstr, 0, 4, 12);
1128 p += strlen(p) + 1;
1130 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1131 stabstrlen, 0, 0, 4, 0);
1132 p += strlen(p) + 1;
1134 /* link -> symtable info -> section to refer to */
1135 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1136 stabrellen, symtabsection, sec_stab, 4, 16);
1137 p += strlen(p) + 1;
1139 } else if (of_elf64.current_dfmt == &df_dwarf) {
1140 /* for dwarf debugging information, create the ten dwarf sections */
1142 /* this function call creates the dwarf sections in memory */
1143 if (dwarf_fsect)
1144 dwarf64_generate();
1146 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1147 arangeslen, 0, 0, 1, 0);
1148 p += strlen(p) + 1;
1150 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1151 arangesrellen, symtabsection, debug_aranges, 1, 24);
1152 p += strlen(p) + 1;
1154 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1155 pubnameslen, 0, 0, 1, 0);
1156 p += strlen(p) + 1;
1158 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1159 infolen, 0, 0, 1, 0);
1160 p += strlen(p) + 1;
1162 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1163 inforellen, symtabsection, debug_info, 1, 24);
1164 p += strlen(p) + 1;
1166 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1167 abbrevlen, 0, 0, 1, 0);
1168 p += strlen(p) + 1;
1170 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1171 linelen, 0, 0, 1, 0);
1172 p += strlen(p) + 1;
1174 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1175 linerellen, symtabsection, debug_line, 1, 24);
1176 p += strlen(p) + 1;
1178 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1179 framelen, 0, 0, 8, 0);
1180 p += strlen(p) + 1;
1182 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1183 loclen, 0, 0, 1, 0);
1184 p += strlen(p) + 1;
1186 fwritezero(align, ofile);
1189 * Now output the sections.
1191 elf_write_sections();
1193 nasm_free(elf_sects);
1194 saa_free(symtab);
1197 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1199 struct SAA *s = saa_init(1L);
1200 struct Symbol *sym;
1201 uint8_t entry[24], *p;
1202 int i;
1204 *len = *local = 0;
1207 * First, an all-zeros entry, required by the ELF spec.
1209 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1210 *len += 24;
1211 (*local)++;
1214 * Next, an entry for the file name.
1216 p = entry;
1217 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1218 WRITESHORT(p, STT_FILE); /* type FILE */
1219 WRITESHORT(p, SHN_ABS);
1220 WRITEDLONG(p, (uint64_t) 0); /* no value */
1221 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1222 saa_wbytes(s, entry, 24L);
1223 *len += 24;
1224 (*local)++;
1227 * Now some standard symbols defining the segments, for relocation
1228 * purposes.
1230 for (i = 1; i <= nsects; i++) {
1231 p = entry;
1232 WRITELONG(p, 0); /* no symbol name */
1233 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1234 WRITESHORT(p, i); /* section id */
1235 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1236 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1237 saa_wbytes(s, entry, 24L);
1238 *len += 24;
1239 (*local)++;
1244 * Now the other local symbols.
1246 saa_rewind(syms);
1247 while ((sym = saa_rstruct(syms))) {
1248 if (sym->type & SYM_GLOBAL)
1249 continue;
1250 p = entry;
1251 WRITELONG(p, sym->strpos); /* index into symbol string table */
1252 WRITECHAR(p, sym->type); /* type and binding */
1253 WRITECHAR(p, sym->other); /* visibility */
1254 WRITESHORT(p, sym->section); /* index into section header table */
1255 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1256 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1257 saa_wbytes(s, entry, 24L);
1258 *len += 24;
1259 (*local)++;
1262 * dwarf needs symbols for debug sections
1263 * which are relocation targets.
1265 if (of_elf64.current_dfmt == &df_dwarf) {
1266 dwarf_infosym = *local;
1267 p = entry;
1268 WRITELONG(p, 0); /* no symbol name */
1269 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1270 WRITESHORT(p, debug_info); /* section id */
1271 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1272 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1273 saa_wbytes(s, entry, 24L);
1274 *len += 24;
1275 (*local)++;
1276 dwarf_abbrevsym = *local;
1277 p = entry;
1278 WRITELONG(p, 0); /* no symbol name */
1279 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1280 WRITESHORT(p, debug_abbrev); /* section id */
1281 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1282 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1283 saa_wbytes(s, entry, 24L);
1284 *len += 24;
1285 (*local)++;
1286 dwarf_linesym = *local;
1287 p = entry;
1288 WRITELONG(p, 0); /* no symbol name */
1289 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1290 WRITESHORT(p, debug_line); /* section id */
1291 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1292 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1293 saa_wbytes(s, entry, 24L);
1294 *len += 24;
1295 (*local)++;
1299 * Now the global symbols.
1301 saa_rewind(syms);
1302 while ((sym = saa_rstruct(syms))) {
1303 if (!(sym->type & SYM_GLOBAL))
1304 continue;
1305 p = entry;
1306 WRITELONG(p, sym->strpos);
1307 WRITECHAR(p, sym->type); /* type and binding */
1308 WRITECHAR(p, sym->other); /* visibility */
1309 WRITESHORT(p, sym->section);
1310 WRITEDLONG(p, (int64_t)sym->symv.key);
1311 WRITEDLONG(p, (int64_t)sym->size);
1312 saa_wbytes(s, entry, 24L);
1313 *len += 24;
1316 return s;
1319 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1321 struct SAA *s;
1322 uint8_t *p, entry[24];
1323 int32_t global_offset;
1325 if (!r)
1326 return NULL;
1328 s = saa_init(1L);
1329 *len = 0;
1332 * How to onvert from a global placeholder to a real symbol index;
1333 * the +2 refers to the two special entries, the null entry and
1334 * the filename entry.
1336 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1338 while (r) {
1339 int32_t sym = r->symbol;
1341 if (sym >= GLOBAL_TEMP_BASE)
1342 sym += global_offset;
1344 p = entry;
1345 WRITEDLONG(p, r->address);
1346 WRITELONG(p, r->type);
1347 WRITELONG(p, sym);
1348 WRITEDLONG(p, r->offset);
1349 saa_wbytes(s, entry, 24L);
1350 *len += 24;
1352 r = r->next;
1355 return s;
1358 static void elf_section_header(int name, int type, uint64_t flags,
1359 void *data, bool is_saa, uint64_t datalen,
1360 int link, int info, int align, int eltsize)
1362 elf_sects[elf_nsect].data = data;
1363 elf_sects[elf_nsect].len = datalen;
1364 elf_sects[elf_nsect].is_saa = is_saa;
1365 elf_nsect++;
1367 fwriteint32_t((int32_t)name, ofile);
1368 fwriteint32_t((int32_t)type, ofile);
1369 fwriteint64_t((int64_t)flags, ofile);
1370 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1371 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1372 fwriteint64_t(datalen, ofile);
1373 if (data)
1374 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1375 fwriteint32_t((int32_t)link, ofile);
1376 fwriteint32_t((int32_t)info, ofile);
1377 fwriteint64_t((int64_t)align, ofile);
1378 fwriteint64_t((int64_t)eltsize, ofile);
1381 static void elf_write_sections(void)
1383 int i;
1384 for (i = 0; i < elf_nsect; i++)
1385 if (elf_sects[i].data) {
1386 int32_t len = elf_sects[i].len;
1387 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1388 int32_t align = reallen - len;
1389 if (elf_sects[i].is_saa)
1390 saa_fpwrite(elf_sects[i].data, ofile);
1391 else
1392 fwrite(elf_sects[i].data, len, 1, ofile);
1393 fwritezero(align, ofile);
1397 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1399 saa_wbytes(sect->data, data, len);
1400 sect->len += len;
1402 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1404 saa_writeaddr(sect->data, data, len);
1405 sect->len += len;
1408 static int32_t elf_segbase(int32_t segment)
1410 return segment;
1413 static int elf_directive(enum directives directive, char *value, int pass)
1415 bool err;
1416 int64_t n;
1417 char *p;
1419 switch (directive) {
1420 case D_OSABI:
1421 if (pass == 2)
1422 return 1; /* ignore in pass 2 */
1424 n = readnum(value, &err);
1425 if (err) {
1426 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1427 return 1;
1429 if (n < 0 || n > 255) {
1430 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1431 return 1;
1433 elf_osabi = n;
1434 elf_abiver = 0;
1436 if ((p = strchr(value,',')) == NULL)
1437 return 1;
1439 n = readnum(p+1, &err);
1440 if (err || n < 0 || n > 255) {
1441 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1442 return 1;
1445 elf_abiver = n;
1446 return 1;
1448 default:
1449 return 0;
1453 static void elf_filename(char *inname, char *outname)
1455 strcpy(elf_module, inname);
1456 standard_extension(inname, outname, ".o");
1459 extern macros_t elf_stdmac[];
1461 static int elf_set_info(enum geninfo type, char **val)
1463 (void)type;
1464 (void)val;
1465 return 0;
1467 static struct dfmt df_dwarf = {
1468 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1469 "dwarf",
1470 dwarf64_init,
1471 dwarf64_linenum,
1472 debug64_deflabel,
1473 debug64_directive,
1474 debug64_typevalue,
1475 dwarf64_output,
1476 dwarf64_cleanup
1478 static struct dfmt df_stabs = {
1479 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1480 "stabs",
1481 null_debug_init,
1482 stabs64_linenum,
1483 debug64_deflabel,
1484 debug64_directive,
1485 debug64_typevalue,
1486 stabs64_output,
1487 stabs64_cleanup
1490 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1492 struct ofmt of_elf64 = {
1493 "ELF64 (x86_64) object files (e.g. Linux)",
1494 "elf64",
1496 elf64_debugs_arr,
1497 &df_stabs,
1498 elf_stdmac,
1499 elf_init,
1500 elf_set_info,
1501 elf_out,
1502 elf_deflabel,
1503 elf_section_names,
1504 elf_segbase,
1505 elf_directive,
1506 elf_filename,
1507 elf_cleanup
1510 /* common debugging routines */
1511 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1512 int is_global, char *special)
1514 (void)name;
1515 (void)segment;
1516 (void)offset;
1517 (void)is_global;
1518 (void)special;
1521 static void debug64_directive(const char *directive, const char *params)
1523 (void)directive;
1524 (void)params;
1527 static void debug64_typevalue(int32_t type)
1529 int32_t stype, ssize;
1530 switch (TYM_TYPE(type)) {
1531 case TY_LABEL:
1532 ssize = 0;
1533 stype = STT_NOTYPE;
1534 break;
1535 case TY_BYTE:
1536 ssize = 1;
1537 stype = STT_OBJECT;
1538 break;
1539 case TY_WORD:
1540 ssize = 2;
1541 stype = STT_OBJECT;
1542 break;
1543 case TY_DWORD:
1544 ssize = 4;
1545 stype = STT_OBJECT;
1546 break;
1547 case TY_FLOAT:
1548 ssize = 4;
1549 stype = STT_OBJECT;
1550 break;
1551 case TY_QWORD:
1552 ssize = 8;
1553 stype = STT_OBJECT;
1554 break;
1555 case TY_TBYTE:
1556 ssize = 10;
1557 stype = STT_OBJECT;
1558 break;
1559 case TY_OWORD:
1560 ssize = 16;
1561 stype = STT_OBJECT;
1562 break;
1563 case TY_YWORD:
1564 ssize = 32;
1565 stype = STT_OBJECT;
1566 break;
1567 case TY_COMMON:
1568 ssize = 0;
1569 stype = STT_COMMON;
1570 break;
1571 case TY_SEG:
1572 ssize = 0;
1573 stype = STT_SECTION;
1574 break;
1575 case TY_EXTERN:
1576 ssize = 0;
1577 stype = STT_NOTYPE;
1578 break;
1579 case TY_EQU:
1580 ssize = 0;
1581 stype = STT_NOTYPE;
1582 break;
1583 default:
1584 ssize = 0;
1585 stype = STT_NOTYPE;
1586 break;
1588 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1589 lastsym->size = ssize;
1590 lastsym->type = stype;
1594 /* stabs debugging routines */
1596 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1598 (void)segto;
1599 if (!stabs_filename) {
1600 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1601 strcpy(stabs_filename, filename);
1602 } else {
1603 if (strcmp(stabs_filename, filename)) {
1604 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1605 in fact, this leak comes in quite handy to maintain a list of files
1606 encountered so far in the symbol lines... */
1608 /* why not nasm_free(stabs_filename); we're done with the old one */
1610 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1611 strcpy(stabs_filename, filename);
1614 debug_immcall = 1;
1615 currentline = linenumber;
1619 static void stabs64_output(int type, void *param)
1621 struct symlininfo *s;
1622 struct linelist *el;
1623 if (type == TY_DEBUGSYMLIN) {
1624 if (debug_immcall) {
1625 s = (struct symlininfo *)param;
1626 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1627 return; /* line info is only collected for executable sections */
1628 numlinestabs++;
1629 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1630 el->info.offset = s->offset;
1631 el->info.section = s->section;
1632 el->info.name = s->name;
1633 el->line = currentline;
1634 el->filename = stabs_filename;
1635 el->next = 0;
1636 if (stabslines) {
1637 stabslines->last->next = el;
1638 stabslines->last = el;
1639 } else {
1640 stabslines = el;
1641 stabslines->last = el;
1645 debug_immcall = 0;
1648 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1650 static void stabs64_generate(void)
1652 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1653 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1654 char **allfiles;
1655 int *fileidx;
1657 struct linelist *ptr;
1659 ptr = stabslines;
1661 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1662 for (i = 0; i < numlinestabs; i++)
1663 allfiles[i] = 0;
1664 numfiles = 0;
1665 while (ptr) {
1666 if (numfiles == 0) {
1667 allfiles[0] = ptr->filename;
1668 numfiles++;
1669 } else {
1670 for (i = 0; i < numfiles; i++) {
1671 if (!strcmp(allfiles[i], ptr->filename))
1672 break;
1674 if (i >= numfiles) {
1675 allfiles[i] = ptr->filename;
1676 numfiles++;
1679 ptr = ptr->next;
1681 strsize = 1;
1682 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1683 for (i = 0; i < numfiles; i++) {
1684 fileidx[i] = strsize;
1685 strsize += strlen(allfiles[i]) + 1;
1687 mainfileindex = 0;
1688 for (i = 0; i < numfiles; i++) {
1689 if (!strcmp(allfiles[i], elf_module)) {
1690 mainfileindex = i;
1691 break;
1696 * worst case size of the stab buffer would be:
1697 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1698 * plus one "ending" entry
1700 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1701 sizeof(struct stabentry));
1702 ssbuf = (uint8_t *)nasm_malloc(strsize);
1703 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1704 rptr = rbuf;
1706 for (i = 0; i < numfiles; i++)
1707 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1708 ssbuf[0] = 0;
1710 stabstrlen = strsize; /* set global variable for length of stab strings */
1712 sptr = sbuf;
1713 ptr = stabslines;
1714 numstabs = 0;
1716 if (ptr) {
1718 * this is the first stab, its strx points to the filename of the
1719 * the source-file, the n_desc field should be set to the number
1720 * of remaining stabs
1722 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1724 /* this is the stab for the main source file */
1725 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1727 /* relocation table entry */
1730 * Since the symbol table has two entries before
1731 * the section symbols, the index in the info.section
1732 * member must be adjusted by adding 2
1735 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1736 WRITELONG(rptr, R_X86_64_32);
1737 WRITELONG(rptr, ptr->info.section + 2);
1739 numstabs++;
1740 currfile = mainfileindex;
1743 while (ptr) {
1744 if (strcmp(allfiles[currfile], ptr->filename)) {
1745 /* oops file has changed... */
1746 for (i = 0; i < numfiles; i++)
1747 if (!strcmp(allfiles[i], ptr->filename))
1748 break;
1749 currfile = i;
1750 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1751 ptr->info.offset);
1752 numstabs++;
1754 /* relocation table entry */
1756 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1757 WRITELONG(rptr, R_X86_64_32);
1758 WRITELONG(rptr, ptr->info.section + 2);
1761 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1762 numstabs++;
1764 /* relocation table entry */
1766 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1767 WRITELONG(rptr, R_X86_64_32);
1768 WRITELONG(rptr, ptr->info.section + 2);
1770 ptr = ptr->next;
1774 /* this is an "ending" token */
1775 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1776 numstabs++;
1778 ((struct stabentry *)sbuf)->n_desc = numstabs;
1780 nasm_free(allfiles);
1781 nasm_free(fileidx);
1783 stablen = (sptr - sbuf);
1784 stabrellen = (rptr - rbuf);
1785 stabrelbuf = rbuf;
1786 stabbuf = sbuf;
1787 stabstrbuf = ssbuf;
1790 static void stabs64_cleanup(void)
1792 struct linelist *ptr, *del;
1793 if (!stabslines)
1794 return;
1796 ptr = stabslines;
1797 while (ptr) {
1798 del = ptr;
1799 ptr = ptr->next;
1800 nasm_free(del);
1803 nasm_free(stabbuf);
1804 nasm_free(stabrelbuf);
1805 nasm_free(stabstrbuf);
1808 /* dwarf routines */
1810 static void dwarf64_init(void)
1812 ndebugs = 3; /* 3 debug symbols */
1815 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1816 int32_t segto)
1818 (void)segto;
1819 dwarf64_findfile(filename);
1820 debug_immcall = 1;
1821 currentline = linenumber;
1824 /* called from elf_out with type == TY_DEBUGSYMLIN */
1825 static void dwarf64_output(int type, void *param)
1827 int ln, aa, inx, maxln, soc;
1828 struct symlininfo *s;
1829 struct SAA *plinep;
1831 (void)type;
1833 s = (struct symlininfo *)param;
1835 /* line number info is only gathered for executable sections */
1836 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1837 return;
1839 /* Check if section index has changed */
1840 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1841 dwarf64_findsect(s->section);
1843 /* do nothing unless line or file has changed */
1844 if (!debug_immcall)
1845 return;
1847 ln = currentline - dwarf_csect->line;
1848 aa = s->offset - dwarf_csect->offset;
1849 inx = dwarf_clist->line;
1850 plinep = dwarf_csect->psaa;
1851 /* check for file change */
1852 if (!(inx == dwarf_csect->file)) {
1853 saa_write8(plinep,DW_LNS_set_file);
1854 saa_write8(plinep,inx);
1855 dwarf_csect->file = inx;
1857 /* check for line change */
1858 if (ln) {
1859 /* test if in range of special op code */
1860 maxln = line_base + line_range;
1861 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1862 if (ln >= line_base && ln < maxln && soc < 256) {
1863 saa_write8(plinep,soc);
1864 } else {
1865 saa_write8(plinep,DW_LNS_advance_line);
1866 saa_wleb128s(plinep,ln);
1867 if (aa) {
1868 saa_write8(plinep,DW_LNS_advance_pc);
1869 saa_wleb128u(plinep,aa);
1872 dwarf_csect->line = currentline;
1873 dwarf_csect->offset = s->offset;
1876 /* show change handled */
1877 debug_immcall = 0;
1881 static void dwarf64_generate(void)
1883 uint8_t *pbuf;
1884 int indx;
1885 struct linelist *ftentry;
1886 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1887 struct SAA *parangesrel, *plinesrel, *pinforel;
1888 struct sectlist *psect;
1889 size_t saalen, linepoff, totlen, highaddr;
1891 /* write epilogues for each line program range */
1892 /* and build aranges section */
1893 paranges = saa_init(1L);
1894 parangesrel = saa_init(1L);
1895 saa_write16(paranges,3); /* dwarf version */
1896 saa_write64(parangesrel, paranges->datalen+4);
1897 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1898 saa_write64(parangesrel, 0);
1899 saa_write32(paranges,0); /* offset into info */
1900 saa_write8(paranges,8); /* pointer size */
1901 saa_write8(paranges,0); /* not segmented */
1902 saa_write32(paranges,0); /* padding */
1903 /* iterate though sectlist entries */
1904 psect = dwarf_fsect;
1905 totlen = 0;
1906 highaddr = 0;
1907 for (indx = 0; indx < dwarf_nsections; indx++)
1909 plinep = psect->psaa;
1910 /* Line Number Program Epilogue */
1911 saa_write8(plinep,2); /* std op 2 */
1912 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1913 saa_write8(plinep,DW_LNS_extended_op);
1914 saa_write8(plinep,1); /* operand length */
1915 saa_write8(plinep,DW_LNE_end_sequence);
1916 totlen += plinep->datalen;
1917 /* range table relocation entry */
1918 saa_write64(parangesrel, paranges->datalen + 4);
1919 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1920 saa_write64(parangesrel, (uint64_t) 0);
1921 /* range table entry */
1922 saa_write64(paranges,0x0000); /* range start */
1923 saa_write64(paranges,sects[psect->section]->len); /* range length */
1924 highaddr += sects[psect->section]->len;
1925 /* done with this entry */
1926 psect = psect->next;
1928 saa_write64(paranges,0); /* null address */
1929 saa_write64(paranges,0); /* null length */
1930 saalen = paranges->datalen;
1931 arangeslen = saalen + 4;
1932 arangesbuf = pbuf = nasm_malloc(arangeslen);
1933 WRITELONG(pbuf,saalen); /* initial length */
1934 saa_rnbytes(paranges, pbuf, saalen);
1935 saa_free(paranges);
1937 /* build rela.aranges section */
1938 arangesrellen = saalen = parangesrel->datalen;
1939 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1940 saa_rnbytes(parangesrel, pbuf, saalen);
1941 saa_free(parangesrel);
1943 /* build pubnames section */
1944 ppubnames = saa_init(1L);
1945 saa_write16(ppubnames,3); /* dwarf version */
1946 saa_write32(ppubnames,0); /* offset into info */
1947 saa_write32(ppubnames,0); /* space used in info */
1948 saa_write32(ppubnames,0); /* end of list */
1949 saalen = ppubnames->datalen;
1950 pubnameslen = saalen + 4;
1951 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1952 WRITELONG(pbuf,saalen); /* initial length */
1953 saa_rnbytes(ppubnames, pbuf, saalen);
1954 saa_free(ppubnames);
1956 /* build info section */
1957 pinfo = saa_init(1L);
1958 pinforel = saa_init(1L);
1959 saa_write16(pinfo,3); /* dwarf version */
1960 saa_write64(pinforel, pinfo->datalen + 4);
1961 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
1962 saa_write64(pinforel, 0);
1963 saa_write32(pinfo,0); /* offset into abbrev */
1964 saa_write8(pinfo,8); /* pointer size */
1965 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1966 saa_write64(pinforel, pinfo->datalen + 4);
1967 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1968 saa_write64(pinforel, 0);
1969 saa_write64(pinfo,0); /* DW_AT_low_pc */
1970 saa_write64(pinforel, pinfo->datalen + 4);
1971 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1972 saa_write64(pinforel, 0);
1973 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
1974 saa_write64(pinforel, pinfo->datalen + 4);
1975 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
1976 saa_write64(pinforel, 0);
1977 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1978 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1979 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1980 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1981 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1982 saa_write64(pinforel, pinfo->datalen + 4);
1983 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1984 saa_write64(pinforel, 0);
1985 saa_write64(pinfo,0); /* DW_AT_low_pc */
1986 saa_write64(pinfo,0); /* DW_AT_frame_base */
1987 saa_write8(pinfo,0); /* end of entries */
1988 saalen = pinfo->datalen;
1989 infolen = saalen + 4;
1990 infobuf = pbuf = nasm_malloc(infolen);
1991 WRITELONG(pbuf,saalen); /* initial length */
1992 saa_rnbytes(pinfo, pbuf, saalen);
1993 saa_free(pinfo);
1995 /* build rela.info section */
1996 inforellen = saalen = pinforel->datalen;
1997 inforelbuf = pbuf = nasm_malloc(inforellen);
1998 saa_rnbytes(pinforel, pbuf, saalen);
1999 saa_free(pinforel);
2001 /* build abbrev section */
2002 pabbrev = saa_init(1L);
2003 saa_write8(pabbrev,1); /* entry number LEB128u */
2004 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2005 saa_write8(pabbrev,1); /* has children */
2006 /* the following attributes and forms are all LEB128u values */
2007 saa_write8(pabbrev,DW_AT_low_pc);
2008 saa_write8(pabbrev,DW_FORM_addr);
2009 saa_write8(pabbrev,DW_AT_high_pc);
2010 saa_write8(pabbrev,DW_FORM_addr);
2011 saa_write8(pabbrev,DW_AT_stmt_list);
2012 saa_write8(pabbrev,DW_FORM_data4);
2013 saa_write8(pabbrev,DW_AT_name);
2014 saa_write8(pabbrev,DW_FORM_string);
2015 saa_write8(pabbrev,DW_AT_producer);
2016 saa_write8(pabbrev,DW_FORM_string);
2017 saa_write8(pabbrev,DW_AT_language);
2018 saa_write8(pabbrev,DW_FORM_data2);
2019 saa_write16(pabbrev,0); /* end of entry */
2020 /* LEB128u usage same as above */
2021 saa_write8(pabbrev,2); /* entry number */
2022 saa_write8(pabbrev,DW_TAG_subprogram);
2023 saa_write8(pabbrev,0); /* no children */
2024 saa_write8(pabbrev,DW_AT_low_pc);
2025 saa_write8(pabbrev,DW_FORM_addr);
2026 saa_write8(pabbrev,DW_AT_frame_base);
2027 saa_write8(pabbrev,DW_FORM_data4);
2028 saa_write16(pabbrev,0); /* end of entry */
2029 abbrevlen = saalen = pabbrev->datalen;
2030 abbrevbuf = pbuf = nasm_malloc(saalen);
2031 saa_rnbytes(pabbrev, pbuf, saalen);
2032 saa_free(pabbrev);
2034 /* build line section */
2035 /* prolog */
2036 plines = saa_init(1L);
2037 saa_write8(plines,1); /* Minimum Instruction Length */
2038 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2039 saa_write8(plines,line_base); /* Line Base */
2040 saa_write8(plines,line_range); /* Line Range */
2041 saa_write8(plines,opcode_base); /* Opcode Base */
2042 /* standard opcode lengths (# of LEB128u operands) */
2043 saa_write8(plines,0); /* Std opcode 1 length */
2044 saa_write8(plines,1); /* Std opcode 2 length */
2045 saa_write8(plines,1); /* Std opcode 3 length */
2046 saa_write8(plines,1); /* Std opcode 4 length */
2047 saa_write8(plines,1); /* Std opcode 5 length */
2048 saa_write8(plines,0); /* Std opcode 6 length */
2049 saa_write8(plines,0); /* Std opcode 7 length */
2050 saa_write8(plines,0); /* Std opcode 8 length */
2051 saa_write8(plines,1); /* Std opcode 9 length */
2052 saa_write8(plines,0); /* Std opcode 10 length */
2053 saa_write8(plines,0); /* Std opcode 11 length */
2054 saa_write8(plines,1); /* Std opcode 12 length */
2055 /* Directory Table */
2056 saa_write8(plines,0); /* End of table */
2057 /* File Name Table */
2058 ftentry = dwarf_flist;
2059 for (indx = 0;indx<dwarf_numfiles;indx++)
2061 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2062 saa_write8(plines,0); /* directory LEB128u */
2063 saa_write8(plines,0); /* time LEB128u */
2064 saa_write8(plines,0); /* size LEB128u */
2065 ftentry = ftentry->next;
2067 saa_write8(plines,0); /* End of table */
2068 linepoff = plines->datalen;
2069 linelen = linepoff + totlen + 10;
2070 linebuf = pbuf = nasm_malloc(linelen);
2071 WRITELONG(pbuf,linelen-4); /* initial length */
2072 WRITESHORT(pbuf,3); /* dwarf version */
2073 WRITELONG(pbuf,linepoff); /* offset to line number program */
2074 /* write line header */
2075 saalen = linepoff;
2076 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2077 pbuf += linepoff;
2078 saa_free(plines);
2079 /* concatonate line program ranges */
2080 linepoff += 13;
2081 plinesrel = saa_init(1L);
2082 psect = dwarf_fsect;
2083 for (indx = 0; indx < dwarf_nsections; indx++) {
2084 saa_write64(plinesrel, linepoff);
2085 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2086 saa_write64(plinesrel, (uint64_t) 0);
2087 plinep = psect->psaa;
2088 saalen = plinep->datalen;
2089 saa_rnbytes(plinep, pbuf, saalen);
2090 pbuf += saalen;
2091 linepoff += saalen;
2092 saa_free(plinep);
2093 /* done with this entry */
2094 psect = psect->next;
2098 /* build rela.lines section */
2099 linerellen =saalen = plinesrel->datalen;
2100 linerelbuf = pbuf = nasm_malloc(linerellen);
2101 saa_rnbytes(plinesrel, pbuf, saalen);
2102 saa_free(plinesrel);
2104 /* build frame section */
2105 framelen = 4;
2106 framebuf = pbuf = nasm_malloc(framelen);
2107 WRITELONG(pbuf,framelen-4); /* initial length */
2109 /* build loc section */
2110 loclen = 16;
2111 locbuf = pbuf = nasm_malloc(loclen);
2112 WRITEDLONG(pbuf,0); /* null beginning offset */
2113 WRITEDLONG(pbuf,0); /* null ending offset */
2116 static void dwarf64_cleanup(void)
2118 nasm_free(arangesbuf);
2119 nasm_free(arangesrelbuf);
2120 nasm_free(pubnamesbuf);
2121 nasm_free(infobuf);
2122 nasm_free(inforelbuf);
2123 nasm_free(abbrevbuf);
2124 nasm_free(linebuf);
2125 nasm_free(linerelbuf);
2126 nasm_free(framebuf);
2127 nasm_free(locbuf);
2130 static void dwarf64_findfile(const char * fname)
2132 int finx;
2133 struct linelist *match;
2135 /* return if fname is current file name */
2136 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2137 return;
2139 /* search for match */
2140 match = 0;
2141 if (dwarf_flist) {
2142 match = dwarf_flist;
2143 for (finx = 0; finx < dwarf_numfiles; finx++) {
2144 if (!(strcmp(fname, match->filename))) {
2145 dwarf_clist = match;
2146 return;
2151 /* add file name to end of list */
2152 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2153 dwarf_numfiles++;
2154 dwarf_clist->line = dwarf_numfiles;
2155 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2156 strcpy(dwarf_clist->filename,fname);
2157 dwarf_clist->next = 0;
2158 if (!dwarf_flist) { /* if first entry */
2159 dwarf_flist = dwarf_elist = dwarf_clist;
2160 dwarf_clist->last = 0;
2161 } else { /* chain to previous entry */
2162 dwarf_elist->next = dwarf_clist;
2163 dwarf_elist = dwarf_clist;
2167 static void dwarf64_findsect(const int index)
2169 int sinx;
2170 struct sectlist *match;
2171 struct SAA *plinep;
2173 /* return if index is current section index */
2174 if (dwarf_csect && (dwarf_csect->section == index))
2175 return;
2177 /* search for match */
2178 match = 0;
2179 if (dwarf_fsect) {
2180 match = dwarf_fsect;
2181 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2182 if ((match->section == index)) {
2183 dwarf_csect = match;
2184 return;
2186 match = match->next;
2190 /* add entry to end of list */
2191 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2192 dwarf_nsections++;
2193 dwarf_csect->psaa = plinep = saa_init(1L);
2194 dwarf_csect->line = 1;
2195 dwarf_csect->offset = 0;
2196 dwarf_csect->file = 1;
2197 dwarf_csect->section = index;
2198 dwarf_csect->next = 0;
2199 /* set relocatable address at start of line program */
2200 saa_write8(plinep,DW_LNS_extended_op);
2201 saa_write8(plinep,9); /* operand length */
2202 saa_write8(plinep,DW_LNE_set_address);
2203 saa_write64(plinep,0); /* Start Address */
2205 if (!dwarf_fsect) { /* if first entry */
2206 dwarf_fsect = dwarf_esect = dwarf_csect;
2207 dwarf_csect->last = 0;
2208 } else { /* chain to previous entry */
2209 dwarf_esect->next = dwarf_csect;
2210 dwarf_esect = dwarf_csect;
2214 #endif /* OF_ELF */