ELF: add header files, begin merging common code, drop .comment
[nasm/nasm.git] / output / outelf64.c
blob4b2325faeea688e4977e6b09631faecc25d6a425
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 "elf64.h"
27 #include "dwarf.h"
28 #include "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 uint32_t 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;
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 void debug64_typevalue(int32_t);
192 void debug64_init(struct ofmt *, void *, FILE *, efunc);
193 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
194 void debug64_directive(const char *, const char *);
196 /* stabs debugging routines */
197 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
198 void stabs64_output(int, void *);
199 void stabs64_generate(void);
200 void stabs64_cleanup(void);
202 /* dwarf debugging routines */
203 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
204 void dwarf64_output(int, void *);
205 void dwarf64_generate(void);
206 void dwarf64_cleanup(void);
207 void dwarf64_findfile(const char *);
208 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 = 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 =
330 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) error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
404 " declaration of section `%s'", q, name);
407 if (!strcmp(name, ".shstrtab") ||
408 !strcmp(name, ".symtab") ||
409 !strcmp(name, ".strtab")) {
410 error(ERR_NONFATAL, "attempt to redefine reserved section"
411 "name `%s'", name);
412 return NO_SEG;
415 for (i = 0; i < nsects; i++)
416 if (!strcmp(name, sects[i]->name))
417 break;
418 if (i == nsects) {
419 const struct elf_known_section *ks = elf_known_sections;
421 while (ks->name) {
422 if (!strcmp(name, ks->name))
423 break;
424 ks++;
427 type = type ? type : ks->type;
428 align = align ? align : ks->align;
429 flags = (ks->flags & ~flags_and) | flags_or;
431 i = elf_make_section(name, type, flags, align);
432 } else if (pass == 1) {
433 if ((type && sects[i]->type != type)
434 || (align && sects[i]->align != align)
435 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
436 error(ERR_WARNING, "incompatible section attributes ignored on"
437 " redeclaration of section `%s'", name);
440 return sects[i]->index;
443 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
444 int is_global, char *special)
446 int pos = strslen;
447 struct Symbol *sym;
448 bool special_used = false;
450 #if defined(DEBUG) && DEBUG>2
451 fprintf(stderr,
452 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
453 name, segment, offset, is_global, special);
454 #endif
455 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
457 * This is a NASM special symbol. We never allow it into
458 * the ELF symbol table, even if it's a valid one. If it
459 * _isn't_ a valid one, we should barf immediately.
461 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
462 strcmp(name, "..got") && strcmp(name, "..plt") &&
463 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
464 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
465 return;
468 if (is_global == 3) {
469 struct Symbol **s;
471 * Fix up a forward-reference symbol size from the first
472 * pass.
474 for (s = &fwds; *s; s = &(*s)->nextfwd)
475 if (!strcmp((*s)->name, name)) {
476 struct tokenval tokval;
477 expr *e;
478 char *p = special;
480 while (*p && !nasm_isspace(*p))
481 p++;
482 while (*p && nasm_isspace(*p))
483 p++;
484 stdscan_reset();
485 stdscan_bufptr = p;
486 tokval.t_type = TOKEN_INVALID;
487 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
488 if (e) {
489 if (!is_simple(e))
490 error(ERR_NONFATAL, "cannot use relocatable"
491 " expression as symbol size");
492 else
493 (*s)->size = reloc_value(e);
497 * Remove it from the list of unresolved sizes.
499 nasm_free((*s)->name);
500 *s = (*s)->nextfwd;
501 return;
503 return; /* it wasn't an important one */
506 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
507 strslen += 1 + strlen(name);
509 lastsym = sym = saa_wstruct(syms);
511 memset(&sym->symv, 0, sizeof(struct rbtree));
513 sym->strpos = pos;
514 sym->type = is_global ? SYM_GLOBAL : 0;
515 sym->other = STV_DEFAULT;
516 sym->size = 0;
517 if (segment == NO_SEG)
518 sym->section = SHN_ABS;
519 else {
520 int i;
521 sym->section = SHN_UNDEF;
522 if (nsects == 0 && segment == def_seg) {
523 int tempint;
524 if (segment != elf_section_names(".text", 2, &tempint))
525 error(ERR_PANIC,
526 "strange segment conditions in ELF driver");
527 sym->section = nsects;
528 } else {
529 for (i = 0; i < nsects; i++)
530 if (segment == sects[i]->index) {
531 sym->section = i + 1;
532 break;
537 if (is_global == 2) {
538 sym->size = offset;
539 sym->symv.key = 0;
540 sym->section = SHN_COMMON;
542 * We have a common variable. Check the special text to see
543 * if it's a valid number and power of two; if so, store it
544 * as the alignment for the common variable.
546 if (special) {
547 bool err;
548 sym->symv.key = readnum(special, &err);
549 if (err)
550 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
551 " valid number", special);
552 else if ((sym->symv.key | (sym->symv.key - 1))
553 != 2 * sym->symv.key - 1)
554 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
555 " power of two", special);
557 special_used = true;
558 } else
559 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
561 if (sym->type == SYM_GLOBAL) {
563 * If sym->section == SHN_ABS, then the first line of the
564 * else section would cause a core dump, because its a reference
565 * beyond the end of the section array.
566 * This behaviour is exhibited by this code:
567 * GLOBAL crash_nasm
568 * crash_nasm equ 0
569 * To avoid such a crash, such requests are silently discarded.
570 * This may not be the best solution.
572 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
573 bsym = raa_write(bsym, segment, nglobs);
574 } else if (sym->section != SHN_ABS) {
576 * This is a global symbol; so we must add it to the rbtree
577 * of global symbols in its section.
579 * In addition, we check the special text for symbol
580 * type and size information.
582 sects[sym->section-1]->gsyms =
583 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
585 if (special) {
586 int n = strcspn(special, " \t");
588 if (!nasm_strnicmp(special, "function", n))
589 sym->type |= STT_FUNC;
590 else if (!nasm_strnicmp(special, "data", n) ||
591 !nasm_strnicmp(special, "object", n))
592 sym->type |= STT_OBJECT;
593 else if (!nasm_strnicmp(special, "notype", n))
594 sym->type |= STT_NOTYPE;
595 else
596 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
597 n, special);
598 special += n;
600 while (nasm_isspace(*special))
601 ++special;
602 if (*special) {
603 n = strcspn(special, " \t");
604 if (!nasm_strnicmp(special, "default", n))
605 sym->other = STV_DEFAULT;
606 else if (!nasm_strnicmp(special, "internal", n))
607 sym->other = STV_INTERNAL;
608 else if (!nasm_strnicmp(special, "hidden", n))
609 sym->other = STV_HIDDEN;
610 else if (!nasm_strnicmp(special, "protected", n))
611 sym->other = STV_PROTECTED;
612 else
613 n = 0;
614 special += n;
617 if (*special) {
618 struct tokenval tokval;
619 expr *e;
620 int fwd = 0;
621 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
623 while (special[n] && nasm_isspace(special[n]))
624 n++;
626 * We have a size expression; attempt to
627 * evaluate it.
629 stdscan_reset();
630 stdscan_bufptr = special + n;
631 tokval.t_type = TOKEN_INVALID;
632 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
633 NULL);
634 if (fwd) {
635 sym->nextfwd = fwds;
636 fwds = sym;
637 sym->name = nasm_strdup(name);
638 } else if (e) {
639 if (!is_simple(e))
640 error(ERR_NONFATAL, "cannot use relocatable"
641 " expression as symbol size");
642 else
643 sym->size = reloc_value(e);
645 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
647 special_used = true;
650 * If TLS segment, mark symbol accordingly.
652 if (sects[sym->section - 1]->flags & SHF_TLS) {
653 sym->type &= 0xf0;
654 sym->type |= STT_TLS;
657 sym->globnum = nglobs;
658 nglobs++;
659 } else
660 nlocals++;
662 if (special && !special_used)
663 error(ERR_NONFATAL, "no special symbol features supported here");
666 static void elf_add_reloc(struct Section *sect, int32_t segment,
667 int64_t offset, int type)
669 struct Reloc *r;
670 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
671 sect->tail = &r->next;
672 r->next = NULL;
674 r->address = sect->len;
675 r->offset = offset;
676 if (segment == NO_SEG)
677 r->symbol = 0;
678 else {
679 int i;
680 r->symbol = 0;
681 for (i = 0; i < nsects; i++)
682 if (segment == sects[i]->index)
683 r->symbol = i + 2;
684 if (!r->symbol)
685 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
687 r->type = type;
689 sect->nrelocs++;
693 * This routine deals with ..got and ..sym relocations: the more
694 * complicated kinds. In shared-library writing, some relocations
695 * with respect to global symbols must refer to the precise symbol
696 * rather than referring to an offset from the base of the section
697 * _containing_ the symbol. Such relocations call to this routine,
698 * which searches the symbol list for the symbol in question.
700 * R_386_GOT32 references require the _exact_ symbol address to be
701 * used; R_386_32 references can be at an offset from the symbol.
702 * The boolean argument `exact' tells us this.
704 * Return value is the adjusted value of `addr', having become an
705 * offset from the symbol rather than the section. Should always be
706 * zero when returning from an exact call.
708 * Limitation: if you define two symbols at the same place,
709 * confusion will occur.
711 * Inefficiency: we search, currently, using a linked list which
712 * isn't even necessarily sorted.
714 static void elf_add_gsym_reloc(struct Section *sect,
715 int32_t segment, uint64_t offset, int64_t pcrel,
716 int type, bool exact)
718 struct Reloc *r;
719 struct Section *s;
720 struct Symbol *sym;
721 struct rbtree *srb;
722 int i;
725 * First look up the segment/offset pair and find a global
726 * symbol corresponding to it. If it's not one of our segments,
727 * then it must be an external symbol, in which case we're fine
728 * doing a normal elf_add_reloc after first sanity-checking
729 * that the offset from the symbol is zero.
731 s = NULL;
732 for (i = 0; i < nsects; i++)
733 if (segment == sects[i]->index) {
734 s = sects[i];
735 break;
738 if (!s) {
739 if (exact && offset)
740 error(ERR_NONFATAL, "invalid access to an external symbol");
741 else
742 elf_add_reloc(sect, segment, offset - pcrel, type);
743 return;
746 srb = rb_search(s->gsyms, offset);
747 if (!srb || (exact && srb->key != offset)) {
748 error(ERR_NONFATAL, "unable to find a suitable global symbol"
749 " for this reference");
750 return;
752 sym = container_of(srb, struct Symbol, symv);
754 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
755 sect->tail = &r->next;
756 r->next = NULL;
758 r->address = sect->len;
759 r->offset = offset - pcrel - sym->symv.key;
760 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
761 r->type = type;
763 sect->nrelocs++;
766 static void elf_out(int32_t segto, const void *data,
767 enum out_type type, uint64_t size,
768 int32_t segment, int32_t wrt)
770 struct Section *s;
771 int64_t addr, zero;
772 int i;
773 static struct symlininfo sinfo;
775 zero = 0;
777 #if defined(DEBUG) && DEBUG>2
778 if (data) fprintf(stderr,
779 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
780 currentline, type, segment, segto, size, *(int64_t *)data);
781 else fprintf(stderr,
782 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
783 currentline, type, segment, segto, size);
784 #endif
787 * handle absolute-assembly (structure definitions)
789 if (segto == NO_SEG) {
790 if (type != OUT_RESERVE)
791 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
792 " space");
793 return;
796 s = NULL;
797 for (i = 0; i < nsects; i++)
798 if (segto == sects[i]->index) {
799 s = sects[i];
800 break;
802 if (!s) {
803 int tempint; /* ignored */
804 if (segto != elf_section_names(".text", 2, &tempint))
805 error(ERR_PANIC, "strange segment conditions in ELF driver");
806 else {
807 s = sects[nsects - 1];
808 i = nsects - 1;
811 /* invoke current debug_output routine */
812 if (of_elf64.current_dfmt) {
813 sinfo.offset = s->len;
814 sinfo.section = i;
815 sinfo.segto = segto;
816 sinfo.name = s->name;
817 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
819 /* end of debugging stuff */
821 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
822 error(ERR_WARNING, "attempt to initialize memory in"
823 " BSS section `%s': ignored", s->name);
824 s->len += realsize(type, size);
825 return;
828 if (type == OUT_RESERVE) {
829 if (s->type == SHT_PROGBITS) {
830 error(ERR_WARNING, "uninitialized space declared in"
831 " non-BSS section `%s': zeroing", s->name);
832 elf_sect_write(s, NULL, size);
833 } else
834 s->len += size;
835 } else if (type == OUT_RAWDATA) {
836 if (segment != NO_SEG)
837 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
838 elf_sect_write(s, data, size);
839 } else if (type == OUT_ADDRESS) {
840 addr = *(int64_t *)data;
841 if (segment == NO_SEG) {
842 /* Do nothing */
843 } else if (segment % 2) {
844 error(ERR_NONFATAL, "ELF format does not support"
845 " segment base references");
846 } else {
847 if (wrt == NO_SEG) {
848 switch ((int)size) {
849 case 1:
850 elf_add_reloc(s, segment, addr, R_X86_64_8);
851 break;
852 case 2:
853 elf_add_reloc(s, segment, addr, R_X86_64_16);
854 break;
855 case 4:
856 elf_add_reloc(s, segment, addr, R_X86_64_32);
857 break;
858 case 8:
859 elf_add_reloc(s, segment, addr, R_X86_64_64);
860 break;
861 default:
862 error(ERR_PANIC, "internal error elf64-hpa-871");
863 break;
865 addr = 0;
866 } else if (wrt == elf_gotpc_sect + 1) {
868 * The user will supply GOT relative to $$. ELF
869 * will let us have GOT relative to $. So we
870 * need to fix up the data item by $-$$.
872 addr += s->len;
873 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
874 addr = 0;
875 } else if (wrt == elf_gotoff_sect + 1) {
876 if (size != 8) {
877 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
878 "references to be qword");
879 } else {
880 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
881 addr = 0;
883 } else if (wrt == elf_got_sect + 1) {
884 switch ((int)size) {
885 case 4:
886 elf_add_gsym_reloc(s, segment, addr, 0,
887 R_X86_64_GOT32, true);
888 addr = 0;
889 break;
890 case 8:
891 elf_add_gsym_reloc(s, segment, addr, 0,
892 R_X86_64_GOT64, true);
893 addr = 0;
894 break;
895 default:
896 error(ERR_NONFATAL, "invalid ..got reference");
897 break;
899 } else if (wrt == elf_sym_sect + 1) {
900 switch ((int)size) {
901 case 1:
902 elf_add_gsym_reloc(s, segment, addr, 0,
903 R_X86_64_8, false);
904 addr = 0;
905 break;
906 case 2:
907 elf_add_gsym_reloc(s, segment, addr, 0,
908 R_X86_64_16, false);
909 addr = 0;
910 break;
911 case 4:
912 elf_add_gsym_reloc(s, segment, addr, 0,
913 R_X86_64_32, false);
914 addr = 0;
915 break;
916 case 8:
917 elf_add_gsym_reloc(s, segment, addr, 0,
918 R_X86_64_64, false);
919 addr = 0;
920 break;
921 default:
922 error(ERR_PANIC, "internal error elf64-hpa-903");
923 break;
925 } else if (wrt == elf_plt_sect + 1) {
926 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
927 "relative PLT references");
928 } else {
929 error(ERR_NONFATAL, "ELF format does not support this"
930 " use of WRT");
933 elf_sect_writeaddr(s, addr, size);
934 } else if (type == OUT_REL2ADR) {
935 addr = *(int64_t *)data - size;
936 if (segment == segto)
937 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
938 if (segment == NO_SEG) {
939 /* Do nothing */
940 } else if (segment % 2) {
941 error(ERR_NONFATAL, "ELF format does not support"
942 " segment base references");
943 } else {
944 if (wrt == NO_SEG) {
945 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
946 addr = 0;
947 } else {
948 error(ERR_NONFATAL,
949 "Unsupported non-32-bit ELF relocation [2]");
952 elf_sect_writeaddr(s, addr, 2);
953 } else if (type == OUT_REL4ADR) {
954 addr = *(int64_t *)data - size;
955 if (segment == segto)
956 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
957 if (segment == NO_SEG) {
958 /* Do nothing */
959 } else if (segment % 2) {
960 error(ERR_NONFATAL, "ELF64 format does not support"
961 " segment base references");
962 } else {
963 if (wrt == NO_SEG) {
964 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
965 addr = 0;
966 } else if (wrt == elf_plt_sect + 1) {
967 elf_add_gsym_reloc(s, segment, addr+size, size,
968 R_X86_64_PLT32, true);
969 addr = 0;
970 } else if (wrt == elf_gotpc_sect + 1 ||
971 wrt == elf_got_sect + 1) {
972 elf_add_gsym_reloc(s, segment, addr+size, size,
973 R_X86_64_GOTPCREL, true);
974 addr = 0;
975 } else if (wrt == elf_gotoff_sect + 1 ||
976 wrt == elf_got_sect + 1) {
977 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
978 "qword absolute");
979 } else if (wrt == elf_gottpoff_sect + 1) {
980 elf_add_gsym_reloc(s, segment, addr+size, size,
981 R_X86_64_GOTTPOFF, true);
982 addr = 0;
983 } else {
984 error(ERR_NONFATAL, "ELF64 format does not support this"
985 " use of WRT");
988 elf_sect_writeaddr(s, addr, 4);
989 } else if (type == OUT_REL8ADR) {
990 addr = *(int64_t *)data - size;
991 if (segment == segto)
992 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
993 if (segment == NO_SEG) {
994 /* Do nothing */
995 } else if (segment % 2) {
996 error(ERR_NONFATAL, "ELF64 format does not support"
997 " segment base references");
998 } else {
999 if (wrt == NO_SEG) {
1000 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1001 addr = 0;
1002 } else if (wrt == elf_gotpc_sect + 1 ||
1003 wrt == elf_got_sect + 1) {
1004 elf_add_gsym_reloc(s, segment, addr+size, size,
1005 R_X86_64_GOTPCREL64, true);
1006 addr = 0;
1007 } else if (wrt == elf_gotoff_sect + 1 ||
1008 wrt == elf_got_sect + 1) {
1009 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1010 "absolute");
1011 } else if (wrt == elf_gottpoff_sect + 1) {
1012 error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1013 "dword");
1014 } else {
1015 error(ERR_NONFATAL, "ELF64 format does not support this"
1016 " use of WRT");
1019 elf_sect_writeaddr(s, addr, 8);
1023 static void elf_write(void)
1025 int align;
1026 int scount;
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 = 4;
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(nsects + 2, 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);
1124 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1125 scount = 1; /* needed for the stabs debugging to track the symtable section */
1126 p = shstrtab + 1;
1127 for (i = 0; i < nsects; i++) {
1128 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1129 (sects[i]->type == SHT_PROGBITS ?
1130 sects[i]->data : NULL), true,
1131 sects[i]->len, 0, 0, sects[i]->align, 0);
1132 p += strlen(p) + 1;
1133 scount++; /* ditto */
1135 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1136 scount++; /* ditto */
1137 p += strlen(p) + 1;
1138 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1139 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1140 p += strlen(p) + 1;
1141 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1142 for (i = 0; i < nsects; i++)
1143 if (sects[i]->head) {
1144 p += strlen(p) + 1;
1145 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1146 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1148 if (of_elf64.current_dfmt == &df_stabs) {
1149 /* for debugging information, create the last three sections
1150 which are the .stab , .stabstr and .rel.stab sections respectively */
1152 /* this function call creates the stab sections in memory */
1153 stabs64_generate();
1155 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1156 p += strlen(p) + 1;
1157 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1158 nsections - 2, 0, 4, 12);
1160 p += strlen(p) + 1;
1161 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1162 stabstrlen, 0, 0, 4, 0);
1164 p += strlen(p) + 1;
1165 /* link -> symtable info -> section to refer to */
1166 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1167 stabrellen, symtabsection, nsections - 3, 4,
1168 16);
1171 else if (of_elf64.current_dfmt == &df_dwarf) {
1172 /* for dwarf debugging information, create the ten dwarf sections */
1174 /* this function call creates the dwarf sections in memory */
1175 if (dwarf_fsect) dwarf64_generate();
1177 p += strlen(p) + 1;
1178 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1179 arangeslen, 0, 0, 1, 0);
1180 p += strlen(p) + 1;
1181 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1182 arangesrellen, symtabsection, debug_aranges, 1, 24);
1183 p += strlen(p) + 1;
1184 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1185 pubnameslen, 0, 0, 1, 0);
1186 p += strlen(p) + 1;
1187 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1188 infolen, 0, 0, 1, 0);
1189 p += strlen(p) + 1;
1190 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1191 inforellen, symtabsection, debug_info, 1, 24);
1192 p += strlen(p) + 1;
1193 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1194 abbrevlen, 0, 0, 1, 0);
1195 p += strlen(p) + 1;
1196 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1197 linelen, 0, 0, 1, 0);
1198 p += strlen(p) + 1;
1199 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1200 linerellen, symtabsection, debug_line, 1, 24);
1201 p += strlen(p) + 1;
1202 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1203 framelen, 0, 0, 8, 0);
1204 p += strlen(p) + 1;
1205 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1206 loclen, 0, 0, 1, 0);
1209 fwritezero(align, elffp);
1212 * Now output the sections.
1214 elf_write_sections();
1216 nasm_free(elf_sects);
1217 saa_free(symtab);
1220 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1222 struct SAA *s = saa_init(1L);
1223 struct Symbol *sym;
1224 uint8_t entry[24], *p;
1225 int i;
1227 *len = *local = 0;
1230 * First, an all-zeros entry, required by the ELF spec.
1232 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1233 *len += 24;
1234 (*local)++;
1237 * Next, an entry for the file name.
1239 p = entry;
1240 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1241 WRITESHORT(p, STT_FILE); /* type FILE */
1242 WRITESHORT(p, SHN_ABS);
1243 WRITEDLONG(p, (uint64_t) 0); /* no value */
1244 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1245 saa_wbytes(s, entry, 24L);
1246 *len += 24;
1247 (*local)++;
1250 * Now some standard symbols defining the segments, for relocation
1251 * purposes.
1253 for (i = 1; i <= nsects; i++) {
1254 p = entry;
1255 WRITELONG(p, 0); /* no symbol name */
1256 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1257 WRITESHORT(p, i); /* section id */
1258 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1259 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1260 saa_wbytes(s, entry, 24L);
1261 *len += 24;
1262 (*local)++;
1267 * Now the other local symbols.
1269 saa_rewind(syms);
1270 while ((sym = saa_rstruct(syms))) {
1271 if (sym->type & SYM_GLOBAL)
1272 continue;
1273 p = entry;
1274 WRITELONG(p, sym->strpos); /* index into symbol string table */
1275 WRITECHAR(p, sym->type); /* type and binding */
1276 WRITECHAR(p, sym->other); /* visibility */
1277 WRITESHORT(p, sym->section); /* index into section header table */
1278 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1279 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1280 saa_wbytes(s, entry, 24L);
1281 *len += 24;
1282 (*local)++;
1285 * dwarf needs symbols for debug sections
1286 * which are relocation targets.
1288 if (of_elf64.current_dfmt == &df_dwarf) {
1289 dwarf_infosym = *local;
1290 p = entry;
1291 WRITELONG(p, 0); /* no symbol name */
1292 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1293 WRITESHORT(p, debug_info); /* section id */
1294 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1295 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1296 saa_wbytes(s, entry, 24L);
1297 *len += 24;
1298 (*local)++;
1299 dwarf_abbrevsym = *local;
1300 p = entry;
1301 WRITELONG(p, 0); /* no symbol name */
1302 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1303 WRITESHORT(p, debug_abbrev); /* section id */
1304 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1305 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1306 saa_wbytes(s, entry, 24L);
1307 *len += 24;
1308 (*local)++;
1309 dwarf_linesym = *local;
1310 p = entry;
1311 WRITELONG(p, 0); /* no symbol name */
1312 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1313 WRITESHORT(p, debug_line); /* section id */
1314 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1315 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1316 saa_wbytes(s, entry, 24L);
1317 *len += 24;
1318 (*local)++;
1322 * Now the global symbols.
1324 saa_rewind(syms);
1325 while ((sym = saa_rstruct(syms))) {
1326 if (!(sym->type & SYM_GLOBAL))
1327 continue;
1328 p = entry;
1329 WRITELONG(p, sym->strpos);
1330 WRITECHAR(p, sym->type); /* type and binding */
1331 WRITECHAR(p, sym->other); /* visibility */
1332 WRITESHORT(p, sym->section);
1333 WRITEDLONG(p, (int64_t)sym->symv.key);
1334 WRITEDLONG(p, (int64_t)sym->size);
1335 saa_wbytes(s, entry, 24L);
1336 *len += 24;
1339 return s;
1342 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1344 struct SAA *s;
1345 uint8_t *p, entry[24];
1347 if (!r)
1348 return NULL;
1350 s = saa_init(1L);
1351 *len = 0;
1353 while (r) {
1354 int64_t sym = r->symbol;
1356 if (sym >= GLOBAL_TEMP_BASE)
1358 if (of_elf64.current_dfmt == &df_dwarf)
1359 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1360 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1362 p = entry;
1363 WRITEDLONG(p, r->address);
1364 WRITEDLONG(p, (sym << 32) + r->type);
1365 WRITEDLONG(p, r->offset);
1366 saa_wbytes(s, entry, 24L);
1367 *len += 24;
1369 r = r->next;
1372 return s;
1375 static void elf_section_header(int name, int type, uint64_t flags,
1376 void *data, bool is_saa, uint64_t datalen,
1377 int link, int info, int align, int eltsize)
1379 elf_sects[elf_nsect].data = data;
1380 elf_sects[elf_nsect].len = datalen;
1381 elf_sects[elf_nsect].is_saa = is_saa;
1382 elf_nsect++;
1384 fwriteint32_t((int32_t)name, elffp);
1385 fwriteint32_t((int32_t)type, elffp);
1386 fwriteint64_t((int64_t)flags, elffp);
1387 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1388 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1389 fwriteint64_t(datalen, elffp);
1390 if (data)
1391 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1392 fwriteint32_t((int32_t)link, elffp);
1393 fwriteint32_t((int32_t)info, elffp);
1394 fwriteint64_t((int64_t)align, elffp);
1395 fwriteint64_t((int64_t)eltsize, elffp);
1398 static void elf_write_sections(void)
1400 int i;
1401 for (i = 0; i < elf_nsect; i++)
1402 if (elf_sects[i].data) {
1403 int32_t len = elf_sects[i].len;
1404 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1405 int32_t align = reallen - len;
1406 if (elf_sects[i].is_saa)
1407 saa_fpwrite(elf_sects[i].data, elffp);
1408 else
1409 fwrite(elf_sects[i].data, len, 1, elffp);
1410 fwritezero(align, elffp);
1414 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1416 saa_wbytes(sect->data, data, len);
1417 sect->len += len;
1419 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1421 saa_writeaddr(sect->data, data, len);
1422 sect->len += len;
1425 static int32_t elf_segbase(int32_t segment)
1427 return segment;
1430 static int elf_directive(char *directive, char *value, int pass)
1432 bool err;
1433 int64_t n;
1434 char *p;
1436 if (!strcmp(directive, "osabi")) {
1437 if (pass == 2)
1438 return 1; /* ignore in pass 2 */
1440 n = readnum(value, &err);
1441 if (err) {
1442 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1443 return 1;
1445 if (n < 0 || n > 255) {
1446 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1447 return 1;
1449 elf_osabi = n;
1450 elf_abiver = 0;
1452 if ((p = strchr(value,',')) == NULL)
1453 return 1;
1455 n = readnum(p+1, &err);
1456 if (err || n < 0 || n > 255) {
1457 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1458 return 1;
1461 elf_abiver = n;
1462 return 1;
1465 return 0;
1468 static void elf_filename(char *inname, char *outname, efunc error)
1470 strcpy(elf_module, inname);
1471 standard_extension(inname, outname, ".o", error);
1474 extern macros_t elf_stdmac[];
1476 static int elf_set_info(enum geninfo type, char **val)
1478 (void)type;
1479 (void)val;
1480 return 0;
1482 static struct dfmt df_dwarf = {
1483 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1484 "dwarf",
1485 debug64_init,
1486 dwarf64_linenum,
1487 debug64_deflabel,
1488 debug64_directive,
1489 debug64_typevalue,
1490 dwarf64_output,
1491 dwarf64_cleanup
1493 static struct dfmt df_stabs = {
1494 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1495 "stabs",
1496 debug64_init,
1497 stabs64_linenum,
1498 debug64_deflabel,
1499 debug64_directive,
1500 debug64_typevalue,
1501 stabs64_output,
1502 stabs64_cleanup
1505 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1507 struct ofmt of_elf64 = {
1508 "ELF64 (x86_64) object files (e.g. Linux)",
1509 "elf64",
1510 NULL,
1511 elf64_debugs_arr,
1512 &df_stabs,
1513 elf_stdmac,
1514 elf_init,
1515 elf_set_info,
1516 elf_out,
1517 elf_deflabel,
1518 elf_section_names,
1519 elf_segbase,
1520 elf_directive,
1521 elf_filename,
1522 elf_cleanup
1525 /* common debugging routines */
1526 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1528 (void)of;
1529 (void)id;
1530 (void)fp;
1531 (void)error;
1533 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1534 char *special)
1536 (void)name;
1537 (void)segment;
1538 (void)offset;
1539 (void)is_global;
1540 (void)special;
1543 void debug64_directive(const char *directive, const char *params)
1545 (void)directive;
1546 (void)params;
1549 void debug64_typevalue(int32_t type)
1551 int32_t stype, ssize;
1552 switch (TYM_TYPE(type)) {
1553 case TY_LABEL:
1554 ssize = 0;
1555 stype = STT_NOTYPE;
1556 break;
1557 case TY_BYTE:
1558 ssize = 1;
1559 stype = STT_OBJECT;
1560 break;
1561 case TY_WORD:
1562 ssize = 2;
1563 stype = STT_OBJECT;
1564 break;
1565 case TY_DWORD:
1566 ssize = 4;
1567 stype = STT_OBJECT;
1568 break;
1569 case TY_FLOAT:
1570 ssize = 4;
1571 stype = STT_OBJECT;
1572 break;
1573 case TY_QWORD:
1574 ssize = 8;
1575 stype = STT_OBJECT;
1576 break;
1577 case TY_TBYTE:
1578 ssize = 10;
1579 stype = STT_OBJECT;
1580 break;
1581 case TY_OWORD:
1582 ssize = 16;
1583 stype = STT_OBJECT;
1584 break;
1585 case TY_COMMON:
1586 ssize = 0;
1587 stype = STT_COMMON;
1588 break;
1589 case TY_SEG:
1590 ssize = 0;
1591 stype = STT_SECTION;
1592 break;
1593 case TY_EXTERN:
1594 ssize = 0;
1595 stype = STT_NOTYPE;
1596 break;
1597 case TY_EQU:
1598 ssize = 0;
1599 stype = STT_NOTYPE;
1600 break;
1601 default:
1602 ssize = 0;
1603 stype = STT_NOTYPE;
1604 break;
1606 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1607 lastsym->size = ssize;
1608 lastsym->type = stype;
1612 /* stabs debugging routines */
1615 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1617 (void)segto;
1618 if (!stabs_filename) {
1619 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1620 strcpy(stabs_filename, filename);
1621 } else {
1622 if (strcmp(stabs_filename, filename)) {
1623 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1624 in fact, this leak comes in quite handy to maintain a list of files
1625 encountered so far in the symbol lines... */
1627 /* why not nasm_free(stabs_filename); we're done with the old one */
1629 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1630 strcpy(stabs_filename, filename);
1633 debug_immcall = 1;
1634 currentline = linenumber;
1638 void stabs64_output(int type, void *param)
1640 struct symlininfo *s;
1641 struct linelist *el;
1642 if (type == TY_DEBUGSYMLIN) {
1643 if (debug_immcall) {
1644 s = (struct symlininfo *)param;
1645 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1646 return; /* line info is only collected for executable sections */
1647 numlinestabs++;
1648 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1649 el->info.offset = s->offset;
1650 el->info.section = s->section;
1651 el->info.name = s->name;
1652 el->line = currentline;
1653 el->filename = stabs_filename;
1654 el->next = 0;
1655 if (stabslines) {
1656 stabslines->last->next = el;
1657 stabslines->last = el;
1658 } else {
1659 stabslines = el;
1660 stabslines->last = el;
1664 debug_immcall = 0;
1667 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1668 do {\
1669 WRITELONG(p,n_strx); \
1670 WRITECHAR(p,n_type); \
1671 WRITECHAR(p,n_other); \
1672 WRITESHORT(p,n_desc); \
1673 WRITELONG(p,n_value); \
1674 } while (0)
1676 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1678 void stabs64_generate(void)
1680 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1681 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1682 char **allfiles;
1683 int *fileidx;
1685 struct linelist *ptr;
1687 ptr = stabslines;
1689 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1690 for (i = 0; i < numlinestabs; i++)
1691 allfiles[i] = 0;
1692 numfiles = 0;
1693 while (ptr) {
1694 if (numfiles == 0) {
1695 allfiles[0] = ptr->filename;
1696 numfiles++;
1697 } else {
1698 for (i = 0; i < numfiles; i++) {
1699 if (!strcmp(allfiles[i], ptr->filename))
1700 break;
1702 if (i >= numfiles) {
1703 allfiles[i] = ptr->filename;
1704 numfiles++;
1707 ptr = ptr->next;
1709 strsize = 1;
1710 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1711 for (i = 0; i < numfiles; i++) {
1712 fileidx[i] = strsize;
1713 strsize += strlen(allfiles[i]) + 1;
1715 mainfileindex = 0;
1716 for (i = 0; i < numfiles; i++) {
1717 if (!strcmp(allfiles[i], elf_module)) {
1718 mainfileindex = i;
1719 break;
1723 /* worst case size of the stab buffer would be:
1724 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1726 sbuf =
1727 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1728 sizeof(struct stabentry));
1730 ssbuf = (uint8_t *)nasm_malloc(strsize);
1732 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1733 rptr = rbuf;
1735 for (i = 0; i < numfiles; i++) {
1736 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1738 ssbuf[0] = 0;
1740 stabstrlen = strsize; /* set global variable for length of stab strings */
1742 sptr = sbuf;
1743 ptr = stabslines;
1744 numstabs = 0;
1746 if (ptr) {
1747 /* this is the first stab, its strx points to the filename of the
1748 the source-file, the n_desc field should be set to the number
1749 of remaining stabs
1751 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1753 /* this is the stab for the main source file */
1754 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1756 /* relocation table entry */
1758 /* Since the symbol table has two entries before */
1759 /* the section symbols, the index in the info.section */
1760 /* member must be adjusted by adding 2 */
1762 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1763 WRITELONG(rptr, R_X86_64_32);
1764 WRITELONG(rptr, ptr->info.section + 2);
1766 numstabs++;
1767 currfile = mainfileindex;
1770 while (ptr) {
1771 if (strcmp(allfiles[currfile], ptr->filename)) {
1772 /* oops file has changed... */
1773 for (i = 0; i < numfiles; i++)
1774 if (!strcmp(allfiles[i], ptr->filename))
1775 break;
1776 currfile = i;
1777 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1778 ptr->info.offset);
1779 numstabs++;
1781 /* relocation table entry */
1783 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1784 WRITELONG(rptr, R_X86_64_32);
1785 WRITELONG(rptr, ptr->info.section + 2);
1788 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1789 numstabs++;
1791 /* relocation table entry */
1793 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1794 WRITELONG(rptr, R_X86_64_32);
1795 WRITELONG(rptr, ptr->info.section + 2);
1797 ptr = ptr->next;
1801 ((struct stabentry *)sbuf)->n_desc = numstabs;
1803 nasm_free(allfiles);
1804 nasm_free(fileidx);
1806 stablen = (sptr - sbuf);
1807 stabrellen = (rptr - rbuf);
1808 stabrelbuf = rbuf;
1809 stabbuf = sbuf;
1810 stabstrbuf = ssbuf;
1813 void stabs64_cleanup(void)
1815 struct linelist *ptr, *del;
1816 if (!stabslines)
1817 return;
1818 ptr = stabslines;
1819 while (ptr) {
1820 del = ptr;
1821 ptr = ptr->next;
1822 nasm_free(del);
1824 if (stabbuf)
1825 nasm_free(stabbuf);
1826 if (stabrelbuf)
1827 nasm_free(stabrelbuf);
1828 if (stabstrbuf)
1829 nasm_free(stabstrbuf);
1831 /* dwarf routines */
1834 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1836 (void)segto;
1837 dwarf64_findfile(filename);
1838 debug_immcall = 1;
1839 currentline = linenumber;
1842 /* called from elf_out with type == TY_DEBUGSYMLIN */
1843 void dwarf64_output(int type, void *param)
1845 int ln, aa, inx, maxln, soc;
1846 struct symlininfo *s;
1847 struct SAA *plinep;
1849 (void)type;
1851 s = (struct symlininfo *)param;
1852 /* line number info is only gathered for executable sections */
1853 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1854 return;
1855 /* Check if section index has changed */
1856 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1858 dwarf64_findsect(s->section);
1860 /* do nothing unless line or file has changed */
1861 if (debug_immcall)
1863 ln = currentline - dwarf_csect->line;
1864 aa = s->offset - dwarf_csect->offset;
1865 inx = dwarf_clist->line;
1866 plinep = dwarf_csect->psaa;
1867 /* check for file change */
1868 if (!(inx == dwarf_csect->file))
1870 saa_write8(plinep,DW_LNS_set_file);
1871 saa_write8(plinep,inx);
1872 dwarf_csect->file = inx;
1874 /* check for line change */
1875 if (ln)
1877 /* test if in range of special op code */
1878 maxln = line_base + line_range;
1879 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1880 if (ln >= line_base && ln < maxln && soc < 256)
1882 saa_write8(plinep,soc);
1884 else
1886 if (ln)
1888 saa_write8(plinep,DW_LNS_advance_line);
1889 saa_wleb128s(plinep,ln);
1891 if (aa)
1893 saa_write8(plinep,DW_LNS_advance_pc);
1894 saa_wleb128u(plinep,aa);
1897 dwarf_csect->line = currentline;
1898 dwarf_csect->offset = s->offset;
1900 /* show change handled */
1901 debug_immcall = 0;
1906 void dwarf64_generate(void)
1908 uint8_t *pbuf;
1909 int indx;
1910 struct linelist *ftentry;
1911 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1912 struct SAA *parangesrel, *plinesrel, *pinforel;
1913 struct sectlist *psect;
1914 size_t saalen, linepoff, totlen, highaddr;
1916 /* write epilogues for each line program range */
1917 /* and build aranges section */
1918 paranges = saa_init(1L);
1919 parangesrel = saa_init(1L);
1920 saa_write16(paranges,3); /* dwarf version */
1921 saa_write64(parangesrel, paranges->datalen+4);
1922 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1923 saa_write64(parangesrel, 0);
1924 saa_write32(paranges,0); /* offset into info */
1925 saa_write8(paranges,8); /* pointer size */
1926 saa_write8(paranges,0); /* not segmented */
1927 saa_write32(paranges,0); /* padding */
1928 /* iterate though sectlist entries */
1929 psect = dwarf_fsect;
1930 totlen = 0;
1931 highaddr = 0;
1932 for (indx = 0; indx < dwarf_nsections; indx++)
1934 plinep = psect->psaa;
1935 /* Line Number Program Epilogue */
1936 saa_write8(plinep,2); /* std op 2 */
1937 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1938 saa_write8(plinep,DW_LNS_extended_op);
1939 saa_write8(plinep,1); /* operand length */
1940 saa_write8(plinep,DW_LNE_end_sequence);
1941 totlen += plinep->datalen;
1942 /* range table relocation entry */
1943 saa_write64(parangesrel, paranges->datalen + 4);
1944 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1945 saa_write64(parangesrel, (uint64_t) 0);
1946 /* range table entry */
1947 saa_write64(paranges,0x0000); /* range start */
1948 saa_write64(paranges,sects[psect->section]->len); /* range length */
1949 highaddr += sects[psect->section]->len;
1950 /* done with this entry */
1951 psect = psect->next;
1953 saa_write64(paranges,0); /* null address */
1954 saa_write64(paranges,0); /* null length */
1955 saalen = paranges->datalen;
1956 arangeslen = saalen + 4;
1957 arangesbuf = pbuf = nasm_malloc(arangeslen);
1958 WRITELONG(pbuf,saalen); /* initial length */
1959 saa_rnbytes(paranges, pbuf, saalen);
1960 saa_free(paranges);
1962 /* build rela.aranges section */
1963 arangesrellen = saalen = parangesrel->datalen;
1964 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1965 saa_rnbytes(parangesrel, pbuf, saalen);
1966 saa_free(parangesrel);
1968 /* build pubnames section */
1969 ppubnames = saa_init(1L);
1970 saa_write16(ppubnames,3); /* dwarf version */
1971 saa_write32(ppubnames,0); /* offset into info */
1972 saa_write32(ppubnames,0); /* space used in info */
1973 saa_write32(ppubnames,0); /* end of list */
1974 saalen = ppubnames->datalen;
1975 pubnameslen = saalen + 4;
1976 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1977 WRITELONG(pbuf,saalen); /* initial length */
1978 saa_rnbytes(ppubnames, pbuf, saalen);
1979 saa_free(ppubnames);
1981 /* build info section */
1982 pinfo = saa_init(1L);
1983 pinforel = saa_init(1L);
1984 saa_write16(pinfo,3); /* dwarf version */
1985 saa_write64(pinforel, pinfo->datalen + 4);
1986 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
1987 saa_write64(pinforel, 0);
1988 saa_write32(pinfo,0); /* offset into abbrev */
1989 saa_write8(pinfo,8); /* pointer size */
1990 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1991 saa_write64(pinforel, pinfo->datalen + 4);
1992 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1993 saa_write64(pinforel, 0);
1994 saa_write64(pinfo,0); /* DW_AT_low_pc */
1995 saa_write64(pinforel, pinfo->datalen + 4);
1996 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1997 saa_write64(pinforel, 0);
1998 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
1999 saa_write64(pinforel, pinfo->datalen + 4);
2000 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2001 saa_write64(pinforel, 0);
2002 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2003 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2004 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2005 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2006 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2007 saa_write64(pinforel, pinfo->datalen + 4);
2008 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2009 saa_write64(pinforel, 0);
2010 saa_write64(pinfo,0); /* DW_AT_low_pc */
2011 saa_write64(pinfo,0); /* DW_AT_frame_base */
2012 saa_write8(pinfo,0); /* end of entries */
2013 saalen = pinfo->datalen;
2014 infolen = saalen + 4;
2015 infobuf = pbuf = nasm_malloc(infolen);
2016 WRITELONG(pbuf,saalen); /* initial length */
2017 saa_rnbytes(pinfo, pbuf, saalen);
2018 saa_free(pinfo);
2020 /* build rela.info section */
2021 inforellen = saalen = pinforel->datalen;
2022 inforelbuf = pbuf = nasm_malloc(inforellen);
2023 saa_rnbytes(pinforel, pbuf, saalen);
2024 saa_free(pinforel);
2026 /* build abbrev section */
2027 pabbrev = saa_init(1L);
2028 saa_write8(pabbrev,1); /* entry number LEB128u */
2029 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2030 saa_write8(pabbrev,1); /* has children */
2031 /* the following attributes and forms are all LEB128u values */
2032 saa_write8(pabbrev,DW_AT_low_pc);
2033 saa_write8(pabbrev,DW_FORM_addr);
2034 saa_write8(pabbrev,DW_AT_high_pc);
2035 saa_write8(pabbrev,DW_FORM_addr);
2036 saa_write8(pabbrev,DW_AT_stmt_list);
2037 saa_write8(pabbrev,DW_FORM_data4);
2038 saa_write8(pabbrev,DW_AT_name);
2039 saa_write8(pabbrev,DW_FORM_string);
2040 saa_write8(pabbrev,DW_AT_producer);
2041 saa_write8(pabbrev,DW_FORM_string);
2042 saa_write8(pabbrev,DW_AT_language);
2043 saa_write8(pabbrev,DW_FORM_data2);
2044 saa_write16(pabbrev,0); /* end of entry */
2045 /* LEB128u usage same as above */
2046 saa_write8(pabbrev,2); /* entry number */
2047 saa_write8(pabbrev,DW_TAG_subprogram);
2048 saa_write8(pabbrev,0); /* no children */
2049 saa_write8(pabbrev,DW_AT_low_pc);
2050 saa_write8(pabbrev,DW_FORM_addr);
2051 saa_write8(pabbrev,DW_AT_frame_base);
2052 saa_write8(pabbrev,DW_FORM_data4);
2053 saa_write16(pabbrev,0); /* end of entry */
2054 abbrevlen = saalen = pabbrev->datalen;
2055 abbrevbuf = pbuf = nasm_malloc(saalen);
2056 saa_rnbytes(pabbrev, pbuf, saalen);
2057 saa_free(pabbrev);
2059 /* build line section */
2060 /* prolog */
2061 plines = saa_init(1L);
2062 saa_write8(plines,1); /* Minimum Instruction Length */
2063 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2064 saa_write8(plines,line_base); /* Line Base */
2065 saa_write8(plines,line_range); /* Line Range */
2066 saa_write8(plines,opcode_base); /* Opcode Base */
2067 /* standard opcode lengths (# of LEB128u operands) */
2068 saa_write8(plines,0); /* Std opcode 1 length */
2069 saa_write8(plines,1); /* Std opcode 2 length */
2070 saa_write8(plines,1); /* Std opcode 3 length */
2071 saa_write8(plines,1); /* Std opcode 4 length */
2072 saa_write8(plines,1); /* Std opcode 5 length */
2073 saa_write8(plines,0); /* Std opcode 6 length */
2074 saa_write8(plines,0); /* Std opcode 7 length */
2075 saa_write8(plines,0); /* Std opcode 8 length */
2076 saa_write8(plines,1); /* Std opcode 9 length */
2077 saa_write8(plines,0); /* Std opcode 10 length */
2078 saa_write8(plines,0); /* Std opcode 11 length */
2079 saa_write8(plines,1); /* Std opcode 12 length */
2080 /* Directory Table */
2081 saa_write8(plines,0); /* End of table */
2082 /* File Name Table */
2083 ftentry = dwarf_flist;
2084 for (indx = 0;indx<dwarf_numfiles;indx++)
2086 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2087 saa_write8(plines,0); /* directory LEB128u */
2088 saa_write8(plines,0); /* time LEB128u */
2089 saa_write8(plines,0); /* size LEB128u */
2090 ftentry = ftentry->next;
2092 saa_write8(plines,0); /* End of table */
2093 linepoff = plines->datalen;
2094 linelen = linepoff + totlen + 10;
2095 linebuf = pbuf = nasm_malloc(linelen);
2096 WRITELONG(pbuf,linelen-4); /* initial length */
2097 WRITESHORT(pbuf,3); /* dwarf version */
2098 WRITELONG(pbuf,linepoff); /* offset to line number program */
2099 /* write line header */
2100 saalen = linepoff;
2101 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2102 pbuf += linepoff;
2103 saa_free(plines);
2104 /* concatonate line program ranges */
2105 linepoff += 13;
2106 plinesrel = saa_init(1L);
2107 psect = dwarf_fsect;
2108 for (indx = 0; indx < dwarf_nsections; indx++)
2110 saa_write64(plinesrel, linepoff);
2111 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2112 saa_write64(plinesrel, (uint64_t) 0);
2113 plinep = psect->psaa;
2114 saalen = plinep->datalen;
2115 saa_rnbytes(plinep, pbuf, saalen);
2116 pbuf += saalen;
2117 linepoff += saalen;
2118 saa_free(plinep);
2119 /* done with this entry */
2120 psect = psect->next;
2124 /* build rela.lines section */
2125 linerellen =saalen = plinesrel->datalen;
2126 linerelbuf = pbuf = nasm_malloc(linerellen);
2127 saa_rnbytes(plinesrel, pbuf, saalen);
2128 saa_free(plinesrel);
2130 /* build frame section */
2131 framelen = 4;
2132 framebuf = pbuf = nasm_malloc(framelen);
2133 WRITELONG(pbuf,framelen-4); /* initial length */
2135 /* build loc section */
2136 loclen = 16;
2137 locbuf = pbuf = nasm_malloc(loclen);
2138 WRITEDLONG(pbuf,0); /* null beginning offset */
2139 WRITEDLONG(pbuf,0); /* null ending offset */
2142 void dwarf64_cleanup(void)
2144 if (arangesbuf)
2145 nasm_free(arangesbuf);
2146 if (arangesrelbuf)
2147 nasm_free(arangesrelbuf);
2148 if (pubnamesbuf)
2149 nasm_free(pubnamesbuf);
2150 if (infobuf)
2151 nasm_free(infobuf);
2152 if (inforelbuf)
2153 nasm_free(inforelbuf);
2154 if (abbrevbuf)
2155 nasm_free(abbrevbuf);
2156 if (linebuf)
2157 nasm_free(linebuf);
2158 if (linerelbuf)
2159 nasm_free(linerelbuf);
2160 if (framebuf)
2161 nasm_free(framebuf);
2162 if (locbuf)
2163 nasm_free(locbuf);
2165 void dwarf64_findfile(const char * fname)
2167 int finx;
2168 struct linelist *match;
2170 /* return if fname is current file name */
2171 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2172 /* search for match */
2173 else
2175 match = 0;
2176 if (dwarf_flist)
2178 match = dwarf_flist;
2179 for (finx = 0; finx < dwarf_numfiles; finx++)
2181 if (!(strcmp(fname, match->filename)))
2183 dwarf_clist = match;
2184 return;
2188 /* add file name to end of list */
2189 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2190 dwarf_numfiles++;
2191 dwarf_clist->line = dwarf_numfiles;
2192 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2193 strcpy(dwarf_clist->filename,fname);
2194 dwarf_clist->next = 0;
2195 /* if first entry */
2196 if (!dwarf_flist)
2198 dwarf_flist = dwarf_elist = dwarf_clist;
2199 dwarf_clist->last = 0;
2201 /* chain to previous entry */
2202 else
2204 dwarf_elist->next = dwarf_clist;
2205 dwarf_elist = dwarf_clist;
2209 /* */
2210 void dwarf64_findsect(const int index)
2212 int sinx;
2213 struct sectlist *match;
2214 struct SAA *plinep;
2215 /* return if index is current section index */
2216 if (dwarf_csect && (dwarf_csect->section == index))
2218 return;
2220 /* search for match */
2221 else
2223 match = 0;
2224 if (dwarf_fsect)
2226 match = dwarf_fsect;
2227 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2229 if ((match->section == index))
2231 dwarf_csect = match;
2232 return;
2234 match = match->next;
2237 /* add entry to end of list */
2238 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2239 dwarf_nsections++;
2240 dwarf_csect->psaa = plinep = saa_init(1L);
2241 dwarf_csect->line = 1;
2242 dwarf_csect->offset = 0;
2243 dwarf_csect->file = 1;
2244 dwarf_csect->section = index;
2245 dwarf_csect->next = 0;
2246 /* set relocatable address at start of line program */
2247 saa_write8(plinep,DW_LNS_extended_op);
2248 saa_write8(plinep,9); /* operand length */
2249 saa_write8(plinep,DW_LNE_set_address);
2250 saa_write64(plinep,0); /* Start Address */
2251 /* if first entry */
2252 if (!dwarf_fsect)
2254 dwarf_fsect = dwarf_esect = dwarf_csect;
2255 dwarf_csect->last = 0;
2257 /* chain to previous entry */
2258 else
2260 dwarf_esect->next = dwarf_csect;
2261 dwarf_esect = dwarf_csect;
2266 #endif /* OF_ELF */