NASM 2.04rc5
[nasm.git] / output / outelf64.c
blobf483393cd589c54cb88b0ed53b5c61e7a2a76b2c
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"
24 /* Definitions in lieu of elf.h */
25 #define SHT_NULL 0 /* Inactive section header */
26 #define SHT_PROGBITS 1 /* Program defined content */
27 #define SHT_RELA 4 /* Relocation entries with addends */
28 #define SHT_NOBITS 8 /* Section requires no space in file */
29 #define SHF_WRITE (1 << 0) /* Writable */
30 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
31 #define SHF_EXECINSTR (1 << 2) /* Executable */
32 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
33 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
34 #define R_X86_64_NONE 0 /* No reloc */
35 #define R_X86_64_64 1 /* Direct 64 bit address */
36 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
37 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
38 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
39 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
40 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
41 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
42 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
43 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
44 #define ET_REL 1 /* Relocatable file */
45 #define EM_X86_64 62 /* AMD x86-64 architecture */
46 #define STT_NOTYPE 0 /* Symbol type is unspecified */
47 #define STT_OBJECT 1 /* Symbol is a data object */
48 #define STT_FUNC 2 /* Symbol is a code object */
49 #define STT_SECTION 3 /* Symbol associated with a section */
50 #define STT_FILE 4 /* Symbol's name is file name */
51 #define STT_COMMON 5 /* Symbol is a common data object */
52 #define STT_TLS 6 /* Symbol is thread-local data object*/
53 #define STT_NUM 7 /* Number of defined types. */
55 /* Definitions in lieu of dwarf.h */
56 #define DW_TAG_compile_unit 0x11
57 #define DW_TAG_subprogram 0x2e
58 #define DW_AT_name 0x03
59 #define DW_AT_stmt_list 0x10
60 #define DW_AT_low_pc 0x11
61 #define DW_AT_high_pc 0x12
62 #define DW_AT_language 0x13
63 #define DW_AT_producer 0x25
64 #define DW_AT_frame_base 0x40
65 #define DW_FORM_addr 0x01
66 #define DW_FORM_data2 0x05
67 #define DW_FORM_data4 0x06
68 #define DW_FORM_string 0x08
69 #define DW_LNS_extended_op 0
70 #define DW_LNS_advance_pc 2
71 #define DW_LNS_advance_line 3
72 #define DW_LNS_set_file 4
73 #define DW_LNE_end_sequence 1
74 #define DW_LNE_set_address 2
75 #define DW_LNE_define_file 3
76 #define DW_LANG_Mips_Assembler 0x8001
78 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
80 typedef uint32_t Elf64_Word;
81 typedef uint64_t Elf64_Xword;
82 typedef uint64_t Elf64_Addr;
83 typedef uint64_t Elf64_Off;
84 typedef struct
86 Elf64_Word sh_name; /* Section name (string tbl index) */
87 Elf64_Word sh_type; /* Section type */
88 Elf64_Xword sh_flags; /* Section flags */
89 Elf64_Addr sh_addr; /* Section virtual addr at execution */
90 Elf64_Off sh_offset; /* Section file offset */
91 Elf64_Xword sh_size; /* Section size in bytes */
92 Elf64_Word sh_link; /* Link to another section */
93 Elf64_Word sh_info; /* Additional section information */
94 Elf64_Xword sh_addralign; /* Section alignment */
95 Elf64_Xword sh_entsize; /* Entry size if section holds table */
96 } Elf64_Shdr;
99 #ifdef OF_ELF64
102 struct Reloc {
103 struct Reloc *next;
104 int64_t address; /* relative to _start_ of section */
105 int64_t symbol; /* symbol index */
106 int type; /* type of relocation */
109 struct Symbol {
110 int32_t strpos; /* string table position of name */
111 int32_t section; /* section ID of the symbol */
112 int type; /* symbol type */
113 int other; /* symbol visibility */
114 int64_t value; /* address, or COMMON variable align */
115 int32_t size; /* size of symbol */
116 int32_t globnum; /* symbol table offset if global */
117 struct Symbol *next; /* list of globals in each section */
118 struct Symbol *nextfwd; /* list of unresolved-size symbols */
119 char *name; /* used temporarily if in above list */
123 struct Section {
124 struct SAA *data;
125 uint64_t len, size;
126 uint32_t nrelocs;
127 int32_t index; /* index into sects array */
128 uint32_t type; /* SHT_PROGBITS or SHT_NOBITS */
129 uint64_t align; /* alignment: power of two */
130 uint64_t flags; /* section flags */
131 char *name;
132 struct SAA *rel;
133 uint64_t rellen;
134 struct Reloc *head, **tail;
135 struct Symbol *gsyms; /* global symbols in section */
138 #define SECT_DELTA 32
139 static struct Section **sects;
140 static int nsects, sectlen;
142 #define SHSTR_DELTA 256
143 static char *shstrtab;
144 static int shstrtablen, shstrtabsize;
146 static struct SAA *syms;
147 static uint32_t nlocals, nglobs;
149 static int32_t def_seg;
151 static struct RAA *bsym;
153 static struct SAA *strs;
154 static uint32_t strslen;
156 static FILE *elffp;
157 static efunc error;
158 static evalfunc evaluate;
160 static struct Symbol *fwds;
162 static char elf_module[FILENAME_MAX];
164 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
165 static uint8_t elf_abiver = 0; /* Current ABI version */
167 extern struct ofmt of_elf64;
169 #define SHN_UNDEF 0
171 #define SYM_GLOBAL 0x10
173 #define STV_DEFAULT 0
174 #define STV_INTERNAL 1
175 #define STV_HIDDEN 2
176 #define STV_PROTECTED 3
178 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
180 #define SEG_ALIGN 16 /* alignment of sections in file */
181 #define SEG_ALIGN_1 (SEG_ALIGN-1)
183 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
185 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
187 static struct ELF_SECTDATA {
188 void *data;
189 int64_t len;
190 bool is_saa;
191 } *elf_sects;
192 static int elf_nsect, nsections;
193 static int64_t elf_foffs;
195 static void elf_write(void);
196 static void elf_sect_write(struct Section *, const uint8_t *,
197 uint64_t);
198 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
199 int, int);
200 static void elf_write_sections(void);
201 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
202 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
203 static void add_sectname(char *, char *);
205 /* type values for stabs debugging sections */
206 #define N_SO 0x64 /* ID for main source file */
207 #define N_SOL 0x84 /* ID for sub-source file */
208 #define N_BINCL 0x82 /* not currently used */
209 #define N_EINCL 0xA2 /* not currently used */
210 #define N_SLINE 0x44
212 struct stabentry {
213 uint32_t n_strx;
214 uint8_t n_type;
215 uint8_t n_other;
216 uint16_t n_desc;
217 uint32_t n_value;
220 struct erel {
221 int offset, info;
224 struct symlininfo {
225 int offset;
226 int section; /* index into sects[] */
227 int segto; /* internal section number */
228 char *name; /* shallow-copied pointer of section name */
231 struct linelist {
232 struct symlininfo info;
233 int line;
234 char *filename;
235 struct linelist *next;
236 struct linelist *last;
239 struct sectlist {
240 struct SAA *psaa;
241 int section;
242 int line;
243 int offset;
244 int file;
245 struct sectlist *next;
246 struct sectlist *last;
249 /* common debug variables */
250 static int currentline = 1;
251 static int debug_immcall = 0;
253 /* stabs debug variables */
254 static struct linelist *stabslines = 0;
255 static int numlinestabs = 0;
256 static char *stabs_filename = 0;
257 static int symtabsection;
258 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
259 static int stablen, stabstrlen, stabrellen;
261 /* dwarf debug variables */
262 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
263 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
264 static int dwarf_numfiles = 0, dwarf_nsections;
265 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
266 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
267 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
268 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
269 abbrevlen, linelen, linerellen, framelen, loclen;
270 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
273 static struct dfmt df_dwarf;
274 static struct dfmt df_stabs;
275 static struct Symbol *lastsym;
277 /* common debugging routines */
278 void debug64_typevalue(int32_t);
279 void debug64_init(struct ofmt *, void *, FILE *, efunc);
280 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
281 void debug64_directive(const char *, const char *);
283 /* stabs debugging routines */
284 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
285 void stabs64_output(int, void *);
286 void stabs64_generate(void);
287 void stabs64_cleanup(void);
289 /* dwarf debugging routines */
290 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
291 void dwarf64_output(int, void *);
292 void dwarf64_generate(void);
293 void dwarf64_cleanup(void);
294 void dwarf64_findfile(const char *);
295 void dwarf64_findsect(const int);
298 * Special section numbers which are used to define ELF special
299 * symbols, which can be used with WRT to provide PIC relocation
300 * types.
302 static int32_t elf_gotpc_sect, elf_gotoff_sect;
303 static int32_t elf_got_sect, elf_plt_sect;
304 static int32_t elf_sym_sect;
306 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
308 maxbits = 64;
309 elffp = fp;
310 error = errfunc;
311 evaluate = eval;
312 (void)ldef; /* placate optimisers */
313 sects = NULL;
314 nsects = sectlen = 0;
315 syms = saa_init((int32_t)sizeof(struct Symbol));
316 nlocals = nglobs = 0;
317 bsym = raa_init();
318 strs = saa_init(1L);
319 saa_wbytes(strs, "\0", 1L);
320 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
321 strslen = 2 + strlen(elf_module);
322 shstrtab = NULL;
323 shstrtablen = shstrtabsize = 0;;
324 add_sectname("", "");
326 fwds = NULL;
328 elf_gotpc_sect = seg_alloc();
329 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
330 error);
331 elf_gotoff_sect = seg_alloc();
332 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
333 error);
334 elf_got_sect = seg_alloc();
335 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
336 error);
337 elf_plt_sect = seg_alloc();
338 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
339 error);
340 elf_sym_sect = seg_alloc();
341 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
342 error);
344 def_seg = seg_alloc();
348 static void elf_cleanup(int debuginfo)
350 struct Reloc *r;
351 int i;
353 (void)debuginfo;
355 elf_write();
356 fclose(elffp);
357 for (i = 0; i < nsects; i++) {
358 if (sects[i]->type != SHT_NOBITS)
359 saa_free(sects[i]->data);
360 if (sects[i]->head)
361 saa_free(sects[i]->rel);
362 while (sects[i]->head) {
363 r = sects[i]->head;
364 sects[i]->head = sects[i]->head->next;
365 nasm_free(r);
368 nasm_free(sects);
369 saa_free(syms);
370 raa_free(bsym);
371 saa_free(strs);
372 if (of_elf64.current_dfmt) {
373 of_elf64.current_dfmt->cleanup();
376 /* add entry to the elf .shstrtab section */
377 static void add_sectname(char *firsthalf, char *secondhalf)
379 int len = strlen(firsthalf) + strlen(secondhalf);
380 while (shstrtablen + len + 1 > shstrtabsize)
381 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
382 strcpy(shstrtab + shstrtablen, firsthalf);
383 strcat(shstrtab + shstrtablen, secondhalf);
384 shstrtablen += len + 1;
387 static int elf_make_section(char *name, int type, int flags, int align)
389 struct Section *s;
391 s = nasm_malloc(sizeof(*s));
393 if (type != SHT_NOBITS)
394 s->data = saa_init(1L);
395 s->head = NULL;
396 s->tail = &s->head;
397 s->len = s->size = 0;
398 s->nrelocs = 0;
399 if (!strcmp(name, ".text"))
400 s->index = def_seg;
401 else
402 s->index = seg_alloc();
403 add_sectname("", name);
404 s->name = nasm_malloc(1 + strlen(name));
405 strcpy(s->name, name);
406 s->type = type;
407 s->flags = flags;
408 s->align = align;
409 s->gsyms = NULL;
411 if (nsects >= sectlen)
412 sects =
413 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
414 sects[nsects++] = s;
416 return nsects - 1;
419 static int32_t elf_section_names(char *name, int pass, int *bits)
421 char *p;
422 unsigned flags_and, flags_or;
423 uint64_t type, align;
424 int i;
427 * Default is 64 bits.
429 if (!name) {
430 *bits = 64;
431 return def_seg;
434 p = name;
435 while (*p && !nasm_isspace(*p))
436 p++;
437 if (*p)
438 *p++ = '\0';
439 flags_and = flags_or = type = align = 0;
441 while (*p && nasm_isspace(*p))
442 p++;
443 while (*p) {
444 char *q = p;
445 while (*p && !nasm_isspace(*p))
446 p++;
447 if (*p)
448 *p++ = '\0';
449 while (*p && nasm_isspace(*p))
450 p++;
452 if (!nasm_strnicmp(q, "align=", 6)) {
453 align = atoi(q + 6);
454 if (align == 0)
455 align = 1;
456 if ((align - 1) & align) { /* means it's not a power of two */
457 error(ERR_NONFATAL, "section alignment %d is not"
458 " a power of two", align);
459 align = 1;
461 } else if (!nasm_stricmp(q, "alloc")) {
462 flags_and |= SHF_ALLOC;
463 flags_or |= SHF_ALLOC;
464 } else if (!nasm_stricmp(q, "noalloc")) {
465 flags_and |= SHF_ALLOC;
466 flags_or &= ~SHF_ALLOC;
467 } else if (!nasm_stricmp(q, "exec")) {
468 flags_and |= SHF_EXECINSTR;
469 flags_or |= SHF_EXECINSTR;
470 } else if (!nasm_stricmp(q, "noexec")) {
471 flags_and |= SHF_EXECINSTR;
472 flags_or &= ~SHF_EXECINSTR;
473 } else if (!nasm_stricmp(q, "write")) {
474 flags_and |= SHF_WRITE;
475 flags_or |= SHF_WRITE;
476 } else if (!nasm_stricmp(q, "nowrite")) {
477 flags_and |= SHF_WRITE;
478 flags_or &= ~SHF_WRITE;
479 } else if (!nasm_stricmp(q, "progbits")) {
480 type = SHT_PROGBITS;
481 } else if (!nasm_stricmp(q, "nobits")) {
482 type = SHT_NOBITS;
486 if (!strcmp(name, ".comment") ||
487 !strcmp(name, ".shstrtab") ||
488 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
489 error(ERR_NONFATAL, "attempt to redefine reserved section"
490 "name `%s'", name);
491 return NO_SEG;
494 for (i = 0; i < nsects; i++)
495 if (!strcmp(name, sects[i]->name))
496 break;
497 if (i == nsects) {
498 if (!strcmp(name, ".text"))
499 i = elf_make_section(name, SHT_PROGBITS,
500 SHF_ALLOC | SHF_EXECINSTR, 16);
501 else if (!strcmp(name, ".rodata"))
502 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
503 else if (!strcmp(name, ".data"))
504 i = elf_make_section(name, SHT_PROGBITS,
505 SHF_ALLOC | SHF_WRITE, 4);
506 else if (!strcmp(name, ".bss"))
507 i = elf_make_section(name, SHT_NOBITS,
508 SHF_ALLOC | SHF_WRITE, 4);
509 else
510 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
511 if (type)
512 sects[i]->type = type;
513 if (align)
514 sects[i]->align = align;
515 sects[i]->flags &= ~flags_and;
516 sects[i]->flags |= flags_or;
517 } else if (pass == 1) {
518 if ((type && sects[i]->type != type)
519 || (align && sects[i]->align != align)
520 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
521 error(ERR_WARNING, "incompatible section attributes ignored on"
522 " redeclaration of section `%s'", name);
525 return sects[i]->index;
528 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
529 int is_global, char *special)
531 int pos = strslen;
532 struct Symbol *sym;
533 bool special_used = false;
535 #if defined(DEBUG) && DEBUG>2
536 fprintf(stderr,
537 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
538 name, segment, offset, is_global, special);
539 #endif
540 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
542 * This is a NASM special symbol. We never allow it into
543 * the ELF symbol table, even if it's a valid one. If it
544 * _isn't_ a valid one, we should barf immediately.
546 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
547 strcmp(name, "..got") && strcmp(name, "..plt") &&
548 strcmp(name, "..sym"))
549 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
550 return;
553 if (is_global == 3) {
554 struct Symbol **s;
556 * Fix up a forward-reference symbol size from the first
557 * pass.
559 for (s = &fwds; *s; s = &(*s)->nextfwd)
560 if (!strcmp((*s)->name, name)) {
561 struct tokenval tokval;
562 expr *e;
563 char *p = special;
565 while (*p && !nasm_isspace(*p))
566 p++;
567 while (*p && nasm_isspace(*p))
568 p++;
569 stdscan_reset();
570 stdscan_bufptr = p;
571 tokval.t_type = TOKEN_INVALID;
572 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
573 if (e) {
574 if (!is_simple(e))
575 error(ERR_NONFATAL, "cannot use relocatable"
576 " expression as symbol size");
577 else
578 (*s)->size = reloc_value(e);
582 * Remove it from the list of unresolved sizes.
584 nasm_free((*s)->name);
585 *s = (*s)->nextfwd;
586 return;
588 return; /* it wasn't an important one */
591 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
592 strslen += 1 + strlen(name);
594 lastsym = sym = saa_wstruct(syms);
596 sym->strpos = pos;
597 sym->type = is_global ? SYM_GLOBAL : 0;
598 sym->other = STV_DEFAULT;
599 sym->size = 0;
600 if (segment == NO_SEG)
601 sym->section = SHN_ABS;
602 else {
603 int i;
604 sym->section = SHN_UNDEF;
605 if (nsects == 0 && segment == def_seg) {
606 int tempint;
607 if (segment != elf_section_names(".text", 2, &tempint))
608 error(ERR_PANIC,
609 "strange segment conditions in ELF driver");
610 sym->section = nsects;
611 } else {
612 for (i = 0; i < nsects; i++)
613 if (segment == sects[i]->index) {
614 sym->section = i + 1;
615 break;
620 if (is_global == 2) {
621 sym->size = offset;
622 sym->value = 0;
623 sym->section = SHN_COMMON;
625 * We have a common variable. Check the special text to see
626 * if it's a valid number and power of two; if so, store it
627 * as the alignment for the common variable.
629 if (special) {
630 bool err;
631 sym->value = readnum(special, &err);
632 if (err)
633 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
634 " valid number", special);
635 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
636 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
637 " power of two", special);
639 special_used = true;
640 } else
641 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
643 if (sym->type == SYM_GLOBAL) {
645 * If sym->section == SHN_ABS, then the first line of the
646 * else section would cause a core dump, because its a reference
647 * beyond the end of the section array.
648 * This behaviour is exhibited by this code:
649 * GLOBAL crash_nasm
650 * crash_nasm equ 0
651 * To avoid such a crash, such requests are silently discarded.
652 * This may not be the best solution.
654 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
655 bsym = raa_write(bsym, segment, nglobs);
656 } else if (sym->section != SHN_ABS) {
658 * This is a global symbol; so we must add it to the linked
659 * list of global symbols in its section. We'll push it on
660 * the beginning of the list, because it doesn't matter
661 * much which end we put it on and it's easier like this.
663 * In addition, we check the special text for symbol
664 * type and size information.
666 sym->next = sects[sym->section - 1]->gsyms;
667 sects[sym->section - 1]->gsyms = sym;
669 if (special) {
670 int n = strcspn(special, " \t");
672 if (!nasm_strnicmp(special, "function", n))
673 sym->type |= STT_FUNC;
674 else if (!nasm_strnicmp(special, "data", n) ||
675 !nasm_strnicmp(special, "object", n))
676 sym->type |= STT_OBJECT;
677 else if (!nasm_strnicmp(special, "notype", n))
678 sym->type |= STT_NOTYPE;
679 else
680 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
681 n, special);
682 special += n;
684 while (nasm_isspace(*special))
685 ++special;
686 if (*special) {
687 n = strcspn(special, " \t");
688 if (!nasm_strnicmp(special, "default", n))
689 sym->other = STV_DEFAULT;
690 else if (!nasm_strnicmp(special, "internal", n))
691 sym->other = STV_INTERNAL;
692 else if (!nasm_strnicmp(special, "hidden", n))
693 sym->other = STV_HIDDEN;
694 else if (!nasm_strnicmp(special, "protected", n))
695 sym->other = STV_PROTECTED;
696 else
697 n = 0;
698 special += n;
701 if (*special) {
702 struct tokenval tokval;
703 expr *e;
704 int fwd = 0;
705 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
707 while (special[n] && nasm_isspace(special[n]))
708 n++;
710 * We have a size expression; attempt to
711 * evaluate it.
713 stdscan_reset();
714 stdscan_bufptr = special + n;
715 tokval.t_type = TOKEN_INVALID;
716 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
717 NULL);
718 if (fwd) {
719 sym->nextfwd = fwds;
720 fwds = sym;
721 sym->name = nasm_strdup(name);
722 } else if (e) {
723 if (!is_simple(e))
724 error(ERR_NONFATAL, "cannot use relocatable"
725 " expression as symbol size");
726 else
727 sym->size = reloc_value(e);
729 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
731 special_used = true;
734 sym->globnum = nglobs;
735 nglobs++;
736 } else
737 nlocals++;
739 if (special && !special_used)
740 error(ERR_NONFATAL, "no special symbol features supported here");
743 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
745 struct Reloc *r;
746 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
747 sect->tail = &r->next;
748 r->next = NULL;
750 r->address = sect->len;
751 if (segment == NO_SEG)
752 r->symbol = 0;
753 else {
754 int i;
755 r->symbol = 0;
756 for (i = 0; i < nsects; i++)
757 if (segment == sects[i]->index)
758 r->symbol = i + 2;
759 if (!r->symbol)
760 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
762 r->type = type;
764 sect->nrelocs++;
768 * This routine deals with ..got and ..sym relocations: the more
769 * complicated kinds. In shared-library writing, some relocations
770 * with respect to global symbols must refer to the precise symbol
771 * rather than referring to an offset from the base of the section
772 * _containing_ the symbol. Such relocations call to this routine,
773 * which searches the symbol list for the symbol in question.
775 * R_386_GOT32 references require the _exact_ symbol address to be
776 * used; R_386_32 references can be at an offset from the symbol.
777 * The boolean argument `exact' tells us this.
779 * Return value is the adjusted value of `addr', having become an
780 * offset from the symbol rather than the section. Should always be
781 * zero when returning from an exact call.
783 * Limitation: if you define two symbols at the same place,
784 * confusion will occur.
786 * Inefficiency: we search, currently, using a linked list which
787 * isn't even necessarily sorted.
789 static int32_t elf_add_gsym_reloc(struct Section *sect,
790 int32_t segment, int64_t offset,
791 int type, bool exact)
793 struct Reloc *r;
794 struct Section *s;
795 struct Symbol *sym, *sm;
796 int i;
799 * First look up the segment/offset pair and find a global
800 * symbol corresponding to it. If it's not one of our segments,
801 * then it must be an external symbol, in which case we're fine
802 * doing a normal elf_add_reloc after first sanity-checking
803 * that the offset from the symbol is zero.
805 s = NULL;
806 for (i = 0; i < nsects; i++)
807 if (segment == sects[i]->index) {
808 s = sects[i];
809 break;
811 if (!s) {
812 if (exact && offset != 0)
813 error(ERR_NONFATAL, "unable to find a suitable global symbol"
814 " for this reference");
815 else
816 elf_add_reloc(sect, segment, type);
817 return offset;
820 if (exact) {
822 * Find a symbol pointing _exactly_ at this one.
824 for (sym = s->gsyms; sym; sym = sym->next)
825 if (sym->value == offset)
826 break;
827 } else {
829 * Find the nearest symbol below this one.
831 sym = NULL;
832 for (sm = s->gsyms; sm; sm = sm->next)
833 if (sm->value <= offset && (!sym || sm->value > sym->value))
834 sym = sm;
836 if (!sym && exact) {
837 error(ERR_NONFATAL, "unable to find a suitable global symbol"
838 " for this reference");
839 return 0;
842 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
843 sect->tail = &r->next;
844 r->next = NULL;
846 r->address = sect->len;
847 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
848 r->type = type;
850 sect->nrelocs++;
852 return offset - sym->value;
855 static void elf_out(int32_t segto, const void *data,
856 enum out_type type, uint64_t size,
857 int32_t segment, int32_t wrt)
859 struct Section *s;
860 int64_t addr;
861 uint8_t mydata[16], *p;
862 int i;
863 static struct symlininfo sinfo;
865 #if defined(DEBUG) && DEBUG>2
866 if (data) fprintf(stderr,
867 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
868 currentline, type, segment, segto, size, *(int64_t *)data);
869 else fprintf(stderr,
870 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
871 currentline, type, segment, segto, size);
872 #endif
875 * handle absolute-assembly (structure definitions)
877 if (segto == NO_SEG) {
878 if (type != OUT_RESERVE)
879 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
880 " space");
881 return;
884 s = NULL;
885 for (i = 0; i < nsects; i++)
886 if (segto == sects[i]->index) {
887 s = sects[i];
888 break;
890 if (!s) {
891 int tempint; /* ignored */
892 if (segto != elf_section_names(".text", 2, &tempint))
893 error(ERR_PANIC, "strange segment conditions in ELF driver");
894 else {
895 s = sects[nsects - 1];
896 i = nsects - 1;
899 /* invoke current debug_output routine */
900 if (of_elf64.current_dfmt) {
901 sinfo.offset = s->len;
902 sinfo.section = i;
903 sinfo.segto = segto;
904 sinfo.name = s->name;
905 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
907 /* end of debugging stuff */
909 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
910 error(ERR_WARNING, "attempt to initialize memory in"
911 " BSS section `%s': ignored", s->name);
912 if (type == OUT_REL2ADR)
913 size = 2;
914 else if (type == OUT_REL4ADR)
915 size = 4;
916 s->len += size;
917 return;
920 if (type == OUT_RESERVE) {
921 if (s->type == SHT_PROGBITS) {
922 error(ERR_WARNING, "uninitialized space declared in"
923 " non-BSS section `%s': zeroing", s->name);
924 elf_sect_write(s, NULL, size);
925 } else
926 s->len += size;
927 } else if (type == OUT_RAWDATA) {
928 if (segment != NO_SEG)
929 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
930 elf_sect_write(s, data, size);
931 } else if (type == OUT_ADDRESS) {
932 bool gnu16 = false;
933 addr = *(int64_t *)data;
934 if (segment != NO_SEG) {
935 if (segment % 2) {
936 error(ERR_NONFATAL, "ELF format does not support"
937 " segment base references");
938 } else {
939 if (wrt == NO_SEG) {
940 switch ((int)size) {
941 case 2:
942 elf_add_reloc(s, segment, R_X86_64_16);
943 break;
944 case 4:
945 elf_add_reloc(s, segment, R_X86_64_32);
946 break;
947 case 8:
948 elf_add_reloc(s, segment, R_X86_64_64);
949 break;
950 default:
951 error(ERR_PANIC, "internal error elf64-hpa-871");
952 break;
954 } else if (wrt == elf_gotpc_sect + 1) {
956 * The user will supply GOT relative to $$. ELF
957 * will let us have GOT relative to $. So we
958 * need to fix up the data item by $-$$.
960 addr += s->len;
961 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
962 } else if (wrt == elf_gotoff_sect + 1) {
963 elf_add_reloc(s, segment, R_X86_64_GOTTPOFF);
964 } else if (wrt == elf_got_sect + 1) {
965 addr = elf_add_gsym_reloc(s, segment, addr,
966 R_X86_64_GOT32, true);
967 } else if (wrt == elf_sym_sect + 1) {
968 switch ((int)size) {
969 case 2:
970 gnu16 = true;
971 addr = elf_add_gsym_reloc(s, segment, addr,
972 R_X86_64_16, false);
973 break;
974 case 4:
975 addr = elf_add_gsym_reloc(s, segment, addr,
976 R_X86_64_32, false);
977 break;
978 case 8:
979 addr = elf_add_gsym_reloc(s, segment, addr,
980 R_X86_64_64, false);
981 break;
982 default:
983 error(ERR_PANIC, "internal error elf64-hpa-903");
984 break;
986 } else if (wrt == elf_plt_sect + 1) {
987 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
988 "relative PLT references");
989 } else {
990 error(ERR_NONFATAL, "ELF format does not support this"
991 " use of WRT");
992 wrt = NO_SEG; /* we can at least _try_ to continue */
996 p = mydata;
997 if (gnu16) {
998 WRITESHORT(p, addr);
999 } else {
1000 if (size != 8 && size != 4 && segment != NO_SEG) {
1001 error(ERR_NONFATAL,
1002 "Unsupported non-64-bit ELF relocation");
1004 if (size == 4) WRITELONG(p, addr);
1005 else WRITEDLONG(p, (int64_t)addr);
1007 elf_sect_write(s, mydata, size);
1008 } else if (type == OUT_REL2ADR) {
1009 if (segment == segto)
1010 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
1011 if (segment != NO_SEG && segment % 2) {
1012 error(ERR_NONFATAL, "ELF format does not support"
1013 " segment base references");
1014 } else {
1015 if (wrt == NO_SEG) {
1016 elf_add_reloc(s, segment, R_X86_64_PC16);
1017 } else {
1018 error(ERR_NONFATAL,
1019 "Unsupported non-32-bit ELF relocation [2]");
1022 p = mydata;
1023 WRITESHORT(p, *(int64_t *)data - size);
1024 elf_sect_write(s, mydata, 2L);
1025 } else if (type == OUT_REL4ADR) {
1026 if (segment == segto)
1027 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1028 if (segment != NO_SEG && segment % 2) {
1029 error(ERR_NONFATAL, "ELF format does not support"
1030 " segment base references");
1031 } else {
1032 if (wrt == NO_SEG) {
1033 elf_add_reloc(s, segment, R_X86_64_PC32);
1034 } else if (wrt == elf_plt_sect + 1) {
1035 elf_add_reloc(s, segment, R_X86_64_PLT32);
1036 } else if (wrt == elf_gotpc_sect + 1 ||
1037 wrt == elf_gotoff_sect + 1 ||
1038 wrt == elf_got_sect + 1) {
1039 error(ERR_NONFATAL, "ELF format cannot produce PC-"
1040 "relative GOT references");
1041 } else {
1042 error(ERR_NONFATAL, "ELF format does not support this"
1043 " use of WRT");
1044 wrt = NO_SEG; /* we can at least _try_ to continue */
1047 p = mydata;
1048 WRITELONG(p, *(int64_t *)data - size);
1049 elf_sect_write(s, mydata, 4L);
1053 static void elf_write(void)
1055 int align;
1056 int scount;
1057 char *p;
1058 int commlen;
1059 char comment[64];
1060 int i;
1062 struct SAA *symtab;
1063 int32_t symtablen, symtablocal;
1066 * Work out how many sections we will have. We have SHN_UNDEF,
1067 * then the flexible user sections, then the four fixed
1068 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1069 * then optionally relocation sections for the user sections.
1071 if (of_elf64.current_dfmt == &df_stabs)
1072 nsections = 8;
1073 else if (of_elf64.current_dfmt == &df_dwarf)
1074 nsections = 15;
1075 else
1076 nsections = 5; /* SHN_UNDEF and the fixed ones */
1078 add_sectname("", ".comment");
1079 add_sectname("", ".shstrtab");
1080 add_sectname("", ".symtab");
1081 add_sectname("", ".strtab");
1082 for (i = 0; i < nsects; i++) {
1083 nsections++; /* for the section itself */
1084 if (sects[i]->head) {
1085 nsections++; /* for its relocations */
1086 add_sectname(".rela", sects[i]->name);
1090 if (of_elf64.current_dfmt == &df_stabs) {
1091 /* in case the debug information is wanted, just add these three sections... */
1092 add_sectname("", ".stab");
1093 add_sectname("", ".stabstr");
1094 add_sectname(".rel", ".stab");
1097 else if (of_elf64.current_dfmt == &df_dwarf) {
1098 /* the dwarf debug standard specifies the following ten sections,
1099 not all of which are currently implemented,
1100 although all of them are defined. */
1101 #define debug_aranges (int64_t) (nsections-10)
1102 #define debug_info (int64_t) (nsections-7)
1103 #define debug_abbrev (int64_t) (nsections-5)
1104 #define debug_line (int64_t) (nsections-4)
1105 add_sectname("", ".debug_aranges");
1106 add_sectname(".rela", ".debug_aranges");
1107 add_sectname("", ".debug_pubnames");
1108 add_sectname("", ".debug_info");
1109 add_sectname(".rela", ".debug_info");
1110 add_sectname("", ".debug_abbrev");
1111 add_sectname("", ".debug_line");
1112 add_sectname(".rela", ".debug_line");
1113 add_sectname("", ".debug_frame");
1114 add_sectname("", ".debug_loc");
1118 * Do the comment.
1120 *comment = '\0';
1121 commlen =
1122 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1125 * Output the ELF header.
1127 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1128 fputc(elf_osabi, elffp);
1129 fputc(elf_abiver, elffp);
1130 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1131 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1132 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1133 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1134 fwriteint64_t(0L, elffp); /* no entry point */
1135 fwriteint64_t(0L, elffp); /* no program header table */
1136 fwriteint64_t(0x40L, elffp); /* section headers straight after
1137 * ELF header plus alignment */
1138 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1139 fwriteint16_t(0x40, elffp); /* size of ELF header */
1140 fwriteint16_t(0, elffp); /* no program header table, again */
1141 fwriteint16_t(0, elffp); /* still no program header table */
1142 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1143 fwriteint16_t(nsections, elffp); /* number of sections */
1144 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1145 * section header table */
1148 * Build the symbol table and relocation tables.
1150 symtab = elf_build_symtab(&symtablen, &symtablocal);
1151 for (i = 0; i < nsects; i++)
1152 if (sects[i]->head)
1153 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1154 sects[i]->head);
1157 * Now output the section header table.
1160 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1161 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1162 elf_foffs += align;
1163 elf_nsect = 0;
1164 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1165 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1166 scount = 1; /* needed for the stabs debugging to track the symtable section */
1167 p = shstrtab + 1;
1168 for (i = 0; i < nsects; i++) {
1169 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1170 (sects[i]->type == SHT_PROGBITS ?
1171 sects[i]->data : NULL), true,
1172 sects[i]->len, 0, 0, sects[i]->align, 0);
1173 p += strlen(p) + 1;
1174 scount++; /* ditto */
1176 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1177 scount++; /* ditto */
1178 p += strlen(p) + 1;
1179 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1180 scount++; /* ditto */
1181 p += strlen(p) + 1;
1182 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1183 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1184 p += strlen(p) + 1;
1185 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1186 for (i = 0; i < nsects; i++)
1187 if (sects[i]->head) {
1188 p += strlen(p) + 1;
1189 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1190 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1192 if (of_elf64.current_dfmt == &df_stabs) {
1193 /* for debugging information, create the last three sections
1194 which are the .stab , .stabstr and .rel.stab sections respectively */
1196 /* this function call creates the stab sections in memory */
1197 stabs64_generate();
1199 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1200 p += strlen(p) + 1;
1201 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1202 nsections - 2, 0, 4, 12);
1204 p += strlen(p) + 1;
1205 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1206 stabstrlen, 0, 0, 4, 0);
1208 p += strlen(p) + 1;
1209 /* link -> symtable info -> section to refer to */
1210 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1211 stabrellen, symtabsection, nsections - 3, 4,
1212 16);
1215 else if (of_elf64.current_dfmt == &df_dwarf) {
1216 /* for dwarf debugging information, create the ten dwarf sections */
1218 /* this function call creates the dwarf sections in memory */
1219 if (dwarf_fsect) dwarf64_generate();
1221 p += strlen(p) + 1;
1222 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1223 arangeslen, 0, 0, 1, 0);
1224 p += strlen(p) + 1;
1225 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1226 arangesrellen, symtabsection, debug_aranges, 1, 24);
1227 p += strlen(p) + 1;
1228 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1229 pubnameslen, 0, 0, 1, 0);
1230 p += strlen(p) + 1;
1231 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1232 infolen, 0, 0, 1, 0);
1233 p += strlen(p) + 1;
1234 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1235 inforellen, symtabsection, debug_info, 1, 24);
1236 p += strlen(p) + 1;
1237 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1238 abbrevlen, 0, 0, 1, 0);
1239 p += strlen(p) + 1;
1240 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1241 linelen, 0, 0, 1, 0);
1242 p += strlen(p) + 1;
1243 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1244 linerellen, symtabsection, debug_line, 1, 24);
1245 p += strlen(p) + 1;
1246 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1247 framelen, 0, 0, 8, 0);
1248 p += strlen(p) + 1;
1249 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1250 loclen, 0, 0, 1, 0);
1253 fwrite(align_str, align, 1, elffp);
1256 * Now output the sections.
1258 elf_write_sections();
1260 nasm_free(elf_sects);
1261 saa_free(symtab);
1264 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1266 struct SAA *s = saa_init(1L);
1267 struct Symbol *sym;
1268 uint8_t entry[24], *p;
1269 int i;
1271 *len = *local = 0;
1274 * First, an all-zeros entry, required by the ELF spec.
1276 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1277 *len += 24;
1278 (*local)++;
1281 * Next, an entry for the file name.
1283 p = entry;
1284 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1285 WRITESHORT(p, STT_FILE); /* type FILE */
1286 WRITESHORT(p, SHN_ABS);
1287 WRITEDLONG(p, (uint64_t) 0); /* no value */
1288 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1289 saa_wbytes(s, entry, 24L);
1290 *len += 24;
1291 (*local)++;
1294 * Now some standard symbols defining the segments, for relocation
1295 * purposes.
1297 for (i = 1; i <= nsects; i++) {
1298 p = entry;
1299 WRITELONG(p, 0); /* no symbol name */
1300 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1301 WRITESHORT(p, i); /* section id */
1302 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1303 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1304 saa_wbytes(s, entry, 24L);
1305 *len += 24;
1306 (*local)++;
1311 * Now the other local symbols.
1313 saa_rewind(syms);
1314 while ((sym = saa_rstruct(syms))) {
1315 if (sym->type & SYM_GLOBAL)
1316 continue;
1317 p = entry;
1318 WRITELONG(p, sym->strpos); /* index into symbol string table */
1319 WRITECHAR(p, sym->type); /* type and binding */
1320 WRITECHAR(p, sym->other); /* visibility */
1321 WRITESHORT(p, sym->section); /* index into section header table */
1322 WRITEDLONG(p, (int64_t)sym->value); /* value of symbol */
1323 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1324 saa_wbytes(s, entry, 24L);
1325 *len += 24;
1326 (*local)++;
1329 * dwarf needs symbols for debug sections
1330 * which are relocation targets.
1332 if (of_elf64.current_dfmt == &df_dwarf) {
1333 dwarf_infosym = *local;
1334 p = entry;
1335 WRITELONG(p, 0); /* no symbol name */
1336 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1337 WRITESHORT(p, debug_info); /* section id */
1338 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1339 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1340 saa_wbytes(s, entry, 24L);
1341 *len += 24;
1342 (*local)++;
1343 dwarf_abbrevsym = *local;
1344 p = entry;
1345 WRITELONG(p, 0); /* no symbol name */
1346 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1347 WRITESHORT(p, debug_abbrev); /* section id */
1348 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1349 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1350 saa_wbytes(s, entry, 24L);
1351 *len += 24;
1352 (*local)++;
1353 dwarf_linesym = *local;
1354 p = entry;
1355 WRITELONG(p, 0); /* no symbol name */
1356 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1357 WRITESHORT(p, debug_line); /* section id */
1358 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1359 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1360 saa_wbytes(s, entry, 24L);
1361 *len += 24;
1362 (*local)++;
1366 * Now the global symbols.
1368 saa_rewind(syms);
1369 while ((sym = saa_rstruct(syms))) {
1370 if (!(sym->type & SYM_GLOBAL))
1371 continue;
1372 p = entry;
1373 WRITELONG(p, sym->strpos);
1374 WRITECHAR(p, sym->type); /* type and binding */
1375 WRITECHAR(p, sym->other); /* visibility */
1376 WRITESHORT(p, sym->section);
1377 WRITEDLONG(p, (int64_t)sym->value);
1378 WRITEDLONG(p, (int64_t)sym->size);
1379 saa_wbytes(s, entry, 24L);
1380 *len += 24;
1383 return s;
1386 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1388 struct SAA *s;
1389 uint8_t *p, entry[24];
1391 if (!r)
1392 return NULL;
1394 s = saa_init(1L);
1395 *len = 0;
1397 while (r) {
1398 int64_t sym = r->symbol;
1400 if (sym >= GLOBAL_TEMP_BASE)
1402 if (of_elf64.current_dfmt == &df_dwarf)
1403 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1404 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1406 p = entry;
1407 WRITEDLONG(p, r->address);
1408 WRITEDLONG(p, (sym << 32) + r->type);
1409 WRITEDLONG(p, (uint64_t) 0);
1410 saa_wbytes(s, entry, 24L);
1411 *len += 24;
1413 r = r->next;
1416 return s;
1419 static void elf_section_header(int name, int type, uint64_t flags,
1420 void *data, bool is_saa, uint64_t datalen,
1421 int link, int info, int align, int eltsize)
1423 elf_sects[elf_nsect].data = data;
1424 elf_sects[elf_nsect].len = datalen;
1425 elf_sects[elf_nsect].is_saa = is_saa;
1426 elf_nsect++;
1428 fwriteint32_t((int32_t)name, elffp);
1429 fwriteint32_t((int32_t)type, elffp);
1430 fwriteint64_t((int64_t)flags, elffp);
1431 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1432 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1433 fwriteint64_t(datalen, elffp);
1434 if (data)
1435 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1436 fwriteint32_t((int32_t)link, elffp);
1437 fwriteint32_t((int32_t)info, elffp);
1438 fwriteint64_t((int64_t)align, elffp);
1439 fwriteint64_t((int64_t)eltsize, elffp);
1442 static void elf_write_sections(void)
1444 int i;
1445 for (i = 0; i < elf_nsect; i++)
1446 if (elf_sects[i].data) {
1447 int32_t len = elf_sects[i].len;
1448 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1449 int32_t align = reallen - len;
1450 if (elf_sects[i].is_saa)
1451 saa_fpwrite(elf_sects[i].data, elffp);
1452 else
1453 fwrite(elf_sects[i].data, len, 1, elffp);
1454 fwrite(align_str, align, 1, elffp);
1458 static void elf_sect_write(struct Section *sect,
1459 const uint8_t *data, uint64_t len)
1461 saa_wbytes(sect->data, data, len);
1462 sect->len += len;
1465 static int32_t elf_segbase(int32_t segment)
1467 return segment;
1470 static int elf_directive(char *directive, char *value, int pass)
1472 bool err;
1473 int64_t n;
1474 char *p;
1476 if (!strcmp(directive, "osabi")) {
1477 if (pass == 2)
1478 return 1; /* ignore in pass 2 */
1480 n = readnum(value, &err);
1481 if (err) {
1482 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1483 return 1;
1485 if (n < 0 || n > 255) {
1486 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1487 return 1;
1489 elf_osabi = n;
1490 elf_abiver = 0;
1492 if ((p = strchr(value,',')) == NULL)
1493 return 1;
1495 n = readnum(p+1, &err);
1496 if (err || n < 0 || n > 255) {
1497 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1498 return 1;
1501 elf_abiver = n;
1502 return 1;
1505 return 0;
1508 static void elf_filename(char *inname, char *outname, efunc error)
1510 strcpy(elf_module, inname);
1511 standard_extension(inname, outname, ".o", error);
1514 extern macros_t elf_stdmac[];
1516 static int elf_set_info(enum geninfo type, char **val)
1518 (void)type;
1519 (void)val;
1520 return 0;
1522 static struct dfmt df_dwarf = {
1523 "ELF64 (X86_64) dwarf debug format for Linux",
1524 "dwarf",
1525 debug64_init,
1526 dwarf64_linenum,
1527 debug64_deflabel,
1528 debug64_directive,
1529 debug64_typevalue,
1530 dwarf64_output,
1531 dwarf64_cleanup
1533 static struct dfmt df_stabs = {
1534 "ELF64 (X86_64) stabs debug format for Linux",
1535 "stabs",
1536 debug64_init,
1537 stabs64_linenum,
1538 debug64_deflabel,
1539 debug64_directive,
1540 debug64_typevalue,
1541 stabs64_output,
1542 stabs64_cleanup
1545 struct dfmt *elf64_debugs_arr[3] = { &df_stabs, &df_dwarf, NULL };
1547 struct ofmt of_elf64 = {
1548 "ELF64 (x86_64) object files (e.g. Linux)",
1549 "elf64",
1550 NULL,
1551 elf64_debugs_arr,
1552 &null_debug_form,
1553 elf_stdmac,
1554 elf_init,
1555 elf_set_info,
1556 elf_out,
1557 elf_deflabel,
1558 elf_section_names,
1559 elf_segbase,
1560 elf_directive,
1561 elf_filename,
1562 elf_cleanup
1565 /* common debugging routines */
1566 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1568 (void)of;
1569 (void)id;
1570 (void)fp;
1571 (void)error;
1573 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1574 char *special)
1576 (void)name;
1577 (void)segment;
1578 (void)offset;
1579 (void)is_global;
1580 (void)special;
1583 void debug64_directive(const char *directive, const char *params)
1585 (void)directive;
1586 (void)params;
1589 void debug64_typevalue(int32_t type)
1591 int32_t stype, ssize;
1592 switch (TYM_TYPE(type)) {
1593 case TY_LABEL:
1594 ssize = 0;
1595 stype = STT_NOTYPE;
1596 break;
1597 case TY_BYTE:
1598 ssize = 1;
1599 stype = STT_OBJECT;
1600 break;
1601 case TY_WORD:
1602 ssize = 2;
1603 stype = STT_OBJECT;
1604 break;
1605 case TY_DWORD:
1606 ssize = 4;
1607 stype = STT_OBJECT;
1608 break;
1609 case TY_FLOAT:
1610 ssize = 4;
1611 stype = STT_OBJECT;
1612 break;
1613 case TY_QWORD:
1614 ssize = 8;
1615 stype = STT_OBJECT;
1616 break;
1617 case TY_TBYTE:
1618 ssize = 10;
1619 stype = STT_OBJECT;
1620 break;
1621 case TY_OWORD:
1622 ssize = 16;
1623 stype = STT_OBJECT;
1624 break;
1625 case TY_COMMON:
1626 ssize = 0;
1627 stype = STT_COMMON;
1628 break;
1629 case TY_SEG:
1630 ssize = 0;
1631 stype = STT_SECTION;
1632 break;
1633 case TY_EXTERN:
1634 ssize = 0;
1635 stype = STT_NOTYPE;
1636 break;
1637 case TY_EQU:
1638 ssize = 0;
1639 stype = STT_NOTYPE;
1640 break;
1641 default:
1642 ssize = 0;
1643 stype = STT_NOTYPE;
1644 break;
1646 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1647 lastsym->size = ssize;
1648 lastsym->type = stype;
1652 /* stabs debugging routines */
1655 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1657 (void)segto;
1658 if (!stabs_filename) {
1659 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1660 strcpy(stabs_filename, filename);
1661 } else {
1662 if (strcmp(stabs_filename, filename)) {
1663 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1664 in fact, this leak comes in quite handy to maintain a list of files
1665 encountered so far in the symbol lines... */
1667 /* why not nasm_free(stabs_filename); we're done with the old one */
1669 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1670 strcpy(stabs_filename, filename);
1673 debug_immcall = 1;
1674 currentline = linenumber;
1678 void stabs64_output(int type, void *param)
1680 struct symlininfo *s;
1681 struct linelist *el;
1682 if (type == TY_DEBUGSYMLIN) {
1683 if (debug_immcall) {
1684 s = (struct symlininfo *)param;
1685 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1686 return; /* line info is only collected for executable sections */
1687 numlinestabs++;
1688 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1689 el->info.offset = s->offset;
1690 el->info.section = s->section;
1691 el->info.name = s->name;
1692 el->line = currentline;
1693 el->filename = stabs_filename;
1694 el->next = 0;
1695 if (stabslines) {
1696 stabslines->last->next = el;
1697 stabslines->last = el;
1698 } else {
1699 stabslines = el;
1700 stabslines->last = el;
1704 debug_immcall = 0;
1707 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1708 do {\
1709 WRITELONG(p,n_strx); \
1710 WRITECHAR(p,n_type); \
1711 WRITECHAR(p,n_other); \
1712 WRITESHORT(p,n_desc); \
1713 WRITELONG(p,n_value); \
1714 } while (0)
1716 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1718 void stabs64_generate(void)
1720 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1721 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1722 char **allfiles;
1723 int *fileidx;
1725 struct linelist *ptr;
1727 ptr = stabslines;
1729 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1730 for (i = 0; i < numlinestabs; i++)
1731 allfiles[i] = 0;
1732 numfiles = 0;
1733 while (ptr) {
1734 if (numfiles == 0) {
1735 allfiles[0] = ptr->filename;
1736 numfiles++;
1737 } else {
1738 for (i = 0; i < numfiles; i++) {
1739 if (!strcmp(allfiles[i], ptr->filename))
1740 break;
1742 if (i >= numfiles) {
1743 allfiles[i] = ptr->filename;
1744 numfiles++;
1747 ptr = ptr->next;
1749 strsize = 1;
1750 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1751 for (i = 0; i < numfiles; i++) {
1752 fileidx[i] = strsize;
1753 strsize += strlen(allfiles[i]) + 1;
1755 mainfileindex = 0;
1756 for (i = 0; i < numfiles; i++) {
1757 if (!strcmp(allfiles[i], elf_module)) {
1758 mainfileindex = i;
1759 break;
1763 /* worst case size of the stab buffer would be:
1764 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1766 sbuf =
1767 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1768 sizeof(struct stabentry));
1770 ssbuf = (uint8_t *)nasm_malloc(strsize);
1772 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1773 rptr = rbuf;
1775 for (i = 0; i < numfiles; i++) {
1776 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1778 ssbuf[0] = 0;
1780 stabstrlen = strsize; /* set global variable for length of stab strings */
1782 sptr = sbuf;
1783 ptr = stabslines;
1784 numstabs = 0;
1786 if (ptr) {
1787 /* this is the first stab, its strx points to the filename of the
1788 the source-file, the n_desc field should be set to the number
1789 of remaining stabs
1791 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1793 /* this is the stab for the main source file */
1794 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1796 /* relocation table entry */
1798 /* Since the symbol table has two entries before */
1799 /* the section symbols, the index in the info.section */
1800 /* member must be adjusted by adding 2 */
1802 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1803 WRITELONG(rptr, R_X86_64_32);
1804 WRITELONG(rptr, ptr->info.section + 2);
1806 numstabs++;
1807 currfile = mainfileindex;
1810 while (ptr) {
1811 if (strcmp(allfiles[currfile], ptr->filename)) {
1812 /* oops file has changed... */
1813 for (i = 0; i < numfiles; i++)
1814 if (!strcmp(allfiles[i], ptr->filename))
1815 break;
1816 currfile = i;
1817 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1818 ptr->info.offset);
1819 numstabs++;
1821 /* relocation table entry */
1823 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1824 WRITELONG(rptr, R_X86_64_32);
1825 WRITELONG(rptr, ptr->info.section + 2);
1828 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1829 numstabs++;
1831 /* relocation table entry */
1833 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1834 WRITELONG(rptr, R_X86_64_32);
1835 WRITELONG(rptr, ptr->info.section + 2);
1837 ptr = ptr->next;
1841 ((struct stabentry *)sbuf)->n_desc = numstabs;
1843 nasm_free(allfiles);
1844 nasm_free(fileidx);
1846 stablen = (sptr - sbuf);
1847 stabrellen = (rptr - rbuf);
1848 stabrelbuf = rbuf;
1849 stabbuf = sbuf;
1850 stabstrbuf = ssbuf;
1853 void stabs64_cleanup(void)
1855 struct linelist *ptr, *del;
1856 if (!stabslines)
1857 return;
1858 ptr = stabslines;
1859 while (ptr) {
1860 del = ptr;
1861 ptr = ptr->next;
1862 nasm_free(del);
1864 if (stabbuf)
1865 nasm_free(stabbuf);
1866 if (stabrelbuf)
1867 nasm_free(stabrelbuf);
1868 if (stabstrbuf)
1869 nasm_free(stabstrbuf);
1871 /* dwarf routines */
1874 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1876 (void)segto;
1877 dwarf64_findfile(filename);
1878 debug_immcall = 1;
1879 currentline = linenumber;
1882 /* called from elf_out with type == TY_DEBUGSYMLIN */
1883 void dwarf64_output(int type, void *param)
1885 int ln, aa, inx, maxln, soc;
1886 struct symlininfo *s;
1887 struct SAA *plinep;
1889 (void)type;
1891 s = (struct symlininfo *)param;
1892 /* line number info is only gathered for executable sections */
1893 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1894 return;
1895 /* Check if section index has changed */
1896 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1898 dwarf64_findsect(s->section);
1900 /* do nothing unless line or file has changed */
1901 if (debug_immcall)
1903 ln = currentline - dwarf_csect->line;
1904 aa = s->offset - dwarf_csect->offset;
1905 inx = dwarf_clist->line;
1906 plinep = dwarf_csect->psaa;
1907 /* check for file change */
1908 if (!(inx == dwarf_csect->file))
1910 saa_write8(plinep,DW_LNS_set_file);
1911 saa_write8(plinep,inx);
1912 dwarf_csect->file = inx;
1914 /* check for line change */
1915 if (ln)
1917 /* test if in range of special op code */
1918 maxln = line_base + line_range;
1919 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1920 if (ln >= line_base && ln < maxln && soc < 256)
1922 saa_write8(plinep,soc);
1924 else
1926 if (ln)
1928 saa_write8(plinep,DW_LNS_advance_line);
1929 saa_wleb128s(plinep,ln);
1931 if (aa)
1933 saa_write8(plinep,DW_LNS_advance_pc);
1934 saa_wleb128u(plinep,aa);
1937 dwarf_csect->line = currentline;
1938 dwarf_csect->offset = s->offset;
1940 /* show change handled */
1941 debug_immcall = 0;
1946 void dwarf64_generate(void)
1948 static const char nasm_signature[] = "NASM " NASM_VER;
1949 uint8_t *pbuf;
1950 int indx;
1951 struct linelist *ftentry;
1952 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1953 struct SAA *parangesrel, *plinesrel, *pinforel;
1954 struct sectlist *psect;
1955 size_t saalen, linepoff, totlen, highaddr;
1957 /* write epilogues for each line program range */
1958 /* and build aranges section */
1959 paranges = saa_init(1L);
1960 parangesrel = saa_init(1L);
1961 saa_write16(paranges,3); /* dwarf version */
1962 saa_write64(parangesrel, paranges->datalen+4);
1963 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1964 saa_write64(parangesrel, 0);
1965 saa_write32(paranges,0); /* offset into info */
1966 saa_write8(paranges,8); /* pointer size */
1967 saa_write8(paranges,0); /* not segmented */
1968 saa_write32(paranges,0); /* padding */
1969 /* iterate though sectlist entries */
1970 psect = dwarf_fsect;
1971 totlen = 0;
1972 highaddr = 0;
1973 for (indx = 0; indx < dwarf_nsections; indx++)
1975 plinep = psect->psaa;
1976 /* Line Number Program Epilogue */
1977 saa_write8(plinep,2); /* std op 2 */
1978 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1979 saa_write8(plinep,DW_LNS_extended_op);
1980 saa_write8(plinep,1); /* operand length */
1981 saa_write8(plinep,DW_LNE_end_sequence);
1982 totlen += plinep->datalen;
1983 /* range table relocation entry */
1984 saa_write64(parangesrel, paranges->datalen + 4);
1985 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1986 saa_write64(parangesrel, (uint64_t) 0);
1987 /* range table entry */
1988 saa_write64(paranges,0x0000); /* range start */
1989 saa_write64(paranges,sects[psect->section]->len); /* range length */
1990 highaddr += sects[psect->section]->len;
1991 /* done with this entry */
1992 psect = psect->next;
1994 saa_write64(paranges,0); /* null address */
1995 saa_write64(paranges,0); /* null length */
1996 saalen = paranges->datalen;
1997 arangeslen = saalen + 4;
1998 arangesbuf = pbuf = nasm_malloc(arangeslen);
1999 WRITELONG(pbuf,saalen); /* initial length */
2000 saa_rnbytes(paranges, pbuf, saalen);
2001 saa_free(paranges);
2003 /* build rela.aranges section */
2004 arangesrellen = saalen = parangesrel->datalen;
2005 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2006 saa_rnbytes(parangesrel, pbuf, saalen);
2007 saa_free(parangesrel);
2009 /* build pubnames section */
2010 ppubnames = saa_init(1L);
2011 saa_write16(ppubnames,3); /* dwarf version */
2012 saa_write32(ppubnames,0); /* offset into info */
2013 saa_write32(ppubnames,0); /* space used in info */
2014 saa_write32(ppubnames,0); /* end of list */
2015 saalen = ppubnames->datalen;
2016 pubnameslen = saalen + 4;
2017 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2018 WRITELONG(pbuf,saalen); /* initial length */
2019 saa_rnbytes(ppubnames, pbuf, saalen);
2020 saa_free(ppubnames);
2022 /* build info section */
2023 pinfo = saa_init(1L);
2024 pinforel = saa_init(1L);
2025 saa_write16(pinfo,3); /* dwarf version */
2026 saa_write64(pinforel, pinfo->datalen + 4);
2027 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2028 saa_write64(pinforel, 0);
2029 saa_write32(pinfo,0); /* offset into abbrev */
2030 saa_write8(pinfo,8); /* pointer size */
2031 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2032 saa_write64(pinforel, pinfo->datalen + 4);
2033 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2034 saa_write64(pinforel, 0);
2035 saa_write64(pinfo,0); /* DW_AT_low_pc */
2036 saa_write64(pinforel, pinfo->datalen + 4);
2037 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2038 saa_write64(pinforel, 0);
2039 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2040 saa_write64(pinforel, pinfo->datalen + 4);
2041 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2042 saa_write64(pinforel, 0);
2043 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2044 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2045 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2046 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2047 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2048 saa_write64(pinforel, pinfo->datalen + 4);
2049 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2050 saa_write64(pinforel, 0);
2051 saa_write64(pinfo,0); /* DW_AT_low_pc */
2052 saa_write64(pinfo,0); /* DW_AT_frame_base */
2053 saa_write8(pinfo,0); /* end of entries */
2054 saalen = pinfo->datalen;
2055 infolen = saalen + 4;
2056 infobuf = pbuf = nasm_malloc(infolen);
2057 WRITELONG(pbuf,saalen); /* initial length */
2058 saa_rnbytes(pinfo, pbuf, saalen);
2059 saa_free(pinfo);
2061 /* build rela.info section */
2062 inforellen = saalen = pinforel->datalen;
2063 inforelbuf = pbuf = nasm_malloc(inforellen);
2064 saa_rnbytes(pinforel, pbuf, saalen);
2065 saa_free(pinforel);
2067 /* build abbrev section */
2068 pabbrev = saa_init(1L);
2069 saa_write8(pabbrev,1); /* entry number LEB128u */
2070 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2071 saa_write8(pabbrev,1); /* has children */
2072 /* the following attributes and forms are all LEB128u values */
2073 saa_write8(pabbrev,DW_AT_low_pc);
2074 saa_write8(pabbrev,DW_FORM_addr);
2075 saa_write8(pabbrev,DW_AT_high_pc);
2076 saa_write8(pabbrev,DW_FORM_addr);
2077 saa_write8(pabbrev,DW_AT_stmt_list);
2078 saa_write8(pabbrev,DW_FORM_data4);
2079 saa_write8(pabbrev,DW_AT_name);
2080 saa_write8(pabbrev,DW_FORM_string);
2081 saa_write8(pabbrev,DW_AT_producer);
2082 saa_write8(pabbrev,DW_FORM_string);
2083 saa_write8(pabbrev,DW_AT_language);
2084 saa_write8(pabbrev,DW_FORM_data2);
2085 saa_write16(pabbrev,0); /* end of entry */
2086 /* LEB128u usage same as above */
2087 saa_write8(pabbrev,2); /* entry number */
2088 saa_write8(pabbrev,DW_TAG_subprogram);
2089 saa_write8(pabbrev,0); /* no children */
2090 saa_write8(pabbrev,DW_AT_low_pc);
2091 saa_write8(pabbrev,DW_FORM_addr);
2092 saa_write8(pabbrev,DW_AT_frame_base);
2093 saa_write8(pabbrev,DW_FORM_data4);
2094 saa_write16(pabbrev,0); /* end of entry */
2095 abbrevlen = saalen = pabbrev->datalen;
2096 abbrevbuf = pbuf = nasm_malloc(saalen);
2097 saa_rnbytes(pabbrev, pbuf, saalen);
2098 saa_free(pabbrev);
2100 /* build line section */
2101 /* prolog */
2102 plines = saa_init(1L);
2103 saa_write8(plines,1); /* Minimum Instruction Length */
2104 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2105 saa_write8(plines,line_base); /* Line Base */
2106 saa_write8(plines,line_range); /* Line Range */
2107 saa_write8(plines,opcode_base); /* Opcode Base */
2108 /* standard opcode lengths (# of LEB128u operands) */
2109 saa_write8(plines,0); /* Std opcode 1 length */
2110 saa_write8(plines,1); /* Std opcode 2 length */
2111 saa_write8(plines,1); /* Std opcode 3 length */
2112 saa_write8(plines,1); /* Std opcode 4 length */
2113 saa_write8(plines,1); /* Std opcode 5 length */
2114 saa_write8(plines,0); /* Std opcode 6 length */
2115 saa_write8(plines,0); /* Std opcode 7 length */
2116 saa_write8(plines,0); /* Std opcode 8 length */
2117 saa_write8(plines,1); /* Std opcode 9 length */
2118 saa_write8(plines,0); /* Std opcode 10 length */
2119 saa_write8(plines,0); /* Std opcode 11 length */
2120 saa_write8(plines,1); /* Std opcode 12 length */
2121 /* Directory Table */
2122 saa_write8(plines,0); /* End of table */
2123 /* File Name Table */
2124 ftentry = dwarf_flist;
2125 for (indx = 0;indx<dwarf_numfiles;indx++)
2127 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2128 saa_write8(plines,0); /* directory LEB128u */
2129 saa_write8(plines,0); /* time LEB128u */
2130 saa_write8(plines,0); /* size LEB128u */
2131 ftentry = ftentry->next;
2133 saa_write8(plines,0); /* End of table */
2134 linepoff = plines->datalen;
2135 linelen = linepoff + totlen + 10;
2136 linebuf = pbuf = nasm_malloc(linelen);
2137 WRITELONG(pbuf,linelen-4); /* initial length */
2138 WRITESHORT(pbuf,3); /* dwarf version */
2139 WRITELONG(pbuf,linepoff); /* offset to line number program */
2140 /* write line header */
2141 saalen = linepoff;
2142 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2143 pbuf += linepoff;
2144 saa_free(plines);
2145 /* concatonate line program ranges */
2146 linepoff += 13;
2147 plinesrel = saa_init(1L);
2148 psect = dwarf_fsect;
2149 for (indx = 0; indx < dwarf_nsections; indx++)
2151 saa_write64(plinesrel, linepoff);
2152 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2153 saa_write64(plinesrel, (uint64_t) 0);
2154 plinep = psect->psaa;
2155 saalen = plinep->datalen;
2156 saa_rnbytes(plinep, pbuf, saalen);
2157 pbuf += saalen;
2158 linepoff += saalen;
2159 saa_free(plinep);
2160 /* done with this entry */
2161 psect = psect->next;
2165 /* build rela.lines section */
2166 linerellen =saalen = plinesrel->datalen;
2167 linerelbuf = pbuf = nasm_malloc(linerellen);
2168 saa_rnbytes(plinesrel, pbuf, saalen);
2169 saa_free(plinesrel);
2171 /* build frame section */
2172 framelen = 4;
2173 framebuf = pbuf = nasm_malloc(framelen);
2174 WRITELONG(pbuf,framelen-4); /* initial length */
2176 /* build loc section */
2177 loclen = 16;
2178 locbuf = pbuf = nasm_malloc(loclen);
2179 WRITEDLONG(pbuf,0); /* null beginning offset */
2180 WRITEDLONG(pbuf,0); /* null ending offset */
2183 void dwarf64_cleanup(void)
2185 if (arangesbuf)
2186 nasm_free(arangesbuf);
2187 if (arangesrelbuf)
2188 nasm_free(arangesrelbuf);
2189 if (pubnamesbuf)
2190 nasm_free(pubnamesbuf);
2191 if (infobuf)
2192 nasm_free(infobuf);
2193 if (inforelbuf)
2194 nasm_free(inforelbuf);
2195 if (abbrevbuf)
2196 nasm_free(abbrevbuf);
2197 if (linebuf)
2198 nasm_free(linebuf);
2199 if (linerelbuf)
2200 nasm_free(linerelbuf);
2201 if (framebuf)
2202 nasm_free(framebuf);
2203 if (locbuf)
2204 nasm_free(locbuf);
2206 void dwarf64_findfile(const char * fname)
2208 int finx;
2209 struct linelist *match;
2211 /* return if fname is current file name */
2212 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2213 /* search for match */
2214 else
2216 match = 0;
2217 if (dwarf_flist)
2219 match = dwarf_flist;
2220 for (finx = 0; finx < dwarf_numfiles; finx++)
2222 if (!(strcmp(fname, match->filename)))
2224 dwarf_clist = match;
2225 return;
2229 /* add file name to end of list */
2230 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2231 dwarf_numfiles++;
2232 dwarf_clist->line = dwarf_numfiles;
2233 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2234 strcpy(dwarf_clist->filename,fname);
2235 dwarf_clist->next = 0;
2236 /* if first entry */
2237 if (!dwarf_flist)
2239 dwarf_flist = dwarf_elist = dwarf_clist;
2240 dwarf_clist->last = 0;
2242 /* chain to previous entry */
2243 else
2245 dwarf_elist->next = dwarf_clist;
2246 dwarf_elist = dwarf_clist;
2250 /* */
2251 void dwarf64_findsect(const int index)
2253 int sinx;
2254 struct sectlist *match;
2255 struct SAA *plinep;
2256 /* return if index is current section index */
2257 if (dwarf_csect && (dwarf_csect->section == index))
2259 return;
2261 /* search for match */
2262 else
2264 match = 0;
2265 if (dwarf_fsect)
2267 match = dwarf_fsect;
2268 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2270 if ((match->section == index))
2272 dwarf_csect = match;
2273 return;
2275 match = match->next;
2278 /* add entry to end of list */
2279 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2280 dwarf_nsections++;
2281 dwarf_csect->psaa = plinep = saa_init(1L);
2282 dwarf_csect->line = 1;
2283 dwarf_csect->offset = 0;
2284 dwarf_csect->file = 1;
2285 dwarf_csect->section = index;
2286 dwarf_csect->next = 0;
2287 /* set relocatable address at start of line program */
2288 saa_write8(plinep,DW_LNS_extended_op);
2289 saa_write8(plinep,9); /* operand length */
2290 saa_write8(plinep,DW_LNE_set_address);
2291 saa_write64(plinep,0); /* Start Address */
2292 /* if first entry */
2293 if (!dwarf_fsect)
2295 dwarf_fsect = dwarf_esect = dwarf_csect;
2296 dwarf_csect->last = 0;
2298 /* chain to previous entry */
2299 else
2301 dwarf_esect->next = dwarf_csect;
2302 dwarf_esect = dwarf_csect;
2307 #endif /* OF_ELF */