Add new copyright headers to the rdoff code
[nasm/avx512.git] / output / outelf64.c
blob9834cdc71e1ad46f4e0f51e636ac75ac372a78ef
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_SYMTAB, 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",
1567 NULL,
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_COMMON:
1636 ssize = 0;
1637 stype = STT_COMMON;
1638 break;
1639 case TY_SEG:
1640 ssize = 0;
1641 stype = STT_SECTION;
1642 break;
1643 case TY_EXTERN:
1644 ssize = 0;
1645 stype = STT_NOTYPE;
1646 break;
1647 case TY_EQU:
1648 ssize = 0;
1649 stype = STT_NOTYPE;
1650 break;
1651 default:
1652 ssize = 0;
1653 stype = STT_NOTYPE;
1654 break;
1656 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1657 lastsym->size = ssize;
1658 lastsym->type = stype;
1662 /* stabs debugging routines */
1664 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1666 (void)segto;
1667 if (!stabs_filename) {
1668 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1669 strcpy(stabs_filename, filename);
1670 } else {
1671 if (strcmp(stabs_filename, filename)) {
1672 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1673 in fact, this leak comes in quite handy to maintain a list of files
1674 encountered so far in the symbol lines... */
1676 /* why not nasm_free(stabs_filename); we're done with the old one */
1678 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1679 strcpy(stabs_filename, filename);
1682 debug_immcall = 1;
1683 currentline = linenumber;
1687 static void stabs64_output(int type, void *param)
1689 struct symlininfo *s;
1690 struct linelist *el;
1691 if (type == TY_DEBUGSYMLIN) {
1692 if (debug_immcall) {
1693 s = (struct symlininfo *)param;
1694 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1695 return; /* line info is only collected for executable sections */
1696 numlinestabs++;
1697 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1698 el->info.offset = s->offset;
1699 el->info.section = s->section;
1700 el->info.name = s->name;
1701 el->line = currentline;
1702 el->filename = stabs_filename;
1703 el->next = 0;
1704 if (stabslines) {
1705 stabslines->last->next = el;
1706 stabslines->last = el;
1707 } else {
1708 stabslines = el;
1709 stabslines->last = el;
1713 debug_immcall = 0;
1716 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1717 do {\
1718 WRITELONG(p,n_strx); \
1719 WRITECHAR(p,n_type); \
1720 WRITECHAR(p,n_other); \
1721 WRITESHORT(p,n_desc); \
1722 WRITELONG(p,n_value); \
1723 } while (0)
1725 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1727 static void stabs64_generate(void)
1729 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1730 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1731 char **allfiles;
1732 int *fileidx;
1734 struct linelist *ptr;
1736 ptr = stabslines;
1738 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1739 for (i = 0; i < numlinestabs; i++)
1740 allfiles[i] = 0;
1741 numfiles = 0;
1742 while (ptr) {
1743 if (numfiles == 0) {
1744 allfiles[0] = ptr->filename;
1745 numfiles++;
1746 } else {
1747 for (i = 0; i < numfiles; i++) {
1748 if (!strcmp(allfiles[i], ptr->filename))
1749 break;
1751 if (i >= numfiles) {
1752 allfiles[i] = ptr->filename;
1753 numfiles++;
1756 ptr = ptr->next;
1758 strsize = 1;
1759 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1760 for (i = 0; i < numfiles; i++) {
1761 fileidx[i] = strsize;
1762 strsize += strlen(allfiles[i]) + 1;
1764 mainfileindex = 0;
1765 for (i = 0; i < numfiles; i++) {
1766 if (!strcmp(allfiles[i], elf_module)) {
1767 mainfileindex = i;
1768 break;
1772 /* worst case size of the stab buffer would be:
1773 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1775 sbuf =
1776 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1777 sizeof(struct stabentry));
1779 ssbuf = (uint8_t *)nasm_malloc(strsize);
1781 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1782 rptr = rbuf;
1784 for (i = 0; i < numfiles; i++) {
1785 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1787 ssbuf[0] = 0;
1789 stabstrlen = strsize; /* set global variable for length of stab strings */
1791 sptr = sbuf;
1792 ptr = stabslines;
1793 numstabs = 0;
1795 if (ptr) {
1796 /* this is the first stab, its strx points to the filename of the
1797 the source-file, the n_desc field should be set to the number
1798 of remaining stabs
1800 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1802 /* this is the stab for the main source file */
1803 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1805 /* relocation table entry */
1807 /* Since the symbol table has two entries before */
1808 /* the section symbols, the index in the info.section */
1809 /* member must be adjusted by adding 2 */
1811 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1812 WRITELONG(rptr, R_X86_64_32);
1813 WRITELONG(rptr, ptr->info.section + 2);
1815 numstabs++;
1816 currfile = mainfileindex;
1819 while (ptr) {
1820 if (strcmp(allfiles[currfile], ptr->filename)) {
1821 /* oops file has changed... */
1822 for (i = 0; i < numfiles; i++)
1823 if (!strcmp(allfiles[i], ptr->filename))
1824 break;
1825 currfile = i;
1826 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1827 ptr->info.offset);
1828 numstabs++;
1830 /* relocation table entry */
1832 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1833 WRITELONG(rptr, R_X86_64_32);
1834 WRITELONG(rptr, ptr->info.section + 2);
1837 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1838 numstabs++;
1840 /* relocation table entry */
1842 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1843 WRITELONG(rptr, R_X86_64_32);
1844 WRITELONG(rptr, ptr->info.section + 2);
1846 ptr = ptr->next;
1850 ((struct stabentry *)sbuf)->n_desc = numstabs;
1852 nasm_free(allfiles);
1853 nasm_free(fileidx);
1855 stablen = (sptr - sbuf);
1856 stabrellen = (rptr - rbuf);
1857 stabrelbuf = rbuf;
1858 stabbuf = sbuf;
1859 stabstrbuf = ssbuf;
1862 static void stabs64_cleanup(void)
1864 struct linelist *ptr, *del;
1865 if (!stabslines)
1866 return;
1867 ptr = stabslines;
1868 while (ptr) {
1869 del = ptr;
1870 ptr = ptr->next;
1871 nasm_free(del);
1873 if (stabbuf)
1874 nasm_free(stabbuf);
1875 if (stabrelbuf)
1876 nasm_free(stabrelbuf);
1877 if (stabstrbuf)
1878 nasm_free(stabstrbuf);
1880 /* dwarf routines */
1881 static void dwarf64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1883 (void)of;
1884 (void)id;
1885 (void)fp;
1886 (void)error;
1888 ndebugs = 3; /* 3 debug symbols */
1891 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1892 int32_t segto)
1894 (void)segto;
1895 dwarf64_findfile(filename);
1896 debug_immcall = 1;
1897 currentline = linenumber;
1900 /* called from elf_out with type == TY_DEBUGSYMLIN */
1901 static void dwarf64_output(int type, void *param)
1903 int ln, aa, inx, maxln, soc;
1904 struct symlininfo *s;
1905 struct SAA *plinep;
1907 (void)type;
1909 s = (struct symlininfo *)param;
1910 /* line number info is only gathered for executable sections */
1911 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1912 return;
1913 /* Check if section index has changed */
1914 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1916 dwarf64_findsect(s->section);
1918 /* do nothing unless line or file has changed */
1919 if (debug_immcall)
1921 ln = currentline - dwarf_csect->line;
1922 aa = s->offset - dwarf_csect->offset;
1923 inx = dwarf_clist->line;
1924 plinep = dwarf_csect->psaa;
1925 /* check for file change */
1926 if (!(inx == dwarf_csect->file))
1928 saa_write8(plinep,DW_LNS_set_file);
1929 saa_write8(plinep,inx);
1930 dwarf_csect->file = inx;
1932 /* check for line change */
1933 if (ln)
1935 /* test if in range of special op code */
1936 maxln = line_base + line_range;
1937 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1938 if (ln >= line_base && ln < maxln && soc < 256)
1940 saa_write8(plinep,soc);
1942 else
1944 if (ln)
1946 saa_write8(plinep,DW_LNS_advance_line);
1947 saa_wleb128s(plinep,ln);
1949 if (aa)
1951 saa_write8(plinep,DW_LNS_advance_pc);
1952 saa_wleb128u(plinep,aa);
1955 dwarf_csect->line = currentline;
1956 dwarf_csect->offset = s->offset;
1958 /* show change handled */
1959 debug_immcall = 0;
1964 static void dwarf64_generate(void)
1966 uint8_t *pbuf;
1967 int indx;
1968 struct linelist *ftentry;
1969 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1970 struct SAA *parangesrel, *plinesrel, *pinforel;
1971 struct sectlist *psect;
1972 size_t saalen, linepoff, totlen, highaddr;
1974 /* write epilogues for each line program range */
1975 /* and build aranges section */
1976 paranges = saa_init(1L);
1977 parangesrel = saa_init(1L);
1978 saa_write16(paranges,3); /* dwarf version */
1979 saa_write64(parangesrel, paranges->datalen+4);
1980 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1981 saa_write64(parangesrel, 0);
1982 saa_write32(paranges,0); /* offset into info */
1983 saa_write8(paranges,8); /* pointer size */
1984 saa_write8(paranges,0); /* not segmented */
1985 saa_write32(paranges,0); /* padding */
1986 /* iterate though sectlist entries */
1987 psect = dwarf_fsect;
1988 totlen = 0;
1989 highaddr = 0;
1990 for (indx = 0; indx < dwarf_nsections; indx++)
1992 plinep = psect->psaa;
1993 /* Line Number Program Epilogue */
1994 saa_write8(plinep,2); /* std op 2 */
1995 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1996 saa_write8(plinep,DW_LNS_extended_op);
1997 saa_write8(plinep,1); /* operand length */
1998 saa_write8(plinep,DW_LNE_end_sequence);
1999 totlen += plinep->datalen;
2000 /* range table relocation entry */
2001 saa_write64(parangesrel, paranges->datalen + 4);
2002 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2003 saa_write64(parangesrel, (uint64_t) 0);
2004 /* range table entry */
2005 saa_write64(paranges,0x0000); /* range start */
2006 saa_write64(paranges,sects[psect->section]->len); /* range length */
2007 highaddr += sects[psect->section]->len;
2008 /* done with this entry */
2009 psect = psect->next;
2011 saa_write64(paranges,0); /* null address */
2012 saa_write64(paranges,0); /* null length */
2013 saalen = paranges->datalen;
2014 arangeslen = saalen + 4;
2015 arangesbuf = pbuf = nasm_malloc(arangeslen);
2016 WRITELONG(pbuf,saalen); /* initial length */
2017 saa_rnbytes(paranges, pbuf, saalen);
2018 saa_free(paranges);
2020 /* build rela.aranges section */
2021 arangesrellen = saalen = parangesrel->datalen;
2022 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2023 saa_rnbytes(parangesrel, pbuf, saalen);
2024 saa_free(parangesrel);
2026 /* build pubnames section */
2027 ppubnames = saa_init(1L);
2028 saa_write16(ppubnames,3); /* dwarf version */
2029 saa_write32(ppubnames,0); /* offset into info */
2030 saa_write32(ppubnames,0); /* space used in info */
2031 saa_write32(ppubnames,0); /* end of list */
2032 saalen = ppubnames->datalen;
2033 pubnameslen = saalen + 4;
2034 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2035 WRITELONG(pbuf,saalen); /* initial length */
2036 saa_rnbytes(ppubnames, pbuf, saalen);
2037 saa_free(ppubnames);
2039 /* build info section */
2040 pinfo = saa_init(1L);
2041 pinforel = saa_init(1L);
2042 saa_write16(pinfo,3); /* dwarf version */
2043 saa_write64(pinforel, pinfo->datalen + 4);
2044 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2045 saa_write64(pinforel, 0);
2046 saa_write32(pinfo,0); /* offset into abbrev */
2047 saa_write8(pinfo,8); /* pointer size */
2048 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2049 saa_write64(pinforel, pinfo->datalen + 4);
2050 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2051 saa_write64(pinforel, 0);
2052 saa_write64(pinfo,0); /* DW_AT_low_pc */
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,highaddr); /* DW_AT_high_pc */
2057 saa_write64(pinforel, pinfo->datalen + 4);
2058 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2059 saa_write64(pinforel, 0);
2060 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2061 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2062 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2063 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2064 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2065 saa_write64(pinforel, pinfo->datalen + 4);
2066 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2067 saa_write64(pinforel, 0);
2068 saa_write64(pinfo,0); /* DW_AT_low_pc */
2069 saa_write64(pinfo,0); /* DW_AT_frame_base */
2070 saa_write8(pinfo,0); /* end of entries */
2071 saalen = pinfo->datalen;
2072 infolen = saalen + 4;
2073 infobuf = pbuf = nasm_malloc(infolen);
2074 WRITELONG(pbuf,saalen); /* initial length */
2075 saa_rnbytes(pinfo, pbuf, saalen);
2076 saa_free(pinfo);
2078 /* build rela.info section */
2079 inforellen = saalen = pinforel->datalen;
2080 inforelbuf = pbuf = nasm_malloc(inforellen);
2081 saa_rnbytes(pinforel, pbuf, saalen);
2082 saa_free(pinforel);
2084 /* build abbrev section */
2085 pabbrev = saa_init(1L);
2086 saa_write8(pabbrev,1); /* entry number LEB128u */
2087 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2088 saa_write8(pabbrev,1); /* has children */
2089 /* the following attributes and forms are all LEB128u values */
2090 saa_write8(pabbrev,DW_AT_low_pc);
2091 saa_write8(pabbrev,DW_FORM_addr);
2092 saa_write8(pabbrev,DW_AT_high_pc);
2093 saa_write8(pabbrev,DW_FORM_addr);
2094 saa_write8(pabbrev,DW_AT_stmt_list);
2095 saa_write8(pabbrev,DW_FORM_data4);
2096 saa_write8(pabbrev,DW_AT_name);
2097 saa_write8(pabbrev,DW_FORM_string);
2098 saa_write8(pabbrev,DW_AT_producer);
2099 saa_write8(pabbrev,DW_FORM_string);
2100 saa_write8(pabbrev,DW_AT_language);
2101 saa_write8(pabbrev,DW_FORM_data2);
2102 saa_write16(pabbrev,0); /* end of entry */
2103 /* LEB128u usage same as above */
2104 saa_write8(pabbrev,2); /* entry number */
2105 saa_write8(pabbrev,DW_TAG_subprogram);
2106 saa_write8(pabbrev,0); /* no children */
2107 saa_write8(pabbrev,DW_AT_low_pc);
2108 saa_write8(pabbrev,DW_FORM_addr);
2109 saa_write8(pabbrev,DW_AT_frame_base);
2110 saa_write8(pabbrev,DW_FORM_data4);
2111 saa_write16(pabbrev,0); /* end of entry */
2112 abbrevlen = saalen = pabbrev->datalen;
2113 abbrevbuf = pbuf = nasm_malloc(saalen);
2114 saa_rnbytes(pabbrev, pbuf, saalen);
2115 saa_free(pabbrev);
2117 /* build line section */
2118 /* prolog */
2119 plines = saa_init(1L);
2120 saa_write8(plines,1); /* Minimum Instruction Length */
2121 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2122 saa_write8(plines,line_base); /* Line Base */
2123 saa_write8(plines,line_range); /* Line Range */
2124 saa_write8(plines,opcode_base); /* Opcode Base */
2125 /* standard opcode lengths (# of LEB128u operands) */
2126 saa_write8(plines,0); /* Std opcode 1 length */
2127 saa_write8(plines,1); /* Std opcode 2 length */
2128 saa_write8(plines,1); /* Std opcode 3 length */
2129 saa_write8(plines,1); /* Std opcode 4 length */
2130 saa_write8(plines,1); /* Std opcode 5 length */
2131 saa_write8(plines,0); /* Std opcode 6 length */
2132 saa_write8(plines,0); /* Std opcode 7 length */
2133 saa_write8(plines,0); /* Std opcode 8 length */
2134 saa_write8(plines,1); /* Std opcode 9 length */
2135 saa_write8(plines,0); /* Std opcode 10 length */
2136 saa_write8(plines,0); /* Std opcode 11 length */
2137 saa_write8(plines,1); /* Std opcode 12 length */
2138 /* Directory Table */
2139 saa_write8(plines,0); /* End of table */
2140 /* File Name Table */
2141 ftentry = dwarf_flist;
2142 for (indx = 0;indx<dwarf_numfiles;indx++)
2144 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2145 saa_write8(plines,0); /* directory LEB128u */
2146 saa_write8(plines,0); /* time LEB128u */
2147 saa_write8(plines,0); /* size LEB128u */
2148 ftentry = ftentry->next;
2150 saa_write8(plines,0); /* End of table */
2151 linepoff = plines->datalen;
2152 linelen = linepoff + totlen + 10;
2153 linebuf = pbuf = nasm_malloc(linelen);
2154 WRITELONG(pbuf,linelen-4); /* initial length */
2155 WRITESHORT(pbuf,3); /* dwarf version */
2156 WRITELONG(pbuf,linepoff); /* offset to line number program */
2157 /* write line header */
2158 saalen = linepoff;
2159 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2160 pbuf += linepoff;
2161 saa_free(plines);
2162 /* concatonate line program ranges */
2163 linepoff += 13;
2164 plinesrel = saa_init(1L);
2165 psect = dwarf_fsect;
2166 for (indx = 0; indx < dwarf_nsections; indx++)
2168 saa_write64(plinesrel, linepoff);
2169 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2170 saa_write64(plinesrel, (uint64_t) 0);
2171 plinep = psect->psaa;
2172 saalen = plinep->datalen;
2173 saa_rnbytes(plinep, pbuf, saalen);
2174 pbuf += saalen;
2175 linepoff += saalen;
2176 saa_free(plinep);
2177 /* done with this entry */
2178 psect = psect->next;
2182 /* build rela.lines section */
2183 linerellen =saalen = plinesrel->datalen;
2184 linerelbuf = pbuf = nasm_malloc(linerellen);
2185 saa_rnbytes(plinesrel, pbuf, saalen);
2186 saa_free(plinesrel);
2188 /* build frame section */
2189 framelen = 4;
2190 framebuf = pbuf = nasm_malloc(framelen);
2191 WRITELONG(pbuf,framelen-4); /* initial length */
2193 /* build loc section */
2194 loclen = 16;
2195 locbuf = pbuf = nasm_malloc(loclen);
2196 WRITEDLONG(pbuf,0); /* null beginning offset */
2197 WRITEDLONG(pbuf,0); /* null ending offset */
2200 static void dwarf64_cleanup(void)
2202 if (arangesbuf)
2203 nasm_free(arangesbuf);
2204 if (arangesrelbuf)
2205 nasm_free(arangesrelbuf);
2206 if (pubnamesbuf)
2207 nasm_free(pubnamesbuf);
2208 if (infobuf)
2209 nasm_free(infobuf);
2210 if (inforelbuf)
2211 nasm_free(inforelbuf);
2212 if (abbrevbuf)
2213 nasm_free(abbrevbuf);
2214 if (linebuf)
2215 nasm_free(linebuf);
2216 if (linerelbuf)
2217 nasm_free(linerelbuf);
2218 if (framebuf)
2219 nasm_free(framebuf);
2220 if (locbuf)
2221 nasm_free(locbuf);
2223 static void dwarf64_findfile(const char * fname)
2225 int finx;
2226 struct linelist *match;
2228 /* return if fname is current file name */
2229 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2230 /* search for match */
2231 else
2233 match = 0;
2234 if (dwarf_flist)
2236 match = dwarf_flist;
2237 for (finx = 0; finx < dwarf_numfiles; finx++)
2239 if (!(strcmp(fname, match->filename)))
2241 dwarf_clist = match;
2242 return;
2246 /* add file name to end of list */
2247 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2248 dwarf_numfiles++;
2249 dwarf_clist->line = dwarf_numfiles;
2250 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2251 strcpy(dwarf_clist->filename,fname);
2252 dwarf_clist->next = 0;
2253 /* if first entry */
2254 if (!dwarf_flist)
2256 dwarf_flist = dwarf_elist = dwarf_clist;
2257 dwarf_clist->last = 0;
2259 /* chain to previous entry */
2260 else
2262 dwarf_elist->next = dwarf_clist;
2263 dwarf_elist = dwarf_clist;
2267 /* */
2268 static void dwarf64_findsect(const int index)
2270 int sinx;
2271 struct sectlist *match;
2272 struct SAA *plinep;
2273 /* return if index is current section index */
2274 if (dwarf_csect && (dwarf_csect->section == index))
2276 return;
2278 /* search for match */
2279 else
2281 match = 0;
2282 if (dwarf_fsect)
2284 match = dwarf_fsect;
2285 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2287 if ((match->section == index))
2289 dwarf_csect = match;
2290 return;
2292 match = match->next;
2295 /* add entry to end of list */
2296 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2297 dwarf_nsections++;
2298 dwarf_csect->psaa = plinep = saa_init(1L);
2299 dwarf_csect->line = 1;
2300 dwarf_csect->offset = 0;
2301 dwarf_csect->file = 1;
2302 dwarf_csect->section = index;
2303 dwarf_csect->next = 0;
2304 /* set relocatable address at start of line program */
2305 saa_write8(plinep,DW_LNS_extended_op);
2306 saa_write8(plinep,9); /* operand length */
2307 saa_write8(plinep,DW_LNE_set_address);
2308 saa_write64(plinep,0); /* Start Address */
2309 /* if first entry */
2310 if (!dwarf_fsect)
2312 dwarf_fsect = dwarf_esect = dwarf_csect;
2313 dwarf_csect->last = 0;
2315 /* chain to previous entry */
2316 else
2318 dwarf_esect->next = dwarf_csect;
2319 dwarf_esect = dwarf_csect;
2324 #endif /* OF_ELF */