NASM 2.05rc4
[nasm/avx512.git] / output / outelf64.c
blob02f2d07e60836788c85f8d97729a4b426049883c
1 /* outelf64.c output routines for the Netwide Assembler to produce
2 * ELF64 (x86_64 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
9 #include "compiler.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <inttypes.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "saa.h"
20 #include "raa.h"
21 #include "stdscan.h"
22 #include "outform.h"
24 /* Definitions in lieu of elf.h */
25 #define SHT_NULL 0 /* Inactive section header */
26 #define SHT_PROGBITS 1 /* Program defined content */
27 #define SHT_RELA 4 /* Relocation entries with addends */
28 #define SHT_NOBITS 8 /* Section requires no space in file */
29 #define SHF_WRITE (1 << 0) /* Writable */
30 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
31 #define SHF_EXECINSTR (1 << 2) /* Executable */
32 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
33 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
34 #define R_X86_64_NONE 0 /* No reloc */
35 #define R_X86_64_64 1 /* Direct 64 bit */
36 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
37 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
38 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
39 #define R_X86_64_COPY 5 /* Copy symbol at runtime */
40 #define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
41 #define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
42 #define R_X86_64_RELATIVE 8 /* Adjust by program base */
43 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative
44 offset to GOT */
45 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
46 #define R_X86_64_32S 11 /* Direct 32 bit sign extended */
47 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
48 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
49 #define R_X86_64_8 14 /* Direct 8 bit sign extended */
50 #define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
51 #define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */
52 #define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */
53 #define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */
54 #define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset
55 to two GOT entries for GD symbol */
56 #define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset
57 to two GOT entries for LD symbol */
58 #define R_X86_64_DTPOFF32 21 /* Offset in TLS block */
59 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset
60 to GOT entry for IE symbol */
61 #define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */
62 #define R_X86_64_PC64 24 /* word64 S + A - P */
63 #define R_X86_64_GOTOFF64 25 /* word64 S + A - GOT */
64 #define R_X86_64_GOTPC32 26 /* word32 GOT + A - P */
65 #define R_X86_64_GOT64 27 /* word64 G + A */
66 #define R_X86_64_GOTPCREL64 28 /* word64 G + GOT - P + A */
67 #define R_X86_64_GOTPC64 29 /* word64 GOT - P + A */
68 #define R_X86_64_GOTPLT64 30 /* word64 G + A */
69 #define R_X86_64_PLTOFF64 31 /* word64 L - GOT + A */
70 #define R_X86_64_SIZE32 32 /* word32 Z + A */
71 #define R_X86_64_SIZE64 33 /* word64 Z + A */
72 #define R_X86_64_GOTPC32_TLSDESC 34 /* word32 */
73 #define R_X86_64_TLSDESC_CALL 35 /* none */
74 #define R_X86_64_TLSDESC 36 /* word64×2 */
75 #define ET_REL 1 /* Relocatable file */
76 #define EM_X86_64 62 /* AMD x86-64 architecture */
77 #define STT_NOTYPE 0 /* Symbol type is unspecified */
78 #define STT_OBJECT 1 /* Symbol is a data object */
79 #define STT_FUNC 2 /* Symbol is a code object */
80 #define STT_SECTION 3 /* Symbol associated with a section */
81 #define STT_FILE 4 /* Symbol's name is file name */
82 #define STT_COMMON 5 /* Symbol is a common data object */
83 #define STT_TLS 6 /* Symbol is thread-local data object*/
84 #define STT_NUM 7 /* Number of defined types. */
86 /* Definitions in lieu of dwarf.h */
87 #define DW_TAG_compile_unit 0x11
88 #define DW_TAG_subprogram 0x2e
89 #define DW_AT_name 0x03
90 #define DW_AT_stmt_list 0x10
91 #define DW_AT_low_pc 0x11
92 #define DW_AT_high_pc 0x12
93 #define DW_AT_language 0x13
94 #define DW_AT_producer 0x25
95 #define DW_AT_frame_base 0x40
96 #define DW_FORM_addr 0x01
97 #define DW_FORM_data2 0x05
98 #define DW_FORM_data4 0x06
99 #define DW_FORM_string 0x08
100 #define DW_LNS_extended_op 0
101 #define DW_LNS_advance_pc 2
102 #define DW_LNS_advance_line 3
103 #define DW_LNS_set_file 4
104 #define DW_LNE_end_sequence 1
105 #define DW_LNE_set_address 2
106 #define DW_LNE_define_file 3
107 #define DW_LANG_Mips_Assembler 0x8001
109 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
111 typedef uint32_t Elf64_Word;
112 typedef uint64_t Elf64_Xword;
113 typedef uint64_t Elf64_Addr;
114 typedef uint64_t Elf64_Off;
115 typedef struct
117 Elf64_Word sh_name; /* Section name (string tbl index) */
118 Elf64_Word sh_type; /* Section type */
119 Elf64_Xword sh_flags; /* Section flags */
120 Elf64_Addr sh_addr; /* Section virtual addr at execution */
121 Elf64_Off sh_offset; /* Section file offset */
122 Elf64_Xword sh_size; /* Section size in bytes */
123 Elf64_Word sh_link; /* Link to another section */
124 Elf64_Word sh_info; /* Additional section information */
125 Elf64_Xword sh_addralign; /* Section alignment */
126 Elf64_Xword sh_entsize; /* Entry size if section holds table */
127 } Elf64_Shdr;
130 #ifdef OF_ELF64
133 struct Reloc {
134 struct Reloc *next;
135 int64_t address; /* relative to _start_ of section */
136 int64_t symbol; /* symbol index */
137 int type; /* type of relocation */
140 struct Symbol {
141 int32_t strpos; /* string table position of name */
142 int32_t section; /* section ID of the symbol */
143 int type; /* symbol type */
144 int other; /* symbol visibility */
145 int64_t value; /* address, or COMMON variable align */
146 int32_t size; /* size of symbol */
147 int32_t globnum; /* symbol table offset if global */
148 struct Symbol *next; /* list of globals in each section */
149 struct Symbol *nextfwd; /* list of unresolved-size symbols */
150 char *name; /* used temporarily if in above list */
154 struct Section {
155 struct SAA *data;
156 uint64_t len, size;
157 uint32_t nrelocs;
158 int32_t index; /* index into sects array */
159 uint32_t type; /* SHT_PROGBITS or SHT_NOBITS */
160 uint64_t align; /* alignment: power of two */
161 uint64_t flags; /* section flags */
162 char *name;
163 struct SAA *rel;
164 uint64_t rellen;
165 struct Reloc *head, **tail;
166 struct Symbol *gsyms; /* global symbols in section */
169 #define SECT_DELTA 32
170 static struct Section **sects;
171 static int nsects, sectlen;
173 #define SHSTR_DELTA 256
174 static char *shstrtab;
175 static int shstrtablen, shstrtabsize;
177 static struct SAA *syms;
178 static uint32_t nlocals, nglobs;
180 static int32_t def_seg;
182 static struct RAA *bsym;
184 static struct SAA *strs;
185 static uint32_t strslen;
187 static FILE *elffp;
188 static efunc error;
189 static evalfunc evaluate;
191 static struct Symbol *fwds;
193 static char elf_module[FILENAME_MAX];
195 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
196 static uint8_t elf_abiver = 0; /* Current ABI version */
198 extern struct ofmt of_elf64;
200 #define SHN_UNDEF 0
202 #define SYM_GLOBAL 0x10
204 #define STV_DEFAULT 0
205 #define STV_INTERNAL 1
206 #define STV_HIDDEN 2
207 #define STV_PROTECTED 3
209 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
211 #define SEG_ALIGN 16 /* alignment of sections in file */
212 #define SEG_ALIGN_1 (SEG_ALIGN-1)
214 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
216 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
218 static struct ELF_SECTDATA {
219 void *data;
220 int64_t len;
221 bool is_saa;
222 } *elf_sects;
223 static int elf_nsect, nsections;
224 static int64_t elf_foffs;
226 static void elf_write(void);
227 static void elf_sect_write(struct Section *, const uint8_t *,
228 uint64_t);
229 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
230 int, int);
231 static void elf_write_sections(void);
232 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
233 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
234 static void add_sectname(char *, char *);
236 /* type values for stabs debugging sections */
237 #define N_SO 0x64 /* ID for main source file */
238 #define N_SOL 0x84 /* ID for sub-source file */
239 #define N_BINCL 0x82 /* not currently used */
240 #define N_EINCL 0xA2 /* not currently used */
241 #define N_SLINE 0x44
243 struct stabentry {
244 uint32_t n_strx;
245 uint8_t n_type;
246 uint8_t n_other;
247 uint16_t n_desc;
248 uint32_t n_value;
251 struct erel {
252 int offset, info;
255 struct symlininfo {
256 int offset;
257 int section; /* index into sects[] */
258 int segto; /* internal section number */
259 char *name; /* shallow-copied pointer of section name */
262 struct linelist {
263 struct symlininfo info;
264 int line;
265 char *filename;
266 struct linelist *next;
267 struct linelist *last;
270 struct sectlist {
271 struct SAA *psaa;
272 int section;
273 int line;
274 int offset;
275 int file;
276 struct sectlist *next;
277 struct sectlist *last;
280 /* common debug variables */
281 static int currentline = 1;
282 static int debug_immcall = 0;
284 /* stabs debug variables */
285 static struct linelist *stabslines = 0;
286 static int numlinestabs = 0;
287 static char *stabs_filename = 0;
288 static int symtabsection;
289 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
290 static int stablen, stabstrlen, stabrellen;
292 /* dwarf debug variables */
293 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
294 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
295 static int dwarf_numfiles = 0, dwarf_nsections;
296 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
297 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
298 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
299 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
300 abbrevlen, linelen, linerellen, framelen, loclen;
301 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
304 static struct dfmt df_dwarf;
305 static struct dfmt df_stabs;
306 static struct Symbol *lastsym;
308 /* common debugging routines */
309 void debug64_typevalue(int32_t);
310 void debug64_init(struct ofmt *, void *, FILE *, efunc);
311 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
312 void debug64_directive(const char *, const char *);
314 /* stabs debugging routines */
315 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
316 void stabs64_output(int, void *);
317 void stabs64_generate(void);
318 void stabs64_cleanup(void);
320 /* dwarf debugging routines */
321 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
322 void dwarf64_output(int, void *);
323 void dwarf64_generate(void);
324 void dwarf64_cleanup(void);
325 void dwarf64_findfile(const char *);
326 void dwarf64_findsect(const int);
329 * Special section numbers which are used to define ELF special
330 * symbols, which can be used with WRT to provide PIC relocation
331 * types.
333 static int32_t elf_gotpc_sect, elf_gotoff_sect;
334 static int32_t elf_got_sect, elf_plt_sect;
335 static int32_t elf_sym_sect;
337 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
339 maxbits = 64;
340 elffp = fp;
341 error = errfunc;
342 evaluate = eval;
343 (void)ldef; /* placate optimisers */
344 sects = NULL;
345 nsects = sectlen = 0;
346 syms = saa_init((int32_t)sizeof(struct Symbol));
347 nlocals = nglobs = 0;
348 bsym = raa_init();
349 strs = saa_init(1L);
350 saa_wbytes(strs, "\0", 1L);
351 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
352 strslen = 2 + strlen(elf_module);
353 shstrtab = NULL;
354 shstrtablen = shstrtabsize = 0;;
355 add_sectname("", "");
357 fwds = NULL;
359 elf_gotpc_sect = seg_alloc();
360 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
361 error);
362 elf_gotoff_sect = seg_alloc();
363 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
364 error);
365 elf_got_sect = seg_alloc();
366 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
367 error);
368 elf_plt_sect = seg_alloc();
369 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
370 error);
371 elf_sym_sect = seg_alloc();
372 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
373 error);
375 def_seg = seg_alloc();
379 static void elf_cleanup(int debuginfo)
381 struct Reloc *r;
382 int i;
384 (void)debuginfo;
386 elf_write();
387 fclose(elffp);
388 for (i = 0; i < nsects; i++) {
389 if (sects[i]->type != SHT_NOBITS)
390 saa_free(sects[i]->data);
391 if (sects[i]->head)
392 saa_free(sects[i]->rel);
393 while (sects[i]->head) {
394 r = sects[i]->head;
395 sects[i]->head = sects[i]->head->next;
396 nasm_free(r);
399 nasm_free(sects);
400 saa_free(syms);
401 raa_free(bsym);
402 saa_free(strs);
403 if (of_elf64.current_dfmt) {
404 of_elf64.current_dfmt->cleanup();
407 /* add entry to the elf .shstrtab section */
408 static void add_sectname(char *firsthalf, char *secondhalf)
410 int len = strlen(firsthalf) + strlen(secondhalf);
411 while (shstrtablen + len + 1 > shstrtabsize)
412 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
413 strcpy(shstrtab + shstrtablen, firsthalf);
414 strcat(shstrtab + shstrtablen, secondhalf);
415 shstrtablen += len + 1;
418 static int elf_make_section(char *name, int type, int flags, int align)
420 struct Section *s;
422 s = nasm_malloc(sizeof(*s));
424 if (type != SHT_NOBITS)
425 s->data = saa_init(1L);
426 s->head = NULL;
427 s->tail = &s->head;
428 s->len = s->size = 0;
429 s->nrelocs = 0;
430 if (!strcmp(name, ".text"))
431 s->index = def_seg;
432 else
433 s->index = seg_alloc();
434 add_sectname("", name);
435 s->name = nasm_malloc(1 + strlen(name));
436 strcpy(s->name, name);
437 s->type = type;
438 s->flags = flags;
439 s->align = align;
440 s->gsyms = NULL;
442 if (nsects >= sectlen)
443 sects =
444 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
445 sects[nsects++] = s;
447 return nsects - 1;
450 static int32_t elf_section_names(char *name, int pass, int *bits)
452 char *p;
453 unsigned flags_and, flags_or;
454 uint64_t type, align;
455 int i;
458 * Default is 64 bits.
460 if (!name) {
461 *bits = 64;
462 return def_seg;
465 p = name;
466 while (*p && !nasm_isspace(*p))
467 p++;
468 if (*p)
469 *p++ = '\0';
470 flags_and = flags_or = type = align = 0;
472 while (*p && nasm_isspace(*p))
473 p++;
474 while (*p) {
475 char *q = p;
476 while (*p && !nasm_isspace(*p))
477 p++;
478 if (*p)
479 *p++ = '\0';
480 while (*p && nasm_isspace(*p))
481 p++;
483 if (!nasm_strnicmp(q, "align=", 6)) {
484 align = atoi(q + 6);
485 if (align == 0)
486 align = 1;
487 if ((align - 1) & align) { /* means it's not a power of two */
488 error(ERR_NONFATAL, "section alignment %d is not"
489 " a power of two", align);
490 align = 1;
492 } else if (!nasm_stricmp(q, "alloc")) {
493 flags_and |= SHF_ALLOC;
494 flags_or |= SHF_ALLOC;
495 } else if (!nasm_stricmp(q, "noalloc")) {
496 flags_and |= SHF_ALLOC;
497 flags_or &= ~SHF_ALLOC;
498 } else if (!nasm_stricmp(q, "exec")) {
499 flags_and |= SHF_EXECINSTR;
500 flags_or |= SHF_EXECINSTR;
501 } else if (!nasm_stricmp(q, "noexec")) {
502 flags_and |= SHF_EXECINSTR;
503 flags_or &= ~SHF_EXECINSTR;
504 } else if (!nasm_stricmp(q, "write")) {
505 flags_and |= SHF_WRITE;
506 flags_or |= SHF_WRITE;
507 } else if (!nasm_stricmp(q, "nowrite")) {
508 flags_and |= SHF_WRITE;
509 flags_or &= ~SHF_WRITE;
510 } else if (!nasm_stricmp(q, "progbits")) {
511 type = SHT_PROGBITS;
512 } else if (!nasm_stricmp(q, "nobits")) {
513 type = SHT_NOBITS;
517 if (!strcmp(name, ".comment") ||
518 !strcmp(name, ".shstrtab") ||
519 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
520 error(ERR_NONFATAL, "attempt to redefine reserved section"
521 "name `%s'", name);
522 return NO_SEG;
525 for (i = 0; i < nsects; i++)
526 if (!strcmp(name, sects[i]->name))
527 break;
528 if (i == nsects) {
529 if (!strcmp(name, ".text"))
530 i = elf_make_section(name, SHT_PROGBITS,
531 SHF_ALLOC | SHF_EXECINSTR, 16);
532 else if (!strcmp(name, ".rodata"))
533 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
534 else if (!strcmp(name, ".data"))
535 i = elf_make_section(name, SHT_PROGBITS,
536 SHF_ALLOC | SHF_WRITE, 4);
537 else if (!strcmp(name, ".bss"))
538 i = elf_make_section(name, SHT_NOBITS,
539 SHF_ALLOC | SHF_WRITE, 4);
540 else
541 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
542 if (type)
543 sects[i]->type = type;
544 if (align)
545 sects[i]->align = align;
546 sects[i]->flags &= ~flags_and;
547 sects[i]->flags |= flags_or;
548 } else if (pass == 1) {
549 if ((type && sects[i]->type != type)
550 || (align && sects[i]->align != align)
551 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
552 error(ERR_WARNING, "incompatible section attributes ignored on"
553 " redeclaration of section `%s'", name);
556 return sects[i]->index;
559 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
560 int is_global, char *special)
562 int pos = strslen;
563 struct Symbol *sym;
564 bool special_used = false;
566 #if defined(DEBUG) && DEBUG>2
567 fprintf(stderr,
568 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
569 name, segment, offset, is_global, special);
570 #endif
571 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
573 * This is a NASM special symbol. We never allow it into
574 * the ELF symbol table, even if it's a valid one. If it
575 * _isn't_ a valid one, we should barf immediately.
577 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
578 strcmp(name, "..got") && strcmp(name, "..plt") &&
579 strcmp(name, "..sym"))
580 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
581 return;
584 if (is_global == 3) {
585 struct Symbol **s;
587 * Fix up a forward-reference symbol size from the first
588 * pass.
590 for (s = &fwds; *s; s = &(*s)->nextfwd)
591 if (!strcmp((*s)->name, name)) {
592 struct tokenval tokval;
593 expr *e;
594 char *p = special;
596 while (*p && !nasm_isspace(*p))
597 p++;
598 while (*p && nasm_isspace(*p))
599 p++;
600 stdscan_reset();
601 stdscan_bufptr = p;
602 tokval.t_type = TOKEN_INVALID;
603 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
604 if (e) {
605 if (!is_simple(e))
606 error(ERR_NONFATAL, "cannot use relocatable"
607 " expression as symbol size");
608 else
609 (*s)->size = reloc_value(e);
613 * Remove it from the list of unresolved sizes.
615 nasm_free((*s)->name);
616 *s = (*s)->nextfwd;
617 return;
619 return; /* it wasn't an important one */
622 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
623 strslen += 1 + strlen(name);
625 lastsym = sym = saa_wstruct(syms);
627 sym->strpos = pos;
628 sym->type = is_global ? SYM_GLOBAL : 0;
629 sym->other = STV_DEFAULT;
630 sym->size = 0;
631 if (segment == NO_SEG)
632 sym->section = SHN_ABS;
633 else {
634 int i;
635 sym->section = SHN_UNDEF;
636 if (nsects == 0 && segment == def_seg) {
637 int tempint;
638 if (segment != elf_section_names(".text", 2, &tempint))
639 error(ERR_PANIC,
640 "strange segment conditions in ELF driver");
641 sym->section = nsects;
642 } else {
643 for (i = 0; i < nsects; i++)
644 if (segment == sects[i]->index) {
645 sym->section = i + 1;
646 break;
651 if (is_global == 2) {
652 sym->size = offset;
653 sym->value = 0;
654 sym->section = SHN_COMMON;
656 * We have a common variable. Check the special text to see
657 * if it's a valid number and power of two; if so, store it
658 * as the alignment for the common variable.
660 if (special) {
661 bool err;
662 sym->value = readnum(special, &err);
663 if (err)
664 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
665 " valid number", special);
666 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
667 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
668 " power of two", special);
670 special_used = true;
671 } else
672 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
674 if (sym->type == SYM_GLOBAL) {
676 * If sym->section == SHN_ABS, then the first line of the
677 * else section would cause a core dump, because its a reference
678 * beyond the end of the section array.
679 * This behaviour is exhibited by this code:
680 * GLOBAL crash_nasm
681 * crash_nasm equ 0
682 * To avoid such a crash, such requests are silently discarded.
683 * This may not be the best solution.
685 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
686 bsym = raa_write(bsym, segment, nglobs);
687 } else if (sym->section != SHN_ABS) {
689 * This is a global symbol; so we must add it to the linked
690 * list of global symbols in its section. We'll push it on
691 * the beginning of the list, because it doesn't matter
692 * much which end we put it on and it's easier like this.
694 * In addition, we check the special text for symbol
695 * type and size information.
697 sym->next = sects[sym->section - 1]->gsyms;
698 sects[sym->section - 1]->gsyms = sym;
700 if (special) {
701 int n = strcspn(special, " \t");
703 if (!nasm_strnicmp(special, "function", n))
704 sym->type |= STT_FUNC;
705 else if (!nasm_strnicmp(special, "data", n) ||
706 !nasm_strnicmp(special, "object", n))
707 sym->type |= STT_OBJECT;
708 else if (!nasm_strnicmp(special, "notype", n))
709 sym->type |= STT_NOTYPE;
710 else
711 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
712 n, special);
713 special += n;
715 while (nasm_isspace(*special))
716 ++special;
717 if (*special) {
718 n = strcspn(special, " \t");
719 if (!nasm_strnicmp(special, "default", n))
720 sym->other = STV_DEFAULT;
721 else if (!nasm_strnicmp(special, "internal", n))
722 sym->other = STV_INTERNAL;
723 else if (!nasm_strnicmp(special, "hidden", n))
724 sym->other = STV_HIDDEN;
725 else if (!nasm_strnicmp(special, "protected", n))
726 sym->other = STV_PROTECTED;
727 else
728 n = 0;
729 special += n;
732 if (*special) {
733 struct tokenval tokval;
734 expr *e;
735 int fwd = 0;
736 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
738 while (special[n] && nasm_isspace(special[n]))
739 n++;
741 * We have a size expression; attempt to
742 * evaluate it.
744 stdscan_reset();
745 stdscan_bufptr = special + n;
746 tokval.t_type = TOKEN_INVALID;
747 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
748 NULL);
749 if (fwd) {
750 sym->nextfwd = fwds;
751 fwds = sym;
752 sym->name = nasm_strdup(name);
753 } else if (e) {
754 if (!is_simple(e))
755 error(ERR_NONFATAL, "cannot use relocatable"
756 " expression as symbol size");
757 else
758 sym->size = reloc_value(e);
760 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
762 special_used = true;
765 sym->globnum = nglobs;
766 nglobs++;
767 } else
768 nlocals++;
770 if (special && !special_used)
771 error(ERR_NONFATAL, "no special symbol features supported here");
774 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
776 struct Reloc *r;
777 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
778 sect->tail = &r->next;
779 r->next = NULL;
781 r->address = sect->len;
782 if (segment == NO_SEG)
783 r->symbol = 0;
784 else {
785 int i;
786 r->symbol = 0;
787 for (i = 0; i < nsects; i++)
788 if (segment == sects[i]->index)
789 r->symbol = i + 2;
790 if (!r->symbol)
791 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
793 r->type = type;
795 sect->nrelocs++;
799 * This routine deals with ..got and ..sym relocations: the more
800 * complicated kinds. In shared-library writing, some relocations
801 * with respect to global symbols must refer to the precise symbol
802 * rather than referring to an offset from the base of the section
803 * _containing_ the symbol. Such relocations call to this routine,
804 * which searches the symbol list for the symbol in question.
806 * R_386_GOT32 references require the _exact_ symbol address to be
807 * used; R_386_32 references can be at an offset from the symbol.
808 * The boolean argument `exact' tells us this.
810 * Return value is the adjusted value of `addr', having become an
811 * offset from the symbol rather than the section. Should always be
812 * zero when returning from an exact call.
814 * Limitation: if you define two symbols at the same place,
815 * confusion will occur.
817 * Inefficiency: we search, currently, using a linked list which
818 * isn't even necessarily sorted.
820 static int32_t elf_add_gsym_reloc(struct Section *sect,
821 int32_t segment, int64_t offset,
822 int type, bool exact)
824 struct Reloc *r;
825 struct Section *s;
826 struct Symbol *sym, *sm;
827 int i;
830 * First look up the segment/offset pair and find a global
831 * symbol corresponding to it. If it's not one of our segments,
832 * then it must be an external symbol, in which case we're fine
833 * doing a normal elf_add_reloc after first sanity-checking
834 * that the offset from the symbol is zero.
836 s = NULL;
837 for (i = 0; i < nsects; i++)
838 if (segment == sects[i]->index) {
839 s = sects[i];
840 break;
842 if (!s) {
843 if (exact && offset != 0)
844 error(ERR_NONFATAL, "unable to find a suitable global symbol"
845 " for this reference");
846 else
847 elf_add_reloc(sect, segment, type);
848 return offset;
851 if (exact) {
853 * Find a symbol pointing _exactly_ at this one.
855 for (sym = s->gsyms; sym; sym = sym->next)
856 if (sym->value == offset)
857 break;
858 } else {
860 * Find the nearest symbol below this one.
862 sym = NULL;
863 for (sm = s->gsyms; sm; sm = sm->next)
864 if (sm->value <= offset && (!sym || sm->value > sym->value))
865 sym = sm;
867 if (!sym && exact) {
868 error(ERR_NONFATAL, "unable to find a suitable global symbol"
869 " for this reference");
870 return 0;
873 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
874 sect->tail = &r->next;
875 r->next = NULL;
877 r->address = sect->len;
878 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
879 r->type = type;
881 sect->nrelocs++;
883 return offset - sym->value;
886 static void elf_out(int32_t segto, const void *data,
887 enum out_type type, uint64_t size,
888 int32_t segment, int32_t wrt)
890 struct Section *s;
891 int64_t addr;
892 uint8_t mydata[16], *p;
893 int i;
894 static struct symlininfo sinfo;
896 #if defined(DEBUG) && DEBUG>2
897 if (data) fprintf(stderr,
898 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
899 currentline, type, segment, segto, size, *(int64_t *)data);
900 else fprintf(stderr,
901 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
902 currentline, type, segment, segto, size);
903 #endif
906 * handle absolute-assembly (structure definitions)
908 if (segto == NO_SEG) {
909 if (type != OUT_RESERVE)
910 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
911 " space");
912 return;
915 s = NULL;
916 for (i = 0; i < nsects; i++)
917 if (segto == sects[i]->index) {
918 s = sects[i];
919 break;
921 if (!s) {
922 int tempint; /* ignored */
923 if (segto != elf_section_names(".text", 2, &tempint))
924 error(ERR_PANIC, "strange segment conditions in ELF driver");
925 else {
926 s = sects[nsects - 1];
927 i = nsects - 1;
930 /* invoke current debug_output routine */
931 if (of_elf64.current_dfmt) {
932 sinfo.offset = s->len;
933 sinfo.section = i;
934 sinfo.segto = segto;
935 sinfo.name = s->name;
936 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
938 /* end of debugging stuff */
940 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
941 error(ERR_WARNING, "attempt to initialize memory in"
942 " BSS section `%s': ignored", s->name);
943 if (type == OUT_REL2ADR)
944 size = 2;
945 else if (type == OUT_REL4ADR)
946 size = 4;
947 s->len += size;
948 return;
951 if (type == OUT_RESERVE) {
952 if (s->type == SHT_PROGBITS) {
953 error(ERR_WARNING, "uninitialized space declared in"
954 " non-BSS section `%s': zeroing", s->name);
955 elf_sect_write(s, NULL, size);
956 } else
957 s->len += size;
958 } else if (type == OUT_RAWDATA) {
959 if (segment != NO_SEG)
960 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
961 elf_sect_write(s, data, size);
962 } else if (type == OUT_ADDRESS) {
963 bool gnu16 = false;
964 addr = *(int64_t *)data;
965 if (segment != NO_SEG) {
966 if (segment % 2) {
967 error(ERR_NONFATAL, "ELF format does not support"
968 " segment base references");
969 } else {
970 if (wrt == NO_SEG) {
971 switch ((int)size) {
972 case 2:
973 elf_add_reloc(s, segment, R_X86_64_16);
974 break;
975 case 4:
976 elf_add_reloc(s, segment, R_X86_64_32);
977 break;
978 case 8:
979 elf_add_reloc(s, segment, R_X86_64_64);
980 break;
981 default:
982 error(ERR_PANIC, "internal error elf64-hpa-871");
983 break;
985 } else if (wrt == elf_gotpc_sect + 1) {
987 * The user will supply GOT relative to $$. ELF
988 * will let us have GOT relative to $. So we
989 * need to fix up the data item by $-$$.
991 addr += s->len;
992 elf_add_reloc(s, segment, R_X86_64_GOTPC32);
993 } else if (wrt == elf_gotoff_sect + 1) {
994 elf_add_reloc(s, segment, R_X86_64_GOTOFF64);
995 } else if (wrt == elf_got_sect + 1) {
996 addr = elf_add_gsym_reloc(s, segment, addr,
997 R_X86_64_GOT32, true);
998 } else if (wrt == elf_sym_sect + 1) {
999 switch ((int)size) {
1000 case 2:
1001 gnu16 = true;
1002 addr = elf_add_gsym_reloc(s, segment, addr,
1003 R_X86_64_16, false);
1004 break;
1005 case 4:
1006 addr = elf_add_gsym_reloc(s, segment, addr,
1007 R_X86_64_32, false);
1008 break;
1009 case 8:
1010 addr = elf_add_gsym_reloc(s, segment, addr,
1011 R_X86_64_64, false);
1012 break;
1013 default:
1014 error(ERR_PANIC, "internal error elf64-hpa-903");
1015 break;
1017 } else if (wrt == elf_plt_sect + 1) {
1018 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1019 "relative PLT references");
1020 } else {
1021 error(ERR_NONFATAL, "ELF format does not support this"
1022 " use of WRT");
1023 wrt = NO_SEG; /* we can at least _try_ to continue */
1027 p = mydata;
1028 if (gnu16) {
1029 WRITESHORT(p, addr);
1030 } else {
1031 if (size != 8 && size != 4 && segment != NO_SEG) {
1032 error(ERR_NONFATAL,
1033 "Unsupported non-64-bit ELF relocation");
1035 if (size == 4) WRITELONG(p, addr);
1036 else WRITEDLONG(p, (int64_t)addr);
1038 elf_sect_write(s, mydata, size);
1039 } else if (type == OUT_REL2ADR) {
1040 if (segment == segto)
1041 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
1042 if (segment != NO_SEG && segment % 2) {
1043 error(ERR_NONFATAL, "ELF format does not support"
1044 " segment base references");
1045 } else {
1046 if (wrt == NO_SEG) {
1047 elf_add_reloc(s, segment, R_X86_64_PC16);
1048 } else {
1049 error(ERR_NONFATAL,
1050 "Unsupported non-32-bit ELF relocation [2]");
1053 p = mydata;
1054 WRITESHORT(p, *(int64_t *)data - size);
1055 elf_sect_write(s, mydata, 2L);
1056 } else if (type == OUT_REL4ADR) {
1057 if (segment == segto)
1058 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1059 if (segment != NO_SEG && segment % 2) {
1060 error(ERR_NONFATAL, "ELF64 format does not support"
1061 " segment base references");
1062 } else {
1063 if (wrt == NO_SEG) {
1064 elf_add_reloc(s, segment, R_X86_64_PC32);
1065 } else if (wrt == elf_plt_sect + 1) {
1066 elf_add_reloc(s, segment, R_X86_64_PLT32);
1067 } else if (wrt == elf_gotpc_sect + 1) {
1068 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
1069 } else if (wrt == elf_gotoff_sect + 1) {
1070 elf_add_reloc(s, segment, R_X86_64_GOTOFF64);
1071 } else if (wrt == elf_got_sect + 1) {
1072 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
1073 } else {
1074 error(ERR_NONFATAL, "ELF64 format does not support this"
1075 " use of WRT");
1076 wrt = NO_SEG; /* we can at least _try_ to continue */
1079 p = mydata;
1080 WRITELONG(p, *(int64_t *)data - size);
1081 elf_sect_write(s, mydata, 4L);
1085 static void elf_write(void)
1087 int align;
1088 int scount;
1089 char *p;
1090 int commlen;
1091 char comment[64];
1092 int i;
1094 struct SAA *symtab;
1095 int32_t symtablen, symtablocal;
1098 * Work out how many sections we will have. We have SHN_UNDEF,
1099 * then the flexible user sections, then the four fixed
1100 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1101 * then optionally relocation sections for the user sections.
1103 if (of_elf64.current_dfmt == &df_stabs)
1104 nsections = 8;
1105 else if (of_elf64.current_dfmt == &df_dwarf)
1106 nsections = 15;
1107 else
1108 nsections = 5; /* SHN_UNDEF and the fixed ones */
1110 add_sectname("", ".comment");
1111 add_sectname("", ".shstrtab");
1112 add_sectname("", ".symtab");
1113 add_sectname("", ".strtab");
1114 for (i = 0; i < nsects; i++) {
1115 nsections++; /* for the section itself */
1116 if (sects[i]->head) {
1117 nsections++; /* for its relocations */
1118 add_sectname(".rela", sects[i]->name);
1122 if (of_elf64.current_dfmt == &df_stabs) {
1123 /* in case the debug information is wanted, just add these three sections... */
1124 add_sectname("", ".stab");
1125 add_sectname("", ".stabstr");
1126 add_sectname(".rel", ".stab");
1129 else if (of_elf64.current_dfmt == &df_dwarf) {
1130 /* the dwarf debug standard specifies the following ten sections,
1131 not all of which are currently implemented,
1132 although all of them are defined. */
1133 #define debug_aranges (int64_t) (nsections-10)
1134 #define debug_info (int64_t) (nsections-7)
1135 #define debug_abbrev (int64_t) (nsections-5)
1136 #define debug_line (int64_t) (nsections-4)
1137 add_sectname("", ".debug_aranges");
1138 add_sectname(".rela", ".debug_aranges");
1139 add_sectname("", ".debug_pubnames");
1140 add_sectname("", ".debug_info");
1141 add_sectname(".rela", ".debug_info");
1142 add_sectname("", ".debug_abbrev");
1143 add_sectname("", ".debug_line");
1144 add_sectname(".rela", ".debug_line");
1145 add_sectname("", ".debug_frame");
1146 add_sectname("", ".debug_loc");
1150 * Do the comment.
1152 *comment = '\0';
1153 commlen =
1154 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1157 * Output the ELF header.
1159 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1160 fputc(elf_osabi, elffp);
1161 fputc(elf_abiver, elffp);
1162 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1163 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1164 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1165 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1166 fwriteint64_t(0L, elffp); /* no entry point */
1167 fwriteint64_t(0L, elffp); /* no program header table */
1168 fwriteint64_t(0x40L, elffp); /* section headers straight after
1169 * ELF header plus alignment */
1170 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1171 fwriteint16_t(0x40, elffp); /* size of ELF header */
1172 fwriteint16_t(0, elffp); /* no program header table, again */
1173 fwriteint16_t(0, elffp); /* still no program header table */
1174 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1175 fwriteint16_t(nsections, elffp); /* number of sections */
1176 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1177 * section header table */
1180 * Build the symbol table and relocation tables.
1182 symtab = elf_build_symtab(&symtablen, &symtablocal);
1183 for (i = 0; i < nsects; i++)
1184 if (sects[i]->head)
1185 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1186 sects[i]->head);
1189 * Now output the section header table.
1192 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1193 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1194 elf_foffs += align;
1195 elf_nsect = 0;
1196 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1197 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1198 scount = 1; /* needed for the stabs debugging to track the symtable section */
1199 p = shstrtab + 1;
1200 for (i = 0; i < nsects; i++) {
1201 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1202 (sects[i]->type == SHT_PROGBITS ?
1203 sects[i]->data : NULL), true,
1204 sects[i]->len, 0, 0, sects[i]->align, 0);
1205 p += strlen(p) + 1;
1206 scount++; /* ditto */
1208 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1209 scount++; /* ditto */
1210 p += strlen(p) + 1;
1211 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1212 scount++; /* ditto */
1213 p += strlen(p) + 1;
1214 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1215 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1216 p += strlen(p) + 1;
1217 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1218 for (i = 0; i < nsects; i++)
1219 if (sects[i]->head) {
1220 p += strlen(p) + 1;
1221 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1222 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1224 if (of_elf64.current_dfmt == &df_stabs) {
1225 /* for debugging information, create the last three sections
1226 which are the .stab , .stabstr and .rel.stab sections respectively */
1228 /* this function call creates the stab sections in memory */
1229 stabs64_generate();
1231 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1232 p += strlen(p) + 1;
1233 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1234 nsections - 2, 0, 4, 12);
1236 p += strlen(p) + 1;
1237 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1238 stabstrlen, 0, 0, 4, 0);
1240 p += strlen(p) + 1;
1241 /* link -> symtable info -> section to refer to */
1242 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1243 stabrellen, symtabsection, nsections - 3, 4,
1244 16);
1247 else if (of_elf64.current_dfmt == &df_dwarf) {
1248 /* for dwarf debugging information, create the ten dwarf sections */
1250 /* this function call creates the dwarf sections in memory */
1251 if (dwarf_fsect) dwarf64_generate();
1253 p += strlen(p) + 1;
1254 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1255 arangeslen, 0, 0, 1, 0);
1256 p += strlen(p) + 1;
1257 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1258 arangesrellen, symtabsection, debug_aranges, 1, 24);
1259 p += strlen(p) + 1;
1260 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1261 pubnameslen, 0, 0, 1, 0);
1262 p += strlen(p) + 1;
1263 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1264 infolen, 0, 0, 1, 0);
1265 p += strlen(p) + 1;
1266 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1267 inforellen, symtabsection, debug_info, 1, 24);
1268 p += strlen(p) + 1;
1269 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1270 abbrevlen, 0, 0, 1, 0);
1271 p += strlen(p) + 1;
1272 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1273 linelen, 0, 0, 1, 0);
1274 p += strlen(p) + 1;
1275 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1276 linerellen, symtabsection, debug_line, 1, 24);
1277 p += strlen(p) + 1;
1278 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1279 framelen, 0, 0, 8, 0);
1280 p += strlen(p) + 1;
1281 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1282 loclen, 0, 0, 1, 0);
1285 fwrite(align_str, align, 1, elffp);
1288 * Now output the sections.
1290 elf_write_sections();
1292 nasm_free(elf_sects);
1293 saa_free(symtab);
1296 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1298 struct SAA *s = saa_init(1L);
1299 struct Symbol *sym;
1300 uint8_t entry[24], *p;
1301 int i;
1303 *len = *local = 0;
1306 * First, an all-zeros entry, required by the ELF spec.
1308 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1309 *len += 24;
1310 (*local)++;
1313 * Next, an entry for the file name.
1315 p = entry;
1316 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1317 WRITESHORT(p, STT_FILE); /* type FILE */
1318 WRITESHORT(p, SHN_ABS);
1319 WRITEDLONG(p, (uint64_t) 0); /* no value */
1320 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1321 saa_wbytes(s, entry, 24L);
1322 *len += 24;
1323 (*local)++;
1326 * Now some standard symbols defining the segments, for relocation
1327 * purposes.
1329 for (i = 1; i <= nsects; i++) {
1330 p = entry;
1331 WRITELONG(p, 0); /* no symbol name */
1332 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1333 WRITESHORT(p, i); /* section id */
1334 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1335 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1336 saa_wbytes(s, entry, 24L);
1337 *len += 24;
1338 (*local)++;
1343 * Now the other local symbols.
1345 saa_rewind(syms);
1346 while ((sym = saa_rstruct(syms))) {
1347 if (sym->type & SYM_GLOBAL)
1348 continue;
1349 p = entry;
1350 WRITELONG(p, sym->strpos); /* index into symbol string table */
1351 WRITECHAR(p, sym->type); /* type and binding */
1352 WRITECHAR(p, sym->other); /* visibility */
1353 WRITESHORT(p, sym->section); /* index into section header table */
1354 WRITEDLONG(p, (int64_t)sym->value); /* value of symbol */
1355 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1356 saa_wbytes(s, entry, 24L);
1357 *len += 24;
1358 (*local)++;
1361 * dwarf needs symbols for debug sections
1362 * which are relocation targets.
1364 if (of_elf64.current_dfmt == &df_dwarf) {
1365 dwarf_infosym = *local;
1366 p = entry;
1367 WRITELONG(p, 0); /* no symbol name */
1368 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1369 WRITESHORT(p, debug_info); /* section id */
1370 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1371 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1372 saa_wbytes(s, entry, 24L);
1373 *len += 24;
1374 (*local)++;
1375 dwarf_abbrevsym = *local;
1376 p = entry;
1377 WRITELONG(p, 0); /* no symbol name */
1378 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1379 WRITESHORT(p, debug_abbrev); /* section id */
1380 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1381 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1382 saa_wbytes(s, entry, 24L);
1383 *len += 24;
1384 (*local)++;
1385 dwarf_linesym = *local;
1386 p = entry;
1387 WRITELONG(p, 0); /* no symbol name */
1388 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1389 WRITESHORT(p, debug_line); /* section id */
1390 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1391 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1392 saa_wbytes(s, entry, 24L);
1393 *len += 24;
1394 (*local)++;
1398 * Now the global symbols.
1400 saa_rewind(syms);
1401 while ((sym = saa_rstruct(syms))) {
1402 if (!(sym->type & SYM_GLOBAL))
1403 continue;
1404 p = entry;
1405 WRITELONG(p, sym->strpos);
1406 WRITECHAR(p, sym->type); /* type and binding */
1407 WRITECHAR(p, sym->other); /* visibility */
1408 WRITESHORT(p, sym->section);
1409 WRITEDLONG(p, (int64_t)sym->value);
1410 WRITEDLONG(p, (int64_t)sym->size);
1411 saa_wbytes(s, entry, 24L);
1412 *len += 24;
1415 return s;
1418 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1420 struct SAA *s;
1421 uint8_t *p, entry[24];
1423 if (!r)
1424 return NULL;
1426 s = saa_init(1L);
1427 *len = 0;
1429 while (r) {
1430 int64_t sym = r->symbol;
1432 if (sym >= GLOBAL_TEMP_BASE)
1434 if (of_elf64.current_dfmt == &df_dwarf)
1435 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1436 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1438 p = entry;
1439 WRITEDLONG(p, r->address);
1440 WRITEDLONG(p, (sym << 32) + r->type);
1441 WRITEDLONG(p, (uint64_t) 0);
1442 saa_wbytes(s, entry, 24L);
1443 *len += 24;
1445 r = r->next;
1448 return s;
1451 static void elf_section_header(int name, int type, uint64_t flags,
1452 void *data, bool is_saa, uint64_t datalen,
1453 int link, int info, int align, int eltsize)
1455 elf_sects[elf_nsect].data = data;
1456 elf_sects[elf_nsect].len = datalen;
1457 elf_sects[elf_nsect].is_saa = is_saa;
1458 elf_nsect++;
1460 fwriteint32_t((int32_t)name, elffp);
1461 fwriteint32_t((int32_t)type, elffp);
1462 fwriteint64_t((int64_t)flags, elffp);
1463 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1464 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1465 fwriteint64_t(datalen, elffp);
1466 if (data)
1467 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1468 fwriteint32_t((int32_t)link, elffp);
1469 fwriteint32_t((int32_t)info, elffp);
1470 fwriteint64_t((int64_t)align, elffp);
1471 fwriteint64_t((int64_t)eltsize, elffp);
1474 static void elf_write_sections(void)
1476 int i;
1477 for (i = 0; i < elf_nsect; i++)
1478 if (elf_sects[i].data) {
1479 int32_t len = elf_sects[i].len;
1480 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1481 int32_t align = reallen - len;
1482 if (elf_sects[i].is_saa)
1483 saa_fpwrite(elf_sects[i].data, elffp);
1484 else
1485 fwrite(elf_sects[i].data, len, 1, elffp);
1486 fwrite(align_str, align, 1, elffp);
1490 static void elf_sect_write(struct Section *sect,
1491 const uint8_t *data, uint64_t len)
1493 saa_wbytes(sect->data, data, len);
1494 sect->len += len;
1497 static int32_t elf_segbase(int32_t segment)
1499 return segment;
1502 static int elf_directive(char *directive, char *value, int pass)
1504 bool err;
1505 int64_t n;
1506 char *p;
1508 if (!strcmp(directive, "osabi")) {
1509 if (pass == 2)
1510 return 1; /* ignore in pass 2 */
1512 n = readnum(value, &err);
1513 if (err) {
1514 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1515 return 1;
1517 if (n < 0 || n > 255) {
1518 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1519 return 1;
1521 elf_osabi = n;
1522 elf_abiver = 0;
1524 if ((p = strchr(value,',')) == NULL)
1525 return 1;
1527 n = readnum(p+1, &err);
1528 if (err || n < 0 || n > 255) {
1529 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1530 return 1;
1533 elf_abiver = n;
1534 return 1;
1537 return 0;
1540 static void elf_filename(char *inname, char *outname, efunc error)
1542 strcpy(elf_module, inname);
1543 standard_extension(inname, outname, ".o", error);
1546 extern macros_t elf_stdmac[];
1548 static int elf_set_info(enum geninfo type, char **val)
1550 (void)type;
1551 (void)val;
1552 return 0;
1554 static struct dfmt df_dwarf = {
1555 "ELF64 (X86_64) dwarf debug format for Linux",
1556 "dwarf",
1557 debug64_init,
1558 dwarf64_linenum,
1559 debug64_deflabel,
1560 debug64_directive,
1561 debug64_typevalue,
1562 dwarf64_output,
1563 dwarf64_cleanup
1565 static struct dfmt df_stabs = {
1566 "ELF64 (X86_64) stabs debug format for Linux",
1567 "stabs",
1568 debug64_init,
1569 stabs64_linenum,
1570 debug64_deflabel,
1571 debug64_directive,
1572 debug64_typevalue,
1573 stabs64_output,
1574 stabs64_cleanup
1577 struct dfmt *elf64_debugs_arr[3] = { &df_stabs, &df_dwarf, NULL };
1579 struct ofmt of_elf64 = {
1580 "ELF64 (x86_64) object files (e.g. Linux)",
1581 "elf64",
1582 NULL,
1583 elf64_debugs_arr,
1584 &null_debug_form,
1585 elf_stdmac,
1586 elf_init,
1587 elf_set_info,
1588 elf_out,
1589 elf_deflabel,
1590 elf_section_names,
1591 elf_segbase,
1592 elf_directive,
1593 elf_filename,
1594 elf_cleanup
1597 /* common debugging routines */
1598 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1600 (void)of;
1601 (void)id;
1602 (void)fp;
1603 (void)error;
1605 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1606 char *special)
1608 (void)name;
1609 (void)segment;
1610 (void)offset;
1611 (void)is_global;
1612 (void)special;
1615 void debug64_directive(const char *directive, const char *params)
1617 (void)directive;
1618 (void)params;
1621 void debug64_typevalue(int32_t type)
1623 int32_t stype, ssize;
1624 switch (TYM_TYPE(type)) {
1625 case TY_LABEL:
1626 ssize = 0;
1627 stype = STT_NOTYPE;
1628 break;
1629 case TY_BYTE:
1630 ssize = 1;
1631 stype = STT_OBJECT;
1632 break;
1633 case TY_WORD:
1634 ssize = 2;
1635 stype = STT_OBJECT;
1636 break;
1637 case TY_DWORD:
1638 ssize = 4;
1639 stype = STT_OBJECT;
1640 break;
1641 case TY_FLOAT:
1642 ssize = 4;
1643 stype = STT_OBJECT;
1644 break;
1645 case TY_QWORD:
1646 ssize = 8;
1647 stype = STT_OBJECT;
1648 break;
1649 case TY_TBYTE:
1650 ssize = 10;
1651 stype = STT_OBJECT;
1652 break;
1653 case TY_OWORD:
1654 ssize = 16;
1655 stype = STT_OBJECT;
1656 break;
1657 case TY_COMMON:
1658 ssize = 0;
1659 stype = STT_COMMON;
1660 break;
1661 case TY_SEG:
1662 ssize = 0;
1663 stype = STT_SECTION;
1664 break;
1665 case TY_EXTERN:
1666 ssize = 0;
1667 stype = STT_NOTYPE;
1668 break;
1669 case TY_EQU:
1670 ssize = 0;
1671 stype = STT_NOTYPE;
1672 break;
1673 default:
1674 ssize = 0;
1675 stype = STT_NOTYPE;
1676 break;
1678 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1679 lastsym->size = ssize;
1680 lastsym->type = stype;
1684 /* stabs debugging routines */
1687 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1689 (void)segto;
1690 if (!stabs_filename) {
1691 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1692 strcpy(stabs_filename, filename);
1693 } else {
1694 if (strcmp(stabs_filename, filename)) {
1695 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1696 in fact, this leak comes in quite handy to maintain a list of files
1697 encountered so far in the symbol lines... */
1699 /* why not nasm_free(stabs_filename); we're done with the old one */
1701 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1702 strcpy(stabs_filename, filename);
1705 debug_immcall = 1;
1706 currentline = linenumber;
1710 void stabs64_output(int type, void *param)
1712 struct symlininfo *s;
1713 struct linelist *el;
1714 if (type == TY_DEBUGSYMLIN) {
1715 if (debug_immcall) {
1716 s = (struct symlininfo *)param;
1717 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1718 return; /* line info is only collected for executable sections */
1719 numlinestabs++;
1720 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1721 el->info.offset = s->offset;
1722 el->info.section = s->section;
1723 el->info.name = s->name;
1724 el->line = currentline;
1725 el->filename = stabs_filename;
1726 el->next = 0;
1727 if (stabslines) {
1728 stabslines->last->next = el;
1729 stabslines->last = el;
1730 } else {
1731 stabslines = el;
1732 stabslines->last = el;
1736 debug_immcall = 0;
1739 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1740 do {\
1741 WRITELONG(p,n_strx); \
1742 WRITECHAR(p,n_type); \
1743 WRITECHAR(p,n_other); \
1744 WRITESHORT(p,n_desc); \
1745 WRITELONG(p,n_value); \
1746 } while (0)
1748 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1750 void stabs64_generate(void)
1752 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1753 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1754 char **allfiles;
1755 int *fileidx;
1757 struct linelist *ptr;
1759 ptr = stabslines;
1761 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1762 for (i = 0; i < numlinestabs; i++)
1763 allfiles[i] = 0;
1764 numfiles = 0;
1765 while (ptr) {
1766 if (numfiles == 0) {
1767 allfiles[0] = ptr->filename;
1768 numfiles++;
1769 } else {
1770 for (i = 0; i < numfiles; i++) {
1771 if (!strcmp(allfiles[i], ptr->filename))
1772 break;
1774 if (i >= numfiles) {
1775 allfiles[i] = ptr->filename;
1776 numfiles++;
1779 ptr = ptr->next;
1781 strsize = 1;
1782 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1783 for (i = 0; i < numfiles; i++) {
1784 fileidx[i] = strsize;
1785 strsize += strlen(allfiles[i]) + 1;
1787 mainfileindex = 0;
1788 for (i = 0; i < numfiles; i++) {
1789 if (!strcmp(allfiles[i], elf_module)) {
1790 mainfileindex = i;
1791 break;
1795 /* worst case size of the stab buffer would be:
1796 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1798 sbuf =
1799 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1800 sizeof(struct stabentry));
1802 ssbuf = (uint8_t *)nasm_malloc(strsize);
1804 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1805 rptr = rbuf;
1807 for (i = 0; i < numfiles; i++) {
1808 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1810 ssbuf[0] = 0;
1812 stabstrlen = strsize; /* set global variable for length of stab strings */
1814 sptr = sbuf;
1815 ptr = stabslines;
1816 numstabs = 0;
1818 if (ptr) {
1819 /* this is the first stab, its strx points to the filename of the
1820 the source-file, the n_desc field should be set to the number
1821 of remaining stabs
1823 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1825 /* this is the stab for the main source file */
1826 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1828 /* relocation table entry */
1830 /* Since the symbol table has two entries before */
1831 /* the section symbols, the index in the info.section */
1832 /* member must be adjusted by adding 2 */
1834 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1835 WRITELONG(rptr, R_X86_64_32);
1836 WRITELONG(rptr, ptr->info.section + 2);
1838 numstabs++;
1839 currfile = mainfileindex;
1842 while (ptr) {
1843 if (strcmp(allfiles[currfile], ptr->filename)) {
1844 /* oops file has changed... */
1845 for (i = 0; i < numfiles; i++)
1846 if (!strcmp(allfiles[i], ptr->filename))
1847 break;
1848 currfile = i;
1849 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1850 ptr->info.offset);
1851 numstabs++;
1853 /* relocation table entry */
1855 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1856 WRITELONG(rptr, R_X86_64_32);
1857 WRITELONG(rptr, ptr->info.section + 2);
1860 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1861 numstabs++;
1863 /* relocation table entry */
1865 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1866 WRITELONG(rptr, R_X86_64_32);
1867 WRITELONG(rptr, ptr->info.section + 2);
1869 ptr = ptr->next;
1873 ((struct stabentry *)sbuf)->n_desc = numstabs;
1875 nasm_free(allfiles);
1876 nasm_free(fileidx);
1878 stablen = (sptr - sbuf);
1879 stabrellen = (rptr - rbuf);
1880 stabrelbuf = rbuf;
1881 stabbuf = sbuf;
1882 stabstrbuf = ssbuf;
1885 void stabs64_cleanup(void)
1887 struct linelist *ptr, *del;
1888 if (!stabslines)
1889 return;
1890 ptr = stabslines;
1891 while (ptr) {
1892 del = ptr;
1893 ptr = ptr->next;
1894 nasm_free(del);
1896 if (stabbuf)
1897 nasm_free(stabbuf);
1898 if (stabrelbuf)
1899 nasm_free(stabrelbuf);
1900 if (stabstrbuf)
1901 nasm_free(stabstrbuf);
1903 /* dwarf routines */
1906 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1908 (void)segto;
1909 dwarf64_findfile(filename);
1910 debug_immcall = 1;
1911 currentline = linenumber;
1914 /* called from elf_out with type == TY_DEBUGSYMLIN */
1915 void dwarf64_output(int type, void *param)
1917 int ln, aa, inx, maxln, soc;
1918 struct symlininfo *s;
1919 struct SAA *plinep;
1921 (void)type;
1923 s = (struct symlininfo *)param;
1924 /* line number info is only gathered for executable sections */
1925 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1926 return;
1927 /* Check if section index has changed */
1928 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1930 dwarf64_findsect(s->section);
1932 /* do nothing unless line or file has changed */
1933 if (debug_immcall)
1935 ln = currentline - dwarf_csect->line;
1936 aa = s->offset - dwarf_csect->offset;
1937 inx = dwarf_clist->line;
1938 plinep = dwarf_csect->psaa;
1939 /* check for file change */
1940 if (!(inx == dwarf_csect->file))
1942 saa_write8(plinep,DW_LNS_set_file);
1943 saa_write8(plinep,inx);
1944 dwarf_csect->file = inx;
1946 /* check for line change */
1947 if (ln)
1949 /* test if in range of special op code */
1950 maxln = line_base + line_range;
1951 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1952 if (ln >= line_base && ln < maxln && soc < 256)
1954 saa_write8(plinep,soc);
1956 else
1958 if (ln)
1960 saa_write8(plinep,DW_LNS_advance_line);
1961 saa_wleb128s(plinep,ln);
1963 if (aa)
1965 saa_write8(plinep,DW_LNS_advance_pc);
1966 saa_wleb128u(plinep,aa);
1969 dwarf_csect->line = currentline;
1970 dwarf_csect->offset = s->offset;
1972 /* show change handled */
1973 debug_immcall = 0;
1978 void dwarf64_generate(void)
1980 static const char nasm_signature[] = "NASM " NASM_VER;
1981 uint8_t *pbuf;
1982 int indx;
1983 struct linelist *ftentry;
1984 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1985 struct SAA *parangesrel, *plinesrel, *pinforel;
1986 struct sectlist *psect;
1987 size_t saalen, linepoff, totlen, highaddr;
1989 /* write epilogues for each line program range */
1990 /* and build aranges section */
1991 paranges = saa_init(1L);
1992 parangesrel = saa_init(1L);
1993 saa_write16(paranges,3); /* dwarf version */
1994 saa_write64(parangesrel, paranges->datalen+4);
1995 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1996 saa_write64(parangesrel, 0);
1997 saa_write32(paranges,0); /* offset into info */
1998 saa_write8(paranges,8); /* pointer size */
1999 saa_write8(paranges,0); /* not segmented */
2000 saa_write32(paranges,0); /* padding */
2001 /* iterate though sectlist entries */
2002 psect = dwarf_fsect;
2003 totlen = 0;
2004 highaddr = 0;
2005 for (indx = 0; indx < dwarf_nsections; indx++)
2007 plinep = psect->psaa;
2008 /* Line Number Program Epilogue */
2009 saa_write8(plinep,2); /* std op 2 */
2010 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2011 saa_write8(plinep,DW_LNS_extended_op);
2012 saa_write8(plinep,1); /* operand length */
2013 saa_write8(plinep,DW_LNE_end_sequence);
2014 totlen += plinep->datalen;
2015 /* range table relocation entry */
2016 saa_write64(parangesrel, paranges->datalen + 4);
2017 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2018 saa_write64(parangesrel, (uint64_t) 0);
2019 /* range table entry */
2020 saa_write64(paranges,0x0000); /* range start */
2021 saa_write64(paranges,sects[psect->section]->len); /* range length */
2022 highaddr += sects[psect->section]->len;
2023 /* done with this entry */
2024 psect = psect->next;
2026 saa_write64(paranges,0); /* null address */
2027 saa_write64(paranges,0); /* null length */
2028 saalen = paranges->datalen;
2029 arangeslen = saalen + 4;
2030 arangesbuf = pbuf = nasm_malloc(arangeslen);
2031 WRITELONG(pbuf,saalen); /* initial length */
2032 saa_rnbytes(paranges, pbuf, saalen);
2033 saa_free(paranges);
2035 /* build rela.aranges section */
2036 arangesrellen = saalen = parangesrel->datalen;
2037 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2038 saa_rnbytes(parangesrel, pbuf, saalen);
2039 saa_free(parangesrel);
2041 /* build pubnames section */
2042 ppubnames = saa_init(1L);
2043 saa_write16(ppubnames,3); /* dwarf version */
2044 saa_write32(ppubnames,0); /* offset into info */
2045 saa_write32(ppubnames,0); /* space used in info */
2046 saa_write32(ppubnames,0); /* end of list */
2047 saalen = ppubnames->datalen;
2048 pubnameslen = saalen + 4;
2049 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2050 WRITELONG(pbuf,saalen); /* initial length */
2051 saa_rnbytes(ppubnames, pbuf, saalen);
2052 saa_free(ppubnames);
2054 /* build info section */
2055 pinfo = saa_init(1L);
2056 pinforel = saa_init(1L);
2057 saa_write16(pinfo,3); /* dwarf version */
2058 saa_write64(pinforel, pinfo->datalen + 4);
2059 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2060 saa_write64(pinforel, 0);
2061 saa_write32(pinfo,0); /* offset into abbrev */
2062 saa_write8(pinfo,8); /* pointer size */
2063 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2064 saa_write64(pinforel, pinfo->datalen + 4);
2065 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2066 saa_write64(pinforel, 0);
2067 saa_write64(pinfo,0); /* DW_AT_low_pc */
2068 saa_write64(pinforel, pinfo->datalen + 4);
2069 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2070 saa_write64(pinforel, 0);
2071 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2072 saa_write64(pinforel, pinfo->datalen + 4);
2073 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2074 saa_write64(pinforel, 0);
2075 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2076 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2077 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2078 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2079 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2080 saa_write64(pinforel, pinfo->datalen + 4);
2081 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2082 saa_write64(pinforel, 0);
2083 saa_write64(pinfo,0); /* DW_AT_low_pc */
2084 saa_write64(pinfo,0); /* DW_AT_frame_base */
2085 saa_write8(pinfo,0); /* end of entries */
2086 saalen = pinfo->datalen;
2087 infolen = saalen + 4;
2088 infobuf = pbuf = nasm_malloc(infolen);
2089 WRITELONG(pbuf,saalen); /* initial length */
2090 saa_rnbytes(pinfo, pbuf, saalen);
2091 saa_free(pinfo);
2093 /* build rela.info section */
2094 inforellen = saalen = pinforel->datalen;
2095 inforelbuf = pbuf = nasm_malloc(inforellen);
2096 saa_rnbytes(pinforel, pbuf, saalen);
2097 saa_free(pinforel);
2099 /* build abbrev section */
2100 pabbrev = saa_init(1L);
2101 saa_write8(pabbrev,1); /* entry number LEB128u */
2102 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2103 saa_write8(pabbrev,1); /* has children */
2104 /* the following attributes and forms are all LEB128u values */
2105 saa_write8(pabbrev,DW_AT_low_pc);
2106 saa_write8(pabbrev,DW_FORM_addr);
2107 saa_write8(pabbrev,DW_AT_high_pc);
2108 saa_write8(pabbrev,DW_FORM_addr);
2109 saa_write8(pabbrev,DW_AT_stmt_list);
2110 saa_write8(pabbrev,DW_FORM_data4);
2111 saa_write8(pabbrev,DW_AT_name);
2112 saa_write8(pabbrev,DW_FORM_string);
2113 saa_write8(pabbrev,DW_AT_producer);
2114 saa_write8(pabbrev,DW_FORM_string);
2115 saa_write8(pabbrev,DW_AT_language);
2116 saa_write8(pabbrev,DW_FORM_data2);
2117 saa_write16(pabbrev,0); /* end of entry */
2118 /* LEB128u usage same as above */
2119 saa_write8(pabbrev,2); /* entry number */
2120 saa_write8(pabbrev,DW_TAG_subprogram);
2121 saa_write8(pabbrev,0); /* no children */
2122 saa_write8(pabbrev,DW_AT_low_pc);
2123 saa_write8(pabbrev,DW_FORM_addr);
2124 saa_write8(pabbrev,DW_AT_frame_base);
2125 saa_write8(pabbrev,DW_FORM_data4);
2126 saa_write16(pabbrev,0); /* end of entry */
2127 abbrevlen = saalen = pabbrev->datalen;
2128 abbrevbuf = pbuf = nasm_malloc(saalen);
2129 saa_rnbytes(pabbrev, pbuf, saalen);
2130 saa_free(pabbrev);
2132 /* build line section */
2133 /* prolog */
2134 plines = saa_init(1L);
2135 saa_write8(plines,1); /* Minimum Instruction Length */
2136 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2137 saa_write8(plines,line_base); /* Line Base */
2138 saa_write8(plines,line_range); /* Line Range */
2139 saa_write8(plines,opcode_base); /* Opcode Base */
2140 /* standard opcode lengths (# of LEB128u operands) */
2141 saa_write8(plines,0); /* Std opcode 1 length */
2142 saa_write8(plines,1); /* Std opcode 2 length */
2143 saa_write8(plines,1); /* Std opcode 3 length */
2144 saa_write8(plines,1); /* Std opcode 4 length */
2145 saa_write8(plines,1); /* Std opcode 5 length */
2146 saa_write8(plines,0); /* Std opcode 6 length */
2147 saa_write8(plines,0); /* Std opcode 7 length */
2148 saa_write8(plines,0); /* Std opcode 8 length */
2149 saa_write8(plines,1); /* Std opcode 9 length */
2150 saa_write8(plines,0); /* Std opcode 10 length */
2151 saa_write8(plines,0); /* Std opcode 11 length */
2152 saa_write8(plines,1); /* Std opcode 12 length */
2153 /* Directory Table */
2154 saa_write8(plines,0); /* End of table */
2155 /* File Name Table */
2156 ftentry = dwarf_flist;
2157 for (indx = 0;indx<dwarf_numfiles;indx++)
2159 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2160 saa_write8(plines,0); /* directory LEB128u */
2161 saa_write8(plines,0); /* time LEB128u */
2162 saa_write8(plines,0); /* size LEB128u */
2163 ftentry = ftentry->next;
2165 saa_write8(plines,0); /* End of table */
2166 linepoff = plines->datalen;
2167 linelen = linepoff + totlen + 10;
2168 linebuf = pbuf = nasm_malloc(linelen);
2169 WRITELONG(pbuf,linelen-4); /* initial length */
2170 WRITESHORT(pbuf,3); /* dwarf version */
2171 WRITELONG(pbuf,linepoff); /* offset to line number program */
2172 /* write line header */
2173 saalen = linepoff;
2174 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2175 pbuf += linepoff;
2176 saa_free(plines);
2177 /* concatonate line program ranges */
2178 linepoff += 13;
2179 plinesrel = saa_init(1L);
2180 psect = dwarf_fsect;
2181 for (indx = 0; indx < dwarf_nsections; indx++)
2183 saa_write64(plinesrel, linepoff);
2184 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2185 saa_write64(plinesrel, (uint64_t) 0);
2186 plinep = psect->psaa;
2187 saalen = plinep->datalen;
2188 saa_rnbytes(plinep, pbuf, saalen);
2189 pbuf += saalen;
2190 linepoff += saalen;
2191 saa_free(plinep);
2192 /* done with this entry */
2193 psect = psect->next;
2197 /* build rela.lines section */
2198 linerellen =saalen = plinesrel->datalen;
2199 linerelbuf = pbuf = nasm_malloc(linerellen);
2200 saa_rnbytes(plinesrel, pbuf, saalen);
2201 saa_free(plinesrel);
2203 /* build frame section */
2204 framelen = 4;
2205 framebuf = pbuf = nasm_malloc(framelen);
2206 WRITELONG(pbuf,framelen-4); /* initial length */
2208 /* build loc section */
2209 loclen = 16;
2210 locbuf = pbuf = nasm_malloc(loclen);
2211 WRITEDLONG(pbuf,0); /* null beginning offset */
2212 WRITEDLONG(pbuf,0); /* null ending offset */
2215 void dwarf64_cleanup(void)
2217 if (arangesbuf)
2218 nasm_free(arangesbuf);
2219 if (arangesrelbuf)
2220 nasm_free(arangesrelbuf);
2221 if (pubnamesbuf)
2222 nasm_free(pubnamesbuf);
2223 if (infobuf)
2224 nasm_free(infobuf);
2225 if (inforelbuf)
2226 nasm_free(inforelbuf);
2227 if (abbrevbuf)
2228 nasm_free(abbrevbuf);
2229 if (linebuf)
2230 nasm_free(linebuf);
2231 if (linerelbuf)
2232 nasm_free(linerelbuf);
2233 if (framebuf)
2234 nasm_free(framebuf);
2235 if (locbuf)
2236 nasm_free(locbuf);
2238 void dwarf64_findfile(const char * fname)
2240 int finx;
2241 struct linelist *match;
2243 /* return if fname is current file name */
2244 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2245 /* search for match */
2246 else
2248 match = 0;
2249 if (dwarf_flist)
2251 match = dwarf_flist;
2252 for (finx = 0; finx < dwarf_numfiles; finx++)
2254 if (!(strcmp(fname, match->filename)))
2256 dwarf_clist = match;
2257 return;
2261 /* add file name to end of list */
2262 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2263 dwarf_numfiles++;
2264 dwarf_clist->line = dwarf_numfiles;
2265 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2266 strcpy(dwarf_clist->filename,fname);
2267 dwarf_clist->next = 0;
2268 /* if first entry */
2269 if (!dwarf_flist)
2271 dwarf_flist = dwarf_elist = dwarf_clist;
2272 dwarf_clist->last = 0;
2274 /* chain to previous entry */
2275 else
2277 dwarf_elist->next = dwarf_clist;
2278 dwarf_elist = dwarf_clist;
2282 /* */
2283 void dwarf64_findsect(const int index)
2285 int sinx;
2286 struct sectlist *match;
2287 struct SAA *plinep;
2288 /* return if index is current section index */
2289 if (dwarf_csect && (dwarf_csect->section == index))
2291 return;
2293 /* search for match */
2294 else
2296 match = 0;
2297 if (dwarf_fsect)
2299 match = dwarf_fsect;
2300 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2302 if ((match->section == index))
2304 dwarf_csect = match;
2305 return;
2307 match = match->next;
2310 /* add entry to end of list */
2311 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2312 dwarf_nsections++;
2313 dwarf_csect->psaa = plinep = saa_init(1L);
2314 dwarf_csect->line = 1;
2315 dwarf_csect->offset = 0;
2316 dwarf_csect->file = 1;
2317 dwarf_csect->section = index;
2318 dwarf_csect->next = 0;
2319 /* set relocatable address at start of line program */
2320 saa_write8(plinep,DW_LNS_extended_op);
2321 saa_write8(plinep,9); /* operand length */
2322 saa_write8(plinep,DW_LNE_set_address);
2323 saa_write64(plinep,0); /* Start Address */
2324 /* if first entry */
2325 if (!dwarf_fsect)
2327 dwarf_fsect = dwarf_esect = dwarf_csect;
2328 dwarf_csect->last = 0;
2330 /* chain to previous entry */
2331 else
2333 dwarf_esect->next = dwarf_csect;
2334 dwarf_esect = dwarf_csect;
2339 #endif /* OF_ELF */