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 typedef uint32_t Elf64_Word
;
46 typedef uint64_t Elf64_Xword
;
47 typedef uint64_t Elf64_Addr
;
48 typedef uint64_t Elf64_Off
;
51 Elf64_Word sh_name
; /* Section name (string tbl index) */
52 Elf64_Word sh_type
; /* Section type */
53 Elf64_Xword sh_flags
; /* Section flags */
54 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
55 Elf64_Off sh_offset
; /* Section file offset */
56 Elf64_Xword sh_size
; /* Section size in bytes */
57 Elf64_Word sh_link
; /* Link to another section */
58 Elf64_Word sh_info
; /* Additional section information */
59 Elf64_Xword sh_addralign
; /* Section alignment */
60 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
69 int64_t address
; /* relative to _start_ of section */
70 int64_t symbol
; /* ELF symbol info thingy */
71 int type
; /* type of relocation */
75 int32_t strpos
; /* string table position of name */
76 int32_t section
; /* section ID of the symbol */
77 int type
; /* symbol type */
78 int other
; /* symbol visibility */
79 int64_t value
; /* address, or COMMON variable align */
80 int32_t size
; /* size of symbol */
81 int32_t globnum
; /* symbol table offset if global */
82 struct Symbol
*next
; /* list of globals in each section */
83 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
84 char *name
; /* used temporarily if in above list */
90 uint32_t len
, size
, nrelocs
;
92 int type
; /* SHT_PROGBITS or SHT_NOBITS */
93 int align
; /* alignment: power of two */
94 uint32_t flags
; /* section flags */
98 struct Reloc
*head
, **tail
;
99 struct Symbol
*gsyms
; /* global symbols in section */
102 #define SECT_DELTA 32
103 static struct Section
**sects
;
104 static int nsects
, sectlen
;
106 #define SHSTR_DELTA 256
107 static char *shstrtab
;
108 static int shstrtablen
, shstrtabsize
;
110 static struct SAA
*syms
;
111 static uint32_t nlocals
, nglobs
;
113 static int32_t def_seg
;
115 static struct RAA
*bsym
;
117 static struct SAA
*strs
;
118 static uint32_t strslen
;
122 static evalfunc evaluate
;
124 static struct Symbol
*fwds
;
126 static char elf_module
[FILENAME_MAX
];
128 extern struct ofmt of_elf64
;
132 #define SYM_SECTION 0x04
133 #define SYM_GLOBAL 0x10
134 #define SYM_NOTYPE 0x00
135 #define SYM_DATA 0x01
136 #define SYM_FUNCTION 0x02
138 #define STV_DEFAULT 0
139 #define STV_INTERNAL 1
141 #define STV_PROTECTED 3
143 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
145 #define SEG_ALIGN 16 /* alignment of sections in file */
146 #define SEG_ALIGN_1 (SEG_ALIGN-1)
148 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
150 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
151 static struct ELF_SECTDATA
{
156 static int elf_nsect
;
157 static int32_t elf_foffs
;
159 static void elf_write(void);
160 static void elf_sect_write(struct Section
*, const uint8_t *,
162 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
164 static void elf_write_sections(void);
165 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
166 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
167 static void add_sectname(char *, char *);
169 /* this stuff is needed for the stabs debugging format */
170 #define N_SO 0x64 /* ID for main source file */
171 #define N_SOL 0x84 /* ID for sub-source file */
175 #define TY_STABSSYMLIN 0x40 /* ouch */
191 int section
; /* section index */
192 char *name
; /* shallow-copied pointer of section name */
196 struct symlininfo info
;
199 struct linelist
*next
;
200 struct linelist
*last
;
203 static struct linelist
*stabslines
= 0;
204 static int stabs_immcall
= 0;
205 static int currentline
= 0;
206 static int numlinestabs
= 0;
207 static char *stabs_filename
= 0;
208 static int symtabsection
;
209 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
210 static int stablen
, stabstrlen
, stabrellen
;
212 static struct dfmt df_stabs
;
214 void stabs64_init(struct ofmt
*, void *, FILE *, efunc
);
215 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
216 void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
217 void stabs64_directive(const char *, const char *);
218 void stabs64_typevalue(int32_t);
219 void stabs64_output(int, void *);
220 void stabs64_generate(void);
221 void stabs64_cleanup(void);
223 /* end of stabs debugging stuff */
226 * Special section numbers which are used to define ELF special
227 * symbols, which can be used with WRT to provide PIC relocation
230 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
231 static int32_t elf_got_sect
, elf_plt_sect
;
232 static int32_t elf_sym_sect
;
234 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
240 (void)ldef
; /* placate optimisers */
242 nsects
= sectlen
= 0;
243 syms
= saa_init((int32_t)sizeof(struct Symbol
));
244 nlocals
= nglobs
= 0;
247 saa_wbytes(strs
, "\0", 1L);
248 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
249 strslen
= 2 + strlen(elf_module
);
251 shstrtablen
= shstrtabsize
= 0;;
252 add_sectname("", "");
256 elf_gotpc_sect
= seg_alloc();
257 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
259 elf_gotoff_sect
= seg_alloc();
260 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
262 elf_got_sect
= seg_alloc();
263 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
265 elf_plt_sect
= seg_alloc();
266 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
268 elf_sym_sect
= seg_alloc();
269 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
272 def_seg
= seg_alloc();
275 static void elf_cleanup(int debuginfo
)
284 for (i
= 0; i
< nsects
; i
++) {
285 if (sects
[i
]->type
!= SHT_NOBITS
)
286 saa_free(sects
[i
]->data
);
288 saa_free(sects
[i
]->rel
);
289 while (sects
[i
]->head
) {
291 sects
[i
]->head
= sects
[i
]->head
->next
;
299 if (of_elf64
.current_dfmt
) {
300 of_elf64
.current_dfmt
->cleanup();
304 static void add_sectname(char *firsthalf
, char *secondhalf
)
306 int len
= strlen(firsthalf
) + strlen(secondhalf
);
307 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
308 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
309 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
310 strcat(shstrtab
+ shstrtablen
, secondhalf
);
311 shstrtablen
+= len
+ 1;
314 static int elf_make_section(char *name
, int type
, int flags
, int align
)
318 s
= nasm_malloc(sizeof(*s
));
320 if (type
!= SHT_NOBITS
)
321 s
->data
= saa_init(1L);
324 s
->len
= s
->size
= 0;
326 if (!strcmp(name
, ".text"))
329 s
->index
= seg_alloc();
330 add_sectname("", name
);
331 s
->name
= nasm_malloc(1 + strlen(name
));
332 strcpy(s
->name
, name
);
338 if (nsects
>= sectlen
)
340 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
346 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
349 int flags_and
, flags_or
, type
, align
, i
;
352 * Default is 64 bits.
360 while (*p
&& !isspace(*p
))
364 flags_and
= flags_or
= type
= align
= 0;
366 while (*p
&& isspace(*p
))
370 while (*p
&& !isspace(*p
))
374 while (*p
&& isspace(*p
))
377 if (!nasm_strnicmp(q
, "align=", 6)) {
381 if ((align
- 1) & align
) { /* means it's not a power of two */
382 error(ERR_NONFATAL
, "section alignment %d is not"
383 " a power of two", align
);
386 } else if (!nasm_stricmp(q
, "alloc")) {
387 flags_and
|= SHF_ALLOC
;
388 flags_or
|= SHF_ALLOC
;
389 } else if (!nasm_stricmp(q
, "noalloc")) {
390 flags_and
|= SHF_ALLOC
;
391 flags_or
&= ~SHF_ALLOC
;
392 } else if (!nasm_stricmp(q
, "exec")) {
393 flags_and
|= SHF_EXECINSTR
;
394 flags_or
|= SHF_EXECINSTR
;
395 } else if (!nasm_stricmp(q
, "noexec")) {
396 flags_and
|= SHF_EXECINSTR
;
397 flags_or
&= ~SHF_EXECINSTR
;
398 } else if (!nasm_stricmp(q
, "write")) {
399 flags_and
|= SHF_WRITE
;
400 flags_or
|= SHF_WRITE
;
401 } else if (!nasm_stricmp(q
, "nowrite")) {
402 flags_and
|= SHF_WRITE
;
403 flags_or
&= ~SHF_WRITE
;
404 } else if (!nasm_stricmp(q
, "progbits")) {
406 } else if (!nasm_stricmp(q
, "nobits")) {
411 if (!strcmp(name
, ".comment") ||
412 !strcmp(name
, ".shstrtab") ||
413 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
414 error(ERR_NONFATAL
, "attempt to redefine reserved section"
419 for (i
= 0; i
< nsects
; i
++)
420 if (!strcmp(name
, sects
[i
]->name
))
423 if (!strcmp(name
, ".text"))
424 i
= elf_make_section(name
, SHT_PROGBITS
,
425 SHF_ALLOC
| SHF_EXECINSTR
, 16);
426 else if (!strcmp(name
, ".rodata"))
427 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
428 else if (!strcmp(name
, ".data"))
429 i
= elf_make_section(name
, SHT_PROGBITS
,
430 SHF_ALLOC
| SHF_WRITE
, 4);
431 else if (!strcmp(name
, ".bss"))
432 i
= elf_make_section(name
, SHT_NOBITS
,
433 SHF_ALLOC
| SHF_WRITE
, 4);
435 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
437 sects
[i
]->type
= type
;
439 sects
[i
]->align
= align
;
440 sects
[i
]->flags
&= ~flags_and
;
441 sects
[i
]->flags
|= flags_or
;
442 } else if (pass
== 1) {
443 if (type
|| align
|| flags_and
)
444 error(ERR_WARNING
, "section attributes ignored on"
445 " redeclaration of section `%s'", name
);
448 return sects
[i
]->index
;
451 static void elf_deflabel(char *name
, int32_t segment
, int32_t offset
,
452 int is_global
, char *special
)
456 bool special_used
= false;
458 #if defined(DEBUG) && DEBUG>2
460 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
461 name
, segment
, offset
, is_global
, special
);
463 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
465 * This is a NASM special symbol. We never allow it into
466 * the ELF symbol table, even if it's a valid one. If it
467 * _isn't_ a valid one, we should barf immediately.
469 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
470 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
471 strcmp(name
, "..sym"))
472 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
476 if (is_global
== 3) {
479 * Fix up a forward-reference symbol size from the first
482 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
483 if (!strcmp((*s
)->name
, name
)) {
484 struct tokenval tokval
;
488 while (*p
&& !isspace(*p
))
490 while (*p
&& isspace(*p
))
494 tokval
.t_type
= TOKEN_INVALID
;
495 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
498 error(ERR_NONFATAL
, "cannot use relocatable"
499 " expression as symbol size");
501 (*s
)->size
= reloc_value(e
);
505 * Remove it from the list of unresolved sizes.
507 nasm_free((*s
)->name
);
511 return; /* it wasn't an important one */
514 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
515 strslen
+= 1 + strlen(name
);
517 sym
= saa_wstruct(syms
);
520 sym
->type
= is_global
? SYM_GLOBAL
: 0;
521 sym
->other
= STV_DEFAULT
;
523 if (segment
== NO_SEG
)
524 sym
->section
= SHN_ABS
;
527 sym
->section
= SHN_UNDEF
;
528 if (nsects
== 0 && segment
== def_seg
) {
530 if (segment
!= elf_section_names(".text", 2, &tempint
))
532 "strange segment conditions in ELF driver");
533 sym
->section
= nsects
;
535 for (i
= 0; i
< nsects
; i
++)
536 if (segment
== sects
[i
]->index
) {
537 sym
->section
= i
+ 1;
543 if (is_global
== 2) {
546 sym
->section
= SHN_COMMON
;
548 * We have a common variable. Check the special text to see
549 * if it's a valid number and power of two; if so, store it
550 * as the alignment for the common variable.
554 sym
->value
= readnum(special
, &err
);
556 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
557 " valid number", special
);
558 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
559 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
560 " power of two", special
);
564 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
566 if (sym
->type
== SYM_GLOBAL
) {
568 * If sym->section == SHN_ABS, then the first line of the
569 * else section would cause a core dump, because its a reference
570 * beyond the end of the section array.
571 * This behaviour is exhibited by this code:
574 * To avoid such a crash, such requests are silently discarded.
575 * This may not be the best solution.
577 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
578 bsym
= raa_write(bsym
, segment
, nglobs
);
579 } else if (sym
->section
!= SHN_ABS
) {
581 * This is a global symbol; so we must add it to the linked
582 * list of global symbols in its section. We'll push it on
583 * the beginning of the list, because it doesn't matter
584 * much which end we put it on and it's easier like this.
586 * In addition, we check the special text for symbol
587 * type and size information.
589 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
590 sects
[sym
->section
- 1]->gsyms
= sym
;
593 int n
= strcspn(special
, " \t");
595 if (!nasm_strnicmp(special
, "function", n
))
596 sym
->type
|= SYM_FUNCTION
;
597 else if (!nasm_strnicmp(special
, "data", n
) ||
598 !nasm_strnicmp(special
, "object", n
))
599 sym
->type
|= SYM_DATA
;
600 else if (!nasm_strnicmp(special
, "notype", n
))
601 sym
->type
|= SYM_NOTYPE
;
603 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
607 while (isspace(*special
))
610 n
= strcspn(special
, " \t");
611 if (!nasm_strnicmp(special
, "default", n
))
612 sym
->other
= STV_DEFAULT
;
613 else if (!nasm_strnicmp(special
, "internal", n
))
614 sym
->other
= STV_INTERNAL
;
615 else if (!nasm_strnicmp(special
, "hidden", n
))
616 sym
->other
= STV_HIDDEN
;
617 else if (!nasm_strnicmp(special
, "protected", n
))
618 sym
->other
= STV_PROTECTED
;
625 struct tokenval tokval
;
628 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
630 while (special
[n
] && isspace(special
[n
]))
633 * We have a size expression; attempt to
637 stdscan_bufptr
= special
+ n
;
638 tokval
.t_type
= TOKEN_INVALID
;
639 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
644 sym
->name
= nasm_strdup(name
);
647 error(ERR_NONFATAL
, "cannot use relocatable"
648 " expression as symbol size");
650 sym
->size
= reloc_value(e
);
652 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
657 sym
->globnum
= nglobs
;
662 if (special
&& !special_used
)
663 error(ERR_NONFATAL
, "no special symbol features supported here");
666 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
670 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
671 sect
->tail
= &r
->next
;
674 r
->address
= sect
->len
;
675 if (segment
== NO_SEG
)
680 for (i
= 0; i
< nsects
; i
++)
681 if (segment
== sects
[i
]->index
)
684 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
692 * This routine deals with ..got and ..sym relocations: the more
693 * complicated kinds. In shared-library writing, some relocations
694 * with respect to global symbols must refer to the precise symbol
695 * rather than referring to an offset from the base of the section
696 * _containing_ the symbol. Such relocations call to this routine,
697 * which searches the symbol list for the symbol in question.
699 * R_386_GOT32 references require the _exact_ symbol address to be
700 * used; R_386_32 references can be at an offset from the symbol.
701 * The boolean argument `exact' tells us this.
703 * Return value is the adjusted value of `addr', having become an
704 * offset from the symbol rather than the section. Should always be
705 * zero when returning from an exact call.
707 * Limitation: if you define two symbols at the same place,
708 * confusion will occur.
710 * Inefficiency: we search, currently, using a linked list which
711 * isn't even necessarily sorted.
713 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
714 int32_t segment
, int64_t offset
,
715 int type
, bool exact
)
719 struct Symbol
*sym
, *sm
;
723 * First look up the segment/offset pair and find a global
724 * symbol corresponding to it. If it's not one of our segments,
725 * then it must be an external symbol, in which case we're fine
726 * doing a normal elf_add_reloc after first sanity-checking
727 * that the offset from the symbol is zero.
730 for (i
= 0; i
< nsects
; i
++)
731 if (segment
== sects
[i
]->index
) {
736 if (exact
&& offset
!= 0)
737 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
738 " for this reference");
740 elf_add_reloc(sect
, segment
, type
);
746 * Find a symbol pointing _exactly_ at this one.
748 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
749 if (sym
->value
== offset
)
753 * Find the nearest symbol below this one.
756 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
757 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
761 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
762 " for this reference");
766 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
767 sect
->tail
= &r
->next
;
770 r
->address
= sect
->len
;
771 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
776 return offset
- sym
->value
;
779 static void elf_out(int32_t segto
, const void *data
, uint32_t type
,
780 int32_t segment
, int32_t wrt
)
783 int32_t realbytes
= type
& OUT_SIZMASK
;
785 uint8_t mydata
[16], *p
;
787 static struct symlininfo sinfo
;
791 #if defined(DEBUG) && DEBUG>2
793 " elf_out type: %x seg: %d bytes: %x data: %x\n",
794 (type
>> 24), segment
, realbytes
, *(int32_t *)data
);
798 * handle absolute-assembly (structure definitions)
800 if (segto
== NO_SEG
) {
801 if (type
!= OUT_RESERVE
)
802 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
808 for (i
= 0; i
< nsects
; i
++)
809 if (segto
== sects
[i
]->index
) {
814 int tempint
; /* ignored */
815 if (segto
!= elf_section_names(".text", 2, &tempint
))
816 error(ERR_PANIC
, "strange segment conditions in ELF driver");
818 s
= sects
[nsects
- 1];
823 /* again some stabs debugging stuff */
824 if (of_elf64
.current_dfmt
) {
825 sinfo
.offset
= s
->len
;
827 sinfo
.name
= s
->name
;
828 of_elf64
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
830 /* end of debugging stuff */
832 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
833 error(ERR_WARNING
, "attempt to initialize memory in"
834 " BSS section `%s': ignored", s
->name
);
835 if (type
== OUT_REL2ADR
)
837 else if (type
== OUT_REL4ADR
)
843 if (type
== OUT_RESERVE
) {
844 if (s
->type
== SHT_PROGBITS
) {
845 error(ERR_WARNING
, "uninitialized space declared in"
846 " non-BSS section `%s': zeroing", s
->name
);
847 elf_sect_write(s
, NULL
, realbytes
);
850 } else if (type
== OUT_RAWDATA
) {
851 if (segment
!= NO_SEG
)
852 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
853 elf_sect_write(s
, data
, realbytes
);
854 } else if (type
== OUT_ADDRESS
) {
856 addr
= *(int64_t *)data
;
857 if (segment
!= NO_SEG
) {
859 error(ERR_NONFATAL
, "ELF format does not support"
860 " segment base references");
865 elf_add_reloc(s
, segment
, R_X86_64_16
);
868 elf_add_reloc(s
, segment
, R_X86_64_32
);
871 elf_add_reloc(s
, segment
, R_X86_64_64
);
874 error(ERR_PANIC
, "internal error elf64-hpa-871");
877 } else if (wrt
== elf_gotpc_sect
+ 1) {
879 * The user will supply GOT relative to $$. ELF
880 * will let us have GOT relative to $. So we
881 * need to fix up the data item by $-$$.
884 elf_add_reloc(s
, segment
, R_X86_64_GOTPCREL
);
885 } else if (wrt
== elf_gotoff_sect
+ 1) {
886 elf_add_reloc(s
, segment
, R_X86_64_GOTTPOFF
);
887 } else if (wrt
== elf_got_sect
+ 1) {
888 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
889 R_X86_64_GOT32
, true);
890 } else if (wrt
== elf_sym_sect
+ 1) {
894 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
898 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
902 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
906 error(ERR_PANIC
, "internal error elf64-hpa-903");
909 } else if (wrt
== elf_plt_sect
+ 1) {
910 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
911 "relative PLT references");
913 error(ERR_NONFATAL
, "ELF format does not support this"
915 wrt
= NO_SEG
; /* we can at least _try_ to continue */
923 if (realbytes
!= 8 && realbytes
!= 4 && segment
!= NO_SEG
) {
925 "Unsupported non-64-bit ELF relocation");
927 if (realbytes
== 4) WRITELONG(p
, addr
);
928 else WRITEDLONG(p
, (int64_t)addr
);
930 elf_sect_write(s
, mydata
, realbytes
);
931 } else if (type
== OUT_REL2ADR
) {
932 if (segment
== segto
)
933 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
934 if (segment
!= NO_SEG
&& segment
% 2) {
935 error(ERR_NONFATAL
, "ELF format does not support"
936 " segment base references");
939 elf_add_reloc(s
, segment
, R_X86_64_PC16
);
942 "Unsupported non-32-bit ELF relocation [2]");
946 WRITESHORT(p
, *(int32_t *)data
- realbytes
);
947 elf_sect_write(s
, mydata
, 2L);
948 } else if (type
== OUT_REL4ADR
) {
949 if (segment
== segto
)
950 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
951 if (segment
!= NO_SEG
&& segment
% 2) {
952 error(ERR_NONFATAL
, "ELF format does not support"
953 " segment base references");
956 elf_add_reloc(s
, segment
, R_X86_64_PC32
);
957 } else if (wrt
== elf_plt_sect
+ 1) {
958 elf_add_reloc(s
, segment
, R_X86_64_PLT32
);
959 } else if (wrt
== elf_gotpc_sect
+ 1 ||
960 wrt
== elf_gotoff_sect
+ 1 ||
961 wrt
== elf_got_sect
+ 1) {
962 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
963 "relative GOT references");
965 error(ERR_NONFATAL
, "ELF format does not support this"
967 wrt
= NO_SEG
; /* we can at least _try_ to continue */
971 WRITELONG(p
, *(int32_t *)data
- realbytes
);
972 elf_sect_write(s
, mydata
, 4L);
976 static void elf_write(void)
978 int nsections
, align
;
986 int32_t symtablen
, symtablocal
;
989 * Work out how many sections we will have. We have SHN_UNDEF,
990 * then the flexible user sections, then the four fixed
991 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
992 * then optionally relocation sections for the user sections.
994 if (of_elf64
.current_dfmt
== &df_stabs
)
997 nsections
= 5; /* SHN_UNDEF and the fixed ones */
999 add_sectname("", ".comment");
1000 add_sectname("", ".shstrtab");
1001 add_sectname("", ".symtab");
1002 add_sectname("", ".strtab");
1003 for (i
= 0; i
< nsects
; i
++) {
1004 nsections
++; /* for the section itself */
1005 if (sects
[i
]->head
) {
1006 nsections
++; /* for its relocations without addends*/
1007 add_sectname(".rela", sects
[i
]->name
);
1011 if (of_elf64
.current_dfmt
== &df_stabs
) {
1012 /* in case the debug information is wanted, just add these three sections... */
1013 add_sectname("", ".stab");
1014 add_sectname("", ".stabstr");
1015 add_sectname(".rel", ".stab");
1023 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1026 * Output the ELF header.
1028 fwrite("\177ELF\2\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
1029 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1030 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1031 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1032 fwriteint64_t(0L, elffp
); /* no entry point */
1033 fwriteint64_t(0L, elffp
); /* no program header table */
1034 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1035 * ELF header plus alignment */
1036 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1037 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1038 fwriteint16_t(0, elffp
); /* no program header table, again */
1039 fwriteint16_t(0, elffp
); /* still no program header table */
1040 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1041 fwriteint16_t(nsections
, elffp
); /* number of sections */
1042 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1043 * section header table */
1046 * Build the symbol table and relocation tables.
1048 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1049 for (i
= 0; i
< nsects
; i
++)
1051 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1055 * Now output the section header table.
1058 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1059 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1062 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1064 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1065 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1067 for (i
= 0; i
< nsects
; i
++) {
1068 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1069 (sects
[i
]->type
== SHT_PROGBITS
?
1070 sects
[i
]->data
: NULL
), true,
1071 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1073 scount
++; /* dito */
1075 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1076 scount
++; /* dito */
1078 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1079 scount
++; /* dito */
1081 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1082 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1084 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1085 for (i
= 0; i
< nsects
; i
++)
1086 if (sects
[i
]->head
) {
1088 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, true,
1089 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1091 if (of_elf64
.current_dfmt
== &df_stabs
) {
1092 /* for debugging information, create the last three sections
1093 which are the .stab , .stabstr and .rel.stab sections respectively */
1095 /* this function call creates the stab sections in memory */
1098 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1100 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1101 nsections
- 2, 0, 4, 12);
1104 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1105 stabstrlen
, 0, 0, 4, 0);
1108 /* link -> symtable info -> section to refer to */
1109 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1110 stabrellen
, symtabsection
, nsections
- 3, 4,
1114 fwrite(align_str
, align
, 1, elffp
);
1117 * Now output the sections.
1119 elf_write_sections();
1121 nasm_free(elf_sects
);
1125 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1127 struct SAA
*s
= saa_init(1L);
1129 uint8_t entry
[24], *p
;
1135 * First, an all-zeros entry, required by the ELF spec.
1137 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1142 * Next, an entry for the file name.
1145 WRITELONG(p
, 1); /* we know it's 1st thing in strtab */
1146 WRITESHORT(p
, 4); /* type FILE */
1147 WRITESHORT(p
, SHN_ABS
);
1148 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1149 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1150 saa_wbytes(s
, entry
, 24L);
1155 * Now some standard symbols defining the segments, for relocation
1158 for (i
= 1; i
<= nsects
+ 1; i
++) {
1160 WRITELONG(p
, 0); /* no symbol name */
1161 WRITESHORT(p
, 3); /* local section-type thing */
1162 WRITESHORT(p
, (i
== 1 ? SHN_ABS
: i
- 1)); /* the section id */
1163 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1164 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1165 saa_wbytes(s
, entry
, 24L);
1171 * Now the other local symbols.
1174 while ((sym
= saa_rstruct(syms
))) {
1175 if (sym
->type
& SYM_GLOBAL
)
1178 WRITELONG(p
, sym
->strpos
);
1179 WRITECHAR(p
, sym
->type
); /* local non-typed thing */
1180 WRITECHAR(p
, sym
->other
);
1181 WRITESHORT(p
, sym
->section
);
1182 WRITEDLONG(p
, (int64_t)sym
->value
);
1183 WRITEDLONG(p
, (int64_t)sym
->size
);
1184 saa_wbytes(s
, entry
, 24L);
1190 * Now the global symbols.
1193 while ((sym
= saa_rstruct(syms
))) {
1194 if (!(sym
->type
& SYM_GLOBAL
))
1197 WRITELONG(p
, sym
->strpos
);
1198 WRITECHAR(p
, sym
->type
); /* global non-typed thing */
1199 WRITECHAR(p
, sym
->other
);
1200 WRITESHORT(p
, sym
->section
);
1201 WRITEDLONG(p
, (int64_t)sym
->value
);
1202 WRITEDLONG(p
, (int64_t)sym
->size
);
1203 saa_wbytes(s
, entry
, 24L);
1210 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1213 uint8_t *p
, entry
[24];
1222 int64_t sym
= r
->symbol
;
1224 if (sym
>= GLOBAL_TEMP_BASE
)
1225 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 3) + nlocals
;
1228 WRITEDLONG(p
, r
->address
);
1229 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1230 WRITEDLONG(p
, (uint64_t) 0);
1231 saa_wbytes(s
, entry
, 24L);
1240 static void elf_section_header(int name
, int type
, int flags
,
1241 void *data
, bool is_saa
, int32_t datalen
,
1242 int link
, int info
, int align
, int eltsize
)
1244 elf_sects
[elf_nsect
].data
= data
;
1245 elf_sects
[elf_nsect
].len
= datalen
;
1246 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1249 fwriteint32_t((int32_t)name
, elffp
);
1250 fwriteint32_t((int32_t)type
, elffp
);
1251 fwriteint64_t((int64_t)flags
, elffp
);
1252 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1253 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1254 fwriteint64_t(datalen
, elffp
);
1256 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1257 fwriteint32_t((int32_t)link
, elffp
);
1258 fwriteint32_t((int32_t)info
, elffp
);
1259 fwriteint64_t((int64_t)align
, elffp
);
1260 fwriteint64_t((int64_t)eltsize
, elffp
);
1263 static void elf_write_sections(void)
1266 for (i
= 0; i
< elf_nsect
; i
++)
1267 if (elf_sects
[i
].data
) {
1268 int32_t len
= elf_sects
[i
].len
;
1269 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1270 int32_t align
= reallen
- len
;
1271 if (elf_sects
[i
].is_saa
)
1272 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1274 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1275 fwrite(align_str
, align
, 1, elffp
);
1279 static void elf_sect_write(struct Section
*sect
,
1280 const uint8_t *data
, uint32_t len
)
1282 saa_wbytes(sect
->data
, data
, len
);
1286 static int32_t elf_segbase(int32_t segment
)
1291 static int elf_directive(char *directive
, char *value
, int pass
)
1299 static void elf_filename(char *inname
, char *outname
, efunc error
)
1301 strcpy(elf_module
, inname
);
1302 standard_extension(inname
, outname
, ".o", error
);
1305 static const char *elf_stdmac
[] = {
1306 "%define __SECT__ [section .text]",
1307 "%macro __NASM_CDecl__ 1",
1312 static int elf_set_info(enum geninfo type
, char **val
)
1319 static struct dfmt df_stabs
= {
1320 "ELF64 (X86_64) stabs debug format for Linux",
1331 struct dfmt
*elf64_debugs_arr
[2] = { &df_stabs
, NULL
};
1333 struct ofmt of_elf64
= {
1334 "ELF64 (x86_64) object files (e.g. Linux)",
1351 /* again, the stabs debugging stuff (code) */
1353 void stabs64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1361 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1365 if (!stabs_filename
) {
1366 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1367 strcpy(stabs_filename
, filename
);
1369 if (strcmp(stabs_filename
, filename
)) {
1370 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1371 in fact, this leak comes in quite handy to maintain a list of files
1372 encountered so far in the symbol lines... */
1374 /* why not nasm_free(stabs_filename); we're done with the old one */
1376 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1377 strcpy(stabs_filename
, filename
);
1381 currentline
= linenumber
;
1384 void stabs64_deflabel(char *name
, int32_t segment
, int32_t offset
, int is_global
,
1394 void stabs64_directive(const char *directive
, const char *params
)
1400 void stabs64_typevalue(int32_t type
)
1405 void stabs64_output(int type
, void *param
)
1407 struct symlininfo
*s
;
1408 struct linelist
*el
;
1409 if (type
== TY_STABSSYMLIN
) {
1410 if (stabs_immcall
) {
1411 s
= (struct symlininfo
*)param
;
1412 if (strcmp(s
->name
, ".text"))
1413 return; /* we are only interested in the text stuff */
1415 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1416 el
->info
.offset
= s
->offset
;
1417 el
->info
.section
= s
->section
;
1418 el
->info
.name
= s
->name
;
1419 el
->line
= currentline
;
1420 el
->filename
= stabs_filename
;
1423 stabslines
->last
->next
= el
;
1424 stabslines
->last
= el
;
1427 stabslines
->last
= el
;
1434 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1436 WRITELONG(p,n_strx); \
1437 WRITECHAR(p,n_type); \
1438 WRITECHAR(p,n_other); \
1439 WRITESHORT(p,n_desc); \
1440 WRITELONG(p,n_value); \
1443 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1445 void stabs64_generate(void)
1447 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1448 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1452 struct linelist
*ptr
;
1456 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1457 for (i
= 0; i
< numlinestabs
; i
++)
1461 if (numfiles
== 0) {
1462 allfiles
[0] = ptr
->filename
;
1465 for (i
= 0; i
< numfiles
; i
++) {
1466 if (!strcmp(allfiles
[i
], ptr
->filename
))
1469 if (i
>= numfiles
) {
1470 allfiles
[i
] = ptr
->filename
;
1477 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1478 for (i
= 0; i
< numfiles
; i
++) {
1479 fileidx
[i
] = strsize
;
1480 strsize
+= strlen(allfiles
[i
]) + 1;
1483 for (i
= 0; i
< numfiles
; i
++) {
1484 if (!strcmp(allfiles
[i
], elf_module
)) {
1490 /* worst case size of the stab buffer would be:
1491 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1494 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1495 sizeof(struct stabentry
));
1497 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1499 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1502 for (i
= 0; i
< numfiles
; i
++) {
1503 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1507 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1510 /* this is the first stab, its strx points to the filename of the
1511 the source-file, the n_desc field should be set to the number
1514 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1520 /* this is the stab for the main source file */
1521 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1523 /* relocation table entry */
1525 /* Since the above WRITE_STAB calls have already */
1526 /* created two entries, the index in the info.section */
1527 /* member must be adjusted by adding 3 */
1529 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1530 WRITELONG(rptr
, R_X86_64_32
);
1531 WRITELONG(rptr
, ptr
->info
.section
+ 3);
1534 currfile
= mainfileindex
;
1538 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1539 /* oops file has changed... */
1540 for (i
= 0; i
< numfiles
; i
++)
1541 if (!strcmp(allfiles
[i
], ptr
->filename
))
1544 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1548 /* relocation table entry */
1550 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1551 WRITELONG(rptr
, R_X86_64_32
);
1552 WRITELONG(rptr
, ptr
->info
.section
+ 3);
1555 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1558 /* relocation table entry */
1560 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1561 WRITELONG(rptr
, R_X86_64_32
);
1562 WRITELONG(rptr
, ptr
->info
.section
+ 3);
1568 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1570 nasm_free(allfiles
);
1573 stablen
= (sptr
- sbuf
);
1574 stabrellen
= (rptr
- rbuf
);
1580 void stabs64_cleanup(void)
1582 struct linelist
*ptr
, *del
;
1594 nasm_free(stabrelbuf
);
1596 nasm_free(stabstrbuf
);