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.
20 /* Definitions in lieu of elf.h */
22 #define SHT_PROGBITS 1
23 #define SHT_RELA 4 /* Relocation entries with addends */
25 #define SHF_WRITE (1 << 0) /* Writable */
26 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
27 #define SHF_EXECINSTR (1 << 2) /* Executable */
28 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
29 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
30 #define R_X86_64_NONE 0 /* No reloc */
31 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
32 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
33 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
34 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
35 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
36 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
37 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
38 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
39 #define ET_REL 1 /* Relocatable file */
40 #define EM_X86_64 62 /* AMD x86-64 architecture */
41 typedef uint32_t Elf64_Word
;
42 typedef uint64_t Elf64_Xword
;
43 typedef uint64_t Elf64_Addr
;
44 typedef uint64_t Elf64_Off
;
47 Elf64_Word sh_name
; /* Section name (string tbl index) */
48 Elf64_Word sh_type
; /* Section type */
49 Elf64_Xword sh_flags
; /* Section flags */
50 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
51 Elf64_Off sh_offset
; /* Section file offset */
52 Elf64_Xword sh_size
; /* Section size in bytes */
53 Elf64_Word sh_link
; /* Link to another section */
54 Elf64_Word sh_info
; /* Additional section information */
55 Elf64_Xword sh_addralign
; /* Section alignment */
56 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
65 int64_t address
; /* relative to _start_ of section */
66 int64_t symbol
; /* ELF symbol info thingy */
67 int type
; /* type of relocation */
71 int32_t strpos
; /* string table position of name */
72 int32_t section
; /* section ID of the symbol */
73 int type
; /* symbol type */
74 int other
; /* symbol visibility */
75 int32_t value
; /* address, or COMMON variable align */
76 int32_t size
; /* size of symbol */
77 int32_t globnum
; /* symbol table offset if global */
78 struct Symbol
*next
; /* list of globals in each section */
79 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
80 char *name
; /* used temporarily if in above list */
86 uint32_t len
, size
, nrelocs
;
88 int type
; /* SHT_PROGBITS or SHT_NOBITS */
89 int align
; /* alignment: power of two */
90 uint32_t flags
; /* section flags */
94 struct Reloc
*head
, **tail
;
95 struct Symbol
*gsyms
; /* global symbols in section */
99 static struct Section
**sects
;
100 static int nsects
, sectlen
;
102 #define SHSTR_DELTA 256
103 static char *shstrtab
;
104 static int shstrtablen
, shstrtabsize
;
106 static struct SAA
*syms
;
107 static uint32_t nlocals
, nglobs
;
109 static int32_t def_seg
;
111 static struct RAA
*bsym
;
113 static struct SAA
*strs
;
114 static uint32_t strslen
;
118 static evalfunc evaluate
;
120 static struct Symbol
*fwds
;
122 static char elf_module
[FILENAME_MAX
];
124 extern struct ofmt of_elf64
;
128 #define SYM_SECTION 0x04
129 #define SYM_GLOBAL 0x10
130 #define SYM_NOTYPE 0x00
131 #define SYM_DATA 0x01
132 #define SYM_FUNCTION 0x02
134 #define STV_DEFAULT 0
135 #define STV_INTERNAL 1
137 #define STV_PROTECTED 3
139 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
141 #define SEG_ALIGN 16 /* alignment of sections in file */
142 #define SEG_ALIGN_1 (SEG_ALIGN-1)
144 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
146 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
147 static struct ELF_SECTDATA
{
152 static int elf_nsect
;
153 static int32_t elf_foffs
;
155 static void elf_write(void);
156 static void elf_sect_write(struct Section
*, const uint8_t *,
158 static void elf_section_header(int, int, int, void *, int, int32_t, int, int,
160 static void elf_write_sections(void);
161 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
162 static struct SAA
*elf_build_reltab(int32_t *, struct Reloc
*);
163 static void add_sectname(char *, char *);
165 /* this stuff is needed for the stabs debugging format */
166 #define N_SO 0x64 /* ID for main source file */
167 #define N_SOL 0x84 /* ID for sub-source file */
171 #define TY_STABSSYMLIN 0x40 /* ouch */
187 int section
; /* section index */
188 char *name
; /* shallow-copied pointer of section name */
192 struct symlininfo info
;
195 struct linelist
*next
;
196 struct linelist
*last
;
199 static struct linelist
*stabslines
= 0;
200 static int stabs_immcall
= 0;
201 static int currentline
= 0;
202 static int numlinestabs
= 0;
203 static char *stabs_filename
= 0;
204 static int symtabsection
;
205 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
206 static int stablen
, stabstrlen
, stabrellen
;
208 static struct dfmt df_stabs
;
210 void stabs64_init(struct ofmt
*, void *, FILE *, efunc
);
211 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
212 void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
213 void stabs64_directive(const char *, const char *);
214 void stabs64_typevalue(int32_t);
215 void stabs64_output(int, void *);
216 void stabs64_generate();
217 void stabs64_cleanup();
219 /* end of stabs debugging stuff */
222 * Special section numbers which are used to define ELF special
223 * symbols, which can be used with WRT to provide PIC relocation
226 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
227 static int32_t elf_got_sect
, elf_plt_sect
;
228 static int32_t elf_sym_sect
;
230 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
236 (void)ldef
; /* placate optimisers */
238 nsects
= sectlen
= 0;
239 syms
= saa_init((int32_t)sizeof(struct Symbol
));
240 nlocals
= nglobs
= 0;
243 saa_wbytes(strs
, "\0", 1L);
244 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
245 strslen
= 2 + strlen(elf_module
);
247 shstrtablen
= shstrtabsize
= 0;;
248 add_sectname("", "");
252 elf_gotpc_sect
= seg_alloc();
253 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
255 elf_gotoff_sect
= seg_alloc();
256 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
258 elf_got_sect
= seg_alloc();
259 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
261 elf_plt_sect
= seg_alloc();
262 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
264 elf_sym_sect
= seg_alloc();
265 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf64
,
268 def_seg
= seg_alloc();
271 static void elf_cleanup(int debuginfo
)
280 for (i
= 0; i
< nsects
; i
++) {
281 if (sects
[i
]->type
!= SHT_NOBITS
)
282 saa_free(sects
[i
]->data
);
284 saa_free(sects
[i
]->rel
);
285 while (sects
[i
]->head
) {
287 sects
[i
]->head
= sects
[i
]->head
->next
;
295 if (of_elf64
.current_dfmt
) {
296 of_elf64
.current_dfmt
->cleanup();
300 static void add_sectname(char *firsthalf
, char *secondhalf
)
302 int len
= strlen(firsthalf
) + strlen(secondhalf
);
303 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
304 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
305 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
306 strcat(shstrtab
+ shstrtablen
, secondhalf
);
307 shstrtablen
+= len
+ 1;
310 static int elf_make_section(char *name
, int type
, int flags
, int align
)
314 s
= nasm_malloc(sizeof(*s
));
316 if (type
!= SHT_NOBITS
)
317 s
->data
= saa_init(1L);
320 s
->len
= s
->size
= 0;
322 if (!strcmp(name
, ".text"))
325 s
->index
= seg_alloc();
326 add_sectname("", name
);
327 s
->name
= nasm_malloc(1 + strlen(name
));
328 strcpy(s
->name
, name
);
334 if (nsects
>= sectlen
)
336 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
342 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
345 int flags_and
, flags_or
, type
, align
, i
;
348 * Default is 64 bits.
356 while (*p
&& !isspace(*p
))
360 flags_and
= flags_or
= type
= align
= 0;
362 while (*p
&& isspace(*p
))
366 while (*p
&& !isspace(*p
))
370 while (*p
&& isspace(*p
))
373 if (!nasm_strnicmp(q
, "align=", 6)) {
377 if ((align
- 1) & align
) { /* means it's not a power of two */
378 error(ERR_NONFATAL
, "section alignment %d is not"
379 " a power of two", align
);
382 } else if (!nasm_stricmp(q
, "alloc")) {
383 flags_and
|= SHF_ALLOC
;
384 flags_or
|= SHF_ALLOC
;
385 } else if (!nasm_stricmp(q
, "noalloc")) {
386 flags_and
|= SHF_ALLOC
;
387 flags_or
&= ~SHF_ALLOC
;
388 } else if (!nasm_stricmp(q
, "exec")) {
389 flags_and
|= SHF_EXECINSTR
;
390 flags_or
|= SHF_EXECINSTR
;
391 } else if (!nasm_stricmp(q
, "noexec")) {
392 flags_and
|= SHF_EXECINSTR
;
393 flags_or
&= ~SHF_EXECINSTR
;
394 } else if (!nasm_stricmp(q
, "write")) {
395 flags_and
|= SHF_WRITE
;
396 flags_or
|= SHF_WRITE
;
397 } else if (!nasm_stricmp(q
, "nowrite")) {
398 flags_and
|= SHF_WRITE
;
399 flags_or
&= ~SHF_WRITE
;
400 } else if (!nasm_stricmp(q
, "progbits")) {
402 } else if (!nasm_stricmp(q
, "nobits")) {
407 if (!strcmp(name
, ".comment") ||
408 !strcmp(name
, ".shstrtab") ||
409 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
410 error(ERR_NONFATAL
, "attempt to redefine reserved section"
415 for (i
= 0; i
< nsects
; i
++)
416 if (!strcmp(name
, sects
[i
]->name
))
419 if (!strcmp(name
, ".text"))
420 i
= elf_make_section(name
, SHT_PROGBITS
,
421 SHF_ALLOC
| SHF_EXECINSTR
, 16);
422 else if (!strcmp(name
, ".rodata"))
423 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
424 else if (!strcmp(name
, ".data"))
425 i
= elf_make_section(name
, SHT_PROGBITS
,
426 SHF_ALLOC
| SHF_WRITE
, 4);
427 else if (!strcmp(name
, ".bss"))
428 i
= elf_make_section(name
, SHT_NOBITS
,
429 SHF_ALLOC
| SHF_WRITE
, 4);
431 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
433 sects
[i
]->type
= type
;
435 sects
[i
]->align
= align
;
436 sects
[i
]->flags
&= ~flags_and
;
437 sects
[i
]->flags
|= flags_or
;
438 } else if (pass
== 1) {
439 if (type
|| align
|| flags_and
)
440 error(ERR_WARNING
, "section attributes ignored on"
441 " redeclaration of section `%s'", name
);
444 return sects
[i
]->index
;
447 static void elf_deflabel(char *name
, int32_t segment
, int32_t offset
,
448 int is_global
, char *special
)
452 int special_used
= FALSE
;
454 #if defined(DEBUG) && DEBUG>2
456 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
457 name
, segment
, offset
, is_global
, special
);
459 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
461 * This is a NASM special symbol. We never allow it into
462 * the ELF symbol table, even if it's a valid one. If it
463 * _isn't_ a valid one, we should barf immediately.
465 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
466 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
467 strcmp(name
, "..sym"))
468 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
472 if (is_global
== 3) {
475 * Fix up a forward-reference symbol size from the first
478 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
479 if (!strcmp((*s
)->name
, name
)) {
480 struct tokenval tokval
;
484 while (*p
&& !isspace(*p
))
486 while (*p
&& isspace(*p
))
490 tokval
.t_type
= TOKEN_INVALID
;
491 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
494 error(ERR_NONFATAL
, "cannot use relocatable"
495 " expression as symbol size");
497 (*s
)->size
= reloc_value(e
);
501 * Remove it from the list of unresolved sizes.
503 nasm_free((*s
)->name
);
507 return; /* it wasn't an important one */
510 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
511 strslen
+= 1 + strlen(name
);
513 sym
= saa_wstruct(syms
);
516 sym
->type
= is_global
? SYM_GLOBAL
: 0;
517 sym
->other
= STV_DEFAULT
;
519 if (segment
== NO_SEG
)
520 sym
->section
= SHN_ABS
;
523 sym
->section
= SHN_UNDEF
;
524 if (nsects
== 0 && segment
== def_seg
) {
526 if (segment
!= elf_section_names(".text", 2, &tempint
))
528 "strange segment conditions in ELF driver");
529 sym
->section
= nsects
;
531 for (i
= 0; i
< nsects
; i
++)
532 if (segment
== sects
[i
]->index
) {
533 sym
->section
= i
+ 1;
539 if (is_global
== 2) {
542 sym
->section
= SHN_COMMON
;
544 * We have a common variable. Check the special text to see
545 * if it's a valid number and power of two; if so, store it
546 * as the alignment for the common variable.
550 sym
->value
= readnum(special
, &err
);
552 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
553 " valid number", special
);
554 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
555 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
556 " power of two", special
);
560 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
562 if (sym
->type
== SYM_GLOBAL
) {
564 * If sym->section == SHN_ABS, then the first line of the
565 * else section would cause a core dump, because its a reference
566 * beyond the end of the section array.
567 * This behaviour is exhibited by this code:
570 * To avoid such a crash, such requests are silently discarded.
571 * This may not be the best solution.
573 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
574 bsym
= raa_write(bsym
, segment
, nglobs
);
575 } else if (sym
->section
!= SHN_ABS
) {
577 * This is a global symbol; so we must add it to the linked
578 * list of global symbols in its section. We'll push it on
579 * the beginning of the list, because it doesn't matter
580 * much which end we put it on and it's easier like this.
582 * In addition, we check the special text for symbol
583 * type and size information.
585 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
586 sects
[sym
->section
- 1]->gsyms
= sym
;
589 int n
= strcspn(special
, " \t");
591 if (!nasm_strnicmp(special
, "function", n
))
592 sym
->type
|= SYM_FUNCTION
;
593 else if (!nasm_strnicmp(special
, "data", n
) ||
594 !nasm_strnicmp(special
, "object", n
))
595 sym
->type
|= SYM_DATA
;
596 else if (!nasm_strnicmp(special
, "notype", n
))
597 sym
->type
|= SYM_NOTYPE
;
599 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
603 while (isspace(*special
))
606 n
= strcspn(special
, " \t");
607 if (!nasm_strnicmp(special
, "default", n
))
608 sym
->other
= STV_DEFAULT
;
609 else if (!nasm_strnicmp(special
, "internal", n
))
610 sym
->other
= STV_INTERNAL
;
611 else if (!nasm_strnicmp(special
, "hidden", n
))
612 sym
->other
= STV_HIDDEN
;
613 else if (!nasm_strnicmp(special
, "protected", n
))
614 sym
->other
= STV_PROTECTED
;
621 struct tokenval tokval
;
624 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
626 while (special
[n
] && isspace(special
[n
]))
629 * We have a size expression; attempt to
633 stdscan_bufptr
= special
+ n
;
634 tokval
.t_type
= TOKEN_INVALID
;
635 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
640 sym
->name
= nasm_strdup(name
);
643 error(ERR_NONFATAL
, "cannot use relocatable"
644 " expression as symbol size");
646 sym
->size
= reloc_value(e
);
648 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
653 sym
->globnum
= nglobs
;
658 if (special
&& !special_used
)
659 error(ERR_NONFATAL
, "no special symbol features supported here");
662 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
666 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
667 sect
->tail
= &r
->next
;
670 r
->address
= sect
->len
;
671 if (segment
== NO_SEG
)
676 for (i
= 0; i
< nsects
; i
++)
677 if (segment
== sects
[i
]->index
)
680 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
688 * This routine deals with ..got and ..sym relocations: the more
689 * complicated kinds. In shared-library writing, some relocations
690 * with respect to global symbols must refer to the precise symbol
691 * rather than referring to an offset from the base of the section
692 * _containing_ the symbol. Such relocations call to this routine,
693 * which searches the symbol list for the symbol in question.
695 * R_386_GOT32 references require the _exact_ symbol address to be
696 * used; R_386_32 references can be at an offset from the symbol.
697 * The boolean argument `exact' tells us this.
699 * Return value is the adjusted value of `addr', having become an
700 * offset from the symbol rather than the section. Should always be
701 * zero when returning from an exact call.
703 * Limitation: if you define two symbols at the same place,
704 * confusion will occur.
706 * Inefficiency: we search, currently, using a linked list which
707 * isn't even necessarily sorted.
709 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
710 int32_t segment
, int32_t offset
,
715 struct Symbol
*sym
, *sm
;
719 * First look up the segment/offset pair and find a global
720 * symbol corresponding to it. If it's not one of our segments,
721 * then it must be an external symbol, in which case we're fine
722 * doing a normal elf_add_reloc after first sanity-checking
723 * that the offset from the symbol is zero.
726 for (i
= 0; i
< nsects
; i
++)
727 if (segment
== sects
[i
]->index
) {
732 if (exact
&& offset
!= 0)
733 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
734 " for this reference");
736 elf_add_reloc(sect
, segment
, type
);
742 * Find a symbol pointing _exactly_ at this one.
744 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
745 if (sym
->value
== offset
)
749 * Find the nearest symbol below this one.
752 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
753 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
757 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
758 " for this reference");
762 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
763 sect
->tail
= &r
->next
;
766 r
->address
= sect
->len
;
767 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
772 return offset
- sym
->value
;
775 static void elf_out(int32_t segto
, const void *data
, uint32_t type
,
776 int32_t segment
, int32_t wrt
)
779 int32_t realbytes
= type
& OUT_SIZMASK
;
781 uint8_t mydata
[16], *p
;
783 static struct symlininfo sinfo
;
787 #if defined(DEBUG) && DEBUG>2
789 " elf_out type: %x seg: %d bytes: %x data: %x\n",
790 (type
>> 24), segment
, realbytes
, *(int32_t *)data
);
794 * handle absolute-assembly (structure definitions)
796 if (segto
== NO_SEG
) {
797 if (type
!= OUT_RESERVE
)
798 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
804 for (i
= 0; i
< nsects
; i
++)
805 if (segto
== sects
[i
]->index
) {
810 int tempint
; /* ignored */
811 if (segto
!= elf_section_names(".text", 2, &tempint
))
812 error(ERR_PANIC
, "strange segment conditions in ELF driver");
814 s
= sects
[nsects
- 1];
819 /* again some stabs debugging stuff */
820 if (of_elf64
.current_dfmt
) {
821 sinfo
.offset
= s
->len
;
823 sinfo
.name
= s
->name
;
824 of_elf64
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
826 /* end of debugging stuff */
828 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
829 error(ERR_WARNING
, "attempt to initialize memory in"
830 " BSS section `%s': ignored", s
->name
);
831 if (type
== OUT_REL2ADR
)
833 else if (type
== OUT_REL4ADR
)
839 if (type
== OUT_RESERVE
) {
840 if (s
->type
== SHT_PROGBITS
) {
841 error(ERR_WARNING
, "uninitialized space declared in"
842 " non-BSS section `%s': zeroing", s
->name
);
843 elf_sect_write(s
, NULL
, realbytes
);
846 } else if (type
== OUT_RAWDATA
) {
847 if (segment
!= NO_SEG
)
848 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
849 elf_sect_write(s
, data
, realbytes
);
850 } else if (type
== OUT_ADDRESS
) {
852 addr
= *(int32_t *)data
;
853 if (segment
!= NO_SEG
) {
855 error(ERR_NONFATAL
, "ELF format does not support"
856 " segment base references");
859 if (realbytes
== 2) {
861 elf_add_reloc(s
, segment
, R_X86_64_16
);
863 elf_add_reloc(s
, segment
, R_X86_64_32
);
865 } else if (wrt
== elf_gotpc_sect
+ 1) {
867 * The user will supply GOT relative to $$. ELF
868 * will let us have GOT relative to $. So we
869 * need to fix up the data item by $-$$.
872 elf_add_reloc(s
, segment
, R_X86_64_GOTPCREL
);
873 } else if (wrt
== elf_gotoff_sect
+ 1) {
874 elf_add_reloc(s
, segment
, R_X86_64_GOTTPOFF
);
875 } else if (wrt
== elf_got_sect
+ 1) {
876 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
877 R_X86_64_GOT32
, TRUE
);
878 } else if (wrt
== elf_sym_sect
+ 1) {
879 if (realbytes
== 2) {
881 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
884 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
887 } else if (wrt
== elf_plt_sect
+ 1) {
888 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
889 "relative PLT references");
891 error(ERR_NONFATAL
, "ELF format does not support this"
893 wrt
= NO_SEG
; /* we can at least _try_ to continue */
901 if (realbytes
!= 8 && realbytes
!= 4 && segment
!= NO_SEG
) {
903 "Unsupported non-64-bit ELF relocation");
905 if (realbytes
== 4) WRITELONG(p
, addr
);
906 else WRITEDLONG(p
, (int64_t)addr
);
908 elf_sect_write(s
, mydata
, realbytes
);
909 } else if (type
== OUT_REL2ADR
) {
910 if (segment
== segto
)
911 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
912 if (segment
!= NO_SEG
&& segment
% 2) {
913 error(ERR_NONFATAL
, "ELF format does not support"
914 " segment base references");
917 error(ERR_WARNING
| ERR_WARN_GNUELF
,
918 "16-bit relocations in ELF is a GNU extension");
919 elf_add_reloc(s
, segment
, R_X86_64_PC16
);
922 "Unsupported non-32-bit ELF relocation [2]");
926 WRITESHORT(p
, *(int32_t *)data
- realbytes
);
927 elf_sect_write(s
, mydata
, 2L);
928 } else if (type
== OUT_REL4ADR
) {
929 if (segment
== segto
)
930 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
931 if (segment
!= NO_SEG
&& segment
% 2) {
932 error(ERR_NONFATAL
, "ELF format does not support"
933 " segment base references");
936 elf_add_reloc(s
, segment
, R_X86_64_PC32
);
937 } else if (wrt
== elf_plt_sect
+ 1) {
938 elf_add_reloc(s
, segment
, R_X86_64_PLT32
);
939 } else if (wrt
== elf_gotpc_sect
+ 1 ||
940 wrt
== elf_gotoff_sect
+ 1 ||
941 wrt
== elf_got_sect
+ 1) {
942 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
943 "relative GOT references");
945 error(ERR_NONFATAL
, "ELF format does not support this"
947 wrt
= NO_SEG
; /* we can at least _try_ to continue */
951 WRITELONG(p
, *(int32_t *)data
- realbytes
);
952 elf_sect_write(s
, mydata
, 4L);
956 static void elf_write(void)
958 int nsections
, align
;
966 int32_t symtablen
, symtablocal
;
969 * Work out how many sections we will have. We have SHN_UNDEF,
970 * then the flexible user sections, then the four fixed
971 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
972 * then optionally relocation sections for the user sections.
974 if (of_elf64
.current_dfmt
== &df_stabs
)
977 nsections
= 5; /* SHN_UNDEF and the fixed ones */
979 add_sectname("", ".comment");
980 add_sectname("", ".shstrtab");
981 add_sectname("", ".symtab");
982 add_sectname("", ".strtab");
983 for (i
= 0; i
< nsects
; i
++) {
984 nsections
++; /* for the section itself */
985 if (sects
[i
]->head
) {
986 nsections
++; /* for its relocations without addends*/
987 add_sectname(".rela", sects
[i
]->name
);
991 if (of_elf64
.current_dfmt
== &df_stabs
) {
992 /* in case the debug information is wanted, just add these three sections... */
993 add_sectname("", ".stab");
994 add_sectname("", ".stabstr");
995 add_sectname(".rel", ".stab");
1003 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1006 * Output the ELF header.
1008 fwrite("\177ELF\2\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
1009 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1010 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1011 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1012 fwriteint64_t(0L, elffp
); /* no entry point */
1013 fwriteint64_t(0L, elffp
); /* no program header table */
1014 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1015 * ELF header plus alignment */
1016 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1017 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1018 fwriteint16_t(0, elffp
); /* no program header table, again */
1019 fwriteint16_t(0, elffp
); /* still no program header table */
1020 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1021 fwriteint16_t(nsections
, elffp
); /* number of sections */
1022 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1023 * section header table */
1026 * Build the symbol table and relocation tables.
1028 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1029 for (i
= 0; i
< nsects
; i
++)
1031 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1035 * Now output the section header table.
1038 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1039 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1042 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1044 elf_section_header(0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1045 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1047 for (i
= 0; i
< nsects
; i
++) {
1048 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1049 (sects
[i
]->type
== SHT_PROGBITS
?
1050 sects
[i
]->data
: NULL
), TRUE
,
1051 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1053 scount
++; /* dito */
1055 elf_section_header(p
- shstrtab
, 1, 0, comment
, FALSE
, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1056 scount
++; /* dito */
1058 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, FALSE
, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1059 scount
++; /* dito */
1061 elf_section_header(p
- shstrtab
, 2, 0, symtab
, TRUE
, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1062 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1064 elf_section_header(p
- shstrtab
, 3, 0, strs
, TRUE
, strslen
, 0, 0, 1, 0); /* .strtab */
1065 for (i
= 0; i
< nsects
; i
++)
1066 if (sects
[i
]->head
) {
1068 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, TRUE
,
1069 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1071 if (of_elf64
.current_dfmt
== &df_stabs
) {
1072 /* for debugging information, create the last three sections
1073 which are the .stab , .stabstr and .rel.stab sections respectively */
1075 /* this function call creates the stab sections in memory */
1078 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1080 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, 0, stablen
,
1081 nsections
- 2, 0, 4, 12);
1084 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, 0,
1085 stabstrlen
, 0, 0, 4, 0);
1088 /* link -> symtable info -> section to refer to */
1089 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, 0,
1090 stabrellen
, symtabsection
, nsections
- 3, 4,
1094 fwrite(align_str
, align
, 1, elffp
);
1097 * Now output the sections.
1099 elf_write_sections();
1101 nasm_free(elf_sects
);
1105 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1107 struct SAA
*s
= saa_init(1L);
1109 uint8_t entry
[24], *p
;
1115 * First, an all-zeros entry, required by the ELF spec.
1117 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1122 * Next, an entry for the file name.
1125 WRITELONG(p
, 1); /* we know it's 1st thing in strtab */
1126 WRITESHORT(p
, 4); /* type FILE */
1127 WRITESHORT(p
, SHN_ABS
);
1128 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1129 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1130 saa_wbytes(s
, entry
, 24L);
1135 * Now some standard symbols defining the segments, for relocation
1138 for (i
= 1; i
<= nsects
+ 1; i
++) {
1140 WRITELONG(p
, 0); /* no symbol name */
1141 WRITESHORT(p
, 3); /* local section-type thing */
1142 WRITESHORT(p
, (i
== 1 ? SHN_ABS
: i
- 1)); /* the section id */
1143 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1144 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1145 saa_wbytes(s
, entry
, 24L);
1151 * Now the other local symbols.
1154 while ((sym
= saa_rstruct(syms
))) {
1155 if (sym
->type
& SYM_GLOBAL
)
1158 WRITELONG(p
, sym
->strpos
);
1159 WRITECHAR(p
, sym
->type
); /* local non-typed thing */
1160 WRITECHAR(p
, sym
->other
);
1161 WRITESHORT(p
, sym
->section
);
1162 WRITEDLONG(p
, (int64_t)sym
->value
);
1163 WRITEDLONG(p
, (int64_t)sym
->size
);
1164 saa_wbytes(s
, entry
, 24L);
1170 * Now the global symbols.
1173 while ((sym
= saa_rstruct(syms
))) {
1174 if (!(sym
->type
& SYM_GLOBAL
))
1177 WRITELONG(p
, sym
->strpos
);
1178 WRITECHAR(p
, sym
->type
); /* global non-typed thing */
1179 WRITECHAR(p
, sym
->other
);
1180 WRITESHORT(p
, sym
->section
);
1181 WRITEDLONG(p
, (int64_t)sym
->value
);
1182 WRITEDLONG(p
, (int64_t)sym
->size
);
1183 saa_wbytes(s
, entry
, 24L);
1190 static struct SAA
*elf_build_reltab(int32_t *len
, struct Reloc
*r
)
1193 uint8_t *p
, entry
[24];
1202 int64_t sym
= r
->symbol
;
1204 if (sym
>= GLOBAL_TEMP_BASE
)
1205 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 3) + nlocals
;
1208 WRITEDLONG(p
, r
->address
);
1209 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1210 WRITEDLONG(p
, (uint64_t) 0);
1211 saa_wbytes(s
, entry
, 24L);
1220 static void elf_section_header(int name
, int type
, int flags
,
1221 void *data
, int is_saa
, int32_t datalen
,
1222 int link
, int info
, int align
, int eltsize
)
1224 elf_sects
[elf_nsect
].data
= data
;
1225 elf_sects
[elf_nsect
].len
= datalen
;
1226 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1229 fwriteint32_t((int32_t)name
, elffp
);
1230 fwriteint32_t((int32_t)type
, elffp
);
1231 fwriteint64_t((int64_t)flags
, elffp
);
1232 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1233 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1234 fwriteint64_t(datalen
, elffp
);
1236 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1237 fwriteint32_t((int32_t)link
, elffp
);
1238 fwriteint32_t((int32_t)info
, elffp
);
1239 fwriteint64_t((int64_t)align
, elffp
);
1240 fwriteint64_t((int64_t)eltsize
, elffp
);
1243 static void elf_write_sections(void)
1246 for (i
= 0; i
< elf_nsect
; i
++)
1247 if (elf_sects
[i
].data
) {
1248 int32_t len
= elf_sects
[i
].len
;
1249 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1250 int32_t align
= reallen
- len
;
1251 if (elf_sects
[i
].is_saa
)
1252 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1254 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1255 fwrite(align_str
, align
, 1, elffp
);
1259 static void elf_sect_write(struct Section
*sect
,
1260 const uint8_t *data
, uint32_t len
)
1262 saa_wbytes(sect
->data
, data
, len
);
1266 static int32_t elf_segbase(int32_t segment
)
1271 static int elf_directive(char *directive
, char *value
, int pass
)
1279 static void elf_filename(char *inname
, char *outname
, efunc error
)
1281 strcpy(elf_module
, inname
);
1282 standard_extension(inname
, outname
, ".o", error
);
1285 static const char *elf_stdmac
[] = {
1286 "%define __SECT__ [section .text]",
1287 "%macro __NASM_CDecl__ 1",
1292 static int elf_set_info(enum geninfo type
, char **val
)
1299 static struct dfmt df_stabs
= {
1300 "ELF64 (X86_64) stabs debug format for Linux",
1311 struct dfmt
*elf64_debugs_arr
[2] = { &df_stabs
, NULL
};
1313 struct ofmt of_elf64
= {
1314 "ELF64 (x86_64) object files (e.g. Linux)",
1331 /* again, the stabs debugging stuff (code) */
1333 void stabs64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1341 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1345 if (!stabs_filename
) {
1346 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1347 strcpy(stabs_filename
, filename
);
1349 if (strcmp(stabs_filename
, filename
)) {
1350 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1351 in fact, this leak comes in quite handy to maintain a list of files
1352 encountered so far in the symbol lines... */
1354 /* why not nasm_free(stabs_filename); we're done with the old one */
1356 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1357 strcpy(stabs_filename
, filename
);
1361 currentline
= linenumber
;
1364 void stabs64_deflabel(char *name
, int32_t segment
, int32_t offset
, int is_global
,
1374 void stabs64_directive(const char *directive
, const char *params
)
1380 void stabs64_typevalue(int32_t type
)
1385 void stabs64_output(int type
, void *param
)
1387 struct symlininfo
*s
;
1388 struct linelist
*el
;
1389 if (type
== TY_STABSSYMLIN
) {
1390 if (stabs_immcall
) {
1391 s
= (struct symlininfo
*)param
;
1392 if (strcmp(s
->name
, ".text"))
1393 return; /* we are only interested in the text stuff */
1395 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1396 el
->info
.offset
= s
->offset
;
1397 el
->info
.section
= s
->section
;
1398 el
->info
.name
= s
->name
;
1399 el
->line
= currentline
;
1400 el
->filename
= stabs_filename
;
1403 stabslines
->last
->next
= el
;
1404 stabslines
->last
= el
;
1407 stabslines
->last
= el
;
1414 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1416 WRITELONG(p,n_strx); \
1417 WRITECHAR(p,n_type); \
1418 WRITECHAR(p,n_other); \
1419 WRITESHORT(p,n_desc); \
1420 WRITELONG(p,n_value); \
1423 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1425 void stabs64_generate(void)
1427 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1428 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1432 struct linelist
*ptr
;
1436 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1437 for (i
= 0; i
< numlinestabs
; i
++)
1441 if (numfiles
== 0) {
1442 allfiles
[0] = ptr
->filename
;
1445 for (i
= 0; i
< numfiles
; i
++) {
1446 if (!strcmp(allfiles
[i
], ptr
->filename
))
1449 if (i
>= numfiles
) {
1450 allfiles
[i
] = ptr
->filename
;
1457 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1458 for (i
= 0; i
< numfiles
; i
++) {
1459 fileidx
[i
] = strsize
;
1460 strsize
+= strlen(allfiles
[i
]) + 1;
1463 for (i
= 0; i
< numfiles
; i
++) {
1464 if (!strcmp(allfiles
[i
], elf_module
)) {
1470 /* worst case size of the stab buffer would be:
1471 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1474 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1475 sizeof(struct stabentry
));
1477 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1479 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1482 for (i
= 0; i
< numfiles
; i
++) {
1483 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1487 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1490 /* this is the first stab, its strx points to the filename of the
1491 the source-file, the n_desc field should be set to the number
1494 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1500 /* this is the stab for the main source file */
1501 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1503 /* relocation table entry */
1505 /* Since the above WRITE_STAB calls have already */
1506 /* created two entries, the index in the info.section */
1507 /* member must be adjusted by adding 3 */
1509 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1510 WRITEDLONG(rptr
, ((int64_t)(ptr
->info
.section
+ 3) << 32) | R_X86_64_32
);
1513 currfile
= mainfileindex
;
1517 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1518 /* oops file has changed... */
1519 for (i
= 0; i
< numfiles
; i
++)
1520 if (!strcmp(allfiles
[i
], ptr
->filename
))
1523 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1527 /* relocation table entry */
1529 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1530 WRITEDLONG(rptr
, ((int64_t)(ptr
->info
.section
+ 3) << 32) | R_X86_64_32
);
1533 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1536 /* relocation table entry */
1538 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1539 WRITEDLONG(rptr
, ((int64_t)(ptr
->info
.section
+ 3) << 32) | R_X86_64_32
);
1545 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1547 nasm_free(allfiles
);
1550 stablen
= (sptr
- sbuf
);
1551 stabrellen
= (rptr
- rbuf
);
1557 void stabs64_cleanup(void)
1559 struct linelist
*ptr
, *del
;
1571 nasm_free(stabrelbuf
);
1573 nasm_free(stabstrbuf
);