ELF64: We apparently don't need exactitude for GOTOFF64
[nasm/perl-rewrite.git] / output / outelf64.c
blob1405d2fdd6f13f79e45ad8a39210c831794d3c44
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 int64_t offset; /* symbol addend */
138 int type; /* type of relocation */
141 struct Symbol {
142 int32_t strpos; /* string table position of name */
143 int32_t section; /* section ID of the symbol */
144 int type; /* symbol type */
145 int other; /* symbol visibility */
146 int64_t value; /* address, or COMMON variable align */
147 int32_t size; /* size of symbol */
148 int32_t globnum; /* symbol table offset if global */
149 struct Symbol *next; /* list of globals in each section */
150 struct Symbol *nextfwd; /* list of unresolved-size symbols */
151 char *name; /* used temporarily if in above list */
155 struct Section {
156 struct SAA *data;
157 uint64_t len, size;
158 uint32_t nrelocs;
159 int32_t index; /* index into sects array */
160 uint32_t type; /* SHT_PROGBITS or SHT_NOBITS */
161 uint64_t align; /* alignment: power of two */
162 uint64_t flags; /* section flags */
163 char *name;
164 struct SAA *rel;
165 uint64_t rellen;
166 struct Reloc *head, **tail;
167 struct Symbol *gsyms; /* global symbols in section */
170 #define SECT_DELTA 32
171 static struct Section **sects;
172 static int nsects, sectlen;
174 #define SHSTR_DELTA 256
175 static char *shstrtab;
176 static int shstrtablen, shstrtabsize;
178 static struct SAA *syms;
179 static uint32_t nlocals, nglobs;
181 static int32_t def_seg;
183 static struct RAA *bsym;
185 static struct SAA *strs;
186 static uint32_t strslen;
188 static FILE *elffp;
189 static efunc error;
190 static evalfunc evaluate;
192 static struct Symbol *fwds;
194 static char elf_module[FILENAME_MAX];
196 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
197 static uint8_t elf_abiver = 0; /* Current ABI version */
199 extern struct ofmt of_elf64;
201 #define SHN_UNDEF 0
203 #define SYM_GLOBAL 0x10
205 #define STV_DEFAULT 0
206 #define STV_INTERNAL 1
207 #define STV_HIDDEN 2
208 #define STV_PROTECTED 3
210 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
212 #define SEG_ALIGN 16 /* alignment of sections in file */
213 #define SEG_ALIGN_1 (SEG_ALIGN-1)
215 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
217 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
219 static struct ELF_SECTDATA {
220 void *data;
221 int64_t len;
222 bool is_saa;
223 } *elf_sects;
224 static int elf_nsect, nsections;
225 static int64_t elf_foffs;
227 static void elf_write(void);
228 static void elf_sect_write(struct Section *, const void *, size_t);
229 static void elf_sect_writeaddr(struct Section *, int64_t, size_t);
230 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
231 int, int);
232 static void elf_write_sections(void);
233 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
234 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
235 static void add_sectname(char *, char *);
237 /* type values for stabs debugging sections */
238 #define N_SO 0x64 /* ID for main source file */
239 #define N_SOL 0x84 /* ID for sub-source file */
240 #define N_BINCL 0x82 /* not currently used */
241 #define N_EINCL 0xA2 /* not currently used */
242 #define N_SLINE 0x44
244 struct stabentry {
245 uint32_t n_strx;
246 uint8_t n_type;
247 uint8_t n_other;
248 uint16_t n_desc;
249 uint32_t n_value;
252 struct erel {
253 int offset, info;
256 struct symlininfo {
257 int offset;
258 int section; /* index into sects[] */
259 int segto; /* internal section number */
260 char *name; /* shallow-copied pointer of section name */
263 struct linelist {
264 struct symlininfo info;
265 int line;
266 char *filename;
267 struct linelist *next;
268 struct linelist *last;
271 struct sectlist {
272 struct SAA *psaa;
273 int section;
274 int line;
275 int offset;
276 int file;
277 struct sectlist *next;
278 struct sectlist *last;
281 /* common debug variables */
282 static int currentline = 1;
283 static int debug_immcall = 0;
285 /* stabs debug variables */
286 static struct linelist *stabslines = 0;
287 static int numlinestabs = 0;
288 static char *stabs_filename = 0;
289 static int symtabsection;
290 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
291 static int stablen, stabstrlen, stabrellen;
293 /* dwarf debug variables */
294 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
295 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
296 static int dwarf_numfiles = 0, dwarf_nsections;
297 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
298 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
299 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
300 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
301 abbrevlen, linelen, linerellen, framelen, loclen;
302 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
305 static struct dfmt df_dwarf;
306 static struct dfmt df_stabs;
307 static struct Symbol *lastsym;
309 /* common debugging routines */
310 void debug64_typevalue(int32_t);
311 void debug64_init(struct ofmt *, void *, FILE *, efunc);
312 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
313 void debug64_directive(const char *, const char *);
315 /* stabs debugging routines */
316 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
317 void stabs64_output(int, void *);
318 void stabs64_generate(void);
319 void stabs64_cleanup(void);
321 /* dwarf debugging routines */
322 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
323 void dwarf64_output(int, void *);
324 void dwarf64_generate(void);
325 void dwarf64_cleanup(void);
326 void dwarf64_findfile(const char *);
327 void dwarf64_findsect(const int);
330 * Special section numbers which are used to define ELF special
331 * symbols, which can be used with WRT to provide PIC relocation
332 * types.
334 static int32_t elf_gotpc_sect, elf_gotoff_sect;
335 static int32_t elf_got_sect, elf_plt_sect;
336 static int32_t elf_sym_sect;
338 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
340 maxbits = 64;
341 elffp = fp;
342 error = errfunc;
343 evaluate = eval;
344 (void)ldef; /* placate optimisers */
345 sects = NULL;
346 nsects = sectlen = 0;
347 syms = saa_init((int32_t)sizeof(struct Symbol));
348 nlocals = nglobs = 0;
349 bsym = raa_init();
350 strs = saa_init(1L);
351 saa_wbytes(strs, "\0", 1L);
352 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
353 strslen = 2 + strlen(elf_module);
354 shstrtab = NULL;
355 shstrtablen = shstrtabsize = 0;;
356 add_sectname("", "");
358 fwds = NULL;
360 elf_gotpc_sect = seg_alloc();
361 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
362 error);
363 elf_gotoff_sect = seg_alloc();
364 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
365 error);
366 elf_got_sect = seg_alloc();
367 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
368 error);
369 elf_plt_sect = seg_alloc();
370 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
371 error);
372 elf_sym_sect = seg_alloc();
373 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
374 error);
376 def_seg = seg_alloc();
380 static void elf_cleanup(int debuginfo)
382 struct Reloc *r;
383 int i;
385 (void)debuginfo;
387 elf_write();
388 fclose(elffp);
389 for (i = 0; i < nsects; i++) {
390 if (sects[i]->type != SHT_NOBITS)
391 saa_free(sects[i]->data);
392 if (sects[i]->head)
393 saa_free(sects[i]->rel);
394 while (sects[i]->head) {
395 r = sects[i]->head;
396 sects[i]->head = sects[i]->head->next;
397 nasm_free(r);
400 nasm_free(sects);
401 saa_free(syms);
402 raa_free(bsym);
403 saa_free(strs);
404 if (of_elf64.current_dfmt) {
405 of_elf64.current_dfmt->cleanup();
408 /* add entry to the elf .shstrtab section */
409 static void add_sectname(char *firsthalf, char *secondhalf)
411 int len = strlen(firsthalf) + strlen(secondhalf);
412 while (shstrtablen + len + 1 > shstrtabsize)
413 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
414 strcpy(shstrtab + shstrtablen, firsthalf);
415 strcat(shstrtab + shstrtablen, secondhalf);
416 shstrtablen += len + 1;
419 static int elf_make_section(char *name, int type, int flags, int align)
421 struct Section *s;
423 s = nasm_malloc(sizeof(*s));
425 if (type != SHT_NOBITS)
426 s->data = saa_init(1L);
427 s->head = NULL;
428 s->tail = &s->head;
429 s->len = s->size = 0;
430 s->nrelocs = 0;
431 if (!strcmp(name, ".text"))
432 s->index = def_seg;
433 else
434 s->index = seg_alloc();
435 add_sectname("", name);
436 s->name = nasm_malloc(1 + strlen(name));
437 strcpy(s->name, name);
438 s->type = type;
439 s->flags = flags;
440 s->align = align;
441 s->gsyms = NULL;
443 if (nsects >= sectlen)
444 sects =
445 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
446 sects[nsects++] = s;
448 return nsects - 1;
451 static int32_t elf_section_names(char *name, int pass, int *bits)
453 char *p;
454 unsigned flags_and, flags_or;
455 uint64_t type, align;
456 int i;
459 * Default is 64 bits.
461 if (!name) {
462 *bits = 64;
463 return def_seg;
466 p = name;
467 while (*p && !nasm_isspace(*p))
468 p++;
469 if (*p)
470 *p++ = '\0';
471 flags_and = flags_or = type = align = 0;
473 while (*p && nasm_isspace(*p))
474 p++;
475 while (*p) {
476 char *q = p;
477 while (*p && !nasm_isspace(*p))
478 p++;
479 if (*p)
480 *p++ = '\0';
481 while (*p && nasm_isspace(*p))
482 p++;
484 if (!nasm_strnicmp(q, "align=", 6)) {
485 align = atoi(q + 6);
486 if (align == 0)
487 align = 1;
488 if ((align - 1) & align) { /* means it's not a power of two */
489 error(ERR_NONFATAL, "section alignment %d is not"
490 " a power of two", align);
491 align = 1;
493 } else if (!nasm_stricmp(q, "alloc")) {
494 flags_and |= SHF_ALLOC;
495 flags_or |= SHF_ALLOC;
496 } else if (!nasm_stricmp(q, "noalloc")) {
497 flags_and |= SHF_ALLOC;
498 flags_or &= ~SHF_ALLOC;
499 } else if (!nasm_stricmp(q, "exec")) {
500 flags_and |= SHF_EXECINSTR;
501 flags_or |= SHF_EXECINSTR;
502 } else if (!nasm_stricmp(q, "noexec")) {
503 flags_and |= SHF_EXECINSTR;
504 flags_or &= ~SHF_EXECINSTR;
505 } else if (!nasm_stricmp(q, "write")) {
506 flags_and |= SHF_WRITE;
507 flags_or |= SHF_WRITE;
508 } else if (!nasm_stricmp(q, "nowrite")) {
509 flags_and |= SHF_WRITE;
510 flags_or &= ~SHF_WRITE;
511 } else if (!nasm_stricmp(q, "progbits")) {
512 type = SHT_PROGBITS;
513 } else if (!nasm_stricmp(q, "nobits")) {
514 type = SHT_NOBITS;
518 if (!strcmp(name, ".comment") ||
519 !strcmp(name, ".shstrtab") ||
520 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
521 error(ERR_NONFATAL, "attempt to redefine reserved section"
522 "name `%s'", name);
523 return NO_SEG;
526 for (i = 0; i < nsects; i++)
527 if (!strcmp(name, sects[i]->name))
528 break;
529 if (i == nsects) {
530 if (!strcmp(name, ".text"))
531 i = elf_make_section(name, SHT_PROGBITS,
532 SHF_ALLOC | SHF_EXECINSTR, 16);
533 else if (!strcmp(name, ".rodata"))
534 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
535 else if (!strcmp(name, ".data"))
536 i = elf_make_section(name, SHT_PROGBITS,
537 SHF_ALLOC | SHF_WRITE, 4);
538 else if (!strcmp(name, ".bss"))
539 i = elf_make_section(name, SHT_NOBITS,
540 SHF_ALLOC | SHF_WRITE, 4);
541 else
542 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
543 if (type)
544 sects[i]->type = type;
545 if (align)
546 sects[i]->align = align;
547 sects[i]->flags &= ~flags_and;
548 sects[i]->flags |= flags_or;
549 } else if (pass == 1) {
550 if ((type && sects[i]->type != type)
551 || (align && sects[i]->align != align)
552 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
553 error(ERR_WARNING, "incompatible section attributes ignored on"
554 " redeclaration of section `%s'", name);
557 return sects[i]->index;
560 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
561 int is_global, char *special)
563 int pos = strslen;
564 struct Symbol *sym;
565 bool special_used = false;
567 #if defined(DEBUG) && DEBUG>2
568 fprintf(stderr,
569 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
570 name, segment, offset, is_global, special);
571 #endif
572 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
574 * This is a NASM special symbol. We never allow it into
575 * the ELF symbol table, even if it's a valid one. If it
576 * _isn't_ a valid one, we should barf immediately.
578 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
579 strcmp(name, "..got") && strcmp(name, "..plt") &&
580 strcmp(name, "..sym"))
581 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
582 return;
585 if (is_global == 3) {
586 struct Symbol **s;
588 * Fix up a forward-reference symbol size from the first
589 * pass.
591 for (s = &fwds; *s; s = &(*s)->nextfwd)
592 if (!strcmp((*s)->name, name)) {
593 struct tokenval tokval;
594 expr *e;
595 char *p = special;
597 while (*p && !nasm_isspace(*p))
598 p++;
599 while (*p && nasm_isspace(*p))
600 p++;
601 stdscan_reset();
602 stdscan_bufptr = p;
603 tokval.t_type = TOKEN_INVALID;
604 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
605 if (e) {
606 if (!is_simple(e))
607 error(ERR_NONFATAL, "cannot use relocatable"
608 " expression as symbol size");
609 else
610 (*s)->size = reloc_value(e);
614 * Remove it from the list of unresolved sizes.
616 nasm_free((*s)->name);
617 *s = (*s)->nextfwd;
618 return;
620 return; /* it wasn't an important one */
623 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
624 strslen += 1 + strlen(name);
626 lastsym = sym = saa_wstruct(syms);
628 sym->strpos = pos;
629 sym->type = is_global ? SYM_GLOBAL : 0;
630 sym->other = STV_DEFAULT;
631 sym->size = 0;
632 if (segment == NO_SEG)
633 sym->section = SHN_ABS;
634 else {
635 int i;
636 sym->section = SHN_UNDEF;
637 if (nsects == 0 && segment == def_seg) {
638 int tempint;
639 if (segment != elf_section_names(".text", 2, &tempint))
640 error(ERR_PANIC,
641 "strange segment conditions in ELF driver");
642 sym->section = nsects;
643 } else {
644 for (i = 0; i < nsects; i++)
645 if (segment == sects[i]->index) {
646 sym->section = i + 1;
647 break;
652 if (is_global == 2) {
653 sym->size = offset;
654 sym->value = 0;
655 sym->section = SHN_COMMON;
657 * We have a common variable. Check the special text to see
658 * if it's a valid number and power of two; if so, store it
659 * as the alignment for the common variable.
661 if (special) {
662 bool err;
663 sym->value = readnum(special, &err);
664 if (err)
665 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
666 " valid number", special);
667 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
668 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
669 " power of two", special);
671 special_used = true;
672 } else
673 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
675 if (sym->type == SYM_GLOBAL) {
677 * If sym->section == SHN_ABS, then the first line of the
678 * else section would cause a core dump, because its a reference
679 * beyond the end of the section array.
680 * This behaviour is exhibited by this code:
681 * GLOBAL crash_nasm
682 * crash_nasm equ 0
683 * To avoid such a crash, such requests are silently discarded.
684 * This may not be the best solution.
686 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
687 bsym = raa_write(bsym, segment, nglobs);
688 } else if (sym->section != SHN_ABS) {
690 * This is a global symbol; so we must add it to the linked
691 * list of global symbols in its section. We'll push it on
692 * the beginning of the list, because it doesn't matter
693 * much which end we put it on and it's easier like this.
695 * In addition, we check the special text for symbol
696 * type and size information.
698 sym->next = sects[sym->section - 1]->gsyms;
699 sects[sym->section - 1]->gsyms = sym;
701 if (special) {
702 int n = strcspn(special, " \t");
704 if (!nasm_strnicmp(special, "function", n))
705 sym->type |= STT_FUNC;
706 else if (!nasm_strnicmp(special, "data", n) ||
707 !nasm_strnicmp(special, "object", n))
708 sym->type |= STT_OBJECT;
709 else if (!nasm_strnicmp(special, "notype", n))
710 sym->type |= STT_NOTYPE;
711 else
712 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
713 n, special);
714 special += n;
716 while (nasm_isspace(*special))
717 ++special;
718 if (*special) {
719 n = strcspn(special, " \t");
720 if (!nasm_strnicmp(special, "default", n))
721 sym->other = STV_DEFAULT;
722 else if (!nasm_strnicmp(special, "internal", n))
723 sym->other = STV_INTERNAL;
724 else if (!nasm_strnicmp(special, "hidden", n))
725 sym->other = STV_HIDDEN;
726 else if (!nasm_strnicmp(special, "protected", n))
727 sym->other = STV_PROTECTED;
728 else
729 n = 0;
730 special += n;
733 if (*special) {
734 struct tokenval tokval;
735 expr *e;
736 int fwd = 0;
737 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
739 while (special[n] && nasm_isspace(special[n]))
740 n++;
742 * We have a size expression; attempt to
743 * evaluate it.
745 stdscan_reset();
746 stdscan_bufptr = special + n;
747 tokval.t_type = TOKEN_INVALID;
748 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
749 NULL);
750 if (fwd) {
751 sym->nextfwd = fwds;
752 fwds = sym;
753 sym->name = nasm_strdup(name);
754 } else if (e) {
755 if (!is_simple(e))
756 error(ERR_NONFATAL, "cannot use relocatable"
757 " expression as symbol size");
758 else
759 sym->size = reloc_value(e);
761 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
763 special_used = true;
766 sym->globnum = nglobs;
767 nglobs++;
768 } else
769 nlocals++;
771 if (special && !special_used)
772 error(ERR_NONFATAL, "no special symbol features supported here");
775 static void elf_add_reloc(struct Section *sect, int32_t segment,
776 int64_t offset, int type)
778 struct Reloc *r;
779 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
780 sect->tail = &r->next;
781 r->next = NULL;
783 r->address = sect->len;
784 r->offset = offset;
785 if (segment == NO_SEG)
786 r->symbol = 0;
787 else {
788 int i;
789 r->symbol = 0;
790 for (i = 0; i < nsects; i++)
791 if (segment == sects[i]->index)
792 r->symbol = i + 2;
793 if (!r->symbol)
794 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
796 r->type = type;
798 sect->nrelocs++;
802 * This routine deals with ..got and ..sym relocations: the more
803 * complicated kinds. In shared-library writing, some relocations
804 * with respect to global symbols must refer to the precise symbol
805 * rather than referring to an offset from the base of the section
806 * _containing_ the symbol. Such relocations call to this routine,
807 * which searches the symbol list for the symbol in question.
809 * R_386_GOT32 references require the _exact_ symbol address to be
810 * used; R_386_32 references can be at an offset from the symbol.
811 * The boolean argument `exact' tells us this.
813 * Return value is the adjusted value of `addr', having become an
814 * offset from the symbol rather than the section. Should always be
815 * zero when returning from an exact call.
817 * Limitation: if you define two symbols at the same place,
818 * confusion will occur.
820 * Inefficiency: we search, currently, using a linked list which
821 * isn't even necessarily sorted.
823 static void elf_add_gsym_reloc(struct Section *sect,
824 int32_t segment, int64_t offset, int64_t pcrel,
825 int type, bool exact)
827 struct Reloc *r;
828 struct Section *s;
829 struct Symbol *sym, *sm;
830 int i;
833 * First look up the segment/offset pair and find a global
834 * symbol corresponding to it. If it's not one of our segments,
835 * then it must be an external symbol, in which case we're fine
836 * doing a normal elf_add_reloc after first sanity-checking
837 * that the offset from the symbol is zero.
839 s = NULL;
840 for (i = 0; i < nsects; i++)
841 if (segment == sects[i]->index) {
842 s = sects[i];
843 break;
846 if (!s) {
847 if (exact && offset)
848 error(ERR_NONFATAL, "invalid access to an external symbol");
849 else
850 elf_add_reloc(sect, segment, offset - pcrel, type);
851 return;
854 if (exact) {
856 * Find a symbol pointing _exactly_ at this one.
858 for (sym = s->gsyms; sym; sym = sym->next)
859 if (sym->value == offset)
860 break;
861 if (!sym) {
862 error(ERR_NONFATAL, "unable to find a suitable global symbol"
863 " for this reference");
864 return;
866 } else {
868 * Find the nearest symbol below this one.
870 sym = NULL;
871 for (sm = s->gsyms; sm; sm = sm->next)
872 if (sm->value <= offset && (!sym || sm->value > sym->value))
873 sym = sm;
876 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
877 sect->tail = &r->next;
878 r->next = NULL;
880 r->address = sect->len;
881 r->offset = offset - pcrel - sym->value;
882 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
883 r->type = type;
885 sect->nrelocs++;
888 static void elf_out(int32_t segto, const void *data,
889 enum out_type type, uint64_t size,
890 int32_t segment, int32_t wrt)
892 struct Section *s;
893 int64_t addr, zero;
894 int i;
895 static struct symlininfo sinfo;
897 zero = 0;
899 #if defined(DEBUG) && DEBUG>2
900 if (data) fprintf(stderr,
901 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64"\n",
902 currentline, type, segment, segto, size, *(int64_t *)data);
903 else fprintf(stderr,
904 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
905 currentline, type, segment, segto, size);
906 #endif
909 * handle absolute-assembly (structure definitions)
911 if (segto == NO_SEG) {
912 if (type != OUT_RESERVE)
913 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
914 " space");
915 return;
918 s = NULL;
919 for (i = 0; i < nsects; i++)
920 if (segto == sects[i]->index) {
921 s = sects[i];
922 break;
924 if (!s) {
925 int tempint; /* ignored */
926 if (segto != elf_section_names(".text", 2, &tempint))
927 error(ERR_PANIC, "strange segment conditions in ELF driver");
928 else {
929 s = sects[nsects - 1];
930 i = nsects - 1;
933 /* invoke current debug_output routine */
934 if (of_elf64.current_dfmt) {
935 sinfo.offset = s->len;
936 sinfo.section = i;
937 sinfo.segto = segto;
938 sinfo.name = s->name;
939 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
941 /* end of debugging stuff */
943 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
944 error(ERR_WARNING, "attempt to initialize memory in"
945 " BSS section `%s': ignored", s->name);
946 switch (type) {
947 case OUT_REL2ADR:
948 size = 2;
949 break;
950 case OUT_REL4ADR:
951 size = 4;
952 break;
953 case OUT_REL8ADR:
954 size = 8;
955 break;
956 default:
957 break; /* size is already set */
959 s->len += size;
960 return;
963 if (type == OUT_RESERVE) {
964 if (s->type == SHT_PROGBITS) {
965 error(ERR_WARNING, "uninitialized space declared in"
966 " non-BSS section `%s': zeroing", s->name);
967 elf_sect_write(s, NULL, size);
968 } else
969 s->len += size;
970 } else if (type == OUT_RAWDATA) {
971 if (segment != NO_SEG)
972 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
973 elf_sect_write(s, data, size);
974 } else if (type == OUT_ADDRESS) {
975 addr = *(int64_t *)data;
976 if (segment == NO_SEG) {
977 /* Do nothing */
978 } else if (segment % 2) {
979 error(ERR_NONFATAL, "ELF format does not support"
980 " segment base references");
981 } else {
982 if (wrt == NO_SEG) {
983 switch ((int)size) {
984 case 1:
985 elf_add_reloc(s, segment, addr, R_X86_64_8);
986 break;
987 case 2:
988 elf_add_reloc(s, segment, addr, R_X86_64_16);
989 break;
990 case 4:
991 elf_add_reloc(s, segment, addr, R_X86_64_32);
992 break;
993 case 8:
994 elf_add_reloc(s, segment, addr, R_X86_64_64);
995 break;
996 default:
997 error(ERR_PANIC, "internal error elf64-hpa-871");
998 break;
1000 addr = 0;
1001 } else if (wrt == elf_gotpc_sect + 1) {
1003 * The user will supply GOT relative to $$. ELF
1004 * will let us have GOT relative to $. So we
1005 * need to fix up the data item by $-$$.
1007 addr += s->len;
1008 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1009 addr = 0;
1010 } else if (wrt == elf_gotoff_sect + 1) {
1011 if (size != 8) {
1012 error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1013 "references to be qword absolute");
1014 } else {
1015 elf_add_gsym_reloc(s, segment, addr, 0,
1016 R_X86_64_GOTOFF64, false);
1017 addr = 0;
1019 } else if (wrt == elf_got_sect + 1) {
1020 switch ((int)size) {
1021 case 4:
1022 elf_add_gsym_reloc(s, segment, addr, 0,
1023 R_X86_64_GOT32, true);
1024 addr = 0;
1025 break;
1026 case 8:
1027 elf_add_gsym_reloc(s, segment, addr, 0,
1028 R_X86_64_GOT64, true);
1029 addr = 0;
1030 break;
1031 default:
1032 error(ERR_NONFATAL, "invalid ..got reference");
1033 break;
1035 } else if (wrt == elf_sym_sect + 1) {
1036 switch ((int)size) {
1037 case 1:
1038 elf_add_gsym_reloc(s, segment, addr, 0,
1039 R_X86_64_8, false);
1040 addr = 0;
1041 break;
1042 case 2:
1043 elf_add_gsym_reloc(s, segment, addr, 0,
1044 R_X86_64_16, false);
1045 addr = 0;
1046 break;
1047 case 4:
1048 elf_add_gsym_reloc(s, segment, addr, 0,
1049 R_X86_64_32, false);
1050 addr = 0;
1051 break;
1052 case 8:
1053 elf_add_gsym_reloc(s, segment, addr, 0,
1054 R_X86_64_64, false);
1055 addr = 0;
1056 break;
1057 default:
1058 error(ERR_PANIC, "internal error elf64-hpa-903");
1059 break;
1061 } else if (wrt == elf_plt_sect + 1) {
1062 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1063 "relative PLT references");
1064 } else {
1065 error(ERR_NONFATAL, "ELF format does not support this"
1066 " use of WRT");
1069 elf_sect_writeaddr(s, addr, size);
1070 } else if (type == OUT_REL2ADR) {
1071 addr = *(int64_t *)data - size;
1072 if (segment == segto)
1073 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
1074 if (segment == NO_SEG) {
1075 /* Do nothing */
1076 } else if (segment % 2) {
1077 error(ERR_NONFATAL, "ELF format does not support"
1078 " segment base references");
1079 } else {
1080 if (wrt == NO_SEG) {
1081 elf_add_reloc(s, segment, addr, R_X86_64_PC16);
1082 addr = 0;
1083 } else {
1084 error(ERR_NONFATAL,
1085 "Unsupported non-32-bit ELF relocation [2]");
1088 elf_sect_writeaddr(s, addr, size);
1089 } else if (type == OUT_REL4ADR) {
1090 addr = *(int64_t *)data - size;
1091 if (segment == segto)
1092 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
1093 if (segment == NO_SEG) {
1094 /* Do nothing */
1095 } else if (segment % 2) {
1096 error(ERR_NONFATAL, "ELF64 format does not support"
1097 " segment base references");
1098 } else {
1099 if (wrt == NO_SEG) {
1100 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1101 addr = 0;
1102 } else if (wrt == elf_plt_sect + 1) {
1103 int64_t pcrel = s->len + size;
1104 elf_add_gsym_reloc(s, segment, addr+pcrel, pcrel,
1105 R_X86_64_PLT32, false);
1106 addr = 0;
1107 } else if (wrt == elf_gotpc_sect + 1 ||
1108 wrt == elf_got_sect + 1) {
1109 int64_t pcrel = s->len + size;
1110 elf_add_gsym_reloc(s, segment, addr+pcrel, pcrel,
1111 R_X86_64_GOTPCREL, false);
1112 addr = 0;
1113 } else if (wrt == elf_gotoff_sect + 1 ||
1114 wrt == elf_got_sect + 1) {
1115 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1116 "qword absolute");
1117 } else {
1118 error(ERR_NONFATAL, "ELF64 format does not support this"
1119 " use of WRT");
1122 elf_sect_writeaddr(s, addr, size);
1123 } else if (type == OUT_REL8ADR) {
1124 addr = *(int64_t *)data - size;
1125 if (segment == segto)
1126 error(ERR_PANIC, "intra-segment OUT_REL8ADR");
1127 if (segment == NO_SEG) {
1128 /* Do nothing */
1129 } else if (segment % 2) {
1130 error(ERR_NONFATAL, "ELF64 format does not support"
1131 " segment base references");
1132 } else {
1133 if (wrt == NO_SEG) {
1134 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1135 addr = 0;
1136 } else if (wrt == elf_gotpc_sect + 1 ||
1137 wrt == elf_got_sect + 1) {
1138 int64_t pcrel = s->len + size;
1139 elf_add_gsym_reloc(s, segment, addr+pcrel, pcrel,
1140 R_X86_64_GOTPCREL64, false);
1141 addr = 0;
1142 } else if (wrt == elf_gotoff_sect + 1 ||
1143 wrt == elf_got_sect + 1) {
1144 error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1145 "qword absolute");
1146 } else {
1147 error(ERR_NONFATAL, "ELF64 format does not support this"
1148 " use of WRT");
1151 elf_sect_writeaddr(s, addr, size);
1155 static void elf_write(void)
1157 int align;
1158 int scount;
1159 char *p;
1160 int commlen;
1161 char comment[64];
1162 int i;
1164 struct SAA *symtab;
1165 int32_t symtablen, symtablocal;
1168 * Work out how many sections we will have. We have SHN_UNDEF,
1169 * then the flexible user sections, then the four fixed
1170 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1171 * then optionally relocation sections for the user sections.
1173 if (of_elf64.current_dfmt == &df_stabs)
1174 nsections = 8;
1175 else if (of_elf64.current_dfmt == &df_dwarf)
1176 nsections = 15;
1177 else
1178 nsections = 5; /* SHN_UNDEF and the fixed ones */
1180 add_sectname("", ".comment");
1181 add_sectname("", ".shstrtab");
1182 add_sectname("", ".symtab");
1183 add_sectname("", ".strtab");
1184 for (i = 0; i < nsects; i++) {
1185 nsections++; /* for the section itself */
1186 if (sects[i]->head) {
1187 nsections++; /* for its relocations */
1188 add_sectname(".rela", sects[i]->name);
1192 if (of_elf64.current_dfmt == &df_stabs) {
1193 /* in case the debug information is wanted, just add these three sections... */
1194 add_sectname("", ".stab");
1195 add_sectname("", ".stabstr");
1196 add_sectname(".rel", ".stab");
1199 else if (of_elf64.current_dfmt == &df_dwarf) {
1200 /* the dwarf debug standard specifies the following ten sections,
1201 not all of which are currently implemented,
1202 although all of them are defined. */
1203 #define debug_aranges (int64_t) (nsections-10)
1204 #define debug_info (int64_t) (nsections-7)
1205 #define debug_abbrev (int64_t) (nsections-5)
1206 #define debug_line (int64_t) (nsections-4)
1207 add_sectname("", ".debug_aranges");
1208 add_sectname(".rela", ".debug_aranges");
1209 add_sectname("", ".debug_pubnames");
1210 add_sectname("", ".debug_info");
1211 add_sectname(".rela", ".debug_info");
1212 add_sectname("", ".debug_abbrev");
1213 add_sectname("", ".debug_line");
1214 add_sectname(".rela", ".debug_line");
1215 add_sectname("", ".debug_frame");
1216 add_sectname("", ".debug_loc");
1220 * Do the comment.
1222 *comment = '\0';
1223 commlen =
1224 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1227 * Output the ELF header.
1229 fwrite("\177ELF\2\1\1", 7, 1, elffp);
1230 fputc(elf_osabi, elffp);
1231 fputc(elf_abiver, elffp);
1232 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1233 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1234 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1235 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1236 fwriteint64_t(0L, elffp); /* no entry point */
1237 fwriteint64_t(0L, elffp); /* no program header table */
1238 fwriteint64_t(0x40L, elffp); /* section headers straight after
1239 * ELF header plus alignment */
1240 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1241 fwriteint16_t(0x40, elffp); /* size of ELF header */
1242 fwriteint16_t(0, elffp); /* no program header table, again */
1243 fwriteint16_t(0, elffp); /* still no program header table */
1244 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1245 fwriteint16_t(nsections, elffp); /* number of sections */
1246 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1247 * section header table */
1250 * Build the symbol table and relocation tables.
1252 symtab = elf_build_symtab(&symtablen, &symtablocal);
1253 for (i = 0; i < nsects; i++)
1254 if (sects[i]->head)
1255 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1256 sects[i]->head);
1259 * Now output the section header table.
1262 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1263 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1264 elf_foffs += align;
1265 elf_nsect = 0;
1266 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1267 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1268 scount = 1; /* needed for the stabs debugging to track the symtable section */
1269 p = shstrtab + 1;
1270 for (i = 0; i < nsects; i++) {
1271 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1272 (sects[i]->type == SHT_PROGBITS ?
1273 sects[i]->data : NULL), true,
1274 sects[i]->len, 0, 0, sects[i]->align, 0);
1275 p += strlen(p) + 1;
1276 scount++; /* ditto */
1278 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1279 scount++; /* ditto */
1280 p += strlen(p) + 1;
1281 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1282 scount++; /* ditto */
1283 p += strlen(p) + 1;
1284 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1285 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1286 p += strlen(p) + 1;
1287 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1288 for (i = 0; i < nsects; i++)
1289 if (sects[i]->head) {
1290 p += strlen(p) + 1;
1291 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1292 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1294 if (of_elf64.current_dfmt == &df_stabs) {
1295 /* for debugging information, create the last three sections
1296 which are the .stab , .stabstr and .rel.stab sections respectively */
1298 /* this function call creates the stab sections in memory */
1299 stabs64_generate();
1301 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1302 p += strlen(p) + 1;
1303 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1304 nsections - 2, 0, 4, 12);
1306 p += strlen(p) + 1;
1307 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1308 stabstrlen, 0, 0, 4, 0);
1310 p += strlen(p) + 1;
1311 /* link -> symtable info -> section to refer to */
1312 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1313 stabrellen, symtabsection, nsections - 3, 4,
1314 16);
1317 else if (of_elf64.current_dfmt == &df_dwarf) {
1318 /* for dwarf debugging information, create the ten dwarf sections */
1320 /* this function call creates the dwarf sections in memory */
1321 if (dwarf_fsect) dwarf64_generate();
1323 p += strlen(p) + 1;
1324 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1325 arangeslen, 0, 0, 1, 0);
1326 p += strlen(p) + 1;
1327 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1328 arangesrellen, symtabsection, debug_aranges, 1, 24);
1329 p += strlen(p) + 1;
1330 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1331 pubnameslen, 0, 0, 1, 0);
1332 p += strlen(p) + 1;
1333 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1334 infolen, 0, 0, 1, 0);
1335 p += strlen(p) + 1;
1336 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1337 inforellen, symtabsection, debug_info, 1, 24);
1338 p += strlen(p) + 1;
1339 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1340 abbrevlen, 0, 0, 1, 0);
1341 p += strlen(p) + 1;
1342 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1343 linelen, 0, 0, 1, 0);
1344 p += strlen(p) + 1;
1345 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1346 linerellen, symtabsection, debug_line, 1, 24);
1347 p += strlen(p) + 1;
1348 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1349 framelen, 0, 0, 8, 0);
1350 p += strlen(p) + 1;
1351 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1352 loclen, 0, 0, 1, 0);
1355 fwrite(align_str, align, 1, elffp);
1358 * Now output the sections.
1360 elf_write_sections();
1362 nasm_free(elf_sects);
1363 saa_free(symtab);
1366 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1368 struct SAA *s = saa_init(1L);
1369 struct Symbol *sym;
1370 uint8_t entry[24], *p;
1371 int i;
1373 *len = *local = 0;
1376 * First, an all-zeros entry, required by the ELF spec.
1378 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1379 *len += 24;
1380 (*local)++;
1383 * Next, an entry for the file name.
1385 p = entry;
1386 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1387 WRITESHORT(p, STT_FILE); /* type FILE */
1388 WRITESHORT(p, SHN_ABS);
1389 WRITEDLONG(p, (uint64_t) 0); /* no value */
1390 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1391 saa_wbytes(s, entry, 24L);
1392 *len += 24;
1393 (*local)++;
1396 * Now some standard symbols defining the segments, for relocation
1397 * purposes.
1399 for (i = 1; i <= nsects; i++) {
1400 p = entry;
1401 WRITELONG(p, 0); /* no symbol name */
1402 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1403 WRITESHORT(p, i); /* section id */
1404 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1405 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1406 saa_wbytes(s, entry, 24L);
1407 *len += 24;
1408 (*local)++;
1413 * Now the other local symbols.
1415 saa_rewind(syms);
1416 while ((sym = saa_rstruct(syms))) {
1417 if (sym->type & SYM_GLOBAL)
1418 continue;
1419 p = entry;
1420 WRITELONG(p, sym->strpos); /* index into symbol string table */
1421 WRITECHAR(p, sym->type); /* type and binding */
1422 WRITECHAR(p, sym->other); /* visibility */
1423 WRITESHORT(p, sym->section); /* index into section header table */
1424 WRITEDLONG(p, (int64_t)sym->value); /* value of symbol */
1425 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1426 saa_wbytes(s, entry, 24L);
1427 *len += 24;
1428 (*local)++;
1431 * dwarf needs symbols for debug sections
1432 * which are relocation targets.
1434 if (of_elf64.current_dfmt == &df_dwarf) {
1435 dwarf_infosym = *local;
1436 p = entry;
1437 WRITELONG(p, 0); /* no symbol name */
1438 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1439 WRITESHORT(p, debug_info); /* section id */
1440 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1441 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1442 saa_wbytes(s, entry, 24L);
1443 *len += 24;
1444 (*local)++;
1445 dwarf_abbrevsym = *local;
1446 p = entry;
1447 WRITELONG(p, 0); /* no symbol name */
1448 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1449 WRITESHORT(p, debug_abbrev); /* section id */
1450 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1451 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1452 saa_wbytes(s, entry, 24L);
1453 *len += 24;
1454 (*local)++;
1455 dwarf_linesym = *local;
1456 p = entry;
1457 WRITELONG(p, 0); /* no symbol name */
1458 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1459 WRITESHORT(p, debug_line); /* section id */
1460 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1461 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1462 saa_wbytes(s, entry, 24L);
1463 *len += 24;
1464 (*local)++;
1468 * Now the global symbols.
1470 saa_rewind(syms);
1471 while ((sym = saa_rstruct(syms))) {
1472 if (!(sym->type & SYM_GLOBAL))
1473 continue;
1474 p = entry;
1475 WRITELONG(p, sym->strpos);
1476 WRITECHAR(p, sym->type); /* type and binding */
1477 WRITECHAR(p, sym->other); /* visibility */
1478 WRITESHORT(p, sym->section);
1479 WRITEDLONG(p, (int64_t)sym->value);
1480 WRITEDLONG(p, (int64_t)sym->size);
1481 saa_wbytes(s, entry, 24L);
1482 *len += 24;
1485 return s;
1488 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1490 struct SAA *s;
1491 uint8_t *p, entry[24];
1493 if (!r)
1494 return NULL;
1496 s = saa_init(1L);
1497 *len = 0;
1499 while (r) {
1500 int64_t sym = r->symbol;
1502 if (sym >= GLOBAL_TEMP_BASE)
1504 if (of_elf64.current_dfmt == &df_dwarf)
1505 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1506 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1508 p = entry;
1509 WRITEDLONG(p, r->address);
1510 WRITEDLONG(p, (sym << 32) + r->type);
1511 WRITEDLONG(p, r->offset);
1512 saa_wbytes(s, entry, 24L);
1513 *len += 24;
1515 r = r->next;
1518 return s;
1521 static void elf_section_header(int name, int type, uint64_t flags,
1522 void *data, bool is_saa, uint64_t datalen,
1523 int link, int info, int align, int eltsize)
1525 elf_sects[elf_nsect].data = data;
1526 elf_sects[elf_nsect].len = datalen;
1527 elf_sects[elf_nsect].is_saa = is_saa;
1528 elf_nsect++;
1530 fwriteint32_t((int32_t)name, elffp);
1531 fwriteint32_t((int32_t)type, elffp);
1532 fwriteint64_t((int64_t)flags, elffp);
1533 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1534 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1535 fwriteint64_t(datalen, elffp);
1536 if (data)
1537 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1538 fwriteint32_t((int32_t)link, elffp);
1539 fwriteint32_t((int32_t)info, elffp);
1540 fwriteint64_t((int64_t)align, elffp);
1541 fwriteint64_t((int64_t)eltsize, elffp);
1544 static void elf_write_sections(void)
1546 int i;
1547 for (i = 0; i < elf_nsect; i++)
1548 if (elf_sects[i].data) {
1549 int32_t len = elf_sects[i].len;
1550 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1551 int32_t align = reallen - len;
1552 if (elf_sects[i].is_saa)
1553 saa_fpwrite(elf_sects[i].data, elffp);
1554 else
1555 fwrite(elf_sects[i].data, len, 1, elffp);
1556 fwrite(align_str, align, 1, elffp);
1560 static void elf_sect_write(struct Section *sect, const void *data, size_t len)
1562 saa_wbytes(sect->data, data, len);
1563 sect->len += len;
1565 static void elf_sect_writeaddr(struct Section *sect, int64_t data, size_t len)
1567 saa_writeaddr(sect->data, data, len);
1568 sect->len += len;
1571 static int32_t elf_segbase(int32_t segment)
1573 return segment;
1576 static int elf_directive(char *directive, char *value, int pass)
1578 bool err;
1579 int64_t n;
1580 char *p;
1582 if (!strcmp(directive, "osabi")) {
1583 if (pass == 2)
1584 return 1; /* ignore in pass 2 */
1586 n = readnum(value, &err);
1587 if (err) {
1588 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1589 return 1;
1591 if (n < 0 || n > 255) {
1592 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1593 return 1;
1595 elf_osabi = n;
1596 elf_abiver = 0;
1598 if ((p = strchr(value,',')) == NULL)
1599 return 1;
1601 n = readnum(p+1, &err);
1602 if (err || n < 0 || n > 255) {
1603 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1604 return 1;
1607 elf_abiver = n;
1608 return 1;
1611 return 0;
1614 static void elf_filename(char *inname, char *outname, efunc error)
1616 strcpy(elf_module, inname);
1617 standard_extension(inname, outname, ".o", error);
1620 extern macros_t elf_stdmac[];
1622 static int elf_set_info(enum geninfo type, char **val)
1624 (void)type;
1625 (void)val;
1626 return 0;
1628 static struct dfmt df_dwarf = {
1629 "ELF64 (X86_64) dwarf debug format for Linux",
1630 "dwarf",
1631 debug64_init,
1632 dwarf64_linenum,
1633 debug64_deflabel,
1634 debug64_directive,
1635 debug64_typevalue,
1636 dwarf64_output,
1637 dwarf64_cleanup
1639 static struct dfmt df_stabs = {
1640 "ELF64 (X86_64) stabs debug format for Linux",
1641 "stabs",
1642 debug64_init,
1643 stabs64_linenum,
1644 debug64_deflabel,
1645 debug64_directive,
1646 debug64_typevalue,
1647 stabs64_output,
1648 stabs64_cleanup
1651 struct dfmt *elf64_debugs_arr[3] = { &df_stabs, &df_dwarf, NULL };
1653 struct ofmt of_elf64 = {
1654 "ELF64 (x86_64) object files (e.g. Linux)",
1655 "elf64",
1656 NULL,
1657 elf64_debugs_arr,
1658 &null_debug_form,
1659 elf_stdmac,
1660 elf_init,
1661 elf_set_info,
1662 elf_out,
1663 elf_deflabel,
1664 elf_section_names,
1665 elf_segbase,
1666 elf_directive,
1667 elf_filename,
1668 elf_cleanup
1671 /* common debugging routines */
1672 void debug64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1674 (void)of;
1675 (void)id;
1676 (void)fp;
1677 (void)error;
1679 void debug64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1680 char *special)
1682 (void)name;
1683 (void)segment;
1684 (void)offset;
1685 (void)is_global;
1686 (void)special;
1689 void debug64_directive(const char *directive, const char *params)
1691 (void)directive;
1692 (void)params;
1695 void debug64_typevalue(int32_t type)
1697 int32_t stype, ssize;
1698 switch (TYM_TYPE(type)) {
1699 case TY_LABEL:
1700 ssize = 0;
1701 stype = STT_NOTYPE;
1702 break;
1703 case TY_BYTE:
1704 ssize = 1;
1705 stype = STT_OBJECT;
1706 break;
1707 case TY_WORD:
1708 ssize = 2;
1709 stype = STT_OBJECT;
1710 break;
1711 case TY_DWORD:
1712 ssize = 4;
1713 stype = STT_OBJECT;
1714 break;
1715 case TY_FLOAT:
1716 ssize = 4;
1717 stype = STT_OBJECT;
1718 break;
1719 case TY_QWORD:
1720 ssize = 8;
1721 stype = STT_OBJECT;
1722 break;
1723 case TY_TBYTE:
1724 ssize = 10;
1725 stype = STT_OBJECT;
1726 break;
1727 case TY_OWORD:
1728 ssize = 16;
1729 stype = STT_OBJECT;
1730 break;
1731 case TY_COMMON:
1732 ssize = 0;
1733 stype = STT_COMMON;
1734 break;
1735 case TY_SEG:
1736 ssize = 0;
1737 stype = STT_SECTION;
1738 break;
1739 case TY_EXTERN:
1740 ssize = 0;
1741 stype = STT_NOTYPE;
1742 break;
1743 case TY_EQU:
1744 ssize = 0;
1745 stype = STT_NOTYPE;
1746 break;
1747 default:
1748 ssize = 0;
1749 stype = STT_NOTYPE;
1750 break;
1752 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1753 lastsym->size = ssize;
1754 lastsym->type = stype;
1758 /* stabs debugging routines */
1761 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1763 (void)segto;
1764 if (!stabs_filename) {
1765 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1766 strcpy(stabs_filename, filename);
1767 } else {
1768 if (strcmp(stabs_filename, filename)) {
1769 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1770 in fact, this leak comes in quite handy to maintain a list of files
1771 encountered so far in the symbol lines... */
1773 /* why not nasm_free(stabs_filename); we're done with the old one */
1775 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1776 strcpy(stabs_filename, filename);
1779 debug_immcall = 1;
1780 currentline = linenumber;
1784 void stabs64_output(int type, void *param)
1786 struct symlininfo *s;
1787 struct linelist *el;
1788 if (type == TY_DEBUGSYMLIN) {
1789 if (debug_immcall) {
1790 s = (struct symlininfo *)param;
1791 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1792 return; /* line info is only collected for executable sections */
1793 numlinestabs++;
1794 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1795 el->info.offset = s->offset;
1796 el->info.section = s->section;
1797 el->info.name = s->name;
1798 el->line = currentline;
1799 el->filename = stabs_filename;
1800 el->next = 0;
1801 if (stabslines) {
1802 stabslines->last->next = el;
1803 stabslines->last = el;
1804 } else {
1805 stabslines = el;
1806 stabslines->last = el;
1810 debug_immcall = 0;
1813 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1814 do {\
1815 WRITELONG(p,n_strx); \
1816 WRITECHAR(p,n_type); \
1817 WRITECHAR(p,n_other); \
1818 WRITESHORT(p,n_desc); \
1819 WRITELONG(p,n_value); \
1820 } while (0)
1822 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1824 void stabs64_generate(void)
1826 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1827 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1828 char **allfiles;
1829 int *fileidx;
1831 struct linelist *ptr;
1833 ptr = stabslines;
1835 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1836 for (i = 0; i < numlinestabs; i++)
1837 allfiles[i] = 0;
1838 numfiles = 0;
1839 while (ptr) {
1840 if (numfiles == 0) {
1841 allfiles[0] = ptr->filename;
1842 numfiles++;
1843 } else {
1844 for (i = 0; i < numfiles; i++) {
1845 if (!strcmp(allfiles[i], ptr->filename))
1846 break;
1848 if (i >= numfiles) {
1849 allfiles[i] = ptr->filename;
1850 numfiles++;
1853 ptr = ptr->next;
1855 strsize = 1;
1856 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1857 for (i = 0; i < numfiles; i++) {
1858 fileidx[i] = strsize;
1859 strsize += strlen(allfiles[i]) + 1;
1861 mainfileindex = 0;
1862 for (i = 0; i < numfiles; i++) {
1863 if (!strcmp(allfiles[i], elf_module)) {
1864 mainfileindex = i;
1865 break;
1869 /* worst case size of the stab buffer would be:
1870 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1872 sbuf =
1873 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1874 sizeof(struct stabentry));
1876 ssbuf = (uint8_t *)nasm_malloc(strsize);
1878 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1879 rptr = rbuf;
1881 for (i = 0; i < numfiles; i++) {
1882 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1884 ssbuf[0] = 0;
1886 stabstrlen = strsize; /* set global variable for length of stab strings */
1888 sptr = sbuf;
1889 ptr = stabslines;
1890 numstabs = 0;
1892 if (ptr) {
1893 /* this is the first stab, its strx points to the filename of the
1894 the source-file, the n_desc field should be set to the number
1895 of remaining stabs
1897 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1899 /* this is the stab for the main source file */
1900 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1902 /* relocation table entry */
1904 /* Since the symbol table has two entries before */
1905 /* the section symbols, the index in the info.section */
1906 /* member must be adjusted by adding 2 */
1908 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1909 WRITELONG(rptr, R_X86_64_32);
1910 WRITELONG(rptr, ptr->info.section + 2);
1912 numstabs++;
1913 currfile = mainfileindex;
1916 while (ptr) {
1917 if (strcmp(allfiles[currfile], ptr->filename)) {
1918 /* oops file has changed... */
1919 for (i = 0; i < numfiles; i++)
1920 if (!strcmp(allfiles[i], ptr->filename))
1921 break;
1922 currfile = i;
1923 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1924 ptr->info.offset);
1925 numstabs++;
1927 /* relocation table entry */
1929 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1930 WRITELONG(rptr, R_X86_64_32);
1931 WRITELONG(rptr, ptr->info.section + 2);
1934 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1935 numstabs++;
1937 /* relocation table entry */
1939 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1940 WRITELONG(rptr, R_X86_64_32);
1941 WRITELONG(rptr, ptr->info.section + 2);
1943 ptr = ptr->next;
1947 ((struct stabentry *)sbuf)->n_desc = numstabs;
1949 nasm_free(allfiles);
1950 nasm_free(fileidx);
1952 stablen = (sptr - sbuf);
1953 stabrellen = (rptr - rbuf);
1954 stabrelbuf = rbuf;
1955 stabbuf = sbuf;
1956 stabstrbuf = ssbuf;
1959 void stabs64_cleanup(void)
1961 struct linelist *ptr, *del;
1962 if (!stabslines)
1963 return;
1964 ptr = stabslines;
1965 while (ptr) {
1966 del = ptr;
1967 ptr = ptr->next;
1968 nasm_free(del);
1970 if (stabbuf)
1971 nasm_free(stabbuf);
1972 if (stabrelbuf)
1973 nasm_free(stabrelbuf);
1974 if (stabstrbuf)
1975 nasm_free(stabstrbuf);
1977 /* dwarf routines */
1980 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1982 (void)segto;
1983 dwarf64_findfile(filename);
1984 debug_immcall = 1;
1985 currentline = linenumber;
1988 /* called from elf_out with type == TY_DEBUGSYMLIN */
1989 void dwarf64_output(int type, void *param)
1991 int ln, aa, inx, maxln, soc;
1992 struct symlininfo *s;
1993 struct SAA *plinep;
1995 (void)type;
1997 s = (struct symlininfo *)param;
1998 /* line number info is only gathered for executable sections */
1999 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2000 return;
2001 /* Check if section index has changed */
2002 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
2004 dwarf64_findsect(s->section);
2006 /* do nothing unless line or file has changed */
2007 if (debug_immcall)
2009 ln = currentline - dwarf_csect->line;
2010 aa = s->offset - dwarf_csect->offset;
2011 inx = dwarf_clist->line;
2012 plinep = dwarf_csect->psaa;
2013 /* check for file change */
2014 if (!(inx == dwarf_csect->file))
2016 saa_write8(plinep,DW_LNS_set_file);
2017 saa_write8(plinep,inx);
2018 dwarf_csect->file = inx;
2020 /* check for line change */
2021 if (ln)
2023 /* test if in range of special op code */
2024 maxln = line_base + line_range;
2025 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2026 if (ln >= line_base && ln < maxln && soc < 256)
2028 saa_write8(plinep,soc);
2030 else
2032 if (ln)
2034 saa_write8(plinep,DW_LNS_advance_line);
2035 saa_wleb128s(plinep,ln);
2037 if (aa)
2039 saa_write8(plinep,DW_LNS_advance_pc);
2040 saa_wleb128u(plinep,aa);
2043 dwarf_csect->line = currentline;
2044 dwarf_csect->offset = s->offset;
2046 /* show change handled */
2047 debug_immcall = 0;
2052 void dwarf64_generate(void)
2054 static const char nasm_signature[] = "NASM " NASM_VER;
2055 uint8_t *pbuf;
2056 int indx;
2057 struct linelist *ftentry;
2058 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2059 struct SAA *parangesrel, *plinesrel, *pinforel;
2060 struct sectlist *psect;
2061 size_t saalen, linepoff, totlen, highaddr;
2063 /* write epilogues for each line program range */
2064 /* and build aranges section */
2065 paranges = saa_init(1L);
2066 parangesrel = saa_init(1L);
2067 saa_write16(paranges,3); /* dwarf version */
2068 saa_write64(parangesrel, paranges->datalen+4);
2069 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2070 saa_write64(parangesrel, 0);
2071 saa_write32(paranges,0); /* offset into info */
2072 saa_write8(paranges,8); /* pointer size */
2073 saa_write8(paranges,0); /* not segmented */
2074 saa_write32(paranges,0); /* padding */
2075 /* iterate though sectlist entries */
2076 psect = dwarf_fsect;
2077 totlen = 0;
2078 highaddr = 0;
2079 for (indx = 0; indx < dwarf_nsections; indx++)
2081 plinep = psect->psaa;
2082 /* Line Number Program Epilogue */
2083 saa_write8(plinep,2); /* std op 2 */
2084 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2085 saa_write8(plinep,DW_LNS_extended_op);
2086 saa_write8(plinep,1); /* operand length */
2087 saa_write8(plinep,DW_LNE_end_sequence);
2088 totlen += plinep->datalen;
2089 /* range table relocation entry */
2090 saa_write64(parangesrel, paranges->datalen + 4);
2091 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2092 saa_write64(parangesrel, (uint64_t) 0);
2093 /* range table entry */
2094 saa_write64(paranges,0x0000); /* range start */
2095 saa_write64(paranges,sects[psect->section]->len); /* range length */
2096 highaddr += sects[psect->section]->len;
2097 /* done with this entry */
2098 psect = psect->next;
2100 saa_write64(paranges,0); /* null address */
2101 saa_write64(paranges,0); /* null length */
2102 saalen = paranges->datalen;
2103 arangeslen = saalen + 4;
2104 arangesbuf = pbuf = nasm_malloc(arangeslen);
2105 WRITELONG(pbuf,saalen); /* initial length */
2106 saa_rnbytes(paranges, pbuf, saalen);
2107 saa_free(paranges);
2109 /* build rela.aranges section */
2110 arangesrellen = saalen = parangesrel->datalen;
2111 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2112 saa_rnbytes(parangesrel, pbuf, saalen);
2113 saa_free(parangesrel);
2115 /* build pubnames section */
2116 ppubnames = saa_init(1L);
2117 saa_write16(ppubnames,3); /* dwarf version */
2118 saa_write32(ppubnames,0); /* offset into info */
2119 saa_write32(ppubnames,0); /* space used in info */
2120 saa_write32(ppubnames,0); /* end of list */
2121 saalen = ppubnames->datalen;
2122 pubnameslen = saalen + 4;
2123 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2124 WRITELONG(pbuf,saalen); /* initial length */
2125 saa_rnbytes(ppubnames, pbuf, saalen);
2126 saa_free(ppubnames);
2128 /* build info section */
2129 pinfo = saa_init(1L);
2130 pinforel = saa_init(1L);
2131 saa_write16(pinfo,3); /* dwarf version */
2132 saa_write64(pinforel, pinfo->datalen + 4);
2133 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
2134 saa_write64(pinforel, 0);
2135 saa_write32(pinfo,0); /* offset into abbrev */
2136 saa_write8(pinfo,8); /* pointer size */
2137 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2138 saa_write64(pinforel, pinfo->datalen + 4);
2139 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2140 saa_write64(pinforel, 0);
2141 saa_write64(pinfo,0); /* DW_AT_low_pc */
2142 saa_write64(pinforel, pinfo->datalen + 4);
2143 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2144 saa_write64(pinforel, 0);
2145 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
2146 saa_write64(pinforel, pinfo->datalen + 4);
2147 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
2148 saa_write64(pinforel, 0);
2149 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2150 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2151 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2152 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2153 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2154 saa_write64(pinforel, pinfo->datalen + 4);
2155 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
2156 saa_write64(pinforel, 0);
2157 saa_write64(pinfo,0); /* DW_AT_low_pc */
2158 saa_write64(pinfo,0); /* DW_AT_frame_base */
2159 saa_write8(pinfo,0); /* end of entries */
2160 saalen = pinfo->datalen;
2161 infolen = saalen + 4;
2162 infobuf = pbuf = nasm_malloc(infolen);
2163 WRITELONG(pbuf,saalen); /* initial length */
2164 saa_rnbytes(pinfo, pbuf, saalen);
2165 saa_free(pinfo);
2167 /* build rela.info section */
2168 inforellen = saalen = pinforel->datalen;
2169 inforelbuf = pbuf = nasm_malloc(inforellen);
2170 saa_rnbytes(pinforel, pbuf, saalen);
2171 saa_free(pinforel);
2173 /* build abbrev section */
2174 pabbrev = saa_init(1L);
2175 saa_write8(pabbrev,1); /* entry number LEB128u */
2176 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2177 saa_write8(pabbrev,1); /* has children */
2178 /* the following attributes and forms are all LEB128u values */
2179 saa_write8(pabbrev,DW_AT_low_pc);
2180 saa_write8(pabbrev,DW_FORM_addr);
2181 saa_write8(pabbrev,DW_AT_high_pc);
2182 saa_write8(pabbrev,DW_FORM_addr);
2183 saa_write8(pabbrev,DW_AT_stmt_list);
2184 saa_write8(pabbrev,DW_FORM_data4);
2185 saa_write8(pabbrev,DW_AT_name);
2186 saa_write8(pabbrev,DW_FORM_string);
2187 saa_write8(pabbrev,DW_AT_producer);
2188 saa_write8(pabbrev,DW_FORM_string);
2189 saa_write8(pabbrev,DW_AT_language);
2190 saa_write8(pabbrev,DW_FORM_data2);
2191 saa_write16(pabbrev,0); /* end of entry */
2192 /* LEB128u usage same as above */
2193 saa_write8(pabbrev,2); /* entry number */
2194 saa_write8(pabbrev,DW_TAG_subprogram);
2195 saa_write8(pabbrev,0); /* no children */
2196 saa_write8(pabbrev,DW_AT_low_pc);
2197 saa_write8(pabbrev,DW_FORM_addr);
2198 saa_write8(pabbrev,DW_AT_frame_base);
2199 saa_write8(pabbrev,DW_FORM_data4);
2200 saa_write16(pabbrev,0); /* end of entry */
2201 abbrevlen = saalen = pabbrev->datalen;
2202 abbrevbuf = pbuf = nasm_malloc(saalen);
2203 saa_rnbytes(pabbrev, pbuf, saalen);
2204 saa_free(pabbrev);
2206 /* build line section */
2207 /* prolog */
2208 plines = saa_init(1L);
2209 saa_write8(plines,1); /* Minimum Instruction Length */
2210 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2211 saa_write8(plines,line_base); /* Line Base */
2212 saa_write8(plines,line_range); /* Line Range */
2213 saa_write8(plines,opcode_base); /* Opcode Base */
2214 /* standard opcode lengths (# of LEB128u operands) */
2215 saa_write8(plines,0); /* Std opcode 1 length */
2216 saa_write8(plines,1); /* Std opcode 2 length */
2217 saa_write8(plines,1); /* Std opcode 3 length */
2218 saa_write8(plines,1); /* Std opcode 4 length */
2219 saa_write8(plines,1); /* Std opcode 5 length */
2220 saa_write8(plines,0); /* Std opcode 6 length */
2221 saa_write8(plines,0); /* Std opcode 7 length */
2222 saa_write8(plines,0); /* Std opcode 8 length */
2223 saa_write8(plines,1); /* Std opcode 9 length */
2224 saa_write8(plines,0); /* Std opcode 10 length */
2225 saa_write8(plines,0); /* Std opcode 11 length */
2226 saa_write8(plines,1); /* Std opcode 12 length */
2227 /* Directory Table */
2228 saa_write8(plines,0); /* End of table */
2229 /* File Name Table */
2230 ftentry = dwarf_flist;
2231 for (indx = 0;indx<dwarf_numfiles;indx++)
2233 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2234 saa_write8(plines,0); /* directory LEB128u */
2235 saa_write8(plines,0); /* time LEB128u */
2236 saa_write8(plines,0); /* size LEB128u */
2237 ftentry = ftentry->next;
2239 saa_write8(plines,0); /* End of table */
2240 linepoff = plines->datalen;
2241 linelen = linepoff + totlen + 10;
2242 linebuf = pbuf = nasm_malloc(linelen);
2243 WRITELONG(pbuf,linelen-4); /* initial length */
2244 WRITESHORT(pbuf,3); /* dwarf version */
2245 WRITELONG(pbuf,linepoff); /* offset to line number program */
2246 /* write line header */
2247 saalen = linepoff;
2248 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2249 pbuf += linepoff;
2250 saa_free(plines);
2251 /* concatonate line program ranges */
2252 linepoff += 13;
2253 plinesrel = saa_init(1L);
2254 psect = dwarf_fsect;
2255 for (indx = 0; indx < dwarf_nsections; indx++)
2257 saa_write64(plinesrel, linepoff);
2258 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2259 saa_write64(plinesrel, (uint64_t) 0);
2260 plinep = psect->psaa;
2261 saalen = plinep->datalen;
2262 saa_rnbytes(plinep, pbuf, saalen);
2263 pbuf += saalen;
2264 linepoff += saalen;
2265 saa_free(plinep);
2266 /* done with this entry */
2267 psect = psect->next;
2271 /* build rela.lines section */
2272 linerellen =saalen = plinesrel->datalen;
2273 linerelbuf = pbuf = nasm_malloc(linerellen);
2274 saa_rnbytes(plinesrel, pbuf, saalen);
2275 saa_free(plinesrel);
2277 /* build frame section */
2278 framelen = 4;
2279 framebuf = pbuf = nasm_malloc(framelen);
2280 WRITELONG(pbuf,framelen-4); /* initial length */
2282 /* build loc section */
2283 loclen = 16;
2284 locbuf = pbuf = nasm_malloc(loclen);
2285 WRITEDLONG(pbuf,0); /* null beginning offset */
2286 WRITEDLONG(pbuf,0); /* null ending offset */
2289 void dwarf64_cleanup(void)
2291 if (arangesbuf)
2292 nasm_free(arangesbuf);
2293 if (arangesrelbuf)
2294 nasm_free(arangesrelbuf);
2295 if (pubnamesbuf)
2296 nasm_free(pubnamesbuf);
2297 if (infobuf)
2298 nasm_free(infobuf);
2299 if (inforelbuf)
2300 nasm_free(inforelbuf);
2301 if (abbrevbuf)
2302 nasm_free(abbrevbuf);
2303 if (linebuf)
2304 nasm_free(linebuf);
2305 if (linerelbuf)
2306 nasm_free(linerelbuf);
2307 if (framebuf)
2308 nasm_free(framebuf);
2309 if (locbuf)
2310 nasm_free(locbuf);
2312 void dwarf64_findfile(const char * fname)
2314 int finx;
2315 struct linelist *match;
2317 /* return if fname is current file name */
2318 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2319 /* search for match */
2320 else
2322 match = 0;
2323 if (dwarf_flist)
2325 match = dwarf_flist;
2326 for (finx = 0; finx < dwarf_numfiles; finx++)
2328 if (!(strcmp(fname, match->filename)))
2330 dwarf_clist = match;
2331 return;
2335 /* add file name to end of list */
2336 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2337 dwarf_numfiles++;
2338 dwarf_clist->line = dwarf_numfiles;
2339 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2340 strcpy(dwarf_clist->filename,fname);
2341 dwarf_clist->next = 0;
2342 /* if first entry */
2343 if (!dwarf_flist)
2345 dwarf_flist = dwarf_elist = dwarf_clist;
2346 dwarf_clist->last = 0;
2348 /* chain to previous entry */
2349 else
2351 dwarf_elist->next = dwarf_clist;
2352 dwarf_elist = dwarf_clist;
2356 /* */
2357 void dwarf64_findsect(const int index)
2359 int sinx;
2360 struct sectlist *match;
2361 struct SAA *plinep;
2362 /* return if index is current section index */
2363 if (dwarf_csect && (dwarf_csect->section == index))
2365 return;
2367 /* search for match */
2368 else
2370 match = 0;
2371 if (dwarf_fsect)
2373 match = dwarf_fsect;
2374 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2376 if ((match->section == index))
2378 dwarf_csect = match;
2379 return;
2381 match = match->next;
2384 /* add entry to end of list */
2385 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2386 dwarf_nsections++;
2387 dwarf_csect->psaa = plinep = saa_init(1L);
2388 dwarf_csect->line = 1;
2389 dwarf_csect->offset = 0;
2390 dwarf_csect->file = 1;
2391 dwarf_csect->section = index;
2392 dwarf_csect->next = 0;
2393 /* set relocatable address at start of line program */
2394 saa_write8(plinep,DW_LNS_extended_op);
2395 saa_write8(plinep,9); /* operand length */
2396 saa_write8(plinep,DW_LNE_set_address);
2397 saa_write64(plinep,0); /* Start Address */
2398 /* if first entry */
2399 if (!dwarf_fsect)
2401 dwarf_fsect = dwarf_esect = dwarf_csect;
2402 dwarf_csect->last = 0;
2404 /* chain to previous entry */
2405 else
2407 dwarf_esect->next = dwarf_csect;
2408 dwarf_esect = dwarf_csect;
2413 #endif /* OF_ELF */