ndisasm: fix disassembly of JRCXZ
[nasm/sigaren-mirror.git] / output / outelf64.c
blob5d53bd2302579e8c4f6606d4a7970e0c50363644
1 /* outelf64.c output routines for the Netwide Assembler to produce
2 * ELF64 (x86_64 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
9 #include "compiler.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <inttypes.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "saa.h"
20 #include "raa.h"
21 #include "stdscan.h"
22 #include "outform.h"
23 #include "outlib.h"
24 #include "rbtree.h"
26 #include "output/elf64.h"
27 #include "output/dwarf.h"
28 #include "output/outelf.h"
30 #ifdef OF_ELF64
32 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
34 struct Reloc {
35 struct Reloc *next;
36 int64_t address; /* relative to _start_ of section */
37 int64_t symbol; /* symbol index */
38 int64_t offset; /* symbol addend */
39 int type; /* type of relocation */
42 struct Symbol {
43 struct rbtree symv; /* symbol value and rbtree of globals */
44 int32_t strpos; /* string table position of name */
45 int32_t section; /* section ID of the symbol */
46 int type; /* symbol type */
47 int other; /* symbol visibility */
48 int32_t size; /* size of symbol */
49 int32_t globnum; /* symbol table offset if global */
50 struct Symbol *nextfwd; /* list of unresolved-size symbols */
51 char *name; /* used temporarily if in above list */
54 struct Section {
55 struct SAA *data;
56 uint64_t len, size;
57 uint32_t nrelocs;
58 int32_t index; /* index into sects array */
59 int type; /* SHT_PROGBITS or SHT_NOBITS */
60 uint64_t align; /* alignment: power of two */
61 uint64_t flags; /* section flags */
62 char *name;
63 struct SAA *rel;
64 uint64_t rellen;
65 struct Reloc *head, **tail;
66 struct rbtree *gsyms; /* global symbols in section */
69 #define SECT_DELTA 32
70 static struct Section **sects;
71 static int nsects, sectlen;
73 #define SHSTR_DELTA 256
74 static char *shstrtab;
75 static int shstrtablen, shstrtabsize;
77 static struct SAA *syms;
78 static uint32_t nlocals, nglobs, ndebugs;
80 static int32_t def_seg;
82 static struct RAA *bsym;
84 static struct SAA *strs;
85 static uint32_t strslen;
87 static FILE *elffp;
88 static efunc error;
89 static evalfunc evaluate;
91 static struct Symbol *fwds;
93 static char elf_module[FILENAME_MAX];
95 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
96 static uint8_t elf_abiver = 0; /* Current ABI version */
98 extern struct ofmt of_elf64;
100 static struct ELF_SECTDATA {
101 void *data;
102 int64_t len;
103 bool is_saa;
104 } *elf_sects;
105 static int elf_nsect, nsections;
106 static int64_t elf_foffs;
108 static void elf_write(void);
109 static void elf_sect_write(struct Section *, const void *, size_t);
110 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
111 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
112 int, int);
113 static void elf_write_sections(void);
114 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
115 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
116 static void add_sectname(char *, char *);
118 /* type values for stabs debugging sections */
119 #define N_SO 0x64 /* ID for main source file */
120 #define N_SOL 0x84 /* ID for sub-source file */
121 #define N_BINCL 0x82 /* not currently used */
122 #define N_EINCL 0xA2 /* not currently used */
123 #define N_SLINE 0x44
125 struct stabentry {
126 uint32_t n_strx;
127 uint8_t n_type;
128 uint8_t n_other;
129 uint16_t n_desc;
130 uint32_t n_value;
133 struct erel {
134 int offset, info;
137 struct symlininfo {
138 int offset;
139 int section; /* index into sects[] */
140 int segto; /* internal section number */
141 char *name; /* shallow-copied pointer of section name */
144 struct linelist {
145 struct symlininfo info;
146 int line;
147 char *filename;
148 struct linelist *next;
149 struct linelist *last;
152 struct sectlist {
153 struct SAA *psaa;
154 int section;
155 int line;
156 int offset;
157 int file;
158 struct sectlist *next;
159 struct sectlist *last;
162 /* common debug variables */
163 static int currentline = 1;
164 static int debug_immcall = 0;
166 /* stabs debug variables */
167 static struct linelist *stabslines = 0;
168 static int numlinestabs = 0;
169 static char *stabs_filename = 0;
170 static int symtabsection;
171 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
172 static int stablen, stabstrlen, stabrellen;
174 /* dwarf debug variables */
175 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
176 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
177 static int dwarf_numfiles = 0, dwarf_nsections;
178 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
179 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
180 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
181 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
182 abbrevlen, linelen, linerellen, framelen, loclen;
183 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
186 static struct dfmt df_dwarf;
187 static struct dfmt df_stabs;
188 static struct Symbol *lastsym;
190 /* common debugging routines */
191 static void debug64_typevalue(int32_t);
192 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
193 static void debug64_directive(const char *, const char *);
195 /* stabs debugging routines */
196 static void stabs64_init(struct ofmt *, void *, FILE *, efunc);
197 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
198 static void stabs64_output(int, void *);
199 static void stabs64_generate(void);
200 static void stabs64_cleanup(void);
202 /* dwarf debugging routines */
203 static void dwarf64_init(struct ofmt *, void *, FILE *, efunc);
204 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
205 static void dwarf64_output(int, void *);
206 static void dwarf64_generate(void);
207 static void dwarf64_cleanup(void);
208 static void dwarf64_findfile(const char *);
209 static void dwarf64_findsect(const int);
212 * Special section numbers which are used to define ELF special
213 * symbols, which can be used with WRT to provide PIC relocation
214 * types.
216 static int32_t elf_gotpc_sect, elf_gotoff_sect;
217 static int32_t elf_got_sect, elf_plt_sect;
218 static int32_t elf_sym_sect;
219 static int32_t elf_gottpoff_sect;
221 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
223 maxbits = 64;
224 elffp = fp;
225 error = errfunc;
226 evaluate = eval;
227 (void)ldef; /* placate optimisers */
228 sects = NULL;
229 nsects = sectlen = 0;
230 syms = saa_init((int32_t)sizeof(struct Symbol));
231 nlocals = nglobs = ndebugs = 0;
232 bsym = raa_init();
233 strs = saa_init(1L);
234 saa_wbytes(strs, "\0", 1L);
235 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
236 strslen = 2 + strlen(elf_module);
237 shstrtab = NULL;
238 shstrtablen = shstrtabsize = 0;;
239 add_sectname("", "");
241 fwds = NULL;
243 elf_gotpc_sect = seg_alloc();
244 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
245 error);
246 elf_gotoff_sect = seg_alloc();
247 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
248 error);
249 elf_got_sect = seg_alloc();
250 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
251 error);
252 elf_plt_sect = seg_alloc();
253 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
254 error);
255 elf_sym_sect = seg_alloc();
256 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
257 error);
258 elf_gottpoff_sect = seg_alloc();
259 ldef("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false, &of_elf64,
260 error);
262 def_seg = seg_alloc();
266 static void elf_cleanup(int debuginfo)
268 struct Reloc *r;
269 int i;
271 (void)debuginfo;
273 elf_write();
274 fclose(elffp);
275 for (i = 0; i < nsects; i++) {
276 if (sects[i]->type != SHT_NOBITS)
277 saa_free(sects[i]->data);
278 if (sects[i]->head)
279 saa_free(sects[i]->rel);
280 while (sects[i]->head) {
281 r = sects[i]->head;
282 sects[i]->head = sects[i]->head->next;
283 nasm_free(r);
286 nasm_free(sects);
287 saa_free(syms);
288 raa_free(bsym);
289 saa_free(strs);
290 if (of_elf64.current_dfmt) {
291 of_elf64.current_dfmt->cleanup();
294 /* add entry to the elf .shstrtab section */
295 static void add_sectname(char *firsthalf, char *secondhalf)
297 int len = strlen(firsthalf) + strlen(secondhalf);
298 while (shstrtablen + len + 1 > shstrtabsize)
299 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
300 strcpy(shstrtab + shstrtablen, firsthalf);
301 strcat(shstrtab + shstrtablen, secondhalf);
302 shstrtablen += len + 1;
305 static int elf_make_section(char *name, int type, int flags, int align)
307 struct Section *s;
309 s = nasm_malloc(sizeof(*s));
311 if (type != SHT_NOBITS)
312 s->data = saa_init(1L);
313 s->head = NULL;
314 s->tail = &s->head;
315 s->len = s->size = 0;
316 s->nrelocs = 0;
317 if (!strcmp(name, ".text"))
318 s->index = def_seg;
319 else
320 s->index = seg_alloc();
321 add_sectname("", name);
322 s->name = nasm_malloc(1 + strlen(name));
323 strcpy(s->name, name);
324 s->type = type;
325 s->flags = flags;
326 s->align = align;
327 s->gsyms = NULL;
329 if (nsects >= sectlen)
330 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
331 sects[nsects++] = s;
333 return nsects - 1;
336 static int32_t elf_section_names(char *name, int pass, int *bits)
338 char *p;
339 uint32_t flags, flags_and, flags_or;
340 uint64_t align;
341 int type, i;
344 * Default is 64 bits.
346 if (!name) {
347 *bits = 64;
348 return def_seg;
351 p = name;
352 while (*p && !nasm_isspace(*p))
353 p++;
354 if (*p)
355 *p++ = '\0';
356 flags_and = flags_or = type = align = 0;
358 while (*p && nasm_isspace(*p))
359 p++;
360 while (*p) {
361 char *q = p;
362 while (*p && !nasm_isspace(*p))
363 p++;
364 if (*p)
365 *p++ = '\0';
366 while (*p && nasm_isspace(*p))
367 p++;
369 if (!nasm_strnicmp(q, "align=", 6)) {
370 align = atoi(q + 6);
371 if (align == 0)
372 align = 1;
373 if ((align - 1) & align) { /* means it's not a power of two */
374 error(ERR_NONFATAL, "section alignment %d is not"
375 " a power of two", align);
376 align = 1;
378 } else if (!nasm_stricmp(q, "alloc")) {
379 flags_and |= SHF_ALLOC;
380 flags_or |= SHF_ALLOC;
381 } else if (!nasm_stricmp(q, "noalloc")) {
382 flags_and |= SHF_ALLOC;
383 flags_or &= ~SHF_ALLOC;
384 } else if (!nasm_stricmp(q, "exec")) {
385 flags_and |= SHF_EXECINSTR;
386 flags_or |= SHF_EXECINSTR;
387 } else if (!nasm_stricmp(q, "noexec")) {
388 flags_and |= SHF_EXECINSTR;
389 flags_or &= ~SHF_EXECINSTR;
390 } else if (!nasm_stricmp(q, "write")) {
391 flags_and |= SHF_WRITE;
392 flags_or |= SHF_WRITE;
393 } else if (!nasm_stricmp(q, "tls")) {
394 flags_and |= SHF_TLS;
395 flags_or |= SHF_TLS;
396 } else if (!nasm_stricmp(q, "nowrite")) {
397 flags_and |= SHF_WRITE;
398 flags_or &= ~SHF_WRITE;
399 } else if (!nasm_stricmp(q, "progbits")) {
400 type = SHT_PROGBITS;
401 } else if (!nasm_stricmp(q, "nobits")) {
402 type = SHT_NOBITS;
403 } else if (pass == 1) {
404 error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
405 " declaration of section `%s'", q, name);
409 if (!strcmp(name, ".shstrtab") ||
410 !strcmp(name, ".symtab") ||
411 !strcmp(name, ".strtab")) {
412 error(ERR_NONFATAL, "attempt to redefine reserved section"
413 "name `%s'", name);
414 return NO_SEG;
417 for (i = 0; i < nsects; i++)
418 if (!strcmp(name, sects[i]->name))
419 break;
420 if (i == nsects) {
421 const struct elf_known_section *ks = elf_known_sections;
423 while (ks->name) {
424 if (!strcmp(name, ks->name))
425 break;
426 ks++;
429 type = type ? type : ks->type;
430 align = align ? align : ks->align;
431 flags = (ks->flags & ~flags_and) | flags_or;
433 i = elf_make_section(name, type, flags, align);
434 } else if (pass == 1) {
435 if ((type && sects[i]->type != type)
436 || (align && sects[i]->align != align)
437 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
438 error(ERR_WARNING, "incompatible section attributes ignored on"
439 " redeclaration of section `%s'", name);
442 return sects[i]->index;
445 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
446 int is_global, char *special)
448 int pos = strslen;
449 struct Symbol *sym;
450 bool special_used = false;
452 #if defined(DEBUG) && DEBUG>2
453 fprintf(stderr,
454 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
455 name, segment, offset, is_global, special);
456 #endif
457 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
459 * This is a NASM special symbol. We never allow it into
460 * the ELF symbol table, even if it's a valid one. If it
461 * _isn't_ a valid one, we should barf immediately.
463 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
464 strcmp(name, "..got") && strcmp(name, "..plt") &&
465 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
466 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
467 return;
470 if (is_global == 3) {
471 struct Symbol **s;
473 * Fix up a forward-reference symbol size from the first
474 * pass.
476 for (s = &fwds; *s; s = &(*s)->nextfwd)
477 if (!strcmp((*s)->name, name)) {
478 struct tokenval tokval;
479 expr *e;
480 char *p = special;
482 while (*p && !nasm_isspace(*p))
483 p++;
484 while (*p && nasm_isspace(*p))
485 p++;
486 stdscan_reset();
487 stdscan_bufptr = p;
488 tokval.t_type = TOKEN_INVALID;
489 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
490 if (e) {
491 if (!is_simple(e))
492 error(ERR_NONFATAL, "cannot use relocatable"
493 " expression as symbol size");
494 else
495 (*s)->size = reloc_value(e);
499 * Remove it from the list of unresolved sizes.
501 nasm_free((*s)->name);
502 *s = (*s)->nextfwd;
503 return;
505 return; /* it wasn't an important one */
508 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
509 strslen += 1 + strlen(name);
511 lastsym = sym = saa_wstruct(syms);
513 memset(&sym->symv, 0, sizeof(struct rbtree));
515 sym->strpos = pos;
516 sym->type = is_global ? SYM_GLOBAL : 0;
517 sym->other = STV_DEFAULT;
518 sym->size = 0;
519 if (segment == NO_SEG)
520 sym->section = SHN_ABS;
521 else {
522 int i;
523 sym->section = SHN_UNDEF;
524 if (nsects == 0 && segment == def_seg) {
525 int tempint;
526 if (segment != elf_section_names(".text", 2, &tempint))
527 error(ERR_PANIC,
528 "strange segment conditions in ELF driver");
529 sym->section = nsects;
530 } else {
531 for (i = 0; i < nsects; i++)
532 if (segment == sects[i]->index) {
533 sym->section = i + 1;
534 break;
539 if (is_global == 2) {
540 sym->size = offset;
541 sym->symv.key = 0;
542 sym->section = SHN_COMMON;
544 * We have a common variable. Check the special text to see
545 * if it's a valid number and power of two; if so, store it
546 * as the alignment for the common variable.
548 if (special) {
549 bool err;
550 sym->symv.key = readnum(special, &err);
551 if (err)
552 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
553 " valid number", special);
554 else if ((sym->symv.key | (sym->symv.key - 1))
555 != 2 * sym->symv.key - 1)
556 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
557 " power of two", special);
559 special_used = true;
560 } else
561 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
563 if (sym->type == SYM_GLOBAL) {
565 * If sym->section == SHN_ABS, then the first line of the
566 * else section would cause a core dump, because its a reference
567 * beyond the end of the section array.
568 * This behaviour is exhibited by this code:
569 * GLOBAL crash_nasm
570 * crash_nasm equ 0
571 * To avoid such a crash, such requests are silently discarded.
572 * This may not be the best solution.
574 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
575 bsym = raa_write(bsym, segment, nglobs);
576 } else if (sym->section != SHN_ABS) {
578 * This is a global symbol; so we must add it to the rbtree
579 * of global symbols in its section.
581 * In addition, we check the special text for symbol
582 * type and size information.
584 sects[sym->section-1]->gsyms =
585 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
587 if (special) {
588 int n = strcspn(special, " \t");
590 if (!nasm_strnicmp(special, "function", n))
591 sym->type |= STT_FUNC;
592 else if (!nasm_strnicmp(special, "data", n) ||
593 !nasm_strnicmp(special, "object", n))
594 sym->type |= STT_OBJECT;
595 else if (!nasm_strnicmp(special, "notype", n))
596 sym->type |= STT_NOTYPE;
597 else
598 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
599 n, special);
600 special += n;
602 while (nasm_isspace(*special))
603 ++special;
604 if (*special) {
605 n = strcspn(special, " \t");
606 if (!nasm_strnicmp(special, "default", n))
607 sym->other = STV_DEFAULT;
608 else if (!nasm_strnicmp(special, "internal", n))
609 sym->other = STV_INTERNAL;
610 else if (!nasm_strnicmp(special, "hidden", n))
611 sym->other = STV_HIDDEN;
612 else if (!nasm_strnicmp(special, "protected", n))
613 sym->other = STV_PROTECTED;
614 else
615 n = 0;
616 special += n;
619 if (*special) {
620 struct tokenval tokval;
621 expr *e;
622 int fwd = 0;
623 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
625 while (special[n] && nasm_isspace(special[n]))
626 n++;
628 * We have a size expression; attempt to
629 * evaluate it.
631 stdscan_reset();
632 stdscan_bufptr = special + n;
633 tokval.t_type = TOKEN_INVALID;
634 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
635 NULL);
636 if (fwd) {
637 sym->nextfwd = fwds;
638 fwds = sym;
639 sym->name = nasm_strdup(name);
640 } else if (e) {
641 if (!is_simple(e))
642 error(ERR_NONFATAL, "cannot use relocatable"
643 " expression as symbol size");
644 else
645 sym->size = reloc_value(e);
647 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
649 special_used = true;
652 * If TLS segment, mark symbol accordingly.
654 if (sects[sym->section - 1]->flags & SHF_TLS) {
655 sym->type &= 0xf0;
656 sym->type |= STT_TLS;
659 sym->globnum = nglobs;
660 nglobs++;
661 } else
662 nlocals++;
664 if (special && !special_used)
665 error(ERR_NONFATAL, "no special symbol features supported here");
668 static void elf_add_reloc(struct Section *sect, int32_t segment,
669 int64_t offset, int type)
671 struct Reloc *r;
672 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
673 sect->tail = &r->next;
674 r->next = NULL;
676 r->address = sect->len;
677 r->offset = offset;
678 if (segment == NO_SEG)
679 r->symbol = 0;
680 else {
681 int i;
682 r->symbol = 0;
683 for (i = 0; i < nsects; i++)
684 if (segment == sects[i]->index)
685 r->symbol = i + 2;
686 if (!r->symbol)
687 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
689 r->type = type;
691 sect->nrelocs++;
695 * This routine deals with ..got and ..sym relocations: the more
696 * complicated kinds. In shared-library writing, some relocations
697 * with respect to global symbols must refer to the precise symbol
698 * rather than referring to an offset from the base of the section
699 * _containing_ the symbol. Such relocations call to this routine,
700 * which searches the symbol list for the symbol in question.
702 * R_386_GOT32 references require the _exact_ symbol address to be
703 * used; R_386_32 references can be at an offset from the symbol.
704 * The boolean argument `exact' tells us this.
706 * Return value is the adjusted value of `addr', having become an
707 * offset from the symbol rather than the section. Should always be
708 * zero when returning from an exact call.
710 * Limitation: if you define two symbols at the same place,
711 * confusion will occur.
713 * Inefficiency: we search, currently, using a linked list which
714 * isn't even necessarily sorted.
716 static void elf_add_gsym_reloc(struct Section *sect,
717 int32_t segment, uint64_t offset, int64_t pcrel,
718 int type, bool exact)
720 struct Reloc *r;
721 struct Section *s;
722 struct Symbol *sym;
723 struct rbtree *srb;
724 int i;
727 * First look up the segment/offset pair and find a global
728 * symbol corresponding to it. If it's not one of our segments,
729 * then it must be an external symbol, in which case we're fine
730 * doing a normal elf_add_reloc after first sanity-checking
731 * that the offset from the symbol is zero.
733 s = NULL;
734 for (i = 0; i < nsects; i++)
735 if (segment == sects[i]->index) {
736 s = sects[i];
737 break;
740 if (!s) {
741 if (exact && offset)
742 error(ERR_NONFATAL, "invalid access to an external symbol");
743 else
744 elf_add_reloc(sect, segment, offset - pcrel, type);
745 return;
748 srb = rb_search(s->gsyms, offset);
749 if (!srb || (exact && srb->key != offset)) {
750 error(ERR_NONFATAL, "unable to find a suitable global symbol"
751 " for this reference");
752 return;
754 sym = container_of(srb, struct Symbol, symv);
756 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
757 sect->tail = &r->next;
758 r->next = NULL;
760 r->address = sect->len;
761 r->offset = offset - pcrel - sym->symv.key;
762 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
763 r->type = type;
765 sect->nrelocs++;
768 static void elf_out(int32_t segto, const void *data,
769 enum out_type type, uint64_t size,
770 int32_t segment, int32_t wrt)
772 struct Section *s;
773 int64_t addr, zero;
774 int i;
775 static struct symlininfo sinfo;
777 zero = 0;
779 #if defined(DEBUG) && DEBUG>2
780 if (data) fprintf(stderr,
781 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
782 currentline, type, segment, segto, size, *(int64_t *)data);
783 else fprintf(stderr,
784 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
785 currentline, type, segment, segto, size);
786 #endif
789 * handle absolute-assembly (structure definitions)
791 if (segto == NO_SEG) {
792 if (type != OUT_RESERVE)
793 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
794 " space");
795 return;
798 s = NULL;
799 for (i = 0; i < nsects; i++)
800 if (segto == sects[i]->index) {
801 s = sects[i];
802 break;
804 if (!s) {
805 int tempint; /* ignored */
806 if (segto != elf_section_names(".text", 2, &tempint))
807 error(ERR_PANIC, "strange segment conditions in ELF driver");
808 else {
809 s = sects[nsects - 1];
810 i = nsects - 1;
813 /* invoke current debug_output routine */
814 if (of_elf64.current_dfmt) {
815 sinfo.offset = s->len;
816 sinfo.section = i;
817 sinfo.segto = segto;
818 sinfo.name = s->name;
819 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
821 /* end of debugging stuff */
823 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
824 error(ERR_WARNING, "attempt to initialize memory in"
825 " BSS section `%s': ignored", s->name);
826 s->len += realsize(type, size);
827 return;
830 if (type == OUT_RESERVE) {
831 if (s->type == SHT_PROGBITS) {
832 error(ERR_WARNING, "uninitialized space declared in"
833 " non-BSS section `%s': zeroing", s->name);
834 elf_sect_write(s, NULL, size);
835 } else
836 s->len += size;
837 } else if (type == OUT_RAWDATA) {
838 if (segment != NO_SEG)
839 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
840 elf_sect_write(s, data, size);
841 } else if (type == OUT_ADDRESS) {
842 addr = *(int64_t *)data;
843 if (segment == NO_SEG) {
844 /* Do nothing */
845 } else if (segment % 2) {
846 error(ERR_NONFATAL, "ELF format does not support"
847 " segment base references");
848 } else {
849 if (wrt == NO_SEG) {
850 switch ((int)size) {
851 case 1:
852 elf_add_reloc(s, segment, addr, R_X86_64_8);
853 break;
854 case 2:
855 elf_add_reloc(s, segment, addr, R_X86_64_16);
856 break;
857 case 4:
858 elf_add_reloc(s, segment, addr, R_X86_64_32);
859 break;
860 case 8:
861 elf_add_reloc(s, segment, addr, R_X86_64_64);
862 break;
863 default:
864 error(ERR_PANIC, "internal error elf64-hpa-871");
865 break;
867 addr = 0;
868 } else if (wrt == elf_gotpc_sect + 1) {
870 * The user will supply GOT relative to $$. ELF
871 * will let us have GOT relative to $. So we
872 * need to fix up the data item by $-$$.
874 addr += s->len;
875 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
876 addr = 0;
877 } else if (wrt == elf_gotoff_sect + 1) {
878 if (size != 8) {
879 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
880 "references to be qword");
881 } else {
882 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
883 addr = 0;
885 } else if (wrt == elf_got_sect + 1) {
886 switch ((int)size) {
887 case 4:
888 elf_add_gsym_reloc(s, segment, addr, 0,
889 R_X86_64_GOT32, true);
890 addr = 0;
891 break;
892 case 8:
893 elf_add_gsym_reloc(s, segment, addr, 0,
894 R_X86_64_GOT64, true);
895 addr = 0;
896 break;
897 default:
898 error(ERR_NONFATAL, "invalid ..got reference");
899 break;
901 } else if (wrt == elf_sym_sect + 1) {
902 switch ((int)size) {
903 case 1:
904 elf_add_gsym_reloc(s, segment, addr, 0,
905 R_X86_64_8, false);
906 addr = 0;
907 break;
908 case 2:
909 elf_add_gsym_reloc(s, segment, addr, 0,
910 R_X86_64_16, false);
911 addr = 0;
912 break;
913 case 4:
914 elf_add_gsym_reloc(s, segment, addr, 0,
915 R_X86_64_32, false);
916 addr = 0;
917 break;
918 case 8:
919 elf_add_gsym_reloc(s, segment, addr, 0,
920 R_X86_64_64, false);
921 addr = 0;
922 break;
923 default:
924 error(ERR_PANIC, "internal error elf64-hpa-903");
925 break;
927 } else if (wrt == elf_plt_sect + 1) {
928 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
929 "relative PLT references");
930 } else {
931 error(ERR_NONFATAL, "ELF format does not support this"
932 " use of WRT");
935 elf_sect_writeaddr(s, addr, size);
936 } else if (type == OUT_REL2ADR) {
937 addr = *(int64_t *)data - size;
938 if (segment == segto)
939 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
940 if (segment == NO_SEG) {
941 /* Do nothing */
942 } else if (segment % 2) {
943 error(ERR_NONFATAL, "ELF format does not support"
944 " segment base references");
945 } else {
946 if (wrt == NO_SEG) {
947 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
948 addr = 0;
949 } else {
950 error(ERR_NONFATAL,
951 "Unsupported non-32-bit ELF relocation [2]");
954 elf_sect_writeaddr(s, addr, 2);
955 } else if (type == OUT_REL4ADR) {
956 addr = *(int64_t *)data - size;
957 if (segment == segto)
958 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
959 if (segment == NO_SEG) {
960 /* Do nothing */
961 } else if (segment % 2) {
962 error(ERR_NONFATAL, "ELF64 format does not support"
963 " segment base references");
964 } else {
965 if (wrt == NO_SEG) {
966 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
967 addr = 0;
968 } else if (wrt == elf_plt_sect + 1) {
969 elf_add_gsym_reloc(s, segment, addr+size, size,
970 R_X86_64_PLT32, true);
971 addr = 0;
972 } else if (wrt == elf_gotpc_sect + 1 ||
973 wrt == elf_got_sect + 1) {
974 elf_add_gsym_reloc(s, segment, addr+size, size,
975 R_X86_64_GOTPCREL, true);
976 addr = 0;
977 } else if (wrt == elf_gotoff_sect + 1 ||
978 wrt == elf_got_sect + 1) {
979 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
980 "qword absolute");
981 } else if (wrt == elf_gottpoff_sect + 1) {
982 elf_add_gsym_reloc(s, segment, addr+size, size,
983 R_X86_64_GOTTPOFF, true);
984 addr = 0;
985 } else {
986 error(ERR_NONFATAL, "ELF64 format does not support this"
987 " use of WRT");
990 elf_sect_writeaddr(s, addr, 4);
991 } else if (type == OUT_REL8ADR) {
992 addr = *(int64_t *)data - size;
993 if (segment == segto)
994 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
995 if (segment == NO_SEG) {
996 /* Do nothing */
997 } else if (segment % 2) {
998 error(ERR_NONFATAL, "ELF64 format does not support"
999 " segment base references");
1000 } else {
1001 if (wrt == NO_SEG) {
1002 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1003 addr = 0;
1004 } else if (wrt == elf_gotpc_sect + 1 ||
1005 wrt == elf_got_sect + 1) {
1006 elf_add_gsym_reloc(s, segment, addr+size, size,
1007 R_X86_64_GOTPCREL64, true);
1008 addr = 0;
1009 } else if (wrt == elf_gotoff_sect + 1 ||
1010 wrt == elf_got_sect + 1) {
1011 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1012 "absolute");
1013 } else if (wrt == elf_gottpoff_sect + 1) {
1014 error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1015 "dword");
1016 } else {
1017 error(ERR_NONFATAL, "ELF64 format does not support this"
1018 " use of WRT");
1021 elf_sect_writeaddr(s, addr, 8);
1025 static void elf_write(void)
1027 int align;
1028 char *p;
1029 int i;
1031 struct SAA *symtab;
1032 int32_t symtablen, symtablocal;
1035 * Work out how many sections we will have. We have SHN_UNDEF,
1036 * then the flexible user sections, then the fixed sections
1037 * `.shstrtab', `.symtab' and `.strtab', then optionally
1038 * relocation sections for the user sections.
1040 nsections = sec_numspecial + 1;
1041 if (of_elf64.current_dfmt == &df_stabs)
1042 nsections += 3;
1043 else if (of_elf64.current_dfmt == &df_dwarf)
1044 nsections += 10;
1046 add_sectname("", ".shstrtab");
1047 add_sectname("", ".symtab");
1048 add_sectname("", ".strtab");
1049 for (i = 0; i < nsects; i++) {
1050 nsections++; /* for the section itself */
1051 if (sects[i]->head) {
1052 nsections++; /* for its relocations */
1053 add_sectname(".rela", sects[i]->name);
1057 if (of_elf64.current_dfmt == &df_stabs) {
1058 /* in case the debug information is wanted, just add these three sections... */
1059 add_sectname("", ".stab");
1060 add_sectname("", ".stabstr");
1061 add_sectname(".rel", ".stab");
1064 else if (of_elf64.current_dfmt == &df_dwarf) {
1065 /* the dwarf debug standard specifies the following ten sections,
1066 not all of which are currently implemented,
1067 although all of them are defined. */
1068 #define debug_aranges (int64_t) (nsections-10)
1069 #define debug_info (int64_t) (nsections-7)
1070 #define debug_abbrev (int64_t) (nsections-5)
1071 #define debug_line (int64_t) (nsections-4)
1072 add_sectname("", ".debug_aranges");
1073 add_sectname(".rela", ".debug_aranges");
1074 add_sectname("", ".debug_pubnames");
1075 add_sectname("", ".debug_info");
1076 add_sectname(".rela", ".debug_info");
1077 add_sectname("", ".debug_abbrev");
1078 add_sectname("", ".debug_line");
1079 add_sectname(".rela", ".debug_line");
1080 add_sectname("", ".debug_frame");
1081 add_sectname("", ".debug_loc");
1085 * Output the ELF header.
1087 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1088 fputc(elf_osabi, elffp);
1089 fputc(elf_abiver, elffp);
1090 fwritezero(7, elffp);
1091 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1092 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1093 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1094 fwriteint64_t(0L, elffp); /* no entry point */
1095 fwriteint64_t(0L, elffp); /* no program header table */
1096 fwriteint64_t(0x40L, elffp); /* section headers straight after
1097 * ELF header plus alignment */
1098 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1099 fwriteint16_t(0x40, elffp); /* size of ELF header */
1100 fwriteint16_t(0, elffp); /* no program header table, again */
1101 fwriteint16_t(0, elffp); /* still no program header table */
1102 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1103 fwriteint16_t(nsections, elffp); /* number of sections */
1104 fwriteint16_t(sec_shstrtab, elffp); /* string table section index for
1105 * section header table */
1108 * Build the symbol table and relocation tables.
1110 symtab = elf_build_symtab(&symtablen, &symtablocal);
1111 for (i = 0; i < nsects; i++)
1112 if (sects[i]->head)
1113 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1114 sects[i]->head);
1117 * Now output the section header table.
1120 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1121 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1122 elf_foffs += align;
1123 elf_nsect = 0;
1124 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1126 /* SHN_UNDEF */
1127 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1128 p = shstrtab + 1;
1130 /* The normal sections */
1131 for (i = 0; i < nsects; i++) {
1132 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1133 (sects[i]->type == SHT_PROGBITS ?
1134 sects[i]->data : NULL), true,
1135 sects[i]->len, 0, 0, sects[i]->align, 0);
1136 p += strlen(p) + 1;
1139 /* .shstrtab */
1140 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1141 shstrtablen, 0, 0, 1, 0);
1142 p += strlen(p) + 1;
1144 /* .symtab */
1145 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1146 symtablen, sec_strtab, symtablocal, 4, 24);
1147 p += strlen(p) + 1;
1149 /* .strtab */
1150 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, strs, true,
1151 strslen, 0, 0, 1, 0);
1152 p += strlen(p) + 1;
1154 /* The relocation sections */
1155 for (i = 0; i < nsects; i++)
1156 if (sects[i]->head) {
1157 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1158 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1159 p += strlen(p) + 1;
1162 if (of_elf64.current_dfmt == &df_stabs) {
1163 /* for debugging information, create the last three sections
1164 which are the .stab , .stabstr and .rel.stab sections respectively */
1166 /* this function call creates the stab sections in memory */
1167 stabs64_generate();
1169 if (stabbuf && stabstrbuf && stabrelbuf) {
1170 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1171 stablen, sec_stabstr, 0, 4, 12);
1172 p += strlen(p) + 1;
1174 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1175 stabstrlen, 0, 0, 4, 0);
1176 p += strlen(p) + 1;
1178 /* link -> symtable info -> section to refer to */
1179 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1180 stabrellen, symtabsection, sec_stab, 4, 16);
1181 p += strlen(p) + 1;
1184 else if (of_elf64.current_dfmt == &df_dwarf) {
1185 /* for dwarf debugging information, create the ten dwarf sections */
1187 /* this function call creates the dwarf sections in memory */
1188 if (dwarf_fsect)
1189 dwarf64_generate();
1191 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1192 arangeslen, 0, 0, 1, 0);
1193 p += strlen(p) + 1;
1195 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1196 arangesrellen, symtabsection, debug_aranges, 1, 24);
1197 p += strlen(p) + 1;
1199 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1200 pubnameslen, 0, 0, 1, 0);
1201 p += strlen(p) + 1;
1203 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1204 infolen, 0, 0, 1, 0);
1205 p += strlen(p) + 1;
1207 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1208 inforellen, symtabsection, debug_info, 1, 24);
1209 p += strlen(p) + 1;
1211 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1212 abbrevlen, 0, 0, 1, 0);
1213 p += strlen(p) + 1;
1215 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1216 linelen, 0, 0, 1, 0);
1217 p += strlen(p) + 1;
1219 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1220 linerellen, symtabsection, debug_line, 1, 24);
1221 p += strlen(p) + 1;
1223 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1224 framelen, 0, 0, 8, 0);
1225 p += strlen(p) + 1;
1227 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1228 loclen, 0, 0, 1, 0);
1229 p += strlen(p) + 1;
1231 fwritezero(align, elffp);
1234 * Now output the sections.
1236 elf_write_sections();
1238 nasm_free(elf_sects);
1239 saa_free(symtab);
1242 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1244 struct SAA *s = saa_init(1L);
1245 struct Symbol *sym;
1246 uint8_t entry[24], *p;
1247 int i;
1249 *len = *local = 0;
1252 * First, an all-zeros entry, required by the ELF spec.
1254 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1255 *len += 24;
1256 (*local)++;
1259 * Next, an entry for the file name.
1261 p = entry;
1262 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1263 WRITESHORT(p, STT_FILE); /* type FILE */
1264 WRITESHORT(p, SHN_ABS);
1265 WRITEDLONG(p, (uint64_t) 0); /* no value */
1266 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1267 saa_wbytes(s, entry, 24L);
1268 *len += 24;
1269 (*local)++;
1272 * Now some standard symbols defining the segments, for relocation
1273 * purposes.
1275 for (i = 1; i <= nsects; i++) {
1276 p = entry;
1277 WRITELONG(p, 0); /* no symbol name */
1278 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1279 WRITESHORT(p, i); /* section id */
1280 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1281 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1282 saa_wbytes(s, entry, 24L);
1283 *len += 24;
1284 (*local)++;
1289 * Now the other local symbols.
1291 saa_rewind(syms);
1292 while ((sym = saa_rstruct(syms))) {
1293 if (sym->type & SYM_GLOBAL)
1294 continue;
1295 p = entry;
1296 WRITELONG(p, sym->strpos); /* index into symbol string table */
1297 WRITECHAR(p, sym->type); /* type and binding */
1298 WRITECHAR(p, sym->other); /* visibility */
1299 WRITESHORT(p, sym->section); /* index into section header table */
1300 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1301 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1302 saa_wbytes(s, entry, 24L);
1303 *len += 24;
1304 (*local)++;
1307 * dwarf needs symbols for debug sections
1308 * which are relocation targets.
1310 if (of_elf64.current_dfmt == &df_dwarf) {
1311 dwarf_infosym = *local;
1312 p = entry;
1313 WRITELONG(p, 0); /* no symbol name */
1314 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1315 WRITESHORT(p, debug_info); /* section id */
1316 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1317 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1318 saa_wbytes(s, entry, 24L);
1319 *len += 24;
1320 (*local)++;
1321 dwarf_abbrevsym = *local;
1322 p = entry;
1323 WRITELONG(p, 0); /* no symbol name */
1324 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1325 WRITESHORT(p, debug_abbrev); /* section id */
1326 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1327 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1328 saa_wbytes(s, entry, 24L);
1329 *len += 24;
1330 (*local)++;
1331 dwarf_linesym = *local;
1332 p = entry;
1333 WRITELONG(p, 0); /* no symbol name */
1334 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1335 WRITESHORT(p, debug_line); /* section id */
1336 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1337 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1338 saa_wbytes(s, entry, 24L);
1339 *len += 24;
1340 (*local)++;
1344 * Now the global symbols.
1346 saa_rewind(syms);
1347 while ((sym = saa_rstruct(syms))) {
1348 if (!(sym->type & SYM_GLOBAL))
1349 continue;
1350 p = entry;
1351 WRITELONG(p, sym->strpos);
1352 WRITECHAR(p, sym->type); /* type and binding */
1353 WRITECHAR(p, sym->other); /* visibility */
1354 WRITESHORT(p, sym->section);
1355 WRITEDLONG(p, (int64_t)sym->symv.key);
1356 WRITEDLONG(p, (int64_t)sym->size);
1357 saa_wbytes(s, entry, 24L);
1358 *len += 24;
1361 return s;
1364 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1366 struct SAA *s;
1367 uint8_t *p, entry[24];
1368 int32_t global_offset;
1370 if (!r)
1371 return NULL;
1373 s = saa_init(1L);
1374 *len = 0;
1377 * How to onvert from a global placeholder to a real symbol index;
1378 * the +2 refers to the two special entries, the null entry and
1379 * the filename entry.
1381 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1383 while (r) {
1384 int32_t sym = r->symbol;
1386 if (sym >= GLOBAL_TEMP_BASE)
1387 sym += global_offset;
1389 p = entry;
1390 WRITEDLONG(p, r->address);
1391 WRITELONG(p, r->type);
1392 WRITELONG(p, sym);
1393 WRITEDLONG(p, r->offset);
1394 saa_wbytes(s, entry, 24L);
1395 *len += 24;
1397 r = r->next;
1400 return s;
1403 static void elf_section_header(int name, int type, uint64_t flags,
1404 void *data, bool is_saa, uint64_t datalen,
1405 int link, int info, int align, int eltsize)
1407 elf_sects[elf_nsect].data = data;
1408 elf_sects[elf_nsect].len = datalen;
1409 elf_sects[elf_nsect].is_saa = is_saa;
1410 elf_nsect++;
1412 fwriteint32_t((int32_t)name, elffp);
1413 fwriteint32_t((int32_t)type, elffp);
1414 fwriteint64_t((int64_t)flags, elffp);
1415 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1416 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1417 fwriteint64_t(datalen, elffp);
1418 if (data)
1419 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1420 fwriteint32_t((int32_t)link, elffp);
1421 fwriteint32_t((int32_t)info, elffp);
1422 fwriteint64_t((int64_t)align, elffp);
1423 fwriteint64_t((int64_t)eltsize, elffp);
1426 static void elf_write_sections(void)
1428 int i;
1429 for (i = 0; i < elf_nsect; i++)
1430 if (elf_sects[i].data) {
1431 int32_t len = elf_sects[i].len;
1432 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1433 int32_t align = reallen - len;
1434 if (elf_sects[i].is_saa)
1435 saa_fpwrite(elf_sects[i].data, elffp);
1436 else
1437 fwrite(elf_sects[i].data, len, 1, elffp);
1438 fwritezero(align, elffp);
1442 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1444 saa_wbytes(sect->data, data, len);
1445 sect->len += len;
1447 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1449 saa_writeaddr(sect->data, data, len);
1450 sect->len += len;
1453 static int32_t elf_segbase(int32_t segment)
1455 return segment;
1458 static int elf_directive(char *directive, char *value, int pass)
1460 bool err;
1461 int64_t n;
1462 char *p;
1464 if (!strcmp(directive, "osabi")) {
1465 if (pass == 2)
1466 return 1; /* ignore in pass 2 */
1468 n = readnum(value, &err);
1469 if (err) {
1470 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1471 return 1;
1473 if (n < 0 || n > 255) {
1474 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1475 return 1;
1477 elf_osabi = n;
1478 elf_abiver = 0;
1480 if ((p = strchr(value,',')) == NULL)
1481 return 1;
1483 n = readnum(p+1, &err);
1484 if (err || n < 0 || n > 255) {
1485 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1486 return 1;
1489 elf_abiver = n;
1490 return 1;
1493 return 0;
1496 static void elf_filename(char *inname, char *outname, efunc error)
1498 strcpy(elf_module, inname);
1499 standard_extension(inname, outname, ".o", error);
1502 extern macros_t elf_stdmac[];
1504 static int elf_set_info(enum geninfo type, char **val)
1506 (void)type;
1507 (void)val;
1508 return 0;
1510 static struct dfmt df_dwarf = {
1511 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1512 "dwarf",
1513 dwarf64_init,
1514 dwarf64_linenum,
1515 debug64_deflabel,
1516 debug64_directive,
1517 debug64_typevalue,
1518 dwarf64_output,
1519 dwarf64_cleanup
1521 static struct dfmt df_stabs = {
1522 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1523 "stabs",
1524 stabs64_init,
1525 stabs64_linenum,
1526 debug64_deflabel,
1527 debug64_directive,
1528 debug64_typevalue,
1529 stabs64_output,
1530 stabs64_cleanup
1533 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1535 struct ofmt of_elf64 = {
1536 "ELF64 (x86_64) object files (e.g. Linux)",
1537 "elf64",
1538 NULL,
1539 elf64_debugs_arr,
1540 &df_stabs,
1541 elf_stdmac,
1542 elf_init,
1543 elf_set_info,
1544 elf_out,
1545 elf_deflabel,
1546 elf_section_names,
1547 elf_segbase,
1548 elf_directive,
1549 elf_filename,
1550 elf_cleanup
1553 /* common debugging routines */
1554 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1555 int is_global, char *special)
1557 (void)name;
1558 (void)segment;
1559 (void)offset;
1560 (void)is_global;
1561 (void)special;
1564 static void debug64_directive(const char *directive, const char *params)
1566 (void)directive;
1567 (void)params;
1570 static void debug64_typevalue(int32_t type)
1572 int32_t stype, ssize;
1573 switch (TYM_TYPE(type)) {
1574 case TY_LABEL:
1575 ssize = 0;
1576 stype = STT_NOTYPE;
1577 break;
1578 case TY_BYTE:
1579 ssize = 1;
1580 stype = STT_OBJECT;
1581 break;
1582 case TY_WORD:
1583 ssize = 2;
1584 stype = STT_OBJECT;
1585 break;
1586 case TY_DWORD:
1587 ssize = 4;
1588 stype = STT_OBJECT;
1589 break;
1590 case TY_FLOAT:
1591 ssize = 4;
1592 stype = STT_OBJECT;
1593 break;
1594 case TY_QWORD:
1595 ssize = 8;
1596 stype = STT_OBJECT;
1597 break;
1598 case TY_TBYTE:
1599 ssize = 10;
1600 stype = STT_OBJECT;
1601 break;
1602 case TY_OWORD:
1603 ssize = 16;
1604 stype = STT_OBJECT;
1605 break;
1606 case TY_COMMON:
1607 ssize = 0;
1608 stype = STT_COMMON;
1609 break;
1610 case TY_SEG:
1611 ssize = 0;
1612 stype = STT_SECTION;
1613 break;
1614 case TY_EXTERN:
1615 ssize = 0;
1616 stype = STT_NOTYPE;
1617 break;
1618 case TY_EQU:
1619 ssize = 0;
1620 stype = STT_NOTYPE;
1621 break;
1622 default:
1623 ssize = 0;
1624 stype = STT_NOTYPE;
1625 break;
1627 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1628 lastsym->size = ssize;
1629 lastsym->type = stype;
1633 /* stabs debugging routines */
1634 static void stabs64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1636 (void)of;
1637 (void)id;
1638 (void)fp;
1639 (void)error;
1643 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1645 (void)segto;
1646 if (!stabs_filename) {
1647 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1648 strcpy(stabs_filename, filename);
1649 } else {
1650 if (strcmp(stabs_filename, filename)) {
1651 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1652 in fact, this leak comes in quite handy to maintain a list of files
1653 encountered so far in the symbol lines... */
1655 /* why not nasm_free(stabs_filename); we're done with the old one */
1657 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1658 strcpy(stabs_filename, filename);
1661 debug_immcall = 1;
1662 currentline = linenumber;
1666 static void stabs64_output(int type, void *param)
1668 struct symlininfo *s;
1669 struct linelist *el;
1670 if (type == TY_DEBUGSYMLIN) {
1671 if (debug_immcall) {
1672 s = (struct symlininfo *)param;
1673 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1674 return; /* line info is only collected for executable sections */
1675 numlinestabs++;
1676 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1677 el->info.offset = s->offset;
1678 el->info.section = s->section;
1679 el->info.name = s->name;
1680 el->line = currentline;
1681 el->filename = stabs_filename;
1682 el->next = 0;
1683 if (stabslines) {
1684 stabslines->last->next = el;
1685 stabslines->last = el;
1686 } else {
1687 stabslines = el;
1688 stabslines->last = el;
1692 debug_immcall = 0;
1695 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1696 do {\
1697 WRITELONG(p,n_strx); \
1698 WRITECHAR(p,n_type); \
1699 WRITECHAR(p,n_other); \
1700 WRITESHORT(p,n_desc); \
1701 WRITELONG(p,n_value); \
1702 } while (0)
1704 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1706 static void stabs64_generate(void)
1708 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1709 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1710 char **allfiles;
1711 int *fileidx;
1713 struct linelist *ptr;
1715 ptr = stabslines;
1717 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1718 for (i = 0; i < numlinestabs; i++)
1719 allfiles[i] = 0;
1720 numfiles = 0;
1721 while (ptr) {
1722 if (numfiles == 0) {
1723 allfiles[0] = ptr->filename;
1724 numfiles++;
1725 } else {
1726 for (i = 0; i < numfiles; i++) {
1727 if (!strcmp(allfiles[i], ptr->filename))
1728 break;
1730 if (i >= numfiles) {
1731 allfiles[i] = ptr->filename;
1732 numfiles++;
1735 ptr = ptr->next;
1737 strsize = 1;
1738 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1739 for (i = 0; i < numfiles; i++) {
1740 fileidx[i] = strsize;
1741 strsize += strlen(allfiles[i]) + 1;
1743 mainfileindex = 0;
1744 for (i = 0; i < numfiles; i++) {
1745 if (!strcmp(allfiles[i], elf_module)) {
1746 mainfileindex = i;
1747 break;
1751 /* worst case size of the stab buffer would be:
1752 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1754 sbuf =
1755 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1756 sizeof(struct stabentry));
1758 ssbuf = (uint8_t *)nasm_malloc(strsize);
1760 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1761 rptr = rbuf;
1763 for (i = 0; i < numfiles; i++) {
1764 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1766 ssbuf[0] = 0;
1768 stabstrlen = strsize; /* set global variable for length of stab strings */
1770 sptr = sbuf;
1771 ptr = stabslines;
1772 numstabs = 0;
1774 if (ptr) {
1775 /* this is the first stab, its strx points to the filename of the
1776 the source-file, the n_desc field should be set to the number
1777 of remaining stabs
1779 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1781 /* this is the stab for the main source file */
1782 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1784 /* relocation table entry */
1786 /* Since the symbol table has two entries before */
1787 /* the section symbols, the index in the info.section */
1788 /* member must be adjusted by adding 2 */
1790 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1791 WRITELONG(rptr, R_X86_64_32);
1792 WRITELONG(rptr, ptr->info.section + 2);
1794 numstabs++;
1795 currfile = mainfileindex;
1798 while (ptr) {
1799 if (strcmp(allfiles[currfile], ptr->filename)) {
1800 /* oops file has changed... */
1801 for (i = 0; i < numfiles; i++)
1802 if (!strcmp(allfiles[i], ptr->filename))
1803 break;
1804 currfile = i;
1805 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1806 ptr->info.offset);
1807 numstabs++;
1809 /* relocation table entry */
1811 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1812 WRITELONG(rptr, R_X86_64_32);
1813 WRITELONG(rptr, ptr->info.section + 2);
1816 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1817 numstabs++;
1819 /* relocation table entry */
1821 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1822 WRITELONG(rptr, R_X86_64_32);
1823 WRITELONG(rptr, ptr->info.section + 2);
1825 ptr = ptr->next;
1829 ((struct stabentry *)sbuf)->n_desc = numstabs;
1831 nasm_free(allfiles);
1832 nasm_free(fileidx);
1834 stablen = (sptr - sbuf);
1835 stabrellen = (rptr - rbuf);
1836 stabrelbuf = rbuf;
1837 stabbuf = sbuf;
1838 stabstrbuf = ssbuf;
1841 static void stabs64_cleanup(void)
1843 struct linelist *ptr, *del;
1844 if (!stabslines)
1845 return;
1846 ptr = stabslines;
1847 while (ptr) {
1848 del = ptr;
1849 ptr = ptr->next;
1850 nasm_free(del);
1852 if (stabbuf)
1853 nasm_free(stabbuf);
1854 if (stabrelbuf)
1855 nasm_free(stabrelbuf);
1856 if (stabstrbuf)
1857 nasm_free(stabstrbuf);
1859 /* dwarf routines */
1860 static void dwarf64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1862 (void)of;
1863 (void)id;
1864 (void)fp;
1865 (void)error;
1867 ndebugs = 3; /* 3 debug symbols */
1870 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1871 int32_t segto)
1873 (void)segto;
1874 dwarf64_findfile(filename);
1875 debug_immcall = 1;
1876 currentline = linenumber;
1879 /* called from elf_out with type == TY_DEBUGSYMLIN */
1880 static void dwarf64_output(int type, void *param)
1882 int ln, aa, inx, maxln, soc;
1883 struct symlininfo *s;
1884 struct SAA *plinep;
1886 (void)type;
1888 s = (struct symlininfo *)param;
1889 /* line number info is only gathered for executable sections */
1890 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1891 return;
1892 /* Check if section index has changed */
1893 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1895 dwarf64_findsect(s->section);
1897 /* do nothing unless line or file has changed */
1898 if (debug_immcall)
1900 ln = currentline - dwarf_csect->line;
1901 aa = s->offset - dwarf_csect->offset;
1902 inx = dwarf_clist->line;
1903 plinep = dwarf_csect->psaa;
1904 /* check for file change */
1905 if (!(inx == dwarf_csect->file))
1907 saa_write8(plinep,DW_LNS_set_file);
1908 saa_write8(plinep,inx);
1909 dwarf_csect->file = inx;
1911 /* check for line change */
1912 if (ln)
1914 /* test if in range of special op code */
1915 maxln = line_base + line_range;
1916 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1917 if (ln >= line_base && ln < maxln && soc < 256)
1919 saa_write8(plinep,soc);
1921 else
1923 if (ln)
1925 saa_write8(plinep,DW_LNS_advance_line);
1926 saa_wleb128s(plinep,ln);
1928 if (aa)
1930 saa_write8(plinep,DW_LNS_advance_pc);
1931 saa_wleb128u(plinep,aa);
1934 dwarf_csect->line = currentline;
1935 dwarf_csect->offset = s->offset;
1937 /* show change handled */
1938 debug_immcall = 0;
1943 static void dwarf64_generate(void)
1945 uint8_t *pbuf;
1946 int indx;
1947 struct linelist *ftentry;
1948 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1949 struct SAA *parangesrel, *plinesrel, *pinforel;
1950 struct sectlist *psect;
1951 size_t saalen, linepoff, totlen, highaddr;
1953 /* write epilogues for each line program range */
1954 /* and build aranges section */
1955 paranges = saa_init(1L);
1956 parangesrel = saa_init(1L);
1957 saa_write16(paranges,3); /* dwarf version */
1958 saa_write64(parangesrel, paranges->datalen+4);
1959 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1960 saa_write64(parangesrel, 0);
1961 saa_write32(paranges,0); /* offset into info */
1962 saa_write8(paranges,8); /* pointer size */
1963 saa_write8(paranges,0); /* not segmented */
1964 saa_write32(paranges,0); /* padding */
1965 /* iterate though sectlist entries */
1966 psect = dwarf_fsect;
1967 totlen = 0;
1968 highaddr = 0;
1969 for (indx = 0; indx < dwarf_nsections; indx++)
1971 plinep = psect->psaa;
1972 /* Line Number Program Epilogue */
1973 saa_write8(plinep,2); /* std op 2 */
1974 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1975 saa_write8(plinep,DW_LNS_extended_op);
1976 saa_write8(plinep,1); /* operand length */
1977 saa_write8(plinep,DW_LNE_end_sequence);
1978 totlen += plinep->datalen;
1979 /* range table relocation entry */
1980 saa_write64(parangesrel, paranges->datalen + 4);
1981 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1982 saa_write64(parangesrel, (uint64_t) 0);
1983 /* range table entry */
1984 saa_write64(paranges,0x0000); /* range start */
1985 saa_write64(paranges,sects[psect->section]->len); /* range length */
1986 highaddr += sects[psect->section]->len;
1987 /* done with this entry */
1988 psect = psect->next;
1990 saa_write64(paranges,0); /* null address */
1991 saa_write64(paranges,0); /* null length */
1992 saalen = paranges->datalen;
1993 arangeslen = saalen + 4;
1994 arangesbuf = pbuf = nasm_malloc(arangeslen);
1995 WRITELONG(pbuf,saalen); /* initial length */
1996 saa_rnbytes(paranges, pbuf, saalen);
1997 saa_free(paranges);
1999 /* build rela.aranges section */
2000 arangesrellen = saalen = parangesrel->datalen;
2001 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2002 saa_rnbytes(parangesrel, pbuf, saalen);
2003 saa_free(parangesrel);
2005 /* build pubnames section */
2006 ppubnames = saa_init(1L);
2007 saa_write16(ppubnames,3); /* dwarf version */
2008 saa_write32(ppubnames,0); /* offset into info */
2009 saa_write32(ppubnames,0); /* space used in info */
2010 saa_write32(ppubnames,0); /* end of list */
2011 saalen = ppubnames->datalen;
2012 pubnameslen = saalen + 4;
2013 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2014 WRITELONG(pbuf,saalen); /* initial length */
2015 saa_rnbytes(ppubnames, pbuf, saalen);
2016 saa_free(ppubnames);
2018 /* build info section */
2019 pinfo = saa_init(1L);
2020 pinforel = saa_init(1L);
2021 saa_write16(pinfo,3); /* dwarf version */
2022 saa_write64(pinforel, pinfo->datalen + 4);
2023 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2024 saa_write64(pinforel, 0);
2025 saa_write32(pinfo,0); /* offset into abbrev */
2026 saa_write8(pinfo,8); /* pointer size */
2027 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2028 saa_write64(pinforel, pinfo->datalen + 4);
2029 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2030 saa_write64(pinforel, 0);
2031 saa_write64(pinfo,0); /* DW_AT_low_pc */
2032 saa_write64(pinforel, pinfo->datalen + 4);
2033 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2034 saa_write64(pinforel, 0);
2035 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2036 saa_write64(pinforel, pinfo->datalen + 4);
2037 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2038 saa_write64(pinforel, 0);
2039 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2040 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2041 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2042 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2043 saa_write8(pinfo,2); /* abbrviation number LEB128u */
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,0); /* DW_AT_low_pc */
2048 saa_write64(pinfo,0); /* DW_AT_frame_base */
2049 saa_write8(pinfo,0); /* end of entries */
2050 saalen = pinfo->datalen;
2051 infolen = saalen + 4;
2052 infobuf = pbuf = nasm_malloc(infolen);
2053 WRITELONG(pbuf,saalen); /* initial length */
2054 saa_rnbytes(pinfo, pbuf, saalen);
2055 saa_free(pinfo);
2057 /* build rela.info section */
2058 inforellen = saalen = pinforel->datalen;
2059 inforelbuf = pbuf = nasm_malloc(inforellen);
2060 saa_rnbytes(pinforel, pbuf, saalen);
2061 saa_free(pinforel);
2063 /* build abbrev section */
2064 pabbrev = saa_init(1L);
2065 saa_write8(pabbrev,1); /* entry number LEB128u */
2066 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2067 saa_write8(pabbrev,1); /* has children */
2068 /* the following attributes and forms are all LEB128u values */
2069 saa_write8(pabbrev,DW_AT_low_pc);
2070 saa_write8(pabbrev,DW_FORM_addr);
2071 saa_write8(pabbrev,DW_AT_high_pc);
2072 saa_write8(pabbrev,DW_FORM_addr);
2073 saa_write8(pabbrev,DW_AT_stmt_list);
2074 saa_write8(pabbrev,DW_FORM_data4);
2075 saa_write8(pabbrev,DW_AT_name);
2076 saa_write8(pabbrev,DW_FORM_string);
2077 saa_write8(pabbrev,DW_AT_producer);
2078 saa_write8(pabbrev,DW_FORM_string);
2079 saa_write8(pabbrev,DW_AT_language);
2080 saa_write8(pabbrev,DW_FORM_data2);
2081 saa_write16(pabbrev,0); /* end of entry */
2082 /* LEB128u usage same as above */
2083 saa_write8(pabbrev,2); /* entry number */
2084 saa_write8(pabbrev,DW_TAG_subprogram);
2085 saa_write8(pabbrev,0); /* no children */
2086 saa_write8(pabbrev,DW_AT_low_pc);
2087 saa_write8(pabbrev,DW_FORM_addr);
2088 saa_write8(pabbrev,DW_AT_frame_base);
2089 saa_write8(pabbrev,DW_FORM_data4);
2090 saa_write16(pabbrev,0); /* end of entry */
2091 abbrevlen = saalen = pabbrev->datalen;
2092 abbrevbuf = pbuf = nasm_malloc(saalen);
2093 saa_rnbytes(pabbrev, pbuf, saalen);
2094 saa_free(pabbrev);
2096 /* build line section */
2097 /* prolog */
2098 plines = saa_init(1L);
2099 saa_write8(plines,1); /* Minimum Instruction Length */
2100 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2101 saa_write8(plines,line_base); /* Line Base */
2102 saa_write8(plines,line_range); /* Line Range */
2103 saa_write8(plines,opcode_base); /* Opcode Base */
2104 /* standard opcode lengths (# of LEB128u operands) */
2105 saa_write8(plines,0); /* Std opcode 1 length */
2106 saa_write8(plines,1); /* Std opcode 2 length */
2107 saa_write8(plines,1); /* Std opcode 3 length */
2108 saa_write8(plines,1); /* Std opcode 4 length */
2109 saa_write8(plines,1); /* Std opcode 5 length */
2110 saa_write8(plines,0); /* Std opcode 6 length */
2111 saa_write8(plines,0); /* Std opcode 7 length */
2112 saa_write8(plines,0); /* Std opcode 8 length */
2113 saa_write8(plines,1); /* Std opcode 9 length */
2114 saa_write8(plines,0); /* Std opcode 10 length */
2115 saa_write8(plines,0); /* Std opcode 11 length */
2116 saa_write8(plines,1); /* Std opcode 12 length */
2117 /* Directory Table */
2118 saa_write8(plines,0); /* End of table */
2119 /* File Name Table */
2120 ftentry = dwarf_flist;
2121 for (indx = 0;indx<dwarf_numfiles;indx++)
2123 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2124 saa_write8(plines,0); /* directory LEB128u */
2125 saa_write8(plines,0); /* time LEB128u */
2126 saa_write8(plines,0); /* size LEB128u */
2127 ftentry = ftentry->next;
2129 saa_write8(plines,0); /* End of table */
2130 linepoff = plines->datalen;
2131 linelen = linepoff + totlen + 10;
2132 linebuf = pbuf = nasm_malloc(linelen);
2133 WRITELONG(pbuf,linelen-4); /* initial length */
2134 WRITESHORT(pbuf,3); /* dwarf version */
2135 WRITELONG(pbuf,linepoff); /* offset to line number program */
2136 /* write line header */
2137 saalen = linepoff;
2138 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2139 pbuf += linepoff;
2140 saa_free(plines);
2141 /* concatonate line program ranges */
2142 linepoff += 13;
2143 plinesrel = saa_init(1L);
2144 psect = dwarf_fsect;
2145 for (indx = 0; indx < dwarf_nsections; indx++)
2147 saa_write64(plinesrel, linepoff);
2148 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2149 saa_write64(plinesrel, (uint64_t) 0);
2150 plinep = psect->psaa;
2151 saalen = plinep->datalen;
2152 saa_rnbytes(plinep, pbuf, saalen);
2153 pbuf += saalen;
2154 linepoff += saalen;
2155 saa_free(plinep);
2156 /* done with this entry */
2157 psect = psect->next;
2161 /* build rela.lines section */
2162 linerellen =saalen = plinesrel->datalen;
2163 linerelbuf = pbuf = nasm_malloc(linerellen);
2164 saa_rnbytes(plinesrel, pbuf, saalen);
2165 saa_free(plinesrel);
2167 /* build frame section */
2168 framelen = 4;
2169 framebuf = pbuf = nasm_malloc(framelen);
2170 WRITELONG(pbuf,framelen-4); /* initial length */
2172 /* build loc section */
2173 loclen = 16;
2174 locbuf = pbuf = nasm_malloc(loclen);
2175 WRITEDLONG(pbuf,0); /* null beginning offset */
2176 WRITEDLONG(pbuf,0); /* null ending offset */
2179 static void dwarf64_cleanup(void)
2181 if (arangesbuf)
2182 nasm_free(arangesbuf);
2183 if (arangesrelbuf)
2184 nasm_free(arangesrelbuf);
2185 if (pubnamesbuf)
2186 nasm_free(pubnamesbuf);
2187 if (infobuf)
2188 nasm_free(infobuf);
2189 if (inforelbuf)
2190 nasm_free(inforelbuf);
2191 if (abbrevbuf)
2192 nasm_free(abbrevbuf);
2193 if (linebuf)
2194 nasm_free(linebuf);
2195 if (linerelbuf)
2196 nasm_free(linerelbuf);
2197 if (framebuf)
2198 nasm_free(framebuf);
2199 if (locbuf)
2200 nasm_free(locbuf);
2202 static void dwarf64_findfile(const char * fname)
2204 int finx;
2205 struct linelist *match;
2207 /* return if fname is current file name */
2208 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2209 /* search for match */
2210 else
2212 match = 0;
2213 if (dwarf_flist)
2215 match = dwarf_flist;
2216 for (finx = 0; finx < dwarf_numfiles; finx++)
2218 if (!(strcmp(fname, match->filename)))
2220 dwarf_clist = match;
2221 return;
2225 /* add file name to end of list */
2226 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2227 dwarf_numfiles++;
2228 dwarf_clist->line = dwarf_numfiles;
2229 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2230 strcpy(dwarf_clist->filename,fname);
2231 dwarf_clist->next = 0;
2232 /* if first entry */
2233 if (!dwarf_flist)
2235 dwarf_flist = dwarf_elist = dwarf_clist;
2236 dwarf_clist->last = 0;
2238 /* chain to previous entry */
2239 else
2241 dwarf_elist->next = dwarf_clist;
2242 dwarf_elist = dwarf_clist;
2246 /* */
2247 static void dwarf64_findsect(const int index)
2249 int sinx;
2250 struct sectlist *match;
2251 struct SAA *plinep;
2252 /* return if index is current section index */
2253 if (dwarf_csect && (dwarf_csect->section == index))
2255 return;
2257 /* search for match */
2258 else
2260 match = 0;
2261 if (dwarf_fsect)
2263 match = dwarf_fsect;
2264 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2266 if ((match->section == index))
2268 dwarf_csect = match;
2269 return;
2271 match = match->next;
2274 /* add entry to end of list */
2275 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2276 dwarf_nsections++;
2277 dwarf_csect->psaa = plinep = saa_init(1L);
2278 dwarf_csect->line = 1;
2279 dwarf_csect->offset = 0;
2280 dwarf_csect->file = 1;
2281 dwarf_csect->section = index;
2282 dwarf_csect->next = 0;
2283 /* set relocatable address at start of line program */
2284 saa_write8(plinep,DW_LNS_extended_op);
2285 saa_write8(plinep,9); /* operand length */
2286 saa_write8(plinep,DW_LNE_set_address);
2287 saa_write64(plinep,0); /* Start Address */
2288 /* if first entry */
2289 if (!dwarf_fsect)
2291 dwarf_fsect = dwarf_esect = dwarf_csect;
2292 dwarf_csect->last = 0;
2294 /* chain to previous entry */
2295 else
2297 dwarf_esect->next = dwarf_csect;
2298 dwarf_esect = dwarf_csect;
2303 #endif /* OF_ELF */