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.
21 /* Definitions in lieu of elf.h */
23 #define SHT_PROGBITS 1
24 #define SHT_RELA 4 /* Relocation entries with addends */
26 #define SHF_WRITE (1 << 0) /* Writable */
27 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
28 #define SHF_EXECINSTR (1 << 2) /* Executable */
29 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
30 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
31 #define R_X86_64_NONE 0 /* No reloc */
32 #define R_X86_64_64 1 /* Direct 64 bit address */
33 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
34 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
35 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
36 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
37 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
38 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
39 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
40 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
41 #define ET_REL 1 /* Relocatable file */
42 #define EM_X86_64 62 /* AMD x86-64 architecture */
43 typedef uint32_t Elf64_Word
;
44 typedef uint64_t Elf64_Xword
;
45 typedef uint64_t Elf64_Addr
;
46 typedef uint64_t Elf64_Off
;
49 Elf64_Word sh_name
; /* Section name (string tbl index) */
50 Elf64_Word sh_type
; /* Section type */
51 Elf64_Xword sh_flags
; /* Section flags */
52 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
53 Elf64_Off sh_offset
; /* Section file offset */
54 Elf64_Xword sh_size
; /* Section size in bytes */
55 Elf64_Word sh_link
; /* Link to another section */
56 Elf64_Word sh_info
; /* Additional section information */
57 Elf64_Xword sh_addralign
; /* Section alignment */
58 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
67 int64_t address
; /* relative to _start_ of section */
68 int64_t symbol
; /* ELF symbol info thingy */
69 int type
; /* type of relocation */
73 int32_t strpos
; /* string table position of name */
74 int32_t section
; /* section ID of the symbol */
75 int type
; /* symbol type */
76 int other
; /* symbol visibility */
77 int32_t value
; /* address, or COMMON variable align */
78 int32_t size
; /* size of symbol */
79 int32_t globnum
; /* symbol table offset if global */
80 struct Symbol
*next
; /* list of globals in each section */
81 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
82 char *name
; /* used temporarily if in above list */
88 uint32_t len
, size
, nrelocs
;
90 int type
; /* SHT_PROGBITS or SHT_NOBITS */
91 int align
; /* alignment: power of two */
92 uint32_t flags
; /* section flags */
96 struct Reloc
*head
, **tail
;
97 struct Symbol
*gsyms
; /* global symbols in section */
100 #define SECT_DELTA 32
101 static struct Section
**sects
;
102 static int nsects
, sectlen
;
104 #define SHSTR_DELTA 256
105 static char *shstrtab
;
106 static int shstrtablen
, shstrtabsize
;
108 static struct SAA
*syms
;
109 static uint32_t nlocals
, nglobs
;
111 static int32_t def_seg
;
113 static struct RAA
*bsym
;
115 static struct SAA
*strs
;
116 static uint32_t strslen
;
120 static evalfunc evaluate
;
122 static struct Symbol
*fwds
;
124 static char elf_module
[FILENAME_MAX
];
126 extern struct ofmt of_elf64
;
130 #define SYM_SECTION 0x04
131 #define SYM_GLOBAL 0x10
132 #define SYM_NOTYPE 0x00
133 #define SYM_DATA 0x01
134 #define SYM_FUNCTION 0x02
136 #define STV_DEFAULT 0
137 #define STV_INTERNAL 1
139 #define STV_PROTECTED 3
141 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
143 #define SEG_ALIGN 16 /* alignment of sections in file */
144 #define SEG_ALIGN_1 (SEG_ALIGN-1)
146 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
148 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
149 static struct ELF_SECTDATA
{
154 static int elf_nsect
;
155 static int32_t elf_foffs
;
157 static void elf_write(void);
158 static void elf_sect_write(struct Section
*, const uint8_t *,
160 static void elf_section_header(int, int, int, void *, int, int32_t, int, int,
162 static void elf_write_sections(void);
163 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
164 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
165 static void add_sectname(char *, char *);
167 /* this stuff is needed for the stabs debugging format */
168 #define N_SO 0x64 /* ID for main source file */
169 #define N_SOL 0x84 /* ID for sub-source file */
173 #define TY_STABSSYMLIN 0x40 /* ouch */
189 int section
; /* section index */
190 char *name
; /* shallow-copied pointer of section name */
194 struct symlininfo info
;
197 struct linelist
*next
;
198 struct linelist
*last
;
201 static struct linelist
*stabslines
= 0;
202 static int stabs_immcall
= 0;
203 static int currentline
= 0;
204 static int numlinestabs
= 0;
205 static char *stabs_filename
= 0;
206 static int symtabsection
;
207 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
208 static int stablen
, stabstrlen
, stabrellen
;
210 static struct dfmt df_stabs
;
212 void stabs64_init(struct ofmt
*, void *, FILE *, efunc
);
213 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
214 void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
215 void stabs64_directive(const char *, const char *);
216 void stabs64_typevalue(int32_t);
217 void stabs64_output(int, void *);
218 void stabs64_generate();
219 void stabs64_cleanup();
221 /* end of stabs debugging stuff */
224 * Special section numbers which are used to define ELF special
225 * symbols, which can be used with WRT to provide PIC relocation
228 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
229 static int32_t elf_got_sect
, elf_plt_sect
;
230 static int32_t elf_sym_sect
;
232 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
238 (void)ldef
; /* placate optimisers */
240 nsects
= sectlen
= 0;
241 syms
= saa_init((int32_t)sizeof(struct Symbol
));
242 nlocals
= nglobs
= 0;
245 saa_wbytes(strs
, "\0", 1L);
246 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
247 strslen
= 2 + strlen(elf_module
);
249 shstrtablen
= shstrtabsize
= 0;;
250 add_sectname("", "");
254 elf_gotpc_sect
= seg_alloc();
255 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
257 elf_gotoff_sect
= seg_alloc();
258 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
260 elf_got_sect
= seg_alloc();
261 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
263 elf_plt_sect
= seg_alloc();
264 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
266 elf_sym_sect
= seg_alloc();
267 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
270 def_seg
= seg_alloc();
273 static void elf_cleanup(int debuginfo
)
282 for (i
= 0; i
< nsects
; i
++) {
283 if (sects
[i
]->type
!= SHT_NOBITS
)
284 saa_free(sects
[i
]->data
);
286 saa_free(sects
[i
]->rel
);
287 while (sects
[i
]->head
) {
289 sects
[i
]->head
= sects
[i
]->head
->next
;
297 if (of_elf64
.current_dfmt
) {
298 of_elf64
.current_dfmt
->cleanup();
302 static void add_sectname(char *firsthalf
, char *secondhalf
)
304 int len
= strlen(firsthalf
) + strlen(secondhalf
);
305 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
306 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
307 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
308 strcat(shstrtab
+ shstrtablen
, secondhalf
);
309 shstrtablen
+= len
+ 1;
312 static int elf_make_section(char *name
, int type
, int flags
, int align
)
316 s
= nasm_malloc(sizeof(*s
));
318 if (type
!= SHT_NOBITS
)
319 s
->data
= saa_init(1L);
322 s
->len
= s
->size
= 0;
324 if (!strcmp(name
, ".text"))
327 s
->index
= seg_alloc();
328 add_sectname("", name
);
329 s
->name
= nasm_malloc(1 + strlen(name
));
330 strcpy(s
->name
, name
);
336 if (nsects
>= sectlen
)
338 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
344 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
347 int flags_and
, flags_or
, type
, align
, i
;
350 * Default is 64 bits.
358 while (*p
&& !isspace(*p
))
362 flags_and
= flags_or
= type
= align
= 0;
364 while (*p
&& isspace(*p
))
368 while (*p
&& !isspace(*p
))
372 while (*p
&& isspace(*p
))
375 if (!nasm_strnicmp(q
, "align=", 6)) {
379 if ((align
- 1) & align
) { /* means it's not a power of two */
380 error(ERR_NONFATAL
, "section alignment %d is not"
381 " a power of two", align
);
384 } else if (!nasm_stricmp(q
, "alloc")) {
385 flags_and
|= SHF_ALLOC
;
386 flags_or
|= SHF_ALLOC
;
387 } else if (!nasm_stricmp(q
, "noalloc")) {
388 flags_and
|= SHF_ALLOC
;
389 flags_or
&= ~SHF_ALLOC
;
390 } else if (!nasm_stricmp(q
, "exec")) {
391 flags_and
|= SHF_EXECINSTR
;
392 flags_or
|= SHF_EXECINSTR
;
393 } else if (!nasm_stricmp(q
, "noexec")) {
394 flags_and
|= SHF_EXECINSTR
;
395 flags_or
&= ~SHF_EXECINSTR
;
396 } else if (!nasm_stricmp(q
, "write")) {
397 flags_and
|= SHF_WRITE
;
398 flags_or
|= SHF_WRITE
;
399 } else if (!nasm_stricmp(q
, "nowrite")) {
400 flags_and
|= SHF_WRITE
;
401 flags_or
&= ~SHF_WRITE
;
402 } else if (!nasm_stricmp(q
, "progbits")) {
404 } else if (!nasm_stricmp(q
, "nobits")) {
409 if (!strcmp(name
, ".comment") ||
410 !strcmp(name
, ".shstrtab") ||
411 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
412 error(ERR_NONFATAL
, "attempt to redefine reserved section"
417 for (i
= 0; i
< nsects
; i
++)
418 if (!strcmp(name
, sects
[i
]->name
))
421 if (!strcmp(name
, ".text"))
422 i
= elf_make_section(name
, SHT_PROGBITS
,
423 SHF_ALLOC
| SHF_EXECINSTR
, 16);
424 else if (!strcmp(name
, ".rodata"))
425 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
426 else if (!strcmp(name
, ".data"))
427 i
= elf_make_section(name
, SHT_PROGBITS
,
428 SHF_ALLOC
| SHF_WRITE
, 4);
429 else if (!strcmp(name
, ".bss"))
430 i
= elf_make_section(name
, SHT_NOBITS
,
431 SHF_ALLOC
| SHF_WRITE
, 4);
433 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
435 sects
[i
]->type
= type
;
437 sects
[i
]->align
= align
;
438 sects
[i
]->flags
&= ~flags_and
;
439 sects
[i
]->flags
|= flags_or
;
440 } else if (pass
== 1) {
441 if (type
|| align
|| flags_and
)
442 error(ERR_WARNING
, "section attributes ignored on"
443 " redeclaration of section `%s'", name
);
446 return sects
[i
]->index
;
449 static void elf_deflabel(char *name
, int32_t segment
, int32_t offset
,
450 int is_global
, char *special
)
454 int special_used
= FALSE
;
456 #if defined(DEBUG) && DEBUG>2
458 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
459 name
, segment
, offset
, is_global
, special
);
461 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
463 * This is a NASM special symbol. We never allow it into
464 * the ELF symbol table, even if it's a valid one. If it
465 * _isn't_ a valid one, we should barf immediately.
467 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
468 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
469 strcmp(name
, "..sym"))
470 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
474 if (is_global
== 3) {
477 * Fix up a forward-reference symbol size from the first
480 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
481 if (!strcmp((*s
)->name
, name
)) {
482 struct tokenval tokval
;
486 while (*p
&& !isspace(*p
))
488 while (*p
&& isspace(*p
))
492 tokval
.t_type
= TOKEN_INVALID
;
493 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
496 error(ERR_NONFATAL
, "cannot use relocatable"
497 " expression as symbol size");
499 (*s
)->size
= reloc_value(e
);
503 * Remove it from the list of unresolved sizes.
505 nasm_free((*s
)->name
);
509 return; /* it wasn't an important one */
512 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
513 strslen
+= 1 + strlen(name
);
515 sym
= saa_wstruct(syms
);
518 sym
->type
= is_global
? SYM_GLOBAL
: 0;
519 sym
->other
= STV_DEFAULT
;
521 if (segment
== NO_SEG
)
522 sym
->section
= SHN_ABS
;
525 sym
->section
= SHN_UNDEF
;
526 if (nsects
== 0 && segment
== def_seg
) {
528 if (segment
!= elf_section_names(".text", 2, &tempint
))
530 "strange segment conditions in ELF driver");
531 sym
->section
= nsects
;
533 for (i
= 0; i
< nsects
; i
++)
534 if (segment
== sects
[i
]->index
) {
535 sym
->section
= i
+ 1;
541 if (is_global
== 2) {
544 sym
->section
= SHN_COMMON
;
546 * We have a common variable. Check the special text to see
547 * if it's a valid number and power of two; if so, store it
548 * as the alignment for the common variable.
552 sym
->value
= readnum(special
, &err
);
554 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
555 " valid number", special
);
556 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
557 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
558 " power of two", special
);
562 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
564 if (sym
->type
== SYM_GLOBAL
) {
566 * If sym->section == SHN_ABS, then the first line of the
567 * else section would cause a core dump, because its a reference
568 * beyond the end of the section array.
569 * This behaviour is exhibited by this code:
572 * To avoid such a crash, such requests are silently discarded.
573 * This may not be the best solution.
575 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
576 bsym
= raa_write(bsym
, segment
, nglobs
);
577 } else if (sym
->section
!= SHN_ABS
) {
579 * This is a global symbol; so we must add it to the linked
580 * list of global symbols in its section. We'll push it on
581 * the beginning of the list, because it doesn't matter
582 * much which end we put it on and it's easier like this.
584 * In addition, we check the special text for symbol
585 * type and size information.
587 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
588 sects
[sym
->section
- 1]->gsyms
= sym
;
591 int n
= strcspn(special
, " \t");
593 if (!nasm_strnicmp(special
, "function", n
))
594 sym
->type
|= SYM_FUNCTION
;
595 else if (!nasm_strnicmp(special
, "data", n
) ||
596 !nasm_strnicmp(special
, "object", n
))
597 sym
->type
|= SYM_DATA
;
598 else if (!nasm_strnicmp(special
, "notype", n
))
599 sym
->type
|= SYM_NOTYPE
;
601 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
605 while (isspace(*special
))
608 n
= strcspn(special
, " \t");
609 if (!nasm_strnicmp(special
, "default", n
))
610 sym
->other
= STV_DEFAULT
;
611 else if (!nasm_strnicmp(special
, "internal", n
))
612 sym
->other
= STV_INTERNAL
;
613 else if (!nasm_strnicmp(special
, "hidden", n
))
614 sym
->other
= STV_HIDDEN
;
615 else if (!nasm_strnicmp(special
, "protected", n
))
616 sym
->other
= STV_PROTECTED
;
623 struct tokenval tokval
;
626 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
628 while (special
[n
] && isspace(special
[n
]))
631 * We have a size expression; attempt to
635 stdscan_bufptr
= special
+ n
;
636 tokval
.t_type
= TOKEN_INVALID
;
637 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
642 sym
->name
= nasm_strdup(name
);
645 error(ERR_NONFATAL
, "cannot use relocatable"
646 " expression as symbol size");
648 sym
->size
= reloc_value(e
);
650 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
655 sym
->globnum
= nglobs
;
660 if (special
&& !special_used
)
661 error(ERR_NONFATAL
, "no special symbol features supported here");
664 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
668 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
669 sect
->tail
= &r
->next
;
672 r
->address
= sect
->len
;
673 if (segment
== NO_SEG
)
678 for (i
= 0; i
< nsects
; i
++)
679 if (segment
== sects
[i
]->index
)
682 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
690 * This routine deals with ..got and ..sym relocations: the more
691 * complicated kinds. In shared-library writing, some relocations
692 * with respect to global symbols must refer to the precise symbol
693 * rather than referring to an offset from the base of the section
694 * _containing_ the symbol. Such relocations call to this routine,
695 * which searches the symbol list for the symbol in question.
697 * R_386_GOT32 references require the _exact_ symbol address to be
698 * used; R_386_32 references can be at an offset from the symbol.
699 * The boolean argument `exact' tells us this.
701 * Return value is the adjusted value of `addr', having become an
702 * offset from the symbol rather than the section. Should always be
703 * zero when returning from an exact call.
705 * Limitation: if you define two symbols at the same place,
706 * confusion will occur.
708 * Inefficiency: we search, currently, using a linked list which
709 * isn't even necessarily sorted.
711 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
712 int32_t segment
, int32_t offset
,
717 struct Symbol
*sym
, *sm
;
721 * First look up the segment/offset pair and find a global
722 * symbol corresponding to it. If it's not one of our segments,
723 * then it must be an external symbol, in which case we're fine
724 * doing a normal elf_add_reloc after first sanity-checking
725 * that the offset from the symbol is zero.
728 for (i
= 0; i
< nsects
; i
++)
729 if (segment
== sects
[i
]->index
) {
734 if (exact
&& offset
!= 0)
735 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
736 " for this reference");
738 elf_add_reloc(sect
, segment
, type
);
744 * Find a symbol pointing _exactly_ at this one.
746 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
747 if (sym
->value
== offset
)
751 * Find the nearest symbol below this one.
754 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
755 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
759 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
760 " for this reference");
764 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
765 sect
->tail
= &r
->next
;
768 r
->address
= sect
->len
;
769 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
774 return offset
- sym
->value
;
777 static void elf_out(int32_t segto
, const void *data
, uint32_t type
,
778 int32_t segment
, int32_t wrt
)
781 int32_t realbytes
= type
& OUT_SIZMASK
;
783 uint8_t mydata
[16], *p
;
785 static struct symlininfo sinfo
;
789 #if defined(DEBUG) && DEBUG>2
791 " elf_out type: %x seg: %d bytes: %x data: %x\n",
792 (type
>> 24), segment
, realbytes
, *(int32_t *)data
);
796 * handle absolute-assembly (structure definitions)
798 if (segto
== NO_SEG
) {
799 if (type
!= OUT_RESERVE
)
800 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
806 for (i
= 0; i
< nsects
; i
++)
807 if (segto
== sects
[i
]->index
) {
812 int tempint
; /* ignored */
813 if (segto
!= elf_section_names(".text", 2, &tempint
))
814 error(ERR_PANIC
, "strange segment conditions in ELF driver");
816 s
= sects
[nsects
- 1];
821 /* again some stabs debugging stuff */
822 if (of_elf64
.current_dfmt
) {
823 sinfo
.offset
= s
->len
;
825 sinfo
.name
= s
->name
;
826 of_elf64
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
828 /* end of debugging stuff */
830 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
831 error(ERR_WARNING
, "attempt to initialize memory in"
832 " BSS section `%s': ignored", s
->name
);
833 if (type
== OUT_REL2ADR
)
835 else if (type
== OUT_REL4ADR
)
841 if (type
== OUT_RESERVE
) {
842 if (s
->type
== SHT_PROGBITS
) {
843 error(ERR_WARNING
, "uninitialized space declared in"
844 " non-BSS section `%s': zeroing", s
->name
);
845 elf_sect_write(s
, NULL
, realbytes
);
848 } else if (type
== OUT_RAWDATA
) {
849 if (segment
!= NO_SEG
)
850 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
851 elf_sect_write(s
, data
, realbytes
);
852 } else if (type
== OUT_ADDRESS
) {
854 addr
= *(int32_t *)data
;
855 if (segment
!= NO_SEG
) {
857 error(ERR_NONFATAL
, "ELF format does not support"
858 " segment base references");
863 elf_add_reloc(s
, segment
, R_X86_64_16
);
866 elf_add_reloc(s
, segment
, R_X86_64_32
);
869 elf_add_reloc(s
, segment
, R_X86_64_64
);
872 error(ERR_PANIC
, "internal error elf64-hpa-871");
875 } else if (wrt
== elf_gotpc_sect
+ 1) {
877 * The user will supply GOT relative to $$. ELF
878 * will let us have GOT relative to $. So we
879 * need to fix up the data item by $-$$.
882 elf_add_reloc(s
, segment
, R_X86_64_GOTPCREL
);
883 } else if (wrt
== elf_gotoff_sect
+ 1) {
884 elf_add_reloc(s
, segment
, R_X86_64_GOTTPOFF
);
885 } else if (wrt
== elf_got_sect
+ 1) {
886 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
887 R_X86_64_GOT32
, TRUE
);
888 } else if (wrt
== elf_sym_sect
+ 1) {
892 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
896 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
900 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
904 error(ERR_PANIC
, "internal error elf64-hpa-903");
907 } else if (wrt
== elf_plt_sect
+ 1) {
908 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
909 "relative PLT references");
911 error(ERR_NONFATAL
, "ELF format does not support this"
913 wrt
= NO_SEG
; /* we can at least _try_ to continue */
921 if (realbytes
!= 8 && realbytes
!= 4 && segment
!= NO_SEG
) {
923 "Unsupported non-64-bit ELF relocation");
925 if (realbytes
== 4) WRITELONG(p
, addr
);
926 else WRITEDLONG(p
, (int64_t)addr
);
928 elf_sect_write(s
, mydata
, realbytes
);
929 } else if (type
== OUT_REL2ADR
) {
930 if (segment
== segto
)
931 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
932 if (segment
!= NO_SEG
&& segment
% 2) {
933 error(ERR_NONFATAL
, "ELF format does not support"
934 " segment base references");
937 elf_add_reloc(s
, segment
, R_X86_64_PC16
);
940 "Unsupported non-32-bit ELF relocation [2]");
944 WRITESHORT(p
, *(int32_t *)data
- realbytes
);
945 elf_sect_write(s
, mydata
, 2L);
946 } else if (type
== OUT_REL4ADR
) {
947 if (segment
== segto
)
948 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
949 if (segment
!= NO_SEG
&& segment
% 2) {
950 error(ERR_NONFATAL
, "ELF format does not support"
951 " segment base references");
954 elf_add_reloc(s
, segment
, R_X86_64_PC32
);
955 } else if (wrt
== elf_plt_sect
+ 1) {
956 elf_add_reloc(s
, segment
, R_X86_64_PLT32
);
957 } else if (wrt
== elf_gotpc_sect
+ 1 ||
958 wrt
== elf_gotoff_sect
+ 1 ||
959 wrt
== elf_got_sect
+ 1) {
960 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
961 "relative GOT references");
963 error(ERR_NONFATAL
, "ELF format does not support this"
965 wrt
= NO_SEG
; /* we can at least _try_ to continue */
969 WRITELONG(p
, *(int32_t *)data
- realbytes
);
970 elf_sect_write(s
, mydata
, 4L);
974 static void elf_write(void)
976 int nsections
, align
;
984 int32_t symtablen
, symtablocal
;
987 * Work out how many sections we will have. We have SHN_UNDEF,
988 * then the flexible user sections, then the four fixed
989 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
990 * then optionally relocation sections for the user sections.
992 if (of_elf64
.current_dfmt
== &df_stabs
)
995 nsections
= 5; /* SHN_UNDEF and the fixed ones */
997 add_sectname("", ".comment");
998 add_sectname("", ".shstrtab");
999 add_sectname("", ".symtab");
1000 add_sectname("", ".strtab");
1001 for (i
= 0; i
< nsects
; i
++) {
1002 nsections
++; /* for the section itself */
1003 if (sects
[i
]->head
) {
1004 nsections
++; /* for its relocations without addends*/
1005 add_sectname(".rela", sects
[i
]->name
);
1009 if (of_elf64
.current_dfmt
== &df_stabs
) {
1010 /* in case the debug information is wanted, just add these three sections... */
1011 add_sectname("", ".stab");
1012 add_sectname("", ".stabstr");
1013 add_sectname(".rel", ".stab");
1021 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1024 * Output the ELF header.
1026 fwrite("\177ELF\2\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
1027 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1028 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1029 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1030 fwriteint64_t(0L, elffp
); /* no entry point */
1031 fwriteint64_t(0L, elffp
); /* no program header table */
1032 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1033 * ELF header plus alignment */
1034 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1035 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1036 fwriteint16_t(0, elffp
); /* no program header table, again */
1037 fwriteint16_t(0, elffp
); /* still no program header table */
1038 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1039 fwriteint16_t(nsections
, elffp
); /* number of sections */
1040 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1041 * section header table */
1044 * Build the symbol table and relocation tables.
1046 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1047 for (i
= 0; i
< nsects
; i
++)
1049 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1053 * Now output the section header table.
1056 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1057 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1060 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1062 elf_section_header(0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1063 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1065 for (i
= 0; i
< nsects
; i
++) {
1066 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1067 (sects
[i
]->type
== SHT_PROGBITS
?
1068 sects
[i
]->data
: NULL
), TRUE
,
1069 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1071 scount
++; /* dito */
1073 elf_section_header(p
- shstrtab
, 1, 0, comment
, FALSE
, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1074 scount
++; /* dito */
1076 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, FALSE
, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1077 scount
++; /* dito */
1079 elf_section_header(p
- shstrtab
, 2, 0, symtab
, TRUE
, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1080 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1082 elf_section_header(p
- shstrtab
, 3, 0, strs
, TRUE
, strslen
, 0, 0, 1, 0); /* .strtab */
1083 for (i
= 0; i
< nsects
; i
++)
1084 if (sects
[i
]->head
) {
1086 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, TRUE
,
1087 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1089 if (of_elf64
.current_dfmt
== &df_stabs
) {
1090 /* for debugging information, create the last three sections
1091 which are the .stab , .stabstr and .rel.stab sections respectively */
1093 /* this function call creates the stab sections in memory */
1096 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1098 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, 0, stablen
,
1099 nsections
- 2, 0, 4, 12);
1102 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, 0,
1103 stabstrlen
, 0, 0, 4, 0);
1106 /* link -> symtable info -> section to refer to */
1107 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, 0,
1108 stabrellen
, symtabsection
, nsections
- 3, 4,
1112 fwrite(align_str
, align
, 1, elffp
);
1115 * Now output the sections.
1117 elf_write_sections();
1119 nasm_free(elf_sects
);
1123 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1125 struct SAA
*s
= saa_init(1L);
1127 uint8_t entry
[24], *p
;
1133 * First, an all-zeros entry, required by the ELF spec.
1135 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1140 * Next, an entry for the file name.
1143 WRITELONG(p
, 1); /* we know it's 1st thing in strtab */
1144 WRITESHORT(p
, 4); /* type FILE */
1145 WRITESHORT(p
, SHN_ABS
);
1146 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1147 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1148 saa_wbytes(s
, entry
, 24L);
1153 * Now some standard symbols defining the segments, for relocation
1156 for (i
= 1; i
<= nsects
+ 1; i
++) {
1158 WRITELONG(p
, 0); /* no symbol name */
1159 WRITESHORT(p
, 3); /* local section-type thing */
1160 WRITESHORT(p
, (i
== 1 ? SHN_ABS
: i
- 1)); /* the section id */
1161 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1162 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1163 saa_wbytes(s
, entry
, 24L);
1169 * Now the other local symbols.
1172 while ((sym
= saa_rstruct(syms
))) {
1173 if (sym
->type
& SYM_GLOBAL
)
1176 WRITELONG(p
, sym
->strpos
);
1177 WRITECHAR(p
, sym
->type
); /* local non-typed thing */
1178 WRITECHAR(p
, sym
->other
);
1179 WRITESHORT(p
, sym
->section
);
1180 WRITEDLONG(p
, (int64_t)sym
->value
);
1181 WRITEDLONG(p
, (int64_t)sym
->size
);
1182 saa_wbytes(s
, entry
, 24L);
1188 * Now the global symbols.
1191 while ((sym
= saa_rstruct(syms
))) {
1192 if (!(sym
->type
& SYM_GLOBAL
))
1195 WRITELONG(p
, sym
->strpos
);
1196 WRITECHAR(p
, sym
->type
); /* global non-typed thing */
1197 WRITECHAR(p
, sym
->other
);
1198 WRITESHORT(p
, sym
->section
);
1199 WRITEDLONG(p
, (int64_t)sym
->value
);
1200 WRITEDLONG(p
, (int64_t)sym
->size
);
1201 saa_wbytes(s
, entry
, 24L);
1208 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1211 uint8_t *p
, entry
[24];
1220 int64_t sym
= r
->symbol
;
1222 if (sym
>= GLOBAL_TEMP_BASE
)
1223 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 3) + nlocals
;
1226 WRITEDLONG(p
, r
->address
);
1227 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1228 WRITEDLONG(p
, (uint64_t) 0);
1229 saa_wbytes(s
, entry
, 24L);
1238 static void elf_section_header(int name
, int type
, int flags
,
1239 void *data
, int is_saa
, int32_t datalen
,
1240 int link
, int info
, int align
, int eltsize
)
1242 elf_sects
[elf_nsect
].data
= data
;
1243 elf_sects
[elf_nsect
].len
= datalen
;
1244 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1247 fwriteint32_t((int32_t)name
, elffp
);
1248 fwriteint32_t((int32_t)type
, elffp
);
1249 fwriteint64_t((int64_t)flags
, elffp
);
1250 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1251 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1252 fwriteint64_t(datalen
, elffp
);
1254 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1255 fwriteint32_t((int32_t)link
, elffp
);
1256 fwriteint32_t((int32_t)info
, elffp
);
1257 fwriteint64_t((int64_t)align
, elffp
);
1258 fwriteint64_t((int64_t)eltsize
, elffp
);
1261 static void elf_write_sections(void)
1264 for (i
= 0; i
< elf_nsect
; i
++)
1265 if (elf_sects
[i
].data
) {
1266 int32_t len
= elf_sects
[i
].len
;
1267 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1268 int32_t align
= reallen
- len
;
1269 if (elf_sects
[i
].is_saa
)
1270 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1272 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1273 fwrite(align_str
, align
, 1, elffp
);
1277 static void elf_sect_write(struct Section
*sect
,
1278 const uint8_t *data
, uint32_t len
)
1280 saa_wbytes(sect
->data
, data
, len
);
1284 static int32_t elf_segbase(int32_t segment
)
1289 static int elf_directive(char *directive
, char *value
, int pass
)
1297 static void elf_filename(char *inname
, char *outname
, efunc error
)
1299 strcpy(elf_module
, inname
);
1300 standard_extension(inname
, outname
, ".o", error
);
1303 static const char *elf_stdmac
[] = {
1304 "%define __SECT__ [section .text]",
1305 "%macro __NASM_CDecl__ 1",
1310 static int elf_set_info(enum geninfo type
, char **val
)
1317 static struct dfmt df_stabs
= {
1318 "ELF64 (X86_64) stabs debug format for Linux",
1329 struct dfmt
*elf64_debugs_arr
[2] = { &df_stabs
, NULL
};
1331 struct ofmt of_elf64
= {
1332 "ELF64 (x86_64) object files (e.g. Linux)",
1349 /* again, the stabs debugging stuff (code) */
1351 void stabs64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1359 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1363 if (!stabs_filename
) {
1364 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1365 strcpy(stabs_filename
, filename
);
1367 if (strcmp(stabs_filename
, filename
)) {
1368 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1369 in fact, this leak comes in quite handy to maintain a list of files
1370 encountered so far in the symbol lines... */
1372 /* why not nasm_free(stabs_filename); we're done with the old one */
1374 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1375 strcpy(stabs_filename
, filename
);
1379 currentline
= linenumber
;
1382 void stabs64_deflabel(char *name
, int32_t segment
, int32_t offset
, int is_global
,
1392 void stabs64_directive(const char *directive
, const char *params
)
1398 void stabs64_typevalue(int32_t type
)
1403 void stabs64_output(int type
, void *param
)
1405 struct symlininfo
*s
;
1406 struct linelist
*el
;
1407 if (type
== TY_STABSSYMLIN
) {
1408 if (stabs_immcall
) {
1409 s
= (struct symlininfo
*)param
;
1410 if (strcmp(s
->name
, ".text"))
1411 return; /* we are only interested in the text stuff */
1413 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1414 el
->info
.offset
= s
->offset
;
1415 el
->info
.section
= s
->section
;
1416 el
->info
.name
= s
->name
;
1417 el
->line
= currentline
;
1418 el
->filename
= stabs_filename
;
1421 stabslines
->last
->next
= el
;
1422 stabslines
->last
= el
;
1425 stabslines
->last
= el
;
1432 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1434 WRITELONG(p,n_strx); \
1435 WRITECHAR(p,n_type); \
1436 WRITECHAR(p,n_other); \
1437 WRITESHORT(p,n_desc); \
1438 WRITELONG(p,n_value); \
1441 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1443 void stabs64_generate(void)
1445 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1446 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1450 struct linelist
*ptr
;
1454 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1455 for (i
= 0; i
< numlinestabs
; i
++)
1459 if (numfiles
== 0) {
1460 allfiles
[0] = ptr
->filename
;
1463 for (i
= 0; i
< numfiles
; i
++) {
1464 if (!strcmp(allfiles
[i
], ptr
->filename
))
1467 if (i
>= numfiles
) {
1468 allfiles
[i
] = ptr
->filename
;
1475 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1476 for (i
= 0; i
< numfiles
; i
++) {
1477 fileidx
[i
] = strsize
;
1478 strsize
+= strlen(allfiles
[i
]) + 1;
1481 for (i
= 0; i
< numfiles
; i
++) {
1482 if (!strcmp(allfiles
[i
], elf_module
)) {
1488 /* worst case size of the stab buffer would be:
1489 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1492 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1493 sizeof(struct stabentry
));
1495 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1497 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1500 for (i
= 0; i
< numfiles
; i
++) {
1501 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1505 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1508 /* this is the first stab, its strx points to the filename of the
1509 the source-file, the n_desc field should be set to the number
1512 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1518 /* this is the stab for the main source file */
1519 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1521 /* relocation table entry */
1523 /* Since the above WRITE_STAB calls have already */
1524 /* created two entries, the index in the info.section */
1525 /* member must be adjusted by adding 3 */
1527 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1528 WRITEDLONG(rptr
, ((int64_t)(ptr
->info
.section
+ 3) << 32) | R_X86_64_32
);
1531 currfile
= mainfileindex
;
1535 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1536 /* oops file has changed... */
1537 for (i
= 0; i
< numfiles
; i
++)
1538 if (!strcmp(allfiles
[i
], ptr
->filename
))
1541 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1545 /* relocation table entry */
1547 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1548 WRITEDLONG(rptr
, ((int64_t)(ptr
->info
.section
+ 3) << 32) | R_X86_64_32
);
1551 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1554 /* relocation table entry */
1556 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1557 WRITEDLONG(rptr
, ((int64_t)(ptr
->info
.section
+ 3) << 32) | R_X86_64_32
);
1563 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1565 nasm_free(allfiles
);
1568 stablen
= (sptr
- sbuf
);
1569 stabrellen
= (rptr
- rbuf
);
1575 void stabs64_cleanup(void)
1577 struct linelist
*ptr
, *del
;
1589 nasm_free(stabrelbuf
);
1591 nasm_free(stabstrbuf
);