outcoff: fix invalid reference to ofmt
[nasm/avx512.git] / output / outelf64.c
bloba7c51b8abb61bd0b47adff1791da6d14885bca64
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 fclose(elffp);
304 for (i = 0; i < nsects; i++) {
305 if (sects[i]->type != SHT_NOBITS)
306 saa_free(sects[i]->data);
307 if (sects[i]->head)
308 saa_free(sects[i]->rel);
309 while (sects[i]->head) {
310 r = sects[i]->head;
311 sects[i]->head = sects[i]->head->next;
312 nasm_free(r);
315 nasm_free(sects);
316 saa_free(syms);
317 raa_free(bsym);
318 saa_free(strs);
319 if (of_elf64.current_dfmt) {
320 of_elf64.current_dfmt->cleanup();
323 /* add entry to the elf .shstrtab section */
324 static void add_sectname(char *firsthalf, char *secondhalf)
326 int len = strlen(firsthalf) + strlen(secondhalf);
327 while (shstrtablen + len + 1 > shstrtabsize)
328 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
329 strcpy(shstrtab + shstrtablen, firsthalf);
330 strcat(shstrtab + shstrtablen, secondhalf);
331 shstrtablen += len + 1;
334 static int elf_make_section(char *name, int type, int flags, int align)
336 struct Section *s;
338 s = nasm_malloc(sizeof(*s));
340 if (type != SHT_NOBITS)
341 s->data = saa_init(1L);
342 s->head = NULL;
343 s->tail = &s->head;
344 s->len = s->size = 0;
345 s->nrelocs = 0;
346 if (!strcmp(name, ".text"))
347 s->index = def_seg;
348 else
349 s->index = seg_alloc();
350 add_sectname("", name);
351 s->name = nasm_malloc(1 + strlen(name));
352 strcpy(s->name, name);
353 s->type = type;
354 s->flags = flags;
355 s->align = align;
356 s->gsyms = NULL;
358 if (nsects >= sectlen)
359 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
360 sects[nsects++] = s;
362 return nsects - 1;
365 static int32_t elf_section_names(char *name, int pass, int *bits)
367 char *p;
368 uint32_t flags, flags_and, flags_or;
369 uint64_t align;
370 int type, i;
373 * Default is 64 bits.
375 if (!name) {
376 *bits = 64;
377 return def_seg;
380 p = name;
381 while (*p && !nasm_isspace(*p))
382 p++;
383 if (*p)
384 *p++ = '\0';
385 flags_and = flags_or = type = align = 0;
387 while (*p && nasm_isspace(*p))
388 p++;
389 while (*p) {
390 char *q = p;
391 while (*p && !nasm_isspace(*p))
392 p++;
393 if (*p)
394 *p++ = '\0';
395 while (*p && nasm_isspace(*p))
396 p++;
398 if (!nasm_strnicmp(q, "align=", 6)) {
399 align = atoi(q + 6);
400 if (align == 0)
401 align = 1;
402 if ((align - 1) & align) { /* means it's not a power of two */
403 error(ERR_NONFATAL, "section alignment %d is not"
404 " a power of two", align);
405 align = 1;
407 } else if (!nasm_stricmp(q, "alloc")) {
408 flags_and |= SHF_ALLOC;
409 flags_or |= SHF_ALLOC;
410 } else if (!nasm_stricmp(q, "noalloc")) {
411 flags_and |= SHF_ALLOC;
412 flags_or &= ~SHF_ALLOC;
413 } else if (!nasm_stricmp(q, "exec")) {
414 flags_and |= SHF_EXECINSTR;
415 flags_or |= SHF_EXECINSTR;
416 } else if (!nasm_stricmp(q, "noexec")) {
417 flags_and |= SHF_EXECINSTR;
418 flags_or &= ~SHF_EXECINSTR;
419 } else if (!nasm_stricmp(q, "write")) {
420 flags_and |= SHF_WRITE;
421 flags_or |= SHF_WRITE;
422 } else if (!nasm_stricmp(q, "tls")) {
423 flags_and |= SHF_TLS;
424 flags_or |= SHF_TLS;
425 } else if (!nasm_stricmp(q, "nowrite")) {
426 flags_and |= SHF_WRITE;
427 flags_or &= ~SHF_WRITE;
428 } else if (!nasm_stricmp(q, "progbits")) {
429 type = SHT_PROGBITS;
430 } else if (!nasm_stricmp(q, "nobits")) {
431 type = SHT_NOBITS;
432 } else if (pass == 1) {
433 error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
434 " declaration of section `%s'", q, name);
438 if (!strcmp(name, ".shstrtab") ||
439 !strcmp(name, ".symtab") ||
440 !strcmp(name, ".strtab")) {
441 error(ERR_NONFATAL, "attempt to redefine reserved section"
442 "name `%s'", name);
443 return NO_SEG;
446 for (i = 0; i < nsects; i++)
447 if (!strcmp(name, sects[i]->name))
448 break;
449 if (i == nsects) {
450 const struct elf_known_section *ks = elf_known_sections;
452 while (ks->name) {
453 if (!strcmp(name, ks->name))
454 break;
455 ks++;
458 type = type ? type : ks->type;
459 align = align ? align : ks->align;
460 flags = (ks->flags & ~flags_and) | flags_or;
462 i = elf_make_section(name, type, flags, align);
463 } else if (pass == 1) {
464 if ((type && sects[i]->type != type)
465 || (align && sects[i]->align != align)
466 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
467 error(ERR_WARNING, "incompatible section attributes ignored on"
468 " redeclaration of section `%s'", name);
471 return sects[i]->index;
474 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
475 int is_global, char *special)
477 int pos = strslen;
478 struct Symbol *sym;
479 bool special_used = false;
481 #if defined(DEBUG) && DEBUG>2
482 fprintf(stderr,
483 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
484 name, segment, offset, is_global, special);
485 #endif
486 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
488 * This is a NASM special symbol. We never allow it into
489 * the ELF symbol table, even if it's a valid one. If it
490 * _isn't_ a valid one, we should barf immediately.
492 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
493 strcmp(name, "..got") && strcmp(name, "..plt") &&
494 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
495 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
496 return;
499 if (is_global == 3) {
500 struct Symbol **s;
502 * Fix up a forward-reference symbol size from the first
503 * pass.
505 for (s = &fwds; *s; s = &(*s)->nextfwd)
506 if (!strcmp((*s)->name, name)) {
507 struct tokenval tokval;
508 expr *e;
509 char *p = special;
511 while (*p && !nasm_isspace(*p))
512 p++;
513 while (*p && nasm_isspace(*p))
514 p++;
515 stdscan_reset();
516 stdscan_bufptr = p;
517 tokval.t_type = TOKEN_INVALID;
518 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
519 if (e) {
520 if (!is_simple(e))
521 error(ERR_NONFATAL, "cannot use relocatable"
522 " expression as symbol size");
523 else
524 (*s)->size = reloc_value(e);
528 * Remove it from the list of unresolved sizes.
530 nasm_free((*s)->name);
531 *s = (*s)->nextfwd;
532 return;
534 return; /* it wasn't an important one */
537 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
538 strslen += 1 + strlen(name);
540 lastsym = sym = saa_wstruct(syms);
542 memset(&sym->symv, 0, sizeof(struct rbtree));
544 sym->strpos = pos;
545 sym->type = is_global ? SYM_GLOBAL : 0;
546 sym->other = STV_DEFAULT;
547 sym->size = 0;
548 if (segment == NO_SEG)
549 sym->section = SHN_ABS;
550 else {
551 int i;
552 sym->section = SHN_UNDEF;
553 if (nsects == 0 && segment == def_seg) {
554 int tempint;
555 if (segment != elf_section_names(".text", 2, &tempint))
556 error(ERR_PANIC,
557 "strange segment conditions in ELF driver");
558 sym->section = nsects;
559 } else {
560 for (i = 0; i < nsects; i++)
561 if (segment == sects[i]->index) {
562 sym->section = i + 1;
563 break;
568 if (is_global == 2) {
569 sym->size = offset;
570 sym->symv.key = 0;
571 sym->section = SHN_COMMON;
573 * We have a common variable. Check the special text to see
574 * if it's a valid number and power of two; if so, store it
575 * as the alignment for the common variable.
577 if (special) {
578 bool err;
579 sym->symv.key = readnum(special, &err);
580 if (err)
581 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
582 " valid number", special);
583 else if ((sym->symv.key | (sym->symv.key - 1))
584 != 2 * sym->symv.key - 1)
585 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
586 " power of two", special);
588 special_used = true;
589 } else
590 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
592 if (sym->type == SYM_GLOBAL) {
594 * If sym->section == SHN_ABS, then the first line of the
595 * else section would cause a core dump, because its a reference
596 * beyond the end of the section array.
597 * This behaviour is exhibited by this code:
598 * GLOBAL crash_nasm
599 * crash_nasm equ 0
600 * To avoid such a crash, such requests are silently discarded.
601 * This may not be the best solution.
603 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
604 bsym = raa_write(bsym, segment, nglobs);
605 } else if (sym->section != SHN_ABS) {
607 * This is a global symbol; so we must add it to the rbtree
608 * of global symbols in its section.
610 * In addition, we check the special text for symbol
611 * type and size information.
613 sects[sym->section-1]->gsyms =
614 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
616 if (special) {
617 int n = strcspn(special, " \t");
619 if (!nasm_strnicmp(special, "function", n))
620 sym->type |= STT_FUNC;
621 else if (!nasm_strnicmp(special, "data", n) ||
622 !nasm_strnicmp(special, "object", n))
623 sym->type |= STT_OBJECT;
624 else if (!nasm_strnicmp(special, "notype", n))
625 sym->type |= STT_NOTYPE;
626 else
627 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
628 n, special);
629 special += n;
631 while (nasm_isspace(*special))
632 ++special;
633 if (*special) {
634 n = strcspn(special, " \t");
635 if (!nasm_strnicmp(special, "default", n))
636 sym->other = STV_DEFAULT;
637 else if (!nasm_strnicmp(special, "internal", n))
638 sym->other = STV_INTERNAL;
639 else if (!nasm_strnicmp(special, "hidden", n))
640 sym->other = STV_HIDDEN;
641 else if (!nasm_strnicmp(special, "protected", n))
642 sym->other = STV_PROTECTED;
643 else
644 n = 0;
645 special += n;
648 if (*special) {
649 struct tokenval tokval;
650 expr *e;
651 int fwd = 0;
652 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
654 while (special[n] && nasm_isspace(special[n]))
655 n++;
657 * We have a size expression; attempt to
658 * evaluate it.
660 stdscan_reset();
661 stdscan_bufptr = special + n;
662 tokval.t_type = TOKEN_INVALID;
663 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
664 NULL);
665 if (fwd) {
666 sym->nextfwd = fwds;
667 fwds = sym;
668 sym->name = nasm_strdup(name);
669 } else if (e) {
670 if (!is_simple(e))
671 error(ERR_NONFATAL, "cannot use relocatable"
672 " expression as symbol size");
673 else
674 sym->size = reloc_value(e);
676 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
678 special_used = true;
681 * If TLS segment, mark symbol accordingly.
683 if (sects[sym->section - 1]->flags & SHF_TLS) {
684 sym->type &= 0xf0;
685 sym->type |= STT_TLS;
688 sym->globnum = nglobs;
689 nglobs++;
690 } else
691 nlocals++;
693 if (special && !special_used)
694 error(ERR_NONFATAL, "no special symbol features supported here");
697 static void elf_add_reloc(struct Section *sect, int32_t segment,
698 int64_t offset, int type)
700 struct Reloc *r;
701 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
702 sect->tail = &r->next;
703 r->next = NULL;
705 r->address = sect->len;
706 r->offset = offset;
707 if (segment == NO_SEG)
708 r->symbol = 0;
709 else {
710 int i;
711 r->symbol = 0;
712 for (i = 0; i < nsects; i++)
713 if (segment == sects[i]->index)
714 r->symbol = i + 2;
715 if (!r->symbol)
716 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
718 r->type = type;
720 sect->nrelocs++;
724 * This routine deals with ..got and ..sym relocations: the more
725 * complicated kinds. In shared-library writing, some relocations
726 * with respect to global symbols must refer to the precise symbol
727 * rather than referring to an offset from the base of the section
728 * _containing_ the symbol. Such relocations call to this routine,
729 * which searches the symbol list for the symbol in question.
731 * R_386_GOT32 references require the _exact_ symbol address to be
732 * used; R_386_32 references can be at an offset from the symbol.
733 * The boolean argument `exact' tells us this.
735 * Return value is the adjusted value of `addr', having become an
736 * offset from the symbol rather than the section. Should always be
737 * zero when returning from an exact call.
739 * Limitation: if you define two symbols at the same place,
740 * confusion will occur.
742 * Inefficiency: we search, currently, using a linked list which
743 * isn't even necessarily sorted.
745 static void elf_add_gsym_reloc(struct Section *sect,
746 int32_t segment, uint64_t offset, int64_t pcrel,
747 int type, bool exact)
749 struct Reloc *r;
750 struct Section *s;
751 struct Symbol *sym;
752 struct rbtree *srb;
753 int i;
756 * First look up the segment/offset pair and find a global
757 * symbol corresponding to it. If it's not one of our segments,
758 * then it must be an external symbol, in which case we're fine
759 * doing a normal elf_add_reloc after first sanity-checking
760 * that the offset from the symbol is zero.
762 s = NULL;
763 for (i = 0; i < nsects; i++)
764 if (segment == sects[i]->index) {
765 s = sects[i];
766 break;
769 if (!s) {
770 if (exact && offset)
771 error(ERR_NONFATAL, "invalid access to an external symbol");
772 else
773 elf_add_reloc(sect, segment, offset - pcrel, type);
774 return;
777 srb = rb_search(s->gsyms, offset);
778 if (!srb || (exact && srb->key != offset)) {
779 error(ERR_NONFATAL, "unable to find a suitable global symbol"
780 " for this reference");
781 return;
783 sym = container_of(srb, struct Symbol, symv);
785 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
786 sect->tail = &r->next;
787 r->next = NULL;
789 r->address = sect->len;
790 r->offset = offset - pcrel - sym->symv.key;
791 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
792 r->type = type;
794 sect->nrelocs++;
797 static void elf_out(int32_t segto, const void *data,
798 enum out_type type, uint64_t size,
799 int32_t segment, int32_t wrt)
801 struct Section *s;
802 int64_t addr, zero;
803 int i;
804 static struct symlininfo sinfo;
806 zero = 0;
808 #if defined(DEBUG) && DEBUG>2
809 if (data) fprintf(stderr,
810 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
811 currentline, type, segment, segto, size, *(int64_t *)data);
812 else fprintf(stderr,
813 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
814 currentline, type, segment, segto, size);
815 #endif
818 * handle absolute-assembly (structure definitions)
820 if (segto == NO_SEG) {
821 if (type != OUT_RESERVE)
822 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
823 " space");
824 return;
827 s = NULL;
828 for (i = 0; i < nsects; i++)
829 if (segto == sects[i]->index) {
830 s = sects[i];
831 break;
833 if (!s) {
834 int tempint; /* ignored */
835 if (segto != elf_section_names(".text", 2, &tempint))
836 error(ERR_PANIC, "strange segment conditions in ELF driver");
837 else {
838 s = sects[nsects - 1];
839 i = nsects - 1;
842 /* invoke current debug_output routine */
843 if (of_elf64.current_dfmt) {
844 sinfo.offset = s->len;
845 sinfo.section = i;
846 sinfo.segto = segto;
847 sinfo.name = s->name;
848 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
850 /* end of debugging stuff */
852 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
853 error(ERR_WARNING, "attempt to initialize memory in"
854 " BSS section `%s': ignored", s->name);
855 s->len += realsize(type, size);
856 return;
859 if (type == OUT_RESERVE) {
860 if (s->type == SHT_PROGBITS) {
861 error(ERR_WARNING, "uninitialized space declared in"
862 " non-BSS section `%s': zeroing", s->name);
863 elf_sect_write(s, NULL, size);
864 } else
865 s->len += size;
866 } else if (type == OUT_RAWDATA) {
867 if (segment != NO_SEG)
868 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
869 elf_sect_write(s, data, size);
870 } else if (type == OUT_ADDRESS) {
871 addr = *(int64_t *)data;
872 if (segment == NO_SEG) {
873 /* Do nothing */
874 } else if (segment % 2) {
875 error(ERR_NONFATAL, "ELF format does not support"
876 " segment base references");
877 } else {
878 if (wrt == NO_SEG) {
879 switch ((int)size) {
880 case 1:
881 elf_add_reloc(s, segment, addr, R_X86_64_8);
882 break;
883 case 2:
884 elf_add_reloc(s, segment, addr, R_X86_64_16);
885 break;
886 case 4:
887 elf_add_reloc(s, segment, addr, R_X86_64_32);
888 break;
889 case 8:
890 elf_add_reloc(s, segment, addr, R_X86_64_64);
891 break;
892 default:
893 error(ERR_PANIC, "internal error elf64-hpa-871");
894 break;
896 addr = 0;
897 } else if (wrt == elf_gotpc_sect + 1) {
899 * The user will supply GOT relative to $$. ELF
900 * will let us have GOT relative to $. So we
901 * need to fix up the data item by $-$$.
903 addr += s->len;
904 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
905 addr = 0;
906 } else if (wrt == elf_gotoff_sect + 1) {
907 if (size != 8) {
908 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
909 "references to be qword");
910 } else {
911 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
912 addr = 0;
914 } else if (wrt == elf_got_sect + 1) {
915 switch ((int)size) {
916 case 4:
917 elf_add_gsym_reloc(s, segment, addr, 0,
918 R_X86_64_GOT32, true);
919 addr = 0;
920 break;
921 case 8:
922 elf_add_gsym_reloc(s, segment, addr, 0,
923 R_X86_64_GOT64, true);
924 addr = 0;
925 break;
926 default:
927 error(ERR_NONFATAL, "invalid ..got reference");
928 break;
930 } else if (wrt == elf_sym_sect + 1) {
931 switch ((int)size) {
932 case 1:
933 elf_add_gsym_reloc(s, segment, addr, 0,
934 R_X86_64_8, false);
935 addr = 0;
936 break;
937 case 2:
938 elf_add_gsym_reloc(s, segment, addr, 0,
939 R_X86_64_16, false);
940 addr = 0;
941 break;
942 case 4:
943 elf_add_gsym_reloc(s, segment, addr, 0,
944 R_X86_64_32, false);
945 addr = 0;
946 break;
947 case 8:
948 elf_add_gsym_reloc(s, segment, addr, 0,
949 R_X86_64_64, false);
950 addr = 0;
951 break;
952 default:
953 error(ERR_PANIC, "internal error elf64-hpa-903");
954 break;
956 } else if (wrt == elf_plt_sect + 1) {
957 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
958 "relative PLT references");
959 } else {
960 error(ERR_NONFATAL, "ELF format does not support this"
961 " use of WRT");
964 elf_sect_writeaddr(s, addr, size);
965 } else if (type == OUT_REL2ADR) {
966 addr = *(int64_t *)data - size;
967 if (segment == segto)
968 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
969 if (segment == NO_SEG) {
970 /* Do nothing */
971 } else if (segment % 2) {
972 error(ERR_NONFATAL, "ELF format does not support"
973 " segment base references");
974 } else {
975 if (wrt == NO_SEG) {
976 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
977 addr = 0;
978 } else {
979 error(ERR_NONFATAL,
980 "Unsupported non-32-bit ELF relocation [2]");
983 elf_sect_writeaddr(s, addr, 2);
984 } else if (type == OUT_REL4ADR) {
985 addr = *(int64_t *)data - size;
986 if (segment == segto)
987 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
988 if (segment == NO_SEG) {
989 /* Do nothing */
990 } else if (segment % 2) {
991 error(ERR_NONFATAL, "ELF64 format does not support"
992 " segment base references");
993 } else {
994 if (wrt == NO_SEG) {
995 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
996 addr = 0;
997 } else if (wrt == elf_plt_sect + 1) {
998 elf_add_gsym_reloc(s, segment, addr+size, size,
999 R_X86_64_PLT32, true);
1000 addr = 0;
1001 } else if (wrt == elf_gotpc_sect + 1 ||
1002 wrt == elf_got_sect + 1) {
1003 elf_add_gsym_reloc(s, segment, addr+size, size,
1004 R_X86_64_GOTPCREL, true);
1005 addr = 0;
1006 } else if (wrt == elf_gotoff_sect + 1 ||
1007 wrt == elf_got_sect + 1) {
1008 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1009 "qword absolute");
1010 } else if (wrt == elf_gottpoff_sect + 1) {
1011 elf_add_gsym_reloc(s, segment, addr+size, size,
1012 R_X86_64_GOTTPOFF, true);
1013 addr = 0;
1014 } else {
1015 error(ERR_NONFATAL, "ELF64 format does not support this"
1016 " use of WRT");
1019 elf_sect_writeaddr(s, addr, 4);
1020 } else if (type == OUT_REL8ADR) {
1021 addr = *(int64_t *)data - size;
1022 if (segment == segto)
1023 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1024 if (segment == NO_SEG) {
1025 /* Do nothing */
1026 } else if (segment % 2) {
1027 error(ERR_NONFATAL, "ELF64 format does not support"
1028 " segment base references");
1029 } else {
1030 if (wrt == NO_SEG) {
1031 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1032 addr = 0;
1033 } else if (wrt == elf_gotpc_sect + 1 ||
1034 wrt == elf_got_sect + 1) {
1035 elf_add_gsym_reloc(s, segment, addr+size, size,
1036 R_X86_64_GOTPCREL64, true);
1037 addr = 0;
1038 } else if (wrt == elf_gotoff_sect + 1 ||
1039 wrt == elf_got_sect + 1) {
1040 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1041 "absolute");
1042 } else if (wrt == elf_gottpoff_sect + 1) {
1043 error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1044 "dword");
1045 } else {
1046 error(ERR_NONFATAL, "ELF64 format does not support this"
1047 " use of WRT");
1050 elf_sect_writeaddr(s, addr, 8);
1054 static void elf_write(void)
1056 int align;
1057 char *p;
1058 int i;
1060 struct SAA *symtab;
1061 int32_t symtablen, symtablocal;
1064 * Work out how many sections we will have. We have SHN_UNDEF,
1065 * then the flexible user sections, then the fixed sections
1066 * `.shstrtab', `.symtab' and `.strtab', then optionally
1067 * relocation sections for the user sections.
1069 nsections = sec_numspecial + 1;
1070 if (of_elf64.current_dfmt == &df_stabs)
1071 nsections += 3;
1072 else if (of_elf64.current_dfmt == &df_dwarf)
1073 nsections += 10;
1075 add_sectname("", ".shstrtab");
1076 add_sectname("", ".symtab");
1077 add_sectname("", ".strtab");
1078 for (i = 0; i < nsects; i++) {
1079 nsections++; /* for the section itself */
1080 if (sects[i]->head) {
1081 nsections++; /* for its relocations */
1082 add_sectname(".rela", sects[i]->name);
1086 if (of_elf64.current_dfmt == &df_stabs) {
1087 /* in case the debug information is wanted, just add these three sections... */
1088 add_sectname("", ".stab");
1089 add_sectname("", ".stabstr");
1090 add_sectname(".rel", ".stab");
1093 else if (of_elf64.current_dfmt == &df_dwarf) {
1094 /* the dwarf debug standard specifies the following ten sections,
1095 not all of which are currently implemented,
1096 although all of them are defined. */
1097 #define debug_aranges (int64_t) (nsections-10)
1098 #define debug_info (int64_t) (nsections-7)
1099 #define debug_abbrev (int64_t) (nsections-5)
1100 #define debug_line (int64_t) (nsections-4)
1101 add_sectname("", ".debug_aranges");
1102 add_sectname(".rela", ".debug_aranges");
1103 add_sectname("", ".debug_pubnames");
1104 add_sectname("", ".debug_info");
1105 add_sectname(".rela", ".debug_info");
1106 add_sectname("", ".debug_abbrev");
1107 add_sectname("", ".debug_line");
1108 add_sectname(".rela", ".debug_line");
1109 add_sectname("", ".debug_frame");
1110 add_sectname("", ".debug_loc");
1114 * Output the ELF header.
1116 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1117 fputc(elf_osabi, elffp);
1118 fputc(elf_abiver, elffp);
1119 fwritezero(7, elffp);
1120 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1121 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1122 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1123 fwriteint64_t(0L, elffp); /* no entry point */
1124 fwriteint64_t(0L, elffp); /* no program header table */
1125 fwriteint64_t(0x40L, elffp); /* section headers straight after
1126 * ELF header plus alignment */
1127 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1128 fwriteint16_t(0x40, elffp); /* size of ELF header */
1129 fwriteint16_t(0, elffp); /* no program header table, again */
1130 fwriteint16_t(0, elffp); /* still no program header table */
1131 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1132 fwriteint16_t(nsections, elffp); /* number of sections */
1133 fwriteint16_t(sec_shstrtab, elffp); /* string table section index for
1134 * section header table */
1137 * Build the symbol table and relocation tables.
1139 symtab = elf_build_symtab(&symtablen, &symtablocal);
1140 for (i = 0; i < nsects; i++)
1141 if (sects[i]->head)
1142 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1143 sects[i]->head);
1146 * Now output the section header table.
1149 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1150 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1151 elf_foffs += align;
1152 elf_nsect = 0;
1153 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1155 /* SHN_UNDEF */
1156 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1157 p = shstrtab + 1;
1159 /* The normal sections */
1160 for (i = 0; i < nsects; i++) {
1161 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1162 (sects[i]->type == SHT_PROGBITS ?
1163 sects[i]->data : NULL), true,
1164 sects[i]->len, 0, 0, sects[i]->align, 0);
1165 p += strlen(p) + 1;
1168 /* .shstrtab */
1169 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1170 shstrtablen, 0, 0, 1, 0);
1171 p += strlen(p) + 1;
1173 /* .symtab */
1174 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1175 symtablen, sec_strtab, symtablocal, 4, 24);
1176 p += strlen(p) + 1;
1178 /* .strtab */
1179 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1180 strslen, 0, 0, 1, 0);
1181 p += strlen(p) + 1;
1183 /* The relocation sections */
1184 for (i = 0; i < nsects; i++)
1185 if (sects[i]->head) {
1186 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1187 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1188 p += strlen(p) + 1;
1191 if (of_elf64.current_dfmt == &df_stabs) {
1192 /* for debugging information, create the last three sections
1193 which are the .stab , .stabstr and .rel.stab sections respectively */
1195 /* this function call creates the stab sections in memory */
1196 stabs64_generate();
1198 if (stabbuf && stabstrbuf && stabrelbuf) {
1199 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1200 stablen, sec_stabstr, 0, 4, 12);
1201 p += strlen(p) + 1;
1203 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1204 stabstrlen, 0, 0, 4, 0);
1205 p += strlen(p) + 1;
1207 /* link -> symtable info -> section to refer to */
1208 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1209 stabrellen, symtabsection, sec_stab, 4, 16);
1210 p += strlen(p) + 1;
1213 else if (of_elf64.current_dfmt == &df_dwarf) {
1214 /* for dwarf debugging information, create the ten dwarf sections */
1216 /* this function call creates the dwarf sections in memory */
1217 if (dwarf_fsect)
1218 dwarf64_generate();
1220 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1221 arangeslen, 0, 0, 1, 0);
1222 p += strlen(p) + 1;
1224 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1225 arangesrellen, symtabsection, debug_aranges, 1, 24);
1226 p += strlen(p) + 1;
1228 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1229 pubnameslen, 0, 0, 1, 0);
1230 p += strlen(p) + 1;
1232 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1233 infolen, 0, 0, 1, 0);
1234 p += strlen(p) + 1;
1236 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1237 inforellen, symtabsection, debug_info, 1, 24);
1238 p += strlen(p) + 1;
1240 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1241 abbrevlen, 0, 0, 1, 0);
1242 p += strlen(p) + 1;
1244 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1245 linelen, 0, 0, 1, 0);
1246 p += strlen(p) + 1;
1248 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1249 linerellen, symtabsection, debug_line, 1, 24);
1250 p += strlen(p) + 1;
1252 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1253 framelen, 0, 0, 8, 0);
1254 p += strlen(p) + 1;
1256 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1257 loclen, 0, 0, 1, 0);
1258 p += strlen(p) + 1;
1260 fwritezero(align, elffp);
1263 * Now output the sections.
1265 elf_write_sections();
1267 nasm_free(elf_sects);
1268 saa_free(symtab);
1271 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1273 struct SAA *s = saa_init(1L);
1274 struct Symbol *sym;
1275 uint8_t entry[24], *p;
1276 int i;
1278 *len = *local = 0;
1281 * First, an all-zeros entry, required by the ELF spec.
1283 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1284 *len += 24;
1285 (*local)++;
1288 * Next, an entry for the file name.
1290 p = entry;
1291 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1292 WRITESHORT(p, STT_FILE); /* type FILE */
1293 WRITESHORT(p, SHN_ABS);
1294 WRITEDLONG(p, (uint64_t) 0); /* no value */
1295 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1296 saa_wbytes(s, entry, 24L);
1297 *len += 24;
1298 (*local)++;
1301 * Now some standard symbols defining the segments, for relocation
1302 * purposes.
1304 for (i = 1; i <= nsects; i++) {
1305 p = entry;
1306 WRITELONG(p, 0); /* no symbol name */
1307 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1308 WRITESHORT(p, i); /* section id */
1309 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1310 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1311 saa_wbytes(s, entry, 24L);
1312 *len += 24;
1313 (*local)++;
1318 * Now the other local symbols.
1320 saa_rewind(syms);
1321 while ((sym = saa_rstruct(syms))) {
1322 if (sym->type & SYM_GLOBAL)
1323 continue;
1324 p = entry;
1325 WRITELONG(p, sym->strpos); /* index into symbol string table */
1326 WRITECHAR(p, sym->type); /* type and binding */
1327 WRITECHAR(p, sym->other); /* visibility */
1328 WRITESHORT(p, sym->section); /* index into section header table */
1329 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1330 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1331 saa_wbytes(s, entry, 24L);
1332 *len += 24;
1333 (*local)++;
1336 * dwarf needs symbols for debug sections
1337 * which are relocation targets.
1339 if (of_elf64.current_dfmt == &df_dwarf) {
1340 dwarf_infosym = *local;
1341 p = entry;
1342 WRITELONG(p, 0); /* no symbol name */
1343 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1344 WRITESHORT(p, debug_info); /* section id */
1345 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1346 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1347 saa_wbytes(s, entry, 24L);
1348 *len += 24;
1349 (*local)++;
1350 dwarf_abbrevsym = *local;
1351 p = entry;
1352 WRITELONG(p, 0); /* no symbol name */
1353 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1354 WRITESHORT(p, debug_abbrev); /* section id */
1355 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1356 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1357 saa_wbytes(s, entry, 24L);
1358 *len += 24;
1359 (*local)++;
1360 dwarf_linesym = *local;
1361 p = entry;
1362 WRITELONG(p, 0); /* no symbol name */
1363 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1364 WRITESHORT(p, debug_line); /* section id */
1365 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1366 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1367 saa_wbytes(s, entry, 24L);
1368 *len += 24;
1369 (*local)++;
1373 * Now the global symbols.
1375 saa_rewind(syms);
1376 while ((sym = saa_rstruct(syms))) {
1377 if (!(sym->type & SYM_GLOBAL))
1378 continue;
1379 p = entry;
1380 WRITELONG(p, sym->strpos);
1381 WRITECHAR(p, sym->type); /* type and binding */
1382 WRITECHAR(p, sym->other); /* visibility */
1383 WRITESHORT(p, sym->section);
1384 WRITEDLONG(p, (int64_t)sym->symv.key);
1385 WRITEDLONG(p, (int64_t)sym->size);
1386 saa_wbytes(s, entry, 24L);
1387 *len += 24;
1390 return s;
1393 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1395 struct SAA *s;
1396 uint8_t *p, entry[24];
1397 int32_t global_offset;
1399 if (!r)
1400 return NULL;
1402 s = saa_init(1L);
1403 *len = 0;
1406 * How to onvert from a global placeholder to a real symbol index;
1407 * the +2 refers to the two special entries, the null entry and
1408 * the filename entry.
1410 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1412 while (r) {
1413 int32_t sym = r->symbol;
1415 if (sym >= GLOBAL_TEMP_BASE)
1416 sym += global_offset;
1418 p = entry;
1419 WRITEDLONG(p, r->address);
1420 WRITELONG(p, r->type);
1421 WRITELONG(p, sym);
1422 WRITEDLONG(p, r->offset);
1423 saa_wbytes(s, entry, 24L);
1424 *len += 24;
1426 r = r->next;
1429 return s;
1432 static void elf_section_header(int name, int type, uint64_t flags,
1433 void *data, bool is_saa, uint64_t datalen,
1434 int link, int info, int align, int eltsize)
1436 elf_sects[elf_nsect].data = data;
1437 elf_sects[elf_nsect].len = datalen;
1438 elf_sects[elf_nsect].is_saa = is_saa;
1439 elf_nsect++;
1441 fwriteint32_t((int32_t)name, elffp);
1442 fwriteint32_t((int32_t)type, elffp);
1443 fwriteint64_t((int64_t)flags, elffp);
1444 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1445 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1446 fwriteint64_t(datalen, elffp);
1447 if (data)
1448 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1449 fwriteint32_t((int32_t)link, elffp);
1450 fwriteint32_t((int32_t)info, elffp);
1451 fwriteint64_t((int64_t)align, elffp);
1452 fwriteint64_t((int64_t)eltsize, elffp);
1455 static void elf_write_sections(void)
1457 int i;
1458 for (i = 0; i < elf_nsect; i++)
1459 if (elf_sects[i].data) {
1460 int32_t len = elf_sects[i].len;
1461 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1462 int32_t align = reallen - len;
1463 if (elf_sects[i].is_saa)
1464 saa_fpwrite(elf_sects[i].data, elffp);
1465 else
1466 fwrite(elf_sects[i].data, len, 1, elffp);
1467 fwritezero(align, elffp);
1471 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1473 saa_wbytes(sect->data, data, len);
1474 sect->len += len;
1476 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1478 saa_writeaddr(sect->data, data, len);
1479 sect->len += len;
1482 static int32_t elf_segbase(int32_t segment)
1484 return segment;
1487 static int elf_directive(char *directive, char *value, int pass)
1489 bool err;
1490 int64_t n;
1491 char *p;
1493 if (!strcmp(directive, "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;
1522 return 0;
1525 static void elf_filename(char *inname, char *outname, efunc error)
1527 strcpy(elf_module, inname);
1528 standard_extension(inname, outname, ".o", error);
1531 extern macros_t elf_stdmac[];
1533 static int elf_set_info(enum geninfo type, char **val)
1535 (void)type;
1536 (void)val;
1537 return 0;
1539 static struct dfmt df_dwarf = {
1540 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1541 "dwarf",
1542 dwarf64_init,
1543 dwarf64_linenum,
1544 debug64_deflabel,
1545 debug64_directive,
1546 debug64_typevalue,
1547 dwarf64_output,
1548 dwarf64_cleanup
1550 static struct dfmt df_stabs = {
1551 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1552 "stabs",
1553 null_debug_init,
1554 stabs64_linenum,
1555 debug64_deflabel,
1556 debug64_directive,
1557 debug64_typevalue,
1558 stabs64_output,
1559 stabs64_cleanup
1562 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1564 struct ofmt of_elf64 = {
1565 "ELF64 (x86_64) object files (e.g. Linux)",
1566 "elf64",
1568 elf64_debugs_arr,
1569 &df_stabs,
1570 elf_stdmac,
1571 elf_init,
1572 elf_set_info,
1573 elf_out,
1574 elf_deflabel,
1575 elf_section_names,
1576 elf_segbase,
1577 elf_directive,
1578 elf_filename,
1579 elf_cleanup
1582 /* common debugging routines */
1583 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1584 int is_global, char *special)
1586 (void)name;
1587 (void)segment;
1588 (void)offset;
1589 (void)is_global;
1590 (void)special;
1593 static void debug64_directive(const char *directive, const char *params)
1595 (void)directive;
1596 (void)params;
1599 static void debug64_typevalue(int32_t type)
1601 int32_t stype, ssize;
1602 switch (TYM_TYPE(type)) {
1603 case TY_LABEL:
1604 ssize = 0;
1605 stype = STT_NOTYPE;
1606 break;
1607 case TY_BYTE:
1608 ssize = 1;
1609 stype = STT_OBJECT;
1610 break;
1611 case TY_WORD:
1612 ssize = 2;
1613 stype = STT_OBJECT;
1614 break;
1615 case TY_DWORD:
1616 ssize = 4;
1617 stype = STT_OBJECT;
1618 break;
1619 case TY_FLOAT:
1620 ssize = 4;
1621 stype = STT_OBJECT;
1622 break;
1623 case TY_QWORD:
1624 ssize = 8;
1625 stype = STT_OBJECT;
1626 break;
1627 case TY_TBYTE:
1628 ssize = 10;
1629 stype = STT_OBJECT;
1630 break;
1631 case TY_OWORD:
1632 ssize = 16;
1633 stype = STT_OBJECT;
1634 break;
1635 case TY_YWORD:
1636 ssize = 32;
1637 stype = STT_OBJECT;
1638 break;
1639 case TY_COMMON:
1640 ssize = 0;
1641 stype = STT_COMMON;
1642 break;
1643 case TY_SEG:
1644 ssize = 0;
1645 stype = STT_SECTION;
1646 break;
1647 case TY_EXTERN:
1648 ssize = 0;
1649 stype = STT_NOTYPE;
1650 break;
1651 case TY_EQU:
1652 ssize = 0;
1653 stype = STT_NOTYPE;
1654 break;
1655 default:
1656 ssize = 0;
1657 stype = STT_NOTYPE;
1658 break;
1660 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1661 lastsym->size = ssize;
1662 lastsym->type = stype;
1666 /* stabs debugging routines */
1668 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1670 (void)segto;
1671 if (!stabs_filename) {
1672 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1673 strcpy(stabs_filename, filename);
1674 } else {
1675 if (strcmp(stabs_filename, filename)) {
1676 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1677 in fact, this leak comes in quite handy to maintain a list of files
1678 encountered so far in the symbol lines... */
1680 /* why not nasm_free(stabs_filename); we're done with the old one */
1682 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1683 strcpy(stabs_filename, filename);
1686 debug_immcall = 1;
1687 currentline = linenumber;
1691 static void stabs64_output(int type, void *param)
1693 struct symlininfo *s;
1694 struct linelist *el;
1695 if (type == TY_DEBUGSYMLIN) {
1696 if (debug_immcall) {
1697 s = (struct symlininfo *)param;
1698 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1699 return; /* line info is only collected for executable sections */
1700 numlinestabs++;
1701 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1702 el->info.offset = s->offset;
1703 el->info.section = s->section;
1704 el->info.name = s->name;
1705 el->line = currentline;
1706 el->filename = stabs_filename;
1707 el->next = 0;
1708 if (stabslines) {
1709 stabslines->last->next = el;
1710 stabslines->last = el;
1711 } else {
1712 stabslines = el;
1713 stabslines->last = el;
1717 debug_immcall = 0;
1720 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1721 do {\
1722 WRITELONG(p,n_strx); \
1723 WRITECHAR(p,n_type); \
1724 WRITECHAR(p,n_other); \
1725 WRITESHORT(p,n_desc); \
1726 WRITELONG(p,n_value); \
1727 } while (0)
1729 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1731 static void stabs64_generate(void)
1733 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1734 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1735 char **allfiles;
1736 int *fileidx;
1738 struct linelist *ptr;
1740 ptr = stabslines;
1742 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1743 for (i = 0; i < numlinestabs; i++)
1744 allfiles[i] = 0;
1745 numfiles = 0;
1746 while (ptr) {
1747 if (numfiles == 0) {
1748 allfiles[0] = ptr->filename;
1749 numfiles++;
1750 } else {
1751 for (i = 0; i < numfiles; i++) {
1752 if (!strcmp(allfiles[i], ptr->filename))
1753 break;
1755 if (i >= numfiles) {
1756 allfiles[i] = ptr->filename;
1757 numfiles++;
1760 ptr = ptr->next;
1762 strsize = 1;
1763 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1764 for (i = 0; i < numfiles; i++) {
1765 fileidx[i] = strsize;
1766 strsize += strlen(allfiles[i]) + 1;
1768 mainfileindex = 0;
1769 for (i = 0; i < numfiles; i++) {
1770 if (!strcmp(allfiles[i], elf_module)) {
1771 mainfileindex = i;
1772 break;
1776 /* worst case size of the stab buffer would be:
1777 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1779 sbuf =
1780 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1781 sizeof(struct stabentry));
1783 ssbuf = (uint8_t *)nasm_malloc(strsize);
1785 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1786 rptr = rbuf;
1788 for (i = 0; i < numfiles; i++) {
1789 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1791 ssbuf[0] = 0;
1793 stabstrlen = strsize; /* set global variable for length of stab strings */
1795 sptr = sbuf;
1796 ptr = stabslines;
1797 numstabs = 0;
1799 if (ptr) {
1800 /* this is the first stab, its strx points to the filename of the
1801 the source-file, the n_desc field should be set to the number
1802 of remaining stabs
1804 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1806 /* this is the stab for the main source file */
1807 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1809 /* relocation table entry */
1811 /* Since the symbol table has two entries before */
1812 /* the section symbols, the index in the info.section */
1813 /* member must be adjusted by adding 2 */
1815 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1816 WRITELONG(rptr, R_X86_64_32);
1817 WRITELONG(rptr, ptr->info.section + 2);
1819 numstabs++;
1820 currfile = mainfileindex;
1823 while (ptr) {
1824 if (strcmp(allfiles[currfile], ptr->filename)) {
1825 /* oops file has changed... */
1826 for (i = 0; i < numfiles; i++)
1827 if (!strcmp(allfiles[i], ptr->filename))
1828 break;
1829 currfile = i;
1830 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1831 ptr->info.offset);
1832 numstabs++;
1834 /* relocation table entry */
1836 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1837 WRITELONG(rptr, R_X86_64_32);
1838 WRITELONG(rptr, ptr->info.section + 2);
1841 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1842 numstabs++;
1844 /* relocation table entry */
1846 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1847 WRITELONG(rptr, R_X86_64_32);
1848 WRITELONG(rptr, ptr->info.section + 2);
1850 ptr = ptr->next;
1854 ((struct stabentry *)sbuf)->n_desc = numstabs;
1856 nasm_free(allfiles);
1857 nasm_free(fileidx);
1859 stablen = (sptr - sbuf);
1860 stabrellen = (rptr - rbuf);
1861 stabrelbuf = rbuf;
1862 stabbuf = sbuf;
1863 stabstrbuf = ssbuf;
1866 static void stabs64_cleanup(void)
1868 struct linelist *ptr, *del;
1869 if (!stabslines)
1870 return;
1871 ptr = stabslines;
1872 while (ptr) {
1873 del = ptr;
1874 ptr = ptr->next;
1875 nasm_free(del);
1877 if (stabbuf)
1878 nasm_free(stabbuf);
1879 if (stabrelbuf)
1880 nasm_free(stabrelbuf);
1881 if (stabstrbuf)
1882 nasm_free(stabstrbuf);
1884 /* dwarf routines */
1885 static void dwarf64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1887 (void)of;
1888 (void)id;
1889 (void)fp;
1890 (void)error;
1892 ndebugs = 3; /* 3 debug symbols */
1895 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1896 int32_t segto)
1898 (void)segto;
1899 dwarf64_findfile(filename);
1900 debug_immcall = 1;
1901 currentline = linenumber;
1904 /* called from elf_out with type == TY_DEBUGSYMLIN */
1905 static void dwarf64_output(int type, void *param)
1907 int ln, aa, inx, maxln, soc;
1908 struct symlininfo *s;
1909 struct SAA *plinep;
1911 (void)type;
1913 s = (struct symlininfo *)param;
1914 /* line number info is only gathered for executable sections */
1915 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1916 return;
1917 /* Check if section index has changed */
1918 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1920 dwarf64_findsect(s->section);
1922 /* do nothing unless line or file has changed */
1923 if (debug_immcall)
1925 ln = currentline - dwarf_csect->line;
1926 aa = s->offset - dwarf_csect->offset;
1927 inx = dwarf_clist->line;
1928 plinep = dwarf_csect->psaa;
1929 /* check for file change */
1930 if (!(inx == dwarf_csect->file))
1932 saa_write8(plinep,DW_LNS_set_file);
1933 saa_write8(plinep,inx);
1934 dwarf_csect->file = inx;
1936 /* check for line change */
1937 if (ln)
1939 /* test if in range of special op code */
1940 maxln = line_base + line_range;
1941 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1942 if (ln >= line_base && ln < maxln && soc < 256)
1944 saa_write8(plinep,soc);
1946 else
1948 if (ln)
1950 saa_write8(plinep,DW_LNS_advance_line);
1951 saa_wleb128s(plinep,ln);
1953 if (aa)
1955 saa_write8(plinep,DW_LNS_advance_pc);
1956 saa_wleb128u(plinep,aa);
1959 dwarf_csect->line = currentline;
1960 dwarf_csect->offset = s->offset;
1962 /* show change handled */
1963 debug_immcall = 0;
1968 static void dwarf64_generate(void)
1970 uint8_t *pbuf;
1971 int indx;
1972 struct linelist *ftentry;
1973 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1974 struct SAA *parangesrel, *plinesrel, *pinforel;
1975 struct sectlist *psect;
1976 size_t saalen, linepoff, totlen, highaddr;
1978 /* write epilogues for each line program range */
1979 /* and build aranges section */
1980 paranges = saa_init(1L);
1981 parangesrel = saa_init(1L);
1982 saa_write16(paranges,3); /* dwarf version */
1983 saa_write64(parangesrel, paranges->datalen+4);
1984 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1985 saa_write64(parangesrel, 0);
1986 saa_write32(paranges,0); /* offset into info */
1987 saa_write8(paranges,8); /* pointer size */
1988 saa_write8(paranges,0); /* not segmented */
1989 saa_write32(paranges,0); /* padding */
1990 /* iterate though sectlist entries */
1991 psect = dwarf_fsect;
1992 totlen = 0;
1993 highaddr = 0;
1994 for (indx = 0; indx < dwarf_nsections; indx++)
1996 plinep = psect->psaa;
1997 /* Line Number Program Epilogue */
1998 saa_write8(plinep,2); /* std op 2 */
1999 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2000 saa_write8(plinep,DW_LNS_extended_op);
2001 saa_write8(plinep,1); /* operand length */
2002 saa_write8(plinep,DW_LNE_end_sequence);
2003 totlen += plinep->datalen;
2004 /* range table relocation entry */
2005 saa_write64(parangesrel, paranges->datalen + 4);
2006 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2007 saa_write64(parangesrel, (uint64_t) 0);
2008 /* range table entry */
2009 saa_write64(paranges,0x0000); /* range start */
2010 saa_write64(paranges,sects[psect->section]->len); /* range length */
2011 highaddr += sects[psect->section]->len;
2012 /* done with this entry */
2013 psect = psect->next;
2015 saa_write64(paranges,0); /* null address */
2016 saa_write64(paranges,0); /* null length */
2017 saalen = paranges->datalen;
2018 arangeslen = saalen + 4;
2019 arangesbuf = pbuf = nasm_malloc(arangeslen);
2020 WRITELONG(pbuf,saalen); /* initial length */
2021 saa_rnbytes(paranges, pbuf, saalen);
2022 saa_free(paranges);
2024 /* build rela.aranges section */
2025 arangesrellen = saalen = parangesrel->datalen;
2026 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2027 saa_rnbytes(parangesrel, pbuf, saalen);
2028 saa_free(parangesrel);
2030 /* build pubnames section */
2031 ppubnames = saa_init(1L);
2032 saa_write16(ppubnames,3); /* dwarf version */
2033 saa_write32(ppubnames,0); /* offset into info */
2034 saa_write32(ppubnames,0); /* space used in info */
2035 saa_write32(ppubnames,0); /* end of list */
2036 saalen = ppubnames->datalen;
2037 pubnameslen = saalen + 4;
2038 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2039 WRITELONG(pbuf,saalen); /* initial length */
2040 saa_rnbytes(ppubnames, pbuf, saalen);
2041 saa_free(ppubnames);
2043 /* build info section */
2044 pinfo = saa_init(1L);
2045 pinforel = saa_init(1L);
2046 saa_write16(pinfo,3); /* dwarf version */
2047 saa_write64(pinforel, pinfo->datalen + 4);
2048 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2049 saa_write64(pinforel, 0);
2050 saa_write32(pinfo,0); /* offset into abbrev */
2051 saa_write8(pinfo,8); /* pointer size */
2052 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2053 saa_write64(pinforel, pinfo->datalen + 4);
2054 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2055 saa_write64(pinforel, 0);
2056 saa_write64(pinfo,0); /* DW_AT_low_pc */
2057 saa_write64(pinforel, pinfo->datalen + 4);
2058 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2059 saa_write64(pinforel, 0);
2060 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2061 saa_write64(pinforel, pinfo->datalen + 4);
2062 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2063 saa_write64(pinforel, 0);
2064 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2065 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2066 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2067 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2068 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2069 saa_write64(pinforel, pinfo->datalen + 4);
2070 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2071 saa_write64(pinforel, 0);
2072 saa_write64(pinfo,0); /* DW_AT_low_pc */
2073 saa_write64(pinfo,0); /* DW_AT_frame_base */
2074 saa_write8(pinfo,0); /* end of entries */
2075 saalen = pinfo->datalen;
2076 infolen = saalen + 4;
2077 infobuf = pbuf = nasm_malloc(infolen);
2078 WRITELONG(pbuf,saalen); /* initial length */
2079 saa_rnbytes(pinfo, pbuf, saalen);
2080 saa_free(pinfo);
2082 /* build rela.info section */
2083 inforellen = saalen = pinforel->datalen;
2084 inforelbuf = pbuf = nasm_malloc(inforellen);
2085 saa_rnbytes(pinforel, pbuf, saalen);
2086 saa_free(pinforel);
2088 /* build abbrev section */
2089 pabbrev = saa_init(1L);
2090 saa_write8(pabbrev,1); /* entry number LEB128u */
2091 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2092 saa_write8(pabbrev,1); /* has children */
2093 /* the following attributes and forms are all LEB128u values */
2094 saa_write8(pabbrev,DW_AT_low_pc);
2095 saa_write8(pabbrev,DW_FORM_addr);
2096 saa_write8(pabbrev,DW_AT_high_pc);
2097 saa_write8(pabbrev,DW_FORM_addr);
2098 saa_write8(pabbrev,DW_AT_stmt_list);
2099 saa_write8(pabbrev,DW_FORM_data4);
2100 saa_write8(pabbrev,DW_AT_name);
2101 saa_write8(pabbrev,DW_FORM_string);
2102 saa_write8(pabbrev,DW_AT_producer);
2103 saa_write8(pabbrev,DW_FORM_string);
2104 saa_write8(pabbrev,DW_AT_language);
2105 saa_write8(pabbrev,DW_FORM_data2);
2106 saa_write16(pabbrev,0); /* end of entry */
2107 /* LEB128u usage same as above */
2108 saa_write8(pabbrev,2); /* entry number */
2109 saa_write8(pabbrev,DW_TAG_subprogram);
2110 saa_write8(pabbrev,0); /* no children */
2111 saa_write8(pabbrev,DW_AT_low_pc);
2112 saa_write8(pabbrev,DW_FORM_addr);
2113 saa_write8(pabbrev,DW_AT_frame_base);
2114 saa_write8(pabbrev,DW_FORM_data4);
2115 saa_write16(pabbrev,0); /* end of entry */
2116 abbrevlen = saalen = pabbrev->datalen;
2117 abbrevbuf = pbuf = nasm_malloc(saalen);
2118 saa_rnbytes(pabbrev, pbuf, saalen);
2119 saa_free(pabbrev);
2121 /* build line section */
2122 /* prolog */
2123 plines = saa_init(1L);
2124 saa_write8(plines,1); /* Minimum Instruction Length */
2125 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2126 saa_write8(plines,line_base); /* Line Base */
2127 saa_write8(plines,line_range); /* Line Range */
2128 saa_write8(plines,opcode_base); /* Opcode Base */
2129 /* standard opcode lengths (# of LEB128u operands) */
2130 saa_write8(plines,0); /* Std opcode 1 length */
2131 saa_write8(plines,1); /* Std opcode 2 length */
2132 saa_write8(plines,1); /* Std opcode 3 length */
2133 saa_write8(plines,1); /* Std opcode 4 length */
2134 saa_write8(plines,1); /* Std opcode 5 length */
2135 saa_write8(plines,0); /* Std opcode 6 length */
2136 saa_write8(plines,0); /* Std opcode 7 length */
2137 saa_write8(plines,0); /* Std opcode 8 length */
2138 saa_write8(plines,1); /* Std opcode 9 length */
2139 saa_write8(plines,0); /* Std opcode 10 length */
2140 saa_write8(plines,0); /* Std opcode 11 length */
2141 saa_write8(plines,1); /* Std opcode 12 length */
2142 /* Directory Table */
2143 saa_write8(plines,0); /* End of table */
2144 /* File Name Table */
2145 ftentry = dwarf_flist;
2146 for (indx = 0;indx<dwarf_numfiles;indx++)
2148 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2149 saa_write8(plines,0); /* directory LEB128u */
2150 saa_write8(plines,0); /* time LEB128u */
2151 saa_write8(plines,0); /* size LEB128u */
2152 ftentry = ftentry->next;
2154 saa_write8(plines,0); /* End of table */
2155 linepoff = plines->datalen;
2156 linelen = linepoff + totlen + 10;
2157 linebuf = pbuf = nasm_malloc(linelen);
2158 WRITELONG(pbuf,linelen-4); /* initial length */
2159 WRITESHORT(pbuf,3); /* dwarf version */
2160 WRITELONG(pbuf,linepoff); /* offset to line number program */
2161 /* write line header */
2162 saalen = linepoff;
2163 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2164 pbuf += linepoff;
2165 saa_free(plines);
2166 /* concatonate line program ranges */
2167 linepoff += 13;
2168 plinesrel = saa_init(1L);
2169 psect = dwarf_fsect;
2170 for (indx = 0; indx < dwarf_nsections; indx++)
2172 saa_write64(plinesrel, linepoff);
2173 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2174 saa_write64(plinesrel, (uint64_t) 0);
2175 plinep = psect->psaa;
2176 saalen = plinep->datalen;
2177 saa_rnbytes(plinep, pbuf, saalen);
2178 pbuf += saalen;
2179 linepoff += saalen;
2180 saa_free(plinep);
2181 /* done with this entry */
2182 psect = psect->next;
2186 /* build rela.lines section */
2187 linerellen =saalen = plinesrel->datalen;
2188 linerelbuf = pbuf = nasm_malloc(linerellen);
2189 saa_rnbytes(plinesrel, pbuf, saalen);
2190 saa_free(plinesrel);
2192 /* build frame section */
2193 framelen = 4;
2194 framebuf = pbuf = nasm_malloc(framelen);
2195 WRITELONG(pbuf,framelen-4); /* initial length */
2197 /* build loc section */
2198 loclen = 16;
2199 locbuf = pbuf = nasm_malloc(loclen);
2200 WRITEDLONG(pbuf,0); /* null beginning offset */
2201 WRITEDLONG(pbuf,0); /* null ending offset */
2204 static void dwarf64_cleanup(void)
2206 if (arangesbuf)
2207 nasm_free(arangesbuf);
2208 if (arangesrelbuf)
2209 nasm_free(arangesrelbuf);
2210 if (pubnamesbuf)
2211 nasm_free(pubnamesbuf);
2212 if (infobuf)
2213 nasm_free(infobuf);
2214 if (inforelbuf)
2215 nasm_free(inforelbuf);
2216 if (abbrevbuf)
2217 nasm_free(abbrevbuf);
2218 if (linebuf)
2219 nasm_free(linebuf);
2220 if (linerelbuf)
2221 nasm_free(linerelbuf);
2222 if (framebuf)
2223 nasm_free(framebuf);
2224 if (locbuf)
2225 nasm_free(locbuf);
2227 static void dwarf64_findfile(const char * fname)
2229 int finx;
2230 struct linelist *match;
2232 /* return if fname is current file name */
2233 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2234 /* search for match */
2235 else
2237 match = 0;
2238 if (dwarf_flist)
2240 match = dwarf_flist;
2241 for (finx = 0; finx < dwarf_numfiles; finx++)
2243 if (!(strcmp(fname, match->filename)))
2245 dwarf_clist = match;
2246 return;
2250 /* add file name to end of list */
2251 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2252 dwarf_numfiles++;
2253 dwarf_clist->line = dwarf_numfiles;
2254 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2255 strcpy(dwarf_clist->filename,fname);
2256 dwarf_clist->next = 0;
2257 /* if first entry */
2258 if (!dwarf_flist)
2260 dwarf_flist = dwarf_elist = dwarf_clist;
2261 dwarf_clist->last = 0;
2263 /* chain to previous entry */
2264 else
2266 dwarf_elist->next = dwarf_clist;
2267 dwarf_elist = dwarf_clist;
2271 /* */
2272 static void dwarf64_findsect(const int index)
2274 int sinx;
2275 struct sectlist *match;
2276 struct SAA *plinep;
2277 /* return if index is current section index */
2278 if (dwarf_csect && (dwarf_csect->section == index))
2280 return;
2282 /* search for match */
2283 else
2285 match = 0;
2286 if (dwarf_fsect)
2288 match = dwarf_fsect;
2289 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2291 if ((match->section == index))
2293 dwarf_csect = match;
2294 return;
2296 match = match->next;
2299 /* add entry to end of list */
2300 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2301 dwarf_nsections++;
2302 dwarf_csect->psaa = plinep = saa_init(1L);
2303 dwarf_csect->line = 1;
2304 dwarf_csect->offset = 0;
2305 dwarf_csect->file = 1;
2306 dwarf_csect->section = index;
2307 dwarf_csect->next = 0;
2308 /* set relocatable address at start of line program */
2309 saa_write8(plinep,DW_LNS_extended_op);
2310 saa_write8(plinep,9); /* operand length */
2311 saa_write8(plinep,DW_LNE_set_address);
2312 saa_write64(plinep,0); /* Start Address */
2313 /* if first entry */
2314 if (!dwarf_fsect)
2316 dwarf_fsect = dwarf_esect = dwarf_csect;
2317 dwarf_csect->last = 0;
2319 /* chain to previous entry */
2320 else
2322 dwarf_esect->next = dwarf_csect;
2323 dwarf_esect = dwarf_csect;
2328 #endif /* OF_ELF */