ELF64: fix various GOT relocations
[nasm.git] / output / outelf64.c
blobb70550f5055f9e111d3a6d44fbcfa87b640f69e7
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 1:
973 elf_add_reloc(s, segment, R_X86_64_8);
974 break;
975 case 2:
976 elf_add_reloc(s, segment, R_X86_64_16);
977 break;
978 case 4:
979 elf_add_reloc(s, segment, R_X86_64_32);
980 break;
981 case 8:
982 elf_add_reloc(s, segment, R_X86_64_64);
983 break;
984 default:
985 error(ERR_PANIC, "internal error elf64-hpa-871");
986 break;
988 } else if (wrt == elf_gotpc_sect + 1) {
990 * The user will supply GOT relative to $$. ELF
991 * will let us have GOT relative to $. So we
992 * need to fix up the data item by $-$$.
994 addr += s->len;
995 elf_add_reloc(s, segment, R_X86_64_GOTPC32);
996 } else if (wrt == elf_gotoff_sect + 1) {
997 if (size != 8) {
998 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
999 "references to be qword absolute");
1000 wrt = NO_SEG;
1001 } else {
1002 elf_add_reloc(s, segment, R_X86_64_GOTOFF64);
1004 } else if (wrt == elf_got_sect + 1) {
1005 switch ((int)size) {
1006 case 4:
1007 addr = elf_add_gsym_reloc(s, segment, addr,
1008 R_X86_64_GOT32, true);
1009 break;
1010 case 8:
1011 addr = elf_add_gsym_reloc(s, segment, addr,
1012 R_X86_64_GOT64, true);
1013 break;
1014 default:
1015 error(ERR_NONFATAL, "invalid ..got reference");
1016 wrt = NO_SEG;
1017 break;
1019 } else if (wrt == elf_sym_sect + 1) {
1020 switch ((int)size) {
1021 case 1:
1022 addr = elf_add_gsym_reloc(s, segment, addr,
1023 R_X86_64_8, false);
1024 break;
1025 case 2:
1026 addr = elf_add_gsym_reloc(s, segment, addr,
1027 R_X86_64_16, false);
1028 break;
1029 case 4:
1030 addr = elf_add_gsym_reloc(s, segment, addr,
1031 R_X86_64_32, false);
1032 break;
1033 case 8:
1034 addr = elf_add_gsym_reloc(s, segment, addr,
1035 R_X86_64_64, false);
1036 break;
1037 default:
1038 error(ERR_PANIC, "internal error elf64-hpa-903");
1039 break;
1041 } else if (wrt == elf_plt_sect + 1) {
1042 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1043 "relative PLT references");
1044 } else {
1045 error(ERR_NONFATAL, "ELF format does not support this"
1046 " use of WRT");
1047 wrt = NO_SEG; /* we can at least _try_ to continue */
1051 p = mydata;
1052 if (gnu16) {
1053 WRITESHORT(p, addr);
1054 } else {
1055 if (size != 8 && size != 4 && segment != NO_SEG) {
1056 error(ERR_NONFATAL,
1057 "Unsupported non-64-bit ELF relocation");
1059 if (size == 4) WRITELONG(p, addr);
1060 else WRITEDLONG(p, (int64_t)addr);
1062 elf_sect_write(s, mydata, size);
1063 } else if (type == OUT_REL2ADR) {
1064 if (segment == segto)
1065 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
1066 if (segment != NO_SEG && segment % 2) {
1067 error(ERR_NONFATAL, "ELF format does not support"
1068 " segment base references");
1069 } else {
1070 if (wrt == NO_SEG) {
1071 elf_add_reloc(s, segment, R_X86_64_PC16);
1072 } else {
1073 error(ERR_NONFATAL,
1074 "Unsupported non-32-bit ELF relocation [2]");
1077 p = mydata;
1078 WRITESHORT(p, *(int64_t *)data - size);
1079 elf_sect_write(s, mydata, 2L);
1080 } else if (type == OUT_REL4ADR) {
1081 if (segment == segto)
1082 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1083 if (segment != NO_SEG && segment % 2) {
1084 error(ERR_NONFATAL, "ELF64 format does not support"
1085 " segment base references");
1086 } else {
1087 if (wrt == NO_SEG) {
1088 elf_add_reloc(s, segment, R_X86_64_PC32);
1089 } else if (wrt == elf_plt_sect + 1) {
1090 elf_add_reloc(s, segment, R_X86_64_PLT32);
1091 } else if (wrt == elf_gotpc_sect + 1 ||
1092 wrt == elf_got_sect + 1) {
1093 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
1094 } else if (wrt == elf_gotoff_sect + 1 ||
1095 wrt == elf_got_sect + 1) {
1096 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1097 "qword absolute");
1098 wrt = NO_SEG;
1099 } else {
1100 error(ERR_NONFATAL, "ELF64 format does not support this"
1101 " use of WRT");
1102 wrt = NO_SEG; /* we can at least _try_ to continue */
1105 p = mydata;
1106 WRITELONG(p, *(int64_t *)data - size);
1107 elf_sect_write(s, mydata, 4L);
1111 static void elf_write(void)
1113 int align;
1114 int scount;
1115 char *p;
1116 int commlen;
1117 char comment[64];
1118 int i;
1120 struct SAA *symtab;
1121 int32_t symtablen, symtablocal;
1124 * Work out how many sections we will have. We have SHN_UNDEF,
1125 * then the flexible user sections, then the four fixed
1126 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1127 * then optionally relocation sections for the user sections.
1129 if (of_elf64.current_dfmt == &df_stabs)
1130 nsections = 8;
1131 else if (of_elf64.current_dfmt == &df_dwarf)
1132 nsections = 15;
1133 else
1134 nsections = 5; /* SHN_UNDEF and the fixed ones */
1136 add_sectname("", ".comment");
1137 add_sectname("", ".shstrtab");
1138 add_sectname("", ".symtab");
1139 add_sectname("", ".strtab");
1140 for (i = 0; i < nsects; i++) {
1141 nsections++; /* for the section itself */
1142 if (sects[i]->head) {
1143 nsections++; /* for its relocations */
1144 add_sectname(".rela", sects[i]->name);
1148 if (of_elf64.current_dfmt == &df_stabs) {
1149 /* in case the debug information is wanted, just add these three sections... */
1150 add_sectname("", ".stab");
1151 add_sectname("", ".stabstr");
1152 add_sectname(".rel", ".stab");
1155 else if (of_elf64.current_dfmt == &df_dwarf) {
1156 /* the dwarf debug standard specifies the following ten sections,
1157 not all of which are currently implemented,
1158 although all of them are defined. */
1159 #define debug_aranges (int64_t) (nsections-10)
1160 #define debug_info (int64_t) (nsections-7)
1161 #define debug_abbrev (int64_t) (nsections-5)
1162 #define debug_line (int64_t) (nsections-4)
1163 add_sectname("", ".debug_aranges");
1164 add_sectname(".rela", ".debug_aranges");
1165 add_sectname("", ".debug_pubnames");
1166 add_sectname("", ".debug_info");
1167 add_sectname(".rela", ".debug_info");
1168 add_sectname("", ".debug_abbrev");
1169 add_sectname("", ".debug_line");
1170 add_sectname(".rela", ".debug_line");
1171 add_sectname("", ".debug_frame");
1172 add_sectname("", ".debug_loc");
1176 * Do the comment.
1178 *comment = '\0';
1179 commlen =
1180 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1183 * Output the ELF header.
1185 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1186 fputc(elf_osabi, elffp);
1187 fputc(elf_abiver, elffp);
1188 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1189 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1190 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1191 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1192 fwriteint64_t(0L, elffp); /* no entry point */
1193 fwriteint64_t(0L, elffp); /* no program header table */
1194 fwriteint64_t(0x40L, elffp); /* section headers straight after
1195 * ELF header plus alignment */
1196 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1197 fwriteint16_t(0x40, elffp); /* size of ELF header */
1198 fwriteint16_t(0, elffp); /* no program header table, again */
1199 fwriteint16_t(0, elffp); /* still no program header table */
1200 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1201 fwriteint16_t(nsections, elffp); /* number of sections */
1202 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1203 * section header table */
1206 * Build the symbol table and relocation tables.
1208 symtab = elf_build_symtab(&symtablen, &symtablocal);
1209 for (i = 0; i < nsects; i++)
1210 if (sects[i]->head)
1211 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1212 sects[i]->head);
1215 * Now output the section header table.
1218 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1219 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1220 elf_foffs += align;
1221 elf_nsect = 0;
1222 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1223 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1224 scount = 1; /* needed for the stabs debugging to track the symtable section */
1225 p = shstrtab + 1;
1226 for (i = 0; i < nsects; i++) {
1227 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1228 (sects[i]->type == SHT_PROGBITS ?
1229 sects[i]->data : NULL), true,
1230 sects[i]->len, 0, 0, sects[i]->align, 0);
1231 p += strlen(p) + 1;
1232 scount++; /* ditto */
1234 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1235 scount++; /* ditto */
1236 p += strlen(p) + 1;
1237 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1238 scount++; /* ditto */
1239 p += strlen(p) + 1;
1240 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1241 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1242 p += strlen(p) + 1;
1243 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1244 for (i = 0; i < nsects; i++)
1245 if (sects[i]->head) {
1246 p += strlen(p) + 1;
1247 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1248 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1250 if (of_elf64.current_dfmt == &df_stabs) {
1251 /* for debugging information, create the last three sections
1252 which are the .stab , .stabstr and .rel.stab sections respectively */
1254 /* this function call creates the stab sections in memory */
1255 stabs64_generate();
1257 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1258 p += strlen(p) + 1;
1259 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1260 nsections - 2, 0, 4, 12);
1262 p += strlen(p) + 1;
1263 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1264 stabstrlen, 0, 0, 4, 0);
1266 p += strlen(p) + 1;
1267 /* link -> symtable info -> section to refer to */
1268 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1269 stabrellen, symtabsection, nsections - 3, 4,
1270 16);
1273 else if (of_elf64.current_dfmt == &df_dwarf) {
1274 /* for dwarf debugging information, create the ten dwarf sections */
1276 /* this function call creates the dwarf sections in memory */
1277 if (dwarf_fsect) dwarf64_generate();
1279 p += strlen(p) + 1;
1280 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1281 arangeslen, 0, 0, 1, 0);
1282 p += strlen(p) + 1;
1283 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1284 arangesrellen, symtabsection, debug_aranges, 1, 24);
1285 p += strlen(p) + 1;
1286 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1287 pubnameslen, 0, 0, 1, 0);
1288 p += strlen(p) + 1;
1289 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1290 infolen, 0, 0, 1, 0);
1291 p += strlen(p) + 1;
1292 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1293 inforellen, symtabsection, debug_info, 1, 24);
1294 p += strlen(p) + 1;
1295 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1296 abbrevlen, 0, 0, 1, 0);
1297 p += strlen(p) + 1;
1298 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1299 linelen, 0, 0, 1, 0);
1300 p += strlen(p) + 1;
1301 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1302 linerellen, symtabsection, debug_line, 1, 24);
1303 p += strlen(p) + 1;
1304 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1305 framelen, 0, 0, 8, 0);
1306 p += strlen(p) + 1;
1307 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1308 loclen, 0, 0, 1, 0);
1311 fwrite(align_str, align, 1, elffp);
1314 * Now output the sections.
1316 elf_write_sections();
1318 nasm_free(elf_sects);
1319 saa_free(symtab);
1322 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1324 struct SAA *s = saa_init(1L);
1325 struct Symbol *sym;
1326 uint8_t entry[24], *p;
1327 int i;
1329 *len = *local = 0;
1332 * First, an all-zeros entry, required by the ELF spec.
1334 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1335 *len += 24;
1336 (*local)++;
1339 * Next, an entry for the file name.
1341 p = entry;
1342 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1343 WRITESHORT(p, STT_FILE); /* type FILE */
1344 WRITESHORT(p, SHN_ABS);
1345 WRITEDLONG(p, (uint64_t) 0); /* no value */
1346 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1347 saa_wbytes(s, entry, 24L);
1348 *len += 24;
1349 (*local)++;
1352 * Now some standard symbols defining the segments, for relocation
1353 * purposes.
1355 for (i = 1; i <= nsects; i++) {
1356 p = entry;
1357 WRITELONG(p, 0); /* no symbol name */
1358 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1359 WRITESHORT(p, i); /* section id */
1360 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1361 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1362 saa_wbytes(s, entry, 24L);
1363 *len += 24;
1364 (*local)++;
1369 * Now the other local symbols.
1371 saa_rewind(syms);
1372 while ((sym = saa_rstruct(syms))) {
1373 if (sym->type & SYM_GLOBAL)
1374 continue;
1375 p = entry;
1376 WRITELONG(p, sym->strpos); /* index into symbol string table */
1377 WRITECHAR(p, sym->type); /* type and binding */
1378 WRITECHAR(p, sym->other); /* visibility */
1379 WRITESHORT(p, sym->section); /* index into section header table */
1380 WRITEDLONG(p, (int64_t)sym->value); /* value of symbol */
1381 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1382 saa_wbytes(s, entry, 24L);
1383 *len += 24;
1384 (*local)++;
1387 * dwarf needs symbols for debug sections
1388 * which are relocation targets.
1390 if (of_elf64.current_dfmt == &df_dwarf) {
1391 dwarf_infosym = *local;
1392 p = entry;
1393 WRITELONG(p, 0); /* no symbol name */
1394 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1395 WRITESHORT(p, debug_info); /* section id */
1396 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1397 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1398 saa_wbytes(s, entry, 24L);
1399 *len += 24;
1400 (*local)++;
1401 dwarf_abbrevsym = *local;
1402 p = entry;
1403 WRITELONG(p, 0); /* no symbol name */
1404 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1405 WRITESHORT(p, debug_abbrev); /* section id */
1406 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1407 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1408 saa_wbytes(s, entry, 24L);
1409 *len += 24;
1410 (*local)++;
1411 dwarf_linesym = *local;
1412 p = entry;
1413 WRITELONG(p, 0); /* no symbol name */
1414 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1415 WRITESHORT(p, debug_line); /* section id */
1416 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1417 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1418 saa_wbytes(s, entry, 24L);
1419 *len += 24;
1420 (*local)++;
1424 * Now the global 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);
1432 WRITECHAR(p, sym->type); /* type and binding */
1433 WRITECHAR(p, sym->other); /* visibility */
1434 WRITESHORT(p, sym->section);
1435 WRITEDLONG(p, (int64_t)sym->value);
1436 WRITEDLONG(p, (int64_t)sym->size);
1437 saa_wbytes(s, entry, 24L);
1438 *len += 24;
1441 return s;
1444 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1446 struct SAA *s;
1447 uint8_t *p, entry[24];
1449 if (!r)
1450 return NULL;
1452 s = saa_init(1L);
1453 *len = 0;
1455 while (r) {
1456 int64_t sym = r->symbol;
1458 if (sym >= GLOBAL_TEMP_BASE)
1460 if (of_elf64.current_dfmt == &df_dwarf)
1461 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1462 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1464 p = entry;
1465 WRITEDLONG(p, r->address);
1466 WRITEDLONG(p, (sym << 32) + r->type);
1467 WRITEDLONG(p, (uint64_t) 0);
1468 saa_wbytes(s, entry, 24L);
1469 *len += 24;
1471 r = r->next;
1474 return s;
1477 static void elf_section_header(int name, int type, uint64_t flags,
1478 void *data, bool is_saa, uint64_t datalen,
1479 int link, int info, int align, int eltsize)
1481 elf_sects[elf_nsect].data = data;
1482 elf_sects[elf_nsect].len = datalen;
1483 elf_sects[elf_nsect].is_saa = is_saa;
1484 elf_nsect++;
1486 fwriteint32_t((int32_t)name, elffp);
1487 fwriteint32_t((int32_t)type, elffp);
1488 fwriteint64_t((int64_t)flags, elffp);
1489 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1490 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1491 fwriteint64_t(datalen, elffp);
1492 if (data)
1493 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1494 fwriteint32_t((int32_t)link, elffp);
1495 fwriteint32_t((int32_t)info, elffp);
1496 fwriteint64_t((int64_t)align, elffp);
1497 fwriteint64_t((int64_t)eltsize, elffp);
1500 static void elf_write_sections(void)
1502 int i;
1503 for (i = 0; i < elf_nsect; i++)
1504 if (elf_sects[i].data) {
1505 int32_t len = elf_sects[i].len;
1506 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1507 int32_t align = reallen - len;
1508 if (elf_sects[i].is_saa)
1509 saa_fpwrite(elf_sects[i].data, elffp);
1510 else
1511 fwrite(elf_sects[i].data, len, 1, elffp);
1512 fwrite(align_str, align, 1, elffp);
1516 static void elf_sect_write(struct Section *sect,
1517 const uint8_t *data, uint64_t len)
1519 saa_wbytes(sect->data, data, len);
1520 sect->len += len;
1523 static int32_t elf_segbase(int32_t segment)
1525 return segment;
1528 static int elf_directive(char *directive, char *value, int pass)
1530 bool err;
1531 int64_t n;
1532 char *p;
1534 if (!strcmp(directive, "osabi")) {
1535 if (pass == 2)
1536 return 1; /* ignore in pass 2 */
1538 n = readnum(value, &err);
1539 if (err) {
1540 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1541 return 1;
1543 if (n < 0 || n > 255) {
1544 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1545 return 1;
1547 elf_osabi = n;
1548 elf_abiver = 0;
1550 if ((p = strchr(value,',')) == NULL)
1551 return 1;
1553 n = readnum(p+1, &err);
1554 if (err || n < 0 || n > 255) {
1555 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1556 return 1;
1559 elf_abiver = n;
1560 return 1;
1563 return 0;
1566 static void elf_filename(char *inname, char *outname, efunc error)
1568 strcpy(elf_module, inname);
1569 standard_extension(inname, outname, ".o", error);
1572 extern macros_t elf_stdmac[];
1574 static int elf_set_info(enum geninfo type, char **val)
1576 (void)type;
1577 (void)val;
1578 return 0;
1580 static struct dfmt df_dwarf = {
1581 "ELF64 (X86_64) dwarf debug format for Linux",
1582 "dwarf",
1583 debug64_init,
1584 dwarf64_linenum,
1585 debug64_deflabel,
1586 debug64_directive,
1587 debug64_typevalue,
1588 dwarf64_output,
1589 dwarf64_cleanup
1591 static struct dfmt df_stabs = {
1592 "ELF64 (X86_64) stabs debug format for Linux",
1593 "stabs",
1594 debug64_init,
1595 stabs64_linenum,
1596 debug64_deflabel,
1597 debug64_directive,
1598 debug64_typevalue,
1599 stabs64_output,
1600 stabs64_cleanup
1603 struct dfmt *elf64_debugs_arr[3] = { &df_stabs, &df_dwarf, NULL };
1605 struct ofmt of_elf64 = {
1606 "ELF64 (x86_64) object files (e.g. Linux)",
1607 "elf64",
1608 NULL,
1609 elf64_debugs_arr,
1610 &null_debug_form,
1611 elf_stdmac,
1612 elf_init,
1613 elf_set_info,
1614 elf_out,
1615 elf_deflabel,
1616 elf_section_names,
1617 elf_segbase,
1618 elf_directive,
1619 elf_filename,
1620 elf_cleanup
1623 /* common debugging routines */
1624 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1626 (void)of;
1627 (void)id;
1628 (void)fp;
1629 (void)error;
1631 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1632 char *special)
1634 (void)name;
1635 (void)segment;
1636 (void)offset;
1637 (void)is_global;
1638 (void)special;
1641 void debug64_directive(const char *directive, const char *params)
1643 (void)directive;
1644 (void)params;
1647 void debug64_typevalue(int32_t type)
1649 int32_t stype, ssize;
1650 switch (TYM_TYPE(type)) {
1651 case TY_LABEL:
1652 ssize = 0;
1653 stype = STT_NOTYPE;
1654 break;
1655 case TY_BYTE:
1656 ssize = 1;
1657 stype = STT_OBJECT;
1658 break;
1659 case TY_WORD:
1660 ssize = 2;
1661 stype = STT_OBJECT;
1662 break;
1663 case TY_DWORD:
1664 ssize = 4;
1665 stype = STT_OBJECT;
1666 break;
1667 case TY_FLOAT:
1668 ssize = 4;
1669 stype = STT_OBJECT;
1670 break;
1671 case TY_QWORD:
1672 ssize = 8;
1673 stype = STT_OBJECT;
1674 break;
1675 case TY_TBYTE:
1676 ssize = 10;
1677 stype = STT_OBJECT;
1678 break;
1679 case TY_OWORD:
1680 ssize = 16;
1681 stype = STT_OBJECT;
1682 break;
1683 case TY_COMMON:
1684 ssize = 0;
1685 stype = STT_COMMON;
1686 break;
1687 case TY_SEG:
1688 ssize = 0;
1689 stype = STT_SECTION;
1690 break;
1691 case TY_EXTERN:
1692 ssize = 0;
1693 stype = STT_NOTYPE;
1694 break;
1695 case TY_EQU:
1696 ssize = 0;
1697 stype = STT_NOTYPE;
1698 break;
1699 default:
1700 ssize = 0;
1701 stype = STT_NOTYPE;
1702 break;
1704 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1705 lastsym->size = ssize;
1706 lastsym->type = stype;
1710 /* stabs debugging routines */
1713 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1715 (void)segto;
1716 if (!stabs_filename) {
1717 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1718 strcpy(stabs_filename, filename);
1719 } else {
1720 if (strcmp(stabs_filename, filename)) {
1721 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1722 in fact, this leak comes in quite handy to maintain a list of files
1723 encountered so far in the symbol lines... */
1725 /* why not nasm_free(stabs_filename); we're done with the old one */
1727 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1728 strcpy(stabs_filename, filename);
1731 debug_immcall = 1;
1732 currentline = linenumber;
1736 void stabs64_output(int type, void *param)
1738 struct symlininfo *s;
1739 struct linelist *el;
1740 if (type == TY_DEBUGSYMLIN) {
1741 if (debug_immcall) {
1742 s = (struct symlininfo *)param;
1743 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1744 return; /* line info is only collected for executable sections */
1745 numlinestabs++;
1746 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1747 el->info.offset = s->offset;
1748 el->info.section = s->section;
1749 el->info.name = s->name;
1750 el->line = currentline;
1751 el->filename = stabs_filename;
1752 el->next = 0;
1753 if (stabslines) {
1754 stabslines->last->next = el;
1755 stabslines->last = el;
1756 } else {
1757 stabslines = el;
1758 stabslines->last = el;
1762 debug_immcall = 0;
1765 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1766 do {\
1767 WRITELONG(p,n_strx); \
1768 WRITECHAR(p,n_type); \
1769 WRITECHAR(p,n_other); \
1770 WRITESHORT(p,n_desc); \
1771 WRITELONG(p,n_value); \
1772 } while (0)
1774 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1776 void stabs64_generate(void)
1778 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1779 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1780 char **allfiles;
1781 int *fileidx;
1783 struct linelist *ptr;
1785 ptr = stabslines;
1787 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1788 for (i = 0; i < numlinestabs; i++)
1789 allfiles[i] = 0;
1790 numfiles = 0;
1791 while (ptr) {
1792 if (numfiles == 0) {
1793 allfiles[0] = ptr->filename;
1794 numfiles++;
1795 } else {
1796 for (i = 0; i < numfiles; i++) {
1797 if (!strcmp(allfiles[i], ptr->filename))
1798 break;
1800 if (i >= numfiles) {
1801 allfiles[i] = ptr->filename;
1802 numfiles++;
1805 ptr = ptr->next;
1807 strsize = 1;
1808 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1809 for (i = 0; i < numfiles; i++) {
1810 fileidx[i] = strsize;
1811 strsize += strlen(allfiles[i]) + 1;
1813 mainfileindex = 0;
1814 for (i = 0; i < numfiles; i++) {
1815 if (!strcmp(allfiles[i], elf_module)) {
1816 mainfileindex = i;
1817 break;
1821 /* worst case size of the stab buffer would be:
1822 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1824 sbuf =
1825 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1826 sizeof(struct stabentry));
1828 ssbuf = (uint8_t *)nasm_malloc(strsize);
1830 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1831 rptr = rbuf;
1833 for (i = 0; i < numfiles; i++) {
1834 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1836 ssbuf[0] = 0;
1838 stabstrlen = strsize; /* set global variable for length of stab strings */
1840 sptr = sbuf;
1841 ptr = stabslines;
1842 numstabs = 0;
1844 if (ptr) {
1845 /* this is the first stab, its strx points to the filename of the
1846 the source-file, the n_desc field should be set to the number
1847 of remaining stabs
1849 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1851 /* this is the stab for the main source file */
1852 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1854 /* relocation table entry */
1856 /* Since the symbol table has two entries before */
1857 /* the section symbols, the index in the info.section */
1858 /* member must be adjusted by adding 2 */
1860 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1861 WRITELONG(rptr, R_X86_64_32);
1862 WRITELONG(rptr, ptr->info.section + 2);
1864 numstabs++;
1865 currfile = mainfileindex;
1868 while (ptr) {
1869 if (strcmp(allfiles[currfile], ptr->filename)) {
1870 /* oops file has changed... */
1871 for (i = 0; i < numfiles; i++)
1872 if (!strcmp(allfiles[i], ptr->filename))
1873 break;
1874 currfile = i;
1875 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1876 ptr->info.offset);
1877 numstabs++;
1879 /* relocation table entry */
1881 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1882 WRITELONG(rptr, R_X86_64_32);
1883 WRITELONG(rptr, ptr->info.section + 2);
1886 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1887 numstabs++;
1889 /* relocation table entry */
1891 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1892 WRITELONG(rptr, R_X86_64_32);
1893 WRITELONG(rptr, ptr->info.section + 2);
1895 ptr = ptr->next;
1899 ((struct stabentry *)sbuf)->n_desc = numstabs;
1901 nasm_free(allfiles);
1902 nasm_free(fileidx);
1904 stablen = (sptr - sbuf);
1905 stabrellen = (rptr - rbuf);
1906 stabrelbuf = rbuf;
1907 stabbuf = sbuf;
1908 stabstrbuf = ssbuf;
1911 void stabs64_cleanup(void)
1913 struct linelist *ptr, *del;
1914 if (!stabslines)
1915 return;
1916 ptr = stabslines;
1917 while (ptr) {
1918 del = ptr;
1919 ptr = ptr->next;
1920 nasm_free(del);
1922 if (stabbuf)
1923 nasm_free(stabbuf);
1924 if (stabrelbuf)
1925 nasm_free(stabrelbuf);
1926 if (stabstrbuf)
1927 nasm_free(stabstrbuf);
1929 /* dwarf routines */
1932 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1934 (void)segto;
1935 dwarf64_findfile(filename);
1936 debug_immcall = 1;
1937 currentline = linenumber;
1940 /* called from elf_out with type == TY_DEBUGSYMLIN */
1941 void dwarf64_output(int type, void *param)
1943 int ln, aa, inx, maxln, soc;
1944 struct symlininfo *s;
1945 struct SAA *plinep;
1947 (void)type;
1949 s = (struct symlininfo *)param;
1950 /* line number info is only gathered for executable sections */
1951 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1952 return;
1953 /* Check if section index has changed */
1954 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1956 dwarf64_findsect(s->section);
1958 /* do nothing unless line or file has changed */
1959 if (debug_immcall)
1961 ln = currentline - dwarf_csect->line;
1962 aa = s->offset - dwarf_csect->offset;
1963 inx = dwarf_clist->line;
1964 plinep = dwarf_csect->psaa;
1965 /* check for file change */
1966 if (!(inx == dwarf_csect->file))
1968 saa_write8(plinep,DW_LNS_set_file);
1969 saa_write8(plinep,inx);
1970 dwarf_csect->file = inx;
1972 /* check for line change */
1973 if (ln)
1975 /* test if in range of special op code */
1976 maxln = line_base + line_range;
1977 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1978 if (ln >= line_base && ln < maxln && soc < 256)
1980 saa_write8(plinep,soc);
1982 else
1984 if (ln)
1986 saa_write8(plinep,DW_LNS_advance_line);
1987 saa_wleb128s(plinep,ln);
1989 if (aa)
1991 saa_write8(plinep,DW_LNS_advance_pc);
1992 saa_wleb128u(plinep,aa);
1995 dwarf_csect->line = currentline;
1996 dwarf_csect->offset = s->offset;
1998 /* show change handled */
1999 debug_immcall = 0;
2004 void dwarf64_generate(void)
2006 static const char nasm_signature[] = "NASM " NASM_VER;
2007 uint8_t *pbuf;
2008 int indx;
2009 struct linelist *ftentry;
2010 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2011 struct SAA *parangesrel, *plinesrel, *pinforel;
2012 struct sectlist *psect;
2013 size_t saalen, linepoff, totlen, highaddr;
2015 /* write epilogues for each line program range */
2016 /* and build aranges section */
2017 paranges = saa_init(1L);
2018 parangesrel = saa_init(1L);
2019 saa_write16(paranges,3); /* dwarf version */
2020 saa_write64(parangesrel, paranges->datalen+4);
2021 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2022 saa_write64(parangesrel, 0);
2023 saa_write32(paranges,0); /* offset into info */
2024 saa_write8(paranges,8); /* pointer size */
2025 saa_write8(paranges,0); /* not segmented */
2026 saa_write32(paranges,0); /* padding */
2027 /* iterate though sectlist entries */
2028 psect = dwarf_fsect;
2029 totlen = 0;
2030 highaddr = 0;
2031 for (indx = 0; indx < dwarf_nsections; indx++)
2033 plinep = psect->psaa;
2034 /* Line Number Program Epilogue */
2035 saa_write8(plinep,2); /* std op 2 */
2036 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2037 saa_write8(plinep,DW_LNS_extended_op);
2038 saa_write8(plinep,1); /* operand length */
2039 saa_write8(plinep,DW_LNE_end_sequence);
2040 totlen += plinep->datalen;
2041 /* range table relocation entry */
2042 saa_write64(parangesrel, paranges->datalen + 4);
2043 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2044 saa_write64(parangesrel, (uint64_t) 0);
2045 /* range table entry */
2046 saa_write64(paranges,0x0000); /* range start */
2047 saa_write64(paranges,sects[psect->section]->len); /* range length */
2048 highaddr += sects[psect->section]->len;
2049 /* done with this entry */
2050 psect = psect->next;
2052 saa_write64(paranges,0); /* null address */
2053 saa_write64(paranges,0); /* null length */
2054 saalen = paranges->datalen;
2055 arangeslen = saalen + 4;
2056 arangesbuf = pbuf = nasm_malloc(arangeslen);
2057 WRITELONG(pbuf,saalen); /* initial length */
2058 saa_rnbytes(paranges, pbuf, saalen);
2059 saa_free(paranges);
2061 /* build rela.aranges section */
2062 arangesrellen = saalen = parangesrel->datalen;
2063 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2064 saa_rnbytes(parangesrel, pbuf, saalen);
2065 saa_free(parangesrel);
2067 /* build pubnames section */
2068 ppubnames = saa_init(1L);
2069 saa_write16(ppubnames,3); /* dwarf version */
2070 saa_write32(ppubnames,0); /* offset into info */
2071 saa_write32(ppubnames,0); /* space used in info */
2072 saa_write32(ppubnames,0); /* end of list */
2073 saalen = ppubnames->datalen;
2074 pubnameslen = saalen + 4;
2075 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2076 WRITELONG(pbuf,saalen); /* initial length */
2077 saa_rnbytes(ppubnames, pbuf, saalen);
2078 saa_free(ppubnames);
2080 /* build info section */
2081 pinfo = saa_init(1L);
2082 pinforel = saa_init(1L);
2083 saa_write16(pinfo,3); /* dwarf version */
2084 saa_write64(pinforel, pinfo->datalen + 4);
2085 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2086 saa_write64(pinforel, 0);
2087 saa_write32(pinfo,0); /* offset into abbrev */
2088 saa_write8(pinfo,8); /* pointer size */
2089 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2090 saa_write64(pinforel, pinfo->datalen + 4);
2091 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2092 saa_write64(pinforel, 0);
2093 saa_write64(pinfo,0); /* DW_AT_low_pc */
2094 saa_write64(pinforel, pinfo->datalen + 4);
2095 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2096 saa_write64(pinforel, 0);
2097 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2098 saa_write64(pinforel, pinfo->datalen + 4);
2099 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2100 saa_write64(pinforel, 0);
2101 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2102 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2103 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2104 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2105 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2106 saa_write64(pinforel, pinfo->datalen + 4);
2107 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2108 saa_write64(pinforel, 0);
2109 saa_write64(pinfo,0); /* DW_AT_low_pc */
2110 saa_write64(pinfo,0); /* DW_AT_frame_base */
2111 saa_write8(pinfo,0); /* end of entries */
2112 saalen = pinfo->datalen;
2113 infolen = saalen + 4;
2114 infobuf = pbuf = nasm_malloc(infolen);
2115 WRITELONG(pbuf,saalen); /* initial length */
2116 saa_rnbytes(pinfo, pbuf, saalen);
2117 saa_free(pinfo);
2119 /* build rela.info section */
2120 inforellen = saalen = pinforel->datalen;
2121 inforelbuf = pbuf = nasm_malloc(inforellen);
2122 saa_rnbytes(pinforel, pbuf, saalen);
2123 saa_free(pinforel);
2125 /* build abbrev section */
2126 pabbrev = saa_init(1L);
2127 saa_write8(pabbrev,1); /* entry number LEB128u */
2128 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2129 saa_write8(pabbrev,1); /* has children */
2130 /* the following attributes and forms are all LEB128u values */
2131 saa_write8(pabbrev,DW_AT_low_pc);
2132 saa_write8(pabbrev,DW_FORM_addr);
2133 saa_write8(pabbrev,DW_AT_high_pc);
2134 saa_write8(pabbrev,DW_FORM_addr);
2135 saa_write8(pabbrev,DW_AT_stmt_list);
2136 saa_write8(pabbrev,DW_FORM_data4);
2137 saa_write8(pabbrev,DW_AT_name);
2138 saa_write8(pabbrev,DW_FORM_string);
2139 saa_write8(pabbrev,DW_AT_producer);
2140 saa_write8(pabbrev,DW_FORM_string);
2141 saa_write8(pabbrev,DW_AT_language);
2142 saa_write8(pabbrev,DW_FORM_data2);
2143 saa_write16(pabbrev,0); /* end of entry */
2144 /* LEB128u usage same as above */
2145 saa_write8(pabbrev,2); /* entry number */
2146 saa_write8(pabbrev,DW_TAG_subprogram);
2147 saa_write8(pabbrev,0); /* no children */
2148 saa_write8(pabbrev,DW_AT_low_pc);
2149 saa_write8(pabbrev,DW_FORM_addr);
2150 saa_write8(pabbrev,DW_AT_frame_base);
2151 saa_write8(pabbrev,DW_FORM_data4);
2152 saa_write16(pabbrev,0); /* end of entry */
2153 abbrevlen = saalen = pabbrev->datalen;
2154 abbrevbuf = pbuf = nasm_malloc(saalen);
2155 saa_rnbytes(pabbrev, pbuf, saalen);
2156 saa_free(pabbrev);
2158 /* build line section */
2159 /* prolog */
2160 plines = saa_init(1L);
2161 saa_write8(plines,1); /* Minimum Instruction Length */
2162 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2163 saa_write8(plines,line_base); /* Line Base */
2164 saa_write8(plines,line_range); /* Line Range */
2165 saa_write8(plines,opcode_base); /* Opcode Base */
2166 /* standard opcode lengths (# of LEB128u operands) */
2167 saa_write8(plines,0); /* Std opcode 1 length */
2168 saa_write8(plines,1); /* Std opcode 2 length */
2169 saa_write8(plines,1); /* Std opcode 3 length */
2170 saa_write8(plines,1); /* Std opcode 4 length */
2171 saa_write8(plines,1); /* Std opcode 5 length */
2172 saa_write8(plines,0); /* Std opcode 6 length */
2173 saa_write8(plines,0); /* Std opcode 7 length */
2174 saa_write8(plines,0); /* Std opcode 8 length */
2175 saa_write8(plines,1); /* Std opcode 9 length */
2176 saa_write8(plines,0); /* Std opcode 10 length */
2177 saa_write8(plines,0); /* Std opcode 11 length */
2178 saa_write8(plines,1); /* Std opcode 12 length */
2179 /* Directory Table */
2180 saa_write8(plines,0); /* End of table */
2181 /* File Name Table */
2182 ftentry = dwarf_flist;
2183 for (indx = 0;indx<dwarf_numfiles;indx++)
2185 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2186 saa_write8(plines,0); /* directory LEB128u */
2187 saa_write8(plines,0); /* time LEB128u */
2188 saa_write8(plines,0); /* size LEB128u */
2189 ftentry = ftentry->next;
2191 saa_write8(plines,0); /* End of table */
2192 linepoff = plines->datalen;
2193 linelen = linepoff + totlen + 10;
2194 linebuf = pbuf = nasm_malloc(linelen);
2195 WRITELONG(pbuf,linelen-4); /* initial length */
2196 WRITESHORT(pbuf,3); /* dwarf version */
2197 WRITELONG(pbuf,linepoff); /* offset to line number program */
2198 /* write line header */
2199 saalen = linepoff;
2200 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2201 pbuf += linepoff;
2202 saa_free(plines);
2203 /* concatonate line program ranges */
2204 linepoff += 13;
2205 plinesrel = saa_init(1L);
2206 psect = dwarf_fsect;
2207 for (indx = 0; indx < dwarf_nsections; indx++)
2209 saa_write64(plinesrel, linepoff);
2210 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2211 saa_write64(plinesrel, (uint64_t) 0);
2212 plinep = psect->psaa;
2213 saalen = plinep->datalen;
2214 saa_rnbytes(plinep, pbuf, saalen);
2215 pbuf += saalen;
2216 linepoff += saalen;
2217 saa_free(plinep);
2218 /* done with this entry */
2219 psect = psect->next;
2223 /* build rela.lines section */
2224 linerellen =saalen = plinesrel->datalen;
2225 linerelbuf = pbuf = nasm_malloc(linerellen);
2226 saa_rnbytes(plinesrel, pbuf, saalen);
2227 saa_free(plinesrel);
2229 /* build frame section */
2230 framelen = 4;
2231 framebuf = pbuf = nasm_malloc(framelen);
2232 WRITELONG(pbuf,framelen-4); /* initial length */
2234 /* build loc section */
2235 loclen = 16;
2236 locbuf = pbuf = nasm_malloc(loclen);
2237 WRITEDLONG(pbuf,0); /* null beginning offset */
2238 WRITEDLONG(pbuf,0); /* null ending offset */
2241 void dwarf64_cleanup(void)
2243 if (arangesbuf)
2244 nasm_free(arangesbuf);
2245 if (arangesrelbuf)
2246 nasm_free(arangesrelbuf);
2247 if (pubnamesbuf)
2248 nasm_free(pubnamesbuf);
2249 if (infobuf)
2250 nasm_free(infobuf);
2251 if (inforelbuf)
2252 nasm_free(inforelbuf);
2253 if (abbrevbuf)
2254 nasm_free(abbrevbuf);
2255 if (linebuf)
2256 nasm_free(linebuf);
2257 if (linerelbuf)
2258 nasm_free(linerelbuf);
2259 if (framebuf)
2260 nasm_free(framebuf);
2261 if (locbuf)
2262 nasm_free(locbuf);
2264 void dwarf64_findfile(const char * fname)
2266 int finx;
2267 struct linelist *match;
2269 /* return if fname is current file name */
2270 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2271 /* search for match */
2272 else
2274 match = 0;
2275 if (dwarf_flist)
2277 match = dwarf_flist;
2278 for (finx = 0; finx < dwarf_numfiles; finx++)
2280 if (!(strcmp(fname, match->filename)))
2282 dwarf_clist = match;
2283 return;
2287 /* add file name to end of list */
2288 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2289 dwarf_numfiles++;
2290 dwarf_clist->line = dwarf_numfiles;
2291 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2292 strcpy(dwarf_clist->filename,fname);
2293 dwarf_clist->next = 0;
2294 /* if first entry */
2295 if (!dwarf_flist)
2297 dwarf_flist = dwarf_elist = dwarf_clist;
2298 dwarf_clist->last = 0;
2300 /* chain to previous entry */
2301 else
2303 dwarf_elist->next = dwarf_clist;
2304 dwarf_elist = dwarf_clist;
2308 /* */
2309 void dwarf64_findsect(const int index)
2311 int sinx;
2312 struct sectlist *match;
2313 struct SAA *plinep;
2314 /* return if index is current section index */
2315 if (dwarf_csect && (dwarf_csect->section == index))
2317 return;
2319 /* search for match */
2320 else
2322 match = 0;
2323 if (dwarf_fsect)
2325 match = dwarf_fsect;
2326 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2328 if ((match->section == index))
2330 dwarf_csect = match;
2331 return;
2333 match = match->next;
2336 /* add entry to end of list */
2337 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2338 dwarf_nsections++;
2339 dwarf_csect->psaa = plinep = saa_init(1L);
2340 dwarf_csect->line = 1;
2341 dwarf_csect->offset = 0;
2342 dwarf_csect->file = 1;
2343 dwarf_csect->section = index;
2344 dwarf_csect->next = 0;
2345 /* set relocatable address at start of line program */
2346 saa_write8(plinep,DW_LNS_extended_op);
2347 saa_write8(plinep,9); /* operand length */
2348 saa_write8(plinep,DW_LNE_set_address);
2349 saa_write64(plinep,0); /* Start Address */
2350 /* if first entry */
2351 if (!dwarf_fsect)
2353 dwarf_fsect = dwarf_esect = dwarf_csect;
2354 dwarf_csect->last = 0;
2356 /* chain to previous entry */
2357 else
2359 dwarf_esect->next = dwarf_csect;
2360 dwarf_esect = dwarf_csect;
2365 #endif /* OF_ELF */