insns.dat: Restore default size of memory operands
[nasm/avx512.git] / output / outelf64.c
blob18508fb3b84df618b7c5a2689b1364b5cff05272
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelf64.c output routines for the Netwide Assembler to produce
36 * ELF64 (x86_64 of course) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/dwarf.h"
58 #include "output/elf.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELF64
63 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
65 struct Reloc {
66 struct Reloc *next;
67 int64_t address; /* relative to _start_ of section */
68 int64_t symbol; /* symbol index */
69 int64_t offset; /* symbol addend */
70 int type; /* type of relocation */
73 struct Symbol {
74 struct rbtree symv; /* symbol value and rbtree of globals */
75 int32_t strpos; /* string table position of name */
76 int32_t section; /* section ID of the symbol */
77 int type; /* symbol type */
78 int other; /* symbol visibility */
79 int32_t size; /* size of symbol */
80 int32_t globnum; /* symbol table offset if global */
81 struct Symbol *nextfwd; /* list of unresolved-size symbols */
82 char *name; /* used temporarily if in above list */
85 struct Section {
86 struct SAA *data;
87 uint64_t len, size;
88 uint32_t nrelocs;
89 int32_t index; /* index into sects array */
90 int type; /* SHT_PROGBITS or SHT_NOBITS */
91 uint64_t align; /* alignment: power of two */
92 uint64_t flags; /* section flags */
93 char *name;
94 struct SAA *rel;
95 uint64_t rellen;
96 struct Reloc *head, **tail;
97 struct rbtree *gsyms; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section **sects;
102 static int nsects, sectlen;
104 #define SHSTR_DELTA 256
105 static char *shstrtab;
106 static int shstrtablen, shstrtabsize;
108 static struct SAA *syms;
109 static uint32_t nlocals, nglobs, ndebugs;
111 static int32_t def_seg;
113 static struct RAA *bsym;
115 static struct SAA *strs;
116 static uint32_t strslen;
118 static struct Symbol *fwds;
120 static char elf_module[FILENAME_MAX];
122 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
123 static uint8_t elf_abiver = 0; /* Current ABI version */
125 extern struct ofmt of_elf64;
127 static struct ELF_SECTDATA {
128 void *data;
129 int64_t len;
130 bool is_saa;
131 } *elf_sects;
132 static int elf_nsect, nsections;
133 static int64_t elf_foffs;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section *, const void *, size_t);
137 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
138 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
139 int, int);
140 static void elf_write_sections(void);
141 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
143 static void add_sectname(char *, char *);
145 /* type values for stabs debugging sections */
146 #define N_SO 0x64 /* ID for main source file */
147 #define N_SOL 0x84 /* ID for sub-source file */
148 #define N_BINCL 0x82 /* not currently used */
149 #define N_EINCL 0xA2 /* not currently used */
150 #define N_SLINE 0x44
152 struct stabentry {
153 uint32_t n_strx;
154 uint8_t n_type;
155 uint8_t n_other;
156 uint16_t n_desc;
157 uint32_t n_value;
160 struct erel {
161 int offset, info;
164 struct symlininfo {
165 int offset;
166 int section; /* index into sects[] */
167 int segto; /* internal section number */
168 char *name; /* shallow-copied pointer of section name */
171 struct linelist {
172 struct symlininfo info;
173 int line;
174 char *filename;
175 struct linelist *next;
176 struct linelist *last;
179 struct sectlist {
180 struct SAA *psaa;
181 int section;
182 int line;
183 int offset;
184 int file;
185 struct sectlist *next;
186 struct sectlist *last;
189 /* common debug variables */
190 static int currentline = 1;
191 static int debug_immcall = 0;
193 /* stabs debug variables */
194 static struct linelist *stabslines = 0;
195 static int numlinestabs = 0;
196 static char *stabs_filename = 0;
197 static int symtabsection;
198 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
199 static int stablen, stabstrlen, stabrellen;
201 /* dwarf debug variables */
202 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
203 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
204 static int dwarf_numfiles = 0, dwarf_nsections;
205 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
206 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
207 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
208 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
209 abbrevlen, linelen, linerellen, framelen, loclen;
210 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
213 static struct dfmt df_dwarf;
214 static struct dfmt df_stabs;
215 static struct Symbol *lastsym;
217 /* common debugging routines */
218 static void debug64_typevalue(int32_t);
219 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
220 static void debug64_directive(const char *, const char *);
222 /* stabs debugging routines */
223 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
224 static void stabs64_output(int, void *);
225 static void stabs64_generate(void);
226 static void stabs64_cleanup(void);
228 /* dwarf debugging routines */
229 static void dwarf64_init(void);
230 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
231 static void dwarf64_output(int, void *);
232 static void dwarf64_generate(void);
233 static void dwarf64_cleanup(void);
234 static void dwarf64_findfile(const char *);
235 static void dwarf64_findsect(const int);
238 * Special section numbers which are used to define ELF special
239 * symbols, which can be used with WRT to provide PIC relocation
240 * types.
242 static int32_t elf_gotpc_sect, elf_gotoff_sect;
243 static int32_t elf_got_sect, elf_plt_sect;
244 static int32_t elf_sym_sect;
245 static int32_t elf_gottpoff_sect;
247 static void elf_init(void)
249 maxbits = 64;
250 sects = NULL;
251 nsects = sectlen = 0;
252 syms = saa_init((int32_t)sizeof(struct Symbol));
253 nlocals = nglobs = ndebugs = 0;
254 bsym = raa_init();
255 strs = saa_init(1L);
256 saa_wbytes(strs, "\0", 1L);
257 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
258 strslen = 2 + strlen(elf_module);
259 shstrtab = NULL;
260 shstrtablen = shstrtabsize = 0;;
261 add_sectname("", "");
263 fwds = NULL;
265 elf_gotpc_sect = seg_alloc();
266 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
267 elf_gotoff_sect = seg_alloc();
268 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
269 elf_got_sect = seg_alloc();
270 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
271 elf_plt_sect = seg_alloc();
272 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
273 elf_sym_sect = seg_alloc();
274 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
275 elf_gottpoff_sect = seg_alloc();
276 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
278 def_seg = seg_alloc();
282 static void elf_cleanup(int debuginfo)
284 struct Reloc *r;
285 int i;
287 (void)debuginfo;
289 elf_write();
290 for (i = 0; i < nsects; i++) {
291 if (sects[i]->type != SHT_NOBITS)
292 saa_free(sects[i]->data);
293 if (sects[i]->head)
294 saa_free(sects[i]->rel);
295 while (sects[i]->head) {
296 r = sects[i]->head;
297 sects[i]->head = sects[i]->head->next;
298 nasm_free(r);
301 nasm_free(sects);
302 saa_free(syms);
303 raa_free(bsym);
304 saa_free(strs);
305 if (of_elf64.current_dfmt) {
306 of_elf64.current_dfmt->cleanup();
309 /* add entry to the elf .shstrtab section */
310 static void add_sectname(char *firsthalf, char *secondhalf)
312 int len = strlen(firsthalf) + strlen(secondhalf);
313 while (shstrtablen + len + 1 > shstrtabsize)
314 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
315 strcpy(shstrtab + shstrtablen, firsthalf);
316 strcat(shstrtab + shstrtablen, secondhalf);
317 shstrtablen += len + 1;
320 static int elf_make_section(char *name, int type, int flags, int align)
322 struct Section *s;
324 s = nasm_malloc(sizeof(*s));
326 if (type != SHT_NOBITS)
327 s->data = saa_init(1L);
328 s->head = NULL;
329 s->tail = &s->head;
330 s->len = s->size = 0;
331 s->nrelocs = 0;
332 if (!strcmp(name, ".text"))
333 s->index = def_seg;
334 else
335 s->index = seg_alloc();
336 add_sectname("", name);
337 s->name = nasm_malloc(1 + strlen(name));
338 strcpy(s->name, name);
339 s->type = type;
340 s->flags = flags;
341 s->align = align;
342 s->gsyms = NULL;
344 if (nsects >= sectlen)
345 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
346 sects[nsects++] = s;
348 return nsects - 1;
351 static int32_t elf_section_names(char *name, int pass, int *bits)
353 char *p;
354 uint32_t flags, flags_and, flags_or;
355 uint64_t align;
356 int type, i;
359 * Default is 64 bits.
361 if (!name) {
362 *bits = 64;
363 return def_seg;
366 p = nasm_skip_word(name);
367 if (*p)
368 *p++ = '\0';
369 flags_and = flags_or = type = align = 0;
371 p = nasm_skip_spaces(p);
372 while (*p) {
373 char *q = p;
374 p = nasm_skip_word(p);
375 if (*p)
376 *p++ = '\0';
377 p = nasm_skip_spaces(p);
379 if (!nasm_strnicmp(q, "align=", 6)) {
380 align = atoi(q + 6);
381 if (align == 0)
382 align = 1;
383 if ((align - 1) & align) { /* means it's not a power of two */
384 nasm_error(ERR_NONFATAL, "section alignment %"PRId64" is not"
385 " a power of two", align);
386 align = 1;
388 } else if (!nasm_stricmp(q, "alloc")) {
389 flags_and |= SHF_ALLOC;
390 flags_or |= SHF_ALLOC;
391 } else if (!nasm_stricmp(q, "noalloc")) {
392 flags_and |= SHF_ALLOC;
393 flags_or &= ~SHF_ALLOC;
394 } else if (!nasm_stricmp(q, "exec")) {
395 flags_and |= SHF_EXECINSTR;
396 flags_or |= SHF_EXECINSTR;
397 } else if (!nasm_stricmp(q, "noexec")) {
398 flags_and |= SHF_EXECINSTR;
399 flags_or &= ~SHF_EXECINSTR;
400 } else if (!nasm_stricmp(q, "write")) {
401 flags_and |= SHF_WRITE;
402 flags_or |= SHF_WRITE;
403 } else if (!nasm_stricmp(q, "tls")) {
404 flags_and |= SHF_TLS;
405 flags_or |= SHF_TLS;
406 } else if (!nasm_stricmp(q, "nowrite")) {
407 flags_and |= SHF_WRITE;
408 flags_or &= ~SHF_WRITE;
409 } else if (!nasm_stricmp(q, "progbits")) {
410 type = SHT_PROGBITS;
411 } else if (!nasm_stricmp(q, "nobits")) {
412 type = SHT_NOBITS;
413 } else if (pass == 1) {
414 nasm_error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
415 " declaration of section `%s'", q, name);
419 if (!strcmp(name, ".shstrtab") ||
420 !strcmp(name, ".symtab") ||
421 !strcmp(name, ".strtab")) {
422 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
423 "name `%s'", name);
424 return NO_SEG;
427 for (i = 0; i < nsects; i++)
428 if (!strcmp(name, sects[i]->name))
429 break;
430 if (i == nsects) {
431 const struct elf_known_section *ks = elf_known_sections;
433 while (ks->name) {
434 if (!strcmp(name, ks->name))
435 break;
436 ks++;
439 type = type ? type : ks->type;
440 align = align ? align : ks->align;
441 flags = (ks->flags & ~flags_and) | flags_or;
443 i = elf_make_section(name, type, flags, align);
444 } else if (pass == 1) {
445 if ((type && sects[i]->type != type)
446 || (align && sects[i]->align != align)
447 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
448 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
449 " redeclaration of section `%s'", name);
452 return sects[i]->index;
455 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
456 int is_global, char *special)
458 int pos = strslen;
459 struct Symbol *sym;
460 bool special_used = false;
462 #if defined(DEBUG) && DEBUG>2
463 nasm_error(ERR_DEBUG,
464 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
465 name, segment, offset, is_global, special);
466 #endif
467 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
469 * This is a NASM special symbol. We never allow it into
470 * the ELF symbol table, even if it's a valid one. If it
471 * _isn't_ a valid one, we should barf immediately.
473 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
474 strcmp(name, "..got") && strcmp(name, "..plt") &&
475 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
476 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
477 return;
480 if (is_global == 3) {
481 struct Symbol **s;
483 * Fix up a forward-reference symbol size from the first
484 * pass.
486 for (s = &fwds; *s; s = &(*s)->nextfwd)
487 if (!strcmp((*s)->name, name)) {
488 struct tokenval tokval;
489 expr *e;
490 char *p = nasm_skip_spaces(nasm_skip_word(special));
492 stdscan_reset();
493 stdscan_set(p);
494 tokval.t_type = TOKEN_INVALID;
495 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
496 if (e) {
497 if (!is_simple(e))
498 nasm_error(ERR_NONFATAL, "cannot use relocatable"
499 " expression as symbol size");
500 else
501 (*s)->size = reloc_value(e);
505 * Remove it from the list of unresolved sizes.
507 nasm_free((*s)->name);
508 *s = (*s)->nextfwd;
509 return;
511 return; /* it wasn't an important one */
514 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
515 strslen += 1 + strlen(name);
517 lastsym = sym = saa_wstruct(syms);
519 memset(&sym->symv, 0, sizeof(struct rbtree));
521 sym->strpos = pos;
522 sym->type = is_global ? SYM_GLOBAL : 0;
523 sym->other = STV_DEFAULT;
524 sym->size = 0;
525 if (segment == NO_SEG)
526 sym->section = SHN_ABS;
527 else {
528 int i;
529 sym->section = SHN_UNDEF;
530 if (segment == def_seg) {
531 /* we have to be sure at least text section is there */
532 int tempint;
533 elf_section_names(".text", 2, &tempint);
535 sym->section = nsects;
536 for (i = 0; i < nsects; i++) {
537 if (segment == sects[i]->index) {
538 sym->section = i + 1;
539 break;
542 if (nsects && i == nsects)
543 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
546 if (is_global == 2) {
547 sym->size = offset;
548 sym->symv.key = 0;
549 sym->section = SHN_COMMON;
551 * We have a common variable. Check the special text to see
552 * if it's a valid number and power of two; if so, store it
553 * as the alignment for the common variable.
555 if (special) {
556 bool err;
557 sym->symv.key = readnum(special, &err);
558 if (err)
559 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
560 " valid number", special);
561 else if ((sym->symv.key | (sym->symv.key - 1))
562 != 2 * sym->symv.key - 1)
563 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
564 " power of two", special);
566 special_used = true;
567 } else
568 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
570 if (sym->type == SYM_GLOBAL) {
572 * If sym->section == SHN_ABS, then the first line of the
573 * else section would cause a core dump, because its a reference
574 * beyond the end of the section array.
575 * This behaviour is exhibited by this code:
576 * GLOBAL crash_nasm
577 * crash_nasm equ 0
578 * To avoid such a crash, such requests are silently discarded.
579 * This may not be the best solution.
581 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
582 bsym = raa_write(bsym, segment, nglobs);
583 } else if (sym->section != SHN_ABS) {
585 * This is a global symbol; so we must add it to the rbtree
586 * of global symbols in its section.
588 * In addition, we check the special text for symbol
589 * type and size information.
591 sects[sym->section-1]->gsyms =
592 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
594 if (special) {
595 int n = strcspn(special, " \t");
597 if (!nasm_strnicmp(special, "function", n))
598 sym->type |= STT_FUNC;
599 else if (!nasm_strnicmp(special, "data", n) ||
600 !nasm_strnicmp(special, "object", n))
601 sym->type |= STT_OBJECT;
602 else if (!nasm_strnicmp(special, "notype", n))
603 sym->type |= STT_NOTYPE;
604 else
605 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
606 n, special);
607 special += n;
609 special = nasm_skip_spaces(special);
610 if (*special) {
611 n = strcspn(special, " \t");
612 if (!nasm_strnicmp(special, "default", n))
613 sym->other = STV_DEFAULT;
614 else if (!nasm_strnicmp(special, "internal", n))
615 sym->other = STV_INTERNAL;
616 else if (!nasm_strnicmp(special, "hidden", n))
617 sym->other = STV_HIDDEN;
618 else if (!nasm_strnicmp(special, "protected", n))
619 sym->other = STV_PROTECTED;
620 else
621 n = 0;
622 special += n;
625 if (*special) {
626 struct tokenval tokval;
627 expr *e;
628 int fwd = 0;
629 char *saveme = stdscan_get();
631 while (special[n] && nasm_isspace(special[n]))
632 n++;
634 * We have a size expression; attempt to
635 * evaluate it.
637 stdscan_reset();
638 stdscan_set(special + n);
639 tokval.t_type = TOKEN_INVALID;
640 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
641 NULL);
642 if (fwd) {
643 sym->nextfwd = fwds;
644 fwds = sym;
645 sym->name = nasm_strdup(name);
646 } else if (e) {
647 if (!is_simple(e))
648 nasm_error(ERR_NONFATAL, "cannot use relocatable"
649 " expression as symbol size");
650 else
651 sym->size = reloc_value(e);
653 stdscan_set(saveme);
655 special_used = true;
658 * If TLS segment, mark symbol accordingly.
660 if (sects[sym->section - 1]->flags & SHF_TLS) {
661 sym->type &= 0xf0;
662 sym->type |= STT_TLS;
665 sym->globnum = nglobs;
666 nglobs++;
667 } else
668 nlocals++;
670 if (special && !special_used)
671 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
674 static void elf_add_reloc(struct Section *sect, int32_t segment,
675 int64_t offset, int type)
677 struct Reloc *r;
678 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
679 sect->tail = &r->next;
680 r->next = NULL;
682 r->address = sect->len;
683 r->offset = offset;
684 if (segment == NO_SEG)
685 r->symbol = 0;
686 else {
687 int i;
688 r->symbol = 0;
689 for (i = 0; i < nsects; i++)
690 if (segment == sects[i]->index)
691 r->symbol = i + 2;
692 if (!r->symbol)
693 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
695 r->type = type;
697 sect->nrelocs++;
701 * This routine deals with ..got and ..sym relocations: the more
702 * complicated kinds. In shared-library writing, some relocations
703 * with respect to global symbols must refer to the precise symbol
704 * rather than referring to an offset from the base of the section
705 * _containing_ the symbol. Such relocations call to this routine,
706 * which searches the symbol list for the symbol in question.
708 * R_386_GOT32 references require the _exact_ symbol address to be
709 * used; R_386_32 references can be at an offset from the symbol.
710 * The boolean argument `exact' tells us this.
712 * Return value is the adjusted value of `addr', having become an
713 * offset from the symbol rather than the section. Should always be
714 * zero when returning from an exact call.
716 * Limitation: if you define two symbols at the same place,
717 * confusion will occur.
719 * Inefficiency: we search, currently, using a linked list which
720 * isn't even necessarily sorted.
722 static void elf_add_gsym_reloc(struct Section *sect,
723 int32_t segment, uint64_t offset, int64_t pcrel,
724 int type, bool exact)
726 struct Reloc *r;
727 struct Section *s;
728 struct Symbol *sym;
729 struct rbtree *srb;
730 int i;
733 * First look up the segment/offset pair and find a global
734 * symbol corresponding to it. If it's not one of our segments,
735 * then it must be an external symbol, in which case we're fine
736 * doing a normal elf_add_reloc after first sanity-checking
737 * that the offset from the symbol is zero.
739 s = NULL;
740 for (i = 0; i < nsects; i++)
741 if (segment == sects[i]->index) {
742 s = sects[i];
743 break;
746 if (!s) {
747 if (exact && offset)
748 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
749 else
750 elf_add_reloc(sect, segment, offset - pcrel, type);
751 return;
754 srb = rb_search(s->gsyms, offset);
755 if (!srb || (exact && srb->key != offset)) {
756 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
757 " for this reference");
758 return;
760 sym = container_of(srb, struct Symbol, symv);
762 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
763 sect->tail = &r->next;
764 r->next = NULL;
766 r->address = sect->len;
767 r->offset = offset - pcrel - sym->symv.key;
768 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
769 r->type = type;
771 sect->nrelocs++;
774 static void elf_out(int32_t segto, const void *data,
775 enum out_type type, uint64_t size,
776 int32_t segment, int32_t wrt)
778 struct Section *s;
779 int64_t addr, zero;
780 int i;
781 static struct symlininfo sinfo;
783 zero = 0;
785 #if defined(DEBUG) && DEBUG>2
786 if (data)
787 nasm_error(ERR_DEBUG,
788 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
789 currentline, type, segment, segto, size, *(int64_t *)data);
790 else
791 nasm_error(ERR_DEBUG,
792 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
793 currentline, type, segment, segto, size);
794 #endif
797 * handle absolute-assembly (structure definitions)
799 if (segto == NO_SEG) {
800 if (type != OUT_RESERVE)
801 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
802 " space");
803 return;
806 s = NULL;
807 for (i = 0; i < nsects; i++)
808 if (segto == sects[i]->index) {
809 s = sects[i];
810 break;
812 if (!s) {
813 int tempint; /* ignored */
814 if (segto != elf_section_names(".text", 2, &tempint))
815 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
816 else {
817 s = sects[nsects - 1];
818 i = nsects - 1;
821 /* invoke current debug_output routine */
822 if (of_elf64.current_dfmt) {
823 sinfo.offset = s->len;
824 sinfo.section = i;
825 sinfo.segto = segto;
826 sinfo.name = s->name;
827 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
829 /* end of debugging stuff */
831 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
832 nasm_error(ERR_WARNING, "attempt to initialize memory in"
833 " BSS section `%s': ignored", s->name);
834 s->len += realsize(type, size);
835 return;
838 if (type == OUT_RESERVE) {
839 if (s->type == SHT_PROGBITS) {
840 nasm_error(ERR_WARNING, "uninitialized space declared in"
841 " non-BSS section `%s': zeroing", s->name);
842 elf_sect_write(s, NULL, size);
843 } else
844 s->len += size;
845 } else if (type == OUT_RAWDATA) {
846 if (segment != NO_SEG)
847 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
848 elf_sect_write(s, data, size);
849 } else if (type == OUT_ADDRESS) {
850 addr = *(int64_t *)data;
851 if (segment == NO_SEG) {
852 /* Do nothing */
853 } else if (segment % 2) {
854 nasm_error(ERR_NONFATAL, "ELF format does not support"
855 " segment base references");
856 } else {
857 if (wrt == NO_SEG) {
858 switch ((int)size) {
859 case 1:
860 elf_add_reloc(s, segment, addr, R_X86_64_8);
861 break;
862 case 2:
863 elf_add_reloc(s, segment, addr, R_X86_64_16);
864 break;
865 case 4:
866 elf_add_reloc(s, segment, addr, R_X86_64_32);
867 break;
868 case 8:
869 elf_add_reloc(s, segment, addr, R_X86_64_64);
870 break;
871 default:
872 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
873 break;
875 addr = 0;
876 } else if (wrt == elf_gotpc_sect + 1) {
878 * The user will supply GOT relative to $$. ELF
879 * will let us have GOT relative to $. So we
880 * need to fix up the data item by $-$$.
882 addr += s->len;
883 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
884 addr = 0;
885 } else if (wrt == elf_gotoff_sect + 1) {
886 if (size != 8) {
887 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
888 "references to be qword");
889 } else {
890 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
891 addr = 0;
893 } else if (wrt == elf_got_sect + 1) {
894 switch ((int)size) {
895 case 4:
896 elf_add_gsym_reloc(s, segment, addr, 0,
897 R_X86_64_GOT32, true);
898 addr = 0;
899 break;
900 case 8:
901 elf_add_gsym_reloc(s, segment, addr, 0,
902 R_X86_64_GOT64, true);
903 addr = 0;
904 break;
905 default:
906 nasm_error(ERR_NONFATAL, "invalid ..got reference");
907 break;
909 } else if (wrt == elf_sym_sect + 1) {
910 switch ((int)size) {
911 case 1:
912 elf_add_gsym_reloc(s, segment, addr, 0,
913 R_X86_64_8, false);
914 addr = 0;
915 break;
916 case 2:
917 elf_add_gsym_reloc(s, segment, addr, 0,
918 R_X86_64_16, false);
919 addr = 0;
920 break;
921 case 4:
922 elf_add_gsym_reloc(s, segment, addr, 0,
923 R_X86_64_32, false);
924 addr = 0;
925 break;
926 case 8:
927 elf_add_gsym_reloc(s, segment, addr, 0,
928 R_X86_64_64, false);
929 addr = 0;
930 break;
931 default:
932 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
933 break;
935 } else if (wrt == elf_plt_sect + 1) {
936 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
937 "relative PLT references");
938 } else {
939 nasm_error(ERR_NONFATAL, "ELF format does not support this"
940 " use of WRT");
943 elf_sect_writeaddr(s, addr, size);
944 } else if (type == OUT_REL2ADR) {
945 addr = *(int64_t *)data - size;
946 if (segment == segto)
947 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
948 if (segment == NO_SEG) {
949 /* Do nothing */
950 } else if (segment % 2) {
951 nasm_error(ERR_NONFATAL, "ELF format does not support"
952 " segment base references");
953 } else {
954 if (wrt == NO_SEG) {
955 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
956 addr = 0;
957 } else {
958 nasm_error(ERR_NONFATAL,
959 "Unsupported non-32-bit ELF relocation [2]");
962 elf_sect_writeaddr(s, addr, 2);
963 } else if (type == OUT_REL4ADR) {
964 addr = *(int64_t *)data - size;
965 if (segment == segto)
966 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
967 if (segment == NO_SEG) {
968 /* Do nothing */
969 } else if (segment % 2) {
970 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
971 " segment base references");
972 } else {
973 if (wrt == NO_SEG) {
974 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
975 addr = 0;
976 } else if (wrt == elf_plt_sect + 1) {
977 elf_add_gsym_reloc(s, segment, addr+size, size,
978 R_X86_64_PLT32, true);
979 addr = 0;
980 } else if (wrt == elf_gotpc_sect + 1 ||
981 wrt == elf_got_sect + 1) {
982 elf_add_gsym_reloc(s, segment, addr+size, size,
983 R_X86_64_GOTPCREL, true);
984 addr = 0;
985 } else if (wrt == elf_gotoff_sect + 1 ||
986 wrt == elf_got_sect + 1) {
987 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
988 "qword absolute");
989 } else if (wrt == elf_gottpoff_sect + 1) {
990 elf_add_gsym_reloc(s, segment, addr+size, size,
991 R_X86_64_GOTTPOFF, true);
992 addr = 0;
993 } else {
994 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
995 " use of WRT");
998 elf_sect_writeaddr(s, addr, 4);
999 } else if (type == OUT_REL8ADR) {
1000 addr = *(int64_t *)data - size;
1001 if (segment == segto)
1002 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1003 if (segment == NO_SEG) {
1004 /* Do nothing */
1005 } else if (segment % 2) {
1006 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1007 " segment base references");
1008 } else {
1009 if (wrt == NO_SEG) {
1010 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1011 addr = 0;
1012 } else if (wrt == elf_gotpc_sect + 1 ||
1013 wrt == elf_got_sect + 1) {
1014 elf_add_gsym_reloc(s, segment, addr+size, size,
1015 R_X86_64_GOTPCREL64, true);
1016 addr = 0;
1017 } else if (wrt == elf_gotoff_sect + 1 ||
1018 wrt == elf_got_sect + 1) {
1019 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1020 "absolute");
1021 } else if (wrt == elf_gottpoff_sect + 1) {
1022 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1023 "dword");
1024 } else {
1025 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1026 " use of WRT");
1029 elf_sect_writeaddr(s, addr, 8);
1033 static void elf_write(void)
1035 int align;
1036 char *p;
1037 int i;
1039 struct SAA *symtab;
1040 int32_t symtablen, symtablocal;
1043 * Work out how many sections we will have. We have SHN_UNDEF,
1044 * then the flexible user sections, then the fixed sections
1045 * `.shstrtab', `.symtab' and `.strtab', then optionally
1046 * relocation sections for the user sections.
1048 nsections = sec_numspecial + 1;
1049 if (of_elf64.current_dfmt == &df_stabs)
1050 nsections += 3;
1051 else if (of_elf64.current_dfmt == &df_dwarf)
1052 nsections += 10;
1054 add_sectname("", ".shstrtab");
1055 add_sectname("", ".symtab");
1056 add_sectname("", ".strtab");
1057 for (i = 0; i < nsects; i++) {
1058 nsections++; /* for the section itself */
1059 if (sects[i]->head) {
1060 nsections++; /* for its relocations */
1061 add_sectname(".rela", sects[i]->name);
1065 if (of_elf64.current_dfmt == &df_stabs) {
1066 /* in case the debug information is wanted, just add these three sections... */
1067 add_sectname("", ".stab");
1068 add_sectname("", ".stabstr");
1069 add_sectname(".rel", ".stab");
1072 else if (of_elf64.current_dfmt == &df_dwarf) {
1073 /* the dwarf debug standard specifies the following ten sections,
1074 not all of which are currently implemented,
1075 although all of them are defined. */
1076 #define debug_aranges (int64_t) (nsections-10)
1077 #define debug_info (int64_t) (nsections-7)
1078 #define debug_abbrev (int64_t) (nsections-5)
1079 #define debug_line (int64_t) (nsections-4)
1080 add_sectname("", ".debug_aranges");
1081 add_sectname(".rela", ".debug_aranges");
1082 add_sectname("", ".debug_pubnames");
1083 add_sectname("", ".debug_info");
1084 add_sectname(".rela", ".debug_info");
1085 add_sectname("", ".debug_abbrev");
1086 add_sectname("", ".debug_line");
1087 add_sectname(".rela", ".debug_line");
1088 add_sectname("", ".debug_frame");
1089 add_sectname("", ".debug_loc");
1093 * Output the ELF header.
1095 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1096 fputc(elf_osabi, ofile);
1097 fputc(elf_abiver, ofile);
1098 fwritezero(7, ofile);
1099 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1100 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1101 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1102 fwriteint64_t(0L, ofile); /* no entry point */
1103 fwriteint64_t(0L, ofile); /* no program header table */
1104 fwriteint64_t(0x40L, ofile); /* section headers straight after
1105 * ELF header plus alignment */
1106 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1107 fwriteint16_t(0x40, ofile); /* size of ELF header */
1108 fwriteint16_t(0, ofile); /* no program header table, again */
1109 fwriteint16_t(0, ofile); /* still no program header table */
1110 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1111 fwriteint16_t(nsections, ofile); /* number of sections */
1112 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1113 * section header table */
1116 * Build the symbol table and relocation tables.
1118 symtab = elf_build_symtab(&symtablen, &symtablocal);
1119 for (i = 0; i < nsects; i++)
1120 if (sects[i]->head)
1121 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1122 sects[i]->head);
1125 * Now output the section header table.
1128 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1129 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1130 elf_foffs += align;
1131 elf_nsect = 0;
1132 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1134 /* SHN_UNDEF */
1135 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1136 p = shstrtab + 1;
1138 /* The normal sections */
1139 for (i = 0; i < nsects; i++) {
1140 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1141 (sects[i]->type == SHT_PROGBITS ?
1142 sects[i]->data : NULL), true,
1143 sects[i]->len, 0, 0, sects[i]->align, 0);
1144 p += strlen(p) + 1;
1147 /* .shstrtab */
1148 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1149 shstrtablen, 0, 0, 1, 0);
1150 p += strlen(p) + 1;
1152 /* .symtab */
1153 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1154 symtablen, sec_strtab, symtablocal, 4, 24);
1155 p += strlen(p) + 1;
1157 /* .strtab */
1158 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1159 strslen, 0, 0, 1, 0);
1160 p += strlen(p) + 1;
1162 /* The relocation sections */
1163 for (i = 0; i < nsects; i++)
1164 if (sects[i]->head) {
1165 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1166 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1167 p += strlen(p) + 1;
1170 if (of_elf64.current_dfmt == &df_stabs) {
1171 /* for debugging information, create the last three sections
1172 which are the .stab , .stabstr and .rel.stab sections respectively */
1174 /* this function call creates the stab sections in memory */
1175 stabs64_generate();
1177 if (stabbuf && stabstrbuf && stabrelbuf) {
1178 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1179 stablen, sec_stabstr, 0, 4, 12);
1180 p += strlen(p) + 1;
1182 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1183 stabstrlen, 0, 0, 4, 0);
1184 p += strlen(p) + 1;
1186 /* link -> symtable info -> section to refer to */
1187 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1188 stabrellen, symtabsection, sec_stab, 4, 16);
1189 p += strlen(p) + 1;
1192 else if (of_elf64.current_dfmt == &df_dwarf) {
1193 /* for dwarf debugging information, create the ten dwarf sections */
1195 /* this function call creates the dwarf sections in memory */
1196 if (dwarf_fsect)
1197 dwarf64_generate();
1199 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1200 arangeslen, 0, 0, 1, 0);
1201 p += strlen(p) + 1;
1203 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1204 arangesrellen, symtabsection, debug_aranges, 1, 24);
1205 p += strlen(p) + 1;
1207 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1208 pubnameslen, 0, 0, 1, 0);
1209 p += strlen(p) + 1;
1211 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1212 infolen, 0, 0, 1, 0);
1213 p += strlen(p) + 1;
1215 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1216 inforellen, symtabsection, debug_info, 1, 24);
1217 p += strlen(p) + 1;
1219 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1220 abbrevlen, 0, 0, 1, 0);
1221 p += strlen(p) + 1;
1223 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1224 linelen, 0, 0, 1, 0);
1225 p += strlen(p) + 1;
1227 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1228 linerellen, symtabsection, debug_line, 1, 24);
1229 p += strlen(p) + 1;
1231 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1232 framelen, 0, 0, 8, 0);
1233 p += strlen(p) + 1;
1235 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1236 loclen, 0, 0, 1, 0);
1237 p += strlen(p) + 1;
1239 fwritezero(align, ofile);
1242 * Now output the sections.
1244 elf_write_sections();
1246 nasm_free(elf_sects);
1247 saa_free(symtab);
1250 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1252 struct SAA *s = saa_init(1L);
1253 struct Symbol *sym;
1254 uint8_t entry[24], *p;
1255 int i;
1257 *len = *local = 0;
1260 * First, an all-zeros entry, required by the ELF spec.
1262 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1263 *len += 24;
1264 (*local)++;
1267 * Next, an entry for the file name.
1269 p = entry;
1270 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1271 WRITESHORT(p, STT_FILE); /* type FILE */
1272 WRITESHORT(p, SHN_ABS);
1273 WRITEDLONG(p, (uint64_t) 0); /* no value */
1274 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1275 saa_wbytes(s, entry, 24L);
1276 *len += 24;
1277 (*local)++;
1280 * Now some standard symbols defining the segments, for relocation
1281 * purposes.
1283 for (i = 1; i <= nsects; i++) {
1284 p = entry;
1285 WRITELONG(p, 0); /* no symbol name */
1286 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1287 WRITESHORT(p, i); /* section id */
1288 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1289 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1290 saa_wbytes(s, entry, 24L);
1291 *len += 24;
1292 (*local)++;
1297 * Now the other local symbols.
1299 saa_rewind(syms);
1300 while ((sym = saa_rstruct(syms))) {
1301 if (sym->type & SYM_GLOBAL)
1302 continue;
1303 p = entry;
1304 WRITELONG(p, sym->strpos); /* index into symbol string table */
1305 WRITECHAR(p, sym->type); /* type and binding */
1306 WRITECHAR(p, sym->other); /* visibility */
1307 WRITESHORT(p, sym->section); /* index into section header table */
1308 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1309 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1310 saa_wbytes(s, entry, 24L);
1311 *len += 24;
1312 (*local)++;
1315 * dwarf needs symbols for debug sections
1316 * which are relocation targets.
1318 if (of_elf64.current_dfmt == &df_dwarf) {
1319 dwarf_infosym = *local;
1320 p = entry;
1321 WRITELONG(p, 0); /* no symbol name */
1322 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1323 WRITESHORT(p, debug_info); /* section id */
1324 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1325 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1326 saa_wbytes(s, entry, 24L);
1327 *len += 24;
1328 (*local)++;
1329 dwarf_abbrevsym = *local;
1330 p = entry;
1331 WRITELONG(p, 0); /* no symbol name */
1332 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1333 WRITESHORT(p, debug_abbrev); /* section id */
1334 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1335 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1336 saa_wbytes(s, entry, 24L);
1337 *len += 24;
1338 (*local)++;
1339 dwarf_linesym = *local;
1340 p = entry;
1341 WRITELONG(p, 0); /* no symbol name */
1342 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1343 WRITESHORT(p, debug_line); /* section id */
1344 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1345 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1346 saa_wbytes(s, entry, 24L);
1347 *len += 24;
1348 (*local)++;
1352 * Now the global symbols.
1354 saa_rewind(syms);
1355 while ((sym = saa_rstruct(syms))) {
1356 if (!(sym->type & SYM_GLOBAL))
1357 continue;
1358 p = entry;
1359 WRITELONG(p, sym->strpos);
1360 WRITECHAR(p, sym->type); /* type and binding */
1361 WRITECHAR(p, sym->other); /* visibility */
1362 WRITESHORT(p, sym->section);
1363 WRITEDLONG(p, (int64_t)sym->symv.key);
1364 WRITEDLONG(p, (int64_t)sym->size);
1365 saa_wbytes(s, entry, 24L);
1366 *len += 24;
1369 return s;
1372 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1374 struct SAA *s;
1375 uint8_t *p, entry[24];
1376 int32_t global_offset;
1378 if (!r)
1379 return NULL;
1381 s = saa_init(1L);
1382 *len = 0;
1385 * How to onvert from a global placeholder to a real symbol index;
1386 * the +2 refers to the two special entries, the null entry and
1387 * the filename entry.
1389 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1391 while (r) {
1392 int32_t sym = r->symbol;
1394 if (sym >= GLOBAL_TEMP_BASE)
1395 sym += global_offset;
1397 p = entry;
1398 WRITEDLONG(p, r->address);
1399 WRITELONG(p, r->type);
1400 WRITELONG(p, sym);
1401 WRITEDLONG(p, r->offset);
1402 saa_wbytes(s, entry, 24L);
1403 *len += 24;
1405 r = r->next;
1408 return s;
1411 static void elf_section_header(int name, int type, uint64_t flags,
1412 void *data, bool is_saa, uint64_t datalen,
1413 int link, int info, int align, int eltsize)
1415 elf_sects[elf_nsect].data = data;
1416 elf_sects[elf_nsect].len = datalen;
1417 elf_sects[elf_nsect].is_saa = is_saa;
1418 elf_nsect++;
1420 fwriteint32_t((int32_t)name, ofile);
1421 fwriteint32_t((int32_t)type, ofile);
1422 fwriteint64_t((int64_t)flags, ofile);
1423 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1424 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1425 fwriteint64_t(datalen, ofile);
1426 if (data)
1427 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1428 fwriteint32_t((int32_t)link, ofile);
1429 fwriteint32_t((int32_t)info, ofile);
1430 fwriteint64_t((int64_t)align, ofile);
1431 fwriteint64_t((int64_t)eltsize, ofile);
1434 static void elf_write_sections(void)
1436 int i;
1437 for (i = 0; i < elf_nsect; i++)
1438 if (elf_sects[i].data) {
1439 int32_t len = elf_sects[i].len;
1440 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1441 int32_t align = reallen - len;
1442 if (elf_sects[i].is_saa)
1443 saa_fpwrite(elf_sects[i].data, ofile);
1444 else
1445 fwrite(elf_sects[i].data, len, 1, ofile);
1446 fwritezero(align, ofile);
1450 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1452 saa_wbytes(sect->data, data, len);
1453 sect->len += len;
1455 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1457 saa_writeaddr(sect->data, data, len);
1458 sect->len += len;
1461 static int32_t elf_segbase(int32_t segment)
1463 return segment;
1466 static int elf_directive(enum directives directive, char *value, int pass)
1468 bool err;
1469 int64_t n;
1470 char *p;
1472 switch (directive) {
1473 case D_OSABI:
1474 if (pass == 2)
1475 return 1; /* ignore in pass 2 */
1477 n = readnum(value, &err);
1478 if (err) {
1479 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1480 return 1;
1482 if (n < 0 || n > 255) {
1483 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1484 return 1;
1486 elf_osabi = n;
1487 elf_abiver = 0;
1489 if ((p = strchr(value,',')) == NULL)
1490 return 1;
1492 n = readnum(p+1, &err);
1493 if (err || n < 0 || n > 255) {
1494 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1495 return 1;
1498 elf_abiver = n;
1499 return 1;
1501 default:
1502 return 0;
1506 static void elf_filename(char *inname, char *outname)
1508 strcpy(elf_module, inname);
1509 standard_extension(inname, outname, ".o");
1512 extern macros_t elf_stdmac[];
1514 static int elf_set_info(enum geninfo type, char **val)
1516 (void)type;
1517 (void)val;
1518 return 0;
1520 static struct dfmt df_dwarf = {
1521 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1522 "dwarf",
1523 dwarf64_init,
1524 dwarf64_linenum,
1525 debug64_deflabel,
1526 debug64_directive,
1527 debug64_typevalue,
1528 dwarf64_output,
1529 dwarf64_cleanup
1531 static struct dfmt df_stabs = {
1532 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1533 "stabs",
1534 null_debug_init,
1535 stabs64_linenum,
1536 debug64_deflabel,
1537 debug64_directive,
1538 debug64_typevalue,
1539 stabs64_output,
1540 stabs64_cleanup
1543 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1545 struct ofmt of_elf64 = {
1546 "ELF64 (x86_64) object files (e.g. Linux)",
1547 "elf64",
1549 elf64_debugs_arr,
1550 &df_stabs,
1551 elf_stdmac,
1552 elf_init,
1553 elf_set_info,
1554 elf_out,
1555 elf_deflabel,
1556 elf_section_names,
1557 elf_segbase,
1558 elf_directive,
1559 elf_filename,
1560 elf_cleanup
1563 /* common debugging routines */
1564 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1565 int is_global, char *special)
1567 (void)name;
1568 (void)segment;
1569 (void)offset;
1570 (void)is_global;
1571 (void)special;
1574 static void debug64_directive(const char *directive, const char *params)
1576 (void)directive;
1577 (void)params;
1580 static void debug64_typevalue(int32_t type)
1582 int32_t stype, ssize;
1583 switch (TYM_TYPE(type)) {
1584 case TY_LABEL:
1585 ssize = 0;
1586 stype = STT_NOTYPE;
1587 break;
1588 case TY_BYTE:
1589 ssize = 1;
1590 stype = STT_OBJECT;
1591 break;
1592 case TY_WORD:
1593 ssize = 2;
1594 stype = STT_OBJECT;
1595 break;
1596 case TY_DWORD:
1597 ssize = 4;
1598 stype = STT_OBJECT;
1599 break;
1600 case TY_FLOAT:
1601 ssize = 4;
1602 stype = STT_OBJECT;
1603 break;
1604 case TY_QWORD:
1605 ssize = 8;
1606 stype = STT_OBJECT;
1607 break;
1608 case TY_TBYTE:
1609 ssize = 10;
1610 stype = STT_OBJECT;
1611 break;
1612 case TY_OWORD:
1613 ssize = 16;
1614 stype = STT_OBJECT;
1615 break;
1616 case TY_YWORD:
1617 ssize = 32;
1618 stype = STT_OBJECT;
1619 break;
1620 case TY_COMMON:
1621 ssize = 0;
1622 stype = STT_COMMON;
1623 break;
1624 case TY_SEG:
1625 ssize = 0;
1626 stype = STT_SECTION;
1627 break;
1628 case TY_EXTERN:
1629 ssize = 0;
1630 stype = STT_NOTYPE;
1631 break;
1632 case TY_EQU:
1633 ssize = 0;
1634 stype = STT_NOTYPE;
1635 break;
1636 default:
1637 ssize = 0;
1638 stype = STT_NOTYPE;
1639 break;
1641 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1642 lastsym->size = ssize;
1643 lastsym->type = stype;
1647 /* stabs debugging routines */
1649 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1651 (void)segto;
1652 if (!stabs_filename) {
1653 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1654 strcpy(stabs_filename, filename);
1655 } else {
1656 if (strcmp(stabs_filename, filename)) {
1657 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1658 in fact, this leak comes in quite handy to maintain a list of files
1659 encountered so far in the symbol lines... */
1661 /* why not nasm_free(stabs_filename); we're done with the old one */
1663 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1664 strcpy(stabs_filename, filename);
1667 debug_immcall = 1;
1668 currentline = linenumber;
1672 static void stabs64_output(int type, void *param)
1674 struct symlininfo *s;
1675 struct linelist *el;
1676 if (type == TY_DEBUGSYMLIN) {
1677 if (debug_immcall) {
1678 s = (struct symlininfo *)param;
1679 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1680 return; /* line info is only collected for executable sections */
1681 numlinestabs++;
1682 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1683 el->info.offset = s->offset;
1684 el->info.section = s->section;
1685 el->info.name = s->name;
1686 el->line = currentline;
1687 el->filename = stabs_filename;
1688 el->next = 0;
1689 if (stabslines) {
1690 stabslines->last->next = el;
1691 stabslines->last = el;
1692 } else {
1693 stabslines = el;
1694 stabslines->last = el;
1698 debug_immcall = 0;
1701 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1702 do { \
1703 WRITELONG(p,n_strx); \
1704 WRITECHAR(p,n_type); \
1705 WRITECHAR(p,n_other); \
1706 WRITESHORT(p,n_desc); \
1707 WRITELONG(p,n_value); \
1708 } while (0)
1710 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1712 static void stabs64_generate(void)
1714 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1715 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1716 char **allfiles;
1717 int *fileidx;
1719 struct linelist *ptr;
1721 ptr = stabslines;
1723 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1724 for (i = 0; i < numlinestabs; i++)
1725 allfiles[i] = 0;
1726 numfiles = 0;
1727 while (ptr) {
1728 if (numfiles == 0) {
1729 allfiles[0] = ptr->filename;
1730 numfiles++;
1731 } else {
1732 for (i = 0; i < numfiles; i++) {
1733 if (!strcmp(allfiles[i], ptr->filename))
1734 break;
1736 if (i >= numfiles) {
1737 allfiles[i] = ptr->filename;
1738 numfiles++;
1741 ptr = ptr->next;
1743 strsize = 1;
1744 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1745 for (i = 0; i < numfiles; i++) {
1746 fileidx[i] = strsize;
1747 strsize += strlen(allfiles[i]) + 1;
1749 mainfileindex = 0;
1750 for (i = 0; i < numfiles; i++) {
1751 if (!strcmp(allfiles[i], elf_module)) {
1752 mainfileindex = i;
1753 break;
1758 * worst case size of the stab buffer would be:
1759 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1760 * plus one "ending" entry
1762 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1763 sizeof(struct stabentry));
1764 ssbuf = (uint8_t *)nasm_malloc(strsize);
1765 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1766 rptr = rbuf;
1768 for (i = 0; i < numfiles; i++)
1769 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1770 ssbuf[0] = 0;
1772 stabstrlen = strsize; /* set global variable for length of stab strings */
1774 sptr = sbuf;
1775 ptr = stabslines;
1776 numstabs = 0;
1778 if (ptr) {
1780 * this is the first stab, its strx points to the filename of the
1781 * the source-file, the n_desc field should be set to the number
1782 * of remaining stabs
1784 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1786 /* this is the stab for the main source file */
1787 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1789 /* relocation table entry */
1792 * Since the symbol table has two entries before
1793 * the section symbols, the index in the info.section
1794 * member must be adjusted by adding 2
1797 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1798 WRITELONG(rptr, R_X86_64_32);
1799 WRITELONG(rptr, ptr->info.section + 2);
1801 numstabs++;
1802 currfile = mainfileindex;
1805 while (ptr) {
1806 if (strcmp(allfiles[currfile], ptr->filename)) {
1807 /* oops file has changed... */
1808 for (i = 0; i < numfiles; i++)
1809 if (!strcmp(allfiles[i], ptr->filename))
1810 break;
1811 currfile = i;
1812 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1813 ptr->info.offset);
1814 numstabs++;
1816 /* relocation table entry */
1818 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1819 WRITELONG(rptr, R_X86_64_32);
1820 WRITELONG(rptr, ptr->info.section + 2);
1823 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1824 numstabs++;
1826 /* relocation table entry */
1828 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1829 WRITELONG(rptr, R_X86_64_32);
1830 WRITELONG(rptr, ptr->info.section + 2);
1832 ptr = ptr->next;
1836 /* this is an "ending" token */
1837 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1838 numstabs++;
1840 ((struct stabentry *)sbuf)->n_desc = numstabs;
1842 nasm_free(allfiles);
1843 nasm_free(fileidx);
1845 stablen = (sptr - sbuf);
1846 stabrellen = (rptr - rbuf);
1847 stabrelbuf = rbuf;
1848 stabbuf = sbuf;
1849 stabstrbuf = ssbuf;
1852 static void stabs64_cleanup(void)
1854 struct linelist *ptr, *del;
1855 if (!stabslines)
1856 return;
1858 ptr = stabslines;
1859 while (ptr) {
1860 del = ptr;
1861 ptr = ptr->next;
1862 nasm_free(del);
1865 nasm_free(stabbuf);
1866 nasm_free(stabrelbuf);
1867 nasm_free(stabstrbuf);
1870 /* dwarf routines */
1872 static void dwarf64_init(void)
1874 ndebugs = 3; /* 3 debug symbols */
1877 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1878 int32_t segto)
1880 (void)segto;
1881 dwarf64_findfile(filename);
1882 debug_immcall = 1;
1883 currentline = linenumber;
1886 /* called from elf_out with type == TY_DEBUGSYMLIN */
1887 static void dwarf64_output(int type, void *param)
1889 int ln, aa, inx, maxln, soc;
1890 struct symlininfo *s;
1891 struct SAA *plinep;
1893 (void)type;
1895 s = (struct symlininfo *)param;
1897 /* line number info is only gathered for executable sections */
1898 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1899 return;
1901 /* Check if section index has changed */
1902 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1903 dwarf64_findsect(s->section);
1905 /* do nothing unless line or file has changed */
1906 if (!debug_immcall)
1907 return;
1909 ln = currentline - dwarf_csect->line;
1910 aa = s->offset - dwarf_csect->offset;
1911 inx = dwarf_clist->line;
1912 plinep = dwarf_csect->psaa;
1913 /* check for file change */
1914 if (!(inx == dwarf_csect->file)) {
1915 saa_write8(plinep,DW_LNS_set_file);
1916 saa_write8(plinep,inx);
1917 dwarf_csect->file = inx;
1919 /* check for line change */
1920 if (ln) {
1921 /* test if in range of special op code */
1922 maxln = line_base + line_range;
1923 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1924 if (ln >= line_base && ln < maxln && soc < 256) {
1925 saa_write8(plinep,soc);
1926 } else {
1927 saa_write8(plinep,DW_LNS_advance_line);
1928 saa_wleb128s(plinep,ln);
1929 if (aa) {
1930 saa_write8(plinep,DW_LNS_advance_pc);
1931 saa_wleb128u(plinep,aa);
1934 dwarf_csect->line = currentline;
1935 dwarf_csect->offset = s->offset;
1938 /* show change handled */
1939 debug_immcall = 0;
1943 static void dwarf64_generate(void)
1945 uint8_t *pbuf;
1946 int indx;
1947 struct linelist *ftentry;
1948 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1949 struct SAA *parangesrel, *plinesrel, *pinforel;
1950 struct sectlist *psect;
1951 size_t saalen, linepoff, totlen, highaddr;
1953 /* write epilogues for each line program range */
1954 /* and build aranges section */
1955 paranges = saa_init(1L);
1956 parangesrel = saa_init(1L);
1957 saa_write16(paranges,3); /* dwarf version */
1958 saa_write64(parangesrel, paranges->datalen+4);
1959 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1960 saa_write64(parangesrel, 0);
1961 saa_write32(paranges,0); /* offset into info */
1962 saa_write8(paranges,8); /* pointer size */
1963 saa_write8(paranges,0); /* not segmented */
1964 saa_write32(paranges,0); /* padding */
1965 /* iterate though sectlist entries */
1966 psect = dwarf_fsect;
1967 totlen = 0;
1968 highaddr = 0;
1969 for (indx = 0; indx < dwarf_nsections; indx++)
1971 plinep = psect->psaa;
1972 /* Line Number Program Epilogue */
1973 saa_write8(plinep,2); /* std op 2 */
1974 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1975 saa_write8(plinep,DW_LNS_extended_op);
1976 saa_write8(plinep,1); /* operand length */
1977 saa_write8(plinep,DW_LNE_end_sequence);
1978 totlen += plinep->datalen;
1979 /* range table relocation entry */
1980 saa_write64(parangesrel, paranges->datalen + 4);
1981 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1982 saa_write64(parangesrel, (uint64_t) 0);
1983 /* range table entry */
1984 saa_write64(paranges,0x0000); /* range start */
1985 saa_write64(paranges,sects[psect->section]->len); /* range length */
1986 highaddr += sects[psect->section]->len;
1987 /* done with this entry */
1988 psect = psect->next;
1990 saa_write64(paranges,0); /* null address */
1991 saa_write64(paranges,0); /* null length */
1992 saalen = paranges->datalen;
1993 arangeslen = saalen + 4;
1994 arangesbuf = pbuf = nasm_malloc(arangeslen);
1995 WRITELONG(pbuf,saalen); /* initial length */
1996 saa_rnbytes(paranges, pbuf, saalen);
1997 saa_free(paranges);
1999 /* build rela.aranges section */
2000 arangesrellen = saalen = parangesrel->datalen;
2001 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2002 saa_rnbytes(parangesrel, pbuf, saalen);
2003 saa_free(parangesrel);
2005 /* build pubnames section */
2006 ppubnames = saa_init(1L);
2007 saa_write16(ppubnames,3); /* dwarf version */
2008 saa_write32(ppubnames,0); /* offset into info */
2009 saa_write32(ppubnames,0); /* space used in info */
2010 saa_write32(ppubnames,0); /* end of list */
2011 saalen = ppubnames->datalen;
2012 pubnameslen = saalen + 4;
2013 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2014 WRITELONG(pbuf,saalen); /* initial length */
2015 saa_rnbytes(ppubnames, pbuf, saalen);
2016 saa_free(ppubnames);
2018 /* build info section */
2019 pinfo = saa_init(1L);
2020 pinforel = saa_init(1L);
2021 saa_write16(pinfo,3); /* dwarf version */
2022 saa_write64(pinforel, pinfo->datalen + 4);
2023 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2024 saa_write64(pinforel, 0);
2025 saa_write32(pinfo,0); /* offset into abbrev */
2026 saa_write8(pinfo,8); /* pointer size */
2027 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2028 saa_write64(pinforel, pinfo->datalen + 4);
2029 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2030 saa_write64(pinforel, 0);
2031 saa_write64(pinfo,0); /* DW_AT_low_pc */
2032 saa_write64(pinforel, pinfo->datalen + 4);
2033 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2034 saa_write64(pinforel, 0);
2035 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2036 saa_write64(pinforel, pinfo->datalen + 4);
2037 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2038 saa_write64(pinforel, 0);
2039 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2040 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2041 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2042 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2043 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2044 saa_write64(pinforel, pinfo->datalen + 4);
2045 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2046 saa_write64(pinforel, 0);
2047 saa_write64(pinfo,0); /* DW_AT_low_pc */
2048 saa_write64(pinfo,0); /* DW_AT_frame_base */
2049 saa_write8(pinfo,0); /* end of entries */
2050 saalen = pinfo->datalen;
2051 infolen = saalen + 4;
2052 infobuf = pbuf = nasm_malloc(infolen);
2053 WRITELONG(pbuf,saalen); /* initial length */
2054 saa_rnbytes(pinfo, pbuf, saalen);
2055 saa_free(pinfo);
2057 /* build rela.info section */
2058 inforellen = saalen = pinforel->datalen;
2059 inforelbuf = pbuf = nasm_malloc(inforellen);
2060 saa_rnbytes(pinforel, pbuf, saalen);
2061 saa_free(pinforel);
2063 /* build abbrev section */
2064 pabbrev = saa_init(1L);
2065 saa_write8(pabbrev,1); /* entry number LEB128u */
2066 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2067 saa_write8(pabbrev,1); /* has children */
2068 /* the following attributes and forms are all LEB128u values */
2069 saa_write8(pabbrev,DW_AT_low_pc);
2070 saa_write8(pabbrev,DW_FORM_addr);
2071 saa_write8(pabbrev,DW_AT_high_pc);
2072 saa_write8(pabbrev,DW_FORM_addr);
2073 saa_write8(pabbrev,DW_AT_stmt_list);
2074 saa_write8(pabbrev,DW_FORM_data4);
2075 saa_write8(pabbrev,DW_AT_name);
2076 saa_write8(pabbrev,DW_FORM_string);
2077 saa_write8(pabbrev,DW_AT_producer);
2078 saa_write8(pabbrev,DW_FORM_string);
2079 saa_write8(pabbrev,DW_AT_language);
2080 saa_write8(pabbrev,DW_FORM_data2);
2081 saa_write16(pabbrev,0); /* end of entry */
2082 /* LEB128u usage same as above */
2083 saa_write8(pabbrev,2); /* entry number */
2084 saa_write8(pabbrev,DW_TAG_subprogram);
2085 saa_write8(pabbrev,0); /* no children */
2086 saa_write8(pabbrev,DW_AT_low_pc);
2087 saa_write8(pabbrev,DW_FORM_addr);
2088 saa_write8(pabbrev,DW_AT_frame_base);
2089 saa_write8(pabbrev,DW_FORM_data4);
2090 saa_write16(pabbrev,0); /* end of entry */
2091 abbrevlen = saalen = pabbrev->datalen;
2092 abbrevbuf = pbuf = nasm_malloc(saalen);
2093 saa_rnbytes(pabbrev, pbuf, saalen);
2094 saa_free(pabbrev);
2096 /* build line section */
2097 /* prolog */
2098 plines = saa_init(1L);
2099 saa_write8(plines,1); /* Minimum Instruction Length */
2100 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2101 saa_write8(plines,line_base); /* Line Base */
2102 saa_write8(plines,line_range); /* Line Range */
2103 saa_write8(plines,opcode_base); /* Opcode Base */
2104 /* standard opcode lengths (# of LEB128u operands) */
2105 saa_write8(plines,0); /* Std opcode 1 length */
2106 saa_write8(plines,1); /* Std opcode 2 length */
2107 saa_write8(plines,1); /* Std opcode 3 length */
2108 saa_write8(plines,1); /* Std opcode 4 length */
2109 saa_write8(plines,1); /* Std opcode 5 length */
2110 saa_write8(plines,0); /* Std opcode 6 length */
2111 saa_write8(plines,0); /* Std opcode 7 length */
2112 saa_write8(plines,0); /* Std opcode 8 length */
2113 saa_write8(plines,1); /* Std opcode 9 length */
2114 saa_write8(plines,0); /* Std opcode 10 length */
2115 saa_write8(plines,0); /* Std opcode 11 length */
2116 saa_write8(plines,1); /* Std opcode 12 length */
2117 /* Directory Table */
2118 saa_write8(plines,0); /* End of table */
2119 /* File Name Table */
2120 ftentry = dwarf_flist;
2121 for (indx = 0;indx<dwarf_numfiles;indx++)
2123 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2124 saa_write8(plines,0); /* directory LEB128u */
2125 saa_write8(plines,0); /* time LEB128u */
2126 saa_write8(plines,0); /* size LEB128u */
2127 ftentry = ftentry->next;
2129 saa_write8(plines,0); /* End of table */
2130 linepoff = plines->datalen;
2131 linelen = linepoff + totlen + 10;
2132 linebuf = pbuf = nasm_malloc(linelen);
2133 WRITELONG(pbuf,linelen-4); /* initial length */
2134 WRITESHORT(pbuf,3); /* dwarf version */
2135 WRITELONG(pbuf,linepoff); /* offset to line number program */
2136 /* write line header */
2137 saalen = linepoff;
2138 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2139 pbuf += linepoff;
2140 saa_free(plines);
2141 /* concatonate line program ranges */
2142 linepoff += 13;
2143 plinesrel = saa_init(1L);
2144 psect = dwarf_fsect;
2145 for (indx = 0; indx < dwarf_nsections; indx++) {
2146 saa_write64(plinesrel, linepoff);
2147 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2148 saa_write64(plinesrel, (uint64_t) 0);
2149 plinep = psect->psaa;
2150 saalen = plinep->datalen;
2151 saa_rnbytes(plinep, pbuf, saalen);
2152 pbuf += saalen;
2153 linepoff += saalen;
2154 saa_free(plinep);
2155 /* done with this entry */
2156 psect = psect->next;
2160 /* build rela.lines section */
2161 linerellen =saalen = plinesrel->datalen;
2162 linerelbuf = pbuf = nasm_malloc(linerellen);
2163 saa_rnbytes(plinesrel, pbuf, saalen);
2164 saa_free(plinesrel);
2166 /* build frame section */
2167 framelen = 4;
2168 framebuf = pbuf = nasm_malloc(framelen);
2169 WRITELONG(pbuf,framelen-4); /* initial length */
2171 /* build loc section */
2172 loclen = 16;
2173 locbuf = pbuf = nasm_malloc(loclen);
2174 WRITEDLONG(pbuf,0); /* null beginning offset */
2175 WRITEDLONG(pbuf,0); /* null ending offset */
2178 static void dwarf64_cleanup(void)
2180 nasm_free(arangesbuf);
2181 nasm_free(arangesrelbuf);
2182 nasm_free(pubnamesbuf);
2183 nasm_free(infobuf);
2184 nasm_free(inforelbuf);
2185 nasm_free(abbrevbuf);
2186 nasm_free(linebuf);
2187 nasm_free(linerelbuf);
2188 nasm_free(framebuf);
2189 nasm_free(locbuf);
2192 static void dwarf64_findfile(const char * fname)
2194 int finx;
2195 struct linelist *match;
2197 /* return if fname is current file name */
2198 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2199 return;
2201 /* search for match */
2202 match = 0;
2203 if (dwarf_flist) {
2204 match = dwarf_flist;
2205 for (finx = 0; finx < dwarf_numfiles; finx++) {
2206 if (!(strcmp(fname, match->filename))) {
2207 dwarf_clist = match;
2208 return;
2213 /* add file name to end of list */
2214 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2215 dwarf_numfiles++;
2216 dwarf_clist->line = dwarf_numfiles;
2217 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2218 strcpy(dwarf_clist->filename,fname);
2219 dwarf_clist->next = 0;
2220 if (!dwarf_flist) { /* if first entry */
2221 dwarf_flist = dwarf_elist = dwarf_clist;
2222 dwarf_clist->last = 0;
2223 } else { /* chain to previous entry */
2224 dwarf_elist->next = dwarf_clist;
2225 dwarf_elist = dwarf_clist;
2229 static void dwarf64_findsect(const int index)
2231 int sinx;
2232 struct sectlist *match;
2233 struct SAA *plinep;
2235 /* return if index is current section index */
2236 if (dwarf_csect && (dwarf_csect->section == index))
2237 return;
2239 /* search for match */
2240 match = 0;
2241 if (dwarf_fsect) {
2242 match = dwarf_fsect;
2243 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2244 if ((match->section == index)) {
2245 dwarf_csect = match;
2246 return;
2248 match = match->next;
2252 /* add entry to end of list */
2253 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2254 dwarf_nsections++;
2255 dwarf_csect->psaa = plinep = saa_init(1L);
2256 dwarf_csect->line = 1;
2257 dwarf_csect->offset = 0;
2258 dwarf_csect->file = 1;
2259 dwarf_csect->section = index;
2260 dwarf_csect->next = 0;
2261 /* set relocatable address at start of line program */
2262 saa_write8(plinep,DW_LNS_extended_op);
2263 saa_write8(plinep,9); /* operand length */
2264 saa_write8(plinep,DW_LNE_set_address);
2265 saa_write64(plinep,0); /* Start Address */
2267 if (!dwarf_fsect) { /* if first entry */
2268 dwarf_fsect = dwarf_esect = dwarf_csect;
2269 dwarf_csect->last = 0;
2270 } else { /* chain to previous entry */
2271 dwarf_esect->next = dwarf_csect;
2272 dwarf_esect = dwarf_csect;
2276 #endif /* OF_ELF */