Remove function pointers in output, simplify error handling
[nasm/sigaren-mirror.git] / output / outelf64.c
blob1e73fccd9b07d3ef63db965dc44ea282ba75b1c3
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/elf64.h"
58 #include "output/dwarf.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, &of_elf64,
267 nasm_error);
268 elf_gotoff_sect = seg_alloc();
269 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
270 nasm_error);
271 elf_got_sect = seg_alloc();
272 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
273 nasm_error);
274 elf_plt_sect = seg_alloc();
275 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
276 nasm_error);
277 elf_sym_sect = seg_alloc();
278 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
279 nasm_error);
280 elf_gottpoff_sect = seg_alloc();
281 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false, &of_elf64,
282 nasm_error);
284 def_seg = seg_alloc();
288 static void elf_cleanup(int debuginfo)
290 struct Reloc *r;
291 int i;
293 (void)debuginfo;
295 elf_write();
296 for (i = 0; i < nsects; i++) {
297 if (sects[i]->type != SHT_NOBITS)
298 saa_free(sects[i]->data);
299 if (sects[i]->head)
300 saa_free(sects[i]->rel);
301 while (sects[i]->head) {
302 r = sects[i]->head;
303 sects[i]->head = sects[i]->head->next;
304 nasm_free(r);
307 nasm_free(sects);
308 saa_free(syms);
309 raa_free(bsym);
310 saa_free(strs);
311 if (of_elf64.current_dfmt) {
312 of_elf64.current_dfmt->cleanup();
315 /* add entry to the elf .shstrtab section */
316 static void add_sectname(char *firsthalf, char *secondhalf)
318 int len = strlen(firsthalf) + strlen(secondhalf);
319 while (shstrtablen + len + 1 > shstrtabsize)
320 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
321 strcpy(shstrtab + shstrtablen, firsthalf);
322 strcat(shstrtab + shstrtablen, secondhalf);
323 shstrtablen += len + 1;
326 static int elf_make_section(char *name, int type, int flags, int align)
328 struct Section *s;
330 s = nasm_malloc(sizeof(*s));
332 if (type != SHT_NOBITS)
333 s->data = saa_init(1L);
334 s->head = NULL;
335 s->tail = &s->head;
336 s->len = s->size = 0;
337 s->nrelocs = 0;
338 if (!strcmp(name, ".text"))
339 s->index = def_seg;
340 else
341 s->index = seg_alloc();
342 add_sectname("", name);
343 s->name = nasm_malloc(1 + strlen(name));
344 strcpy(s->name, name);
345 s->type = type;
346 s->flags = flags;
347 s->align = align;
348 s->gsyms = NULL;
350 if (nsects >= sectlen)
351 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
352 sects[nsects++] = s;
354 return nsects - 1;
357 static int32_t elf_section_names(char *name, int pass, int *bits)
359 char *p;
360 uint32_t flags, flags_and, flags_or;
361 uint64_t align;
362 int type, i;
365 * Default is 64 bits.
367 if (!name) {
368 *bits = 64;
369 return def_seg;
372 p = name;
373 while (*p && !nasm_isspace(*p))
374 p++;
375 if (*p)
376 *p++ = '\0';
377 flags_and = flags_or = type = align = 0;
379 while (*p && nasm_isspace(*p))
380 p++;
381 while (*p) {
382 char *q = p;
383 while (*p && !nasm_isspace(*p))
384 p++;
385 if (*p)
386 *p++ = '\0';
387 while (*p && nasm_isspace(*p))
388 p++;
390 if (!nasm_strnicmp(q, "align=", 6)) {
391 align = atoi(q + 6);
392 if (align == 0)
393 align = 1;
394 if ((align - 1) & align) { /* means it's not a power of two */
395 nasm_error(ERR_NONFATAL, "section alignment %d is not"
396 " a power of two", align);
397 align = 1;
399 } else if (!nasm_stricmp(q, "alloc")) {
400 flags_and |= SHF_ALLOC;
401 flags_or |= SHF_ALLOC;
402 } else if (!nasm_stricmp(q, "noalloc")) {
403 flags_and |= SHF_ALLOC;
404 flags_or &= ~SHF_ALLOC;
405 } else if (!nasm_stricmp(q, "exec")) {
406 flags_and |= SHF_EXECINSTR;
407 flags_or |= SHF_EXECINSTR;
408 } else if (!nasm_stricmp(q, "noexec")) {
409 flags_and |= SHF_EXECINSTR;
410 flags_or &= ~SHF_EXECINSTR;
411 } else if (!nasm_stricmp(q, "write")) {
412 flags_and |= SHF_WRITE;
413 flags_or |= SHF_WRITE;
414 } else if (!nasm_stricmp(q, "tls")) {
415 flags_and |= SHF_TLS;
416 flags_or |= SHF_TLS;
417 } else if (!nasm_stricmp(q, "nowrite")) {
418 flags_and |= SHF_WRITE;
419 flags_or &= ~SHF_WRITE;
420 } else if (!nasm_stricmp(q, "progbits")) {
421 type = SHT_PROGBITS;
422 } else if (!nasm_stricmp(q, "nobits")) {
423 type = SHT_NOBITS;
424 } else if (pass == 1) {
425 nasm_error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
426 " declaration of section `%s'", q, name);
430 if (!strcmp(name, ".shstrtab") ||
431 !strcmp(name, ".symtab") ||
432 !strcmp(name, ".strtab")) {
433 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
434 "name `%s'", name);
435 return NO_SEG;
438 for (i = 0; i < nsects; i++)
439 if (!strcmp(name, sects[i]->name))
440 break;
441 if (i == nsects) {
442 const struct elf_known_section *ks = elf_known_sections;
444 while (ks->name) {
445 if (!strcmp(name, ks->name))
446 break;
447 ks++;
450 type = type ? type : ks->type;
451 align = align ? align : ks->align;
452 flags = (ks->flags & ~flags_and) | flags_or;
454 i = elf_make_section(name, type, flags, align);
455 } else if (pass == 1) {
456 if ((type && sects[i]->type != type)
457 || (align && sects[i]->align != align)
458 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
459 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
460 " redeclaration of section `%s'", name);
463 return sects[i]->index;
466 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
467 int is_global, char *special)
469 int pos = strslen;
470 struct Symbol *sym;
471 bool special_used = false;
473 #if defined(DEBUG) && DEBUG>2
474 fprintf(stderr,
475 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
476 name, segment, offset, is_global, special);
477 #endif
478 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
480 * This is a NASM special symbol. We never allow it into
481 * the ELF symbol table, even if it's a valid one. If it
482 * _isn't_ a valid one, we should barf immediately.
484 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
485 strcmp(name, "..got") && strcmp(name, "..plt") &&
486 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
487 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
488 return;
491 if (is_global == 3) {
492 struct Symbol **s;
494 * Fix up a forward-reference symbol size from the first
495 * pass.
497 for (s = &fwds; *s; s = &(*s)->nextfwd)
498 if (!strcmp((*s)->name, name)) {
499 struct tokenval tokval;
500 expr *e;
501 char *p = special;
503 while (*p && !nasm_isspace(*p))
504 p++;
505 while (*p && nasm_isspace(*p))
506 p++;
507 stdscan_reset();
508 stdscan_bufptr = p;
509 tokval.t_type = TOKEN_INVALID;
510 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
511 if (e) {
512 if (!is_simple(e))
513 nasm_error(ERR_NONFATAL, "cannot use relocatable"
514 " expression as symbol size");
515 else
516 (*s)->size = reloc_value(e);
520 * Remove it from the list of unresolved sizes.
522 nasm_free((*s)->name);
523 *s = (*s)->nextfwd;
524 return;
526 return; /* it wasn't an important one */
529 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
530 strslen += 1 + strlen(name);
532 lastsym = sym = saa_wstruct(syms);
534 memset(&sym->symv, 0, sizeof(struct rbtree));
536 sym->strpos = pos;
537 sym->type = is_global ? SYM_GLOBAL : 0;
538 sym->other = STV_DEFAULT;
539 sym->size = 0;
540 if (segment == NO_SEG)
541 sym->section = SHN_ABS;
542 else {
543 int i;
544 sym->section = SHN_UNDEF;
545 if (nsects == 0 && segment == def_seg) {
546 int tempint;
547 if (segment != elf_section_names(".text", 2, &tempint))
548 nasm_error(ERR_PANIC,
549 "strange segment conditions in ELF driver");
550 sym->section = nsects;
551 } else {
552 for (i = 0; i < nsects; i++)
553 if (segment == sects[i]->index) {
554 sym->section = i + 1;
555 break;
560 if (is_global == 2) {
561 sym->size = offset;
562 sym->symv.key = 0;
563 sym->section = SHN_COMMON;
565 * We have a common variable. Check the special text to see
566 * if it's a valid number and power of two; if so, store it
567 * as the alignment for the common variable.
569 if (special) {
570 bool err;
571 sym->symv.key = readnum(special, &err);
572 if (err)
573 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
574 " valid number", special);
575 else if ((sym->symv.key | (sym->symv.key - 1))
576 != 2 * sym->symv.key - 1)
577 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
578 " power of two", special);
580 special_used = true;
581 } else
582 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
584 if (sym->type == SYM_GLOBAL) {
586 * If sym->section == SHN_ABS, then the first line of the
587 * else section would cause a core dump, because its a reference
588 * beyond the end of the section array.
589 * This behaviour is exhibited by this code:
590 * GLOBAL crash_nasm
591 * crash_nasm equ 0
592 * To avoid such a crash, such requests are silently discarded.
593 * This may not be the best solution.
595 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
596 bsym = raa_write(bsym, segment, nglobs);
597 } else if (sym->section != SHN_ABS) {
599 * This is a global symbol; so we must add it to the rbtree
600 * of global symbols in its section.
602 * In addition, we check the special text for symbol
603 * type and size information.
605 sects[sym->section-1]->gsyms =
606 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
608 if (special) {
609 int n = strcspn(special, " \t");
611 if (!nasm_strnicmp(special, "function", n))
612 sym->type |= STT_FUNC;
613 else if (!nasm_strnicmp(special, "data", n) ||
614 !nasm_strnicmp(special, "object", n))
615 sym->type |= STT_OBJECT;
616 else if (!nasm_strnicmp(special, "notype", n))
617 sym->type |= STT_NOTYPE;
618 else
619 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
620 n, special);
621 special += n;
623 while (nasm_isspace(*special))
624 ++special;
625 if (*special) {
626 n = strcspn(special, " \t");
627 if (!nasm_strnicmp(special, "default", n))
628 sym->other = STV_DEFAULT;
629 else if (!nasm_strnicmp(special, "internal", n))
630 sym->other = STV_INTERNAL;
631 else if (!nasm_strnicmp(special, "hidden", n))
632 sym->other = STV_HIDDEN;
633 else if (!nasm_strnicmp(special, "protected", n))
634 sym->other = STV_PROTECTED;
635 else
636 n = 0;
637 special += n;
640 if (*special) {
641 struct tokenval tokval;
642 expr *e;
643 int fwd = 0;
644 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
646 while (special[n] && nasm_isspace(special[n]))
647 n++;
649 * We have a size expression; attempt to
650 * evaluate it.
652 stdscan_reset();
653 stdscan_bufptr = special + n;
654 tokval.t_type = TOKEN_INVALID;
655 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
656 NULL);
657 if (fwd) {
658 sym->nextfwd = fwds;
659 fwds = sym;
660 sym->name = nasm_strdup(name);
661 } else if (e) {
662 if (!is_simple(e))
663 nasm_error(ERR_NONFATAL, "cannot use relocatable"
664 " expression as symbol size");
665 else
666 sym->size = reloc_value(e);
668 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
670 special_used = true;
673 * If TLS segment, mark symbol accordingly.
675 if (sects[sym->section - 1]->flags & SHF_TLS) {
676 sym->type &= 0xf0;
677 sym->type |= STT_TLS;
680 sym->globnum = nglobs;
681 nglobs++;
682 } else
683 nlocals++;
685 if (special && !special_used)
686 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
689 static void elf_add_reloc(struct Section *sect, int32_t segment,
690 int64_t offset, int type)
692 struct Reloc *r;
693 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
694 sect->tail = &r->next;
695 r->next = NULL;
697 r->address = sect->len;
698 r->offset = offset;
699 if (segment == NO_SEG)
700 r->symbol = 0;
701 else {
702 int i;
703 r->symbol = 0;
704 for (i = 0; i < nsects; i++)
705 if (segment == sects[i]->index)
706 r->symbol = i + 2;
707 if (!r->symbol)
708 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
710 r->type = type;
712 sect->nrelocs++;
716 * This routine deals with ..got and ..sym relocations: the more
717 * complicated kinds. In shared-library writing, some relocations
718 * with respect to global symbols must refer to the precise symbol
719 * rather than referring to an offset from the base of the section
720 * _containing_ the symbol. Such relocations call to this routine,
721 * which searches the symbol list for the symbol in question.
723 * R_386_GOT32 references require the _exact_ symbol address to be
724 * used; R_386_32 references can be at an offset from the symbol.
725 * The boolean argument `exact' tells us this.
727 * Return value is the adjusted value of `addr', having become an
728 * offset from the symbol rather than the section. Should always be
729 * zero when returning from an exact call.
731 * Limitation: if you define two symbols at the same place,
732 * confusion will occur.
734 * Inefficiency: we search, currently, using a linked list which
735 * isn't even necessarily sorted.
737 static void elf_add_gsym_reloc(struct Section *sect,
738 int32_t segment, uint64_t offset, int64_t pcrel,
739 int type, bool exact)
741 struct Reloc *r;
742 struct Section *s;
743 struct Symbol *sym;
744 struct rbtree *srb;
745 int i;
748 * First look up the segment/offset pair and find a global
749 * symbol corresponding to it. If it's not one of our segments,
750 * then it must be an external symbol, in which case we're fine
751 * doing a normal elf_add_reloc after first sanity-checking
752 * that the offset from the symbol is zero.
754 s = NULL;
755 for (i = 0; i < nsects; i++)
756 if (segment == sects[i]->index) {
757 s = sects[i];
758 break;
761 if (!s) {
762 if (exact && offset)
763 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
764 else
765 elf_add_reloc(sect, segment, offset - pcrel, type);
766 return;
769 srb = rb_search(s->gsyms, offset);
770 if (!srb || (exact && srb->key != offset)) {
771 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
772 " for this reference");
773 return;
775 sym = container_of(srb, struct Symbol, symv);
777 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
778 sect->tail = &r->next;
779 r->next = NULL;
781 r->address = sect->len;
782 r->offset = offset - pcrel - sym->symv.key;
783 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
784 r->type = type;
786 sect->nrelocs++;
789 static void elf_out(int32_t segto, const void *data,
790 enum out_type type, uint64_t size,
791 int32_t segment, int32_t wrt)
793 struct Section *s;
794 int64_t addr, zero;
795 int i;
796 static struct symlininfo sinfo;
798 zero = 0;
800 #if defined(DEBUG) && DEBUG>2
801 if (data) fprintf(stderr,
802 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
803 currentline, type, segment, segto, size, *(int64_t *)data);
804 else fprintf(stderr,
805 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
806 currentline, type, segment, segto, size);
807 #endif
810 * handle absolute-assembly (structure definitions)
812 if (segto == NO_SEG) {
813 if (type != OUT_RESERVE)
814 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
815 " space");
816 return;
819 s = NULL;
820 for (i = 0; i < nsects; i++)
821 if (segto == sects[i]->index) {
822 s = sects[i];
823 break;
825 if (!s) {
826 int tempint; /* ignored */
827 if (segto != elf_section_names(".text", 2, &tempint))
828 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
829 else {
830 s = sects[nsects - 1];
831 i = nsects - 1;
834 /* invoke current debug_output routine */
835 if (of_elf64.current_dfmt) {
836 sinfo.offset = s->len;
837 sinfo.section = i;
838 sinfo.segto = segto;
839 sinfo.name = s->name;
840 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
842 /* end of debugging stuff */
844 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
845 nasm_error(ERR_WARNING, "attempt to initialize memory in"
846 " BSS section `%s': ignored", s->name);
847 s->len += realsize(type, size);
848 return;
851 if (type == OUT_RESERVE) {
852 if (s->type == SHT_PROGBITS) {
853 nasm_error(ERR_WARNING, "uninitialized space declared in"
854 " non-BSS section `%s': zeroing", s->name);
855 elf_sect_write(s, NULL, size);
856 } else
857 s->len += size;
858 } else if (type == OUT_RAWDATA) {
859 if (segment != NO_SEG)
860 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
861 elf_sect_write(s, data, size);
862 } else if (type == OUT_ADDRESS) {
863 addr = *(int64_t *)data;
864 if (segment == NO_SEG) {
865 /* Do nothing */
866 } else if (segment % 2) {
867 nasm_error(ERR_NONFATAL, "ELF format does not support"
868 " segment base references");
869 } else {
870 if (wrt == NO_SEG) {
871 switch ((int)size) {
872 case 1:
873 elf_add_reloc(s, segment, addr, R_X86_64_8);
874 break;
875 case 2:
876 elf_add_reloc(s, segment, addr, R_X86_64_16);
877 break;
878 case 4:
879 elf_add_reloc(s, segment, addr, R_X86_64_32);
880 break;
881 case 8:
882 elf_add_reloc(s, segment, addr, R_X86_64_64);
883 break;
884 default:
885 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
886 break;
888 addr = 0;
889 } else if (wrt == elf_gotpc_sect + 1) {
891 * The user will supply GOT relative to $$. ELF
892 * will let us have GOT relative to $. So we
893 * need to fix up the data item by $-$$.
895 addr += s->len;
896 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
897 addr = 0;
898 } else if (wrt == elf_gotoff_sect + 1) {
899 if (size != 8) {
900 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
901 "references to be qword");
902 } else {
903 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
904 addr = 0;
906 } else if (wrt == elf_got_sect + 1) {
907 switch ((int)size) {
908 case 4:
909 elf_add_gsym_reloc(s, segment, addr, 0,
910 R_X86_64_GOT32, true);
911 addr = 0;
912 break;
913 case 8:
914 elf_add_gsym_reloc(s, segment, addr, 0,
915 R_X86_64_GOT64, true);
916 addr = 0;
917 break;
918 default:
919 nasm_error(ERR_NONFATAL, "invalid ..got reference");
920 break;
922 } else if (wrt == elf_sym_sect + 1) {
923 switch ((int)size) {
924 case 1:
925 elf_add_gsym_reloc(s, segment, addr, 0,
926 R_X86_64_8, false);
927 addr = 0;
928 break;
929 case 2:
930 elf_add_gsym_reloc(s, segment, addr, 0,
931 R_X86_64_16, false);
932 addr = 0;
933 break;
934 case 4:
935 elf_add_gsym_reloc(s, segment, addr, 0,
936 R_X86_64_32, false);
937 addr = 0;
938 break;
939 case 8:
940 elf_add_gsym_reloc(s, segment, addr, 0,
941 R_X86_64_64, false);
942 addr = 0;
943 break;
944 default:
945 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
946 break;
948 } else if (wrt == elf_plt_sect + 1) {
949 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
950 "relative PLT references");
951 } else {
952 nasm_error(ERR_NONFATAL, "ELF format does not support this"
953 " use of WRT");
956 elf_sect_writeaddr(s, addr, size);
957 } else if (type == OUT_REL2ADR) {
958 addr = *(int64_t *)data - size;
959 if (segment == segto)
960 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
961 if (segment == NO_SEG) {
962 /* Do nothing */
963 } else if (segment % 2) {
964 nasm_error(ERR_NONFATAL, "ELF format does not support"
965 " segment base references");
966 } else {
967 if (wrt == NO_SEG) {
968 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
969 addr = 0;
970 } else {
971 nasm_error(ERR_NONFATAL,
972 "Unsupported non-32-bit ELF relocation [2]");
975 elf_sect_writeaddr(s, addr, 2);
976 } else if (type == OUT_REL4ADR) {
977 addr = *(int64_t *)data - size;
978 if (segment == segto)
979 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
980 if (segment == NO_SEG) {
981 /* Do nothing */
982 } else if (segment % 2) {
983 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
984 " segment base references");
985 } else {
986 if (wrt == NO_SEG) {
987 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
988 addr = 0;
989 } else if (wrt == elf_plt_sect + 1) {
990 elf_add_gsym_reloc(s, segment, addr+size, size,
991 R_X86_64_PLT32, true);
992 addr = 0;
993 } else if (wrt == elf_gotpc_sect + 1 ||
994 wrt == elf_got_sect + 1) {
995 elf_add_gsym_reloc(s, segment, addr+size, size,
996 R_X86_64_GOTPCREL, true);
997 addr = 0;
998 } else if (wrt == elf_gotoff_sect + 1 ||
999 wrt == elf_got_sect + 1) {
1000 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1001 "qword absolute");
1002 } else if (wrt == elf_gottpoff_sect + 1) {
1003 elf_add_gsym_reloc(s, segment, addr+size, size,
1004 R_X86_64_GOTTPOFF, true);
1005 addr = 0;
1006 } else {
1007 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1008 " use of WRT");
1011 elf_sect_writeaddr(s, addr, 4);
1012 } else if (type == OUT_REL8ADR) {
1013 addr = *(int64_t *)data - size;
1014 if (segment == segto)
1015 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1016 if (segment == NO_SEG) {
1017 /* Do nothing */
1018 } else if (segment % 2) {
1019 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1020 " segment base references");
1021 } else {
1022 if (wrt == NO_SEG) {
1023 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1024 addr = 0;
1025 } else if (wrt == elf_gotpc_sect + 1 ||
1026 wrt == elf_got_sect + 1) {
1027 elf_add_gsym_reloc(s, segment, addr+size, size,
1028 R_X86_64_GOTPCREL64, true);
1029 addr = 0;
1030 } else if (wrt == elf_gotoff_sect + 1 ||
1031 wrt == elf_got_sect + 1) {
1032 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1033 "absolute");
1034 } else if (wrt == elf_gottpoff_sect + 1) {
1035 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1036 "dword");
1037 } else {
1038 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1039 " use of WRT");
1042 elf_sect_writeaddr(s, addr, 8);
1046 static void elf_write(void)
1048 int align;
1049 char *p;
1050 int i;
1052 struct SAA *symtab;
1053 int32_t symtablen, symtablocal;
1056 * Work out how many sections we will have. We have SHN_UNDEF,
1057 * then the flexible user sections, then the fixed sections
1058 * `.shstrtab', `.symtab' and `.strtab', then optionally
1059 * relocation sections for the user sections.
1061 nsections = sec_numspecial + 1;
1062 if (of_elf64.current_dfmt == &df_stabs)
1063 nsections += 3;
1064 else if (of_elf64.current_dfmt == &df_dwarf)
1065 nsections += 10;
1067 add_sectname("", ".shstrtab");
1068 add_sectname("", ".symtab");
1069 add_sectname("", ".strtab");
1070 for (i = 0; i < nsects; i++) {
1071 nsections++; /* for the section itself */
1072 if (sects[i]->head) {
1073 nsections++; /* for its relocations */
1074 add_sectname(".rela", sects[i]->name);
1078 if (of_elf64.current_dfmt == &df_stabs) {
1079 /* in case the debug information is wanted, just add these three sections... */
1080 add_sectname("", ".stab");
1081 add_sectname("", ".stabstr");
1082 add_sectname(".rel", ".stab");
1085 else if (of_elf64.current_dfmt == &df_dwarf) {
1086 /* the dwarf debug standard specifies the following ten sections,
1087 not all of which are currently implemented,
1088 although all of them are defined. */
1089 #define debug_aranges (int64_t) (nsections-10)
1090 #define debug_info (int64_t) (nsections-7)
1091 #define debug_abbrev (int64_t) (nsections-5)
1092 #define debug_line (int64_t) (nsections-4)
1093 add_sectname("", ".debug_aranges");
1094 add_sectname(".rela", ".debug_aranges");
1095 add_sectname("", ".debug_pubnames");
1096 add_sectname("", ".debug_info");
1097 add_sectname(".rela", ".debug_info");
1098 add_sectname("", ".debug_abbrev");
1099 add_sectname("", ".debug_line");
1100 add_sectname(".rela", ".debug_line");
1101 add_sectname("", ".debug_frame");
1102 add_sectname("", ".debug_loc");
1106 * Output the ELF header.
1108 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1109 fputc(elf_osabi, ofile);
1110 fputc(elf_abiver, ofile);
1111 fwritezero(7, ofile);
1112 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1113 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1114 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1115 fwriteint64_t(0L, ofile); /* no entry point */
1116 fwriteint64_t(0L, ofile); /* no program header table */
1117 fwriteint64_t(0x40L, ofile); /* section headers straight after
1118 * ELF header plus alignment */
1119 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1120 fwriteint16_t(0x40, ofile); /* size of ELF header */
1121 fwriteint16_t(0, ofile); /* no program header table, again */
1122 fwriteint16_t(0, ofile); /* still no program header table */
1123 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1124 fwriteint16_t(nsections, ofile); /* number of sections */
1125 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1126 * section header table */
1129 * Build the symbol table and relocation tables.
1131 symtab = elf_build_symtab(&symtablen, &symtablocal);
1132 for (i = 0; i < nsects; i++)
1133 if (sects[i]->head)
1134 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1135 sects[i]->head);
1138 * Now output the section header table.
1141 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1142 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1143 elf_foffs += align;
1144 elf_nsect = 0;
1145 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1147 /* SHN_UNDEF */
1148 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1149 p = shstrtab + 1;
1151 /* The normal sections */
1152 for (i = 0; i < nsects; i++) {
1153 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1154 (sects[i]->type == SHT_PROGBITS ?
1155 sects[i]->data : NULL), true,
1156 sects[i]->len, 0, 0, sects[i]->align, 0);
1157 p += strlen(p) + 1;
1160 /* .shstrtab */
1161 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1162 shstrtablen, 0, 0, 1, 0);
1163 p += strlen(p) + 1;
1165 /* .symtab */
1166 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1167 symtablen, sec_strtab, symtablocal, 4, 24);
1168 p += strlen(p) + 1;
1170 /* .strtab */
1171 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1172 strslen, 0, 0, 1, 0);
1173 p += strlen(p) + 1;
1175 /* The relocation sections */
1176 for (i = 0; i < nsects; i++)
1177 if (sects[i]->head) {
1178 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1179 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1180 p += strlen(p) + 1;
1183 if (of_elf64.current_dfmt == &df_stabs) {
1184 /* for debugging information, create the last three sections
1185 which are the .stab , .stabstr and .rel.stab sections respectively */
1187 /* this function call creates the stab sections in memory */
1188 stabs64_generate();
1190 if (stabbuf && stabstrbuf && stabrelbuf) {
1191 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1192 stablen, sec_stabstr, 0, 4, 12);
1193 p += strlen(p) + 1;
1195 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1196 stabstrlen, 0, 0, 4, 0);
1197 p += strlen(p) + 1;
1199 /* link -> symtable info -> section to refer to */
1200 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1201 stabrellen, symtabsection, sec_stab, 4, 16);
1202 p += strlen(p) + 1;
1205 else if (of_elf64.current_dfmt == &df_dwarf) {
1206 /* for dwarf debugging information, create the ten dwarf sections */
1208 /* this function call creates the dwarf sections in memory */
1209 if (dwarf_fsect)
1210 dwarf64_generate();
1212 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1213 arangeslen, 0, 0, 1, 0);
1214 p += strlen(p) + 1;
1216 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1217 arangesrellen, symtabsection, debug_aranges, 1, 24);
1218 p += strlen(p) + 1;
1220 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1221 pubnameslen, 0, 0, 1, 0);
1222 p += strlen(p) + 1;
1224 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1225 infolen, 0, 0, 1, 0);
1226 p += strlen(p) + 1;
1228 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1229 inforellen, symtabsection, debug_info, 1, 24);
1230 p += strlen(p) + 1;
1232 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1233 abbrevlen, 0, 0, 1, 0);
1234 p += strlen(p) + 1;
1236 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1237 linelen, 0, 0, 1, 0);
1238 p += strlen(p) + 1;
1240 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1241 linerellen, symtabsection, debug_line, 1, 24);
1242 p += strlen(p) + 1;
1244 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1245 framelen, 0, 0, 8, 0);
1246 p += strlen(p) + 1;
1248 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1249 loclen, 0, 0, 1, 0);
1250 p += strlen(p) + 1;
1252 fwritezero(align, ofile);
1255 * Now output the sections.
1257 elf_write_sections();
1259 nasm_free(elf_sects);
1260 saa_free(symtab);
1263 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1265 struct SAA *s = saa_init(1L);
1266 struct Symbol *sym;
1267 uint8_t entry[24], *p;
1268 int i;
1270 *len = *local = 0;
1273 * First, an all-zeros entry, required by the ELF spec.
1275 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1276 *len += 24;
1277 (*local)++;
1280 * Next, an entry for the file name.
1282 p = entry;
1283 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1284 WRITESHORT(p, STT_FILE); /* type FILE */
1285 WRITESHORT(p, SHN_ABS);
1286 WRITEDLONG(p, (uint64_t) 0); /* no value */
1287 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1288 saa_wbytes(s, entry, 24L);
1289 *len += 24;
1290 (*local)++;
1293 * Now some standard symbols defining the segments, for relocation
1294 * purposes.
1296 for (i = 1; i <= nsects; i++) {
1297 p = entry;
1298 WRITELONG(p, 0); /* no symbol name */
1299 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1300 WRITESHORT(p, i); /* section id */
1301 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1302 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1303 saa_wbytes(s, entry, 24L);
1304 *len += 24;
1305 (*local)++;
1310 * Now the other local symbols.
1312 saa_rewind(syms);
1313 while ((sym = saa_rstruct(syms))) {
1314 if (sym->type & SYM_GLOBAL)
1315 continue;
1316 p = entry;
1317 WRITELONG(p, sym->strpos); /* index into symbol string table */
1318 WRITECHAR(p, sym->type); /* type and binding */
1319 WRITECHAR(p, sym->other); /* visibility */
1320 WRITESHORT(p, sym->section); /* index into section header table */
1321 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1322 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1323 saa_wbytes(s, entry, 24L);
1324 *len += 24;
1325 (*local)++;
1328 * dwarf needs symbols for debug sections
1329 * which are relocation targets.
1331 if (of_elf64.current_dfmt == &df_dwarf) {
1332 dwarf_infosym = *local;
1333 p = entry;
1334 WRITELONG(p, 0); /* no symbol name */
1335 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1336 WRITESHORT(p, debug_info); /* section id */
1337 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1338 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1339 saa_wbytes(s, entry, 24L);
1340 *len += 24;
1341 (*local)++;
1342 dwarf_abbrevsym = *local;
1343 p = entry;
1344 WRITELONG(p, 0); /* no symbol name */
1345 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1346 WRITESHORT(p, debug_abbrev); /* section id */
1347 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1348 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1349 saa_wbytes(s, entry, 24L);
1350 *len += 24;
1351 (*local)++;
1352 dwarf_linesym = *local;
1353 p = entry;
1354 WRITELONG(p, 0); /* no symbol name */
1355 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1356 WRITESHORT(p, debug_line); /* section id */
1357 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1358 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1359 saa_wbytes(s, entry, 24L);
1360 *len += 24;
1361 (*local)++;
1365 * Now the global symbols.
1367 saa_rewind(syms);
1368 while ((sym = saa_rstruct(syms))) {
1369 if (!(sym->type & SYM_GLOBAL))
1370 continue;
1371 p = entry;
1372 WRITELONG(p, sym->strpos);
1373 WRITECHAR(p, sym->type); /* type and binding */
1374 WRITECHAR(p, sym->other); /* visibility */
1375 WRITESHORT(p, sym->section);
1376 WRITEDLONG(p, (int64_t)sym->symv.key);
1377 WRITEDLONG(p, (int64_t)sym->size);
1378 saa_wbytes(s, entry, 24L);
1379 *len += 24;
1382 return s;
1385 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1387 struct SAA *s;
1388 uint8_t *p, entry[24];
1389 int32_t global_offset;
1391 if (!r)
1392 return NULL;
1394 s = saa_init(1L);
1395 *len = 0;
1398 * How to onvert from a global placeholder to a real symbol index;
1399 * the +2 refers to the two special entries, the null entry and
1400 * the filename entry.
1402 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1404 while (r) {
1405 int32_t sym = r->symbol;
1407 if (sym >= GLOBAL_TEMP_BASE)
1408 sym += global_offset;
1410 p = entry;
1411 WRITEDLONG(p, r->address);
1412 WRITELONG(p, r->type);
1413 WRITELONG(p, sym);
1414 WRITEDLONG(p, r->offset);
1415 saa_wbytes(s, entry, 24L);
1416 *len += 24;
1418 r = r->next;
1421 return s;
1424 static void elf_section_header(int name, int type, uint64_t flags,
1425 void *data, bool is_saa, uint64_t datalen,
1426 int link, int info, int align, int eltsize)
1428 elf_sects[elf_nsect].data = data;
1429 elf_sects[elf_nsect].len = datalen;
1430 elf_sects[elf_nsect].is_saa = is_saa;
1431 elf_nsect++;
1433 fwriteint32_t((int32_t)name, ofile);
1434 fwriteint32_t((int32_t)type, ofile);
1435 fwriteint64_t((int64_t)flags, ofile);
1436 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1437 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1438 fwriteint64_t(datalen, ofile);
1439 if (data)
1440 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1441 fwriteint32_t((int32_t)link, ofile);
1442 fwriteint32_t((int32_t)info, ofile);
1443 fwriteint64_t((int64_t)align, ofile);
1444 fwriteint64_t((int64_t)eltsize, ofile);
1447 static void elf_write_sections(void)
1449 int i;
1450 for (i = 0; i < elf_nsect; i++)
1451 if (elf_sects[i].data) {
1452 int32_t len = elf_sects[i].len;
1453 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1454 int32_t align = reallen - len;
1455 if (elf_sects[i].is_saa)
1456 saa_fpwrite(elf_sects[i].data, ofile);
1457 else
1458 fwrite(elf_sects[i].data, len, 1, ofile);
1459 fwritezero(align, ofile);
1463 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1465 saa_wbytes(sect->data, data, len);
1466 sect->len += len;
1468 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1470 saa_writeaddr(sect->data, data, len);
1471 sect->len += len;
1474 static int32_t elf_segbase(int32_t segment)
1476 return segment;
1479 static int elf_directive(enum directives directive, char *value, int pass)
1481 bool err;
1482 int64_t n;
1483 char *p;
1485 switch (directive) {
1486 case D_OSABI:
1487 if (pass == 2)
1488 return 1; /* ignore in pass 2 */
1490 n = readnum(value, &err);
1491 if (err) {
1492 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1493 return 1;
1495 if (n < 0 || n > 255) {
1496 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1497 return 1;
1499 elf_osabi = n;
1500 elf_abiver = 0;
1502 if ((p = strchr(value,',')) == NULL)
1503 return 1;
1505 n = readnum(p+1, &err);
1506 if (err || n < 0 || n > 255) {
1507 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1508 return 1;
1511 elf_abiver = n;
1512 return 1;
1514 default:
1515 return 0;
1519 static void elf_filename(char *inname, char *outname)
1521 strcpy(elf_module, inname);
1522 standard_extension(inname, outname, ".o");
1525 extern macros_t elf_stdmac[];
1527 static int elf_set_info(enum geninfo type, char **val)
1529 (void)type;
1530 (void)val;
1531 return 0;
1533 static struct dfmt df_dwarf = {
1534 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1535 "dwarf",
1536 dwarf64_init,
1537 dwarf64_linenum,
1538 debug64_deflabel,
1539 debug64_directive,
1540 debug64_typevalue,
1541 dwarf64_output,
1542 dwarf64_cleanup
1544 static struct dfmt df_stabs = {
1545 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1546 "stabs",
1547 null_debug_init,
1548 stabs64_linenum,
1549 debug64_deflabel,
1550 debug64_directive,
1551 debug64_typevalue,
1552 stabs64_output,
1553 stabs64_cleanup
1556 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1558 struct ofmt of_elf64 = {
1559 "ELF64 (x86_64) object files (e.g. Linux)",
1560 "elf64",
1562 elf64_debugs_arr,
1563 &df_stabs,
1564 elf_stdmac,
1565 elf_init,
1566 elf_set_info,
1567 elf_out,
1568 elf_deflabel,
1569 elf_section_names,
1570 elf_segbase,
1571 elf_directive,
1572 elf_filename,
1573 elf_cleanup
1576 /* common debugging routines */
1577 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1578 int is_global, char *special)
1580 (void)name;
1581 (void)segment;
1582 (void)offset;
1583 (void)is_global;
1584 (void)special;
1587 static void debug64_directive(const char *directive, const char *params)
1589 (void)directive;
1590 (void)params;
1593 static void debug64_typevalue(int32_t type)
1595 int32_t stype, ssize;
1596 switch (TYM_TYPE(type)) {
1597 case TY_LABEL:
1598 ssize = 0;
1599 stype = STT_NOTYPE;
1600 break;
1601 case TY_BYTE:
1602 ssize = 1;
1603 stype = STT_OBJECT;
1604 break;
1605 case TY_WORD:
1606 ssize = 2;
1607 stype = STT_OBJECT;
1608 break;
1609 case TY_DWORD:
1610 ssize = 4;
1611 stype = STT_OBJECT;
1612 break;
1613 case TY_FLOAT:
1614 ssize = 4;
1615 stype = STT_OBJECT;
1616 break;
1617 case TY_QWORD:
1618 ssize = 8;
1619 stype = STT_OBJECT;
1620 break;
1621 case TY_TBYTE:
1622 ssize = 10;
1623 stype = STT_OBJECT;
1624 break;
1625 case TY_OWORD:
1626 ssize = 16;
1627 stype = STT_OBJECT;
1628 break;
1629 case TY_YWORD:
1630 ssize = 32;
1631 stype = STT_OBJECT;
1632 break;
1633 case TY_COMMON:
1634 ssize = 0;
1635 stype = STT_COMMON;
1636 break;
1637 case TY_SEG:
1638 ssize = 0;
1639 stype = STT_SECTION;
1640 break;
1641 case TY_EXTERN:
1642 ssize = 0;
1643 stype = STT_NOTYPE;
1644 break;
1645 case TY_EQU:
1646 ssize = 0;
1647 stype = STT_NOTYPE;
1648 break;
1649 default:
1650 ssize = 0;
1651 stype = STT_NOTYPE;
1652 break;
1654 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1655 lastsym->size = ssize;
1656 lastsym->type = stype;
1660 /* stabs debugging routines */
1662 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1664 (void)segto;
1665 if (!stabs_filename) {
1666 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1667 strcpy(stabs_filename, filename);
1668 } else {
1669 if (strcmp(stabs_filename, filename)) {
1670 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1671 in fact, this leak comes in quite handy to maintain a list of files
1672 encountered so far in the symbol lines... */
1674 /* why not nasm_free(stabs_filename); we're done with the old one */
1676 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1677 strcpy(stabs_filename, filename);
1680 debug_immcall = 1;
1681 currentline = linenumber;
1685 static void stabs64_output(int type, void *param)
1687 struct symlininfo *s;
1688 struct linelist *el;
1689 if (type == TY_DEBUGSYMLIN) {
1690 if (debug_immcall) {
1691 s = (struct symlininfo *)param;
1692 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1693 return; /* line info is only collected for executable sections */
1694 numlinestabs++;
1695 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1696 el->info.offset = s->offset;
1697 el->info.section = s->section;
1698 el->info.name = s->name;
1699 el->line = currentline;
1700 el->filename = stabs_filename;
1701 el->next = 0;
1702 if (stabslines) {
1703 stabslines->last->next = el;
1704 stabslines->last = el;
1705 } else {
1706 stabslines = el;
1707 stabslines->last = el;
1711 debug_immcall = 0;
1714 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1715 do {\
1716 WRITELONG(p,n_strx); \
1717 WRITECHAR(p,n_type); \
1718 WRITECHAR(p,n_other); \
1719 WRITESHORT(p,n_desc); \
1720 WRITELONG(p,n_value); \
1721 } while (0)
1723 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1725 static void stabs64_generate(void)
1727 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1728 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1729 char **allfiles;
1730 int *fileidx;
1732 struct linelist *ptr;
1734 ptr = stabslines;
1736 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1737 for (i = 0; i < numlinestabs; i++)
1738 allfiles[i] = 0;
1739 numfiles = 0;
1740 while (ptr) {
1741 if (numfiles == 0) {
1742 allfiles[0] = ptr->filename;
1743 numfiles++;
1744 } else {
1745 for (i = 0; i < numfiles; i++) {
1746 if (!strcmp(allfiles[i], ptr->filename))
1747 break;
1749 if (i >= numfiles) {
1750 allfiles[i] = ptr->filename;
1751 numfiles++;
1754 ptr = ptr->next;
1756 strsize = 1;
1757 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1758 for (i = 0; i < numfiles; i++) {
1759 fileidx[i] = strsize;
1760 strsize += strlen(allfiles[i]) + 1;
1762 mainfileindex = 0;
1763 for (i = 0; i < numfiles; i++) {
1764 if (!strcmp(allfiles[i], elf_module)) {
1765 mainfileindex = i;
1766 break;
1770 /* worst case size of the stab buffer would be:
1771 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1773 sbuf =
1774 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1775 sizeof(struct stabentry));
1777 ssbuf = (uint8_t *)nasm_malloc(strsize);
1779 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1780 rptr = rbuf;
1782 for (i = 0; i < numfiles; i++) {
1783 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1785 ssbuf[0] = 0;
1787 stabstrlen = strsize; /* set global variable for length of stab strings */
1789 sptr = sbuf;
1790 ptr = stabslines;
1791 numstabs = 0;
1793 if (ptr) {
1794 /* this is the first stab, its strx points to the filename of the
1795 the source-file, the n_desc field should be set to the number
1796 of remaining stabs
1798 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1800 /* this is the stab for the main source file */
1801 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1803 /* relocation table entry */
1805 /* Since the symbol table has two entries before */
1806 /* the section symbols, the index in the info.section */
1807 /* member must be adjusted by adding 2 */
1809 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1810 WRITELONG(rptr, R_X86_64_32);
1811 WRITELONG(rptr, ptr->info.section + 2);
1813 numstabs++;
1814 currfile = mainfileindex;
1817 while (ptr) {
1818 if (strcmp(allfiles[currfile], ptr->filename)) {
1819 /* oops file has changed... */
1820 for (i = 0; i < numfiles; i++)
1821 if (!strcmp(allfiles[i], ptr->filename))
1822 break;
1823 currfile = i;
1824 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1825 ptr->info.offset);
1826 numstabs++;
1828 /* relocation table entry */
1830 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1831 WRITELONG(rptr, R_X86_64_32);
1832 WRITELONG(rptr, ptr->info.section + 2);
1835 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1836 numstabs++;
1838 /* relocation table entry */
1840 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1841 WRITELONG(rptr, R_X86_64_32);
1842 WRITELONG(rptr, ptr->info.section + 2);
1844 ptr = ptr->next;
1848 ((struct stabentry *)sbuf)->n_desc = numstabs;
1850 nasm_free(allfiles);
1851 nasm_free(fileidx);
1853 stablen = (sptr - sbuf);
1854 stabrellen = (rptr - rbuf);
1855 stabrelbuf = rbuf;
1856 stabbuf = sbuf;
1857 stabstrbuf = ssbuf;
1860 static void stabs64_cleanup(void)
1862 struct linelist *ptr, *del;
1863 if (!stabslines)
1864 return;
1865 ptr = stabslines;
1866 while (ptr) {
1867 del = ptr;
1868 ptr = ptr->next;
1869 nasm_free(del);
1871 if (stabbuf)
1872 nasm_free(stabbuf);
1873 if (stabrelbuf)
1874 nasm_free(stabrelbuf);
1875 if (stabstrbuf)
1876 nasm_free(stabstrbuf);
1878 /* dwarf routines */
1879 static void dwarf64_init(void)
1881 ndebugs = 3; /* 3 debug symbols */
1884 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1885 int32_t segto)
1887 (void)segto;
1888 dwarf64_findfile(filename);
1889 debug_immcall = 1;
1890 currentline = linenumber;
1893 /* called from elf_out with type == TY_DEBUGSYMLIN */
1894 static void dwarf64_output(int type, void *param)
1896 int ln, aa, inx, maxln, soc;
1897 struct symlininfo *s;
1898 struct SAA *plinep;
1900 (void)type;
1902 s = (struct symlininfo *)param;
1903 /* line number info is only gathered for executable sections */
1904 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1905 return;
1906 /* Check if section index has changed */
1907 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1909 dwarf64_findsect(s->section);
1911 /* do nothing unless line or file has changed */
1912 if (debug_immcall)
1914 ln = currentline - dwarf_csect->line;
1915 aa = s->offset - dwarf_csect->offset;
1916 inx = dwarf_clist->line;
1917 plinep = dwarf_csect->psaa;
1918 /* check for file change */
1919 if (!(inx == dwarf_csect->file))
1921 saa_write8(plinep,DW_LNS_set_file);
1922 saa_write8(plinep,inx);
1923 dwarf_csect->file = inx;
1925 /* check for line change */
1926 if (ln)
1928 /* test if in range of special op code */
1929 maxln = line_base + line_range;
1930 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1931 if (ln >= line_base && ln < maxln && soc < 256)
1933 saa_write8(plinep,soc);
1935 else
1937 if (ln)
1939 saa_write8(plinep,DW_LNS_advance_line);
1940 saa_wleb128s(plinep,ln);
1942 if (aa)
1944 saa_write8(plinep,DW_LNS_advance_pc);
1945 saa_wleb128u(plinep,aa);
1948 dwarf_csect->line = currentline;
1949 dwarf_csect->offset = s->offset;
1951 /* show change handled */
1952 debug_immcall = 0;
1957 static void dwarf64_generate(void)
1959 uint8_t *pbuf;
1960 int indx;
1961 struct linelist *ftentry;
1962 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1963 struct SAA *parangesrel, *plinesrel, *pinforel;
1964 struct sectlist *psect;
1965 size_t saalen, linepoff, totlen, highaddr;
1967 /* write epilogues for each line program range */
1968 /* and build aranges section */
1969 paranges = saa_init(1L);
1970 parangesrel = saa_init(1L);
1971 saa_write16(paranges,3); /* dwarf version */
1972 saa_write64(parangesrel, paranges->datalen+4);
1973 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1974 saa_write64(parangesrel, 0);
1975 saa_write32(paranges,0); /* offset into info */
1976 saa_write8(paranges,8); /* pointer size */
1977 saa_write8(paranges,0); /* not segmented */
1978 saa_write32(paranges,0); /* padding */
1979 /* iterate though sectlist entries */
1980 psect = dwarf_fsect;
1981 totlen = 0;
1982 highaddr = 0;
1983 for (indx = 0; indx < dwarf_nsections; indx++)
1985 plinep = psect->psaa;
1986 /* Line Number Program Epilogue */
1987 saa_write8(plinep,2); /* std op 2 */
1988 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1989 saa_write8(plinep,DW_LNS_extended_op);
1990 saa_write8(plinep,1); /* operand length */
1991 saa_write8(plinep,DW_LNE_end_sequence);
1992 totlen += plinep->datalen;
1993 /* range table relocation entry */
1994 saa_write64(parangesrel, paranges->datalen + 4);
1995 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1996 saa_write64(parangesrel, (uint64_t) 0);
1997 /* range table entry */
1998 saa_write64(paranges,0x0000); /* range start */
1999 saa_write64(paranges,sects[psect->section]->len); /* range length */
2000 highaddr += sects[psect->section]->len;
2001 /* done with this entry */
2002 psect = psect->next;
2004 saa_write64(paranges,0); /* null address */
2005 saa_write64(paranges,0); /* null length */
2006 saalen = paranges->datalen;
2007 arangeslen = saalen + 4;
2008 arangesbuf = pbuf = nasm_malloc(arangeslen);
2009 WRITELONG(pbuf,saalen); /* initial length */
2010 saa_rnbytes(paranges, pbuf, saalen);
2011 saa_free(paranges);
2013 /* build rela.aranges section */
2014 arangesrellen = saalen = parangesrel->datalen;
2015 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2016 saa_rnbytes(parangesrel, pbuf, saalen);
2017 saa_free(parangesrel);
2019 /* build pubnames section */
2020 ppubnames = saa_init(1L);
2021 saa_write16(ppubnames,3); /* dwarf version */
2022 saa_write32(ppubnames,0); /* offset into info */
2023 saa_write32(ppubnames,0); /* space used in info */
2024 saa_write32(ppubnames,0); /* end of list */
2025 saalen = ppubnames->datalen;
2026 pubnameslen = saalen + 4;
2027 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2028 WRITELONG(pbuf,saalen); /* initial length */
2029 saa_rnbytes(ppubnames, pbuf, saalen);
2030 saa_free(ppubnames);
2032 /* build info section */
2033 pinfo = saa_init(1L);
2034 pinforel = saa_init(1L);
2035 saa_write16(pinfo,3); /* dwarf version */
2036 saa_write64(pinforel, pinfo->datalen + 4);
2037 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2038 saa_write64(pinforel, 0);
2039 saa_write32(pinfo,0); /* offset into abbrev */
2040 saa_write8(pinfo,8); /* pointer size */
2041 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2042 saa_write64(pinforel, pinfo->datalen + 4);
2043 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2044 saa_write64(pinforel, 0);
2045 saa_write64(pinfo,0); /* DW_AT_low_pc */
2046 saa_write64(pinforel, pinfo->datalen + 4);
2047 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2048 saa_write64(pinforel, 0);
2049 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2050 saa_write64(pinforel, pinfo->datalen + 4);
2051 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2052 saa_write64(pinforel, 0);
2053 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2054 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2055 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2056 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2057 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2058 saa_write64(pinforel, pinfo->datalen + 4);
2059 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2060 saa_write64(pinforel, 0);
2061 saa_write64(pinfo,0); /* DW_AT_low_pc */
2062 saa_write64(pinfo,0); /* DW_AT_frame_base */
2063 saa_write8(pinfo,0); /* end of entries */
2064 saalen = pinfo->datalen;
2065 infolen = saalen + 4;
2066 infobuf = pbuf = nasm_malloc(infolen);
2067 WRITELONG(pbuf,saalen); /* initial length */
2068 saa_rnbytes(pinfo, pbuf, saalen);
2069 saa_free(pinfo);
2071 /* build rela.info section */
2072 inforellen = saalen = pinforel->datalen;
2073 inforelbuf = pbuf = nasm_malloc(inforellen);
2074 saa_rnbytes(pinforel, pbuf, saalen);
2075 saa_free(pinforel);
2077 /* build abbrev section */
2078 pabbrev = saa_init(1L);
2079 saa_write8(pabbrev,1); /* entry number LEB128u */
2080 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2081 saa_write8(pabbrev,1); /* has children */
2082 /* the following attributes and forms are all LEB128u values */
2083 saa_write8(pabbrev,DW_AT_low_pc);
2084 saa_write8(pabbrev,DW_FORM_addr);
2085 saa_write8(pabbrev,DW_AT_high_pc);
2086 saa_write8(pabbrev,DW_FORM_addr);
2087 saa_write8(pabbrev,DW_AT_stmt_list);
2088 saa_write8(pabbrev,DW_FORM_data4);
2089 saa_write8(pabbrev,DW_AT_name);
2090 saa_write8(pabbrev,DW_FORM_string);
2091 saa_write8(pabbrev,DW_AT_producer);
2092 saa_write8(pabbrev,DW_FORM_string);
2093 saa_write8(pabbrev,DW_AT_language);
2094 saa_write8(pabbrev,DW_FORM_data2);
2095 saa_write16(pabbrev,0); /* end of entry */
2096 /* LEB128u usage same as above */
2097 saa_write8(pabbrev,2); /* entry number */
2098 saa_write8(pabbrev,DW_TAG_subprogram);
2099 saa_write8(pabbrev,0); /* no children */
2100 saa_write8(pabbrev,DW_AT_low_pc);
2101 saa_write8(pabbrev,DW_FORM_addr);
2102 saa_write8(pabbrev,DW_AT_frame_base);
2103 saa_write8(pabbrev,DW_FORM_data4);
2104 saa_write16(pabbrev,0); /* end of entry */
2105 abbrevlen = saalen = pabbrev->datalen;
2106 abbrevbuf = pbuf = nasm_malloc(saalen);
2107 saa_rnbytes(pabbrev, pbuf, saalen);
2108 saa_free(pabbrev);
2110 /* build line section */
2111 /* prolog */
2112 plines = saa_init(1L);
2113 saa_write8(plines,1); /* Minimum Instruction Length */
2114 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2115 saa_write8(plines,line_base); /* Line Base */
2116 saa_write8(plines,line_range); /* Line Range */
2117 saa_write8(plines,opcode_base); /* Opcode Base */
2118 /* standard opcode lengths (# of LEB128u operands) */
2119 saa_write8(plines,0); /* Std opcode 1 length */
2120 saa_write8(plines,1); /* Std opcode 2 length */
2121 saa_write8(plines,1); /* Std opcode 3 length */
2122 saa_write8(plines,1); /* Std opcode 4 length */
2123 saa_write8(plines,1); /* Std opcode 5 length */
2124 saa_write8(plines,0); /* Std opcode 6 length */
2125 saa_write8(plines,0); /* Std opcode 7 length */
2126 saa_write8(plines,0); /* Std opcode 8 length */
2127 saa_write8(plines,1); /* Std opcode 9 length */
2128 saa_write8(plines,0); /* Std opcode 10 length */
2129 saa_write8(plines,0); /* Std opcode 11 length */
2130 saa_write8(plines,1); /* Std opcode 12 length */
2131 /* Directory Table */
2132 saa_write8(plines,0); /* End of table */
2133 /* File Name Table */
2134 ftentry = dwarf_flist;
2135 for (indx = 0;indx<dwarf_numfiles;indx++)
2137 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2138 saa_write8(plines,0); /* directory LEB128u */
2139 saa_write8(plines,0); /* time LEB128u */
2140 saa_write8(plines,0); /* size LEB128u */
2141 ftentry = ftentry->next;
2143 saa_write8(plines,0); /* End of table */
2144 linepoff = plines->datalen;
2145 linelen = linepoff + totlen + 10;
2146 linebuf = pbuf = nasm_malloc(linelen);
2147 WRITELONG(pbuf,linelen-4); /* initial length */
2148 WRITESHORT(pbuf,3); /* dwarf version */
2149 WRITELONG(pbuf,linepoff); /* offset to line number program */
2150 /* write line header */
2151 saalen = linepoff;
2152 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2153 pbuf += linepoff;
2154 saa_free(plines);
2155 /* concatonate line program ranges */
2156 linepoff += 13;
2157 plinesrel = saa_init(1L);
2158 psect = dwarf_fsect;
2159 for (indx = 0; indx < dwarf_nsections; indx++)
2161 saa_write64(plinesrel, linepoff);
2162 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2163 saa_write64(plinesrel, (uint64_t) 0);
2164 plinep = psect->psaa;
2165 saalen = plinep->datalen;
2166 saa_rnbytes(plinep, pbuf, saalen);
2167 pbuf += saalen;
2168 linepoff += saalen;
2169 saa_free(plinep);
2170 /* done with this entry */
2171 psect = psect->next;
2175 /* build rela.lines section */
2176 linerellen =saalen = plinesrel->datalen;
2177 linerelbuf = pbuf = nasm_malloc(linerellen);
2178 saa_rnbytes(plinesrel, pbuf, saalen);
2179 saa_free(plinesrel);
2181 /* build frame section */
2182 framelen = 4;
2183 framebuf = pbuf = nasm_malloc(framelen);
2184 WRITELONG(pbuf,framelen-4); /* initial length */
2186 /* build loc section */
2187 loclen = 16;
2188 locbuf = pbuf = nasm_malloc(loclen);
2189 WRITEDLONG(pbuf,0); /* null beginning offset */
2190 WRITEDLONG(pbuf,0); /* null ending offset */
2193 static void dwarf64_cleanup(void)
2195 if (arangesbuf)
2196 nasm_free(arangesbuf);
2197 if (arangesrelbuf)
2198 nasm_free(arangesrelbuf);
2199 if (pubnamesbuf)
2200 nasm_free(pubnamesbuf);
2201 if (infobuf)
2202 nasm_free(infobuf);
2203 if (inforelbuf)
2204 nasm_free(inforelbuf);
2205 if (abbrevbuf)
2206 nasm_free(abbrevbuf);
2207 if (linebuf)
2208 nasm_free(linebuf);
2209 if (linerelbuf)
2210 nasm_free(linerelbuf);
2211 if (framebuf)
2212 nasm_free(framebuf);
2213 if (locbuf)
2214 nasm_free(locbuf);
2216 static void dwarf64_findfile(const char * fname)
2218 int finx;
2219 struct linelist *match;
2221 /* return if fname is current file name */
2222 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2223 /* search for match */
2224 else
2226 match = 0;
2227 if (dwarf_flist)
2229 match = dwarf_flist;
2230 for (finx = 0; finx < dwarf_numfiles; finx++)
2232 if (!(strcmp(fname, match->filename)))
2234 dwarf_clist = match;
2235 return;
2239 /* add file name to end of list */
2240 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2241 dwarf_numfiles++;
2242 dwarf_clist->line = dwarf_numfiles;
2243 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2244 strcpy(dwarf_clist->filename,fname);
2245 dwarf_clist->next = 0;
2246 /* if first entry */
2247 if (!dwarf_flist)
2249 dwarf_flist = dwarf_elist = dwarf_clist;
2250 dwarf_clist->last = 0;
2252 /* chain to previous entry */
2253 else
2255 dwarf_elist->next = dwarf_clist;
2256 dwarf_elist = dwarf_clist;
2260 /* */
2261 static void dwarf64_findsect(const int index)
2263 int sinx;
2264 struct sectlist *match;
2265 struct SAA *plinep;
2266 /* return if index is current section index */
2267 if (dwarf_csect && (dwarf_csect->section == index))
2269 return;
2271 /* search for match */
2272 else
2274 match = 0;
2275 if (dwarf_fsect)
2277 match = dwarf_fsect;
2278 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2280 if ((match->section == index))
2282 dwarf_csect = match;
2283 return;
2285 match = match->next;
2288 /* add entry to end of list */
2289 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2290 dwarf_nsections++;
2291 dwarf_csect->psaa = plinep = saa_init(1L);
2292 dwarf_csect->line = 1;
2293 dwarf_csect->offset = 0;
2294 dwarf_csect->file = 1;
2295 dwarf_csect->section = index;
2296 dwarf_csect->next = 0;
2297 /* set relocatable address at start of line program */
2298 saa_write8(plinep,DW_LNS_extended_op);
2299 saa_write8(plinep,9); /* operand length */
2300 saa_write8(plinep,DW_LNE_set_address);
2301 saa_write64(plinep,0); /* Start Address */
2302 /* if first entry */
2303 if (!dwarf_fsect)
2305 dwarf_fsect = dwarf_esect = dwarf_csect;
2306 dwarf_csect->last = 0;
2308 /* chain to previous entry */
2309 else
2311 dwarf_esect->next = dwarf_csect;
2312 dwarf_esect = dwarf_csect;
2317 #endif /* OF_ELF */