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.
26 /* Definitions in lieu of elf.h */
27 #define SHT_NULL 0 /* Inactive section header */
28 #define SHT_PROGBITS 1 /* Program defined content */
29 #define SHT_RELA 4 /* Relocation entries with addends */
30 #define SHT_NOBITS 8 /* Section requires no space in file */
31 #define SHF_WRITE (1 << 0) /* Writable */
32 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
33 #define SHF_EXECINSTR (1 << 2) /* Executable */
34 #define SHF_TLS (1 << 10) /* Section hold thread-local data. */
35 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
36 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
37 #define R_X86_64_NONE 0 /* No reloc */
38 #define R_X86_64_64 1 /* Direct 64 bit */
39 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
40 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
41 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
42 #define R_X86_64_COPY 5 /* Copy symbol at runtime */
43 #define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
44 #define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
45 #define R_X86_64_RELATIVE 8 /* Adjust by program base */
46 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative
48 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
49 #define R_X86_64_32S 11 /* Direct 32 bit sign extended */
50 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
51 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
52 #define R_X86_64_8 14 /* Direct 8 bit sign extended */
53 #define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
54 #define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */
55 #define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */
56 #define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */
57 #define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset
58 to two GOT entries for GD symbol */
59 #define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset
60 to two GOT entries for LD symbol */
61 #define R_X86_64_DTPOFF32 21 /* Offset in TLS block */
62 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset
63 to GOT entry for IE symbol */
64 #define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */
65 #define R_X86_64_PC64 24 /* word64 S + A - P */
66 #define R_X86_64_GOTOFF64 25 /* word64 S + A - GOT */
67 #define R_X86_64_GOTPC32 26 /* word32 GOT + A - P */
68 #define R_X86_64_GOT64 27 /* word64 G + A */
69 #define R_X86_64_GOTPCREL64 28 /* word64 G + GOT - P + A */
70 #define R_X86_64_GOTPC64 29 /* word64 GOT - P + A */
71 #define R_X86_64_GOTPLT64 30 /* word64 G + A */
72 #define R_X86_64_PLTOFF64 31 /* word64 L - GOT + A */
73 #define R_X86_64_SIZE32 32 /* word32 Z + A */
74 #define R_X86_64_SIZE64 33 /* word64 Z + A */
75 #define R_X86_64_GOTPC32_TLSDESC 34 /* word32 */
76 #define R_X86_64_TLSDESC_CALL 35 /* none */
77 #define R_X86_64_TLSDESC 36 /* word64×2 */
78 #define ET_REL 1 /* Relocatable file */
79 #define EM_X86_64 62 /* AMD x86-64 architecture */
80 #define STT_NOTYPE 0 /* Symbol type is unspecified */
81 #define STT_OBJECT 1 /* Symbol is a data object */
82 #define STT_FUNC 2 /* Symbol is a code object */
83 #define STT_SECTION 3 /* Symbol associated with a section */
84 #define STT_FILE 4 /* Symbol's name is file name */
85 #define STT_COMMON 5 /* Symbol is a common data object */
86 #define STT_TLS 6 /* Symbol is thread-local data object*/
87 #define STT_NUM 7 /* Number of defined types. */
89 /* Definitions in lieu of dwarf.h */
90 #define DW_TAG_compile_unit 0x11
91 #define DW_TAG_subprogram 0x2e
92 #define DW_AT_name 0x03
93 #define DW_AT_stmt_list 0x10
94 #define DW_AT_low_pc 0x11
95 #define DW_AT_high_pc 0x12
96 #define DW_AT_language 0x13
97 #define DW_AT_producer 0x25
98 #define DW_AT_frame_base 0x40
99 #define DW_FORM_addr 0x01
100 #define DW_FORM_data2 0x05
101 #define DW_FORM_data4 0x06
102 #define DW_FORM_string 0x08
103 #define DW_LNS_extended_op 0
104 #define DW_LNS_advance_pc 2
105 #define DW_LNS_advance_line 3
106 #define DW_LNS_set_file 4
107 #define DW_LNE_end_sequence 1
108 #define DW_LNE_set_address 2
109 #define DW_LNE_define_file 3
110 #define DW_LANG_Mips_Assembler 0x8001
112 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
114 typedef uint32_t Elf64_Word
;
115 typedef uint64_t Elf64_Xword
;
116 typedef uint64_t Elf64_Addr
;
117 typedef uint64_t Elf64_Off
;
120 Elf64_Word sh_name
; /* Section name (string tbl index) */
121 Elf64_Word sh_type
; /* Section type */
122 Elf64_Xword sh_flags
; /* Section flags */
123 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
124 Elf64_Off sh_offset
; /* Section file offset */
125 Elf64_Xword sh_size
; /* Section size in bytes */
126 Elf64_Word sh_link
; /* Link to another section */
127 Elf64_Word sh_info
; /* Additional section information */
128 Elf64_Xword sh_addralign
; /* Section alignment */
129 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
138 int64_t address
; /* relative to _start_ of section */
139 int64_t symbol
; /* symbol index */
140 int64_t offset
; /* symbol addend */
141 int type
; /* type of relocation */
145 struct rbtree symv
; /* symbol value and rbtree of globals */
146 int32_t strpos
; /* string table position of name */
147 int32_t section
; /* section ID of the symbol */
148 int type
; /* symbol type */
149 int other
; /* symbol visibility */
150 int32_t size
; /* size of symbol */
151 int32_t globnum
; /* symbol table offset if global */
152 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
153 char *name
; /* used temporarily if in above list */
160 int32_t index
; /* index into sects array */
161 uint32_t type
; /* SHT_PROGBITS or SHT_NOBITS */
162 uint64_t align
; /* alignment: power of two */
163 uint64_t flags
; /* section flags */
167 struct Reloc
*head
, **tail
;
168 struct rbtree
*gsyms
; /* global symbols in section */
171 #define SECT_DELTA 32
172 static struct Section
**sects
;
173 static int nsects
, sectlen
;
175 #define SHSTR_DELTA 256
176 static char *shstrtab
;
177 static int shstrtablen
, shstrtabsize
;
179 static struct SAA
*syms
;
180 static uint32_t nlocals
, nglobs
;
182 static int32_t def_seg
;
184 static struct RAA
*bsym
;
186 static struct SAA
*strs
;
187 static uint32_t strslen
;
191 static evalfunc evaluate
;
193 static struct Symbol
*fwds
;
195 static char elf_module
[FILENAME_MAX
];
197 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
198 static uint8_t elf_abiver
= 0; /* Current ABI version */
200 extern struct ofmt of_elf64
;
204 #define SYM_GLOBAL 0x10
206 #define STV_DEFAULT 0
207 #define STV_INTERNAL 1
209 #define STV_PROTECTED 3
211 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
213 #define SEG_ALIGN 16 /* alignment of sections in file */
214 #define SEG_ALIGN_1 (SEG_ALIGN-1)
216 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
218 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
220 static struct ELF_SECTDATA
{
225 static int elf_nsect
, nsections
;
226 static int64_t elf_foffs
;
228 static void elf_write(void);
229 static void elf_sect_write(struct Section
*, const void *, size_t);
230 static void elf_sect_writeaddr(struct Section
*, int64_t, size_t);
231 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
233 static void elf_write_sections(void);
234 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
235 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
236 static void add_sectname(char *, char *);
238 /* type values for stabs debugging sections */
239 #define N_SO 0x64 /* ID for main source file */
240 #define N_SOL 0x84 /* ID for sub-source file */
241 #define N_BINCL 0x82 /* not currently used */
242 #define N_EINCL 0xA2 /* not currently used */
259 int section
; /* index into sects[] */
260 int segto
; /* internal section number */
261 char *name
; /* shallow-copied pointer of section name */
265 struct symlininfo info
;
268 struct linelist
*next
;
269 struct linelist
*last
;
278 struct sectlist
*next
;
279 struct sectlist
*last
;
282 /* common debug variables */
283 static int currentline
= 1;
284 static int debug_immcall
= 0;
286 /* stabs debug variables */
287 static struct linelist
*stabslines
= 0;
288 static int numlinestabs
= 0;
289 static char *stabs_filename
= 0;
290 static int symtabsection
;
291 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
292 static int stablen
, stabstrlen
, stabrellen
;
294 /* dwarf debug variables */
295 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
296 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
297 static int dwarf_numfiles
= 0, dwarf_nsections
;
298 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
299 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
300 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
301 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
302 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
303 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
306 static struct dfmt df_dwarf
;
307 static struct dfmt df_stabs
;
308 static struct Symbol
*lastsym
;
310 /* common debugging routines */
311 void debug64_typevalue(int32_t);
312 void debug64_init(struct ofmt
*, void *, FILE *, efunc
);
313 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
314 void debug64_directive(const char *, const char *);
316 /* stabs debugging routines */
317 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
318 void stabs64_output(int, void *);
319 void stabs64_generate(void);
320 void stabs64_cleanup(void);
322 /* dwarf debugging routines */
323 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
324 void dwarf64_output(int, void *);
325 void dwarf64_generate(void);
326 void dwarf64_cleanup(void);
327 void dwarf64_findfile(const char *);
328 void dwarf64_findsect(const int);
331 * Special section numbers which are used to define ELF special
332 * symbols, which can be used with WRT to provide PIC relocation
335 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
336 static int32_t elf_got_sect
, elf_plt_sect
;
337 static int32_t elf_sym_sect
;
338 static int32_t elf_gottpoff_sect
;
340 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
346 (void)ldef
; /* placate optimisers */
348 nsects
= sectlen
= 0;
349 syms
= saa_init((int32_t)sizeof(struct Symbol
));
350 nlocals
= nglobs
= 0;
353 saa_wbytes(strs
, "\0", 1L);
354 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
355 strslen
= 2 + strlen(elf_module
);
357 shstrtablen
= shstrtabsize
= 0;;
358 add_sectname("", "");
362 elf_gotpc_sect
= seg_alloc();
363 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
365 elf_gotoff_sect
= seg_alloc();
366 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
368 elf_got_sect
= seg_alloc();
369 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
371 elf_plt_sect
= seg_alloc();
372 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
374 elf_sym_sect
= seg_alloc();
375 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
377 elf_gottpoff_sect
= seg_alloc();
378 ldef("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
381 def_seg
= seg_alloc();
385 static void elf_cleanup(int debuginfo
)
394 for (i
= 0; i
< nsects
; i
++) {
395 if (sects
[i
]->type
!= SHT_NOBITS
)
396 saa_free(sects
[i
]->data
);
398 saa_free(sects
[i
]->rel
);
399 while (sects
[i
]->head
) {
401 sects
[i
]->head
= sects
[i
]->head
->next
;
409 if (of_elf64
.current_dfmt
) {
410 of_elf64
.current_dfmt
->cleanup();
413 /* add entry to the elf .shstrtab section */
414 static void add_sectname(char *firsthalf
, char *secondhalf
)
416 int len
= strlen(firsthalf
) + strlen(secondhalf
);
417 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
418 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
419 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
420 strcat(shstrtab
+ shstrtablen
, secondhalf
);
421 shstrtablen
+= len
+ 1;
424 static int elf_make_section(char *name
, int type
, int flags
, int align
)
428 s
= nasm_malloc(sizeof(*s
));
430 if (type
!= SHT_NOBITS
)
431 s
->data
= saa_init(1L);
434 s
->len
= s
->size
= 0;
436 if (!strcmp(name
, ".text"))
439 s
->index
= seg_alloc();
440 add_sectname("", name
);
441 s
->name
= nasm_malloc(1 + strlen(name
));
442 strcpy(s
->name
, name
);
448 if (nsects
>= sectlen
)
450 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
456 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
459 unsigned flags_and
, flags_or
;
460 uint64_t type
, align
;
464 * Default is 64 bits.
472 while (*p
&& !nasm_isspace(*p
))
476 flags_and
= flags_or
= type
= align
= 0;
478 while (*p
&& nasm_isspace(*p
))
482 while (*p
&& !nasm_isspace(*p
))
486 while (*p
&& nasm_isspace(*p
))
489 if (!nasm_strnicmp(q
, "align=", 6)) {
493 if ((align
- 1) & align
) { /* means it's not a power of two */
494 error(ERR_NONFATAL
, "section alignment %d is not"
495 " a power of two", align
);
498 } else if (!nasm_stricmp(q
, "alloc")) {
499 flags_and
|= SHF_ALLOC
;
500 flags_or
|= SHF_ALLOC
;
501 } else if (!nasm_stricmp(q
, "noalloc")) {
502 flags_and
|= SHF_ALLOC
;
503 flags_or
&= ~SHF_ALLOC
;
504 } else if (!nasm_stricmp(q
, "exec")) {
505 flags_and
|= SHF_EXECINSTR
;
506 flags_or
|= SHF_EXECINSTR
;
507 } else if (!nasm_stricmp(q
, "noexec")) {
508 flags_and
|= SHF_EXECINSTR
;
509 flags_or
&= ~SHF_EXECINSTR
;
510 } else if (!nasm_stricmp(q
, "write")) {
511 flags_and
|= SHF_WRITE
;
512 flags_or
|= SHF_WRITE
;
513 } else if (!nasm_stricmp(q
, "tls")) {
514 flags_and
|= SHF_TLS
;
516 } else if (!nasm_stricmp(q
, "nowrite")) {
517 flags_and
|= SHF_WRITE
;
518 flags_or
&= ~SHF_WRITE
;
519 } else if (!nasm_stricmp(q
, "progbits")) {
521 } else if (!nasm_stricmp(q
, "nobits")) {
526 if (!strcmp(name
, ".comment") ||
527 !strcmp(name
, ".shstrtab") ||
528 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
529 error(ERR_NONFATAL
, "attempt to redefine reserved section"
534 for (i
= 0; i
< nsects
; i
++)
535 if (!strcmp(name
, sects
[i
]->name
))
538 if (!strcmp(name
, ".text"))
539 i
= elf_make_section(name
, SHT_PROGBITS
,
540 SHF_ALLOC
| SHF_EXECINSTR
, 16);
541 else if (!strcmp(name
, ".rodata"))
542 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
543 else if (!strcmp(name
, ".data"))
544 i
= elf_make_section(name
, SHT_PROGBITS
,
545 SHF_ALLOC
| SHF_WRITE
, 4);
546 else if (!strcmp(name
, ".bss"))
547 i
= elf_make_section(name
, SHT_NOBITS
,
548 SHF_ALLOC
| SHF_WRITE
, 4);
549 else if (!strcmp(name
, ".tdata"))
550 i
= elf_make_section(name
, SHT_PROGBITS
,
551 SHF_ALLOC
| SHF_WRITE
| SHF_TLS
, 4);
552 else if (!strcmp(name
, ".tbss"))
553 i
= elf_make_section(name
, SHT_NOBITS
,
554 SHF_ALLOC
| SHF_WRITE
| SHF_TLS
, 4);
556 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
558 sects
[i
]->type
= type
;
560 sects
[i
]->align
= align
;
561 sects
[i
]->flags
&= ~flags_and
;
562 sects
[i
]->flags
|= flags_or
;
563 } else if (pass
== 1) {
564 if ((type
&& sects
[i
]->type
!= type
)
565 || (align
&& sects
[i
]->align
!= align
)
566 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
567 error(ERR_WARNING
, "incompatible section attributes ignored on"
568 " redeclaration of section `%s'", name
);
571 return sects
[i
]->index
;
574 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
575 int is_global
, char *special
)
579 bool special_used
= false;
581 #if defined(DEBUG) && DEBUG>2
583 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
584 name
, segment
, offset
, is_global
, special
);
586 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
588 * This is a NASM special symbol. We never allow it into
589 * the ELF symbol table, even if it's a valid one. If it
590 * _isn't_ a valid one, we should barf immediately.
592 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
593 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
594 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
595 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
599 if (is_global
== 3) {
602 * Fix up a forward-reference symbol size from the first
605 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
606 if (!strcmp((*s
)->name
, name
)) {
607 struct tokenval tokval
;
611 while (*p
&& !nasm_isspace(*p
))
613 while (*p
&& nasm_isspace(*p
))
617 tokval
.t_type
= TOKEN_INVALID
;
618 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
621 error(ERR_NONFATAL
, "cannot use relocatable"
622 " expression as symbol size");
624 (*s
)->size
= reloc_value(e
);
628 * Remove it from the list of unresolved sizes.
630 nasm_free((*s
)->name
);
634 return; /* it wasn't an important one */
637 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
638 strslen
+= 1 + strlen(name
);
640 lastsym
= sym
= saa_wstruct(syms
);
643 sym
->type
= is_global
? SYM_GLOBAL
: 0;
644 sym
->other
= STV_DEFAULT
;
646 if (segment
== NO_SEG
)
647 sym
->section
= SHN_ABS
;
650 sym
->section
= SHN_UNDEF
;
651 if (nsects
== 0 && segment
== def_seg
) {
653 if (segment
!= elf_section_names(".text", 2, &tempint
))
655 "strange segment conditions in ELF driver");
656 sym
->section
= nsects
;
658 for (i
= 0; i
< nsects
; i
++)
659 if (segment
== sects
[i
]->index
) {
660 sym
->section
= i
+ 1;
666 if (is_global
== 2) {
669 sym
->section
= SHN_COMMON
;
671 * We have a common variable. Check the special text to see
672 * if it's a valid number and power of two; if so, store it
673 * as the alignment for the common variable.
677 sym
->symv
.key
= readnum(special
, &err
);
679 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
680 " valid number", special
);
681 else if ((sym
->symv
.key
| (sym
->symv
.key
- 1))
682 != 2 * sym
->symv
.key
- 1)
683 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
684 " power of two", special
);
688 sym
->symv
.key
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
690 if (sym
->type
== SYM_GLOBAL
) {
692 * If sym->section == SHN_ABS, then the first line of the
693 * else section would cause a core dump, because its a reference
694 * beyond the end of the section array.
695 * This behaviour is exhibited by this code:
698 * To avoid such a crash, such requests are silently discarded.
699 * This may not be the best solution.
701 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
702 bsym
= raa_write(bsym
, segment
, nglobs
);
703 } else if (sym
->section
!= SHN_ABS
) {
705 * This is a global symbol; so we must add it to the rbtree
706 * of global symbols in its section.
708 * In addition, we check the special text for symbol
709 * type and size information.
711 sects
[sym
->section
-1]->gsyms
=
712 rb_insert(sects
[sym
->section
-1]->gsyms
, &sym
->symv
);
715 int n
= strcspn(special
, " \t");
717 if (!nasm_strnicmp(special
, "function", n
))
718 sym
->type
|= STT_FUNC
;
719 else if (!nasm_strnicmp(special
, "data", n
) ||
720 !nasm_strnicmp(special
, "object", n
))
721 sym
->type
|= STT_OBJECT
;
722 else if (!nasm_strnicmp(special
, "notype", n
))
723 sym
->type
|= STT_NOTYPE
;
725 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
729 while (nasm_isspace(*special
))
732 n
= strcspn(special
, " \t");
733 if (!nasm_strnicmp(special
, "default", n
))
734 sym
->other
= STV_DEFAULT
;
735 else if (!nasm_strnicmp(special
, "internal", n
))
736 sym
->other
= STV_INTERNAL
;
737 else if (!nasm_strnicmp(special
, "hidden", n
))
738 sym
->other
= STV_HIDDEN
;
739 else if (!nasm_strnicmp(special
, "protected", n
))
740 sym
->other
= STV_PROTECTED
;
747 struct tokenval tokval
;
750 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
752 while (special
[n
] && nasm_isspace(special
[n
]))
755 * We have a size expression; attempt to
759 stdscan_bufptr
= special
+ n
;
760 tokval
.t_type
= TOKEN_INVALID
;
761 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
766 sym
->name
= nasm_strdup(name
);
769 error(ERR_NONFATAL
, "cannot use relocatable"
770 " expression as symbol size");
772 sym
->size
= reloc_value(e
);
774 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
779 * If TLS segment, mark symbol accordingly.
781 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
783 sym
->type
|= STT_TLS
;
786 sym
->globnum
= nglobs
;
791 if (special
&& !special_used
)
792 error(ERR_NONFATAL
, "no special symbol features supported here");
795 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
796 int64_t offset
, int type
)
799 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
800 sect
->tail
= &r
->next
;
803 r
->address
= sect
->len
;
805 if (segment
== NO_SEG
)
810 for (i
= 0; i
< nsects
; i
++)
811 if (segment
== sects
[i
]->index
)
814 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
822 * This routine deals with ..got and ..sym relocations: the more
823 * complicated kinds. In shared-library writing, some relocations
824 * with respect to global symbols must refer to the precise symbol
825 * rather than referring to an offset from the base of the section
826 * _containing_ the symbol. Such relocations call to this routine,
827 * which searches the symbol list for the symbol in question.
829 * R_386_GOT32 references require the _exact_ symbol address to be
830 * used; R_386_32 references can be at an offset from the symbol.
831 * The boolean argument `exact' tells us this.
833 * Return value is the adjusted value of `addr', having become an
834 * offset from the symbol rather than the section. Should always be
835 * zero when returning from an exact call.
837 * Limitation: if you define two symbols at the same place,
838 * confusion will occur.
840 * Inefficiency: we search, currently, using a linked list which
841 * isn't even necessarily sorted.
843 static void elf_add_gsym_reloc(struct Section
*sect
,
844 int32_t segment
, uint64_t offset
, int64_t pcrel
,
845 int type
, bool exact
)
854 * First look up the segment/offset pair and find a global
855 * symbol corresponding to it. If it's not one of our segments,
856 * then it must be an external symbol, in which case we're fine
857 * doing a normal elf_add_reloc after first sanity-checking
858 * that the offset from the symbol is zero.
861 for (i
= 0; i
< nsects
; i
++)
862 if (segment
== sects
[i
]->index
) {
869 error(ERR_NONFATAL
, "invalid access to an external symbol");
871 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
875 srb
= rb_search(s
->gsyms
, offset
);
876 if (!srb
|| (exact
&& srb
->key
!= offset
)) {
877 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
878 " for this reference");
881 sym
= container_of(srb
, struct Symbol
, symv
);
883 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
884 sect
->tail
= &r
->next
;
887 r
->address
= sect
->len
;
888 r
->offset
= offset
- pcrel
- sym
->symv
.key
;
889 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
895 static void elf_out(int32_t segto
, const void *data
,
896 enum out_type type
, uint64_t size
,
897 int32_t segment
, int32_t wrt
)
902 static struct symlininfo sinfo
;
906 #if defined(DEBUG) && DEBUG>2
907 if (data
) fprintf(stderr
,
908 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
909 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
911 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
912 currentline
, type
, segment
, segto
, size
);
916 * handle absolute-assembly (structure definitions)
918 if (segto
== NO_SEG
) {
919 if (type
!= OUT_RESERVE
)
920 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
926 for (i
= 0; i
< nsects
; i
++)
927 if (segto
== sects
[i
]->index
) {
932 int tempint
; /* ignored */
933 if (segto
!= elf_section_names(".text", 2, &tempint
))
934 error(ERR_PANIC
, "strange segment conditions in ELF driver");
936 s
= sects
[nsects
- 1];
940 /* invoke current debug_output routine */
941 if (of_elf64
.current_dfmt
) {
942 sinfo
.offset
= s
->len
;
945 sinfo
.name
= s
->name
;
946 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
948 /* end of debugging stuff */
950 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
951 error(ERR_WARNING
, "attempt to initialize memory in"
952 " BSS section `%s': ignored", s
->name
);
953 s
->len
+= realsize(type
, size
);
957 if (type
== OUT_RESERVE
) {
958 if (s
->type
== SHT_PROGBITS
) {
959 error(ERR_WARNING
, "uninitialized space declared in"
960 " non-BSS section `%s': zeroing", s
->name
);
961 elf_sect_write(s
, NULL
, size
);
964 } else if (type
== OUT_RAWDATA
) {
965 if (segment
!= NO_SEG
)
966 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
967 elf_sect_write(s
, data
, size
);
968 } else if (type
== OUT_ADDRESS
) {
969 addr
= *(int64_t *)data
;
970 if (segment
== NO_SEG
) {
972 } else if (segment
% 2) {
973 error(ERR_NONFATAL
, "ELF format does not support"
974 " segment base references");
979 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
982 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
985 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
988 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
991 error(ERR_PANIC
, "internal error elf64-hpa-871");
995 } else if (wrt
== elf_gotpc_sect
+ 1) {
997 * The user will supply GOT relative to $$. ELF
998 * will let us have GOT relative to $. So we
999 * need to fix up the data item by $-$$.
1002 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1004 } else if (wrt
== elf_gotoff_sect
+ 1) {
1006 error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
1007 "references to be qword");
1009 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
1012 } else if (wrt
== elf_got_sect
+ 1) {
1013 switch ((int)size
) {
1015 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1016 R_X86_64_GOT32
, true);
1020 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1021 R_X86_64_GOT64
, true);
1025 error(ERR_NONFATAL
, "invalid ..got reference");
1028 } else if (wrt
== elf_sym_sect
+ 1) {
1029 switch ((int)size
) {
1031 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1036 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1037 R_X86_64_16
, false);
1041 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1042 R_X86_64_32
, false);
1046 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1047 R_X86_64_64
, false);
1051 error(ERR_PANIC
, "internal error elf64-hpa-903");
1054 } else if (wrt
== elf_plt_sect
+ 1) {
1055 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1056 "relative PLT references");
1058 error(ERR_NONFATAL
, "ELF format does not support this"
1062 elf_sect_writeaddr(s
, addr
, size
);
1063 } else if (type
== OUT_REL2ADR
) {
1064 addr
= *(int64_t *)data
- size
;
1065 if (segment
== segto
)
1066 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
1067 if (segment
== NO_SEG
) {
1069 } else if (segment
% 2) {
1070 error(ERR_NONFATAL
, "ELF format does not support"
1071 " segment base references");
1073 if (wrt
== NO_SEG
) {
1074 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
1078 "Unsupported non-32-bit ELF relocation [2]");
1081 elf_sect_writeaddr(s
, addr
, 2);
1082 } else if (type
== OUT_REL4ADR
) {
1083 addr
= *(int64_t *)data
- size
;
1084 if (segment
== segto
)
1085 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
1086 if (segment
== NO_SEG
) {
1088 } else if (segment
% 2) {
1089 error(ERR_NONFATAL
, "ELF64 format does not support"
1090 " segment base references");
1092 if (wrt
== NO_SEG
) {
1093 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1095 } else if (wrt
== elf_plt_sect
+ 1) {
1096 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1097 R_X86_64_PLT32
, true);
1099 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1100 wrt
== elf_got_sect
+ 1) {
1101 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1102 R_X86_64_GOTPCREL
, true);
1104 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1105 wrt
== elf_got_sect
+ 1) {
1106 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1108 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1109 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1110 R_X86_64_GOTTPOFF
, true);
1113 error(ERR_NONFATAL
, "ELF64 format does not support this"
1117 elf_sect_writeaddr(s
, addr
, 4);
1118 } else if (type
== OUT_REL8ADR
) {
1119 addr
= *(int64_t *)data
- size
;
1120 if (segment
== segto
)
1121 error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
1122 if (segment
== NO_SEG
) {
1124 } else if (segment
% 2) {
1125 error(ERR_NONFATAL
, "ELF64 format does not support"
1126 " segment base references");
1128 if (wrt
== NO_SEG
) {
1129 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1131 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1132 wrt
== elf_got_sect
+ 1) {
1133 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1134 R_X86_64_GOTPCREL64
, true);
1136 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1137 wrt
== elf_got_sect
+ 1) {
1138 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1140 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1141 error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1144 error(ERR_NONFATAL
, "ELF64 format does not support this"
1148 elf_sect_writeaddr(s
, addr
, 8);
1152 static void elf_write(void)
1162 int32_t symtablen
, symtablocal
;
1165 * Work out how many sections we will have. We have SHN_UNDEF,
1166 * then the flexible user sections, then the four fixed
1167 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1168 * then optionally relocation sections for the user sections.
1170 if (of_elf64
.current_dfmt
== &df_stabs
)
1172 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1175 nsections
= 5; /* SHN_UNDEF and the fixed ones */
1177 add_sectname("", ".comment");
1178 add_sectname("", ".shstrtab");
1179 add_sectname("", ".symtab");
1180 add_sectname("", ".strtab");
1181 for (i
= 0; i
< nsects
; i
++) {
1182 nsections
++; /* for the section itself */
1183 if (sects
[i
]->head
) {
1184 nsections
++; /* for its relocations */
1185 add_sectname(".rela", sects
[i
]->name
);
1189 if (of_elf64
.current_dfmt
== &df_stabs
) {
1190 /* in case the debug information is wanted, just add these three sections... */
1191 add_sectname("", ".stab");
1192 add_sectname("", ".stabstr");
1193 add_sectname(".rel", ".stab");
1196 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1197 /* the dwarf debug standard specifies the following ten sections,
1198 not all of which are currently implemented,
1199 although all of them are defined. */
1200 #define debug_aranges (int64_t) (nsections-10)
1201 #define debug_info (int64_t) (nsections-7)
1202 #define debug_abbrev (int64_t) (nsections-5)
1203 #define debug_line (int64_t) (nsections-4)
1204 add_sectname("", ".debug_aranges");
1205 add_sectname(".rela", ".debug_aranges");
1206 add_sectname("", ".debug_pubnames");
1207 add_sectname("", ".debug_info");
1208 add_sectname(".rela", ".debug_info");
1209 add_sectname("", ".debug_abbrev");
1210 add_sectname("", ".debug_line");
1211 add_sectname(".rela", ".debug_line");
1212 add_sectname("", ".debug_frame");
1213 add_sectname("", ".debug_loc");
1220 commlen
= 2 + snprintf(comment
+1, sizeof comment
-1, "%s", nasm_comment
);
1223 * Output the ELF header.
1225 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1226 fputc(elf_osabi
, elffp
);
1227 fputc(elf_abiver
, elffp
);
1228 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp
);
1229 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1230 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1231 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1232 fwriteint64_t(0L, elffp
); /* no entry point */
1233 fwriteint64_t(0L, elffp
); /* no program header table */
1234 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1235 * ELF header plus alignment */
1236 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1237 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1238 fwriteint16_t(0, elffp
); /* no program header table, again */
1239 fwriteint16_t(0, elffp
); /* still no program header table */
1240 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1241 fwriteint16_t(nsections
, elffp
); /* number of sections */
1242 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1243 * section header table */
1246 * Build the symbol table and relocation tables.
1248 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1249 for (i
= 0; i
< nsects
; i
++)
1251 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1255 * Now output the section header table.
1258 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1259 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1262 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1263 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1264 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1266 for (i
= 0; i
< nsects
; i
++) {
1267 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1268 (sects
[i
]->type
== SHT_PROGBITS
?
1269 sects
[i
]->data
: NULL
), true,
1270 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1272 scount
++; /* ditto */
1274 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1275 scount
++; /* ditto */
1277 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1278 scount
++; /* ditto */
1280 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1281 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1283 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1284 for (i
= 0; i
< nsects
; i
++)
1285 if (sects
[i
]->head
) {
1287 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, true,
1288 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1290 if (of_elf64
.current_dfmt
== &df_stabs
) {
1291 /* for debugging information, create the last three sections
1292 which are the .stab , .stabstr and .rel.stab sections respectively */
1294 /* this function call creates the stab sections in memory */
1297 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1299 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1300 nsections
- 2, 0, 4, 12);
1303 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1304 stabstrlen
, 0, 0, 4, 0);
1307 /* link -> symtable info -> section to refer to */
1308 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1309 stabrellen
, symtabsection
, nsections
- 3, 4,
1313 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1314 /* for dwarf debugging information, create the ten dwarf sections */
1316 /* this function call creates the dwarf sections in memory */
1317 if (dwarf_fsect
) dwarf64_generate();
1320 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1321 arangeslen
, 0, 0, 1, 0);
1323 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1324 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1326 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1327 pubnameslen
, 0, 0, 1, 0);
1329 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1330 infolen
, 0, 0, 1, 0);
1332 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1333 inforellen
, symtabsection
, debug_info
, 1, 24);
1335 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1336 abbrevlen
, 0, 0, 1, 0);
1338 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1339 linelen
, 0, 0, 1, 0);
1341 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1342 linerellen
, symtabsection
, debug_line
, 1, 24);
1344 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1345 framelen
, 0, 0, 8, 0);
1347 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1348 loclen
, 0, 0, 1, 0);
1351 fwrite(align_str
, align
, 1, elffp
);
1354 * Now output the sections.
1356 elf_write_sections();
1358 nasm_free(elf_sects
);
1362 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1364 struct SAA
*s
= saa_init(1L);
1366 uint8_t entry
[24], *p
;
1372 * First, an all-zeros entry, required by the ELF spec.
1374 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1379 * Next, an entry for the file name.
1382 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1383 WRITESHORT(p
, STT_FILE
); /* type FILE */
1384 WRITESHORT(p
, SHN_ABS
);
1385 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1386 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1387 saa_wbytes(s
, entry
, 24L);
1392 * Now some standard symbols defining the segments, for relocation
1395 for (i
= 1; i
<= nsects
; i
++) {
1397 WRITELONG(p
, 0); /* no symbol name */
1398 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1399 WRITESHORT(p
, i
); /* section id */
1400 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1401 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1402 saa_wbytes(s
, entry
, 24L);
1409 * Now the other local symbols.
1412 while ((sym
= saa_rstruct(syms
))) {
1413 if (sym
->type
& SYM_GLOBAL
)
1416 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1417 WRITECHAR(p
, sym
->type
); /* type and binding */
1418 WRITECHAR(p
, sym
->other
); /* visibility */
1419 WRITESHORT(p
, sym
->section
); /* index into section header table */
1420 WRITEDLONG(p
, (int64_t)sym
->symv
.key
); /* value of symbol */
1421 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1422 saa_wbytes(s
, entry
, 24L);
1427 * dwarf needs symbols for debug sections
1428 * which are relocation targets.
1430 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1431 dwarf_infosym
= *local
;
1433 WRITELONG(p
, 0); /* no symbol name */
1434 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1435 WRITESHORT(p
, debug_info
); /* section id */
1436 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1437 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1438 saa_wbytes(s
, entry
, 24L);
1441 dwarf_abbrevsym
= *local
;
1443 WRITELONG(p
, 0); /* no symbol name */
1444 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1445 WRITESHORT(p
, debug_abbrev
); /* section id */
1446 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1447 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1448 saa_wbytes(s
, entry
, 24L);
1451 dwarf_linesym
= *local
;
1453 WRITELONG(p
, 0); /* no symbol name */
1454 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1455 WRITESHORT(p
, debug_line
); /* section id */
1456 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1457 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1458 saa_wbytes(s
, entry
, 24L);
1464 * Now the global symbols.
1467 while ((sym
= saa_rstruct(syms
))) {
1468 if (!(sym
->type
& SYM_GLOBAL
))
1471 WRITELONG(p
, sym
->strpos
);
1472 WRITECHAR(p
, sym
->type
); /* type and binding */
1473 WRITECHAR(p
, sym
->other
); /* visibility */
1474 WRITESHORT(p
, sym
->section
);
1475 WRITEDLONG(p
, (int64_t)sym
->symv
.key
);
1476 WRITEDLONG(p
, (int64_t)sym
->size
);
1477 saa_wbytes(s
, entry
, 24L);
1484 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1487 uint8_t *p
, entry
[24];
1496 int64_t sym
= r
->symbol
;
1498 if (sym
>= GLOBAL_TEMP_BASE
)
1500 if (of_elf64
.current_dfmt
== &df_dwarf
)
1501 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 5) + nlocals
;
1502 else sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1505 WRITEDLONG(p
, r
->address
);
1506 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1507 WRITEDLONG(p
, r
->offset
);
1508 saa_wbytes(s
, entry
, 24L);
1517 static void elf_section_header(int name
, int type
, uint64_t flags
,
1518 void *data
, bool is_saa
, uint64_t datalen
,
1519 int link
, int info
, int align
, int eltsize
)
1521 elf_sects
[elf_nsect
].data
= data
;
1522 elf_sects
[elf_nsect
].len
= datalen
;
1523 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1526 fwriteint32_t((int32_t)name
, elffp
);
1527 fwriteint32_t((int32_t)type
, elffp
);
1528 fwriteint64_t((int64_t)flags
, elffp
);
1529 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1530 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1531 fwriteint64_t(datalen
, elffp
);
1533 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1534 fwriteint32_t((int32_t)link
, elffp
);
1535 fwriteint32_t((int32_t)info
, elffp
);
1536 fwriteint64_t((int64_t)align
, elffp
);
1537 fwriteint64_t((int64_t)eltsize
, elffp
);
1540 static void elf_write_sections(void)
1543 for (i
= 0; i
< elf_nsect
; i
++)
1544 if (elf_sects
[i
].data
) {
1545 int32_t len
= elf_sects
[i
].len
;
1546 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1547 int32_t align
= reallen
- len
;
1548 if (elf_sects
[i
].is_saa
)
1549 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1551 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1552 fwrite(align_str
, align
, 1, elffp
);
1556 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1558 saa_wbytes(sect
->data
, data
, len
);
1561 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1563 saa_writeaddr(sect
->data
, data
, len
);
1567 static int32_t elf_segbase(int32_t segment
)
1572 static int elf_directive(char *directive
, char *value
, int pass
)
1578 if (!strcmp(directive
, "osabi")) {
1580 return 1; /* ignore in pass 2 */
1582 n
= readnum(value
, &err
);
1584 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1587 if (n
< 0 || n
> 255) {
1588 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1594 if ((p
= strchr(value
,',')) == NULL
)
1597 n
= readnum(p
+1, &err
);
1598 if (err
|| n
< 0 || n
> 255) {
1599 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1610 static void elf_filename(char *inname
, char *outname
, efunc error
)
1612 strcpy(elf_module
, inname
);
1613 standard_extension(inname
, outname
, ".o", error
);
1616 extern macros_t elf_stdmac
[];
1618 static int elf_set_info(enum geninfo type
, char **val
)
1624 static struct dfmt df_dwarf
= {
1625 "ELF64 (X86_64) dwarf debug format for Linux",
1635 static struct dfmt df_stabs
= {
1636 "ELF64 (X86_64) stabs debug format for Linux",
1647 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1649 struct ofmt of_elf64
= {
1650 "ELF64 (x86_64) object files (e.g. Linux)",
1667 /* common debugging routines */
1668 void debug64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1675 void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1685 void debug64_directive(const char *directive
, const char *params
)
1691 void debug64_typevalue(int32_t type
)
1693 int32_t stype
, ssize
;
1694 switch (TYM_TYPE(type
)) {
1733 stype
= STT_SECTION
;
1748 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1749 lastsym
->size
= ssize
;
1750 lastsym
->type
= stype
;
1754 /* stabs debugging routines */
1757 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1760 if (!stabs_filename
) {
1761 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1762 strcpy(stabs_filename
, filename
);
1764 if (strcmp(stabs_filename
, filename
)) {
1765 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1766 in fact, this leak comes in quite handy to maintain a list of files
1767 encountered so far in the symbol lines... */
1769 /* why not nasm_free(stabs_filename); we're done with the old one */
1771 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1772 strcpy(stabs_filename
, filename
);
1776 currentline
= linenumber
;
1780 void stabs64_output(int type
, void *param
)
1782 struct symlininfo
*s
;
1783 struct linelist
*el
;
1784 if (type
== TY_DEBUGSYMLIN
) {
1785 if (debug_immcall
) {
1786 s
= (struct symlininfo
*)param
;
1787 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1788 return; /* line info is only collected for executable sections */
1790 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1791 el
->info
.offset
= s
->offset
;
1792 el
->info
.section
= s
->section
;
1793 el
->info
.name
= s
->name
;
1794 el
->line
= currentline
;
1795 el
->filename
= stabs_filename
;
1798 stabslines
->last
->next
= el
;
1799 stabslines
->last
= el
;
1802 stabslines
->last
= el
;
1809 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1811 WRITELONG(p,n_strx); \
1812 WRITECHAR(p,n_type); \
1813 WRITECHAR(p,n_other); \
1814 WRITESHORT(p,n_desc); \
1815 WRITELONG(p,n_value); \
1818 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1820 void stabs64_generate(void)
1822 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1823 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1827 struct linelist
*ptr
;
1831 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1832 for (i
= 0; i
< numlinestabs
; i
++)
1836 if (numfiles
== 0) {
1837 allfiles
[0] = ptr
->filename
;
1840 for (i
= 0; i
< numfiles
; i
++) {
1841 if (!strcmp(allfiles
[i
], ptr
->filename
))
1844 if (i
>= numfiles
) {
1845 allfiles
[i
] = ptr
->filename
;
1852 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1853 for (i
= 0; i
< numfiles
; i
++) {
1854 fileidx
[i
] = strsize
;
1855 strsize
+= strlen(allfiles
[i
]) + 1;
1858 for (i
= 0; i
< numfiles
; i
++) {
1859 if (!strcmp(allfiles
[i
], elf_module
)) {
1865 /* worst case size of the stab buffer would be:
1866 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1869 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1870 sizeof(struct stabentry
));
1872 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1874 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1877 for (i
= 0; i
< numfiles
; i
++) {
1878 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1882 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1889 /* this is the first stab, its strx points to the filename of the
1890 the source-file, the n_desc field should be set to the number
1893 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1895 /* this is the stab for the main source file */
1896 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1898 /* relocation table entry */
1900 /* Since the symbol table has two entries before */
1901 /* the section symbols, the index in the info.section */
1902 /* member must be adjusted by adding 2 */
1904 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1905 WRITELONG(rptr
, R_X86_64_32
);
1906 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1909 currfile
= mainfileindex
;
1913 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1914 /* oops file has changed... */
1915 for (i
= 0; i
< numfiles
; i
++)
1916 if (!strcmp(allfiles
[i
], ptr
->filename
))
1919 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1923 /* relocation table entry */
1925 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1926 WRITELONG(rptr
, R_X86_64_32
);
1927 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1930 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1933 /* relocation table entry */
1935 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1936 WRITELONG(rptr
, R_X86_64_32
);
1937 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1943 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1945 nasm_free(allfiles
);
1948 stablen
= (sptr
- sbuf
);
1949 stabrellen
= (rptr
- rbuf
);
1955 void stabs64_cleanup(void)
1957 struct linelist
*ptr
, *del
;
1969 nasm_free(stabrelbuf
);
1971 nasm_free(stabstrbuf
);
1973 /* dwarf routines */
1976 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1979 dwarf64_findfile(filename
);
1981 currentline
= linenumber
;
1984 /* called from elf_out with type == TY_DEBUGSYMLIN */
1985 void dwarf64_output(int type
, void *param
)
1987 int ln
, aa
, inx
, maxln
, soc
;
1988 struct symlininfo
*s
;
1993 s
= (struct symlininfo
*)param
;
1994 /* line number info is only gathered for executable sections */
1995 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1997 /* Check if section index has changed */
1998 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
2000 dwarf64_findsect(s
->section
);
2002 /* do nothing unless line or file has changed */
2005 ln
= currentline
- dwarf_csect
->line
;
2006 aa
= s
->offset
- dwarf_csect
->offset
;
2007 inx
= dwarf_clist
->line
;
2008 plinep
= dwarf_csect
->psaa
;
2009 /* check for file change */
2010 if (!(inx
== dwarf_csect
->file
))
2012 saa_write8(plinep
,DW_LNS_set_file
);
2013 saa_write8(plinep
,inx
);
2014 dwarf_csect
->file
= inx
;
2016 /* check for line change */
2019 /* test if in range of special op code */
2020 maxln
= line_base
+ line_range
;
2021 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
2022 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
2024 saa_write8(plinep
,soc
);
2030 saa_write8(plinep
,DW_LNS_advance_line
);
2031 saa_wleb128s(plinep
,ln
);
2035 saa_write8(plinep
,DW_LNS_advance_pc
);
2036 saa_wleb128u(plinep
,aa
);
2039 dwarf_csect
->line
= currentline
;
2040 dwarf_csect
->offset
= s
->offset
;
2042 /* show change handled */
2048 void dwarf64_generate(void)
2052 struct linelist
*ftentry
;
2053 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
2054 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
2055 struct sectlist
*psect
;
2056 size_t saalen
, linepoff
, totlen
, highaddr
;
2058 /* write epilogues for each line program range */
2059 /* and build aranges section */
2060 paranges
= saa_init(1L);
2061 parangesrel
= saa_init(1L);
2062 saa_write16(paranges
,3); /* dwarf version */
2063 saa_write64(parangesrel
, paranges
->datalen
+4);
2064 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
2065 saa_write64(parangesrel
, 0);
2066 saa_write32(paranges
,0); /* offset into info */
2067 saa_write8(paranges
,8); /* pointer size */
2068 saa_write8(paranges
,0); /* not segmented */
2069 saa_write32(paranges
,0); /* padding */
2070 /* iterate though sectlist entries */
2071 psect
= dwarf_fsect
;
2074 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2076 plinep
= psect
->psaa
;
2077 /* Line Number Program Epilogue */
2078 saa_write8(plinep
,2); /* std op 2 */
2079 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2080 saa_write8(plinep
,DW_LNS_extended_op
);
2081 saa_write8(plinep
,1); /* operand length */
2082 saa_write8(plinep
,DW_LNE_end_sequence
);
2083 totlen
+= plinep
->datalen
;
2084 /* range table relocation entry */
2085 saa_write64(parangesrel
, paranges
->datalen
+ 4);
2086 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2087 saa_write64(parangesrel
, (uint64_t) 0);
2088 /* range table entry */
2089 saa_write64(paranges
,0x0000); /* range start */
2090 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
2091 highaddr
+= sects
[psect
->section
]->len
;
2092 /* done with this entry */
2093 psect
= psect
->next
;
2095 saa_write64(paranges
,0); /* null address */
2096 saa_write64(paranges
,0); /* null length */
2097 saalen
= paranges
->datalen
;
2098 arangeslen
= saalen
+ 4;
2099 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2100 WRITELONG(pbuf
,saalen
); /* initial length */
2101 saa_rnbytes(paranges
, pbuf
, saalen
);
2104 /* build rela.aranges section */
2105 arangesrellen
= saalen
= parangesrel
->datalen
;
2106 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2107 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2108 saa_free(parangesrel
);
2110 /* build pubnames section */
2111 ppubnames
= saa_init(1L);
2112 saa_write16(ppubnames
,3); /* dwarf version */
2113 saa_write32(ppubnames
,0); /* offset into info */
2114 saa_write32(ppubnames
,0); /* space used in info */
2115 saa_write32(ppubnames
,0); /* end of list */
2116 saalen
= ppubnames
->datalen
;
2117 pubnameslen
= saalen
+ 4;
2118 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2119 WRITELONG(pbuf
,saalen
); /* initial length */
2120 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2121 saa_free(ppubnames
);
2123 /* build info section */
2124 pinfo
= saa_init(1L);
2125 pinforel
= saa_init(1L);
2126 saa_write16(pinfo
,3); /* dwarf version */
2127 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2128 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2129 saa_write64(pinforel
, 0);
2130 saa_write32(pinfo
,0); /* offset into abbrev */
2131 saa_write8(pinfo
,8); /* pointer size */
2132 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2133 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2134 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2135 saa_write64(pinforel
, 0);
2136 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2137 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2138 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2139 saa_write64(pinforel
, 0);
2140 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2141 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2142 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2143 saa_write64(pinforel
, 0);
2144 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2145 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2146 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2147 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2148 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2149 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2150 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2151 saa_write64(pinforel
, 0);
2152 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2153 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2154 saa_write8(pinfo
,0); /* end of entries */
2155 saalen
= pinfo
->datalen
;
2156 infolen
= saalen
+ 4;
2157 infobuf
= pbuf
= nasm_malloc(infolen
);
2158 WRITELONG(pbuf
,saalen
); /* initial length */
2159 saa_rnbytes(pinfo
, pbuf
, saalen
);
2162 /* build rela.info section */
2163 inforellen
= saalen
= pinforel
->datalen
;
2164 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2165 saa_rnbytes(pinforel
, pbuf
, saalen
);
2168 /* build abbrev section */
2169 pabbrev
= saa_init(1L);
2170 saa_write8(pabbrev
,1); /* entry number LEB128u */
2171 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2172 saa_write8(pabbrev
,1); /* has children */
2173 /* the following attributes and forms are all LEB128u values */
2174 saa_write8(pabbrev
,DW_AT_low_pc
);
2175 saa_write8(pabbrev
,DW_FORM_addr
);
2176 saa_write8(pabbrev
,DW_AT_high_pc
);
2177 saa_write8(pabbrev
,DW_FORM_addr
);
2178 saa_write8(pabbrev
,DW_AT_stmt_list
);
2179 saa_write8(pabbrev
,DW_FORM_data4
);
2180 saa_write8(pabbrev
,DW_AT_name
);
2181 saa_write8(pabbrev
,DW_FORM_string
);
2182 saa_write8(pabbrev
,DW_AT_producer
);
2183 saa_write8(pabbrev
,DW_FORM_string
);
2184 saa_write8(pabbrev
,DW_AT_language
);
2185 saa_write8(pabbrev
,DW_FORM_data2
);
2186 saa_write16(pabbrev
,0); /* end of entry */
2187 /* LEB128u usage same as above */
2188 saa_write8(pabbrev
,2); /* entry number */
2189 saa_write8(pabbrev
,DW_TAG_subprogram
);
2190 saa_write8(pabbrev
,0); /* no children */
2191 saa_write8(pabbrev
,DW_AT_low_pc
);
2192 saa_write8(pabbrev
,DW_FORM_addr
);
2193 saa_write8(pabbrev
,DW_AT_frame_base
);
2194 saa_write8(pabbrev
,DW_FORM_data4
);
2195 saa_write16(pabbrev
,0); /* end of entry */
2196 abbrevlen
= saalen
= pabbrev
->datalen
;
2197 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2198 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2201 /* build line section */
2203 plines
= saa_init(1L);
2204 saa_write8(plines
,1); /* Minimum Instruction Length */
2205 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2206 saa_write8(plines
,line_base
); /* Line Base */
2207 saa_write8(plines
,line_range
); /* Line Range */
2208 saa_write8(plines
,opcode_base
); /* Opcode Base */
2209 /* standard opcode lengths (# of LEB128u operands) */
2210 saa_write8(plines
,0); /* Std opcode 1 length */
2211 saa_write8(plines
,1); /* Std opcode 2 length */
2212 saa_write8(plines
,1); /* Std opcode 3 length */
2213 saa_write8(plines
,1); /* Std opcode 4 length */
2214 saa_write8(plines
,1); /* Std opcode 5 length */
2215 saa_write8(plines
,0); /* Std opcode 6 length */
2216 saa_write8(plines
,0); /* Std opcode 7 length */
2217 saa_write8(plines
,0); /* Std opcode 8 length */
2218 saa_write8(plines
,1); /* Std opcode 9 length */
2219 saa_write8(plines
,0); /* Std opcode 10 length */
2220 saa_write8(plines
,0); /* Std opcode 11 length */
2221 saa_write8(plines
,1); /* Std opcode 12 length */
2222 /* Directory Table */
2223 saa_write8(plines
,0); /* End of table */
2224 /* File Name Table */
2225 ftentry
= dwarf_flist
;
2226 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2228 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2229 saa_write8(plines
,0); /* directory LEB128u */
2230 saa_write8(plines
,0); /* time LEB128u */
2231 saa_write8(plines
,0); /* size LEB128u */
2232 ftentry
= ftentry
->next
;
2234 saa_write8(plines
,0); /* End of table */
2235 linepoff
= plines
->datalen
;
2236 linelen
= linepoff
+ totlen
+ 10;
2237 linebuf
= pbuf
= nasm_malloc(linelen
);
2238 WRITELONG(pbuf
,linelen
-4); /* initial length */
2239 WRITESHORT(pbuf
,3); /* dwarf version */
2240 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2241 /* write line header */
2243 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2246 /* concatonate line program ranges */
2248 plinesrel
= saa_init(1L);
2249 psect
= dwarf_fsect
;
2250 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2252 saa_write64(plinesrel
, linepoff
);
2253 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2254 saa_write64(plinesrel
, (uint64_t) 0);
2255 plinep
= psect
->psaa
;
2256 saalen
= plinep
->datalen
;
2257 saa_rnbytes(plinep
, pbuf
, saalen
);
2261 /* done with this entry */
2262 psect
= psect
->next
;
2266 /* build rela.lines section */
2267 linerellen
=saalen
= plinesrel
->datalen
;
2268 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2269 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2270 saa_free(plinesrel
);
2272 /* build frame section */
2274 framebuf
= pbuf
= nasm_malloc(framelen
);
2275 WRITELONG(pbuf
,framelen
-4); /* initial length */
2277 /* build loc section */
2279 locbuf
= pbuf
= nasm_malloc(loclen
);
2280 WRITEDLONG(pbuf
,0); /* null beginning offset */
2281 WRITEDLONG(pbuf
,0); /* null ending offset */
2284 void dwarf64_cleanup(void)
2287 nasm_free(arangesbuf
);
2289 nasm_free(arangesrelbuf
);
2291 nasm_free(pubnamesbuf
);
2295 nasm_free(inforelbuf
);
2297 nasm_free(abbrevbuf
);
2301 nasm_free(linerelbuf
);
2303 nasm_free(framebuf
);
2307 void dwarf64_findfile(const char * fname
)
2310 struct linelist
*match
;
2312 /* return if fname is current file name */
2313 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2314 /* search for match */
2320 match
= dwarf_flist
;
2321 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2323 if (!(strcmp(fname
, match
->filename
)))
2325 dwarf_clist
= match
;
2330 /* add file name to end of list */
2331 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2333 dwarf_clist
->line
= dwarf_numfiles
;
2334 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2335 strcpy(dwarf_clist
->filename
,fname
);
2336 dwarf_clist
->next
= 0;
2337 /* if first entry */
2340 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2341 dwarf_clist
->last
= 0;
2343 /* chain to previous entry */
2346 dwarf_elist
->next
= dwarf_clist
;
2347 dwarf_elist
= dwarf_clist
;
2352 void dwarf64_findsect(const int index
)
2355 struct sectlist
*match
;
2357 /* return if index is current section index */
2358 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2362 /* search for match */
2368 match
= dwarf_fsect
;
2369 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2371 if ((match
->section
== index
))
2373 dwarf_csect
= match
;
2376 match
= match
->next
;
2379 /* add entry to end of list */
2380 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2382 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2383 dwarf_csect
->line
= 1;
2384 dwarf_csect
->offset
= 0;
2385 dwarf_csect
->file
= 1;
2386 dwarf_csect
->section
= index
;
2387 dwarf_csect
->next
= 0;
2388 /* set relocatable address at start of line program */
2389 saa_write8(plinep
,DW_LNS_extended_op
);
2390 saa_write8(plinep
,9); /* operand length */
2391 saa_write8(plinep
,DW_LNE_set_address
);
2392 saa_write64(plinep
,0); /* Start Address */
2393 /* if first entry */
2396 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2397 dwarf_csect
->last
= 0;
2399 /* chain to previous entry */
2402 dwarf_esect
->next
= dwarf_csect
;
2403 dwarf_esect
= dwarf_csect
;