Elf: Drop never used SOC helper
[nasm.git] / output / outelf64.c
blob34478303cdf352730eb4e680f52a5adbf634d88f
1 /* ----------------------------------------------------------------------- *
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/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELF64
63 struct Reloc {
64 struct Reloc *next;
65 int64_t address; /* relative to _start_ of section */
66 int64_t symbol; /* symbol index */
67 int64_t offset; /* symbol addend */
68 int type; /* type of relocation */
71 struct Symbol {
72 struct rbtree symv; /* symbol value and rbtree of globals */
73 int32_t strpos; /* string table position of name */
74 int32_t section; /* section ID of the symbol */
75 int type; /* symbol type */
76 int other; /* symbol visibility */
77 int32_t size; /* size of symbol */
78 int32_t globnum; /* symbol table offset if global */
79 struct Symbol *nextfwd; /* list of unresolved-size symbols */
80 char *name; /* used temporarily if in above list */
83 struct Section {
84 struct SAA *data;
85 uint64_t len, size;
86 uint32_t nrelocs;
87 int32_t index; /* index into sects array */
88 int type; /* SHT_PROGBITS or SHT_NOBITS */
89 uint64_t align; /* alignment: power of two */
90 uint64_t flags; /* section flags */
91 char *name;
92 struct SAA *rel;
93 uint64_t rellen;
94 struct Reloc *head, **tail;
95 struct rbtree *gsyms; /* global symbols in section */
98 #define SECT_DELTA 32
99 static struct Section **sects;
100 static int nsects, sectlen;
102 #define SHSTR_DELTA 256
103 static char *shstrtab;
104 static int shstrtablen, shstrtabsize;
106 static struct SAA *syms;
107 static uint32_t nlocals, nglobs, ndebugs;
109 static int32_t def_seg;
111 static struct RAA *bsym;
113 static struct SAA *strs;
114 static uint32_t strslen;
116 static struct Symbol *fwds;
118 static char elf_module[FILENAME_MAX];
120 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
121 static uint8_t elf_abiver = 0; /* Current ABI version */
123 extern struct ofmt of_elf64;
125 static struct ELF_SECTDATA {
126 void *data;
127 int64_t len;
128 bool is_saa;
129 } *elf_sects;
130 static int elf_nsect, nsections;
131 static int64_t elf_foffs;
133 static void elf_write(void);
134 static void elf_sect_write(struct Section *, const void *, size_t);
135 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
136 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
137 int, int);
138 static void elf_write_sections(void);
139 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
140 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
141 static void add_sectname(char *, char *);
143 struct stabentry {
144 uint32_t n_strx;
145 uint8_t n_type;
146 uint8_t n_other;
147 uint16_t n_desc;
148 uint32_t n_value;
151 struct erel {
152 int offset, info;
155 struct symlininfo {
156 int offset;
157 int section; /* index into sects[] */
158 int segto; /* internal section number */
159 char *name; /* shallow-copied pointer of section name */
162 struct linelist {
163 struct symlininfo info;
164 int line;
165 char *filename;
166 struct linelist *next;
167 struct linelist *last;
170 struct sectlist {
171 struct SAA *psaa;
172 int section;
173 int line;
174 int offset;
175 int file;
176 struct sectlist *next;
177 struct sectlist *last;
180 /* common debug variables */
181 static int currentline = 1;
182 static int debug_immcall = 0;
184 /* stabs debug variables */
185 static struct linelist *stabslines = 0;
186 static int numlinestabs = 0;
187 static char *stabs_filename = 0;
188 static int symtabsection;
189 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
190 static int stablen, stabstrlen, stabrellen;
192 /* dwarf debug variables */
193 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
194 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
195 static int dwarf_numfiles = 0, dwarf_nsections;
196 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
197 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
198 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
199 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
200 abbrevlen, linelen, linerellen, framelen, loclen;
201 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
204 static struct dfmt df_dwarf;
205 static struct dfmt df_stabs;
206 static struct Symbol *lastsym;
208 /* common debugging routines */
209 static void debug64_typevalue(int32_t);
210 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
211 static void debug64_directive(const char *, const char *);
213 /* stabs debugging routines */
214 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
215 static void stabs64_output(int, void *);
216 static void stabs64_generate(void);
217 static void stabs64_cleanup(void);
219 /* dwarf debugging routines */
220 static void dwarf64_init(void);
221 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
222 static void dwarf64_output(int, void *);
223 static void dwarf64_generate(void);
224 static void dwarf64_cleanup(void);
225 static void dwarf64_findfile(const char *);
226 static void dwarf64_findsect(const int);
229 * Special section numbers which are used to define ELF special
230 * symbols, which can be used with WRT to provide PIC relocation
231 * types.
233 static int32_t elf_gotpc_sect, elf_gotoff_sect;
234 static int32_t elf_got_sect, elf_plt_sect;
235 static int32_t elf_sym_sect;
236 static int32_t elf_gottpoff_sect;
238 static void elf_init(void)
240 maxbits = 64;
241 sects = NULL;
242 nsects = sectlen = 0;
243 syms = saa_init((int32_t)sizeof(struct Symbol));
244 nlocals = nglobs = ndebugs = 0;
245 bsym = raa_init();
246 strs = saa_init(1L);
247 saa_wbytes(strs, "\0", 1L);
248 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
249 strslen = 2 + strlen(elf_module);
250 shstrtab = NULL;
251 shstrtablen = shstrtabsize = 0;;
252 add_sectname("", "");
254 fwds = NULL;
256 elf_gotpc_sect = seg_alloc();
257 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
258 elf_gotoff_sect = seg_alloc();
259 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
260 elf_got_sect = seg_alloc();
261 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
262 elf_plt_sect = seg_alloc();
263 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
264 elf_sym_sect = seg_alloc();
265 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
266 elf_gottpoff_sect = seg_alloc();
267 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
269 def_seg = seg_alloc();
273 static void elf_cleanup(int debuginfo)
275 struct Reloc *r;
276 int i;
278 (void)debuginfo;
280 elf_write();
281 for (i = 0; i < nsects; i++) {
282 if (sects[i]->type != SHT_NOBITS)
283 saa_free(sects[i]->data);
284 if (sects[i]->head)
285 saa_free(sects[i]->rel);
286 while (sects[i]->head) {
287 r = sects[i]->head;
288 sects[i]->head = sects[i]->head->next;
289 nasm_free(r);
292 nasm_free(sects);
293 saa_free(syms);
294 raa_free(bsym);
295 saa_free(strs);
296 if (of_elf64.current_dfmt) {
297 of_elf64.current_dfmt->cleanup();
301 /* add entry to the elf .shstrtab section */
302 static void add_sectname(char *firsthalf, char *secondhalf)
304 int len = strlen(firsthalf) + strlen(secondhalf);
305 while (shstrtablen + len + 1 > shstrtabsize)
306 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
307 strcpy(shstrtab + shstrtablen, firsthalf);
308 strcat(shstrtab + shstrtablen, secondhalf);
309 shstrtablen += len + 1;
312 static int elf_make_section(char *name, int type, int flags, int align)
314 struct Section *s;
316 s = nasm_malloc(sizeof(*s));
318 if (type != SHT_NOBITS)
319 s->data = saa_init(1L);
320 s->head = NULL;
321 s->tail = &s->head;
322 s->len = s->size = 0;
323 s->nrelocs = 0;
324 if (!strcmp(name, ".text"))
325 s->index = def_seg;
326 else
327 s->index = seg_alloc();
328 add_sectname("", name);
329 s->name = nasm_malloc(1 + strlen(name));
330 strcpy(s->name, name);
331 s->type = type;
332 s->flags = flags;
333 s->align = align;
334 s->gsyms = NULL;
336 if (nsects >= sectlen)
337 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
338 sects[nsects++] = s;
340 return nsects - 1;
343 static int32_t elf_section_names(char *name, int pass, int *bits)
345 char *p;
346 uint32_t flags, flags_and, flags_or;
347 uint64_t align;
348 int type, i;
351 * Default is 64 bits.
353 if (!name) {
354 *bits = 64;
355 return def_seg;
358 p = nasm_skip_word(name);
359 if (*p)
360 *p++ = '\0';
361 flags_and = flags_or = type = align = 0;
363 section_attrib(name, p, pass, &flags_and,
364 &flags_or, &align, &type);
366 if (!strcmp(name, ".shstrtab") ||
367 !strcmp(name, ".symtab") ||
368 !strcmp(name, ".strtab")) {
369 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
370 "name `%s'", name);
371 return NO_SEG;
374 for (i = 0; i < nsects; i++)
375 if (!strcmp(name, sects[i]->name))
376 break;
377 if (i == nsects) {
378 const struct elf_known_section *ks = elf_known_sections;
380 while (ks->name) {
381 if (!strcmp(name, ks->name))
382 break;
383 ks++;
386 type = type ? type : ks->type;
387 align = align ? align : ks->align;
388 flags = (ks->flags & ~flags_and) | flags_or;
390 i = elf_make_section(name, type, flags, align);
391 } else if (pass == 1) {
392 if ((type && sects[i]->type != type)
393 || (align && sects[i]->align != align)
394 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
395 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
396 " redeclaration of section `%s'", name);
399 return sects[i]->index;
402 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
403 int is_global, char *special)
405 int pos = strslen;
406 struct Symbol *sym;
407 bool special_used = false;
409 #if defined(DEBUG) && DEBUG>2
410 nasm_error(ERR_DEBUG,
411 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
412 name, segment, offset, is_global, special);
413 #endif
414 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
416 * This is a NASM special symbol. We never allow it into
417 * the ELF symbol table, even if it's a valid one. If it
418 * _isn't_ a valid one, we should barf immediately.
420 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
421 strcmp(name, "..got") && strcmp(name, "..plt") &&
422 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
423 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
424 return;
427 if (is_global == 3) {
428 struct Symbol **s;
430 * Fix up a forward-reference symbol size from the first
431 * pass.
433 for (s = &fwds; *s; s = &(*s)->nextfwd)
434 if (!strcmp((*s)->name, name)) {
435 struct tokenval tokval;
436 expr *e;
437 char *p = nasm_skip_spaces(nasm_skip_word(special));
439 stdscan_reset();
440 stdscan_set(p);
441 tokval.t_type = TOKEN_INVALID;
442 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
443 if (e) {
444 if (!is_simple(e))
445 nasm_error(ERR_NONFATAL, "cannot use relocatable"
446 " expression as symbol size");
447 else
448 (*s)->size = reloc_value(e);
452 * Remove it from the list of unresolved sizes.
454 nasm_free((*s)->name);
455 *s = (*s)->nextfwd;
456 return;
458 return; /* it wasn't an important one */
461 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
462 strslen += 1 + strlen(name);
464 lastsym = sym = saa_wstruct(syms);
466 memset(&sym->symv, 0, sizeof(struct rbtree));
468 sym->strpos = pos;
469 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
470 sym->other = STV_DEFAULT;
471 sym->size = 0;
472 if (segment == NO_SEG)
473 sym->section = SHN_ABS;
474 else {
475 int i;
476 sym->section = SHN_UNDEF;
477 if (segment == def_seg) {
478 /* we have to be sure at least text section is there */
479 int tempint;
480 if (segment != elf_section_names(".text", 2, &tempint))
481 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
483 for (i = 0; i < nsects; i++) {
484 if (segment == sects[i]->index) {
485 sym->section = i + 1;
486 break;
491 if (is_global == 2) {
492 sym->size = offset;
493 sym->symv.key = 0;
494 sym->section = SHN_COMMON;
496 * We have a common variable. Check the special text to see
497 * if it's a valid number and power of two; if so, store it
498 * as the alignment for the common variable.
500 if (special) {
501 bool err;
502 sym->symv.key = readnum(special, &err);
503 if (err)
504 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
505 " valid number", special);
506 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
507 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
508 " power of two", special);
510 special_used = true;
511 } else
512 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
514 if (sym->type == SYM_GLOBAL) {
516 * If sym->section == SHN_ABS, then the first line of the
517 * else section would cause a core dump, because its a reference
518 * beyond the end of the section array.
519 * This behaviour is exhibited by this code:
520 * GLOBAL crash_nasm
521 * crash_nasm equ 0
522 * To avoid such a crash, such requests are silently discarded.
523 * This may not be the best solution.
525 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
526 bsym = raa_write(bsym, segment, nglobs);
527 } else if (sym->section != SHN_ABS) {
529 * This is a global symbol; so we must add it to the rbtree
530 * of global symbols in its section.
532 * In addition, we check the special text for symbol
533 * type and size information.
535 sects[sym->section-1]->gsyms =
536 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
538 if (special) {
539 int n = strcspn(special, " \t");
541 if (!nasm_strnicmp(special, "function", n))
542 sym->type |= STT_FUNC;
543 else if (!nasm_strnicmp(special, "data", n) ||
544 !nasm_strnicmp(special, "object", n))
545 sym->type |= STT_OBJECT;
546 else if (!nasm_strnicmp(special, "notype", n))
547 sym->type |= STT_NOTYPE;
548 else
549 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
550 n, special);
551 special += n;
553 special = nasm_skip_spaces(special);
554 if (*special) {
555 n = strcspn(special, " \t");
556 if (!nasm_strnicmp(special, "default", n))
557 sym->other = STV_DEFAULT;
558 else if (!nasm_strnicmp(special, "internal", n))
559 sym->other = STV_INTERNAL;
560 else if (!nasm_strnicmp(special, "hidden", n))
561 sym->other = STV_HIDDEN;
562 else if (!nasm_strnicmp(special, "protected", n))
563 sym->other = STV_PROTECTED;
564 else
565 n = 0;
566 special += n;
569 if (*special) {
570 struct tokenval tokval;
571 expr *e;
572 int fwd = 0;
573 char *saveme = stdscan_get();
575 while (special[n] && nasm_isspace(special[n]))
576 n++;
578 * We have a size expression; attempt to
579 * evaluate it.
581 stdscan_reset();
582 stdscan_set(special + n);
583 tokval.t_type = TOKEN_INVALID;
584 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
585 NULL);
586 if (fwd) {
587 sym->nextfwd = fwds;
588 fwds = sym;
589 sym->name = nasm_strdup(name);
590 } else if (e) {
591 if (!is_simple(e))
592 nasm_error(ERR_NONFATAL, "cannot use relocatable"
593 " expression as symbol size");
594 else
595 sym->size = reloc_value(e);
597 stdscan_set(saveme);
599 special_used = true;
602 * If TLS segment, mark symbol accordingly.
604 if (sects[sym->section - 1]->flags & SHF_TLS) {
605 sym->type &= 0xf0;
606 sym->type |= STT_TLS;
609 sym->globnum = nglobs;
610 nglobs++;
611 } else
612 nlocals++;
614 if (special && !special_used)
615 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
618 static void elf_add_reloc(struct Section *sect, int32_t segment,
619 int64_t offset, int type)
621 struct Reloc *r;
622 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
623 sect->tail = &r->next;
624 r->next = NULL;
626 r->address = sect->len;
627 r->offset = offset;
628 if (segment == NO_SEG)
629 r->symbol = 0;
630 else {
631 int i;
632 r->symbol = 0;
633 for (i = 0; i < nsects; i++)
634 if (segment == sects[i]->index)
635 r->symbol = i + 2;
636 if (!r->symbol)
637 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
639 r->type = type;
641 sect->nrelocs++;
645 * This routine deals with ..got and ..sym relocations: the more
646 * complicated kinds. In shared-library writing, some relocations
647 * with respect to global symbols must refer to the precise symbol
648 * rather than referring to an offset from the base of the section
649 * _containing_ the symbol. Such relocations call to this routine,
650 * which searches the symbol list for the symbol in question.
652 * R_386_GOT32 references require the _exact_ symbol address to be
653 * used; R_386_32 references can be at an offset from the symbol.
654 * The boolean argument `exact' tells us this.
656 * Return value is the adjusted value of `addr', having become an
657 * offset from the symbol rather than the section. Should always be
658 * zero when returning from an exact call.
660 * Limitation: if you define two symbols at the same place,
661 * confusion will occur.
663 * Inefficiency: we search, currently, using a linked list which
664 * isn't even necessarily sorted.
666 static void elf_add_gsym_reloc(struct Section *sect,
667 int32_t segment, uint64_t offset, int64_t pcrel,
668 int type, bool exact)
670 struct Reloc *r;
671 struct Section *s;
672 struct Symbol *sym;
673 struct rbtree *srb;
674 int i;
677 * First look up the segment/offset pair and find a global
678 * symbol corresponding to it. If it's not one of our segments,
679 * then it must be an external symbol, in which case we're fine
680 * doing a normal elf_add_reloc after first sanity-checking
681 * that the offset from the symbol is zero.
683 s = NULL;
684 for (i = 0; i < nsects; i++)
685 if (segment == sects[i]->index) {
686 s = sects[i];
687 break;
690 if (!s) {
691 if (exact && offset)
692 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
693 else
694 elf_add_reloc(sect, segment, offset - pcrel, type);
695 return;
698 srb = rb_search(s->gsyms, offset);
699 if (!srb || (exact && srb->key != offset)) {
700 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
701 " for this reference");
702 return;
704 sym = container_of(srb, struct Symbol, symv);
706 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
707 sect->tail = &r->next;
708 r->next = NULL;
710 r->address = sect->len;
711 r->offset = offset - pcrel - sym->symv.key;
712 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
713 r->type = type;
715 sect->nrelocs++;
718 static void elf_out(int32_t segto, const void *data,
719 enum out_type type, uint64_t size,
720 int32_t segment, int32_t wrt)
722 struct Section *s;
723 int64_t addr, zero;
724 int i;
725 static struct symlininfo sinfo;
727 zero = 0;
729 #if defined(DEBUG) && DEBUG>2
730 if (data)
731 nasm_error(ERR_DEBUG,
732 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
733 currentline, type, segment, segto, size, *(int64_t *)data);
734 else
735 nasm_error(ERR_DEBUG,
736 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
737 currentline, type, segment, segto, size);
738 #endif
741 * handle absolute-assembly (structure definitions)
743 if (segto == NO_SEG) {
744 if (type != OUT_RESERVE)
745 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
746 " space");
747 return;
750 s = NULL;
751 for (i = 0; i < nsects; i++)
752 if (segto == sects[i]->index) {
753 s = sects[i];
754 break;
756 if (!s) {
757 int tempint; /* ignored */
758 if (segto != elf_section_names(".text", 2, &tempint))
759 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
760 else {
761 s = sects[nsects - 1];
762 i = nsects - 1;
765 /* invoke current debug_output routine */
766 if (of_elf64.current_dfmt) {
767 sinfo.offset = s->len;
768 sinfo.section = i;
769 sinfo.segto = segto;
770 sinfo.name = s->name;
771 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
773 /* end of debugging stuff */
775 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
776 nasm_error(ERR_WARNING, "attempt to initialize memory in"
777 " BSS section `%s': ignored", s->name);
778 s->len += realsize(type, size);
779 return;
782 if (type == OUT_RESERVE) {
783 if (s->type == SHT_PROGBITS) {
784 nasm_error(ERR_WARNING, "uninitialized space declared in"
785 " non-BSS section `%s': zeroing", s->name);
786 elf_sect_write(s, NULL, size);
787 } else
788 s->len += size;
789 } else if (type == OUT_RAWDATA) {
790 if (segment != NO_SEG)
791 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
792 elf_sect_write(s, data, size);
793 } else if (type == OUT_ADDRESS) {
794 addr = *(int64_t *)data;
795 if (segment == NO_SEG) {
796 /* Do nothing */
797 } else if (segment % 2) {
798 nasm_error(ERR_NONFATAL, "ELF format does not support"
799 " segment base references");
800 } else {
801 if (wrt == NO_SEG) {
802 switch ((int)size) {
803 case 1:
804 elf_add_reloc(s, segment, addr, R_X86_64_8);
805 break;
806 case 2:
807 elf_add_reloc(s, segment, addr, R_X86_64_16);
808 break;
809 case 4:
810 elf_add_reloc(s, segment, addr, R_X86_64_32);
811 break;
812 case 8:
813 elf_add_reloc(s, segment, addr, R_X86_64_64);
814 break;
815 default:
816 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
817 break;
819 addr = 0;
820 } else if (wrt == elf_gotpc_sect + 1) {
822 * The user will supply GOT relative to $$. ELF
823 * will let us have GOT relative to $. So we
824 * need to fix up the data item by $-$$.
826 addr += s->len;
827 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
828 addr = 0;
829 } else if (wrt == elf_gotoff_sect + 1) {
830 if (size != 8) {
831 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
832 "references to be qword");
833 } else {
834 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
835 addr = 0;
837 } else if (wrt == elf_got_sect + 1) {
838 switch ((int)size) {
839 case 4:
840 elf_add_gsym_reloc(s, segment, addr, 0,
841 R_X86_64_GOT32, true);
842 addr = 0;
843 break;
844 case 8:
845 elf_add_gsym_reloc(s, segment, addr, 0,
846 R_X86_64_GOT64, true);
847 addr = 0;
848 break;
849 default:
850 nasm_error(ERR_NONFATAL, "invalid ..got reference");
851 break;
853 } else if (wrt == elf_sym_sect + 1) {
854 switch ((int)size) {
855 case 1:
856 elf_add_gsym_reloc(s, segment, addr, 0,
857 R_X86_64_8, false);
858 addr = 0;
859 break;
860 case 2:
861 elf_add_gsym_reloc(s, segment, addr, 0,
862 R_X86_64_16, false);
863 addr = 0;
864 break;
865 case 4:
866 elf_add_gsym_reloc(s, segment, addr, 0,
867 R_X86_64_32, false);
868 addr = 0;
869 break;
870 case 8:
871 elf_add_gsym_reloc(s, segment, addr, 0,
872 R_X86_64_64, false);
873 addr = 0;
874 break;
875 default:
876 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
877 break;
879 } else if (wrt == elf_plt_sect + 1) {
880 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
881 "relative PLT references");
882 } else {
883 nasm_error(ERR_NONFATAL, "ELF format does not support this"
884 " use of WRT");
887 elf_sect_writeaddr(s, addr, size);
888 } else if (type == OUT_REL2ADR) {
889 addr = *(int64_t *)data - size;
890 if (segment == segto)
891 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
892 if (segment == NO_SEG) {
893 /* Do nothing */
894 } else if (segment % 2) {
895 nasm_error(ERR_NONFATAL, "ELF format does not support"
896 " segment base references");
897 } else {
898 if (wrt == NO_SEG) {
899 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
900 addr = 0;
901 } else {
902 nasm_error(ERR_NONFATAL,
903 "Unsupported non-32-bit ELF relocation [2]");
906 elf_sect_writeaddr(s, addr, 2);
907 } else if (type == OUT_REL4ADR) {
908 addr = *(int64_t *)data - size;
909 if (segment == segto)
910 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
911 if (segment == NO_SEG) {
912 /* Do nothing */
913 } else if (segment % 2) {
914 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
915 " segment base references");
916 } else {
917 if (wrt == NO_SEG) {
918 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
919 addr = 0;
920 } else if (wrt == elf_plt_sect + 1) {
921 elf_add_gsym_reloc(s, segment, addr+size, size,
922 R_X86_64_PLT32, true);
923 addr = 0;
924 } else if (wrt == elf_gotpc_sect + 1 ||
925 wrt == elf_got_sect + 1) {
926 elf_add_gsym_reloc(s, segment, addr+size, size,
927 R_X86_64_GOTPCREL, true);
928 addr = 0;
929 } else if (wrt == elf_gotoff_sect + 1 ||
930 wrt == elf_got_sect + 1) {
931 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
932 "qword absolute");
933 } else if (wrt == elf_gottpoff_sect + 1) {
934 elf_add_gsym_reloc(s, segment, addr+size, size,
935 R_X86_64_GOTTPOFF, true);
936 addr = 0;
937 } else {
938 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
939 " use of WRT");
942 elf_sect_writeaddr(s, addr, 4);
943 } else if (type == OUT_REL8ADR) {
944 addr = *(int64_t *)data - size;
945 if (segment == segto)
946 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
947 if (segment == NO_SEG) {
948 /* Do nothing */
949 } else if (segment % 2) {
950 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
951 " segment base references");
952 } else {
953 if (wrt == NO_SEG) {
954 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
955 addr = 0;
956 } else if (wrt == elf_gotpc_sect + 1 ||
957 wrt == elf_got_sect + 1) {
958 elf_add_gsym_reloc(s, segment, addr+size, size,
959 R_X86_64_GOTPCREL64, true);
960 addr = 0;
961 } else if (wrt == elf_gotoff_sect + 1 ||
962 wrt == elf_got_sect + 1) {
963 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
964 "absolute");
965 } else if (wrt == elf_gottpoff_sect + 1) {
966 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
967 "dword");
968 } else {
969 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
970 " use of WRT");
973 elf_sect_writeaddr(s, addr, 8);
977 static void elf_write(void)
979 int align;
980 char *p;
981 int i;
983 struct SAA *symtab;
984 int32_t symtablen, symtablocal;
987 * Work out how many sections we will have. We have SHN_UNDEF,
988 * then the flexible user sections, then the fixed sections
989 * `.shstrtab', `.symtab' and `.strtab', then optionally
990 * relocation sections for the user sections.
992 nsections = sec_numspecial + 1;
993 if (of_elf64.current_dfmt == &df_stabs)
994 nsections += 3;
995 else if (of_elf64.current_dfmt == &df_dwarf)
996 nsections += 10;
998 add_sectname("", ".shstrtab");
999 add_sectname("", ".symtab");
1000 add_sectname("", ".strtab");
1001 for (i = 0; i < nsects; i++) {
1002 nsections++; /* for the section itself */
1003 if (sects[i]->head) {
1004 nsections++; /* for its relocations */
1005 add_sectname(".rela", sects[i]->name);
1009 if (of_elf64.current_dfmt == &df_stabs) {
1010 /* in case the debug information is wanted, just add these three sections... */
1011 add_sectname("", ".stab");
1012 add_sectname("", ".stabstr");
1013 add_sectname(".rel", ".stab");
1016 else if (of_elf64.current_dfmt == &df_dwarf) {
1017 /* the dwarf debug standard specifies the following ten sections,
1018 not all of which are currently implemented,
1019 although all of them are defined. */
1020 #define debug_aranges (int64_t) (nsections-10)
1021 #define debug_info (int64_t) (nsections-7)
1022 #define debug_abbrev (int64_t) (nsections-5)
1023 #define debug_line (int64_t) (nsections-4)
1024 add_sectname("", ".debug_aranges");
1025 add_sectname(".rela", ".debug_aranges");
1026 add_sectname("", ".debug_pubnames");
1027 add_sectname("", ".debug_info");
1028 add_sectname(".rela", ".debug_info");
1029 add_sectname("", ".debug_abbrev");
1030 add_sectname("", ".debug_line");
1031 add_sectname(".rela", ".debug_line");
1032 add_sectname("", ".debug_frame");
1033 add_sectname("", ".debug_loc");
1037 * Output the ELF header.
1039 fwrite("\177ELF\2\1\1", 7, 1, ofile);
1040 fputc(elf_osabi, ofile);
1041 fputc(elf_abiver, ofile);
1042 fwritezero(7, ofile);
1043 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1044 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1045 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1046 fwriteint64_t(0L, ofile); /* no entry point */
1047 fwriteint64_t(0L, ofile); /* no program header table */
1048 fwriteint64_t(0x40L, ofile); /* section headers straight after
1049 * ELF header plus alignment */
1050 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1051 fwriteint16_t(0x40, ofile); /* size of ELF header */
1052 fwriteint16_t(0, ofile); /* no program header table, again */
1053 fwriteint16_t(0, ofile); /* still no program header table */
1054 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1055 fwriteint16_t(nsections, ofile); /* number of sections */
1056 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1057 * section header table */
1060 * Build the symbol table and relocation tables.
1062 symtab = elf_build_symtab(&symtablen, &symtablocal);
1063 for (i = 0; i < nsects; i++)
1064 if (sects[i]->head)
1065 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1066 sects[i]->head);
1069 * Now output the section header table.
1072 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1073 align = ALIGN(elf_foffs, SEG_ALIGN) - elf_foffs;
1074 elf_foffs += align;
1075 elf_nsect = 0;
1076 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1078 /* SHN_UNDEF */
1079 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1080 p = shstrtab + 1;
1082 /* The normal sections */
1083 for (i = 0; i < nsects; i++) {
1084 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1085 (sects[i]->type == SHT_PROGBITS ?
1086 sects[i]->data : NULL), true,
1087 sects[i]->len, 0, 0, sects[i]->align, 0);
1088 p += strlen(p) + 1;
1091 /* .shstrtab */
1092 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1093 shstrtablen, 0, 0, 1, 0);
1094 p += strlen(p) + 1;
1096 /* .symtab */
1097 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1098 symtablen, sec_strtab, symtablocal, 4, 24);
1099 p += strlen(p) + 1;
1101 /* .strtab */
1102 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1103 strslen, 0, 0, 1, 0);
1104 p += strlen(p) + 1;
1106 /* The relocation sections */
1107 for (i = 0; i < nsects; i++)
1108 if (sects[i]->head) {
1109 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1110 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1111 p += strlen(p) + 1;
1114 if (of_elf64.current_dfmt == &df_stabs) {
1115 /* for debugging information, create the last three sections
1116 which are the .stab , .stabstr and .rel.stab sections respectively */
1118 /* this function call creates the stab sections in memory */
1119 stabs64_generate();
1121 if (stabbuf && stabstrbuf && stabrelbuf) {
1122 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1123 stablen, sec_stabstr, 0, 4, 12);
1124 p += strlen(p) + 1;
1126 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1127 stabstrlen, 0, 0, 4, 0);
1128 p += strlen(p) + 1;
1130 /* link -> symtable info -> section to refer to */
1131 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1132 stabrellen, symtabsection, sec_stab, 4, 16);
1133 p += strlen(p) + 1;
1136 else if (of_elf64.current_dfmt == &df_dwarf) {
1137 /* for dwarf debugging information, create the ten dwarf sections */
1139 /* this function call creates the dwarf sections in memory */
1140 if (dwarf_fsect)
1141 dwarf64_generate();
1143 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1144 arangeslen, 0, 0, 1, 0);
1145 p += strlen(p) + 1;
1147 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1148 arangesrellen, symtabsection, debug_aranges, 1, 24);
1149 p += strlen(p) + 1;
1151 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1152 pubnameslen, 0, 0, 1, 0);
1153 p += strlen(p) + 1;
1155 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1156 infolen, 0, 0, 1, 0);
1157 p += strlen(p) + 1;
1159 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1160 inforellen, symtabsection, debug_info, 1, 24);
1161 p += strlen(p) + 1;
1163 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1164 abbrevlen, 0, 0, 1, 0);
1165 p += strlen(p) + 1;
1167 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1168 linelen, 0, 0, 1, 0);
1169 p += strlen(p) + 1;
1171 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1172 linerellen, symtabsection, debug_line, 1, 24);
1173 p += strlen(p) + 1;
1175 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1176 framelen, 0, 0, 8, 0);
1177 p += strlen(p) + 1;
1179 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1180 loclen, 0, 0, 1, 0);
1181 p += strlen(p) + 1;
1183 fwritezero(align, ofile);
1186 * Now output the sections.
1188 elf_write_sections();
1190 nasm_free(elf_sects);
1191 saa_free(symtab);
1194 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1196 struct SAA *s = saa_init(1L);
1197 struct Symbol *sym;
1198 uint8_t entry[24], *p;
1199 int i;
1201 *len = *local = 0;
1204 * First, an all-zeros entry, required by the ELF spec.
1206 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1207 *len += 24;
1208 (*local)++;
1211 * Next, an entry for the file name.
1213 p = entry;
1214 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1215 WRITESHORT(p, STT_FILE); /* type FILE */
1216 WRITESHORT(p, SHN_ABS);
1217 WRITEDLONG(p, (uint64_t) 0); /* no value */
1218 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1219 saa_wbytes(s, entry, 24L);
1220 *len += 24;
1221 (*local)++;
1224 * Now some standard symbols defining the segments, for relocation
1225 * purposes.
1227 for (i = 1; i <= nsects; i++) {
1228 p = entry;
1229 WRITELONG(p, 0); /* no symbol name */
1230 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1231 WRITESHORT(p, i); /* section id */
1232 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1233 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1234 saa_wbytes(s, entry, 24L);
1235 *len += 24;
1236 (*local)++;
1241 * Now the other local symbols.
1243 saa_rewind(syms);
1244 while ((sym = saa_rstruct(syms))) {
1245 if (sym->type & SYM_GLOBAL)
1246 continue;
1247 p = entry;
1248 WRITELONG(p, sym->strpos); /* index into symbol string table */
1249 WRITECHAR(p, sym->type); /* type and binding */
1250 WRITECHAR(p, sym->other); /* visibility */
1251 WRITESHORT(p, sym->section); /* index into section header table */
1252 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1253 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1254 saa_wbytes(s, entry, 24L);
1255 *len += 24;
1256 (*local)++;
1259 * dwarf needs symbols for debug sections
1260 * which are relocation targets.
1262 if (of_elf64.current_dfmt == &df_dwarf) {
1263 dwarf_infosym = *local;
1264 p = entry;
1265 WRITELONG(p, 0); /* no symbol name */
1266 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1267 WRITESHORT(p, debug_info); /* section id */
1268 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1269 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1270 saa_wbytes(s, entry, 24L);
1271 *len += 24;
1272 (*local)++;
1273 dwarf_abbrevsym = *local;
1274 p = entry;
1275 WRITELONG(p, 0); /* no symbol name */
1276 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1277 WRITESHORT(p, debug_abbrev); /* section id */
1278 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1279 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1280 saa_wbytes(s, entry, 24L);
1281 *len += 24;
1282 (*local)++;
1283 dwarf_linesym = *local;
1284 p = entry;
1285 WRITELONG(p, 0); /* no symbol name */
1286 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1287 WRITESHORT(p, debug_line); /* section id */
1288 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1289 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1290 saa_wbytes(s, entry, 24L);
1291 *len += 24;
1292 (*local)++;
1296 * Now the global symbols.
1298 saa_rewind(syms);
1299 while ((sym = saa_rstruct(syms))) {
1300 if (!(sym->type & SYM_GLOBAL))
1301 continue;
1302 p = entry;
1303 WRITELONG(p, sym->strpos);
1304 WRITECHAR(p, sym->type); /* type and binding */
1305 WRITECHAR(p, sym->other); /* visibility */
1306 WRITESHORT(p, sym->section);
1307 WRITEDLONG(p, (int64_t)sym->symv.key);
1308 WRITEDLONG(p, (int64_t)sym->size);
1309 saa_wbytes(s, entry, 24L);
1310 *len += 24;
1313 return s;
1316 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1318 struct SAA *s;
1319 uint8_t *p, entry[24];
1320 int32_t global_offset;
1322 if (!r)
1323 return NULL;
1325 s = saa_init(1L);
1326 *len = 0;
1329 * How to onvert from a global placeholder to a real symbol index;
1330 * the +2 refers to the two special entries, the null entry and
1331 * the filename entry.
1333 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1335 while (r) {
1336 int32_t sym = r->symbol;
1338 if (sym >= GLOBAL_TEMP_BASE)
1339 sym += global_offset;
1341 p = entry;
1342 WRITEDLONG(p, r->address);
1343 WRITELONG(p, r->type);
1344 WRITELONG(p, sym);
1345 WRITEDLONG(p, r->offset);
1346 saa_wbytes(s, entry, 24L);
1347 *len += 24;
1349 r = r->next;
1352 return s;
1355 static void elf_section_header(int name, int type, uint64_t flags,
1356 void *data, bool is_saa, uint64_t datalen,
1357 int link, int info, int align, int eltsize)
1359 elf_sects[elf_nsect].data = data;
1360 elf_sects[elf_nsect].len = datalen;
1361 elf_sects[elf_nsect].is_saa = is_saa;
1362 elf_nsect++;
1364 fwriteint32_t((int32_t)name, ofile);
1365 fwriteint32_t((int32_t)type, ofile);
1366 fwriteint64_t((int64_t)flags, ofile);
1367 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1368 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1369 fwriteint64_t(datalen, ofile);
1370 if (data)
1371 elf_foffs += ALIGN(datalen, SEG_ALIGN);
1372 fwriteint32_t((int32_t)link, ofile);
1373 fwriteint32_t((int32_t)info, ofile);
1374 fwriteint64_t((int64_t)align, ofile);
1375 fwriteint64_t((int64_t)eltsize, ofile);
1378 static void elf_write_sections(void)
1380 int i;
1381 for (i = 0; i < elf_nsect; i++)
1382 if (elf_sects[i].data) {
1383 int32_t len = elf_sects[i].len;
1384 int32_t reallen = ALIGN(len, SEG_ALIGN);
1385 int32_t align = reallen - len;
1386 if (elf_sects[i].is_saa)
1387 saa_fpwrite(elf_sects[i].data, ofile);
1388 else
1389 fwrite(elf_sects[i].data, len, 1, ofile);
1390 fwritezero(align, ofile);
1394 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1396 saa_wbytes(sect->data, data, len);
1397 sect->len += len;
1399 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1401 saa_writeaddr(sect->data, data, len);
1402 sect->len += len;
1405 static int32_t elf_segbase(int32_t segment)
1407 return segment;
1410 static int elf_directive(enum directives directive, char *value, int pass)
1412 bool err;
1413 int64_t n;
1414 char *p;
1416 switch (directive) {
1417 case D_OSABI:
1418 if (pass == 2)
1419 return 1; /* ignore in pass 2 */
1421 n = readnum(value, &err);
1422 if (err) {
1423 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1424 return 1;
1426 if (n < 0 || n > 255) {
1427 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1428 return 1;
1430 elf_osabi = n;
1431 elf_abiver = 0;
1433 if ((p = strchr(value,',')) == NULL)
1434 return 1;
1436 n = readnum(p+1, &err);
1437 if (err || n < 0 || n > 255) {
1438 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1439 return 1;
1442 elf_abiver = n;
1443 return 1;
1445 default:
1446 return 0;
1450 static void elf_filename(char *inname, char *outname)
1452 strcpy(elf_module, inname);
1453 standard_extension(inname, outname, ".o");
1456 extern macros_t elf_stdmac[];
1458 static int elf_set_info(enum geninfo type, char **val)
1460 (void)type;
1461 (void)val;
1462 return 0;
1464 static struct dfmt df_dwarf = {
1465 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1466 "dwarf",
1467 dwarf64_init,
1468 dwarf64_linenum,
1469 debug64_deflabel,
1470 debug64_directive,
1471 debug64_typevalue,
1472 dwarf64_output,
1473 dwarf64_cleanup
1475 static struct dfmt df_stabs = {
1476 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1477 "stabs",
1478 null_debug_init,
1479 stabs64_linenum,
1480 debug64_deflabel,
1481 debug64_directive,
1482 debug64_typevalue,
1483 stabs64_output,
1484 stabs64_cleanup
1487 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1489 struct ofmt of_elf64 = {
1490 "ELF64 (x86_64) object files (e.g. Linux)",
1491 "elf64",
1493 elf64_debugs_arr,
1494 &df_stabs,
1495 elf_stdmac,
1496 elf_init,
1497 elf_set_info,
1498 elf_out,
1499 elf_deflabel,
1500 elf_section_names,
1501 elf_segbase,
1502 elf_directive,
1503 elf_filename,
1504 elf_cleanup
1507 /* common debugging routines */
1508 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1509 int is_global, char *special)
1511 (void)name;
1512 (void)segment;
1513 (void)offset;
1514 (void)is_global;
1515 (void)special;
1518 static void debug64_directive(const char *directive, const char *params)
1520 (void)directive;
1521 (void)params;
1524 static void debug64_typevalue(int32_t type)
1526 int32_t stype, ssize;
1527 switch (TYM_TYPE(type)) {
1528 case TY_LABEL:
1529 ssize = 0;
1530 stype = STT_NOTYPE;
1531 break;
1532 case TY_BYTE:
1533 ssize = 1;
1534 stype = STT_OBJECT;
1535 break;
1536 case TY_WORD:
1537 ssize = 2;
1538 stype = STT_OBJECT;
1539 break;
1540 case TY_DWORD:
1541 ssize = 4;
1542 stype = STT_OBJECT;
1543 break;
1544 case TY_FLOAT:
1545 ssize = 4;
1546 stype = STT_OBJECT;
1547 break;
1548 case TY_QWORD:
1549 ssize = 8;
1550 stype = STT_OBJECT;
1551 break;
1552 case TY_TBYTE:
1553 ssize = 10;
1554 stype = STT_OBJECT;
1555 break;
1556 case TY_OWORD:
1557 ssize = 16;
1558 stype = STT_OBJECT;
1559 break;
1560 case TY_YWORD:
1561 ssize = 32;
1562 stype = STT_OBJECT;
1563 break;
1564 case TY_COMMON:
1565 ssize = 0;
1566 stype = STT_COMMON;
1567 break;
1568 case TY_SEG:
1569 ssize = 0;
1570 stype = STT_SECTION;
1571 break;
1572 case TY_EXTERN:
1573 ssize = 0;
1574 stype = STT_NOTYPE;
1575 break;
1576 case TY_EQU:
1577 ssize = 0;
1578 stype = STT_NOTYPE;
1579 break;
1580 default:
1581 ssize = 0;
1582 stype = STT_NOTYPE;
1583 break;
1585 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1586 lastsym->size = ssize;
1587 lastsym->type = stype;
1591 /* stabs debugging routines */
1593 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1595 (void)segto;
1596 if (!stabs_filename) {
1597 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1598 strcpy(stabs_filename, filename);
1599 } else {
1600 if (strcmp(stabs_filename, filename)) {
1601 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1602 in fact, this leak comes in quite handy to maintain a list of files
1603 encountered so far in the symbol lines... */
1605 /* why not nasm_free(stabs_filename); we're done with the old one */
1607 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1608 strcpy(stabs_filename, filename);
1611 debug_immcall = 1;
1612 currentline = linenumber;
1616 static void stabs64_output(int type, void *param)
1618 struct symlininfo *s;
1619 struct linelist *el;
1620 if (type == TY_DEBUGSYMLIN) {
1621 if (debug_immcall) {
1622 s = (struct symlininfo *)param;
1623 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1624 return; /* line info is only collected for executable sections */
1625 numlinestabs++;
1626 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1627 el->info.offset = s->offset;
1628 el->info.section = s->section;
1629 el->info.name = s->name;
1630 el->line = currentline;
1631 el->filename = stabs_filename;
1632 el->next = 0;
1633 if (stabslines) {
1634 stabslines->last->next = el;
1635 stabslines->last = el;
1636 } else {
1637 stabslines = el;
1638 stabslines->last = el;
1642 debug_immcall = 0;
1645 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1646 do { \
1647 WRITELONG(p,n_strx); \
1648 WRITECHAR(p,n_type); \
1649 WRITECHAR(p,n_other); \
1650 WRITESHORT(p,n_desc); \
1651 WRITELONG(p,n_value); \
1652 } while (0)
1654 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1656 static void stabs64_generate(void)
1658 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1659 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1660 char **allfiles;
1661 int *fileidx;
1663 struct linelist *ptr;
1665 ptr = stabslines;
1667 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1668 for (i = 0; i < numlinestabs; i++)
1669 allfiles[i] = 0;
1670 numfiles = 0;
1671 while (ptr) {
1672 if (numfiles == 0) {
1673 allfiles[0] = ptr->filename;
1674 numfiles++;
1675 } else {
1676 for (i = 0; i < numfiles; i++) {
1677 if (!strcmp(allfiles[i], ptr->filename))
1678 break;
1680 if (i >= numfiles) {
1681 allfiles[i] = ptr->filename;
1682 numfiles++;
1685 ptr = ptr->next;
1687 strsize = 1;
1688 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1689 for (i = 0; i < numfiles; i++) {
1690 fileidx[i] = strsize;
1691 strsize += strlen(allfiles[i]) + 1;
1693 mainfileindex = 0;
1694 for (i = 0; i < numfiles; i++) {
1695 if (!strcmp(allfiles[i], elf_module)) {
1696 mainfileindex = i;
1697 break;
1702 * worst case size of the stab buffer would be:
1703 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1704 * plus one "ending" entry
1706 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1707 sizeof(struct stabentry));
1708 ssbuf = (uint8_t *)nasm_malloc(strsize);
1709 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1710 rptr = rbuf;
1712 for (i = 0; i < numfiles; i++)
1713 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1714 ssbuf[0] = 0;
1716 stabstrlen = strsize; /* set global variable for length of stab strings */
1718 sptr = sbuf;
1719 ptr = stabslines;
1720 numstabs = 0;
1722 if (ptr) {
1724 * this is the first stab, its strx points to the filename of the
1725 * the source-file, the n_desc field should be set to the number
1726 * of remaining stabs
1728 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1730 /* this is the stab for the main source file */
1731 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1733 /* relocation table entry */
1736 * Since the symbol table has two entries before
1737 * the section symbols, the index in the info.section
1738 * member must be adjusted by adding 2
1741 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1742 WRITELONG(rptr, R_X86_64_32);
1743 WRITELONG(rptr, ptr->info.section + 2);
1745 numstabs++;
1746 currfile = mainfileindex;
1749 while (ptr) {
1750 if (strcmp(allfiles[currfile], ptr->filename)) {
1751 /* oops file has changed... */
1752 for (i = 0; i < numfiles; i++)
1753 if (!strcmp(allfiles[i], ptr->filename))
1754 break;
1755 currfile = i;
1756 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1757 ptr->info.offset);
1758 numstabs++;
1760 /* relocation table entry */
1762 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1763 WRITELONG(rptr, R_X86_64_32);
1764 WRITELONG(rptr, ptr->info.section + 2);
1767 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1768 numstabs++;
1770 /* relocation table entry */
1772 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1773 WRITELONG(rptr, R_X86_64_32);
1774 WRITELONG(rptr, ptr->info.section + 2);
1776 ptr = ptr->next;
1780 /* this is an "ending" token */
1781 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1782 numstabs++;
1784 ((struct stabentry *)sbuf)->n_desc = numstabs;
1786 nasm_free(allfiles);
1787 nasm_free(fileidx);
1789 stablen = (sptr - sbuf);
1790 stabrellen = (rptr - rbuf);
1791 stabrelbuf = rbuf;
1792 stabbuf = sbuf;
1793 stabstrbuf = ssbuf;
1796 static void stabs64_cleanup(void)
1798 struct linelist *ptr, *del;
1799 if (!stabslines)
1800 return;
1802 ptr = stabslines;
1803 while (ptr) {
1804 del = ptr;
1805 ptr = ptr->next;
1806 nasm_free(del);
1809 nasm_free(stabbuf);
1810 nasm_free(stabrelbuf);
1811 nasm_free(stabstrbuf);
1814 /* dwarf routines */
1816 static void dwarf64_init(void)
1818 ndebugs = 3; /* 3 debug symbols */
1821 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1822 int32_t segto)
1824 (void)segto;
1825 dwarf64_findfile(filename);
1826 debug_immcall = 1;
1827 currentline = linenumber;
1830 /* called from elf_out with type == TY_DEBUGSYMLIN */
1831 static void dwarf64_output(int type, void *param)
1833 int ln, aa, inx, maxln, soc;
1834 struct symlininfo *s;
1835 struct SAA *plinep;
1837 (void)type;
1839 s = (struct symlininfo *)param;
1841 /* line number info is only gathered for executable sections */
1842 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1843 return;
1845 /* Check if section index has changed */
1846 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1847 dwarf64_findsect(s->section);
1849 /* do nothing unless line or file has changed */
1850 if (!debug_immcall)
1851 return;
1853 ln = currentline - dwarf_csect->line;
1854 aa = s->offset - dwarf_csect->offset;
1855 inx = dwarf_clist->line;
1856 plinep = dwarf_csect->psaa;
1857 /* check for file change */
1858 if (!(inx == dwarf_csect->file)) {
1859 saa_write8(plinep,DW_LNS_set_file);
1860 saa_write8(plinep,inx);
1861 dwarf_csect->file = inx;
1863 /* check for line change */
1864 if (ln) {
1865 /* test if in range of special op code */
1866 maxln = line_base + line_range;
1867 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1868 if (ln >= line_base && ln < maxln && soc < 256) {
1869 saa_write8(plinep,soc);
1870 } else {
1871 saa_write8(plinep,DW_LNS_advance_line);
1872 saa_wleb128s(plinep,ln);
1873 if (aa) {
1874 saa_write8(plinep,DW_LNS_advance_pc);
1875 saa_wleb128u(plinep,aa);
1878 dwarf_csect->line = currentline;
1879 dwarf_csect->offset = s->offset;
1882 /* show change handled */
1883 debug_immcall = 0;
1887 static void dwarf64_generate(void)
1889 uint8_t *pbuf;
1890 int indx;
1891 struct linelist *ftentry;
1892 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1893 struct SAA *parangesrel, *plinesrel, *pinforel;
1894 struct sectlist *psect;
1895 size_t saalen, linepoff, totlen, highaddr;
1897 /* write epilogues for each line program range */
1898 /* and build aranges section */
1899 paranges = saa_init(1L);
1900 parangesrel = saa_init(1L);
1901 saa_write16(paranges,3); /* dwarf version */
1902 saa_write64(parangesrel, paranges->datalen+4);
1903 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1904 saa_write64(parangesrel, 0);
1905 saa_write32(paranges,0); /* offset into info */
1906 saa_write8(paranges,8); /* pointer size */
1907 saa_write8(paranges,0); /* not segmented */
1908 saa_write32(paranges,0); /* padding */
1909 /* iterate though sectlist entries */
1910 psect = dwarf_fsect;
1911 totlen = 0;
1912 highaddr = 0;
1913 for (indx = 0; indx < dwarf_nsections; indx++)
1915 plinep = psect->psaa;
1916 /* Line Number Program Epilogue */
1917 saa_write8(plinep,2); /* std op 2 */
1918 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1919 saa_write8(plinep,DW_LNS_extended_op);
1920 saa_write8(plinep,1); /* operand length */
1921 saa_write8(plinep,DW_LNE_end_sequence);
1922 totlen += plinep->datalen;
1923 /* range table relocation entry */
1924 saa_write64(parangesrel, paranges->datalen + 4);
1925 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1926 saa_write64(parangesrel, (uint64_t) 0);
1927 /* range table entry */
1928 saa_write64(paranges,0x0000); /* range start */
1929 saa_write64(paranges,sects[psect->section]->len); /* range length */
1930 highaddr += sects[psect->section]->len;
1931 /* done with this entry */
1932 psect = psect->next;
1934 saa_write64(paranges,0); /* null address */
1935 saa_write64(paranges,0); /* null length */
1936 saalen = paranges->datalen;
1937 arangeslen = saalen + 4;
1938 arangesbuf = pbuf = nasm_malloc(arangeslen);
1939 WRITELONG(pbuf,saalen); /* initial length */
1940 saa_rnbytes(paranges, pbuf, saalen);
1941 saa_free(paranges);
1943 /* build rela.aranges section */
1944 arangesrellen = saalen = parangesrel->datalen;
1945 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1946 saa_rnbytes(parangesrel, pbuf, saalen);
1947 saa_free(parangesrel);
1949 /* build pubnames section */
1950 ppubnames = saa_init(1L);
1951 saa_write16(ppubnames,3); /* dwarf version */
1952 saa_write32(ppubnames,0); /* offset into info */
1953 saa_write32(ppubnames,0); /* space used in info */
1954 saa_write32(ppubnames,0); /* end of list */
1955 saalen = ppubnames->datalen;
1956 pubnameslen = saalen + 4;
1957 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1958 WRITELONG(pbuf,saalen); /* initial length */
1959 saa_rnbytes(ppubnames, pbuf, saalen);
1960 saa_free(ppubnames);
1962 /* build info section */
1963 pinfo = saa_init(1L);
1964 pinforel = saa_init(1L);
1965 saa_write16(pinfo,3); /* dwarf version */
1966 saa_write64(pinforel, pinfo->datalen + 4);
1967 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
1968 saa_write64(pinforel, 0);
1969 saa_write32(pinfo,0); /* offset into abbrev */
1970 saa_write8(pinfo,8); /* pointer size */
1971 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1972 saa_write64(pinforel, pinfo->datalen + 4);
1973 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1974 saa_write64(pinforel, 0);
1975 saa_write64(pinfo,0); /* DW_AT_low_pc */
1976 saa_write64(pinforel, pinfo->datalen + 4);
1977 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1978 saa_write64(pinforel, 0);
1979 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
1980 saa_write64(pinforel, pinfo->datalen + 4);
1981 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
1982 saa_write64(pinforel, 0);
1983 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1984 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1985 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1986 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1987 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1988 saa_write64(pinforel, pinfo->datalen + 4);
1989 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1990 saa_write64(pinforel, 0);
1991 saa_write64(pinfo,0); /* DW_AT_low_pc */
1992 saa_write64(pinfo,0); /* DW_AT_frame_base */
1993 saa_write8(pinfo,0); /* end of entries */
1994 saalen = pinfo->datalen;
1995 infolen = saalen + 4;
1996 infobuf = pbuf = nasm_malloc(infolen);
1997 WRITELONG(pbuf,saalen); /* initial length */
1998 saa_rnbytes(pinfo, pbuf, saalen);
1999 saa_free(pinfo);
2001 /* build rela.info section */
2002 inforellen = saalen = pinforel->datalen;
2003 inforelbuf = pbuf = nasm_malloc(inforellen);
2004 saa_rnbytes(pinforel, pbuf, saalen);
2005 saa_free(pinforel);
2007 /* build abbrev section */
2008 pabbrev = saa_init(1L);
2009 saa_write8(pabbrev,1); /* entry number LEB128u */
2010 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2011 saa_write8(pabbrev,1); /* has children */
2012 /* the following attributes and forms are all LEB128u values */
2013 saa_write8(pabbrev,DW_AT_low_pc);
2014 saa_write8(pabbrev,DW_FORM_addr);
2015 saa_write8(pabbrev,DW_AT_high_pc);
2016 saa_write8(pabbrev,DW_FORM_addr);
2017 saa_write8(pabbrev,DW_AT_stmt_list);
2018 saa_write8(pabbrev,DW_FORM_data4);
2019 saa_write8(pabbrev,DW_AT_name);
2020 saa_write8(pabbrev,DW_FORM_string);
2021 saa_write8(pabbrev,DW_AT_producer);
2022 saa_write8(pabbrev,DW_FORM_string);
2023 saa_write8(pabbrev,DW_AT_language);
2024 saa_write8(pabbrev,DW_FORM_data2);
2025 saa_write16(pabbrev,0); /* end of entry */
2026 /* LEB128u usage same as above */
2027 saa_write8(pabbrev,2); /* entry number */
2028 saa_write8(pabbrev,DW_TAG_subprogram);
2029 saa_write8(pabbrev,0); /* no children */
2030 saa_write8(pabbrev,DW_AT_low_pc);
2031 saa_write8(pabbrev,DW_FORM_addr);
2032 saa_write8(pabbrev,DW_AT_frame_base);
2033 saa_write8(pabbrev,DW_FORM_data4);
2034 saa_write16(pabbrev,0); /* end of entry */
2035 abbrevlen = saalen = pabbrev->datalen;
2036 abbrevbuf = pbuf = nasm_malloc(saalen);
2037 saa_rnbytes(pabbrev, pbuf, saalen);
2038 saa_free(pabbrev);
2040 /* build line section */
2041 /* prolog */
2042 plines = saa_init(1L);
2043 saa_write8(plines,1); /* Minimum Instruction Length */
2044 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2045 saa_write8(plines,line_base); /* Line Base */
2046 saa_write8(plines,line_range); /* Line Range */
2047 saa_write8(plines,opcode_base); /* Opcode Base */
2048 /* standard opcode lengths (# of LEB128u operands) */
2049 saa_write8(plines,0); /* Std opcode 1 length */
2050 saa_write8(plines,1); /* Std opcode 2 length */
2051 saa_write8(plines,1); /* Std opcode 3 length */
2052 saa_write8(plines,1); /* Std opcode 4 length */
2053 saa_write8(plines,1); /* Std opcode 5 length */
2054 saa_write8(plines,0); /* Std opcode 6 length */
2055 saa_write8(plines,0); /* Std opcode 7 length */
2056 saa_write8(plines,0); /* Std opcode 8 length */
2057 saa_write8(plines,1); /* Std opcode 9 length */
2058 saa_write8(plines,0); /* Std opcode 10 length */
2059 saa_write8(plines,0); /* Std opcode 11 length */
2060 saa_write8(plines,1); /* Std opcode 12 length */
2061 /* Directory Table */
2062 saa_write8(plines,0); /* End of table */
2063 /* File Name Table */
2064 ftentry = dwarf_flist;
2065 for (indx = 0;indx<dwarf_numfiles;indx++)
2067 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2068 saa_write8(plines,0); /* directory LEB128u */
2069 saa_write8(plines,0); /* time LEB128u */
2070 saa_write8(plines,0); /* size LEB128u */
2071 ftentry = ftentry->next;
2073 saa_write8(plines,0); /* End of table */
2074 linepoff = plines->datalen;
2075 linelen = linepoff + totlen + 10;
2076 linebuf = pbuf = nasm_malloc(linelen);
2077 WRITELONG(pbuf,linelen-4); /* initial length */
2078 WRITESHORT(pbuf,3); /* dwarf version */
2079 WRITELONG(pbuf,linepoff); /* offset to line number program */
2080 /* write line header */
2081 saalen = linepoff;
2082 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2083 pbuf += linepoff;
2084 saa_free(plines);
2085 /* concatonate line program ranges */
2086 linepoff += 13;
2087 plinesrel = saa_init(1L);
2088 psect = dwarf_fsect;
2089 for (indx = 0; indx < dwarf_nsections; indx++) {
2090 saa_write64(plinesrel, linepoff);
2091 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2092 saa_write64(plinesrel, (uint64_t) 0);
2093 plinep = psect->psaa;
2094 saalen = plinep->datalen;
2095 saa_rnbytes(plinep, pbuf, saalen);
2096 pbuf += saalen;
2097 linepoff += saalen;
2098 saa_free(plinep);
2099 /* done with this entry */
2100 psect = psect->next;
2104 /* build rela.lines section */
2105 linerellen =saalen = plinesrel->datalen;
2106 linerelbuf = pbuf = nasm_malloc(linerellen);
2107 saa_rnbytes(plinesrel, pbuf, saalen);
2108 saa_free(plinesrel);
2110 /* build frame section */
2111 framelen = 4;
2112 framebuf = pbuf = nasm_malloc(framelen);
2113 WRITELONG(pbuf,framelen-4); /* initial length */
2115 /* build loc section */
2116 loclen = 16;
2117 locbuf = pbuf = nasm_malloc(loclen);
2118 WRITEDLONG(pbuf,0); /* null beginning offset */
2119 WRITEDLONG(pbuf,0); /* null ending offset */
2122 static void dwarf64_cleanup(void)
2124 nasm_free(arangesbuf);
2125 nasm_free(arangesrelbuf);
2126 nasm_free(pubnamesbuf);
2127 nasm_free(infobuf);
2128 nasm_free(inforelbuf);
2129 nasm_free(abbrevbuf);
2130 nasm_free(linebuf);
2131 nasm_free(linerelbuf);
2132 nasm_free(framebuf);
2133 nasm_free(locbuf);
2136 static void dwarf64_findfile(const char * fname)
2138 int finx;
2139 struct linelist *match;
2141 /* return if fname is current file name */
2142 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2143 return;
2145 /* search for match */
2146 match = 0;
2147 if (dwarf_flist) {
2148 match = dwarf_flist;
2149 for (finx = 0; finx < dwarf_numfiles; finx++) {
2150 if (!(strcmp(fname, match->filename))) {
2151 dwarf_clist = match;
2152 return;
2157 /* add file name to end of list */
2158 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2159 dwarf_numfiles++;
2160 dwarf_clist->line = dwarf_numfiles;
2161 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2162 strcpy(dwarf_clist->filename,fname);
2163 dwarf_clist->next = 0;
2164 if (!dwarf_flist) { /* if first entry */
2165 dwarf_flist = dwarf_elist = dwarf_clist;
2166 dwarf_clist->last = 0;
2167 } else { /* chain to previous entry */
2168 dwarf_elist->next = dwarf_clist;
2169 dwarf_elist = dwarf_clist;
2173 static void dwarf64_findsect(const int index)
2175 int sinx;
2176 struct sectlist *match;
2177 struct SAA *plinep;
2179 /* return if index is current section index */
2180 if (dwarf_csect && (dwarf_csect->section == index))
2181 return;
2183 /* search for match */
2184 match = 0;
2185 if (dwarf_fsect) {
2186 match = dwarf_fsect;
2187 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2188 if ((match->section == index)) {
2189 dwarf_csect = match;
2190 return;
2192 match = match->next;
2196 /* add entry to end of list */
2197 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2198 dwarf_nsections++;
2199 dwarf_csect->psaa = plinep = saa_init(1L);
2200 dwarf_csect->line = 1;
2201 dwarf_csect->offset = 0;
2202 dwarf_csect->file = 1;
2203 dwarf_csect->section = index;
2204 dwarf_csect->next = 0;
2205 /* set relocatable address at start of line program */
2206 saa_write8(plinep,DW_LNS_extended_op);
2207 saa_write8(plinep,9); /* operand length */
2208 saa_write8(plinep,DW_LNE_set_address);
2209 saa_write64(plinep,0); /* Start Address */
2211 if (!dwarf_fsect) { /* if first entry */
2212 dwarf_fsect = dwarf_esect = dwarf_csect;
2213 dwarf_csect->last = 0;
2214 } else { /* chain to previous entry */
2215 dwarf_esect->next = dwarf_csect;
2216 dwarf_esect = dwarf_csect;
2220 #endif /* OF_ELF */