Use a normal quad-case for valueless /is4
[nasm.git] / output / outelf64.c
blob46cc6aa5b2c7893faf27042f6dd60d6676f335f4
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelf64.c output routines for the Netwide Assembler to produce
36 * ELF64 (x86_64 of course) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELF64
64 * Relocation types.
66 struct Reloc {
67 struct Reloc *next;
68 int64_t address; /* relative to _start_ of section */
69 int64_t symbol; /* symbol index */
70 int64_t offset; /* symbol addend */
71 int type; /* type of relocation */
74 struct Symbol {
75 struct rbtree symv; /* symbol value and rbtree of globals */
76 int32_t strpos; /* string table position of name */
77 int32_t section; /* section ID of the symbol */
78 int type; /* symbol type */
79 int other; /* symbol visibility */
80 int32_t size; /* size of symbol */
81 int32_t globnum; /* symbol table offset if global */
82 struct Symbol *nextfwd; /* list of unresolved-size symbols */
83 char *name; /* used temporarily if in above list */
86 struct Section {
87 struct SAA *data;
88 uint64_t len, size;
89 uint32_t nrelocs;
90 int32_t index; /* index into sects array */
91 int type; /* SHT_PROGBITS or SHT_NOBITS */
92 uint64_t align; /* alignment: power of two */
93 uint64_t flags; /* section flags */
94 char *name;
95 struct SAA *rel;
96 uint64_t rellen;
97 struct Reloc *head, **tail;
98 struct rbtree *gsyms; /* global symbols in section */
101 #define SECT_DELTA 32
102 static struct Section **sects;
103 static int nsects, sectlen;
105 #define SHSTR_DELTA 256
106 static char *shstrtab;
107 static int shstrtablen, shstrtabsize;
109 static struct SAA *syms;
110 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
112 static int32_t def_seg;
114 static struct RAA *bsym;
116 static struct SAA *strs;
117 static uint32_t strslen;
119 static struct Symbol *fwds;
121 static char elf_module[FILENAME_MAX];
123 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
124 static uint8_t elf_abiver = 0; /* Current ABI version */
126 extern struct ofmt of_elf64;
128 static struct ELF_SECTDATA {
129 void *data;
130 int64_t len;
131 bool is_saa;
132 } *elf_sects;
133 static int elf_nsect, nsections;
134 static int64_t elf_foffs;
136 static void elf_write(void);
137 static void elf_sect_write(struct Section *, const void *, size_t);
138 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
139 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
140 int, int);
141 static void elf_write_sections(void);
142 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
143 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
144 static void add_sectname(char *, char *);
146 struct erel {
147 int offset, info;
150 struct symlininfo {
151 int offset;
152 int section; /* index into sects[] */
153 int segto; /* internal section number */
154 char *name; /* shallow-copied pointer of section name */
157 struct linelist {
158 struct symlininfo info;
159 int line;
160 char *filename;
161 struct linelist *next;
162 struct linelist *last;
165 struct sectlist {
166 struct SAA *psaa;
167 int section;
168 int line;
169 int offset;
170 int file;
171 struct sectlist *next;
172 struct sectlist *last;
175 /* common debug variables */
176 static int currentline = 1;
177 static int debug_immcall = 0;
179 /* stabs debug variables */
180 static struct linelist *stabslines = 0;
181 static int numlinestabs = 0;
182 static char *stabs_filename = 0;
183 static int symtabsection;
184 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
185 static int stablen, stabstrlen, stabrellen;
187 /* dwarf debug variables */
188 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
189 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
190 static int dwarf_numfiles = 0, dwarf_nsections;
191 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
192 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
193 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
194 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
195 abbrevlen, linelen, linerellen, framelen, loclen;
196 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
199 static struct dfmt df_dwarf;
200 static struct dfmt df_stabs;
201 static struct Symbol *lastsym;
203 /* common debugging routines */
204 static void debug64_typevalue(int32_t);
205 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
206 static void debug64_directive(const char *, const char *);
208 /* stabs debugging routines */
209 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
210 static void stabs64_output(int, void *);
211 static void stabs64_generate(void);
212 static void stabs64_cleanup(void);
214 /* dwarf debugging routines */
215 static void dwarf64_init(void);
216 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
217 static void dwarf64_output(int, void *);
218 static void dwarf64_generate(void);
219 static void dwarf64_cleanup(void);
220 static void dwarf64_findfile(const char *);
221 static void dwarf64_findsect(const int);
224 * Special section numbers which are used to define ELF special
225 * symbols, which can be used with WRT to provide PIC relocation
226 * types.
228 static int32_t elf_gotpc_sect, elf_gotoff_sect;
229 static int32_t elf_got_sect, elf_plt_sect;
230 static int32_t elf_sym_sect;
231 static int32_t elf_gottpoff_sect;
233 static void elf_init(void)
235 maxbits = 64;
236 sects = NULL;
237 nsects = sectlen = 0;
238 syms = saa_init((int32_t)sizeof(struct Symbol));
239 nlocals = nglobs = ndebugs = 0;
240 bsym = raa_init();
241 strs = saa_init(1L);
242 saa_wbytes(strs, "\0", 1L);
243 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
244 strslen = 2 + strlen(elf_module);
245 shstrtab = NULL;
246 shstrtablen = shstrtabsize = 0;;
247 add_sectname("", "");
249 fwds = NULL;
251 elf_gotpc_sect = seg_alloc();
252 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
253 elf_gotoff_sect = seg_alloc();
254 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
255 elf_got_sect = seg_alloc();
256 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
257 elf_plt_sect = seg_alloc();
258 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
259 elf_sym_sect = seg_alloc();
260 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
261 elf_gottpoff_sect = seg_alloc();
262 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
264 def_seg = seg_alloc();
268 static void elf_cleanup(int debuginfo)
270 struct Reloc *r;
271 int i;
273 (void)debuginfo;
275 elf_write();
276 for (i = 0; i < nsects; i++) {
277 if (sects[i]->type != SHT_NOBITS)
278 saa_free(sects[i]->data);
279 if (sects[i]->head)
280 saa_free(sects[i]->rel);
281 while (sects[i]->head) {
282 r = sects[i]->head;
283 sects[i]->head = sects[i]->head->next;
284 nasm_free(r);
287 nasm_free(sects);
288 saa_free(syms);
289 raa_free(bsym);
290 saa_free(strs);
291 if (of_elf64.current_dfmt) {
292 of_elf64.current_dfmt->cleanup();
296 /* add entry to the elf .shstrtab section */
297 static void add_sectname(char *firsthalf, char *secondhalf)
299 int len = strlen(firsthalf) + strlen(secondhalf);
300 while (shstrtablen + len + 1 > shstrtabsize)
301 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
302 strcpy(shstrtab + shstrtablen, firsthalf);
303 strcat(shstrtab + shstrtablen, secondhalf);
304 shstrtablen += len + 1;
307 static int elf_make_section(char *name, int type, int flags, int align)
309 struct Section *s;
311 s = nasm_zalloc(sizeof(*s));
313 if (type != SHT_NOBITS)
314 s->data = saa_init(1L);
315 s->tail = &s->head;
316 if (!strcmp(name, ".text"))
317 s->index = def_seg;
318 else
319 s->index = seg_alloc();
320 add_sectname("", name);
322 s->name = nasm_strdup(name);
323 s->type = type;
324 s->flags = flags;
325 s->align = align;
327 if (nsects >= sectlen)
328 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
329 sects[nsects++] = s;
331 return nsects - 1;
334 static int32_t elf_section_names(char *name, int pass, int *bits)
336 char *p;
337 uint32_t flags, flags_and, flags_or;
338 uint64_t align;
339 int type, i;
342 * Default is 64 bits.
344 if (!name) {
345 *bits = 64;
346 return def_seg;
349 p = nasm_skip_word(name);
350 if (*p)
351 *p++ = '\0';
352 flags_and = flags_or = type = align = 0;
354 section_attrib(name, p, pass, &flags_and,
355 &flags_or, &align, &type);
357 if (!strcmp(name, ".shstrtab") ||
358 !strcmp(name, ".symtab") ||
359 !strcmp(name, ".strtab")) {
360 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
361 "name `%s'", name);
362 return NO_SEG;
365 for (i = 0; i < nsects; i++)
366 if (!strcmp(name, sects[i]->name))
367 break;
368 if (i == nsects) {
369 const struct elf_known_section *ks = elf_known_sections;
371 while (ks->name) {
372 if (!strcmp(name, ks->name))
373 break;
374 ks++;
377 type = type ? type : ks->type;
378 align = align ? align : ks->align;
379 flags = (ks->flags & ~flags_and) | flags_or;
381 i = elf_make_section(name, type, flags, align);
382 } else if (pass == 1) {
383 if ((type && sects[i]->type != type)
384 || (align && sects[i]->align != align)
385 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
386 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
387 " redeclaration of section `%s'", name);
390 return sects[i]->index;
393 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
394 int is_global, char *special)
396 int pos = strslen;
397 struct Symbol *sym;
398 bool special_used = false;
400 #if defined(DEBUG) && DEBUG>2
401 nasm_error(ERR_DEBUG,
402 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
403 name, segment, offset, is_global, special);
404 #endif
405 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
407 * This is a NASM special symbol. We never allow it into
408 * the ELF symbol table, even if it's a valid one. If it
409 * _isn't_ a valid one, we should barf immediately.
411 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
412 strcmp(name, "..got") && strcmp(name, "..plt") &&
413 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
414 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
415 return;
418 if (is_global == 3) {
419 struct Symbol **s;
421 * Fix up a forward-reference symbol size from the first
422 * pass.
424 for (s = &fwds; *s; s = &(*s)->nextfwd)
425 if (!strcmp((*s)->name, name)) {
426 struct tokenval tokval;
427 expr *e;
428 char *p = nasm_skip_spaces(nasm_skip_word(special));
430 stdscan_reset();
431 stdscan_set(p);
432 tokval.t_type = TOKEN_INVALID;
433 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
434 if (e) {
435 if (!is_simple(e))
436 nasm_error(ERR_NONFATAL, "cannot use relocatable"
437 " expression as symbol size");
438 else
439 (*s)->size = reloc_value(e);
443 * Remove it from the list of unresolved sizes.
445 nasm_free((*s)->name);
446 *s = (*s)->nextfwd;
447 return;
449 return; /* it wasn't an important one */
452 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
453 strslen += 1 + strlen(name);
455 lastsym = sym = saa_wstruct(syms);
457 memset(&sym->symv, 0, sizeof(struct rbtree));
459 sym->strpos = pos;
460 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
461 sym->other = STV_DEFAULT;
462 sym->size = 0;
463 if (segment == NO_SEG)
464 sym->section = SHN_ABS;
465 else {
466 int i;
467 sym->section = SHN_UNDEF;
468 if (segment == def_seg) {
469 /* we have to be sure at least text section is there */
470 int tempint;
471 if (segment != elf_section_names(".text", 2, &tempint))
472 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
474 for (i = 0; i < nsects; i++) {
475 if (segment == sects[i]->index) {
476 sym->section = i + 1;
477 break;
482 if (is_global == 2) {
483 sym->size = offset;
484 sym->symv.key = 0;
485 sym->section = SHN_COMMON;
487 * We have a common variable. Check the special text to see
488 * if it's a valid number and power of two; if so, store it
489 * as the alignment for the common variable.
491 if (special) {
492 bool err;
493 sym->symv.key = readnum(special, &err);
494 if (err)
495 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
496 " valid number", special);
497 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
498 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
499 " power of two", special);
501 special_used = true;
502 } else
503 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
505 if (sym->type == SYM_GLOBAL) {
507 * If sym->section == SHN_ABS, then the first line of the
508 * else section would cause a core dump, because its a reference
509 * beyond the end of the section array.
510 * This behaviour is exhibited by this code:
511 * GLOBAL crash_nasm
512 * crash_nasm equ 0
513 * To avoid such a crash, such requests are silently discarded.
514 * This may not be the best solution.
516 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
517 bsym = raa_write(bsym, segment, nglobs);
518 } else if (sym->section != SHN_ABS) {
520 * This is a global symbol; so we must add it to the rbtree
521 * of global symbols in its section.
523 * In addition, we check the special text for symbol
524 * type and size information.
526 sects[sym->section-1]->gsyms =
527 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
529 if (special) {
530 int n = strcspn(special, " \t");
532 if (!nasm_strnicmp(special, "function", n))
533 sym->type |= STT_FUNC;
534 else if (!nasm_strnicmp(special, "data", n) ||
535 !nasm_strnicmp(special, "object", n))
536 sym->type |= STT_OBJECT;
537 else if (!nasm_strnicmp(special, "notype", n))
538 sym->type |= STT_NOTYPE;
539 else
540 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
541 n, special);
542 special += n;
544 special = nasm_skip_spaces(special);
545 if (*special) {
546 n = strcspn(special, " \t");
547 if (!nasm_strnicmp(special, "default", n))
548 sym->other = STV_DEFAULT;
549 else if (!nasm_strnicmp(special, "internal", n))
550 sym->other = STV_INTERNAL;
551 else if (!nasm_strnicmp(special, "hidden", n))
552 sym->other = STV_HIDDEN;
553 else if (!nasm_strnicmp(special, "protected", n))
554 sym->other = STV_PROTECTED;
555 else
556 n = 0;
557 special += n;
560 if (*special) {
561 struct tokenval tokval;
562 expr *e;
563 int fwd = 0;
564 char *saveme = stdscan_get();
566 while (special[n] && nasm_isspace(special[n]))
567 n++;
569 * We have a size expression; attempt to
570 * evaluate it.
572 stdscan_reset();
573 stdscan_set(special + n);
574 tokval.t_type = TOKEN_INVALID;
575 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
576 NULL);
577 if (fwd) {
578 sym->nextfwd = fwds;
579 fwds = sym;
580 sym->name = nasm_strdup(name);
581 } else if (e) {
582 if (!is_simple(e))
583 nasm_error(ERR_NONFATAL, "cannot use relocatable"
584 " expression as symbol size");
585 else
586 sym->size = reloc_value(e);
588 stdscan_set(saveme);
590 special_used = true;
593 * If TLS segment, mark symbol accordingly.
595 if (sects[sym->section - 1]->flags & SHF_TLS) {
596 sym->type &= 0xf0;
597 sym->type |= STT_TLS;
600 sym->globnum = nglobs;
601 nglobs++;
602 } else
603 nlocals++;
605 if (special && !special_used)
606 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
609 static void elf_add_reloc(struct Section *sect, int32_t segment,
610 int64_t offset, int type)
612 struct Reloc *r;
614 r = *sect->tail = nasm_zalloc(sizeof(struct Reloc));
615 sect->tail = &r->next;
617 r->address = sect->len;
618 r->offset = offset;
620 if (segment != NO_SEG) {
621 int i;
622 for (i = 0; i < nsects; i++)
623 if (segment == sects[i]->index)
624 r->symbol = i + 2;
625 if (!r->symbol)
626 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
628 r->type = type;
630 sect->nrelocs++;
634 * This routine deals with ..got and ..sym relocations: the more
635 * complicated kinds. In shared-library writing, some relocations
636 * with respect to global symbols must refer to the precise symbol
637 * rather than referring to an offset from the base of the section
638 * _containing_ the symbol. Such relocations call to this routine,
639 * which searches the symbol list for the symbol in question.
641 * R_386_GOT32 references require the _exact_ symbol address to be
642 * used; R_386_32 references can be at an offset from the symbol.
643 * The boolean argument `exact' tells us this.
645 * Return value is the adjusted value of `addr', having become an
646 * offset from the symbol rather than the section. Should always be
647 * zero when returning from an exact call.
649 * Limitation: if you define two symbols at the same place,
650 * confusion will occur.
652 * Inefficiency: we search, currently, using a linked list which
653 * isn't even necessarily sorted.
655 static void elf_add_gsym_reloc(struct Section *sect,
656 int32_t segment, uint64_t offset, int64_t pcrel,
657 int type, bool exact)
659 struct Reloc *r;
660 struct Section *s;
661 struct Symbol *sym;
662 struct rbtree *srb;
663 int i;
666 * First look up the segment/offset pair and find a global
667 * symbol corresponding to it. If it's not one of our segments,
668 * then it must be an external symbol, in which case we're fine
669 * doing a normal elf_add_reloc after first sanity-checking
670 * that the offset from the symbol is zero.
672 s = NULL;
673 for (i = 0; i < nsects; i++)
674 if (segment == sects[i]->index) {
675 s = sects[i];
676 break;
679 if (!s) {
680 if (exact && offset)
681 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
682 else
683 elf_add_reloc(sect, segment, offset - pcrel, type);
684 return;
687 srb = rb_search(s->gsyms, offset);
688 if (!srb || (exact && srb->key != offset)) {
689 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
690 " for this reference");
691 return;
693 sym = container_of(srb, struct Symbol, symv);
695 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
696 sect->tail = &r->next;
697 r->next = NULL;
699 r->address = sect->len;
700 r->offset = offset - pcrel - sym->symv.key;
701 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
702 r->type = type;
704 sect->nrelocs++;
707 static void elf_out(int32_t segto, const void *data,
708 enum out_type type, uint64_t size,
709 int32_t segment, int32_t wrt)
711 struct Section *s;
712 int64_t addr, zero;
713 int reltype, bytes;
714 int i;
715 static struct symlininfo sinfo;
717 zero = 0;
719 #if defined(DEBUG) && DEBUG>2
720 if (data)
721 nasm_error(ERR_DEBUG,
722 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
723 currentline, type, segment, segto, size, *(int64_t *)data);
724 else
725 nasm_error(ERR_DEBUG,
726 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
727 currentline, type, segment, segto, size);
728 #endif
731 * handle absolute-assembly (structure definitions)
733 if (segto == NO_SEG) {
734 if (type != OUT_RESERVE)
735 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
736 " space");
737 return;
740 s = NULL;
741 for (i = 0; i < nsects; i++)
742 if (segto == sects[i]->index) {
743 s = sects[i];
744 break;
746 if (!s) {
747 int tempint; /* ignored */
748 if (segto != elf_section_names(".text", 2, &tempint))
749 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
750 else {
751 s = sects[nsects - 1];
752 i = nsects - 1;
756 /* again some stabs debugging stuff */
757 if (of_elf64.current_dfmt) {
758 sinfo.offset = s->len;
759 sinfo.section = i;
760 sinfo.segto = segto;
761 sinfo.name = s->name;
762 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
764 /* end of debugging stuff */
766 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
767 nasm_error(ERR_WARNING, "attempt to initialize memory in"
768 " BSS section `%s': ignored", s->name);
769 s->len += realsize(type, size);
770 return;
773 switch (type) {
774 case OUT_RESERVE:
775 if (s->type == SHT_PROGBITS) {
776 nasm_error(ERR_WARNING, "uninitialized space declared in"
777 " non-BSS section `%s': zeroing", s->name);
778 elf_sect_write(s, NULL, size);
779 } else
780 s->len += size;
781 break;
783 case OUT_RAWDATA:
784 if (segment != NO_SEG)
785 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
786 elf_sect_write(s, data, size);
787 break;
789 case OUT_ADDRESS:
790 addr = *(int64_t *)data;
791 if (segment == NO_SEG) {
792 /* Do nothing */
793 } else if (segment % 2) {
794 nasm_error(ERR_NONFATAL, "ELF format does not support"
795 " segment base references");
796 } else {
797 if (wrt == NO_SEG) {
798 switch ((int)size) {
799 case 1:
800 elf_add_reloc(s, segment, addr, R_X86_64_8);
801 break;
802 case 2:
803 elf_add_reloc(s, segment, addr, R_X86_64_16);
804 break;
805 case 4:
806 elf_add_reloc(s, segment, addr, R_X86_64_32);
807 break;
808 case 8:
809 elf_add_reloc(s, segment, addr, R_X86_64_64);
810 break;
811 default:
812 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
813 break;
815 addr = 0;
816 } else if (wrt == elf_gotpc_sect + 1) {
818 * The user will supply GOT relative to $$. ELF
819 * will let us have GOT relative to $. So we
820 * need to fix up the data item by $-$$.
822 addr += s->len;
823 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
824 addr = 0;
825 } else if (wrt == elf_gotoff_sect + 1) {
826 if (size != 8) {
827 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
828 "references to be qword");
829 } else {
830 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
831 addr = 0;
833 } else if (wrt == elf_got_sect + 1) {
834 switch ((int)size) {
835 case 4:
836 elf_add_gsym_reloc(s, segment, addr, 0,
837 R_X86_64_GOT32, true);
838 addr = 0;
839 break;
840 case 8:
841 elf_add_gsym_reloc(s, segment, addr, 0,
842 R_X86_64_GOT64, true);
843 addr = 0;
844 break;
845 default:
846 nasm_error(ERR_NONFATAL, "invalid ..got reference");
847 break;
849 } else if (wrt == elf_sym_sect + 1) {
850 switch ((int)size) {
851 case 1:
852 elf_add_gsym_reloc(s, segment, addr, 0,
853 R_X86_64_8, false);
854 addr = 0;
855 break;
856 case 2:
857 elf_add_gsym_reloc(s, segment, addr, 0,
858 R_X86_64_16, false);
859 addr = 0;
860 break;
861 case 4:
862 elf_add_gsym_reloc(s, segment, addr, 0,
863 R_X86_64_32, false);
864 addr = 0;
865 break;
866 case 8:
867 elf_add_gsym_reloc(s, segment, addr, 0,
868 R_X86_64_64, false);
869 addr = 0;
870 break;
871 default:
872 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
873 break;
875 } else if (wrt == elf_plt_sect + 1) {
876 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
877 "relative PLT references");
878 } else {
879 nasm_error(ERR_NONFATAL, "ELF format does not support this"
880 " use of WRT");
883 elf_sect_writeaddr(s, addr, size);
884 break;
886 case OUT_REL1ADR:
887 reltype = R_X86_64_PC8;
888 bytes = 1;
889 goto rel12adr;
891 case OUT_REL2ADR:
892 reltype = R_X86_64_PC16;
893 bytes = 2;
894 goto rel12adr;
896 rel12adr:
897 addr = *(int64_t *)data - size;
898 if (segment == segto)
899 nasm_error(ERR_PANIC, "intra-segment OUT_REL1ADR");
900 if (segment == NO_SEG) {
901 /* Do nothing */
902 } else if (segment % 2) {
903 nasm_error(ERR_NONFATAL, "ELF format does not support"
904 " segment base references");
905 } else {
906 if (wrt == NO_SEG) {
907 elf_add_reloc(s, segment, addr, reltype);
908 addr = 0;
909 } else {
910 nasm_error(ERR_NONFATAL,
911 "Unsupported non-32-bit ELF relocation");
914 elf_sect_writeaddr(s, addr, bytes);
915 break;
917 case OUT_REL4ADR:
918 addr = *(int64_t *)data - size;
919 if (segment == segto)
920 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
921 if (segment == NO_SEG) {
922 /* Do nothing */
923 } else if (segment % 2) {
924 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
925 " segment base references");
926 } else {
927 if (wrt == NO_SEG) {
928 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
929 addr = 0;
930 } else if (wrt == elf_plt_sect + 1) {
931 elf_add_gsym_reloc(s, segment, addr+size, size,
932 R_X86_64_PLT32, true);
933 addr = 0;
934 } else if (wrt == elf_gotpc_sect + 1 ||
935 wrt == elf_got_sect + 1) {
936 elf_add_gsym_reloc(s, segment, addr+size, size,
937 R_X86_64_GOTPCREL, true);
938 addr = 0;
939 } else if (wrt == elf_gotoff_sect + 1 ||
940 wrt == elf_got_sect + 1) {
941 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
942 "qword absolute");
943 } else if (wrt == elf_gottpoff_sect + 1) {
944 elf_add_gsym_reloc(s, segment, addr+size, size,
945 R_X86_64_GOTTPOFF, true);
946 addr = 0;
947 } else {
948 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
949 " use of WRT");
952 elf_sect_writeaddr(s, addr, 4);
953 break;
955 case OUT_REL8ADR:
956 addr = *(int64_t *)data - size;
957 if (segment == segto)
958 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
959 if (segment == NO_SEG) {
960 /* Do nothing */
961 } else if (segment % 2) {
962 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
963 " segment base references");
964 } else {
965 if (wrt == NO_SEG) {
966 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
967 addr = 0;
968 } else if (wrt == elf_gotpc_sect + 1 ||
969 wrt == elf_got_sect + 1) {
970 elf_add_gsym_reloc(s, segment, addr+size, size,
971 R_X86_64_GOTPCREL64, true);
972 addr = 0;
973 } else if (wrt == elf_gotoff_sect + 1 ||
974 wrt == elf_got_sect + 1) {
975 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
976 "absolute");
977 } else if (wrt == elf_gottpoff_sect + 1) {
978 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
979 "dword");
980 } else {
981 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
982 " use of WRT");
985 elf_sect_writeaddr(s, addr, 8);
986 break;
990 static void elf_write(void)
992 int align;
993 char *p;
994 int i;
996 struct SAA *symtab;
997 int32_t symtablen, symtablocal;
1000 * Work out how many sections we will have. We have SHN_UNDEF,
1001 * then the flexible user sections, then the fixed sections
1002 * `.shstrtab', `.symtab' and `.strtab', then optionally
1003 * relocation sections for the user sections.
1005 nsections = sec_numspecial + 1;
1006 if (of_elf64.current_dfmt == &df_stabs)
1007 nsections += 3;
1008 else if (of_elf64.current_dfmt == &df_dwarf)
1009 nsections += 10;
1011 add_sectname("", ".shstrtab");
1012 add_sectname("", ".symtab");
1013 add_sectname("", ".strtab");
1014 for (i = 0; i < nsects; i++) {
1015 nsections++; /* for the section itself */
1016 if (sects[i]->head) {
1017 nsections++; /* for its relocations */
1018 add_sectname(".rela", sects[i]->name);
1022 if (of_elf64.current_dfmt == &df_stabs) {
1023 /* in case the debug information is wanted, just add these three sections... */
1024 add_sectname("", ".stab");
1025 add_sectname("", ".stabstr");
1026 add_sectname(".rel", ".stab");
1029 else if (of_elf64.current_dfmt == &df_dwarf) {
1030 /* the dwarf debug standard specifies the following ten sections,
1031 not all of which are currently implemented,
1032 although all of them are defined. */
1033 #define debug_aranges (int64_t) (nsections-10)
1034 #define debug_info (int64_t) (nsections-7)
1035 #define debug_abbrev (int64_t) (nsections-5)
1036 #define debug_line (int64_t) (nsections-4)
1037 add_sectname("", ".debug_aranges");
1038 add_sectname(".rela", ".debug_aranges");
1039 add_sectname("", ".debug_pubnames");
1040 add_sectname("", ".debug_info");
1041 add_sectname(".rela", ".debug_info");
1042 add_sectname("", ".debug_abbrev");
1043 add_sectname("", ".debug_line");
1044 add_sectname(".rela", ".debug_line");
1045 add_sectname("", ".debug_frame");
1046 add_sectname("", ".debug_loc");
1050 * Output the ELF header.
1052 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1053 fputc(elf_osabi, ofile);
1054 fputc(elf_abiver, ofile);
1055 fwritezero(7, ofile);
1056 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1057 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1058 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1059 fwriteint64_t(0L, ofile); /* no entry point */
1060 fwriteint64_t(0L, ofile); /* no program header table */
1061 fwriteint64_t(0x40L, ofile); /* section headers straight after
1062 * ELF header plus alignment */
1063 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1064 fwriteint16_t(0x40, ofile); /* size of ELF header */
1065 fwriteint16_t(0, ofile); /* no program header table, again */
1066 fwriteint16_t(0, ofile); /* still no program header table */
1067 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1068 fwriteint16_t(nsections, ofile); /* number of sections */
1069 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1070 * section header table */
1073 * Build the symbol table and relocation tables.
1075 symtab = elf_build_symtab(&symtablen, &symtablocal);
1076 for (i = 0; i < nsects; i++)
1077 if (sects[i]->head)
1078 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1079 sects[i]->head);
1082 * Now output the section header table.
1085 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1086 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1087 elf_foffs += align;
1088 elf_nsect = 0;
1089 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1091 /* SHN_UNDEF */
1092 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1093 p = shstrtab + 1;
1095 /* The normal sections */
1096 for (i = 0; i < nsects; i++) {
1097 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1098 (sects[i]->type == SHT_PROGBITS ?
1099 sects[i]->data : NULL), true,
1100 sects[i]->len, 0, 0, sects[i]->align, 0);
1101 p += strlen(p) + 1;
1104 /* .shstrtab */
1105 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1106 shstrtablen, 0, 0, 1, 0);
1107 p += strlen(p) + 1;
1109 /* .symtab */
1110 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1111 symtablen, sec_strtab, symtablocal, 4, 24);
1112 p += strlen(p) + 1;
1114 /* .strtab */
1115 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1116 strslen, 0, 0, 1, 0);
1117 p += strlen(p) + 1;
1119 /* The relocation sections */
1120 for (i = 0; i < nsects; i++)
1121 if (sects[i]->head) {
1122 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1123 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1124 p += strlen(p) + 1;
1127 if (of_elf64.current_dfmt == &df_stabs) {
1128 /* for debugging information, create the last three sections
1129 which are the .stab , .stabstr and .rel.stab sections respectively */
1131 /* this function call creates the stab sections in memory */
1132 stabs64_generate();
1134 if (stabbuf && stabstrbuf && stabrelbuf) {
1135 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1136 stablen, sec_stabstr, 0, 4, 12);
1137 p += strlen(p) + 1;
1139 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1140 stabstrlen, 0, 0, 4, 0);
1141 p += strlen(p) + 1;
1143 /* link -> symtable info -> section to refer to */
1144 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1145 stabrellen, symtabsection, sec_stab, 4, 16);
1146 p += strlen(p) + 1;
1148 } else if (of_elf64.current_dfmt == &df_dwarf) {
1149 /* for dwarf debugging information, create the ten dwarf sections */
1151 /* this function call creates the dwarf sections in memory */
1152 if (dwarf_fsect)
1153 dwarf64_generate();
1155 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1156 arangeslen, 0, 0, 1, 0);
1157 p += strlen(p) + 1;
1159 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1160 arangesrellen, symtabsection, debug_aranges, 1, 24);
1161 p += strlen(p) + 1;
1163 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1164 pubnameslen, 0, 0, 1, 0);
1165 p += strlen(p) + 1;
1167 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1168 infolen, 0, 0, 1, 0);
1169 p += strlen(p) + 1;
1171 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1172 inforellen, symtabsection, debug_info, 1, 24);
1173 p += strlen(p) + 1;
1175 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1176 abbrevlen, 0, 0, 1, 0);
1177 p += strlen(p) + 1;
1179 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1180 linelen, 0, 0, 1, 0);
1181 p += strlen(p) + 1;
1183 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1184 linerellen, symtabsection, debug_line, 1, 24);
1185 p += strlen(p) + 1;
1187 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1188 framelen, 0, 0, 8, 0);
1189 p += strlen(p) + 1;
1191 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1192 loclen, 0, 0, 1, 0);
1193 p += strlen(p) + 1;
1195 fwritezero(align, ofile);
1198 * Now output the sections.
1200 elf_write_sections();
1202 nasm_free(elf_sects);
1203 saa_free(symtab);
1206 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1208 struct SAA *s = saa_init(1L);
1209 struct Symbol *sym;
1210 uint8_t entry[24], *p;
1211 int i;
1213 *len = *local = 0;
1216 * First, an all-zeros entry, required by the ELF spec.
1218 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1219 *len += 24;
1220 (*local)++;
1223 * Next, an entry for the file name.
1225 p = entry;
1226 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1227 WRITESHORT(p, STT_FILE); /* type FILE */
1228 WRITESHORT(p, SHN_ABS);
1229 WRITEDLONG(p, (uint64_t) 0); /* no value */
1230 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1231 saa_wbytes(s, entry, 24L);
1232 *len += 24;
1233 (*local)++;
1236 * Now some standard symbols defining the segments, for relocation
1237 * purposes.
1239 for (i = 1; i <= nsects; i++) {
1240 p = entry;
1241 WRITELONG(p, 0); /* no symbol name */
1242 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1243 WRITESHORT(p, i); /* section id */
1244 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1245 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1246 saa_wbytes(s, entry, 24L);
1247 *len += 24;
1248 (*local)++;
1253 * Now the other local symbols.
1255 saa_rewind(syms);
1256 while ((sym = saa_rstruct(syms))) {
1257 if (sym->type & SYM_GLOBAL)
1258 continue;
1259 p = entry;
1260 WRITELONG(p, sym->strpos); /* index into symbol string table */
1261 WRITECHAR(p, sym->type); /* type and binding */
1262 WRITECHAR(p, sym->other); /* visibility */
1263 WRITESHORT(p, sym->section); /* index into section header table */
1264 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1265 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1266 saa_wbytes(s, entry, 24L);
1267 *len += 24;
1268 (*local)++;
1271 * dwarf needs symbols for debug sections
1272 * which are relocation targets.
1274 if (of_elf64.current_dfmt == &df_dwarf) {
1275 dwarf_infosym = *local;
1276 p = entry;
1277 WRITELONG(p, 0); /* no symbol name */
1278 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1279 WRITESHORT(p, debug_info); /* section id */
1280 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1281 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1282 saa_wbytes(s, entry, 24L);
1283 *len += 24;
1284 (*local)++;
1285 dwarf_abbrevsym = *local;
1286 p = entry;
1287 WRITELONG(p, 0); /* no symbol name */
1288 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1289 WRITESHORT(p, debug_abbrev); /* section id */
1290 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1291 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1292 saa_wbytes(s, entry, 24L);
1293 *len += 24;
1294 (*local)++;
1295 dwarf_linesym = *local;
1296 p = entry;
1297 WRITELONG(p, 0); /* no symbol name */
1298 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1299 WRITESHORT(p, debug_line); /* section id */
1300 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1301 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1302 saa_wbytes(s, entry, 24L);
1303 *len += 24;
1304 (*local)++;
1308 * Now the global symbols.
1310 saa_rewind(syms);
1311 while ((sym = saa_rstruct(syms))) {
1312 if (!(sym->type & SYM_GLOBAL))
1313 continue;
1314 p = entry;
1315 WRITELONG(p, sym->strpos);
1316 WRITECHAR(p, sym->type); /* type and binding */
1317 WRITECHAR(p, sym->other); /* visibility */
1318 WRITESHORT(p, sym->section);
1319 WRITEDLONG(p, (int64_t)sym->symv.key);
1320 WRITEDLONG(p, (int64_t)sym->size);
1321 saa_wbytes(s, entry, 24L);
1322 *len += 24;
1325 return s;
1328 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1330 struct SAA *s;
1331 uint8_t *p, entry[24];
1332 int32_t global_offset;
1334 if (!r)
1335 return NULL;
1337 s = saa_init(1L);
1338 *len = 0;
1341 * How to onvert from a global placeholder to a real symbol index;
1342 * the +2 refers to the two special entries, the null entry and
1343 * the filename entry.
1345 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1347 while (r) {
1348 int32_t sym = r->symbol;
1350 if (sym >= GLOBAL_TEMP_BASE)
1351 sym += global_offset;
1353 p = entry;
1354 WRITEDLONG(p, r->address);
1355 WRITELONG(p, r->type);
1356 WRITELONG(p, sym);
1357 WRITEDLONG(p, r->offset);
1358 saa_wbytes(s, entry, 24L);
1359 *len += 24;
1361 r = r->next;
1364 return s;
1367 static void elf_section_header(int name, int type, uint64_t flags,
1368 void *data, bool is_saa, uint64_t datalen,
1369 int link, int info, int align, int eltsize)
1371 elf_sects[elf_nsect].data = data;
1372 elf_sects[elf_nsect].len = datalen;
1373 elf_sects[elf_nsect].is_saa = is_saa;
1374 elf_nsect++;
1376 fwriteint32_t((int32_t)name, ofile);
1377 fwriteint32_t((int32_t)type, ofile);
1378 fwriteint64_t((int64_t)flags, ofile);
1379 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1380 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1381 fwriteint64_t(datalen, ofile);
1382 if (data)
1383 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1384 fwriteint32_t((int32_t)link, ofile);
1385 fwriteint32_t((int32_t)info, ofile);
1386 fwriteint64_t((int64_t)align, ofile);
1387 fwriteint64_t((int64_t)eltsize, ofile);
1390 static void elf_write_sections(void)
1392 int i;
1393 for (i = 0; i < elf_nsect; i++)
1394 if (elf_sects[i].data) {
1395 int32_t len = elf_sects[i].len;
1396 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1397 int32_t align = reallen - len;
1398 if (elf_sects[i].is_saa)
1399 saa_fpwrite(elf_sects[i].data, ofile);
1400 else
1401 fwrite(elf_sects[i].data, len, 1, ofile);
1402 fwritezero(align, ofile);
1406 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1408 saa_wbytes(sect->data, data, len);
1409 sect->len += len;
1411 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1413 saa_writeaddr(sect->data, data, len);
1414 sect->len += len;
1417 static void elf_sectalign(int32_t seg, unsigned int value)
1419 struct Section *s = NULL;
1420 int i;
1422 for (i = 0; i < nsects; i++) {
1423 if (sects[i]->index == seg) {
1424 s = sects[i];
1425 break;
1428 if (!s || !is_power2(value))
1429 return;
1431 if (value > s->align)
1432 s->align = value;
1435 static int32_t elf_segbase(int32_t segment)
1437 return segment;
1440 static int elf_directive(enum directives directive, char *value, int pass)
1442 bool err;
1443 int64_t n;
1444 char *p;
1446 switch (directive) {
1447 case D_OSABI:
1448 if (pass == 2)
1449 return 1; /* ignore in pass 2 */
1451 n = readnum(value, &err);
1452 if (err) {
1453 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1454 return 1;
1456 if (n < 0 || n > 255) {
1457 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1458 return 1;
1460 elf_osabi = n;
1461 elf_abiver = 0;
1463 if ((p = strchr(value,',')) == NULL)
1464 return 1;
1466 n = readnum(p+1, &err);
1467 if (err || n < 0 || n > 255) {
1468 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1469 return 1;
1472 elf_abiver = n;
1473 return 1;
1475 default:
1476 return 0;
1480 static void elf_filename(char *inname, char *outname)
1482 strcpy(elf_module, inname);
1483 standard_extension(inname, outname, ".o");
1486 extern macros_t elf_stdmac[];
1488 static int elf_set_info(enum geninfo type, char **val)
1490 (void)type;
1491 (void)val;
1492 return 0;
1494 static struct dfmt df_dwarf = {
1495 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1496 "dwarf",
1497 dwarf64_init,
1498 dwarf64_linenum,
1499 debug64_deflabel,
1500 debug64_directive,
1501 debug64_typevalue,
1502 dwarf64_output,
1503 dwarf64_cleanup
1505 static struct dfmt df_stabs = {
1506 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1507 "stabs",
1508 null_debug_init,
1509 stabs64_linenum,
1510 debug64_deflabel,
1511 debug64_directive,
1512 debug64_typevalue,
1513 stabs64_output,
1514 stabs64_cleanup
1517 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1519 struct ofmt of_elf64 = {
1520 "ELF64 (x86_64) object files (e.g. Linux)",
1521 "elf64",
1523 elf64_debugs_arr,
1524 &df_stabs,
1525 elf_stdmac,
1526 elf_init,
1527 elf_set_info,
1528 elf_out,
1529 elf_deflabel,
1530 elf_section_names,
1531 elf_sectalign,
1532 elf_segbase,
1533 elf_directive,
1534 elf_filename,
1535 elf_cleanup
1538 /* common debugging routines */
1539 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1540 int is_global, char *special)
1542 (void)name;
1543 (void)segment;
1544 (void)offset;
1545 (void)is_global;
1546 (void)special;
1549 static void debug64_directive(const char *directive, const char *params)
1551 (void)directive;
1552 (void)params;
1555 static void debug64_typevalue(int32_t type)
1557 int32_t stype, ssize;
1558 switch (TYM_TYPE(type)) {
1559 case TY_LABEL:
1560 ssize = 0;
1561 stype = STT_NOTYPE;
1562 break;
1563 case TY_BYTE:
1564 ssize = 1;
1565 stype = STT_OBJECT;
1566 break;
1567 case TY_WORD:
1568 ssize = 2;
1569 stype = STT_OBJECT;
1570 break;
1571 case TY_DWORD:
1572 ssize = 4;
1573 stype = STT_OBJECT;
1574 break;
1575 case TY_FLOAT:
1576 ssize = 4;
1577 stype = STT_OBJECT;
1578 break;
1579 case TY_QWORD:
1580 ssize = 8;
1581 stype = STT_OBJECT;
1582 break;
1583 case TY_TBYTE:
1584 ssize = 10;
1585 stype = STT_OBJECT;
1586 break;
1587 case TY_OWORD:
1588 ssize = 16;
1589 stype = STT_OBJECT;
1590 break;
1591 case TY_YWORD:
1592 ssize = 32;
1593 stype = STT_OBJECT;
1594 break;
1595 case TY_COMMON:
1596 ssize = 0;
1597 stype = STT_COMMON;
1598 break;
1599 case TY_SEG:
1600 ssize = 0;
1601 stype = STT_SECTION;
1602 break;
1603 case TY_EXTERN:
1604 ssize = 0;
1605 stype = STT_NOTYPE;
1606 break;
1607 case TY_EQU:
1608 ssize = 0;
1609 stype = STT_NOTYPE;
1610 break;
1611 default:
1612 ssize = 0;
1613 stype = STT_NOTYPE;
1614 break;
1616 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1617 lastsym->size = ssize;
1618 lastsym->type = stype;
1622 /* stabs debugging routines */
1624 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1626 (void)segto;
1627 if (!stabs_filename) {
1628 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1629 strcpy(stabs_filename, filename);
1630 } else {
1631 if (strcmp(stabs_filename, filename)) {
1632 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1633 in fact, this leak comes in quite handy to maintain a list of files
1634 encountered so far in the symbol lines... */
1636 /* why not nasm_free(stabs_filename); we're done with the old one */
1638 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1639 strcpy(stabs_filename, filename);
1642 debug_immcall = 1;
1643 currentline = linenumber;
1647 static void stabs64_output(int type, void *param)
1649 struct symlininfo *s;
1650 struct linelist *el;
1651 if (type == TY_DEBUGSYMLIN) {
1652 if (debug_immcall) {
1653 s = (struct symlininfo *)param;
1654 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1655 return; /* line info is only collected for executable sections */
1656 numlinestabs++;
1657 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1658 el->info.offset = s->offset;
1659 el->info.section = s->section;
1660 el->info.name = s->name;
1661 el->line = currentline;
1662 el->filename = stabs_filename;
1663 el->next = 0;
1664 if (stabslines) {
1665 stabslines->last->next = el;
1666 stabslines->last = el;
1667 } else {
1668 stabslines = el;
1669 stabslines->last = el;
1673 debug_immcall = 0;
1676 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1678 static void stabs64_generate(void)
1680 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1681 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1682 char **allfiles;
1683 int *fileidx;
1685 struct linelist *ptr;
1687 ptr = stabslines;
1689 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1690 numfiles = 0;
1691 while (ptr) {
1692 if (numfiles == 0) {
1693 allfiles[0] = ptr->filename;
1694 numfiles++;
1695 } else {
1696 for (i = 0; i < numfiles; i++) {
1697 if (!strcmp(allfiles[i], ptr->filename))
1698 break;
1700 if (i >= numfiles) {
1701 allfiles[i] = ptr->filename;
1702 numfiles++;
1705 ptr = ptr->next;
1707 strsize = 1;
1708 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1709 for (i = 0; i < numfiles; i++) {
1710 fileidx[i] = strsize;
1711 strsize += strlen(allfiles[i]) + 1;
1713 mainfileindex = 0;
1714 for (i = 0; i < numfiles; i++) {
1715 if (!strcmp(allfiles[i], elf_module)) {
1716 mainfileindex = i;
1717 break;
1722 * worst case size of the stab buffer would be:
1723 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1724 * plus one "ending" entry
1726 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1727 sizeof(struct stabentry));
1728 ssbuf = (uint8_t *)nasm_malloc(strsize);
1729 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1730 rptr = rbuf;
1732 for (i = 0; i < numfiles; i++)
1733 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1734 ssbuf[0] = 0;
1736 stabstrlen = strsize; /* set global variable for length of stab strings */
1738 sptr = sbuf;
1739 ptr = stabslines;
1740 numstabs = 0;
1742 if (ptr) {
1744 * this is the first stab, its strx points to the filename of the
1745 * the source-file, the n_desc field should be set to the number
1746 * of remaining stabs
1748 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1750 /* this is the stab for the main source file */
1751 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1753 /* relocation table entry */
1756 * Since the symbol table has two entries before
1757 * the section symbols, the index in the info.section
1758 * member must be adjusted by adding 2
1761 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1762 WRITELONG(rptr, R_X86_64_32);
1763 WRITELONG(rptr, ptr->info.section + 2);
1765 numstabs++;
1766 currfile = mainfileindex;
1769 while (ptr) {
1770 if (strcmp(allfiles[currfile], ptr->filename)) {
1771 /* oops file has changed... */
1772 for (i = 0; i < numfiles; i++)
1773 if (!strcmp(allfiles[i], ptr->filename))
1774 break;
1775 currfile = i;
1776 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1777 ptr->info.offset);
1778 numstabs++;
1780 /* relocation table entry */
1782 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1783 WRITELONG(rptr, R_X86_64_32);
1784 WRITELONG(rptr, ptr->info.section + 2);
1787 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1788 numstabs++;
1790 /* relocation table entry */
1792 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1793 WRITELONG(rptr, R_X86_64_32);
1794 WRITELONG(rptr, ptr->info.section + 2);
1796 ptr = ptr->next;
1800 /* this is an "ending" token */
1801 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1802 numstabs++;
1804 ((struct stabentry *)sbuf)->n_desc = numstabs;
1806 nasm_free(allfiles);
1807 nasm_free(fileidx);
1809 stablen = (sptr - sbuf);
1810 stabrellen = (rptr - rbuf);
1811 stabrelbuf = rbuf;
1812 stabbuf = sbuf;
1813 stabstrbuf = ssbuf;
1816 static void stabs64_cleanup(void)
1818 struct linelist *ptr, *del;
1819 if (!stabslines)
1820 return;
1822 ptr = stabslines;
1823 while (ptr) {
1824 del = ptr;
1825 ptr = ptr->next;
1826 nasm_free(del);
1829 nasm_free(stabbuf);
1830 nasm_free(stabrelbuf);
1831 nasm_free(stabstrbuf);
1834 /* dwarf routines */
1836 static void dwarf64_init(void)
1838 ndebugs = 3; /* 3 debug symbols */
1841 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1842 int32_t segto)
1844 (void)segto;
1845 dwarf64_findfile(filename);
1846 debug_immcall = 1;
1847 currentline = linenumber;
1850 /* called from elf_out with type == TY_DEBUGSYMLIN */
1851 static void dwarf64_output(int type, void *param)
1853 int ln, aa, inx, maxln, soc;
1854 struct symlininfo *s;
1855 struct SAA *plinep;
1857 (void)type;
1859 s = (struct symlininfo *)param;
1861 /* line number info is only gathered for executable sections */
1862 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1863 return;
1865 /* Check if section index has changed */
1866 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1867 dwarf64_findsect(s->section);
1869 /* do nothing unless line or file has changed */
1870 if (!debug_immcall)
1871 return;
1873 ln = currentline - dwarf_csect->line;
1874 aa = s->offset - dwarf_csect->offset;
1875 inx = dwarf_clist->line;
1876 plinep = dwarf_csect->psaa;
1877 /* check for file change */
1878 if (!(inx == dwarf_csect->file)) {
1879 saa_write8(plinep,DW_LNS_set_file);
1880 saa_write8(plinep,inx);
1881 dwarf_csect->file = inx;
1883 /* check for line change */
1884 if (ln) {
1885 /* test if in range of special op code */
1886 maxln = line_base + line_range;
1887 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1888 if (ln >= line_base && ln < maxln && soc < 256) {
1889 saa_write8(plinep,soc);
1890 } else {
1891 saa_write8(plinep,DW_LNS_advance_line);
1892 saa_wleb128s(plinep,ln);
1893 if (aa) {
1894 saa_write8(plinep,DW_LNS_advance_pc);
1895 saa_wleb128u(plinep,aa);
1898 dwarf_csect->line = currentline;
1899 dwarf_csect->offset = s->offset;
1902 /* show change handled */
1903 debug_immcall = 0;
1907 static void dwarf64_generate(void)
1909 uint8_t *pbuf;
1910 int indx;
1911 struct linelist *ftentry;
1912 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1913 struct SAA *parangesrel, *plinesrel, *pinforel;
1914 struct sectlist *psect;
1915 size_t saalen, linepoff, totlen, highaddr;
1917 /* write epilogues for each line program range */
1918 /* and build aranges section */
1919 paranges = saa_init(1L);
1920 parangesrel = saa_init(1L);
1921 saa_write16(paranges,3); /* dwarf version */
1922 saa_write64(parangesrel, paranges->datalen+4);
1923 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1924 saa_write64(parangesrel, 0);
1925 saa_write32(paranges,0); /* offset into info */
1926 saa_write8(paranges,8); /* pointer size */
1927 saa_write8(paranges,0); /* not segmented */
1928 saa_write32(paranges,0); /* padding */
1929 /* iterate though sectlist entries */
1930 psect = dwarf_fsect;
1931 totlen = 0;
1932 highaddr = 0;
1933 for (indx = 0; indx < dwarf_nsections; indx++)
1935 plinep = psect->psaa;
1936 /* Line Number Program Epilogue */
1937 saa_write8(plinep,2); /* std op 2 */
1938 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1939 saa_write8(plinep,DW_LNS_extended_op);
1940 saa_write8(plinep,1); /* operand length */
1941 saa_write8(plinep,DW_LNE_end_sequence);
1942 totlen += plinep->datalen;
1943 /* range table relocation entry */
1944 saa_write64(parangesrel, paranges->datalen + 4);
1945 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1946 saa_write64(parangesrel, (uint64_t) 0);
1947 /* range table entry */
1948 saa_write64(paranges,0x0000); /* range start */
1949 saa_write64(paranges,sects[psect->section]->len); /* range length */
1950 highaddr += sects[psect->section]->len;
1951 /* done with this entry */
1952 psect = psect->next;
1954 saa_write64(paranges,0); /* null address */
1955 saa_write64(paranges,0); /* null length */
1956 saalen = paranges->datalen;
1957 arangeslen = saalen + 4;
1958 arangesbuf = pbuf = nasm_malloc(arangeslen);
1959 WRITELONG(pbuf,saalen); /* initial length */
1960 saa_rnbytes(paranges, pbuf, saalen);
1961 saa_free(paranges);
1963 /* build rela.aranges section */
1964 arangesrellen = saalen = parangesrel->datalen;
1965 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1966 saa_rnbytes(parangesrel, pbuf, saalen);
1967 saa_free(parangesrel);
1969 /* build pubnames section */
1970 ppubnames = saa_init(1L);
1971 saa_write16(ppubnames,3); /* dwarf version */
1972 saa_write32(ppubnames,0); /* offset into info */
1973 saa_write32(ppubnames,0); /* space used in info */
1974 saa_write32(ppubnames,0); /* end of list */
1975 saalen = ppubnames->datalen;
1976 pubnameslen = saalen + 4;
1977 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1978 WRITELONG(pbuf,saalen); /* initial length */
1979 saa_rnbytes(ppubnames, pbuf, saalen);
1980 saa_free(ppubnames);
1982 /* build info section */
1983 pinfo = saa_init(1L);
1984 pinforel = saa_init(1L);
1985 saa_write16(pinfo,3); /* dwarf version */
1986 saa_write64(pinforel, pinfo->datalen + 4);
1987 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
1988 saa_write64(pinforel, 0);
1989 saa_write32(pinfo,0); /* offset into abbrev */
1990 saa_write8(pinfo,8); /* pointer size */
1991 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1992 saa_write64(pinforel, pinfo->datalen + 4);
1993 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1994 saa_write64(pinforel, 0);
1995 saa_write64(pinfo,0); /* DW_AT_low_pc */
1996 saa_write64(pinforel, pinfo->datalen + 4);
1997 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1998 saa_write64(pinforel, 0);
1999 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2000 saa_write64(pinforel, pinfo->datalen + 4);
2001 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2002 saa_write64(pinforel, 0);
2003 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2004 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2005 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2006 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2007 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2008 saa_write64(pinforel, pinfo->datalen + 4);
2009 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2010 saa_write64(pinforel, 0);
2011 saa_write64(pinfo,0); /* DW_AT_low_pc */
2012 saa_write64(pinfo,0); /* DW_AT_frame_base */
2013 saa_write8(pinfo,0); /* end of entries */
2014 saalen = pinfo->datalen;
2015 infolen = saalen + 4;
2016 infobuf = pbuf = nasm_malloc(infolen);
2017 WRITELONG(pbuf,saalen); /* initial length */
2018 saa_rnbytes(pinfo, pbuf, saalen);
2019 saa_free(pinfo);
2021 /* build rela.info section */
2022 inforellen = saalen = pinforel->datalen;
2023 inforelbuf = pbuf = nasm_malloc(inforellen);
2024 saa_rnbytes(pinforel, pbuf, saalen);
2025 saa_free(pinforel);
2027 /* build abbrev section */
2028 pabbrev = saa_init(1L);
2029 saa_write8(pabbrev,1); /* entry number LEB128u */
2030 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2031 saa_write8(pabbrev,1); /* has children */
2032 /* the following attributes and forms are all LEB128u values */
2033 saa_write8(pabbrev,DW_AT_low_pc);
2034 saa_write8(pabbrev,DW_FORM_addr);
2035 saa_write8(pabbrev,DW_AT_high_pc);
2036 saa_write8(pabbrev,DW_FORM_addr);
2037 saa_write8(pabbrev,DW_AT_stmt_list);
2038 saa_write8(pabbrev,DW_FORM_data4);
2039 saa_write8(pabbrev,DW_AT_name);
2040 saa_write8(pabbrev,DW_FORM_string);
2041 saa_write8(pabbrev,DW_AT_producer);
2042 saa_write8(pabbrev,DW_FORM_string);
2043 saa_write8(pabbrev,DW_AT_language);
2044 saa_write8(pabbrev,DW_FORM_data2);
2045 saa_write16(pabbrev,0); /* end of entry */
2046 /* LEB128u usage same as above */
2047 saa_write8(pabbrev,2); /* entry number */
2048 saa_write8(pabbrev,DW_TAG_subprogram);
2049 saa_write8(pabbrev,0); /* no children */
2050 saa_write8(pabbrev,DW_AT_low_pc);
2051 saa_write8(pabbrev,DW_FORM_addr);
2052 saa_write8(pabbrev,DW_AT_frame_base);
2053 saa_write8(pabbrev,DW_FORM_data4);
2054 saa_write16(pabbrev,0); /* end of entry */
2055 abbrevlen = saalen = pabbrev->datalen;
2056 abbrevbuf = pbuf = nasm_malloc(saalen);
2057 saa_rnbytes(pabbrev, pbuf, saalen);
2058 saa_free(pabbrev);
2060 /* build line section */
2061 /* prolog */
2062 plines = saa_init(1L);
2063 saa_write8(plines,1); /* Minimum Instruction Length */
2064 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2065 saa_write8(plines,line_base); /* Line Base */
2066 saa_write8(plines,line_range); /* Line Range */
2067 saa_write8(plines,opcode_base); /* Opcode Base */
2068 /* standard opcode lengths (# of LEB128u operands) */
2069 saa_write8(plines,0); /* Std opcode 1 length */
2070 saa_write8(plines,1); /* Std opcode 2 length */
2071 saa_write8(plines,1); /* Std opcode 3 length */
2072 saa_write8(plines,1); /* Std opcode 4 length */
2073 saa_write8(plines,1); /* Std opcode 5 length */
2074 saa_write8(plines,0); /* Std opcode 6 length */
2075 saa_write8(plines,0); /* Std opcode 7 length */
2076 saa_write8(plines,0); /* Std opcode 8 length */
2077 saa_write8(plines,1); /* Std opcode 9 length */
2078 saa_write8(plines,0); /* Std opcode 10 length */
2079 saa_write8(plines,0); /* Std opcode 11 length */
2080 saa_write8(plines,1); /* Std opcode 12 length */
2081 /* Directory Table */
2082 saa_write8(plines,0); /* End of table */
2083 /* File Name Table */
2084 ftentry = dwarf_flist;
2085 for (indx = 0;indx<dwarf_numfiles;indx++)
2087 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2088 saa_write8(plines,0); /* directory LEB128u */
2089 saa_write8(plines,0); /* time LEB128u */
2090 saa_write8(plines,0); /* size LEB128u */
2091 ftentry = ftentry->next;
2093 saa_write8(plines,0); /* End of table */
2094 linepoff = plines->datalen;
2095 linelen = linepoff + totlen + 10;
2096 linebuf = pbuf = nasm_malloc(linelen);
2097 WRITELONG(pbuf,linelen-4); /* initial length */
2098 WRITESHORT(pbuf,3); /* dwarf version */
2099 WRITELONG(pbuf,linepoff); /* offset to line number program */
2100 /* write line header */
2101 saalen = linepoff;
2102 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2103 pbuf += linepoff;
2104 saa_free(plines);
2105 /* concatonate line program ranges */
2106 linepoff += 13;
2107 plinesrel = saa_init(1L);
2108 psect = dwarf_fsect;
2109 for (indx = 0; indx < dwarf_nsections; indx++) {
2110 saa_write64(plinesrel, linepoff);
2111 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2112 saa_write64(plinesrel, (uint64_t) 0);
2113 plinep = psect->psaa;
2114 saalen = plinep->datalen;
2115 saa_rnbytes(plinep, pbuf, saalen);
2116 pbuf += saalen;
2117 linepoff += saalen;
2118 saa_free(plinep);
2119 /* done with this entry */
2120 psect = psect->next;
2124 /* build rela.lines section */
2125 linerellen =saalen = plinesrel->datalen;
2126 linerelbuf = pbuf = nasm_malloc(linerellen);
2127 saa_rnbytes(plinesrel, pbuf, saalen);
2128 saa_free(plinesrel);
2130 /* build frame section */
2131 framelen = 4;
2132 framebuf = pbuf = nasm_malloc(framelen);
2133 WRITELONG(pbuf,framelen-4); /* initial length */
2135 /* build loc section */
2136 loclen = 16;
2137 locbuf = pbuf = nasm_malloc(loclen);
2138 WRITEDLONG(pbuf,0); /* null beginning offset */
2139 WRITEDLONG(pbuf,0); /* null ending offset */
2142 static void dwarf64_cleanup(void)
2144 nasm_free(arangesbuf);
2145 nasm_free(arangesrelbuf);
2146 nasm_free(pubnamesbuf);
2147 nasm_free(infobuf);
2148 nasm_free(inforelbuf);
2149 nasm_free(abbrevbuf);
2150 nasm_free(linebuf);
2151 nasm_free(linerelbuf);
2152 nasm_free(framebuf);
2153 nasm_free(locbuf);
2156 static void dwarf64_findfile(const char * fname)
2158 int finx;
2159 struct linelist *match;
2161 /* return if fname is current file name */
2162 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2163 return;
2165 /* search for match */
2166 match = 0;
2167 if (dwarf_flist) {
2168 match = dwarf_flist;
2169 for (finx = 0; finx < dwarf_numfiles; finx++) {
2170 if (!(strcmp(fname, match->filename))) {
2171 dwarf_clist = match;
2172 return;
2177 /* add file name to end of list */
2178 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2179 dwarf_numfiles++;
2180 dwarf_clist->line = dwarf_numfiles;
2181 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2182 strcpy(dwarf_clist->filename,fname);
2183 dwarf_clist->next = 0;
2184 if (!dwarf_flist) { /* if first entry */
2185 dwarf_flist = dwarf_elist = dwarf_clist;
2186 dwarf_clist->last = 0;
2187 } else { /* chain to previous entry */
2188 dwarf_elist->next = dwarf_clist;
2189 dwarf_elist = dwarf_clist;
2193 static void dwarf64_findsect(const int index)
2195 int sinx;
2196 struct sectlist *match;
2197 struct SAA *plinep;
2199 /* return if index is current section index */
2200 if (dwarf_csect && (dwarf_csect->section == index))
2201 return;
2203 /* search for match */
2204 match = 0;
2205 if (dwarf_fsect) {
2206 match = dwarf_fsect;
2207 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2208 if ((match->section == index)) {
2209 dwarf_csect = match;
2210 return;
2212 match = match->next;
2216 /* add entry to end of list */
2217 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2218 dwarf_nsections++;
2219 dwarf_csect->psaa = plinep = saa_init(1L);
2220 dwarf_csect->line = 1;
2221 dwarf_csect->offset = 0;
2222 dwarf_csect->file = 1;
2223 dwarf_csect->section = index;
2224 dwarf_csect->next = 0;
2225 /* set relocatable address at start of line program */
2226 saa_write8(plinep,DW_LNS_extended_op);
2227 saa_write8(plinep,9); /* operand length */
2228 saa_write8(plinep,DW_LNE_set_address);
2229 saa_write64(plinep,0); /* Start Address */
2231 if (!dwarf_fsect) { /* if first entry */
2232 dwarf_fsect = dwarf_esect = dwarf_csect;
2233 dwarf_csect->last = 0;
2234 } else { /* chain to previous entry */
2235 dwarf_esect->next = dwarf_csect;
2236 dwarf_esect = dwarf_csect;
2240 #endif /* OF_ELF */