Add copyright notice to insns.dat
[nasm.git] / output / outelf64.c
blob59df79241b3e34d54227c97a2e5ae88efdd3049f
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 "output/outform.h"
23 #include "output/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_linenum(const char *filename, int32_t linenumber, int32_t);
197 static void stabs64_output(int, void *);
198 static void stabs64_generate(void);
199 static void stabs64_cleanup(void);
201 /* dwarf debugging routines */
202 static void dwarf64_init(struct ofmt *, void *, FILE *, efunc);
203 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
204 static void dwarf64_output(int, void *);
205 static void dwarf64_generate(void);
206 static void dwarf64_cleanup(void);
207 static void dwarf64_findfile(const char *);
208 static void dwarf64_findsect(const int);
211 * Special section numbers which are used to define ELF special
212 * symbols, which can be used with WRT to provide PIC relocation
213 * types.
215 static int32_t elf_gotpc_sect, elf_gotoff_sect;
216 static int32_t elf_got_sect, elf_plt_sect;
217 static int32_t elf_sym_sect;
218 static int32_t elf_gottpoff_sect;
220 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
222 maxbits = 64;
223 elffp = fp;
224 error = errfunc;
225 evaluate = eval;
226 (void)ldef; /* placate optimisers */
227 sects = NULL;
228 nsects = sectlen = 0;
229 syms = saa_init((int32_t)sizeof(struct Symbol));
230 nlocals = nglobs = ndebugs = 0;
231 bsym = raa_init();
232 strs = saa_init(1L);
233 saa_wbytes(strs, "\0", 1L);
234 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
235 strslen = 2 + strlen(elf_module);
236 shstrtab = NULL;
237 shstrtablen = shstrtabsize = 0;;
238 add_sectname("", "");
240 fwds = NULL;
242 elf_gotpc_sect = seg_alloc();
243 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
244 error);
245 elf_gotoff_sect = seg_alloc();
246 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
247 error);
248 elf_got_sect = seg_alloc();
249 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
250 error);
251 elf_plt_sect = seg_alloc();
252 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
253 error);
254 elf_sym_sect = seg_alloc();
255 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
256 error);
257 elf_gottpoff_sect = seg_alloc();
258 ldef("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false, &of_elf64,
259 error);
261 def_seg = seg_alloc();
265 static void elf_cleanup(int debuginfo)
267 struct Reloc *r;
268 int i;
270 (void)debuginfo;
272 elf_write();
273 fclose(elffp);
274 for (i = 0; i < nsects; i++) {
275 if (sects[i]->type != SHT_NOBITS)
276 saa_free(sects[i]->data);
277 if (sects[i]->head)
278 saa_free(sects[i]->rel);
279 while (sects[i]->head) {
280 r = sects[i]->head;
281 sects[i]->head = sects[i]->head->next;
282 nasm_free(r);
285 nasm_free(sects);
286 saa_free(syms);
287 raa_free(bsym);
288 saa_free(strs);
289 if (of_elf64.current_dfmt) {
290 of_elf64.current_dfmt->cleanup();
293 /* add entry to the elf .shstrtab section */
294 static void add_sectname(char *firsthalf, char *secondhalf)
296 int len = strlen(firsthalf) + strlen(secondhalf);
297 while (shstrtablen + len + 1 > shstrtabsize)
298 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
299 strcpy(shstrtab + shstrtablen, firsthalf);
300 strcat(shstrtab + shstrtablen, secondhalf);
301 shstrtablen += len + 1;
304 static int elf_make_section(char *name, int type, int flags, int align)
306 struct Section *s;
308 s = nasm_malloc(sizeof(*s));
310 if (type != SHT_NOBITS)
311 s->data = saa_init(1L);
312 s->head = NULL;
313 s->tail = &s->head;
314 s->len = s->size = 0;
315 s->nrelocs = 0;
316 if (!strcmp(name, ".text"))
317 s->index = def_seg;
318 else
319 s->index = seg_alloc();
320 add_sectname("", name);
321 s->name = nasm_malloc(1 + strlen(name));
322 strcpy(s->name, name);
323 s->type = type;
324 s->flags = flags;
325 s->align = align;
326 s->gsyms = NULL;
328 if (nsects >= sectlen)
329 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
330 sects[nsects++] = s;
332 return nsects - 1;
335 static int32_t elf_section_names(char *name, int pass, int *bits)
337 char *p;
338 uint32_t flags, flags_and, flags_or;
339 uint64_t align;
340 int type, i;
343 * Default is 64 bits.
345 if (!name) {
346 *bits = 64;
347 return def_seg;
350 p = name;
351 while (*p && !nasm_isspace(*p))
352 p++;
353 if (*p)
354 *p++ = '\0';
355 flags_and = flags_or = type = align = 0;
357 while (*p && nasm_isspace(*p))
358 p++;
359 while (*p) {
360 char *q = p;
361 while (*p && !nasm_isspace(*p))
362 p++;
363 if (*p)
364 *p++ = '\0';
365 while (*p && nasm_isspace(*p))
366 p++;
368 if (!nasm_strnicmp(q, "align=", 6)) {
369 align = atoi(q + 6);
370 if (align == 0)
371 align = 1;
372 if ((align - 1) & align) { /* means it's not a power of two */
373 error(ERR_NONFATAL, "section alignment %d is not"
374 " a power of two", align);
375 align = 1;
377 } else if (!nasm_stricmp(q, "alloc")) {
378 flags_and |= SHF_ALLOC;
379 flags_or |= SHF_ALLOC;
380 } else if (!nasm_stricmp(q, "noalloc")) {
381 flags_and |= SHF_ALLOC;
382 flags_or &= ~SHF_ALLOC;
383 } else if (!nasm_stricmp(q, "exec")) {
384 flags_and |= SHF_EXECINSTR;
385 flags_or |= SHF_EXECINSTR;
386 } else if (!nasm_stricmp(q, "noexec")) {
387 flags_and |= SHF_EXECINSTR;
388 flags_or &= ~SHF_EXECINSTR;
389 } else if (!nasm_stricmp(q, "write")) {
390 flags_and |= SHF_WRITE;
391 flags_or |= SHF_WRITE;
392 } else if (!nasm_stricmp(q, "tls")) {
393 flags_and |= SHF_TLS;
394 flags_or |= SHF_TLS;
395 } else if (!nasm_stricmp(q, "nowrite")) {
396 flags_and |= SHF_WRITE;
397 flags_or &= ~SHF_WRITE;
398 } else if (!nasm_stricmp(q, "progbits")) {
399 type = SHT_PROGBITS;
400 } else if (!nasm_stricmp(q, "nobits")) {
401 type = SHT_NOBITS;
402 } else if (pass == 1) {
403 error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
404 " declaration of section `%s'", q, name);
408 if (!strcmp(name, ".shstrtab") ||
409 !strcmp(name, ".symtab") ||
410 !strcmp(name, ".strtab")) {
411 error(ERR_NONFATAL, "attempt to redefine reserved section"
412 "name `%s'", name);
413 return NO_SEG;
416 for (i = 0; i < nsects; i++)
417 if (!strcmp(name, sects[i]->name))
418 break;
419 if (i == nsects) {
420 const struct elf_known_section *ks = elf_known_sections;
422 while (ks->name) {
423 if (!strcmp(name, ks->name))
424 break;
425 ks++;
428 type = type ? type : ks->type;
429 align = align ? align : ks->align;
430 flags = (ks->flags & ~flags_and) | flags_or;
432 i = elf_make_section(name, type, flags, align);
433 } else if (pass == 1) {
434 if ((type && sects[i]->type != type)
435 || (align && sects[i]->align != align)
436 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
437 error(ERR_WARNING, "incompatible section attributes ignored on"
438 " redeclaration of section `%s'", name);
441 return sects[i]->index;
444 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
445 int is_global, char *special)
447 int pos = strslen;
448 struct Symbol *sym;
449 bool special_used = false;
451 #if defined(DEBUG) && DEBUG>2
452 fprintf(stderr,
453 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
454 name, segment, offset, is_global, special);
455 #endif
456 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
458 * This is a NASM special symbol. We never allow it into
459 * the ELF symbol table, even if it's a valid one. If it
460 * _isn't_ a valid one, we should barf immediately.
462 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
463 strcmp(name, "..got") && strcmp(name, "..plt") &&
464 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
465 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
466 return;
469 if (is_global == 3) {
470 struct Symbol **s;
472 * Fix up a forward-reference symbol size from the first
473 * pass.
475 for (s = &fwds; *s; s = &(*s)->nextfwd)
476 if (!strcmp((*s)->name, name)) {
477 struct tokenval tokval;
478 expr *e;
479 char *p = special;
481 while (*p && !nasm_isspace(*p))
482 p++;
483 while (*p && nasm_isspace(*p))
484 p++;
485 stdscan_reset();
486 stdscan_bufptr = p;
487 tokval.t_type = TOKEN_INVALID;
488 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
489 if (e) {
490 if (!is_simple(e))
491 error(ERR_NONFATAL, "cannot use relocatable"
492 " expression as symbol size");
493 else
494 (*s)->size = reloc_value(e);
498 * Remove it from the list of unresolved sizes.
500 nasm_free((*s)->name);
501 *s = (*s)->nextfwd;
502 return;
504 return; /* it wasn't an important one */
507 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
508 strslen += 1 + strlen(name);
510 lastsym = sym = saa_wstruct(syms);
512 memset(&sym->symv, 0, sizeof(struct rbtree));
514 sym->strpos = pos;
515 sym->type = is_global ? SYM_GLOBAL : 0;
516 sym->other = STV_DEFAULT;
517 sym->size = 0;
518 if (segment == NO_SEG)
519 sym->section = SHN_ABS;
520 else {
521 int i;
522 sym->section = SHN_UNDEF;
523 if (nsects == 0 && segment == def_seg) {
524 int tempint;
525 if (segment != elf_section_names(".text", 2, &tempint))
526 error(ERR_PANIC,
527 "strange segment conditions in ELF driver");
528 sym->section = nsects;
529 } else {
530 for (i = 0; i < nsects; i++)
531 if (segment == sects[i]->index) {
532 sym->section = i + 1;
533 break;
538 if (is_global == 2) {
539 sym->size = offset;
540 sym->symv.key = 0;
541 sym->section = SHN_COMMON;
543 * We have a common variable. Check the special text to see
544 * if it's a valid number and power of two; if so, store it
545 * as the alignment for the common variable.
547 if (special) {
548 bool err;
549 sym->symv.key = readnum(special, &err);
550 if (err)
551 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
552 " valid number", special);
553 else if ((sym->symv.key | (sym->symv.key - 1))
554 != 2 * sym->symv.key - 1)
555 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
556 " power of two", special);
558 special_used = true;
559 } else
560 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
562 if (sym->type == SYM_GLOBAL) {
564 * If sym->section == SHN_ABS, then the first line of the
565 * else section would cause a core dump, because its a reference
566 * beyond the end of the section array.
567 * This behaviour is exhibited by this code:
568 * GLOBAL crash_nasm
569 * crash_nasm equ 0
570 * To avoid such a crash, such requests are silently discarded.
571 * This may not be the best solution.
573 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
574 bsym = raa_write(bsym, segment, nglobs);
575 } else if (sym->section != SHN_ABS) {
577 * This is a global symbol; so we must add it to the rbtree
578 * of global symbols in its section.
580 * In addition, we check the special text for symbol
581 * type and size information.
583 sects[sym->section-1]->gsyms =
584 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
586 if (special) {
587 int n = strcspn(special, " \t");
589 if (!nasm_strnicmp(special, "function", n))
590 sym->type |= STT_FUNC;
591 else if (!nasm_strnicmp(special, "data", n) ||
592 !nasm_strnicmp(special, "object", n))
593 sym->type |= STT_OBJECT;
594 else if (!nasm_strnicmp(special, "notype", n))
595 sym->type |= STT_NOTYPE;
596 else
597 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
598 n, special);
599 special += n;
601 while (nasm_isspace(*special))
602 ++special;
603 if (*special) {
604 n = strcspn(special, " \t");
605 if (!nasm_strnicmp(special, "default", n))
606 sym->other = STV_DEFAULT;
607 else if (!nasm_strnicmp(special, "internal", n))
608 sym->other = STV_INTERNAL;
609 else if (!nasm_strnicmp(special, "hidden", n))
610 sym->other = STV_HIDDEN;
611 else if (!nasm_strnicmp(special, "protected", n))
612 sym->other = STV_PROTECTED;
613 else
614 n = 0;
615 special += n;
618 if (*special) {
619 struct tokenval tokval;
620 expr *e;
621 int fwd = 0;
622 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
624 while (special[n] && nasm_isspace(special[n]))
625 n++;
627 * We have a size expression; attempt to
628 * evaluate it.
630 stdscan_reset();
631 stdscan_bufptr = special + n;
632 tokval.t_type = TOKEN_INVALID;
633 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
634 NULL);
635 if (fwd) {
636 sym->nextfwd = fwds;
637 fwds = sym;
638 sym->name = nasm_strdup(name);
639 } else if (e) {
640 if (!is_simple(e))
641 error(ERR_NONFATAL, "cannot use relocatable"
642 " expression as symbol size");
643 else
644 sym->size = reloc_value(e);
646 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
648 special_used = true;
651 * If TLS segment, mark symbol accordingly.
653 if (sects[sym->section - 1]->flags & SHF_TLS) {
654 sym->type &= 0xf0;
655 sym->type |= STT_TLS;
658 sym->globnum = nglobs;
659 nglobs++;
660 } else
661 nlocals++;
663 if (special && !special_used)
664 error(ERR_NONFATAL, "no special symbol features supported here");
667 static void elf_add_reloc(struct Section *sect, int32_t segment,
668 int64_t offset, int type)
670 struct Reloc *r;
671 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
672 sect->tail = &r->next;
673 r->next = NULL;
675 r->address = sect->len;
676 r->offset = offset;
677 if (segment == NO_SEG)
678 r->symbol = 0;
679 else {
680 int i;
681 r->symbol = 0;
682 for (i = 0; i < nsects; i++)
683 if (segment == sects[i]->index)
684 r->symbol = i + 2;
685 if (!r->symbol)
686 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
688 r->type = type;
690 sect->nrelocs++;
694 * This routine deals with ..got and ..sym relocations: the more
695 * complicated kinds. In shared-library writing, some relocations
696 * with respect to global symbols must refer to the precise symbol
697 * rather than referring to an offset from the base of the section
698 * _containing_ the symbol. Such relocations call to this routine,
699 * which searches the symbol list for the symbol in question.
701 * R_386_GOT32 references require the _exact_ symbol address to be
702 * used; R_386_32 references can be at an offset from the symbol.
703 * The boolean argument `exact' tells us this.
705 * Return value is the adjusted value of `addr', having become an
706 * offset from the symbol rather than the section. Should always be
707 * zero when returning from an exact call.
709 * Limitation: if you define two symbols at the same place,
710 * confusion will occur.
712 * Inefficiency: we search, currently, using a linked list which
713 * isn't even necessarily sorted.
715 static void elf_add_gsym_reloc(struct Section *sect,
716 int32_t segment, uint64_t offset, int64_t pcrel,
717 int type, bool exact)
719 struct Reloc *r;
720 struct Section *s;
721 struct Symbol *sym;
722 struct rbtree *srb;
723 int i;
726 * First look up the segment/offset pair and find a global
727 * symbol corresponding to it. If it's not one of our segments,
728 * then it must be an external symbol, in which case we're fine
729 * doing a normal elf_add_reloc after first sanity-checking
730 * that the offset from the symbol is zero.
732 s = NULL;
733 for (i = 0; i < nsects; i++)
734 if (segment == sects[i]->index) {
735 s = sects[i];
736 break;
739 if (!s) {
740 if (exact && offset)
741 error(ERR_NONFATAL, "invalid access to an external symbol");
742 else
743 elf_add_reloc(sect, segment, offset - pcrel, type);
744 return;
747 srb = rb_search(s->gsyms, offset);
748 if (!srb || (exact && srb->key != offset)) {
749 error(ERR_NONFATAL, "unable to find a suitable global symbol"
750 " for this reference");
751 return;
753 sym = container_of(srb, struct Symbol, symv);
755 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
756 sect->tail = &r->next;
757 r->next = NULL;
759 r->address = sect->len;
760 r->offset = offset - pcrel - sym->symv.key;
761 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
762 r->type = type;
764 sect->nrelocs++;
767 static void elf_out(int32_t segto, const void *data,
768 enum out_type type, uint64_t size,
769 int32_t segment, int32_t wrt)
771 struct Section *s;
772 int64_t addr, zero;
773 int i;
774 static struct symlininfo sinfo;
776 zero = 0;
778 #if defined(DEBUG) && DEBUG>2
779 if (data) fprintf(stderr,
780 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
781 currentline, type, segment, segto, size, *(int64_t *)data);
782 else fprintf(stderr,
783 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
784 currentline, type, segment, segto, size);
785 #endif
788 * handle absolute-assembly (structure definitions)
790 if (segto == NO_SEG) {
791 if (type != OUT_RESERVE)
792 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
793 " space");
794 return;
797 s = NULL;
798 for (i = 0; i < nsects; i++)
799 if (segto == sects[i]->index) {
800 s = sects[i];
801 break;
803 if (!s) {
804 int tempint; /* ignored */
805 if (segto != elf_section_names(".text", 2, &tempint))
806 error(ERR_PANIC, "strange segment conditions in ELF driver");
807 else {
808 s = sects[nsects - 1];
809 i = nsects - 1;
812 /* invoke current debug_output routine */
813 if (of_elf64.current_dfmt) {
814 sinfo.offset = s->len;
815 sinfo.section = i;
816 sinfo.segto = segto;
817 sinfo.name = s->name;
818 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
820 /* end of debugging stuff */
822 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
823 error(ERR_WARNING, "attempt to initialize memory in"
824 " BSS section `%s': ignored", s->name);
825 s->len += realsize(type, size);
826 return;
829 if (type == OUT_RESERVE) {
830 if (s->type == SHT_PROGBITS) {
831 error(ERR_WARNING, "uninitialized space declared in"
832 " non-BSS section `%s': zeroing", s->name);
833 elf_sect_write(s, NULL, size);
834 } else
835 s->len += size;
836 } else if (type == OUT_RAWDATA) {
837 if (segment != NO_SEG)
838 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
839 elf_sect_write(s, data, size);
840 } else if (type == OUT_ADDRESS) {
841 addr = *(int64_t *)data;
842 if (segment == NO_SEG) {
843 /* Do nothing */
844 } else if (segment % 2) {
845 error(ERR_NONFATAL, "ELF format does not support"
846 " segment base references");
847 } else {
848 if (wrt == NO_SEG) {
849 switch ((int)size) {
850 case 1:
851 elf_add_reloc(s, segment, addr, R_X86_64_8);
852 break;
853 case 2:
854 elf_add_reloc(s, segment, addr, R_X86_64_16);
855 break;
856 case 4:
857 elf_add_reloc(s, segment, addr, R_X86_64_32);
858 break;
859 case 8:
860 elf_add_reloc(s, segment, addr, R_X86_64_64);
861 break;
862 default:
863 error(ERR_PANIC, "internal error elf64-hpa-871");
864 break;
866 addr = 0;
867 } else if (wrt == elf_gotpc_sect + 1) {
869 * The user will supply GOT relative to $$. ELF
870 * will let us have GOT relative to $. So we
871 * need to fix up the data item by $-$$.
873 addr += s->len;
874 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
875 addr = 0;
876 } else if (wrt == elf_gotoff_sect + 1) {
877 if (size != 8) {
878 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
879 "references to be qword");
880 } else {
881 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
882 addr = 0;
884 } else if (wrt == elf_got_sect + 1) {
885 switch ((int)size) {
886 case 4:
887 elf_add_gsym_reloc(s, segment, addr, 0,
888 R_X86_64_GOT32, true);
889 addr = 0;
890 break;
891 case 8:
892 elf_add_gsym_reloc(s, segment, addr, 0,
893 R_X86_64_GOT64, true);
894 addr = 0;
895 break;
896 default:
897 error(ERR_NONFATAL, "invalid ..got reference");
898 break;
900 } else if (wrt == elf_sym_sect + 1) {
901 switch ((int)size) {
902 case 1:
903 elf_add_gsym_reloc(s, segment, addr, 0,
904 R_X86_64_8, false);
905 addr = 0;
906 break;
907 case 2:
908 elf_add_gsym_reloc(s, segment, addr, 0,
909 R_X86_64_16, false);
910 addr = 0;
911 break;
912 case 4:
913 elf_add_gsym_reloc(s, segment, addr, 0,
914 R_X86_64_32, false);
915 addr = 0;
916 break;
917 case 8:
918 elf_add_gsym_reloc(s, segment, addr, 0,
919 R_X86_64_64, false);
920 addr = 0;
921 break;
922 default:
923 error(ERR_PANIC, "internal error elf64-hpa-903");
924 break;
926 } else if (wrt == elf_plt_sect + 1) {
927 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
928 "relative PLT references");
929 } else {
930 error(ERR_NONFATAL, "ELF format does not support this"
931 " use of WRT");
934 elf_sect_writeaddr(s, addr, size);
935 } else if (type == OUT_REL2ADR) {
936 addr = *(int64_t *)data - size;
937 if (segment == segto)
938 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
939 if (segment == NO_SEG) {
940 /* Do nothing */
941 } else if (segment % 2) {
942 error(ERR_NONFATAL, "ELF format does not support"
943 " segment base references");
944 } else {
945 if (wrt == NO_SEG) {
946 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
947 addr = 0;
948 } else {
949 error(ERR_NONFATAL,
950 "Unsupported non-32-bit ELF relocation [2]");
953 elf_sect_writeaddr(s, addr, 2);
954 } else if (type == OUT_REL4ADR) {
955 addr = *(int64_t *)data - size;
956 if (segment == segto)
957 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
958 if (segment == NO_SEG) {
959 /* Do nothing */
960 } else if (segment % 2) {
961 error(ERR_NONFATAL, "ELF64 format does not support"
962 " segment base references");
963 } else {
964 if (wrt == NO_SEG) {
965 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
966 addr = 0;
967 } else if (wrt == elf_plt_sect + 1) {
968 elf_add_gsym_reloc(s, segment, addr+size, size,
969 R_X86_64_PLT32, true);
970 addr = 0;
971 } else if (wrt == elf_gotpc_sect + 1 ||
972 wrt == elf_got_sect + 1) {
973 elf_add_gsym_reloc(s, segment, addr+size, size,
974 R_X86_64_GOTPCREL, true);
975 addr = 0;
976 } else if (wrt == elf_gotoff_sect + 1 ||
977 wrt == elf_got_sect + 1) {
978 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
979 "qword absolute");
980 } else if (wrt == elf_gottpoff_sect + 1) {
981 elf_add_gsym_reloc(s, segment, addr+size, size,
982 R_X86_64_GOTTPOFF, true);
983 addr = 0;
984 } else {
985 error(ERR_NONFATAL, "ELF64 format does not support this"
986 " use of WRT");
989 elf_sect_writeaddr(s, addr, 4);
990 } else if (type == OUT_REL8ADR) {
991 addr = *(int64_t *)data - size;
992 if (segment == segto)
993 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
994 if (segment == NO_SEG) {
995 /* Do nothing */
996 } else if (segment % 2) {
997 error(ERR_NONFATAL, "ELF64 format does not support"
998 " segment base references");
999 } else {
1000 if (wrt == NO_SEG) {
1001 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1002 addr = 0;
1003 } else if (wrt == elf_gotpc_sect + 1 ||
1004 wrt == elf_got_sect + 1) {
1005 elf_add_gsym_reloc(s, segment, addr+size, size,
1006 R_X86_64_GOTPCREL64, true);
1007 addr = 0;
1008 } else if (wrt == elf_gotoff_sect + 1 ||
1009 wrt == elf_got_sect + 1) {
1010 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1011 "absolute");
1012 } else if (wrt == elf_gottpoff_sect + 1) {
1013 error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1014 "dword");
1015 } else {
1016 error(ERR_NONFATAL, "ELF64 format does not support this"
1017 " use of WRT");
1020 elf_sect_writeaddr(s, addr, 8);
1024 static void elf_write(void)
1026 int align;
1027 char *p;
1028 int i;
1030 struct SAA *symtab;
1031 int32_t symtablen, symtablocal;
1034 * Work out how many sections we will have. We have SHN_UNDEF,
1035 * then the flexible user sections, then the fixed sections
1036 * `.shstrtab', `.symtab' and `.strtab', then optionally
1037 * relocation sections for the user sections.
1039 nsections = sec_numspecial + 1;
1040 if (of_elf64.current_dfmt == &df_stabs)
1041 nsections += 3;
1042 else if (of_elf64.current_dfmt == &df_dwarf)
1043 nsections += 10;
1045 add_sectname("", ".shstrtab");
1046 add_sectname("", ".symtab");
1047 add_sectname("", ".strtab");
1048 for (i = 0; i < nsects; i++) {
1049 nsections++; /* for the section itself */
1050 if (sects[i]->head) {
1051 nsections++; /* for its relocations */
1052 add_sectname(".rela", sects[i]->name);
1056 if (of_elf64.current_dfmt == &df_stabs) {
1057 /* in case the debug information is wanted, just add these three sections... */
1058 add_sectname("", ".stab");
1059 add_sectname("", ".stabstr");
1060 add_sectname(".rel", ".stab");
1063 else if (of_elf64.current_dfmt == &df_dwarf) {
1064 /* the dwarf debug standard specifies the following ten sections,
1065 not all of which are currently implemented,
1066 although all of them are defined. */
1067 #define debug_aranges (int64_t) (nsections-10)
1068 #define debug_info (int64_t) (nsections-7)
1069 #define debug_abbrev (int64_t) (nsections-5)
1070 #define debug_line (int64_t) (nsections-4)
1071 add_sectname("", ".debug_aranges");
1072 add_sectname(".rela", ".debug_aranges");
1073 add_sectname("", ".debug_pubnames");
1074 add_sectname("", ".debug_info");
1075 add_sectname(".rela", ".debug_info");
1076 add_sectname("", ".debug_abbrev");
1077 add_sectname("", ".debug_line");
1078 add_sectname(".rela", ".debug_line");
1079 add_sectname("", ".debug_frame");
1080 add_sectname("", ".debug_loc");
1084 * Output the ELF header.
1086 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1087 fputc(elf_osabi, elffp);
1088 fputc(elf_abiver, elffp);
1089 fwritezero(7, elffp);
1090 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1091 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1092 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1093 fwriteint64_t(0L, elffp); /* no entry point */
1094 fwriteint64_t(0L, elffp); /* no program header table */
1095 fwriteint64_t(0x40L, elffp); /* section headers straight after
1096 * ELF header plus alignment */
1097 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1098 fwriteint16_t(0x40, elffp); /* size of ELF header */
1099 fwriteint16_t(0, elffp); /* no program header table, again */
1100 fwriteint16_t(0, elffp); /* still no program header table */
1101 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1102 fwriteint16_t(nsections, elffp); /* number of sections */
1103 fwriteint16_t(sec_shstrtab, elffp); /* string table section index for
1104 * section header table */
1107 * Build the symbol table and relocation tables.
1109 symtab = elf_build_symtab(&symtablen, &symtablocal);
1110 for (i = 0; i < nsects; i++)
1111 if (sects[i]->head)
1112 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1113 sects[i]->head);
1116 * Now output the section header table.
1119 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1120 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1121 elf_foffs += align;
1122 elf_nsect = 0;
1123 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1125 /* SHN_UNDEF */
1126 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1127 p = shstrtab + 1;
1129 /* The normal sections */
1130 for (i = 0; i < nsects; i++) {
1131 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1132 (sects[i]->type == SHT_PROGBITS ?
1133 sects[i]->data : NULL), true,
1134 sects[i]->len, 0, 0, sects[i]->align, 0);
1135 p += strlen(p) + 1;
1138 /* .shstrtab */
1139 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1140 shstrtablen, 0, 0, 1, 0);
1141 p += strlen(p) + 1;
1143 /* .symtab */
1144 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1145 symtablen, sec_strtab, symtablocal, 4, 24);
1146 p += strlen(p) + 1;
1148 /* .strtab */
1149 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, strs, true,
1150 strslen, 0, 0, 1, 0);
1151 p += strlen(p) + 1;
1153 /* The relocation sections */
1154 for (i = 0; i < nsects; i++)
1155 if (sects[i]->head) {
1156 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1157 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1158 p += strlen(p) + 1;
1161 if (of_elf64.current_dfmt == &df_stabs) {
1162 /* for debugging information, create the last three sections
1163 which are the .stab , .stabstr and .rel.stab sections respectively */
1165 /* this function call creates the stab sections in memory */
1166 stabs64_generate();
1168 if (stabbuf && stabstrbuf && stabrelbuf) {
1169 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1170 stablen, sec_stabstr, 0, 4, 12);
1171 p += strlen(p) + 1;
1173 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1174 stabstrlen, 0, 0, 4, 0);
1175 p += strlen(p) + 1;
1177 /* link -> symtable info -> section to refer to */
1178 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1179 stabrellen, symtabsection, sec_stab, 4, 16);
1180 p += strlen(p) + 1;
1183 else if (of_elf64.current_dfmt == &df_dwarf) {
1184 /* for dwarf debugging information, create the ten dwarf sections */
1186 /* this function call creates the dwarf sections in memory */
1187 if (dwarf_fsect)
1188 dwarf64_generate();
1190 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1191 arangeslen, 0, 0, 1, 0);
1192 p += strlen(p) + 1;
1194 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1195 arangesrellen, symtabsection, debug_aranges, 1, 24);
1196 p += strlen(p) + 1;
1198 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1199 pubnameslen, 0, 0, 1, 0);
1200 p += strlen(p) + 1;
1202 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1203 infolen, 0, 0, 1, 0);
1204 p += strlen(p) + 1;
1206 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1207 inforellen, symtabsection, debug_info, 1, 24);
1208 p += strlen(p) + 1;
1210 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1211 abbrevlen, 0, 0, 1, 0);
1212 p += strlen(p) + 1;
1214 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1215 linelen, 0, 0, 1, 0);
1216 p += strlen(p) + 1;
1218 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1219 linerellen, symtabsection, debug_line, 1, 24);
1220 p += strlen(p) + 1;
1222 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1223 framelen, 0, 0, 8, 0);
1224 p += strlen(p) + 1;
1226 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1227 loclen, 0, 0, 1, 0);
1228 p += strlen(p) + 1;
1230 fwritezero(align, elffp);
1233 * Now output the sections.
1235 elf_write_sections();
1237 nasm_free(elf_sects);
1238 saa_free(symtab);
1241 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1243 struct SAA *s = saa_init(1L);
1244 struct Symbol *sym;
1245 uint8_t entry[24], *p;
1246 int i;
1248 *len = *local = 0;
1251 * First, an all-zeros entry, required by the ELF spec.
1253 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1254 *len += 24;
1255 (*local)++;
1258 * Next, an entry for the file name.
1260 p = entry;
1261 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1262 WRITESHORT(p, STT_FILE); /* type FILE */
1263 WRITESHORT(p, SHN_ABS);
1264 WRITEDLONG(p, (uint64_t) 0); /* no value */
1265 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1266 saa_wbytes(s, entry, 24L);
1267 *len += 24;
1268 (*local)++;
1271 * Now some standard symbols defining the segments, for relocation
1272 * purposes.
1274 for (i = 1; i <= nsects; i++) {
1275 p = entry;
1276 WRITELONG(p, 0); /* no symbol name */
1277 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1278 WRITESHORT(p, i); /* section id */
1279 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1280 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1281 saa_wbytes(s, entry, 24L);
1282 *len += 24;
1283 (*local)++;
1288 * Now the other local symbols.
1290 saa_rewind(syms);
1291 while ((sym = saa_rstruct(syms))) {
1292 if (sym->type & SYM_GLOBAL)
1293 continue;
1294 p = entry;
1295 WRITELONG(p, sym->strpos); /* index into symbol string table */
1296 WRITECHAR(p, sym->type); /* type and binding */
1297 WRITECHAR(p, sym->other); /* visibility */
1298 WRITESHORT(p, sym->section); /* index into section header table */
1299 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1300 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1301 saa_wbytes(s, entry, 24L);
1302 *len += 24;
1303 (*local)++;
1306 * dwarf needs symbols for debug sections
1307 * which are relocation targets.
1309 if (of_elf64.current_dfmt == &df_dwarf) {
1310 dwarf_infosym = *local;
1311 p = entry;
1312 WRITELONG(p, 0); /* no symbol name */
1313 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1314 WRITESHORT(p, debug_info); /* section id */
1315 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1316 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1317 saa_wbytes(s, entry, 24L);
1318 *len += 24;
1319 (*local)++;
1320 dwarf_abbrevsym = *local;
1321 p = entry;
1322 WRITELONG(p, 0); /* no symbol name */
1323 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1324 WRITESHORT(p, debug_abbrev); /* section id */
1325 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1326 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1327 saa_wbytes(s, entry, 24L);
1328 *len += 24;
1329 (*local)++;
1330 dwarf_linesym = *local;
1331 p = entry;
1332 WRITELONG(p, 0); /* no symbol name */
1333 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1334 WRITESHORT(p, debug_line); /* section id */
1335 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1336 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1337 saa_wbytes(s, entry, 24L);
1338 *len += 24;
1339 (*local)++;
1343 * Now the global symbols.
1345 saa_rewind(syms);
1346 while ((sym = saa_rstruct(syms))) {
1347 if (!(sym->type & SYM_GLOBAL))
1348 continue;
1349 p = entry;
1350 WRITELONG(p, sym->strpos);
1351 WRITECHAR(p, sym->type); /* type and binding */
1352 WRITECHAR(p, sym->other); /* visibility */
1353 WRITESHORT(p, sym->section);
1354 WRITEDLONG(p, (int64_t)sym->symv.key);
1355 WRITEDLONG(p, (int64_t)sym->size);
1356 saa_wbytes(s, entry, 24L);
1357 *len += 24;
1360 return s;
1363 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1365 struct SAA *s;
1366 uint8_t *p, entry[24];
1367 int32_t global_offset;
1369 if (!r)
1370 return NULL;
1372 s = saa_init(1L);
1373 *len = 0;
1376 * How to onvert from a global placeholder to a real symbol index;
1377 * the +2 refers to the two special entries, the null entry and
1378 * the filename entry.
1380 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1382 while (r) {
1383 int32_t sym = r->symbol;
1385 if (sym >= GLOBAL_TEMP_BASE)
1386 sym += global_offset;
1388 p = entry;
1389 WRITEDLONG(p, r->address);
1390 WRITELONG(p, r->type);
1391 WRITELONG(p, sym);
1392 WRITEDLONG(p, r->offset);
1393 saa_wbytes(s, entry, 24L);
1394 *len += 24;
1396 r = r->next;
1399 return s;
1402 static void elf_section_header(int name, int type, uint64_t flags,
1403 void *data, bool is_saa, uint64_t datalen,
1404 int link, int info, int align, int eltsize)
1406 elf_sects[elf_nsect].data = data;
1407 elf_sects[elf_nsect].len = datalen;
1408 elf_sects[elf_nsect].is_saa = is_saa;
1409 elf_nsect++;
1411 fwriteint32_t((int32_t)name, elffp);
1412 fwriteint32_t((int32_t)type, elffp);
1413 fwriteint64_t((int64_t)flags, elffp);
1414 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1415 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1416 fwriteint64_t(datalen, elffp);
1417 if (data)
1418 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1419 fwriteint32_t((int32_t)link, elffp);
1420 fwriteint32_t((int32_t)info, elffp);
1421 fwriteint64_t((int64_t)align, elffp);
1422 fwriteint64_t((int64_t)eltsize, elffp);
1425 static void elf_write_sections(void)
1427 int i;
1428 for (i = 0; i < elf_nsect; i++)
1429 if (elf_sects[i].data) {
1430 int32_t len = elf_sects[i].len;
1431 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1432 int32_t align = reallen - len;
1433 if (elf_sects[i].is_saa)
1434 saa_fpwrite(elf_sects[i].data, elffp);
1435 else
1436 fwrite(elf_sects[i].data, len, 1, elffp);
1437 fwritezero(align, elffp);
1441 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1443 saa_wbytes(sect->data, data, len);
1444 sect->len += len;
1446 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1448 saa_writeaddr(sect->data, data, len);
1449 sect->len += len;
1452 static int32_t elf_segbase(int32_t segment)
1454 return segment;
1457 static int elf_directive(char *directive, char *value, int pass)
1459 bool err;
1460 int64_t n;
1461 char *p;
1463 if (!strcmp(directive, "osabi")) {
1464 if (pass == 2)
1465 return 1; /* ignore in pass 2 */
1467 n = readnum(value, &err);
1468 if (err) {
1469 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1470 return 1;
1472 if (n < 0 || n > 255) {
1473 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1474 return 1;
1476 elf_osabi = n;
1477 elf_abiver = 0;
1479 if ((p = strchr(value,',')) == NULL)
1480 return 1;
1482 n = readnum(p+1, &err);
1483 if (err || n < 0 || n > 255) {
1484 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1485 return 1;
1488 elf_abiver = n;
1489 return 1;
1492 return 0;
1495 static void elf_filename(char *inname, char *outname, efunc error)
1497 strcpy(elf_module, inname);
1498 standard_extension(inname, outname, ".o", error);
1501 extern macros_t elf_stdmac[];
1503 static int elf_set_info(enum geninfo type, char **val)
1505 (void)type;
1506 (void)val;
1507 return 0;
1509 static struct dfmt df_dwarf = {
1510 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1511 "dwarf",
1512 dwarf64_init,
1513 dwarf64_linenum,
1514 debug64_deflabel,
1515 debug64_directive,
1516 debug64_typevalue,
1517 dwarf64_output,
1518 dwarf64_cleanup
1520 static struct dfmt df_stabs = {
1521 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1522 "stabs",
1523 null_debug_init,
1524 stabs64_linenum,
1525 debug64_deflabel,
1526 debug64_directive,
1527 debug64_typevalue,
1528 stabs64_output,
1529 stabs64_cleanup
1532 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1534 struct ofmt of_elf64 = {
1535 "ELF64 (x86_64) object files (e.g. Linux)",
1536 "elf64",
1537 NULL,
1538 elf64_debugs_arr,
1539 &df_stabs,
1540 elf_stdmac,
1541 elf_init,
1542 elf_set_info,
1543 elf_out,
1544 elf_deflabel,
1545 elf_section_names,
1546 elf_segbase,
1547 elf_directive,
1548 elf_filename,
1549 elf_cleanup
1552 /* common debugging routines */
1553 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1554 int is_global, char *special)
1556 (void)name;
1557 (void)segment;
1558 (void)offset;
1559 (void)is_global;
1560 (void)special;
1563 static void debug64_directive(const char *directive, const char *params)
1565 (void)directive;
1566 (void)params;
1569 static void debug64_typevalue(int32_t type)
1571 int32_t stype, ssize;
1572 switch (TYM_TYPE(type)) {
1573 case TY_LABEL:
1574 ssize = 0;
1575 stype = STT_NOTYPE;
1576 break;
1577 case TY_BYTE:
1578 ssize = 1;
1579 stype = STT_OBJECT;
1580 break;
1581 case TY_WORD:
1582 ssize = 2;
1583 stype = STT_OBJECT;
1584 break;
1585 case TY_DWORD:
1586 ssize = 4;
1587 stype = STT_OBJECT;
1588 break;
1589 case TY_FLOAT:
1590 ssize = 4;
1591 stype = STT_OBJECT;
1592 break;
1593 case TY_QWORD:
1594 ssize = 8;
1595 stype = STT_OBJECT;
1596 break;
1597 case TY_TBYTE:
1598 ssize = 10;
1599 stype = STT_OBJECT;
1600 break;
1601 case TY_OWORD:
1602 ssize = 16;
1603 stype = STT_OBJECT;
1604 break;
1605 case TY_COMMON:
1606 ssize = 0;
1607 stype = STT_COMMON;
1608 break;
1609 case TY_SEG:
1610 ssize = 0;
1611 stype = STT_SECTION;
1612 break;
1613 case TY_EXTERN:
1614 ssize = 0;
1615 stype = STT_NOTYPE;
1616 break;
1617 case TY_EQU:
1618 ssize = 0;
1619 stype = STT_NOTYPE;
1620 break;
1621 default:
1622 ssize = 0;
1623 stype = STT_NOTYPE;
1624 break;
1626 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1627 lastsym->size = ssize;
1628 lastsym->type = stype;
1632 /* stabs debugging routines */
1634 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1636 (void)segto;
1637 if (!stabs_filename) {
1638 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1639 strcpy(stabs_filename, filename);
1640 } else {
1641 if (strcmp(stabs_filename, filename)) {
1642 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1643 in fact, this leak comes in quite handy to maintain a list of files
1644 encountered so far in the symbol lines... */
1646 /* why not nasm_free(stabs_filename); we're done with the old one */
1648 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1649 strcpy(stabs_filename, filename);
1652 debug_immcall = 1;
1653 currentline = linenumber;
1657 static void stabs64_output(int type, void *param)
1659 struct symlininfo *s;
1660 struct linelist *el;
1661 if (type == TY_DEBUGSYMLIN) {
1662 if (debug_immcall) {
1663 s = (struct symlininfo *)param;
1664 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1665 return; /* line info is only collected for executable sections */
1666 numlinestabs++;
1667 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1668 el->info.offset = s->offset;
1669 el->info.section = s->section;
1670 el->info.name = s->name;
1671 el->line = currentline;
1672 el->filename = stabs_filename;
1673 el->next = 0;
1674 if (stabslines) {
1675 stabslines->last->next = el;
1676 stabslines->last = el;
1677 } else {
1678 stabslines = el;
1679 stabslines->last = el;
1683 debug_immcall = 0;
1686 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1687 do {\
1688 WRITELONG(p,n_strx); \
1689 WRITECHAR(p,n_type); \
1690 WRITECHAR(p,n_other); \
1691 WRITESHORT(p,n_desc); \
1692 WRITELONG(p,n_value); \
1693 } while (0)
1695 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1697 static void stabs64_generate(void)
1699 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1700 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1701 char **allfiles;
1702 int *fileidx;
1704 struct linelist *ptr;
1706 ptr = stabslines;
1708 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1709 for (i = 0; i < numlinestabs; i++)
1710 allfiles[i] = 0;
1711 numfiles = 0;
1712 while (ptr) {
1713 if (numfiles == 0) {
1714 allfiles[0] = ptr->filename;
1715 numfiles++;
1716 } else {
1717 for (i = 0; i < numfiles; i++) {
1718 if (!strcmp(allfiles[i], ptr->filename))
1719 break;
1721 if (i >= numfiles) {
1722 allfiles[i] = ptr->filename;
1723 numfiles++;
1726 ptr = ptr->next;
1728 strsize = 1;
1729 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1730 for (i = 0; i < numfiles; i++) {
1731 fileidx[i] = strsize;
1732 strsize += strlen(allfiles[i]) + 1;
1734 mainfileindex = 0;
1735 for (i = 0; i < numfiles; i++) {
1736 if (!strcmp(allfiles[i], elf_module)) {
1737 mainfileindex = i;
1738 break;
1742 /* worst case size of the stab buffer would be:
1743 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1745 sbuf =
1746 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1747 sizeof(struct stabentry));
1749 ssbuf = (uint8_t *)nasm_malloc(strsize);
1751 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1752 rptr = rbuf;
1754 for (i = 0; i < numfiles; i++) {
1755 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1757 ssbuf[0] = 0;
1759 stabstrlen = strsize; /* set global variable for length of stab strings */
1761 sptr = sbuf;
1762 ptr = stabslines;
1763 numstabs = 0;
1765 if (ptr) {
1766 /* this is the first stab, its strx points to the filename of the
1767 the source-file, the n_desc field should be set to the number
1768 of remaining stabs
1770 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1772 /* this is the stab for the main source file */
1773 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1775 /* relocation table entry */
1777 /* Since the symbol table has two entries before */
1778 /* the section symbols, the index in the info.section */
1779 /* member must be adjusted by adding 2 */
1781 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1782 WRITELONG(rptr, R_X86_64_32);
1783 WRITELONG(rptr, ptr->info.section + 2);
1785 numstabs++;
1786 currfile = mainfileindex;
1789 while (ptr) {
1790 if (strcmp(allfiles[currfile], ptr->filename)) {
1791 /* oops file has changed... */
1792 for (i = 0; i < numfiles; i++)
1793 if (!strcmp(allfiles[i], ptr->filename))
1794 break;
1795 currfile = i;
1796 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1797 ptr->info.offset);
1798 numstabs++;
1800 /* relocation table entry */
1802 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1803 WRITELONG(rptr, R_X86_64_32);
1804 WRITELONG(rptr, ptr->info.section + 2);
1807 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1808 numstabs++;
1810 /* relocation table entry */
1812 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1813 WRITELONG(rptr, R_X86_64_32);
1814 WRITELONG(rptr, ptr->info.section + 2);
1816 ptr = ptr->next;
1820 ((struct stabentry *)sbuf)->n_desc = numstabs;
1822 nasm_free(allfiles);
1823 nasm_free(fileidx);
1825 stablen = (sptr - sbuf);
1826 stabrellen = (rptr - rbuf);
1827 stabrelbuf = rbuf;
1828 stabbuf = sbuf;
1829 stabstrbuf = ssbuf;
1832 static void stabs64_cleanup(void)
1834 struct linelist *ptr, *del;
1835 if (!stabslines)
1836 return;
1837 ptr = stabslines;
1838 while (ptr) {
1839 del = ptr;
1840 ptr = ptr->next;
1841 nasm_free(del);
1843 if (stabbuf)
1844 nasm_free(stabbuf);
1845 if (stabrelbuf)
1846 nasm_free(stabrelbuf);
1847 if (stabstrbuf)
1848 nasm_free(stabstrbuf);
1850 /* dwarf routines */
1851 static void dwarf64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1853 (void)of;
1854 (void)id;
1855 (void)fp;
1856 (void)error;
1858 ndebugs = 3; /* 3 debug symbols */
1861 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1862 int32_t segto)
1864 (void)segto;
1865 dwarf64_findfile(filename);
1866 debug_immcall = 1;
1867 currentline = linenumber;
1870 /* called from elf_out with type == TY_DEBUGSYMLIN */
1871 static void dwarf64_output(int type, void *param)
1873 int ln, aa, inx, maxln, soc;
1874 struct symlininfo *s;
1875 struct SAA *plinep;
1877 (void)type;
1879 s = (struct symlininfo *)param;
1880 /* line number info is only gathered for executable sections */
1881 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1882 return;
1883 /* Check if section index has changed */
1884 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1886 dwarf64_findsect(s->section);
1888 /* do nothing unless line or file has changed */
1889 if (debug_immcall)
1891 ln = currentline - dwarf_csect->line;
1892 aa = s->offset - dwarf_csect->offset;
1893 inx = dwarf_clist->line;
1894 plinep = dwarf_csect->psaa;
1895 /* check for file change */
1896 if (!(inx == dwarf_csect->file))
1898 saa_write8(plinep,DW_LNS_set_file);
1899 saa_write8(plinep,inx);
1900 dwarf_csect->file = inx;
1902 /* check for line change */
1903 if (ln)
1905 /* test if in range of special op code */
1906 maxln = line_base + line_range;
1907 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1908 if (ln >= line_base && ln < maxln && soc < 256)
1910 saa_write8(plinep,soc);
1912 else
1914 if (ln)
1916 saa_write8(plinep,DW_LNS_advance_line);
1917 saa_wleb128s(plinep,ln);
1919 if (aa)
1921 saa_write8(plinep,DW_LNS_advance_pc);
1922 saa_wleb128u(plinep,aa);
1925 dwarf_csect->line = currentline;
1926 dwarf_csect->offset = s->offset;
1928 /* show change handled */
1929 debug_immcall = 0;
1934 static void dwarf64_generate(void)
1936 uint8_t *pbuf;
1937 int indx;
1938 struct linelist *ftentry;
1939 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1940 struct SAA *parangesrel, *plinesrel, *pinforel;
1941 struct sectlist *psect;
1942 size_t saalen, linepoff, totlen, highaddr;
1944 /* write epilogues for each line program range */
1945 /* and build aranges section */
1946 paranges = saa_init(1L);
1947 parangesrel = saa_init(1L);
1948 saa_write16(paranges,3); /* dwarf version */
1949 saa_write64(parangesrel, paranges->datalen+4);
1950 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1951 saa_write64(parangesrel, 0);
1952 saa_write32(paranges,0); /* offset into info */
1953 saa_write8(paranges,8); /* pointer size */
1954 saa_write8(paranges,0); /* not segmented */
1955 saa_write32(paranges,0); /* padding */
1956 /* iterate though sectlist entries */
1957 psect = dwarf_fsect;
1958 totlen = 0;
1959 highaddr = 0;
1960 for (indx = 0; indx < dwarf_nsections; indx++)
1962 plinep = psect->psaa;
1963 /* Line Number Program Epilogue */
1964 saa_write8(plinep,2); /* std op 2 */
1965 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1966 saa_write8(plinep,DW_LNS_extended_op);
1967 saa_write8(plinep,1); /* operand length */
1968 saa_write8(plinep,DW_LNE_end_sequence);
1969 totlen += plinep->datalen;
1970 /* range table relocation entry */
1971 saa_write64(parangesrel, paranges->datalen + 4);
1972 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1973 saa_write64(parangesrel, (uint64_t) 0);
1974 /* range table entry */
1975 saa_write64(paranges,0x0000); /* range start */
1976 saa_write64(paranges,sects[psect->section]->len); /* range length */
1977 highaddr += sects[psect->section]->len;
1978 /* done with this entry */
1979 psect = psect->next;
1981 saa_write64(paranges,0); /* null address */
1982 saa_write64(paranges,0); /* null length */
1983 saalen = paranges->datalen;
1984 arangeslen = saalen + 4;
1985 arangesbuf = pbuf = nasm_malloc(arangeslen);
1986 WRITELONG(pbuf,saalen); /* initial length */
1987 saa_rnbytes(paranges, pbuf, saalen);
1988 saa_free(paranges);
1990 /* build rela.aranges section */
1991 arangesrellen = saalen = parangesrel->datalen;
1992 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1993 saa_rnbytes(parangesrel, pbuf, saalen);
1994 saa_free(parangesrel);
1996 /* build pubnames section */
1997 ppubnames = saa_init(1L);
1998 saa_write16(ppubnames,3); /* dwarf version */
1999 saa_write32(ppubnames,0); /* offset into info */
2000 saa_write32(ppubnames,0); /* space used in info */
2001 saa_write32(ppubnames,0); /* end of list */
2002 saalen = ppubnames->datalen;
2003 pubnameslen = saalen + 4;
2004 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2005 WRITELONG(pbuf,saalen); /* initial length */
2006 saa_rnbytes(ppubnames, pbuf, saalen);
2007 saa_free(ppubnames);
2009 /* build info section */
2010 pinfo = saa_init(1L);
2011 pinforel = saa_init(1L);
2012 saa_write16(pinfo,3); /* dwarf version */
2013 saa_write64(pinforel, pinfo->datalen + 4);
2014 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2015 saa_write64(pinforel, 0);
2016 saa_write32(pinfo,0); /* offset into abbrev */
2017 saa_write8(pinfo,8); /* pointer size */
2018 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2019 saa_write64(pinforel, pinfo->datalen + 4);
2020 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2021 saa_write64(pinforel, 0);
2022 saa_write64(pinfo,0); /* DW_AT_low_pc */
2023 saa_write64(pinforel, pinfo->datalen + 4);
2024 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2025 saa_write64(pinforel, 0);
2026 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2027 saa_write64(pinforel, pinfo->datalen + 4);
2028 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2029 saa_write64(pinforel, 0);
2030 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2031 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2032 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2033 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2034 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2035 saa_write64(pinforel, pinfo->datalen + 4);
2036 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2037 saa_write64(pinforel, 0);
2038 saa_write64(pinfo,0); /* DW_AT_low_pc */
2039 saa_write64(pinfo,0); /* DW_AT_frame_base */
2040 saa_write8(pinfo,0); /* end of entries */
2041 saalen = pinfo->datalen;
2042 infolen = saalen + 4;
2043 infobuf = pbuf = nasm_malloc(infolen);
2044 WRITELONG(pbuf,saalen); /* initial length */
2045 saa_rnbytes(pinfo, pbuf, saalen);
2046 saa_free(pinfo);
2048 /* build rela.info section */
2049 inforellen = saalen = pinforel->datalen;
2050 inforelbuf = pbuf = nasm_malloc(inforellen);
2051 saa_rnbytes(pinforel, pbuf, saalen);
2052 saa_free(pinforel);
2054 /* build abbrev section */
2055 pabbrev = saa_init(1L);
2056 saa_write8(pabbrev,1); /* entry number LEB128u */
2057 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2058 saa_write8(pabbrev,1); /* has children */
2059 /* the following attributes and forms are all LEB128u values */
2060 saa_write8(pabbrev,DW_AT_low_pc);
2061 saa_write8(pabbrev,DW_FORM_addr);
2062 saa_write8(pabbrev,DW_AT_high_pc);
2063 saa_write8(pabbrev,DW_FORM_addr);
2064 saa_write8(pabbrev,DW_AT_stmt_list);
2065 saa_write8(pabbrev,DW_FORM_data4);
2066 saa_write8(pabbrev,DW_AT_name);
2067 saa_write8(pabbrev,DW_FORM_string);
2068 saa_write8(pabbrev,DW_AT_producer);
2069 saa_write8(pabbrev,DW_FORM_string);
2070 saa_write8(pabbrev,DW_AT_language);
2071 saa_write8(pabbrev,DW_FORM_data2);
2072 saa_write16(pabbrev,0); /* end of entry */
2073 /* LEB128u usage same as above */
2074 saa_write8(pabbrev,2); /* entry number */
2075 saa_write8(pabbrev,DW_TAG_subprogram);
2076 saa_write8(pabbrev,0); /* no children */
2077 saa_write8(pabbrev,DW_AT_low_pc);
2078 saa_write8(pabbrev,DW_FORM_addr);
2079 saa_write8(pabbrev,DW_AT_frame_base);
2080 saa_write8(pabbrev,DW_FORM_data4);
2081 saa_write16(pabbrev,0); /* end of entry */
2082 abbrevlen = saalen = pabbrev->datalen;
2083 abbrevbuf = pbuf = nasm_malloc(saalen);
2084 saa_rnbytes(pabbrev, pbuf, saalen);
2085 saa_free(pabbrev);
2087 /* build line section */
2088 /* prolog */
2089 plines = saa_init(1L);
2090 saa_write8(plines,1); /* Minimum Instruction Length */
2091 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2092 saa_write8(plines,line_base); /* Line Base */
2093 saa_write8(plines,line_range); /* Line Range */
2094 saa_write8(plines,opcode_base); /* Opcode Base */
2095 /* standard opcode lengths (# of LEB128u operands) */
2096 saa_write8(plines,0); /* Std opcode 1 length */
2097 saa_write8(plines,1); /* Std opcode 2 length */
2098 saa_write8(plines,1); /* Std opcode 3 length */
2099 saa_write8(plines,1); /* Std opcode 4 length */
2100 saa_write8(plines,1); /* Std opcode 5 length */
2101 saa_write8(plines,0); /* Std opcode 6 length */
2102 saa_write8(plines,0); /* Std opcode 7 length */
2103 saa_write8(plines,0); /* Std opcode 8 length */
2104 saa_write8(plines,1); /* Std opcode 9 length */
2105 saa_write8(plines,0); /* Std opcode 10 length */
2106 saa_write8(plines,0); /* Std opcode 11 length */
2107 saa_write8(plines,1); /* Std opcode 12 length */
2108 /* Directory Table */
2109 saa_write8(plines,0); /* End of table */
2110 /* File Name Table */
2111 ftentry = dwarf_flist;
2112 for (indx = 0;indx<dwarf_numfiles;indx++)
2114 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2115 saa_write8(plines,0); /* directory LEB128u */
2116 saa_write8(plines,0); /* time LEB128u */
2117 saa_write8(plines,0); /* size LEB128u */
2118 ftentry = ftentry->next;
2120 saa_write8(plines,0); /* End of table */
2121 linepoff = plines->datalen;
2122 linelen = linepoff + totlen + 10;
2123 linebuf = pbuf = nasm_malloc(linelen);
2124 WRITELONG(pbuf,linelen-4); /* initial length */
2125 WRITESHORT(pbuf,3); /* dwarf version */
2126 WRITELONG(pbuf,linepoff); /* offset to line number program */
2127 /* write line header */
2128 saalen = linepoff;
2129 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2130 pbuf += linepoff;
2131 saa_free(plines);
2132 /* concatonate line program ranges */
2133 linepoff += 13;
2134 plinesrel = saa_init(1L);
2135 psect = dwarf_fsect;
2136 for (indx = 0; indx < dwarf_nsections; indx++)
2138 saa_write64(plinesrel, linepoff);
2139 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2140 saa_write64(plinesrel, (uint64_t) 0);
2141 plinep = psect->psaa;
2142 saalen = plinep->datalen;
2143 saa_rnbytes(plinep, pbuf, saalen);
2144 pbuf += saalen;
2145 linepoff += saalen;
2146 saa_free(plinep);
2147 /* done with this entry */
2148 psect = psect->next;
2152 /* build rela.lines section */
2153 linerellen =saalen = plinesrel->datalen;
2154 linerelbuf = pbuf = nasm_malloc(linerellen);
2155 saa_rnbytes(plinesrel, pbuf, saalen);
2156 saa_free(plinesrel);
2158 /* build frame section */
2159 framelen = 4;
2160 framebuf = pbuf = nasm_malloc(framelen);
2161 WRITELONG(pbuf,framelen-4); /* initial length */
2163 /* build loc section */
2164 loclen = 16;
2165 locbuf = pbuf = nasm_malloc(loclen);
2166 WRITEDLONG(pbuf,0); /* null beginning offset */
2167 WRITEDLONG(pbuf,0); /* null ending offset */
2170 static void dwarf64_cleanup(void)
2172 if (arangesbuf)
2173 nasm_free(arangesbuf);
2174 if (arangesrelbuf)
2175 nasm_free(arangesrelbuf);
2176 if (pubnamesbuf)
2177 nasm_free(pubnamesbuf);
2178 if (infobuf)
2179 nasm_free(infobuf);
2180 if (inforelbuf)
2181 nasm_free(inforelbuf);
2182 if (abbrevbuf)
2183 nasm_free(abbrevbuf);
2184 if (linebuf)
2185 nasm_free(linebuf);
2186 if (linerelbuf)
2187 nasm_free(linerelbuf);
2188 if (framebuf)
2189 nasm_free(framebuf);
2190 if (locbuf)
2191 nasm_free(locbuf);
2193 static void dwarf64_findfile(const char * fname)
2195 int finx;
2196 struct linelist *match;
2198 /* return if fname is current file name */
2199 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2200 /* search for match */
2201 else
2203 match = 0;
2204 if (dwarf_flist)
2206 match = dwarf_flist;
2207 for (finx = 0; finx < dwarf_numfiles; finx++)
2209 if (!(strcmp(fname, match->filename)))
2211 dwarf_clist = match;
2212 return;
2216 /* add file name to end of list */
2217 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2218 dwarf_numfiles++;
2219 dwarf_clist->line = dwarf_numfiles;
2220 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2221 strcpy(dwarf_clist->filename,fname);
2222 dwarf_clist->next = 0;
2223 /* if first entry */
2224 if (!dwarf_flist)
2226 dwarf_flist = dwarf_elist = dwarf_clist;
2227 dwarf_clist->last = 0;
2229 /* chain to previous entry */
2230 else
2232 dwarf_elist->next = dwarf_clist;
2233 dwarf_elist = dwarf_clist;
2237 /* */
2238 static void dwarf64_findsect(const int index)
2240 int sinx;
2241 struct sectlist *match;
2242 struct SAA *plinep;
2243 /* return if index is current section index */
2244 if (dwarf_csect && (dwarf_csect->section == index))
2246 return;
2248 /* search for match */
2249 else
2251 match = 0;
2252 if (dwarf_fsect)
2254 match = dwarf_fsect;
2255 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2257 if ((match->section == index))
2259 dwarf_csect = match;
2260 return;
2262 match = match->next;
2265 /* add entry to end of list */
2266 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2267 dwarf_nsections++;
2268 dwarf_csect->psaa = plinep = saa_init(1L);
2269 dwarf_csect->line = 1;
2270 dwarf_csect->offset = 0;
2271 dwarf_csect->file = 1;
2272 dwarf_csect->section = index;
2273 dwarf_csect->next = 0;
2274 /* set relocatable address at start of line program */
2275 saa_write8(plinep,DW_LNS_extended_op);
2276 saa_write8(plinep,9); /* operand length */
2277 saa_write8(plinep,DW_LNE_set_address);
2278 saa_write64(plinep,0); /* Start Address */
2279 /* if first entry */
2280 if (!dwarf_fsect)
2282 dwarf_fsect = dwarf_esect = dwarf_csect;
2283 dwarf_csect->last = 0;
2285 /* chain to previous entry */
2286 else
2288 dwarf_esect->next = dwarf_csect;
2289 dwarf_esect = dwarf_csect;
2294 #endif /* OF_ELF */