Fix BR #916647
[nasm/perl-rewrite.git] / output / outelf64.c
blob4254585c28e3a4702d9edf0d8b06952364fb0c12
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"
25 /* Definitions in lieu of elf.h */
26 #define SHT_NULL 0 /* Inactive section header */
27 #define SHT_PROGBITS 1 /* Program defined content */
28 #define SHT_RELA 4 /* Relocation entries with addends */
29 #define SHT_NOBITS 8 /* Section requires no space in file */
30 #define SHF_WRITE (1 << 0) /* Writable */
31 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
32 #define SHF_EXECINSTR (1 << 2) /* Executable */
33 #define SHF_TLS (1 << 10) /* Section hold thread-local data. */
34 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
35 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
36 #define R_X86_64_NONE 0 /* No reloc */
37 #define R_X86_64_64 1 /* Direct 64 bit */
38 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
39 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
40 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
41 #define R_X86_64_COPY 5 /* Copy symbol at runtime */
42 #define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
43 #define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
44 #define R_X86_64_RELATIVE 8 /* Adjust by program base */
45 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative
46 offset to GOT */
47 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
48 #define R_X86_64_32S 11 /* Direct 32 bit sign extended */
49 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
50 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
51 #define R_X86_64_8 14 /* Direct 8 bit sign extended */
52 #define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
53 #define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */
54 #define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */
55 #define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */
56 #define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset
57 to two GOT entries for GD symbol */
58 #define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset
59 to two GOT entries for LD symbol */
60 #define R_X86_64_DTPOFF32 21 /* Offset in TLS block */
61 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset
62 to GOT entry for IE symbol */
63 #define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */
64 #define R_X86_64_PC64 24 /* word64 S + A - P */
65 #define R_X86_64_GOTOFF64 25 /* word64 S + A - GOT */
66 #define R_X86_64_GOTPC32 26 /* word32 GOT + A - P */
67 #define R_X86_64_GOT64 27 /* word64 G + A */
68 #define R_X86_64_GOTPCREL64 28 /* word64 G + GOT - P + A */
69 #define R_X86_64_GOTPC64 29 /* word64 GOT - P + A */
70 #define R_X86_64_GOTPLT64 30 /* word64 G + A */
71 #define R_X86_64_PLTOFF64 31 /* word64 L - GOT + A */
72 #define R_X86_64_SIZE32 32 /* word32 Z + A */
73 #define R_X86_64_SIZE64 33 /* word64 Z + A */
74 #define R_X86_64_GOTPC32_TLSDESC 34 /* word32 */
75 #define R_X86_64_TLSDESC_CALL 35 /* none */
76 #define R_X86_64_TLSDESC 36 /* word64×2 */
77 #define ET_REL 1 /* Relocatable file */
78 #define EM_X86_64 62 /* AMD x86-64 architecture */
79 #define STT_NOTYPE 0 /* Symbol type is unspecified */
80 #define STT_OBJECT 1 /* Symbol is a data object */
81 #define STT_FUNC 2 /* Symbol is a code object */
82 #define STT_SECTION 3 /* Symbol associated with a section */
83 #define STT_FILE 4 /* Symbol's name is file name */
84 #define STT_COMMON 5 /* Symbol is a common data object */
85 #define STT_TLS 6 /* Symbol is thread-local data object*/
86 #define STT_NUM 7 /* Number of defined types. */
88 /* Definitions in lieu of dwarf.h */
89 #define DW_TAG_compile_unit 0x11
90 #define DW_TAG_subprogram 0x2e
91 #define DW_AT_name 0x03
92 #define DW_AT_stmt_list 0x10
93 #define DW_AT_low_pc 0x11
94 #define DW_AT_high_pc 0x12
95 #define DW_AT_language 0x13
96 #define DW_AT_producer 0x25
97 #define DW_AT_frame_base 0x40
98 #define DW_FORM_addr 0x01
99 #define DW_FORM_data2 0x05
100 #define DW_FORM_data4 0x06
101 #define DW_FORM_string 0x08
102 #define DW_LNS_extended_op 0
103 #define DW_LNS_advance_pc 2
104 #define DW_LNS_advance_line 3
105 #define DW_LNS_set_file 4
106 #define DW_LNE_end_sequence 1
107 #define DW_LNE_set_address 2
108 #define DW_LNE_define_file 3
109 #define DW_LANG_Mips_Assembler 0x8001
111 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
113 typedef uint32_t Elf64_Word;
114 typedef uint64_t Elf64_Xword;
115 typedef uint64_t Elf64_Addr;
116 typedef uint64_t Elf64_Off;
117 typedef struct
119 Elf64_Word sh_name; /* Section name (string tbl index) */
120 Elf64_Word sh_type; /* Section type */
121 Elf64_Xword sh_flags; /* Section flags */
122 Elf64_Addr sh_addr; /* Section virtual addr at execution */
123 Elf64_Off sh_offset; /* Section file offset */
124 Elf64_Xword sh_size; /* Section size in bytes */
125 Elf64_Word sh_link; /* Link to another section */
126 Elf64_Word sh_info; /* Additional section information */
127 Elf64_Xword sh_addralign; /* Section alignment */
128 Elf64_Xword sh_entsize; /* Entry size if section holds table */
129 } Elf64_Shdr;
132 #ifdef OF_ELF64
135 struct Reloc {
136 struct Reloc *next;
137 int64_t address; /* relative to _start_ of section */
138 int64_t symbol; /* symbol index */
139 int64_t offset; /* symbol addend */
140 int type; /* type of relocation */
143 struct Symbol {
144 int32_t strpos; /* string table position of name */
145 int32_t section; /* section ID of the symbol */
146 int type; /* symbol type */
147 int other; /* symbol visibility */
148 int64_t value; /* address, or COMMON variable align */
149 int32_t size; /* size of symbol */
150 int32_t globnum; /* symbol table offset if global */
151 struct Symbol *next; /* list of globals in each section */
152 struct Symbol *nextfwd; /* list of unresolved-size symbols */
153 char *name; /* used temporarily if in above list */
157 struct Section {
158 struct SAA *data;
159 uint64_t len, size;
160 uint32_t nrelocs;
161 int32_t index; /* index into sects array */
162 uint32_t type; /* SHT_PROGBITS or SHT_NOBITS */
163 uint64_t align; /* alignment: power of two */
164 uint64_t flags; /* section flags */
165 char *name;
166 struct SAA *rel;
167 uint64_t rellen;
168 struct Reloc *head, **tail;
169 struct Symbol *gsyms; /* global symbols in section */
172 #define SECT_DELTA 32
173 static struct Section **sects;
174 static int nsects, sectlen;
176 #define SHSTR_DELTA 256
177 static char *shstrtab;
178 static int shstrtablen, shstrtabsize;
180 static struct SAA *syms;
181 static uint32_t nlocals, nglobs;
183 static int32_t def_seg;
185 static struct RAA *bsym;
187 static struct SAA *strs;
188 static uint32_t strslen;
190 static FILE *elffp;
191 static efunc error;
192 static evalfunc evaluate;
194 static struct Symbol *fwds;
196 static char elf_module[FILENAME_MAX];
198 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
199 static uint8_t elf_abiver = 0; /* Current ABI version */
201 extern struct ofmt of_elf64;
203 #define SHN_UNDEF 0
205 #define SYM_GLOBAL 0x10
207 #define STV_DEFAULT 0
208 #define STV_INTERNAL 1
209 #define STV_HIDDEN 2
210 #define STV_PROTECTED 3
212 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
214 #define SEG_ALIGN 16 /* alignment of sections in file */
215 #define SEG_ALIGN_1 (SEG_ALIGN-1)
217 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
219 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
221 static struct ELF_SECTDATA {
222 void *data;
223 int64_t len;
224 bool is_saa;
225 } *elf_sects;
226 static int elf_nsect, nsections;
227 static int64_t elf_foffs;
229 static void elf_write(void);
230 static void elf_sect_write(struct Section *, const void *, size_t);
231 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
232 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
233 int, int);
234 static void elf_write_sections(void);
235 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
236 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
237 static void add_sectname(char *, char *);
239 /* type values for stabs debugging sections */
240 #define N_SO 0x64 /* ID for main source file */
241 #define N_SOL 0x84 /* ID for sub-source file */
242 #define N_BINCL 0x82 /* not currently used */
243 #define N_EINCL 0xA2 /* not currently used */
244 #define N_SLINE 0x44
246 struct stabentry {
247 uint32_t n_strx;
248 uint8_t n_type;
249 uint8_t n_other;
250 uint16_t n_desc;
251 uint32_t n_value;
254 struct erel {
255 int offset, info;
258 struct symlininfo {
259 int offset;
260 int section; /* index into sects[] */
261 int segto; /* internal section number */
262 char *name; /* shallow-copied pointer of section name */
265 struct linelist {
266 struct symlininfo info;
267 int line;
268 char *filename;
269 struct linelist *next;
270 struct linelist *last;
273 struct sectlist {
274 struct SAA *psaa;
275 int section;
276 int line;
277 int offset;
278 int file;
279 struct sectlist *next;
280 struct sectlist *last;
283 /* common debug variables */
284 static int currentline = 1;
285 static int debug_immcall = 0;
287 /* stabs debug variables */
288 static struct linelist *stabslines = 0;
289 static int numlinestabs = 0;
290 static char *stabs_filename = 0;
291 static int symtabsection;
292 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
293 static int stablen, stabstrlen, stabrellen;
295 /* dwarf debug variables */
296 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
297 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
298 static int dwarf_numfiles = 0, dwarf_nsections;
299 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
300 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
301 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
302 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
303 abbrevlen, linelen, linerellen, framelen, loclen;
304 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
307 static struct dfmt df_dwarf;
308 static struct dfmt df_stabs;
309 static struct Symbol *lastsym;
311 /* common debugging routines */
312 void debug64_typevalue(int32_t);
313 void debug64_init(struct ofmt *, void *, FILE *, efunc);
314 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
315 void debug64_directive(const char *, const char *);
317 /* stabs debugging routines */
318 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
319 void stabs64_output(int, void *);
320 void stabs64_generate(void);
321 void stabs64_cleanup(void);
323 /* dwarf debugging routines */
324 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
325 void dwarf64_output(int, void *);
326 void dwarf64_generate(void);
327 void dwarf64_cleanup(void);
328 void dwarf64_findfile(const char *);
329 void dwarf64_findsect(const int);
332 * Special section numbers which are used to define ELF special
333 * symbols, which can be used with WRT to provide PIC relocation
334 * types.
336 static int32_t elf_gotpc_sect, elf_gotoff_sect;
337 static int32_t elf_got_sect, elf_plt_sect;
338 static int32_t elf_sym_sect;
339 static int32_t elf_gottpoff_sect;
341 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
343 maxbits = 64;
344 elffp = fp;
345 error = errfunc;
346 evaluate = eval;
347 (void)ldef; /* placate optimisers */
348 sects = NULL;
349 nsects = sectlen = 0;
350 syms = saa_init((int32_t)sizeof(struct Symbol));
351 nlocals = nglobs = 0;
352 bsym = raa_init();
353 strs = saa_init(1L);
354 saa_wbytes(strs, "\0", 1L);
355 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
356 strslen = 2 + strlen(elf_module);
357 shstrtab = NULL;
358 shstrtablen = shstrtabsize = 0;;
359 add_sectname("", "");
361 fwds = NULL;
363 elf_gotpc_sect = seg_alloc();
364 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
365 error);
366 elf_gotoff_sect = seg_alloc();
367 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
368 error);
369 elf_got_sect = seg_alloc();
370 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
371 error);
372 elf_plt_sect = seg_alloc();
373 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
374 error);
375 elf_sym_sect = seg_alloc();
376 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
377 error);
378 elf_gottpoff_sect = seg_alloc();
379 ldef("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false, &of_elf64,
380 error);
382 def_seg = seg_alloc();
386 static void elf_cleanup(int debuginfo)
388 struct Reloc *r;
389 int i;
391 (void)debuginfo;
393 elf_write();
394 fclose(elffp);
395 for (i = 0; i < nsects; i++) {
396 if (sects[i]->type != SHT_NOBITS)
397 saa_free(sects[i]->data);
398 if (sects[i]->head)
399 saa_free(sects[i]->rel);
400 while (sects[i]->head) {
401 r = sects[i]->head;
402 sects[i]->head = sects[i]->head->next;
403 nasm_free(r);
406 nasm_free(sects);
407 saa_free(syms);
408 raa_free(bsym);
409 saa_free(strs);
410 if (of_elf64.current_dfmt) {
411 of_elf64.current_dfmt->cleanup();
414 /* add entry to the elf .shstrtab section */
415 static void add_sectname(char *firsthalf, char *secondhalf)
417 int len = strlen(firsthalf) + strlen(secondhalf);
418 while (shstrtablen + len + 1 > shstrtabsize)
419 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
420 strcpy(shstrtab + shstrtablen, firsthalf);
421 strcat(shstrtab + shstrtablen, secondhalf);
422 shstrtablen += len + 1;
425 static int elf_make_section(char *name, int type, int flags, int align)
427 struct Section *s;
429 s = nasm_malloc(sizeof(*s));
431 if (type != SHT_NOBITS)
432 s->data = saa_init(1L);
433 s->head = NULL;
434 s->tail = &s->head;
435 s->len = s->size = 0;
436 s->nrelocs = 0;
437 if (!strcmp(name, ".text"))
438 s->index = def_seg;
439 else
440 s->index = seg_alloc();
441 add_sectname("", name);
442 s->name = nasm_malloc(1 + strlen(name));
443 strcpy(s->name, name);
444 s->type = type;
445 s->flags = flags;
446 s->align = align;
447 s->gsyms = NULL;
449 if (nsects >= sectlen)
450 sects =
451 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
452 sects[nsects++] = s;
454 return nsects - 1;
457 static int32_t elf_section_names(char *name, int pass, int *bits)
459 char *p;
460 unsigned flags_and, flags_or;
461 uint64_t type, align;
462 int i;
465 * Default is 64 bits.
467 if (!name) {
468 *bits = 64;
469 return def_seg;
472 p = name;
473 while (*p && !nasm_isspace(*p))
474 p++;
475 if (*p)
476 *p++ = '\0';
477 flags_and = flags_or = type = align = 0;
479 while (*p && nasm_isspace(*p))
480 p++;
481 while (*p) {
482 char *q = p;
483 while (*p && !nasm_isspace(*p))
484 p++;
485 if (*p)
486 *p++ = '\0';
487 while (*p && nasm_isspace(*p))
488 p++;
490 if (!nasm_strnicmp(q, "align=", 6)) {
491 align = atoi(q + 6);
492 if (align == 0)
493 align = 1;
494 if ((align - 1) & align) { /* means it's not a power of two */
495 error(ERR_NONFATAL, "section alignment %d is not"
496 " a power of two", align);
497 align = 1;
499 } else if (!nasm_stricmp(q, "alloc")) {
500 flags_and |= SHF_ALLOC;
501 flags_or |= SHF_ALLOC;
502 } else if (!nasm_stricmp(q, "noalloc")) {
503 flags_and |= SHF_ALLOC;
504 flags_or &= ~SHF_ALLOC;
505 } else if (!nasm_stricmp(q, "exec")) {
506 flags_and |= SHF_EXECINSTR;
507 flags_or |= SHF_EXECINSTR;
508 } else if (!nasm_stricmp(q, "noexec")) {
509 flags_and |= SHF_EXECINSTR;
510 flags_or &= ~SHF_EXECINSTR;
511 } else if (!nasm_stricmp(q, "write")) {
512 flags_and |= SHF_WRITE;
513 flags_or |= SHF_WRITE;
514 } else if (!nasm_stricmp(q, "tls")) {
515 flags_and |= SHF_TLS;
516 flags_or |= SHF_TLS;
517 } else if (!nasm_stricmp(q, "nowrite")) {
518 flags_and |= SHF_WRITE;
519 flags_or &= ~SHF_WRITE;
520 } else if (!nasm_stricmp(q, "progbits")) {
521 type = SHT_PROGBITS;
522 } else if (!nasm_stricmp(q, "nobits")) {
523 type = SHT_NOBITS;
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->value = 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->value = readnum(special, &err);
679 if (err)
680 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
681 " valid number", special);
682 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
683 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
684 " power of two", special);
686 special_used = true;
687 } else
688 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
690 if (sym->type == SYM_GLOBAL) {
692 * If sym->section == SHN_ABS, then the first line of the
693 * else section would cause a core dump, because its a reference
694 * beyond the end of the section array.
695 * This behaviour is exhibited by this code:
696 * GLOBAL crash_nasm
697 * crash_nasm equ 0
698 * To avoid such a crash, such requests are silently discarded.
699 * This may not be the best solution.
701 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
702 bsym = raa_write(bsym, segment, nglobs);
703 } else if (sym->section != SHN_ABS) {
705 * This is a global symbol; so we must add it to the linked
706 * list of global symbols in its section. We'll push it on
707 * the beginning of the list, because it doesn't matter
708 * much which end we put it on and it's easier like this.
710 * In addition, we check the special text for symbol
711 * type and size information.
713 sym->next = sects[sym->section - 1]->gsyms;
714 sects[sym->section - 1]->gsyms = sym;
716 if (special) {
717 int n = strcspn(special, " \t");
719 if (!nasm_strnicmp(special, "function", n))
720 sym->type |= STT_FUNC;
721 else if (!nasm_strnicmp(special, "data", n) ||
722 !nasm_strnicmp(special, "object", n))
723 sym->type |= STT_OBJECT;
724 else if (!nasm_strnicmp(special, "notype", n))
725 sym->type |= STT_NOTYPE;
726 else
727 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
728 n, special);
729 special += n;
731 while (nasm_isspace(*special))
732 ++special;
733 if (*special) {
734 n = strcspn(special, " \t");
735 if (!nasm_strnicmp(special, "default", n))
736 sym->other = STV_DEFAULT;
737 else if (!nasm_strnicmp(special, "internal", n))
738 sym->other = STV_INTERNAL;
739 else if (!nasm_strnicmp(special, "hidden", n))
740 sym->other = STV_HIDDEN;
741 else if (!nasm_strnicmp(special, "protected", n))
742 sym->other = STV_PROTECTED;
743 else
744 n = 0;
745 special += n;
748 if (*special) {
749 struct tokenval tokval;
750 expr *e;
751 int fwd = 0;
752 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
754 while (special[n] && nasm_isspace(special[n]))
755 n++;
757 * We have a size expression; attempt to
758 * evaluate it.
760 stdscan_reset();
761 stdscan_bufptr = special + n;
762 tokval.t_type = TOKEN_INVALID;
763 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
764 NULL);
765 if (fwd) {
766 sym->nextfwd = fwds;
767 fwds = sym;
768 sym->name = nasm_strdup(name);
769 } else if (e) {
770 if (!is_simple(e))
771 error(ERR_NONFATAL, "cannot use relocatable"
772 " expression as symbol size");
773 else
774 sym->size = reloc_value(e);
776 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
778 special_used = true;
781 * If TLS segment, mark symbol accordingly.
783 if (sects[sym->section - 1]->flags & SHF_TLS) {
784 sym->type &= 0xf0;
785 sym->type |= STT_TLS;
788 sym->globnum = nglobs;
789 nglobs++;
790 } else
791 nlocals++;
793 if (special && !special_used)
794 error(ERR_NONFATAL, "no special symbol features supported here");
797 static void elf_add_reloc(struct Section *sect, int32_t segment,
798 int64_t offset, int type)
800 struct Reloc *r;
801 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
802 sect->tail = &r->next;
803 r->next = NULL;
805 r->address = sect->len;
806 r->offset = offset;
807 if (segment == NO_SEG)
808 r->symbol = 0;
809 else {
810 int i;
811 r->symbol = 0;
812 for (i = 0; i < nsects; i++)
813 if (segment == sects[i]->index)
814 r->symbol = i + 2;
815 if (!r->symbol)
816 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
818 r->type = type;
820 sect->nrelocs++;
824 * This routine deals with ..got and ..sym relocations: the more
825 * complicated kinds. In shared-library writing, some relocations
826 * with respect to global symbols must refer to the precise symbol
827 * rather than referring to an offset from the base of the section
828 * _containing_ the symbol. Such relocations call to this routine,
829 * which searches the symbol list for the symbol in question.
831 * R_386_GOT32 references require the _exact_ symbol address to be
832 * used; R_386_32 references can be at an offset from the symbol.
833 * The boolean argument `exact' tells us this.
835 * Return value is the adjusted value of `addr', having become an
836 * offset from the symbol rather than the section. Should always be
837 * zero when returning from an exact call.
839 * Limitation: if you define two symbols at the same place,
840 * confusion will occur.
842 * Inefficiency: we search, currently, using a linked list which
843 * isn't even necessarily sorted.
845 static void elf_add_gsym_reloc(struct Section *sect,
846 int32_t segment, int64_t offset, int64_t pcrel,
847 int type, bool exact)
849 struct Reloc *r;
850 struct Section *s;
851 struct Symbol *sym, *sm;
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 if (exact) {
878 * Find a symbol pointing _exactly_ at this one.
880 for (sym = s->gsyms; sym; sym = sym->next)
881 if (sym->value == offset)
882 break;
883 if (!sym) {
884 error(ERR_NONFATAL, "unable to find a suitable global symbol"
885 " for this reference");
886 return;
888 } else {
890 * Find the nearest symbol below this one.
892 sym = NULL;
893 for (sm = s->gsyms; sm; sm = sm->next)
894 if (sm->value <= offset && (!sym || sm->value > sym->value))
895 sym = sm;
898 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
899 sect->tail = &r->next;
900 r->next = NULL;
902 r->address = sect->len;
903 r->offset = offset - pcrel - sym->value;
904 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
905 r->type = type;
907 sect->nrelocs++;
910 static void elf_out(int32_t segto, const void *data,
911 enum out_type type, uint64_t size,
912 int32_t segment, int32_t wrt)
914 struct Section *s;
915 int64_t addr, zero;
916 int i;
917 static struct symlininfo sinfo;
919 zero = 0;
921 #if defined(DEBUG) && DEBUG>2
922 if (data) fprintf(stderr,
923 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
924 currentline, type, segment, segto, size, *(int64_t *)data);
925 else fprintf(stderr,
926 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
927 currentline, type, segment, segto, size);
928 #endif
931 * handle absolute-assembly (structure definitions)
933 if (segto == NO_SEG) {
934 if (type != OUT_RESERVE)
935 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
936 " space");
937 return;
940 s = NULL;
941 for (i = 0; i < nsects; i++)
942 if (segto == sects[i]->index) {
943 s = sects[i];
944 break;
946 if (!s) {
947 int tempint; /* ignored */
948 if (segto != elf_section_names(".text", 2, &tempint))
949 error(ERR_PANIC, "strange segment conditions in ELF driver");
950 else {
951 s = sects[nsects - 1];
952 i = nsects - 1;
955 /* invoke current debug_output routine */
956 if (of_elf64.current_dfmt) {
957 sinfo.offset = s->len;
958 sinfo.section = i;
959 sinfo.segto = segto;
960 sinfo.name = s->name;
961 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
963 /* end of debugging stuff */
965 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
966 error(ERR_WARNING, "attempt to initialize memory in"
967 " BSS section `%s': ignored", s->name);
968 s->len += realsize(type, size);
969 return;
972 if (type == OUT_RESERVE) {
973 if (s->type == SHT_PROGBITS) {
974 error(ERR_WARNING, "uninitialized space declared in"
975 " non-BSS section `%s': zeroing", s->name);
976 elf_sect_write(s, NULL, size);
977 } else
978 s->len += size;
979 } else if (type == OUT_RAWDATA) {
980 if (segment != NO_SEG)
981 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
982 elf_sect_write(s, data, size);
983 } else if (type == OUT_ADDRESS) {
984 addr = *(int64_t *)data;
985 if (segment == NO_SEG) {
986 /* Do nothing */
987 } else if (segment % 2) {
988 error(ERR_NONFATAL, "ELF format does not support"
989 " segment base references");
990 } else {
991 if (wrt == NO_SEG) {
992 switch ((int)size) {
993 case 1:
994 elf_add_reloc(s, segment, addr, R_X86_64_8);
995 break;
996 case 2:
997 elf_add_reloc(s, segment, addr, R_X86_64_16);
998 break;
999 case 4:
1000 elf_add_reloc(s, segment, addr, R_X86_64_32);
1001 break;
1002 case 8:
1003 elf_add_reloc(s, segment, addr, R_X86_64_64);
1004 break;
1005 default:
1006 error(ERR_PANIC, "internal error elf64-hpa-871");
1007 break;
1009 addr = 0;
1010 } else if (wrt == elf_gotpc_sect + 1) {
1012 * The user will supply GOT relative to $$. ELF
1013 * will let us have GOT relative to $. So we
1014 * need to fix up the data item by $-$$.
1016 addr += s->len;
1017 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1018 addr = 0;
1019 } else if (wrt == elf_gotoff_sect + 1) {
1020 if (size != 8) {
1021 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1022 "references to be qword");
1023 } else {
1024 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1025 addr = 0;
1027 } else if (wrt == elf_got_sect + 1) {
1028 switch ((int)size) {
1029 case 4:
1030 elf_add_gsym_reloc(s, segment, addr, 0,
1031 R_X86_64_GOT32, true);
1032 addr = 0;
1033 break;
1034 case 8:
1035 elf_add_gsym_reloc(s, segment, addr, 0,
1036 R_X86_64_GOT64, true);
1037 addr = 0;
1038 break;
1039 default:
1040 error(ERR_NONFATAL, "invalid ..got reference");
1041 break;
1043 } else if (wrt == elf_sym_sect + 1) {
1044 switch ((int)size) {
1045 case 1:
1046 elf_add_gsym_reloc(s, segment, addr, 0,
1047 R_X86_64_8, false);
1048 addr = 0;
1049 break;
1050 case 2:
1051 elf_add_gsym_reloc(s, segment, addr, 0,
1052 R_X86_64_16, false);
1053 addr = 0;
1054 break;
1055 case 4:
1056 elf_add_gsym_reloc(s, segment, addr, 0,
1057 R_X86_64_32, false);
1058 addr = 0;
1059 break;
1060 case 8:
1061 elf_add_gsym_reloc(s, segment, addr, 0,
1062 R_X86_64_64, false);
1063 addr = 0;
1064 break;
1065 default:
1066 error(ERR_PANIC, "internal error elf64-hpa-903");
1067 break;
1069 } else if (wrt == elf_plt_sect + 1) {
1070 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1071 "relative PLT references");
1072 } else {
1073 error(ERR_NONFATAL, "ELF format does not support this"
1074 " use of WRT");
1077 elf_sect_writeaddr(s, addr, size);
1078 } else if (type == OUT_REL2ADR) {
1079 addr = *(int64_t *)data - size;
1080 if (segment == segto)
1081 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
1082 if (segment == NO_SEG) {
1083 /* Do nothing */
1084 } else if (segment % 2) {
1085 error(ERR_NONFATAL, "ELF format does not support"
1086 " segment base references");
1087 } else {
1088 if (wrt == NO_SEG) {
1089 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
1090 addr = 0;
1091 } else {
1092 error(ERR_NONFATAL,
1093 "Unsupported non-32-bit ELF relocation [2]");
1096 elf_sect_writeaddr(s, addr, 2);
1097 } else if (type == OUT_REL4ADR) {
1098 addr = *(int64_t *)data - size;
1099 if (segment == segto)
1100 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1101 if (segment == NO_SEG) {
1102 /* Do nothing */
1103 } else if (segment % 2) {
1104 error(ERR_NONFATAL, "ELF64 format does not support"
1105 " segment base references");
1106 } else {
1107 if (wrt == NO_SEG) {
1108 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1109 addr = 0;
1110 } else if (wrt == elf_plt_sect + 1) {
1111 elf_add_gsym_reloc(s, segment, addr+size, size,
1112 R_X86_64_PLT32, true);
1113 addr = 0;
1114 } else if (wrt == elf_gotpc_sect + 1 ||
1115 wrt == elf_got_sect + 1) {
1116 elf_add_gsym_reloc(s, segment, addr+size, size,
1117 R_X86_64_GOTPCREL, true);
1118 addr = 0;
1119 } else if (wrt == elf_gotoff_sect + 1 ||
1120 wrt == elf_got_sect + 1) {
1121 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1122 "qword absolute");
1123 } else if (wrt == elf_gottpoff_sect + 1) {
1124 elf_add_gsym_reloc(s, segment, addr+size, size,
1125 R_X86_64_GOTTPOFF, true);
1126 addr = 0;
1127 } else {
1128 error(ERR_NONFATAL, "ELF64 format does not support this"
1129 " use of WRT");
1132 elf_sect_writeaddr(s, addr, 4);
1133 } else if (type == OUT_REL8ADR) {
1134 addr = *(int64_t *)data - size;
1135 if (segment == segto)
1136 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1137 if (segment == NO_SEG) {
1138 /* Do nothing */
1139 } else if (segment % 2) {
1140 error(ERR_NONFATAL, "ELF64 format does not support"
1141 " segment base references");
1142 } else {
1143 if (wrt == NO_SEG) {
1144 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1145 addr = 0;
1146 } else if (wrt == elf_gotpc_sect + 1 ||
1147 wrt == elf_got_sect + 1) {
1148 elf_add_gsym_reloc(s, segment, addr+size, size,
1149 R_X86_64_GOTPCREL64, true);
1150 addr = 0;
1151 } else if (wrt == elf_gotoff_sect + 1 ||
1152 wrt == elf_got_sect + 1) {
1153 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1154 "absolute");
1155 } else if (wrt == elf_gottpoff_sect + 1) {
1156 error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1157 "dword");
1158 } else {
1159 error(ERR_NONFATAL, "ELF64 format does not support this"
1160 " use of WRT");
1163 elf_sect_writeaddr(s, addr, 8);
1167 static void elf_write(void)
1169 int align;
1170 int scount;
1171 char *p;
1172 int commlen;
1173 char comment[64];
1174 int i;
1176 struct SAA *symtab;
1177 int32_t symtablen, symtablocal;
1180 * Work out how many sections we will have. We have SHN_UNDEF,
1181 * then the flexible user sections, then the four fixed
1182 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1183 * then optionally relocation sections for the user sections.
1185 if (of_elf64.current_dfmt == &df_stabs)
1186 nsections = 8;
1187 else if (of_elf64.current_dfmt == &df_dwarf)
1188 nsections = 15;
1189 else
1190 nsections = 5; /* SHN_UNDEF and the fixed ones */
1192 add_sectname("", ".comment");
1193 add_sectname("", ".shstrtab");
1194 add_sectname("", ".symtab");
1195 add_sectname("", ".strtab");
1196 for (i = 0; i < nsects; i++) {
1197 nsections++; /* for the section itself */
1198 if (sects[i]->head) {
1199 nsections++; /* for its relocations */
1200 add_sectname(".rela", sects[i]->name);
1204 if (of_elf64.current_dfmt == &df_stabs) {
1205 /* in case the debug information is wanted, just add these three sections... */
1206 add_sectname("", ".stab");
1207 add_sectname("", ".stabstr");
1208 add_sectname(".rel", ".stab");
1211 else if (of_elf64.current_dfmt == &df_dwarf) {
1212 /* the dwarf debug standard specifies the following ten sections,
1213 not all of which are currently implemented,
1214 although all of them are defined. */
1215 #define debug_aranges (int64_t) (nsections-10)
1216 #define debug_info (int64_t) (nsections-7)
1217 #define debug_abbrev (int64_t) (nsections-5)
1218 #define debug_line (int64_t) (nsections-4)
1219 add_sectname("", ".debug_aranges");
1220 add_sectname(".rela", ".debug_aranges");
1221 add_sectname("", ".debug_pubnames");
1222 add_sectname("", ".debug_info");
1223 add_sectname(".rela", ".debug_info");
1224 add_sectname("", ".debug_abbrev");
1225 add_sectname("", ".debug_line");
1226 add_sectname(".rela", ".debug_line");
1227 add_sectname("", ".debug_frame");
1228 add_sectname("", ".debug_loc");
1232 * Do the comment.
1234 *comment = '\0';
1235 commlen = 2 + snprintf(comment+1, sizeof comment-1, "%s", nasm_comment);
1238 * Output the ELF header.
1240 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1241 fputc(elf_osabi, elffp);
1242 fputc(elf_abiver, elffp);
1243 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1244 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1245 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1246 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1247 fwriteint64_t(0L, elffp); /* no entry point */
1248 fwriteint64_t(0L, elffp); /* no program header table */
1249 fwriteint64_t(0x40L, elffp); /* section headers straight after
1250 * ELF header plus alignment */
1251 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1252 fwriteint16_t(0x40, elffp); /* size of ELF header */
1253 fwriteint16_t(0, elffp); /* no program header table, again */
1254 fwriteint16_t(0, elffp); /* still no program header table */
1255 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1256 fwriteint16_t(nsections, elffp); /* number of sections */
1257 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1258 * section header table */
1261 * Build the symbol table and relocation tables.
1263 symtab = elf_build_symtab(&symtablen, &symtablocal);
1264 for (i = 0; i < nsects; i++)
1265 if (sects[i]->head)
1266 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1267 sects[i]->head);
1270 * Now output the section header table.
1273 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1274 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1275 elf_foffs += align;
1276 elf_nsect = 0;
1277 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1278 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1279 scount = 1; /* needed for the stabs debugging to track the symtable section */
1280 p = shstrtab + 1;
1281 for (i = 0; i < nsects; i++) {
1282 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1283 (sects[i]->type == SHT_PROGBITS ?
1284 sects[i]->data : NULL), true,
1285 sects[i]->len, 0, 0, sects[i]->align, 0);
1286 p += strlen(p) + 1;
1287 scount++; /* ditto */
1289 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1290 scount++; /* ditto */
1291 p += strlen(p) + 1;
1292 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1293 scount++; /* ditto */
1294 p += strlen(p) + 1;
1295 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1296 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1297 p += strlen(p) + 1;
1298 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1299 for (i = 0; i < nsects; i++)
1300 if (sects[i]->head) {
1301 p += strlen(p) + 1;
1302 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1303 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1305 if (of_elf64.current_dfmt == &df_stabs) {
1306 /* for debugging information, create the last three sections
1307 which are the .stab , .stabstr and .rel.stab sections respectively */
1309 /* this function call creates the stab sections in memory */
1310 stabs64_generate();
1312 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1313 p += strlen(p) + 1;
1314 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1315 nsections - 2, 0, 4, 12);
1317 p += strlen(p) + 1;
1318 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1319 stabstrlen, 0, 0, 4, 0);
1321 p += strlen(p) + 1;
1322 /* link -> symtable info -> section to refer to */
1323 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1324 stabrellen, symtabsection, nsections - 3, 4,
1325 16);
1328 else if (of_elf64.current_dfmt == &df_dwarf) {
1329 /* for dwarf debugging information, create the ten dwarf sections */
1331 /* this function call creates the dwarf sections in memory */
1332 if (dwarf_fsect) dwarf64_generate();
1334 p += strlen(p) + 1;
1335 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1336 arangeslen, 0, 0, 1, 0);
1337 p += strlen(p) + 1;
1338 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1339 arangesrellen, symtabsection, debug_aranges, 1, 24);
1340 p += strlen(p) + 1;
1341 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1342 pubnameslen, 0, 0, 1, 0);
1343 p += strlen(p) + 1;
1344 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1345 infolen, 0, 0, 1, 0);
1346 p += strlen(p) + 1;
1347 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1348 inforellen, symtabsection, debug_info, 1, 24);
1349 p += strlen(p) + 1;
1350 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1351 abbrevlen, 0, 0, 1, 0);
1352 p += strlen(p) + 1;
1353 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1354 linelen, 0, 0, 1, 0);
1355 p += strlen(p) + 1;
1356 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1357 linerellen, symtabsection, debug_line, 1, 24);
1358 p += strlen(p) + 1;
1359 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1360 framelen, 0, 0, 8, 0);
1361 p += strlen(p) + 1;
1362 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1363 loclen, 0, 0, 1, 0);
1366 fwrite(align_str, align, 1, elffp);
1369 * Now output the sections.
1371 elf_write_sections();
1373 nasm_free(elf_sects);
1374 saa_free(symtab);
1377 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1379 struct SAA *s = saa_init(1L);
1380 struct Symbol *sym;
1381 uint8_t entry[24], *p;
1382 int i;
1384 *len = *local = 0;
1387 * First, an all-zeros entry, required by the ELF spec.
1389 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1390 *len += 24;
1391 (*local)++;
1394 * Next, an entry for the file name.
1396 p = entry;
1397 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1398 WRITESHORT(p, STT_FILE); /* type FILE */
1399 WRITESHORT(p, SHN_ABS);
1400 WRITEDLONG(p, (uint64_t) 0); /* no value */
1401 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1402 saa_wbytes(s, entry, 24L);
1403 *len += 24;
1404 (*local)++;
1407 * Now some standard symbols defining the segments, for relocation
1408 * purposes.
1410 for (i = 1; i <= nsects; i++) {
1411 p = entry;
1412 WRITELONG(p, 0); /* no symbol name */
1413 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1414 WRITESHORT(p, i); /* section id */
1415 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1416 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1417 saa_wbytes(s, entry, 24L);
1418 *len += 24;
1419 (*local)++;
1424 * Now the other local symbols.
1426 saa_rewind(syms);
1427 while ((sym = saa_rstruct(syms))) {
1428 if (sym->type & SYM_GLOBAL)
1429 continue;
1430 p = entry;
1431 WRITELONG(p, sym->strpos); /* index into symbol string table */
1432 WRITECHAR(p, sym->type); /* type and binding */
1433 WRITECHAR(p, sym->other); /* visibility */
1434 WRITESHORT(p, sym->section); /* index into section header table */
1435 WRITEDLONG(p, (int64_t)sym->value); /* value of symbol */
1436 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1437 saa_wbytes(s, entry, 24L);
1438 *len += 24;
1439 (*local)++;
1442 * dwarf needs symbols for debug sections
1443 * which are relocation targets.
1445 if (of_elf64.current_dfmt == &df_dwarf) {
1446 dwarf_infosym = *local;
1447 p = entry;
1448 WRITELONG(p, 0); /* no symbol name */
1449 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1450 WRITESHORT(p, debug_info); /* section id */
1451 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1452 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1453 saa_wbytes(s, entry, 24L);
1454 *len += 24;
1455 (*local)++;
1456 dwarf_abbrevsym = *local;
1457 p = entry;
1458 WRITELONG(p, 0); /* no symbol name */
1459 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1460 WRITESHORT(p, debug_abbrev); /* section id */
1461 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1462 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1463 saa_wbytes(s, entry, 24L);
1464 *len += 24;
1465 (*local)++;
1466 dwarf_linesym = *local;
1467 p = entry;
1468 WRITELONG(p, 0); /* no symbol name */
1469 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1470 WRITESHORT(p, debug_line); /* section id */
1471 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1472 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1473 saa_wbytes(s, entry, 24L);
1474 *len += 24;
1475 (*local)++;
1479 * Now the global symbols.
1481 saa_rewind(syms);
1482 while ((sym = saa_rstruct(syms))) {
1483 if (!(sym->type & SYM_GLOBAL))
1484 continue;
1485 p = entry;
1486 WRITELONG(p, sym->strpos);
1487 WRITECHAR(p, sym->type); /* type and binding */
1488 WRITECHAR(p, sym->other); /* visibility */
1489 WRITESHORT(p, sym->section);
1490 WRITEDLONG(p, (int64_t)sym->value);
1491 WRITEDLONG(p, (int64_t)sym->size);
1492 saa_wbytes(s, entry, 24L);
1493 *len += 24;
1496 return s;
1499 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1501 struct SAA *s;
1502 uint8_t *p, entry[24];
1504 if (!r)
1505 return NULL;
1507 s = saa_init(1L);
1508 *len = 0;
1510 while (r) {
1511 int64_t sym = r->symbol;
1513 if (sym >= GLOBAL_TEMP_BASE)
1515 if (of_elf64.current_dfmt == &df_dwarf)
1516 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1517 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1519 p = entry;
1520 WRITEDLONG(p, r->address);
1521 WRITEDLONG(p, (sym << 32) + r->type);
1522 WRITEDLONG(p, r->offset);
1523 saa_wbytes(s, entry, 24L);
1524 *len += 24;
1526 r = r->next;
1529 return s;
1532 static void elf_section_header(int name, int type, uint64_t flags,
1533 void *data, bool is_saa, uint64_t datalen,
1534 int link, int info, int align, int eltsize)
1536 elf_sects[elf_nsect].data = data;
1537 elf_sects[elf_nsect].len = datalen;
1538 elf_sects[elf_nsect].is_saa = is_saa;
1539 elf_nsect++;
1541 fwriteint32_t((int32_t)name, elffp);
1542 fwriteint32_t((int32_t)type, elffp);
1543 fwriteint64_t((int64_t)flags, elffp);
1544 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1545 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1546 fwriteint64_t(datalen, elffp);
1547 if (data)
1548 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1549 fwriteint32_t((int32_t)link, elffp);
1550 fwriteint32_t((int32_t)info, elffp);
1551 fwriteint64_t((int64_t)align, elffp);
1552 fwriteint64_t((int64_t)eltsize, elffp);
1555 static void elf_write_sections(void)
1557 int i;
1558 for (i = 0; i < elf_nsect; i++)
1559 if (elf_sects[i].data) {
1560 int32_t len = elf_sects[i].len;
1561 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1562 int32_t align = reallen - len;
1563 if (elf_sects[i].is_saa)
1564 saa_fpwrite(elf_sects[i].data, elffp);
1565 else
1566 fwrite(elf_sects[i].data, len, 1, elffp);
1567 fwrite(align_str, align, 1, elffp);
1571 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1573 saa_wbytes(sect->data, data, len);
1574 sect->len += len;
1576 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1578 saa_writeaddr(sect->data, data, len);
1579 sect->len += len;
1582 static int32_t elf_segbase(int32_t segment)
1584 return segment;
1587 static int elf_directive(char *directive, char *value, int pass)
1589 bool err;
1590 int64_t n;
1591 char *p;
1593 if (!strcmp(directive, "osabi")) {
1594 if (pass == 2)
1595 return 1; /* ignore in pass 2 */
1597 n = readnum(value, &err);
1598 if (err) {
1599 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1600 return 1;
1602 if (n < 0 || n > 255) {
1603 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1604 return 1;
1606 elf_osabi = n;
1607 elf_abiver = 0;
1609 if ((p = strchr(value,',')) == NULL)
1610 return 1;
1612 n = readnum(p+1, &err);
1613 if (err || n < 0 || n > 255) {
1614 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1615 return 1;
1618 elf_abiver = n;
1619 return 1;
1622 return 0;
1625 static void elf_filename(char *inname, char *outname, efunc error)
1627 strcpy(elf_module, inname);
1628 standard_extension(inname, outname, ".o", error);
1631 extern macros_t elf_stdmac[];
1633 static int elf_set_info(enum geninfo type, char **val)
1635 (void)type;
1636 (void)val;
1637 return 0;
1639 static struct dfmt df_dwarf = {
1640 "ELF64 (X86_64) dwarf debug format for Linux",
1641 "dwarf",
1642 debug64_init,
1643 dwarf64_linenum,
1644 debug64_deflabel,
1645 debug64_directive,
1646 debug64_typevalue,
1647 dwarf64_output,
1648 dwarf64_cleanup
1650 static struct dfmt df_stabs = {
1651 "ELF64 (X86_64) stabs debug format for Linux",
1652 "stabs",
1653 debug64_init,
1654 stabs64_linenum,
1655 debug64_deflabel,
1656 debug64_directive,
1657 debug64_typevalue,
1658 stabs64_output,
1659 stabs64_cleanup
1662 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1664 struct ofmt of_elf64 = {
1665 "ELF64 (x86_64) object files (e.g. Linux)",
1666 "elf64",
1667 NULL,
1668 elf64_debugs_arr,
1669 &df_stabs,
1670 elf_stdmac,
1671 elf_init,
1672 elf_set_info,
1673 elf_out,
1674 elf_deflabel,
1675 elf_section_names,
1676 elf_segbase,
1677 elf_directive,
1678 elf_filename,
1679 elf_cleanup
1682 /* common debugging routines */
1683 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1685 (void)of;
1686 (void)id;
1687 (void)fp;
1688 (void)error;
1690 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1691 char *special)
1693 (void)name;
1694 (void)segment;
1695 (void)offset;
1696 (void)is_global;
1697 (void)special;
1700 void debug64_directive(const char *directive, const char *params)
1702 (void)directive;
1703 (void)params;
1706 void debug64_typevalue(int32_t type)
1708 int32_t stype, ssize;
1709 switch (TYM_TYPE(type)) {
1710 case TY_LABEL:
1711 ssize = 0;
1712 stype = STT_NOTYPE;
1713 break;
1714 case TY_BYTE:
1715 ssize = 1;
1716 stype = STT_OBJECT;
1717 break;
1718 case TY_WORD:
1719 ssize = 2;
1720 stype = STT_OBJECT;
1721 break;
1722 case TY_DWORD:
1723 ssize = 4;
1724 stype = STT_OBJECT;
1725 break;
1726 case TY_FLOAT:
1727 ssize = 4;
1728 stype = STT_OBJECT;
1729 break;
1730 case TY_QWORD:
1731 ssize = 8;
1732 stype = STT_OBJECT;
1733 break;
1734 case TY_TBYTE:
1735 ssize = 10;
1736 stype = STT_OBJECT;
1737 break;
1738 case TY_OWORD:
1739 ssize = 16;
1740 stype = STT_OBJECT;
1741 break;
1742 case TY_COMMON:
1743 ssize = 0;
1744 stype = STT_COMMON;
1745 break;
1746 case TY_SEG:
1747 ssize = 0;
1748 stype = STT_SECTION;
1749 break;
1750 case TY_EXTERN:
1751 ssize = 0;
1752 stype = STT_NOTYPE;
1753 break;
1754 case TY_EQU:
1755 ssize = 0;
1756 stype = STT_NOTYPE;
1757 break;
1758 default:
1759 ssize = 0;
1760 stype = STT_NOTYPE;
1761 break;
1763 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1764 lastsym->size = ssize;
1765 lastsym->type = stype;
1769 /* stabs debugging routines */
1772 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1774 (void)segto;
1775 if (!stabs_filename) {
1776 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1777 strcpy(stabs_filename, filename);
1778 } else {
1779 if (strcmp(stabs_filename, filename)) {
1780 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1781 in fact, this leak comes in quite handy to maintain a list of files
1782 encountered so far in the symbol lines... */
1784 /* why not nasm_free(stabs_filename); we're done with the old one */
1786 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1787 strcpy(stabs_filename, filename);
1790 debug_immcall = 1;
1791 currentline = linenumber;
1795 void stabs64_output(int type, void *param)
1797 struct symlininfo *s;
1798 struct linelist *el;
1799 if (type == TY_DEBUGSYMLIN) {
1800 if (debug_immcall) {
1801 s = (struct symlininfo *)param;
1802 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1803 return; /* line info is only collected for executable sections */
1804 numlinestabs++;
1805 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1806 el->info.offset = s->offset;
1807 el->info.section = s->section;
1808 el->info.name = s->name;
1809 el->line = currentline;
1810 el->filename = stabs_filename;
1811 el->next = 0;
1812 if (stabslines) {
1813 stabslines->last->next = el;
1814 stabslines->last = el;
1815 } else {
1816 stabslines = el;
1817 stabslines->last = el;
1821 debug_immcall = 0;
1824 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1825 do {\
1826 WRITELONG(p,n_strx); \
1827 WRITECHAR(p,n_type); \
1828 WRITECHAR(p,n_other); \
1829 WRITESHORT(p,n_desc); \
1830 WRITELONG(p,n_value); \
1831 } while (0)
1833 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1835 void stabs64_generate(void)
1837 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1838 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1839 char **allfiles;
1840 int *fileidx;
1842 struct linelist *ptr;
1844 ptr = stabslines;
1846 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1847 for (i = 0; i < numlinestabs; i++)
1848 allfiles[i] = 0;
1849 numfiles = 0;
1850 while (ptr) {
1851 if (numfiles == 0) {
1852 allfiles[0] = ptr->filename;
1853 numfiles++;
1854 } else {
1855 for (i = 0; i < numfiles; i++) {
1856 if (!strcmp(allfiles[i], ptr->filename))
1857 break;
1859 if (i >= numfiles) {
1860 allfiles[i] = ptr->filename;
1861 numfiles++;
1864 ptr = ptr->next;
1866 strsize = 1;
1867 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1868 for (i = 0; i < numfiles; i++) {
1869 fileidx[i] = strsize;
1870 strsize += strlen(allfiles[i]) + 1;
1872 mainfileindex = 0;
1873 for (i = 0; i < numfiles; i++) {
1874 if (!strcmp(allfiles[i], elf_module)) {
1875 mainfileindex = i;
1876 break;
1880 /* worst case size of the stab buffer would be:
1881 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1883 sbuf =
1884 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1885 sizeof(struct stabentry));
1887 ssbuf = (uint8_t *)nasm_malloc(strsize);
1889 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1890 rptr = rbuf;
1892 for (i = 0; i < numfiles; i++) {
1893 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1895 ssbuf[0] = 0;
1897 stabstrlen = strsize; /* set global variable for length of stab strings */
1899 sptr = sbuf;
1900 ptr = stabslines;
1901 numstabs = 0;
1903 if (ptr) {
1904 /* this is the first stab, its strx points to the filename of the
1905 the source-file, the n_desc field should be set to the number
1906 of remaining stabs
1908 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1910 /* this is the stab for the main source file */
1911 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1913 /* relocation table entry */
1915 /* Since the symbol table has two entries before */
1916 /* the section symbols, the index in the info.section */
1917 /* member must be adjusted by adding 2 */
1919 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1920 WRITELONG(rptr, R_X86_64_32);
1921 WRITELONG(rptr, ptr->info.section + 2);
1923 numstabs++;
1924 currfile = mainfileindex;
1927 while (ptr) {
1928 if (strcmp(allfiles[currfile], ptr->filename)) {
1929 /* oops file has changed... */
1930 for (i = 0; i < numfiles; i++)
1931 if (!strcmp(allfiles[i], ptr->filename))
1932 break;
1933 currfile = i;
1934 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1935 ptr->info.offset);
1936 numstabs++;
1938 /* relocation table entry */
1940 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1941 WRITELONG(rptr, R_X86_64_32);
1942 WRITELONG(rptr, ptr->info.section + 2);
1945 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1946 numstabs++;
1948 /* relocation table entry */
1950 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1951 WRITELONG(rptr, R_X86_64_32);
1952 WRITELONG(rptr, ptr->info.section + 2);
1954 ptr = ptr->next;
1958 ((struct stabentry *)sbuf)->n_desc = numstabs;
1960 nasm_free(allfiles);
1961 nasm_free(fileidx);
1963 stablen = (sptr - sbuf);
1964 stabrellen = (rptr - rbuf);
1965 stabrelbuf = rbuf;
1966 stabbuf = sbuf;
1967 stabstrbuf = ssbuf;
1970 void stabs64_cleanup(void)
1972 struct linelist *ptr, *del;
1973 if (!stabslines)
1974 return;
1975 ptr = stabslines;
1976 while (ptr) {
1977 del = ptr;
1978 ptr = ptr->next;
1979 nasm_free(del);
1981 if (stabbuf)
1982 nasm_free(stabbuf);
1983 if (stabrelbuf)
1984 nasm_free(stabrelbuf);
1985 if (stabstrbuf)
1986 nasm_free(stabstrbuf);
1988 /* dwarf routines */
1991 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1993 (void)segto;
1994 dwarf64_findfile(filename);
1995 debug_immcall = 1;
1996 currentline = linenumber;
1999 /* called from elf_out with type == TY_DEBUGSYMLIN */
2000 void dwarf64_output(int type, void *param)
2002 int ln, aa, inx, maxln, soc;
2003 struct symlininfo *s;
2004 struct SAA *plinep;
2006 (void)type;
2008 s = (struct symlininfo *)param;
2009 /* line number info is only gathered for executable sections */
2010 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2011 return;
2012 /* Check if section index has changed */
2013 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
2015 dwarf64_findsect(s->section);
2017 /* do nothing unless line or file has changed */
2018 if (debug_immcall)
2020 ln = currentline - dwarf_csect->line;
2021 aa = s->offset - dwarf_csect->offset;
2022 inx = dwarf_clist->line;
2023 plinep = dwarf_csect->psaa;
2024 /* check for file change */
2025 if (!(inx == dwarf_csect->file))
2027 saa_write8(plinep,DW_LNS_set_file);
2028 saa_write8(plinep,inx);
2029 dwarf_csect->file = inx;
2031 /* check for line change */
2032 if (ln)
2034 /* test if in range of special op code */
2035 maxln = line_base + line_range;
2036 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2037 if (ln >= line_base && ln < maxln && soc < 256)
2039 saa_write8(plinep,soc);
2041 else
2043 if (ln)
2045 saa_write8(plinep,DW_LNS_advance_line);
2046 saa_wleb128s(plinep,ln);
2048 if (aa)
2050 saa_write8(plinep,DW_LNS_advance_pc);
2051 saa_wleb128u(plinep,aa);
2054 dwarf_csect->line = currentline;
2055 dwarf_csect->offset = s->offset;
2057 /* show change handled */
2058 debug_immcall = 0;
2063 void dwarf64_generate(void)
2065 uint8_t *pbuf;
2066 int indx;
2067 struct linelist *ftentry;
2068 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2069 struct SAA *parangesrel, *plinesrel, *pinforel;
2070 struct sectlist *psect;
2071 size_t saalen, linepoff, totlen, highaddr;
2073 /* write epilogues for each line program range */
2074 /* and build aranges section */
2075 paranges = saa_init(1L);
2076 parangesrel = saa_init(1L);
2077 saa_write16(paranges,3); /* dwarf version */
2078 saa_write64(parangesrel, paranges->datalen+4);
2079 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2080 saa_write64(parangesrel, 0);
2081 saa_write32(paranges,0); /* offset into info */
2082 saa_write8(paranges,8); /* pointer size */
2083 saa_write8(paranges,0); /* not segmented */
2084 saa_write32(paranges,0); /* padding */
2085 /* iterate though sectlist entries */
2086 psect = dwarf_fsect;
2087 totlen = 0;
2088 highaddr = 0;
2089 for (indx = 0; indx < dwarf_nsections; indx++)
2091 plinep = psect->psaa;
2092 /* Line Number Program Epilogue */
2093 saa_write8(plinep,2); /* std op 2 */
2094 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2095 saa_write8(plinep,DW_LNS_extended_op);
2096 saa_write8(plinep,1); /* operand length */
2097 saa_write8(plinep,DW_LNE_end_sequence);
2098 totlen += plinep->datalen;
2099 /* range table relocation entry */
2100 saa_write64(parangesrel, paranges->datalen + 4);
2101 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2102 saa_write64(parangesrel, (uint64_t) 0);
2103 /* range table entry */
2104 saa_write64(paranges,0x0000); /* range start */
2105 saa_write64(paranges,sects[psect->section]->len); /* range length */
2106 highaddr += sects[psect->section]->len;
2107 /* done with this entry */
2108 psect = psect->next;
2110 saa_write64(paranges,0); /* null address */
2111 saa_write64(paranges,0); /* null length */
2112 saalen = paranges->datalen;
2113 arangeslen = saalen + 4;
2114 arangesbuf = pbuf = nasm_malloc(arangeslen);
2115 WRITELONG(pbuf,saalen); /* initial length */
2116 saa_rnbytes(paranges, pbuf, saalen);
2117 saa_free(paranges);
2119 /* build rela.aranges section */
2120 arangesrellen = saalen = parangesrel->datalen;
2121 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2122 saa_rnbytes(parangesrel, pbuf, saalen);
2123 saa_free(parangesrel);
2125 /* build pubnames section */
2126 ppubnames = saa_init(1L);
2127 saa_write16(ppubnames,3); /* dwarf version */
2128 saa_write32(ppubnames,0); /* offset into info */
2129 saa_write32(ppubnames,0); /* space used in info */
2130 saa_write32(ppubnames,0); /* end of list */
2131 saalen = ppubnames->datalen;
2132 pubnameslen = saalen + 4;
2133 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2134 WRITELONG(pbuf,saalen); /* initial length */
2135 saa_rnbytes(ppubnames, pbuf, saalen);
2136 saa_free(ppubnames);
2138 /* build info section */
2139 pinfo = saa_init(1L);
2140 pinforel = saa_init(1L);
2141 saa_write16(pinfo,3); /* dwarf version */
2142 saa_write64(pinforel, pinfo->datalen + 4);
2143 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2144 saa_write64(pinforel, 0);
2145 saa_write32(pinfo,0); /* offset into abbrev */
2146 saa_write8(pinfo,8); /* pointer size */
2147 saa_write8(pinfo,1); /* 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(pinforel, pinfo->datalen + 4);
2153 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2154 saa_write64(pinforel, 0);
2155 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2156 saa_write64(pinforel, pinfo->datalen + 4);
2157 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2158 saa_write64(pinforel, 0);
2159 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2160 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2161 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2162 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2163 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2164 saa_write64(pinforel, pinfo->datalen + 4);
2165 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2166 saa_write64(pinforel, 0);
2167 saa_write64(pinfo,0); /* DW_AT_low_pc */
2168 saa_write64(pinfo,0); /* DW_AT_frame_base */
2169 saa_write8(pinfo,0); /* end of entries */
2170 saalen = pinfo->datalen;
2171 infolen = saalen + 4;
2172 infobuf = pbuf = nasm_malloc(infolen);
2173 WRITELONG(pbuf,saalen); /* initial length */
2174 saa_rnbytes(pinfo, pbuf, saalen);
2175 saa_free(pinfo);
2177 /* build rela.info section */
2178 inforellen = saalen = pinforel->datalen;
2179 inforelbuf = pbuf = nasm_malloc(inforellen);
2180 saa_rnbytes(pinforel, pbuf, saalen);
2181 saa_free(pinforel);
2183 /* build abbrev section */
2184 pabbrev = saa_init(1L);
2185 saa_write8(pabbrev,1); /* entry number LEB128u */
2186 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2187 saa_write8(pabbrev,1); /* has children */
2188 /* the following attributes and forms are all LEB128u values */
2189 saa_write8(pabbrev,DW_AT_low_pc);
2190 saa_write8(pabbrev,DW_FORM_addr);
2191 saa_write8(pabbrev,DW_AT_high_pc);
2192 saa_write8(pabbrev,DW_FORM_addr);
2193 saa_write8(pabbrev,DW_AT_stmt_list);
2194 saa_write8(pabbrev,DW_FORM_data4);
2195 saa_write8(pabbrev,DW_AT_name);
2196 saa_write8(pabbrev,DW_FORM_string);
2197 saa_write8(pabbrev,DW_AT_producer);
2198 saa_write8(pabbrev,DW_FORM_string);
2199 saa_write8(pabbrev,DW_AT_language);
2200 saa_write8(pabbrev,DW_FORM_data2);
2201 saa_write16(pabbrev,0); /* end of entry */
2202 /* LEB128u usage same as above */
2203 saa_write8(pabbrev,2); /* entry number */
2204 saa_write8(pabbrev,DW_TAG_subprogram);
2205 saa_write8(pabbrev,0); /* no children */
2206 saa_write8(pabbrev,DW_AT_low_pc);
2207 saa_write8(pabbrev,DW_FORM_addr);
2208 saa_write8(pabbrev,DW_AT_frame_base);
2209 saa_write8(pabbrev,DW_FORM_data4);
2210 saa_write16(pabbrev,0); /* end of entry */
2211 abbrevlen = saalen = pabbrev->datalen;
2212 abbrevbuf = pbuf = nasm_malloc(saalen);
2213 saa_rnbytes(pabbrev, pbuf, saalen);
2214 saa_free(pabbrev);
2216 /* build line section */
2217 /* prolog */
2218 plines = saa_init(1L);
2219 saa_write8(plines,1); /* Minimum Instruction Length */
2220 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2221 saa_write8(plines,line_base); /* Line Base */
2222 saa_write8(plines,line_range); /* Line Range */
2223 saa_write8(plines,opcode_base); /* Opcode Base */
2224 /* standard opcode lengths (# of LEB128u operands) */
2225 saa_write8(plines,0); /* Std opcode 1 length */
2226 saa_write8(plines,1); /* Std opcode 2 length */
2227 saa_write8(plines,1); /* Std opcode 3 length */
2228 saa_write8(plines,1); /* Std opcode 4 length */
2229 saa_write8(plines,1); /* Std opcode 5 length */
2230 saa_write8(plines,0); /* Std opcode 6 length */
2231 saa_write8(plines,0); /* Std opcode 7 length */
2232 saa_write8(plines,0); /* Std opcode 8 length */
2233 saa_write8(plines,1); /* Std opcode 9 length */
2234 saa_write8(plines,0); /* Std opcode 10 length */
2235 saa_write8(plines,0); /* Std opcode 11 length */
2236 saa_write8(plines,1); /* Std opcode 12 length */
2237 /* Directory Table */
2238 saa_write8(plines,0); /* End of table */
2239 /* File Name Table */
2240 ftentry = dwarf_flist;
2241 for (indx = 0;indx<dwarf_numfiles;indx++)
2243 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2244 saa_write8(plines,0); /* directory LEB128u */
2245 saa_write8(plines,0); /* time LEB128u */
2246 saa_write8(plines,0); /* size LEB128u */
2247 ftentry = ftentry->next;
2249 saa_write8(plines,0); /* End of table */
2250 linepoff = plines->datalen;
2251 linelen = linepoff + totlen + 10;
2252 linebuf = pbuf = nasm_malloc(linelen);
2253 WRITELONG(pbuf,linelen-4); /* initial length */
2254 WRITESHORT(pbuf,3); /* dwarf version */
2255 WRITELONG(pbuf,linepoff); /* offset to line number program */
2256 /* write line header */
2257 saalen = linepoff;
2258 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2259 pbuf += linepoff;
2260 saa_free(plines);
2261 /* concatonate line program ranges */
2262 linepoff += 13;
2263 plinesrel = saa_init(1L);
2264 psect = dwarf_fsect;
2265 for (indx = 0; indx < dwarf_nsections; indx++)
2267 saa_write64(plinesrel, linepoff);
2268 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2269 saa_write64(plinesrel, (uint64_t) 0);
2270 plinep = psect->psaa;
2271 saalen = plinep->datalen;
2272 saa_rnbytes(plinep, pbuf, saalen);
2273 pbuf += saalen;
2274 linepoff += saalen;
2275 saa_free(plinep);
2276 /* done with this entry */
2277 psect = psect->next;
2281 /* build rela.lines section */
2282 linerellen =saalen = plinesrel->datalen;
2283 linerelbuf = pbuf = nasm_malloc(linerellen);
2284 saa_rnbytes(plinesrel, pbuf, saalen);
2285 saa_free(plinesrel);
2287 /* build frame section */
2288 framelen = 4;
2289 framebuf = pbuf = nasm_malloc(framelen);
2290 WRITELONG(pbuf,framelen-4); /* initial length */
2292 /* build loc section */
2293 loclen = 16;
2294 locbuf = pbuf = nasm_malloc(loclen);
2295 WRITEDLONG(pbuf,0); /* null beginning offset */
2296 WRITEDLONG(pbuf,0); /* null ending offset */
2299 void dwarf64_cleanup(void)
2301 if (arangesbuf)
2302 nasm_free(arangesbuf);
2303 if (arangesrelbuf)
2304 nasm_free(arangesrelbuf);
2305 if (pubnamesbuf)
2306 nasm_free(pubnamesbuf);
2307 if (infobuf)
2308 nasm_free(infobuf);
2309 if (inforelbuf)
2310 nasm_free(inforelbuf);
2311 if (abbrevbuf)
2312 nasm_free(abbrevbuf);
2313 if (linebuf)
2314 nasm_free(linebuf);
2315 if (linerelbuf)
2316 nasm_free(linerelbuf);
2317 if (framebuf)
2318 nasm_free(framebuf);
2319 if (locbuf)
2320 nasm_free(locbuf);
2322 void dwarf64_findfile(const char * fname)
2324 int finx;
2325 struct linelist *match;
2327 /* return if fname is current file name */
2328 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2329 /* search for match */
2330 else
2332 match = 0;
2333 if (dwarf_flist)
2335 match = dwarf_flist;
2336 for (finx = 0; finx < dwarf_numfiles; finx++)
2338 if (!(strcmp(fname, match->filename)))
2340 dwarf_clist = match;
2341 return;
2345 /* add file name to end of list */
2346 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2347 dwarf_numfiles++;
2348 dwarf_clist->line = dwarf_numfiles;
2349 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2350 strcpy(dwarf_clist->filename,fname);
2351 dwarf_clist->next = 0;
2352 /* if first entry */
2353 if (!dwarf_flist)
2355 dwarf_flist = dwarf_elist = dwarf_clist;
2356 dwarf_clist->last = 0;
2358 /* chain to previous entry */
2359 else
2361 dwarf_elist->next = dwarf_clist;
2362 dwarf_elist = dwarf_clist;
2366 /* */
2367 void dwarf64_findsect(const int index)
2369 int sinx;
2370 struct sectlist *match;
2371 struct SAA *plinep;
2372 /* return if index is current section index */
2373 if (dwarf_csect && (dwarf_csect->section == index))
2375 return;
2377 /* search for match */
2378 else
2380 match = 0;
2381 if (dwarf_fsect)
2383 match = dwarf_fsect;
2384 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2386 if ((match->section == index))
2388 dwarf_csect = match;
2389 return;
2391 match = match->next;
2394 /* add entry to end of list */
2395 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2396 dwarf_nsections++;
2397 dwarf_csect->psaa = plinep = saa_init(1L);
2398 dwarf_csect->line = 1;
2399 dwarf_csect->offset = 0;
2400 dwarf_csect->file = 1;
2401 dwarf_csect->section = index;
2402 dwarf_csect->next = 0;
2403 /* set relocatable address at start of line program */
2404 saa_write8(plinep,DW_LNS_extended_op);
2405 saa_write8(plinep,9); /* operand length */
2406 saa_write8(plinep,DW_LNE_set_address);
2407 saa_write64(plinep,0); /* Start Address */
2408 /* if first entry */
2409 if (!dwarf_fsect)
2411 dwarf_fsect = dwarf_esect = dwarf_csect;
2412 dwarf_csect->last = 0;
2414 /* chain to previous entry */
2415 else
2417 dwarf_esect->next = dwarf_csect;
2418 dwarf_esect = dwarf_csect;
2423 #endif /* OF_ELF */