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