Fix opcode for VADDSUBPS; operands for VBLEND; add SSE for AES ops
[nasm/avx512.git] / output / outelf64.c
bloba12559e8c809c3e1a883a6eb881059182fadfb5d
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 const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
220 static struct ELF_SECTDATA {
221 void *data;
222 int64_t len;
223 bool is_saa;
224 } *elf_sects;
225 static int elf_nsect, nsections;
226 static int64_t elf_foffs;
228 static void elf_write(void);
229 static void elf_sect_write(struct Section *, const void *, size_t);
230 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
231 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
232 int, int);
233 static void elf_write_sections(void);
234 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
235 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
236 static void add_sectname(char *, char *);
238 /* type values for stabs debugging sections */
239 #define N_SO 0x64 /* ID for main source file */
240 #define N_SOL 0x84 /* ID for sub-source file */
241 #define N_BINCL 0x82 /* not currently used */
242 #define N_EINCL 0xA2 /* not currently used */
243 #define N_SLINE 0x44
245 struct stabentry {
246 uint32_t n_strx;
247 uint8_t n_type;
248 uint8_t n_other;
249 uint16_t n_desc;
250 uint32_t n_value;
253 struct erel {
254 int offset, info;
257 struct symlininfo {
258 int offset;
259 int section; /* index into sects[] */
260 int segto; /* internal section number */
261 char *name; /* shallow-copied pointer of section name */
264 struct linelist {
265 struct symlininfo info;
266 int line;
267 char *filename;
268 struct linelist *next;
269 struct linelist *last;
272 struct sectlist {
273 struct SAA *psaa;
274 int section;
275 int line;
276 int offset;
277 int file;
278 struct sectlist *next;
279 struct sectlist *last;
282 /* common debug variables */
283 static int currentline = 1;
284 static int debug_immcall = 0;
286 /* stabs debug variables */
287 static struct linelist *stabslines = 0;
288 static int numlinestabs = 0;
289 static char *stabs_filename = 0;
290 static int symtabsection;
291 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
292 static int stablen, stabstrlen, stabrellen;
294 /* dwarf debug variables */
295 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
296 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
297 static int dwarf_numfiles = 0, dwarf_nsections;
298 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
299 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
300 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
301 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
302 abbrevlen, linelen, linerellen, framelen, loclen;
303 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
306 static struct dfmt df_dwarf;
307 static struct dfmt df_stabs;
308 static struct Symbol *lastsym;
310 /* common debugging routines */
311 void debug64_typevalue(int32_t);
312 void debug64_init(struct ofmt *, void *, FILE *, efunc);
313 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
314 void debug64_directive(const char *, const char *);
316 /* stabs debugging routines */
317 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
318 void stabs64_output(int, void *);
319 void stabs64_generate(void);
320 void stabs64_cleanup(void);
322 /* dwarf debugging routines */
323 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
324 void dwarf64_output(int, void *);
325 void dwarf64_generate(void);
326 void dwarf64_cleanup(void);
327 void dwarf64_findfile(const char *);
328 void dwarf64_findsect(const int);
331 * Special section numbers which are used to define ELF special
332 * symbols, which can be used with WRT to provide PIC relocation
333 * types.
335 static int32_t elf_gotpc_sect, elf_gotoff_sect;
336 static int32_t elf_got_sect, elf_plt_sect;
337 static int32_t elf_sym_sect;
338 static int32_t elf_gottpoff_sect;
340 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
342 maxbits = 64;
343 elffp = fp;
344 error = errfunc;
345 evaluate = eval;
346 (void)ldef; /* placate optimisers */
347 sects = NULL;
348 nsects = sectlen = 0;
349 syms = saa_init((int32_t)sizeof(struct Symbol));
350 nlocals = nglobs = 0;
351 bsym = raa_init();
352 strs = saa_init(1L);
353 saa_wbytes(strs, "\0", 1L);
354 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
355 strslen = 2 + strlen(elf_module);
356 shstrtab = NULL;
357 shstrtablen = shstrtabsize = 0;;
358 add_sectname("", "");
360 fwds = NULL;
362 elf_gotpc_sect = seg_alloc();
363 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
364 error);
365 elf_gotoff_sect = seg_alloc();
366 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
367 error);
368 elf_got_sect = seg_alloc();
369 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
370 error);
371 elf_plt_sect = seg_alloc();
372 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
373 error);
374 elf_sym_sect = seg_alloc();
375 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
376 error);
377 elf_gottpoff_sect = seg_alloc();
378 ldef("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false, &of_elf64,
379 error);
381 def_seg = seg_alloc();
385 static void elf_cleanup(int debuginfo)
387 struct Reloc *r;
388 int i;
390 (void)debuginfo;
392 elf_write();
393 fclose(elffp);
394 for (i = 0; i < nsects; i++) {
395 if (sects[i]->type != SHT_NOBITS)
396 saa_free(sects[i]->data);
397 if (sects[i]->head)
398 saa_free(sects[i]->rel);
399 while (sects[i]->head) {
400 r = sects[i]->head;
401 sects[i]->head = sects[i]->head->next;
402 nasm_free(r);
405 nasm_free(sects);
406 saa_free(syms);
407 raa_free(bsym);
408 saa_free(strs);
409 if (of_elf64.current_dfmt) {
410 of_elf64.current_dfmt->cleanup();
413 /* add entry to the elf .shstrtab section */
414 static void add_sectname(char *firsthalf, char *secondhalf)
416 int len = strlen(firsthalf) + strlen(secondhalf);
417 while (shstrtablen + len + 1 > shstrtabsize)
418 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
419 strcpy(shstrtab + shstrtablen, firsthalf);
420 strcat(shstrtab + shstrtablen, secondhalf);
421 shstrtablen += len + 1;
424 static int elf_make_section(char *name, int type, int flags, int align)
426 struct Section *s;
428 s = nasm_malloc(sizeof(*s));
430 if (type != SHT_NOBITS)
431 s->data = saa_init(1L);
432 s->head = NULL;
433 s->tail = &s->head;
434 s->len = s->size = 0;
435 s->nrelocs = 0;
436 if (!strcmp(name, ".text"))
437 s->index = def_seg;
438 else
439 s->index = seg_alloc();
440 add_sectname("", name);
441 s->name = nasm_malloc(1 + strlen(name));
442 strcpy(s->name, name);
443 s->type = type;
444 s->flags = flags;
445 s->align = align;
446 s->gsyms = NULL;
448 if (nsects >= sectlen)
449 sects =
450 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
451 sects[nsects++] = s;
453 return nsects - 1;
456 static int32_t elf_section_names(char *name, int pass, int *bits)
458 char *p;
459 unsigned flags_and, flags_or;
460 uint64_t type, align;
461 int i;
464 * Default is 64 bits.
466 if (!name) {
467 *bits = 64;
468 return def_seg;
471 p = name;
472 while (*p && !nasm_isspace(*p))
473 p++;
474 if (*p)
475 *p++ = '\0';
476 flags_and = flags_or = type = align = 0;
478 while (*p && nasm_isspace(*p))
479 p++;
480 while (*p) {
481 char *q = p;
482 while (*p && !nasm_isspace(*p))
483 p++;
484 if (*p)
485 *p++ = '\0';
486 while (*p && nasm_isspace(*p))
487 p++;
489 if (!nasm_strnicmp(q, "align=", 6)) {
490 align = atoi(q + 6);
491 if (align == 0)
492 align = 1;
493 if ((align - 1) & align) { /* means it's not a power of two */
494 error(ERR_NONFATAL, "section alignment %d is not"
495 " a power of two", align);
496 align = 1;
498 } else if (!nasm_stricmp(q, "alloc")) {
499 flags_and |= SHF_ALLOC;
500 flags_or |= SHF_ALLOC;
501 } else if (!nasm_stricmp(q, "noalloc")) {
502 flags_and |= SHF_ALLOC;
503 flags_or &= ~SHF_ALLOC;
504 } else if (!nasm_stricmp(q, "exec")) {
505 flags_and |= SHF_EXECINSTR;
506 flags_or |= SHF_EXECINSTR;
507 } else if (!nasm_stricmp(q, "noexec")) {
508 flags_and |= SHF_EXECINSTR;
509 flags_or &= ~SHF_EXECINSTR;
510 } else if (!nasm_stricmp(q, "write")) {
511 flags_and |= SHF_WRITE;
512 flags_or |= SHF_WRITE;
513 } else if (!nasm_stricmp(q, "tls")) {
514 flags_and |= SHF_TLS;
515 flags_or |= SHF_TLS;
516 } else if (!nasm_stricmp(q, "nowrite")) {
517 flags_and |= SHF_WRITE;
518 flags_or &= ~SHF_WRITE;
519 } else if (!nasm_stricmp(q, "progbits")) {
520 type = SHT_PROGBITS;
521 } else if (!nasm_stricmp(q, "nobits")) {
522 type = SHT_NOBITS;
523 } else if (pass == 1) error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
524 " declaration of section `%s'", q, name);
527 if (!strcmp(name, ".comment") ||
528 !strcmp(name, ".shstrtab") ||
529 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
530 error(ERR_NONFATAL, "attempt to redefine reserved section"
531 "name `%s'", name);
532 return NO_SEG;
535 for (i = 0; i < nsects; i++)
536 if (!strcmp(name, sects[i]->name))
537 break;
538 if (i == nsects) {
539 if (!strcmp(name, ".text"))
540 i = elf_make_section(name, SHT_PROGBITS,
541 SHF_ALLOC | SHF_EXECINSTR, 16);
542 else if (!strcmp(name, ".rodata"))
543 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
544 else if (!strcmp(name, ".data"))
545 i = elf_make_section(name, SHT_PROGBITS,
546 SHF_ALLOC | SHF_WRITE, 4);
547 else if (!strcmp(name, ".bss"))
548 i = elf_make_section(name, SHT_NOBITS,
549 SHF_ALLOC | SHF_WRITE, 4);
550 else if (!strcmp(name, ".tdata"))
551 i = elf_make_section(name, SHT_PROGBITS,
552 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
553 else if (!strcmp(name, ".tbss"))
554 i = elf_make_section(name, SHT_NOBITS,
555 SHF_ALLOC | SHF_WRITE | SHF_TLS, 4);
556 else
557 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
558 if (type)
559 sects[i]->type = type;
560 if (align)
561 sects[i]->align = align;
562 sects[i]->flags &= ~flags_and;
563 sects[i]->flags |= flags_or;
564 } else if (pass == 1) {
565 if ((type && sects[i]->type != type)
566 || (align && sects[i]->align != align)
567 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
568 error(ERR_WARNING, "incompatible section attributes ignored on"
569 " redeclaration of section `%s'", name);
572 return sects[i]->index;
575 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
576 int is_global, char *special)
578 int pos = strslen;
579 struct Symbol *sym;
580 bool special_used = false;
582 #if defined(DEBUG) && DEBUG>2
583 fprintf(stderr,
584 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
585 name, segment, offset, is_global, special);
586 #endif
587 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
589 * This is a NASM special symbol. We never allow it into
590 * the ELF symbol table, even if it's a valid one. If it
591 * _isn't_ a valid one, we should barf immediately.
593 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
594 strcmp(name, "..got") && strcmp(name, "..plt") &&
595 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
596 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
597 return;
600 if (is_global == 3) {
601 struct Symbol **s;
603 * Fix up a forward-reference symbol size from the first
604 * pass.
606 for (s = &fwds; *s; s = &(*s)->nextfwd)
607 if (!strcmp((*s)->name, name)) {
608 struct tokenval tokval;
609 expr *e;
610 char *p = special;
612 while (*p && !nasm_isspace(*p))
613 p++;
614 while (*p && nasm_isspace(*p))
615 p++;
616 stdscan_reset();
617 stdscan_bufptr = p;
618 tokval.t_type = TOKEN_INVALID;
619 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
620 if (e) {
621 if (!is_simple(e))
622 error(ERR_NONFATAL, "cannot use relocatable"
623 " expression as symbol size");
624 else
625 (*s)->size = reloc_value(e);
629 * Remove it from the list of unresolved sizes.
631 nasm_free((*s)->name);
632 *s = (*s)->nextfwd;
633 return;
635 return; /* it wasn't an important one */
638 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
639 strslen += 1 + strlen(name);
641 lastsym = sym = saa_wstruct(syms);
643 sym->strpos = pos;
644 sym->type = is_global ? SYM_GLOBAL : 0;
645 sym->other = STV_DEFAULT;
646 sym->size = 0;
647 if (segment == NO_SEG)
648 sym->section = SHN_ABS;
649 else {
650 int i;
651 sym->section = SHN_UNDEF;
652 if (nsects == 0 && segment == def_seg) {
653 int tempint;
654 if (segment != elf_section_names(".text", 2, &tempint))
655 error(ERR_PANIC,
656 "strange segment conditions in ELF driver");
657 sym->section = nsects;
658 } else {
659 for (i = 0; i < nsects; i++)
660 if (segment == sects[i]->index) {
661 sym->section = i + 1;
662 break;
667 if (is_global == 2) {
668 sym->size = offset;
669 sym->symv.key = 0;
670 sym->section = SHN_COMMON;
672 * We have a common variable. Check the special text to see
673 * if it's a valid number and power of two; if so, store it
674 * as the alignment for the common variable.
676 if (special) {
677 bool err;
678 sym->symv.key = readnum(special, &err);
679 if (err)
680 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
681 " valid number", special);
682 else if ((sym->symv.key | (sym->symv.key - 1))
683 != 2 * sym->symv.key - 1)
684 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
685 " power of two", special);
687 special_used = true;
688 } else
689 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
691 if (sym->type == SYM_GLOBAL) {
693 * If sym->section == SHN_ABS, then the first line of the
694 * else section would cause a core dump, because its a reference
695 * beyond the end of the section array.
696 * This behaviour is exhibited by this code:
697 * GLOBAL crash_nasm
698 * crash_nasm equ 0
699 * To avoid such a crash, such requests are silently discarded.
700 * This may not be the best solution.
702 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
703 bsym = raa_write(bsym, segment, nglobs);
704 } else if (sym->section != SHN_ABS) {
706 * This is a global symbol; so we must add it to the rbtree
707 * of global symbols in its section.
709 * In addition, we check the special text for symbol
710 * type and size information.
712 sects[sym->section-1]->gsyms =
713 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
715 if (special) {
716 int n = strcspn(special, " \t");
718 if (!nasm_strnicmp(special, "function", n))
719 sym->type |= STT_FUNC;
720 else if (!nasm_strnicmp(special, "data", n) ||
721 !nasm_strnicmp(special, "object", n))
722 sym->type |= STT_OBJECT;
723 else if (!nasm_strnicmp(special, "notype", n))
724 sym->type |= STT_NOTYPE;
725 else
726 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
727 n, special);
728 special += n;
730 while (nasm_isspace(*special))
731 ++special;
732 if (*special) {
733 n = strcspn(special, " \t");
734 if (!nasm_strnicmp(special, "default", n))
735 sym->other = STV_DEFAULT;
736 else if (!nasm_strnicmp(special, "internal", n))
737 sym->other = STV_INTERNAL;
738 else if (!nasm_strnicmp(special, "hidden", n))
739 sym->other = STV_HIDDEN;
740 else if (!nasm_strnicmp(special, "protected", n))
741 sym->other = STV_PROTECTED;
742 else
743 n = 0;
744 special += n;
747 if (*special) {
748 struct tokenval tokval;
749 expr *e;
750 int fwd = 0;
751 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
753 while (special[n] && nasm_isspace(special[n]))
754 n++;
756 * We have a size expression; attempt to
757 * evaluate it.
759 stdscan_reset();
760 stdscan_bufptr = special + n;
761 tokval.t_type = TOKEN_INVALID;
762 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
763 NULL);
764 if (fwd) {
765 sym->nextfwd = fwds;
766 fwds = sym;
767 sym->name = nasm_strdup(name);
768 } else if (e) {
769 if (!is_simple(e))
770 error(ERR_NONFATAL, "cannot use relocatable"
771 " expression as symbol size");
772 else
773 sym->size = reloc_value(e);
775 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
777 special_used = true;
780 * If TLS segment, mark symbol accordingly.
782 if (sects[sym->section - 1]->flags & SHF_TLS) {
783 sym->type &= 0xf0;
784 sym->type |= STT_TLS;
787 sym->globnum = nglobs;
788 nglobs++;
789 } else
790 nlocals++;
792 if (special && !special_used)
793 error(ERR_NONFATAL, "no special symbol features supported here");
796 static void elf_add_reloc(struct Section *sect, int32_t segment,
797 int64_t offset, int type)
799 struct Reloc *r;
800 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
801 sect->tail = &r->next;
802 r->next = NULL;
804 r->address = sect->len;
805 r->offset = offset;
806 if (segment == NO_SEG)
807 r->symbol = 0;
808 else {
809 int i;
810 r->symbol = 0;
811 for (i = 0; i < nsects; i++)
812 if (segment == sects[i]->index)
813 r->symbol = i + 2;
814 if (!r->symbol)
815 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
817 r->type = type;
819 sect->nrelocs++;
823 * This routine deals with ..got and ..sym relocations: the more
824 * complicated kinds. In shared-library writing, some relocations
825 * with respect to global symbols must refer to the precise symbol
826 * rather than referring to an offset from the base of the section
827 * _containing_ the symbol. Such relocations call to this routine,
828 * which searches the symbol list for the symbol in question.
830 * R_386_GOT32 references require the _exact_ symbol address to be
831 * used; R_386_32 references can be at an offset from the symbol.
832 * The boolean argument `exact' tells us this.
834 * Return value is the adjusted value of `addr', having become an
835 * offset from the symbol rather than the section. Should always be
836 * zero when returning from an exact call.
838 * Limitation: if you define two symbols at the same place,
839 * confusion will occur.
841 * Inefficiency: we search, currently, using a linked list which
842 * isn't even necessarily sorted.
844 static void elf_add_gsym_reloc(struct Section *sect,
845 int32_t segment, uint64_t offset, int64_t pcrel,
846 int type, bool exact)
848 struct Reloc *r;
849 struct Section *s;
850 struct Symbol *sym;
851 struct rbtree *srb;
852 int i;
855 * First look up the segment/offset pair and find a global
856 * symbol corresponding to it. If it's not one of our segments,
857 * then it must be an external symbol, in which case we're fine
858 * doing a normal elf_add_reloc after first sanity-checking
859 * that the offset from the symbol is zero.
861 s = NULL;
862 for (i = 0; i < nsects; i++)
863 if (segment == sects[i]->index) {
864 s = sects[i];
865 break;
868 if (!s) {
869 if (exact && offset)
870 error(ERR_NONFATAL, "invalid access to an external symbol");
871 else
872 elf_add_reloc(sect, segment, offset - pcrel, type);
873 return;
876 srb = rb_search(s->gsyms, offset);
877 if (!srb || (exact && srb->key != offset)) {
878 error(ERR_NONFATAL, "unable to find a suitable global symbol"
879 " for this reference");
880 return;
882 sym = container_of(srb, struct Symbol, symv);
884 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
885 sect->tail = &r->next;
886 r->next = NULL;
888 r->address = sect->len;
889 r->offset = offset - pcrel - sym->symv.key;
890 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
891 r->type = type;
893 sect->nrelocs++;
896 static void elf_out(int32_t segto, const void *data,
897 enum out_type type, uint64_t size,
898 int32_t segment, int32_t wrt)
900 struct Section *s;
901 int64_t addr, zero;
902 int i;
903 static struct symlininfo sinfo;
905 zero = 0;
907 #if defined(DEBUG) && DEBUG>2
908 if (data) fprintf(stderr,
909 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
910 currentline, type, segment, segto, size, *(int64_t *)data);
911 else fprintf(stderr,
912 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
913 currentline, type, segment, segto, size);
914 #endif
917 * handle absolute-assembly (structure definitions)
919 if (segto == NO_SEG) {
920 if (type != OUT_RESERVE)
921 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
922 " space");
923 return;
926 s = NULL;
927 for (i = 0; i < nsects; i++)
928 if (segto == sects[i]->index) {
929 s = sects[i];
930 break;
932 if (!s) {
933 int tempint; /* ignored */
934 if (segto != elf_section_names(".text", 2, &tempint))
935 error(ERR_PANIC, "strange segment conditions in ELF driver");
936 else {
937 s = sects[nsects - 1];
938 i = nsects - 1;
941 /* invoke current debug_output routine */
942 if (of_elf64.current_dfmt) {
943 sinfo.offset = s->len;
944 sinfo.section = i;
945 sinfo.segto = segto;
946 sinfo.name = s->name;
947 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
949 /* end of debugging stuff */
951 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
952 error(ERR_WARNING, "attempt to initialize memory in"
953 " BSS section `%s': ignored", s->name);
954 s->len += realsize(type, size);
955 return;
958 if (type == OUT_RESERVE) {
959 if (s->type == SHT_PROGBITS) {
960 error(ERR_WARNING, "uninitialized space declared in"
961 " non-BSS section `%s': zeroing", s->name);
962 elf_sect_write(s, NULL, size);
963 } else
964 s->len += size;
965 } else if (type == OUT_RAWDATA) {
966 if (segment != NO_SEG)
967 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
968 elf_sect_write(s, data, size);
969 } else if (type == OUT_ADDRESS) {
970 addr = *(int64_t *)data;
971 if (segment == NO_SEG) {
972 /* Do nothing */
973 } else if (segment % 2) {
974 error(ERR_NONFATAL, "ELF format does not support"
975 " segment base references");
976 } else {
977 if (wrt == NO_SEG) {
978 switch ((int)size) {
979 case 1:
980 elf_add_reloc(s, segment, addr, R_X86_64_8);
981 break;
982 case 2:
983 elf_add_reloc(s, segment, addr, R_X86_64_16);
984 break;
985 case 4:
986 elf_add_reloc(s, segment, addr, R_X86_64_32);
987 break;
988 case 8:
989 elf_add_reloc(s, segment, addr, R_X86_64_64);
990 break;
991 default:
992 error(ERR_PANIC, "internal error elf64-hpa-871");
993 break;
995 addr = 0;
996 } else if (wrt == elf_gotpc_sect + 1) {
998 * The user will supply GOT relative to $$. ELF
999 * will let us have GOT relative to $. So we
1000 * need to fix up the data item by $-$$.
1002 addr += s->len;
1003 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1004 addr = 0;
1005 } else if (wrt == elf_gotoff_sect + 1) {
1006 if (size != 8) {
1007 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1008 "references to be qword");
1009 } else {
1010 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1011 addr = 0;
1013 } else if (wrt == elf_got_sect + 1) {
1014 switch ((int)size) {
1015 case 4:
1016 elf_add_gsym_reloc(s, segment, addr, 0,
1017 R_X86_64_GOT32, true);
1018 addr = 0;
1019 break;
1020 case 8:
1021 elf_add_gsym_reloc(s, segment, addr, 0,
1022 R_X86_64_GOT64, true);
1023 addr = 0;
1024 break;
1025 default:
1026 error(ERR_NONFATAL, "invalid ..got reference");
1027 break;
1029 } else if (wrt == elf_sym_sect + 1) {
1030 switch ((int)size) {
1031 case 1:
1032 elf_add_gsym_reloc(s, segment, addr, 0,
1033 R_X86_64_8, false);
1034 addr = 0;
1035 break;
1036 case 2:
1037 elf_add_gsym_reloc(s, segment, addr, 0,
1038 R_X86_64_16, false);
1039 addr = 0;
1040 break;
1041 case 4:
1042 elf_add_gsym_reloc(s, segment, addr, 0,
1043 R_X86_64_32, false);
1044 addr = 0;
1045 break;
1046 case 8:
1047 elf_add_gsym_reloc(s, segment, addr, 0,
1048 R_X86_64_64, false);
1049 addr = 0;
1050 break;
1051 default:
1052 error(ERR_PANIC, "internal error elf64-hpa-903");
1053 break;
1055 } else if (wrt == elf_plt_sect + 1) {
1056 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1057 "relative PLT references");
1058 } else {
1059 error(ERR_NONFATAL, "ELF format does not support this"
1060 " use of WRT");
1063 elf_sect_writeaddr(s, addr, size);
1064 } else if (type == OUT_REL2ADR) {
1065 addr = *(int64_t *)data - size;
1066 if (segment == segto)
1067 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
1068 if (segment == NO_SEG) {
1069 /* Do nothing */
1070 } else if (segment % 2) {
1071 error(ERR_NONFATAL, "ELF format does not support"
1072 " segment base references");
1073 } else {
1074 if (wrt == NO_SEG) {
1075 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
1076 addr = 0;
1077 } else {
1078 error(ERR_NONFATAL,
1079 "Unsupported non-32-bit ELF relocation [2]");
1082 elf_sect_writeaddr(s, addr, 2);
1083 } else if (type == OUT_REL4ADR) {
1084 addr = *(int64_t *)data - size;
1085 if (segment == segto)
1086 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1087 if (segment == NO_SEG) {
1088 /* Do nothing */
1089 } else if (segment % 2) {
1090 error(ERR_NONFATAL, "ELF64 format does not support"
1091 " segment base references");
1092 } else {
1093 if (wrt == NO_SEG) {
1094 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1095 addr = 0;
1096 } else if (wrt == elf_plt_sect + 1) {
1097 elf_add_gsym_reloc(s, segment, addr+size, size,
1098 R_X86_64_PLT32, true);
1099 addr = 0;
1100 } else if (wrt == elf_gotpc_sect + 1 ||
1101 wrt == elf_got_sect + 1) {
1102 elf_add_gsym_reloc(s, segment, addr+size, size,
1103 R_X86_64_GOTPCREL, true);
1104 addr = 0;
1105 } else if (wrt == elf_gotoff_sect + 1 ||
1106 wrt == elf_got_sect + 1) {
1107 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1108 "qword absolute");
1109 } else if (wrt == elf_gottpoff_sect + 1) {
1110 elf_add_gsym_reloc(s, segment, addr+size, size,
1111 R_X86_64_GOTTPOFF, true);
1112 addr = 0;
1113 } else {
1114 error(ERR_NONFATAL, "ELF64 format does not support this"
1115 " use of WRT");
1118 elf_sect_writeaddr(s, addr, 4);
1119 } else if (type == OUT_REL8ADR) {
1120 addr = *(int64_t *)data - size;
1121 if (segment == segto)
1122 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1123 if (segment == NO_SEG) {
1124 /* Do nothing */
1125 } else if (segment % 2) {
1126 error(ERR_NONFATAL, "ELF64 format does not support"
1127 " segment base references");
1128 } else {
1129 if (wrt == NO_SEG) {
1130 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1131 addr = 0;
1132 } else if (wrt == elf_gotpc_sect + 1 ||
1133 wrt == elf_got_sect + 1) {
1134 elf_add_gsym_reloc(s, segment, addr+size, size,
1135 R_X86_64_GOTPCREL64, true);
1136 addr = 0;
1137 } else if (wrt == elf_gotoff_sect + 1 ||
1138 wrt == elf_got_sect + 1) {
1139 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1140 "absolute");
1141 } else if (wrt == elf_gottpoff_sect + 1) {
1142 error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1143 "dword");
1144 } else {
1145 error(ERR_NONFATAL, "ELF64 format does not support this"
1146 " use of WRT");
1149 elf_sect_writeaddr(s, addr, 8);
1153 static void elf_write(void)
1155 int align;
1156 int scount;
1157 char *p;
1158 int commlen;
1159 char comment[64];
1160 int i;
1162 struct SAA *symtab;
1163 int32_t symtablen, symtablocal;
1166 * Work out how many sections we will have. We have SHN_UNDEF,
1167 * then the flexible user sections, then the four fixed
1168 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1169 * then optionally relocation sections for the user sections.
1171 if (of_elf64.current_dfmt == &df_stabs)
1172 nsections = 8;
1173 else if (of_elf64.current_dfmt == &df_dwarf)
1174 nsections = 15;
1175 else
1176 nsections = 5; /* SHN_UNDEF and the fixed ones */
1178 add_sectname("", ".comment");
1179 add_sectname("", ".shstrtab");
1180 add_sectname("", ".symtab");
1181 add_sectname("", ".strtab");
1182 for (i = 0; i < nsects; i++) {
1183 nsections++; /* for the section itself */
1184 if (sects[i]->head) {
1185 nsections++; /* for its relocations */
1186 add_sectname(".rela", sects[i]->name);
1190 if (of_elf64.current_dfmt == &df_stabs) {
1191 /* in case the debug information is wanted, just add these three sections... */
1192 add_sectname("", ".stab");
1193 add_sectname("", ".stabstr");
1194 add_sectname(".rel", ".stab");
1197 else if (of_elf64.current_dfmt == &df_dwarf) {
1198 /* the dwarf debug standard specifies the following ten sections,
1199 not all of which are currently implemented,
1200 although all of them are defined. */
1201 #define debug_aranges (int64_t) (nsections-10)
1202 #define debug_info (int64_t) (nsections-7)
1203 #define debug_abbrev (int64_t) (nsections-5)
1204 #define debug_line (int64_t) (nsections-4)
1205 add_sectname("", ".debug_aranges");
1206 add_sectname(".rela", ".debug_aranges");
1207 add_sectname("", ".debug_pubnames");
1208 add_sectname("", ".debug_info");
1209 add_sectname(".rela", ".debug_info");
1210 add_sectname("", ".debug_abbrev");
1211 add_sectname("", ".debug_line");
1212 add_sectname(".rela", ".debug_line");
1213 add_sectname("", ".debug_frame");
1214 add_sectname("", ".debug_loc");
1218 * Do the comment.
1220 *comment = '\0';
1221 commlen = 2 + snprintf(comment+1, sizeof comment-1, "%s", nasm_comment);
1224 * Output the ELF header.
1226 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1227 fputc(elf_osabi, elffp);
1228 fputc(elf_abiver, elffp);
1229 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1230 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1231 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1232 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1233 fwriteint64_t(0L, elffp); /* no entry point */
1234 fwriteint64_t(0L, elffp); /* no program header table */
1235 fwriteint64_t(0x40L, elffp); /* section headers straight after
1236 * ELF header plus alignment */
1237 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1238 fwriteint16_t(0x40, elffp); /* size of ELF header */
1239 fwriteint16_t(0, elffp); /* no program header table, again */
1240 fwriteint16_t(0, elffp); /* still no program header table */
1241 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1242 fwriteint16_t(nsections, elffp); /* number of sections */
1243 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1244 * section header table */
1247 * Build the symbol table and relocation tables.
1249 symtab = elf_build_symtab(&symtablen, &symtablocal);
1250 for (i = 0; i < nsects; i++)
1251 if (sects[i]->head)
1252 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1253 sects[i]->head);
1256 * Now output the section header table.
1259 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1260 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1261 elf_foffs += align;
1262 elf_nsect = 0;
1263 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1264 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1265 scount = 1; /* needed for the stabs debugging to track the symtable section */
1266 p = shstrtab + 1;
1267 for (i = 0; i < nsects; i++) {
1268 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1269 (sects[i]->type == SHT_PROGBITS ?
1270 sects[i]->data : NULL), true,
1271 sects[i]->len, 0, 0, sects[i]->align, 0);
1272 p += strlen(p) + 1;
1273 scount++; /* ditto */
1275 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1276 scount++; /* ditto */
1277 p += strlen(p) + 1;
1278 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1279 scount++; /* ditto */
1280 p += strlen(p) + 1;
1281 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1282 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1283 p += strlen(p) + 1;
1284 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1285 for (i = 0; i < nsects; i++)
1286 if (sects[i]->head) {
1287 p += strlen(p) + 1;
1288 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1289 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1291 if (of_elf64.current_dfmt == &df_stabs) {
1292 /* for debugging information, create the last three sections
1293 which are the .stab , .stabstr and .rel.stab sections respectively */
1295 /* this function call creates the stab sections in memory */
1296 stabs64_generate();
1298 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1299 p += strlen(p) + 1;
1300 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1301 nsections - 2, 0, 4, 12);
1303 p += strlen(p) + 1;
1304 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1305 stabstrlen, 0, 0, 4, 0);
1307 p += strlen(p) + 1;
1308 /* link -> symtable info -> section to refer to */
1309 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1310 stabrellen, symtabsection, nsections - 3, 4,
1311 16);
1314 else if (of_elf64.current_dfmt == &df_dwarf) {
1315 /* for dwarf debugging information, create the ten dwarf sections */
1317 /* this function call creates the dwarf sections in memory */
1318 if (dwarf_fsect) dwarf64_generate();
1320 p += strlen(p) + 1;
1321 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1322 arangeslen, 0, 0, 1, 0);
1323 p += strlen(p) + 1;
1324 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1325 arangesrellen, symtabsection, debug_aranges, 1, 24);
1326 p += strlen(p) + 1;
1327 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1328 pubnameslen, 0, 0, 1, 0);
1329 p += strlen(p) + 1;
1330 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1331 infolen, 0, 0, 1, 0);
1332 p += strlen(p) + 1;
1333 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1334 inforellen, symtabsection, debug_info, 1, 24);
1335 p += strlen(p) + 1;
1336 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1337 abbrevlen, 0, 0, 1, 0);
1338 p += strlen(p) + 1;
1339 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1340 linelen, 0, 0, 1, 0);
1341 p += strlen(p) + 1;
1342 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1343 linerellen, symtabsection, debug_line, 1, 24);
1344 p += strlen(p) + 1;
1345 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1346 framelen, 0, 0, 8, 0);
1347 p += strlen(p) + 1;
1348 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1349 loclen, 0, 0, 1, 0);
1352 fwrite(align_str, align, 1, elffp);
1355 * Now output the sections.
1357 elf_write_sections();
1359 nasm_free(elf_sects);
1360 saa_free(symtab);
1363 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1365 struct SAA *s = saa_init(1L);
1366 struct Symbol *sym;
1367 uint8_t entry[24], *p;
1368 int i;
1370 *len = *local = 0;
1373 * First, an all-zeros entry, required by the ELF spec.
1375 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1376 *len += 24;
1377 (*local)++;
1380 * Next, an entry for the file name.
1382 p = entry;
1383 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1384 WRITESHORT(p, STT_FILE); /* type FILE */
1385 WRITESHORT(p, SHN_ABS);
1386 WRITEDLONG(p, (uint64_t) 0); /* no value */
1387 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1388 saa_wbytes(s, entry, 24L);
1389 *len += 24;
1390 (*local)++;
1393 * Now some standard symbols defining the segments, for relocation
1394 * purposes.
1396 for (i = 1; i <= nsects; i++) {
1397 p = entry;
1398 WRITELONG(p, 0); /* no symbol name */
1399 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1400 WRITESHORT(p, i); /* section id */
1401 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1402 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1403 saa_wbytes(s, entry, 24L);
1404 *len += 24;
1405 (*local)++;
1410 * Now the other local symbols.
1412 saa_rewind(syms);
1413 while ((sym = saa_rstruct(syms))) {
1414 if (sym->type & SYM_GLOBAL)
1415 continue;
1416 p = entry;
1417 WRITELONG(p, sym->strpos); /* index into symbol string table */
1418 WRITECHAR(p, sym->type); /* type and binding */
1419 WRITECHAR(p, sym->other); /* visibility */
1420 WRITESHORT(p, sym->section); /* index into section header table */
1421 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1422 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1423 saa_wbytes(s, entry, 24L);
1424 *len += 24;
1425 (*local)++;
1428 * dwarf needs symbols for debug sections
1429 * which are relocation targets.
1431 if (of_elf64.current_dfmt == &df_dwarf) {
1432 dwarf_infosym = *local;
1433 p = entry;
1434 WRITELONG(p, 0); /* no symbol name */
1435 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1436 WRITESHORT(p, debug_info); /* section id */
1437 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1438 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1439 saa_wbytes(s, entry, 24L);
1440 *len += 24;
1441 (*local)++;
1442 dwarf_abbrevsym = *local;
1443 p = entry;
1444 WRITELONG(p, 0); /* no symbol name */
1445 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1446 WRITESHORT(p, debug_abbrev); /* section id */
1447 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1448 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1449 saa_wbytes(s, entry, 24L);
1450 *len += 24;
1451 (*local)++;
1452 dwarf_linesym = *local;
1453 p = entry;
1454 WRITELONG(p, 0); /* no symbol name */
1455 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1456 WRITESHORT(p, debug_line); /* section id */
1457 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1458 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1459 saa_wbytes(s, entry, 24L);
1460 *len += 24;
1461 (*local)++;
1465 * Now the global symbols.
1467 saa_rewind(syms);
1468 while ((sym = saa_rstruct(syms))) {
1469 if (!(sym->type & SYM_GLOBAL))
1470 continue;
1471 p = entry;
1472 WRITELONG(p, sym->strpos);
1473 WRITECHAR(p, sym->type); /* type and binding */
1474 WRITECHAR(p, sym->other); /* visibility */
1475 WRITESHORT(p, sym->section);
1476 WRITEDLONG(p, (int64_t)sym->symv.key);
1477 WRITEDLONG(p, (int64_t)sym->size);
1478 saa_wbytes(s, entry, 24L);
1479 *len += 24;
1482 return s;
1485 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1487 struct SAA *s;
1488 uint8_t *p, entry[24];
1490 if (!r)
1491 return NULL;
1493 s = saa_init(1L);
1494 *len = 0;
1496 while (r) {
1497 int64_t sym = r->symbol;
1499 if (sym >= GLOBAL_TEMP_BASE)
1501 if (of_elf64.current_dfmt == &df_dwarf)
1502 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1503 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1505 p = entry;
1506 WRITEDLONG(p, r->address);
1507 WRITEDLONG(p, (sym << 32) + r->type);
1508 WRITEDLONG(p, r->offset);
1509 saa_wbytes(s, entry, 24L);
1510 *len += 24;
1512 r = r->next;
1515 return s;
1518 static void elf_section_header(int name, int type, uint64_t flags,
1519 void *data, bool is_saa, uint64_t datalen,
1520 int link, int info, int align, int eltsize)
1522 elf_sects[elf_nsect].data = data;
1523 elf_sects[elf_nsect].len = datalen;
1524 elf_sects[elf_nsect].is_saa = is_saa;
1525 elf_nsect++;
1527 fwriteint32_t((int32_t)name, elffp);
1528 fwriteint32_t((int32_t)type, elffp);
1529 fwriteint64_t((int64_t)flags, elffp);
1530 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1531 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1532 fwriteint64_t(datalen, elffp);
1533 if (data)
1534 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1535 fwriteint32_t((int32_t)link, elffp);
1536 fwriteint32_t((int32_t)info, elffp);
1537 fwriteint64_t((int64_t)align, elffp);
1538 fwriteint64_t((int64_t)eltsize, elffp);
1541 static void elf_write_sections(void)
1543 int i;
1544 for (i = 0; i < elf_nsect; i++)
1545 if (elf_sects[i].data) {
1546 int32_t len = elf_sects[i].len;
1547 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1548 int32_t align = reallen - len;
1549 if (elf_sects[i].is_saa)
1550 saa_fpwrite(elf_sects[i].data, elffp);
1551 else
1552 fwrite(elf_sects[i].data, len, 1, elffp);
1553 fwrite(align_str, align, 1, elffp);
1557 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1559 saa_wbytes(sect->data, data, len);
1560 sect->len += len;
1562 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1564 saa_writeaddr(sect->data, data, len);
1565 sect->len += len;
1568 static int32_t elf_segbase(int32_t segment)
1570 return segment;
1573 static int elf_directive(char *directive, char *value, int pass)
1575 bool err;
1576 int64_t n;
1577 char *p;
1579 if (!strcmp(directive, "osabi")) {
1580 if (pass == 2)
1581 return 1; /* ignore in pass 2 */
1583 n = readnum(value, &err);
1584 if (err) {
1585 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1586 return 1;
1588 if (n < 0 || n > 255) {
1589 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1590 return 1;
1592 elf_osabi = n;
1593 elf_abiver = 0;
1595 if ((p = strchr(value,',')) == NULL)
1596 return 1;
1598 n = readnum(p+1, &err);
1599 if (err || n < 0 || n > 255) {
1600 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1601 return 1;
1604 elf_abiver = n;
1605 return 1;
1608 return 0;
1611 static void elf_filename(char *inname, char *outname, efunc error)
1613 strcpy(elf_module, inname);
1614 standard_extension(inname, outname, ".o", error);
1617 extern macros_t elf_stdmac[];
1619 static int elf_set_info(enum geninfo type, char **val)
1621 (void)type;
1622 (void)val;
1623 return 0;
1625 static struct dfmt df_dwarf = {
1626 "ELF64 (X86_64) dwarf debug format for Linux",
1627 "dwarf",
1628 debug64_init,
1629 dwarf64_linenum,
1630 debug64_deflabel,
1631 debug64_directive,
1632 debug64_typevalue,
1633 dwarf64_output,
1634 dwarf64_cleanup
1636 static struct dfmt df_stabs = {
1637 "ELF64 (X86_64) stabs debug format for Linux",
1638 "stabs",
1639 debug64_init,
1640 stabs64_linenum,
1641 debug64_deflabel,
1642 debug64_directive,
1643 debug64_typevalue,
1644 stabs64_output,
1645 stabs64_cleanup
1648 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1650 struct ofmt of_elf64 = {
1651 "ELF64 (x86_64) object files (e.g. Linux)",
1652 "elf64",
1653 NULL,
1654 elf64_debugs_arr,
1655 &df_stabs,
1656 elf_stdmac,
1657 elf_init,
1658 elf_set_info,
1659 elf_out,
1660 elf_deflabel,
1661 elf_section_names,
1662 elf_segbase,
1663 elf_directive,
1664 elf_filename,
1665 elf_cleanup
1668 /* common debugging routines */
1669 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1671 (void)of;
1672 (void)id;
1673 (void)fp;
1674 (void)error;
1676 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1677 char *special)
1679 (void)name;
1680 (void)segment;
1681 (void)offset;
1682 (void)is_global;
1683 (void)special;
1686 void debug64_directive(const char *directive, const char *params)
1688 (void)directive;
1689 (void)params;
1692 void debug64_typevalue(int32_t type)
1694 int32_t stype, ssize;
1695 switch (TYM_TYPE(type)) {
1696 case TY_LABEL:
1697 ssize = 0;
1698 stype = STT_NOTYPE;
1699 break;
1700 case TY_BYTE:
1701 ssize = 1;
1702 stype = STT_OBJECT;
1703 break;
1704 case TY_WORD:
1705 ssize = 2;
1706 stype = STT_OBJECT;
1707 break;
1708 case TY_DWORD:
1709 ssize = 4;
1710 stype = STT_OBJECT;
1711 break;
1712 case TY_FLOAT:
1713 ssize = 4;
1714 stype = STT_OBJECT;
1715 break;
1716 case TY_QWORD:
1717 ssize = 8;
1718 stype = STT_OBJECT;
1719 break;
1720 case TY_TBYTE:
1721 ssize = 10;
1722 stype = STT_OBJECT;
1723 break;
1724 case TY_OWORD:
1725 ssize = 16;
1726 stype = STT_OBJECT;
1727 break;
1728 case TY_COMMON:
1729 ssize = 0;
1730 stype = STT_COMMON;
1731 break;
1732 case TY_SEG:
1733 ssize = 0;
1734 stype = STT_SECTION;
1735 break;
1736 case TY_EXTERN:
1737 ssize = 0;
1738 stype = STT_NOTYPE;
1739 break;
1740 case TY_EQU:
1741 ssize = 0;
1742 stype = STT_NOTYPE;
1743 break;
1744 default:
1745 ssize = 0;
1746 stype = STT_NOTYPE;
1747 break;
1749 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1750 lastsym->size = ssize;
1751 lastsym->type = stype;
1755 /* stabs debugging routines */
1758 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1760 (void)segto;
1761 if (!stabs_filename) {
1762 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1763 strcpy(stabs_filename, filename);
1764 } else {
1765 if (strcmp(stabs_filename, filename)) {
1766 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1767 in fact, this leak comes in quite handy to maintain a list of files
1768 encountered so far in the symbol lines... */
1770 /* why not nasm_free(stabs_filename); we're done with the old one */
1772 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1773 strcpy(stabs_filename, filename);
1776 debug_immcall = 1;
1777 currentline = linenumber;
1781 void stabs64_output(int type, void *param)
1783 struct symlininfo *s;
1784 struct linelist *el;
1785 if (type == TY_DEBUGSYMLIN) {
1786 if (debug_immcall) {
1787 s = (struct symlininfo *)param;
1788 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1789 return; /* line info is only collected for executable sections */
1790 numlinestabs++;
1791 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1792 el->info.offset = s->offset;
1793 el->info.section = s->section;
1794 el->info.name = s->name;
1795 el->line = currentline;
1796 el->filename = stabs_filename;
1797 el->next = 0;
1798 if (stabslines) {
1799 stabslines->last->next = el;
1800 stabslines->last = el;
1801 } else {
1802 stabslines = el;
1803 stabslines->last = el;
1807 debug_immcall = 0;
1810 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1811 do {\
1812 WRITELONG(p,n_strx); \
1813 WRITECHAR(p,n_type); \
1814 WRITECHAR(p,n_other); \
1815 WRITESHORT(p,n_desc); \
1816 WRITELONG(p,n_value); \
1817 } while (0)
1819 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1821 void stabs64_generate(void)
1823 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1824 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1825 char **allfiles;
1826 int *fileidx;
1828 struct linelist *ptr;
1830 ptr = stabslines;
1832 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1833 for (i = 0; i < numlinestabs; i++)
1834 allfiles[i] = 0;
1835 numfiles = 0;
1836 while (ptr) {
1837 if (numfiles == 0) {
1838 allfiles[0] = ptr->filename;
1839 numfiles++;
1840 } else {
1841 for (i = 0; i < numfiles; i++) {
1842 if (!strcmp(allfiles[i], ptr->filename))
1843 break;
1845 if (i >= numfiles) {
1846 allfiles[i] = ptr->filename;
1847 numfiles++;
1850 ptr = ptr->next;
1852 strsize = 1;
1853 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1854 for (i = 0; i < numfiles; i++) {
1855 fileidx[i] = strsize;
1856 strsize += strlen(allfiles[i]) + 1;
1858 mainfileindex = 0;
1859 for (i = 0; i < numfiles; i++) {
1860 if (!strcmp(allfiles[i], elf_module)) {
1861 mainfileindex = i;
1862 break;
1866 /* worst case size of the stab buffer would be:
1867 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1869 sbuf =
1870 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1871 sizeof(struct stabentry));
1873 ssbuf = (uint8_t *)nasm_malloc(strsize);
1875 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1876 rptr = rbuf;
1878 for (i = 0; i < numfiles; i++) {
1879 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1881 ssbuf[0] = 0;
1883 stabstrlen = strsize; /* set global variable for length of stab strings */
1885 sptr = sbuf;
1886 ptr = stabslines;
1887 numstabs = 0;
1889 if (ptr) {
1890 /* this is the first stab, its strx points to the filename of the
1891 the source-file, the n_desc field should be set to the number
1892 of remaining stabs
1894 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1896 /* this is the stab for the main source file */
1897 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1899 /* relocation table entry */
1901 /* Since the symbol table has two entries before */
1902 /* the section symbols, the index in the info.section */
1903 /* member must be adjusted by adding 2 */
1905 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1906 WRITELONG(rptr, R_X86_64_32);
1907 WRITELONG(rptr, ptr->info.section + 2);
1909 numstabs++;
1910 currfile = mainfileindex;
1913 while (ptr) {
1914 if (strcmp(allfiles[currfile], ptr->filename)) {
1915 /* oops file has changed... */
1916 for (i = 0; i < numfiles; i++)
1917 if (!strcmp(allfiles[i], ptr->filename))
1918 break;
1919 currfile = i;
1920 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1921 ptr->info.offset);
1922 numstabs++;
1924 /* relocation table entry */
1926 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1927 WRITELONG(rptr, R_X86_64_32);
1928 WRITELONG(rptr, ptr->info.section + 2);
1931 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1932 numstabs++;
1934 /* relocation table entry */
1936 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1937 WRITELONG(rptr, R_X86_64_32);
1938 WRITELONG(rptr, ptr->info.section + 2);
1940 ptr = ptr->next;
1944 ((struct stabentry *)sbuf)->n_desc = numstabs;
1946 nasm_free(allfiles);
1947 nasm_free(fileidx);
1949 stablen = (sptr - sbuf);
1950 stabrellen = (rptr - rbuf);
1951 stabrelbuf = rbuf;
1952 stabbuf = sbuf;
1953 stabstrbuf = ssbuf;
1956 void stabs64_cleanup(void)
1958 struct linelist *ptr, *del;
1959 if (!stabslines)
1960 return;
1961 ptr = stabslines;
1962 while (ptr) {
1963 del = ptr;
1964 ptr = ptr->next;
1965 nasm_free(del);
1967 if (stabbuf)
1968 nasm_free(stabbuf);
1969 if (stabrelbuf)
1970 nasm_free(stabrelbuf);
1971 if (stabstrbuf)
1972 nasm_free(stabstrbuf);
1974 /* dwarf routines */
1977 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1979 (void)segto;
1980 dwarf64_findfile(filename);
1981 debug_immcall = 1;
1982 currentline = linenumber;
1985 /* called from elf_out with type == TY_DEBUGSYMLIN */
1986 void dwarf64_output(int type, void *param)
1988 int ln, aa, inx, maxln, soc;
1989 struct symlininfo *s;
1990 struct SAA *plinep;
1992 (void)type;
1994 s = (struct symlininfo *)param;
1995 /* line number info is only gathered for executable sections */
1996 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1997 return;
1998 /* Check if section index has changed */
1999 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
2001 dwarf64_findsect(s->section);
2003 /* do nothing unless line or file has changed */
2004 if (debug_immcall)
2006 ln = currentline - dwarf_csect->line;
2007 aa = s->offset - dwarf_csect->offset;
2008 inx = dwarf_clist->line;
2009 plinep = dwarf_csect->psaa;
2010 /* check for file change */
2011 if (!(inx == dwarf_csect->file))
2013 saa_write8(plinep,DW_LNS_set_file);
2014 saa_write8(plinep,inx);
2015 dwarf_csect->file = inx;
2017 /* check for line change */
2018 if (ln)
2020 /* test if in range of special op code */
2021 maxln = line_base + line_range;
2022 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2023 if (ln >= line_base && ln < maxln && soc < 256)
2025 saa_write8(plinep,soc);
2027 else
2029 if (ln)
2031 saa_write8(plinep,DW_LNS_advance_line);
2032 saa_wleb128s(plinep,ln);
2034 if (aa)
2036 saa_write8(plinep,DW_LNS_advance_pc);
2037 saa_wleb128u(plinep,aa);
2040 dwarf_csect->line = currentline;
2041 dwarf_csect->offset = s->offset;
2043 /* show change handled */
2044 debug_immcall = 0;
2049 void dwarf64_generate(void)
2051 uint8_t *pbuf;
2052 int indx;
2053 struct linelist *ftentry;
2054 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2055 struct SAA *parangesrel, *plinesrel, *pinforel;
2056 struct sectlist *psect;
2057 size_t saalen, linepoff, totlen, highaddr;
2059 /* write epilogues for each line program range */
2060 /* and build aranges section */
2061 paranges = saa_init(1L);
2062 parangesrel = saa_init(1L);
2063 saa_write16(paranges,3); /* dwarf version */
2064 saa_write64(parangesrel, paranges->datalen+4);
2065 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2066 saa_write64(parangesrel, 0);
2067 saa_write32(paranges,0); /* offset into info */
2068 saa_write8(paranges,8); /* pointer size */
2069 saa_write8(paranges,0); /* not segmented */
2070 saa_write32(paranges,0); /* padding */
2071 /* iterate though sectlist entries */
2072 psect = dwarf_fsect;
2073 totlen = 0;
2074 highaddr = 0;
2075 for (indx = 0; indx < dwarf_nsections; indx++)
2077 plinep = psect->psaa;
2078 /* Line Number Program Epilogue */
2079 saa_write8(plinep,2); /* std op 2 */
2080 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2081 saa_write8(plinep,DW_LNS_extended_op);
2082 saa_write8(plinep,1); /* operand length */
2083 saa_write8(plinep,DW_LNE_end_sequence);
2084 totlen += plinep->datalen;
2085 /* range table relocation entry */
2086 saa_write64(parangesrel, paranges->datalen + 4);
2087 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2088 saa_write64(parangesrel, (uint64_t) 0);
2089 /* range table entry */
2090 saa_write64(paranges,0x0000); /* range start */
2091 saa_write64(paranges,sects[psect->section]->len); /* range length */
2092 highaddr += sects[psect->section]->len;
2093 /* done with this entry */
2094 psect = psect->next;
2096 saa_write64(paranges,0); /* null address */
2097 saa_write64(paranges,0); /* null length */
2098 saalen = paranges->datalen;
2099 arangeslen = saalen + 4;
2100 arangesbuf = pbuf = nasm_malloc(arangeslen);
2101 WRITELONG(pbuf,saalen); /* initial length */
2102 saa_rnbytes(paranges, pbuf, saalen);
2103 saa_free(paranges);
2105 /* build rela.aranges section */
2106 arangesrellen = saalen = parangesrel->datalen;
2107 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2108 saa_rnbytes(parangesrel, pbuf, saalen);
2109 saa_free(parangesrel);
2111 /* build pubnames section */
2112 ppubnames = saa_init(1L);
2113 saa_write16(ppubnames,3); /* dwarf version */
2114 saa_write32(ppubnames,0); /* offset into info */
2115 saa_write32(ppubnames,0); /* space used in info */
2116 saa_write32(ppubnames,0); /* end of list */
2117 saalen = ppubnames->datalen;
2118 pubnameslen = saalen + 4;
2119 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2120 WRITELONG(pbuf,saalen); /* initial length */
2121 saa_rnbytes(ppubnames, pbuf, saalen);
2122 saa_free(ppubnames);
2124 /* build info section */
2125 pinfo = saa_init(1L);
2126 pinforel = saa_init(1L);
2127 saa_write16(pinfo,3); /* dwarf version */
2128 saa_write64(pinforel, pinfo->datalen + 4);
2129 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2130 saa_write64(pinforel, 0);
2131 saa_write32(pinfo,0); /* offset into abbrev */
2132 saa_write8(pinfo,8); /* pointer size */
2133 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2134 saa_write64(pinforel, pinfo->datalen + 4);
2135 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2136 saa_write64(pinforel, 0);
2137 saa_write64(pinfo,0); /* DW_AT_low_pc */
2138 saa_write64(pinforel, pinfo->datalen + 4);
2139 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2140 saa_write64(pinforel, 0);
2141 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2142 saa_write64(pinforel, pinfo->datalen + 4);
2143 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2144 saa_write64(pinforel, 0);
2145 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2146 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2147 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2148 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2149 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2150 saa_write64(pinforel, pinfo->datalen + 4);
2151 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2152 saa_write64(pinforel, 0);
2153 saa_write64(pinfo,0); /* DW_AT_low_pc */
2154 saa_write64(pinfo,0); /* DW_AT_frame_base */
2155 saa_write8(pinfo,0); /* end of entries */
2156 saalen = pinfo->datalen;
2157 infolen = saalen + 4;
2158 infobuf = pbuf = nasm_malloc(infolen);
2159 WRITELONG(pbuf,saalen); /* initial length */
2160 saa_rnbytes(pinfo, pbuf, saalen);
2161 saa_free(pinfo);
2163 /* build rela.info section */
2164 inforellen = saalen = pinforel->datalen;
2165 inforelbuf = pbuf = nasm_malloc(inforellen);
2166 saa_rnbytes(pinforel, pbuf, saalen);
2167 saa_free(pinforel);
2169 /* build abbrev section */
2170 pabbrev = saa_init(1L);
2171 saa_write8(pabbrev,1); /* entry number LEB128u */
2172 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2173 saa_write8(pabbrev,1); /* has children */
2174 /* the following attributes and forms are all LEB128u values */
2175 saa_write8(pabbrev,DW_AT_low_pc);
2176 saa_write8(pabbrev,DW_FORM_addr);
2177 saa_write8(pabbrev,DW_AT_high_pc);
2178 saa_write8(pabbrev,DW_FORM_addr);
2179 saa_write8(pabbrev,DW_AT_stmt_list);
2180 saa_write8(pabbrev,DW_FORM_data4);
2181 saa_write8(pabbrev,DW_AT_name);
2182 saa_write8(pabbrev,DW_FORM_string);
2183 saa_write8(pabbrev,DW_AT_producer);
2184 saa_write8(pabbrev,DW_FORM_string);
2185 saa_write8(pabbrev,DW_AT_language);
2186 saa_write8(pabbrev,DW_FORM_data2);
2187 saa_write16(pabbrev,0); /* end of entry */
2188 /* LEB128u usage same as above */
2189 saa_write8(pabbrev,2); /* entry number */
2190 saa_write8(pabbrev,DW_TAG_subprogram);
2191 saa_write8(pabbrev,0); /* no children */
2192 saa_write8(pabbrev,DW_AT_low_pc);
2193 saa_write8(pabbrev,DW_FORM_addr);
2194 saa_write8(pabbrev,DW_AT_frame_base);
2195 saa_write8(pabbrev,DW_FORM_data4);
2196 saa_write16(pabbrev,0); /* end of entry */
2197 abbrevlen = saalen = pabbrev->datalen;
2198 abbrevbuf = pbuf = nasm_malloc(saalen);
2199 saa_rnbytes(pabbrev, pbuf, saalen);
2200 saa_free(pabbrev);
2202 /* build line section */
2203 /* prolog */
2204 plines = saa_init(1L);
2205 saa_write8(plines,1); /* Minimum Instruction Length */
2206 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2207 saa_write8(plines,line_base); /* Line Base */
2208 saa_write8(plines,line_range); /* Line Range */
2209 saa_write8(plines,opcode_base); /* Opcode Base */
2210 /* standard opcode lengths (# of LEB128u operands) */
2211 saa_write8(plines,0); /* Std opcode 1 length */
2212 saa_write8(plines,1); /* Std opcode 2 length */
2213 saa_write8(plines,1); /* Std opcode 3 length */
2214 saa_write8(plines,1); /* Std opcode 4 length */
2215 saa_write8(plines,1); /* Std opcode 5 length */
2216 saa_write8(plines,0); /* Std opcode 6 length */
2217 saa_write8(plines,0); /* Std opcode 7 length */
2218 saa_write8(plines,0); /* Std opcode 8 length */
2219 saa_write8(plines,1); /* Std opcode 9 length */
2220 saa_write8(plines,0); /* Std opcode 10 length */
2221 saa_write8(plines,0); /* Std opcode 11 length */
2222 saa_write8(plines,1); /* Std opcode 12 length */
2223 /* Directory Table */
2224 saa_write8(plines,0); /* End of table */
2225 /* File Name Table */
2226 ftentry = dwarf_flist;
2227 for (indx = 0;indx<dwarf_numfiles;indx++)
2229 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2230 saa_write8(plines,0); /* directory LEB128u */
2231 saa_write8(plines,0); /* time LEB128u */
2232 saa_write8(plines,0); /* size LEB128u */
2233 ftentry = ftentry->next;
2235 saa_write8(plines,0); /* End of table */
2236 linepoff = plines->datalen;
2237 linelen = linepoff + totlen + 10;
2238 linebuf = pbuf = nasm_malloc(linelen);
2239 WRITELONG(pbuf,linelen-4); /* initial length */
2240 WRITESHORT(pbuf,3); /* dwarf version */
2241 WRITELONG(pbuf,linepoff); /* offset to line number program */
2242 /* write line header */
2243 saalen = linepoff;
2244 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2245 pbuf += linepoff;
2246 saa_free(plines);
2247 /* concatonate line program ranges */
2248 linepoff += 13;
2249 plinesrel = saa_init(1L);
2250 psect = dwarf_fsect;
2251 for (indx = 0; indx < dwarf_nsections; indx++)
2253 saa_write64(plinesrel, linepoff);
2254 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2255 saa_write64(plinesrel, (uint64_t) 0);
2256 plinep = psect->psaa;
2257 saalen = plinep->datalen;
2258 saa_rnbytes(plinep, pbuf, saalen);
2259 pbuf += saalen;
2260 linepoff += saalen;
2261 saa_free(plinep);
2262 /* done with this entry */
2263 psect = psect->next;
2267 /* build rela.lines section */
2268 linerellen =saalen = plinesrel->datalen;
2269 linerelbuf = pbuf = nasm_malloc(linerellen);
2270 saa_rnbytes(plinesrel, pbuf, saalen);
2271 saa_free(plinesrel);
2273 /* build frame section */
2274 framelen = 4;
2275 framebuf = pbuf = nasm_malloc(framelen);
2276 WRITELONG(pbuf,framelen-4); /* initial length */
2278 /* build loc section */
2279 loclen = 16;
2280 locbuf = pbuf = nasm_malloc(loclen);
2281 WRITEDLONG(pbuf,0); /* null beginning offset */
2282 WRITEDLONG(pbuf,0); /* null ending offset */
2285 void dwarf64_cleanup(void)
2287 if (arangesbuf)
2288 nasm_free(arangesbuf);
2289 if (arangesrelbuf)
2290 nasm_free(arangesrelbuf);
2291 if (pubnamesbuf)
2292 nasm_free(pubnamesbuf);
2293 if (infobuf)
2294 nasm_free(infobuf);
2295 if (inforelbuf)
2296 nasm_free(inforelbuf);
2297 if (abbrevbuf)
2298 nasm_free(abbrevbuf);
2299 if (linebuf)
2300 nasm_free(linebuf);
2301 if (linerelbuf)
2302 nasm_free(linerelbuf);
2303 if (framebuf)
2304 nasm_free(framebuf);
2305 if (locbuf)
2306 nasm_free(locbuf);
2308 void dwarf64_findfile(const char * fname)
2310 int finx;
2311 struct linelist *match;
2313 /* return if fname is current file name */
2314 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2315 /* search for match */
2316 else
2318 match = 0;
2319 if (dwarf_flist)
2321 match = dwarf_flist;
2322 for (finx = 0; finx < dwarf_numfiles; finx++)
2324 if (!(strcmp(fname, match->filename)))
2326 dwarf_clist = match;
2327 return;
2331 /* add file name to end of list */
2332 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2333 dwarf_numfiles++;
2334 dwarf_clist->line = dwarf_numfiles;
2335 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2336 strcpy(dwarf_clist->filename,fname);
2337 dwarf_clist->next = 0;
2338 /* if first entry */
2339 if (!dwarf_flist)
2341 dwarf_flist = dwarf_elist = dwarf_clist;
2342 dwarf_clist->last = 0;
2344 /* chain to previous entry */
2345 else
2347 dwarf_elist->next = dwarf_clist;
2348 dwarf_elist = dwarf_clist;
2352 /* */
2353 void dwarf64_findsect(const int index)
2355 int sinx;
2356 struct sectlist *match;
2357 struct SAA *plinep;
2358 /* return if index is current section index */
2359 if (dwarf_csect && (dwarf_csect->section == index))
2361 return;
2363 /* search for match */
2364 else
2366 match = 0;
2367 if (dwarf_fsect)
2369 match = dwarf_fsect;
2370 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2372 if ((match->section == index))
2374 dwarf_csect = match;
2375 return;
2377 match = match->next;
2380 /* add entry to end of list */
2381 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2382 dwarf_nsections++;
2383 dwarf_csect->psaa = plinep = saa_init(1L);
2384 dwarf_csect->line = 1;
2385 dwarf_csect->offset = 0;
2386 dwarf_csect->file = 1;
2387 dwarf_csect->section = index;
2388 dwarf_csect->next = 0;
2389 /* set relocatable address at start of line program */
2390 saa_write8(plinep,DW_LNS_extended_op);
2391 saa_write8(plinep,9); /* operand length */
2392 saa_write8(plinep,DW_LNE_set_address);
2393 saa_write64(plinep,0); /* Start Address */
2394 /* if first entry */
2395 if (!dwarf_fsect)
2397 dwarf_fsect = dwarf_esect = dwarf_csect;
2398 dwarf_csect->last = 0;
2400 /* chain to previous entry */
2401 else
2403 dwarf_esect->next = dwarf_csect;
2404 dwarf_esect = dwarf_csect;
2409 #endif /* OF_ELF */