2 * ELF file handling for TCC
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* Define this to get some debug output during relocation processing. */
26 /********************************************************/
27 /* global variables */
29 /* elf version information */
37 #define nb_sym_versions s1->nb_sym_versions
38 #define sym_versions s1->sym_versions
39 #define nb_sym_to_version s1->nb_sym_to_version
40 #define sym_to_version s1->sym_to_version
41 #define dt_verneednum s1->dt_verneednum
42 #define versym_section s1->versym_section
43 #define verneed_section s1->verneed_section
45 /* special flag to indicate that the section should not be linked to the other ones */
46 #define SHF_PRIVATE 0x80000000
47 /* section is dynsymtab_section */
48 #define SHF_DYNSYM 0x40000000
50 /* ------------------------------------------------------------------------- */
52 ST_FUNC
void tccelf_new(TCCState
*s
)
56 dynarray_add(&s
->sections
, &s
->nb_sections
, NULL
);
58 /* create standard sections */
59 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
60 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
61 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
62 common_section
= new_section(s
, ".common", SHT_NOBITS
, SHF_PRIVATE
);
63 common_section
->sh_num
= SHN_COMMON
;
65 /* symbols are always generated for linking stage */
66 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
68 ".hashtab", SHF_PRIVATE
);
69 s
->symtab
= symtab_section
;
71 /* private symbol table for dynamic symbols */
72 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
|SHF_DYNSYM
,
74 ".dynhashtab", SHF_PRIVATE
);
75 get_sym_attr(s
, 0, 1);
78 #ifdef CONFIG_TCC_BCHECK
79 ST_FUNC
void tccelf_bounds_new(TCCState
*s
)
82 /* create bounds sections */
83 bounds_section
= new_section(s
, ".bounds",
84 SHT_PROGBITS
, SHF_ALLOC
);
85 lbounds_section
= new_section(s
, ".lbounds",
86 SHT_PROGBITS
, SHF_ALLOC
);
90 ST_FUNC
void tccelf_stab_new(TCCState
*s
)
93 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
94 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
95 stab_section
->link
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
96 put_elf_str(stab_section
->link
, "");
98 put_stabs(s
, "", 0, 0, 0, 0);
101 static void free_section(Section
*s
)
106 ST_FUNC
void tccelf_delete(TCCState
*s1
)
111 /* free symbol versions */
112 for (i
= 0; i
< nb_sym_versions
; i
++) {
113 tcc_free(sym_versions
[i
].version
);
114 tcc_free(sym_versions
[i
].lib
);
116 tcc_free(sym_versions
);
117 tcc_free(sym_to_version
);
120 /* free all sections */
121 for(i
= 1; i
< s1
->nb_sections
; i
++)
122 free_section(s1
->sections
[i
]);
123 dynarray_reset(&s1
->sections
, &s1
->nb_sections
);
125 for(i
= 0; i
< s1
->nb_priv_sections
; i
++)
126 free_section(s1
->priv_sections
[i
]);
127 dynarray_reset(&s1
->priv_sections
, &s1
->nb_priv_sections
);
129 /* free any loaded DLLs */
131 for ( i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
132 DLLReference
*ref
= s1
->loaded_dlls
[i
];
135 FreeLibrary((HMODULE
)ref
->handle
);
137 dlclose(ref
->handle
);
141 /* free loaded dlls array */
142 dynarray_reset(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
);
143 tcc_free(s1
->sym_attrs
);
145 symtab_section
= NULL
; /* for tccrun.c:rt_printline() */
148 /* save section data state */
149 ST_FUNC
void tccelf_begin_file(TCCState
*s1
)
152 for (i
= 1; i
< s1
->nb_sections
; i
++) {
154 s
->sh_offset
= s
->data_offset
;
156 /* disable symbol hashing during compilation */
157 s
= s1
->symtab
, s
->reloc
= s
->hash
, s
->hash
= NULL
;
158 #if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE
163 /* At the end of compilation, convert any UNDEF syms to global, and merge
164 with previously existing symbols */
165 ST_FUNC
void tccelf_end_file(TCCState
*s1
)
167 Section
*s
= s1
->symtab
;
168 int first_sym
, nb_syms
, *tr
, i
;
170 first_sym
= s
->sh_offset
/ sizeof (ElfSym
);
171 nb_syms
= s
->data_offset
/ sizeof (ElfSym
) - first_sym
;
172 s
->data_offset
= s
->sh_offset
;
173 s
->link
->data_offset
= s
->link
->sh_offset
;
174 s
->hash
= s
->reloc
, s
->reloc
= NULL
;
175 tr
= tcc_mallocz(nb_syms
* sizeof *tr
);
177 for (i
= 0; i
< nb_syms
; ++i
) {
178 ElfSym
*sym
= (ElfSym
*)s
->data
+ first_sym
+ i
;
179 if (sym
->st_shndx
== SHN_UNDEF
180 && ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
)
181 sym
->st_info
= ELFW(ST_INFO
)(STB_GLOBAL
, ELFW(ST_TYPE
)(sym
->st_info
));
182 tr
[i
] = set_elf_sym(s
, sym
->st_value
, sym
->st_size
, sym
->st_info
,
183 sym
->st_other
, sym
->st_shndx
, (char*)s
->link
->data
+ sym
->st_name
);
185 /* now update relocations */
186 for (i
= 1; i
< s1
->nb_sections
; i
++) {
187 Section
*sr
= s1
->sections
[i
];
188 if (sr
->sh_type
== SHT_RELX
&& sr
->link
== s
) {
189 ElfW_Rel
*rel
= (ElfW_Rel
*)(sr
->data
+ sr
->sh_offset
);
190 ElfW_Rel
*rel_end
= (ElfW_Rel
*)(sr
->data
+ sr
->data_offset
);
191 for (; rel
< rel_end
; ++rel
) {
192 int n
= ELFW(R_SYM
)(rel
->r_info
) - first_sym
;
193 //if (n < 0) tcc_error("internal: invalid symbol index in relocation");
194 rel
->r_info
= ELFW(R_INFO
)(tr
[n
], ELFW(R_TYPE
)(rel
->r_info
));
201 ST_FUNC Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
205 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
207 strcpy(sec
->name
, name
);
208 sec
->sh_type
= sh_type
;
209 sec
->sh_flags
= sh_flags
;
212 sec
->sh_addralign
= 2;
220 case SHT_GNU_verneed
:
222 sec
->sh_addralign
= PTR_SIZE
;
225 sec
->sh_addralign
= 1;
228 sec
->sh_addralign
= PTR_SIZE
; /* gcc/pcc default alignment */
232 if (sh_flags
& SHF_PRIVATE
) {
233 dynarray_add(&s1
->priv_sections
, &s1
->nb_priv_sections
, sec
);
235 sec
->sh_num
= s1
->nb_sections
;
236 dynarray_add(&s1
->sections
, &s1
->nb_sections
, sec
);
242 ST_FUNC Section
*new_symtab(TCCState
*s1
,
243 const char *symtab_name
, int sh_type
, int sh_flags
,
244 const char *strtab_name
,
245 const char *hash_name
, int hash_sh_flags
)
247 Section
*symtab
, *strtab
, *hash
;
248 int *ptr
, nb_buckets
;
250 symtab
= new_section(s1
, symtab_name
, sh_type
, sh_flags
);
251 symtab
->sh_entsize
= sizeof(ElfW(Sym
));
252 strtab
= new_section(s1
, strtab_name
, SHT_STRTAB
, sh_flags
);
253 put_elf_str(strtab
, "");
254 symtab
->link
= strtab
;
255 put_elf_sym(symtab
, 0, 0, 0, 0, 0, NULL
);
259 hash
= new_section(s1
, hash_name
, SHT_HASH
, hash_sh_flags
);
260 hash
->sh_entsize
= sizeof(int);
264 ptr
= section_ptr_add(hash
, (2 + nb_buckets
+ 1) * sizeof(int));
267 memset(ptr
+ 2, 0, (nb_buckets
+ 1) * sizeof(int));
271 /* realloc section and set its content to zero */
272 ST_FUNC
void section_realloc(Section
*sec
, unsigned long new_size
)
277 size
= sec
->data_allocated
;
280 while (size
< new_size
)
282 data
= tcc_realloc(sec
->data
, size
);
283 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
285 sec
->data_allocated
= size
;
288 /* reserve at least 'size' bytes aligned per 'align' in section
289 'sec' from current offset, and return the aligned offset */
290 ST_FUNC
size_t section_add(Section
*sec
, addr_t size
, int align
)
292 size_t offset
, offset1
;
294 offset
= (sec
->data_offset
+ align
- 1) & -align
;
295 offset1
= offset
+ size
;
296 if (sec
->sh_type
!= SHT_NOBITS
&& offset1
> sec
->data_allocated
)
297 section_realloc(sec
, offset1
);
298 sec
->data_offset
= offset1
;
299 if (align
> sec
->sh_addralign
)
300 sec
->sh_addralign
= align
;
304 /* reserve at least 'size' bytes in section 'sec' from
306 ST_FUNC
void *section_ptr_add(Section
*sec
, addr_t size
)
308 size_t offset
= section_add(sec
, size
, 1);
309 return sec
->data
+ offset
;
312 /* reserve at least 'size' bytes from section start */
313 ST_FUNC
void section_reserve(Section
*sec
, unsigned long size
)
315 if (size
> sec
->data_allocated
)
316 section_realloc(sec
, size
);
317 if (size
> sec
->data_offset
)
318 sec
->data_offset
= size
;
321 static Section
*find_section_create (TCCState
*s1
, const char *name
, int create
)
325 for(i
= 1; i
< s1
->nb_sections
; i
++) {
326 sec
= s1
->sections
[i
];
327 if (!strcmp(name
, sec
->name
))
330 /* sections are created as PROGBITS */
331 return create
? new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
) : NULL
;
334 /* return a reference to a section, and create it if it does not
336 ST_FUNC Section
*find_section(TCCState
*s1
, const char *name
)
338 return find_section_create (s1
, name
, 1);
341 /* ------------------------------------------------------------------------- */
343 ST_FUNC
int put_elf_str(Section
*s
, const char *sym
)
348 len
= strlen(sym
) + 1;
349 offset
= s
->data_offset
;
350 ptr
= section_ptr_add(s
, len
);
351 memmove(ptr
, sym
, len
);
355 /* elf symbol hashing function */
356 static unsigned long elf_hash(const unsigned char *name
)
358 unsigned long h
= 0, g
;
361 h
= (h
<< 4) + *name
++;
370 /* rebuild hash table of section s */
371 /* NOTE: we do factorize the hash table code to go faster */
372 static void rebuild_hash(Section
*s
, unsigned int nb_buckets
)
375 int *ptr
, *hash
, nb_syms
, sym_index
, h
;
376 unsigned char *strtab
;
378 strtab
= s
->link
->data
;
379 nb_syms
= s
->data_offset
/ sizeof(ElfW(Sym
));
382 nb_buckets
= ((int*)s
->hash
->data
)[0];
384 s
->hash
->data_offset
= 0;
385 ptr
= section_ptr_add(s
->hash
, (2 + nb_buckets
+ nb_syms
) * sizeof(int));
390 memset(hash
, 0, (nb_buckets
+ 1) * sizeof(int));
391 ptr
+= nb_buckets
+ 1;
393 sym
= (ElfW(Sym
) *)s
->data
+ 1;
394 for(sym_index
= 1; sym_index
< nb_syms
; sym_index
++) {
395 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
396 h
= elf_hash(strtab
+ sym
->st_name
) % nb_buckets
;
407 /* return the symbol number */
408 ST_FUNC
int put_elf_sym(Section
*s
, addr_t value
, unsigned long size
,
409 int info
, int other
, int shndx
, const char *name
)
411 int name_offset
, sym_index
;
416 sym
= section_ptr_add(s
, sizeof(ElfW(Sym
)));
418 name_offset
= put_elf_str(s
->link
, name
);
421 /* XXX: endianness */
422 sym
->st_name
= name_offset
;
423 sym
->st_value
= value
;
426 sym
->st_other
= other
;
427 sym
->st_shndx
= shndx
;
428 sym_index
= sym
- (ElfW(Sym
) *)s
->data
;
432 ptr
= section_ptr_add(hs
, sizeof(int));
433 base
= (int *)hs
->data
;
434 /* only add global or weak symbols. */
435 if (ELFW(ST_BIND
)(info
) != STB_LOCAL
) {
436 /* add another hashing entry */
438 h
= elf_hash((unsigned char *)s
->link
->data
+ name_offset
) % nbuckets
;
440 base
[2 + h
] = sym_index
;
442 /* we resize the hash table */
443 hs
->nb_hashed_syms
++;
444 if (hs
->nb_hashed_syms
> 2 * nbuckets
) {
445 rebuild_hash(s
, 2 * nbuckets
);
455 ST_FUNC
int find_elf_sym(Section
*s
, const char *name
)
459 int nbuckets
, sym_index
, h
;
465 nbuckets
= ((int *)hs
->data
)[0];
466 h
= elf_hash((unsigned char *) name
) % nbuckets
;
467 sym_index
= ((int *)hs
->data
)[2 + h
];
468 while (sym_index
!= 0) {
469 sym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
470 name1
= (char *) s
->link
->data
+ sym
->st_name
;
471 if (!strcmp(name
, name1
))
473 sym_index
= ((int *)hs
->data
)[2 + nbuckets
+ sym_index
];
478 /* return elf symbol value, signal error if 'err' is nonzero */
479 ST_FUNC addr_t
get_elf_sym_addr(TCCState
*s1
, const char *name
, int err
)
484 sym_index
= find_elf_sym(s1
->symtab
, name
);
485 sym
= &((ElfW(Sym
) *)s1
->symtab
->data
)[sym_index
];
486 if (!sym_index
|| sym
->st_shndx
== SHN_UNDEF
) {
488 tcc_error("%s not defined", name
);
491 return sym
->st_value
;
494 /* list elf symbol names and values */
495 ST_FUNC
void list_elf_symbols(TCCState
*s
, void *ctx
,
496 void (*symbol_cb
)(void *ctx
, const char *name
, const void *val
))
500 int sym_index
, end_sym
;
502 unsigned char sym_vis
, sym_bind
;
505 end_sym
= symtab
->data_offset
/ sizeof (ElfSym
);
506 for (sym_index
= 0; sym_index
< end_sym
; ++sym_index
) {
507 sym
= &((ElfW(Sym
) *)symtab
->data
)[sym_index
];
509 name
= (char *) symtab
->link
->data
+ sym
->st_name
;
510 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
511 sym_vis
= ELFW(ST_VISIBILITY
)(sym
->st_other
);
512 if (sym_bind
== STB_GLOBAL
&& sym_vis
== STV_DEFAULT
)
513 symbol_cb(ctx
, name
, (void*)(uintptr_t)sym
->st_value
);
518 /* return elf symbol value */
519 LIBTCCAPI
void *tcc_get_symbol(TCCState
*s
, const char *name
)
521 return (void*)(uintptr_t)get_elf_sym_addr(s
, name
, 0);
524 /* list elf symbol names and values */
525 LIBTCCAPI
void tcc_list_symbols(TCCState
*s
, void *ctx
,
526 void (*symbol_cb
)(void *ctx
, const char *name
, const void *val
))
528 list_elf_symbols(s
, ctx
, symbol_cb
);
531 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
532 /* return elf symbol value or error */
533 ST_FUNC
void* tcc_get_symbol_err(TCCState
*s
, const char *name
)
535 return (void*)(uintptr_t)get_elf_sym_addr(s
, name
, 1);
541 version_add (TCCState
*s1
)
545 ElfW(Verneed
) *vn
= NULL
;
547 int sym_index
, end_sym
, nb_versions
= 2, nb_entries
= 0;
551 if (0 == nb_sym_versions
)
553 versym_section
= new_section(s1
, ".gnu.version", SHT_GNU_versym
, SHF_ALLOC
);
554 versym_section
->sh_entsize
= sizeof(ElfW(Half
));
555 verneed_section
= new_section(s1
, ".gnu.version_r", SHT_GNU_verneed
, SHF_ALLOC
);
556 versym_section
->link
= s1
->dynsym
;
557 verneed_section
->link
= s1
->dynsym
->link
;
559 /* add needed symbols */
561 end_sym
= symtab
->data_offset
/ sizeof (ElfSym
);
562 versym
= section_ptr_add(versym_section
, end_sym
* sizeof(ElfW(Half
)));
563 for (sym_index
= 0; sym_index
< end_sym
; ++sym_index
) {
564 int dllindex
, verndx
;
565 sym
= &((ElfW(Sym
) *)symtab
->data
)[sym_index
];
566 name
= (char *) symtab
->link
->data
+ sym
->st_name
;
567 dllindex
= find_elf_sym(s1
->dynsymtab_section
, name
);
568 verndx
= (dllindex
&& dllindex
< nb_sym_to_version
)
569 ? sym_to_version
[dllindex
] : -1;
571 if (!sym_versions
[verndx
].out_index
)
572 sym_versions
[verndx
].out_index
= nb_versions
++;
573 versym
[sym_index
] = sym_versions
[verndx
].out_index
;
575 versym
[sym_index
] = 0;
577 /* generate verneed section */
578 for (i
= nb_sym_versions
; i
-- > 0;) {
579 struct sym_version
*sv
= &sym_versions
[i
];
580 int n_same_libs
= 0, prev
;
582 ElfW(Vernaux
) *vna
= 0;
583 if (sv
->out_index
< 1)
585 vnofs
= section_add(verneed_section
, sizeof(*vn
), 1);
586 vn
= (ElfW(Verneed
)*)(verneed_section
->data
+ vnofs
);
588 vn
->vn_file
= put_elf_str(verneed_section
->link
, sv
->lib
);
589 vn
->vn_aux
= sizeof (*vn
);
591 prev
= sv
->prev_same_lib
;
592 if (sv
->out_index
> 0) {
593 vna
= section_ptr_add(verneed_section
, sizeof(*vna
));
594 vna
->vna_hash
= elf_hash (sv
->version
);
596 vna
->vna_other
= sv
->out_index
;
598 vna
->vna_name
= put_elf_str(verneed_section
->link
, sv
->version
);
599 vna
->vna_next
= sizeof (*vna
);
602 sv
= &sym_versions
[prev
];
605 vn
= (ElfW(Verneed
)*)(verneed_section
->data
+ vnofs
);
606 vn
->vn_cnt
= n_same_libs
;
607 vn
->vn_next
= sizeof(*vn
) + n_same_libs
* sizeof(*vna
);
612 verneed_section
->sh_info
= nb_entries
;
613 dt_verneednum
= nb_entries
;
617 /* add an elf symbol : check if it is already defined and patch
618 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
619 ST_FUNC
int set_elf_sym(Section
*s
, addr_t value
, unsigned long size
,
620 int info
, int other
, int shndx
, const char *name
)
622 TCCState
*s1
= s
->s1
;
624 int sym_bind
, sym_index
, sym_type
, esym_bind
;
625 unsigned char sym_vis
, esym_vis
, new_vis
;
627 sym_bind
= ELFW(ST_BIND
)(info
);
628 sym_type
= ELFW(ST_TYPE
)(info
);
629 sym_vis
= ELFW(ST_VISIBILITY
)(other
);
631 if (sym_bind
!= STB_LOCAL
) {
632 /* we search global or weak symbols */
633 sym_index
= find_elf_sym(s
, name
);
636 esym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
637 if (esym
->st_value
== value
&& esym
->st_size
== size
&& esym
->st_info
== info
638 && esym
->st_other
== other
&& esym
->st_shndx
== shndx
)
640 if (esym
->st_shndx
!= SHN_UNDEF
) {
641 esym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
642 /* propagate the most constraining visibility */
643 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
644 esym_vis
= ELFW(ST_VISIBILITY
)(esym
->st_other
);
645 if (esym_vis
== STV_DEFAULT
) {
647 } else if (sym_vis
== STV_DEFAULT
) {
650 new_vis
= (esym_vis
< sym_vis
) ? esym_vis
: sym_vis
;
652 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
654 other
= esym
->st_other
; /* in case we have to patch esym */
655 if (shndx
== SHN_UNDEF
) {
656 /* ignore adding of undefined symbol if the
657 corresponding symbol is already defined */
658 } else if (sym_bind
== STB_GLOBAL
&& esym_bind
== STB_WEAK
) {
659 /* global overrides weak, so patch */
661 } else if (sym_bind
== STB_WEAK
&& esym_bind
== STB_GLOBAL
) {
662 /* weak is ignored if already global */
663 } else if (sym_bind
== STB_WEAK
&& esym_bind
== STB_WEAK
) {
664 /* keep first-found weak definition, ignore subsequents */
665 } else if (sym_vis
== STV_HIDDEN
|| sym_vis
== STV_INTERNAL
) {
666 /* ignore hidden symbols after */
667 } else if ((esym
->st_shndx
== SHN_COMMON
668 || esym
->st_shndx
== bss_section
->sh_num
)
669 && (shndx
< SHN_LORESERVE
670 && shndx
!= bss_section
->sh_num
)) {
671 /* data symbol gets precedence over common/bss */
673 } else if (shndx
== SHN_COMMON
|| shndx
== bss_section
->sh_num
) {
674 /* data symbol keeps precedence over common/bss */
675 } else if (s
->sh_flags
& SHF_DYNSYM
) {
676 /* we accept that two DLL define the same symbol */
677 } else if (esym
->st_other
& ST_ASM_SET
) {
678 /* If the existing symbol came from an asm .set
683 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
684 sym_bind
, shndx
, new_vis
, esym_bind
, esym
->st_shndx
, esym_vis
);
686 tcc_error_noabort("'%s' defined twice", name
);
690 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
691 esym
->st_shndx
= shndx
;
692 s1
->new_undef_sym
= 1;
693 esym
->st_value
= value
;
694 esym
->st_size
= size
;
695 esym
->st_other
= other
;
699 sym_index
= put_elf_sym(s
, value
, size
,
700 ELFW(ST_INFO
)(sym_bind
, sym_type
), other
,
707 ST_FUNC
void put_elf_reloca(Section
*symtab
, Section
*s
, unsigned long offset
,
708 int type
, int symbol
, addr_t addend
)
710 TCCState
*s1
= s
->s1
;
717 /* if no relocation section, create it */
718 snprintf(buf
, sizeof(buf
), REL_SECTION_FMT
, s
->name
);
719 /* if the symtab is allocated, then we consider the relocation
721 sr
= new_section(s
->s1
, buf
, SHT_RELX
, symtab
->sh_flags
);
722 sr
->sh_entsize
= sizeof(ElfW_Rel
);
724 sr
->sh_info
= s
->sh_num
;
727 rel
= section_ptr_add(sr
, sizeof(ElfW_Rel
));
728 rel
->r_offset
= offset
;
729 rel
->r_info
= ELFW(R_INFO
)(symbol
, type
);
730 #if SHT_RELX == SHT_RELA
731 rel
->r_addend
= addend
;
733 if (SHT_RELX
!= SHT_RELA
&& addend
)
734 tcc_error("non-zero addend on REL architecture");
737 ST_FUNC
void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
738 int type
, int symbol
)
740 put_elf_reloca(symtab
, s
, offset
, type
, symbol
, 0);
743 /* Remove relocations for section S->reloc starting at oldrelocoffset
744 that are to the same place, retaining the last of them. As side effect
745 the relocations are sorted. Possibly reduces the number of relocs. */
746 ST_FUNC
void squeeze_multi_relocs(Section
*s
, size_t oldrelocoffset
)
748 Section
*sr
= s
->reloc
;
753 if (oldrelocoffset
+ sizeof(*r
) >= sr
->data_offset
)
755 /* The relocs we're dealing with are the result of initializer parsing.
756 So they will be mostly in order and there aren't many of them.
757 Secondly we need a stable sort (which qsort isn't). We use
758 a simple insertion sort. */
759 for (a
= oldrelocoffset
+ sizeof(*r
); a
< sr
->data_offset
; a
+= sizeof(*r
)) {
760 ssize_t i
= a
- sizeof(*r
);
761 addr
= ((ElfW_Rel
*)(sr
->data
+ a
))->r_offset
;
762 for (; i
>= (ssize_t
)oldrelocoffset
&&
763 ((ElfW_Rel
*)(sr
->data
+ i
))->r_offset
> addr
; i
-= sizeof(*r
)) {
764 ElfW_Rel tmp
= *(ElfW_Rel
*)(sr
->data
+ a
);
765 *(ElfW_Rel
*)(sr
->data
+ a
) = *(ElfW_Rel
*)(sr
->data
+ i
);
766 *(ElfW_Rel
*)(sr
->data
+ i
) = tmp
;
770 r
= (ElfW_Rel
*)(sr
->data
+ oldrelocoffset
);
772 for (; r
< (ElfW_Rel
*)(sr
->data
+ sr
->data_offset
); r
++) {
773 if (dest
->r_offset
!= r
->r_offset
)
777 sr
->data_offset
= (unsigned char*)dest
- sr
->data
+ sizeof(*r
);
780 /* put stab debug information */
782 ST_FUNC
void put_stabs(TCCState
*s1
, const char *str
, int type
, int other
, int desc
,
787 sym
= section_ptr_add(stab_section
, sizeof(Stab_Sym
));
789 sym
->n_strx
= put_elf_str(stab_section
->link
, str
);
794 sym
->n_other
= other
;
796 sym
->n_value
= value
;
799 ST_FUNC
void put_stabs_r(TCCState
*s1
, const char *str
, int type
, int other
, int desc
,
800 unsigned long value
, Section
*sec
, int sym_index
)
802 put_stabs(s1
, str
, type
, other
, desc
, value
);
803 put_elf_reloc(symtab_section
, stab_section
,
804 stab_section
->data_offset
- sizeof(unsigned int),
805 R_DATA_32
, sym_index
);
808 ST_FUNC
void put_stabn(TCCState
*s1
, int type
, int other
, int desc
, int value
)
810 put_stabs(s1
, NULL
, type
, other
, desc
, value
);
813 ST_FUNC
void put_stabd(TCCState
*s1
, int type
, int other
, int desc
)
815 put_stabs(s1
, NULL
, type
, other
, desc
, 0);
818 ST_FUNC
struct sym_attr
*get_sym_attr(TCCState
*s1
, int index
, int alloc
)
821 struct sym_attr
*tab
;
823 if (index
>= s1
->nb_sym_attrs
) {
825 return s1
->sym_attrs
;
826 /* find immediately bigger power of 2 and reallocate array */
830 tab
= tcc_realloc(s1
->sym_attrs
, n
* sizeof(*s1
->sym_attrs
));
832 memset(s1
->sym_attrs
+ s1
->nb_sym_attrs
, 0,
833 (n
- s1
->nb_sym_attrs
) * sizeof(*s1
->sym_attrs
));
834 s1
->nb_sym_attrs
= n
;
836 return &s1
->sym_attrs
[index
];
839 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
840 using variable <elem> */
841 #define for_each_elem(sec, startoff, elem, type) \
842 for (elem = (type *) sec->data + startoff; \
843 elem < (type *) (sec->data + sec->data_offset); elem++)
845 /* In an ELF file symbol table, the local symbols must appear below
846 the global and weak ones. Since TCC cannot sort it while generating
847 the code, we must do it after. All the relocation tables are also
848 modified to take into account the symbol table sorting */
849 static void sort_syms(TCCState
*s1
, Section
*s
)
851 int *old_to_new_syms
;
859 nb_syms
= s
->data_offset
/ sizeof(ElfW(Sym
));
860 new_syms
= tcc_malloc(nb_syms
* sizeof(ElfW(Sym
)));
861 old_to_new_syms
= tcc_malloc(nb_syms
* sizeof(int));
863 /* first pass for local symbols */
864 p
= (ElfW(Sym
) *)s
->data
;
866 for(i
= 0; i
< nb_syms
; i
++) {
867 if (ELFW(ST_BIND
)(p
->st_info
) == STB_LOCAL
) {
868 old_to_new_syms
[i
] = q
- new_syms
;
873 /* save the number of local symbols in section header */
874 if( s
->sh_size
) /* this 'if' makes IDA happy */
875 s
->sh_info
= q
- new_syms
;
877 /* then second pass for non local symbols */
878 p
= (ElfW(Sym
) *)s
->data
;
879 for(i
= 0; i
< nb_syms
; i
++) {
880 if (ELFW(ST_BIND
)(p
->st_info
) != STB_LOCAL
) {
881 old_to_new_syms
[i
] = q
- new_syms
;
887 /* we copy the new symbols to the old */
888 memcpy(s
->data
, new_syms
, nb_syms
* sizeof(ElfW(Sym
)));
891 /* now we modify all the relocations */
892 for(i
= 1; i
< s1
->nb_sections
; i
++) {
893 sr
= s1
->sections
[i
];
894 if (sr
->sh_type
== SHT_RELX
&& sr
->link
== s
) {
895 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
896 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
897 type
= ELFW(R_TYPE
)(rel
->r_info
);
898 sym_index
= old_to_new_syms
[sym_index
];
899 rel
->r_info
= ELFW(R_INFO
)(sym_index
, type
);
904 tcc_free(old_to_new_syms
);
907 /* relocate symbol table, resolve undefined symbols if do_resolve is
908 true and output error if undefined symbol. */
909 ST_FUNC
void relocate_syms(TCCState
*s1
, Section
*symtab
, int do_resolve
)
912 int sym_bind
, sh_num
;
915 for_each_elem(symtab
, 1, sym
, ElfW(Sym
)) {
916 sh_num
= sym
->st_shndx
;
917 if (sh_num
== SHN_UNDEF
) {
918 name
= (char *) s1
->symtab
->link
->data
+ sym
->st_name
;
919 /* Use ld.so to resolve symbol for us (for tcc -run) */
921 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
922 void *addr
= dlsym(RTLD_DEFAULT
, name
);
924 sym
->st_value
= (addr_t
) addr
;
926 printf ("relocate_sym: %s -> 0x%lx\n", name
, sym
->st_value
);
931 /* if dynamic symbol exist, it will be used in relocate_section */
932 } else if (s1
->dynsym
&& find_elf_sym(s1
->dynsym
, name
))
934 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
936 if (!strcmp(name
, "_fp_hw"))
938 /* only weak symbols are accepted to be undefined. Their
940 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
941 if (sym_bind
== STB_WEAK
)
944 tcc_error_noabort("undefined symbol '%s'", name
);
945 } else if (sh_num
< SHN_LORESERVE
) {
946 /* add section base */
947 sym
->st_value
+= s1
->sections
[sym
->st_shndx
]->sh_addr
;
953 /* relocate a given section (CPU dependent) by applying the relocations
954 in the associated relocation section */
955 ST_FUNC
void relocate_section(TCCState
*s1
, Section
*s
)
957 Section
*sr
= s
->reloc
;
964 qrel
= (ElfW_Rel
*)sr
->data
;
966 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
967 ptr
= s
->data
+ rel
->r_offset
;
968 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
969 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
970 type
= ELFW(R_TYPE
)(rel
->r_info
);
972 #if SHT_RELX == SHT_RELA
973 tgt
+= rel
->r_addend
;
975 addr
= s
->sh_addr
+ rel
->r_offset
;
976 relocate(s1
, rel
, type
, ptr
, addr
, tgt
);
978 /* if the relocation is allocated, we change its symbol table */
979 if (sr
->sh_flags
& SHF_ALLOC
)
980 sr
->link
= s1
->dynsym
;
984 /* relocate relocation table in 'sr' */
985 static void relocate_rel(TCCState
*s1
, Section
*sr
)
990 s
= s1
->sections
[sr
->sh_info
];
991 for_each_elem(sr
, 0, rel
, ElfW_Rel
)
992 rel
->r_offset
+= s
->sh_addr
;
995 /* count the number of dynamic relocations so that we can reserve
997 static int prepare_dynamic_rel(TCCState
*s1
, Section
*sr
)
1000 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1002 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
1003 int sym_index
= ELFW(R_SYM
)(rel
->r_info
);
1004 int type
= ELFW(R_TYPE
)(rel
->r_info
);
1006 #if defined(TCC_TARGET_I386)
1008 if (!get_sym_attr(s1
, sym_index
, 0)->dyn_index
1009 && ((ElfW(Sym
)*)symtab_section
->data
+ sym_index
)->st_shndx
== SHN_UNDEF
) {
1010 /* don't fixup unresolved (weak) symbols */
1011 rel
->r_info
= ELFW(R_INFO
)(sym_index
, R_386_RELATIVE
);
1014 #elif defined(TCC_TARGET_X86_64)
1021 #if defined(TCC_TARGET_I386)
1023 #elif defined(TCC_TARGET_X86_64)
1026 if (get_sym_attr(s1
, sym_index
, 0)->dyn_index
)
1034 /* allocate the section */
1035 sr
->sh_flags
|= SHF_ALLOC
;
1036 sr
->sh_size
= count
* sizeof(ElfW_Rel
);
1042 static void build_got(TCCState
*s1
)
1044 /* if no got, then create it */
1045 s1
->got
= new_section(s1
, ".got", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
1046 s1
->got
->sh_entsize
= 4;
1047 set_elf_sym(symtab_section
, 0, 4, ELFW(ST_INFO
)(STB_GLOBAL
, STT_OBJECT
),
1048 0, s1
->got
->sh_num
, "_GLOBAL_OFFSET_TABLE_");
1049 /* keep space for _DYNAMIC pointer and two dummy got entries */
1050 section_ptr_add(s1
->got
, 3 * PTR_SIZE
);
1053 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
1054 in s1->symtab. When creating the dynamic symbol table entry for the GOT
1055 relocation, use 'size' and 'info' for the corresponding symbol metadata.
1056 Returns the offset of the GOT or (if any) PLT entry. */
1057 static struct sym_attr
* put_got_entry(TCCState
*s1
, int dyn_reloc_type
,
1063 struct sym_attr
*attr
;
1064 unsigned got_offset
;
1068 need_plt_entry
= (dyn_reloc_type
== R_JMP_SLOT
);
1069 attr
= get_sym_attr(s1
, sym_index
, 1);
1071 /* In case a function is both called and its address taken 2 GOT entries
1072 are created, one for taking the address (GOT) and the other for the PLT
1074 if (need_plt_entry
? attr
->plt_offset
: attr
->got_offset
)
1077 /* create the GOT entry */
1078 got_offset
= s1
->got
->data_offset
;
1079 section_ptr_add(s1
->got
, PTR_SIZE
);
1081 /* Create the GOT relocation that will insert the address of the object or
1082 function of interest in the GOT entry. This is a static relocation for
1083 memory output (dlsym will give us the address of symbols) and dynamic
1084 relocation otherwise (executable and DLLs). The relocation should be
1085 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
1086 associated to a PLT entry) but is currently done at load time for an
1089 sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1090 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1093 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
) {
1094 /* Hack alarm. We don't want to emit dynamic symbols
1095 and symbol based relocs for STB_LOCAL symbols, but rather
1096 want to resolve them directly. At this point the symbol
1097 values aren't final yet, so we must defer this. We will later
1098 have to create a RELATIVE reloc anyway, so we misuse the
1099 relocation slot to smuggle the symbol reference until
1100 fill_local_got_entries. Not that the sym_index is
1101 relative to symtab_section, not s1->dynsym! Nevertheless
1102 we use s1->dyn_sym so that if this is the first call
1103 that got->reloc is correctly created. Also note that
1104 RELATIVE relocs are not normally created for the .got,
1105 so the types serves as a marker for later (and is retained
1106 also for the final output, which is okay because then the
1107 got is just normal data). */
1108 put_elf_reloc(s1
->dynsym
, s1
->got
, got_offset
, R_RELATIVE
,
1111 if (0 == attr
->dyn_index
)
1112 attr
->dyn_index
= set_elf_sym(s1
->dynsym
, sym
->st_value
,
1113 sym
->st_size
, sym
->st_info
, 0,
1114 sym
->st_shndx
, name
);
1115 put_elf_reloc(s1
->dynsym
, s1
->got
, got_offset
, dyn_reloc_type
,
1119 put_elf_reloc(symtab_section
, s1
->got
, got_offset
, dyn_reloc_type
,
1123 if (need_plt_entry
) {
1125 s1
->plt
= new_section(s1
, ".plt", SHT_PROGBITS
,
1126 SHF_ALLOC
| SHF_EXECINSTR
);
1127 s1
->plt
->sh_entsize
= 4;
1130 attr
->plt_offset
= create_plt_entry(s1
, got_offset
, attr
);
1132 /* create a symbol 'sym@plt' for the PLT jump vector */
1134 if (len
> sizeof plt_name
- 5)
1135 len
= sizeof plt_name
- 5;
1136 memcpy(plt_name
, name
, len
);
1137 strcpy(plt_name
+ len
, "@plt");
1138 attr
->plt_sym
= put_elf_sym(s1
->symtab
, attr
->plt_offset
, sym
->st_size
,
1139 ELFW(ST_INFO
)(STB_GLOBAL
, STT_FUNC
), 0, s1
->plt
->sh_num
, plt_name
);
1142 attr
->got_offset
= got_offset
;
1148 /* build GOT and PLT entries */
1149 ST_FUNC
void build_got_entries(TCCState
*s1
)
1154 int i
, type
, gotplt_entry
, reloc_type
, sym_index
;
1155 struct sym_attr
*attr
;
1157 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1158 s
= s1
->sections
[i
];
1159 if (s
->sh_type
!= SHT_RELX
)
1161 /* no need to handle got relocations */
1162 if (s
->link
!= symtab_section
)
1164 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
1165 type
= ELFW(R_TYPE
)(rel
->r_info
);
1166 gotplt_entry
= gotplt_entry_type(type
);
1167 if (gotplt_entry
== -1)
1168 tcc_error ("Unknown relocation type for got: %d", type
);
1169 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
1170 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1172 if (gotplt_entry
== NO_GOTPLT_ENTRY
) {
1176 /* Automatically create PLT/GOT [entry] if it is an undefined
1177 reference (resolved at runtime), or the symbol is absolute,
1178 probably created by tcc_add_symbol, and thus on 64-bit
1179 targets might be too far from application code. */
1180 if (gotplt_entry
== AUTO_GOTPLT_ENTRY
) {
1181 if (sym
->st_shndx
== SHN_UNDEF
) {
1184 if (s1
->output_type
== TCC_OUTPUT_DLL
&& ! PCRELATIVE_DLLPLT
)
1186 /* Relocations for UNDEF symbols would normally need
1187 to be transferred into the executable or shared object.
1188 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1189 But TCC doesn't do that (at least for exes), so we
1190 need to resolve all such relocs locally. And that
1191 means PLT slots for functions in DLLs and COPY relocs for
1192 data symbols. COPY relocs were generated in
1193 bind_exe_dynsyms (and the symbol adjusted to be defined),
1194 and for functions we were generated a dynamic symbol
1195 of function type. */
1197 /* dynsym isn't set for -run :-/ */
1198 dynindex
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
1199 esym
= (ElfW(Sym
) *)s1
->dynsym
->data
+ dynindex
;
1201 && (ELFW(ST_TYPE
)(esym
->st_info
) == STT_FUNC
1202 || (ELFW(ST_TYPE
)(esym
->st_info
) == STT_NOTYPE
1203 && ELFW(ST_TYPE
)(sym
->st_info
) == STT_FUNC
)))
1206 } else if (!(sym
->st_shndx
== SHN_ABS
1207 #ifndef TCC_TARGET_ARM
1214 #ifdef TCC_TARGET_X86_64
1215 if ((type
== R_X86_64_PLT32
|| type
== R_X86_64_PC32
) &&
1216 sym
->st_shndx
!= SHN_UNDEF
&&
1217 (ELFW(ST_VISIBILITY
)(sym
->st_other
) != STV_DEFAULT
||
1218 ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
||
1219 s1
->output_type
== TCC_OUTPUT_EXE
)) {
1220 rel
->r_info
= ELFW(R_INFO
)(sym_index
, R_X86_64_PC32
);
1224 reloc_type
= code_reloc(type
);
1225 if (reloc_type
== -1)
1226 tcc_error ("Unknown relocation type: %d", type
);
1227 else if (reloc_type
!= 0) {
1229 reloc_type
= R_JMP_SLOT
;
1231 reloc_type
= R_GLOB_DAT
;
1236 if (gotplt_entry
== BUILD_GOT_ONLY
)
1239 attr
= put_got_entry(s1
, reloc_type
, sym_index
);
1241 if (reloc_type
== R_JMP_SLOT
)
1242 rel
->r_info
= ELFW(R_INFO
)(attr
->plt_sym
, type
);
1247 /* put dynamic tag */
1248 static void put_dt(Section
*dynamic
, int dt
, addr_t val
)
1251 dyn
= section_ptr_add(dynamic
, sizeof(ElfW(Dyn
)));
1253 dyn
->d_un
.d_val
= val
;
1257 static void add_init_array_defines(TCCState
*s1
, const char *section_name
)
1261 char sym_start
[1024];
1264 snprintf(sym_start
, sizeof(sym_start
), "__%s_start", section_name
+ 1);
1265 snprintf(sym_end
, sizeof(sym_end
), "__%s_end", section_name
+ 1);
1267 s
= find_section(s1
, section_name
);
1272 end_offset
= s
->data_offset
;
1275 set_elf_sym(symtab_section
,
1277 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1278 s
->sh_num
, sym_start
);
1279 set_elf_sym(symtab_section
,
1281 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1282 s
->sh_num
, sym_end
);
1285 #ifndef TCC_TARGET_PE
1286 static int tcc_add_support(TCCState
*s1
, const char *filename
)
1289 snprintf(buf
, sizeof(buf
), "%s/%s", s1
->tcc_lib_path
, filename
);
1290 return tcc_add_file(s1
, buf
);
1294 static void add_array (const char *section
, TCCState
*s1
, Sym
*sym
)
1299 s
= find_section(s1
, section
);
1301 s
->sh_flags
|= SHF_WRITE
;
1302 ptr
= section_ptr_add(s
, PTR_SIZE
);
1303 memset (ptr
, 0, PTR_SIZE
);
1304 put_elf_reloc (s1
->symtab
, s
, ptr
- s
->data
, R_DATA_PTR
, sym
->c
);
1308 ST_FUNC
void add_init_array (TCCState
*s1
, Sym
*sym
)
1310 add_array (".init_array", s1
, sym
);
1313 ST_FUNC
void add_fini_array (TCCState
*s1
, Sym
*sym
)
1315 add_array (".fini_array", s1
, sym
);
1318 /* add tcc runtime libraries */
1319 ST_FUNC
void tcc_add_runtime(TCCState
*s1
)
1322 #ifdef CONFIG_TCC_BCHECK
1325 tcc_add_pragma_libs(s1
);
1326 #ifndef TCC_TARGET_PE
1328 if (!s1
->nostdlib
) {
1329 tcc_add_library_err(s1
, "c");
1331 if (!s1
->static_link
) {
1332 if (TCC_LIBGCC
[0] == '/')
1333 tcc_add_file(s1
, TCC_LIBGCC
);
1335 tcc_add_dll(s1
, TCC_LIBGCC
, 0);
1338 #ifdef CONFIG_TCC_BCHECK
1339 if (s1
->do_bounds_check
&& s1
->output_type
!= TCC_OUTPUT_DLL
) {
1340 tcc_add_library_err(s1
, "pthread");
1341 tcc_add_library_err(s1
, "dl");
1342 tcc_add_support(s1
, TCC_LIBTCCB1
);
1345 tcc_add_support(s1
, TCC_LIBTCC1
);
1346 /* add crt end if not memory output */
1347 if (s1
->output_type
!= TCC_OUTPUT_MEMORY
)
1348 tcc_add_crt(s1
, "crtn.o");
1353 /* add various standard linker symbols (must be done after the
1354 sections are filled (for example after allocating common
1356 static void tcc_add_linker_symbols(TCCState
*s1
)
1362 set_elf_sym(symtab_section
,
1363 text_section
->data_offset
, 0,
1364 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1365 text_section
->sh_num
, "_etext");
1366 set_elf_sym(symtab_section
,
1367 data_section
->data_offset
, 0,
1368 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1369 data_section
->sh_num
, "_edata");
1370 set_elf_sym(symtab_section
,
1371 bss_section
->data_offset
, 0,
1372 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1373 bss_section
->sh_num
, "_end");
1374 #ifdef TCC_TARGET_RISCV64
1375 /* XXX should be .sdata+0x800, not .data+0x800 */
1376 set_elf_sym(symtab_section
,
1378 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1379 data_section
->sh_num
, "__global_pointer$");
1381 /* horrible new standard ldscript defines */
1382 add_init_array_defines(s1
, ".preinit_array");
1383 add_init_array_defines(s1
, ".init_array");
1384 add_init_array_defines(s1
, ".fini_array");
1386 /* add start and stop symbols for sections whose name can be
1388 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1389 s
= s1
->sections
[i
];
1390 if (s
->sh_type
== SHT_PROGBITS
&&
1391 (s
->sh_flags
& SHF_ALLOC
)) {
1395 /* check if section name can be expressed in C */
1401 if (!isid(ch
) && !isnum(ch
))
1405 snprintf(buf
, sizeof(buf
), "__start_%s", s
->name
);
1406 set_elf_sym(symtab_section
,
1408 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1410 snprintf(buf
, sizeof(buf
), "__stop_%s", s
->name
);
1411 set_elf_sym(symtab_section
,
1413 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1420 ST_FUNC
void resolve_common_syms(TCCState
*s1
)
1424 /* Allocate common symbols in BSS. */
1425 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1426 if (sym
->st_shndx
== SHN_COMMON
) {
1427 /* symbol alignment is in st_value for SHN_COMMONs */
1428 sym
->st_value
= section_add(bss_section
, sym
->st_size
,
1430 sym
->st_shndx
= bss_section
->sh_num
;
1434 /* Now assign linker provided symbols their value. */
1435 tcc_add_linker_symbols(s1
);
1438 static void tcc_output_binary(TCCState
*s1
, FILE *f
,
1439 const int *sec_order
)
1442 int i
, offset
, size
;
1445 for(i
=1;i
<s1
->nb_sections
;i
++) {
1446 s
= s1
->sections
[sec_order
[i
]];
1447 if (s
->sh_type
!= SHT_NOBITS
&&
1448 (s
->sh_flags
& SHF_ALLOC
)) {
1449 while (offset
< s
->sh_offset
) {
1454 fwrite(s
->data
, 1, size
, f
);
1460 #ifndef ELF_OBJ_ONLY
1461 ST_FUNC
void fill_got_entry(TCCState
*s1
, ElfW_Rel
*rel
)
1463 int sym_index
= ELFW(R_SYM
) (rel
->r_info
);
1464 ElfW(Sym
) *sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1465 struct sym_attr
*attr
= get_sym_attr(s1
, sym_index
, 0);
1466 unsigned offset
= attr
->got_offset
;
1470 section_reserve(s1
->got
, offset
+ PTR_SIZE
);
1471 #ifdef TCC_TARGET_X86_64
1472 write64le(s1
->got
->data
+ offset
, sym
->st_value
);
1474 write32le(s1
->got
->data
+ offset
, sym
->st_value
);
1478 /* Perform relocation to GOT or PLT entries */
1479 ST_FUNC
void fill_got(TCCState
*s1
)
1485 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1486 s
= s1
->sections
[i
];
1487 if (s
->sh_type
!= SHT_RELX
)
1489 /* no need to handle got relocations */
1490 if (s
->link
!= symtab_section
)
1492 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
1493 switch (ELFW(R_TYPE
) (rel
->r_info
)) {
1494 case R_X86_64_GOT32
:
1495 case R_X86_64_GOTPCREL
:
1496 case R_X86_64_GOTPCRELX
:
1497 case R_X86_64_REX_GOTPCRELX
:
1498 case R_X86_64_PLT32
:
1499 fill_got_entry(s1
, rel
);
1506 /* See put_got_entry for a description. This is the second stage
1507 where GOT references to local defined symbols are rewritten. */
1508 static void fill_local_got_entries(TCCState
*s1
)
1511 if (!s1
->got
->reloc
)
1513 for_each_elem(s1
->got
->reloc
, 0, rel
, ElfW_Rel
) {
1514 if (ELFW(R_TYPE
)(rel
->r_info
) == R_RELATIVE
) {
1515 int sym_index
= ELFW(R_SYM
) (rel
->r_info
);
1516 ElfW(Sym
) *sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1517 struct sym_attr
*attr
= get_sym_attr(s1
, sym_index
, 0);
1518 unsigned offset
= attr
->got_offset
;
1519 if (offset
!= rel
->r_offset
- s1
->got
->sh_addr
)
1520 tcc_error_noabort("huh");
1521 rel
->r_info
= ELFW(R_INFO
)(0, R_RELATIVE
);
1522 #if SHT_RELX == SHT_RELA
1523 rel
->r_addend
= sym
->st_value
;
1525 /* All our REL architectures also happen to be 32bit LE. */
1526 write32le(s1
->got
->data
+ offset
, sym
->st_value
);
1532 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1533 in shared libraries and export non local defined symbols to shared libraries
1534 if -rdynamic switch was given on command line */
1535 static void bind_exe_dynsyms(TCCState
*s1
)
1538 int sym_index
, index
;
1539 ElfW(Sym
) *sym
, *esym
;
1542 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1543 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1544 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1545 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1546 if (sym
->st_shndx
== SHN_UNDEF
) {
1547 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1548 sym_index
= find_elf_sym(s1
->dynsymtab_section
, name
);
1550 esym
= &((ElfW(Sym
) *)s1
->dynsymtab_section
->data
)[sym_index
];
1551 type
= ELFW(ST_TYPE
)(esym
->st_info
);
1552 if ((type
== STT_FUNC
) || (type
== STT_GNU_IFUNC
)) {
1553 /* Indirect functions shall have STT_FUNC type in executable
1554 * dynsym section. Indeed, a dlsym call following a lazy
1555 * resolution would pick the symbol value from the
1556 * executable dynsym entry which would contain the address
1557 * of the function wanted by the caller of dlsym instead of
1558 * the address of the function that would return that
1561 = put_elf_sym(s1
->dynsym
, 0, esym
->st_size
,
1562 ELFW(ST_INFO
)(STB_GLOBAL
,STT_FUNC
), 0, 0,
1564 int index
= sym
- (ElfW(Sym
) *) symtab_section
->data
;
1565 get_sym_attr(s1
, index
, 1)->dyn_index
= dynindex
;
1566 } else if (type
== STT_OBJECT
) {
1567 unsigned long offset
;
1569 offset
= bss_section
->data_offset
;
1570 /* XXX: which alignment ? */
1571 offset
= (offset
+ 16 - 1) & -16;
1572 set_elf_sym (s1
->symtab
, offset
, esym
->st_size
,
1573 esym
->st_info
, 0, bss_section
->sh_num
, name
);
1574 index
= put_elf_sym(s1
->dynsym
, offset
, esym
->st_size
,
1575 esym
->st_info
, 0, bss_section
->sh_num
,
1578 /* Ensure R_COPY works for weak symbol aliases */
1579 if (ELFW(ST_BIND
)(esym
->st_info
) == STB_WEAK
) {
1580 for_each_elem(s1
->dynsymtab_section
, 1, dynsym
, ElfW(Sym
)) {
1581 if ((dynsym
->st_value
== esym
->st_value
)
1582 && (ELFW(ST_BIND
)(dynsym
->st_info
) == STB_GLOBAL
)) {
1583 char *dynname
= (char *) s1
->dynsymtab_section
->link
->data
1585 put_elf_sym(s1
->dynsym
, offset
, dynsym
->st_size
,
1587 bss_section
->sh_num
, dynname
);
1593 put_elf_reloc(s1
->dynsym
, bss_section
,
1594 offset
, R_COPY
, index
);
1595 offset
+= esym
->st_size
;
1596 bss_section
->data_offset
= offset
;
1599 /* STB_WEAK undefined symbols are accepted */
1600 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1601 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
||
1602 !strcmp(name
, "_fp_hw")) {
1604 tcc_error_noabort("undefined symbol '%s'", name
);
1607 } else if (s1
->rdynamic
&& ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1608 /* if -rdynamic option, then export all non local symbols */
1609 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1610 set_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
, sym
->st_info
,
1611 0, sym
->st_shndx
, name
);
1616 /* Bind symbols of libraries: export all non local symbols of executable that
1617 are referenced by shared libraries. The reason is that the dynamic loader
1618 search symbol first in executable and then in libraries. Therefore a
1619 reference to a symbol already defined by a library can still be resolved by
1620 a symbol in the executable. */
1621 static void bind_libs_dynsyms(TCCState
*s1
)
1625 ElfW(Sym
) *sym
, *esym
;
1627 for_each_elem(s1
->dynsymtab_section
, 1, esym
, ElfW(Sym
)) {
1628 name
= (char *) s1
->dynsymtab_section
->link
->data
+ esym
->st_name
;
1629 sym_index
= find_elf_sym(symtab_section
, name
);
1630 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1631 if (sym_index
&& sym
->st_shndx
!= SHN_UNDEF
1632 && ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1633 set_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
,
1634 sym
->st_info
, 0, sym
->st_shndx
, name
);
1635 } else if (esym
->st_shndx
== SHN_UNDEF
) {
1636 /* weak symbols can stay undefined */
1637 if (ELFW(ST_BIND
)(esym
->st_info
) != STB_WEAK
)
1638 tcc_warning("undefined dynamic symbol '%s'", name
);
1643 /* Export all non local symbols. This is used by shared libraries so that the
1644 non local symbols they define can resolve a reference in another shared
1645 library or in the executable. Correspondingly, it allows undefined local
1646 symbols to be resolved by other shared libraries or by the executable. */
1647 static void export_global_syms(TCCState
*s1
)
1649 int dynindex
, index
;
1653 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1654 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1655 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1656 dynindex
= put_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
,
1657 sym
->st_info
, 0, sym
->st_shndx
, name
);
1658 index
= sym
- (ElfW(Sym
) *) symtab_section
->data
;
1659 get_sym_attr(s1
, index
, 1)->dyn_index
= dynindex
;
1665 /* Allocate strings for section names and decide if an unallocated section
1667 NOTE: the strsec section comes last, so its size is also correct ! */
1668 static int alloc_sec_names(TCCState
*s1
, int file_type
, Section
*strsec
)
1674 /* Allocate strings for section names */
1675 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1676 s
= s1
->sections
[i
];
1677 /* when generating a DLL, we include relocations but we may
1679 #ifndef ELF_OBJ_ONLY
1680 if (file_type
== TCC_OUTPUT_DLL
&&
1681 s
->sh_type
== SHT_RELX
&&
1682 !(s
->sh_flags
& SHF_ALLOC
) &&
1683 (s1
->sections
[s
->sh_info
]->sh_flags
& SHF_ALLOC
) &&
1684 prepare_dynamic_rel(s1
, s
)) {
1685 if (s1
->sections
[s
->sh_info
]->sh_flags
& SHF_EXECINSTR
)
1689 if ((s1
->do_debug
&& s
->sh_type
!= SHT_RELX
) ||
1690 file_type
== TCC_OUTPUT_OBJ
||
1691 (s
->sh_flags
& SHF_ALLOC
) ||
1692 i
== (s1
->nb_sections
- 1)) {
1693 /* we output all sections if debug or object file */
1694 s
->sh_size
= s
->data_offset
;
1696 if (s
->sh_size
|| (s
->sh_flags
& SHF_ALLOC
))
1697 s
->sh_name
= put_elf_str(strsec
, s
->name
);
1699 strsec
->sh_size
= strsec
->data_offset
;
1703 /* Info to be copied in dynamic section */
1707 unsigned long data_offset
;
1710 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1716 /* Assign sections to segments and decide how are sections laid out when loaded
1717 in memory. This function also fills corresponding program headers. */
1718 static int layout_sections(TCCState
*s1
, ElfW(Phdr
) *phdr
, int phnum
,
1719 Section
*interp
, Section
* strsec
,
1720 struct dyn_inf
*dyninf
, int *sec_order
)
1722 int i
, j
, k
, file_type
, sh_order_index
, file_offset
;
1723 unsigned long s_align
;
1729 file_type
= s1
->output_type
;
1732 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
)
1733 file_offset
= sizeof(ElfW(Ehdr
)) + phnum
* sizeof(ElfW(Phdr
));
1734 s_align
= ELF_PAGE_SIZE
;
1735 if (s1
->section_align
)
1736 s_align
= s1
->section_align
;
1739 if (s1
->has_text_addr
) {
1740 int a_offset
, p_offset
;
1741 addr
= s1
->text_addr
;
1742 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1744 a_offset
= (int) (addr
& (s_align
- 1));
1745 p_offset
= file_offset
& (s_align
- 1);
1746 if (a_offset
< p_offset
)
1747 a_offset
+= s_align
;
1748 file_offset
+= (a_offset
- p_offset
);
1750 if (file_type
== TCC_OUTPUT_DLL
)
1753 addr
= ELF_START_ADDR
;
1754 /* compute address after headers */
1755 addr
+= (file_offset
& (s_align
- 1));
1759 /* Leave one program headers for the program interpreter and one for
1760 the program header table itself if needed. These are done later as
1761 they require section layout to be done first. */
1765 /* dynamic relocation table information, for .dynamic section */
1766 dyninf
->rel_addr
= dyninf
->rel_size
= 0;
1767 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1768 dyninf
->bss_addr
= dyninf
->bss_size
= 0;
1771 for(j
= 0; j
< 2; j
++) {
1772 ph
->p_type
= PT_LOAD
;
1774 ph
->p_flags
= PF_R
| PF_X
;
1776 ph
->p_flags
= PF_R
| PF_W
;
1777 ph
->p_align
= s_align
;
1779 /* Decide the layout of sections loaded in memory. This must
1780 be done before program headers are filled since they contain
1781 info about the layout. We do the following ordering: interp,
1782 symbol tables, relocations, progbits, nobits */
1783 /* XXX: do faster and simpler sorting */
1784 for(k
= 0; k
< 5; k
++) {
1785 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1786 s
= s1
->sections
[i
];
1787 /* compute if section should be included */
1789 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_WRITE
)) !=
1793 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_WRITE
)) !=
1794 (SHF_ALLOC
| SHF_WRITE
))
1800 } else if (s
->sh_type
== SHT_DYNSYM
||
1801 s
->sh_type
== SHT_STRTAB
||
1802 s
->sh_type
== SHT_HASH
) {
1805 } else if (s
->sh_type
== SHT_RELX
) {
1808 } else if (s
->sh_type
== SHT_NOBITS
) {
1815 sec_order
[sh_order_index
++] = i
;
1817 /* section matches: we align it and add its size */
1819 addr
= (addr
+ s
->sh_addralign
- 1) &
1820 ~(s
->sh_addralign
- 1);
1821 file_offset
+= (int) ( addr
- tmp
);
1822 s
->sh_offset
= file_offset
;
1825 /* update program header infos */
1826 if (ph
->p_offset
== 0) {
1827 ph
->p_offset
= file_offset
;
1829 ph
->p_paddr
= ph
->p_vaddr
;
1831 /* update dynamic relocation infos */
1832 if (s
->sh_type
== SHT_RELX
) {
1833 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1834 if (!strcmp(strsec
->data
+ s
->sh_name
, ".rel.got")) {
1835 dyninf
->rel_addr
= addr
;
1836 dyninf
->rel_size
+= s
->sh_size
; /* XXX only first rel. */
1838 if (!strcmp(strsec
->data
+ s
->sh_name
, ".rel.bss")) {
1839 dyninf
->bss_addr
= addr
;
1840 dyninf
->bss_size
= s
->sh_size
; /* XXX only first rel. */
1843 if (dyninf
->rel_size
== 0)
1844 dyninf
->rel_addr
= addr
;
1845 dyninf
->rel_size
+= s
->sh_size
;
1849 if (s
->sh_type
!= SHT_NOBITS
)
1850 file_offset
+= s
->sh_size
;
1854 /* Make the first PT_LOAD segment include the program
1855 headers itself (and the ELF header as well), it'll
1856 come out with same memory use but will make various
1857 tools like binutils strip work better. */
1858 ph
->p_offset
&= ~(ph
->p_align
- 1);
1859 ph
->p_vaddr
&= ~(ph
->p_align
- 1);
1860 ph
->p_paddr
&= ~(ph
->p_align
- 1);
1862 ph
->p_filesz
= file_offset
- ph
->p_offset
;
1863 ph
->p_memsz
= addr
- ph
->p_vaddr
;
1866 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
) {
1867 /* if in the middle of a page, we duplicate the page in
1868 memory so that one copy is RX and the other is RW */
1869 if ((addr
& (s_align
- 1)) != 0)
1872 addr
= (addr
+ s_align
- 1) & ~(s_align
- 1);
1873 file_offset
= (file_offset
+ s_align
- 1) & ~(s_align
- 1);
1879 /* all other sections come after */
1880 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1881 s
= s1
->sections
[i
];
1882 if (phnum
> 0 && (s
->sh_flags
& SHF_ALLOC
))
1884 sec_order
[sh_order_index
++] = i
;
1886 file_offset
= (file_offset
+ s
->sh_addralign
- 1) &
1887 ~(s
->sh_addralign
- 1);
1888 s
->sh_offset
= file_offset
;
1889 if (s
->sh_type
!= SHT_NOBITS
)
1890 file_offset
+= s
->sh_size
;
1896 #ifndef ELF_OBJ_ONLY
1897 static void fill_unloadable_phdr(ElfW(Phdr
) *phdr
, int phnum
, Section
*interp
,
1902 /* if interpreter, then add corresponding program header */
1906 ph
->p_type
= PT_PHDR
;
1907 ph
->p_offset
= sizeof(ElfW(Ehdr
));
1908 ph
->p_filesz
= ph
->p_memsz
= phnum
* sizeof(ElfW(Phdr
));
1909 ph
->p_vaddr
= interp
->sh_addr
- ph
->p_filesz
;
1910 ph
->p_paddr
= ph
->p_vaddr
;
1911 ph
->p_flags
= PF_R
| PF_X
;
1912 ph
->p_align
= 4; /* interp->sh_addralign; */
1915 ph
->p_type
= PT_INTERP
;
1916 ph
->p_offset
= interp
->sh_offset
;
1917 ph
->p_vaddr
= interp
->sh_addr
;
1918 ph
->p_paddr
= ph
->p_vaddr
;
1919 ph
->p_filesz
= interp
->sh_size
;
1920 ph
->p_memsz
= interp
->sh_size
;
1922 ph
->p_align
= interp
->sh_addralign
;
1925 /* if dynamic section, then add corresponding program header */
1927 ph
= &phdr
[phnum
- 1];
1929 ph
->p_type
= PT_DYNAMIC
;
1930 ph
->p_offset
= dynamic
->sh_offset
;
1931 ph
->p_vaddr
= dynamic
->sh_addr
;
1932 ph
->p_paddr
= ph
->p_vaddr
;
1933 ph
->p_filesz
= dynamic
->sh_size
;
1934 ph
->p_memsz
= dynamic
->sh_size
;
1935 ph
->p_flags
= PF_R
| PF_W
;
1936 ph
->p_align
= dynamic
->sh_addralign
;
1940 /* Fill the dynamic section with tags describing the address and size of
1942 static void fill_dynamic(TCCState
*s1
, struct dyn_inf
*dyninf
)
1944 Section
*dynamic
= dyninf
->dynamic
;
1947 /* put dynamic section entries */
1948 put_dt(dynamic
, DT_HASH
, s1
->dynsym
->hash
->sh_addr
);
1949 put_dt(dynamic
, DT_STRTAB
, dyninf
->dynstr
->sh_addr
);
1950 put_dt(dynamic
, DT_SYMTAB
, s1
->dynsym
->sh_addr
);
1951 put_dt(dynamic
, DT_STRSZ
, dyninf
->dynstr
->data_offset
);
1952 put_dt(dynamic
, DT_SYMENT
, sizeof(ElfW(Sym
)));
1954 put_dt(dynamic
, DT_RELA
, dyninf
->rel_addr
);
1955 put_dt(dynamic
, DT_RELASZ
, dyninf
->rel_size
);
1956 put_dt(dynamic
, DT_RELAENT
, sizeof(ElfW_Rel
));
1958 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1959 put_dt(dynamic
, DT_PLTGOT
, s1
->got
->sh_addr
);
1960 put_dt(dynamic
, DT_PLTRELSZ
, dyninf
->rel_size
);
1961 put_dt(dynamic
, DT_JMPREL
, dyninf
->rel_addr
);
1962 put_dt(dynamic
, DT_PLTREL
, DT_REL
);
1963 put_dt(dynamic
, DT_REL
, dyninf
->bss_addr
);
1964 put_dt(dynamic
, DT_RELSZ
, dyninf
->bss_size
);
1966 put_dt(dynamic
, DT_REL
, dyninf
->rel_addr
);
1967 put_dt(dynamic
, DT_RELSZ
, dyninf
->rel_size
);
1968 put_dt(dynamic
, DT_RELENT
, sizeof(ElfW_Rel
));
1971 if (versym_section
) {
1972 put_dt(dynamic
, DT_VERNEED
, verneed_section
->sh_addr
);
1973 put_dt(dynamic
, DT_VERNEEDNUM
, dt_verneednum
);
1974 put_dt(dynamic
, DT_VERSYM
, versym_section
->sh_addr
);
1976 s
= find_section_create (s1
, ".preinit_array", 0);
1977 if (s
&& s
->data_offset
) {
1978 put_dt(dynamic
, DT_PREINIT_ARRAY
, s
->sh_addr
);
1979 put_dt(dynamic
, DT_PREINIT_ARRAYSZ
, s
->data_offset
);
1981 s
= find_section_create (s1
, ".init_array", 0);
1982 if (s
&& s
->data_offset
) {
1983 put_dt(dynamic
, DT_INIT_ARRAY
, s
->sh_addr
);
1984 put_dt(dynamic
, DT_INIT_ARRAYSZ
, s
->data_offset
);
1986 s
= find_section_create (s1
, ".fini_array", 0);
1987 if (s
&& s
->data_offset
) {
1988 put_dt(dynamic
, DT_FINI_ARRAY
, s
->sh_addr
);
1989 put_dt(dynamic
, DT_FINI_ARRAYSZ
, s
->data_offset
);
1991 s
= find_section_create (s1
, ".init", 0);
1992 if (s
&& s
->data_offset
) {
1993 put_dt(dynamic
, DT_INIT
, s
->sh_addr
);
1995 s
= find_section_create (s1
, ".fini", 0);
1996 if (s
&& s
->data_offset
) {
1997 put_dt(dynamic
, DT_FINI
, s
->sh_addr
);
2000 put_dt(dynamic
, DT_DEBUG
, 0);
2001 put_dt(dynamic
, DT_NULL
, 0);
2004 /* Relocate remaining sections and symbols (that is those not related to
2006 static int final_sections_reloc(TCCState
*s1
)
2011 relocate_syms(s1
, s1
->symtab
, 0);
2013 if (s1
->nb_errors
!= 0)
2016 /* relocate sections */
2017 /* XXX: ignore sections with allocated relocations ? */
2018 for(i
= 1; i
< s1
->nb_sections
; i
++) {
2019 s
= s1
->sections
[i
];
2020 if (s
->reloc
&& (s
!= s1
->got
|| s1
->static_link
))
2021 relocate_section(s1
, s
);
2024 /* relocate relocation entries if the relocation tables are
2025 allocated in the executable */
2026 for(i
= 1; i
< s1
->nb_sections
; i
++) {
2027 s
= s1
->sections
[i
];
2028 if ((s
->sh_flags
& SHF_ALLOC
) &&
2029 s
->sh_type
== SHT_RELX
) {
2030 relocate_rel(s1
, s
);
2037 /* Create an ELF file on disk.
2038 This function handle ELF specific layout requirements */
2039 static void tcc_output_elf(TCCState
*s1
, FILE *f
, int phnum
, ElfW(Phdr
) *phdr
,
2040 int file_offset
, int *sec_order
)
2042 int i
, shnum
, offset
, size
, file_type
;
2045 ElfW(Shdr
) shdr
, *sh
;
2047 file_type
= s1
->output_type
;
2048 shnum
= s1
->nb_sections
;
2050 memset(&ehdr
, 0, sizeof(ehdr
));
2053 ehdr
.e_phentsize
= sizeof(ElfW(Phdr
));
2054 ehdr
.e_phnum
= phnum
;
2055 ehdr
.e_phoff
= sizeof(ElfW(Ehdr
));
2059 file_offset
= (file_offset
+ 3) & -4;
2062 ehdr
.e_ident
[0] = ELFMAG0
;
2063 ehdr
.e_ident
[1] = ELFMAG1
;
2064 ehdr
.e_ident
[2] = ELFMAG2
;
2065 ehdr
.e_ident
[3] = ELFMAG3
;
2066 ehdr
.e_ident
[4] = ELFCLASSW
;
2067 ehdr
.e_ident
[5] = ELFDATA2LSB
;
2068 ehdr
.e_ident
[6] = EV_CURRENT
;
2069 #if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
2070 /* FIXME: should set only for freebsd _target_, but we exclude only PE target */
2071 ehdr
.e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
2073 #ifdef TCC_TARGET_ARM
2075 ehdr
.e_ident
[EI_OSABI
] = 0;
2076 ehdr
.e_flags
= EF_ARM_EABI_VER4
;
2077 if (file_type
== TCC_OUTPUT_EXE
|| file_type
== TCC_OUTPUT_DLL
)
2078 ehdr
.e_flags
|= EF_ARM_HASENTRY
;
2079 if (s1
->float_abi
== ARM_HARD_FLOAT
)
2080 ehdr
.e_flags
|= EF_ARM_VFP_FLOAT
;
2082 ehdr
.e_flags
|= EF_ARM_SOFT_FLOAT
;
2084 ehdr
.e_ident
[EI_OSABI
] = ELFOSABI_ARM
;
2086 #elif defined TCC_TARGET_RISCV64
2087 ehdr
.e_flags
= EF_RISCV_FLOAT_ABI_DOUBLE
;
2091 case TCC_OUTPUT_EXE
:
2092 ehdr
.e_type
= ET_EXEC
;
2093 ehdr
.e_entry
= get_elf_sym_addr(s1
, "_start", 1);
2095 case TCC_OUTPUT_DLL
:
2096 ehdr
.e_type
= ET_DYN
;
2097 ehdr
.e_entry
= text_section
->sh_addr
; /* XXX: is it correct ? */
2099 case TCC_OUTPUT_OBJ
:
2100 ehdr
.e_type
= ET_REL
;
2103 ehdr
.e_machine
= EM_TCC_TARGET
;
2104 ehdr
.e_version
= EV_CURRENT
;
2105 ehdr
.e_shoff
= file_offset
;
2106 ehdr
.e_ehsize
= sizeof(ElfW(Ehdr
));
2107 ehdr
.e_shentsize
= sizeof(ElfW(Shdr
));
2108 ehdr
.e_shnum
= shnum
;
2109 ehdr
.e_shstrndx
= shnum
- 1;
2111 fwrite(&ehdr
, 1, sizeof(ElfW(Ehdr
)), f
);
2112 fwrite(phdr
, 1, phnum
* sizeof(ElfW(Phdr
)), f
);
2113 offset
= sizeof(ElfW(Ehdr
)) + phnum
* sizeof(ElfW(Phdr
));
2115 sort_syms(s1
, symtab_section
);
2116 for(i
= 1; i
< s1
->nb_sections
; i
++) {
2117 s
= s1
->sections
[sec_order
[i
]];
2118 if (s
->sh_type
!= SHT_NOBITS
) {
2119 while (offset
< s
->sh_offset
) {
2125 fwrite(s
->data
, 1, size
, f
);
2130 /* output section headers */
2131 while (offset
< ehdr
.e_shoff
) {
2136 for(i
= 0; i
< s1
->nb_sections
; i
++) {
2138 memset(sh
, 0, sizeof(ElfW(Shdr
)));
2139 s
= s1
->sections
[i
];
2141 sh
->sh_name
= s
->sh_name
;
2142 sh
->sh_type
= s
->sh_type
;
2143 sh
->sh_flags
= s
->sh_flags
;
2144 sh
->sh_entsize
= s
->sh_entsize
;
2145 sh
->sh_info
= s
->sh_info
;
2147 sh
->sh_link
= s
->link
->sh_num
;
2148 sh
->sh_addralign
= s
->sh_addralign
;
2149 sh
->sh_addr
= s
->sh_addr
;
2150 sh
->sh_offset
= s
->sh_offset
;
2151 sh
->sh_size
= s
->sh_size
;
2153 fwrite(sh
, 1, sizeof(ElfW(Shdr
)), f
);
2157 /* Write an elf, coff or "binary" file */
2158 static int tcc_write_elf_file(TCCState
*s1
, const char *filename
, int phnum
,
2159 ElfW(Phdr
) *phdr
, int file_offset
, int *sec_order
)
2161 int fd
, mode
, file_type
;
2164 file_type
= s1
->output_type
;
2165 if (file_type
== TCC_OUTPUT_OBJ
)
2170 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, mode
);
2172 tcc_error_noabort("could not write '%s'", filename
);
2175 f
= fdopen(fd
, "wb");
2177 printf("<- %s\n", filename
);
2179 #ifdef TCC_TARGET_COFF
2180 if (s1
->output_format
== TCC_OUTPUT_FORMAT_COFF
)
2181 tcc_output_coff(s1
, f
);
2184 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
)
2185 tcc_output_elf(s1
, f
, phnum
, phdr
, file_offset
, sec_order
);
2187 tcc_output_binary(s1
, f
, sec_order
);
2193 #ifndef ELF_OBJ_ONLY
2194 /* Sort section headers by assigned sh_addr, remove sections
2195 that we aren't going to output. */
2196 static void tidy_section_headers(TCCState
*s1
, int *sec_order
)
2198 int i
, nnew
, l
, *backmap
;
2202 snew
= tcc_malloc(s1
->nb_sections
* sizeof(snew
[0]));
2203 backmap
= tcc_malloc(s1
->nb_sections
* sizeof(backmap
[0]));
2204 for (i
= 0, nnew
= 0, l
= s1
->nb_sections
; i
< s1
->nb_sections
; i
++) {
2205 s
= s1
->sections
[sec_order
[i
]];
2206 if (!i
|| s
->sh_name
) {
2207 backmap
[sec_order
[i
]] = nnew
;
2211 backmap
[sec_order
[i
]] = 0;
2215 for (i
= 0; i
< nnew
; i
++) {
2219 if (s
->sh_type
== SHT_RELX
)
2220 s
->sh_info
= backmap
[s
->sh_info
];
2224 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
))
2225 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
)
2226 sym
->st_shndx
= backmap
[sym
->st_shndx
];
2227 if( !s1
->static_link
) {
2228 for_each_elem(s1
->dynsym
, 1, sym
, ElfW(Sym
))
2229 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
)
2230 sym
->st_shndx
= backmap
[sym
->st_shndx
];
2232 for (i
= 0; i
< s1
->nb_sections
; i
++)
2234 tcc_free(s1
->sections
);
2235 s1
->sections
= snew
;
2236 s1
->nb_sections
= nnew
;
2241 /* Output an elf, coff or binary file */
2242 /* XXX: suppress unneeded sections */
2243 static int elf_output_file(TCCState
*s1
, const char *filename
)
2245 int ret
, phnum
, shnum
, file_type
, file_offset
, *sec_order
;
2246 struct dyn_inf dyninf
= {0};
2248 Section
*strsec
, *interp
, *dynamic
, *dynstr
;
2250 file_type
= s1
->output_type
;
2255 interp
= dynamic
= dynstr
= NULL
; /* avoid warning */
2257 #ifndef ELF_OBJ_ONLY
2258 if (file_type
!= TCC_OUTPUT_OBJ
) {
2259 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2260 tcc_add_runtime(s1
);
2261 resolve_common_syms(s1
);
2263 if (!s1
->static_link
) {
2264 if (file_type
== TCC_OUTPUT_EXE
) {
2266 /* allow override the dynamic loader */
2267 const char *elfint
= getenv("LD_SO");
2269 elfint
= DEFAULT_ELFINTERP(s1
);
2270 /* add interpreter section only if executable */
2271 interp
= new_section(s1
, ".interp", SHT_PROGBITS
, SHF_ALLOC
);
2272 interp
->sh_addralign
= 1;
2273 ptr
= section_ptr_add(interp
, 1 + strlen(elfint
));
2274 strcpy(ptr
, elfint
);
2277 /* add dynamic symbol table */
2278 s1
->dynsym
= new_symtab(s1
, ".dynsym", SHT_DYNSYM
, SHF_ALLOC
,
2280 ".hash", SHF_ALLOC
);
2281 dynstr
= s1
->dynsym
->link
;
2282 /* add dynamic section */
2283 dynamic
= new_section(s1
, ".dynamic", SHT_DYNAMIC
,
2284 SHF_ALLOC
| SHF_WRITE
);
2285 dynamic
->link
= dynstr
;
2286 dynamic
->sh_entsize
= sizeof(ElfW(Dyn
));
2290 if (file_type
== TCC_OUTPUT_EXE
) {
2291 bind_exe_dynsyms(s1
);
2294 bind_libs_dynsyms(s1
);
2296 /* shared library case: simply export all global symbols */
2297 export_global_syms(s1
);
2300 build_got_entries(s1
);
2305 /* we add a section for symbols */
2306 strsec
= new_section(s1
, ".shstrtab", SHT_STRTAB
, 0);
2307 put_elf_str(strsec
, "");
2309 /* Allocate strings for section names */
2310 ret
= alloc_sec_names(s1
, file_type
, strsec
);
2312 #ifndef ELF_OBJ_ONLY
2315 /* add a list of needed dlls */
2316 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
2317 DLLReference
*dllref
= s1
->loaded_dlls
[i
];
2318 if (dllref
->level
== 0)
2319 put_dt(dynamic
, DT_NEEDED
, put_elf_str(dynstr
, dllref
->name
));
2323 put_dt(dynamic
, s1
->enable_new_dtags
? DT_RUNPATH
: DT_RPATH
,
2324 put_elf_str(dynstr
, s1
->rpath
));
2326 if (file_type
== TCC_OUTPUT_DLL
) {
2328 put_dt(dynamic
, DT_SONAME
, put_elf_str(dynstr
, s1
->soname
));
2329 /* XXX: currently, since we do not handle PIC code, we
2330 must relocate the readonly segments */
2332 put_dt(dynamic
, DT_TEXTREL
, 0);
2336 put_dt(dynamic
, DT_SYMBOLIC
, 0);
2338 dyninf
.dynamic
= dynamic
;
2339 dyninf
.dynstr
= dynstr
;
2340 /* remember offset and reserve space for 2nd call below */
2341 dyninf
.data_offset
= dynamic
->data_offset
;
2342 fill_dynamic(s1
, &dyninf
);
2343 dynamic
->sh_size
= dynamic
->data_offset
;
2344 dynstr
->sh_size
= dynstr
->data_offset
;
2348 /* compute number of program headers */
2349 if (file_type
== TCC_OUTPUT_OBJ
)
2351 else if (file_type
== TCC_OUTPUT_DLL
)
2353 else if (s1
->static_link
)
2358 /* allocate program segment headers */
2359 phdr
= tcc_mallocz(phnum
* sizeof(ElfW(Phdr
)));
2361 /* compute number of sections */
2362 shnum
= s1
->nb_sections
;
2364 /* this array is used to reorder sections in the output file */
2365 sec_order
= tcc_malloc(sizeof(int) * shnum
);
2368 /* compute section to program header mapping */
2369 file_offset
= layout_sections(s1
, phdr
, phnum
, interp
, strsec
, &dyninf
,
2372 #ifndef ELF_OBJ_ONLY
2373 /* Fill remaining program header and finalize relocation related to dynamic
2375 if (file_type
!= TCC_OUTPUT_OBJ
) {
2376 fill_unloadable_phdr(phdr
, phnum
, interp
, dynamic
);
2379 dynamic
->data_offset
= dyninf
.data_offset
;
2380 fill_dynamic(s1
, &dyninf
);
2382 /* put in GOT the dynamic section address and relocate PLT */
2383 write32le(s1
->got
->data
, dynamic
->sh_addr
);
2384 if (file_type
== TCC_OUTPUT_EXE
2385 || (RELOCATE_DLLPLT
&& file_type
== TCC_OUTPUT_DLL
))
2388 /* relocate symbols in .dynsym now that final addresses are known */
2389 for_each_elem(s1
->dynsym
, 1, sym
, ElfW(Sym
)) {
2390 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
) {
2391 /* do symbol relocation */
2392 sym
->st_value
+= s1
->sections
[sym
->st_shndx
]->sh_addr
;
2397 /* if building executable or DLL, then relocate each section
2398 except the GOT which is already relocated */
2399 ret
= final_sections_reloc(s1
);
2402 tidy_section_headers(s1
, sec_order
);
2404 /* Perform relocation to GOT or PLT entries */
2405 if (file_type
== TCC_OUTPUT_EXE
&& s1
->static_link
)
2408 fill_local_got_entries(s1
);
2412 /* Create the ELF file with name 'filename' */
2413 ret
= tcc_write_elf_file(s1
, filename
, phnum
, phdr
, file_offset
, sec_order
);
2414 s1
->nb_sections
= shnum
;
2417 tcc_free(sec_order
);
2422 LIBTCCAPI
int tcc_output_file(TCCState
*s
, const char *filename
)
2425 #ifdef TCC_TARGET_PE
2426 if (s
->output_type
!= TCC_OUTPUT_OBJ
) {
2427 ret
= pe_output_file(s
, filename
);
2430 ret
= elf_output_file(s
, filename
);
2434 ssize_t
full_read(int fd
, void *buf
, size_t count
) {
2438 ssize_t num
= read(fd
, cbuf
, count
-rnum
);
2439 if (num
< 0) return num
;
2440 if (num
== 0) return rnum
;
2446 static void *load_data(int fd
, unsigned long file_offset
, unsigned long size
)
2450 data
= tcc_malloc(size
);
2451 lseek(fd
, file_offset
, SEEK_SET
);
2452 full_read(fd
, data
, size
);
2456 typedef struct SectionMergeInfo
{
2457 Section
*s
; /* corresponding existing section */
2458 unsigned long offset
; /* offset of the new section in the existing section */
2459 uint8_t new_section
; /* true if section 's' was added */
2460 uint8_t link_once
; /* true if link once section */
2463 ST_FUNC
int tcc_object_type(int fd
, ElfW(Ehdr
) *h
)
2465 int size
= full_read(fd
, h
, sizeof *h
);
2466 if (size
== sizeof *h
&& 0 == memcmp(h
, ELFMAG
, 4)) {
2467 if (h
->e_type
== ET_REL
)
2468 return AFF_BINTYPE_REL
;
2469 if (h
->e_type
== ET_DYN
)
2470 return AFF_BINTYPE_DYN
;
2471 } else if (size
>= 8) {
2472 if (0 == memcmp(h
, ARMAG
, 8))
2473 return AFF_BINTYPE_AR
;
2474 #ifdef TCC_TARGET_COFF
2475 if (((struct filehdr
*)h
)->f_magic
== COFF_C67_MAGIC
)
2476 return AFF_BINTYPE_C67
;
2482 /* load an object file and merge it with current files */
2483 /* XXX: handle correctly stab (debug) info */
2484 ST_FUNC
int tcc_load_object_file(TCCState
*s1
,
2485 int fd
, unsigned long file_offset
)
2488 ElfW(Shdr
) *shdr
, *sh
;
2489 int size
, i
, j
, offset
, offseti
, nb_syms
, sym_index
, ret
, seencompressed
;
2490 char *strsec
, *strtab
;
2491 int *old_to_new_syms
;
2492 char *sh_name
, *name
;
2493 SectionMergeInfo
*sm_table
, *sm
;
2494 ElfW(Sym
) *sym
, *symtab
;
2501 stab_index
= stabstr_index
= 0;
2503 lseek(fd
, file_offset
, SEEK_SET
);
2504 if (tcc_object_type(fd
, &ehdr
) != AFF_BINTYPE_REL
)
2506 /* test CPU specific stuff */
2507 if (ehdr
.e_ident
[5] != ELFDATA2LSB
||
2508 ehdr
.e_machine
!= EM_TCC_TARGET
) {
2510 tcc_error_noabort("invalid object file");
2514 shdr
= load_data(fd
, file_offset
+ ehdr
.e_shoff
,
2515 sizeof(ElfW(Shdr
)) * ehdr
.e_shnum
);
2516 sm_table
= tcc_mallocz(sizeof(SectionMergeInfo
) * ehdr
.e_shnum
);
2518 /* load section names */
2519 sh
= &shdr
[ehdr
.e_shstrndx
];
2520 strsec
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2522 /* load symtab and strtab */
2523 old_to_new_syms
= NULL
;
2528 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2530 if (sh
->sh_type
== SHT_SYMTAB
) {
2532 tcc_error_noabort("object must contain only one symtab");
2537 nb_syms
= sh
->sh_size
/ sizeof(ElfW(Sym
));
2538 symtab
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2539 sm_table
[i
].s
= symtab_section
;
2541 /* now load strtab */
2542 sh
= &shdr
[sh
->sh_link
];
2543 strtab
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2545 if (sh
->sh_flags
& SHF_COMPRESSED
)
2549 /* now examine each section and try to merge its content with the
2551 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2552 /* no need to examine section name strtab */
2553 if (i
== ehdr
.e_shstrndx
)
2556 if (sh
->sh_type
== SHT_RELX
)
2557 sh
= &shdr
[sh
->sh_info
];
2558 /* ignore sections types we do not handle (plus relocs to those) */
2559 if (sh
->sh_type
!= SHT_PROGBITS
&&
2561 sh
->sh_type
!= SHT_ARM_EXIDX
&&
2563 sh
->sh_type
!= SHT_NOBITS
&&
2564 sh
->sh_type
!= SHT_PREINIT_ARRAY
&&
2565 sh
->sh_type
!= SHT_INIT_ARRAY
&&
2566 sh
->sh_type
!= SHT_FINI_ARRAY
&&
2567 strcmp(strsec
+ sh
->sh_name
, ".stabstr")
2571 && !strncmp(strsec
+ sh
->sh_name
, ".debug_", sizeof(".debug_")-1))
2575 sh_name
= strsec
+ sh
->sh_name
;
2576 if (sh
->sh_addralign
< 1)
2577 sh
->sh_addralign
= 1;
2578 /* find corresponding section, if any */
2579 for(j
= 1; j
< s1
->nb_sections
;j
++) {
2580 s
= s1
->sections
[j
];
2581 if (!strcmp(s
->name
, sh_name
)) {
2582 if (!strncmp(sh_name
, ".gnu.linkonce",
2583 sizeof(".gnu.linkonce") - 1)) {
2584 /* if a 'linkonce' section is already present, we
2585 do not add it again. It is a little tricky as
2586 symbols can still be defined in
2588 sm_table
[i
].link_once
= 1;
2595 /* not found: create new section */
2596 s
= new_section(s1
, sh_name
, sh
->sh_type
, sh
->sh_flags
& ~SHF_GROUP
);
2597 /* take as much info as possible from the section. sh_link and
2598 sh_info will be updated later */
2599 s
->sh_addralign
= sh
->sh_addralign
;
2600 s
->sh_entsize
= sh
->sh_entsize
;
2601 sm_table
[i
].new_section
= 1;
2603 if (sh
->sh_type
!= s
->sh_type
) {
2604 tcc_error_noabort("invalid section type");
2608 /* align start of section */
2609 offset
= s
->data_offset
;
2611 if (0 == strcmp(sh_name
, ".stab")) {
2615 if (0 == strcmp(sh_name
, ".stabstr")) {
2620 size
= sh
->sh_addralign
- 1;
2621 offset
= (offset
+ size
) & ~size
;
2622 if (sh
->sh_addralign
> s
->sh_addralign
)
2623 s
->sh_addralign
= sh
->sh_addralign
;
2624 s
->data_offset
= offset
;
2626 sm_table
[i
].offset
= offset
;
2628 /* concatenate sections */
2630 if (sh
->sh_type
!= SHT_NOBITS
) {
2632 lseek(fd
, file_offset
+ sh
->sh_offset
, SEEK_SET
);
2633 ptr
= section_ptr_add(s
, size
);
2634 full_read(fd
, ptr
, size
);
2636 s
->data_offset
+= size
;
2641 /* gr relocate stab strings */
2642 if (stab_index
&& stabstr_index
) {
2645 s
= sm_table
[stab_index
].s
;
2646 a
= (Stab_Sym
*)(s
->data
+ sm_table
[stab_index
].offset
);
2647 b
= (Stab_Sym
*)(s
->data
+ s
->data_offset
);
2648 o
= sm_table
[stabstr_index
].offset
;
2656 /* second short pass to update sh_link and sh_info fields of new
2658 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2660 if (!s
|| !sm_table
[i
].new_section
)
2663 if (sh
->sh_link
> 0)
2664 s
->link
= sm_table
[sh
->sh_link
].s
;
2665 if (sh
->sh_type
== SHT_RELX
) {
2666 s
->sh_info
= sm_table
[sh
->sh_info
].s
->sh_num
;
2667 /* update backward link */
2668 s1
->sections
[s
->sh_info
]->reloc
= s
;
2673 /* resolve symbols */
2674 old_to_new_syms
= tcc_mallocz(nb_syms
* sizeof(int));
2677 for(i
= 1; i
< nb_syms
; i
++, sym
++) {
2678 if (sym
->st_shndx
!= SHN_UNDEF
&&
2679 sym
->st_shndx
< SHN_LORESERVE
) {
2680 sm
= &sm_table
[sym
->st_shndx
];
2681 if (sm
->link_once
) {
2682 /* if a symbol is in a link once section, we use the
2683 already defined symbol. It is very important to get
2684 correct relocations */
2685 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
2686 name
= strtab
+ sym
->st_name
;
2687 sym_index
= find_elf_sym(symtab_section
, name
);
2689 old_to_new_syms
[i
] = sym_index
;
2693 /* if no corresponding section added, no need to add symbol */
2696 /* convert section number */
2697 sym
->st_shndx
= sm
->s
->sh_num
;
2699 sym
->st_value
+= sm
->offset
;
2702 name
= strtab
+ sym
->st_name
;
2703 sym_index
= set_elf_sym(symtab_section
, sym
->st_value
, sym
->st_size
,
2704 sym
->st_info
, sym
->st_other
,
2705 sym
->st_shndx
, name
);
2706 old_to_new_syms
[i
] = sym_index
;
2709 /* third pass to patch relocation entries */
2710 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2715 offset
= sm_table
[i
].offset
;
2716 switch(s
->sh_type
) {
2718 /* take relocation offset information */
2719 offseti
= sm_table
[sh
->sh_info
].offset
;
2720 for_each_elem(s
, (offset
/ sizeof(*rel
)), rel
, ElfW_Rel
) {
2723 /* convert symbol index */
2724 type
= ELFW(R_TYPE
)(rel
->r_info
);
2725 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
2726 /* NOTE: only one symtab assumed */
2727 if (sym_index
>= nb_syms
)
2729 sym_index
= old_to_new_syms
[sym_index
];
2730 /* ignore link_once in rel section. */
2731 if (!sym_index
&& !sm
->link_once
2732 #ifdef TCC_TARGET_ARM
2733 && type
!= R_ARM_V4BX
2734 #elif defined TCC_TARGET_RISCV64
2735 && type
!= R_RISCV_ALIGN
2736 && type
!= R_RISCV_RELAX
2740 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2741 i
, strsec
+ sh
->sh_name
, rel
->r_offset
);
2744 rel
->r_info
= ELFW(R_INFO
)(sym_index
, type
);
2745 /* offset the relocation offset */
2746 rel
->r_offset
+= offseti
;
2747 #ifdef TCC_TARGET_ARM
2748 /* Jumps and branches from a Thumb code to a PLT entry need
2749 special handling since PLT entries are ARM code.
2750 Unconditional bl instructions referencing PLT entries are
2751 handled by converting these instructions into blx
2752 instructions. Other case of instructions referencing a PLT
2753 entry require to add a Thumb stub before the PLT entry to
2754 switch to ARM mode. We set bit plt_thumb_stub of the
2755 attribute of a symbol to indicate such a case. */
2756 if (type
== R_ARM_THM_JUMP24
)
2757 get_sym_attr(s1
, sym_index
, 1)->plt_thumb_stub
= 1;
2770 tcc_free(old_to_new_syms
);
2777 typedef struct ArchiveHeader
{
2778 char ar_name
[16]; /* name of this member */
2779 char ar_date
[12]; /* file mtime */
2780 char ar_uid
[6]; /* owner uid; printed as decimal */
2781 char ar_gid
[6]; /* owner gid; printed as decimal */
2782 char ar_mode
[8]; /* file mode, printed as octal */
2783 char ar_size
[10]; /* file size, printed as decimal */
2784 char ar_fmag
[2]; /* should contain ARFMAG */
2787 #define ARFMAG "`\n"
2789 static unsigned long long get_be(const uint8_t *b
, int n
)
2791 unsigned long long ret
= 0;
2793 ret
= (ret
<< 8) | *b
++, --n
;
2797 static int read_ar_header(int fd
, int offset
, ArchiveHeader
*hdr
)
2801 lseek(fd
, offset
, SEEK_SET
);
2802 len
= full_read(fd
, hdr
, sizeof(ArchiveHeader
));
2803 if (len
!= sizeof(ArchiveHeader
))
2804 return len
? -1 : 0;
2806 for (e
= p
+ sizeof hdr
->ar_name
; e
> p
&& e
[-1] == ' ';)
2809 hdr
->ar_size
[sizeof hdr
->ar_size
-1] = 0;
2813 /* load only the objects which resolve undefined symbols */
2814 static int tcc_load_alacarte(TCCState
*s1
, int fd
, int size
, int entrysize
)
2816 int i
, bound
, nsyms
, sym_index
, len
, ret
= -1;
2817 unsigned long long off
;
2819 const char *ar_names
, *p
;
2820 const uint8_t *ar_index
;
2824 data
= tcc_malloc(size
);
2825 if (full_read(fd
, data
, size
) != size
)
2827 nsyms
= get_be(data
, entrysize
);
2828 ar_index
= data
+ entrysize
;
2829 ar_names
= (char *) ar_index
+ nsyms
* entrysize
;
2833 for (p
= ar_names
, i
= 0; i
< nsyms
; i
++, p
+= strlen(p
)+1) {
2834 Section
*s
= symtab_section
;
2835 sym_index
= find_elf_sym(s
, p
);
2836 #ifdef TCC_TARGET_PE /* windows DLL's don't have UNDEF syms */
2837 if (sym_index
== 0) {
2838 s
= s1
->dynsymtab_section
;
2839 sym_index
= find_elf_sym(s
, p
);
2844 sym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
2845 if(sym
->st_shndx
!= SHN_UNDEF
)
2847 off
= get_be(ar_index
+ i
* entrysize
, entrysize
);
2848 len
= read_ar_header(fd
, off
, &hdr
);
2849 if (len
<= 0 || memcmp(hdr
.ar_fmag
, ARFMAG
, 2)) {
2850 tcc_error_noabort("invalid archive");
2854 if (s1
->verbose
== 2)
2855 printf(" -> %s\n", hdr
.ar_name
);
2856 if (tcc_load_object_file(s1
, fd
, off
) < 0)
2867 /* load a '.a' file */
2868 ST_FUNC
int tcc_load_archive(TCCState
*s1
, int fd
, int alacarte
)
2871 /* char magic[8]; */
2873 unsigned long file_offset
;
2876 /* skip magic which was already checked */
2877 /* full_read(fd, magic, sizeof(magic)); */
2878 file_offset
= sizeof ARMAG
- 1;
2881 len
= read_ar_header(fd
, file_offset
, &hdr
);
2885 tcc_error_noabort("invalid archive");
2889 size
= strtol(hdr
.ar_size
, NULL
, 0);
2891 size
= (size
+ 1) & ~1;
2893 /* coff symbol table : we handle it */
2894 if (!strcmp(hdr
.ar_name
, "/"))
2895 return tcc_load_alacarte(s1
, fd
, size
, 4);
2896 if (!strcmp(hdr
.ar_name
, "/SYM64/"))
2897 return tcc_load_alacarte(s1
, fd
, size
, 8);
2898 } else if (tcc_object_type(fd
, &ehdr
) == AFF_BINTYPE_REL
) {
2899 if (s1
->verbose
== 2)
2900 printf(" -> %s\n", hdr
.ar_name
);
2901 if (tcc_load_object_file(s1
, fd
, file_offset
) < 0)
2904 file_offset
+= size
;
2908 #ifndef ELF_OBJ_ONLY
2909 /* Set LV[I] to the global index of sym-version (LIB,VERSION). Maybe resizes
2910 LV, maybe create a new entry for (LIB,VERSION). */
2911 static void set_ver_to_ver(TCCState
*s1
, int *n
, int **lv
, int i
, char *lib
, char *version
)
2914 *lv
= tcc_realloc(*lv
, (*n
+ 1) * sizeof(**lv
));
2917 if ((*lv
)[i
] == -1) {
2918 int v
, prev_same_lib
= -1;
2919 for (v
= 0; v
< nb_sym_versions
; v
++) {
2920 if (strcmp(sym_versions
[v
].lib
, lib
))
2923 if (!strcmp(sym_versions
[v
].version
, version
))
2926 if (v
== nb_sym_versions
) {
2927 sym_versions
= tcc_realloc (sym_versions
,
2928 (v
+ 1) * sizeof(*sym_versions
));
2929 sym_versions
[v
].lib
= tcc_strdup(lib
);
2930 sym_versions
[v
].version
= tcc_strdup(version
);
2931 sym_versions
[v
].out_index
= 0;
2932 sym_versions
[v
].prev_same_lib
= prev_same_lib
;
2939 /* Associates symbol SYM_INDEX (in dynsymtab) with sym-version index
2942 set_sym_version(TCCState
*s1
, int sym_index
, int verndx
)
2944 if (sym_index
>= nb_sym_to_version
) {
2945 int newelems
= sym_index
? sym_index
* 2 : 1;
2946 sym_to_version
= tcc_realloc(sym_to_version
,
2947 newelems
* sizeof(*sym_to_version
));
2948 memset(sym_to_version
+ nb_sym_to_version
, -1,
2949 (newelems
- nb_sym_to_version
) * sizeof(*sym_to_version
));
2950 nb_sym_to_version
= newelems
;
2952 if (sym_to_version
[sym_index
] < 0)
2953 sym_to_version
[sym_index
] = verndx
;
2956 struct versym_info
{
2958 ElfW(Verdef
) *verdef
;
2959 ElfW(Verneed
) *verneed
;
2961 int nb_local_ver
, *local_ver
;
2965 static void store_version(TCCState
*s1
, struct versym_info
*v
, char *dynstr
)
2967 char *lib
, *version
;
2971 #define DEBUG_VERSION 0
2973 if (v
->versym
&& v
->verdef
) {
2974 ElfW(Verdef
) *vdef
= v
->verdef
;
2977 ElfW(Verdaux
) *verdaux
=
2978 (ElfW(Verdaux
) *) (((char *) vdef
) + vdef
->vd_aux
);
2981 printf ("verdef: version:%u flags:%u index:%u, hash:%u\n",
2982 vdef
->vd_version
, vdef
->vd_flags
, vdef
->vd_ndx
,
2986 version
= dynstr
+ verdaux
->vda_name
;
2991 set_ver_to_ver(s1
, &v
->nb_local_ver
, &v
->local_ver
, vdef
->vd_ndx
,
2994 printf (" verdaux(%u): %s\n", vdef
->vd_ndx
, version
);
2997 next
= vdef
->vd_next
;
2998 vdef
= (ElfW(Verdef
) *) (((char *) vdef
) + next
);
3001 if (v
->versym
&& v
->verneed
) {
3002 ElfW(Verneed
) *vneed
= v
->verneed
;
3004 ElfW(Vernaux
) *vernaux
=
3005 (ElfW(Vernaux
) *) (((char *) vneed
) + vneed
->vn_aux
);
3007 lib
= dynstr
+ vneed
->vn_file
;
3009 printf ("verneed: %u %s\n", vneed
->vn_version
, lib
);
3011 for (i
= 0; i
< vneed
->vn_cnt
; i
++) {
3012 if ((vernaux
->vna_other
& 0x8000) == 0) { /* hidden */
3013 version
= dynstr
+ vernaux
->vna_name
;
3014 set_ver_to_ver(s1
, &v
->nb_local_ver
, &v
->local_ver
, vernaux
->vna_other
,
3017 printf (" vernaux(%u): %u %u %s\n",
3018 vernaux
->vna_other
, vernaux
->vna_hash
,
3019 vernaux
->vna_flags
, version
);
3022 vernaux
= (ElfW(Vernaux
) *) (((char *) vernaux
) + vernaux
->vna_next
);
3024 next
= vneed
->vn_next
;
3025 vneed
= (ElfW(Verneed
) *) (((char *) vneed
) + next
);
3030 for (i
= 0; i
< v
->nb_local_ver
; i
++) {
3031 if (v
->local_ver
[i
] > 0) {
3032 printf ("%d: lib: %s, version %s\n",
3033 i
, sym_versions
[v
->local_ver
[i
]].lib
,
3034 sym_versions
[v
->local_ver
[i
]].version
);
3040 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
3041 is referenced by the user (so it should be added as DT_NEEDED in
3042 the generated ELF file) */
3043 ST_FUNC
int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
)
3046 ElfW(Shdr
) *shdr
, *sh
, *sh1
;
3047 int i
, j
, nb_syms
, nb_dts
, sym_bind
, ret
;
3048 ElfW(Sym
) *sym
, *dynsym
;
3049 ElfW(Dyn
) *dt
, *dynamic
;
3053 const char *name
, *soname
;
3054 DLLReference
*dllref
;
3055 struct versym_info v
;
3057 full_read(fd
, &ehdr
, sizeof(ehdr
));
3059 /* test CPU specific stuff */
3060 if (ehdr
.e_ident
[5] != ELFDATA2LSB
||
3061 ehdr
.e_machine
!= EM_TCC_TARGET
) {
3062 tcc_error_noabort("bad architecture");
3067 shdr
= load_data(fd
, ehdr
.e_shoff
, sizeof(ElfW(Shdr
)) * ehdr
.e_shnum
);
3069 /* load dynamic section and dynamic symbols */
3073 dynsym
= NULL
; /* avoid warning */
3074 dynstr
= NULL
; /* avoid warning */
3075 memset(&v
, 0, sizeof v
);
3077 for(i
= 0, sh
= shdr
; i
< ehdr
.e_shnum
; i
++, sh
++) {
3078 switch(sh
->sh_type
) {
3080 nb_dts
= sh
->sh_size
/ sizeof(ElfW(Dyn
));
3081 dynamic
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3084 nb_syms
= sh
->sh_size
/ sizeof(ElfW(Sym
));
3085 dynsym
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3086 sh1
= &shdr
[sh
->sh_link
];
3087 dynstr
= load_data(fd
, sh1
->sh_offset
, sh1
->sh_size
);
3089 case SHT_GNU_verdef
:
3090 v
.verdef
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3092 case SHT_GNU_verneed
:
3093 v
.verneed
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3095 case SHT_GNU_versym
:
3096 v
.nb_versyms
= sh
->sh_size
/ sizeof(ElfW(Half
));
3097 v
.versym
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3104 /* compute the real library name */
3105 soname
= tcc_basename(filename
);
3107 for(i
= 0, dt
= dynamic
; i
< nb_dts
; i
++, dt
++) {
3108 if (dt
->d_tag
== DT_SONAME
) {
3109 soname
= dynstr
+ dt
->d_un
.d_val
;
3113 /* if the dll is already loaded, do not load it */
3114 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
3115 dllref
= s1
->loaded_dlls
[i
];
3116 if (!strcmp(soname
, dllref
->name
)) {
3117 /* but update level if needed */
3118 if (level
< dllref
->level
)
3119 dllref
->level
= level
;
3125 if (v
.nb_versyms
!= nb_syms
)
3126 tcc_free (v
.versym
), v
.versym
= NULL
;
3128 store_version(s1
, &v
, dynstr
);
3130 /* add the dll and its level */
3131 dllref
= tcc_mallocz(sizeof(DLLReference
) + strlen(soname
));
3132 dllref
->level
= level
;
3133 strcpy(dllref
->name
, soname
);
3134 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, dllref
);
3136 /* add dynamic symbols in dynsym_section */
3137 for(i
= 1, sym
= dynsym
+ 1; i
< nb_syms
; i
++, sym
++) {
3138 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
3139 if (sym_bind
== STB_LOCAL
)
3141 name
= dynstr
+ sym
->st_name
;
3142 sym_index
= set_elf_sym(s1
->dynsymtab_section
, sym
->st_value
, sym
->st_size
,
3143 sym
->st_info
, sym
->st_other
, sym
->st_shndx
, name
);
3145 ElfW(Half
) vsym
= v
.versym
[i
];
3146 if ((vsym
& 0x8000) == 0 && vsym
> 0 && vsym
< v
.nb_local_ver
)
3147 set_sym_version(s1
, sym_index
, v
.local_ver
[vsym
]);
3151 /* load all referenced DLLs */
3152 for(i
= 0, dt
= dynamic
; i
< nb_dts
; i
++, dt
++) {
3155 name
= dynstr
+ dt
->d_un
.d_val
;
3156 for(j
= 0; j
< s1
->nb_loaded_dlls
; j
++) {
3157 dllref
= s1
->loaded_dlls
[j
];
3158 if (!strcmp(name
, dllref
->name
))
3159 goto already_loaded
;
3161 if (tcc_add_dll(s1
, name
, AFF_REFERENCED_DLL
) < 0) {
3162 tcc_error_noabort("referenced dll '%s' not found", name
);
3176 tcc_free(v
.local_ver
);
3178 tcc_free(v
.verneed
);
3183 #define LD_TOK_NAME 256
3184 #define LD_TOK_EOF (-1)
3186 static int ld_inp(TCCState
*s1
)
3194 if (1 == read(s1
->fd
, &b
, 1))
3199 /* return next ld script token */
3200 static int ld_next(TCCState
*s1
, char *name
, int name_size
)
3217 if (ch
== '*') { /* comment */
3218 for (d
= 0;; d
= ch
) {
3220 if (ch
== CH_EOF
|| (ch
== '/' && d
== '*'))
3231 /* case 'a' ... 'z': */
3258 /* case 'A' ... 'z': */
3292 if (!((ch
>= 'a' && ch
<= 'z') ||
3293 (ch
>= 'A' && ch
<= 'Z') ||
3294 (ch
>= '0' && ch
<= '9') ||
3295 strchr("/.-_+=$:\\,~", ch
)))
3297 if ((q
- name
) < name_size
- 1) {
3316 static int ld_add_file(TCCState
*s1
, const char filename
[])
3318 if (filename
[0] == '/') {
3319 if (CONFIG_SYSROOT
[0] == '\0'
3320 && tcc_add_file_internal(s1
, filename
, AFF_TYPE_BIN
) == 0)
3322 filename
= tcc_basename(filename
);
3324 return tcc_add_dll(s1
, filename
, 0);
3327 static int ld_add_file_list(TCCState
*s1
, const char *cmd
, int as_needed
)
3329 char filename
[1024], libname
[1024];
3330 int t
, group
, nblibs
= 0, ret
= 0;
3333 group
= !strcmp(cmd
, "GROUP");
3335 s1
->new_undef_sym
= 0;
3336 t
= ld_next(s1
, filename
, sizeof(filename
));
3339 t
= ld_next(s1
, filename
, sizeof(filename
));
3342 if (t
== LD_TOK_EOF
) {
3343 tcc_error_noabort("unexpected end of file");
3345 goto lib_parse_error
;
3346 } else if (t
== ')') {
3348 } else if (t
== '-') {
3349 t
= ld_next(s1
, filename
, sizeof(filename
));
3350 if ((t
!= LD_TOK_NAME
) || (filename
[0] != 'l')) {
3351 tcc_error_noabort("library name expected");
3353 goto lib_parse_error
;
3355 pstrcpy(libname
, sizeof libname
, &filename
[1]);
3356 if (s1
->static_link
) {
3357 snprintf(filename
, sizeof filename
, "lib%s.a", libname
);
3359 snprintf(filename
, sizeof filename
, "lib%s.so", libname
);
3361 } else if (t
!= LD_TOK_NAME
) {
3362 tcc_error_noabort("filename expected");
3364 goto lib_parse_error
;
3366 if (!strcmp(filename
, "AS_NEEDED")) {
3367 ret
= ld_add_file_list(s1
, cmd
, 1);
3369 goto lib_parse_error
;
3371 /* TODO: Implement AS_NEEDED support. Ignore it for now */
3373 ret
= ld_add_file(s1
, filename
);
3375 goto lib_parse_error
;
3377 /* Add the filename *and* the libname to avoid future conversions */
3378 dynarray_add(&libs
, &nblibs
, tcc_strdup(filename
));
3379 if (libname
[0] != '\0')
3380 dynarray_add(&libs
, &nblibs
, tcc_strdup(libname
));
3384 t
= ld_next(s1
, filename
, sizeof(filename
));
3386 t
= ld_next(s1
, filename
, sizeof(filename
));
3389 if (group
&& !as_needed
) {
3390 while (s1
->new_undef_sym
) {
3392 s1
->new_undef_sym
= 0;
3393 for (i
= 0; i
< nblibs
; i
++)
3394 ld_add_file(s1
, libs
[i
]);
3398 dynarray_reset(&libs
, &nblibs
);
3402 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
3404 ST_FUNC
int tcc_load_ldscript(TCCState
*s1
, int fd
)
3407 char filename
[1024];
3413 t
= ld_next(s1
, cmd
, sizeof(cmd
));
3414 if (t
== LD_TOK_EOF
)
3416 else if (t
!= LD_TOK_NAME
)
3418 if (!strcmp(cmd
, "INPUT") ||
3419 !strcmp(cmd
, "GROUP")) {
3420 ret
= ld_add_file_list(s1
, cmd
, 0);
3423 } else if (!strcmp(cmd
, "OUTPUT_FORMAT") ||
3424 !strcmp(cmd
, "TARGET")) {
3425 /* ignore some commands */
3426 t
= ld_next(s1
, cmd
, sizeof(cmd
));
3430 t
= ld_next(s1
, filename
, sizeof(filename
));
3431 if (t
== LD_TOK_EOF
) {
3432 tcc_error_noabort("unexpected end of file");
3434 } else if (t
== ')') {
3444 #endif /* !ELF_OBJ_ONLY */