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.
25 /* Definitions in lieu of elf.h */
26 #define SHT_NULL 0 /* Inactive section header */
27 #define SHT_PROGBITS 1 /* Program defined content */
28 #define SHT_RELA 4 /* Relocation entries with addends */
29 #define SHT_NOBITS 8 /* Section requires no space in file */
30 #define SHF_WRITE (1 << 0) /* Writable */
31 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
32 #define SHF_EXECINSTR (1 << 2) /* Executable */
33 #define SHF_TLS (1 << 10) /* Section hold thread-local data. */
34 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
35 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
36 #define R_X86_64_NONE 0 /* No reloc */
37 #define R_X86_64_64 1 /* Direct 64 bit */
38 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
39 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
40 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
41 #define R_X86_64_COPY 5 /* Copy symbol at runtime */
42 #define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
43 #define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
44 #define R_X86_64_RELATIVE 8 /* Adjust by program base */
45 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative
47 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
48 #define R_X86_64_32S 11 /* Direct 32 bit sign extended */
49 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
50 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
51 #define R_X86_64_8 14 /* Direct 8 bit sign extended */
52 #define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
53 #define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */
54 #define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */
55 #define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */
56 #define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset
57 to two GOT entries for GD symbol */
58 #define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset
59 to two GOT entries for LD symbol */
60 #define R_X86_64_DTPOFF32 21 /* Offset in TLS block */
61 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset
62 to GOT entry for IE symbol */
63 #define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */
64 #define R_X86_64_PC64 24 /* word64 S + A - P */
65 #define R_X86_64_GOTOFF64 25 /* word64 S + A - GOT */
66 #define R_X86_64_GOTPC32 26 /* word32 GOT + A - P */
67 #define R_X86_64_GOT64 27 /* word64 G + A */
68 #define R_X86_64_GOTPCREL64 28 /* word64 G + GOT - P + A */
69 #define R_X86_64_GOTPC64 29 /* word64 GOT - P + A */
70 #define R_X86_64_GOTPLT64 30 /* word64 G + A */
71 #define R_X86_64_PLTOFF64 31 /* word64 L - GOT + A */
72 #define R_X86_64_SIZE32 32 /* word32 Z + A */
73 #define R_X86_64_SIZE64 33 /* word64 Z + A */
74 #define R_X86_64_GOTPC32_TLSDESC 34 /* word32 */
75 #define R_X86_64_TLSDESC_CALL 35 /* none */
76 #define R_X86_64_TLSDESC 36 /* word64×2 */
77 #define ET_REL 1 /* Relocatable file */
78 #define EM_X86_64 62 /* AMD x86-64 architecture */
79 #define STT_NOTYPE 0 /* Symbol type is unspecified */
80 #define STT_OBJECT 1 /* Symbol is a data object */
81 #define STT_FUNC 2 /* Symbol is a code object */
82 #define STT_SECTION 3 /* Symbol associated with a section */
83 #define STT_FILE 4 /* Symbol's name is file name */
84 #define STT_COMMON 5 /* Symbol is a common data object */
85 #define STT_TLS 6 /* Symbol is thread-local data object*/
86 #define STT_NUM 7 /* Number of defined types. */
88 /* Definitions in lieu of dwarf.h */
89 #define DW_TAG_compile_unit 0x11
90 #define DW_TAG_subprogram 0x2e
91 #define DW_AT_name 0x03
92 #define DW_AT_stmt_list 0x10
93 #define DW_AT_low_pc 0x11
94 #define DW_AT_high_pc 0x12
95 #define DW_AT_language 0x13
96 #define DW_AT_producer 0x25
97 #define DW_AT_frame_base 0x40
98 #define DW_FORM_addr 0x01
99 #define DW_FORM_data2 0x05
100 #define DW_FORM_data4 0x06
101 #define DW_FORM_string 0x08
102 #define DW_LNS_extended_op 0
103 #define DW_LNS_advance_pc 2
104 #define DW_LNS_advance_line 3
105 #define DW_LNS_set_file 4
106 #define DW_LNE_end_sequence 1
107 #define DW_LNE_set_address 2
108 #define DW_LNE_define_file 3
109 #define DW_LANG_Mips_Assembler 0x8001
111 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
113 typedef uint32_t Elf64_Word
;
114 typedef uint64_t Elf64_Xword
;
115 typedef uint64_t Elf64_Addr
;
116 typedef uint64_t Elf64_Off
;
119 Elf64_Word sh_name
; /* Section name (string tbl index) */
120 Elf64_Word sh_type
; /* Section type */
121 Elf64_Xword sh_flags
; /* Section flags */
122 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
123 Elf64_Off sh_offset
; /* Section file offset */
124 Elf64_Xword sh_size
; /* Section size in bytes */
125 Elf64_Word sh_link
; /* Link to another section */
126 Elf64_Word sh_info
; /* Additional section information */
127 Elf64_Xword sh_addralign
; /* Section alignment */
128 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
137 int64_t address
; /* relative to _start_ of section */
138 int64_t symbol
; /* symbol index */
139 int64_t offset
; /* symbol addend */
140 int type
; /* type of relocation */
144 int32_t strpos
; /* string table position of name */
145 int32_t section
; /* section ID of the symbol */
146 int type
; /* symbol type */
147 int other
; /* symbol visibility */
148 int64_t value
; /* address, or COMMON variable align */
149 int32_t size
; /* size of symbol */
150 int32_t globnum
; /* symbol table offset if global */
151 struct Symbol
*next
; /* list of globals in each section */
152 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
153 char *name
; /* used temporarily if in above list */
161 int32_t index
; /* index into sects array */
162 uint32_t type
; /* SHT_PROGBITS or SHT_NOBITS */
163 uint64_t align
; /* alignment: power of two */
164 uint64_t flags
; /* section flags */
168 struct Reloc
*head
, **tail
;
169 struct Symbol
*gsyms
; /* global symbols in section */
172 #define SECT_DELTA 32
173 static struct Section
**sects
;
174 static int nsects
, sectlen
;
176 #define SHSTR_DELTA 256
177 static char *shstrtab
;
178 static int shstrtablen
, shstrtabsize
;
180 static struct SAA
*syms
;
181 static uint32_t nlocals
, nglobs
;
183 static int32_t def_seg
;
185 static struct RAA
*bsym
;
187 static struct SAA
*strs
;
188 static uint32_t strslen
;
192 static evalfunc evaluate
;
194 static struct Symbol
*fwds
;
196 static char elf_module
[FILENAME_MAX
];
198 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
199 static uint8_t elf_abiver
= 0; /* Current ABI version */
201 extern struct ofmt of_elf64
;
205 #define SYM_GLOBAL 0x10
207 #define STV_DEFAULT 0
208 #define STV_INTERNAL 1
210 #define STV_PROTECTED 3
212 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
214 #define SEG_ALIGN 16 /* alignment of sections in file */
215 #define SEG_ALIGN_1 (SEG_ALIGN-1)
217 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
219 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
221 static struct ELF_SECTDATA
{
226 static int elf_nsect
, nsections
;
227 static int64_t elf_foffs
;
229 static void elf_write(void);
230 static void elf_sect_write(struct Section
*, const void *, size_t);
231 static void elf_sect_writeaddr(struct Section
*, int64_t, size_t);
232 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
234 static void elf_write_sections(void);
235 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
236 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
237 static void add_sectname(char *, char *);
239 /* type values for stabs debugging sections */
240 #define N_SO 0x64 /* ID for main source file */
241 #define N_SOL 0x84 /* ID for sub-source file */
242 #define N_BINCL 0x82 /* not currently used */
243 #define N_EINCL 0xA2 /* not currently used */
260 int section
; /* index into sects[] */
261 int segto
; /* internal section number */
262 char *name
; /* shallow-copied pointer of section name */
266 struct symlininfo info
;
269 struct linelist
*next
;
270 struct linelist
*last
;
279 struct sectlist
*next
;
280 struct sectlist
*last
;
283 /* common debug variables */
284 static int currentline
= 1;
285 static int debug_immcall
= 0;
287 /* stabs debug variables */
288 static struct linelist
*stabslines
= 0;
289 static int numlinestabs
= 0;
290 static char *stabs_filename
= 0;
291 static int symtabsection
;
292 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
293 static int stablen
, stabstrlen
, stabrellen
;
295 /* dwarf debug variables */
296 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
297 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
298 static int dwarf_numfiles
= 0, dwarf_nsections
;
299 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
300 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
301 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
302 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
303 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
304 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
307 static struct dfmt df_dwarf
;
308 static struct dfmt df_stabs
;
309 static struct Symbol
*lastsym
;
311 /* common debugging routines */
312 void debug64_typevalue(int32_t);
313 void debug64_init(struct ofmt
*, void *, FILE *, efunc
);
314 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
315 void debug64_directive(const char *, const char *);
317 /* stabs debugging routines */
318 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
319 void stabs64_output(int, void *);
320 void stabs64_generate(void);
321 void stabs64_cleanup(void);
323 /* dwarf debugging routines */
324 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
325 void dwarf64_output(int, void *);
326 void dwarf64_generate(void);
327 void dwarf64_cleanup(void);
328 void dwarf64_findfile(const char *);
329 void dwarf64_findsect(const int);
332 * Special section numbers which are used to define ELF special
333 * symbols, which can be used with WRT to provide PIC relocation
336 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
337 static int32_t elf_got_sect
, elf_plt_sect
;
338 static int32_t elf_sym_sect
;
339 static int32_t elf_gottpoff_sect
;
341 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
347 (void)ldef
; /* placate optimisers */
349 nsects
= sectlen
= 0;
350 syms
= saa_init((int32_t)sizeof(struct Symbol
));
351 nlocals
= nglobs
= 0;
354 saa_wbytes(strs
, "\0", 1L);
355 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
356 strslen
= 2 + strlen(elf_module
);
358 shstrtablen
= shstrtabsize
= 0;;
359 add_sectname("", "");
363 elf_gotpc_sect
= seg_alloc();
364 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
366 elf_gotoff_sect
= seg_alloc();
367 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
369 elf_got_sect
= seg_alloc();
370 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
372 elf_plt_sect
= seg_alloc();
373 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
375 elf_sym_sect
= seg_alloc();
376 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
378 elf_gottpoff_sect
= seg_alloc();
379 ldef("..gottpoff", elf_gottpoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
382 def_seg
= seg_alloc();
386 static void elf_cleanup(int debuginfo
)
395 for (i
= 0; i
< nsects
; i
++) {
396 if (sects
[i
]->type
!= SHT_NOBITS
)
397 saa_free(sects
[i
]->data
);
399 saa_free(sects
[i
]->rel
);
400 while (sects
[i
]->head
) {
402 sects
[i
]->head
= sects
[i
]->head
->next
;
410 if (of_elf64
.current_dfmt
) {
411 of_elf64
.current_dfmt
->cleanup();
414 /* add entry to the elf .shstrtab section */
415 static void add_sectname(char *firsthalf
, char *secondhalf
)
417 int len
= strlen(firsthalf
) + strlen(secondhalf
);
418 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
419 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
420 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
421 strcat(shstrtab
+ shstrtablen
, secondhalf
);
422 shstrtablen
+= len
+ 1;
425 static int elf_make_section(char *name
, int type
, int flags
, int align
)
429 s
= nasm_malloc(sizeof(*s
));
431 if (type
!= SHT_NOBITS
)
432 s
->data
= saa_init(1L);
435 s
->len
= s
->size
= 0;
437 if (!strcmp(name
, ".text"))
440 s
->index
= seg_alloc();
441 add_sectname("", name
);
442 s
->name
= nasm_malloc(1 + strlen(name
));
443 strcpy(s
->name
, name
);
449 if (nsects
>= sectlen
)
451 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
457 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
460 unsigned flags_and
, flags_or
;
461 uint64_t type
, align
;
465 * Default is 64 bits.
473 while (*p
&& !nasm_isspace(*p
))
477 flags_and
= flags_or
= type
= align
= 0;
479 while (*p
&& nasm_isspace(*p
))
483 while (*p
&& !nasm_isspace(*p
))
487 while (*p
&& nasm_isspace(*p
))
490 if (!nasm_strnicmp(q
, "align=", 6)) {
494 if ((align
- 1) & align
) { /* means it's not a power of two */
495 error(ERR_NONFATAL
, "section alignment %d is not"
496 " a power of two", align
);
499 } else if (!nasm_stricmp(q
, "alloc")) {
500 flags_and
|= SHF_ALLOC
;
501 flags_or
|= SHF_ALLOC
;
502 } else if (!nasm_stricmp(q
, "noalloc")) {
503 flags_and
|= SHF_ALLOC
;
504 flags_or
&= ~SHF_ALLOC
;
505 } else if (!nasm_stricmp(q
, "exec")) {
506 flags_and
|= SHF_EXECINSTR
;
507 flags_or
|= SHF_EXECINSTR
;
508 } else if (!nasm_stricmp(q
, "noexec")) {
509 flags_and
|= SHF_EXECINSTR
;
510 flags_or
&= ~SHF_EXECINSTR
;
511 } else if (!nasm_stricmp(q
, "write")) {
512 flags_and
|= SHF_WRITE
;
513 flags_or
|= SHF_WRITE
;
514 } else if (!nasm_stricmp(q
, "tls")) {
515 flags_and
|= SHF_TLS
;
517 } else if (!nasm_stricmp(q
, "nowrite")) {
518 flags_and
|= SHF_WRITE
;
519 flags_or
&= ~SHF_WRITE
;
520 } else if (!nasm_stricmp(q
, "progbits")) {
522 } else if (!nasm_stricmp(q
, "nobits")) {
527 if (!strcmp(name
, ".comment") ||
528 !strcmp(name
, ".shstrtab") ||
529 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
530 error(ERR_NONFATAL
, "attempt to redefine reserved section"
535 for (i
= 0; i
< nsects
; i
++)
536 if (!strcmp(name
, sects
[i
]->name
))
539 if (!strcmp(name
, ".text"))
540 i
= elf_make_section(name
, SHT_PROGBITS
,
541 SHF_ALLOC
| SHF_EXECINSTR
, 16);
542 else if (!strcmp(name
, ".rodata"))
543 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
544 else if (!strcmp(name
, ".data"))
545 i
= elf_make_section(name
, SHT_PROGBITS
,
546 SHF_ALLOC
| SHF_WRITE
, 4);
547 else if (!strcmp(name
, ".bss"))
548 i
= elf_make_section(name
, SHT_NOBITS
,
549 SHF_ALLOC
| SHF_WRITE
, 4);
550 else if (!strcmp(name
, ".tdata"))
551 i
= elf_make_section(name
, SHT_PROGBITS
,
552 SHF_ALLOC
| SHF_WRITE
| SHF_TLS
, 4);
553 else if (!strcmp(name
, ".tbss"))
554 i
= elf_make_section(name
, SHT_NOBITS
,
555 SHF_ALLOC
| SHF_WRITE
| SHF_TLS
, 4);
557 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
559 sects
[i
]->type
= type
;
561 sects
[i
]->align
= align
;
562 sects
[i
]->flags
&= ~flags_and
;
563 sects
[i
]->flags
|= flags_or
;
564 } else if (pass
== 1) {
565 if ((type
&& sects
[i
]->type
!= type
)
566 || (align
&& sects
[i
]->align
!= align
)
567 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
568 error(ERR_WARNING
, "incompatible section attributes ignored on"
569 " redeclaration of section `%s'", name
);
572 return sects
[i
]->index
;
575 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
576 int is_global
, char *special
)
580 bool special_used
= false;
582 #if defined(DEBUG) && DEBUG>2
584 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
585 name
, segment
, offset
, is_global
, special
);
587 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
589 * This is a NASM special symbol. We never allow it into
590 * the ELF symbol table, even if it's a valid one. If it
591 * _isn't_ a valid one, we should barf immediately.
593 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
594 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
595 strcmp(name
, "..sym") && strcmp(name
, "..gottpoff"))
596 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
600 if (is_global
== 3) {
603 * Fix up a forward-reference symbol size from the first
606 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
607 if (!strcmp((*s
)->name
, name
)) {
608 struct tokenval tokval
;
612 while (*p
&& !nasm_isspace(*p
))
614 while (*p
&& nasm_isspace(*p
))
618 tokval
.t_type
= TOKEN_INVALID
;
619 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
622 error(ERR_NONFATAL
, "cannot use relocatable"
623 " expression as symbol size");
625 (*s
)->size
= reloc_value(e
);
629 * Remove it from the list of unresolved sizes.
631 nasm_free((*s
)->name
);
635 return; /* it wasn't an important one */
638 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
639 strslen
+= 1 + strlen(name
);
641 lastsym
= sym
= saa_wstruct(syms
);
644 sym
->type
= is_global
? SYM_GLOBAL
: 0;
645 sym
->other
= STV_DEFAULT
;
647 if (segment
== NO_SEG
)
648 sym
->section
= SHN_ABS
;
651 sym
->section
= SHN_UNDEF
;
652 if (nsects
== 0 && segment
== def_seg
) {
654 if (segment
!= elf_section_names(".text", 2, &tempint
))
656 "strange segment conditions in ELF driver");
657 sym
->section
= nsects
;
659 for (i
= 0; i
< nsects
; i
++)
660 if (segment
== sects
[i
]->index
) {
661 sym
->section
= i
+ 1;
667 if (is_global
== 2) {
670 sym
->section
= SHN_COMMON
;
672 * We have a common variable. Check the special text to see
673 * if it's a valid number and power of two; if so, store it
674 * as the alignment for the common variable.
678 sym
->value
= readnum(special
, &err
);
680 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
681 " valid number", special
);
682 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
683 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
684 " power of two", special
);
688 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
690 if (sym
->type
== SYM_GLOBAL
) {
692 * If sym->section == SHN_ABS, then the first line of the
693 * else section would cause a core dump, because its a reference
694 * beyond the end of the section array.
695 * This behaviour is exhibited by this code:
698 * To avoid such a crash, such requests are silently discarded.
699 * This may not be the best solution.
701 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
702 bsym
= raa_write(bsym
, segment
, nglobs
);
703 } else if (sym
->section
!= SHN_ABS
) {
705 * This is a global symbol; so we must add it to the linked
706 * list of global symbols in its section. We'll push it on
707 * the beginning of the list, because it doesn't matter
708 * much which end we put it on and it's easier like this.
710 * In addition, we check the special text for symbol
711 * type and size information.
713 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
714 sects
[sym
->section
- 1]->gsyms
= sym
;
717 int n
= strcspn(special
, " \t");
719 if (!nasm_strnicmp(special
, "function", n
))
720 sym
->type
|= STT_FUNC
;
721 else if (!nasm_strnicmp(special
, "data", n
) ||
722 !nasm_strnicmp(special
, "object", n
))
723 sym
->type
|= STT_OBJECT
;
724 else if (!nasm_strnicmp(special
, "notype", n
))
725 sym
->type
|= STT_NOTYPE
;
727 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
731 while (nasm_isspace(*special
))
734 n
= strcspn(special
, " \t");
735 if (!nasm_strnicmp(special
, "default", n
))
736 sym
->other
= STV_DEFAULT
;
737 else if (!nasm_strnicmp(special
, "internal", n
))
738 sym
->other
= STV_INTERNAL
;
739 else if (!nasm_strnicmp(special
, "hidden", n
))
740 sym
->other
= STV_HIDDEN
;
741 else if (!nasm_strnicmp(special
, "protected", n
))
742 sym
->other
= STV_PROTECTED
;
749 struct tokenval tokval
;
752 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
754 while (special
[n
] && nasm_isspace(special
[n
]))
757 * We have a size expression; attempt to
761 stdscan_bufptr
= special
+ n
;
762 tokval
.t_type
= TOKEN_INVALID
;
763 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
768 sym
->name
= nasm_strdup(name
);
771 error(ERR_NONFATAL
, "cannot use relocatable"
772 " expression as symbol size");
774 sym
->size
= reloc_value(e
);
776 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
781 * If TLS segment, mark symbol accordingly.
783 if (sects
[sym
->section
- 1]->flags
& SHF_TLS
) {
785 sym
->type
|= STT_TLS
;
788 sym
->globnum
= nglobs
;
793 if (special
&& !special_used
)
794 error(ERR_NONFATAL
, "no special symbol features supported here");
797 static void elf_add_reloc(struct Section
*sect
, int32_t segment
,
798 int64_t offset
, int type
)
801 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
802 sect
->tail
= &r
->next
;
805 r
->address
= sect
->len
;
807 if (segment
== NO_SEG
)
812 for (i
= 0; i
< nsects
; i
++)
813 if (segment
== sects
[i
]->index
)
816 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
824 * This routine deals with ..got and ..sym relocations: the more
825 * complicated kinds. In shared-library writing, some relocations
826 * with respect to global symbols must refer to the precise symbol
827 * rather than referring to an offset from the base of the section
828 * _containing_ the symbol. Such relocations call to this routine,
829 * which searches the symbol list for the symbol in question.
831 * R_386_GOT32 references require the _exact_ symbol address to be
832 * used; R_386_32 references can be at an offset from the symbol.
833 * The boolean argument `exact' tells us this.
835 * Return value is the adjusted value of `addr', having become an
836 * offset from the symbol rather than the section. Should always be
837 * zero when returning from an exact call.
839 * Limitation: if you define two symbols at the same place,
840 * confusion will occur.
842 * Inefficiency: we search, currently, using a linked list which
843 * isn't even necessarily sorted.
845 static void elf_add_gsym_reloc(struct Section
*sect
,
846 int32_t segment
, int64_t offset
, int64_t pcrel
,
847 int type
, bool exact
)
851 struct Symbol
*sym
, *sm
;
855 * First look up the segment/offset pair and find a global
856 * symbol corresponding to it. If it's not one of our segments,
857 * then it must be an external symbol, in which case we're fine
858 * doing a normal elf_add_reloc after first sanity-checking
859 * that the offset from the symbol is zero.
862 for (i
= 0; i
< nsects
; i
++)
863 if (segment
== sects
[i
]->index
) {
870 error(ERR_NONFATAL
, "invalid access to an external symbol");
872 elf_add_reloc(sect
, segment
, offset
- pcrel
, type
);
878 * Find a symbol pointing _exactly_ at this one.
880 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
881 if (sym
->value
== offset
)
884 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
885 " for this reference");
890 * Find the nearest symbol below this one.
893 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
894 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
898 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
899 sect
->tail
= &r
->next
;
902 r
->address
= sect
->len
;
903 r
->offset
= offset
- pcrel
- sym
->value
;
904 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
910 static void elf_out(int32_t segto
, const void *data
,
911 enum out_type type
, uint64_t size
,
912 int32_t segment
, int32_t wrt
)
917 static struct symlininfo sinfo
;
921 #if defined(DEBUG) && DEBUG>2
922 if (data
) fprintf(stderr
,
923 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
924 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
926 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
927 currentline
, type
, segment
, segto
, size
);
931 * handle absolute-assembly (structure definitions)
933 if (segto
== NO_SEG
) {
934 if (type
!= OUT_RESERVE
)
935 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
941 for (i
= 0; i
< nsects
; i
++)
942 if (segto
== sects
[i
]->index
) {
947 int tempint
; /* ignored */
948 if (segto
!= elf_section_names(".text", 2, &tempint
))
949 error(ERR_PANIC
, "strange segment conditions in ELF driver");
951 s
= sects
[nsects
- 1];
955 /* invoke current debug_output routine */
956 if (of_elf64
.current_dfmt
) {
957 sinfo
.offset
= s
->len
;
960 sinfo
.name
= s
->name
;
961 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
963 /* end of debugging stuff */
965 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
966 error(ERR_WARNING
, "attempt to initialize memory in"
967 " BSS section `%s': ignored", s
->name
);
968 s
->len
+= realsize(type
, size
);
972 if (type
== OUT_RESERVE
) {
973 if (s
->type
== SHT_PROGBITS
) {
974 error(ERR_WARNING
, "uninitialized space declared in"
975 " non-BSS section `%s': zeroing", s
->name
);
976 elf_sect_write(s
, NULL
, size
);
979 } else if (type
== OUT_RAWDATA
) {
980 if (segment
!= NO_SEG
)
981 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
982 elf_sect_write(s
, data
, size
);
983 } else if (type
== OUT_ADDRESS
) {
984 addr
= *(int64_t *)data
;
985 if (segment
== NO_SEG
) {
987 } else if (segment
% 2) {
988 error(ERR_NONFATAL
, "ELF format does not support"
989 " segment base references");
994 elf_add_reloc(s
, segment
, addr
, R_X86_64_8
);
997 elf_add_reloc(s
, segment
, addr
, R_X86_64_16
);
1000 elf_add_reloc(s
, segment
, addr
, R_X86_64_32
);
1003 elf_add_reloc(s
, segment
, addr
, R_X86_64_64
);
1006 error(ERR_PANIC
, "internal error elf64-hpa-871");
1010 } else if (wrt
== elf_gotpc_sect
+ 1) {
1012 * The user will supply GOT relative to $$. ELF
1013 * will let us have GOT relative to $. So we
1014 * need to fix up the data item by $-$$.
1017 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTPC32
);
1019 } else if (wrt
== elf_gotoff_sect
+ 1) {
1021 error(ERR_NONFATAL
, "ELF64 requires ..gotoff "
1022 "references to be qword");
1024 elf_add_reloc(s
, segment
, addr
, R_X86_64_GOTOFF64
);
1027 } else if (wrt
== elf_got_sect
+ 1) {
1028 switch ((int)size
) {
1030 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1031 R_X86_64_GOT32
, true);
1035 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1036 R_X86_64_GOT64
, true);
1040 error(ERR_NONFATAL
, "invalid ..got reference");
1043 } else if (wrt
== elf_sym_sect
+ 1) {
1044 switch ((int)size
) {
1046 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1051 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1052 R_X86_64_16
, false);
1056 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1057 R_X86_64_32
, false);
1061 elf_add_gsym_reloc(s
, segment
, addr
, 0,
1062 R_X86_64_64
, false);
1066 error(ERR_PANIC
, "internal error elf64-hpa-903");
1069 } else if (wrt
== elf_plt_sect
+ 1) {
1070 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1071 "relative PLT references");
1073 error(ERR_NONFATAL
, "ELF format does not support this"
1077 elf_sect_writeaddr(s
, addr
, size
);
1078 } else if (type
== OUT_REL2ADR
) {
1079 addr
= *(int64_t *)data
- size
;
1080 if (segment
== segto
)
1081 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
1082 if (segment
== NO_SEG
) {
1084 } else if (segment
% 2) {
1085 error(ERR_NONFATAL
, "ELF format does not support"
1086 " segment base references");
1088 if (wrt
== NO_SEG
) {
1089 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC16
);
1093 "Unsupported non-32-bit ELF relocation [2]");
1096 elf_sect_writeaddr(s
, addr
, 2);
1097 } else if (type
== OUT_REL4ADR
) {
1098 addr
= *(int64_t *)data
- size
;
1099 if (segment
== segto
)
1100 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
1101 if (segment
== NO_SEG
) {
1103 } else if (segment
% 2) {
1104 error(ERR_NONFATAL
, "ELF64 format does not support"
1105 " segment base references");
1107 if (wrt
== NO_SEG
) {
1108 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC32
);
1110 } else if (wrt
== elf_plt_sect
+ 1) {
1111 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1112 R_X86_64_PLT32
, true);
1114 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1115 wrt
== elf_got_sect
+ 1) {
1116 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1117 R_X86_64_GOTPCREL
, true);
1119 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1120 wrt
== elf_got_sect
+ 1) {
1121 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1123 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1124 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1125 R_X86_64_GOTTPOFF
, true);
1128 error(ERR_NONFATAL
, "ELF64 format does not support this"
1132 elf_sect_writeaddr(s
, addr
, 4);
1133 } else if (type
== OUT_REL8ADR
) {
1134 addr
= *(int64_t *)data
- size
;
1135 if (segment
== segto
)
1136 error(ERR_PANIC
, "intra-segment OUT_REL8ADR");
1137 if (segment
== NO_SEG
) {
1139 } else if (segment
% 2) {
1140 error(ERR_NONFATAL
, "ELF64 format does not support"
1141 " segment base references");
1143 if (wrt
== NO_SEG
) {
1144 elf_add_reloc(s
, segment
, addr
, R_X86_64_PC64
);
1146 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1147 wrt
== elf_got_sect
+ 1) {
1148 elf_add_gsym_reloc(s
, segment
, addr
+size
, size
,
1149 R_X86_64_GOTPCREL64
, true);
1151 } else if (wrt
== elf_gotoff_sect
+ 1 ||
1152 wrt
== elf_got_sect
+ 1) {
1153 error(ERR_NONFATAL
, "ELF64 requires ..gotoff references to be "
1155 } else if (wrt
== elf_gottpoff_sect
+ 1) {
1156 error(ERR_NONFATAL
, "ELF64 requires ..gottpoff references to be "
1159 error(ERR_NONFATAL
, "ELF64 format does not support this"
1163 elf_sect_writeaddr(s
, addr
, 8);
1167 static void elf_write(void)
1177 int32_t symtablen
, symtablocal
;
1180 * Work out how many sections we will have. We have SHN_UNDEF,
1181 * then the flexible user sections, then the four fixed
1182 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1183 * then optionally relocation sections for the user sections.
1185 if (of_elf64
.current_dfmt
== &df_stabs
)
1187 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1190 nsections
= 5; /* SHN_UNDEF and the fixed ones */
1192 add_sectname("", ".comment");
1193 add_sectname("", ".shstrtab");
1194 add_sectname("", ".symtab");
1195 add_sectname("", ".strtab");
1196 for (i
= 0; i
< nsects
; i
++) {
1197 nsections
++; /* for the section itself */
1198 if (sects
[i
]->head
) {
1199 nsections
++; /* for its relocations */
1200 add_sectname(".rela", sects
[i
]->name
);
1204 if (of_elf64
.current_dfmt
== &df_stabs
) {
1205 /* in case the debug information is wanted, just add these three sections... */
1206 add_sectname("", ".stab");
1207 add_sectname("", ".stabstr");
1208 add_sectname(".rel", ".stab");
1211 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1212 /* the dwarf debug standard specifies the following ten sections,
1213 not all of which are currently implemented,
1214 although all of them are defined. */
1215 #define debug_aranges (int64_t) (nsections-10)
1216 #define debug_info (int64_t) (nsections-7)
1217 #define debug_abbrev (int64_t) (nsections-5)
1218 #define debug_line (int64_t) (nsections-4)
1219 add_sectname("", ".debug_aranges");
1220 add_sectname(".rela", ".debug_aranges");
1221 add_sectname("", ".debug_pubnames");
1222 add_sectname("", ".debug_info");
1223 add_sectname(".rela", ".debug_info");
1224 add_sectname("", ".debug_abbrev");
1225 add_sectname("", ".debug_line");
1226 add_sectname(".rela", ".debug_line");
1227 add_sectname("", ".debug_frame");
1228 add_sectname("", ".debug_loc");
1235 commlen
= 2 + snprintf(comment
+1, sizeof comment
-1, "%s", nasm_comment
);
1238 * Output the ELF header.
1240 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1241 fputc(elf_osabi
, elffp
);
1242 fputc(elf_abiver
, elffp
);
1243 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp
);
1244 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1245 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1246 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1247 fwriteint64_t(0L, elffp
); /* no entry point */
1248 fwriteint64_t(0L, elffp
); /* no program header table */
1249 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1250 * ELF header plus alignment */
1251 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1252 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1253 fwriteint16_t(0, elffp
); /* no program header table, again */
1254 fwriteint16_t(0, elffp
); /* still no program header table */
1255 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1256 fwriteint16_t(nsections
, elffp
); /* number of sections */
1257 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1258 * section header table */
1261 * Build the symbol table and relocation tables.
1263 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1264 for (i
= 0; i
< nsects
; i
++)
1266 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1270 * Now output the section header table.
1273 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1274 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1277 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1278 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1279 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1281 for (i
= 0; i
< nsects
; i
++) {
1282 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1283 (sects
[i
]->type
== SHT_PROGBITS
?
1284 sects
[i
]->data
: NULL
), true,
1285 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1287 scount
++; /* ditto */
1289 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1290 scount
++; /* ditto */
1292 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1293 scount
++; /* ditto */
1295 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1296 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1298 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1299 for (i
= 0; i
< nsects
; i
++)
1300 if (sects
[i
]->head
) {
1302 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, true,
1303 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1305 if (of_elf64
.current_dfmt
== &df_stabs
) {
1306 /* for debugging information, create the last three sections
1307 which are the .stab , .stabstr and .rel.stab sections respectively */
1309 /* this function call creates the stab sections in memory */
1312 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1314 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1315 nsections
- 2, 0, 4, 12);
1318 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1319 stabstrlen
, 0, 0, 4, 0);
1322 /* link -> symtable info -> section to refer to */
1323 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1324 stabrellen
, symtabsection
, nsections
- 3, 4,
1328 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1329 /* for dwarf debugging information, create the ten dwarf sections */
1331 /* this function call creates the dwarf sections in memory */
1332 if (dwarf_fsect
) dwarf64_generate();
1335 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1336 arangeslen
, 0, 0, 1, 0);
1338 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1339 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1341 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1342 pubnameslen
, 0, 0, 1, 0);
1344 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1345 infolen
, 0, 0, 1, 0);
1347 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1348 inforellen
, symtabsection
, debug_info
, 1, 24);
1350 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1351 abbrevlen
, 0, 0, 1, 0);
1353 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1354 linelen
, 0, 0, 1, 0);
1356 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1357 linerellen
, symtabsection
, debug_line
, 1, 24);
1359 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1360 framelen
, 0, 0, 8, 0);
1362 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1363 loclen
, 0, 0, 1, 0);
1366 fwrite(align_str
, align
, 1, elffp
);
1369 * Now output the sections.
1371 elf_write_sections();
1373 nasm_free(elf_sects
);
1377 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1379 struct SAA
*s
= saa_init(1L);
1381 uint8_t entry
[24], *p
;
1387 * First, an all-zeros entry, required by the ELF spec.
1389 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1394 * Next, an entry for the file name.
1397 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1398 WRITESHORT(p
, STT_FILE
); /* type FILE */
1399 WRITESHORT(p
, SHN_ABS
);
1400 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1401 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1402 saa_wbytes(s
, entry
, 24L);
1407 * Now some standard symbols defining the segments, for relocation
1410 for (i
= 1; i
<= nsects
; i
++) {
1412 WRITELONG(p
, 0); /* no symbol name */
1413 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1414 WRITESHORT(p
, i
); /* section id */
1415 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1416 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1417 saa_wbytes(s
, entry
, 24L);
1424 * Now the other local symbols.
1427 while ((sym
= saa_rstruct(syms
))) {
1428 if (sym
->type
& SYM_GLOBAL
)
1431 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1432 WRITECHAR(p
, sym
->type
); /* type and binding */
1433 WRITECHAR(p
, sym
->other
); /* visibility */
1434 WRITESHORT(p
, sym
->section
); /* index into section header table */
1435 WRITEDLONG(p
, (int64_t)sym
->value
); /* value of symbol */
1436 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1437 saa_wbytes(s
, entry
, 24L);
1442 * dwarf needs symbols for debug sections
1443 * which are relocation targets.
1445 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1446 dwarf_infosym
= *local
;
1448 WRITELONG(p
, 0); /* no symbol name */
1449 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1450 WRITESHORT(p
, debug_info
); /* section id */
1451 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1452 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1453 saa_wbytes(s
, entry
, 24L);
1456 dwarf_abbrevsym
= *local
;
1458 WRITELONG(p
, 0); /* no symbol name */
1459 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1460 WRITESHORT(p
, debug_abbrev
); /* section id */
1461 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1462 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1463 saa_wbytes(s
, entry
, 24L);
1466 dwarf_linesym
= *local
;
1468 WRITELONG(p
, 0); /* no symbol name */
1469 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1470 WRITESHORT(p
, debug_line
); /* section id */
1471 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1472 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1473 saa_wbytes(s
, entry
, 24L);
1479 * Now the global symbols.
1482 while ((sym
= saa_rstruct(syms
))) {
1483 if (!(sym
->type
& SYM_GLOBAL
))
1486 WRITELONG(p
, sym
->strpos
);
1487 WRITECHAR(p
, sym
->type
); /* type and binding */
1488 WRITECHAR(p
, sym
->other
); /* visibility */
1489 WRITESHORT(p
, sym
->section
);
1490 WRITEDLONG(p
, (int64_t)sym
->value
);
1491 WRITEDLONG(p
, (int64_t)sym
->size
);
1492 saa_wbytes(s
, entry
, 24L);
1499 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1502 uint8_t *p
, entry
[24];
1511 int64_t sym
= r
->symbol
;
1513 if (sym
>= GLOBAL_TEMP_BASE
)
1515 if (of_elf64
.current_dfmt
== &df_dwarf
)
1516 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 5) + nlocals
;
1517 else sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1520 WRITEDLONG(p
, r
->address
);
1521 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1522 WRITEDLONG(p
, r
->offset
);
1523 saa_wbytes(s
, entry
, 24L);
1532 static void elf_section_header(int name
, int type
, uint64_t flags
,
1533 void *data
, bool is_saa
, uint64_t datalen
,
1534 int link
, int info
, int align
, int eltsize
)
1536 elf_sects
[elf_nsect
].data
= data
;
1537 elf_sects
[elf_nsect
].len
= datalen
;
1538 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1541 fwriteint32_t((int32_t)name
, elffp
);
1542 fwriteint32_t((int32_t)type
, elffp
);
1543 fwriteint64_t((int64_t)flags
, elffp
);
1544 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1545 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1546 fwriteint64_t(datalen
, elffp
);
1548 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1549 fwriteint32_t((int32_t)link
, elffp
);
1550 fwriteint32_t((int32_t)info
, elffp
);
1551 fwriteint64_t((int64_t)align
, elffp
);
1552 fwriteint64_t((int64_t)eltsize
, elffp
);
1555 static void elf_write_sections(void)
1558 for (i
= 0; i
< elf_nsect
; i
++)
1559 if (elf_sects
[i
].data
) {
1560 int32_t len
= elf_sects
[i
].len
;
1561 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1562 int32_t align
= reallen
- len
;
1563 if (elf_sects
[i
].is_saa
)
1564 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1566 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1567 fwrite(align_str
, align
, 1, elffp
);
1571 static void elf_sect_write(struct Section
*sect
, const void *data
, size_t len
)
1573 saa_wbytes(sect
->data
, data
, len
);
1576 static void elf_sect_writeaddr(struct Section
*sect
, int64_t data
, size_t len
)
1578 saa_writeaddr(sect
->data
, data
, len
);
1582 static int32_t elf_segbase(int32_t segment
)
1587 static int elf_directive(char *directive
, char *value
, int pass
)
1593 if (!strcmp(directive
, "osabi")) {
1595 return 1; /* ignore in pass 2 */
1597 n
= readnum(value
, &err
);
1599 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1602 if (n
< 0 || n
> 255) {
1603 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1609 if ((p
= strchr(value
,',')) == NULL
)
1612 n
= readnum(p
+1, &err
);
1613 if (err
|| n
< 0 || n
> 255) {
1614 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1625 static void elf_filename(char *inname
, char *outname
, efunc error
)
1627 strcpy(elf_module
, inname
);
1628 standard_extension(inname
, outname
, ".o", error
);
1631 extern macros_t elf_stdmac
[];
1633 static int elf_set_info(enum geninfo type
, char **val
)
1639 static struct dfmt df_dwarf
= {
1640 "ELF64 (X86_64) dwarf debug format for Linux",
1650 static struct dfmt df_stabs
= {
1651 "ELF64 (X86_64) stabs debug format for Linux",
1662 struct dfmt
*elf64_debugs_arr
[3] = { &df_dwarf
, &df_stabs
, NULL
};
1664 struct ofmt of_elf64
= {
1665 "ELF64 (x86_64) object files (e.g. Linux)",
1682 /* common debugging routines */
1683 void debug64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1690 void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1700 void debug64_directive(const char *directive
, const char *params
)
1706 void debug64_typevalue(int32_t type
)
1708 int32_t stype
, ssize
;
1709 switch (TYM_TYPE(type
)) {
1748 stype
= STT_SECTION
;
1763 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1764 lastsym
->size
= ssize
;
1765 lastsym
->type
= stype
;
1769 /* stabs debugging routines */
1772 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1775 if (!stabs_filename
) {
1776 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1777 strcpy(stabs_filename
, filename
);
1779 if (strcmp(stabs_filename
, filename
)) {
1780 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1781 in fact, this leak comes in quite handy to maintain a list of files
1782 encountered so far in the symbol lines... */
1784 /* why not nasm_free(stabs_filename); we're done with the old one */
1786 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1787 strcpy(stabs_filename
, filename
);
1791 currentline
= linenumber
;
1795 void stabs64_output(int type
, void *param
)
1797 struct symlininfo
*s
;
1798 struct linelist
*el
;
1799 if (type
== TY_DEBUGSYMLIN
) {
1800 if (debug_immcall
) {
1801 s
= (struct symlininfo
*)param
;
1802 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1803 return; /* line info is only collected for executable sections */
1805 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1806 el
->info
.offset
= s
->offset
;
1807 el
->info
.section
= s
->section
;
1808 el
->info
.name
= s
->name
;
1809 el
->line
= currentline
;
1810 el
->filename
= stabs_filename
;
1813 stabslines
->last
->next
= el
;
1814 stabslines
->last
= el
;
1817 stabslines
->last
= el
;
1824 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1826 WRITELONG(p,n_strx); \
1827 WRITECHAR(p,n_type); \
1828 WRITECHAR(p,n_other); \
1829 WRITESHORT(p,n_desc); \
1830 WRITELONG(p,n_value); \
1833 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1835 void stabs64_generate(void)
1837 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1838 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1842 struct linelist
*ptr
;
1846 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1847 for (i
= 0; i
< numlinestabs
; i
++)
1851 if (numfiles
== 0) {
1852 allfiles
[0] = ptr
->filename
;
1855 for (i
= 0; i
< numfiles
; i
++) {
1856 if (!strcmp(allfiles
[i
], ptr
->filename
))
1859 if (i
>= numfiles
) {
1860 allfiles
[i
] = ptr
->filename
;
1867 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1868 for (i
= 0; i
< numfiles
; i
++) {
1869 fileidx
[i
] = strsize
;
1870 strsize
+= strlen(allfiles
[i
]) + 1;
1873 for (i
= 0; i
< numfiles
; i
++) {
1874 if (!strcmp(allfiles
[i
], elf_module
)) {
1880 /* worst case size of the stab buffer would be:
1881 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1884 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1885 sizeof(struct stabentry
));
1887 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1889 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1892 for (i
= 0; i
< numfiles
; i
++) {
1893 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1897 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1904 /* this is the first stab, its strx points to the filename of the
1905 the source-file, the n_desc field should be set to the number
1908 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1910 /* this is the stab for the main source file */
1911 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1913 /* relocation table entry */
1915 /* Since the symbol table has two entries before */
1916 /* the section symbols, the index in the info.section */
1917 /* member must be adjusted by adding 2 */
1919 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1920 WRITELONG(rptr
, R_X86_64_32
);
1921 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1924 currfile
= mainfileindex
;
1928 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1929 /* oops file has changed... */
1930 for (i
= 0; i
< numfiles
; i
++)
1931 if (!strcmp(allfiles
[i
], ptr
->filename
))
1934 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1938 /* relocation table entry */
1940 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1941 WRITELONG(rptr
, R_X86_64_32
);
1942 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1945 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1948 /* relocation table entry */
1950 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1951 WRITELONG(rptr
, R_X86_64_32
);
1952 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1958 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1960 nasm_free(allfiles
);
1963 stablen
= (sptr
- sbuf
);
1964 stabrellen
= (rptr
- rbuf
);
1970 void stabs64_cleanup(void)
1972 struct linelist
*ptr
, *del
;
1984 nasm_free(stabrelbuf
);
1986 nasm_free(stabstrbuf
);
1988 /* dwarf routines */
1991 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1994 dwarf64_findfile(filename
);
1996 currentline
= linenumber
;
1999 /* called from elf_out with type == TY_DEBUGSYMLIN */
2000 void dwarf64_output(int type
, void *param
)
2002 int ln
, aa
, inx
, maxln
, soc
;
2003 struct symlininfo
*s
;
2008 s
= (struct symlininfo
*)param
;
2009 /* line number info is only gathered for executable sections */
2010 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2012 /* Check if section index has changed */
2013 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
2015 dwarf64_findsect(s
->section
);
2017 /* do nothing unless line or file has changed */
2020 ln
= currentline
- dwarf_csect
->line
;
2021 aa
= s
->offset
- dwarf_csect
->offset
;
2022 inx
= dwarf_clist
->line
;
2023 plinep
= dwarf_csect
->psaa
;
2024 /* check for file change */
2025 if (!(inx
== dwarf_csect
->file
))
2027 saa_write8(plinep
,DW_LNS_set_file
);
2028 saa_write8(plinep
,inx
);
2029 dwarf_csect
->file
= inx
;
2031 /* check for line change */
2034 /* test if in range of special op code */
2035 maxln
= line_base
+ line_range
;
2036 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
2037 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
2039 saa_write8(plinep
,soc
);
2045 saa_write8(plinep
,DW_LNS_advance_line
);
2046 saa_wleb128s(plinep
,ln
);
2050 saa_write8(plinep
,DW_LNS_advance_pc
);
2051 saa_wleb128u(plinep
,aa
);
2054 dwarf_csect
->line
= currentline
;
2055 dwarf_csect
->offset
= s
->offset
;
2057 /* show change handled */
2063 void dwarf64_generate(void)
2067 struct linelist
*ftentry
;
2068 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
2069 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
2070 struct sectlist
*psect
;
2071 size_t saalen
, linepoff
, totlen
, highaddr
;
2073 /* write epilogues for each line program range */
2074 /* and build aranges section */
2075 paranges
= saa_init(1L);
2076 parangesrel
= saa_init(1L);
2077 saa_write16(paranges
,3); /* dwarf version */
2078 saa_write64(parangesrel
, paranges
->datalen
+4);
2079 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
2080 saa_write64(parangesrel
, 0);
2081 saa_write32(paranges
,0); /* offset into info */
2082 saa_write8(paranges
,8); /* pointer size */
2083 saa_write8(paranges
,0); /* not segmented */
2084 saa_write32(paranges
,0); /* padding */
2085 /* iterate though sectlist entries */
2086 psect
= dwarf_fsect
;
2089 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2091 plinep
= psect
->psaa
;
2092 /* Line Number Program Epilogue */
2093 saa_write8(plinep
,2); /* std op 2 */
2094 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
2095 saa_write8(plinep
,DW_LNS_extended_op
);
2096 saa_write8(plinep
,1); /* operand length */
2097 saa_write8(plinep
,DW_LNE_end_sequence
);
2098 totlen
+= plinep
->datalen
;
2099 /* range table relocation entry */
2100 saa_write64(parangesrel
, paranges
->datalen
+ 4);
2101 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2102 saa_write64(parangesrel
, (uint64_t) 0);
2103 /* range table entry */
2104 saa_write64(paranges
,0x0000); /* range start */
2105 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
2106 highaddr
+= sects
[psect
->section
]->len
;
2107 /* done with this entry */
2108 psect
= psect
->next
;
2110 saa_write64(paranges
,0); /* null address */
2111 saa_write64(paranges
,0); /* null length */
2112 saalen
= paranges
->datalen
;
2113 arangeslen
= saalen
+ 4;
2114 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2115 WRITELONG(pbuf
,saalen
); /* initial length */
2116 saa_rnbytes(paranges
, pbuf
, saalen
);
2119 /* build rela.aranges section */
2120 arangesrellen
= saalen
= parangesrel
->datalen
;
2121 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2122 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2123 saa_free(parangesrel
);
2125 /* build pubnames section */
2126 ppubnames
= saa_init(1L);
2127 saa_write16(ppubnames
,3); /* dwarf version */
2128 saa_write32(ppubnames
,0); /* offset into info */
2129 saa_write32(ppubnames
,0); /* space used in info */
2130 saa_write32(ppubnames
,0); /* end of list */
2131 saalen
= ppubnames
->datalen
;
2132 pubnameslen
= saalen
+ 4;
2133 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2134 WRITELONG(pbuf
,saalen
); /* initial length */
2135 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2136 saa_free(ppubnames
);
2138 /* build info section */
2139 pinfo
= saa_init(1L);
2140 pinforel
= saa_init(1L);
2141 saa_write16(pinfo
,3); /* dwarf version */
2142 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2143 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2144 saa_write64(pinforel
, 0);
2145 saa_write32(pinfo
,0); /* offset into abbrev */
2146 saa_write8(pinfo
,8); /* pointer size */
2147 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2148 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2149 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2150 saa_write64(pinforel
, 0);
2151 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2152 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2153 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2154 saa_write64(pinforel
, 0);
2155 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2156 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2157 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2158 saa_write64(pinforel
, 0);
2159 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2160 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2161 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2162 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2163 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2164 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2165 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2166 saa_write64(pinforel
, 0);
2167 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2168 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2169 saa_write8(pinfo
,0); /* end of entries */
2170 saalen
= pinfo
->datalen
;
2171 infolen
= saalen
+ 4;
2172 infobuf
= pbuf
= nasm_malloc(infolen
);
2173 WRITELONG(pbuf
,saalen
); /* initial length */
2174 saa_rnbytes(pinfo
, pbuf
, saalen
);
2177 /* build rela.info section */
2178 inforellen
= saalen
= pinforel
->datalen
;
2179 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2180 saa_rnbytes(pinforel
, pbuf
, saalen
);
2183 /* build abbrev section */
2184 pabbrev
= saa_init(1L);
2185 saa_write8(pabbrev
,1); /* entry number LEB128u */
2186 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2187 saa_write8(pabbrev
,1); /* has children */
2188 /* the following attributes and forms are all LEB128u values */
2189 saa_write8(pabbrev
,DW_AT_low_pc
);
2190 saa_write8(pabbrev
,DW_FORM_addr
);
2191 saa_write8(pabbrev
,DW_AT_high_pc
);
2192 saa_write8(pabbrev
,DW_FORM_addr
);
2193 saa_write8(pabbrev
,DW_AT_stmt_list
);
2194 saa_write8(pabbrev
,DW_FORM_data4
);
2195 saa_write8(pabbrev
,DW_AT_name
);
2196 saa_write8(pabbrev
,DW_FORM_string
);
2197 saa_write8(pabbrev
,DW_AT_producer
);
2198 saa_write8(pabbrev
,DW_FORM_string
);
2199 saa_write8(pabbrev
,DW_AT_language
);
2200 saa_write8(pabbrev
,DW_FORM_data2
);
2201 saa_write16(pabbrev
,0); /* end of entry */
2202 /* LEB128u usage same as above */
2203 saa_write8(pabbrev
,2); /* entry number */
2204 saa_write8(pabbrev
,DW_TAG_subprogram
);
2205 saa_write8(pabbrev
,0); /* no children */
2206 saa_write8(pabbrev
,DW_AT_low_pc
);
2207 saa_write8(pabbrev
,DW_FORM_addr
);
2208 saa_write8(pabbrev
,DW_AT_frame_base
);
2209 saa_write8(pabbrev
,DW_FORM_data4
);
2210 saa_write16(pabbrev
,0); /* end of entry */
2211 abbrevlen
= saalen
= pabbrev
->datalen
;
2212 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2213 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2216 /* build line section */
2218 plines
= saa_init(1L);
2219 saa_write8(plines
,1); /* Minimum Instruction Length */
2220 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2221 saa_write8(plines
,line_base
); /* Line Base */
2222 saa_write8(plines
,line_range
); /* Line Range */
2223 saa_write8(plines
,opcode_base
); /* Opcode Base */
2224 /* standard opcode lengths (# of LEB128u operands) */
2225 saa_write8(plines
,0); /* Std opcode 1 length */
2226 saa_write8(plines
,1); /* Std opcode 2 length */
2227 saa_write8(plines
,1); /* Std opcode 3 length */
2228 saa_write8(plines
,1); /* Std opcode 4 length */
2229 saa_write8(plines
,1); /* Std opcode 5 length */
2230 saa_write8(plines
,0); /* Std opcode 6 length */
2231 saa_write8(plines
,0); /* Std opcode 7 length */
2232 saa_write8(plines
,0); /* Std opcode 8 length */
2233 saa_write8(plines
,1); /* Std opcode 9 length */
2234 saa_write8(plines
,0); /* Std opcode 10 length */
2235 saa_write8(plines
,0); /* Std opcode 11 length */
2236 saa_write8(plines
,1); /* Std opcode 12 length */
2237 /* Directory Table */
2238 saa_write8(plines
,0); /* End of table */
2239 /* File Name Table */
2240 ftentry
= dwarf_flist
;
2241 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2243 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2244 saa_write8(plines
,0); /* directory LEB128u */
2245 saa_write8(plines
,0); /* time LEB128u */
2246 saa_write8(plines
,0); /* size LEB128u */
2247 ftentry
= ftentry
->next
;
2249 saa_write8(plines
,0); /* End of table */
2250 linepoff
= plines
->datalen
;
2251 linelen
= linepoff
+ totlen
+ 10;
2252 linebuf
= pbuf
= nasm_malloc(linelen
);
2253 WRITELONG(pbuf
,linelen
-4); /* initial length */
2254 WRITESHORT(pbuf
,3); /* dwarf version */
2255 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2256 /* write line header */
2258 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2261 /* concatonate line program ranges */
2263 plinesrel
= saa_init(1L);
2264 psect
= dwarf_fsect
;
2265 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2267 saa_write64(plinesrel
, linepoff
);
2268 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2269 saa_write64(plinesrel
, (uint64_t) 0);
2270 plinep
= psect
->psaa
;
2271 saalen
= plinep
->datalen
;
2272 saa_rnbytes(plinep
, pbuf
, saalen
);
2276 /* done with this entry */
2277 psect
= psect
->next
;
2281 /* build rela.lines section */
2282 linerellen
=saalen
= plinesrel
->datalen
;
2283 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2284 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2285 saa_free(plinesrel
);
2287 /* build frame section */
2289 framebuf
= pbuf
= nasm_malloc(framelen
);
2290 WRITELONG(pbuf
,framelen
-4); /* initial length */
2292 /* build loc section */
2294 locbuf
= pbuf
= nasm_malloc(loclen
);
2295 WRITEDLONG(pbuf
,0); /* null beginning offset */
2296 WRITEDLONG(pbuf
,0); /* null ending offset */
2299 void dwarf64_cleanup(void)
2302 nasm_free(arangesbuf
);
2304 nasm_free(arangesrelbuf
);
2306 nasm_free(pubnamesbuf
);
2310 nasm_free(inforelbuf
);
2312 nasm_free(abbrevbuf
);
2316 nasm_free(linerelbuf
);
2318 nasm_free(framebuf
);
2322 void dwarf64_findfile(const char * fname
)
2325 struct linelist
*match
;
2327 /* return if fname is current file name */
2328 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2329 /* search for match */
2335 match
= dwarf_flist
;
2336 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2338 if (!(strcmp(fname
, match
->filename
)))
2340 dwarf_clist
= match
;
2345 /* add file name to end of list */
2346 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2348 dwarf_clist
->line
= dwarf_numfiles
;
2349 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2350 strcpy(dwarf_clist
->filename
,fname
);
2351 dwarf_clist
->next
= 0;
2352 /* if first entry */
2355 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2356 dwarf_clist
->last
= 0;
2358 /* chain to previous entry */
2361 dwarf_elist
->next
= dwarf_clist
;
2362 dwarf_elist
= dwarf_clist
;
2367 void dwarf64_findsect(const int index
)
2370 struct sectlist
*match
;
2372 /* return if index is current section index */
2373 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2377 /* search for match */
2383 match
= dwarf_fsect
;
2384 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2386 if ((match
->section
== index
))
2388 dwarf_csect
= match
;
2391 match
= match
->next
;
2394 /* add entry to end of list */
2395 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2397 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2398 dwarf_csect
->line
= 1;
2399 dwarf_csect
->offset
= 0;
2400 dwarf_csect
->file
= 1;
2401 dwarf_csect
->section
= index
;
2402 dwarf_csect
->next
= 0;
2403 /* set relocatable address at start of line program */
2404 saa_write8(plinep
,DW_LNS_extended_op
);
2405 saa_write8(plinep
,9); /* operand length */
2406 saa_write8(plinep
,DW_LNE_set_address
);
2407 saa_write64(plinep
,0); /* Start Address */
2408 /* if first entry */
2411 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2412 dwarf_csect
->last
= 0;
2414 /* chain to previous entry */
2417 dwarf_esect
->next
= dwarf_csect
;
2418 dwarf_esect
= dwarf_csect
;