BR 2672117: handle long lines for db/dw/etc.
[nasm/avx512.git] / output / outelf64.c
blob24d0b81cac5b26e382c7915456637a21167c72e7
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 /* Definitions in lieu of elf.h */
27 #define SHT_NULL 0 /* Inactive section header */
28 #define SHT_PROGBITS 1 /* Program defined content */
29 #define SHT_RELA 4 /* Relocation entries with addends */
30 #define SHT_NOBITS 8 /* Section requires no space in file */
31 #define SHF_WRITE (1 << 0) /* Writable */
32 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
33 #define SHF_EXECINSTR (1 << 2) /* Executable */
34 #define SHF_TLS (1 << 10) /* Section hold thread-local data. */
35 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
36 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
37 #define R_X86_64_NONE 0 /* No reloc */
38 #define R_X86_64_64 1 /* Direct 64 bit */
39 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
40 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
41 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
42 #define R_X86_64_COPY 5 /* Copy symbol at runtime */
43 #define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
44 #define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
45 #define R_X86_64_RELATIVE 8 /* Adjust by program base */
46 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative
47 offset to GOT */
48 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
49 #define R_X86_64_32S 11 /* Direct 32 bit sign extended */
50 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
51 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
52 #define R_X86_64_8 14 /* Direct 8 bit sign extended */
53 #define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
54 #define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */
55 #define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */
56 #define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */
57 #define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset
58 to two GOT entries for GD symbol */
59 #define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset
60 to two GOT entries for LD symbol */
61 #define R_X86_64_DTPOFF32 21 /* Offset in TLS block */
62 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset
63 to GOT entry for IE symbol */
64 #define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */
65 #define R_X86_64_PC64 24 /* word64 S + A - P */
66 #define R_X86_64_GOTOFF64 25 /* word64 S + A - GOT */
67 #define R_X86_64_GOTPC32 26 /* word32 GOT + A - P */
68 #define R_X86_64_GOT64 27 /* word64 G + A */
69 #define R_X86_64_GOTPCREL64 28 /* word64 G + GOT - P + A */
70 #define R_X86_64_GOTPC64 29 /* word64 GOT - P + A */
71 #define R_X86_64_GOTPLT64 30 /* word64 G + A */
72 #define R_X86_64_PLTOFF64 31 /* word64 L - GOT + A */
73 #define R_X86_64_SIZE32 32 /* word32 Z + A */
74 #define R_X86_64_SIZE64 33 /* word64 Z + A */
75 #define R_X86_64_GOTPC32_TLSDESC 34 /* word32 */
76 #define R_X86_64_TLSDESC_CALL 35 /* none */
77 #define R_X86_64_TLSDESC 36 /* word64×2 */
78 #define ET_REL 1 /* Relocatable file */
79 #define EM_X86_64 62 /* AMD x86-64 architecture */
80 #define STT_NOTYPE 0 /* Symbol type is unspecified */
81 #define STT_OBJECT 1 /* Symbol is a data object */
82 #define STT_FUNC 2 /* Symbol is a code object */
83 #define STT_SECTION 3 /* Symbol associated with a section */
84 #define STT_FILE 4 /* Symbol's name is file name */
85 #define STT_COMMON 5 /* Symbol is a common data object */
86 #define STT_TLS 6 /* Symbol is thread-local data object*/
87 #define STT_NUM 7 /* Number of defined types. */
89 /* Definitions in lieu of dwarf.h */
90 #define DW_TAG_compile_unit 0x11
91 #define DW_TAG_subprogram 0x2e
92 #define DW_AT_name 0x03
93 #define DW_AT_stmt_list 0x10
94 #define DW_AT_low_pc 0x11
95 #define DW_AT_high_pc 0x12
96 #define DW_AT_language 0x13
97 #define DW_AT_producer 0x25
98 #define DW_AT_frame_base 0x40
99 #define DW_FORM_addr 0x01
100 #define DW_FORM_data2 0x05
101 #define DW_FORM_data4 0x06
102 #define DW_FORM_string 0x08
103 #define DW_LNS_extended_op 0
104 #define DW_LNS_advance_pc 2
105 #define DW_LNS_advance_line 3
106 #define DW_LNS_set_file 4
107 #define DW_LNE_end_sequence 1
108 #define DW_LNE_set_address 2
109 #define DW_LNE_define_file 3
110 #define DW_LANG_Mips_Assembler 0x8001
112 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
114 typedef uint32_t Elf64_Word;
115 typedef uint64_t Elf64_Xword;
116 typedef uint64_t Elf64_Addr;
117 typedef uint64_t Elf64_Off;
118 typedef struct
120 Elf64_Word sh_name; /* Section name (string tbl index) */
121 Elf64_Word sh_type; /* Section type */
122 Elf64_Xword sh_flags; /* Section flags */
123 Elf64_Addr sh_addr; /* Section virtual addr at execution */
124 Elf64_Off sh_offset; /* Section file offset */
125 Elf64_Xword sh_size; /* Section size in bytes */
126 Elf64_Word sh_link; /* Link to another section */
127 Elf64_Word sh_info; /* Additional section information */
128 Elf64_Xword sh_addralign; /* Section alignment */
129 Elf64_Xword sh_entsize; /* Entry size if section holds table */
130 } Elf64_Shdr;
133 #ifdef OF_ELF64
136 struct Reloc {
137 struct Reloc *next;
138 int64_t address; /* relative to _start_ of section */
139 int64_t symbol; /* symbol index */
140 int64_t offset; /* symbol addend */
141 int type; /* type of relocation */
144 struct Symbol {
145 struct rbtree symv; /* symbol value and rbtree of globals */
146 int32_t strpos; /* string table position of name */
147 int32_t section; /* section ID of the symbol */
148 int type; /* symbol type */
149 int other; /* symbol visibility */
150 int32_t size; /* size of symbol */
151 int32_t globnum; /* symbol table offset if global */
152 struct Symbol *nextfwd; /* list of unresolved-size symbols */
153 char *name; /* used temporarily if in above list */
156 struct Section {
157 struct SAA *data;
158 uint64_t len, size;
159 uint32_t nrelocs;
160 int32_t index; /* index into sects array */
161 uint32_t type; /* SHT_PROGBITS or SHT_NOBITS */
162 uint64_t align; /* alignment: power of two */
163 uint64_t flags; /* section flags */
164 char *name;
165 struct SAA *rel;
166 uint64_t rellen;
167 struct Reloc *head, **tail;
168 struct rbtree *gsyms; /* global symbols in section */
171 #define SECT_DELTA 32
172 static struct Section **sects;
173 static int nsects, sectlen;
175 #define SHSTR_DELTA 256
176 static char *shstrtab;
177 static int shstrtablen, shstrtabsize;
179 static struct SAA *syms;
180 static uint32_t nlocals, nglobs;
182 static int32_t def_seg;
184 static struct RAA *bsym;
186 static struct SAA *strs;
187 static uint32_t strslen;
189 static FILE *elffp;
190 static efunc error;
191 static evalfunc evaluate;
193 static struct Symbol *fwds;
195 static char elf_module[FILENAME_MAX];
197 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
198 static uint8_t elf_abiver = 0; /* Current ABI version */
200 extern struct ofmt of_elf64;
202 #define SHN_UNDEF 0
204 #define SYM_GLOBAL 0x10
206 #define STV_DEFAULT 0
207 #define STV_INTERNAL 1
208 #define STV_HIDDEN 2
209 #define STV_PROTECTED 3
211 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
213 #define SEG_ALIGN 16 /* alignment of sections in file */
214 #define SEG_ALIGN_1 (SEG_ALIGN-1)
216 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
218 static struct ELF_SECTDATA {
219 void *data;
220 int64_t len;
221 bool is_saa;
222 } *elf_sects;
223 static int elf_nsect, nsections;
224 static int64_t elf_foffs;
226 static void elf_write(void);
227 static void elf_sect_write(struct Section *, const void *, size_t);
228 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
229 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
230 int, int);
231 static void elf_write_sections(void);
232 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
233 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
234 static void add_sectname(char *, char *);
236 /* type values for stabs debugging sections */
237 #define N_SO 0x64 /* ID for main source file */
238 #define N_SOL 0x84 /* ID for sub-source file */
239 #define N_BINCL 0x82 /* not currently used */
240 #define N_EINCL 0xA2 /* not currently used */
241 #define N_SLINE 0x44
243 struct stabentry {
244 uint32_t n_strx;
245 uint8_t n_type;
246 uint8_t n_other;
247 uint16_t n_desc;
248 uint32_t n_value;
251 struct erel {
252 int offset, info;
255 struct symlininfo {
256 int offset;
257 int section; /* index into sects[] */
258 int segto; /* internal section number */
259 char *name; /* shallow-copied pointer of section name */
262 struct linelist {
263 struct symlininfo info;
264 int line;
265 char *filename;
266 struct linelist *next;
267 struct linelist *last;
270 struct sectlist {
271 struct SAA *psaa;
272 int section;
273 int line;
274 int offset;
275 int file;
276 struct sectlist *next;
277 struct sectlist *last;
280 /* common debug variables */
281 static int currentline = 1;
282 static int debug_immcall = 0;
284 /* stabs debug variables */
285 static struct linelist *stabslines = 0;
286 static int numlinestabs = 0;
287 static char *stabs_filename = 0;
288 static int symtabsection;
289 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
290 static int stablen, stabstrlen, stabrellen;
292 /* dwarf debug variables */
293 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
294 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
295 static int dwarf_numfiles = 0, dwarf_nsections;
296 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
297 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
298 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
299 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
300 abbrevlen, linelen, linerellen, framelen, loclen;
301 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
304 static struct dfmt df_dwarf;
305 static struct dfmt df_stabs;
306 static struct Symbol *lastsym;
308 /* common debugging routines */
309 void debug64_typevalue(int32_t);
310 void debug64_init(struct ofmt *, void *, FILE *, efunc);
311 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
312 void debug64_directive(const char *, const char *);
314 /* stabs debugging routines */
315 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
316 void stabs64_output(int, void *);
317 void stabs64_generate(void);
318 void stabs64_cleanup(void);
320 /* dwarf debugging routines */
321 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
322 void dwarf64_output(int, void *);
323 void dwarf64_generate(void);
324 void dwarf64_cleanup(void);
325 void dwarf64_findfile(const char *);
326 void dwarf64_findsect(const int);
329 * Special section numbers which are used to define ELF special
330 * symbols, which can be used with WRT to provide PIC relocation
331 * types.
333 static int32_t elf_gotpc_sect, elf_gotoff_sect;
334 static int32_t elf_got_sect, elf_plt_sect;
335 static int32_t elf_sym_sect;
336 static int32_t elf_gottpoff_sect;
338 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
340 maxbits = 64;
341 elffp = fp;
342 error = errfunc;
343 evaluate = eval;
344 (void)ldef; /* placate optimisers */
345 sects = NULL;
346 nsects = sectlen = 0;
347 syms = saa_init((int32_t)sizeof(struct Symbol));
348 nlocals = nglobs = 0;
349 bsym = raa_init();
350 strs = saa_init(1L);
351 saa_wbytes(strs, "\0", 1L);
352 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
353 strslen = 2 + strlen(elf_module);
354 shstrtab = NULL;
355 shstrtablen = shstrtabsize = 0;;
356 add_sectname("", "");
358 fwds = NULL;
360 elf_gotpc_sect = seg_alloc();
361 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
362 error);
363 elf_gotoff_sect = seg_alloc();
364 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
365 error);
366 elf_got_sect = seg_alloc();
367 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
368 error);
369 elf_plt_sect = seg_alloc();
370 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
371 error);
372 elf_sym_sect = seg_alloc();
373 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
374 error);
375 elf_gottpoff_sect = seg_alloc();
376 ldef("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false, &of_elf64,
377 error);
379 def_seg = seg_alloc();
383 static void elf_cleanup(int debuginfo)
385 struct Reloc *r;
386 int i;
388 (void)debuginfo;
390 elf_write();
391 fclose(elffp);
392 for (i = 0; i < nsects; i++) {
393 if (sects[i]->type != SHT_NOBITS)
394 saa_free(sects[i]->data);
395 if (sects[i]->head)
396 saa_free(sects[i]->rel);
397 while (sects[i]->head) {
398 r = sects[i]->head;
399 sects[i]->head = sects[i]->head->next;
400 nasm_free(r);
403 nasm_free(sects);
404 saa_free(syms);
405 raa_free(bsym);
406 saa_free(strs);
407 if (of_elf64.current_dfmt) {
408 of_elf64.current_dfmt->cleanup();
411 /* add entry to the elf .shstrtab section */
412 static void add_sectname(char *firsthalf, char *secondhalf)
414 int len = strlen(firsthalf) + strlen(secondhalf);
415 while (shstrtablen + len + 1 > shstrtabsize)
416 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
417 strcpy(shstrtab + shstrtablen, firsthalf);
418 strcat(shstrtab + shstrtablen, secondhalf);
419 shstrtablen += len + 1;
422 static int elf_make_section(char *name, int type, int flags, int align)
424 struct Section *s;
426 s = nasm_malloc(sizeof(*s));
428 if (type != SHT_NOBITS)
429 s->data = saa_init(1L);
430 s->head = NULL;
431 s->tail = &s->head;
432 s->len = s->size = 0;
433 s->nrelocs = 0;
434 if (!strcmp(name, ".text"))
435 s->index = def_seg;
436 else
437 s->index = seg_alloc();
438 add_sectname("", name);
439 s->name = nasm_malloc(1 + strlen(name));
440 strcpy(s->name, name);
441 s->type = type;
442 s->flags = flags;
443 s->align = align;
444 s->gsyms = NULL;
446 if (nsects >= sectlen)
447 sects =
448 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
449 sects[nsects++] = s;
451 return nsects - 1;
454 static int32_t elf_section_names(char *name, int pass, int *bits)
456 char *p;
457 unsigned flags_and, flags_or;
458 uint64_t type, align;
459 int i;
462 * Default is 64 bits.
464 if (!name) {
465 *bits = 64;
466 return def_seg;
469 p = name;
470 while (*p && !nasm_isspace(*p))
471 p++;
472 if (*p)
473 *p++ = '\0';
474 flags_and = flags_or = type = align = 0;
476 while (*p && nasm_isspace(*p))
477 p++;
478 while (*p) {
479 char *q = p;
480 while (*p && !nasm_isspace(*p))
481 p++;
482 if (*p)
483 *p++ = '\0';
484 while (*p && nasm_isspace(*p))
485 p++;
487 if (!nasm_strnicmp(q, "align=", 6)) {
488 align = atoi(q + 6);
489 if (align == 0)
490 align = 1;
491 if ((align - 1) & align) { /* means it's not a power of two */
492 error(ERR_NONFATAL, "section alignment %d is not"
493 " a power of two", align);
494 align = 1;
496 } else if (!nasm_stricmp(q, "alloc")) {
497 flags_and |= SHF_ALLOC;
498 flags_or |= SHF_ALLOC;
499 } else if (!nasm_stricmp(q, "noalloc")) {
500 flags_and |= SHF_ALLOC;
501 flags_or &= ~SHF_ALLOC;
502 } else if (!nasm_stricmp(q, "exec")) {
503 flags_and |= SHF_EXECINSTR;
504 flags_or |= SHF_EXECINSTR;
505 } else if (!nasm_stricmp(q, "noexec")) {
506 flags_and |= SHF_EXECINSTR;
507 flags_or &= ~SHF_EXECINSTR;
508 } else if (!nasm_stricmp(q, "write")) {
509 flags_and |= SHF_WRITE;
510 flags_or |= SHF_WRITE;
511 } else if (!nasm_stricmp(q, "tls")) {
512 flags_and |= SHF_TLS;
513 flags_or |= SHF_TLS;
514 } else if (!nasm_stricmp(q, "nowrite")) {
515 flags_and |= SHF_WRITE;
516 flags_or &= ~SHF_WRITE;
517 } else if (!nasm_stricmp(q, "progbits")) {
518 type = SHT_PROGBITS;
519 } else if (!nasm_stricmp(q, "nobits")) {
520 type = SHT_NOBITS;
521 } else if (pass == 1) error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
522 " declaration of section `%s'", q, name);
525 if (!strcmp(name, ".comment") ||
526 !strcmp(name, ".shstrtab") ||
527 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
528 error(ERR_NONFATAL, "attempt to redefine reserved section"
529 "name `%s'", name);
530 return NO_SEG;
533 for (i = 0; i < nsects; i++)
534 if (!strcmp(name, sects[i]->name))
535 break;
536 if (i == nsects) {
537 if (!strcmp(name, ".text"))
538 i = elf_make_section(name, SHT_PROGBITS,
539 SHF_ALLOC | SHF_EXECINSTR, 16);
540 else if (!strcmp(name, ".rodata"))
541 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
542 else if (!strcmp(name, ".data"))
543 i = elf_make_section(name, SHT_PROGBITS,
544 SHF_ALLOC | SHF_WRITE, 4);
545 else if (!strcmp(name, ".bss"))
546 i = elf_make_section(name, SHT_NOBITS,
547 SHF_ALLOC | SHF_WRITE, 4);
548 else if (!strcmp(name, ".tdata"))
549 i = elf_make_section(name, SHT_PROGBITS,
550 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
551 else if (!strcmp(name, ".tbss"))
552 i = elf_make_section(name, SHT_NOBITS,
553 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
554 else
555 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
556 if (type)
557 sects[i]->type = type;
558 if (align)
559 sects[i]->align = align;
560 sects[i]->flags &= ~flags_and;
561 sects[i]->flags |= flags_or;
562 } else if (pass == 1) {
563 if ((type && sects[i]->type != type)
564 || (align && sects[i]->align != align)
565 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
566 error(ERR_WARNING, "incompatible section attributes ignored on"
567 " redeclaration of section `%s'", name);
570 return sects[i]->index;
573 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
574 int is_global, char *special)
576 int pos = strslen;
577 struct Symbol *sym;
578 bool special_used = false;
580 #if defined(DEBUG) && DEBUG>2
581 fprintf(stderr,
582 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
583 name, segment, offset, is_global, special);
584 #endif
585 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
587 * This is a NASM special symbol. We never allow it into
588 * the ELF symbol table, even if it's a valid one. If it
589 * _isn't_ a valid one, we should barf immediately.
591 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
592 strcmp(name, "..got") && strcmp(name, "..plt") &&
593 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
594 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
595 return;
598 if (is_global == 3) {
599 struct Symbol **s;
601 * Fix up a forward-reference symbol size from the first
602 * pass.
604 for (s = &fwds; *s; s = &(*s)->nextfwd)
605 if (!strcmp((*s)->name, name)) {
606 struct tokenval tokval;
607 expr *e;
608 char *p = special;
610 while (*p && !nasm_isspace(*p))
611 p++;
612 while (*p && nasm_isspace(*p))
613 p++;
614 stdscan_reset();
615 stdscan_bufptr = p;
616 tokval.t_type = TOKEN_INVALID;
617 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
618 if (e) {
619 if (!is_simple(e))
620 error(ERR_NONFATAL, "cannot use relocatable"
621 " expression as symbol size");
622 else
623 (*s)->size = reloc_value(e);
627 * Remove it from the list of unresolved sizes.
629 nasm_free((*s)->name);
630 *s = (*s)->nextfwd;
631 return;
633 return; /* it wasn't an important one */
636 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
637 strslen += 1 + strlen(name);
639 lastsym = sym = saa_wstruct(syms);
641 sym->strpos = pos;
642 sym->type = is_global ? SYM_GLOBAL : 0;
643 sym->other = STV_DEFAULT;
644 sym->size = 0;
645 if (segment == NO_SEG)
646 sym->section = SHN_ABS;
647 else {
648 int i;
649 sym->section = SHN_UNDEF;
650 if (nsects == 0 && segment == def_seg) {
651 int tempint;
652 if (segment != elf_section_names(".text", 2, &tempint))
653 error(ERR_PANIC,
654 "strange segment conditions in ELF driver");
655 sym->section = nsects;
656 } else {
657 for (i = 0; i < nsects; i++)
658 if (segment == sects[i]->index) {
659 sym->section = i + 1;
660 break;
665 if (is_global == 2) {
666 sym->size = offset;
667 sym->symv.key = 0;
668 sym->section = SHN_COMMON;
670 * We have a common variable. Check the special text to see
671 * if it's a valid number and power of two; if so, store it
672 * as the alignment for the common variable.
674 if (special) {
675 bool err;
676 sym->symv.key = readnum(special, &err);
677 if (err)
678 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
679 " valid number", special);
680 else if ((sym->symv.key | (sym->symv.key - 1))
681 != 2 * sym->symv.key - 1)
682 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
683 " power of two", special);
685 special_used = true;
686 } else
687 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
689 if (sym->type == SYM_GLOBAL) {
691 * If sym->section == SHN_ABS, then the first line of the
692 * else section would cause a core dump, because its a reference
693 * beyond the end of the section array.
694 * This behaviour is exhibited by this code:
695 * GLOBAL crash_nasm
696 * crash_nasm equ 0
697 * To avoid such a crash, such requests are silently discarded.
698 * This may not be the best solution.
700 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
701 bsym = raa_write(bsym, segment, nglobs);
702 } else if (sym->section != SHN_ABS) {
704 * This is a global symbol; so we must add it to the rbtree
705 * of global symbols in its section.
707 * In addition, we check the special text for symbol
708 * type and size information.
710 sects[sym->section-1]->gsyms =
711 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
713 if (special) {
714 int n = strcspn(special, " \t");
716 if (!nasm_strnicmp(special, "function", n))
717 sym->type |= STT_FUNC;
718 else if (!nasm_strnicmp(special, "data", n) ||
719 !nasm_strnicmp(special, "object", n))
720 sym->type |= STT_OBJECT;
721 else if (!nasm_strnicmp(special, "notype", n))
722 sym->type |= STT_NOTYPE;
723 else
724 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
725 n, special);
726 special += n;
728 while (nasm_isspace(*special))
729 ++special;
730 if (*special) {
731 n = strcspn(special, " \t");
732 if (!nasm_strnicmp(special, "default", n))
733 sym->other = STV_DEFAULT;
734 else if (!nasm_strnicmp(special, "internal", n))
735 sym->other = STV_INTERNAL;
736 else if (!nasm_strnicmp(special, "hidden", n))
737 sym->other = STV_HIDDEN;
738 else if (!nasm_strnicmp(special, "protected", n))
739 sym->other = STV_PROTECTED;
740 else
741 n = 0;
742 special += n;
745 if (*special) {
746 struct tokenval tokval;
747 expr *e;
748 int fwd = 0;
749 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
751 while (special[n] && nasm_isspace(special[n]))
752 n++;
754 * We have a size expression; attempt to
755 * evaluate it.
757 stdscan_reset();
758 stdscan_bufptr = special + n;
759 tokval.t_type = TOKEN_INVALID;
760 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
761 NULL);
762 if (fwd) {
763 sym->nextfwd = fwds;
764 fwds = sym;
765 sym->name = nasm_strdup(name);
766 } else if (e) {
767 if (!is_simple(e))
768 error(ERR_NONFATAL, "cannot use relocatable"
769 " expression as symbol size");
770 else
771 sym->size = reloc_value(e);
773 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
775 special_used = true;
778 * If TLS segment, mark symbol accordingly.
780 if (sects[sym->section - 1]->flags & SHF_TLS) {
781 sym->type &= 0xf0;
782 sym->type |= STT_TLS;
785 sym->globnum = nglobs;
786 nglobs++;
787 } else
788 nlocals++;
790 if (special && !special_used)
791 error(ERR_NONFATAL, "no special symbol features supported here");
794 static void elf_add_reloc(struct Section *sect, int32_t segment,
795 int64_t offset, int type)
797 struct Reloc *r;
798 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
799 sect->tail = &r->next;
800 r->next = NULL;
802 r->address = sect->len;
803 r->offset = offset;
804 if (segment == NO_SEG)
805 r->symbol = 0;
806 else {
807 int i;
808 r->symbol = 0;
809 for (i = 0; i < nsects; i++)
810 if (segment == sects[i]->index)
811 r->symbol = i + 2;
812 if (!r->symbol)
813 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
815 r->type = type;
817 sect->nrelocs++;
821 * This routine deals with ..got and ..sym relocations: the more
822 * complicated kinds. In shared-library writing, some relocations
823 * with respect to global symbols must refer to the precise symbol
824 * rather than referring to an offset from the base of the section
825 * _containing_ the symbol. Such relocations call to this routine,
826 * which searches the symbol list for the symbol in question.
828 * R_386_GOT32 references require the _exact_ symbol address to be
829 * used; R_386_32 references can be at an offset from the symbol.
830 * The boolean argument `exact' tells us this.
832 * Return value is the adjusted value of `addr', having become an
833 * offset from the symbol rather than the section. Should always be
834 * zero when returning from an exact call.
836 * Limitation: if you define two symbols at the same place,
837 * confusion will occur.
839 * Inefficiency: we search, currently, using a linked list which
840 * isn't even necessarily sorted.
842 static void elf_add_gsym_reloc(struct Section *sect,
843 int32_t segment, uint64_t offset, int64_t pcrel,
844 int type, bool exact)
846 struct Reloc *r;
847 struct Section *s;
848 struct Symbol *sym;
849 struct rbtree *srb;
850 int i;
853 * First look up the segment/offset pair and find a global
854 * symbol corresponding to it. If it's not one of our segments,
855 * then it must be an external symbol, in which case we're fine
856 * doing a normal elf_add_reloc after first sanity-checking
857 * that the offset from the symbol is zero.
859 s = NULL;
860 for (i = 0; i < nsects; i++)
861 if (segment == sects[i]->index) {
862 s = sects[i];
863 break;
866 if (!s) {
867 if (exact && offset)
868 error(ERR_NONFATAL, "invalid access to an external symbol");
869 else
870 elf_add_reloc(sect, segment, offset - pcrel, type);
871 return;
874 srb = rb_search(s->gsyms, offset);
875 if (!srb || (exact && srb->key != offset)) {
876 error(ERR_NONFATAL, "unable to find a suitable global symbol"
877 " for this reference");
878 return;
880 sym = container_of(srb, struct Symbol, symv);
882 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
883 sect->tail = &r->next;
884 r->next = NULL;
886 r->address = sect->len;
887 r->offset = offset - pcrel - sym->symv.key;
888 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
889 r->type = type;
891 sect->nrelocs++;
894 static void elf_out(int32_t segto, const void *data,
895 enum out_type type, uint64_t size,
896 int32_t segment, int32_t wrt)
898 struct Section *s;
899 int64_t addr, zero;
900 int i;
901 static struct symlininfo sinfo;
903 zero = 0;
905 #if defined(DEBUG) && DEBUG>2
906 if (data) fprintf(stderr,
907 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
908 currentline, type, segment, segto, size, *(int64_t *)data);
909 else fprintf(stderr,
910 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
911 currentline, type, segment, segto, size);
912 #endif
915 * handle absolute-assembly (structure definitions)
917 if (segto == NO_SEG) {
918 if (type != OUT_RESERVE)
919 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
920 " space");
921 return;
924 s = NULL;
925 for (i = 0; i < nsects; i++)
926 if (segto == sects[i]->index) {
927 s = sects[i];
928 break;
930 if (!s) {
931 int tempint; /* ignored */
932 if (segto != elf_section_names(".text", 2, &tempint))
933 error(ERR_PANIC, "strange segment conditions in ELF driver");
934 else {
935 s = sects[nsects - 1];
936 i = nsects - 1;
939 /* invoke current debug_output routine */
940 if (of_elf64.current_dfmt) {
941 sinfo.offset = s->len;
942 sinfo.section = i;
943 sinfo.segto = segto;
944 sinfo.name = s->name;
945 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
947 /* end of debugging stuff */
949 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
950 error(ERR_WARNING, "attempt to initialize memory in"
951 " BSS section `%s': ignored", s->name);
952 s->len += realsize(type, size);
953 return;
956 if (type == OUT_RESERVE) {
957 if (s->type == SHT_PROGBITS) {
958 error(ERR_WARNING, "uninitialized space declared in"
959 " non-BSS section `%s': zeroing", s->name);
960 elf_sect_write(s, NULL, size);
961 } else
962 s->len += size;
963 } else if (type == OUT_RAWDATA) {
964 if (segment != NO_SEG)
965 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
966 elf_sect_write(s, data, size);
967 } else if (type == OUT_ADDRESS) {
968 addr = *(int64_t *)data;
969 if (segment == NO_SEG) {
970 /* Do nothing */
971 } else if (segment % 2) {
972 error(ERR_NONFATAL, "ELF format does not support"
973 " segment base references");
974 } else {
975 if (wrt == NO_SEG) {
976 switch ((int)size) {
977 case 1:
978 elf_add_reloc(s, segment, addr, R_X86_64_8);
979 break;
980 case 2:
981 elf_add_reloc(s, segment, addr, R_X86_64_16);
982 break;
983 case 4:
984 elf_add_reloc(s, segment, addr, R_X86_64_32);
985 break;
986 case 8:
987 elf_add_reloc(s, segment, addr, R_X86_64_64);
988 break;
989 default:
990 error(ERR_PANIC, "internal error elf64-hpa-871");
991 break;
993 addr = 0;
994 } else if (wrt == elf_gotpc_sect + 1) {
996 * The user will supply GOT relative to $$. ELF
997 * will let us have GOT relative to $. So we
998 * need to fix up the data item by $-$$.
1000 addr += s->len;
1001 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1002 addr = 0;
1003 } else if (wrt == elf_gotoff_sect + 1) {
1004 if (size != 8) {
1005 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1006 "references to be qword");
1007 } else {
1008 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1009 addr = 0;
1011 } else if (wrt == elf_got_sect + 1) {
1012 switch ((int)size) {
1013 case 4:
1014 elf_add_gsym_reloc(s, segment, addr, 0,
1015 R_X86_64_GOT32, true);
1016 addr = 0;
1017 break;
1018 case 8:
1019 elf_add_gsym_reloc(s, segment, addr, 0,
1020 R_X86_64_GOT64, true);
1021 addr = 0;
1022 break;
1023 default:
1024 error(ERR_NONFATAL, "invalid ..got reference");
1025 break;
1027 } else if (wrt == elf_sym_sect + 1) {
1028 switch ((int)size) {
1029 case 1:
1030 elf_add_gsym_reloc(s, segment, addr, 0,
1031 R_X86_64_8, false);
1032 addr = 0;
1033 break;
1034 case 2:
1035 elf_add_gsym_reloc(s, segment, addr, 0,
1036 R_X86_64_16, false);
1037 addr = 0;
1038 break;
1039 case 4:
1040 elf_add_gsym_reloc(s, segment, addr, 0,
1041 R_X86_64_32, false);
1042 addr = 0;
1043 break;
1044 case 8:
1045 elf_add_gsym_reloc(s, segment, addr, 0,
1046 R_X86_64_64, false);
1047 addr = 0;
1048 break;
1049 default:
1050 error(ERR_PANIC, "internal error elf64-hpa-903");
1051 break;
1053 } else if (wrt == elf_plt_sect + 1) {
1054 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1055 "relative PLT references");
1056 } else {
1057 error(ERR_NONFATAL, "ELF format does not support this"
1058 " use of WRT");
1061 elf_sect_writeaddr(s, addr, size);
1062 } else if (type == OUT_REL2ADR) {
1063 addr = *(int64_t *)data - size;
1064 if (segment == segto)
1065 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
1066 if (segment == NO_SEG) {
1067 /* Do nothing */
1068 } else if (segment % 2) {
1069 error(ERR_NONFATAL, "ELF format does not support"
1070 " segment base references");
1071 } else {
1072 if (wrt == NO_SEG) {
1073 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
1074 addr = 0;
1075 } else {
1076 error(ERR_NONFATAL,
1077 "Unsupported non-32-bit ELF relocation [2]");
1080 elf_sect_writeaddr(s, addr, 2);
1081 } else if (type == OUT_REL4ADR) {
1082 addr = *(int64_t *)data - size;
1083 if (segment == segto)
1084 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1085 if (segment == NO_SEG) {
1086 /* Do nothing */
1087 } else if (segment % 2) {
1088 error(ERR_NONFATAL, "ELF64 format does not support"
1089 " segment base references");
1090 } else {
1091 if (wrt == NO_SEG) {
1092 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1093 addr = 0;
1094 } else if (wrt == elf_plt_sect + 1) {
1095 elf_add_gsym_reloc(s, segment, addr+size, size,
1096 R_X86_64_PLT32, true);
1097 addr = 0;
1098 } else if (wrt == elf_gotpc_sect + 1 ||
1099 wrt == elf_got_sect + 1) {
1100 elf_add_gsym_reloc(s, segment, addr+size, size,
1101 R_X86_64_GOTPCREL, true);
1102 addr = 0;
1103 } else if (wrt == elf_gotoff_sect + 1 ||
1104 wrt == elf_got_sect + 1) {
1105 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1106 "qword absolute");
1107 } else if (wrt == elf_gottpoff_sect + 1) {
1108 elf_add_gsym_reloc(s, segment, addr+size, size,
1109 R_X86_64_GOTTPOFF, true);
1110 addr = 0;
1111 } else {
1112 error(ERR_NONFATAL, "ELF64 format does not support this"
1113 " use of WRT");
1116 elf_sect_writeaddr(s, addr, 4);
1117 } else if (type == OUT_REL8ADR) {
1118 addr = *(int64_t *)data - size;
1119 if (segment == segto)
1120 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1121 if (segment == NO_SEG) {
1122 /* Do nothing */
1123 } else if (segment % 2) {
1124 error(ERR_NONFATAL, "ELF64 format does not support"
1125 " segment base references");
1126 } else {
1127 if (wrt == NO_SEG) {
1128 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1129 addr = 0;
1130 } else if (wrt == elf_gotpc_sect + 1 ||
1131 wrt == elf_got_sect + 1) {
1132 elf_add_gsym_reloc(s, segment, addr+size, size,
1133 R_X86_64_GOTPCREL64, true);
1134 addr = 0;
1135 } else if (wrt == elf_gotoff_sect + 1 ||
1136 wrt == elf_got_sect + 1) {
1137 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1138 "absolute");
1139 } else if (wrt == elf_gottpoff_sect + 1) {
1140 error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1141 "dword");
1142 } else {
1143 error(ERR_NONFATAL, "ELF64 format does not support this"
1144 " use of WRT");
1147 elf_sect_writeaddr(s, addr, 8);
1151 static void elf_write(void)
1153 int align;
1154 int scount;
1155 char *p;
1156 int commlen;
1157 char comment[64];
1158 int i;
1160 struct SAA *symtab;
1161 int32_t symtablen, symtablocal;
1164 * Work out how many sections we will have. We have SHN_UNDEF,
1165 * then the flexible user sections, then the four fixed
1166 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1167 * then optionally relocation sections for the user sections.
1169 if (of_elf64.current_dfmt == &df_stabs)
1170 nsections = 8;
1171 else if (of_elf64.current_dfmt == &df_dwarf)
1172 nsections = 15;
1173 else
1174 nsections = 5; /* SHN_UNDEF and the fixed ones */
1176 add_sectname("", ".comment");
1177 add_sectname("", ".shstrtab");
1178 add_sectname("", ".symtab");
1179 add_sectname("", ".strtab");
1180 for (i = 0; i < nsects; i++) {
1181 nsections++; /* for the section itself */
1182 if (sects[i]->head) {
1183 nsections++; /* for its relocations */
1184 add_sectname(".rela", sects[i]->name);
1188 if (of_elf64.current_dfmt == &df_stabs) {
1189 /* in case the debug information is wanted, just add these three sections... */
1190 add_sectname("", ".stab");
1191 add_sectname("", ".stabstr");
1192 add_sectname(".rel", ".stab");
1195 else if (of_elf64.current_dfmt == &df_dwarf) {
1196 /* the dwarf debug standard specifies the following ten sections,
1197 not all of which are currently implemented,
1198 although all of them are defined. */
1199 #define debug_aranges (int64_t) (nsections-10)
1200 #define debug_info (int64_t) (nsections-7)
1201 #define debug_abbrev (int64_t) (nsections-5)
1202 #define debug_line (int64_t) (nsections-4)
1203 add_sectname("", ".debug_aranges");
1204 add_sectname(".rela", ".debug_aranges");
1205 add_sectname("", ".debug_pubnames");
1206 add_sectname("", ".debug_info");
1207 add_sectname(".rela", ".debug_info");
1208 add_sectname("", ".debug_abbrev");
1209 add_sectname("", ".debug_line");
1210 add_sectname(".rela", ".debug_line");
1211 add_sectname("", ".debug_frame");
1212 add_sectname("", ".debug_loc");
1216 * Do the comment.
1218 *comment = '\0';
1219 commlen = 2 + snprintf(comment+1, sizeof comment-1, "%s", nasm_comment);
1222 * Output the ELF header.
1224 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1225 fputc(elf_osabi, elffp);
1226 fputc(elf_abiver, elffp);
1227 fwritezero(7, elffp);
1228 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1229 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1230 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1231 fwriteint64_t(0L, elffp); /* no entry point */
1232 fwriteint64_t(0L, elffp); /* no program header table */
1233 fwriteint64_t(0x40L, elffp); /* section headers straight after
1234 * ELF header plus alignment */
1235 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1236 fwriteint16_t(0x40, elffp); /* size of ELF header */
1237 fwriteint16_t(0, elffp); /* no program header table, again */
1238 fwriteint16_t(0, elffp); /* still no program header table */
1239 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1240 fwriteint16_t(nsections, elffp); /* number of sections */
1241 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1242 * section header table */
1245 * Build the symbol table and relocation tables.
1247 symtab = elf_build_symtab(&symtablen, &symtablocal);
1248 for (i = 0; i < nsects; i++)
1249 if (sects[i]->head)
1250 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1251 sects[i]->head);
1254 * Now output the section header table.
1257 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1258 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1259 elf_foffs += align;
1260 elf_nsect = 0;
1261 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1262 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1263 scount = 1; /* needed for the stabs debugging to track the symtable section */
1264 p = shstrtab + 1;
1265 for (i = 0; i < nsects; i++) {
1266 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1267 (sects[i]->type == SHT_PROGBITS ?
1268 sects[i]->data : NULL), true,
1269 sects[i]->len, 0, 0, sects[i]->align, 0);
1270 p += strlen(p) + 1;
1271 scount++; /* ditto */
1273 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1274 scount++; /* ditto */
1275 p += strlen(p) + 1;
1276 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1277 scount++; /* ditto */
1278 p += strlen(p) + 1;
1279 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1280 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1281 p += strlen(p) + 1;
1282 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1283 for (i = 0; i < nsects; i++)
1284 if (sects[i]->head) {
1285 p += strlen(p) + 1;
1286 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1287 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1289 if (of_elf64.current_dfmt == &df_stabs) {
1290 /* for debugging information, create the last three sections
1291 which are the .stab , .stabstr and .rel.stab sections respectively */
1293 /* this function call creates the stab sections in memory */
1294 stabs64_generate();
1296 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1297 p += strlen(p) + 1;
1298 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1299 nsections - 2, 0, 4, 12);
1301 p += strlen(p) + 1;
1302 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1303 stabstrlen, 0, 0, 4, 0);
1305 p += strlen(p) + 1;
1306 /* link -> symtable info -> section to refer to */
1307 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1308 stabrellen, symtabsection, nsections - 3, 4,
1309 16);
1312 else if (of_elf64.current_dfmt == &df_dwarf) {
1313 /* for dwarf debugging information, create the ten dwarf sections */
1315 /* this function call creates the dwarf sections in memory */
1316 if (dwarf_fsect) dwarf64_generate();
1318 p += strlen(p) + 1;
1319 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1320 arangeslen, 0, 0, 1, 0);
1321 p += strlen(p) + 1;
1322 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1323 arangesrellen, symtabsection, debug_aranges, 1, 24);
1324 p += strlen(p) + 1;
1325 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1326 pubnameslen, 0, 0, 1, 0);
1327 p += strlen(p) + 1;
1328 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1329 infolen, 0, 0, 1, 0);
1330 p += strlen(p) + 1;
1331 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1332 inforellen, symtabsection, debug_info, 1, 24);
1333 p += strlen(p) + 1;
1334 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1335 abbrevlen, 0, 0, 1, 0);
1336 p += strlen(p) + 1;
1337 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1338 linelen, 0, 0, 1, 0);
1339 p += strlen(p) + 1;
1340 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1341 linerellen, symtabsection, debug_line, 1, 24);
1342 p += strlen(p) + 1;
1343 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1344 framelen, 0, 0, 8, 0);
1345 p += strlen(p) + 1;
1346 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1347 loclen, 0, 0, 1, 0);
1350 fwritezero(align, elffp);
1353 * Now output the sections.
1355 elf_write_sections();
1357 nasm_free(elf_sects);
1358 saa_free(symtab);
1361 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1363 struct SAA *s = saa_init(1L);
1364 struct Symbol *sym;
1365 uint8_t entry[24], *p;
1366 int i;
1368 *len = *local = 0;
1371 * First, an all-zeros entry, required by the ELF spec.
1373 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1374 *len += 24;
1375 (*local)++;
1378 * Next, an entry for the file name.
1380 p = entry;
1381 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1382 WRITESHORT(p, STT_FILE); /* type FILE */
1383 WRITESHORT(p, SHN_ABS);
1384 WRITEDLONG(p, (uint64_t) 0); /* no value */
1385 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1386 saa_wbytes(s, entry, 24L);
1387 *len += 24;
1388 (*local)++;
1391 * Now some standard symbols defining the segments, for relocation
1392 * purposes.
1394 for (i = 1; i <= nsects; i++) {
1395 p = entry;
1396 WRITELONG(p, 0); /* no symbol name */
1397 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1398 WRITESHORT(p, i); /* section id */
1399 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1400 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1401 saa_wbytes(s, entry, 24L);
1402 *len += 24;
1403 (*local)++;
1408 * Now the other local symbols.
1410 saa_rewind(syms);
1411 while ((sym = saa_rstruct(syms))) {
1412 if (sym->type & SYM_GLOBAL)
1413 continue;
1414 p = entry;
1415 WRITELONG(p, sym->strpos); /* index into symbol string table */
1416 WRITECHAR(p, sym->type); /* type and binding */
1417 WRITECHAR(p, sym->other); /* visibility */
1418 WRITESHORT(p, sym->section); /* index into section header table */
1419 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1420 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1421 saa_wbytes(s, entry, 24L);
1422 *len += 24;
1423 (*local)++;
1426 * dwarf needs symbols for debug sections
1427 * which are relocation targets.
1429 if (of_elf64.current_dfmt == &df_dwarf) {
1430 dwarf_infosym = *local;
1431 p = entry;
1432 WRITELONG(p, 0); /* no symbol name */
1433 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1434 WRITESHORT(p, debug_info); /* section id */
1435 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1436 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1437 saa_wbytes(s, entry, 24L);
1438 *len += 24;
1439 (*local)++;
1440 dwarf_abbrevsym = *local;
1441 p = entry;
1442 WRITELONG(p, 0); /* no symbol name */
1443 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1444 WRITESHORT(p, debug_abbrev); /* section id */
1445 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1446 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1447 saa_wbytes(s, entry, 24L);
1448 *len += 24;
1449 (*local)++;
1450 dwarf_linesym = *local;
1451 p = entry;
1452 WRITELONG(p, 0); /* no symbol name */
1453 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1454 WRITESHORT(p, debug_line); /* section id */
1455 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1456 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1457 saa_wbytes(s, entry, 24L);
1458 *len += 24;
1459 (*local)++;
1463 * Now the global symbols.
1465 saa_rewind(syms);
1466 while ((sym = saa_rstruct(syms))) {
1467 if (!(sym->type & SYM_GLOBAL))
1468 continue;
1469 p = entry;
1470 WRITELONG(p, sym->strpos);
1471 WRITECHAR(p, sym->type); /* type and binding */
1472 WRITECHAR(p, sym->other); /* visibility */
1473 WRITESHORT(p, sym->section);
1474 WRITEDLONG(p, (int64_t)sym->symv.key);
1475 WRITEDLONG(p, (int64_t)sym->size);
1476 saa_wbytes(s, entry, 24L);
1477 *len += 24;
1480 return s;
1483 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1485 struct SAA *s;
1486 uint8_t *p, entry[24];
1488 if (!r)
1489 return NULL;
1491 s = saa_init(1L);
1492 *len = 0;
1494 while (r) {
1495 int64_t sym = r->symbol;
1497 if (sym >= GLOBAL_TEMP_BASE)
1499 if (of_elf64.current_dfmt == &df_dwarf)
1500 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1501 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1503 p = entry;
1504 WRITEDLONG(p, r->address);
1505 WRITEDLONG(p, (sym << 32) + r->type);
1506 WRITEDLONG(p, r->offset);
1507 saa_wbytes(s, entry, 24L);
1508 *len += 24;
1510 r = r->next;
1513 return s;
1516 static void elf_section_header(int name, int type, uint64_t flags,
1517 void *data, bool is_saa, uint64_t datalen,
1518 int link, int info, int align, int eltsize)
1520 elf_sects[elf_nsect].data = data;
1521 elf_sects[elf_nsect].len = datalen;
1522 elf_sects[elf_nsect].is_saa = is_saa;
1523 elf_nsect++;
1525 fwriteint32_t((int32_t)name, elffp);
1526 fwriteint32_t((int32_t)type, elffp);
1527 fwriteint64_t((int64_t)flags, elffp);
1528 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1529 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1530 fwriteint64_t(datalen, elffp);
1531 if (data)
1532 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1533 fwriteint32_t((int32_t)link, elffp);
1534 fwriteint32_t((int32_t)info, elffp);
1535 fwriteint64_t((int64_t)align, elffp);
1536 fwriteint64_t((int64_t)eltsize, elffp);
1539 static void elf_write_sections(void)
1541 int i;
1542 for (i = 0; i < elf_nsect; i++)
1543 if (elf_sects[i].data) {
1544 int32_t len = elf_sects[i].len;
1545 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1546 int32_t align = reallen - len;
1547 if (elf_sects[i].is_saa)
1548 saa_fpwrite(elf_sects[i].data, elffp);
1549 else
1550 fwrite(elf_sects[i].data, len, 1, elffp);
1551 fwritezero(align, elffp);
1555 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1557 saa_wbytes(sect->data, data, len);
1558 sect->len += len;
1560 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1562 saa_writeaddr(sect->data, data, len);
1563 sect->len += len;
1566 static int32_t elf_segbase(int32_t segment)
1568 return segment;
1571 static int elf_directive(char *directive, char *value, int pass)
1573 bool err;
1574 int64_t n;
1575 char *p;
1577 if (!strcmp(directive, "osabi")) {
1578 if (pass == 2)
1579 return 1; /* ignore in pass 2 */
1581 n = readnum(value, &err);
1582 if (err) {
1583 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1584 return 1;
1586 if (n < 0 || n > 255) {
1587 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1588 return 1;
1590 elf_osabi = n;
1591 elf_abiver = 0;
1593 if ((p = strchr(value,',')) == NULL)
1594 return 1;
1596 n = readnum(p+1, &err);
1597 if (err || n < 0 || n > 255) {
1598 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1599 return 1;
1602 elf_abiver = n;
1603 return 1;
1606 return 0;
1609 static void elf_filename(char *inname, char *outname, efunc error)
1611 strcpy(elf_module, inname);
1612 standard_extension(inname, outname, ".o", error);
1615 extern macros_t elf_stdmac[];
1617 static int elf_set_info(enum geninfo type, char **val)
1619 (void)type;
1620 (void)val;
1621 return 0;
1623 static struct dfmt df_dwarf = {
1624 "ELF64 (X86_64) dwarf debug format for Linux",
1625 "dwarf",
1626 debug64_init,
1627 dwarf64_linenum,
1628 debug64_deflabel,
1629 debug64_directive,
1630 debug64_typevalue,
1631 dwarf64_output,
1632 dwarf64_cleanup
1634 static struct dfmt df_stabs = {
1635 "ELF64 (X86_64) stabs debug format for Linux",
1636 "stabs",
1637 debug64_init,
1638 stabs64_linenum,
1639 debug64_deflabel,
1640 debug64_directive,
1641 debug64_typevalue,
1642 stabs64_output,
1643 stabs64_cleanup
1646 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1648 struct ofmt of_elf64 = {
1649 "ELF64 (x86_64) object files (e.g. Linux)",
1650 "elf64",
1651 NULL,
1652 elf64_debugs_arr,
1653 &df_stabs,
1654 elf_stdmac,
1655 elf_init,
1656 elf_set_info,
1657 elf_out,
1658 elf_deflabel,
1659 elf_section_names,
1660 elf_segbase,
1661 elf_directive,
1662 elf_filename,
1663 elf_cleanup
1666 /* common debugging routines */
1667 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1669 (void)of;
1670 (void)id;
1671 (void)fp;
1672 (void)error;
1674 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1675 char *special)
1677 (void)name;
1678 (void)segment;
1679 (void)offset;
1680 (void)is_global;
1681 (void)special;
1684 void debug64_directive(const char *directive, const char *params)
1686 (void)directive;
1687 (void)params;
1690 void debug64_typevalue(int32_t type)
1692 int32_t stype, ssize;
1693 switch (TYM_TYPE(type)) {
1694 case TY_LABEL:
1695 ssize = 0;
1696 stype = STT_NOTYPE;
1697 break;
1698 case TY_BYTE:
1699 ssize = 1;
1700 stype = STT_OBJECT;
1701 break;
1702 case TY_WORD:
1703 ssize = 2;
1704 stype = STT_OBJECT;
1705 break;
1706 case TY_DWORD:
1707 ssize = 4;
1708 stype = STT_OBJECT;
1709 break;
1710 case TY_FLOAT:
1711 ssize = 4;
1712 stype = STT_OBJECT;
1713 break;
1714 case TY_QWORD:
1715 ssize = 8;
1716 stype = STT_OBJECT;
1717 break;
1718 case TY_TBYTE:
1719 ssize = 10;
1720 stype = STT_OBJECT;
1721 break;
1722 case TY_OWORD:
1723 ssize = 16;
1724 stype = STT_OBJECT;
1725 break;
1726 case TY_COMMON:
1727 ssize = 0;
1728 stype = STT_COMMON;
1729 break;
1730 case TY_SEG:
1731 ssize = 0;
1732 stype = STT_SECTION;
1733 break;
1734 case TY_EXTERN:
1735 ssize = 0;
1736 stype = STT_NOTYPE;
1737 break;
1738 case TY_EQU:
1739 ssize = 0;
1740 stype = STT_NOTYPE;
1741 break;
1742 default:
1743 ssize = 0;
1744 stype = STT_NOTYPE;
1745 break;
1747 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1748 lastsym->size = ssize;
1749 lastsym->type = stype;
1753 /* stabs debugging routines */
1756 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1758 (void)segto;
1759 if (!stabs_filename) {
1760 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1761 strcpy(stabs_filename, filename);
1762 } else {
1763 if (strcmp(stabs_filename, filename)) {
1764 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1765 in fact, this leak comes in quite handy to maintain a list of files
1766 encountered so far in the symbol lines... */
1768 /* why not nasm_free(stabs_filename); we're done with the old one */
1770 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1771 strcpy(stabs_filename, filename);
1774 debug_immcall = 1;
1775 currentline = linenumber;
1779 void stabs64_output(int type, void *param)
1781 struct symlininfo *s;
1782 struct linelist *el;
1783 if (type == TY_DEBUGSYMLIN) {
1784 if (debug_immcall) {
1785 s = (struct symlininfo *)param;
1786 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1787 return; /* line info is only collected for executable sections */
1788 numlinestabs++;
1789 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1790 el->info.offset = s->offset;
1791 el->info.section = s->section;
1792 el->info.name = s->name;
1793 el->line = currentline;
1794 el->filename = stabs_filename;
1795 el->next = 0;
1796 if (stabslines) {
1797 stabslines->last->next = el;
1798 stabslines->last = el;
1799 } else {
1800 stabslines = el;
1801 stabslines->last = el;
1805 debug_immcall = 0;
1808 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1809 do {\
1810 WRITELONG(p,n_strx); \
1811 WRITECHAR(p,n_type); \
1812 WRITECHAR(p,n_other); \
1813 WRITESHORT(p,n_desc); \
1814 WRITELONG(p,n_value); \
1815 } while (0)
1817 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1819 void stabs64_generate(void)
1821 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1822 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1823 char **allfiles;
1824 int *fileidx;
1826 struct linelist *ptr;
1828 ptr = stabslines;
1830 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1831 for (i = 0; i < numlinestabs; i++)
1832 allfiles[i] = 0;
1833 numfiles = 0;
1834 while (ptr) {
1835 if (numfiles == 0) {
1836 allfiles[0] = ptr->filename;
1837 numfiles++;
1838 } else {
1839 for (i = 0; i < numfiles; i++) {
1840 if (!strcmp(allfiles[i], ptr->filename))
1841 break;
1843 if (i >= numfiles) {
1844 allfiles[i] = ptr->filename;
1845 numfiles++;
1848 ptr = ptr->next;
1850 strsize = 1;
1851 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1852 for (i = 0; i < numfiles; i++) {
1853 fileidx[i] = strsize;
1854 strsize += strlen(allfiles[i]) + 1;
1856 mainfileindex = 0;
1857 for (i = 0; i < numfiles; i++) {
1858 if (!strcmp(allfiles[i], elf_module)) {
1859 mainfileindex = i;
1860 break;
1864 /* worst case size of the stab buffer would be:
1865 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1867 sbuf =
1868 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1869 sizeof(struct stabentry));
1871 ssbuf = (uint8_t *)nasm_malloc(strsize);
1873 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1874 rptr = rbuf;
1876 for (i = 0; i < numfiles; i++) {
1877 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1879 ssbuf[0] = 0;
1881 stabstrlen = strsize; /* set global variable for length of stab strings */
1883 sptr = sbuf;
1884 ptr = stabslines;
1885 numstabs = 0;
1887 if (ptr) {
1888 /* this is the first stab, its strx points to the filename of the
1889 the source-file, the n_desc field should be set to the number
1890 of remaining stabs
1892 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1894 /* this is the stab for the main source file */
1895 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1897 /* relocation table entry */
1899 /* Since the symbol table has two entries before */
1900 /* the section symbols, the index in the info.section */
1901 /* member must be adjusted by adding 2 */
1903 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1904 WRITELONG(rptr, R_X86_64_32);
1905 WRITELONG(rptr, ptr->info.section + 2);
1907 numstabs++;
1908 currfile = mainfileindex;
1911 while (ptr) {
1912 if (strcmp(allfiles[currfile], ptr->filename)) {
1913 /* oops file has changed... */
1914 for (i = 0; i < numfiles; i++)
1915 if (!strcmp(allfiles[i], ptr->filename))
1916 break;
1917 currfile = i;
1918 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1919 ptr->info.offset);
1920 numstabs++;
1922 /* relocation table entry */
1924 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1925 WRITELONG(rptr, R_X86_64_32);
1926 WRITELONG(rptr, ptr->info.section + 2);
1929 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1930 numstabs++;
1932 /* relocation table entry */
1934 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1935 WRITELONG(rptr, R_X86_64_32);
1936 WRITELONG(rptr, ptr->info.section + 2);
1938 ptr = ptr->next;
1942 ((struct stabentry *)sbuf)->n_desc = numstabs;
1944 nasm_free(allfiles);
1945 nasm_free(fileidx);
1947 stablen = (sptr - sbuf);
1948 stabrellen = (rptr - rbuf);
1949 stabrelbuf = rbuf;
1950 stabbuf = sbuf;
1951 stabstrbuf = ssbuf;
1954 void stabs64_cleanup(void)
1956 struct linelist *ptr, *del;
1957 if (!stabslines)
1958 return;
1959 ptr = stabslines;
1960 while (ptr) {
1961 del = ptr;
1962 ptr = ptr->next;
1963 nasm_free(del);
1965 if (stabbuf)
1966 nasm_free(stabbuf);
1967 if (stabrelbuf)
1968 nasm_free(stabrelbuf);
1969 if (stabstrbuf)
1970 nasm_free(stabstrbuf);
1972 /* dwarf routines */
1975 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1977 (void)segto;
1978 dwarf64_findfile(filename);
1979 debug_immcall = 1;
1980 currentline = linenumber;
1983 /* called from elf_out with type == TY_DEBUGSYMLIN */
1984 void dwarf64_output(int type, void *param)
1986 int ln, aa, inx, maxln, soc;
1987 struct symlininfo *s;
1988 struct SAA *plinep;
1990 (void)type;
1992 s = (struct symlininfo *)param;
1993 /* line number info is only gathered for executable sections */
1994 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1995 return;
1996 /* Check if section index has changed */
1997 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1999 dwarf64_findsect(s->section);
2001 /* do nothing unless line or file has changed */
2002 if (debug_immcall)
2004 ln = currentline - dwarf_csect->line;
2005 aa = s->offset - dwarf_csect->offset;
2006 inx = dwarf_clist->line;
2007 plinep = dwarf_csect->psaa;
2008 /* check for file change */
2009 if (!(inx == dwarf_csect->file))
2011 saa_write8(plinep,DW_LNS_set_file);
2012 saa_write8(plinep,inx);
2013 dwarf_csect->file = inx;
2015 /* check for line change */
2016 if (ln)
2018 /* test if in range of special op code */
2019 maxln = line_base + line_range;
2020 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2021 if (ln >= line_base && ln < maxln && soc < 256)
2023 saa_write8(plinep,soc);
2025 else
2027 if (ln)
2029 saa_write8(plinep,DW_LNS_advance_line);
2030 saa_wleb128s(plinep,ln);
2032 if (aa)
2034 saa_write8(plinep,DW_LNS_advance_pc);
2035 saa_wleb128u(plinep,aa);
2038 dwarf_csect->line = currentline;
2039 dwarf_csect->offset = s->offset;
2041 /* show change handled */
2042 debug_immcall = 0;
2047 void dwarf64_generate(void)
2049 uint8_t *pbuf;
2050 int indx;
2051 struct linelist *ftentry;
2052 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2053 struct SAA *parangesrel, *plinesrel, *pinforel;
2054 struct sectlist *psect;
2055 size_t saalen, linepoff, totlen, highaddr;
2057 /* write epilogues for each line program range */
2058 /* and build aranges section */
2059 paranges = saa_init(1L);
2060 parangesrel = saa_init(1L);
2061 saa_write16(paranges,3); /* dwarf version */
2062 saa_write64(parangesrel, paranges->datalen+4);
2063 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2064 saa_write64(parangesrel, 0);
2065 saa_write32(paranges,0); /* offset into info */
2066 saa_write8(paranges,8); /* pointer size */
2067 saa_write8(paranges,0); /* not segmented */
2068 saa_write32(paranges,0); /* padding */
2069 /* iterate though sectlist entries */
2070 psect = dwarf_fsect;
2071 totlen = 0;
2072 highaddr = 0;
2073 for (indx = 0; indx < dwarf_nsections; indx++)
2075 plinep = psect->psaa;
2076 /* Line Number Program Epilogue */
2077 saa_write8(plinep,2); /* std op 2 */
2078 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2079 saa_write8(plinep,DW_LNS_extended_op);
2080 saa_write8(plinep,1); /* operand length */
2081 saa_write8(plinep,DW_LNE_end_sequence);
2082 totlen += plinep->datalen;
2083 /* range table relocation entry */
2084 saa_write64(parangesrel, paranges->datalen + 4);
2085 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2086 saa_write64(parangesrel, (uint64_t) 0);
2087 /* range table entry */
2088 saa_write64(paranges,0x0000); /* range start */
2089 saa_write64(paranges,sects[psect->section]->len); /* range length */
2090 highaddr += sects[psect->section]->len;
2091 /* done with this entry */
2092 psect = psect->next;
2094 saa_write64(paranges,0); /* null address */
2095 saa_write64(paranges,0); /* null length */
2096 saalen = paranges->datalen;
2097 arangeslen = saalen + 4;
2098 arangesbuf = pbuf = nasm_malloc(arangeslen);
2099 WRITELONG(pbuf,saalen); /* initial length */
2100 saa_rnbytes(paranges, pbuf, saalen);
2101 saa_free(paranges);
2103 /* build rela.aranges section */
2104 arangesrellen = saalen = parangesrel->datalen;
2105 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2106 saa_rnbytes(parangesrel, pbuf, saalen);
2107 saa_free(parangesrel);
2109 /* build pubnames section */
2110 ppubnames = saa_init(1L);
2111 saa_write16(ppubnames,3); /* dwarf version */
2112 saa_write32(ppubnames,0); /* offset into info */
2113 saa_write32(ppubnames,0); /* space used in info */
2114 saa_write32(ppubnames,0); /* end of list */
2115 saalen = ppubnames->datalen;
2116 pubnameslen = saalen + 4;
2117 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2118 WRITELONG(pbuf,saalen); /* initial length */
2119 saa_rnbytes(ppubnames, pbuf, saalen);
2120 saa_free(ppubnames);
2122 /* build info section */
2123 pinfo = saa_init(1L);
2124 pinforel = saa_init(1L);
2125 saa_write16(pinfo,3); /* dwarf version */
2126 saa_write64(pinforel, pinfo->datalen + 4);
2127 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2128 saa_write64(pinforel, 0);
2129 saa_write32(pinfo,0); /* offset into abbrev */
2130 saa_write8(pinfo,8); /* pointer size */
2131 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2132 saa_write64(pinforel, pinfo->datalen + 4);
2133 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2134 saa_write64(pinforel, 0);
2135 saa_write64(pinfo,0); /* DW_AT_low_pc */
2136 saa_write64(pinforel, pinfo->datalen + 4);
2137 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2138 saa_write64(pinforel, 0);
2139 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2140 saa_write64(pinforel, pinfo->datalen + 4);
2141 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2142 saa_write64(pinforel, 0);
2143 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2144 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2145 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2146 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2147 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2148 saa_write64(pinforel, pinfo->datalen + 4);
2149 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2150 saa_write64(pinforel, 0);
2151 saa_write64(pinfo,0); /* DW_AT_low_pc */
2152 saa_write64(pinfo,0); /* DW_AT_frame_base */
2153 saa_write8(pinfo,0); /* end of entries */
2154 saalen = pinfo->datalen;
2155 infolen = saalen + 4;
2156 infobuf = pbuf = nasm_malloc(infolen);
2157 WRITELONG(pbuf,saalen); /* initial length */
2158 saa_rnbytes(pinfo, pbuf, saalen);
2159 saa_free(pinfo);
2161 /* build rela.info section */
2162 inforellen = saalen = pinforel->datalen;
2163 inforelbuf = pbuf = nasm_malloc(inforellen);
2164 saa_rnbytes(pinforel, pbuf, saalen);
2165 saa_free(pinforel);
2167 /* build abbrev section */
2168 pabbrev = saa_init(1L);
2169 saa_write8(pabbrev,1); /* entry number LEB128u */
2170 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2171 saa_write8(pabbrev,1); /* has children */
2172 /* the following attributes and forms are all LEB128u values */
2173 saa_write8(pabbrev,DW_AT_low_pc);
2174 saa_write8(pabbrev,DW_FORM_addr);
2175 saa_write8(pabbrev,DW_AT_high_pc);
2176 saa_write8(pabbrev,DW_FORM_addr);
2177 saa_write8(pabbrev,DW_AT_stmt_list);
2178 saa_write8(pabbrev,DW_FORM_data4);
2179 saa_write8(pabbrev,DW_AT_name);
2180 saa_write8(pabbrev,DW_FORM_string);
2181 saa_write8(pabbrev,DW_AT_producer);
2182 saa_write8(pabbrev,DW_FORM_string);
2183 saa_write8(pabbrev,DW_AT_language);
2184 saa_write8(pabbrev,DW_FORM_data2);
2185 saa_write16(pabbrev,0); /* end of entry */
2186 /* LEB128u usage same as above */
2187 saa_write8(pabbrev,2); /* entry number */
2188 saa_write8(pabbrev,DW_TAG_subprogram);
2189 saa_write8(pabbrev,0); /* no children */
2190 saa_write8(pabbrev,DW_AT_low_pc);
2191 saa_write8(pabbrev,DW_FORM_addr);
2192 saa_write8(pabbrev,DW_AT_frame_base);
2193 saa_write8(pabbrev,DW_FORM_data4);
2194 saa_write16(pabbrev,0); /* end of entry */
2195 abbrevlen = saalen = pabbrev->datalen;
2196 abbrevbuf = pbuf = nasm_malloc(saalen);
2197 saa_rnbytes(pabbrev, pbuf, saalen);
2198 saa_free(pabbrev);
2200 /* build line section */
2201 /* prolog */
2202 plines = saa_init(1L);
2203 saa_write8(plines,1); /* Minimum Instruction Length */
2204 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2205 saa_write8(plines,line_base); /* Line Base */
2206 saa_write8(plines,line_range); /* Line Range */
2207 saa_write8(plines,opcode_base); /* Opcode Base */
2208 /* standard opcode lengths (# of LEB128u operands) */
2209 saa_write8(plines,0); /* Std opcode 1 length */
2210 saa_write8(plines,1); /* Std opcode 2 length */
2211 saa_write8(plines,1); /* Std opcode 3 length */
2212 saa_write8(plines,1); /* Std opcode 4 length */
2213 saa_write8(plines,1); /* Std opcode 5 length */
2214 saa_write8(plines,0); /* Std opcode 6 length */
2215 saa_write8(plines,0); /* Std opcode 7 length */
2216 saa_write8(plines,0); /* Std opcode 8 length */
2217 saa_write8(plines,1); /* Std opcode 9 length */
2218 saa_write8(plines,0); /* Std opcode 10 length */
2219 saa_write8(plines,0); /* Std opcode 11 length */
2220 saa_write8(plines,1); /* Std opcode 12 length */
2221 /* Directory Table */
2222 saa_write8(plines,0); /* End of table */
2223 /* File Name Table */
2224 ftentry = dwarf_flist;
2225 for (indx = 0;indx<dwarf_numfiles;indx++)
2227 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2228 saa_write8(plines,0); /* directory LEB128u */
2229 saa_write8(plines,0); /* time LEB128u */
2230 saa_write8(plines,0); /* size LEB128u */
2231 ftentry = ftentry->next;
2233 saa_write8(plines,0); /* End of table */
2234 linepoff = plines->datalen;
2235 linelen = linepoff + totlen + 10;
2236 linebuf = pbuf = nasm_malloc(linelen);
2237 WRITELONG(pbuf,linelen-4); /* initial length */
2238 WRITESHORT(pbuf,3); /* dwarf version */
2239 WRITELONG(pbuf,linepoff); /* offset to line number program */
2240 /* write line header */
2241 saalen = linepoff;
2242 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2243 pbuf += linepoff;
2244 saa_free(plines);
2245 /* concatonate line program ranges */
2246 linepoff += 13;
2247 plinesrel = saa_init(1L);
2248 psect = dwarf_fsect;
2249 for (indx = 0; indx < dwarf_nsections; indx++)
2251 saa_write64(plinesrel, linepoff);
2252 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2253 saa_write64(plinesrel, (uint64_t) 0);
2254 plinep = psect->psaa;
2255 saalen = plinep->datalen;
2256 saa_rnbytes(plinep, pbuf, saalen);
2257 pbuf += saalen;
2258 linepoff += saalen;
2259 saa_free(plinep);
2260 /* done with this entry */
2261 psect = psect->next;
2265 /* build rela.lines section */
2266 linerellen =saalen = plinesrel->datalen;
2267 linerelbuf = pbuf = nasm_malloc(linerellen);
2268 saa_rnbytes(plinesrel, pbuf, saalen);
2269 saa_free(plinesrel);
2271 /* build frame section */
2272 framelen = 4;
2273 framebuf = pbuf = nasm_malloc(framelen);
2274 WRITELONG(pbuf,framelen-4); /* initial length */
2276 /* build loc section */
2277 loclen = 16;
2278 locbuf = pbuf = nasm_malloc(loclen);
2279 WRITEDLONG(pbuf,0); /* null beginning offset */
2280 WRITEDLONG(pbuf,0); /* null ending offset */
2283 void dwarf64_cleanup(void)
2285 if (arangesbuf)
2286 nasm_free(arangesbuf);
2287 if (arangesrelbuf)
2288 nasm_free(arangesrelbuf);
2289 if (pubnamesbuf)
2290 nasm_free(pubnamesbuf);
2291 if (infobuf)
2292 nasm_free(infobuf);
2293 if (inforelbuf)
2294 nasm_free(inforelbuf);
2295 if (abbrevbuf)
2296 nasm_free(abbrevbuf);
2297 if (linebuf)
2298 nasm_free(linebuf);
2299 if (linerelbuf)
2300 nasm_free(linerelbuf);
2301 if (framebuf)
2302 nasm_free(framebuf);
2303 if (locbuf)
2304 nasm_free(locbuf);
2306 void dwarf64_findfile(const char * fname)
2308 int finx;
2309 struct linelist *match;
2311 /* return if fname is current file name */
2312 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2313 /* search for match */
2314 else
2316 match = 0;
2317 if (dwarf_flist)
2319 match = dwarf_flist;
2320 for (finx = 0; finx < dwarf_numfiles; finx++)
2322 if (!(strcmp(fname, match->filename)))
2324 dwarf_clist = match;
2325 return;
2329 /* add file name to end of list */
2330 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2331 dwarf_numfiles++;
2332 dwarf_clist->line = dwarf_numfiles;
2333 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2334 strcpy(dwarf_clist->filename,fname);
2335 dwarf_clist->next = 0;
2336 /* if first entry */
2337 if (!dwarf_flist)
2339 dwarf_flist = dwarf_elist = dwarf_clist;
2340 dwarf_clist->last = 0;
2342 /* chain to previous entry */
2343 else
2345 dwarf_elist->next = dwarf_clist;
2346 dwarf_elist = dwarf_clist;
2350 /* */
2351 void dwarf64_findsect(const int index)
2353 int sinx;
2354 struct sectlist *match;
2355 struct SAA *plinep;
2356 /* return if index is current section index */
2357 if (dwarf_csect && (dwarf_csect->section == index))
2359 return;
2361 /* search for match */
2362 else
2364 match = 0;
2365 if (dwarf_fsect)
2367 match = dwarf_fsect;
2368 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2370 if ((match->section == index))
2372 dwarf_csect = match;
2373 return;
2375 match = match->next;
2378 /* add entry to end of list */
2379 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2380 dwarf_nsections++;
2381 dwarf_csect->psaa = plinep = saa_init(1L);
2382 dwarf_csect->line = 1;
2383 dwarf_csect->offset = 0;
2384 dwarf_csect->file = 1;
2385 dwarf_csect->section = index;
2386 dwarf_csect->next = 0;
2387 /* set relocatable address at start of line program */
2388 saa_write8(plinep,DW_LNS_extended_op);
2389 saa_write8(plinep,9); /* operand length */
2390 saa_write8(plinep,DW_LNE_set_address);
2391 saa_write64(plinep,0); /* Start Address */
2392 /* if first entry */
2393 if (!dwarf_fsect)
2395 dwarf_fsect = dwarf_esect = dwarf_csect;
2396 dwarf_csect->last = 0;
2398 /* chain to previous entry */
2399 else
2401 dwarf_esect->next = dwarf_csect;
2402 dwarf_esect = dwarf_csect;
2407 #endif /* OF_ELF */