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 */
101 uint32_t type
; /* SHT_PROGBITS or SHT_NOBITS */
102 uint64_t align
; /* alignment: power of two */
103 uint64_t flags
; /* section flags */
107 struct Reloc
*head
, **tail
;
108 struct Symbol
*gsyms
; /* global symbols in section */
111 #define SECT_DELTA 32
112 static struct Section
**sects
;
113 static int nsects
, sectlen
;
115 #define SHSTR_DELTA 256
116 static char *shstrtab
;
117 static int shstrtablen
, shstrtabsize
;
119 static struct SAA
*syms
;
120 static uint32_t nlocals
, nglobs
;
122 static int32_t def_seg
;
124 static struct RAA
*bsym
;
126 static struct SAA
*strs
;
127 static uint32_t strslen
;
131 static evalfunc evaluate
;
133 static struct Symbol
*fwds
;
135 static char elf_module
[FILENAME_MAX
];
137 extern struct ofmt of_elf64
;
141 #define SYM_GLOBAL 0x10
143 #define STV_DEFAULT 0
144 #define STV_INTERNAL 1
146 #define STV_PROTECTED 3
148 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
150 #define SEG_ALIGN 16 /* alignment of sections in file */
151 #define SEG_ALIGN_1 (SEG_ALIGN-1)
153 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
155 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
156 static struct ELF_SECTDATA
{
161 static int elf_nsect
;
162 static int64_t elf_foffs
;
164 static void elf_write(void);
165 static void elf_sect_write(struct Section
*, const uint8_t *,
167 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
169 static void elf_write_sections(void);
170 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
171 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
172 static void add_sectname(char *, char *);
174 /* this stuff is needed for the stabs debugging format */
175 #define N_SO 0x64 /* ID for main source file */
176 #define N_SOL 0x84 /* ID for sub-source file */
180 #define TY_STABSSYMLIN 0x40 /* ouch */
196 int section
; /* section index */
197 char *name
; /* shallow-copied pointer of section name */
201 struct symlininfo info
;
204 struct linelist
*next
;
205 struct linelist
*last
;
208 static struct linelist
*stabslines
= 0;
209 static int stabs_immcall
= 0;
210 static int currentline
= 0;
211 static int numlinestabs
= 0;
212 static char *stabs_filename
= 0;
213 static int symtabsection
;
214 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
215 static int stablen
, stabstrlen
, stabrellen
;
217 static struct dfmt df_stabs
;
218 static struct Symbol
*lastsym
;
220 void stabs64_init(struct ofmt
*, void *, FILE *, efunc
);
221 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
222 void stabs64_deflabel(char *, int32_t, int64_t, int, char *);
223 void stabs64_directive(const char *, const char *);
224 void stabs64_typevalue(int32_t);
225 void stabs64_output(int, void *);
226 void stabs64_generate(void);
227 void stabs64_cleanup(void);
229 /* end of stabs debugging stuff */
232 * Special section numbers which are used to define ELF special
233 * symbols, which can be used with WRT to provide PIC relocation
236 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
237 static int32_t elf_got_sect
, elf_plt_sect
;
238 static int32_t elf_sym_sect
;
240 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
246 (void)ldef
; /* placate optimisers */
248 nsects
= sectlen
= 0;
249 syms
= saa_init((int32_t)sizeof(struct Symbol
));
250 nlocals
= nglobs
= 0;
253 saa_wbytes(strs
, "\0", 1L);
254 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
255 strslen
= 2 + strlen(elf_module
);
257 shstrtablen
= shstrtabsize
= 0;;
258 add_sectname("", "");
262 elf_gotpc_sect
= seg_alloc();
263 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
265 elf_gotoff_sect
= seg_alloc();
266 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
268 elf_got_sect
= seg_alloc();
269 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
271 elf_plt_sect
= seg_alloc();
272 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
274 elf_sym_sect
= seg_alloc();
275 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
278 def_seg
= seg_alloc();
281 static void elf_cleanup(int debuginfo
)
290 for (i
= 0; i
< nsects
; i
++) {
291 if (sects
[i
]->type
!= SHT_NOBITS
)
292 saa_free(sects
[i
]->data
);
294 saa_free(sects
[i
]->rel
);
295 while (sects
[i
]->head
) {
297 sects
[i
]->head
= sects
[i
]->head
->next
;
305 if (of_elf64
.current_dfmt
) {
306 of_elf64
.current_dfmt
->cleanup();
310 static void add_sectname(char *firsthalf
, char *secondhalf
)
312 int len
= strlen(firsthalf
) + strlen(secondhalf
);
313 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
314 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
315 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
316 strcat(shstrtab
+ shstrtablen
, secondhalf
);
317 shstrtablen
+= len
+ 1;
320 static int elf_make_section(char *name
, int type
, int flags
, int align
)
324 s
= nasm_malloc(sizeof(*s
));
326 if (type
!= SHT_NOBITS
)
327 s
->data
= saa_init(1L);
330 s
->len
= s
->size
= 0;
332 if (!strcmp(name
, ".text"))
335 s
->index
= seg_alloc();
336 add_sectname("", name
);
337 s
->name
= nasm_malloc(1 + strlen(name
));
338 strcpy(s
->name
, name
);
344 if (nsects
>= sectlen
)
346 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
352 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
355 unsigned flags_and
, flags_or
;
356 uint64_t type
, align
;
360 * Default is 64 bits.
368 while (*p
&& !isspace(*p
))
372 flags_and
= flags_or
= type
= align
= 0;
374 while (*p
&& isspace(*p
))
378 while (*p
&& !isspace(*p
))
382 while (*p
&& isspace(*p
))
385 if (!nasm_strnicmp(q
, "align=", 6)) {
389 if ((align
- 1) & align
) { /* means it's not a power of two */
390 error(ERR_NONFATAL
, "section alignment %d is not"
391 " a power of two", align
);
394 } else if (!nasm_stricmp(q
, "alloc")) {
395 flags_and
|= SHF_ALLOC
;
396 flags_or
|= SHF_ALLOC
;
397 } else if (!nasm_stricmp(q
, "noalloc")) {
398 flags_and
|= SHF_ALLOC
;
399 flags_or
&= ~SHF_ALLOC
;
400 } else if (!nasm_stricmp(q
, "exec")) {
401 flags_and
|= SHF_EXECINSTR
;
402 flags_or
|= SHF_EXECINSTR
;
403 } else if (!nasm_stricmp(q
, "noexec")) {
404 flags_and
|= SHF_EXECINSTR
;
405 flags_or
&= ~SHF_EXECINSTR
;
406 } else if (!nasm_stricmp(q
, "write")) {
407 flags_and
|= SHF_WRITE
;
408 flags_or
|= SHF_WRITE
;
409 } else if (!nasm_stricmp(q
, "nowrite")) {
410 flags_and
|= SHF_WRITE
;
411 flags_or
&= ~SHF_WRITE
;
412 } else if (!nasm_stricmp(q
, "progbits")) {
414 } else if (!nasm_stricmp(q
, "nobits")) {
419 if (!strcmp(name
, ".comment") ||
420 !strcmp(name
, ".shstrtab") ||
421 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
422 error(ERR_NONFATAL
, "attempt to redefine reserved section"
427 for (i
= 0; i
< nsects
; i
++)
428 if (!strcmp(name
, sects
[i
]->name
))
431 if (!strcmp(name
, ".text"))
432 i
= elf_make_section(name
, SHT_PROGBITS
,
433 SHF_ALLOC
| SHF_EXECINSTR
, 16);
434 else if (!strcmp(name
, ".rodata"))
435 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
436 else if (!strcmp(name
, ".data"))
437 i
= elf_make_section(name
, SHT_PROGBITS
,
438 SHF_ALLOC
| SHF_WRITE
, 4);
439 else if (!strcmp(name
, ".bss"))
440 i
= elf_make_section(name
, SHT_NOBITS
,
441 SHF_ALLOC
| SHF_WRITE
, 4);
443 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
445 sects
[i
]->type
= type
;
447 sects
[i
]->align
= align
;
448 sects
[i
]->flags
&= ~flags_and
;
449 sects
[i
]->flags
|= flags_or
;
450 } else if (pass
== 1) {
451 if ((type
&& sects
[i
]->type
!= type
)
452 || (align
&& sects
[i
]->align
!= align
)
453 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
454 error(ERR_WARNING
, "incompatible section attributes ignored on"
455 " redeclaration of section `%s'", name
);
458 return sects
[i
]->index
;
461 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
462 int is_global
, char *special
)
466 bool special_used
= false;
468 #if defined(DEBUG) && DEBUG>2
470 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
471 name
, segment
, offset
, is_global
, special
);
473 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
475 * This is a NASM special symbol. We never allow it into
476 * the ELF symbol table, even if it's a valid one. If it
477 * _isn't_ a valid one, we should barf immediately.
479 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
480 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
481 strcmp(name
, "..sym"))
482 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
486 if (is_global
== 3) {
489 * Fix up a forward-reference symbol size from the first
492 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
493 if (!strcmp((*s
)->name
, name
)) {
494 struct tokenval tokval
;
498 while (*p
&& !isspace(*p
))
500 while (*p
&& isspace(*p
))
504 tokval
.t_type
= TOKEN_INVALID
;
505 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
508 error(ERR_NONFATAL
, "cannot use relocatable"
509 " expression as symbol size");
511 (*s
)->size
= reloc_value(e
);
515 * Remove it from the list of unresolved sizes.
517 nasm_free((*s
)->name
);
521 return; /* it wasn't an important one */
524 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
525 strslen
+= 1 + strlen(name
);
527 lastsym
= sym
= saa_wstruct(syms
);
530 sym
->type
= is_global
? SYM_GLOBAL
: 0;
531 sym
->other
= STV_DEFAULT
;
533 if (segment
== NO_SEG
)
534 sym
->section
= SHN_ABS
;
537 sym
->section
= SHN_UNDEF
;
538 if (nsects
== 0 && segment
== def_seg
) {
540 if (segment
!= elf_section_names(".text", 2, &tempint
))
542 "strange segment conditions in ELF driver");
543 sym
->section
= nsects
;
545 for (i
= 0; i
< nsects
; i
++)
546 if (segment
== sects
[i
]->index
) {
547 sym
->section
= i
+ 1;
553 if (is_global
== 2) {
556 sym
->section
= SHN_COMMON
;
558 * We have a common variable. Check the special text to see
559 * if it's a valid number and power of two; if so, store it
560 * as the alignment for the common variable.
564 sym
->value
= readnum(special
, &err
);
566 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
567 " valid number", special
);
568 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
569 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
570 " power of two", special
);
574 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
576 if (sym
->type
== SYM_GLOBAL
) {
578 * If sym->section == SHN_ABS, then the first line of the
579 * else section would cause a core dump, because its a reference
580 * beyond the end of the section array.
581 * This behaviour is exhibited by this code:
584 * To avoid such a crash, such requests are silently discarded.
585 * This may not be the best solution.
587 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
588 bsym
= raa_write(bsym
, segment
, nglobs
);
589 } else if (sym
->section
!= SHN_ABS
) {
591 * This is a global symbol; so we must add it to the linked
592 * list of global symbols in its section. We'll push it on
593 * the beginning of the list, because it doesn't matter
594 * much which end we put it on and it's easier like this.
596 * In addition, we check the special text for symbol
597 * type and size information.
599 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
600 sects
[sym
->section
- 1]->gsyms
= sym
;
603 int n
= strcspn(special
, " \t");
605 if (!nasm_strnicmp(special
, "function", n
))
606 sym
->type
|= STT_FUNC
;
607 else if (!nasm_strnicmp(special
, "data", n
) ||
608 !nasm_strnicmp(special
, "object", n
))
609 sym
->type
|= STT_OBJECT
;
610 else if (!nasm_strnicmp(special
, "notype", n
))
611 sym
->type
|= STT_NOTYPE
;
613 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
617 while (isspace(*special
))
620 n
= strcspn(special
, " \t");
621 if (!nasm_strnicmp(special
, "default", n
))
622 sym
->other
= STV_DEFAULT
;
623 else if (!nasm_strnicmp(special
, "internal", n
))
624 sym
->other
= STV_INTERNAL
;
625 else if (!nasm_strnicmp(special
, "hidden", n
))
626 sym
->other
= STV_HIDDEN
;
627 else if (!nasm_strnicmp(special
, "protected", n
))
628 sym
->other
= STV_PROTECTED
;
635 struct tokenval tokval
;
638 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
640 while (special
[n
] && isspace(special
[n
]))
643 * We have a size expression; attempt to
647 stdscan_bufptr
= special
+ n
;
648 tokval
.t_type
= TOKEN_INVALID
;
649 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
654 sym
->name
= nasm_strdup(name
);
657 error(ERR_NONFATAL
, "cannot use relocatable"
658 " expression as symbol size");
660 sym
->size
= reloc_value(e
);
662 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
667 sym
->globnum
= nglobs
;
672 if (special
&& !special_used
)
673 error(ERR_NONFATAL
, "no special symbol features supported here");
676 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
680 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
681 sect
->tail
= &r
->next
;
684 r
->address
= sect
->len
;
685 if (segment
== NO_SEG
)
690 for (i
= 0; i
< nsects
; i
++)
691 if (segment
== sects
[i
]->index
)
694 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
702 * This routine deals with ..got and ..sym relocations: the more
703 * complicated kinds. In shared-library writing, some relocations
704 * with respect to global symbols must refer to the precise symbol
705 * rather than referring to an offset from the base of the section
706 * _containing_ the symbol. Such relocations call to this routine,
707 * which searches the symbol list for the symbol in question.
709 * R_386_GOT32 references require the _exact_ symbol address to be
710 * used; R_386_32 references can be at an offset from the symbol.
711 * The boolean argument `exact' tells us this.
713 * Return value is the adjusted value of `addr', having become an
714 * offset from the symbol rather than the section. Should always be
715 * zero when returning from an exact call.
717 * Limitation: if you define two symbols at the same place,
718 * confusion will occur.
720 * Inefficiency: we search, currently, using a linked list which
721 * isn't even necessarily sorted.
723 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
724 int32_t segment
, int64_t offset
,
725 int type
, bool exact
)
729 struct Symbol
*sym
, *sm
;
733 * First look up the segment/offset pair and find a global
734 * symbol corresponding to it. If it's not one of our segments,
735 * then it must be an external symbol, in which case we're fine
736 * doing a normal elf_add_reloc after first sanity-checking
737 * that the offset from the symbol is zero.
740 for (i
= 0; i
< nsects
; i
++)
741 if (segment
== sects
[i
]->index
) {
746 if (exact
&& offset
!= 0)
747 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
748 " for this reference");
750 elf_add_reloc(sect
, segment
, type
);
756 * Find a symbol pointing _exactly_ at this one.
758 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
759 if (sym
->value
== offset
)
763 * Find the nearest symbol below this one.
766 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
767 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
771 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
772 " for this reference");
776 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
777 sect
->tail
= &r
->next
;
780 r
->address
= sect
->len
;
781 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
786 return offset
- sym
->value
;
789 static void elf_out(int32_t segto
, const void *data
,
790 enum out_type type
, uint64_t size
,
791 int32_t segment
, int32_t wrt
)
795 uint8_t mydata
[16], *p
;
797 static struct symlininfo sinfo
;
799 #if defined(DEBUG) && DEBUG>2
801 " elf_out type: %x seg: %d bytes: %x data: %"PRIx64
"\n",
802 (type
>> 24), segment
, size
, *(int64_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
, size
);
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
, size
);
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 (size
!= 8 && size
!= 4 && segment
!= NO_SEG
) {
933 "Unsupported non-64-bit ELF relocation");
935 if (size
== 4) WRITELONG(p
, addr
);
936 else WRITEDLONG(p
, (int64_t)addr
);
938 elf_sect_write(s
, mydata
, size
);
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
, *(int64_t *)data
- size
);
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
, *(int64_t *)data
- size
);
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(uint64_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
, uint64_t flags
,
1249 void *data
, bool is_saa
, uint64_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
, uint64_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
, int64_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
);