ELF: remove loop invariant calculation of global offset
[nasm.git] / output / outelf64.c
blob05a5356672569adc3f8e339041b8314495225204
1 /* outelf64.c output routines for the Netwide Assembler to produce
2 * ELF64 (x86_64 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
9 #include "compiler.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <inttypes.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "saa.h"
20 #include "raa.h"
21 #include "stdscan.h"
22 #include "outform.h"
23 #include "outlib.h"
24 #include "rbtree.h"
26 #include "output/elf64.h"
27 #include "output/dwarf.h"
28 #include "output/outelf.h"
30 #ifdef OF_ELF64
32 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
34 struct Reloc {
35 struct Reloc *next;
36 int64_t address; /* relative to _start_ of section */
37 int64_t symbol; /* symbol index */
38 int64_t offset; /* symbol addend */
39 int type; /* type of relocation */
42 struct Symbol {
43 struct rbtree symv; /* symbol value and rbtree of globals */
44 int32_t strpos; /* string table position of name */
45 int32_t section; /* section ID of the symbol */
46 int type; /* symbol type */
47 int other; /* symbol visibility */
48 int32_t size; /* size of symbol */
49 int32_t globnum; /* symbol table offset if global */
50 struct Symbol *nextfwd; /* list of unresolved-size symbols */
51 char *name; /* used temporarily if in above list */
54 struct Section {
55 struct SAA *data;
56 uint64_t len, size;
57 uint32_t nrelocs;
58 int32_t index; /* index into sects array */
59 int type; /* SHT_PROGBITS or SHT_NOBITS */
60 uint64_t align; /* alignment: power of two */
61 uint64_t flags; /* section flags */
62 char *name;
63 struct SAA *rel;
64 uint64_t rellen;
65 struct Reloc *head, **tail;
66 struct rbtree *gsyms; /* global symbols in section */
69 #define SECT_DELTA 32
70 static struct Section **sects;
71 static int nsects, sectlen;
73 #define SHSTR_DELTA 256
74 static char *shstrtab;
75 static int shstrtablen, shstrtabsize;
77 static struct SAA *syms;
78 static uint32_t nlocals, nglobs, ndebugs;
80 static int32_t def_seg;
82 static struct RAA *bsym;
84 static struct SAA *strs;
85 static uint32_t strslen;
87 static FILE *elffp;
88 static efunc error;
89 static evalfunc evaluate;
91 static struct Symbol *fwds;
93 static char elf_module[FILENAME_MAX];
95 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
96 static uint8_t elf_abiver = 0; /* Current ABI version */
98 extern struct ofmt of_elf64;
100 static struct ELF_SECTDATA {
101 void *data;
102 int64_t len;
103 bool is_saa;
104 } *elf_sects;
105 static int elf_nsect, nsections;
106 static int64_t elf_foffs;
108 static void elf_write(void);
109 static void elf_sect_write(struct Section *, const void *, size_t);
110 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
111 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
112 int, int);
113 static void elf_write_sections(void);
114 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
115 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
116 static void add_sectname(char *, char *);
118 /* type values for stabs debugging sections */
119 #define N_SO 0x64 /* ID for main source file */
120 #define N_SOL 0x84 /* ID for sub-source file */
121 #define N_BINCL 0x82 /* not currently used */
122 #define N_EINCL 0xA2 /* not currently used */
123 #define N_SLINE 0x44
125 struct stabentry {
126 uint32_t n_strx;
127 uint8_t n_type;
128 uint8_t n_other;
129 uint16_t n_desc;
130 uint32_t n_value;
133 struct erel {
134 int offset, info;
137 struct symlininfo {
138 int offset;
139 int section; /* index into sects[] */
140 int segto; /* internal section number */
141 char *name; /* shallow-copied pointer of section name */
144 struct linelist {
145 struct symlininfo info;
146 int line;
147 char *filename;
148 struct linelist *next;
149 struct linelist *last;
152 struct sectlist {
153 struct SAA *psaa;
154 int section;
155 int line;
156 int offset;
157 int file;
158 struct sectlist *next;
159 struct sectlist *last;
162 /* common debug variables */
163 static int currentline = 1;
164 static int debug_immcall = 0;
166 /* stabs debug variables */
167 static struct linelist *stabslines = 0;
168 static int numlinestabs = 0;
169 static char *stabs_filename = 0;
170 static int symtabsection;
171 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
172 static int stablen, stabstrlen, stabrellen;
174 /* dwarf debug variables */
175 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
176 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
177 static int dwarf_numfiles = 0, dwarf_nsections;
178 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
179 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
180 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
181 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
182 abbrevlen, linelen, linerellen, framelen, loclen;
183 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
186 static struct dfmt df_dwarf;
187 static struct dfmt df_stabs;
188 static struct Symbol *lastsym;
190 /* common debugging routines */
191 void debug64_typevalue(int32_t);
192 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
193 void debug64_directive(const char *, const char *);
195 /* stabs debugging routines */
196 void stabs64_init(struct ofmt *, void *, FILE *, efunc);
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_init(struct ofmt *, void *, FILE *, efunc);
204 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
205 void dwarf64_output(int, void *);
206 void dwarf64_generate(void);
207 void dwarf64_cleanup(void);
208 void dwarf64_findfile(const char *);
209 void dwarf64_findsect(const int);
212 * Special section numbers which are used to define ELF special
213 * symbols, which can be used with WRT to provide PIC relocation
214 * types.
216 static int32_t elf_gotpc_sect, elf_gotoff_sect;
217 static int32_t elf_got_sect, elf_plt_sect;
218 static int32_t elf_sym_sect;
219 static int32_t elf_gottpoff_sect;
221 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
223 maxbits = 64;
224 elffp = fp;
225 error = errfunc;
226 evaluate = eval;
227 (void)ldef; /* placate optimisers */
228 sects = NULL;
229 nsects = sectlen = 0;
230 syms = saa_init((int32_t)sizeof(struct Symbol));
231 nlocals = nglobs = ndebugs = 0;
232 bsym = raa_init();
233 strs = saa_init(1L);
234 saa_wbytes(strs, "\0", 1L);
235 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
236 strslen = 2 + strlen(elf_module);
237 shstrtab = NULL;
238 shstrtablen = shstrtabsize = 0;;
239 add_sectname("", "");
241 fwds = NULL;
243 elf_gotpc_sect = seg_alloc();
244 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
245 error);
246 elf_gotoff_sect = seg_alloc();
247 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
248 error);
249 elf_got_sect = seg_alloc();
250 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
251 error);
252 elf_plt_sect = seg_alloc();
253 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
254 error);
255 elf_sym_sect = seg_alloc();
256 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
257 error);
258 elf_gottpoff_sect = seg_alloc();
259 ldef("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false, &of_elf64,
260 error);
262 def_seg = seg_alloc();
266 static void elf_cleanup(int debuginfo)
268 struct Reloc *r;
269 int i;
271 (void)debuginfo;
273 elf_write();
274 fclose(elffp);
275 for (i = 0; i < nsects; i++) {
276 if (sects[i]->type != SHT_NOBITS)
277 saa_free(sects[i]->data);
278 if (sects[i]->head)
279 saa_free(sects[i]->rel);
280 while (sects[i]->head) {
281 r = sects[i]->head;
282 sects[i]->head = sects[i]->head->next;
283 nasm_free(r);
286 nasm_free(sects);
287 saa_free(syms);
288 raa_free(bsym);
289 saa_free(strs);
290 if (of_elf64.current_dfmt) {
291 of_elf64.current_dfmt->cleanup();
294 /* add entry to the elf .shstrtab section */
295 static void add_sectname(char *firsthalf, char *secondhalf)
297 int len = strlen(firsthalf) + strlen(secondhalf);
298 while (shstrtablen + len + 1 > shstrtabsize)
299 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
300 strcpy(shstrtab + shstrtablen, firsthalf);
301 strcat(shstrtab + shstrtablen, secondhalf);
302 shstrtablen += len + 1;
305 static int elf_make_section(char *name, int type, int flags, int align)
307 struct Section *s;
309 s = nasm_malloc(sizeof(*s));
311 if (type != SHT_NOBITS)
312 s->data = saa_init(1L);
313 s->head = NULL;
314 s->tail = &s->head;
315 s->len = s->size = 0;
316 s->nrelocs = 0;
317 if (!strcmp(name, ".text"))
318 s->index = def_seg;
319 else
320 s->index = seg_alloc();
321 add_sectname("", name);
322 s->name = nasm_malloc(1 + strlen(name));
323 strcpy(s->name, name);
324 s->type = type;
325 s->flags = flags;
326 s->align = align;
327 s->gsyms = NULL;
329 if (nsects >= sectlen)
330 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
331 sects[nsects++] = s;
333 return nsects - 1;
336 static int32_t elf_section_names(char *name, int pass, int *bits)
338 char *p;
339 uint32_t flags, flags_and, flags_or;
340 uint64_t align;
341 int type, i;
344 * Default is 64 bits.
346 if (!name) {
347 *bits = 64;
348 return def_seg;
351 p = name;
352 while (*p && !nasm_isspace(*p))
353 p++;
354 if (*p)
355 *p++ = '\0';
356 flags_and = flags_or = type = align = 0;
358 while (*p && nasm_isspace(*p))
359 p++;
360 while (*p) {
361 char *q = p;
362 while (*p && !nasm_isspace(*p))
363 p++;
364 if (*p)
365 *p++ = '\0';
366 while (*p && nasm_isspace(*p))
367 p++;
369 if (!nasm_strnicmp(q, "align=", 6)) {
370 align = atoi(q + 6);
371 if (align == 0)
372 align = 1;
373 if ((align - 1) & align) { /* means it's not a power of two */
374 error(ERR_NONFATAL, "section alignment %d is not"
375 " a power of two", align);
376 align = 1;
378 } else if (!nasm_stricmp(q, "alloc")) {
379 flags_and |= SHF_ALLOC;
380 flags_or |= SHF_ALLOC;
381 } else if (!nasm_stricmp(q, "noalloc")) {
382 flags_and |= SHF_ALLOC;
383 flags_or &= ~SHF_ALLOC;
384 } else if (!nasm_stricmp(q, "exec")) {
385 flags_and |= SHF_EXECINSTR;
386 flags_or |= SHF_EXECINSTR;
387 } else if (!nasm_stricmp(q, "noexec")) {
388 flags_and |= SHF_EXECINSTR;
389 flags_or &= ~SHF_EXECINSTR;
390 } else if (!nasm_stricmp(q, "write")) {
391 flags_and |= SHF_WRITE;
392 flags_or |= SHF_WRITE;
393 } else if (!nasm_stricmp(q, "tls")) {
394 flags_and |= SHF_TLS;
395 flags_or |= SHF_TLS;
396 } else if (!nasm_stricmp(q, "nowrite")) {
397 flags_and |= SHF_WRITE;
398 flags_or &= ~SHF_WRITE;
399 } else if (!nasm_stricmp(q, "progbits")) {
400 type = SHT_PROGBITS;
401 } else if (!nasm_stricmp(q, "nobits")) {
402 type = SHT_NOBITS;
403 } else if (pass == 1) 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 char *p;
1027 int i;
1029 struct SAA *symtab;
1030 int32_t symtablen, symtablocal;
1033 * Work out how many sections we will have. We have SHN_UNDEF,
1034 * then the flexible user sections, then the fixed sections
1035 * `.shstrtab', `.symtab' and `.strtab', then optionally
1036 * relocation sections for the user sections.
1038 nsections = sec_numspecial + 1;
1039 if (of_elf64.current_dfmt == &df_stabs)
1040 nsections += 3;
1041 else if (of_elf64.current_dfmt == &df_dwarf)
1042 nsections += 10;
1044 add_sectname("", ".shstrtab");
1045 add_sectname("", ".symtab");
1046 add_sectname("", ".strtab");
1047 for (i = 0; i < nsects; i++) {
1048 nsections++; /* for the section itself */
1049 if (sects[i]->head) {
1050 nsections++; /* for its relocations */
1051 add_sectname(".rela", sects[i]->name);
1055 if (of_elf64.current_dfmt == &df_stabs) {
1056 /* in case the debug information is wanted, just add these three sections... */
1057 add_sectname("", ".stab");
1058 add_sectname("", ".stabstr");
1059 add_sectname(".rel", ".stab");
1062 else if (of_elf64.current_dfmt == &df_dwarf) {
1063 /* the dwarf debug standard specifies the following ten sections,
1064 not all of which are currently implemented,
1065 although all of them are defined. */
1066 #define debug_aranges (int64_t) (nsections-10)
1067 #define debug_info (int64_t) (nsections-7)
1068 #define debug_abbrev (int64_t) (nsections-5)
1069 #define debug_line (int64_t) (nsections-4)
1070 add_sectname("", ".debug_aranges");
1071 add_sectname(".rela", ".debug_aranges");
1072 add_sectname("", ".debug_pubnames");
1073 add_sectname("", ".debug_info");
1074 add_sectname(".rela", ".debug_info");
1075 add_sectname("", ".debug_abbrev");
1076 add_sectname("", ".debug_line");
1077 add_sectname(".rela", ".debug_line");
1078 add_sectname("", ".debug_frame");
1079 add_sectname("", ".debug_loc");
1083 * Output the ELF header.
1085 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1086 fputc(elf_osabi, elffp);
1087 fputc(elf_abiver, elffp);
1088 fwritezero(7, elffp);
1089 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1090 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1091 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1092 fwriteint64_t(0L, elffp); /* no entry point */
1093 fwriteint64_t(0L, elffp); /* no program header table */
1094 fwriteint64_t(0x40L, elffp); /* section headers straight after
1095 * ELF header plus alignment */
1096 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1097 fwriteint16_t(0x40, elffp); /* size of ELF header */
1098 fwriteint16_t(0, elffp); /* no program header table, again */
1099 fwriteint16_t(0, elffp); /* still no program header table */
1100 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1101 fwriteint16_t(nsections, elffp); /* number of sections */
1102 fwriteint16_t(sec_shstrtab, elffp); /* string table section index for
1103 * section header table */
1106 * Build the symbol table and relocation tables.
1108 symtab = elf_build_symtab(&symtablen, &symtablocal);
1109 for (i = 0; i < nsects; i++)
1110 if (sects[i]->head)
1111 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1112 sects[i]->head);
1115 * Now output the section header table.
1118 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1119 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1120 elf_foffs += align;
1121 elf_nsect = 0;
1122 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1124 /* SHN_UNDEF */
1125 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1126 p = shstrtab + 1;
1128 /* The normal sections */
1129 for (i = 0; i < nsects; i++) {
1130 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1131 (sects[i]->type == SHT_PROGBITS ?
1132 sects[i]->data : NULL), true,
1133 sects[i]->len, 0, 0, sects[i]->align, 0);
1134 p += strlen(p) + 1;
1137 /* .shstrtab */
1138 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1139 shstrtablen, 0, 0, 1, 0);
1140 p += strlen(p) + 1;
1142 /* .symtab */
1143 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1144 symtablen, sec_strtab, symtablocal, 4, 24);
1145 p += strlen(p) + 1;
1147 /* .strtab */
1148 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, strs, true,
1149 strslen, 0, 0, 1, 0);
1150 p += strlen(p) + 1;
1152 /* The relocation sections */
1153 for (i = 0; i < nsects; i++)
1154 if (sects[i]->head) {
1155 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1156 sects[i]->rellen, sec_symtab, i + 1, 4, 24);
1157 p += strlen(p) + 1;
1160 if (of_elf64.current_dfmt == &df_stabs) {
1161 /* for debugging information, create the last three sections
1162 which are the .stab , .stabstr and .rel.stab sections respectively */
1164 /* this function call creates the stab sections in memory */
1165 stabs64_generate();
1167 if (stabbuf && stabstrbuf && stabrelbuf) {
1168 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1169 stablen, sec_stabstr, 0, 4, 12);
1170 p += strlen(p) + 1;
1172 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1173 stabstrlen, 0, 0, 4, 0);
1174 p += strlen(p) + 1;
1176 /* link -> symtable info -> section to refer to */
1177 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1178 stabrellen, symtabsection, sec_stab, 4, 16);
1179 p += strlen(p) + 1;
1182 else if (of_elf64.current_dfmt == &df_dwarf) {
1183 /* for dwarf debugging information, create the ten dwarf sections */
1185 /* this function call creates the dwarf sections in memory */
1186 if (dwarf_fsect)
1187 dwarf64_generate();
1189 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1190 arangeslen, 0, 0, 1, 0);
1191 p += strlen(p) + 1;
1193 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1194 arangesrellen, symtabsection, debug_aranges, 1, 24);
1195 p += strlen(p) + 1;
1197 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1198 pubnameslen, 0, 0, 1, 0);
1199 p += strlen(p) + 1;
1201 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1202 infolen, 0, 0, 1, 0);
1203 p += strlen(p) + 1;
1205 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1206 inforellen, symtabsection, debug_info, 1, 24);
1207 p += strlen(p) + 1;
1209 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1210 abbrevlen, 0, 0, 1, 0);
1211 p += strlen(p) + 1;
1213 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1214 linelen, 0, 0, 1, 0);
1215 p += strlen(p) + 1;
1217 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1218 linerellen, symtabsection, debug_line, 1, 24);
1219 p += strlen(p) + 1;
1221 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1222 framelen, 0, 0, 8, 0);
1223 p += strlen(p) + 1;
1225 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1226 loclen, 0, 0, 1, 0);
1227 p += strlen(p) + 1;
1229 fwritezero(align, elffp);
1232 * Now output the sections.
1234 elf_write_sections();
1236 nasm_free(elf_sects);
1237 saa_free(symtab);
1240 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1242 struct SAA *s = saa_init(1L);
1243 struct Symbol *sym;
1244 uint8_t entry[24], *p;
1245 int i;
1247 *len = *local = 0;
1250 * First, an all-zeros entry, required by the ELF spec.
1252 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1253 *len += 24;
1254 (*local)++;
1257 * Next, an entry for the file name.
1259 p = entry;
1260 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1261 WRITESHORT(p, STT_FILE); /* type FILE */
1262 WRITESHORT(p, SHN_ABS);
1263 WRITEDLONG(p, (uint64_t) 0); /* no value */
1264 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1265 saa_wbytes(s, entry, 24L);
1266 *len += 24;
1267 (*local)++;
1270 * Now some standard symbols defining the segments, for relocation
1271 * purposes.
1273 for (i = 1; i <= nsects; i++) {
1274 p = entry;
1275 WRITELONG(p, 0); /* no symbol name */
1276 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1277 WRITESHORT(p, i); /* section id */
1278 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1279 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1280 saa_wbytes(s, entry, 24L);
1281 *len += 24;
1282 (*local)++;
1287 * Now the other local symbols.
1289 saa_rewind(syms);
1290 while ((sym = saa_rstruct(syms))) {
1291 if (sym->type & SYM_GLOBAL)
1292 continue;
1293 p = entry;
1294 WRITELONG(p, sym->strpos); /* index into symbol string table */
1295 WRITECHAR(p, sym->type); /* type and binding */
1296 WRITECHAR(p, sym->other); /* visibility */
1297 WRITESHORT(p, sym->section); /* index into section header table */
1298 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1299 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1300 saa_wbytes(s, entry, 24L);
1301 *len += 24;
1302 (*local)++;
1305 * dwarf needs symbols for debug sections
1306 * which are relocation targets.
1308 if (of_elf64.current_dfmt == &df_dwarf) {
1309 dwarf_infosym = *local;
1310 p = entry;
1311 WRITELONG(p, 0); /* no symbol name */
1312 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1313 WRITESHORT(p, debug_info); /* 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)++;
1319 dwarf_abbrevsym = *local;
1320 p = entry;
1321 WRITELONG(p, 0); /* no symbol name */
1322 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1323 WRITESHORT(p, debug_abbrev); /* section id */
1324 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1325 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1326 saa_wbytes(s, entry, 24L);
1327 *len += 24;
1328 (*local)++;
1329 dwarf_linesym = *local;
1330 p = entry;
1331 WRITELONG(p, 0); /* no symbol name */
1332 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1333 WRITESHORT(p, debug_line); /* section id */
1334 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1335 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1336 saa_wbytes(s, entry, 24L);
1337 *len += 24;
1338 (*local)++;
1342 * Now the global symbols.
1344 saa_rewind(syms);
1345 while ((sym = saa_rstruct(syms))) {
1346 if (!(sym->type & SYM_GLOBAL))
1347 continue;
1348 p = entry;
1349 WRITELONG(p, sym->strpos);
1350 WRITECHAR(p, sym->type); /* type and binding */
1351 WRITECHAR(p, sym->other); /* visibility */
1352 WRITESHORT(p, sym->section);
1353 WRITEDLONG(p, (int64_t)sym->symv.key);
1354 WRITEDLONG(p, (int64_t)sym->size);
1355 saa_wbytes(s, entry, 24L);
1356 *len += 24;
1359 return s;
1362 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1364 struct SAA *s;
1365 uint8_t *p, entry[24];
1366 int32_t global_offset;
1368 if (!r)
1369 return NULL;
1371 s = saa_init(1L);
1372 *len = 0;
1375 * How to onvert from a global placeholder to a real symbol index;
1376 * the +2 refers to the two special entries, the null entry and
1377 * the filename entry.
1379 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1381 while (r) {
1382 int32_t sym = r->symbol;
1384 if (sym >= GLOBAL_TEMP_BASE)
1385 sym += global_offset;
1387 p = entry;
1388 WRITEDLONG(p, r->address);
1389 WRITELONG(p, r->type);
1390 WRITELONG(p, sym);
1391 WRITEDLONG(p, r->offset);
1392 saa_wbytes(s, entry, 24L);
1393 *len += 24;
1395 r = r->next;
1398 return s;
1401 static void elf_section_header(int name, int type, uint64_t flags,
1402 void *data, bool is_saa, uint64_t datalen,
1403 int link, int info, int align, int eltsize)
1405 elf_sects[elf_nsect].data = data;
1406 elf_sects[elf_nsect].len = datalen;
1407 elf_sects[elf_nsect].is_saa = is_saa;
1408 elf_nsect++;
1410 fwriteint32_t((int32_t)name, elffp);
1411 fwriteint32_t((int32_t)type, elffp);
1412 fwriteint64_t((int64_t)flags, elffp);
1413 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1414 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1415 fwriteint64_t(datalen, elffp);
1416 if (data)
1417 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1418 fwriteint32_t((int32_t)link, elffp);
1419 fwriteint32_t((int32_t)info, elffp);
1420 fwriteint64_t((int64_t)align, elffp);
1421 fwriteint64_t((int64_t)eltsize, elffp);
1424 static void elf_write_sections(void)
1426 int i;
1427 for (i = 0; i < elf_nsect; i++)
1428 if (elf_sects[i].data) {
1429 int32_t len = elf_sects[i].len;
1430 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1431 int32_t align = reallen - len;
1432 if (elf_sects[i].is_saa)
1433 saa_fpwrite(elf_sects[i].data, elffp);
1434 else
1435 fwrite(elf_sects[i].data, len, 1, elffp);
1436 fwritezero(align, elffp);
1440 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1442 saa_wbytes(sect->data, data, len);
1443 sect->len += len;
1445 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1447 saa_writeaddr(sect->data, data, len);
1448 sect->len += len;
1451 static int32_t elf_segbase(int32_t segment)
1453 return segment;
1456 static int elf_directive(char *directive, char *value, int pass)
1458 bool err;
1459 int64_t n;
1460 char *p;
1462 if (!strcmp(directive, "osabi")) {
1463 if (pass == 2)
1464 return 1; /* ignore in pass 2 */
1466 n = readnum(value, &err);
1467 if (err) {
1468 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1469 return 1;
1471 if (n < 0 || n > 255) {
1472 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1473 return 1;
1475 elf_osabi = n;
1476 elf_abiver = 0;
1478 if ((p = strchr(value,',')) == NULL)
1479 return 1;
1481 n = readnum(p+1, &err);
1482 if (err || n < 0 || n > 255) {
1483 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1484 return 1;
1487 elf_abiver = n;
1488 return 1;
1491 return 0;
1494 static void elf_filename(char *inname, char *outname, efunc error)
1496 strcpy(elf_module, inname);
1497 standard_extension(inname, outname, ".o", error);
1500 extern macros_t elf_stdmac[];
1502 static int elf_set_info(enum geninfo type, char **val)
1504 (void)type;
1505 (void)val;
1506 return 0;
1508 static struct dfmt df_dwarf = {
1509 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1510 "dwarf",
1511 dwarf64_init,
1512 dwarf64_linenum,
1513 debug64_deflabel,
1514 debug64_directive,
1515 debug64_typevalue,
1516 dwarf64_output,
1517 dwarf64_cleanup
1519 static struct dfmt df_stabs = {
1520 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1521 "stabs",
1522 stabs64_init,
1523 stabs64_linenum,
1524 debug64_deflabel,
1525 debug64_directive,
1526 debug64_typevalue,
1527 stabs64_output,
1528 stabs64_cleanup
1531 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1533 struct ofmt of_elf64 = {
1534 "ELF64 (x86_64) object files (e.g. Linux)",
1535 "elf64",
1536 NULL,
1537 elf64_debugs_arr,
1538 &df_stabs,
1539 elf_stdmac,
1540 elf_init,
1541 elf_set_info,
1542 elf_out,
1543 elf_deflabel,
1544 elf_section_names,
1545 elf_segbase,
1546 elf_directive,
1547 elf_filename,
1548 elf_cleanup
1551 /* common debugging routines */
1552 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1553 char *special)
1555 (void)name;
1556 (void)segment;
1557 (void)offset;
1558 (void)is_global;
1559 (void)special;
1562 void debug64_directive(const char *directive, const char *params)
1564 (void)directive;
1565 (void)params;
1568 void debug64_typevalue(int32_t type)
1570 int32_t stype, ssize;
1571 switch (TYM_TYPE(type)) {
1572 case TY_LABEL:
1573 ssize = 0;
1574 stype = STT_NOTYPE;
1575 break;
1576 case TY_BYTE:
1577 ssize = 1;
1578 stype = STT_OBJECT;
1579 break;
1580 case TY_WORD:
1581 ssize = 2;
1582 stype = STT_OBJECT;
1583 break;
1584 case TY_DWORD:
1585 ssize = 4;
1586 stype = STT_OBJECT;
1587 break;
1588 case TY_FLOAT:
1589 ssize = 4;
1590 stype = STT_OBJECT;
1591 break;
1592 case TY_QWORD:
1593 ssize = 8;
1594 stype = STT_OBJECT;
1595 break;
1596 case TY_TBYTE:
1597 ssize = 10;
1598 stype = STT_OBJECT;
1599 break;
1600 case TY_OWORD:
1601 ssize = 16;
1602 stype = STT_OBJECT;
1603 break;
1604 case TY_COMMON:
1605 ssize = 0;
1606 stype = STT_COMMON;
1607 break;
1608 case TY_SEG:
1609 ssize = 0;
1610 stype = STT_SECTION;
1611 break;
1612 case TY_EXTERN:
1613 ssize = 0;
1614 stype = STT_NOTYPE;
1615 break;
1616 case TY_EQU:
1617 ssize = 0;
1618 stype = STT_NOTYPE;
1619 break;
1620 default:
1621 ssize = 0;
1622 stype = STT_NOTYPE;
1623 break;
1625 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1626 lastsym->size = ssize;
1627 lastsym->type = stype;
1631 /* stabs debugging routines */
1632 void stabs64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1634 (void)of;
1635 (void)id;
1636 (void)fp;
1637 (void)error;
1641 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1643 (void)segto;
1644 if (!stabs_filename) {
1645 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1646 strcpy(stabs_filename, filename);
1647 } else {
1648 if (strcmp(stabs_filename, filename)) {
1649 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1650 in fact, this leak comes in quite handy to maintain a list of files
1651 encountered so far in the symbol lines... */
1653 /* why not nasm_free(stabs_filename); we're done with the old one */
1655 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1656 strcpy(stabs_filename, filename);
1659 debug_immcall = 1;
1660 currentline = linenumber;
1664 void stabs64_output(int type, void *param)
1666 struct symlininfo *s;
1667 struct linelist *el;
1668 if (type == TY_DEBUGSYMLIN) {
1669 if (debug_immcall) {
1670 s = (struct symlininfo *)param;
1671 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1672 return; /* line info is only collected for executable sections */
1673 numlinestabs++;
1674 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1675 el->info.offset = s->offset;
1676 el->info.section = s->section;
1677 el->info.name = s->name;
1678 el->line = currentline;
1679 el->filename = stabs_filename;
1680 el->next = 0;
1681 if (stabslines) {
1682 stabslines->last->next = el;
1683 stabslines->last = el;
1684 } else {
1685 stabslines = el;
1686 stabslines->last = el;
1690 debug_immcall = 0;
1693 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1694 do {\
1695 WRITELONG(p,n_strx); \
1696 WRITECHAR(p,n_type); \
1697 WRITECHAR(p,n_other); \
1698 WRITESHORT(p,n_desc); \
1699 WRITELONG(p,n_value); \
1700 } while (0)
1702 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1704 void stabs64_generate(void)
1706 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1707 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1708 char **allfiles;
1709 int *fileidx;
1711 struct linelist *ptr;
1713 ptr = stabslines;
1715 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1716 for (i = 0; i < numlinestabs; i++)
1717 allfiles[i] = 0;
1718 numfiles = 0;
1719 while (ptr) {
1720 if (numfiles == 0) {
1721 allfiles[0] = ptr->filename;
1722 numfiles++;
1723 } else {
1724 for (i = 0; i < numfiles; i++) {
1725 if (!strcmp(allfiles[i], ptr->filename))
1726 break;
1728 if (i >= numfiles) {
1729 allfiles[i] = ptr->filename;
1730 numfiles++;
1733 ptr = ptr->next;
1735 strsize = 1;
1736 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1737 for (i = 0; i < numfiles; i++) {
1738 fileidx[i] = strsize;
1739 strsize += strlen(allfiles[i]) + 1;
1741 mainfileindex = 0;
1742 for (i = 0; i < numfiles; i++) {
1743 if (!strcmp(allfiles[i], elf_module)) {
1744 mainfileindex = i;
1745 break;
1749 /* worst case size of the stab buffer would be:
1750 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1752 sbuf =
1753 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1754 sizeof(struct stabentry));
1756 ssbuf = (uint8_t *)nasm_malloc(strsize);
1758 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1759 rptr = rbuf;
1761 for (i = 0; i < numfiles; i++) {
1762 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1764 ssbuf[0] = 0;
1766 stabstrlen = strsize; /* set global variable for length of stab strings */
1768 sptr = sbuf;
1769 ptr = stabslines;
1770 numstabs = 0;
1772 if (ptr) {
1773 /* this is the first stab, its strx points to the filename of the
1774 the source-file, the n_desc field should be set to the number
1775 of remaining stabs
1777 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1779 /* this is the stab for the main source file */
1780 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1782 /* relocation table entry */
1784 /* Since the symbol table has two entries before */
1785 /* the section symbols, the index in the info.section */
1786 /* member must be adjusted by adding 2 */
1788 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1789 WRITELONG(rptr, R_X86_64_32);
1790 WRITELONG(rptr, ptr->info.section + 2);
1792 numstabs++;
1793 currfile = mainfileindex;
1796 while (ptr) {
1797 if (strcmp(allfiles[currfile], ptr->filename)) {
1798 /* oops file has changed... */
1799 for (i = 0; i < numfiles; i++)
1800 if (!strcmp(allfiles[i], ptr->filename))
1801 break;
1802 currfile = i;
1803 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1804 ptr->info.offset);
1805 numstabs++;
1807 /* relocation table entry */
1809 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1810 WRITELONG(rptr, R_X86_64_32);
1811 WRITELONG(rptr, ptr->info.section + 2);
1814 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1815 numstabs++;
1817 /* relocation table entry */
1819 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1820 WRITELONG(rptr, R_X86_64_32);
1821 WRITELONG(rptr, ptr->info.section + 2);
1823 ptr = ptr->next;
1827 ((struct stabentry *)sbuf)->n_desc = numstabs;
1829 nasm_free(allfiles);
1830 nasm_free(fileidx);
1832 stablen = (sptr - sbuf);
1833 stabrellen = (rptr - rbuf);
1834 stabrelbuf = rbuf;
1835 stabbuf = sbuf;
1836 stabstrbuf = ssbuf;
1839 void stabs64_cleanup(void)
1841 struct linelist *ptr, *del;
1842 if (!stabslines)
1843 return;
1844 ptr = stabslines;
1845 while (ptr) {
1846 del = ptr;
1847 ptr = ptr->next;
1848 nasm_free(del);
1850 if (stabbuf)
1851 nasm_free(stabbuf);
1852 if (stabrelbuf)
1853 nasm_free(stabrelbuf);
1854 if (stabstrbuf)
1855 nasm_free(stabstrbuf);
1857 /* dwarf routines */
1858 void dwarf64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1860 (void)of;
1861 (void)id;
1862 (void)fp;
1863 (void)error;
1865 ndebugs = 3; /* 3 debug symbols */
1868 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1870 (void)segto;
1871 dwarf64_findfile(filename);
1872 debug_immcall = 1;
1873 currentline = linenumber;
1876 /* called from elf_out with type == TY_DEBUGSYMLIN */
1877 void dwarf64_output(int type, void *param)
1879 int ln, aa, inx, maxln, soc;
1880 struct symlininfo *s;
1881 struct SAA *plinep;
1883 (void)type;
1885 s = (struct symlininfo *)param;
1886 /* line number info is only gathered for executable sections */
1887 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1888 return;
1889 /* Check if section index has changed */
1890 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1892 dwarf64_findsect(s->section);
1894 /* do nothing unless line or file has changed */
1895 if (debug_immcall)
1897 ln = currentline - dwarf_csect->line;
1898 aa = s->offset - dwarf_csect->offset;
1899 inx = dwarf_clist->line;
1900 plinep = dwarf_csect->psaa;
1901 /* check for file change */
1902 if (!(inx == dwarf_csect->file))
1904 saa_write8(plinep,DW_LNS_set_file);
1905 saa_write8(plinep,inx);
1906 dwarf_csect->file = inx;
1908 /* check for line change */
1909 if (ln)
1911 /* test if in range of special op code */
1912 maxln = line_base + line_range;
1913 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1914 if (ln >= line_base && ln < maxln && soc < 256)
1916 saa_write8(plinep,soc);
1918 else
1920 if (ln)
1922 saa_write8(plinep,DW_LNS_advance_line);
1923 saa_wleb128s(plinep,ln);
1925 if (aa)
1927 saa_write8(plinep,DW_LNS_advance_pc);
1928 saa_wleb128u(plinep,aa);
1931 dwarf_csect->line = currentline;
1932 dwarf_csect->offset = s->offset;
1934 /* show change handled */
1935 debug_immcall = 0;
1940 void dwarf64_generate(void)
1942 uint8_t *pbuf;
1943 int indx;
1944 struct linelist *ftentry;
1945 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1946 struct SAA *parangesrel, *plinesrel, *pinforel;
1947 struct sectlist *psect;
1948 size_t saalen, linepoff, totlen, highaddr;
1950 /* write epilogues for each line program range */
1951 /* and build aranges section */
1952 paranges = saa_init(1L);
1953 parangesrel = saa_init(1L);
1954 saa_write16(paranges,3); /* dwarf version */
1955 saa_write64(parangesrel, paranges->datalen+4);
1956 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1957 saa_write64(parangesrel, 0);
1958 saa_write32(paranges,0); /* offset into info */
1959 saa_write8(paranges,8); /* pointer size */
1960 saa_write8(paranges,0); /* not segmented */
1961 saa_write32(paranges,0); /* padding */
1962 /* iterate though sectlist entries */
1963 psect = dwarf_fsect;
1964 totlen = 0;
1965 highaddr = 0;
1966 for (indx = 0; indx < dwarf_nsections; indx++)
1968 plinep = psect->psaa;
1969 /* Line Number Program Epilogue */
1970 saa_write8(plinep,2); /* std op 2 */
1971 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1972 saa_write8(plinep,DW_LNS_extended_op);
1973 saa_write8(plinep,1); /* operand length */
1974 saa_write8(plinep,DW_LNE_end_sequence);
1975 totlen += plinep->datalen;
1976 /* range table relocation entry */
1977 saa_write64(parangesrel, paranges->datalen + 4);
1978 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1979 saa_write64(parangesrel, (uint64_t) 0);
1980 /* range table entry */
1981 saa_write64(paranges,0x0000); /* range start */
1982 saa_write64(paranges,sects[psect->section]->len); /* range length */
1983 highaddr += sects[psect->section]->len;
1984 /* done with this entry */
1985 psect = psect->next;
1987 saa_write64(paranges,0); /* null address */
1988 saa_write64(paranges,0); /* null length */
1989 saalen = paranges->datalen;
1990 arangeslen = saalen + 4;
1991 arangesbuf = pbuf = nasm_malloc(arangeslen);
1992 WRITELONG(pbuf,saalen); /* initial length */
1993 saa_rnbytes(paranges, pbuf, saalen);
1994 saa_free(paranges);
1996 /* build rela.aranges section */
1997 arangesrellen = saalen = parangesrel->datalen;
1998 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1999 saa_rnbytes(parangesrel, pbuf, saalen);
2000 saa_free(parangesrel);
2002 /* build pubnames section */
2003 ppubnames = saa_init(1L);
2004 saa_write16(ppubnames,3); /* dwarf version */
2005 saa_write32(ppubnames,0); /* offset into info */
2006 saa_write32(ppubnames,0); /* space used in info */
2007 saa_write32(ppubnames,0); /* end of list */
2008 saalen = ppubnames->datalen;
2009 pubnameslen = saalen + 4;
2010 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2011 WRITELONG(pbuf,saalen); /* initial length */
2012 saa_rnbytes(ppubnames, pbuf, saalen);
2013 saa_free(ppubnames);
2015 /* build info section */
2016 pinfo = saa_init(1L);
2017 pinforel = saa_init(1L);
2018 saa_write16(pinfo,3); /* dwarf version */
2019 saa_write64(pinforel, pinfo->datalen + 4);
2020 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2021 saa_write64(pinforel, 0);
2022 saa_write32(pinfo,0); /* offset into abbrev */
2023 saa_write8(pinfo,8); /* pointer size */
2024 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2025 saa_write64(pinforel, pinfo->datalen + 4);
2026 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2027 saa_write64(pinforel, 0);
2028 saa_write64(pinfo,0); /* DW_AT_low_pc */
2029 saa_write64(pinforel, pinfo->datalen + 4);
2030 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2031 saa_write64(pinforel, 0);
2032 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2033 saa_write64(pinforel, pinfo->datalen + 4);
2034 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2035 saa_write64(pinforel, 0);
2036 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2037 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2038 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2039 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2040 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2041 saa_write64(pinforel, pinfo->datalen + 4);
2042 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2043 saa_write64(pinforel, 0);
2044 saa_write64(pinfo,0); /* DW_AT_low_pc */
2045 saa_write64(pinfo,0); /* DW_AT_frame_base */
2046 saa_write8(pinfo,0); /* end of entries */
2047 saalen = pinfo->datalen;
2048 infolen = saalen + 4;
2049 infobuf = pbuf = nasm_malloc(infolen);
2050 WRITELONG(pbuf,saalen); /* initial length */
2051 saa_rnbytes(pinfo, pbuf, saalen);
2052 saa_free(pinfo);
2054 /* build rela.info section */
2055 inforellen = saalen = pinforel->datalen;
2056 inforelbuf = pbuf = nasm_malloc(inforellen);
2057 saa_rnbytes(pinforel, pbuf, saalen);
2058 saa_free(pinforel);
2060 /* build abbrev section */
2061 pabbrev = saa_init(1L);
2062 saa_write8(pabbrev,1); /* entry number LEB128u */
2063 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2064 saa_write8(pabbrev,1); /* has children */
2065 /* the following attributes and forms are all LEB128u values */
2066 saa_write8(pabbrev,DW_AT_low_pc);
2067 saa_write8(pabbrev,DW_FORM_addr);
2068 saa_write8(pabbrev,DW_AT_high_pc);
2069 saa_write8(pabbrev,DW_FORM_addr);
2070 saa_write8(pabbrev,DW_AT_stmt_list);
2071 saa_write8(pabbrev,DW_FORM_data4);
2072 saa_write8(pabbrev,DW_AT_name);
2073 saa_write8(pabbrev,DW_FORM_string);
2074 saa_write8(pabbrev,DW_AT_producer);
2075 saa_write8(pabbrev,DW_FORM_string);
2076 saa_write8(pabbrev,DW_AT_language);
2077 saa_write8(pabbrev,DW_FORM_data2);
2078 saa_write16(pabbrev,0); /* end of entry */
2079 /* LEB128u usage same as above */
2080 saa_write8(pabbrev,2); /* entry number */
2081 saa_write8(pabbrev,DW_TAG_subprogram);
2082 saa_write8(pabbrev,0); /* no children */
2083 saa_write8(pabbrev,DW_AT_low_pc);
2084 saa_write8(pabbrev,DW_FORM_addr);
2085 saa_write8(pabbrev,DW_AT_frame_base);
2086 saa_write8(pabbrev,DW_FORM_data4);
2087 saa_write16(pabbrev,0); /* end of entry */
2088 abbrevlen = saalen = pabbrev->datalen;
2089 abbrevbuf = pbuf = nasm_malloc(saalen);
2090 saa_rnbytes(pabbrev, pbuf, saalen);
2091 saa_free(pabbrev);
2093 /* build line section */
2094 /* prolog */
2095 plines = saa_init(1L);
2096 saa_write8(plines,1); /* Minimum Instruction Length */
2097 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2098 saa_write8(plines,line_base); /* Line Base */
2099 saa_write8(plines,line_range); /* Line Range */
2100 saa_write8(plines,opcode_base); /* Opcode Base */
2101 /* standard opcode lengths (# of LEB128u operands) */
2102 saa_write8(plines,0); /* Std opcode 1 length */
2103 saa_write8(plines,1); /* Std opcode 2 length */
2104 saa_write8(plines,1); /* Std opcode 3 length */
2105 saa_write8(plines,1); /* Std opcode 4 length */
2106 saa_write8(plines,1); /* Std opcode 5 length */
2107 saa_write8(plines,0); /* Std opcode 6 length */
2108 saa_write8(plines,0); /* Std opcode 7 length */
2109 saa_write8(plines,0); /* Std opcode 8 length */
2110 saa_write8(plines,1); /* Std opcode 9 length */
2111 saa_write8(plines,0); /* Std opcode 10 length */
2112 saa_write8(plines,0); /* Std opcode 11 length */
2113 saa_write8(plines,1); /* Std opcode 12 length */
2114 /* Directory Table */
2115 saa_write8(plines,0); /* End of table */
2116 /* File Name Table */
2117 ftentry = dwarf_flist;
2118 for (indx = 0;indx<dwarf_numfiles;indx++)
2120 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2121 saa_write8(plines,0); /* directory LEB128u */
2122 saa_write8(plines,0); /* time LEB128u */
2123 saa_write8(plines,0); /* size LEB128u */
2124 ftentry = ftentry->next;
2126 saa_write8(plines,0); /* End of table */
2127 linepoff = plines->datalen;
2128 linelen = linepoff + totlen + 10;
2129 linebuf = pbuf = nasm_malloc(linelen);
2130 WRITELONG(pbuf,linelen-4); /* initial length */
2131 WRITESHORT(pbuf,3); /* dwarf version */
2132 WRITELONG(pbuf,linepoff); /* offset to line number program */
2133 /* write line header */
2134 saalen = linepoff;
2135 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2136 pbuf += linepoff;
2137 saa_free(plines);
2138 /* concatonate line program ranges */
2139 linepoff += 13;
2140 plinesrel = saa_init(1L);
2141 psect = dwarf_fsect;
2142 for (indx = 0; indx < dwarf_nsections; indx++)
2144 saa_write64(plinesrel, linepoff);
2145 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2146 saa_write64(plinesrel, (uint64_t) 0);
2147 plinep = psect->psaa;
2148 saalen = plinep->datalen;
2149 saa_rnbytes(plinep, pbuf, saalen);
2150 pbuf += saalen;
2151 linepoff += saalen;
2152 saa_free(plinep);
2153 /* done with this entry */
2154 psect = psect->next;
2158 /* build rela.lines section */
2159 linerellen =saalen = plinesrel->datalen;
2160 linerelbuf = pbuf = nasm_malloc(linerellen);
2161 saa_rnbytes(plinesrel, pbuf, saalen);
2162 saa_free(plinesrel);
2164 /* build frame section */
2165 framelen = 4;
2166 framebuf = pbuf = nasm_malloc(framelen);
2167 WRITELONG(pbuf,framelen-4); /* initial length */
2169 /* build loc section */
2170 loclen = 16;
2171 locbuf = pbuf = nasm_malloc(loclen);
2172 WRITEDLONG(pbuf,0); /* null beginning offset */
2173 WRITEDLONG(pbuf,0); /* null ending offset */
2176 void dwarf64_cleanup(void)
2178 if (arangesbuf)
2179 nasm_free(arangesbuf);
2180 if (arangesrelbuf)
2181 nasm_free(arangesrelbuf);
2182 if (pubnamesbuf)
2183 nasm_free(pubnamesbuf);
2184 if (infobuf)
2185 nasm_free(infobuf);
2186 if (inforelbuf)
2187 nasm_free(inforelbuf);
2188 if (abbrevbuf)
2189 nasm_free(abbrevbuf);
2190 if (linebuf)
2191 nasm_free(linebuf);
2192 if (linerelbuf)
2193 nasm_free(linerelbuf);
2194 if (framebuf)
2195 nasm_free(framebuf);
2196 if (locbuf)
2197 nasm_free(locbuf);
2199 void dwarf64_findfile(const char * fname)
2201 int finx;
2202 struct linelist *match;
2204 /* return if fname is current file name */
2205 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2206 /* search for match */
2207 else
2209 match = 0;
2210 if (dwarf_flist)
2212 match = dwarf_flist;
2213 for (finx = 0; finx < dwarf_numfiles; finx++)
2215 if (!(strcmp(fname, match->filename)))
2217 dwarf_clist = match;
2218 return;
2222 /* add file name to end of list */
2223 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2224 dwarf_numfiles++;
2225 dwarf_clist->line = dwarf_numfiles;
2226 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2227 strcpy(dwarf_clist->filename,fname);
2228 dwarf_clist->next = 0;
2229 /* if first entry */
2230 if (!dwarf_flist)
2232 dwarf_flist = dwarf_elist = dwarf_clist;
2233 dwarf_clist->last = 0;
2235 /* chain to previous entry */
2236 else
2238 dwarf_elist->next = dwarf_clist;
2239 dwarf_elist = dwarf_clist;
2243 /* */
2244 void dwarf64_findsect(const int index)
2246 int sinx;
2247 struct sectlist *match;
2248 struct SAA *plinep;
2249 /* return if index is current section index */
2250 if (dwarf_csect && (dwarf_csect->section == index))
2252 return;
2254 /* search for match */
2255 else
2257 match = 0;
2258 if (dwarf_fsect)
2260 match = dwarf_fsect;
2261 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2263 if ((match->section == index))
2265 dwarf_csect = match;
2266 return;
2268 match = match->next;
2271 /* add entry to end of list */
2272 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2273 dwarf_nsections++;
2274 dwarf_csect->psaa = plinep = saa_init(1L);
2275 dwarf_csect->line = 1;
2276 dwarf_csect->offset = 0;
2277 dwarf_csect->file = 1;
2278 dwarf_csect->section = index;
2279 dwarf_csect->next = 0;
2280 /* set relocatable address at start of line program */
2281 saa_write8(plinep,DW_LNS_extended_op);
2282 saa_write8(plinep,9); /* operand length */
2283 saa_write8(plinep,DW_LNE_set_address);
2284 saa_write64(plinep,0); /* Start Address */
2285 /* if first entry */
2286 if (!dwarf_fsect)
2288 dwarf_fsect = dwarf_esect = dwarf_csect;
2289 dwarf_csect->last = 0;
2291 /* chain to previous entry */
2292 else
2294 dwarf_esect->next = dwarf_csect;
2295 dwarf_esect = dwarf_csect;
2300 #endif /* OF_ELF */