1 /* outelf.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 licence given in the file "Licence"
7 * distributed in the NASM archive.
23 /* Definitions in lieu of elf.h */
25 #define SHT_PROGBITS 1
26 #define SHT_RELA 4 /* Relocation entries with addends */
28 #define SHF_WRITE (1 << 0) /* Writable */
29 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
30 #define SHF_EXECINSTR (1 << 2) /* Executable */
31 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
32 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
33 #define R_X86_64_NONE 0 /* No reloc */
34 #define R_X86_64_64 1 /* Direct 64 bit address */
35 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
36 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
37 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
38 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
39 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
40 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
41 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
42 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
43 #define ET_REL 1 /* Relocatable file */
44 #define EM_X86_64 62 /* AMD x86-64 architecture */
45 #define STT_NOTYPE 0 /* Symbol type is unspecified */
46 #define STT_OBJECT 1 /* Symbol is a data object */
47 #define STT_FUNC 2 /* Symbol is a code object */
48 #define STT_SECTION 3 /* Symbol associated with a section */
49 #define STT_FILE 4 /* Symbol's name is file name */
50 #define STT_COMMON 5 /* Symbol is a common data object */
51 #define STT_TLS 6 /* Symbol is thread-local data object*/
52 #define STT_NUM 7 /* Number of defined types. */
53 typedef uint32_t Elf64_Word
;
54 typedef uint64_t Elf64_Xword
;
55 typedef uint64_t Elf64_Addr
;
56 typedef uint64_t Elf64_Off
;
59 Elf64_Word sh_name
; /* Section name (string tbl index) */
60 Elf64_Word sh_type
; /* Section type */
61 Elf64_Xword sh_flags
; /* Section flags */
62 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
63 Elf64_Off sh_offset
; /* Section file offset */
64 Elf64_Xword sh_size
; /* Section size in bytes */
65 Elf64_Word sh_link
; /* Link to another section */
66 Elf64_Word sh_info
; /* Additional section information */
67 Elf64_Xword sh_addralign
; /* Section alignment */
68 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
77 int64_t address
; /* relative to _start_ of section */
78 int64_t symbol
; /* symbol index */
79 int type
; /* type of relocation */
83 int32_t strpos
; /* string table position of name */
84 int32_t section
; /* section ID of the symbol */
85 int type
; /* symbol type */
86 int other
; /* symbol visibility */
87 int64_t value
; /* address, or COMMON variable align */
88 int32_t size
; /* size of symbol */
89 int32_t globnum
; /* symbol table offset if global */
90 struct Symbol
*next
; /* list of globals in each section */
91 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
92 char *name
; /* used temporarily if in above list */
98 uint32_t len
, size
, nrelocs
;
100 int type
; /* SHT_PROGBITS or SHT_NOBITS */
101 int align
; /* alignment: power of two */
102 uint32_t flags
; /* section flags */
106 struct Reloc
*head
, **tail
;
107 struct Symbol
*gsyms
; /* global symbols in section */
110 #define SECT_DELTA 32
111 static struct Section
**sects
;
112 static int nsects
, sectlen
;
114 #define SHSTR_DELTA 256
115 static char *shstrtab
;
116 static int shstrtablen
, shstrtabsize
;
118 static struct SAA
*syms
;
119 static uint32_t nlocals
, nglobs
;
121 static int32_t def_seg
;
123 static struct RAA
*bsym
;
125 static struct SAA
*strs
;
126 static uint32_t strslen
;
130 static evalfunc evaluate
;
132 static struct Symbol
*fwds
;
134 static char elf_module
[FILENAME_MAX
];
136 extern struct ofmt of_elf64
;
140 #define SYM_GLOBAL 0x10
142 #define STV_DEFAULT 0
143 #define STV_INTERNAL 1
145 #define STV_PROTECTED 3
147 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
149 #define SEG_ALIGN 16 /* alignment of sections in file */
150 #define SEG_ALIGN_1 (SEG_ALIGN-1)
152 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
154 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
155 static struct ELF_SECTDATA
{
160 static int elf_nsect
;
161 static int32_t elf_foffs
;
163 static void elf_write(void);
164 static void elf_sect_write(struct Section
*, const uint8_t *,
166 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
168 static void elf_write_sections(void);
169 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
170 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
171 static void add_sectname(char *, char *);
173 /* this stuff is needed for the stabs debugging format */
174 #define N_SO 0x64 /* ID for main source file */
175 #define N_SOL 0x84 /* ID for sub-source file */
179 #define TY_STABSSYMLIN 0x40 /* ouch */
195 int section
; /* section index */
196 char *name
; /* shallow-copied pointer of section name */
200 struct symlininfo info
;
203 struct linelist
*next
;
204 struct linelist
*last
;
207 static struct linelist
*stabslines
= 0;
208 static int stabs_immcall
= 0;
209 static int currentline
= 0;
210 static int numlinestabs
= 0;
211 static char *stabs_filename
= 0;
212 static int symtabsection
;
213 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
214 static int stablen
, stabstrlen
, stabrellen
;
216 static struct dfmt df_stabs
;
217 static struct Symbol
*lastsym
;
219 void stabs64_init(struct ofmt
*, void *, FILE *, efunc
);
220 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
221 void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
222 void stabs64_directive(const char *, const char *);
223 void stabs64_typevalue(int32_t);
224 void stabs64_output(int, void *);
225 void stabs64_generate(void);
226 void stabs64_cleanup(void);
228 /* end of stabs debugging stuff */
231 * Special section numbers which are used to define ELF special
232 * symbols, which can be used with WRT to provide PIC relocation
235 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
236 static int32_t elf_got_sect
, elf_plt_sect
;
237 static int32_t elf_sym_sect
;
239 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
245 (void)ldef
; /* placate optimisers */
247 nsects
= sectlen
= 0;
248 syms
= saa_init((int32_t)sizeof(struct Symbol
));
249 nlocals
= nglobs
= 0;
252 saa_wbytes(strs
, "\0", 1L);
253 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
254 strslen
= 2 + strlen(elf_module
);
256 shstrtablen
= shstrtabsize
= 0;;
257 add_sectname("", "");
261 elf_gotpc_sect
= seg_alloc();
262 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
264 elf_gotoff_sect
= seg_alloc();
265 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
267 elf_got_sect
= seg_alloc();
268 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
270 elf_plt_sect
= seg_alloc();
271 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
273 elf_sym_sect
= seg_alloc();
274 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
277 def_seg
= seg_alloc();
280 static void elf_cleanup(int debuginfo
)
289 for (i
= 0; i
< nsects
; i
++) {
290 if (sects
[i
]->type
!= SHT_NOBITS
)
291 saa_free(sects
[i
]->data
);
293 saa_free(sects
[i
]->rel
);
294 while (sects
[i
]->head
) {
296 sects
[i
]->head
= sects
[i
]->head
->next
;
304 if (of_elf64
.current_dfmt
) {
305 of_elf64
.current_dfmt
->cleanup();
309 static void add_sectname(char *firsthalf
, char *secondhalf
)
311 int len
= strlen(firsthalf
) + strlen(secondhalf
);
312 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
313 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
314 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
315 strcat(shstrtab
+ shstrtablen
, secondhalf
);
316 shstrtablen
+= len
+ 1;
319 static int elf_make_section(char *name
, int type
, int flags
, int align
)
323 s
= nasm_malloc(sizeof(*s
));
325 if (type
!= SHT_NOBITS
)
326 s
->data
= saa_init(1L);
329 s
->len
= s
->size
= 0;
331 if (!strcmp(name
, ".text"))
334 s
->index
= seg_alloc();
335 add_sectname("", name
);
336 s
->name
= nasm_malloc(1 + strlen(name
));
337 strcpy(s
->name
, name
);
343 if (nsects
>= sectlen
)
345 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
351 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
354 unsigned flags_and
, flags_or
;
358 * Default is 64 bits.
366 while (*p
&& !isspace(*p
))
370 flags_and
= flags_or
= type
= align
= 0;
372 while (*p
&& isspace(*p
))
376 while (*p
&& !isspace(*p
))
380 while (*p
&& isspace(*p
))
383 if (!nasm_strnicmp(q
, "align=", 6)) {
387 if ((align
- 1) & align
) { /* means it's not a power of two */
388 error(ERR_NONFATAL
, "section alignment %d is not"
389 " a power of two", align
);
392 } else if (!nasm_stricmp(q
, "alloc")) {
393 flags_and
|= SHF_ALLOC
;
394 flags_or
|= SHF_ALLOC
;
395 } else if (!nasm_stricmp(q
, "noalloc")) {
396 flags_and
|= SHF_ALLOC
;
397 flags_or
&= ~SHF_ALLOC
;
398 } else if (!nasm_stricmp(q
, "exec")) {
399 flags_and
|= SHF_EXECINSTR
;
400 flags_or
|= SHF_EXECINSTR
;
401 } else if (!nasm_stricmp(q
, "noexec")) {
402 flags_and
|= SHF_EXECINSTR
;
403 flags_or
&= ~SHF_EXECINSTR
;
404 } else if (!nasm_stricmp(q
, "write")) {
405 flags_and
|= SHF_WRITE
;
406 flags_or
|= SHF_WRITE
;
407 } else if (!nasm_stricmp(q
, "nowrite")) {
408 flags_and
|= SHF_WRITE
;
409 flags_or
&= ~SHF_WRITE
;
410 } else if (!nasm_stricmp(q
, "progbits")) {
412 } else if (!nasm_stricmp(q
, "nobits")) {
417 if (!strcmp(name
, ".comment") ||
418 !strcmp(name
, ".shstrtab") ||
419 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
420 error(ERR_NONFATAL
, "attempt to redefine reserved section"
425 for (i
= 0; i
< nsects
; i
++)
426 if (!strcmp(name
, sects
[i
]->name
))
429 if (!strcmp(name
, ".text"))
430 i
= elf_make_section(name
, SHT_PROGBITS
,
431 SHF_ALLOC
| SHF_EXECINSTR
, 16);
432 else if (!strcmp(name
, ".rodata"))
433 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
434 else if (!strcmp(name
, ".data"))
435 i
= elf_make_section(name
, SHT_PROGBITS
,
436 SHF_ALLOC
| SHF_WRITE
, 4);
437 else if (!strcmp(name
, ".bss"))
438 i
= elf_make_section(name
, SHT_NOBITS
,
439 SHF_ALLOC
| SHF_WRITE
, 4);
441 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
443 sects
[i
]->type
= type
;
445 sects
[i
]->align
= align
;
446 sects
[i
]->flags
&= ~flags_and
;
447 sects
[i
]->flags
|= flags_or
;
448 } else if (pass
== 1) {
449 if ((type
&& sects
[i
]->type
!= type
)
450 || (align
&& sects
[i
]->align
!= align
)
451 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
452 error(ERR_WARNING
, "incompatible section attributes ignored on"
453 " redeclaration of section `%s'", name
);
456 return sects
[i
]->index
;
459 static void elf_deflabel(char *name
, int32_t segment
, int32_t offset
,
460 int is_global
, char *special
)
464 bool special_used
= false;
466 #if defined(DEBUG) && DEBUG>2
468 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
469 name
, segment
, offset
, is_global
, special
);
471 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
473 * This is a NASM special symbol. We never allow it into
474 * the ELF symbol table, even if it's a valid one. If it
475 * _isn't_ a valid one, we should barf immediately.
477 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
478 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
479 strcmp(name
, "..sym"))
480 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
484 if (is_global
== 3) {
487 * Fix up a forward-reference symbol size from the first
490 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
491 if (!strcmp((*s
)->name
, name
)) {
492 struct tokenval tokval
;
496 while (*p
&& !isspace(*p
))
498 while (*p
&& isspace(*p
))
502 tokval
.t_type
= TOKEN_INVALID
;
503 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
506 error(ERR_NONFATAL
, "cannot use relocatable"
507 " expression as symbol size");
509 (*s
)->size
= reloc_value(e
);
513 * Remove it from the list of unresolved sizes.
515 nasm_free((*s
)->name
);
519 return; /* it wasn't an important one */
522 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
523 strslen
+= 1 + strlen(name
);
525 lastsym
= sym
= saa_wstruct(syms
);
528 sym
->type
= is_global
? SYM_GLOBAL
: 0;
529 sym
->other
= STV_DEFAULT
;
531 if (segment
== NO_SEG
)
532 sym
->section
= SHN_ABS
;
535 sym
->section
= SHN_UNDEF
;
536 if (nsects
== 0 && segment
== def_seg
) {
538 if (segment
!= elf_section_names(".text", 2, &tempint
))
540 "strange segment conditions in ELF driver");
541 sym
->section
= nsects
;
543 for (i
= 0; i
< nsects
; i
++)
544 if (segment
== sects
[i
]->index
) {
545 sym
->section
= i
+ 1;
551 if (is_global
== 2) {
554 sym
->section
= SHN_COMMON
;
556 * We have a common variable. Check the special text to see
557 * if it's a valid number and power of two; if so, store it
558 * as the alignment for the common variable.
562 sym
->value
= readnum(special
, &err
);
564 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
565 " valid number", special
);
566 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
567 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
568 " power of two", special
);
572 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
574 if (sym
->type
== SYM_GLOBAL
) {
576 * If sym->section == SHN_ABS, then the first line of the
577 * else section would cause a core dump, because its a reference
578 * beyond the end of the section array.
579 * This behaviour is exhibited by this code:
582 * To avoid such a crash, such requests are silently discarded.
583 * This may not be the best solution.
585 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
586 bsym
= raa_write(bsym
, segment
, nglobs
);
587 } else if (sym
->section
!= SHN_ABS
) {
589 * This is a global symbol; so we must add it to the linked
590 * list of global symbols in its section. We'll push it on
591 * the beginning of the list, because it doesn't matter
592 * much which end we put it on and it's easier like this.
594 * In addition, we check the special text for symbol
595 * type and size information.
597 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
598 sects
[sym
->section
- 1]->gsyms
= sym
;
601 int n
= strcspn(special
, " \t");
603 if (!nasm_strnicmp(special
, "function", n
))
604 sym
->type
|= STT_FUNC
;
605 else if (!nasm_strnicmp(special
, "data", n
) ||
606 !nasm_strnicmp(special
, "object", n
))
607 sym
->type
|= STT_OBJECT
;
608 else if (!nasm_strnicmp(special
, "notype", n
))
609 sym
->type
|= STT_NOTYPE
;
611 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
615 while (isspace(*special
))
618 n
= strcspn(special
, " \t");
619 if (!nasm_strnicmp(special
, "default", n
))
620 sym
->other
= STV_DEFAULT
;
621 else if (!nasm_strnicmp(special
, "internal", n
))
622 sym
->other
= STV_INTERNAL
;
623 else if (!nasm_strnicmp(special
, "hidden", n
))
624 sym
->other
= STV_HIDDEN
;
625 else if (!nasm_strnicmp(special
, "protected", n
))
626 sym
->other
= STV_PROTECTED
;
633 struct tokenval tokval
;
636 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
638 while (special
[n
] && isspace(special
[n
]))
641 * We have a size expression; attempt to
645 stdscan_bufptr
= special
+ n
;
646 tokval
.t_type
= TOKEN_INVALID
;
647 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
652 sym
->name
= nasm_strdup(name
);
655 error(ERR_NONFATAL
, "cannot use relocatable"
656 " expression as symbol size");
658 sym
->size
= reloc_value(e
);
660 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
665 sym
->globnum
= nglobs
;
670 if (special
&& !special_used
)
671 error(ERR_NONFATAL
, "no special symbol features supported here");
674 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
678 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
679 sect
->tail
= &r
->next
;
682 r
->address
= sect
->len
;
683 if (segment
== NO_SEG
)
688 for (i
= 0; i
< nsects
; i
++)
689 if (segment
== sects
[i
]->index
)
692 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
700 * This routine deals with ..got and ..sym relocations: the more
701 * complicated kinds. In shared-library writing, some relocations
702 * with respect to global symbols must refer to the precise symbol
703 * rather than referring to an offset from the base of the section
704 * _containing_ the symbol. Such relocations call to this routine,
705 * which searches the symbol list for the symbol in question.
707 * R_386_GOT32 references require the _exact_ symbol address to be
708 * used; R_386_32 references can be at an offset from the symbol.
709 * The boolean argument `exact' tells us this.
711 * Return value is the adjusted value of `addr', having become an
712 * offset from the symbol rather than the section. Should always be
713 * zero when returning from an exact call.
715 * Limitation: if you define two symbols at the same place,
716 * confusion will occur.
718 * Inefficiency: we search, currently, using a linked list which
719 * isn't even necessarily sorted.
721 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
722 int32_t segment
, int64_t offset
,
723 int type
, bool exact
)
727 struct Symbol
*sym
, *sm
;
731 * First look up the segment/offset pair and find a global
732 * symbol corresponding to it. If it's not one of our segments,
733 * then it must be an external symbol, in which case we're fine
734 * doing a normal elf_add_reloc after first sanity-checking
735 * that the offset from the symbol is zero.
738 for (i
= 0; i
< nsects
; i
++)
739 if (segment
== sects
[i
]->index
) {
744 if (exact
&& offset
!= 0)
745 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
746 " for this reference");
748 elf_add_reloc(sect
, segment
, type
);
754 * Find a symbol pointing _exactly_ at this one.
756 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
757 if (sym
->value
== offset
)
761 * Find the nearest symbol below this one.
764 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
765 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
769 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
770 " for this reference");
774 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
775 sect
->tail
= &r
->next
;
778 r
->address
= sect
->len
;
779 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
784 return offset
- sym
->value
;
787 static void elf_out(int32_t segto
, const void *data
, uint32_t type
,
788 int32_t segment
, int32_t wrt
)
791 int32_t realbytes
= type
& OUT_SIZMASK
;
793 uint8_t mydata
[16], *p
;
795 static struct symlininfo sinfo
;
799 #if defined(DEBUG) && DEBUG>2
801 " elf_out type: %x seg: %d bytes: %x data: %x\n",
802 (type
>> 24), segment
, realbytes
, *(int32_t *)data
);
806 * handle absolute-assembly (structure definitions)
808 if (segto
== NO_SEG
) {
809 if (type
!= OUT_RESERVE
)
810 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
816 for (i
= 0; i
< nsects
; i
++)
817 if (segto
== sects
[i
]->index
) {
822 int tempint
; /* ignored */
823 if (segto
!= elf_section_names(".text", 2, &tempint
))
824 error(ERR_PANIC
, "strange segment conditions in ELF driver");
826 s
= sects
[nsects
- 1];
831 /* again some stabs debugging stuff */
832 if (of_elf64
.current_dfmt
) {
833 sinfo
.offset
= s
->len
;
835 sinfo
.name
= s
->name
;
836 of_elf64
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
838 /* end of debugging stuff */
840 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
841 error(ERR_WARNING
, "attempt to initialize memory in"
842 " BSS section `%s': ignored", s
->name
);
843 if (type
== OUT_REL2ADR
)
845 else if (type
== OUT_REL4ADR
)
851 if (type
== OUT_RESERVE
) {
852 if (s
->type
== SHT_PROGBITS
) {
853 error(ERR_WARNING
, "uninitialized space declared in"
854 " non-BSS section `%s': zeroing", s
->name
);
855 elf_sect_write(s
, NULL
, realbytes
);
858 } else if (type
== OUT_RAWDATA
) {
859 if (segment
!= NO_SEG
)
860 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
861 elf_sect_write(s
, data
, realbytes
);
862 } else if (type
== OUT_ADDRESS
) {
864 addr
= *(int64_t *)data
;
865 if (segment
!= NO_SEG
) {
867 error(ERR_NONFATAL
, "ELF format does not support"
868 " segment base references");
873 elf_add_reloc(s
, segment
, R_X86_64_16
);
876 elf_add_reloc(s
, segment
, R_X86_64_32
);
879 elf_add_reloc(s
, segment
, R_X86_64_64
);
882 error(ERR_PANIC
, "internal error elf64-hpa-871");
885 } else if (wrt
== elf_gotpc_sect
+ 1) {
887 * The user will supply GOT relative to $$. ELF
888 * will let us have GOT relative to $. So we
889 * need to fix up the data item by $-$$.
892 elf_add_reloc(s
, segment
, R_X86_64_GOTPCREL
);
893 } else if (wrt
== elf_gotoff_sect
+ 1) {
894 elf_add_reloc(s
, segment
, R_X86_64_GOTTPOFF
);
895 } else if (wrt
== elf_got_sect
+ 1) {
896 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
897 R_X86_64_GOT32
, true);
898 } else if (wrt
== elf_sym_sect
+ 1) {
902 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
906 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
910 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
914 error(ERR_PANIC
, "internal error elf64-hpa-903");
917 } else if (wrt
== elf_plt_sect
+ 1) {
918 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
919 "relative PLT references");
921 error(ERR_NONFATAL
, "ELF format does not support this"
923 wrt
= NO_SEG
; /* we can at least _try_ to continue */
931 if (realbytes
!= 8 && realbytes
!= 4 && segment
!= NO_SEG
) {
933 "Unsupported non-64-bit ELF relocation");
935 if (realbytes
== 4) WRITELONG(p
, addr
);
936 else WRITEDLONG(p
, (int64_t)addr
);
938 elf_sect_write(s
, mydata
, realbytes
);
939 } else if (type
== OUT_REL2ADR
) {
940 if (segment
== segto
)
941 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
942 if (segment
!= NO_SEG
&& segment
% 2) {
943 error(ERR_NONFATAL
, "ELF format does not support"
944 " segment base references");
947 elf_add_reloc(s
, segment
, R_X86_64_PC16
);
950 "Unsupported non-32-bit ELF relocation [2]");
954 WRITESHORT(p
, *(int32_t *)data
- realbytes
);
955 elf_sect_write(s
, mydata
, 2L);
956 } else if (type
== OUT_REL4ADR
) {
957 if (segment
== segto
)
958 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
959 if (segment
!= NO_SEG
&& segment
% 2) {
960 error(ERR_NONFATAL
, "ELF format does not support"
961 " segment base references");
964 elf_add_reloc(s
, segment
, R_X86_64_PC32
);
965 } else if (wrt
== elf_plt_sect
+ 1) {
966 elf_add_reloc(s
, segment
, R_X86_64_PLT32
);
967 } else if (wrt
== elf_gotpc_sect
+ 1 ||
968 wrt
== elf_gotoff_sect
+ 1 ||
969 wrt
== elf_got_sect
+ 1) {
970 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
971 "relative GOT references");
973 error(ERR_NONFATAL
, "ELF format does not support this"
975 wrt
= NO_SEG
; /* we can at least _try_ to continue */
979 WRITELONG(p
, *(int32_t *)data
- realbytes
);
980 elf_sect_write(s
, mydata
, 4L);
984 static void elf_write(void)
986 int nsections
, align
;
994 int32_t symtablen
, symtablocal
;
997 * Work out how many sections we will have. We have SHN_UNDEF,
998 * then the flexible user sections, then the four fixed
999 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1000 * then optionally relocation sections for the user sections.
1002 if (of_elf64
.current_dfmt
== &df_stabs
)
1005 nsections
= 5; /* SHN_UNDEF and the fixed ones */
1007 add_sectname("", ".comment");
1008 add_sectname("", ".shstrtab");
1009 add_sectname("", ".symtab");
1010 add_sectname("", ".strtab");
1011 for (i
= 0; i
< nsects
; i
++) {
1012 nsections
++; /* for the section itself */
1013 if (sects
[i
]->head
) {
1014 nsections
++; /* for its relocations without addends*/
1015 add_sectname(".rela", sects
[i
]->name
);
1019 if (of_elf64
.current_dfmt
== &df_stabs
) {
1020 /* in case the debug information is wanted, just add these three sections... */
1021 add_sectname("", ".stab");
1022 add_sectname("", ".stabstr");
1023 add_sectname(".rel", ".stab");
1031 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1034 * Output the ELF header.
1036 fwrite("\177ELF\2\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
1037 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1038 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1039 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1040 fwriteint64_t(0L, elffp
); /* no entry point */
1041 fwriteint64_t(0L, elffp
); /* no program header table */
1042 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1043 * ELF header plus alignment */
1044 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1045 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1046 fwriteint16_t(0, elffp
); /* no program header table, again */
1047 fwriteint16_t(0, elffp
); /* still no program header table */
1048 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1049 fwriteint16_t(nsections
, elffp
); /* number of sections */
1050 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1051 * section header table */
1054 * Build the symbol table and relocation tables.
1056 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1057 for (i
= 0; i
< nsects
; i
++)
1059 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1063 * Now output the section header table.
1066 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1067 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1070 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1072 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1073 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1075 for (i
= 0; i
< nsects
; i
++) {
1076 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1077 (sects
[i
]->type
== SHT_PROGBITS
?
1078 sects
[i
]->data
: NULL
), true,
1079 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1081 scount
++; /* dito */
1083 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1084 scount
++; /* dito */
1086 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1087 scount
++; /* dito */
1089 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1090 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1092 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1093 for (i
= 0; i
< nsects
; i
++)
1094 if (sects
[i
]->head
) {
1096 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, true,
1097 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1099 if (of_elf64
.current_dfmt
== &df_stabs
) {
1100 /* for debugging information, create the last three sections
1101 which are the .stab , .stabstr and .rel.stab sections respectively */
1103 /* this function call creates the stab sections in memory */
1106 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1108 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1109 nsections
- 2, 0, 4, 12);
1112 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1113 stabstrlen
, 0, 0, 4, 0);
1116 /* link -> symtable info -> section to refer to */
1117 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1118 stabrellen
, symtabsection
, nsections
- 3, 4,
1122 fwrite(align_str
, align
, 1, elffp
);
1125 * Now output the sections.
1127 elf_write_sections();
1129 nasm_free(elf_sects
);
1133 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1135 struct SAA
*s
= saa_init(1L);
1137 uint8_t entry
[24], *p
;
1143 * First, an all-zeros entry, required by the ELF spec.
1145 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1150 * Next, an entry for the file name.
1153 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1154 WRITESHORT(p
, STT_FILE
); /* type FILE */
1155 WRITESHORT(p
, SHN_ABS
);
1156 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1157 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1158 saa_wbytes(s
, entry
, 24L);
1163 * Now some standard symbols defining the segments, for relocation
1166 for (i
= 1; i
<= nsects
; i
++) {
1168 WRITELONG(p
, 0); /* no symbol name */
1169 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1170 WRITESHORT(p
, i
); /* section id */
1171 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1172 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1173 saa_wbytes(s
, entry
, 24L);
1179 * Now the other local symbols.
1182 while ((sym
= saa_rstruct(syms
))) {
1183 if (sym
->type
& SYM_GLOBAL
)
1186 WRITELONG(p
, sym
->strpos
);
1187 WRITECHAR(p
, sym
->type
); /* type and binding */
1188 WRITECHAR(p
, sym
->other
); /* visibility */
1189 WRITESHORT(p
, sym
->section
);
1190 WRITEDLONG(p
, (int64_t)sym
->value
);
1191 WRITEDLONG(p
, (int64_t)sym
->size
);
1192 saa_wbytes(s
, entry
, 24L);
1198 * Now the global symbols.
1201 while ((sym
= saa_rstruct(syms
))) {
1202 if (!(sym
->type
& SYM_GLOBAL
))
1205 WRITELONG(p
, sym
->strpos
);
1206 WRITECHAR(p
, sym
->type
); /* type and binding */
1207 WRITECHAR(p
, sym
->other
); /* visibility */
1208 WRITESHORT(p
, sym
->section
);
1209 WRITEDLONG(p
, (int64_t)sym
->value
);
1210 WRITEDLONG(p
, (int64_t)sym
->size
);
1211 saa_wbytes(s
, entry
, 24L);
1218 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1221 uint8_t *p
, entry
[24];
1230 int64_t sym
= r
->symbol
;
1232 if (sym
>= GLOBAL_TEMP_BASE
)
1233 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1236 WRITEDLONG(p
, r
->address
);
1237 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1238 WRITEDLONG(p
, (uint64_t) 0);
1239 saa_wbytes(s
, entry
, 24L);
1248 static void elf_section_header(int name
, int type
, int flags
,
1249 void *data
, bool is_saa
, int32_t datalen
,
1250 int link
, int info
, int align
, int eltsize
)
1252 elf_sects
[elf_nsect
].data
= data
;
1253 elf_sects
[elf_nsect
].len
= datalen
;
1254 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1257 fwriteint32_t((int32_t)name
, elffp
);
1258 fwriteint32_t((int32_t)type
, elffp
);
1259 fwriteint64_t((int64_t)flags
, elffp
);
1260 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1261 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1262 fwriteint64_t(datalen
, elffp
);
1264 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1265 fwriteint32_t((int32_t)link
, elffp
);
1266 fwriteint32_t((int32_t)info
, elffp
);
1267 fwriteint64_t((int64_t)align
, elffp
);
1268 fwriteint64_t((int64_t)eltsize
, elffp
);
1271 static void elf_write_sections(void)
1274 for (i
= 0; i
< elf_nsect
; i
++)
1275 if (elf_sects
[i
].data
) {
1276 int32_t len
= elf_sects
[i
].len
;
1277 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1278 int32_t align
= reallen
- len
;
1279 if (elf_sects
[i
].is_saa
)
1280 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1282 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1283 fwrite(align_str
, align
, 1, elffp
);
1287 static void elf_sect_write(struct Section
*sect
,
1288 const uint8_t *data
, uint32_t len
)
1290 saa_wbytes(sect
->data
, data
, len
);
1294 static int32_t elf_segbase(int32_t segment
)
1299 static int elf_directive(char *directive
, char *value
, int pass
)
1307 static void elf_filename(char *inname
, char *outname
, efunc error
)
1309 strcpy(elf_module
, inname
);
1310 standard_extension(inname
, outname
, ".o", error
);
1313 static const char *elf_stdmac
[] = {
1314 "%define __SECT__ [section .text]",
1315 "%macro __NASM_CDecl__ 1",
1320 static int elf_set_info(enum geninfo type
, char **val
)
1327 static struct dfmt df_stabs
= {
1328 "ELF64 (X86_64) stabs debug format for Linux",
1339 struct dfmt
*elf64_debugs_arr
[2] = { &df_stabs
, NULL
};
1341 struct ofmt of_elf64
= {
1342 "ELF64 (x86_64) object files (e.g. Linux)",
1359 /* again, the stabs debugging stuff (code) */
1361 void stabs64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1369 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1373 if (!stabs_filename
) {
1374 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1375 strcpy(stabs_filename
, filename
);
1377 if (strcmp(stabs_filename
, filename
)) {
1378 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1379 in fact, this leak comes in quite handy to maintain a list of files
1380 encountered so far in the symbol lines... */
1382 /* why not nasm_free(stabs_filename); we're done with the old one */
1384 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1385 strcpy(stabs_filename
, filename
);
1389 currentline
= linenumber
;
1392 void stabs64_deflabel(char *name
, int32_t segment
, int32_t offset
, int is_global
,
1402 void stabs64_directive(const char *directive
, const char *params
)
1408 void stabs64_typevalue(int32_t type
)
1410 int32_t stype
, ssize
;
1411 switch (TYM_TYPE(type
)) {
1450 stype
= STT_SECTION
;
1465 if (stype
== STT_OBJECT
&& !lastsym
->type
) {
1466 lastsym
->size
= ssize
;
1467 lastsym
->type
= stype
;
1471 void stabs64_output(int type
, void *param
)
1473 struct symlininfo
*s
;
1474 struct linelist
*el
;
1475 if (type
== TY_STABSSYMLIN
) {
1476 if (stabs_immcall
) {
1477 s
= (struct symlininfo
*)param
;
1478 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1479 return; /* we are only interested in the text stuff */
1481 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1482 el
->info
.offset
= s
->offset
;
1483 el
->info
.section
= s
->section
;
1484 el
->info
.name
= s
->name
;
1485 el
->line
= currentline
;
1486 el
->filename
= stabs_filename
;
1489 stabslines
->last
->next
= el
;
1490 stabslines
->last
= el
;
1493 stabslines
->last
= el
;
1500 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1502 WRITELONG(p,n_strx); \
1503 WRITECHAR(p,n_type); \
1504 WRITECHAR(p,n_other); \
1505 WRITESHORT(p,n_desc); \
1506 WRITELONG(p,n_value); \
1509 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1511 void stabs64_generate(void)
1513 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1514 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1518 struct linelist
*ptr
;
1522 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1523 for (i
= 0; i
< numlinestabs
; i
++)
1527 if (numfiles
== 0) {
1528 allfiles
[0] = ptr
->filename
;
1531 for (i
= 0; i
< numfiles
; i
++) {
1532 if (!strcmp(allfiles
[i
], ptr
->filename
))
1535 if (i
>= numfiles
) {
1536 allfiles
[i
] = ptr
->filename
;
1543 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1544 for (i
= 0; i
< numfiles
; i
++) {
1545 fileidx
[i
] = strsize
;
1546 strsize
+= strlen(allfiles
[i
]) + 1;
1549 for (i
= 0; i
< numfiles
; i
++) {
1550 if (!strcmp(allfiles
[i
], elf_module
)) {
1556 /* worst case size of the stab buffer would be:
1557 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1560 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1561 sizeof(struct stabentry
));
1563 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1565 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1568 for (i
= 0; i
< numfiles
; i
++) {
1569 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1573 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1580 /* this is the first stab, its strx points to the filename of the
1581 the source-file, the n_desc field should be set to the number
1584 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1586 /* this is the stab for the main source file */
1587 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1589 /* relocation table entry */
1591 /* Since the symbol table has two entries before */
1592 /* the section symbols, the index in the info.section */
1593 /* member must be adjusted by adding 2 */
1595 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1596 WRITELONG(rptr
, R_X86_64_32
);
1597 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1600 currfile
= mainfileindex
;
1604 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1605 /* oops file has changed... */
1606 for (i
= 0; i
< numfiles
; i
++)
1607 if (!strcmp(allfiles
[i
], ptr
->filename
))
1610 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1614 /* relocation table entry */
1616 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1617 WRITELONG(rptr
, R_X86_64_32
);
1618 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1621 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1624 /* relocation table entry */
1626 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1627 WRITELONG(rptr
, R_X86_64_32
);
1628 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1634 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1636 nasm_free(allfiles
);
1639 stablen
= (sptr
- sbuf
);
1640 stabrellen
= (rptr
- rbuf
);
1646 void stabs64_cleanup(void)
1648 struct linelist
*ptr
, *del
;
1660 nasm_free(stabrelbuf
);
1662 nasm_free(stabstrbuf
);