directives.pl: trivial formatting fix
[nasm.git] / output / outelf64.c
blobbf5f80e874fb564cd2957f1ce0ed33516aa1f802
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
63 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
65 struct Reloc {
66 struct Reloc *next;
67 int64_t address; /* relative to _start_ of section */
68 int64_t symbol; /* symbol index */
69 int64_t offset; /* symbol addend */
70 int type; /* type of relocation */
73 struct Symbol {
74 struct rbtree symv; /* symbol value and rbtree of globals */
75 int32_t strpos; /* string table position of name */
76 int32_t section; /* section ID of the symbol */
77 int type; /* symbol type */
78 int other; /* symbol visibility */
79 int32_t size; /* size of symbol */
80 int32_t globnum; /* symbol table offset if global */
81 struct Symbol *nextfwd; /* list of unresolved-size symbols */
82 char *name; /* used temporarily if in above list */
85 struct Section {
86 struct SAA *data;
87 uint64_t len, size;
88 uint32_t nrelocs;
89 int32_t index; /* index into sects array */
90 int type; /* SHT_PROGBITS or SHT_NOBITS */
91 uint64_t align; /* alignment: power of two */
92 uint64_t flags; /* section flags */
93 char *name;
94 struct SAA *rel;
95 uint64_t rellen;
96 struct Reloc *head, **tail;
97 struct rbtree *gsyms; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section **sects;
102 static int nsects, sectlen;
104 #define SHSTR_DELTA 256
105 static char *shstrtab;
106 static int shstrtablen, shstrtabsize;
108 static struct SAA *syms;
109 static uint32_t nlocals, nglobs, ndebugs;
111 static int32_t def_seg;
113 static struct RAA *bsym;
115 static struct SAA *strs;
116 static uint32_t strslen;
118 static struct Symbol *fwds;
120 static char elf_module[FILENAME_MAX];
122 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
123 static uint8_t elf_abiver = 0; /* Current ABI version */
125 extern struct ofmt of_elf64;
127 static struct ELF_SECTDATA {
128 void *data;
129 int64_t len;
130 bool is_saa;
131 } *elf_sects;
132 static int elf_nsect, nsections;
133 static int64_t elf_foffs;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section *, const void *, size_t);
137 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
138 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
139 int, int);
140 static void elf_write_sections(void);
141 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
143 static void add_sectname(char *, char *);
145 struct stabentry {
146 uint32_t n_strx;
147 uint8_t n_type;
148 uint8_t n_other;
149 uint16_t n_desc;
150 uint32_t n_value;
153 struct erel {
154 int offset, info;
157 struct symlininfo {
158 int offset;
159 int section; /* index into sects[] */
160 int segto; /* internal section number */
161 char *name; /* shallow-copied pointer of section name */
164 struct linelist {
165 struct symlininfo info;
166 int line;
167 char *filename;
168 struct linelist *next;
169 struct linelist *last;
172 struct sectlist {
173 struct SAA *psaa;
174 int section;
175 int line;
176 int offset;
177 int file;
178 struct sectlist *next;
179 struct sectlist *last;
182 /* common debug variables */
183 static int currentline = 1;
184 static int debug_immcall = 0;
186 /* stabs debug variables */
187 static struct linelist *stabslines = 0;
188 static int numlinestabs = 0;
189 static char *stabs_filename = 0;
190 static int symtabsection;
191 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
192 static int stablen, stabstrlen, stabrellen;
194 /* dwarf debug variables */
195 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
196 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
197 static int dwarf_numfiles = 0, dwarf_nsections;
198 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
199 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
200 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
201 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
202 abbrevlen, linelen, linerellen, framelen, loclen;
203 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
206 static struct dfmt df_dwarf;
207 static struct dfmt df_stabs;
208 static struct Symbol *lastsym;
210 /* common debugging routines */
211 static void debug64_typevalue(int32_t);
212 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
213 static void debug64_directive(const char *, const char *);
215 /* stabs debugging routines */
216 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
217 static void stabs64_output(int, void *);
218 static void stabs64_generate(void);
219 static void stabs64_cleanup(void);
221 /* dwarf debugging routines */
222 static void dwarf64_init(void);
223 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
224 static void dwarf64_output(int, void *);
225 static void dwarf64_generate(void);
226 static void dwarf64_cleanup(void);
227 static void dwarf64_findfile(const char *);
228 static void dwarf64_findsect(const int);
231 * Special section numbers which are used to define ELF special
232 * symbols, which can be used with WRT to provide PIC relocation
233 * types.
235 static int32_t elf_gotpc_sect, elf_gotoff_sect;
236 static int32_t elf_got_sect, elf_plt_sect;
237 static int32_t elf_sym_sect;
238 static int32_t elf_gottpoff_sect;
240 static void elf_init(void)
242 maxbits = 64;
243 sects = NULL;
244 nsects = sectlen = 0;
245 syms = saa_init((int32_t)sizeof(struct Symbol));
246 nlocals = nglobs = ndebugs = 0;
247 bsym = raa_init();
248 strs = saa_init(1L);
249 saa_wbytes(strs, "\0", 1L);
250 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
251 strslen = 2 + strlen(elf_module);
252 shstrtab = NULL;
253 shstrtablen = shstrtabsize = 0;;
254 add_sectname("", "");
256 fwds = NULL;
258 elf_gotpc_sect = seg_alloc();
259 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
260 elf_gotoff_sect = seg_alloc();
261 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
262 elf_got_sect = seg_alloc();
263 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
264 elf_plt_sect = seg_alloc();
265 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
266 elf_sym_sect = seg_alloc();
267 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
268 elf_gottpoff_sect = seg_alloc();
269 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
271 def_seg = seg_alloc();
275 static void elf_cleanup(int debuginfo)
277 struct Reloc *r;
278 int i;
280 (void)debuginfo;
282 elf_write();
283 for (i = 0; i < nsects; i++) {
284 if (sects[i]->type != SHT_NOBITS)
285 saa_free(sects[i]->data);
286 if (sects[i]->head)
287 saa_free(sects[i]->rel);
288 while (sects[i]->head) {
289 r = sects[i]->head;
290 sects[i]->head = sects[i]->head->next;
291 nasm_free(r);
294 nasm_free(sects);
295 saa_free(syms);
296 raa_free(bsym);
297 saa_free(strs);
298 if (of_elf64.current_dfmt) {
299 of_elf64.current_dfmt->cleanup();
303 /* add entry to the elf .shstrtab section */
304 static void add_sectname(char *firsthalf, char *secondhalf)
306 int len = strlen(firsthalf) + strlen(secondhalf);
307 while (shstrtablen + len + 1 > shstrtabsize)
308 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
309 strcpy(shstrtab + shstrtablen, firsthalf);
310 strcat(shstrtab + shstrtablen, secondhalf);
311 shstrtablen += len + 1;
314 static int elf_make_section(char *name, int type, int flags, int align)
316 struct Section *s;
318 s = nasm_malloc(sizeof(*s));
320 if (type != SHT_NOBITS)
321 s->data = saa_init(1L);
322 s->head = NULL;
323 s->tail = &s->head;
324 s->len = s->size = 0;
325 s->nrelocs = 0;
326 if (!strcmp(name, ".text"))
327 s->index = def_seg;
328 else
329 s->index = seg_alloc();
330 add_sectname("", name);
331 s->name = nasm_malloc(1 + strlen(name));
332 strcpy(s->name, name);
333 s->type = type;
334 s->flags = flags;
335 s->align = align;
336 s->gsyms = NULL;
338 if (nsects >= sectlen)
339 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
340 sects[nsects++] = s;
342 return nsects - 1;
345 static int32_t elf_section_names(char *name, int pass, int *bits)
347 char *p;
348 uint32_t flags, flags_and, flags_or;
349 uint64_t align;
350 int type, i;
353 * Default is 64 bits.
355 if (!name) {
356 *bits = 64;
357 return def_seg;
360 p = nasm_skip_word(name);
361 if (*p)
362 *p++ = '\0';
363 flags_and = flags_or = type = align = 0;
365 p = nasm_skip_spaces(p);
366 while (*p) {
367 char *q = p;
368 p = nasm_skip_word(p);
369 if (*p)
370 *p++ = '\0';
371 p = nasm_skip_spaces(p);
373 if (!nasm_strnicmp(q, "align=", 6)) {
374 align = atoi(q + 6);
375 if (align == 0)
376 align = 1;
377 if ((align - 1) & align) { /* means it's not a power of two */
378 nasm_error(ERR_NONFATAL, "section alignment %"PRId64" is not"
379 " a power of two", align);
380 align = 1;
382 } else if (!nasm_stricmp(q, "alloc")) {
383 flags_and |= SHF_ALLOC;
384 flags_or |= SHF_ALLOC;
385 } else if (!nasm_stricmp(q, "noalloc")) {
386 flags_and |= SHF_ALLOC;
387 flags_or &= ~SHF_ALLOC;
388 } else if (!nasm_stricmp(q, "exec")) {
389 flags_and |= SHF_EXECINSTR;
390 flags_or |= SHF_EXECINSTR;
391 } else if (!nasm_stricmp(q, "noexec")) {
392 flags_and |= SHF_EXECINSTR;
393 flags_or &= ~SHF_EXECINSTR;
394 } else if (!nasm_stricmp(q, "write")) {
395 flags_and |= SHF_WRITE;
396 flags_or |= SHF_WRITE;
397 } else if (!nasm_stricmp(q, "tls")) {
398 flags_and |= SHF_TLS;
399 flags_or |= SHF_TLS;
400 } else if (!nasm_stricmp(q, "nowrite")) {
401 flags_and |= SHF_WRITE;
402 flags_or &= ~SHF_WRITE;
403 } else if (!nasm_stricmp(q, "progbits")) {
404 type = SHT_PROGBITS;
405 } else if (!nasm_stricmp(q, "nobits")) {
406 type = SHT_NOBITS;
407 } else if (pass == 1) {
408 nasm_error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
409 " declaration of section `%s'", q, name);
413 if (!strcmp(name, ".shstrtab") ||
414 !strcmp(name, ".symtab") ||
415 !strcmp(name, ".strtab")) {
416 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
417 "name `%s'", name);
418 return NO_SEG;
421 for (i = 0; i < nsects; i++)
422 if (!strcmp(name, sects[i]->name))
423 break;
424 if (i == nsects) {
425 const struct elf_known_section *ks = elf_known_sections;
427 while (ks->name) {
428 if (!strcmp(name, ks->name))
429 break;
430 ks++;
433 type = type ? type : ks->type;
434 align = align ? align : ks->align;
435 flags = (ks->flags & ~flags_and) | flags_or;
437 i = elf_make_section(name, type, flags, align);
438 } else if (pass == 1) {
439 if ((type && sects[i]->type != type)
440 || (align && sects[i]->align != align)
441 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
442 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
443 " redeclaration of section `%s'", name);
446 return sects[i]->index;
449 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
450 int is_global, char *special)
452 int pos = strslen;
453 struct Symbol *sym;
454 bool special_used = false;
456 #if defined(DEBUG) && DEBUG>2
457 nasm_error(ERR_DEBUG,
458 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
459 name, segment, offset, is_global, special);
460 #endif
461 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
463 * This is a NASM special symbol. We never allow it into
464 * the ELF symbol table, even if it's a valid one. If it
465 * _isn't_ a valid one, we should barf immediately.
467 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
468 strcmp(name, "..got") && strcmp(name, "..plt") &&
469 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
470 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
471 return;
474 if (is_global == 3) {
475 struct Symbol **s;
477 * Fix up a forward-reference symbol size from the first
478 * pass.
480 for (s = &fwds; *s; s = &(*s)->nextfwd)
481 if (!strcmp((*s)->name, name)) {
482 struct tokenval tokval;
483 expr *e;
484 char *p = nasm_skip_spaces(nasm_skip_word(special));
486 stdscan_reset();
487 stdscan_set(p);
488 tokval.t_type = TOKEN_INVALID;
489 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
490 if (e) {
491 if (!is_simple(e))
492 nasm_error(ERR_NONFATAL, "cannot use relocatable"
493 " expression as symbol size");
494 else
495 (*s)->size = reloc_value(e);
499 * Remove it from the list of unresolved sizes.
501 nasm_free((*s)->name);
502 *s = (*s)->nextfwd;
503 return;
505 return; /* it wasn't an important one */
508 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
509 strslen += 1 + strlen(name);
511 lastsym = sym = saa_wstruct(syms);
513 memset(&sym->symv, 0, sizeof(struct rbtree));
515 sym->strpos = pos;
516 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
517 sym->other = STV_DEFAULT;
518 sym->size = 0;
519 if (segment == NO_SEG)
520 sym->section = SHN_ABS;
521 else {
522 int i;
523 sym->section = SHN_UNDEF;
524 if (segment == def_seg) {
525 /* we have to be sure at least text section is there */
526 int tempint;
527 if (segment != elf_section_names(".text", 2, &tempint))
528 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
530 for (i = 0; i < nsects; i++) {
531 if (segment == sects[i]->index) {
532 sym->section = i + 1;
533 break;
538 if (is_global == 2) {
539 sym->size = offset;
540 sym->symv.key = 0;
541 sym->section = SHN_COMMON;
543 * We have a common variable. Check the special text to see
544 * if it's a valid number and power of two; if so, store it
545 * as the alignment for the common variable.
547 if (special) {
548 bool err;
549 sym->symv.key = readnum(special, &err);
550 if (err)
551 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
552 " valid number", special);
553 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
554 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
555 " power of two", special);
557 special_used = true;
558 } else
559 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
561 if (sym->type == SYM_GLOBAL) {
563 * If sym->section == SHN_ABS, then the first line of the
564 * else section would cause a core dump, because its a reference
565 * beyond the end of the section array.
566 * This behaviour is exhibited by this code:
567 * GLOBAL crash_nasm
568 * crash_nasm equ 0
569 * To avoid such a crash, such requests are silently discarded.
570 * This may not be the best solution.
572 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
573 bsym = raa_write(bsym, segment, nglobs);
574 } else if (sym->section != SHN_ABS) {
576 * This is a global symbol; so we must add it to the rbtree
577 * of global symbols in its section.
579 * In addition, we check the special text for symbol
580 * type and size information.
582 sects[sym->section-1]->gsyms =
583 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
585 if (special) {
586 int n = strcspn(special, " \t");
588 if (!nasm_strnicmp(special, "function", n))
589 sym->type |= STT_FUNC;
590 else if (!nasm_strnicmp(special, "data", n) ||
591 !nasm_strnicmp(special, "object", n))
592 sym->type |= STT_OBJECT;
593 else if (!nasm_strnicmp(special, "notype", n))
594 sym->type |= STT_NOTYPE;
595 else
596 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
597 n, special);
598 special += n;
600 special = nasm_skip_spaces(special);
601 if (*special) {
602 n = strcspn(special, " \t");
603 if (!nasm_strnicmp(special, "default", n))
604 sym->other = STV_DEFAULT;
605 else if (!nasm_strnicmp(special, "internal", n))
606 sym->other = STV_INTERNAL;
607 else if (!nasm_strnicmp(special, "hidden", n))
608 sym->other = STV_HIDDEN;
609 else if (!nasm_strnicmp(special, "protected", n))
610 sym->other = STV_PROTECTED;
611 else
612 n = 0;
613 special += n;
616 if (*special) {
617 struct tokenval tokval;
618 expr *e;
619 int fwd = 0;
620 char *saveme = stdscan_get();
622 while (special[n] && nasm_isspace(special[n]))
623 n++;
625 * We have a size expression; attempt to
626 * evaluate it.
628 stdscan_reset();
629 stdscan_set(special + n);
630 tokval.t_type = TOKEN_INVALID;
631 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
632 NULL);
633 if (fwd) {
634 sym->nextfwd = fwds;
635 fwds = sym;
636 sym->name = nasm_strdup(name);
637 } else if (e) {
638 if (!is_simple(e))
639 nasm_error(ERR_NONFATAL, "cannot use relocatable"
640 " expression as symbol size");
641 else
642 sym->size = reloc_value(e);
644 stdscan_set(saveme);
646 special_used = true;
649 * If TLS segment, mark symbol accordingly.
651 if (sects[sym->section - 1]->flags & SHF_TLS) {
652 sym->type &= 0xf0;
653 sym->type |= STT_TLS;
656 sym->globnum = nglobs;
657 nglobs++;
658 } else
659 nlocals++;
661 if (special && !special_used)
662 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
665 static void elf_add_reloc(struct Section *sect, int32_t segment,
666 int64_t offset, int type)
668 struct Reloc *r;
669 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
670 sect->tail = &r->next;
671 r->next = NULL;
673 r->address = sect->len;
674 r->offset = offset;
675 if (segment == NO_SEG)
676 r->symbol = 0;
677 else {
678 int i;
679 r->symbol = 0;
680 for (i = 0; i < nsects; i++)
681 if (segment == sects[i]->index)
682 r->symbol = i + 2;
683 if (!r->symbol)
684 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
686 r->type = type;
688 sect->nrelocs++;
692 * This routine deals with ..got and ..sym relocations: the more
693 * complicated kinds. In shared-library writing, some relocations
694 * with respect to global symbols must refer to the precise symbol
695 * rather than referring to an offset from the base of the section
696 * _containing_ the symbol. Such relocations call to this routine,
697 * which searches the symbol list for the symbol in question.
699 * R_386_GOT32 references require the _exact_ symbol address to be
700 * used; R_386_32 references can be at an offset from the symbol.
701 * The boolean argument `exact' tells us this.
703 * Return value is the adjusted value of `addr', having become an
704 * offset from the symbol rather than the section. Should always be
705 * zero when returning from an exact call.
707 * Limitation: if you define two symbols at the same place,
708 * confusion will occur.
710 * Inefficiency: we search, currently, using a linked list which
711 * isn't even necessarily sorted.
713 static void elf_add_gsym_reloc(struct Section *sect,
714 int32_t segment, uint64_t offset, int64_t pcrel,
715 int type, bool exact)
717 struct Reloc *r;
718 struct Section *s;
719 struct Symbol *sym;
720 struct rbtree *srb;
721 int i;
724 * First look up the segment/offset pair and find a global
725 * symbol corresponding to it. If it's not one of our segments,
726 * then it must be an external symbol, in which case we're fine
727 * doing a normal elf_add_reloc after first sanity-checking
728 * that the offset from the symbol is zero.
730 s = NULL;
731 for (i = 0; i < nsects; i++)
732 if (segment == sects[i]->index) {
733 s = sects[i];
734 break;
737 if (!s) {
738 if (exact && offset)
739 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
740 else
741 elf_add_reloc(sect, segment, offset - pcrel, type);
742 return;
745 srb = rb_search(s->gsyms, offset);
746 if (!srb || (exact && srb->key != offset)) {
747 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
748 " for this reference");
749 return;
751 sym = container_of(srb, struct Symbol, symv);
753 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
754 sect->tail = &r->next;
755 r->next = NULL;
757 r->address = sect->len;
758 r->offset = offset - pcrel - sym->symv.key;
759 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
760 r->type = type;
762 sect->nrelocs++;
765 static void elf_out(int32_t segto, const void *data,
766 enum out_type type, uint64_t size,
767 int32_t segment, int32_t wrt)
769 struct Section *s;
770 int64_t addr, zero;
771 int i;
772 static struct symlininfo sinfo;
774 zero = 0;
776 #if defined(DEBUG) && DEBUG>2
777 if (data)
778 nasm_error(ERR_DEBUG,
779 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
780 currentline, type, segment, segto, size, *(int64_t *)data);
781 else
782 nasm_error(ERR_DEBUG,
783 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
784 currentline, type, segment, segto, size);
785 #endif
788 * handle absolute-assembly (structure definitions)
790 if (segto == NO_SEG) {
791 if (type != OUT_RESERVE)
792 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
793 " space");
794 return;
797 s = NULL;
798 for (i = 0; i < nsects; i++)
799 if (segto == sects[i]->index) {
800 s = sects[i];
801 break;
803 if (!s) {
804 int tempint; /* ignored */
805 if (segto != elf_section_names(".text", 2, &tempint))
806 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
807 else {
808 s = sects[nsects - 1];
809 i = nsects - 1;
812 /* invoke current debug_output routine */
813 if (of_elf64.current_dfmt) {
814 sinfo.offset = s->len;
815 sinfo.section = i;
816 sinfo.segto = segto;
817 sinfo.name = s->name;
818 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
820 /* end of debugging stuff */
822 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
823 nasm_error(ERR_WARNING, "attempt to initialize memory in"
824 " BSS section `%s': ignored", s->name);
825 s->len += realsize(type, size);
826 return;
829 if (type == OUT_RESERVE) {
830 if (s->type == SHT_PROGBITS) {
831 nasm_error(ERR_WARNING, "uninitialized space declared in"
832 " non-BSS section `%s': zeroing", s->name);
833 elf_sect_write(s, NULL, size);
834 } else
835 s->len += size;
836 } else if (type == OUT_RAWDATA) {
837 if (segment != NO_SEG)
838 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
839 elf_sect_write(s, data, size);
840 } else if (type == OUT_ADDRESS) {
841 addr = *(int64_t *)data;
842 if (segment == NO_SEG) {
843 /* Do nothing */
844 } else if (segment % 2) {
845 nasm_error(ERR_NONFATAL, "ELF format does not support"
846 " segment base references");
847 } else {
848 if (wrt == NO_SEG) {
849 switch ((int)size) {
850 case 1:
851 elf_add_reloc(s, segment, addr, R_X86_64_8);
852 break;
853 case 2:
854 elf_add_reloc(s, segment, addr, R_X86_64_16);
855 break;
856 case 4:
857 elf_add_reloc(s, segment, addr, R_X86_64_32);
858 break;
859 case 8:
860 elf_add_reloc(s, segment, addr, R_X86_64_64);
861 break;
862 default:
863 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
864 break;
866 addr = 0;
867 } else if (wrt == elf_gotpc_sect + 1) {
869 * The user will supply GOT relative to $$. ELF
870 * will let us have GOT relative to $. So we
871 * need to fix up the data item by $-$$.
873 addr += s->len;
874 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
875 addr = 0;
876 } else if (wrt == elf_gotoff_sect + 1) {
877 if (size != 8) {
878 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
879 "references to be qword");
880 } else {
881 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
882 addr = 0;
884 } else if (wrt == elf_got_sect + 1) {
885 switch ((int)size) {
886 case 4:
887 elf_add_gsym_reloc(s, segment, addr, 0,
888 R_X86_64_GOT32, true);
889 addr = 0;
890 break;
891 case 8:
892 elf_add_gsym_reloc(s, segment, addr, 0,
893 R_X86_64_GOT64, true);
894 addr = 0;
895 break;
896 default:
897 nasm_error(ERR_NONFATAL, "invalid ..got reference");
898 break;
900 } else if (wrt == elf_sym_sect + 1) {
901 switch ((int)size) {
902 case 1:
903 elf_add_gsym_reloc(s, segment, addr, 0,
904 R_X86_64_8, false);
905 addr = 0;
906 break;
907 case 2:
908 elf_add_gsym_reloc(s, segment, addr, 0,
909 R_X86_64_16, false);
910 addr = 0;
911 break;
912 case 4:
913 elf_add_gsym_reloc(s, segment, addr, 0,
914 R_X86_64_32, false);
915 addr = 0;
916 break;
917 case 8:
918 elf_add_gsym_reloc(s, segment, addr, 0,
919 R_X86_64_64, false);
920 addr = 0;
921 break;
922 default:
923 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
924 break;
926 } else if (wrt == elf_plt_sect + 1) {
927 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
928 "relative PLT references");
929 } else {
930 nasm_error(ERR_NONFATAL, "ELF format does not support this"
931 " use of WRT");
934 elf_sect_writeaddr(s, addr, size);
935 } else if (type == OUT_REL2ADR) {
936 addr = *(int64_t *)data - size;
937 if (segment == segto)
938 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
939 if (segment == NO_SEG) {
940 /* Do nothing */
941 } else if (segment % 2) {
942 nasm_error(ERR_NONFATAL, "ELF format does not support"
943 " segment base references");
944 } else {
945 if (wrt == NO_SEG) {
946 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
947 addr = 0;
948 } else {
949 nasm_error(ERR_NONFATAL,
950 "Unsupported non-32-bit ELF relocation [2]");
953 elf_sect_writeaddr(s, addr, 2);
954 } else if (type == OUT_REL4ADR) {
955 addr = *(int64_t *)data - size;
956 if (segment == segto)
957 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
958 if (segment == NO_SEG) {
959 /* Do nothing */
960 } else if (segment % 2) {
961 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
962 " segment base references");
963 } else {
964 if (wrt == NO_SEG) {
965 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
966 addr = 0;
967 } else if (wrt == elf_plt_sect + 1) {
968 elf_add_gsym_reloc(s, segment, addr+size, size,
969 R_X86_64_PLT32, true);
970 addr = 0;
971 } else if (wrt == elf_gotpc_sect + 1 ||
972 wrt == elf_got_sect + 1) {
973 elf_add_gsym_reloc(s, segment, addr+size, size,
974 R_X86_64_GOTPCREL, true);
975 addr = 0;
976 } else if (wrt == elf_gotoff_sect + 1 ||
977 wrt == elf_got_sect + 1) {
978 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
979 "qword absolute");
980 } else if (wrt == elf_gottpoff_sect + 1) {
981 elf_add_gsym_reloc(s, segment, addr+size, size,
982 R_X86_64_GOTTPOFF, true);
983 addr = 0;
984 } else {
985 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
986 " use of WRT");
989 elf_sect_writeaddr(s, addr, 4);
990 } else if (type == OUT_REL8ADR) {
991 addr = *(int64_t *)data - size;
992 if (segment == segto)
993 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
994 if (segment == NO_SEG) {
995 /* Do nothing */
996 } else if (segment % 2) {
997 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
998 " segment base references");
999 } else {
1000 if (wrt == NO_SEG) {
1001 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1002 addr = 0;
1003 } else if (wrt == elf_gotpc_sect + 1 ||
1004 wrt == elf_got_sect + 1) {
1005 elf_add_gsym_reloc(s, segment, addr+size, size,
1006 R_X86_64_GOTPCREL64, true);
1007 addr = 0;
1008 } else if (wrt == elf_gotoff_sect + 1 ||
1009 wrt == elf_got_sect + 1) {
1010 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1011 "absolute");
1012 } else if (wrt == elf_gottpoff_sect + 1) {
1013 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1014 "dword");
1015 } else {
1016 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1017 " use of WRT");
1020 elf_sect_writeaddr(s, addr, 8);
1024 static void elf_write(void)
1026 int align;
1027 char *p;
1028 int i;
1030 struct SAA *symtab;
1031 int32_t symtablen, symtablocal;
1034 * Work out how many sections we will have. We have SHN_UNDEF,
1035 * then the flexible user sections, then the fixed sections
1036 * `.shstrtab', `.symtab' and `.strtab', then optionally
1037 * relocation sections for the user sections.
1039 nsections = sec_numspecial + 1;
1040 if (of_elf64.current_dfmt == &df_stabs)
1041 nsections += 3;
1042 else if (of_elf64.current_dfmt == &df_dwarf)
1043 nsections += 10;
1045 add_sectname("", ".shstrtab");
1046 add_sectname("", ".symtab");
1047 add_sectname("", ".strtab");
1048 for (i = 0; i < nsects; i++) {
1049 nsections++; /* for the section itself */
1050 if (sects[i]->head) {
1051 nsections++; /* for its relocations */
1052 add_sectname(".rela", sects[i]->name);
1056 if (of_elf64.current_dfmt == &df_stabs) {
1057 /* in case the debug information is wanted, just add these three sections... */
1058 add_sectname("", ".stab");
1059 add_sectname("", ".stabstr");
1060 add_sectname(".rel", ".stab");
1063 else if (of_elf64.current_dfmt == &df_dwarf) {
1064 /* the dwarf debug standard specifies the following ten sections,
1065 not all of which are currently implemented,
1066 although all of them are defined. */
1067 #define debug_aranges (int64_t) (nsections-10)
1068 #define debug_info (int64_t) (nsections-7)
1069 #define debug_abbrev (int64_t) (nsections-5)
1070 #define debug_line (int64_t) (nsections-4)
1071 add_sectname("", ".debug_aranges");
1072 add_sectname(".rela", ".debug_aranges");
1073 add_sectname("", ".debug_pubnames");
1074 add_sectname("", ".debug_info");
1075 add_sectname(".rela", ".debug_info");
1076 add_sectname("", ".debug_abbrev");
1077 add_sectname("", ".debug_line");
1078 add_sectname(".rela", ".debug_line");
1079 add_sectname("", ".debug_frame");
1080 add_sectname("", ".debug_loc");
1084 * Output the ELF header.
1086 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1087 fputc(elf_osabi, ofile);
1088 fputc(elf_abiver, ofile);
1089 fwritezero(7, ofile);
1090 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1091 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1092 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1093 fwriteint64_t(0L, ofile); /* no entry point */
1094 fwriteint64_t(0L, ofile); /* no program header table */
1095 fwriteint64_t(0x40L, ofile); /* section headers straight after
1096 * ELF header plus alignment */
1097 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1098 fwriteint16_t(0x40, ofile); /* size of ELF header */
1099 fwriteint16_t(0, ofile); /* no program header table, again */
1100 fwriteint16_t(0, ofile); /* still no program header table */
1101 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1102 fwriteint16_t(nsections, ofile); /* number of sections */
1103 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1104 * section header table */
1107 * Build the symbol table and relocation tables.
1109 symtab = elf_build_symtab(&symtablen, &symtablocal);
1110 for (i = 0; i < nsects; i++)
1111 if (sects[i]->head)
1112 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1113 sects[i]->head);
1116 * Now output the section header table.
1119 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1120 align = ALIGN(elf_foffs, SEG_ALIGN) - elf_foffs;
1121 elf_foffs += align;
1122 elf_nsect = 0;
1123 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1125 /* SHN_UNDEF */
1126 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1127 p = shstrtab + 1;
1129 /* The normal sections */
1130 for (i = 0; i < nsects; i++) {
1131 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1132 (sects[i]->type == SHT_PROGBITS ?
1133 sects[i]->data : NULL), true,
1134 sects[i]->len, 0, 0, sects[i]->align, 0);
1135 p += strlen(p) + 1;
1138 /* .shstrtab */
1139 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1140 shstrtablen, 0, 0, 1, 0);
1141 p += strlen(p) + 1;
1143 /* .symtab */
1144 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1145 symtablen, sec_strtab, symtablocal, 4, 24);
1146 p += strlen(p) + 1;
1148 /* .strtab */
1149 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1150 strslen, 0, 0, 1, 0);
1151 p += strlen(p) + 1;
1153 /* The relocation sections */
1154 for (i = 0; i < nsects; i++)
1155 if (sects[i]->head) {
1156 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1157 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1158 p += strlen(p) + 1;
1161 if (of_elf64.current_dfmt == &df_stabs) {
1162 /* for debugging information, create the last three sections
1163 which are the .stab , .stabstr and .rel.stab sections respectively */
1165 /* this function call creates the stab sections in memory */
1166 stabs64_generate();
1168 if (stabbuf && stabstrbuf && stabrelbuf) {
1169 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1170 stablen, sec_stabstr, 0, 4, 12);
1171 p += strlen(p) + 1;
1173 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1174 stabstrlen, 0, 0, 4, 0);
1175 p += strlen(p) + 1;
1177 /* link -> symtable info -> section to refer to */
1178 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1179 stabrellen, symtabsection, sec_stab, 4, 16);
1180 p += strlen(p) + 1;
1183 else if (of_elf64.current_dfmt == &df_dwarf) {
1184 /* for dwarf debugging information, create the ten dwarf sections */
1186 /* this function call creates the dwarf sections in memory */
1187 if (dwarf_fsect)
1188 dwarf64_generate();
1190 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1191 arangeslen, 0, 0, 1, 0);
1192 p += strlen(p) + 1;
1194 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1195 arangesrellen, symtabsection, debug_aranges, 1, 24);
1196 p += strlen(p) + 1;
1198 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1199 pubnameslen, 0, 0, 1, 0);
1200 p += strlen(p) + 1;
1202 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1203 infolen, 0, 0, 1, 0);
1204 p += strlen(p) + 1;
1206 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1207 inforellen, symtabsection, debug_info, 1, 24);
1208 p += strlen(p) + 1;
1210 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1211 abbrevlen, 0, 0, 1, 0);
1212 p += strlen(p) + 1;
1214 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1215 linelen, 0, 0, 1, 0);
1216 p += strlen(p) + 1;
1218 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1219 linerellen, symtabsection, debug_line, 1, 24);
1220 p += strlen(p) + 1;
1222 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1223 framelen, 0, 0, 8, 0);
1224 p += strlen(p) + 1;
1226 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1227 loclen, 0, 0, 1, 0);
1228 p += strlen(p) + 1;
1230 fwritezero(align, ofile);
1233 * Now output the sections.
1235 elf_write_sections();
1237 nasm_free(elf_sects);
1238 saa_free(symtab);
1241 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1243 struct SAA *s = saa_init(1L);
1244 struct Symbol *sym;
1245 uint8_t entry[24], *p;
1246 int i;
1248 *len = *local = 0;
1251 * First, an all-zeros entry, required by the ELF spec.
1253 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1254 *len += 24;
1255 (*local)++;
1258 * Next, an entry for the file name.
1260 p = entry;
1261 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1262 WRITESHORT(p, STT_FILE); /* type FILE */
1263 WRITESHORT(p, SHN_ABS);
1264 WRITEDLONG(p, (uint64_t) 0); /* no value */
1265 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1266 saa_wbytes(s, entry, 24L);
1267 *len += 24;
1268 (*local)++;
1271 * Now some standard symbols defining the segments, for relocation
1272 * purposes.
1274 for (i = 1; i <= nsects; i++) {
1275 p = entry;
1276 WRITELONG(p, 0); /* no symbol name */
1277 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1278 WRITESHORT(p, i); /* section id */
1279 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1280 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1281 saa_wbytes(s, entry, 24L);
1282 *len += 24;
1283 (*local)++;
1288 * Now the other local symbols.
1290 saa_rewind(syms);
1291 while ((sym = saa_rstruct(syms))) {
1292 if (sym->type & SYM_GLOBAL)
1293 continue;
1294 p = entry;
1295 WRITELONG(p, sym->strpos); /* index into symbol string table */
1296 WRITECHAR(p, sym->type); /* type and binding */
1297 WRITECHAR(p, sym->other); /* visibility */
1298 WRITESHORT(p, sym->section); /* index into section header table */
1299 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1300 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1301 saa_wbytes(s, entry, 24L);
1302 *len += 24;
1303 (*local)++;
1306 * dwarf needs symbols for debug sections
1307 * which are relocation targets.
1309 if (of_elf64.current_dfmt == &df_dwarf) {
1310 dwarf_infosym = *local;
1311 p = entry;
1312 WRITELONG(p, 0); /* no symbol name */
1313 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1314 WRITESHORT(p, debug_info); /* section id */
1315 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1316 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1317 saa_wbytes(s, entry, 24L);
1318 *len += 24;
1319 (*local)++;
1320 dwarf_abbrevsym = *local;
1321 p = entry;
1322 WRITELONG(p, 0); /* no symbol name */
1323 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1324 WRITESHORT(p, debug_abbrev); /* section id */
1325 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1326 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1327 saa_wbytes(s, entry, 24L);
1328 *len += 24;
1329 (*local)++;
1330 dwarf_linesym = *local;
1331 p = entry;
1332 WRITELONG(p, 0); /* no symbol name */
1333 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1334 WRITESHORT(p, debug_line); /* section id */
1335 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1336 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1337 saa_wbytes(s, entry, 24L);
1338 *len += 24;
1339 (*local)++;
1343 * Now the global symbols.
1345 saa_rewind(syms);
1346 while ((sym = saa_rstruct(syms))) {
1347 if (!(sym->type & SYM_GLOBAL))
1348 continue;
1349 p = entry;
1350 WRITELONG(p, sym->strpos);
1351 WRITECHAR(p, sym->type); /* type and binding */
1352 WRITECHAR(p, sym->other); /* visibility */
1353 WRITESHORT(p, sym->section);
1354 WRITEDLONG(p, (int64_t)sym->symv.key);
1355 WRITEDLONG(p, (int64_t)sym->size);
1356 saa_wbytes(s, entry, 24L);
1357 *len += 24;
1360 return s;
1363 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1365 struct SAA *s;
1366 uint8_t *p, entry[24];
1367 int32_t global_offset;
1369 if (!r)
1370 return NULL;
1372 s = saa_init(1L);
1373 *len = 0;
1376 * How to onvert from a global placeholder to a real symbol index;
1377 * the +2 refers to the two special entries, the null entry and
1378 * the filename entry.
1380 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1382 while (r) {
1383 int32_t sym = r->symbol;
1385 if (sym >= GLOBAL_TEMP_BASE)
1386 sym += global_offset;
1388 p = entry;
1389 WRITEDLONG(p, r->address);
1390 WRITELONG(p, r->type);
1391 WRITELONG(p, sym);
1392 WRITEDLONG(p, r->offset);
1393 saa_wbytes(s, entry, 24L);
1394 *len += 24;
1396 r = r->next;
1399 return s;
1402 static void elf_section_header(int name, int type, uint64_t flags,
1403 void *data, bool is_saa, uint64_t datalen,
1404 int link, int info, int align, int eltsize)
1406 elf_sects[elf_nsect].data = data;
1407 elf_sects[elf_nsect].len = datalen;
1408 elf_sects[elf_nsect].is_saa = is_saa;
1409 elf_nsect++;
1411 fwriteint32_t((int32_t)name, ofile);
1412 fwriteint32_t((int32_t)type, ofile);
1413 fwriteint64_t((int64_t)flags, ofile);
1414 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1415 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1416 fwriteint64_t(datalen, ofile);
1417 if (data)
1418 elf_foffs += ALIGN(datalen, SEG_ALIGN);
1419 fwriteint32_t((int32_t)link, ofile);
1420 fwriteint32_t((int32_t)info, ofile);
1421 fwriteint64_t((int64_t)align, ofile);
1422 fwriteint64_t((int64_t)eltsize, ofile);
1425 static void elf_write_sections(void)
1427 int i;
1428 for (i = 0; i < elf_nsect; i++)
1429 if (elf_sects[i].data) {
1430 int32_t len = elf_sects[i].len;
1431 int32_t reallen = ALIGN(len, SEG_ALIGN);
1432 int32_t align = reallen - len;
1433 if (elf_sects[i].is_saa)
1434 saa_fpwrite(elf_sects[i].data, ofile);
1435 else
1436 fwrite(elf_sects[i].data, len, 1, ofile);
1437 fwritezero(align, ofile);
1441 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1443 saa_wbytes(sect->data, data, len);
1444 sect->len += len;
1446 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1448 saa_writeaddr(sect->data, data, len);
1449 sect->len += len;
1452 static int32_t elf_segbase(int32_t segment)
1454 return segment;
1457 static int elf_directive(enum directives directive, char *value, int pass)
1459 bool err;
1460 int64_t n;
1461 char *p;
1463 switch (directive) {
1464 case D_OSABI:
1465 if (pass == 2)
1466 return 1; /* ignore in pass 2 */
1468 n = readnum(value, &err);
1469 if (err) {
1470 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1471 return 1;
1473 if (n < 0 || n > 255) {
1474 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1475 return 1;
1477 elf_osabi = n;
1478 elf_abiver = 0;
1480 if ((p = strchr(value,',')) == NULL)
1481 return 1;
1483 n = readnum(p+1, &err);
1484 if (err || n < 0 || n > 255) {
1485 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1486 return 1;
1489 elf_abiver = n;
1490 return 1;
1492 default:
1493 return 0;
1497 static void elf_filename(char *inname, char *outname)
1499 strcpy(elf_module, inname);
1500 standard_extension(inname, outname, ".o");
1503 extern macros_t elf_stdmac[];
1505 static int elf_set_info(enum geninfo type, char **val)
1507 (void)type;
1508 (void)val;
1509 return 0;
1511 static struct dfmt df_dwarf = {
1512 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1513 "dwarf",
1514 dwarf64_init,
1515 dwarf64_linenum,
1516 debug64_deflabel,
1517 debug64_directive,
1518 debug64_typevalue,
1519 dwarf64_output,
1520 dwarf64_cleanup
1522 static struct dfmt df_stabs = {
1523 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1524 "stabs",
1525 null_debug_init,
1526 stabs64_linenum,
1527 debug64_deflabel,
1528 debug64_directive,
1529 debug64_typevalue,
1530 stabs64_output,
1531 stabs64_cleanup
1534 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1536 struct ofmt of_elf64 = {
1537 "ELF64 (x86_64) object files (e.g. Linux)",
1538 "elf64",
1540 elf64_debugs_arr,
1541 &df_stabs,
1542 elf_stdmac,
1543 elf_init,
1544 elf_set_info,
1545 elf_out,
1546 elf_deflabel,
1547 elf_section_names,
1548 elf_segbase,
1549 elf_directive,
1550 elf_filename,
1551 elf_cleanup
1554 /* common debugging routines */
1555 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1556 int is_global, char *special)
1558 (void)name;
1559 (void)segment;
1560 (void)offset;
1561 (void)is_global;
1562 (void)special;
1565 static void debug64_directive(const char *directive, const char *params)
1567 (void)directive;
1568 (void)params;
1571 static void debug64_typevalue(int32_t type)
1573 int32_t stype, ssize;
1574 switch (TYM_TYPE(type)) {
1575 case TY_LABEL:
1576 ssize = 0;
1577 stype = STT_NOTYPE;
1578 break;
1579 case TY_BYTE:
1580 ssize = 1;
1581 stype = STT_OBJECT;
1582 break;
1583 case TY_WORD:
1584 ssize = 2;
1585 stype = STT_OBJECT;
1586 break;
1587 case TY_DWORD:
1588 ssize = 4;
1589 stype = STT_OBJECT;
1590 break;
1591 case TY_FLOAT:
1592 ssize = 4;
1593 stype = STT_OBJECT;
1594 break;
1595 case TY_QWORD:
1596 ssize = 8;
1597 stype = STT_OBJECT;
1598 break;
1599 case TY_TBYTE:
1600 ssize = 10;
1601 stype = STT_OBJECT;
1602 break;
1603 case TY_OWORD:
1604 ssize = 16;
1605 stype = STT_OBJECT;
1606 break;
1607 case TY_YWORD:
1608 ssize = 32;
1609 stype = STT_OBJECT;
1610 break;
1611 case TY_COMMON:
1612 ssize = 0;
1613 stype = STT_COMMON;
1614 break;
1615 case TY_SEG:
1616 ssize = 0;
1617 stype = STT_SECTION;
1618 break;
1619 case TY_EXTERN:
1620 ssize = 0;
1621 stype = STT_NOTYPE;
1622 break;
1623 case TY_EQU:
1624 ssize = 0;
1625 stype = STT_NOTYPE;
1626 break;
1627 default:
1628 ssize = 0;
1629 stype = STT_NOTYPE;
1630 break;
1632 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1633 lastsym->size = ssize;
1634 lastsym->type = stype;
1638 /* stabs debugging routines */
1640 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1642 (void)segto;
1643 if (!stabs_filename) {
1644 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1645 strcpy(stabs_filename, filename);
1646 } else {
1647 if (strcmp(stabs_filename, filename)) {
1648 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1649 in fact, this leak comes in quite handy to maintain a list of files
1650 encountered so far in the symbol lines... */
1652 /* why not nasm_free(stabs_filename); we're done with the old one */
1654 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1655 strcpy(stabs_filename, filename);
1658 debug_immcall = 1;
1659 currentline = linenumber;
1663 static void stabs64_output(int type, void *param)
1665 struct symlininfo *s;
1666 struct linelist *el;
1667 if (type == TY_DEBUGSYMLIN) {
1668 if (debug_immcall) {
1669 s = (struct symlininfo *)param;
1670 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1671 return; /* line info is only collected for executable sections */
1672 numlinestabs++;
1673 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1674 el->info.offset = s->offset;
1675 el->info.section = s->section;
1676 el->info.name = s->name;
1677 el->line = currentline;
1678 el->filename = stabs_filename;
1679 el->next = 0;
1680 if (stabslines) {
1681 stabslines->last->next = el;
1682 stabslines->last = el;
1683 } else {
1684 stabslines = el;
1685 stabslines->last = el;
1689 debug_immcall = 0;
1692 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1693 do { \
1694 WRITELONG(p,n_strx); \
1695 WRITECHAR(p,n_type); \
1696 WRITECHAR(p,n_other); \
1697 WRITESHORT(p,n_desc); \
1698 WRITELONG(p,n_value); \
1699 } while (0)
1701 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1703 static void stabs64_generate(void)
1705 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1706 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1707 char **allfiles;
1708 int *fileidx;
1710 struct linelist *ptr;
1712 ptr = stabslines;
1714 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1715 for (i = 0; i < numlinestabs; i++)
1716 allfiles[i] = 0;
1717 numfiles = 0;
1718 while (ptr) {
1719 if (numfiles == 0) {
1720 allfiles[0] = ptr->filename;
1721 numfiles++;
1722 } else {
1723 for (i = 0; i < numfiles; i++) {
1724 if (!strcmp(allfiles[i], ptr->filename))
1725 break;
1727 if (i >= numfiles) {
1728 allfiles[i] = ptr->filename;
1729 numfiles++;
1732 ptr = ptr->next;
1734 strsize = 1;
1735 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1736 for (i = 0; i < numfiles; i++) {
1737 fileidx[i] = strsize;
1738 strsize += strlen(allfiles[i]) + 1;
1740 mainfileindex = 0;
1741 for (i = 0; i < numfiles; i++) {
1742 if (!strcmp(allfiles[i], elf_module)) {
1743 mainfileindex = i;
1744 break;
1749 * worst case size of the stab buffer would be:
1750 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1751 * plus one "ending" entry
1753 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1754 sizeof(struct stabentry));
1755 ssbuf = (uint8_t *)nasm_malloc(strsize);
1756 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1757 rptr = rbuf;
1759 for (i = 0; i < numfiles; i++)
1760 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1761 ssbuf[0] = 0;
1763 stabstrlen = strsize; /* set global variable for length of stab strings */
1765 sptr = sbuf;
1766 ptr = stabslines;
1767 numstabs = 0;
1769 if (ptr) {
1771 * this is the first stab, its strx points to the filename of the
1772 * the source-file, the n_desc field should be set to the number
1773 * of remaining stabs
1775 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1777 /* this is the stab for the main source file */
1778 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1780 /* relocation table entry */
1783 * Since the symbol table has two entries before
1784 * the section symbols, the index in the info.section
1785 * member must be adjusted by adding 2
1788 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1789 WRITELONG(rptr, R_X86_64_32);
1790 WRITELONG(rptr, ptr->info.section + 2);
1792 numstabs++;
1793 currfile = mainfileindex;
1796 while (ptr) {
1797 if (strcmp(allfiles[currfile], ptr->filename)) {
1798 /* oops file has changed... */
1799 for (i = 0; i < numfiles; i++)
1800 if (!strcmp(allfiles[i], ptr->filename))
1801 break;
1802 currfile = i;
1803 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1804 ptr->info.offset);
1805 numstabs++;
1807 /* relocation table entry */
1809 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1810 WRITELONG(rptr, R_X86_64_32);
1811 WRITELONG(rptr, ptr->info.section + 2);
1814 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1815 numstabs++;
1817 /* relocation table entry */
1819 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1820 WRITELONG(rptr, R_X86_64_32);
1821 WRITELONG(rptr, ptr->info.section + 2);
1823 ptr = ptr->next;
1827 /* this is an "ending" token */
1828 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1829 numstabs++;
1831 ((struct stabentry *)sbuf)->n_desc = numstabs;
1833 nasm_free(allfiles);
1834 nasm_free(fileidx);
1836 stablen = (sptr - sbuf);
1837 stabrellen = (rptr - rbuf);
1838 stabrelbuf = rbuf;
1839 stabbuf = sbuf;
1840 stabstrbuf = ssbuf;
1843 static void stabs64_cleanup(void)
1845 struct linelist *ptr, *del;
1846 if (!stabslines)
1847 return;
1849 ptr = stabslines;
1850 while (ptr) {
1851 del = ptr;
1852 ptr = ptr->next;
1853 nasm_free(del);
1856 nasm_free(stabbuf);
1857 nasm_free(stabrelbuf);
1858 nasm_free(stabstrbuf);
1861 /* dwarf routines */
1863 static void dwarf64_init(void)
1865 ndebugs = 3; /* 3 debug symbols */
1868 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1869 int32_t segto)
1871 (void)segto;
1872 dwarf64_findfile(filename);
1873 debug_immcall = 1;
1874 currentline = linenumber;
1877 /* called from elf_out with type == TY_DEBUGSYMLIN */
1878 static void dwarf64_output(int type, void *param)
1880 int ln, aa, inx, maxln, soc;
1881 struct symlininfo *s;
1882 struct SAA *plinep;
1884 (void)type;
1886 s = (struct symlininfo *)param;
1888 /* line number info is only gathered for executable sections */
1889 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1890 return;
1892 /* Check if section index has changed */
1893 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1894 dwarf64_findsect(s->section);
1896 /* do nothing unless line or file has changed */
1897 if (!debug_immcall)
1898 return;
1900 ln = currentline - dwarf_csect->line;
1901 aa = s->offset - dwarf_csect->offset;
1902 inx = dwarf_clist->line;
1903 plinep = dwarf_csect->psaa;
1904 /* check for file change */
1905 if (!(inx == dwarf_csect->file)) {
1906 saa_write8(plinep,DW_LNS_set_file);
1907 saa_write8(plinep,inx);
1908 dwarf_csect->file = inx;
1910 /* check for line change */
1911 if (ln) {
1912 /* test if in range of special op code */
1913 maxln = line_base + line_range;
1914 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1915 if (ln >= line_base && ln < maxln && soc < 256) {
1916 saa_write8(plinep,soc);
1917 } else {
1918 saa_write8(plinep,DW_LNS_advance_line);
1919 saa_wleb128s(plinep,ln);
1920 if (aa) {
1921 saa_write8(plinep,DW_LNS_advance_pc);
1922 saa_wleb128u(plinep,aa);
1925 dwarf_csect->line = currentline;
1926 dwarf_csect->offset = s->offset;
1929 /* show change handled */
1930 debug_immcall = 0;
1934 static void dwarf64_generate(void)
1936 uint8_t *pbuf;
1937 int indx;
1938 struct linelist *ftentry;
1939 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1940 struct SAA *parangesrel, *plinesrel, *pinforel;
1941 struct sectlist *psect;
1942 size_t saalen, linepoff, totlen, highaddr;
1944 /* write epilogues for each line program range */
1945 /* and build aranges section */
1946 paranges = saa_init(1L);
1947 parangesrel = saa_init(1L);
1948 saa_write16(paranges,3); /* dwarf version */
1949 saa_write64(parangesrel, paranges->datalen+4);
1950 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1951 saa_write64(parangesrel, 0);
1952 saa_write32(paranges,0); /* offset into info */
1953 saa_write8(paranges,8); /* pointer size */
1954 saa_write8(paranges,0); /* not segmented */
1955 saa_write32(paranges,0); /* padding */
1956 /* iterate though sectlist entries */
1957 psect = dwarf_fsect;
1958 totlen = 0;
1959 highaddr = 0;
1960 for (indx = 0; indx < dwarf_nsections; indx++)
1962 plinep = psect->psaa;
1963 /* Line Number Program Epilogue */
1964 saa_write8(plinep,2); /* std op 2 */
1965 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1966 saa_write8(plinep,DW_LNS_extended_op);
1967 saa_write8(plinep,1); /* operand length */
1968 saa_write8(plinep,DW_LNE_end_sequence);
1969 totlen += plinep->datalen;
1970 /* range table relocation entry */
1971 saa_write64(parangesrel, paranges->datalen + 4);
1972 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1973 saa_write64(parangesrel, (uint64_t) 0);
1974 /* range table entry */
1975 saa_write64(paranges,0x0000); /* range start */
1976 saa_write64(paranges,sects[psect->section]->len); /* range length */
1977 highaddr += sects[psect->section]->len;
1978 /* done with this entry */
1979 psect = psect->next;
1981 saa_write64(paranges,0); /* null address */
1982 saa_write64(paranges,0); /* null length */
1983 saalen = paranges->datalen;
1984 arangeslen = saalen + 4;
1985 arangesbuf = pbuf = nasm_malloc(arangeslen);
1986 WRITELONG(pbuf,saalen); /* initial length */
1987 saa_rnbytes(paranges, pbuf, saalen);
1988 saa_free(paranges);
1990 /* build rela.aranges section */
1991 arangesrellen = saalen = parangesrel->datalen;
1992 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1993 saa_rnbytes(parangesrel, pbuf, saalen);
1994 saa_free(parangesrel);
1996 /* build pubnames section */
1997 ppubnames = saa_init(1L);
1998 saa_write16(ppubnames,3); /* dwarf version */
1999 saa_write32(ppubnames,0); /* offset into info */
2000 saa_write32(ppubnames,0); /* space used in info */
2001 saa_write32(ppubnames,0); /* end of list */
2002 saalen = ppubnames->datalen;
2003 pubnameslen = saalen + 4;
2004 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2005 WRITELONG(pbuf,saalen); /* initial length */
2006 saa_rnbytes(ppubnames, pbuf, saalen);
2007 saa_free(ppubnames);
2009 /* build info section */
2010 pinfo = saa_init(1L);
2011 pinforel = saa_init(1L);
2012 saa_write16(pinfo,3); /* dwarf version */
2013 saa_write64(pinforel, pinfo->datalen + 4);
2014 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2015 saa_write64(pinforel, 0);
2016 saa_write32(pinfo,0); /* offset into abbrev */
2017 saa_write8(pinfo,8); /* pointer size */
2018 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2019 saa_write64(pinforel, pinfo->datalen + 4);
2020 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2021 saa_write64(pinforel, 0);
2022 saa_write64(pinfo,0); /* DW_AT_low_pc */
2023 saa_write64(pinforel, pinfo->datalen + 4);
2024 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2025 saa_write64(pinforel, 0);
2026 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2027 saa_write64(pinforel, pinfo->datalen + 4);
2028 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2029 saa_write64(pinforel, 0);
2030 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2031 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2032 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2033 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2034 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2035 saa_write64(pinforel, pinfo->datalen + 4);
2036 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2037 saa_write64(pinforel, 0);
2038 saa_write64(pinfo,0); /* DW_AT_low_pc */
2039 saa_write64(pinfo,0); /* DW_AT_frame_base */
2040 saa_write8(pinfo,0); /* end of entries */
2041 saalen = pinfo->datalen;
2042 infolen = saalen + 4;
2043 infobuf = pbuf = nasm_malloc(infolen);
2044 WRITELONG(pbuf,saalen); /* initial length */
2045 saa_rnbytes(pinfo, pbuf, saalen);
2046 saa_free(pinfo);
2048 /* build rela.info section */
2049 inforellen = saalen = pinforel->datalen;
2050 inforelbuf = pbuf = nasm_malloc(inforellen);
2051 saa_rnbytes(pinforel, pbuf, saalen);
2052 saa_free(pinforel);
2054 /* build abbrev section */
2055 pabbrev = saa_init(1L);
2056 saa_write8(pabbrev,1); /* entry number LEB128u */
2057 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2058 saa_write8(pabbrev,1); /* has children */
2059 /* the following attributes and forms are all LEB128u values */
2060 saa_write8(pabbrev,DW_AT_low_pc);
2061 saa_write8(pabbrev,DW_FORM_addr);
2062 saa_write8(pabbrev,DW_AT_high_pc);
2063 saa_write8(pabbrev,DW_FORM_addr);
2064 saa_write8(pabbrev,DW_AT_stmt_list);
2065 saa_write8(pabbrev,DW_FORM_data4);
2066 saa_write8(pabbrev,DW_AT_name);
2067 saa_write8(pabbrev,DW_FORM_string);
2068 saa_write8(pabbrev,DW_AT_producer);
2069 saa_write8(pabbrev,DW_FORM_string);
2070 saa_write8(pabbrev,DW_AT_language);
2071 saa_write8(pabbrev,DW_FORM_data2);
2072 saa_write16(pabbrev,0); /* end of entry */
2073 /* LEB128u usage same as above */
2074 saa_write8(pabbrev,2); /* entry number */
2075 saa_write8(pabbrev,DW_TAG_subprogram);
2076 saa_write8(pabbrev,0); /* no children */
2077 saa_write8(pabbrev,DW_AT_low_pc);
2078 saa_write8(pabbrev,DW_FORM_addr);
2079 saa_write8(pabbrev,DW_AT_frame_base);
2080 saa_write8(pabbrev,DW_FORM_data4);
2081 saa_write16(pabbrev,0); /* end of entry */
2082 abbrevlen = saalen = pabbrev->datalen;
2083 abbrevbuf = pbuf = nasm_malloc(saalen);
2084 saa_rnbytes(pabbrev, pbuf, saalen);
2085 saa_free(pabbrev);
2087 /* build line section */
2088 /* prolog */
2089 plines = saa_init(1L);
2090 saa_write8(plines,1); /* Minimum Instruction Length */
2091 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2092 saa_write8(plines,line_base); /* Line Base */
2093 saa_write8(plines,line_range); /* Line Range */
2094 saa_write8(plines,opcode_base); /* Opcode Base */
2095 /* standard opcode lengths (# of LEB128u operands) */
2096 saa_write8(plines,0); /* Std opcode 1 length */
2097 saa_write8(plines,1); /* Std opcode 2 length */
2098 saa_write8(plines,1); /* Std opcode 3 length */
2099 saa_write8(plines,1); /* Std opcode 4 length */
2100 saa_write8(plines,1); /* Std opcode 5 length */
2101 saa_write8(plines,0); /* Std opcode 6 length */
2102 saa_write8(plines,0); /* Std opcode 7 length */
2103 saa_write8(plines,0); /* Std opcode 8 length */
2104 saa_write8(plines,1); /* Std opcode 9 length */
2105 saa_write8(plines,0); /* Std opcode 10 length */
2106 saa_write8(plines,0); /* Std opcode 11 length */
2107 saa_write8(plines,1); /* Std opcode 12 length */
2108 /* Directory Table */
2109 saa_write8(plines,0); /* End of table */
2110 /* File Name Table */
2111 ftentry = dwarf_flist;
2112 for (indx = 0;indx<dwarf_numfiles;indx++)
2114 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2115 saa_write8(plines,0); /* directory LEB128u */
2116 saa_write8(plines,0); /* time LEB128u */
2117 saa_write8(plines,0); /* size LEB128u */
2118 ftentry = ftentry->next;
2120 saa_write8(plines,0); /* End of table */
2121 linepoff = plines->datalen;
2122 linelen = linepoff + totlen + 10;
2123 linebuf = pbuf = nasm_malloc(linelen);
2124 WRITELONG(pbuf,linelen-4); /* initial length */
2125 WRITESHORT(pbuf,3); /* dwarf version */
2126 WRITELONG(pbuf,linepoff); /* offset to line number program */
2127 /* write line header */
2128 saalen = linepoff;
2129 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2130 pbuf += linepoff;
2131 saa_free(plines);
2132 /* concatonate line program ranges */
2133 linepoff += 13;
2134 plinesrel = saa_init(1L);
2135 psect = dwarf_fsect;
2136 for (indx = 0; indx < dwarf_nsections; indx++) {
2137 saa_write64(plinesrel, linepoff);
2138 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2139 saa_write64(plinesrel, (uint64_t) 0);
2140 plinep = psect->psaa;
2141 saalen = plinep->datalen;
2142 saa_rnbytes(plinep, pbuf, saalen);
2143 pbuf += saalen;
2144 linepoff += saalen;
2145 saa_free(plinep);
2146 /* done with this entry */
2147 psect = psect->next;
2151 /* build rela.lines section */
2152 linerellen =saalen = plinesrel->datalen;
2153 linerelbuf = pbuf = nasm_malloc(linerellen);
2154 saa_rnbytes(plinesrel, pbuf, saalen);
2155 saa_free(plinesrel);
2157 /* build frame section */
2158 framelen = 4;
2159 framebuf = pbuf = nasm_malloc(framelen);
2160 WRITELONG(pbuf,framelen-4); /* initial length */
2162 /* build loc section */
2163 loclen = 16;
2164 locbuf = pbuf = nasm_malloc(loclen);
2165 WRITEDLONG(pbuf,0); /* null beginning offset */
2166 WRITEDLONG(pbuf,0); /* null ending offset */
2169 static void dwarf64_cleanup(void)
2171 nasm_free(arangesbuf);
2172 nasm_free(arangesrelbuf);
2173 nasm_free(pubnamesbuf);
2174 nasm_free(infobuf);
2175 nasm_free(inforelbuf);
2176 nasm_free(abbrevbuf);
2177 nasm_free(linebuf);
2178 nasm_free(linerelbuf);
2179 nasm_free(framebuf);
2180 nasm_free(locbuf);
2183 static void dwarf64_findfile(const char * fname)
2185 int finx;
2186 struct linelist *match;
2188 /* return if fname is current file name */
2189 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2190 return;
2192 /* search for match */
2193 match = 0;
2194 if (dwarf_flist) {
2195 match = dwarf_flist;
2196 for (finx = 0; finx < dwarf_numfiles; finx++) {
2197 if (!(strcmp(fname, match->filename))) {
2198 dwarf_clist = match;
2199 return;
2204 /* add file name to end of list */
2205 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2206 dwarf_numfiles++;
2207 dwarf_clist->line = dwarf_numfiles;
2208 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2209 strcpy(dwarf_clist->filename,fname);
2210 dwarf_clist->next = 0;
2211 if (!dwarf_flist) { /* if first entry */
2212 dwarf_flist = dwarf_elist = dwarf_clist;
2213 dwarf_clist->last = 0;
2214 } else { /* chain to previous entry */
2215 dwarf_elist->next = dwarf_clist;
2216 dwarf_elist = dwarf_clist;
2220 static void dwarf64_findsect(const int index)
2222 int sinx;
2223 struct sectlist *match;
2224 struct SAA *plinep;
2226 /* return if index is current section index */
2227 if (dwarf_csect && (dwarf_csect->section == index))
2228 return;
2230 /* search for match */
2231 match = 0;
2232 if (dwarf_fsect) {
2233 match = dwarf_fsect;
2234 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2235 if ((match->section == index)) {
2236 dwarf_csect = match;
2237 return;
2239 match = match->next;
2243 /* add entry to end of list */
2244 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2245 dwarf_nsections++;
2246 dwarf_csect->psaa = plinep = saa_init(1L);
2247 dwarf_csect->line = 1;
2248 dwarf_csect->offset = 0;
2249 dwarf_csect->file = 1;
2250 dwarf_csect->section = index;
2251 dwarf_csect->next = 0;
2252 /* set relocatable address at start of line program */
2253 saa_write8(plinep,DW_LNS_extended_op);
2254 saa_write8(plinep,9); /* operand length */
2255 saa_write8(plinep,DW_LNE_set_address);
2256 saa_write64(plinep,0); /* Start Address */
2258 if (!dwarf_fsect) { /* if first entry */
2259 dwarf_fsect = dwarf_esect = dwarf_csect;
2260 dwarf_csect->last = 0;
2261 } else { /* chain to previous entry */
2262 dwarf_esect->next = dwarf_csect;
2263 dwarf_esect = dwarf_csect;
2267 #endif /* OF_ELF */