NASM 2.08rc1
[nasm/avx512.git] / output / outelf64.c
blobb005693c3045a080b30f333a36f89402a62c884e
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelf64.c output routines for the Netwide Assembler to produce
36 * ELF64 (x86_64 of course) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/elf64.h"
58 #include "output/dwarf.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELF64
63 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
65 struct Reloc {
66 struct Reloc *next;
67 int64_t address; /* relative to _start_ of section */
68 int64_t symbol; /* symbol index */
69 int64_t offset; /* symbol addend */
70 int type; /* type of relocation */
73 struct Symbol {
74 struct rbtree symv; /* symbol value and rbtree of globals */
75 int32_t strpos; /* string table position of name */
76 int32_t section; /* section ID of the symbol */
77 int type; /* symbol type */
78 int other; /* symbol visibility */
79 int32_t size; /* size of symbol */
80 int32_t globnum; /* symbol table offset if global */
81 struct Symbol *nextfwd; /* list of unresolved-size symbols */
82 char *name; /* used temporarily if in above list */
85 struct Section {
86 struct SAA *data;
87 uint64_t len, size;
88 uint32_t nrelocs;
89 int32_t index; /* index into sects array */
90 int type; /* SHT_PROGBITS or SHT_NOBITS */
91 uint64_t align; /* alignment: power of two */
92 uint64_t flags; /* section flags */
93 char *name;
94 struct SAA *rel;
95 uint64_t rellen;
96 struct Reloc *head, **tail;
97 struct rbtree *gsyms; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section **sects;
102 static int nsects, sectlen;
104 #define SHSTR_DELTA 256
105 static char *shstrtab;
106 static int shstrtablen, shstrtabsize;
108 static struct SAA *syms;
109 static uint32_t nlocals, nglobs, ndebugs;
111 static int32_t def_seg;
113 static struct RAA *bsym;
115 static struct SAA *strs;
116 static uint32_t strslen;
118 static struct Symbol *fwds;
120 static char elf_module[FILENAME_MAX];
122 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
123 static uint8_t elf_abiver = 0; /* Current ABI version */
125 extern struct ofmt of_elf64;
127 static struct ELF_SECTDATA {
128 void *data;
129 int64_t len;
130 bool is_saa;
131 } *elf_sects;
132 static int elf_nsect, nsections;
133 static int64_t elf_foffs;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section *, const void *, size_t);
137 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
138 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
139 int, int);
140 static void elf_write_sections(void);
141 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
142 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
143 static void add_sectname(char *, char *);
145 /* type values for stabs debugging sections */
146 #define N_SO 0x64 /* ID for main source file */
147 #define N_SOL 0x84 /* ID for sub-source file */
148 #define N_BINCL 0x82 /* not currently used */
149 #define N_EINCL 0xA2 /* not currently used */
150 #define N_SLINE 0x44
152 struct stabentry {
153 uint32_t n_strx;
154 uint8_t n_type;
155 uint8_t n_other;
156 uint16_t n_desc;
157 uint32_t n_value;
160 struct erel {
161 int offset, info;
164 struct symlininfo {
165 int offset;
166 int section; /* index into sects[] */
167 int segto; /* internal section number */
168 char *name; /* shallow-copied pointer of section name */
171 struct linelist {
172 struct symlininfo info;
173 int line;
174 char *filename;
175 struct linelist *next;
176 struct linelist *last;
179 struct sectlist {
180 struct SAA *psaa;
181 int section;
182 int line;
183 int offset;
184 int file;
185 struct sectlist *next;
186 struct sectlist *last;
189 /* common debug variables */
190 static int currentline = 1;
191 static int debug_immcall = 0;
193 /* stabs debug variables */
194 static struct linelist *stabslines = 0;
195 static int numlinestabs = 0;
196 static char *stabs_filename = 0;
197 static int symtabsection;
198 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
199 static int stablen, stabstrlen, stabrellen;
201 /* dwarf debug variables */
202 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
203 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
204 static int dwarf_numfiles = 0, dwarf_nsections;
205 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
206 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
207 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
208 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
209 abbrevlen, linelen, linerellen, framelen, loclen;
210 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
213 static struct dfmt df_dwarf;
214 static struct dfmt df_stabs;
215 static struct Symbol *lastsym;
217 /* common debugging routines */
218 static void debug64_typevalue(int32_t);
219 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
220 static void debug64_directive(const char *, const char *);
222 /* stabs debugging routines */
223 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
224 static void stabs64_output(int, void *);
225 static void stabs64_generate(void);
226 static void stabs64_cleanup(void);
228 /* dwarf debugging routines */
229 static void dwarf64_init(void);
230 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
231 static void dwarf64_output(int, void *);
232 static void dwarf64_generate(void);
233 static void dwarf64_cleanup(void);
234 static void dwarf64_findfile(const char *);
235 static void dwarf64_findsect(const int);
238 * Special section numbers which are used to define ELF special
239 * symbols, which can be used with WRT to provide PIC relocation
240 * types.
242 static int32_t elf_gotpc_sect, elf_gotoff_sect;
243 static int32_t elf_got_sect, elf_plt_sect;
244 static int32_t elf_sym_sect;
245 static int32_t elf_gottpoff_sect;
247 static void elf_init(void)
249 maxbits = 64;
250 sects = NULL;
251 nsects = sectlen = 0;
252 syms = saa_init((int32_t)sizeof(struct Symbol));
253 nlocals = nglobs = ndebugs = 0;
254 bsym = raa_init();
255 strs = saa_init(1L);
256 saa_wbytes(strs, "\0", 1L);
257 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
258 strslen = 2 + strlen(elf_module);
259 shstrtab = NULL;
260 shstrtablen = shstrtabsize = 0;;
261 add_sectname("", "");
263 fwds = NULL;
265 elf_gotpc_sect = seg_alloc();
266 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
267 elf_gotoff_sect = seg_alloc();
268 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
269 elf_got_sect = seg_alloc();
270 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
271 elf_plt_sect = seg_alloc();
272 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
273 elf_sym_sect = seg_alloc();
274 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
275 elf_gottpoff_sect = seg_alloc();
276 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
278 def_seg = seg_alloc();
282 static void elf_cleanup(int debuginfo)
284 struct Reloc *r;
285 int i;
287 (void)debuginfo;
289 elf_write();
290 for (i = 0; i < nsects; i++) {
291 if (sects[i]->type != SHT_NOBITS)
292 saa_free(sects[i]->data);
293 if (sects[i]->head)
294 saa_free(sects[i]->rel);
295 while (sects[i]->head) {
296 r = sects[i]->head;
297 sects[i]->head = sects[i]->head->next;
298 nasm_free(r);
301 nasm_free(sects);
302 saa_free(syms);
303 raa_free(bsym);
304 saa_free(strs);
305 if (of_elf64.current_dfmt) {
306 of_elf64.current_dfmt->cleanup();
309 /* add entry to the elf .shstrtab section */
310 static void add_sectname(char *firsthalf, char *secondhalf)
312 int len = strlen(firsthalf) + strlen(secondhalf);
313 while (shstrtablen + len + 1 > shstrtabsize)
314 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
315 strcpy(shstrtab + shstrtablen, firsthalf);
316 strcat(shstrtab + shstrtablen, secondhalf);
317 shstrtablen += len + 1;
320 static int elf_make_section(char *name, int type, int flags, int align)
322 struct Section *s;
324 s = nasm_malloc(sizeof(*s));
326 if (type != SHT_NOBITS)
327 s->data = saa_init(1L);
328 s->head = NULL;
329 s->tail = &s->head;
330 s->len = s->size = 0;
331 s->nrelocs = 0;
332 if (!strcmp(name, ".text"))
333 s->index = def_seg;
334 else
335 s->index = seg_alloc();
336 add_sectname("", name);
337 s->name = nasm_malloc(1 + strlen(name));
338 strcpy(s->name, name);
339 s->type = type;
340 s->flags = flags;
341 s->align = align;
342 s->gsyms = NULL;
344 if (nsects >= sectlen)
345 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
346 sects[nsects++] = s;
348 return nsects - 1;
351 static int32_t elf_section_names(char *name, int pass, int *bits)
353 char *p;
354 uint32_t flags, flags_and, flags_or;
355 uint64_t align;
356 int type, i;
359 * Default is 64 bits.
361 if (!name) {
362 *bits = 64;
363 return def_seg;
366 p = name;
367 while (*p && !nasm_isspace(*p))
368 p++;
369 if (*p)
370 *p++ = '\0';
371 flags_and = flags_or = type = align = 0;
373 while (*p && nasm_isspace(*p))
374 p++;
375 while (*p) {
376 char *q = p;
377 while (*p && !nasm_isspace(*p))
378 p++;
379 if (*p)
380 *p++ = '\0';
381 while (*p && nasm_isspace(*p))
382 p++;
384 if (!nasm_strnicmp(q, "align=", 6)) {
385 align = atoi(q + 6);
386 if (align == 0)
387 align = 1;
388 if ((align - 1) & align) { /* means it's not a power of two */
389 nasm_error(ERR_NONFATAL, "section alignment %"PRId64" is not"
390 " a power of two", align);
391 align = 1;
393 } else if (!nasm_stricmp(q, "alloc")) {
394 flags_and |= SHF_ALLOC;
395 flags_or |= SHF_ALLOC;
396 } else if (!nasm_stricmp(q, "noalloc")) {
397 flags_and |= SHF_ALLOC;
398 flags_or &= ~SHF_ALLOC;
399 } else if (!nasm_stricmp(q, "exec")) {
400 flags_and |= SHF_EXECINSTR;
401 flags_or |= SHF_EXECINSTR;
402 } else if (!nasm_stricmp(q, "noexec")) {
403 flags_and |= SHF_EXECINSTR;
404 flags_or &= ~SHF_EXECINSTR;
405 } else if (!nasm_stricmp(q, "write")) {
406 flags_and |= SHF_WRITE;
407 flags_or |= SHF_WRITE;
408 } else if (!nasm_stricmp(q, "tls")) {
409 flags_and |= SHF_TLS;
410 flags_or |= SHF_TLS;
411 } else if (!nasm_stricmp(q, "nowrite")) {
412 flags_and |= SHF_WRITE;
413 flags_or &= ~SHF_WRITE;
414 } else if (!nasm_stricmp(q, "progbits")) {
415 type = SHT_PROGBITS;
416 } else if (!nasm_stricmp(q, "nobits")) {
417 type = SHT_NOBITS;
418 } else if (pass == 1) {
419 nasm_error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
420 " declaration of section `%s'", q, name);
424 if (!strcmp(name, ".shstrtab") ||
425 !strcmp(name, ".symtab") ||
426 !strcmp(name, ".strtab")) {
427 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
428 "name `%s'", name);
429 return NO_SEG;
432 for (i = 0; i < nsects; i++)
433 if (!strcmp(name, sects[i]->name))
434 break;
435 if (i == nsects) {
436 const struct elf_known_section *ks = elf_known_sections;
438 while (ks->name) {
439 if (!strcmp(name, ks->name))
440 break;
441 ks++;
444 type = type ? type : ks->type;
445 align = align ? align : ks->align;
446 flags = (ks->flags & ~flags_and) | flags_or;
448 i = elf_make_section(name, type, flags, align);
449 } else if (pass == 1) {
450 if ((type && sects[i]->type != type)
451 || (align && sects[i]->align != align)
452 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
453 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
454 " redeclaration of section `%s'", name);
457 return sects[i]->index;
460 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
461 int is_global, char *special)
463 int pos = strslen;
464 struct Symbol *sym;
465 bool special_used = false;
467 #if defined(DEBUG) && DEBUG>2
468 nasm_error(ERR_DEBUG,
469 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
470 name, segment, offset, is_global, special);
471 #endif
472 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
474 * This is a NASM special symbol. We never allow it into
475 * the ELF symbol table, even if it's a valid one. If it
476 * _isn't_ a valid one, we should barf immediately.
478 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
479 strcmp(name, "..got") && strcmp(name, "..plt") &&
480 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
481 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
482 return;
485 if (is_global == 3) {
486 struct Symbol **s;
488 * Fix up a forward-reference symbol size from the first
489 * pass.
491 for (s = &fwds; *s; s = &(*s)->nextfwd)
492 if (!strcmp((*s)->name, name)) {
493 struct tokenval tokval;
494 expr *e;
495 char *p = special;
497 while (*p && !nasm_isspace(*p))
498 p++;
499 while (*p && nasm_isspace(*p))
500 p++;
501 stdscan_reset();
502 stdscan_bufptr = p;
503 tokval.t_type = TOKEN_INVALID;
504 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
505 if (e) {
506 if (!is_simple(e))
507 nasm_error(ERR_NONFATAL, "cannot use relocatable"
508 " expression as symbol size");
509 else
510 (*s)->size = reloc_value(e);
514 * Remove it from the list of unresolved sizes.
516 nasm_free((*s)->name);
517 *s = (*s)->nextfwd;
518 return;
520 return; /* it wasn't an important one */
523 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
524 strslen += 1 + strlen(name);
526 lastsym = sym = saa_wstruct(syms);
528 memset(&sym->symv, 0, sizeof(struct rbtree));
530 sym->strpos = pos;
531 sym->type = is_global ? SYM_GLOBAL : 0;
532 sym->other = STV_DEFAULT;
533 sym->size = 0;
534 if (segment == NO_SEG)
535 sym->section = SHN_ABS;
536 else {
537 int i;
538 sym->section = SHN_UNDEF;
539 if (nsects == 0 && segment == def_seg) {
540 int tempint;
541 if (segment != elf_section_names(".text", 2, &tempint))
542 nasm_error(ERR_PANIC,
543 "strange segment conditions in ELF driver");
544 sym->section = nsects;
545 } else {
546 for (i = 0; i < nsects; i++)
547 if (segment == sects[i]->index) {
548 sym->section = i + 1;
549 break;
554 if (is_global == 2) {
555 sym->size = offset;
556 sym->symv.key = 0;
557 sym->section = SHN_COMMON;
559 * We have a common variable. Check the special text to see
560 * if it's a valid number and power of two; if so, store it
561 * as the alignment for the common variable.
563 if (special) {
564 bool err;
565 sym->symv.key = readnum(special, &err);
566 if (err)
567 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
568 " valid number", special);
569 else if ((sym->symv.key | (sym->symv.key - 1))
570 != 2 * sym->symv.key - 1)
571 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
572 " power of two", special);
574 special_used = true;
575 } else
576 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
578 if (sym->type == SYM_GLOBAL) {
580 * If sym->section == SHN_ABS, then the first line of the
581 * else section would cause a core dump, because its a reference
582 * beyond the end of the section array.
583 * This behaviour is exhibited by this code:
584 * GLOBAL crash_nasm
585 * crash_nasm equ 0
586 * To avoid such a crash, such requests are silently discarded.
587 * This may not be the best solution.
589 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
590 bsym = raa_write(bsym, segment, nglobs);
591 } else if (sym->section != SHN_ABS) {
593 * This is a global symbol; so we must add it to the rbtree
594 * of global symbols in its section.
596 * In addition, we check the special text for symbol
597 * type and size information.
599 sects[sym->section-1]->gsyms =
600 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
602 if (special) {
603 int n = strcspn(special, " \t");
605 if (!nasm_strnicmp(special, "function", n))
606 sym->type |= STT_FUNC;
607 else if (!nasm_strnicmp(special, "data", n) ||
608 !nasm_strnicmp(special, "object", n))
609 sym->type |= STT_OBJECT;
610 else if (!nasm_strnicmp(special, "notype", n))
611 sym->type |= STT_NOTYPE;
612 else
613 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
614 n, special);
615 special += n;
617 while (nasm_isspace(*special))
618 ++special;
619 if (*special) {
620 n = strcspn(special, " \t");
621 if (!nasm_strnicmp(special, "default", n))
622 sym->other = STV_DEFAULT;
623 else if (!nasm_strnicmp(special, "internal", n))
624 sym->other = STV_INTERNAL;
625 else if (!nasm_strnicmp(special, "hidden", n))
626 sym->other = STV_HIDDEN;
627 else if (!nasm_strnicmp(special, "protected", n))
628 sym->other = STV_PROTECTED;
629 else
630 n = 0;
631 special += n;
634 if (*special) {
635 struct tokenval tokval;
636 expr *e;
637 int fwd = 0;
638 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
640 while (special[n] && nasm_isspace(special[n]))
641 n++;
643 * We have a size expression; attempt to
644 * evaluate it.
646 stdscan_reset();
647 stdscan_bufptr = special + n;
648 tokval.t_type = TOKEN_INVALID;
649 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
650 NULL);
651 if (fwd) {
652 sym->nextfwd = fwds;
653 fwds = sym;
654 sym->name = nasm_strdup(name);
655 } else if (e) {
656 if (!is_simple(e))
657 nasm_error(ERR_NONFATAL, "cannot use relocatable"
658 " expression as symbol size");
659 else
660 sym->size = reloc_value(e);
662 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
664 special_used = true;
667 * If TLS segment, mark symbol accordingly.
669 if (sects[sym->section - 1]->flags & SHF_TLS) {
670 sym->type &= 0xf0;
671 sym->type |= STT_TLS;
674 sym->globnum = nglobs;
675 nglobs++;
676 } else
677 nlocals++;
679 if (special && !special_used)
680 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
683 static void elf_add_reloc(struct Section *sect, int32_t segment,
684 int64_t offset, int type)
686 struct Reloc *r;
687 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
688 sect->tail = &r->next;
689 r->next = NULL;
691 r->address = sect->len;
692 r->offset = offset;
693 if (segment == NO_SEG)
694 r->symbol = 0;
695 else {
696 int i;
697 r->symbol = 0;
698 for (i = 0; i < nsects; i++)
699 if (segment == sects[i]->index)
700 r->symbol = i + 2;
701 if (!r->symbol)
702 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
704 r->type = type;
706 sect->nrelocs++;
710 * This routine deals with ..got and ..sym relocations: the more
711 * complicated kinds. In shared-library writing, some relocations
712 * with respect to global symbols must refer to the precise symbol
713 * rather than referring to an offset from the base of the section
714 * _containing_ the symbol. Such relocations call to this routine,
715 * which searches the symbol list for the symbol in question.
717 * R_386_GOT32 references require the _exact_ symbol address to be
718 * used; R_386_32 references can be at an offset from the symbol.
719 * The boolean argument `exact' tells us this.
721 * Return value is the adjusted value of `addr', having become an
722 * offset from the symbol rather than the section. Should always be
723 * zero when returning from an exact call.
725 * Limitation: if you define two symbols at the same place,
726 * confusion will occur.
728 * Inefficiency: we search, currently, using a linked list which
729 * isn't even necessarily sorted.
731 static void elf_add_gsym_reloc(struct Section *sect,
732 int32_t segment, uint64_t offset, int64_t pcrel,
733 int type, bool exact)
735 struct Reloc *r;
736 struct Section *s;
737 struct Symbol *sym;
738 struct rbtree *srb;
739 int i;
742 * First look up the segment/offset pair and find a global
743 * symbol corresponding to it. If it's not one of our segments,
744 * then it must be an external symbol, in which case we're fine
745 * doing a normal elf_add_reloc after first sanity-checking
746 * that the offset from the symbol is zero.
748 s = NULL;
749 for (i = 0; i < nsects; i++)
750 if (segment == sects[i]->index) {
751 s = sects[i];
752 break;
755 if (!s) {
756 if (exact && offset)
757 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
758 else
759 elf_add_reloc(sect, segment, offset - pcrel, type);
760 return;
763 srb = rb_search(s->gsyms, offset);
764 if (!srb || (exact && srb->key != offset)) {
765 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
766 " for this reference");
767 return;
769 sym = container_of(srb, struct Symbol, symv);
771 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
772 sect->tail = &r->next;
773 r->next = NULL;
775 r->address = sect->len;
776 r->offset = offset - pcrel - sym->symv.key;
777 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
778 r->type = type;
780 sect->nrelocs++;
783 static void elf_out(int32_t segto, const void *data,
784 enum out_type type, uint64_t size,
785 int32_t segment, int32_t wrt)
787 struct Section *s;
788 int64_t addr, zero;
789 int i;
790 static struct symlininfo sinfo;
792 zero = 0;
794 #if defined(DEBUG) && DEBUG>2
795 if (data)
796 nasm_error(ERR_DEBUG,
797 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
798 currentline, type, segment, segto, size, *(int64_t *)data);
799 else
800 nasm_error(ERR_DEBUG,
801 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
802 currentline, type, segment, segto, size);
803 #endif
806 * handle absolute-assembly (structure definitions)
808 if (segto == NO_SEG) {
809 if (type != OUT_RESERVE)
810 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
811 " space");
812 return;
815 s = NULL;
816 for (i = 0; i < nsects; i++)
817 if (segto == sects[i]->index) {
818 s = sects[i];
819 break;
821 if (!s) {
822 int tempint; /* ignored */
823 if (segto != elf_section_names(".text", 2, &tempint))
824 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
825 else {
826 s = sects[nsects - 1];
827 i = nsects - 1;
830 /* invoke current debug_output routine */
831 if (of_elf64.current_dfmt) {
832 sinfo.offset = s->len;
833 sinfo.section = i;
834 sinfo.segto = segto;
835 sinfo.name = s->name;
836 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
838 /* end of debugging stuff */
840 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
841 nasm_error(ERR_WARNING, "attempt to initialize memory in"
842 " BSS section `%s': ignored", s->name);
843 s->len += realsize(type, size);
844 return;
847 if (type == OUT_RESERVE) {
848 if (s->type == SHT_PROGBITS) {
849 nasm_error(ERR_WARNING, "uninitialized space declared in"
850 " non-BSS section `%s': zeroing", s->name);
851 elf_sect_write(s, NULL, size);
852 } else
853 s->len += size;
854 } else if (type == OUT_RAWDATA) {
855 if (segment != NO_SEG)
856 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
857 elf_sect_write(s, data, size);
858 } else if (type == OUT_ADDRESS) {
859 addr = *(int64_t *)data;
860 if (segment == NO_SEG) {
861 /* Do nothing */
862 } else if (segment % 2) {
863 nasm_error(ERR_NONFATAL, "ELF format does not support"
864 " segment base references");
865 } else {
866 if (wrt == NO_SEG) {
867 switch ((int)size) {
868 case 1:
869 elf_add_reloc(s, segment, addr, R_X86_64_8);
870 break;
871 case 2:
872 elf_add_reloc(s, segment, addr, R_X86_64_16);
873 break;
874 case 4:
875 elf_add_reloc(s, segment, addr, R_X86_64_32);
876 break;
877 case 8:
878 elf_add_reloc(s, segment, addr, R_X86_64_64);
879 break;
880 default:
881 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
882 break;
884 addr = 0;
885 } else if (wrt == elf_gotpc_sect + 1) {
887 * The user will supply GOT relative to $$. ELF
888 * will let us have GOT relative to $. So we
889 * need to fix up the data item by $-$$.
891 addr += s->len;
892 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
893 addr = 0;
894 } else if (wrt == elf_gotoff_sect + 1) {
895 if (size != 8) {
896 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
897 "references to be qword");
898 } else {
899 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
900 addr = 0;
902 } else if (wrt == elf_got_sect + 1) {
903 switch ((int)size) {
904 case 4:
905 elf_add_gsym_reloc(s, segment, addr, 0,
906 R_X86_64_GOT32, true);
907 addr = 0;
908 break;
909 case 8:
910 elf_add_gsym_reloc(s, segment, addr, 0,
911 R_X86_64_GOT64, true);
912 addr = 0;
913 break;
914 default:
915 nasm_error(ERR_NONFATAL, "invalid ..got reference");
916 break;
918 } else if (wrt == elf_sym_sect + 1) {
919 switch ((int)size) {
920 case 1:
921 elf_add_gsym_reloc(s, segment, addr, 0,
922 R_X86_64_8, false);
923 addr = 0;
924 break;
925 case 2:
926 elf_add_gsym_reloc(s, segment, addr, 0,
927 R_X86_64_16, false);
928 addr = 0;
929 break;
930 case 4:
931 elf_add_gsym_reloc(s, segment, addr, 0,
932 R_X86_64_32, false);
933 addr = 0;
934 break;
935 case 8:
936 elf_add_gsym_reloc(s, segment, addr, 0,
937 R_X86_64_64, false);
938 addr = 0;
939 break;
940 default:
941 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
942 break;
944 } else if (wrt == elf_plt_sect + 1) {
945 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
946 "relative PLT references");
947 } else {
948 nasm_error(ERR_NONFATAL, "ELF format does not support this"
949 " use of WRT");
952 elf_sect_writeaddr(s, addr, size);
953 } else if (type == OUT_REL2ADR) {
954 addr = *(int64_t *)data - size;
955 if (segment == segto)
956 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
957 if (segment == NO_SEG) {
958 /* Do nothing */
959 } else if (segment % 2) {
960 nasm_error(ERR_NONFATAL, "ELF format does not support"
961 " segment base references");
962 } else {
963 if (wrt == NO_SEG) {
964 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
965 addr = 0;
966 } else {
967 nasm_error(ERR_NONFATAL,
968 "Unsupported non-32-bit ELF relocation [2]");
971 elf_sect_writeaddr(s, addr, 2);
972 } else if (type == OUT_REL4ADR) {
973 addr = *(int64_t *)data - size;
974 if (segment == segto)
975 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
976 if (segment == NO_SEG) {
977 /* Do nothing */
978 } else if (segment % 2) {
979 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
980 " segment base references");
981 } else {
982 if (wrt == NO_SEG) {
983 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
984 addr = 0;
985 } else if (wrt == elf_plt_sect + 1) {
986 elf_add_gsym_reloc(s, segment, addr+size, size,
987 R_X86_64_PLT32, true);
988 addr = 0;
989 } else if (wrt == elf_gotpc_sect + 1 ||
990 wrt == elf_got_sect + 1) {
991 elf_add_gsym_reloc(s, segment, addr+size, size,
992 R_X86_64_GOTPCREL, true);
993 addr = 0;
994 } else if (wrt == elf_gotoff_sect + 1 ||
995 wrt == elf_got_sect + 1) {
996 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
997 "qword absolute");
998 } else if (wrt == elf_gottpoff_sect + 1) {
999 elf_add_gsym_reloc(s, segment, addr+size, size,
1000 R_X86_64_GOTTPOFF, true);
1001 addr = 0;
1002 } else {
1003 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1004 " use of WRT");
1007 elf_sect_writeaddr(s, addr, 4);
1008 } else if (type == OUT_REL8ADR) {
1009 addr = *(int64_t *)data - size;
1010 if (segment == segto)
1011 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1012 if (segment == NO_SEG) {
1013 /* Do nothing */
1014 } else if (segment % 2) {
1015 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1016 " segment base references");
1017 } else {
1018 if (wrt == NO_SEG) {
1019 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1020 addr = 0;
1021 } else if (wrt == elf_gotpc_sect + 1 ||
1022 wrt == elf_got_sect + 1) {
1023 elf_add_gsym_reloc(s, segment, addr+size, size,
1024 R_X86_64_GOTPCREL64, true);
1025 addr = 0;
1026 } else if (wrt == elf_gotoff_sect + 1 ||
1027 wrt == elf_got_sect + 1) {
1028 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1029 "absolute");
1030 } else if (wrt == elf_gottpoff_sect + 1) {
1031 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1032 "dword");
1033 } else {
1034 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1035 " use of WRT");
1038 elf_sect_writeaddr(s, addr, 8);
1042 static void elf_write(void)
1044 int align;
1045 char *p;
1046 int i;
1048 struct SAA *symtab;
1049 int32_t symtablen, symtablocal;
1052 * Work out how many sections we will have. We have SHN_UNDEF,
1053 * then the flexible user sections, then the fixed sections
1054 * `.shstrtab', `.symtab' and `.strtab', then optionally
1055 * relocation sections for the user sections.
1057 nsections = sec_numspecial + 1;
1058 if (of_elf64.current_dfmt == &df_stabs)
1059 nsections += 3;
1060 else if (of_elf64.current_dfmt == &df_dwarf)
1061 nsections += 10;
1063 add_sectname("", ".shstrtab");
1064 add_sectname("", ".symtab");
1065 add_sectname("", ".strtab");
1066 for (i = 0; i < nsects; i++) {
1067 nsections++; /* for the section itself */
1068 if (sects[i]->head) {
1069 nsections++; /* for its relocations */
1070 add_sectname(".rela", sects[i]->name);
1074 if (of_elf64.current_dfmt == &df_stabs) {
1075 /* in case the debug information is wanted, just add these three sections... */
1076 add_sectname("", ".stab");
1077 add_sectname("", ".stabstr");
1078 add_sectname(".rel", ".stab");
1081 else if (of_elf64.current_dfmt == &df_dwarf) {
1082 /* the dwarf debug standard specifies the following ten sections,
1083 not all of which are currently implemented,
1084 although all of them are defined. */
1085 #define debug_aranges (int64_t) (nsections-10)
1086 #define debug_info (int64_t) (nsections-7)
1087 #define debug_abbrev (int64_t) (nsections-5)
1088 #define debug_line (int64_t) (nsections-4)
1089 add_sectname("", ".debug_aranges");
1090 add_sectname(".rela", ".debug_aranges");
1091 add_sectname("", ".debug_pubnames");
1092 add_sectname("", ".debug_info");
1093 add_sectname(".rela", ".debug_info");
1094 add_sectname("", ".debug_abbrev");
1095 add_sectname("", ".debug_line");
1096 add_sectname(".rela", ".debug_line");
1097 add_sectname("", ".debug_frame");
1098 add_sectname("", ".debug_loc");
1102 * Output the ELF header.
1104 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1105 fputc(elf_osabi, ofile);
1106 fputc(elf_abiver, ofile);
1107 fwritezero(7, ofile);
1108 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1109 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1110 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1111 fwriteint64_t(0L, ofile); /* no entry point */
1112 fwriteint64_t(0L, ofile); /* no program header table */
1113 fwriteint64_t(0x40L, ofile); /* section headers straight after
1114 * ELF header plus alignment */
1115 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1116 fwriteint16_t(0x40, ofile); /* size of ELF header */
1117 fwriteint16_t(0, ofile); /* no program header table, again */
1118 fwriteint16_t(0, ofile); /* still no program header table */
1119 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1120 fwriteint16_t(nsections, ofile); /* number of sections */
1121 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1122 * section header table */
1125 * Build the symbol table and relocation tables.
1127 symtab = elf_build_symtab(&symtablen, &symtablocal);
1128 for (i = 0; i < nsects; i++)
1129 if (sects[i]->head)
1130 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1131 sects[i]->head);
1134 * Now output the section header table.
1137 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1138 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1139 elf_foffs += align;
1140 elf_nsect = 0;
1141 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1143 /* SHN_UNDEF */
1144 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1145 p = shstrtab + 1;
1147 /* The normal sections */
1148 for (i = 0; i < nsects; i++) {
1149 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1150 (sects[i]->type == SHT_PROGBITS ?
1151 sects[i]->data : NULL), true,
1152 sects[i]->len, 0, 0, sects[i]->align, 0);
1153 p += strlen(p) + 1;
1156 /* .shstrtab */
1157 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1158 shstrtablen, 0, 0, 1, 0);
1159 p += strlen(p) + 1;
1161 /* .symtab */
1162 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1163 symtablen, sec_strtab, symtablocal, 4, 24);
1164 p += strlen(p) + 1;
1166 /* .strtab */
1167 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1168 strslen, 0, 0, 1, 0);
1169 p += strlen(p) + 1;
1171 /* The relocation sections */
1172 for (i = 0; i < nsects; i++)
1173 if (sects[i]->head) {
1174 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1175 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1176 p += strlen(p) + 1;
1179 if (of_elf64.current_dfmt == &df_stabs) {
1180 /* for debugging information, create the last three sections
1181 which are the .stab , .stabstr and .rel.stab sections respectively */
1183 /* this function call creates the stab sections in memory */
1184 stabs64_generate();
1186 if (stabbuf && stabstrbuf && stabrelbuf) {
1187 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1188 stablen, sec_stabstr, 0, 4, 12);
1189 p += strlen(p) + 1;
1191 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1192 stabstrlen, 0, 0, 4, 0);
1193 p += strlen(p) + 1;
1195 /* link -> symtable info -> section to refer to */
1196 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1197 stabrellen, symtabsection, sec_stab, 4, 16);
1198 p += strlen(p) + 1;
1201 else if (of_elf64.current_dfmt == &df_dwarf) {
1202 /* for dwarf debugging information, create the ten dwarf sections */
1204 /* this function call creates the dwarf sections in memory */
1205 if (dwarf_fsect)
1206 dwarf64_generate();
1208 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1209 arangeslen, 0, 0, 1, 0);
1210 p += strlen(p) + 1;
1212 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1213 arangesrellen, symtabsection, debug_aranges, 1, 24);
1214 p += strlen(p) + 1;
1216 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1217 pubnameslen, 0, 0, 1, 0);
1218 p += strlen(p) + 1;
1220 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1221 infolen, 0, 0, 1, 0);
1222 p += strlen(p) + 1;
1224 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1225 inforellen, symtabsection, debug_info, 1, 24);
1226 p += strlen(p) + 1;
1228 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1229 abbrevlen, 0, 0, 1, 0);
1230 p += strlen(p) + 1;
1232 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1233 linelen, 0, 0, 1, 0);
1234 p += strlen(p) + 1;
1236 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1237 linerellen, symtabsection, debug_line, 1, 24);
1238 p += strlen(p) + 1;
1240 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1241 framelen, 0, 0, 8, 0);
1242 p += strlen(p) + 1;
1244 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1245 loclen, 0, 0, 1, 0);
1246 p += strlen(p) + 1;
1248 fwritezero(align, ofile);
1251 * Now output the sections.
1253 elf_write_sections();
1255 nasm_free(elf_sects);
1256 saa_free(symtab);
1259 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1261 struct SAA *s = saa_init(1L);
1262 struct Symbol *sym;
1263 uint8_t entry[24], *p;
1264 int i;
1266 *len = *local = 0;
1269 * First, an all-zeros entry, required by the ELF spec.
1271 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1272 *len += 24;
1273 (*local)++;
1276 * Next, an entry for the file name.
1278 p = entry;
1279 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1280 WRITESHORT(p, STT_FILE); /* type FILE */
1281 WRITESHORT(p, SHN_ABS);
1282 WRITEDLONG(p, (uint64_t) 0); /* no value */
1283 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1284 saa_wbytes(s, entry, 24L);
1285 *len += 24;
1286 (*local)++;
1289 * Now some standard symbols defining the segments, for relocation
1290 * purposes.
1292 for (i = 1; i <= nsects; i++) {
1293 p = entry;
1294 WRITELONG(p, 0); /* no symbol name */
1295 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1296 WRITESHORT(p, i); /* section id */
1297 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1298 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1299 saa_wbytes(s, entry, 24L);
1300 *len += 24;
1301 (*local)++;
1306 * Now the other local symbols.
1308 saa_rewind(syms);
1309 while ((sym = saa_rstruct(syms))) {
1310 if (sym->type & SYM_GLOBAL)
1311 continue;
1312 p = entry;
1313 WRITELONG(p, sym->strpos); /* index into symbol string table */
1314 WRITECHAR(p, sym->type); /* type and binding */
1315 WRITECHAR(p, sym->other); /* visibility */
1316 WRITESHORT(p, sym->section); /* index into section header table */
1317 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1318 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1319 saa_wbytes(s, entry, 24L);
1320 *len += 24;
1321 (*local)++;
1324 * dwarf needs symbols for debug sections
1325 * which are relocation targets.
1327 if (of_elf64.current_dfmt == &df_dwarf) {
1328 dwarf_infosym = *local;
1329 p = entry;
1330 WRITELONG(p, 0); /* no symbol name */
1331 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1332 WRITESHORT(p, debug_info); /* section id */
1333 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1334 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1335 saa_wbytes(s, entry, 24L);
1336 *len += 24;
1337 (*local)++;
1338 dwarf_abbrevsym = *local;
1339 p = entry;
1340 WRITELONG(p, 0); /* no symbol name */
1341 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1342 WRITESHORT(p, debug_abbrev); /* section id */
1343 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1344 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1345 saa_wbytes(s, entry, 24L);
1346 *len += 24;
1347 (*local)++;
1348 dwarf_linesym = *local;
1349 p = entry;
1350 WRITELONG(p, 0); /* no symbol name */
1351 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1352 WRITESHORT(p, debug_line); /* section id */
1353 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1354 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1355 saa_wbytes(s, entry, 24L);
1356 *len += 24;
1357 (*local)++;
1361 * Now the global symbols.
1363 saa_rewind(syms);
1364 while ((sym = saa_rstruct(syms))) {
1365 if (!(sym->type & SYM_GLOBAL))
1366 continue;
1367 p = entry;
1368 WRITELONG(p, sym->strpos);
1369 WRITECHAR(p, sym->type); /* type and binding */
1370 WRITECHAR(p, sym->other); /* visibility */
1371 WRITESHORT(p, sym->section);
1372 WRITEDLONG(p, (int64_t)sym->symv.key);
1373 WRITEDLONG(p, (int64_t)sym->size);
1374 saa_wbytes(s, entry, 24L);
1375 *len += 24;
1378 return s;
1381 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1383 struct SAA *s;
1384 uint8_t *p, entry[24];
1385 int32_t global_offset;
1387 if (!r)
1388 return NULL;
1390 s = saa_init(1L);
1391 *len = 0;
1394 * How to onvert from a global placeholder to a real symbol index;
1395 * the +2 refers to the two special entries, the null entry and
1396 * the filename entry.
1398 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1400 while (r) {
1401 int32_t sym = r->symbol;
1403 if (sym >= GLOBAL_TEMP_BASE)
1404 sym += global_offset;
1406 p = entry;
1407 WRITEDLONG(p, r->address);
1408 WRITELONG(p, r->type);
1409 WRITELONG(p, sym);
1410 WRITEDLONG(p, r->offset);
1411 saa_wbytes(s, entry, 24L);
1412 *len += 24;
1414 r = r->next;
1417 return s;
1420 static void elf_section_header(int name, int type, uint64_t flags,
1421 void *data, bool is_saa, uint64_t datalen,
1422 int link, int info, int align, int eltsize)
1424 elf_sects[elf_nsect].data = data;
1425 elf_sects[elf_nsect].len = datalen;
1426 elf_sects[elf_nsect].is_saa = is_saa;
1427 elf_nsect++;
1429 fwriteint32_t((int32_t)name, ofile);
1430 fwriteint32_t((int32_t)type, ofile);
1431 fwriteint64_t((int64_t)flags, ofile);
1432 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1433 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1434 fwriteint64_t(datalen, ofile);
1435 if (data)
1436 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1437 fwriteint32_t((int32_t)link, ofile);
1438 fwriteint32_t((int32_t)info, ofile);
1439 fwriteint64_t((int64_t)align, ofile);
1440 fwriteint64_t((int64_t)eltsize, ofile);
1443 static void elf_write_sections(void)
1445 int i;
1446 for (i = 0; i < elf_nsect; i++)
1447 if (elf_sects[i].data) {
1448 int32_t len = elf_sects[i].len;
1449 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1450 int32_t align = reallen - len;
1451 if (elf_sects[i].is_saa)
1452 saa_fpwrite(elf_sects[i].data, ofile);
1453 else
1454 fwrite(elf_sects[i].data, len, 1, ofile);
1455 fwritezero(align, ofile);
1459 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1461 saa_wbytes(sect->data, data, len);
1462 sect->len += len;
1464 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1466 saa_writeaddr(sect->data, data, len);
1467 sect->len += len;
1470 static int32_t elf_segbase(int32_t segment)
1472 return segment;
1475 static int elf_directive(enum directives directive, char *value, int pass)
1477 bool err;
1478 int64_t n;
1479 char *p;
1481 switch (directive) {
1482 case D_OSABI:
1483 if (pass == 2)
1484 return 1; /* ignore in pass 2 */
1486 n = readnum(value, &err);
1487 if (err) {
1488 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1489 return 1;
1491 if (n < 0 || n > 255) {
1492 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1493 return 1;
1495 elf_osabi = n;
1496 elf_abiver = 0;
1498 if ((p = strchr(value,',')) == NULL)
1499 return 1;
1501 n = readnum(p+1, &err);
1502 if (err || n < 0 || n > 255) {
1503 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1504 return 1;
1507 elf_abiver = n;
1508 return 1;
1510 default:
1511 return 0;
1515 static void elf_filename(char *inname, char *outname)
1517 strcpy(elf_module, inname);
1518 standard_extension(inname, outname, ".o");
1521 extern macros_t elf_stdmac[];
1523 static int elf_set_info(enum geninfo type, char **val)
1525 (void)type;
1526 (void)val;
1527 return 0;
1529 static struct dfmt df_dwarf = {
1530 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1531 "dwarf",
1532 dwarf64_init,
1533 dwarf64_linenum,
1534 debug64_deflabel,
1535 debug64_directive,
1536 debug64_typevalue,
1537 dwarf64_output,
1538 dwarf64_cleanup
1540 static struct dfmt df_stabs = {
1541 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1542 "stabs",
1543 null_debug_init,
1544 stabs64_linenum,
1545 debug64_deflabel,
1546 debug64_directive,
1547 debug64_typevalue,
1548 stabs64_output,
1549 stabs64_cleanup
1552 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1554 struct ofmt of_elf64 = {
1555 "ELF64 (x86_64) object files (e.g. Linux)",
1556 "elf64",
1558 elf64_debugs_arr,
1559 &df_stabs,
1560 elf_stdmac,
1561 elf_init,
1562 elf_set_info,
1563 elf_out,
1564 elf_deflabel,
1565 elf_section_names,
1566 elf_segbase,
1567 elf_directive,
1568 elf_filename,
1569 elf_cleanup
1572 /* common debugging routines */
1573 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1574 int is_global, char *special)
1576 (void)name;
1577 (void)segment;
1578 (void)offset;
1579 (void)is_global;
1580 (void)special;
1583 static void debug64_directive(const char *directive, const char *params)
1585 (void)directive;
1586 (void)params;
1589 static void debug64_typevalue(int32_t type)
1591 int32_t stype, ssize;
1592 switch (TYM_TYPE(type)) {
1593 case TY_LABEL:
1594 ssize = 0;
1595 stype = STT_NOTYPE;
1596 break;
1597 case TY_BYTE:
1598 ssize = 1;
1599 stype = STT_OBJECT;
1600 break;
1601 case TY_WORD:
1602 ssize = 2;
1603 stype = STT_OBJECT;
1604 break;
1605 case TY_DWORD:
1606 ssize = 4;
1607 stype = STT_OBJECT;
1608 break;
1609 case TY_FLOAT:
1610 ssize = 4;
1611 stype = STT_OBJECT;
1612 break;
1613 case TY_QWORD:
1614 ssize = 8;
1615 stype = STT_OBJECT;
1616 break;
1617 case TY_TBYTE:
1618 ssize = 10;
1619 stype = STT_OBJECT;
1620 break;
1621 case TY_OWORD:
1622 ssize = 16;
1623 stype = STT_OBJECT;
1624 break;
1625 case TY_YWORD:
1626 ssize = 32;
1627 stype = STT_OBJECT;
1628 break;
1629 case TY_COMMON:
1630 ssize = 0;
1631 stype = STT_COMMON;
1632 break;
1633 case TY_SEG:
1634 ssize = 0;
1635 stype = STT_SECTION;
1636 break;
1637 case TY_EXTERN:
1638 ssize = 0;
1639 stype = STT_NOTYPE;
1640 break;
1641 case TY_EQU:
1642 ssize = 0;
1643 stype = STT_NOTYPE;
1644 break;
1645 default:
1646 ssize = 0;
1647 stype = STT_NOTYPE;
1648 break;
1650 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1651 lastsym->size = ssize;
1652 lastsym->type = stype;
1656 /* stabs debugging routines */
1658 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1660 (void)segto;
1661 if (!stabs_filename) {
1662 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1663 strcpy(stabs_filename, filename);
1664 } else {
1665 if (strcmp(stabs_filename, filename)) {
1666 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1667 in fact, this leak comes in quite handy to maintain a list of files
1668 encountered so far in the symbol lines... */
1670 /* why not nasm_free(stabs_filename); we're done with the old one */
1672 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1673 strcpy(stabs_filename, filename);
1676 debug_immcall = 1;
1677 currentline = linenumber;
1681 static void stabs64_output(int type, void *param)
1683 struct symlininfo *s;
1684 struct linelist *el;
1685 if (type == TY_DEBUGSYMLIN) {
1686 if (debug_immcall) {
1687 s = (struct symlininfo *)param;
1688 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1689 return; /* line info is only collected for executable sections */
1690 numlinestabs++;
1691 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1692 el->info.offset = s->offset;
1693 el->info.section = s->section;
1694 el->info.name = s->name;
1695 el->line = currentline;
1696 el->filename = stabs_filename;
1697 el->next = 0;
1698 if (stabslines) {
1699 stabslines->last->next = el;
1700 stabslines->last = el;
1701 } else {
1702 stabslines = el;
1703 stabslines->last = el;
1707 debug_immcall = 0;
1710 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1711 do {\
1712 WRITELONG(p,n_strx); \
1713 WRITECHAR(p,n_type); \
1714 WRITECHAR(p,n_other); \
1715 WRITESHORT(p,n_desc); \
1716 WRITELONG(p,n_value); \
1717 } while (0)
1719 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1721 static void stabs64_generate(void)
1723 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1724 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1725 char **allfiles;
1726 int *fileidx;
1728 struct linelist *ptr;
1730 ptr = stabslines;
1732 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1733 for (i = 0; i < numlinestabs; i++)
1734 allfiles[i] = 0;
1735 numfiles = 0;
1736 while (ptr) {
1737 if (numfiles == 0) {
1738 allfiles[0] = ptr->filename;
1739 numfiles++;
1740 } else {
1741 for (i = 0; i < numfiles; i++) {
1742 if (!strcmp(allfiles[i], ptr->filename))
1743 break;
1745 if (i >= numfiles) {
1746 allfiles[i] = ptr->filename;
1747 numfiles++;
1750 ptr = ptr->next;
1752 strsize = 1;
1753 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1754 for (i = 0; i < numfiles; i++) {
1755 fileidx[i] = strsize;
1756 strsize += strlen(allfiles[i]) + 1;
1758 mainfileindex = 0;
1759 for (i = 0; i < numfiles; i++) {
1760 if (!strcmp(allfiles[i], elf_module)) {
1761 mainfileindex = i;
1762 break;
1767 * worst case size of the stab buffer would be:
1768 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1769 * plus one "ending" entry
1771 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1772 sizeof(struct stabentry));
1773 ssbuf = (uint8_t *)nasm_malloc(strsize);
1774 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1775 rptr = rbuf;
1777 for (i = 0; i < numfiles; i++)
1778 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1779 ssbuf[0] = 0;
1781 stabstrlen = strsize; /* set global variable for length of stab strings */
1783 sptr = sbuf;
1784 ptr = stabslines;
1785 numstabs = 0;
1787 if (ptr) {
1788 /* this is the first stab, its strx points to the filename of the
1789 the source-file, the n_desc field should be set to the number
1790 of remaining stabs
1792 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1794 /* this is the stab for the main source file */
1795 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1797 /* relocation table entry */
1799 /* Since the symbol table has two entries before */
1800 /* the section symbols, the index in the info.section */
1801 /* member must be adjusted by adding 2 */
1803 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1804 WRITELONG(rptr, R_X86_64_32);
1805 WRITELONG(rptr, ptr->info.section + 2);
1807 numstabs++;
1808 currfile = mainfileindex;
1811 while (ptr) {
1812 if (strcmp(allfiles[currfile], ptr->filename)) {
1813 /* oops file has changed... */
1814 for (i = 0; i < numfiles; i++)
1815 if (!strcmp(allfiles[i], ptr->filename))
1816 break;
1817 currfile = i;
1818 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1819 ptr->info.offset);
1820 numstabs++;
1822 /* relocation table entry */
1824 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1825 WRITELONG(rptr, R_X86_64_32);
1826 WRITELONG(rptr, ptr->info.section + 2);
1829 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1830 numstabs++;
1832 /* relocation table entry */
1834 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1835 WRITELONG(rptr, R_X86_64_32);
1836 WRITELONG(rptr, ptr->info.section + 2);
1838 ptr = ptr->next;
1842 /* this is an "ending" token */
1843 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1844 numstabs++;
1846 ((struct stabentry *)sbuf)->n_desc = numstabs;
1848 nasm_free(allfiles);
1849 nasm_free(fileidx);
1851 stablen = (sptr - sbuf);
1852 stabrellen = (rptr - rbuf);
1853 stabrelbuf = rbuf;
1854 stabbuf = sbuf;
1855 stabstrbuf = ssbuf;
1858 static void stabs64_cleanup(void)
1860 struct linelist *ptr, *del;
1861 if (!stabslines)
1862 return;
1863 ptr = stabslines;
1864 while (ptr) {
1865 del = ptr;
1866 ptr = ptr->next;
1867 nasm_free(del);
1869 if (stabbuf)
1870 nasm_free(stabbuf);
1871 if (stabrelbuf)
1872 nasm_free(stabrelbuf);
1873 if (stabstrbuf)
1874 nasm_free(stabstrbuf);
1876 /* dwarf routines */
1877 static void dwarf64_init(void)
1879 ndebugs = 3; /* 3 debug symbols */
1882 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1883 int32_t segto)
1885 (void)segto;
1886 dwarf64_findfile(filename);
1887 debug_immcall = 1;
1888 currentline = linenumber;
1891 /* called from elf_out with type == TY_DEBUGSYMLIN */
1892 static void dwarf64_output(int type, void *param)
1894 int ln, aa, inx, maxln, soc;
1895 struct symlininfo *s;
1896 struct SAA *plinep;
1898 (void)type;
1900 s = (struct symlininfo *)param;
1901 /* line number info is only gathered for executable sections */
1902 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1903 return;
1904 /* Check if section index has changed */
1905 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1907 dwarf64_findsect(s->section);
1909 /* do nothing unless line or file has changed */
1910 if (debug_immcall)
1912 ln = currentline - dwarf_csect->line;
1913 aa = s->offset - dwarf_csect->offset;
1914 inx = dwarf_clist->line;
1915 plinep = dwarf_csect->psaa;
1916 /* check for file change */
1917 if (!(inx == dwarf_csect->file))
1919 saa_write8(plinep,DW_LNS_set_file);
1920 saa_write8(plinep,inx);
1921 dwarf_csect->file = inx;
1923 /* check for line change */
1924 if (ln)
1926 /* test if in range of special op code */
1927 maxln = line_base + line_range;
1928 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1929 if (ln >= line_base && ln < maxln && soc < 256)
1931 saa_write8(plinep,soc);
1933 else
1935 if (ln)
1937 saa_write8(plinep,DW_LNS_advance_line);
1938 saa_wleb128s(plinep,ln);
1940 if (aa)
1942 saa_write8(plinep,DW_LNS_advance_pc);
1943 saa_wleb128u(plinep,aa);
1946 dwarf_csect->line = currentline;
1947 dwarf_csect->offset = s->offset;
1949 /* show change handled */
1950 debug_immcall = 0;
1955 static void dwarf64_generate(void)
1957 uint8_t *pbuf;
1958 int indx;
1959 struct linelist *ftentry;
1960 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1961 struct SAA *parangesrel, *plinesrel, *pinforel;
1962 struct sectlist *psect;
1963 size_t saalen, linepoff, totlen, highaddr;
1965 /* write epilogues for each line program range */
1966 /* and build aranges section */
1967 paranges = saa_init(1L);
1968 parangesrel = saa_init(1L);
1969 saa_write16(paranges,3); /* dwarf version */
1970 saa_write64(parangesrel, paranges->datalen+4);
1971 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1972 saa_write64(parangesrel, 0);
1973 saa_write32(paranges,0); /* offset into info */
1974 saa_write8(paranges,8); /* pointer size */
1975 saa_write8(paranges,0); /* not segmented */
1976 saa_write32(paranges,0); /* padding */
1977 /* iterate though sectlist entries */
1978 psect = dwarf_fsect;
1979 totlen = 0;
1980 highaddr = 0;
1981 for (indx = 0; indx < dwarf_nsections; indx++)
1983 plinep = psect->psaa;
1984 /* Line Number Program Epilogue */
1985 saa_write8(plinep,2); /* std op 2 */
1986 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1987 saa_write8(plinep,DW_LNS_extended_op);
1988 saa_write8(plinep,1); /* operand length */
1989 saa_write8(plinep,DW_LNE_end_sequence);
1990 totlen += plinep->datalen;
1991 /* range table relocation entry */
1992 saa_write64(parangesrel, paranges->datalen + 4);
1993 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1994 saa_write64(parangesrel, (uint64_t) 0);
1995 /* range table entry */
1996 saa_write64(paranges,0x0000); /* range start */
1997 saa_write64(paranges,sects[psect->section]->len); /* range length */
1998 highaddr += sects[psect->section]->len;
1999 /* done with this entry */
2000 psect = psect->next;
2002 saa_write64(paranges,0); /* null address */
2003 saa_write64(paranges,0); /* null length */
2004 saalen = paranges->datalen;
2005 arangeslen = saalen + 4;
2006 arangesbuf = pbuf = nasm_malloc(arangeslen);
2007 WRITELONG(pbuf,saalen); /* initial length */
2008 saa_rnbytes(paranges, pbuf, saalen);
2009 saa_free(paranges);
2011 /* build rela.aranges section */
2012 arangesrellen = saalen = parangesrel->datalen;
2013 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2014 saa_rnbytes(parangesrel, pbuf, saalen);
2015 saa_free(parangesrel);
2017 /* build pubnames section */
2018 ppubnames = saa_init(1L);
2019 saa_write16(ppubnames,3); /* dwarf version */
2020 saa_write32(ppubnames,0); /* offset into info */
2021 saa_write32(ppubnames,0); /* space used in info */
2022 saa_write32(ppubnames,0); /* end of list */
2023 saalen = ppubnames->datalen;
2024 pubnameslen = saalen + 4;
2025 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2026 WRITELONG(pbuf,saalen); /* initial length */
2027 saa_rnbytes(ppubnames, pbuf, saalen);
2028 saa_free(ppubnames);
2030 /* build info section */
2031 pinfo = saa_init(1L);
2032 pinforel = saa_init(1L);
2033 saa_write16(pinfo,3); /* dwarf version */
2034 saa_write64(pinforel, pinfo->datalen + 4);
2035 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2036 saa_write64(pinforel, 0);
2037 saa_write32(pinfo,0); /* offset into abbrev */
2038 saa_write8(pinfo,8); /* pointer size */
2039 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2040 saa_write64(pinforel, pinfo->datalen + 4);
2041 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2042 saa_write64(pinforel, 0);
2043 saa_write64(pinfo,0); /* DW_AT_low_pc */
2044 saa_write64(pinforel, pinfo->datalen + 4);
2045 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2046 saa_write64(pinforel, 0);
2047 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2048 saa_write64(pinforel, pinfo->datalen + 4);
2049 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2050 saa_write64(pinforel, 0);
2051 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2052 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2053 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2054 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2055 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2056 saa_write64(pinforel, pinfo->datalen + 4);
2057 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2058 saa_write64(pinforel, 0);
2059 saa_write64(pinfo,0); /* DW_AT_low_pc */
2060 saa_write64(pinfo,0); /* DW_AT_frame_base */
2061 saa_write8(pinfo,0); /* end of entries */
2062 saalen = pinfo->datalen;
2063 infolen = saalen + 4;
2064 infobuf = pbuf = nasm_malloc(infolen);
2065 WRITELONG(pbuf,saalen); /* initial length */
2066 saa_rnbytes(pinfo, pbuf, saalen);
2067 saa_free(pinfo);
2069 /* build rela.info section */
2070 inforellen = saalen = pinforel->datalen;
2071 inforelbuf = pbuf = nasm_malloc(inforellen);
2072 saa_rnbytes(pinforel, pbuf, saalen);
2073 saa_free(pinforel);
2075 /* build abbrev section */
2076 pabbrev = saa_init(1L);
2077 saa_write8(pabbrev,1); /* entry number LEB128u */
2078 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2079 saa_write8(pabbrev,1); /* has children */
2080 /* the following attributes and forms are all LEB128u values */
2081 saa_write8(pabbrev,DW_AT_low_pc);
2082 saa_write8(pabbrev,DW_FORM_addr);
2083 saa_write8(pabbrev,DW_AT_high_pc);
2084 saa_write8(pabbrev,DW_FORM_addr);
2085 saa_write8(pabbrev,DW_AT_stmt_list);
2086 saa_write8(pabbrev,DW_FORM_data4);
2087 saa_write8(pabbrev,DW_AT_name);
2088 saa_write8(pabbrev,DW_FORM_string);
2089 saa_write8(pabbrev,DW_AT_producer);
2090 saa_write8(pabbrev,DW_FORM_string);
2091 saa_write8(pabbrev,DW_AT_language);
2092 saa_write8(pabbrev,DW_FORM_data2);
2093 saa_write16(pabbrev,0); /* end of entry */
2094 /* LEB128u usage same as above */
2095 saa_write8(pabbrev,2); /* entry number */
2096 saa_write8(pabbrev,DW_TAG_subprogram);
2097 saa_write8(pabbrev,0); /* no children */
2098 saa_write8(pabbrev,DW_AT_low_pc);
2099 saa_write8(pabbrev,DW_FORM_addr);
2100 saa_write8(pabbrev,DW_AT_frame_base);
2101 saa_write8(pabbrev,DW_FORM_data4);
2102 saa_write16(pabbrev,0); /* end of entry */
2103 abbrevlen = saalen = pabbrev->datalen;
2104 abbrevbuf = pbuf = nasm_malloc(saalen);
2105 saa_rnbytes(pabbrev, pbuf, saalen);
2106 saa_free(pabbrev);
2108 /* build line section */
2109 /* prolog */
2110 plines = saa_init(1L);
2111 saa_write8(plines,1); /* Minimum Instruction Length */
2112 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2113 saa_write8(plines,line_base); /* Line Base */
2114 saa_write8(plines,line_range); /* Line Range */
2115 saa_write8(plines,opcode_base); /* Opcode Base */
2116 /* standard opcode lengths (# of LEB128u operands) */
2117 saa_write8(plines,0); /* Std opcode 1 length */
2118 saa_write8(plines,1); /* Std opcode 2 length */
2119 saa_write8(plines,1); /* Std opcode 3 length */
2120 saa_write8(plines,1); /* Std opcode 4 length */
2121 saa_write8(plines,1); /* Std opcode 5 length */
2122 saa_write8(plines,0); /* Std opcode 6 length */
2123 saa_write8(plines,0); /* Std opcode 7 length */
2124 saa_write8(plines,0); /* Std opcode 8 length */
2125 saa_write8(plines,1); /* Std opcode 9 length */
2126 saa_write8(plines,0); /* Std opcode 10 length */
2127 saa_write8(plines,0); /* Std opcode 11 length */
2128 saa_write8(plines,1); /* Std opcode 12 length */
2129 /* Directory Table */
2130 saa_write8(plines,0); /* End of table */
2131 /* File Name Table */
2132 ftentry = dwarf_flist;
2133 for (indx = 0;indx<dwarf_numfiles;indx++)
2135 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2136 saa_write8(plines,0); /* directory LEB128u */
2137 saa_write8(plines,0); /* time LEB128u */
2138 saa_write8(plines,0); /* size LEB128u */
2139 ftentry = ftentry->next;
2141 saa_write8(plines,0); /* End of table */
2142 linepoff = plines->datalen;
2143 linelen = linepoff + totlen + 10;
2144 linebuf = pbuf = nasm_malloc(linelen);
2145 WRITELONG(pbuf,linelen-4); /* initial length */
2146 WRITESHORT(pbuf,3); /* dwarf version */
2147 WRITELONG(pbuf,linepoff); /* offset to line number program */
2148 /* write line header */
2149 saalen = linepoff;
2150 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2151 pbuf += linepoff;
2152 saa_free(plines);
2153 /* concatonate line program ranges */
2154 linepoff += 13;
2155 plinesrel = saa_init(1L);
2156 psect = dwarf_fsect;
2157 for (indx = 0; indx < dwarf_nsections; indx++)
2159 saa_write64(plinesrel, linepoff);
2160 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2161 saa_write64(plinesrel, (uint64_t) 0);
2162 plinep = psect->psaa;
2163 saalen = plinep->datalen;
2164 saa_rnbytes(plinep, pbuf, saalen);
2165 pbuf += saalen;
2166 linepoff += saalen;
2167 saa_free(plinep);
2168 /* done with this entry */
2169 psect = psect->next;
2173 /* build rela.lines section */
2174 linerellen =saalen = plinesrel->datalen;
2175 linerelbuf = pbuf = nasm_malloc(linerellen);
2176 saa_rnbytes(plinesrel, pbuf, saalen);
2177 saa_free(plinesrel);
2179 /* build frame section */
2180 framelen = 4;
2181 framebuf = pbuf = nasm_malloc(framelen);
2182 WRITELONG(pbuf,framelen-4); /* initial length */
2184 /* build loc section */
2185 loclen = 16;
2186 locbuf = pbuf = nasm_malloc(loclen);
2187 WRITEDLONG(pbuf,0); /* null beginning offset */
2188 WRITEDLONG(pbuf,0); /* null ending offset */
2191 static void dwarf64_cleanup(void)
2193 if (arangesbuf)
2194 nasm_free(arangesbuf);
2195 if (arangesrelbuf)
2196 nasm_free(arangesrelbuf);
2197 if (pubnamesbuf)
2198 nasm_free(pubnamesbuf);
2199 if (infobuf)
2200 nasm_free(infobuf);
2201 if (inforelbuf)
2202 nasm_free(inforelbuf);
2203 if (abbrevbuf)
2204 nasm_free(abbrevbuf);
2205 if (linebuf)
2206 nasm_free(linebuf);
2207 if (linerelbuf)
2208 nasm_free(linerelbuf);
2209 if (framebuf)
2210 nasm_free(framebuf);
2211 if (locbuf)
2212 nasm_free(locbuf);
2214 static void dwarf64_findfile(const char * fname)
2216 int finx;
2217 struct linelist *match;
2219 /* return if fname is current file name */
2220 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2221 /* search for match */
2222 else
2224 match = 0;
2225 if (dwarf_flist)
2227 match = dwarf_flist;
2228 for (finx = 0; finx < dwarf_numfiles; finx++)
2230 if (!(strcmp(fname, match->filename)))
2232 dwarf_clist = match;
2233 return;
2237 /* add file name to end of list */
2238 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2239 dwarf_numfiles++;
2240 dwarf_clist->line = dwarf_numfiles;
2241 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2242 strcpy(dwarf_clist->filename,fname);
2243 dwarf_clist->next = 0;
2244 /* if first entry */
2245 if (!dwarf_flist)
2247 dwarf_flist = dwarf_elist = dwarf_clist;
2248 dwarf_clist->last = 0;
2250 /* chain to previous entry */
2251 else
2253 dwarf_elist->next = dwarf_clist;
2254 dwarf_elist = dwarf_clist;
2258 /* */
2259 static void dwarf64_findsect(const int index)
2261 int sinx;
2262 struct sectlist *match;
2263 struct SAA *plinep;
2264 /* return if index is current section index */
2265 if (dwarf_csect && (dwarf_csect->section == index))
2267 return;
2269 /* search for match */
2270 else
2272 match = 0;
2273 if (dwarf_fsect)
2275 match = dwarf_fsect;
2276 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2278 if ((match->section == index))
2280 dwarf_csect = match;
2281 return;
2283 match = match->next;
2286 /* add entry to end of list */
2287 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2288 dwarf_nsections++;
2289 dwarf_csect->psaa = plinep = saa_init(1L);
2290 dwarf_csect->line = 1;
2291 dwarf_csect->offset = 0;
2292 dwarf_csect->file = 1;
2293 dwarf_csect->section = index;
2294 dwarf_csect->next = 0;
2295 /* set relocatable address at start of line program */
2296 saa_write8(plinep,DW_LNS_extended_op);
2297 saa_write8(plinep,9); /* operand length */
2298 saa_write8(plinep,DW_LNE_set_address);
2299 saa_write64(plinep,0); /* Start Address */
2300 /* if first entry */
2301 if (!dwarf_fsect)
2303 dwarf_fsect = dwarf_esect = dwarf_csect;
2304 dwarf_csect->last = 0;
2306 /* chain to previous entry */
2307 else
2309 dwarf_esect->next = dwarf_csect;
2310 dwarf_esect = dwarf_csect;
2315 #endif /* OF_ELF */