Hash even backend-specific directives, unify null functions
[nasm/sigaren-mirror.git] / output / outelf64.c
blobcf6e378d42557853a3f9b7934b94771d6ebe00b4
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 "output/outform.h"
53 #include "output/outlib.h"
54 #include "rbtree.h"
56 #include "output/elf64.h"
57 #include "output/dwarf.h"
58 #include "output/outelf.h"
60 #ifdef OF_ELF64
62 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
64 struct Reloc {
65 struct Reloc *next;
66 int64_t address; /* relative to _start_ of section */
67 int64_t symbol; /* symbol index */
68 int64_t offset; /* symbol addend */
69 int type; /* type of relocation */
72 struct Symbol {
73 struct rbtree symv; /* symbol value and rbtree of globals */
74 int32_t strpos; /* string table position of name */
75 int32_t section; /* section ID of the symbol */
76 int type; /* symbol type */
77 int other; /* symbol visibility */
78 int32_t size; /* size of symbol */
79 int32_t globnum; /* symbol table offset if global */
80 struct Symbol *nextfwd; /* list of unresolved-size symbols */
81 char *name; /* used temporarily if in above list */
84 struct Section {
85 struct SAA *data;
86 uint64_t len, size;
87 uint32_t nrelocs;
88 int32_t index; /* index into sects array */
89 int type; /* SHT_PROGBITS or SHT_NOBITS */
90 uint64_t align; /* alignment: power of two */
91 uint64_t flags; /* section flags */
92 char *name;
93 struct SAA *rel;
94 uint64_t rellen;
95 struct Reloc *head, **tail;
96 struct rbtree *gsyms; /* global symbols in section */
99 #define SECT_DELTA 32
100 static struct Section **sects;
101 static int nsects, sectlen;
103 #define SHSTR_DELTA 256
104 static char *shstrtab;
105 static int shstrtablen, shstrtabsize;
107 static struct SAA *syms;
108 static uint32_t nlocals, nglobs, ndebugs;
110 static int32_t def_seg;
112 static struct RAA *bsym;
114 static struct SAA *strs;
115 static uint32_t strslen;
117 static FILE *elffp;
118 static efunc error;
119 static evalfunc evaluate;
121 static struct Symbol *fwds;
123 static char elf_module[FILENAME_MAX];
125 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
126 static uint8_t elf_abiver = 0; /* Current ABI version */
128 extern struct ofmt of_elf64;
130 static struct ELF_SECTDATA {
131 void *data;
132 int64_t len;
133 bool is_saa;
134 } *elf_sects;
135 static int elf_nsect, nsections;
136 static int64_t elf_foffs;
138 static void elf_write(void);
139 static void elf_sect_write(struct Section *, const void *, size_t);
140 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
141 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
142 int, int);
143 static void elf_write_sections(void);
144 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
145 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
146 static void add_sectname(char *, char *);
148 /* type values for stabs debugging sections */
149 #define N_SO 0x64 /* ID for main source file */
150 #define N_SOL 0x84 /* ID for sub-source file */
151 #define N_BINCL 0x82 /* not currently used */
152 #define N_EINCL 0xA2 /* not currently used */
153 #define N_SLINE 0x44
155 struct stabentry {
156 uint32_t n_strx;
157 uint8_t n_type;
158 uint8_t n_other;
159 uint16_t n_desc;
160 uint32_t n_value;
163 struct erel {
164 int offset, info;
167 struct symlininfo {
168 int offset;
169 int section; /* index into sects[] */
170 int segto; /* internal section number */
171 char *name; /* shallow-copied pointer of section name */
174 struct linelist {
175 struct symlininfo info;
176 int line;
177 char *filename;
178 struct linelist *next;
179 struct linelist *last;
182 struct sectlist {
183 struct SAA *psaa;
184 int section;
185 int line;
186 int offset;
187 int file;
188 struct sectlist *next;
189 struct sectlist *last;
192 /* common debug variables */
193 static int currentline = 1;
194 static int debug_immcall = 0;
196 /* stabs debug variables */
197 static struct linelist *stabslines = 0;
198 static int numlinestabs = 0;
199 static char *stabs_filename = 0;
200 static int symtabsection;
201 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
202 static int stablen, stabstrlen, stabrellen;
204 /* dwarf debug variables */
205 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
206 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
207 static int dwarf_numfiles = 0, dwarf_nsections;
208 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
209 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
210 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
211 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
212 abbrevlen, linelen, linerellen, framelen, loclen;
213 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
216 static struct dfmt df_dwarf;
217 static struct dfmt df_stabs;
218 static struct Symbol *lastsym;
220 /* common debugging routines */
221 static void debug64_typevalue(int32_t);
222 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
223 static void debug64_directive(const char *, const char *);
225 /* stabs debugging routines */
226 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
227 static void stabs64_output(int, void *);
228 static void stabs64_generate(void);
229 static void stabs64_cleanup(void);
231 /* dwarf debugging routines */
232 static void dwarf64_init(struct ofmt *, void *, FILE *, efunc);
233 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
234 static void dwarf64_output(int, void *);
235 static void dwarf64_generate(void);
236 static void dwarf64_cleanup(void);
237 static void dwarf64_findfile(const char *);
238 static void dwarf64_findsect(const int);
241 * Special section numbers which are used to define ELF special
242 * symbols, which can be used with WRT to provide PIC relocation
243 * types.
245 static int32_t elf_gotpc_sect, elf_gotoff_sect;
246 static int32_t elf_got_sect, elf_plt_sect;
247 static int32_t elf_sym_sect;
248 static int32_t elf_gottpoff_sect;
250 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
252 maxbits = 64;
253 elffp = fp;
254 error = errfunc;
255 evaluate = eval;
256 (void)ldef; /* placate optimisers */
257 sects = NULL;
258 nsects = sectlen = 0;
259 syms = saa_init((int32_t)sizeof(struct Symbol));
260 nlocals = nglobs = ndebugs = 0;
261 bsym = raa_init();
262 strs = saa_init(1L);
263 saa_wbytes(strs, "\0", 1L);
264 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
265 strslen = 2 + strlen(elf_module);
266 shstrtab = NULL;
267 shstrtablen = shstrtabsize = 0;;
268 add_sectname("", "");
270 fwds = NULL;
272 elf_gotpc_sect = seg_alloc();
273 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
274 error);
275 elf_gotoff_sect = seg_alloc();
276 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
277 error);
278 elf_got_sect = seg_alloc();
279 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
280 error);
281 elf_plt_sect = seg_alloc();
282 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
283 error);
284 elf_sym_sect = seg_alloc();
285 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
286 error);
287 elf_gottpoff_sect = seg_alloc();
288 ldef("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false, &of_elf64,
289 error);
291 def_seg = seg_alloc();
295 static void elf_cleanup(int debuginfo)
297 struct Reloc *r;
298 int i;
300 (void)debuginfo;
302 elf_write();
303 for (i = 0; i < nsects; i++) {
304 if (sects[i]->type != SHT_NOBITS)
305 saa_free(sects[i]->data);
306 if (sects[i]->head)
307 saa_free(sects[i]->rel);
308 while (sects[i]->head) {
309 r = sects[i]->head;
310 sects[i]->head = sects[i]->head->next;
311 nasm_free(r);
314 nasm_free(sects);
315 saa_free(syms);
316 raa_free(bsym);
317 saa_free(strs);
318 if (of_elf64.current_dfmt) {
319 of_elf64.current_dfmt->cleanup();
322 /* add entry to the elf .shstrtab section */
323 static void add_sectname(char *firsthalf, char *secondhalf)
325 int len = strlen(firsthalf) + strlen(secondhalf);
326 while (shstrtablen + len + 1 > shstrtabsize)
327 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
328 strcpy(shstrtab + shstrtablen, firsthalf);
329 strcat(shstrtab + shstrtablen, secondhalf);
330 shstrtablen += len + 1;
333 static int elf_make_section(char *name, int type, int flags, int align)
335 struct Section *s;
337 s = nasm_malloc(sizeof(*s));
339 if (type != SHT_NOBITS)
340 s->data = saa_init(1L);
341 s->head = NULL;
342 s->tail = &s->head;
343 s->len = s->size = 0;
344 s->nrelocs = 0;
345 if (!strcmp(name, ".text"))
346 s->index = def_seg;
347 else
348 s->index = seg_alloc();
349 add_sectname("", name);
350 s->name = nasm_malloc(1 + strlen(name));
351 strcpy(s->name, name);
352 s->type = type;
353 s->flags = flags;
354 s->align = align;
355 s->gsyms = NULL;
357 if (nsects >= sectlen)
358 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
359 sects[nsects++] = s;
361 return nsects - 1;
364 static int32_t elf_section_names(char *name, int pass, int *bits)
366 char *p;
367 uint32_t flags, flags_and, flags_or;
368 uint64_t align;
369 int type, i;
372 * Default is 64 bits.
374 if (!name) {
375 *bits = 64;
376 return def_seg;
379 p = name;
380 while (*p && !nasm_isspace(*p))
381 p++;
382 if (*p)
383 *p++ = '\0';
384 flags_and = flags_or = type = align = 0;
386 while (*p && nasm_isspace(*p))
387 p++;
388 while (*p) {
389 char *q = p;
390 while (*p && !nasm_isspace(*p))
391 p++;
392 if (*p)
393 *p++ = '\0';
394 while (*p && nasm_isspace(*p))
395 p++;
397 if (!nasm_strnicmp(q, "align=", 6)) {
398 align = atoi(q + 6);
399 if (align == 0)
400 align = 1;
401 if ((align - 1) & align) { /* means it's not a power of two */
402 error(ERR_NONFATAL, "section alignment %d is not"
403 " a power of two", align);
404 align = 1;
406 } else if (!nasm_stricmp(q, "alloc")) {
407 flags_and |= SHF_ALLOC;
408 flags_or |= SHF_ALLOC;
409 } else if (!nasm_stricmp(q, "noalloc")) {
410 flags_and |= SHF_ALLOC;
411 flags_or &= ~SHF_ALLOC;
412 } else if (!nasm_stricmp(q, "exec")) {
413 flags_and |= SHF_EXECINSTR;
414 flags_or |= SHF_EXECINSTR;
415 } else if (!nasm_stricmp(q, "noexec")) {
416 flags_and |= SHF_EXECINSTR;
417 flags_or &= ~SHF_EXECINSTR;
418 } else if (!nasm_stricmp(q, "write")) {
419 flags_and |= SHF_WRITE;
420 flags_or |= SHF_WRITE;
421 } else if (!nasm_stricmp(q, "tls")) {
422 flags_and |= SHF_TLS;
423 flags_or |= SHF_TLS;
424 } else if (!nasm_stricmp(q, "nowrite")) {
425 flags_and |= SHF_WRITE;
426 flags_or &= ~SHF_WRITE;
427 } else if (!nasm_stricmp(q, "progbits")) {
428 type = SHT_PROGBITS;
429 } else if (!nasm_stricmp(q, "nobits")) {
430 type = SHT_NOBITS;
431 } else if (pass == 1) {
432 error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
433 " declaration of section `%s'", q, name);
437 if (!strcmp(name, ".shstrtab") ||
438 !strcmp(name, ".symtab") ||
439 !strcmp(name, ".strtab")) {
440 error(ERR_NONFATAL, "attempt to redefine reserved section"
441 "name `%s'", name);
442 return NO_SEG;
445 for (i = 0; i < nsects; i++)
446 if (!strcmp(name, sects[i]->name))
447 break;
448 if (i == nsects) {
449 const struct elf_known_section *ks = elf_known_sections;
451 while (ks->name) {
452 if (!strcmp(name, ks->name))
453 break;
454 ks++;
457 type = type ? type : ks->type;
458 align = align ? align : ks->align;
459 flags = (ks->flags & ~flags_and) | flags_or;
461 i = elf_make_section(name, type, flags, align);
462 } else if (pass == 1) {
463 if ((type && sects[i]->type != type)
464 || (align && sects[i]->align != align)
465 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
466 error(ERR_WARNING, "incompatible section attributes ignored on"
467 " redeclaration of section `%s'", name);
470 return sects[i]->index;
473 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
474 int is_global, char *special)
476 int pos = strslen;
477 struct Symbol *sym;
478 bool special_used = false;
480 #if defined(DEBUG) && DEBUG>2
481 fprintf(stderr,
482 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
483 name, segment, offset, is_global, special);
484 #endif
485 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
487 * This is a NASM special symbol. We never allow it into
488 * the ELF symbol table, even if it's a valid one. If it
489 * _isn't_ a valid one, we should barf immediately.
491 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
492 strcmp(name, "..got") && strcmp(name, "..plt") &&
493 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
494 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
495 return;
498 if (is_global == 3) {
499 struct Symbol **s;
501 * Fix up a forward-reference symbol size from the first
502 * pass.
504 for (s = &fwds; *s; s = &(*s)->nextfwd)
505 if (!strcmp((*s)->name, name)) {
506 struct tokenval tokval;
507 expr *e;
508 char *p = special;
510 while (*p && !nasm_isspace(*p))
511 p++;
512 while (*p && nasm_isspace(*p))
513 p++;
514 stdscan_reset();
515 stdscan_bufptr = p;
516 tokval.t_type = TOKEN_INVALID;
517 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
518 if (e) {
519 if (!is_simple(e))
520 error(ERR_NONFATAL, "cannot use relocatable"
521 " expression as symbol size");
522 else
523 (*s)->size = reloc_value(e);
527 * Remove it from the list of unresolved sizes.
529 nasm_free((*s)->name);
530 *s = (*s)->nextfwd;
531 return;
533 return; /* it wasn't an important one */
536 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
537 strslen += 1 + strlen(name);
539 lastsym = sym = saa_wstruct(syms);
541 memset(&sym->symv, 0, sizeof(struct rbtree));
543 sym->strpos = pos;
544 sym->type = is_global ? SYM_GLOBAL : 0;
545 sym->other = STV_DEFAULT;
546 sym->size = 0;
547 if (segment == NO_SEG)
548 sym->section = SHN_ABS;
549 else {
550 int i;
551 sym->section = SHN_UNDEF;
552 if (nsects == 0 && segment == def_seg) {
553 int tempint;
554 if (segment != elf_section_names(".text", 2, &tempint))
555 error(ERR_PANIC,
556 "strange segment conditions in ELF driver");
557 sym->section = nsects;
558 } else {
559 for (i = 0; i < nsects; i++)
560 if (segment == sects[i]->index) {
561 sym->section = i + 1;
562 break;
567 if (is_global == 2) {
568 sym->size = offset;
569 sym->symv.key = 0;
570 sym->section = SHN_COMMON;
572 * We have a common variable. Check the special text to see
573 * if it's a valid number and power of two; if so, store it
574 * as the alignment for the common variable.
576 if (special) {
577 bool err;
578 sym->symv.key = readnum(special, &err);
579 if (err)
580 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
581 " valid number", special);
582 else if ((sym->symv.key | (sym->symv.key - 1))
583 != 2 * sym->symv.key - 1)
584 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
585 " power of two", special);
587 special_used = true;
588 } else
589 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
591 if (sym->type == SYM_GLOBAL) {
593 * If sym->section == SHN_ABS, then the first line of the
594 * else section would cause a core dump, because its a reference
595 * beyond the end of the section array.
596 * This behaviour is exhibited by this code:
597 * GLOBAL crash_nasm
598 * crash_nasm equ 0
599 * To avoid such a crash, such requests are silently discarded.
600 * This may not be the best solution.
602 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
603 bsym = raa_write(bsym, segment, nglobs);
604 } else if (sym->section != SHN_ABS) {
606 * This is a global symbol; so we must add it to the rbtree
607 * of global symbols in its section.
609 * In addition, we check the special text for symbol
610 * type and size information.
612 sects[sym->section-1]->gsyms =
613 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
615 if (special) {
616 int n = strcspn(special, " \t");
618 if (!nasm_strnicmp(special, "function", n))
619 sym->type |= STT_FUNC;
620 else if (!nasm_strnicmp(special, "data", n) ||
621 !nasm_strnicmp(special, "object", n))
622 sym->type |= STT_OBJECT;
623 else if (!nasm_strnicmp(special, "notype", n))
624 sym->type |= STT_NOTYPE;
625 else
626 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
627 n, special);
628 special += n;
630 while (nasm_isspace(*special))
631 ++special;
632 if (*special) {
633 n = strcspn(special, " \t");
634 if (!nasm_strnicmp(special, "default", n))
635 sym->other = STV_DEFAULT;
636 else if (!nasm_strnicmp(special, "internal", n))
637 sym->other = STV_INTERNAL;
638 else if (!nasm_strnicmp(special, "hidden", n))
639 sym->other = STV_HIDDEN;
640 else if (!nasm_strnicmp(special, "protected", n))
641 sym->other = STV_PROTECTED;
642 else
643 n = 0;
644 special += n;
647 if (*special) {
648 struct tokenval tokval;
649 expr *e;
650 int fwd = 0;
651 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
653 while (special[n] && nasm_isspace(special[n]))
654 n++;
656 * We have a size expression; attempt to
657 * evaluate it.
659 stdscan_reset();
660 stdscan_bufptr = special + n;
661 tokval.t_type = TOKEN_INVALID;
662 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
663 NULL);
664 if (fwd) {
665 sym->nextfwd = fwds;
666 fwds = sym;
667 sym->name = nasm_strdup(name);
668 } else if (e) {
669 if (!is_simple(e))
670 error(ERR_NONFATAL, "cannot use relocatable"
671 " expression as symbol size");
672 else
673 sym->size = reloc_value(e);
675 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
677 special_used = true;
680 * If TLS segment, mark symbol accordingly.
682 if (sects[sym->section - 1]->flags & SHF_TLS) {
683 sym->type &= 0xf0;
684 sym->type |= STT_TLS;
687 sym->globnum = nglobs;
688 nglobs++;
689 } else
690 nlocals++;
692 if (special && !special_used)
693 error(ERR_NONFATAL, "no special symbol features supported here");
696 static void elf_add_reloc(struct Section *sect, int32_t segment,
697 int64_t offset, int type)
699 struct Reloc *r;
700 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
701 sect->tail = &r->next;
702 r->next = NULL;
704 r->address = sect->len;
705 r->offset = offset;
706 if (segment == NO_SEG)
707 r->symbol = 0;
708 else {
709 int i;
710 r->symbol = 0;
711 for (i = 0; i < nsects; i++)
712 if (segment == sects[i]->index)
713 r->symbol = i + 2;
714 if (!r->symbol)
715 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
717 r->type = type;
719 sect->nrelocs++;
723 * This routine deals with ..got and ..sym relocations: the more
724 * complicated kinds. In shared-library writing, some relocations
725 * with respect to global symbols must refer to the precise symbol
726 * rather than referring to an offset from the base of the section
727 * _containing_ the symbol. Such relocations call to this routine,
728 * which searches the symbol list for the symbol in question.
730 * R_386_GOT32 references require the _exact_ symbol address to be
731 * used; R_386_32 references can be at an offset from the symbol.
732 * The boolean argument `exact' tells us this.
734 * Return value is the adjusted value of `addr', having become an
735 * offset from the symbol rather than the section. Should always be
736 * zero when returning from an exact call.
738 * Limitation: if you define two symbols at the same place,
739 * confusion will occur.
741 * Inefficiency: we search, currently, using a linked list which
742 * isn't even necessarily sorted.
744 static void elf_add_gsym_reloc(struct Section *sect,
745 int32_t segment, uint64_t offset, int64_t pcrel,
746 int type, bool exact)
748 struct Reloc *r;
749 struct Section *s;
750 struct Symbol *sym;
751 struct rbtree *srb;
752 int i;
755 * First look up the segment/offset pair and find a global
756 * symbol corresponding to it. If it's not one of our segments,
757 * then it must be an external symbol, in which case we're fine
758 * doing a normal elf_add_reloc after first sanity-checking
759 * that the offset from the symbol is zero.
761 s = NULL;
762 for (i = 0; i < nsects; i++)
763 if (segment == sects[i]->index) {
764 s = sects[i];
765 break;
768 if (!s) {
769 if (exact && offset)
770 error(ERR_NONFATAL, "invalid access to an external symbol");
771 else
772 elf_add_reloc(sect, segment, offset - pcrel, type);
773 return;
776 srb = rb_search(s->gsyms, offset);
777 if (!srb || (exact && srb->key != offset)) {
778 error(ERR_NONFATAL, "unable to find a suitable global symbol"
779 " for this reference");
780 return;
782 sym = container_of(srb, struct Symbol, symv);
784 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
785 sect->tail = &r->next;
786 r->next = NULL;
788 r->address = sect->len;
789 r->offset = offset - pcrel - sym->symv.key;
790 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
791 r->type = type;
793 sect->nrelocs++;
796 static void elf_out(int32_t segto, const void *data,
797 enum out_type type, uint64_t size,
798 int32_t segment, int32_t wrt)
800 struct Section *s;
801 int64_t addr, zero;
802 int i;
803 static struct symlininfo sinfo;
805 zero = 0;
807 #if defined(DEBUG) && DEBUG>2
808 if (data) fprintf(stderr,
809 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
810 currentline, type, segment, segto, size, *(int64_t *)data);
811 else fprintf(stderr,
812 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
813 currentline, type, segment, segto, size);
814 #endif
817 * handle absolute-assembly (structure definitions)
819 if (segto == NO_SEG) {
820 if (type != OUT_RESERVE)
821 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
822 " space");
823 return;
826 s = NULL;
827 for (i = 0; i < nsects; i++)
828 if (segto == sects[i]->index) {
829 s = sects[i];
830 break;
832 if (!s) {
833 int tempint; /* ignored */
834 if (segto != elf_section_names(".text", 2, &tempint))
835 error(ERR_PANIC, "strange segment conditions in ELF driver");
836 else {
837 s = sects[nsects - 1];
838 i = nsects - 1;
841 /* invoke current debug_output routine */
842 if (of_elf64.current_dfmt) {
843 sinfo.offset = s->len;
844 sinfo.section = i;
845 sinfo.segto = segto;
846 sinfo.name = s->name;
847 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
849 /* end of debugging stuff */
851 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
852 error(ERR_WARNING, "attempt to initialize memory in"
853 " BSS section `%s': ignored", s->name);
854 s->len += realsize(type, size);
855 return;
858 if (type == OUT_RESERVE) {
859 if (s->type == SHT_PROGBITS) {
860 error(ERR_WARNING, "uninitialized space declared in"
861 " non-BSS section `%s': zeroing", s->name);
862 elf_sect_write(s, NULL, size);
863 } else
864 s->len += size;
865 } else if (type == OUT_RAWDATA) {
866 if (segment != NO_SEG)
867 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
868 elf_sect_write(s, data, size);
869 } else if (type == OUT_ADDRESS) {
870 addr = *(int64_t *)data;
871 if (segment == NO_SEG) {
872 /* Do nothing */
873 } else if (segment % 2) {
874 error(ERR_NONFATAL, "ELF format does not support"
875 " segment base references");
876 } else {
877 if (wrt == NO_SEG) {
878 switch ((int)size) {
879 case 1:
880 elf_add_reloc(s, segment, addr, R_X86_64_8);
881 break;
882 case 2:
883 elf_add_reloc(s, segment, addr, R_X86_64_16);
884 break;
885 case 4:
886 elf_add_reloc(s, segment, addr, R_X86_64_32);
887 break;
888 case 8:
889 elf_add_reloc(s, segment, addr, R_X86_64_64);
890 break;
891 default:
892 error(ERR_PANIC, "internal error elf64-hpa-871");
893 break;
895 addr = 0;
896 } else if (wrt == elf_gotpc_sect + 1) {
898 * The user will supply GOT relative to $$. ELF
899 * will let us have GOT relative to $. So we
900 * need to fix up the data item by $-$$.
902 addr += s->len;
903 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
904 addr = 0;
905 } else if (wrt == elf_gotoff_sect + 1) {
906 if (size != 8) {
907 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
908 "references to be qword");
909 } else {
910 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
911 addr = 0;
913 } else if (wrt == elf_got_sect + 1) {
914 switch ((int)size) {
915 case 4:
916 elf_add_gsym_reloc(s, segment, addr, 0,
917 R_X86_64_GOT32, true);
918 addr = 0;
919 break;
920 case 8:
921 elf_add_gsym_reloc(s, segment, addr, 0,
922 R_X86_64_GOT64, true);
923 addr = 0;
924 break;
925 default:
926 error(ERR_NONFATAL, "invalid ..got reference");
927 break;
929 } else if (wrt == elf_sym_sect + 1) {
930 switch ((int)size) {
931 case 1:
932 elf_add_gsym_reloc(s, segment, addr, 0,
933 R_X86_64_8, false);
934 addr = 0;
935 break;
936 case 2:
937 elf_add_gsym_reloc(s, segment, addr, 0,
938 R_X86_64_16, false);
939 addr = 0;
940 break;
941 case 4:
942 elf_add_gsym_reloc(s, segment, addr, 0,
943 R_X86_64_32, false);
944 addr = 0;
945 break;
946 case 8:
947 elf_add_gsym_reloc(s, segment, addr, 0,
948 R_X86_64_64, false);
949 addr = 0;
950 break;
951 default:
952 error(ERR_PANIC, "internal error elf64-hpa-903");
953 break;
955 } else if (wrt == elf_plt_sect + 1) {
956 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
957 "relative PLT references");
958 } else {
959 error(ERR_NONFATAL, "ELF format does not support this"
960 " use of WRT");
963 elf_sect_writeaddr(s, addr, size);
964 } else if (type == OUT_REL2ADR) {
965 addr = *(int64_t *)data - size;
966 if (segment == segto)
967 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
968 if (segment == NO_SEG) {
969 /* Do nothing */
970 } else if (segment % 2) {
971 error(ERR_NONFATAL, "ELF format does not support"
972 " segment base references");
973 } else {
974 if (wrt == NO_SEG) {
975 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
976 addr = 0;
977 } else {
978 error(ERR_NONFATAL,
979 "Unsupported non-32-bit ELF relocation [2]");
982 elf_sect_writeaddr(s, addr, 2);
983 } else if (type == OUT_REL4ADR) {
984 addr = *(int64_t *)data - size;
985 if (segment == segto)
986 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
987 if (segment == NO_SEG) {
988 /* Do nothing */
989 } else if (segment % 2) {
990 error(ERR_NONFATAL, "ELF64 format does not support"
991 " segment base references");
992 } else {
993 if (wrt == NO_SEG) {
994 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
995 addr = 0;
996 } else if (wrt == elf_plt_sect + 1) {
997 elf_add_gsym_reloc(s, segment, addr+size, size,
998 R_X86_64_PLT32, true);
999 addr = 0;
1000 } else if (wrt == elf_gotpc_sect + 1 ||
1001 wrt == elf_got_sect + 1) {
1002 elf_add_gsym_reloc(s, segment, addr+size, size,
1003 R_X86_64_GOTPCREL, true);
1004 addr = 0;
1005 } else if (wrt == elf_gotoff_sect + 1 ||
1006 wrt == elf_got_sect + 1) {
1007 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1008 "qword absolute");
1009 } else if (wrt == elf_gottpoff_sect + 1) {
1010 elf_add_gsym_reloc(s, segment, addr+size, size,
1011 R_X86_64_GOTTPOFF, true);
1012 addr = 0;
1013 } else {
1014 error(ERR_NONFATAL, "ELF64 format does not support this"
1015 " use of WRT");
1018 elf_sect_writeaddr(s, addr, 4);
1019 } else if (type == OUT_REL8ADR) {
1020 addr = *(int64_t *)data - size;
1021 if (segment == segto)
1022 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1023 if (segment == NO_SEG) {
1024 /* Do nothing */
1025 } else if (segment % 2) {
1026 error(ERR_NONFATAL, "ELF64 format does not support"
1027 " segment base references");
1028 } else {
1029 if (wrt == NO_SEG) {
1030 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1031 addr = 0;
1032 } else if (wrt == elf_gotpc_sect + 1 ||
1033 wrt == elf_got_sect + 1) {
1034 elf_add_gsym_reloc(s, segment, addr+size, size,
1035 R_X86_64_GOTPCREL64, true);
1036 addr = 0;
1037 } else if (wrt == elf_gotoff_sect + 1 ||
1038 wrt == elf_got_sect + 1) {
1039 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1040 "absolute");
1041 } else if (wrt == elf_gottpoff_sect + 1) {
1042 error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1043 "dword");
1044 } else {
1045 error(ERR_NONFATAL, "ELF64 format does not support this"
1046 " use of WRT");
1049 elf_sect_writeaddr(s, addr, 8);
1053 static void elf_write(void)
1055 int align;
1056 char *p;
1057 int i;
1059 struct SAA *symtab;
1060 int32_t symtablen, symtablocal;
1063 * Work out how many sections we will have. We have SHN_UNDEF,
1064 * then the flexible user sections, then the fixed sections
1065 * `.shstrtab', `.symtab' and `.strtab', then optionally
1066 * relocation sections for the user sections.
1068 nsections = sec_numspecial + 1;
1069 if (of_elf64.current_dfmt == &df_stabs)
1070 nsections += 3;
1071 else if (of_elf64.current_dfmt == &df_dwarf)
1072 nsections += 10;
1074 add_sectname("", ".shstrtab");
1075 add_sectname("", ".symtab");
1076 add_sectname("", ".strtab");
1077 for (i = 0; i < nsects; i++) {
1078 nsections++; /* for the section itself */
1079 if (sects[i]->head) {
1080 nsections++; /* for its relocations */
1081 add_sectname(".rela", sects[i]->name);
1085 if (of_elf64.current_dfmt == &df_stabs) {
1086 /* in case the debug information is wanted, just add these three sections... */
1087 add_sectname("", ".stab");
1088 add_sectname("", ".stabstr");
1089 add_sectname(".rel", ".stab");
1092 else if (of_elf64.current_dfmt == &df_dwarf) {
1093 /* the dwarf debug standard specifies the following ten sections,
1094 not all of which are currently implemented,
1095 although all of them are defined. */
1096 #define debug_aranges (int64_t) (nsections-10)
1097 #define debug_info (int64_t) (nsections-7)
1098 #define debug_abbrev (int64_t) (nsections-5)
1099 #define debug_line (int64_t) (nsections-4)
1100 add_sectname("", ".debug_aranges");
1101 add_sectname(".rela", ".debug_aranges");
1102 add_sectname("", ".debug_pubnames");
1103 add_sectname("", ".debug_info");
1104 add_sectname(".rela", ".debug_info");
1105 add_sectname("", ".debug_abbrev");
1106 add_sectname("", ".debug_line");
1107 add_sectname(".rela", ".debug_line");
1108 add_sectname("", ".debug_frame");
1109 add_sectname("", ".debug_loc");
1113 * Output the ELF header.
1115 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1116 fputc(elf_osabi, elffp);
1117 fputc(elf_abiver, elffp);
1118 fwritezero(7, elffp);
1119 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1120 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1121 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1122 fwriteint64_t(0L, elffp); /* no entry point */
1123 fwriteint64_t(0L, elffp); /* no program header table */
1124 fwriteint64_t(0x40L, elffp); /* section headers straight after
1125 * ELF header plus alignment */
1126 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1127 fwriteint16_t(0x40, elffp); /* size of ELF header */
1128 fwriteint16_t(0, elffp); /* no program header table, again */
1129 fwriteint16_t(0, elffp); /* still no program header table */
1130 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1131 fwriteint16_t(nsections, elffp); /* number of sections */
1132 fwriteint16_t(sec_shstrtab, elffp); /* string table section index for
1133 * section header table */
1136 * Build the symbol table and relocation tables.
1138 symtab = elf_build_symtab(&symtablen, &symtablocal);
1139 for (i = 0; i < nsects; i++)
1140 if (sects[i]->head)
1141 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1142 sects[i]->head);
1145 * Now output the section header table.
1148 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1149 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1150 elf_foffs += align;
1151 elf_nsect = 0;
1152 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1154 /* SHN_UNDEF */
1155 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1156 p = shstrtab + 1;
1158 /* The normal sections */
1159 for (i = 0; i < nsects; i++) {
1160 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1161 (sects[i]->type == SHT_PROGBITS ?
1162 sects[i]->data : NULL), true,
1163 sects[i]->len, 0, 0, sects[i]->align, 0);
1164 p += strlen(p) + 1;
1167 /* .shstrtab */
1168 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1169 shstrtablen, 0, 0, 1, 0);
1170 p += strlen(p) + 1;
1172 /* .symtab */
1173 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1174 symtablen, sec_strtab, symtablocal, 4, 24);
1175 p += strlen(p) + 1;
1177 /* .strtab */
1178 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1179 strslen, 0, 0, 1, 0);
1180 p += strlen(p) + 1;
1182 /* The relocation sections */
1183 for (i = 0; i < nsects; i++)
1184 if (sects[i]->head) {
1185 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1186 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1187 p += strlen(p) + 1;
1190 if (of_elf64.current_dfmt == &df_stabs) {
1191 /* for debugging information, create the last three sections
1192 which are the .stab , .stabstr and .rel.stab sections respectively */
1194 /* this function call creates the stab sections in memory */
1195 stabs64_generate();
1197 if (stabbuf && stabstrbuf && stabrelbuf) {
1198 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1199 stablen, sec_stabstr, 0, 4, 12);
1200 p += strlen(p) + 1;
1202 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1203 stabstrlen, 0, 0, 4, 0);
1204 p += strlen(p) + 1;
1206 /* link -> symtable info -> section to refer to */
1207 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1208 stabrellen, symtabsection, sec_stab, 4, 16);
1209 p += strlen(p) + 1;
1212 else if (of_elf64.current_dfmt == &df_dwarf) {
1213 /* for dwarf debugging information, create the ten dwarf sections */
1215 /* this function call creates the dwarf sections in memory */
1216 if (dwarf_fsect)
1217 dwarf64_generate();
1219 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1220 arangeslen, 0, 0, 1, 0);
1221 p += strlen(p) + 1;
1223 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1224 arangesrellen, symtabsection, debug_aranges, 1, 24);
1225 p += strlen(p) + 1;
1227 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1228 pubnameslen, 0, 0, 1, 0);
1229 p += strlen(p) + 1;
1231 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1232 infolen, 0, 0, 1, 0);
1233 p += strlen(p) + 1;
1235 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1236 inforellen, symtabsection, debug_info, 1, 24);
1237 p += strlen(p) + 1;
1239 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1240 abbrevlen, 0, 0, 1, 0);
1241 p += strlen(p) + 1;
1243 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1244 linelen, 0, 0, 1, 0);
1245 p += strlen(p) + 1;
1247 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1248 linerellen, symtabsection, debug_line, 1, 24);
1249 p += strlen(p) + 1;
1251 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1252 framelen, 0, 0, 8, 0);
1253 p += strlen(p) + 1;
1255 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1256 loclen, 0, 0, 1, 0);
1257 p += strlen(p) + 1;
1259 fwritezero(align, elffp);
1262 * Now output the sections.
1264 elf_write_sections();
1266 nasm_free(elf_sects);
1267 saa_free(symtab);
1270 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1272 struct SAA *s = saa_init(1L);
1273 struct Symbol *sym;
1274 uint8_t entry[24], *p;
1275 int i;
1277 *len = *local = 0;
1280 * First, an all-zeros entry, required by the ELF spec.
1282 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1283 *len += 24;
1284 (*local)++;
1287 * Next, an entry for the file name.
1289 p = entry;
1290 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1291 WRITESHORT(p, STT_FILE); /* type FILE */
1292 WRITESHORT(p, SHN_ABS);
1293 WRITEDLONG(p, (uint64_t) 0); /* no value */
1294 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1295 saa_wbytes(s, entry, 24L);
1296 *len += 24;
1297 (*local)++;
1300 * Now some standard symbols defining the segments, for relocation
1301 * purposes.
1303 for (i = 1; i <= nsects; i++) {
1304 p = entry;
1305 WRITELONG(p, 0); /* no symbol name */
1306 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1307 WRITESHORT(p, i); /* section id */
1308 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1309 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1310 saa_wbytes(s, entry, 24L);
1311 *len += 24;
1312 (*local)++;
1317 * Now the other local symbols.
1319 saa_rewind(syms);
1320 while ((sym = saa_rstruct(syms))) {
1321 if (sym->type & SYM_GLOBAL)
1322 continue;
1323 p = entry;
1324 WRITELONG(p, sym->strpos); /* index into symbol string table */
1325 WRITECHAR(p, sym->type); /* type and binding */
1326 WRITECHAR(p, sym->other); /* visibility */
1327 WRITESHORT(p, sym->section); /* index into section header table */
1328 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1329 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1330 saa_wbytes(s, entry, 24L);
1331 *len += 24;
1332 (*local)++;
1335 * dwarf needs symbols for debug sections
1336 * which are relocation targets.
1338 if (of_elf64.current_dfmt == &df_dwarf) {
1339 dwarf_infosym = *local;
1340 p = entry;
1341 WRITELONG(p, 0); /* no symbol name */
1342 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1343 WRITESHORT(p, debug_info); /* 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)++;
1349 dwarf_abbrevsym = *local;
1350 p = entry;
1351 WRITELONG(p, 0); /* no symbol name */
1352 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1353 WRITESHORT(p, debug_abbrev); /* section id */
1354 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1355 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1356 saa_wbytes(s, entry, 24L);
1357 *len += 24;
1358 (*local)++;
1359 dwarf_linesym = *local;
1360 p = entry;
1361 WRITELONG(p, 0); /* no symbol name */
1362 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1363 WRITESHORT(p, debug_line); /* section id */
1364 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1365 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1366 saa_wbytes(s, entry, 24L);
1367 *len += 24;
1368 (*local)++;
1372 * Now the global symbols.
1374 saa_rewind(syms);
1375 while ((sym = saa_rstruct(syms))) {
1376 if (!(sym->type & SYM_GLOBAL))
1377 continue;
1378 p = entry;
1379 WRITELONG(p, sym->strpos);
1380 WRITECHAR(p, sym->type); /* type and binding */
1381 WRITECHAR(p, sym->other); /* visibility */
1382 WRITESHORT(p, sym->section);
1383 WRITEDLONG(p, (int64_t)sym->symv.key);
1384 WRITEDLONG(p, (int64_t)sym->size);
1385 saa_wbytes(s, entry, 24L);
1386 *len += 24;
1389 return s;
1392 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1394 struct SAA *s;
1395 uint8_t *p, entry[24];
1396 int32_t global_offset;
1398 if (!r)
1399 return NULL;
1401 s = saa_init(1L);
1402 *len = 0;
1405 * How to onvert from a global placeholder to a real symbol index;
1406 * the +2 refers to the two special entries, the null entry and
1407 * the filename entry.
1409 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1411 while (r) {
1412 int32_t sym = r->symbol;
1414 if (sym >= GLOBAL_TEMP_BASE)
1415 sym += global_offset;
1417 p = entry;
1418 WRITEDLONG(p, r->address);
1419 WRITELONG(p, r->type);
1420 WRITELONG(p, sym);
1421 WRITEDLONG(p, r->offset);
1422 saa_wbytes(s, entry, 24L);
1423 *len += 24;
1425 r = r->next;
1428 return s;
1431 static void elf_section_header(int name, int type, uint64_t flags,
1432 void *data, bool is_saa, uint64_t datalen,
1433 int link, int info, int align, int eltsize)
1435 elf_sects[elf_nsect].data = data;
1436 elf_sects[elf_nsect].len = datalen;
1437 elf_sects[elf_nsect].is_saa = is_saa;
1438 elf_nsect++;
1440 fwriteint32_t((int32_t)name, elffp);
1441 fwriteint32_t((int32_t)type, elffp);
1442 fwriteint64_t((int64_t)flags, elffp);
1443 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1444 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1445 fwriteint64_t(datalen, elffp);
1446 if (data)
1447 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1448 fwriteint32_t((int32_t)link, elffp);
1449 fwriteint32_t((int32_t)info, elffp);
1450 fwriteint64_t((int64_t)align, elffp);
1451 fwriteint64_t((int64_t)eltsize, elffp);
1454 static void elf_write_sections(void)
1456 int i;
1457 for (i = 0; i < elf_nsect; i++)
1458 if (elf_sects[i].data) {
1459 int32_t len = elf_sects[i].len;
1460 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1461 int32_t align = reallen - len;
1462 if (elf_sects[i].is_saa)
1463 saa_fpwrite(elf_sects[i].data, elffp);
1464 else
1465 fwrite(elf_sects[i].data, len, 1, elffp);
1466 fwritezero(align, elffp);
1470 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1472 saa_wbytes(sect->data, data, len);
1473 sect->len += len;
1475 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1477 saa_writeaddr(sect->data, data, len);
1478 sect->len += len;
1481 static int32_t elf_segbase(int32_t segment)
1483 return segment;
1486 static int elf_directive(enum directives directive, char *value, int pass)
1488 bool err;
1489 int64_t n;
1490 char *p;
1492 switch (directive) {
1493 case D_OSABI:
1494 if (pass == 2)
1495 return 1; /* ignore in pass 2 */
1497 n = readnum(value, &err);
1498 if (err) {
1499 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1500 return 1;
1502 if (n < 0 || n > 255) {
1503 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1504 return 1;
1506 elf_osabi = n;
1507 elf_abiver = 0;
1509 if ((p = strchr(value,',')) == NULL)
1510 return 1;
1512 n = readnum(p+1, &err);
1513 if (err || n < 0 || n > 255) {
1514 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1515 return 1;
1518 elf_abiver = n;
1519 return 1;
1521 default:
1522 return 0;
1526 static void elf_filename(char *inname, char *outname, efunc error)
1528 strcpy(elf_module, inname);
1529 standard_extension(inname, outname, ".o", error);
1532 extern macros_t elf_stdmac[];
1534 static int elf_set_info(enum geninfo type, char **val)
1536 (void)type;
1537 (void)val;
1538 return 0;
1540 static struct dfmt df_dwarf = {
1541 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1542 "dwarf",
1543 dwarf64_init,
1544 dwarf64_linenum,
1545 debug64_deflabel,
1546 debug64_directive,
1547 debug64_typevalue,
1548 dwarf64_output,
1549 dwarf64_cleanup
1551 static struct dfmt df_stabs = {
1552 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1553 "stabs",
1554 null_debug_init,
1555 stabs64_linenum,
1556 debug64_deflabel,
1557 debug64_directive,
1558 debug64_typevalue,
1559 stabs64_output,
1560 stabs64_cleanup
1563 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1565 struct ofmt of_elf64 = {
1566 "ELF64 (x86_64) object files (e.g. Linux)",
1567 "elf64",
1569 elf64_debugs_arr,
1570 &df_stabs,
1571 elf_stdmac,
1572 elf_init,
1573 elf_set_info,
1574 elf_out,
1575 elf_deflabel,
1576 elf_section_names,
1577 elf_segbase,
1578 elf_directive,
1579 elf_filename,
1580 elf_cleanup
1583 /* common debugging routines */
1584 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1585 int is_global, char *special)
1587 (void)name;
1588 (void)segment;
1589 (void)offset;
1590 (void)is_global;
1591 (void)special;
1594 static void debug64_directive(const char *directive, const char *params)
1596 (void)directive;
1597 (void)params;
1600 static void debug64_typevalue(int32_t type)
1602 int32_t stype, ssize;
1603 switch (TYM_TYPE(type)) {
1604 case TY_LABEL:
1605 ssize = 0;
1606 stype = STT_NOTYPE;
1607 break;
1608 case TY_BYTE:
1609 ssize = 1;
1610 stype = STT_OBJECT;
1611 break;
1612 case TY_WORD:
1613 ssize = 2;
1614 stype = STT_OBJECT;
1615 break;
1616 case TY_DWORD:
1617 ssize = 4;
1618 stype = STT_OBJECT;
1619 break;
1620 case TY_FLOAT:
1621 ssize = 4;
1622 stype = STT_OBJECT;
1623 break;
1624 case TY_QWORD:
1625 ssize = 8;
1626 stype = STT_OBJECT;
1627 break;
1628 case TY_TBYTE:
1629 ssize = 10;
1630 stype = STT_OBJECT;
1631 break;
1632 case TY_OWORD:
1633 ssize = 16;
1634 stype = STT_OBJECT;
1635 break;
1636 case TY_COMMON:
1637 ssize = 0;
1638 stype = STT_COMMON;
1639 break;
1640 case TY_SEG:
1641 ssize = 0;
1642 stype = STT_SECTION;
1643 break;
1644 case TY_EXTERN:
1645 ssize = 0;
1646 stype = STT_NOTYPE;
1647 break;
1648 case TY_EQU:
1649 ssize = 0;
1650 stype = STT_NOTYPE;
1651 break;
1652 default:
1653 ssize = 0;
1654 stype = STT_NOTYPE;
1655 break;
1657 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1658 lastsym->size = ssize;
1659 lastsym->type = stype;
1663 /* stabs debugging routines */
1665 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1667 (void)segto;
1668 if (!stabs_filename) {
1669 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1670 strcpy(stabs_filename, filename);
1671 } else {
1672 if (strcmp(stabs_filename, filename)) {
1673 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1674 in fact, this leak comes in quite handy to maintain a list of files
1675 encountered so far in the symbol lines... */
1677 /* why not nasm_free(stabs_filename); we're done with the old one */
1679 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1680 strcpy(stabs_filename, filename);
1683 debug_immcall = 1;
1684 currentline = linenumber;
1688 static void stabs64_output(int type, void *param)
1690 struct symlininfo *s;
1691 struct linelist *el;
1692 if (type == TY_DEBUGSYMLIN) {
1693 if (debug_immcall) {
1694 s = (struct symlininfo *)param;
1695 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1696 return; /* line info is only collected for executable sections */
1697 numlinestabs++;
1698 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1699 el->info.offset = s->offset;
1700 el->info.section = s->section;
1701 el->info.name = s->name;
1702 el->line = currentline;
1703 el->filename = stabs_filename;
1704 el->next = 0;
1705 if (stabslines) {
1706 stabslines->last->next = el;
1707 stabslines->last = el;
1708 } else {
1709 stabslines = el;
1710 stabslines->last = el;
1714 debug_immcall = 0;
1717 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1718 do {\
1719 WRITELONG(p,n_strx); \
1720 WRITECHAR(p,n_type); \
1721 WRITECHAR(p,n_other); \
1722 WRITESHORT(p,n_desc); \
1723 WRITELONG(p,n_value); \
1724 } while (0)
1726 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1728 static void stabs64_generate(void)
1730 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1731 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1732 char **allfiles;
1733 int *fileidx;
1735 struct linelist *ptr;
1737 ptr = stabslines;
1739 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1740 for (i = 0; i < numlinestabs; i++)
1741 allfiles[i] = 0;
1742 numfiles = 0;
1743 while (ptr) {
1744 if (numfiles == 0) {
1745 allfiles[0] = ptr->filename;
1746 numfiles++;
1747 } else {
1748 for (i = 0; i < numfiles; i++) {
1749 if (!strcmp(allfiles[i], ptr->filename))
1750 break;
1752 if (i >= numfiles) {
1753 allfiles[i] = ptr->filename;
1754 numfiles++;
1757 ptr = ptr->next;
1759 strsize = 1;
1760 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1761 for (i = 0; i < numfiles; i++) {
1762 fileidx[i] = strsize;
1763 strsize += strlen(allfiles[i]) + 1;
1765 mainfileindex = 0;
1766 for (i = 0; i < numfiles; i++) {
1767 if (!strcmp(allfiles[i], elf_module)) {
1768 mainfileindex = i;
1769 break;
1773 /* worst case size of the stab buffer would be:
1774 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1776 sbuf =
1777 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1778 sizeof(struct stabentry));
1780 ssbuf = (uint8_t *)nasm_malloc(strsize);
1782 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1783 rptr = rbuf;
1785 for (i = 0; i < numfiles; i++) {
1786 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1788 ssbuf[0] = 0;
1790 stabstrlen = strsize; /* set global variable for length of stab strings */
1792 sptr = sbuf;
1793 ptr = stabslines;
1794 numstabs = 0;
1796 if (ptr) {
1797 /* this is the first stab, its strx points to the filename of the
1798 the source-file, the n_desc field should be set to the number
1799 of remaining stabs
1801 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1803 /* this is the stab for the main source file */
1804 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1806 /* relocation table entry */
1808 /* Since the symbol table has two entries before */
1809 /* the section symbols, the index in the info.section */
1810 /* member must be adjusted by adding 2 */
1812 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1813 WRITELONG(rptr, R_X86_64_32);
1814 WRITELONG(rptr, ptr->info.section + 2);
1816 numstabs++;
1817 currfile = mainfileindex;
1820 while (ptr) {
1821 if (strcmp(allfiles[currfile], ptr->filename)) {
1822 /* oops file has changed... */
1823 for (i = 0; i < numfiles; i++)
1824 if (!strcmp(allfiles[i], ptr->filename))
1825 break;
1826 currfile = i;
1827 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1828 ptr->info.offset);
1829 numstabs++;
1831 /* relocation table entry */
1833 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1834 WRITELONG(rptr, R_X86_64_32);
1835 WRITELONG(rptr, ptr->info.section + 2);
1838 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1839 numstabs++;
1841 /* relocation table entry */
1843 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1844 WRITELONG(rptr, R_X86_64_32);
1845 WRITELONG(rptr, ptr->info.section + 2);
1847 ptr = ptr->next;
1851 ((struct stabentry *)sbuf)->n_desc = numstabs;
1853 nasm_free(allfiles);
1854 nasm_free(fileidx);
1856 stablen = (sptr - sbuf);
1857 stabrellen = (rptr - rbuf);
1858 stabrelbuf = rbuf;
1859 stabbuf = sbuf;
1860 stabstrbuf = ssbuf;
1863 static void stabs64_cleanup(void)
1865 struct linelist *ptr, *del;
1866 if (!stabslines)
1867 return;
1868 ptr = stabslines;
1869 while (ptr) {
1870 del = ptr;
1871 ptr = ptr->next;
1872 nasm_free(del);
1874 if (stabbuf)
1875 nasm_free(stabbuf);
1876 if (stabrelbuf)
1877 nasm_free(stabrelbuf);
1878 if (stabstrbuf)
1879 nasm_free(stabstrbuf);
1881 /* dwarf routines */
1882 static void dwarf64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1884 (void)of;
1885 (void)id;
1886 (void)fp;
1887 (void)error;
1889 ndebugs = 3; /* 3 debug symbols */
1892 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1893 int32_t segto)
1895 (void)segto;
1896 dwarf64_findfile(filename);
1897 debug_immcall = 1;
1898 currentline = linenumber;
1901 /* called from elf_out with type == TY_DEBUGSYMLIN */
1902 static void dwarf64_output(int type, void *param)
1904 int ln, aa, inx, maxln, soc;
1905 struct symlininfo *s;
1906 struct SAA *plinep;
1908 (void)type;
1910 s = (struct symlininfo *)param;
1911 /* line number info is only gathered for executable sections */
1912 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1913 return;
1914 /* Check if section index has changed */
1915 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1917 dwarf64_findsect(s->section);
1919 /* do nothing unless line or file has changed */
1920 if (debug_immcall)
1922 ln = currentline - dwarf_csect->line;
1923 aa = s->offset - dwarf_csect->offset;
1924 inx = dwarf_clist->line;
1925 plinep = dwarf_csect->psaa;
1926 /* check for file change */
1927 if (!(inx == dwarf_csect->file))
1929 saa_write8(plinep,DW_LNS_set_file);
1930 saa_write8(plinep,inx);
1931 dwarf_csect->file = inx;
1933 /* check for line change */
1934 if (ln)
1936 /* test if in range of special op code */
1937 maxln = line_base + line_range;
1938 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1939 if (ln >= line_base && ln < maxln && soc < 256)
1941 saa_write8(plinep,soc);
1943 else
1945 if (ln)
1947 saa_write8(plinep,DW_LNS_advance_line);
1948 saa_wleb128s(plinep,ln);
1950 if (aa)
1952 saa_write8(plinep,DW_LNS_advance_pc);
1953 saa_wleb128u(plinep,aa);
1956 dwarf_csect->line = currentline;
1957 dwarf_csect->offset = s->offset;
1959 /* show change handled */
1960 debug_immcall = 0;
1965 static void dwarf64_generate(void)
1967 uint8_t *pbuf;
1968 int indx;
1969 struct linelist *ftentry;
1970 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1971 struct SAA *parangesrel, *plinesrel, *pinforel;
1972 struct sectlist *psect;
1973 size_t saalen, linepoff, totlen, highaddr;
1975 /* write epilogues for each line program range */
1976 /* and build aranges section */
1977 paranges = saa_init(1L);
1978 parangesrel = saa_init(1L);
1979 saa_write16(paranges,3); /* dwarf version */
1980 saa_write64(parangesrel, paranges->datalen+4);
1981 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1982 saa_write64(parangesrel, 0);
1983 saa_write32(paranges,0); /* offset into info */
1984 saa_write8(paranges,8); /* pointer size */
1985 saa_write8(paranges,0); /* not segmented */
1986 saa_write32(paranges,0); /* padding */
1987 /* iterate though sectlist entries */
1988 psect = dwarf_fsect;
1989 totlen = 0;
1990 highaddr = 0;
1991 for (indx = 0; indx < dwarf_nsections; indx++)
1993 plinep = psect->psaa;
1994 /* Line Number Program Epilogue */
1995 saa_write8(plinep,2); /* std op 2 */
1996 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1997 saa_write8(plinep,DW_LNS_extended_op);
1998 saa_write8(plinep,1); /* operand length */
1999 saa_write8(plinep,DW_LNE_end_sequence);
2000 totlen += plinep->datalen;
2001 /* range table relocation entry */
2002 saa_write64(parangesrel, paranges->datalen + 4);
2003 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2004 saa_write64(parangesrel, (uint64_t) 0);
2005 /* range table entry */
2006 saa_write64(paranges,0x0000); /* range start */
2007 saa_write64(paranges,sects[psect->section]->len); /* range length */
2008 highaddr += sects[psect->section]->len;
2009 /* done with this entry */
2010 psect = psect->next;
2012 saa_write64(paranges,0); /* null address */
2013 saa_write64(paranges,0); /* null length */
2014 saalen = paranges->datalen;
2015 arangeslen = saalen + 4;
2016 arangesbuf = pbuf = nasm_malloc(arangeslen);
2017 WRITELONG(pbuf,saalen); /* initial length */
2018 saa_rnbytes(paranges, pbuf, saalen);
2019 saa_free(paranges);
2021 /* build rela.aranges section */
2022 arangesrellen = saalen = parangesrel->datalen;
2023 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2024 saa_rnbytes(parangesrel, pbuf, saalen);
2025 saa_free(parangesrel);
2027 /* build pubnames section */
2028 ppubnames = saa_init(1L);
2029 saa_write16(ppubnames,3); /* dwarf version */
2030 saa_write32(ppubnames,0); /* offset into info */
2031 saa_write32(ppubnames,0); /* space used in info */
2032 saa_write32(ppubnames,0); /* end of list */
2033 saalen = ppubnames->datalen;
2034 pubnameslen = saalen + 4;
2035 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2036 WRITELONG(pbuf,saalen); /* initial length */
2037 saa_rnbytes(ppubnames, pbuf, saalen);
2038 saa_free(ppubnames);
2040 /* build info section */
2041 pinfo = saa_init(1L);
2042 pinforel = saa_init(1L);
2043 saa_write16(pinfo,3); /* dwarf version */
2044 saa_write64(pinforel, pinfo->datalen + 4);
2045 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2046 saa_write64(pinforel, 0);
2047 saa_write32(pinfo,0); /* offset into abbrev */
2048 saa_write8(pinfo,8); /* pointer size */
2049 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2050 saa_write64(pinforel, pinfo->datalen + 4);
2051 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2052 saa_write64(pinforel, 0);
2053 saa_write64(pinfo,0); /* DW_AT_low_pc */
2054 saa_write64(pinforel, pinfo->datalen + 4);
2055 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2056 saa_write64(pinforel, 0);
2057 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2058 saa_write64(pinforel, pinfo->datalen + 4);
2059 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2060 saa_write64(pinforel, 0);
2061 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2062 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2063 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2064 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2065 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2066 saa_write64(pinforel, pinfo->datalen + 4);
2067 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2068 saa_write64(pinforel, 0);
2069 saa_write64(pinfo,0); /* DW_AT_low_pc */
2070 saa_write64(pinfo,0); /* DW_AT_frame_base */
2071 saa_write8(pinfo,0); /* end of entries */
2072 saalen = pinfo->datalen;
2073 infolen = saalen + 4;
2074 infobuf = pbuf = nasm_malloc(infolen);
2075 WRITELONG(pbuf,saalen); /* initial length */
2076 saa_rnbytes(pinfo, pbuf, saalen);
2077 saa_free(pinfo);
2079 /* build rela.info section */
2080 inforellen = saalen = pinforel->datalen;
2081 inforelbuf = pbuf = nasm_malloc(inforellen);
2082 saa_rnbytes(pinforel, pbuf, saalen);
2083 saa_free(pinforel);
2085 /* build abbrev section */
2086 pabbrev = saa_init(1L);
2087 saa_write8(pabbrev,1); /* entry number LEB128u */
2088 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2089 saa_write8(pabbrev,1); /* has children */
2090 /* the following attributes and forms are all LEB128u values */
2091 saa_write8(pabbrev,DW_AT_low_pc);
2092 saa_write8(pabbrev,DW_FORM_addr);
2093 saa_write8(pabbrev,DW_AT_high_pc);
2094 saa_write8(pabbrev,DW_FORM_addr);
2095 saa_write8(pabbrev,DW_AT_stmt_list);
2096 saa_write8(pabbrev,DW_FORM_data4);
2097 saa_write8(pabbrev,DW_AT_name);
2098 saa_write8(pabbrev,DW_FORM_string);
2099 saa_write8(pabbrev,DW_AT_producer);
2100 saa_write8(pabbrev,DW_FORM_string);
2101 saa_write8(pabbrev,DW_AT_language);
2102 saa_write8(pabbrev,DW_FORM_data2);
2103 saa_write16(pabbrev,0); /* end of entry */
2104 /* LEB128u usage same as above */
2105 saa_write8(pabbrev,2); /* entry number */
2106 saa_write8(pabbrev,DW_TAG_subprogram);
2107 saa_write8(pabbrev,0); /* no children */
2108 saa_write8(pabbrev,DW_AT_low_pc);
2109 saa_write8(pabbrev,DW_FORM_addr);
2110 saa_write8(pabbrev,DW_AT_frame_base);
2111 saa_write8(pabbrev,DW_FORM_data4);
2112 saa_write16(pabbrev,0); /* end of entry */
2113 abbrevlen = saalen = pabbrev->datalen;
2114 abbrevbuf = pbuf = nasm_malloc(saalen);
2115 saa_rnbytes(pabbrev, pbuf, saalen);
2116 saa_free(pabbrev);
2118 /* build line section */
2119 /* prolog */
2120 plines = saa_init(1L);
2121 saa_write8(plines,1); /* Minimum Instruction Length */
2122 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2123 saa_write8(plines,line_base); /* Line Base */
2124 saa_write8(plines,line_range); /* Line Range */
2125 saa_write8(plines,opcode_base); /* Opcode Base */
2126 /* standard opcode lengths (# of LEB128u operands) */
2127 saa_write8(plines,0); /* Std opcode 1 length */
2128 saa_write8(plines,1); /* Std opcode 2 length */
2129 saa_write8(plines,1); /* Std opcode 3 length */
2130 saa_write8(plines,1); /* Std opcode 4 length */
2131 saa_write8(plines,1); /* Std opcode 5 length */
2132 saa_write8(plines,0); /* Std opcode 6 length */
2133 saa_write8(plines,0); /* Std opcode 7 length */
2134 saa_write8(plines,0); /* Std opcode 8 length */
2135 saa_write8(plines,1); /* Std opcode 9 length */
2136 saa_write8(plines,0); /* Std opcode 10 length */
2137 saa_write8(plines,0); /* Std opcode 11 length */
2138 saa_write8(plines,1); /* Std opcode 12 length */
2139 /* Directory Table */
2140 saa_write8(plines,0); /* End of table */
2141 /* File Name Table */
2142 ftentry = dwarf_flist;
2143 for (indx = 0;indx<dwarf_numfiles;indx++)
2145 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2146 saa_write8(plines,0); /* directory LEB128u */
2147 saa_write8(plines,0); /* time LEB128u */
2148 saa_write8(plines,0); /* size LEB128u */
2149 ftentry = ftentry->next;
2151 saa_write8(plines,0); /* End of table */
2152 linepoff = plines->datalen;
2153 linelen = linepoff + totlen + 10;
2154 linebuf = pbuf = nasm_malloc(linelen);
2155 WRITELONG(pbuf,linelen-4); /* initial length */
2156 WRITESHORT(pbuf,3); /* dwarf version */
2157 WRITELONG(pbuf,linepoff); /* offset to line number program */
2158 /* write line header */
2159 saalen = linepoff;
2160 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2161 pbuf += linepoff;
2162 saa_free(plines);
2163 /* concatonate line program ranges */
2164 linepoff += 13;
2165 plinesrel = saa_init(1L);
2166 psect = dwarf_fsect;
2167 for (indx = 0; indx < dwarf_nsections; indx++)
2169 saa_write64(plinesrel, linepoff);
2170 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2171 saa_write64(plinesrel, (uint64_t) 0);
2172 plinep = psect->psaa;
2173 saalen = plinep->datalen;
2174 saa_rnbytes(plinep, pbuf, saalen);
2175 pbuf += saalen;
2176 linepoff += saalen;
2177 saa_free(plinep);
2178 /* done with this entry */
2179 psect = psect->next;
2183 /* build rela.lines section */
2184 linerellen =saalen = plinesrel->datalen;
2185 linerelbuf = pbuf = nasm_malloc(linerellen);
2186 saa_rnbytes(plinesrel, pbuf, saalen);
2187 saa_free(plinesrel);
2189 /* build frame section */
2190 framelen = 4;
2191 framebuf = pbuf = nasm_malloc(framelen);
2192 WRITELONG(pbuf,framelen-4); /* initial length */
2194 /* build loc section */
2195 loclen = 16;
2196 locbuf = pbuf = nasm_malloc(loclen);
2197 WRITEDLONG(pbuf,0); /* null beginning offset */
2198 WRITEDLONG(pbuf,0); /* null ending offset */
2201 static void dwarf64_cleanup(void)
2203 if (arangesbuf)
2204 nasm_free(arangesbuf);
2205 if (arangesrelbuf)
2206 nasm_free(arangesrelbuf);
2207 if (pubnamesbuf)
2208 nasm_free(pubnamesbuf);
2209 if (infobuf)
2210 nasm_free(infobuf);
2211 if (inforelbuf)
2212 nasm_free(inforelbuf);
2213 if (abbrevbuf)
2214 nasm_free(abbrevbuf);
2215 if (linebuf)
2216 nasm_free(linebuf);
2217 if (linerelbuf)
2218 nasm_free(linerelbuf);
2219 if (framebuf)
2220 nasm_free(framebuf);
2221 if (locbuf)
2222 nasm_free(locbuf);
2224 static void dwarf64_findfile(const char * fname)
2226 int finx;
2227 struct linelist *match;
2229 /* return if fname is current file name */
2230 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2231 /* search for match */
2232 else
2234 match = 0;
2235 if (dwarf_flist)
2237 match = dwarf_flist;
2238 for (finx = 0; finx < dwarf_numfiles; finx++)
2240 if (!(strcmp(fname, match->filename)))
2242 dwarf_clist = match;
2243 return;
2247 /* add file name to end of list */
2248 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2249 dwarf_numfiles++;
2250 dwarf_clist->line = dwarf_numfiles;
2251 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2252 strcpy(dwarf_clist->filename,fname);
2253 dwarf_clist->next = 0;
2254 /* if first entry */
2255 if (!dwarf_flist)
2257 dwarf_flist = dwarf_elist = dwarf_clist;
2258 dwarf_clist->last = 0;
2260 /* chain to previous entry */
2261 else
2263 dwarf_elist->next = dwarf_clist;
2264 dwarf_elist = dwarf_clist;
2268 /* */
2269 static void dwarf64_findsect(const int index)
2271 int sinx;
2272 struct sectlist *match;
2273 struct SAA *plinep;
2274 /* return if index is current section index */
2275 if (dwarf_csect && (dwarf_csect->section == index))
2277 return;
2279 /* search for match */
2280 else
2282 match = 0;
2283 if (dwarf_fsect)
2285 match = dwarf_fsect;
2286 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2288 if ((match->section == index))
2290 dwarf_csect = match;
2291 return;
2293 match = match->next;
2296 /* add entry to end of list */
2297 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2298 dwarf_nsections++;
2299 dwarf_csect->psaa = plinep = saa_init(1L);
2300 dwarf_csect->line = 1;
2301 dwarf_csect->offset = 0;
2302 dwarf_csect->file = 1;
2303 dwarf_csect->section = index;
2304 dwarf_csect->next = 0;
2305 /* set relocatable address at start of line program */
2306 saa_write8(plinep,DW_LNS_extended_op);
2307 saa_write8(plinep,9); /* operand length */
2308 saa_write8(plinep,DW_LNE_set_address);
2309 saa_write64(plinep,0); /* Start Address */
2310 /* if first entry */
2311 if (!dwarf_fsect)
2313 dwarf_fsect = dwarf_esect = dwarf_csect;
2314 dwarf_csect->last = 0;
2316 /* chain to previous entry */
2317 else
2319 dwarf_esect->next = dwarf_csect;
2320 dwarf_esect = dwarf_csect;
2325 #endif /* OF_ELF */