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
)
94 #ifdef CONFIG_TCC_BACKTRACE
95 /* include stab info with standalone backtrace support */
96 if (s
->do_backtrace
&& s
->output_type
!= TCC_OUTPUT_MEMORY
)
99 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, shf
);
100 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
101 stab_section
->sh_addralign
= sizeof ((Stab_Sym
*)0)->n_value
;
102 stab_section
->link
= new_section(s
, ".stabstr", SHT_STRTAB
, shf
);
103 /* put first entry */
104 put_stabs(s
, "", 0, 0, 0, 0);
107 static void free_section(Section
*s
)
112 ST_FUNC
void tccelf_delete(TCCState
*s1
)
117 /* free symbol versions */
118 for (i
= 0; i
< nb_sym_versions
; i
++) {
119 tcc_free(sym_versions
[i
].version
);
120 tcc_free(sym_versions
[i
].lib
);
122 tcc_free(sym_versions
);
123 tcc_free(sym_to_version
);
126 /* free all sections */
127 for(i
= 1; i
< s1
->nb_sections
; i
++)
128 free_section(s1
->sections
[i
]);
129 dynarray_reset(&s1
->sections
, &s1
->nb_sections
);
131 for(i
= 0; i
< s1
->nb_priv_sections
; i
++)
132 free_section(s1
->priv_sections
[i
]);
133 dynarray_reset(&s1
->priv_sections
, &s1
->nb_priv_sections
);
135 /* free any loaded DLLs */
137 for ( i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
138 DLLReference
*ref
= s1
->loaded_dlls
[i
];
141 FreeLibrary((HMODULE
)ref
->handle
);
143 dlclose(ref
->handle
);
147 /* free loaded dlls array */
148 dynarray_reset(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
);
149 tcc_free(s1
->sym_attrs
);
151 symtab_section
= NULL
; /* for tccrun.c:rt_printline() */
154 /* save section data state */
155 ST_FUNC
void tccelf_begin_file(TCCState
*s1
)
158 for (i
= 1; i
< s1
->nb_sections
; i
++) {
160 s
->sh_offset
= s
->data_offset
;
162 /* disable symbol hashing during compilation */
163 s
= s1
->symtab
, s
->reloc
= s
->hash
, s
->hash
= NULL
;
164 #if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE
169 /* At the end of compilation, convert any UNDEF syms to global, and merge
170 with previously existing symbols */
171 ST_FUNC
void tccelf_end_file(TCCState
*s1
)
173 Section
*s
= s1
->symtab
;
174 int first_sym
, nb_syms
, *tr
, i
;
176 first_sym
= s
->sh_offset
/ sizeof (ElfSym
);
177 nb_syms
= s
->data_offset
/ sizeof (ElfSym
) - first_sym
;
178 s
->data_offset
= s
->sh_offset
;
179 s
->link
->data_offset
= s
->link
->sh_offset
;
180 s
->hash
= s
->reloc
, s
->reloc
= NULL
;
181 tr
= tcc_mallocz(nb_syms
* sizeof *tr
);
183 for (i
= 0; i
< nb_syms
; ++i
) {
184 ElfSym
*sym
= (ElfSym
*)s
->data
+ first_sym
+ i
;
185 if (sym
->st_shndx
== SHN_UNDEF
186 && ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
)
187 sym
->st_info
= ELFW(ST_INFO
)(STB_GLOBAL
, ELFW(ST_TYPE
)(sym
->st_info
));
188 tr
[i
] = set_elf_sym(s
, sym
->st_value
, sym
->st_size
, sym
->st_info
,
189 sym
->st_other
, sym
->st_shndx
, (char*)s
->link
->data
+ sym
->st_name
);
191 /* now update relocations */
192 for (i
= 1; i
< s1
->nb_sections
; i
++) {
193 Section
*sr
= s1
->sections
[i
];
194 if (sr
->sh_type
== SHT_RELX
&& sr
->link
== s
) {
195 ElfW_Rel
*rel
= (ElfW_Rel
*)(sr
->data
+ sr
->sh_offset
);
196 ElfW_Rel
*rel_end
= (ElfW_Rel
*)(sr
->data
+ sr
->data_offset
);
197 for (; rel
< rel_end
; ++rel
) {
198 int n
= ELFW(R_SYM
)(rel
->r_info
) - first_sym
;
199 //if (n < 0) tcc_error("internal: invalid symbol index in relocation");
200 rel
->r_info
= ELFW(R_INFO
)(tr
[n
], ELFW(R_TYPE
)(rel
->r_info
));
207 ST_FUNC Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
211 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
213 strcpy(sec
->name
, name
);
214 sec
->sh_type
= sh_type
;
215 sec
->sh_flags
= sh_flags
;
218 sec
->sh_addralign
= 2;
226 case SHT_GNU_verneed
:
228 sec
->sh_addralign
= PTR_SIZE
;
231 sec
->sh_addralign
= 1;
234 sec
->sh_addralign
= PTR_SIZE
; /* gcc/pcc default alignment */
238 if (sh_flags
& SHF_PRIVATE
) {
239 dynarray_add(&s1
->priv_sections
, &s1
->nb_priv_sections
, sec
);
241 sec
->sh_num
= s1
->nb_sections
;
242 dynarray_add(&s1
->sections
, &s1
->nb_sections
, sec
);
248 ST_FUNC Section
*new_symtab(TCCState
*s1
,
249 const char *symtab_name
, int sh_type
, int sh_flags
,
250 const char *strtab_name
,
251 const char *hash_name
, int hash_sh_flags
)
253 Section
*symtab
, *strtab
, *hash
;
254 int *ptr
, nb_buckets
;
256 symtab
= new_section(s1
, symtab_name
, sh_type
, sh_flags
);
257 symtab
->sh_entsize
= sizeof(ElfW(Sym
));
258 strtab
= new_section(s1
, strtab_name
, SHT_STRTAB
, sh_flags
);
259 put_elf_str(strtab
, "");
260 symtab
->link
= strtab
;
261 put_elf_sym(symtab
, 0, 0, 0, 0, 0, NULL
);
265 hash
= new_section(s1
, hash_name
, SHT_HASH
, hash_sh_flags
);
266 hash
->sh_entsize
= sizeof(int);
270 ptr
= section_ptr_add(hash
, (2 + nb_buckets
+ 1) * sizeof(int));
273 memset(ptr
+ 2, 0, (nb_buckets
+ 1) * sizeof(int));
277 /* realloc section and set its content to zero */
278 ST_FUNC
void section_realloc(Section
*sec
, unsigned long new_size
)
283 size
= sec
->data_allocated
;
286 while (size
< new_size
)
288 data
= tcc_realloc(sec
->data
, size
);
289 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
291 sec
->data_allocated
= size
;
294 /* reserve at least 'size' bytes aligned per 'align' in section
295 'sec' from current offset, and return the aligned offset */
296 ST_FUNC
size_t section_add(Section
*sec
, addr_t size
, int align
)
298 size_t offset
, offset1
;
300 offset
= (sec
->data_offset
+ align
- 1) & -align
;
301 offset1
= offset
+ size
;
302 if (sec
->sh_type
!= SHT_NOBITS
&& offset1
> sec
->data_allocated
)
303 section_realloc(sec
, offset1
);
304 sec
->data_offset
= offset1
;
305 if (align
> sec
->sh_addralign
)
306 sec
->sh_addralign
= align
;
310 /* reserve at least 'size' bytes in section 'sec' from
312 ST_FUNC
void *section_ptr_add(Section
*sec
, addr_t size
)
314 size_t offset
= section_add(sec
, size
, 1);
315 return sec
->data
+ offset
;
318 /* reserve at least 'size' bytes from section start */
319 ST_FUNC
void section_reserve(Section
*sec
, unsigned long size
)
321 if (size
> sec
->data_allocated
)
322 section_realloc(sec
, size
);
323 if (size
> sec
->data_offset
)
324 sec
->data_offset
= size
;
327 static Section
*find_section_create (TCCState
*s1
, const char *name
, int create
)
331 for(i
= 1; i
< s1
->nb_sections
; i
++) {
332 sec
= s1
->sections
[i
];
333 if (!strcmp(name
, sec
->name
))
336 /* sections are created as PROGBITS */
337 return create
? new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
) : NULL
;
340 /* return a reference to a section, and create it if it does not
342 ST_FUNC Section
*find_section(TCCState
*s1
, const char *name
)
344 return find_section_create (s1
, name
, 1);
347 /* ------------------------------------------------------------------------- */
349 ST_FUNC
int put_elf_str(Section
*s
, const char *sym
)
354 len
= strlen(sym
) + 1;
355 offset
= s
->data_offset
;
356 ptr
= section_ptr_add(s
, len
);
357 memmove(ptr
, sym
, len
);
361 /* elf symbol hashing function */
362 static unsigned long elf_hash(const unsigned char *name
)
364 unsigned long h
= 0, g
;
367 h
= (h
<< 4) + *name
++;
376 /* rebuild hash table of section s */
377 /* NOTE: we do factorize the hash table code to go faster */
378 static void rebuild_hash(Section
*s
, unsigned int nb_buckets
)
381 int *ptr
, *hash
, nb_syms
, sym_index
, h
;
382 unsigned char *strtab
;
384 strtab
= s
->link
->data
;
385 nb_syms
= s
->data_offset
/ sizeof(ElfW(Sym
));
388 nb_buckets
= ((int*)s
->hash
->data
)[0];
390 s
->hash
->data_offset
= 0;
391 ptr
= section_ptr_add(s
->hash
, (2 + nb_buckets
+ nb_syms
) * sizeof(int));
396 memset(hash
, 0, (nb_buckets
+ 1) * sizeof(int));
397 ptr
+= nb_buckets
+ 1;
399 sym
= (ElfW(Sym
) *)s
->data
+ 1;
400 for(sym_index
= 1; sym_index
< nb_syms
; sym_index
++) {
401 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
402 h
= elf_hash(strtab
+ sym
->st_name
) % nb_buckets
;
413 /* return the symbol number */
414 ST_FUNC
int put_elf_sym(Section
*s
, addr_t value
, unsigned long size
,
415 int info
, int other
, int shndx
, const char *name
)
417 int name_offset
, sym_index
;
422 sym
= section_ptr_add(s
, sizeof(ElfW(Sym
)));
424 name_offset
= put_elf_str(s
->link
, name
);
427 /* XXX: endianness */
428 sym
->st_name
= name_offset
;
429 sym
->st_value
= value
;
432 sym
->st_other
= other
;
433 sym
->st_shndx
= shndx
;
434 sym_index
= sym
- (ElfW(Sym
) *)s
->data
;
438 ptr
= section_ptr_add(hs
, sizeof(int));
439 base
= (int *)hs
->data
;
440 /* only add global or weak symbols. */
441 if (ELFW(ST_BIND
)(info
) != STB_LOCAL
) {
442 /* add another hashing entry */
444 h
= elf_hash((unsigned char *)s
->link
->data
+ name_offset
) % nbuckets
;
446 base
[2 + h
] = sym_index
;
448 /* we resize the hash table */
449 hs
->nb_hashed_syms
++;
450 if (hs
->nb_hashed_syms
> 2 * nbuckets
) {
451 rebuild_hash(s
, 2 * nbuckets
);
461 ST_FUNC
int find_elf_sym(Section
*s
, const char *name
)
465 int nbuckets
, sym_index
, h
;
471 nbuckets
= ((int *)hs
->data
)[0];
472 h
= elf_hash((unsigned char *) name
) % nbuckets
;
473 sym_index
= ((int *)hs
->data
)[2 + h
];
474 while (sym_index
!= 0) {
475 sym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
476 name1
= (char *) s
->link
->data
+ sym
->st_name
;
477 if (!strcmp(name
, name1
))
479 sym_index
= ((int *)hs
->data
)[2 + nbuckets
+ sym_index
];
484 ST_FUNC
int find_c_sym(TCCState
*s1
, const char *name
)
488 if (s1
->leading_underscore
) {
490 cstr_ccat(&cstr
, '_');
491 cstr_cat(&cstr
, name
, 0);
494 ret
= find_elf_sym(s1
->symtab
, name
);
495 if (s1
->leading_underscore
)
500 /* return elf symbol value, signal error if 'err' is nonzero, decorate
502 ST_FUNC addr_t
get_sym_addr(TCCState
*s1
, const char *name
, int err
, int forc
)
508 sym_index
= find_c_sym(s1
, name
);
510 sym_index
= find_elf_sym(s1
->symtab
, name
);
511 sym
= &((ElfW(Sym
) *)s1
->symtab
->data
)[sym_index
];
512 if (!sym_index
|| sym
->st_shndx
== SHN_UNDEF
) {
514 tcc_error("%s not defined", name
);
517 return sym
->st_value
;
520 /* list elf symbol names and values */
521 ST_FUNC
void list_elf_symbols(TCCState
*s
, void *ctx
,
522 void (*symbol_cb
)(void *ctx
, const char *name
, const void *val
))
526 int sym_index
, end_sym
;
528 unsigned char sym_vis
, sym_bind
;
531 end_sym
= symtab
->data_offset
/ sizeof (ElfSym
);
532 for (sym_index
= 0; sym_index
< end_sym
; ++sym_index
) {
533 sym
= &((ElfW(Sym
) *)symtab
->data
)[sym_index
];
535 name
= (char *) symtab
->link
->data
+ sym
->st_name
;
536 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
537 sym_vis
= ELFW(ST_VISIBILITY
)(sym
->st_other
);
538 if (sym_bind
== STB_GLOBAL
&& sym_vis
== STV_DEFAULT
)
539 symbol_cb(ctx
, name
, (void*)(uintptr_t)sym
->st_value
);
544 /* return elf symbol value */
545 LIBTCCAPI
void *tcc_get_symbol(TCCState
*s
, const char *name
)
547 return (void*)(uintptr_t)get_sym_addr(s
, name
, 0, 1);
550 /* list elf symbol names and values */
551 LIBTCCAPI
void tcc_list_symbols(TCCState
*s
, void *ctx
,
552 void (*symbol_cb
)(void *ctx
, const char *name
, const void *val
))
554 list_elf_symbols(s
, ctx
, symbol_cb
);
557 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
558 /* return elf symbol value or error */
559 ST_FUNC
void* tcc_get_symbol_err(TCCState
*s
, const char *name
)
561 return (void*)(uintptr_t)get_sym_addr(s
, name
, 1, 1);
567 version_add (TCCState
*s1
)
571 ElfW(Verneed
) *vn
= NULL
;
573 int sym_index
, end_sym
, nb_versions
= 2, nb_entries
= 0;
577 if (0 == nb_sym_versions
)
579 versym_section
= new_section(s1
, ".gnu.version", SHT_GNU_versym
, SHF_ALLOC
);
580 versym_section
->sh_entsize
= sizeof(ElfW(Half
));
581 versym_section
->link
= s1
->dynsym
;
583 /* add needed symbols */
585 end_sym
= symtab
->data_offset
/ sizeof (ElfSym
);
586 versym
= section_ptr_add(versym_section
, end_sym
* sizeof(ElfW(Half
)));
587 for (sym_index
= 0; sym_index
< end_sym
; ++sym_index
) {
588 int dllindex
, verndx
;
589 sym
= &((ElfW(Sym
) *)symtab
->data
)[sym_index
];
590 name
= (char *) symtab
->link
->data
+ sym
->st_name
;
591 dllindex
= find_elf_sym(s1
->dynsymtab_section
, name
);
592 verndx
= (dllindex
&& dllindex
< nb_sym_to_version
)
593 ? sym_to_version
[dllindex
] : -1;
595 if (!sym_versions
[verndx
].out_index
)
596 sym_versions
[verndx
].out_index
= nb_versions
++;
597 versym
[sym_index
] = sym_versions
[verndx
].out_index
;
599 versym
[sym_index
] = 0;
601 /* generate verneed section, but not when it will be empty. Some
602 dynamic linkers look at their contents even when DTVERNEEDNUM and
603 section size is zero. */
604 if (nb_versions
> 2) {
605 verneed_section
= new_section(s1
, ".gnu.version_r",
606 SHT_GNU_verneed
, SHF_ALLOC
);
607 verneed_section
->link
= s1
->dynsym
->link
;
608 for (i
= nb_sym_versions
; i
-- > 0;) {
609 struct sym_version
*sv
= &sym_versions
[i
];
610 int n_same_libs
= 0, prev
;
612 ElfW(Vernaux
) *vna
= 0;
613 if (sv
->out_index
< 1)
615 vnofs
= section_add(verneed_section
, sizeof(*vn
), 1);
616 vn
= (ElfW(Verneed
)*)(verneed_section
->data
+ vnofs
);
618 vn
->vn_file
= put_elf_str(verneed_section
->link
, sv
->lib
);
619 vn
->vn_aux
= sizeof (*vn
);
621 prev
= sv
->prev_same_lib
;
622 if (sv
->out_index
> 0) {
623 vna
= section_ptr_add(verneed_section
, sizeof(*vna
));
624 vna
->vna_hash
= elf_hash ((const unsigned char *)sv
->version
);
626 vna
->vna_other
= sv
->out_index
;
628 vna
->vna_name
= put_elf_str(verneed_section
->link
, sv
->version
);
629 vna
->vna_next
= sizeof (*vna
);
633 sv
= &sym_versions
[prev
];
636 vn
= (ElfW(Verneed
)*)(verneed_section
->data
+ vnofs
);
637 vn
->vn_cnt
= n_same_libs
;
638 vn
->vn_next
= sizeof(*vn
) + n_same_libs
* sizeof(*vna
);
643 verneed_section
->sh_info
= nb_entries
;
645 dt_verneednum
= nb_entries
;
649 /* add an elf symbol : check if it is already defined and patch
650 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
651 ST_FUNC
int set_elf_sym(Section
*s
, addr_t value
, unsigned long size
,
652 int info
, int other
, int shndx
, const char *name
)
654 TCCState
*s1
= s
->s1
;
656 int sym_bind
, sym_index
, sym_type
, esym_bind
;
657 unsigned char sym_vis
, esym_vis
, new_vis
;
659 sym_bind
= ELFW(ST_BIND
)(info
);
660 sym_type
= ELFW(ST_TYPE
)(info
);
661 sym_vis
= ELFW(ST_VISIBILITY
)(other
);
663 if (sym_bind
!= STB_LOCAL
) {
664 /* we search global or weak symbols */
665 sym_index
= find_elf_sym(s
, name
);
668 esym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
669 if (esym
->st_value
== value
&& esym
->st_size
== size
&& esym
->st_info
== info
670 && esym
->st_other
== other
&& esym
->st_shndx
== shndx
)
672 if (esym
->st_shndx
!= SHN_UNDEF
) {
673 esym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
674 /* propagate the most constraining visibility */
675 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
676 esym_vis
= ELFW(ST_VISIBILITY
)(esym
->st_other
);
677 if (esym_vis
== STV_DEFAULT
) {
679 } else if (sym_vis
== STV_DEFAULT
) {
682 new_vis
= (esym_vis
< sym_vis
) ? esym_vis
: sym_vis
;
684 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
686 other
= esym
->st_other
; /* in case we have to patch esym */
687 if (shndx
== SHN_UNDEF
) {
688 /* ignore adding of undefined symbol if the
689 corresponding symbol is already defined */
690 } else if (sym_bind
== STB_GLOBAL
&& esym_bind
== STB_WEAK
) {
691 /* global overrides weak, so patch */
693 } else if (sym_bind
== STB_WEAK
&& esym_bind
== STB_GLOBAL
) {
694 /* weak is ignored if already global */
695 } else if (sym_bind
== STB_WEAK
&& esym_bind
== STB_WEAK
) {
696 /* keep first-found weak definition, ignore subsequents */
697 } else if (sym_vis
== STV_HIDDEN
|| sym_vis
== STV_INTERNAL
) {
698 /* ignore hidden symbols after */
699 } else if ((esym
->st_shndx
== SHN_COMMON
700 || esym
->st_shndx
== bss_section
->sh_num
)
701 && (shndx
< SHN_LORESERVE
702 && shndx
!= bss_section
->sh_num
)) {
703 /* data symbol gets precedence over common/bss */
705 } else if (shndx
== SHN_COMMON
|| shndx
== bss_section
->sh_num
) {
706 /* data symbol keeps precedence over common/bss */
707 } else if (s
->sh_flags
& SHF_DYNSYM
) {
708 /* we accept that two DLL define the same symbol */
709 } else if (esym
->st_other
& ST_ASM_SET
) {
710 /* If the existing symbol came from an asm .set
715 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
716 sym_bind
, shndx
, new_vis
, esym_bind
, esym
->st_shndx
, esym_vis
);
718 tcc_error_noabort("'%s' defined twice", name
);
722 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
723 esym
->st_shndx
= shndx
;
724 s1
->new_undef_sym
= 1;
725 esym
->st_value
= value
;
726 esym
->st_size
= size
;
727 esym
->st_other
= other
;
731 sym_index
= put_elf_sym(s
, value
, size
,
732 ELFW(ST_INFO
)(sym_bind
, sym_type
), other
,
739 ST_FUNC
void put_elf_reloca(Section
*symtab
, Section
*s
, unsigned long offset
,
740 int type
, int symbol
, addr_t addend
)
742 TCCState
*s1
= s
->s1
;
749 /* if no relocation section, create it */
750 snprintf(buf
, sizeof(buf
), REL_SECTION_FMT
, s
->name
);
751 /* if the symtab is allocated, then we consider the relocation
753 sr
= new_section(s
->s1
, buf
, SHT_RELX
, symtab
->sh_flags
);
754 sr
->sh_entsize
= sizeof(ElfW_Rel
);
756 sr
->sh_info
= s
->sh_num
;
759 rel
= section_ptr_add(sr
, sizeof(ElfW_Rel
));
760 rel
->r_offset
= offset
;
761 rel
->r_info
= ELFW(R_INFO
)(symbol
, type
);
762 #if SHT_RELX == SHT_RELA
763 rel
->r_addend
= addend
;
765 if (SHT_RELX
!= SHT_RELA
&& addend
)
766 tcc_error("non-zero addend on REL architecture");
769 ST_FUNC
void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
770 int type
, int symbol
)
772 put_elf_reloca(symtab
, s
, offset
, type
, symbol
, 0);
775 /* Remove relocations for section S->reloc starting at oldrelocoffset
776 that are to the same place, retaining the last of them. As side effect
777 the relocations are sorted. Possibly reduces the number of relocs. */
778 ST_FUNC
void squeeze_multi_relocs(Section
*s
, size_t oldrelocoffset
)
780 Section
*sr
= s
->reloc
;
785 if (oldrelocoffset
+ sizeof(*r
) >= sr
->data_offset
)
787 /* The relocs we're dealing with are the result of initializer parsing.
788 So they will be mostly in order and there aren't many of them.
789 Secondly we need a stable sort (which qsort isn't). We use
790 a simple insertion sort. */
791 for (a
= oldrelocoffset
+ sizeof(*r
); a
< sr
->data_offset
; a
+= sizeof(*r
)) {
792 ssize_t i
= a
- sizeof(*r
);
793 addr
= ((ElfW_Rel
*)(sr
->data
+ a
))->r_offset
;
794 for (; i
>= (ssize_t
)oldrelocoffset
&&
795 ((ElfW_Rel
*)(sr
->data
+ i
))->r_offset
> addr
; i
-= sizeof(*r
)) {
796 ElfW_Rel tmp
= *(ElfW_Rel
*)(sr
->data
+ a
);
797 *(ElfW_Rel
*)(sr
->data
+ a
) = *(ElfW_Rel
*)(sr
->data
+ i
);
798 *(ElfW_Rel
*)(sr
->data
+ i
) = tmp
;
802 r
= (ElfW_Rel
*)(sr
->data
+ oldrelocoffset
);
804 for (; r
< (ElfW_Rel
*)(sr
->data
+ sr
->data_offset
); r
++) {
805 if (dest
->r_offset
!= r
->r_offset
)
809 sr
->data_offset
= (unsigned char*)dest
- sr
->data
+ sizeof(*r
);
812 /* put stab debug information */
814 ST_FUNC
void put_stabs(TCCState
*s1
, const char *str
, int type
, int other
, int desc
,
821 && (offset
= stab_section
->data_offset
)
822 && (sym
= (Stab_Sym
*)(stab_section
->data
+ offset
) - 1)
823 && sym
->n_type
== type
824 && sym
->n_value
== value
) {
825 /* just update line_number in previous entry */
830 sym
= section_ptr_add(stab_section
, sizeof(Stab_Sym
));
832 sym
->n_strx
= put_elf_str(stab_section
->link
, str
);
837 sym
->n_other
= other
;
839 sym
->n_value
= value
;
842 ST_FUNC
void put_stabs_r(TCCState
*s1
, const char *str
, int type
, int other
, int desc
,
843 unsigned long value
, Section
*sec
, int sym_index
)
845 put_elf_reloc(symtab_section
, stab_section
,
846 stab_section
->data_offset
+ 8,
847 sizeof ((Stab_Sym
*)0)->n_value
== PTR_SIZE
? R_DATA_PTR
: R_DATA_32
,
849 put_stabs(s1
, str
, type
, other
, desc
, value
);
852 ST_FUNC
void put_stabn(TCCState
*s1
, int type
, int other
, int desc
, int value
)
854 put_stabs(s1
, NULL
, type
, other
, desc
, value
);
857 ST_FUNC
struct sym_attr
*get_sym_attr(TCCState
*s1
, int index
, int alloc
)
860 struct sym_attr
*tab
;
862 if (index
>= s1
->nb_sym_attrs
) {
864 return s1
->sym_attrs
;
865 /* find immediately bigger power of 2 and reallocate array */
869 tab
= tcc_realloc(s1
->sym_attrs
, n
* sizeof(*s1
->sym_attrs
));
871 memset(s1
->sym_attrs
+ s1
->nb_sym_attrs
, 0,
872 (n
- s1
->nb_sym_attrs
) * sizeof(*s1
->sym_attrs
));
873 s1
->nb_sym_attrs
= n
;
875 return &s1
->sym_attrs
[index
];
878 /* In an ELF file symbol table, the local symbols must appear below
879 the global and weak ones. Since TCC cannot sort it while generating
880 the code, we must do it after. All the relocation tables are also
881 modified to take into account the symbol table sorting */
882 static void sort_syms(TCCState
*s1
, Section
*s
)
884 int *old_to_new_syms
;
892 nb_syms
= s
->data_offset
/ sizeof(ElfW(Sym
));
893 new_syms
= tcc_malloc(nb_syms
* sizeof(ElfW(Sym
)));
894 old_to_new_syms
= tcc_malloc(nb_syms
* sizeof(int));
896 /* first pass for local symbols */
897 p
= (ElfW(Sym
) *)s
->data
;
899 for(i
= 0; i
< nb_syms
; i
++) {
900 if (ELFW(ST_BIND
)(p
->st_info
) == STB_LOCAL
) {
901 old_to_new_syms
[i
] = q
- new_syms
;
906 /* save the number of local symbols in section header */
907 if( s
->sh_size
) /* this 'if' makes IDA happy */
908 s
->sh_info
= q
- new_syms
;
910 /* then second pass for non local symbols */
911 p
= (ElfW(Sym
) *)s
->data
;
912 for(i
= 0; i
< nb_syms
; i
++) {
913 if (ELFW(ST_BIND
)(p
->st_info
) != STB_LOCAL
) {
914 old_to_new_syms
[i
] = q
- new_syms
;
920 /* we copy the new symbols to the old */
921 memcpy(s
->data
, new_syms
, nb_syms
* sizeof(ElfW(Sym
)));
924 /* now we modify all the relocations */
925 for(i
= 1; i
< s1
->nb_sections
; i
++) {
926 sr
= s1
->sections
[i
];
927 if (sr
->sh_type
== SHT_RELX
&& sr
->link
== s
) {
928 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
929 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
930 type
= ELFW(R_TYPE
)(rel
->r_info
);
931 sym_index
= old_to_new_syms
[sym_index
];
932 rel
->r_info
= ELFW(R_INFO
)(sym_index
, type
);
937 tcc_free(old_to_new_syms
);
940 /* relocate symbol table, resolve undefined symbols if do_resolve is
941 true and output error if undefined symbol. */
942 ST_FUNC
void relocate_syms(TCCState
*s1
, Section
*symtab
, int do_resolve
)
945 int sym_bind
, sh_num
;
948 for_each_elem(symtab
, 1, sym
, ElfW(Sym
)) {
949 sh_num
= sym
->st_shndx
;
950 if (sh_num
== SHN_UNDEF
) {
951 name
= (char *) s1
->symtab
->link
->data
+ sym
->st_name
;
952 /* Use ld.so to resolve symbol for us (for tcc -run) */
954 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
955 #ifdef TCC_TARGET_MACHO
956 /* The symbols in the symtables have a prepended '_'
957 but dlsym() needs the undecorated name. */
958 void *addr
= dlsym(RTLD_DEFAULT
, name
+ 1);
960 void *addr
= dlsym(RTLD_DEFAULT
, name
);
963 sym
->st_value
= (addr_t
) addr
;
965 printf ("relocate_sym: %s -> 0x%lx\n", name
, sym
->st_value
);
970 /* if dynamic symbol exist, it will be used in relocate_section */
971 } else if (s1
->dynsym
&& find_elf_sym(s1
->dynsym
, name
))
973 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
975 if (!strcmp(name
, "_fp_hw"))
977 /* only weak symbols are accepted to be undefined. Their
979 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
980 if (sym_bind
== STB_WEAK
)
983 tcc_error_noabort("undefined symbol '%s'", name
);
984 } else if (sh_num
< SHN_LORESERVE
) {
985 /* add section base */
986 sym
->st_value
+= s1
->sections
[sym
->st_shndx
]->sh_addr
;
992 /* relocate a given section (CPU dependent) by applying the relocations
993 in the associated relocation section */
994 ST_FUNC
void relocate_section(TCCState
*s1
, Section
*s
)
996 Section
*sr
= s
->reloc
;
1003 qrel
= (ElfW_Rel
*)sr
->data
;
1005 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
1006 ptr
= s
->data
+ rel
->r_offset
;
1007 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
1008 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1009 type
= ELFW(R_TYPE
)(rel
->r_info
);
1010 tgt
= sym
->st_value
;
1011 #if SHT_RELX == SHT_RELA
1012 tgt
+= rel
->r_addend
;
1014 addr
= s
->sh_addr
+ rel
->r_offset
;
1015 relocate(s1
, rel
, type
, ptr
, addr
, tgt
);
1017 /* if the relocation is allocated, we change its symbol table */
1018 if (sr
->sh_flags
& SHF_ALLOC
) {
1019 sr
->link
= s1
->dynsym
;
1020 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
1021 size_t r
= (uint8_t*)qrel
- sr
->data
;
1022 if (sizeof ((Stab_Sym
*)0)->n_value
< PTR_SIZE
1023 && 0 == strcmp(s
->name
, ".stab"))
1024 r
= 0; /* cannot apply 64bit relocation to 32bit value */
1025 sr
->data_offset
= sr
->sh_size
= r
;
1030 #ifndef ELF_OBJ_ONLY
1031 /* relocate relocation table in 'sr' */
1032 static void relocate_rel(TCCState
*s1
, Section
*sr
)
1037 s
= s1
->sections
[sr
->sh_info
];
1038 for_each_elem(sr
, 0, rel
, ElfW_Rel
)
1039 rel
->r_offset
+= s
->sh_addr
;
1042 /* count the number of dynamic relocations so that we can reserve
1044 static int prepare_dynamic_rel(TCCState
*s1
, Section
*sr
)
1047 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1049 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
1050 int sym_index
= ELFW(R_SYM
)(rel
->r_info
);
1051 int type
= ELFW(R_TYPE
)(rel
->r_info
);
1053 #if defined(TCC_TARGET_I386)
1055 if (!get_sym_attr(s1
, sym_index
, 0)->dyn_index
1056 && ((ElfW(Sym
)*)symtab_section
->data
+ sym_index
)->st_shndx
== SHN_UNDEF
) {
1057 /* don't fixup unresolved (weak) symbols */
1058 rel
->r_info
= ELFW(R_INFO
)(sym_index
, R_386_RELATIVE
);
1061 #elif defined(TCC_TARGET_X86_64)
1068 #if defined(TCC_TARGET_I386)
1070 #elif defined(TCC_TARGET_X86_64)
1073 if (get_sym_attr(s1
, sym_index
, 0)->dyn_index
)
1081 /* allocate the section */
1082 sr
->sh_flags
|= SHF_ALLOC
;
1083 sr
->sh_size
= count
* sizeof(ElfW_Rel
);
1090 #if !defined(ELF_OBJ_ONLY) || defined(TCC_TARGET_MACHO)
1091 static void build_got(TCCState
*s1
)
1093 /* if no got, then create it */
1094 s1
->got
= new_section(s1
, ".got", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
1095 s1
->got
->sh_entsize
= 4;
1096 set_elf_sym(symtab_section
, 0, 4, ELFW(ST_INFO
)(STB_GLOBAL
, STT_OBJECT
),
1097 0, s1
->got
->sh_num
, "_GLOBAL_OFFSET_TABLE_");
1098 /* keep space for _DYNAMIC pointer and two dummy got entries */
1099 section_ptr_add(s1
->got
, 3 * PTR_SIZE
);
1102 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
1103 in s1->symtab. When creating the dynamic symbol table entry for the GOT
1104 relocation, use 'size' and 'info' for the corresponding symbol metadata.
1105 Returns the offset of the GOT or (if any) PLT entry. */
1106 static struct sym_attr
* put_got_entry(TCCState
*s1
, int dyn_reloc_type
,
1112 struct sym_attr
*attr
;
1113 unsigned got_offset
;
1117 need_plt_entry
= (dyn_reloc_type
== R_JMP_SLOT
);
1118 attr
= get_sym_attr(s1
, sym_index
, 1);
1120 /* In case a function is both called and its address taken 2 GOT entries
1121 are created, one for taking the address (GOT) and the other for the PLT
1123 if (need_plt_entry
? attr
->plt_offset
: attr
->got_offset
)
1126 /* create the GOT entry */
1127 got_offset
= s1
->got
->data_offset
;
1128 section_ptr_add(s1
->got
, PTR_SIZE
);
1130 /* Create the GOT relocation that will insert the address of the object or
1131 function of interest in the GOT entry. This is a static relocation for
1132 memory output (dlsym will give us the address of symbols) and dynamic
1133 relocation otherwise (executable and DLLs). The relocation should be
1134 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
1135 associated to a PLT entry) but is currently done at load time for an
1138 sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1139 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1142 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
) {
1143 /* Hack alarm. We don't want to emit dynamic symbols
1144 and symbol based relocs for STB_LOCAL symbols, but rather
1145 want to resolve them directly. At this point the symbol
1146 values aren't final yet, so we must defer this. We will later
1147 have to create a RELATIVE reloc anyway, so we misuse the
1148 relocation slot to smuggle the symbol reference until
1149 fill_local_got_entries. Not that the sym_index is
1150 relative to symtab_section, not s1->dynsym! Nevertheless
1151 we use s1->dyn_sym so that if this is the first call
1152 that got->reloc is correctly created. Also note that
1153 RELATIVE relocs are not normally created for the .got,
1154 so the types serves as a marker for later (and is retained
1155 also for the final output, which is okay because then the
1156 got is just normal data). */
1157 put_elf_reloc(s1
->dynsym
, s1
->got
, got_offset
, R_RELATIVE
,
1160 if (0 == attr
->dyn_index
)
1161 attr
->dyn_index
= set_elf_sym(s1
->dynsym
, sym
->st_value
,
1162 sym
->st_size
, sym
->st_info
, 0,
1163 sym
->st_shndx
, name
);
1164 put_elf_reloc(s1
->dynsym
, s1
->got
, got_offset
, dyn_reloc_type
,
1168 put_elf_reloc(symtab_section
, s1
->got
, got_offset
, dyn_reloc_type
,
1172 if (need_plt_entry
) {
1174 s1
->plt
= new_section(s1
, ".plt", SHT_PROGBITS
,
1175 SHF_ALLOC
| SHF_EXECINSTR
);
1176 s1
->plt
->sh_entsize
= 4;
1179 attr
->plt_offset
= create_plt_entry(s1
, got_offset
, attr
);
1181 /* create a symbol 'sym@plt' for the PLT jump vector */
1183 if (len
> sizeof plt_name
- 5)
1184 len
= sizeof plt_name
- 5;
1185 memcpy(plt_name
, name
, len
);
1186 strcpy(plt_name
+ len
, "@plt");
1187 attr
->plt_sym
= put_elf_sym(s1
->symtab
, attr
->plt_offset
, sym
->st_size
,
1188 ELFW(ST_INFO
)(STB_GLOBAL
, STT_FUNC
), 0, s1
->plt
->sh_num
, plt_name
);
1191 attr
->got_offset
= got_offset
;
1197 /* build GOT and PLT entries */
1198 ST_FUNC
void build_got_entries(TCCState
*s1
)
1203 int i
, type
, gotplt_entry
, reloc_type
, sym_index
;
1204 struct sym_attr
*attr
;
1206 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1207 s
= s1
->sections
[i
];
1208 if (s
->sh_type
!= SHT_RELX
)
1210 /* no need to handle got relocations */
1211 if (s
->link
!= symtab_section
)
1213 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
1214 type
= ELFW(R_TYPE
)(rel
->r_info
);
1215 gotplt_entry
= gotplt_entry_type(type
);
1216 if (gotplt_entry
== -1)
1217 tcc_error ("Unknown relocation type for got: %d", type
);
1218 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
1219 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1221 if (gotplt_entry
== NO_GOTPLT_ENTRY
) {
1225 /* Automatically create PLT/GOT [entry] if it is an undefined
1226 reference (resolved at runtime), or the symbol is absolute,
1227 probably created by tcc_add_symbol, and thus on 64-bit
1228 targets might be too far from application code. */
1229 if (gotplt_entry
== AUTO_GOTPLT_ENTRY
) {
1230 if (sym
->st_shndx
== SHN_UNDEF
) {
1233 if (s1
->output_type
== TCC_OUTPUT_DLL
&& ! PCRELATIVE_DLLPLT
)
1235 /* Relocations for UNDEF symbols would normally need
1236 to be transferred into the executable or shared object.
1237 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1238 But TCC doesn't do that (at least for exes), so we
1239 need to resolve all such relocs locally. And that
1240 means PLT slots for functions in DLLs and COPY relocs for
1241 data symbols. COPY relocs were generated in
1242 bind_exe_dynsyms (and the symbol adjusted to be defined),
1243 and for functions we were generated a dynamic symbol
1244 of function type. */
1246 /* dynsym isn't set for -run :-/ */
1247 dynindex
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
1248 esym
= (ElfW(Sym
) *)s1
->dynsym
->data
+ dynindex
;
1250 && (ELFW(ST_TYPE
)(esym
->st_info
) == STT_FUNC
1251 || (ELFW(ST_TYPE
)(esym
->st_info
) == STT_NOTYPE
1252 && ELFW(ST_TYPE
)(sym
->st_info
) == STT_FUNC
)))
1255 } else if (!(sym
->st_shndx
== SHN_ABS
1256 #ifndef TCC_TARGET_ARM
1263 #ifdef TCC_TARGET_X86_64
1264 if ((type
== R_X86_64_PLT32
|| type
== R_X86_64_PC32
) &&
1265 sym
->st_shndx
!= SHN_UNDEF
&&
1266 (ELFW(ST_VISIBILITY
)(sym
->st_other
) != STV_DEFAULT
||
1267 ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
||
1268 s1
->output_type
== TCC_OUTPUT_EXE
)) {
1269 rel
->r_info
= ELFW(R_INFO
)(sym_index
, R_X86_64_PC32
);
1273 reloc_type
= code_reloc(type
);
1274 if (reloc_type
== -1)
1275 tcc_error ("Unknown relocation type: %d", type
);
1276 else if (reloc_type
!= 0) {
1278 reloc_type
= R_JMP_SLOT
;
1280 reloc_type
= R_GLOB_DAT
;
1285 if (gotplt_entry
== BUILD_GOT_ONLY
)
1288 attr
= put_got_entry(s1
, reloc_type
, sym_index
);
1290 if (reloc_type
== R_JMP_SLOT
)
1291 rel
->r_info
= ELFW(R_INFO
)(attr
->plt_sym
, type
);
1297 ST_FUNC
int set_global_sym(TCCState
*s1
, const char *name
, Section
*sec
, long offs
)
1299 int shn
= sec
? sec
->sh_num
: offs
? SHN_ABS
: SHN_UNDEF
;
1300 if (sec
&& offs
== -1)
1301 offs
= sec
->data_offset
;
1302 return set_elf_sym(symtab_section
, offs
, 0,
1303 ELFW(ST_INFO
)(name
? STB_GLOBAL
: STB_LOCAL
, STT_NOTYPE
), 0, shn
, name
);
1306 static void add_init_array_defines(TCCState
*s1
, const char *section_name
)
1311 s
= find_section(s1
, section_name
);
1316 end_offset
= s
->data_offset
;
1318 snprintf(buf
, sizeof(buf
), "__%s_start", section_name
+ 1);
1319 set_global_sym(s1
, buf
, s
, 0);
1320 snprintf(buf
, sizeof(buf
), "__%s_end", section_name
+ 1);
1321 set_global_sym(s1
, buf
, s
, end_offset
);
1324 #ifndef TCC_TARGET_PE
1325 static int tcc_add_support(TCCState
*s1
, const char *filename
)
1328 snprintf(buf
, sizeof(buf
), "%s/%s", s1
->tcc_lib_path
, filename
);
1329 return tcc_add_file(s1
, buf
);
1333 ST_FUNC
void add_array (TCCState
*s1
, const char *sec
, int c
)
1336 s
= find_section(s1
, sec
);
1337 s
->sh_flags
|= SHF_WRITE
;
1338 #ifndef TCC_TARGET_PE
1339 s
->sh_type
= sec
[1] == 'i' ? SHT_INIT_ARRAY
: SHT_FINI_ARRAY
;
1341 put_elf_reloc (s1
->symtab
, s
, s
->data_offset
, R_DATA_PTR
, c
);
1342 section_ptr_add(s
, PTR_SIZE
);
1345 #ifdef CONFIG_TCC_BCHECK
1346 ST_FUNC
void tcc_add_bcheck(TCCState
*s1
)
1348 if (0 == s1
->do_bounds_check
)
1350 section_ptr_add(bounds_section
, sizeof(addr_t
));
1354 #ifdef CONFIG_TCC_BACKTRACE
1355 static void put_ptr(TCCState
*s1
, Section
*s
, int offs
)
1358 c
= set_global_sym(s1
, NULL
, s
, offs
);
1360 put_elf_reloc (s1
->symtab
, s
, s
->data_offset
, R_DATA_PTR
, c
);
1361 section_ptr_add(s
, PTR_SIZE
);
1364 /* set symbol to STB_LOCAL and resolve. The point is to not export it as
1365 a dynamic symbol to allow so's to have one each with a different value. */
1366 static void set_local_sym(TCCState
*s1
, const char *name
, Section
*s
, int offset
)
1368 int c
= find_elf_sym(s1
->symtab
, name
);
1370 ElfW(Sym
) *esym
= (ElfW(Sym
)*)s1
->symtab
->data
+ c
;
1371 esym
->st_info
= ELFW(ST_INFO
)(STB_LOCAL
, STT_NOTYPE
);
1372 esym
->st_value
= offset
;
1373 esym
->st_shndx
= s
->sh_num
;
1377 ST_FUNC
void tcc_add_btstub(TCCState
*s1
)
1385 /* create (part of) a struct rt_context (see tccrun.c) */
1386 put_ptr(s1
, stab_section
, 0);
1387 put_ptr(s1
, stab_section
, -1);
1388 put_ptr(s1
, stab_section
->link
, 0);
1389 section_ptr_add(s
, 3 * PTR_SIZE
);
1391 #ifndef TCC_TARGET_MACHO
1392 /* XXX this relocation is wrong, it uses sym-index 0 (local,undef) */
1393 put_elf_reloc(s1
->symtab
, s
, s
->data_offset
, R_DATA_PTR
, 0);
1395 section_ptr_add(s
, PTR_SIZE
);
1397 #ifdef CONFIG_TCC_BCHECK
1398 if (s1
->do_bounds_check
) {
1399 put_ptr(s1
, bounds_section
, 0);
1403 section_ptr_add(s
, n
);
1407 " extern void __bt_init(),*__rt_info[],__bt_init_dll();"
1408 "__attribute__((constructor)) static void __bt_init_rt(){");
1409 #ifdef TCC_TARGET_PE
1410 if (s1
->output_type
== TCC_OUTPUT_DLL
)
1411 #ifdef CONFIG_TCC_BCHECK
1412 cstr_printf(&cstr
, "__bt_init_dll(%d);", s1
->do_bounds_check
);
1414 cstr_printf(&cstr
, "__bt_init_dll(0);");
1417 cstr_printf(&cstr
, "__bt_init(__rt_info,%d, 0);}",
1418 s1
->output_type
== TCC_OUTPUT_DLL
? 0 : s1
->rt_num_callers
+ 1);
1419 tcc_compile_string(s1
, cstr
.data
);
1421 set_local_sym(s1
, "___rt_info" + !s1
->leading_underscore
, s
, o
);
1425 #ifndef TCC_TARGET_PE
1426 /* add tcc runtime libraries */
1427 ST_FUNC
void tcc_add_runtime(TCCState
*s1
)
1430 #ifdef CONFIG_TCC_BCHECK
1433 tcc_add_pragma_libs(s1
);
1435 if (!s1
->nostdlib
) {
1436 if (s1
->option_pthread
)
1437 tcc_add_library_err(s1
, "pthread");
1438 tcc_add_library_err(s1
, "c");
1440 if (!s1
->static_link
) {
1441 if (TCC_LIBGCC
[0] == '/')
1442 tcc_add_file(s1
, TCC_LIBGCC
);
1444 tcc_add_dll(s1
, TCC_LIBGCC
, 0);
1447 #ifdef CONFIG_TCC_BCHECK
1448 if (s1
->do_bounds_check
&& s1
->output_type
!= TCC_OUTPUT_DLL
) {
1449 tcc_add_library_err(s1
, "pthread");
1450 tcc_add_library_err(s1
, "dl");
1451 tcc_add_support(s1
, "bcheck.o");
1454 #ifdef CONFIG_TCC_BACKTRACE
1455 if (s1
->do_backtrace
) {
1456 if (s1
->output_type
== TCC_OUTPUT_EXE
)
1457 tcc_add_support(s1
, "bt-exe.o");
1458 if (s1
->output_type
!= TCC_OUTPUT_DLL
)
1459 tcc_add_support(s1
, "bt-log.o");
1460 if (s1
->output_type
!= TCC_OUTPUT_MEMORY
)
1464 tcc_add_support(s1
, TCC_LIBTCC1
);
1465 #ifndef TCC_TARGET_MACHO
1466 /* add crt end if not memory output */
1467 if (s1
->output_type
!= TCC_OUTPUT_MEMORY
)
1468 tcc_add_crt(s1
, "crtn.o");
1474 /* add various standard linker symbols (must be done after the
1475 sections are filled (for example after allocating common
1477 static void tcc_add_linker_symbols(TCCState
*s1
)
1483 set_global_sym(s1
, "_etext", text_section
, -1);
1484 set_global_sym(s1
, "_edata", data_section
, -1);
1485 set_global_sym(s1
, "_end", bss_section
, -1);
1486 #ifdef TCC_TARGET_RISCV64
1487 /* XXX should be .sdata+0x800, not .data+0x800 */
1488 set_global_sym(s1
, "__global_pointer$", data_section
, 0x800);
1490 /* horrible new standard ldscript defines */
1491 add_init_array_defines(s1
, ".preinit_array");
1492 add_init_array_defines(s1
, ".init_array");
1493 add_init_array_defines(s1
, ".fini_array");
1494 /* add start and stop symbols for sections whose name can be
1496 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1497 s
= s1
->sections
[i
];
1498 if ((s
->sh_flags
& SHF_ALLOC
)
1499 && (s
->sh_type
== SHT_PROGBITS
1500 || s
->sh_type
== SHT_STRTAB
)) {
1502 /* check if section name can be expressed in C */
1508 if (!isid(c
) && !isnum(c
))
1512 snprintf(buf
, sizeof(buf
), "__start_%s", s
->name
);
1513 set_global_sym(s1
, buf
, s
, 0);
1514 snprintf(buf
, sizeof(buf
), "__stop_%s", s
->name
);
1515 set_global_sym(s1
, buf
, s
, -1);
1521 ST_FUNC
void resolve_common_syms(TCCState
*s1
)
1525 /* Allocate common symbols in BSS. */
1526 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1527 if (sym
->st_shndx
== SHN_COMMON
) {
1528 /* symbol alignment is in st_value for SHN_COMMONs */
1529 sym
->st_value
= section_add(bss_section
, sym
->st_size
,
1531 sym
->st_shndx
= bss_section
->sh_num
;
1535 /* Now assign linker provided symbols their value. */
1536 tcc_add_linker_symbols(s1
);
1539 static void tcc_output_binary(TCCState
*s1
, FILE *f
,
1540 const int *sec_order
)
1543 int i
, offset
, size
;
1546 for(i
=1;i
<s1
->nb_sections
;i
++) {
1547 s
= s1
->sections
[sec_order
[i
]];
1548 if (s
->sh_type
!= SHT_NOBITS
&&
1549 (s
->sh_flags
& SHF_ALLOC
)) {
1550 while (offset
< s
->sh_offset
) {
1555 fwrite(s
->data
, 1, size
, f
);
1561 #ifndef ELF_OBJ_ONLY
1562 ST_FUNC
void fill_got_entry(TCCState
*s1
, ElfW_Rel
*rel
)
1564 int sym_index
= ELFW(R_SYM
) (rel
->r_info
);
1565 ElfW(Sym
) *sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1566 struct sym_attr
*attr
= get_sym_attr(s1
, sym_index
, 0);
1567 unsigned offset
= attr
->got_offset
;
1571 section_reserve(s1
->got
, offset
+ PTR_SIZE
);
1572 #ifdef TCC_TARGET_X86_64
1573 write64le(s1
->got
->data
+ offset
, sym
->st_value
);
1575 write32le(s1
->got
->data
+ offset
, sym
->st_value
);
1579 /* Perform relocation to GOT or PLT entries */
1580 ST_FUNC
void fill_got(TCCState
*s1
)
1586 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1587 s
= s1
->sections
[i
];
1588 if (s
->sh_type
!= SHT_RELX
)
1590 /* no need to handle got relocations */
1591 if (s
->link
!= symtab_section
)
1593 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
1594 switch (ELFW(R_TYPE
) (rel
->r_info
)) {
1595 case R_X86_64_GOT32
:
1596 case R_X86_64_GOTPCREL
:
1597 case R_X86_64_GOTPCRELX
:
1598 case R_X86_64_REX_GOTPCRELX
:
1599 case R_X86_64_PLT32
:
1600 fill_got_entry(s1
, rel
);
1607 /* See put_got_entry for a description. This is the second stage
1608 where GOT references to local defined symbols are rewritten. */
1609 static void fill_local_got_entries(TCCState
*s1
)
1612 if (!s1
->got
->reloc
)
1614 for_each_elem(s1
->got
->reloc
, 0, rel
, ElfW_Rel
) {
1615 if (ELFW(R_TYPE
)(rel
->r_info
) == R_RELATIVE
) {
1616 int sym_index
= ELFW(R_SYM
) (rel
->r_info
);
1617 ElfW(Sym
) *sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1618 struct sym_attr
*attr
= get_sym_attr(s1
, sym_index
, 0);
1619 unsigned offset
= attr
->got_offset
;
1620 if (offset
!= rel
->r_offset
- s1
->got
->sh_addr
)
1621 tcc_error_noabort("huh");
1622 rel
->r_info
= ELFW(R_INFO
)(0, R_RELATIVE
);
1623 #if SHT_RELX == SHT_RELA
1624 rel
->r_addend
= sym
->st_value
;
1626 /* All our REL architectures also happen to be 32bit LE. */
1627 write32le(s1
->got
->data
+ offset
, sym
->st_value
);
1633 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1634 in shared libraries and export non local defined symbols to shared libraries
1635 if -rdynamic switch was given on command line */
1636 static void bind_exe_dynsyms(TCCState
*s1
)
1639 int sym_index
, index
;
1640 ElfW(Sym
) *sym
, *esym
;
1643 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1644 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1645 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1646 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1647 if (sym
->st_shndx
== SHN_UNDEF
) {
1648 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1649 sym_index
= find_elf_sym(s1
->dynsymtab_section
, name
);
1651 esym
= &((ElfW(Sym
) *)s1
->dynsymtab_section
->data
)[sym_index
];
1652 type
= ELFW(ST_TYPE
)(esym
->st_info
);
1653 if ((type
== STT_FUNC
) || (type
== STT_GNU_IFUNC
)) {
1654 /* Indirect functions shall have STT_FUNC type in executable
1655 * dynsym section. Indeed, a dlsym call following a lazy
1656 * resolution would pick the symbol value from the
1657 * executable dynsym entry which would contain the address
1658 * of the function wanted by the caller of dlsym instead of
1659 * the address of the function that would return that
1662 = put_elf_sym(s1
->dynsym
, 0, esym
->st_size
,
1663 ELFW(ST_INFO
)(STB_GLOBAL
,STT_FUNC
), 0, 0,
1665 int index
= sym
- (ElfW(Sym
) *) symtab_section
->data
;
1666 get_sym_attr(s1
, index
, 1)->dyn_index
= dynindex
;
1667 } else if (type
== STT_OBJECT
) {
1668 unsigned long offset
;
1670 offset
= bss_section
->data_offset
;
1671 /* XXX: which alignment ? */
1672 offset
= (offset
+ 16 - 1) & -16;
1673 set_elf_sym (s1
->symtab
, offset
, esym
->st_size
,
1674 esym
->st_info
, 0, bss_section
->sh_num
, name
);
1675 index
= put_elf_sym(s1
->dynsym
, offset
, esym
->st_size
,
1676 esym
->st_info
, 0, bss_section
->sh_num
,
1679 /* Ensure R_COPY works for weak symbol aliases */
1680 if (ELFW(ST_BIND
)(esym
->st_info
) == STB_WEAK
) {
1681 for_each_elem(s1
->dynsymtab_section
, 1, dynsym
, ElfW(Sym
)) {
1682 if ((dynsym
->st_value
== esym
->st_value
)
1683 && (ELFW(ST_BIND
)(dynsym
->st_info
) == STB_GLOBAL
)) {
1684 char *dynname
= (char *) s1
->dynsymtab_section
->link
->data
1686 put_elf_sym(s1
->dynsym
, offset
, dynsym
->st_size
,
1688 bss_section
->sh_num
, dynname
);
1694 put_elf_reloc(s1
->dynsym
, bss_section
,
1695 offset
, R_COPY
, index
);
1696 offset
+= esym
->st_size
;
1697 bss_section
->data_offset
= offset
;
1700 /* STB_WEAK undefined symbols are accepted */
1701 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1702 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
||
1703 !strcmp(name
, "_fp_hw")) {
1705 tcc_error_noabort("undefined symbol '%s'", name
);
1708 } else if (s1
->rdynamic
&& ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1709 /* if -rdynamic option, then export all non local symbols */
1710 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1711 set_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
, sym
->st_info
,
1712 0, sym
->st_shndx
, name
);
1717 /* Bind symbols of libraries: export all non local symbols of executable that
1718 are referenced by shared libraries. The reason is that the dynamic loader
1719 search symbol first in executable and then in libraries. Therefore a
1720 reference to a symbol already defined by a library can still be resolved by
1721 a symbol in the executable. */
1722 static void bind_libs_dynsyms(TCCState
*s1
)
1726 ElfW(Sym
) *sym
, *esym
;
1728 for_each_elem(s1
->dynsymtab_section
, 1, esym
, ElfW(Sym
)) {
1729 name
= (char *) s1
->dynsymtab_section
->link
->data
+ esym
->st_name
;
1730 sym_index
= find_elf_sym(symtab_section
, name
);
1731 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1732 if (sym_index
&& sym
->st_shndx
!= SHN_UNDEF
1733 && ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1734 set_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
,
1735 sym
->st_info
, 0, sym
->st_shndx
, name
);
1736 } else if (esym
->st_shndx
== SHN_UNDEF
) {
1737 /* weak symbols can stay undefined */
1738 if (ELFW(ST_BIND
)(esym
->st_info
) != STB_WEAK
)
1739 tcc_warning("undefined dynamic symbol '%s'", name
);
1744 /* Export all non local symbols. This is used by shared libraries so that the
1745 non local symbols they define can resolve a reference in another shared
1746 library or in the executable. Correspondingly, it allows undefined local
1747 symbols to be resolved by other shared libraries or by the executable. */
1748 static void export_global_syms(TCCState
*s1
)
1750 int dynindex
, index
;
1754 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1755 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1756 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1757 dynindex
= put_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
,
1758 sym
->st_info
, 0, sym
->st_shndx
, name
);
1759 index
= sym
- (ElfW(Sym
) *) symtab_section
->data
;
1760 get_sym_attr(s1
, index
, 1)->dyn_index
= dynindex
;
1766 /* Allocate strings for section names and decide if an unallocated section
1768 NOTE: the strsec section comes last, so its size is also correct ! */
1769 static int alloc_sec_names(TCCState
*s1
, int file_type
, Section
*strsec
)
1775 /* Allocate strings for section names */
1776 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1777 s
= s1
->sections
[i
];
1778 /* when generating a DLL, we include relocations but we may
1780 #ifndef ELF_OBJ_ONLY
1781 if (file_type
== TCC_OUTPUT_DLL
&&
1782 s
->sh_type
== SHT_RELX
&&
1783 !(s
->sh_flags
& SHF_ALLOC
) &&
1784 (s1
->sections
[s
->sh_info
]->sh_flags
& SHF_ALLOC
) &&
1785 prepare_dynamic_rel(s1
, s
)) {
1786 if (!(s1
->sections
[s
->sh_info
]->sh_flags
& SHF_WRITE
))
1790 if ((s1
->do_debug
&& s
->sh_type
!= SHT_RELX
) ||
1791 file_type
== TCC_OUTPUT_OBJ
||
1792 (s
->sh_flags
& SHF_ALLOC
) ||
1793 i
== (s1
->nb_sections
- 1)) {
1794 /* we output all sections if debug or object file */
1795 s
->sh_size
= s
->data_offset
;
1797 if (s
->sh_size
|| (s
->sh_flags
& SHF_ALLOC
))
1798 s
->sh_name
= put_elf_str(strsec
, s
->name
);
1800 strsec
->sh_size
= strsec
->data_offset
;
1804 /* Info to be copied in dynamic section */
1808 unsigned long data_offset
;
1811 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1817 /* Assign sections to segments and decide how are sections laid out when loaded
1818 in memory. This function also fills corresponding program headers. */
1819 static int layout_sections(TCCState
*s1
, ElfW(Phdr
) *phdr
, int phnum
,
1820 Section
*interp
, Section
* strsec
,
1821 struct dyn_inf
*dyninf
, int *sec_order
)
1823 int i
, j
, k
, file_type
, sh_order_index
, file_offset
;
1824 unsigned long s_align
;
1830 file_type
= s1
->output_type
;
1833 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
)
1834 file_offset
= sizeof(ElfW(Ehdr
)) + phnum
* sizeof(ElfW(Phdr
));
1835 s_align
= ELF_PAGE_SIZE
;
1836 if (s1
->section_align
)
1837 s_align
= s1
->section_align
;
1840 if (s1
->has_text_addr
) {
1841 int a_offset
, p_offset
;
1842 addr
= s1
->text_addr
;
1843 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1845 a_offset
= (int) (addr
& (s_align
- 1));
1846 p_offset
= file_offset
& (s_align
- 1);
1847 if (a_offset
< p_offset
)
1848 a_offset
+= s_align
;
1849 file_offset
+= (a_offset
- p_offset
);
1851 if (file_type
== TCC_OUTPUT_DLL
)
1854 addr
= ELF_START_ADDR
;
1855 /* compute address after headers */
1856 addr
+= (file_offset
& (s_align
- 1));
1860 /* Leave one program headers for the program interpreter and one for
1861 the program header table itself if needed. These are done later as
1862 they require section layout to be done first. */
1866 /* dynamic relocation table information, for .dynamic section */
1867 dyninf
->rel_addr
= dyninf
->rel_size
= 0;
1868 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1869 dyninf
->bss_addr
= dyninf
->bss_size
= 0;
1872 for(j
= 0; j
< 2; j
++) {
1873 ph
->p_type
= PT_LOAD
;
1875 ph
->p_flags
= PF_R
| PF_X
;
1877 ph
->p_flags
= PF_R
| PF_W
;
1878 ph
->p_align
= s_align
;
1880 /* Decide the layout of sections loaded in memory. This must
1881 be done before program headers are filled since they contain
1882 info about the layout. We do the following ordering: interp,
1883 symbol tables, relocations, progbits, nobits */
1884 /* XXX: do faster and simpler sorting */
1885 for(k
= 0; k
< 5; k
++) {
1886 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1887 s
= s1
->sections
[i
];
1888 /* compute if section should be included */
1890 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_WRITE
)) !=
1894 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_WRITE
)) !=
1895 (SHF_ALLOC
| SHF_WRITE
))
1901 } else if ((s
->sh_type
== SHT_DYNSYM
||
1902 s
->sh_type
== SHT_STRTAB
||
1903 s
->sh_type
== SHT_HASH
)
1904 && !strstr(s
->name
, ".stab")) {
1907 } else if (s
->sh_type
== SHT_RELX
) {
1910 } else if (s
->sh_type
== SHT_NOBITS
) {
1917 sec_order
[sh_order_index
++] = i
;
1919 /* section matches: we align it and add its size */
1921 addr
= (addr
+ s
->sh_addralign
- 1) &
1922 ~(s
->sh_addralign
- 1);
1923 file_offset
+= (int) ( addr
- tmp
);
1924 s
->sh_offset
= file_offset
;
1927 /* update program header infos */
1928 if (ph
->p_offset
== 0) {
1929 ph
->p_offset
= file_offset
;
1931 ph
->p_paddr
= ph
->p_vaddr
;
1933 /* update dynamic relocation infos */
1934 if (s
->sh_type
== SHT_RELX
) {
1935 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1936 if (!strcmp(strsec
->data
+ s
->sh_name
, ".rel.got")) {
1937 dyninf
->rel_addr
= addr
;
1938 dyninf
->rel_size
+= s
->sh_size
; /* XXX only first rel. */
1940 if (!strcmp(strsec
->data
+ s
->sh_name
, ".rel.bss")) {
1941 dyninf
->bss_addr
= addr
;
1942 dyninf
->bss_size
= s
->sh_size
; /* XXX only first rel. */
1945 if (dyninf
->rel_size
== 0)
1946 dyninf
->rel_addr
= addr
;
1947 dyninf
->rel_size
+= s
->sh_size
;
1951 if (s
->sh_type
!= SHT_NOBITS
)
1952 file_offset
+= s
->sh_size
;
1956 /* Make the first PT_LOAD segment include the program
1957 headers itself (and the ELF header as well), it'll
1958 come out with same memory use but will make various
1959 tools like binutils strip work better. */
1960 ph
->p_offset
&= ~(ph
->p_align
- 1);
1961 ph
->p_vaddr
&= ~(ph
->p_align
- 1);
1962 ph
->p_paddr
&= ~(ph
->p_align
- 1);
1964 ph
->p_filesz
= file_offset
- ph
->p_offset
;
1965 ph
->p_memsz
= addr
- ph
->p_vaddr
;
1968 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
) {
1969 /* if in the middle of a page, we duplicate the page in
1970 memory so that one copy is RX and the other is RW */
1971 if ((addr
& (s_align
- 1)) != 0)
1974 addr
= (addr
+ s_align
- 1) & ~(s_align
- 1);
1975 file_offset
= (file_offset
+ s_align
- 1) & ~(s_align
- 1);
1981 /* all other sections come after */
1982 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1983 s
= s1
->sections
[i
];
1984 if (phnum
> 0 && (s
->sh_flags
& SHF_ALLOC
))
1986 sec_order
[sh_order_index
++] = i
;
1988 file_offset
= (file_offset
+ s
->sh_addralign
- 1) &
1989 ~(s
->sh_addralign
- 1);
1990 s
->sh_offset
= file_offset
;
1991 if (s
->sh_type
!= SHT_NOBITS
)
1992 file_offset
+= s
->sh_size
;
1998 #ifndef ELF_OBJ_ONLY
1999 /* put dynamic tag */
2000 static void put_dt(Section
*dynamic
, int dt
, addr_t val
)
2003 dyn
= section_ptr_add(dynamic
, sizeof(ElfW(Dyn
)));
2005 dyn
->d_un
.d_val
= val
;
2008 static void fill_unloadable_phdr(ElfW(Phdr
) *phdr
, int phnum
, Section
*interp
,
2013 /* if interpreter, then add corresponding program header */
2017 ph
->p_type
= PT_PHDR
;
2018 ph
->p_offset
= sizeof(ElfW(Ehdr
));
2019 ph
->p_filesz
= ph
->p_memsz
= phnum
* sizeof(ElfW(Phdr
));
2020 ph
->p_vaddr
= interp
->sh_addr
- ph
->p_filesz
;
2021 ph
->p_paddr
= ph
->p_vaddr
;
2022 ph
->p_flags
= PF_R
| PF_X
;
2023 ph
->p_align
= 4; /* interp->sh_addralign; */
2026 ph
->p_type
= PT_INTERP
;
2027 ph
->p_offset
= interp
->sh_offset
;
2028 ph
->p_vaddr
= interp
->sh_addr
;
2029 ph
->p_paddr
= ph
->p_vaddr
;
2030 ph
->p_filesz
= interp
->sh_size
;
2031 ph
->p_memsz
= interp
->sh_size
;
2033 ph
->p_align
= interp
->sh_addralign
;
2036 /* if dynamic section, then add corresponding program header */
2038 ph
= &phdr
[phnum
- 1];
2040 ph
->p_type
= PT_DYNAMIC
;
2041 ph
->p_offset
= dynamic
->sh_offset
;
2042 ph
->p_vaddr
= dynamic
->sh_addr
;
2043 ph
->p_paddr
= ph
->p_vaddr
;
2044 ph
->p_filesz
= dynamic
->sh_size
;
2045 ph
->p_memsz
= dynamic
->sh_size
;
2046 ph
->p_flags
= PF_R
| PF_W
;
2047 ph
->p_align
= dynamic
->sh_addralign
;
2051 /* Fill the dynamic section with tags describing the address and size of
2053 static void fill_dynamic(TCCState
*s1
, struct dyn_inf
*dyninf
)
2055 Section
*dynamic
= dyninf
->dynamic
;
2058 /* put dynamic section entries */
2059 put_dt(dynamic
, DT_HASH
, s1
->dynsym
->hash
->sh_addr
);
2060 put_dt(dynamic
, DT_STRTAB
, dyninf
->dynstr
->sh_addr
);
2061 put_dt(dynamic
, DT_SYMTAB
, s1
->dynsym
->sh_addr
);
2062 put_dt(dynamic
, DT_STRSZ
, dyninf
->dynstr
->data_offset
);
2063 put_dt(dynamic
, DT_SYMENT
, sizeof(ElfW(Sym
)));
2065 put_dt(dynamic
, DT_RELA
, dyninf
->rel_addr
);
2066 put_dt(dynamic
, DT_RELASZ
, dyninf
->rel_size
);
2067 put_dt(dynamic
, DT_RELAENT
, sizeof(ElfW_Rel
));
2069 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2070 put_dt(dynamic
, DT_PLTGOT
, s1
->got
->sh_addr
);
2071 put_dt(dynamic
, DT_PLTRELSZ
, dyninf
->rel_size
);
2072 put_dt(dynamic
, DT_JMPREL
, dyninf
->rel_addr
);
2073 put_dt(dynamic
, DT_PLTREL
, DT_REL
);
2074 put_dt(dynamic
, DT_REL
, dyninf
->bss_addr
);
2075 put_dt(dynamic
, DT_RELSZ
, dyninf
->bss_size
);
2077 put_dt(dynamic
, DT_REL
, dyninf
->rel_addr
);
2078 put_dt(dynamic
, DT_RELSZ
, dyninf
->rel_size
);
2079 put_dt(dynamic
, DT_RELENT
, sizeof(ElfW_Rel
));
2083 put_dt(dynamic
, DT_VERSYM
, versym_section
->sh_addr
);
2084 if (verneed_section
) {
2085 put_dt(dynamic
, DT_VERNEED
, verneed_section
->sh_addr
);
2086 put_dt(dynamic
, DT_VERNEEDNUM
, dt_verneednum
);
2088 s
= find_section_create (s1
, ".preinit_array", 0);
2089 if (s
&& s
->data_offset
) {
2090 put_dt(dynamic
, DT_PREINIT_ARRAY
, s
->sh_addr
);
2091 put_dt(dynamic
, DT_PREINIT_ARRAYSZ
, s
->data_offset
);
2093 s
= find_section_create (s1
, ".init_array", 0);
2094 if (s
&& s
->data_offset
) {
2095 put_dt(dynamic
, DT_INIT_ARRAY
, s
->sh_addr
);
2096 put_dt(dynamic
, DT_INIT_ARRAYSZ
, s
->data_offset
);
2098 s
= find_section_create (s1
, ".fini_array", 0);
2099 if (s
&& s
->data_offset
) {
2100 put_dt(dynamic
, DT_FINI_ARRAY
, s
->sh_addr
);
2101 put_dt(dynamic
, DT_FINI_ARRAYSZ
, s
->data_offset
);
2103 s
= find_section_create (s1
, ".init", 0);
2104 if (s
&& s
->data_offset
) {
2105 put_dt(dynamic
, DT_INIT
, s
->sh_addr
);
2107 s
= find_section_create (s1
, ".fini", 0);
2108 if (s
&& s
->data_offset
) {
2109 put_dt(dynamic
, DT_FINI
, s
->sh_addr
);
2112 put_dt(dynamic
, DT_DEBUG
, 0);
2113 put_dt(dynamic
, DT_NULL
, 0);
2116 /* Relocate remaining sections and symbols (that is those not related to
2118 static int final_sections_reloc(TCCState
*s1
)
2123 relocate_syms(s1
, s1
->symtab
, 0);
2125 if (s1
->nb_errors
!= 0)
2128 /* relocate sections */
2129 /* XXX: ignore sections with allocated relocations ? */
2130 for(i
= 1; i
< s1
->nb_sections
; i
++) {
2131 s
= s1
->sections
[i
];
2132 if (s
->reloc
&& (s
!= s1
->got
|| s1
->static_link
))
2133 relocate_section(s1
, s
);
2136 /* relocate relocation entries if the relocation tables are
2137 allocated in the executable */
2138 for(i
= 1; i
< s1
->nb_sections
; i
++) {
2139 s
= s1
->sections
[i
];
2140 if ((s
->sh_flags
& SHF_ALLOC
) &&
2141 s
->sh_type
== SHT_RELX
) {
2142 relocate_rel(s1
, s
);
2149 /* Create an ELF file on disk.
2150 This function handle ELF specific layout requirements */
2151 static void tcc_output_elf(TCCState
*s1
, FILE *f
, int phnum
, ElfW(Phdr
) *phdr
,
2152 int file_offset
, int *sec_order
)
2154 int i
, shnum
, offset
, size
, file_type
;
2157 ElfW(Shdr
) shdr
, *sh
;
2159 file_type
= s1
->output_type
;
2160 shnum
= s1
->nb_sections
;
2162 memset(&ehdr
, 0, sizeof(ehdr
));
2165 ehdr
.e_phentsize
= sizeof(ElfW(Phdr
));
2166 ehdr
.e_phnum
= phnum
;
2167 ehdr
.e_phoff
= sizeof(ElfW(Ehdr
));
2171 file_offset
= (file_offset
+ 3) & -4;
2174 ehdr
.e_ident
[0] = ELFMAG0
;
2175 ehdr
.e_ident
[1] = ELFMAG1
;
2176 ehdr
.e_ident
[2] = ELFMAG2
;
2177 ehdr
.e_ident
[3] = ELFMAG3
;
2178 ehdr
.e_ident
[4] = ELFCLASSW
;
2179 ehdr
.e_ident
[5] = ELFDATA2LSB
;
2180 ehdr
.e_ident
[6] = EV_CURRENT
;
2181 #if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
2182 /* FIXME: should set only for freebsd _target_, but we exclude only PE target */
2183 ehdr
.e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
2185 #ifdef TCC_TARGET_ARM
2187 ehdr
.e_ident
[EI_OSABI
] = 0;
2188 ehdr
.e_flags
= EF_ARM_EABI_VER4
;
2189 if (file_type
== TCC_OUTPUT_EXE
|| file_type
== TCC_OUTPUT_DLL
)
2190 ehdr
.e_flags
|= EF_ARM_HASENTRY
;
2191 if (s1
->float_abi
== ARM_HARD_FLOAT
)
2192 ehdr
.e_flags
|= EF_ARM_VFP_FLOAT
;
2194 ehdr
.e_flags
|= EF_ARM_SOFT_FLOAT
;
2196 ehdr
.e_ident
[EI_OSABI
] = ELFOSABI_ARM
;
2198 #elif defined TCC_TARGET_RISCV64
2199 ehdr
.e_flags
= EF_RISCV_FLOAT_ABI_DOUBLE
;
2203 case TCC_OUTPUT_EXE
:
2204 ehdr
.e_type
= ET_EXEC
;
2205 ehdr
.e_entry
= get_sym_addr(s1
, "_start", 1, 0);
2207 case TCC_OUTPUT_DLL
:
2208 ehdr
.e_type
= ET_DYN
;
2209 ehdr
.e_entry
= text_section
->sh_addr
; /* XXX: is it correct ? */
2211 case TCC_OUTPUT_OBJ
:
2212 ehdr
.e_type
= ET_REL
;
2215 ehdr
.e_machine
= EM_TCC_TARGET
;
2216 ehdr
.e_version
= EV_CURRENT
;
2217 ehdr
.e_shoff
= file_offset
;
2218 ehdr
.e_ehsize
= sizeof(ElfW(Ehdr
));
2219 ehdr
.e_shentsize
= sizeof(ElfW(Shdr
));
2220 ehdr
.e_shnum
= shnum
;
2221 ehdr
.e_shstrndx
= shnum
- 1;
2223 fwrite(&ehdr
, 1, sizeof(ElfW(Ehdr
)), f
);
2224 fwrite(phdr
, 1, phnum
* sizeof(ElfW(Phdr
)), f
);
2225 offset
= sizeof(ElfW(Ehdr
)) + phnum
* sizeof(ElfW(Phdr
));
2227 sort_syms(s1
, symtab_section
);
2228 for(i
= 1; i
< s1
->nb_sections
; i
++) {
2229 s
= s1
->sections
[sec_order
[i
]];
2230 if (s
->sh_type
!= SHT_NOBITS
) {
2231 while (offset
< s
->sh_offset
) {
2237 fwrite(s
->data
, 1, size
, f
);
2242 /* output section headers */
2243 while (offset
< ehdr
.e_shoff
) {
2248 for(i
= 0; i
< s1
->nb_sections
; i
++) {
2250 memset(sh
, 0, sizeof(ElfW(Shdr
)));
2251 s
= s1
->sections
[i
];
2253 sh
->sh_name
= s
->sh_name
;
2254 sh
->sh_type
= s
->sh_type
;
2255 sh
->sh_flags
= s
->sh_flags
;
2256 sh
->sh_entsize
= s
->sh_entsize
;
2257 sh
->sh_info
= s
->sh_info
;
2259 sh
->sh_link
= s
->link
->sh_num
;
2260 sh
->sh_addralign
= s
->sh_addralign
;
2261 sh
->sh_addr
= s
->sh_addr
;
2262 sh
->sh_offset
= s
->sh_offset
;
2263 sh
->sh_size
= s
->sh_size
;
2265 fwrite(sh
, 1, sizeof(ElfW(Shdr
)), f
);
2269 /* Write an elf, coff or "binary" file */
2270 static int tcc_write_elf_file(TCCState
*s1
, const char *filename
, int phnum
,
2271 ElfW(Phdr
) *phdr
, int file_offset
, int *sec_order
)
2273 int fd
, mode
, file_type
;
2276 file_type
= s1
->output_type
;
2277 if (file_type
== TCC_OUTPUT_OBJ
)
2282 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, mode
);
2284 tcc_error_noabort("could not write '%s'", filename
);
2287 f
= fdopen(fd
, "wb");
2289 printf("<- %s\n", filename
);
2291 #ifdef TCC_TARGET_COFF
2292 if (s1
->output_format
== TCC_OUTPUT_FORMAT_COFF
)
2293 tcc_output_coff(s1
, f
);
2296 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
)
2297 tcc_output_elf(s1
, f
, phnum
, phdr
, file_offset
, sec_order
);
2299 tcc_output_binary(s1
, f
, sec_order
);
2305 #ifndef ELF_OBJ_ONLY
2306 /* Sort section headers by assigned sh_addr, remove sections
2307 that we aren't going to output. */
2308 static void tidy_section_headers(TCCState
*s1
, int *sec_order
)
2310 int i
, nnew
, l
, *backmap
;
2314 snew
= tcc_malloc(s1
->nb_sections
* sizeof(snew
[0]));
2315 backmap
= tcc_malloc(s1
->nb_sections
* sizeof(backmap
[0]));
2316 for (i
= 0, nnew
= 0, l
= s1
->nb_sections
; i
< s1
->nb_sections
; i
++) {
2317 s
= s1
->sections
[sec_order
[i
]];
2318 if (!i
|| s
->sh_name
) {
2319 backmap
[sec_order
[i
]] = nnew
;
2323 backmap
[sec_order
[i
]] = 0;
2327 for (i
= 0; i
< nnew
; i
++) {
2331 if (s
->sh_type
== SHT_RELX
)
2332 s
->sh_info
= backmap
[s
->sh_info
];
2336 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
))
2337 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
)
2338 sym
->st_shndx
= backmap
[sym
->st_shndx
];
2339 if( !s1
->static_link
) {
2340 for_each_elem(s1
->dynsym
, 1, sym
, ElfW(Sym
))
2341 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
)
2342 sym
->st_shndx
= backmap
[sym
->st_shndx
];
2344 for (i
= 0; i
< s1
->nb_sections
; i
++)
2346 tcc_free(s1
->sections
);
2347 s1
->sections
= snew
;
2348 s1
->nb_sections
= nnew
;
2353 /* Output an elf, coff or binary file */
2354 /* XXX: suppress unneeded sections */
2355 static int elf_output_file(TCCState
*s1
, const char *filename
)
2357 int ret
, phnum
, shnum
, file_type
, file_offset
, *sec_order
;
2358 struct dyn_inf dyninf
= {0};
2360 Section
*strsec
, *interp
, *dynamic
, *dynstr
;
2362 file_type
= s1
->output_type
;
2367 interp
= dynamic
= dynstr
= NULL
; /* avoid warning */
2369 #ifndef ELF_OBJ_ONLY
2370 if (file_type
!= TCC_OUTPUT_OBJ
) {
2371 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2372 tcc_add_runtime(s1
);
2373 resolve_common_syms(s1
);
2375 if (!s1
->static_link
) {
2376 if (file_type
== TCC_OUTPUT_EXE
) {
2378 /* allow override the dynamic loader */
2379 const char *elfint
= getenv("LD_SO");
2381 elfint
= DEFAULT_ELFINTERP(s1
);
2382 /* add interpreter section only if executable */
2383 interp
= new_section(s1
, ".interp", SHT_PROGBITS
, SHF_ALLOC
);
2384 interp
->sh_addralign
= 1;
2385 ptr
= section_ptr_add(interp
, 1 + strlen(elfint
));
2386 strcpy(ptr
, elfint
);
2389 /* add dynamic symbol table */
2390 s1
->dynsym
= new_symtab(s1
, ".dynsym", SHT_DYNSYM
, SHF_ALLOC
,
2392 ".hash", SHF_ALLOC
);
2393 dynstr
= s1
->dynsym
->link
;
2394 /* add dynamic section */
2395 dynamic
= new_section(s1
, ".dynamic", SHT_DYNAMIC
,
2396 SHF_ALLOC
| SHF_WRITE
);
2397 dynamic
->link
= dynstr
;
2398 dynamic
->sh_entsize
= sizeof(ElfW(Dyn
));
2402 if (file_type
== TCC_OUTPUT_EXE
) {
2403 bind_exe_dynsyms(s1
);
2406 bind_libs_dynsyms(s1
);
2408 /* shared library case: simply export all global symbols */
2409 export_global_syms(s1
);
2412 build_got_entries(s1
);
2417 /* we add a section for symbols */
2418 strsec
= new_section(s1
, ".shstrtab", SHT_STRTAB
, 0);
2419 put_elf_str(strsec
, "");
2421 /* Allocate strings for section names */
2422 ret
= alloc_sec_names(s1
, file_type
, strsec
);
2424 #ifndef ELF_OBJ_ONLY
2427 /* add a list of needed dlls */
2428 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
2429 DLLReference
*dllref
= s1
->loaded_dlls
[i
];
2430 if (dllref
->level
== 0)
2431 put_dt(dynamic
, DT_NEEDED
, put_elf_str(dynstr
, dllref
->name
));
2435 put_dt(dynamic
, s1
->enable_new_dtags
? DT_RUNPATH
: DT_RPATH
,
2436 put_elf_str(dynstr
, s1
->rpath
));
2438 if (file_type
== TCC_OUTPUT_DLL
) {
2440 put_dt(dynamic
, DT_SONAME
, put_elf_str(dynstr
, s1
->soname
));
2441 /* XXX: currently, since we do not handle PIC code, we
2442 must relocate the readonly segments */
2444 put_dt(dynamic
, DT_TEXTREL
, 0);
2448 put_dt(dynamic
, DT_SYMBOLIC
, 0);
2450 dyninf
.dynamic
= dynamic
;
2451 dyninf
.dynstr
= dynstr
;
2452 /* remember offset and reserve space for 2nd call below */
2453 dyninf
.data_offset
= dynamic
->data_offset
;
2454 fill_dynamic(s1
, &dyninf
);
2455 dynamic
->sh_size
= dynamic
->data_offset
;
2456 dynstr
->sh_size
= dynstr
->data_offset
;
2460 /* compute number of program headers */
2461 if (file_type
== TCC_OUTPUT_OBJ
)
2463 else if (file_type
== TCC_OUTPUT_DLL
)
2465 else if (s1
->static_link
)
2470 /* allocate program segment headers */
2471 phdr
= tcc_mallocz(phnum
* sizeof(ElfW(Phdr
)));
2473 /* compute number of sections */
2474 shnum
= s1
->nb_sections
;
2476 /* this array is used to reorder sections in the output file */
2477 sec_order
= tcc_malloc(sizeof(int) * shnum
);
2480 /* compute section to program header mapping */
2481 file_offset
= layout_sections(s1
, phdr
, phnum
, interp
, strsec
, &dyninf
,
2484 #ifndef ELF_OBJ_ONLY
2485 /* Fill remaining program header and finalize relocation related to dynamic
2487 if (file_type
!= TCC_OUTPUT_OBJ
) {
2488 fill_unloadable_phdr(phdr
, phnum
, interp
, dynamic
);
2491 dynamic
->data_offset
= dyninf
.data_offset
;
2492 fill_dynamic(s1
, &dyninf
);
2494 /* put in GOT the dynamic section address and relocate PLT */
2495 write32le(s1
->got
->data
, dynamic
->sh_addr
);
2496 if (file_type
== TCC_OUTPUT_EXE
2497 || (RELOCATE_DLLPLT
&& file_type
== TCC_OUTPUT_DLL
))
2500 /* relocate symbols in .dynsym now that final addresses are known */
2501 for_each_elem(s1
->dynsym
, 1, sym
, ElfW(Sym
)) {
2502 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
) {
2503 /* do symbol relocation */
2504 sym
->st_value
+= s1
->sections
[sym
->st_shndx
]->sh_addr
;
2509 /* if building executable or DLL, then relocate each section
2510 except the GOT which is already relocated */
2511 ret
= final_sections_reloc(s1
);
2514 tidy_section_headers(s1
, sec_order
);
2516 /* Perform relocation to GOT or PLT entries */
2517 if (file_type
== TCC_OUTPUT_EXE
&& s1
->static_link
)
2520 fill_local_got_entries(s1
);
2524 /* Create the ELF file with name 'filename' */
2525 ret
= tcc_write_elf_file(s1
, filename
, phnum
, phdr
, file_offset
, sec_order
);
2526 s1
->nb_sections
= shnum
;
2529 tcc_free(sec_order
);
2534 LIBTCCAPI
int tcc_output_file(TCCState
*s
, const char *filename
)
2537 #ifdef TCC_TARGET_PE
2538 if (s
->output_type
!= TCC_OUTPUT_OBJ
) {
2539 ret
= pe_output_file(s
, filename
);
2541 #elif TCC_TARGET_MACHO
2542 if (s
->output_type
!= TCC_OUTPUT_OBJ
) {
2543 ret
= macho_output_file(s
, filename
);
2546 ret
= elf_output_file(s
, filename
);
2550 ssize_t
full_read(int fd
, void *buf
, size_t count
) {
2554 ssize_t num
= read(fd
, cbuf
, count
-rnum
);
2555 if (num
< 0) return num
;
2556 if (num
== 0) return rnum
;
2562 static void *load_data(int fd
, unsigned long file_offset
, unsigned long size
)
2566 data
= tcc_malloc(size
);
2567 lseek(fd
, file_offset
, SEEK_SET
);
2568 full_read(fd
, data
, size
);
2572 typedef struct SectionMergeInfo
{
2573 Section
*s
; /* corresponding existing section */
2574 unsigned long offset
; /* offset of the new section in the existing section */
2575 uint8_t new_section
; /* true if section 's' was added */
2576 uint8_t link_once
; /* true if link once section */
2579 ST_FUNC
int tcc_object_type(int fd
, ElfW(Ehdr
) *h
)
2581 int size
= full_read(fd
, h
, sizeof *h
);
2582 if (size
== sizeof *h
&& 0 == memcmp(h
, ELFMAG
, 4)) {
2583 if (h
->e_type
== ET_REL
)
2584 return AFF_BINTYPE_REL
;
2585 if (h
->e_type
== ET_DYN
)
2586 return AFF_BINTYPE_DYN
;
2587 } else if (size
>= 8) {
2588 if (0 == memcmp(h
, ARMAG
, 8))
2589 return AFF_BINTYPE_AR
;
2590 #ifdef TCC_TARGET_COFF
2591 if (((struct filehdr
*)h
)->f_magic
== COFF_C67_MAGIC
)
2592 return AFF_BINTYPE_C67
;
2598 /* load an object file and merge it with current files */
2599 /* XXX: handle correctly stab (debug) info */
2600 ST_FUNC
int tcc_load_object_file(TCCState
*s1
,
2601 int fd
, unsigned long file_offset
)
2604 ElfW(Shdr
) *shdr
, *sh
;
2605 int size
, i
, j
, offset
, offseti
, nb_syms
, sym_index
, ret
, seencompressed
;
2606 char *strsec
, *strtab
;
2607 int stab_index
, stabstr_index
;
2608 int *old_to_new_syms
;
2609 char *sh_name
, *name
;
2610 SectionMergeInfo
*sm_table
, *sm
;
2611 ElfW(Sym
) *sym
, *symtab
;
2615 lseek(fd
, file_offset
, SEEK_SET
);
2616 if (tcc_object_type(fd
, &ehdr
) != AFF_BINTYPE_REL
)
2618 /* test CPU specific stuff */
2619 if (ehdr
.e_ident
[5] != ELFDATA2LSB
||
2620 ehdr
.e_machine
!= EM_TCC_TARGET
) {
2622 tcc_error_noabort("invalid object file");
2626 shdr
= load_data(fd
, file_offset
+ ehdr
.e_shoff
,
2627 sizeof(ElfW(Shdr
)) * ehdr
.e_shnum
);
2628 sm_table
= tcc_mallocz(sizeof(SectionMergeInfo
) * ehdr
.e_shnum
);
2630 /* load section names */
2631 sh
= &shdr
[ehdr
.e_shstrndx
];
2632 strsec
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2634 /* load symtab and strtab */
2635 old_to_new_syms
= NULL
;
2640 stab_index
= stabstr_index
= 0;
2642 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2644 if (sh
->sh_type
== SHT_SYMTAB
) {
2646 tcc_error_noabort("object must contain only one symtab");
2651 nb_syms
= sh
->sh_size
/ sizeof(ElfW(Sym
));
2652 symtab
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2653 sm_table
[i
].s
= symtab_section
;
2655 /* now load strtab */
2656 sh
= &shdr
[sh
->sh_link
];
2657 strtab
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2659 if (sh
->sh_flags
& SHF_COMPRESSED
)
2663 /* now examine each section and try to merge its content with the
2665 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2666 /* no need to examine section name strtab */
2667 if (i
== ehdr
.e_shstrndx
)
2670 if (sh
->sh_type
== SHT_RELX
)
2671 sh
= &shdr
[sh
->sh_info
];
2672 /* ignore sections types we do not handle (plus relocs to those) */
2673 if (sh
->sh_type
!= SHT_PROGBITS
&&
2675 sh
->sh_type
!= SHT_ARM_EXIDX
&&
2677 sh
->sh_type
!= SHT_NOBITS
&&
2678 sh
->sh_type
!= SHT_PREINIT_ARRAY
&&
2679 sh
->sh_type
!= SHT_INIT_ARRAY
&&
2680 sh
->sh_type
!= SHT_FINI_ARRAY
&&
2681 strcmp(strsec
+ sh
->sh_name
, ".stabstr")
2685 && !strncmp(strsec
+ sh
->sh_name
, ".debug_", sizeof(".debug_")-1))
2689 sh_name
= strsec
+ sh
->sh_name
;
2690 if (sh
->sh_addralign
< 1)
2691 sh
->sh_addralign
= 1;
2692 /* find corresponding section, if any */
2693 for(j
= 1; j
< s1
->nb_sections
;j
++) {
2694 s
= s1
->sections
[j
];
2695 if (!strcmp(s
->name
, sh_name
)) {
2696 if (!strncmp(sh_name
, ".gnu.linkonce",
2697 sizeof(".gnu.linkonce") - 1)) {
2698 /* if a 'linkonce' section is already present, we
2699 do not add it again. It is a little tricky as
2700 symbols can still be defined in
2702 sm_table
[i
].link_once
= 1;
2706 if (s
== stab_section
)
2708 if (s
== stab_section
->link
)
2714 /* not found: create new section */
2715 s
= new_section(s1
, sh_name
, sh
->sh_type
, sh
->sh_flags
& ~SHF_GROUP
);
2716 /* take as much info as possible from the section. sh_link and
2717 sh_info will be updated later */
2718 s
->sh_addralign
= sh
->sh_addralign
;
2719 s
->sh_entsize
= sh
->sh_entsize
;
2720 sm_table
[i
].new_section
= 1;
2722 if (sh
->sh_type
!= s
->sh_type
) {
2723 tcc_error_noabort("invalid section type");
2726 /* align start of section */
2727 s
->data_offset
+= -s
->data_offset
& (sh
->sh_addralign
- 1);
2728 if (sh
->sh_addralign
> s
->sh_addralign
)
2729 s
->sh_addralign
= sh
->sh_addralign
;
2730 sm_table
[i
].offset
= s
->data_offset
;
2732 /* concatenate sections */
2734 if (sh
->sh_type
!= SHT_NOBITS
) {
2736 lseek(fd
, file_offset
+ sh
->sh_offset
, SEEK_SET
);
2737 ptr
= section_ptr_add(s
, size
);
2738 full_read(fd
, ptr
, size
);
2740 s
->data_offset
+= size
;
2745 /* gr relocate stab strings */
2746 if (stab_index
&& stabstr_index
) {
2749 s
= sm_table
[stab_index
].s
;
2750 a
= (Stab_Sym
*)(s
->data
+ sm_table
[stab_index
].offset
);
2751 b
= (Stab_Sym
*)(s
->data
+ s
->data_offset
);
2752 o
= sm_table
[stabstr_index
].offset
;
2760 /* second short pass to update sh_link and sh_info fields of new
2762 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2764 if (!s
|| !sm_table
[i
].new_section
)
2767 if (sh
->sh_link
> 0)
2768 s
->link
= sm_table
[sh
->sh_link
].s
;
2769 if (sh
->sh_type
== SHT_RELX
) {
2770 s
->sh_info
= sm_table
[sh
->sh_info
].s
->sh_num
;
2771 /* update backward link */
2772 s1
->sections
[s
->sh_info
]->reloc
= s
;
2776 /* resolve symbols */
2777 old_to_new_syms
= tcc_mallocz(nb_syms
* sizeof(int));
2780 for(i
= 1; i
< nb_syms
; i
++, sym
++) {
2781 if (sym
->st_shndx
!= SHN_UNDEF
&&
2782 sym
->st_shndx
< SHN_LORESERVE
) {
2783 sm
= &sm_table
[sym
->st_shndx
];
2784 if (sm
->link_once
) {
2785 /* if a symbol is in a link once section, we use the
2786 already defined symbol. It is very important to get
2787 correct relocations */
2788 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
2789 name
= strtab
+ sym
->st_name
;
2790 sym_index
= find_elf_sym(symtab_section
, name
);
2792 old_to_new_syms
[i
] = sym_index
;
2796 /* if no corresponding section added, no need to add symbol */
2799 /* convert section number */
2800 sym
->st_shndx
= sm
->s
->sh_num
;
2802 sym
->st_value
+= sm
->offset
;
2805 name
= strtab
+ sym
->st_name
;
2806 sym_index
= set_elf_sym(symtab_section
, sym
->st_value
, sym
->st_size
,
2807 sym
->st_info
, sym
->st_other
,
2808 sym
->st_shndx
, name
);
2809 old_to_new_syms
[i
] = sym_index
;
2812 /* third pass to patch relocation entries */
2813 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2818 offset
= sm_table
[i
].offset
;
2819 switch(s
->sh_type
) {
2821 /* take relocation offset information */
2822 offseti
= sm_table
[sh
->sh_info
].offset
;
2823 for_each_elem(s
, (offset
/ sizeof(*rel
)), rel
, ElfW_Rel
) {
2826 /* convert symbol index */
2827 type
= ELFW(R_TYPE
)(rel
->r_info
);
2828 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
2829 /* NOTE: only one symtab assumed */
2830 if (sym_index
>= nb_syms
)
2832 sym_index
= old_to_new_syms
[sym_index
];
2833 /* ignore link_once in rel section. */
2834 if (!sym_index
&& !sm_table
[sh
->sh_info
].link_once
2835 #ifdef TCC_TARGET_ARM
2836 && type
!= R_ARM_V4BX
2837 #elif defined TCC_TARGET_RISCV64
2838 && type
!= R_RISCV_ALIGN
2839 && type
!= R_RISCV_RELAX
2843 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2844 i
, strsec
+ sh
->sh_name
, (int)rel
->r_offset
);
2847 rel
->r_info
= ELFW(R_INFO
)(sym_index
, type
);
2848 /* offset the relocation offset */
2849 rel
->r_offset
+= offseti
;
2850 #ifdef TCC_TARGET_ARM
2851 /* Jumps and branches from a Thumb code to a PLT entry need
2852 special handling since PLT entries are ARM code.
2853 Unconditional bl instructions referencing PLT entries are
2854 handled by converting these instructions into blx
2855 instructions. Other case of instructions referencing a PLT
2856 entry require to add a Thumb stub before the PLT entry to
2857 switch to ARM mode. We set bit plt_thumb_stub of the
2858 attribute of a symbol to indicate such a case. */
2859 if (type
== R_ARM_THM_JUMP24
)
2860 get_sym_attr(s1
, sym_index
, 1)->plt_thumb_stub
= 1;
2873 tcc_free(old_to_new_syms
);
2880 typedef struct ArchiveHeader
{
2881 char ar_name
[16]; /* name of this member */
2882 char ar_date
[12]; /* file mtime */
2883 char ar_uid
[6]; /* owner uid; printed as decimal */
2884 char ar_gid
[6]; /* owner gid; printed as decimal */
2885 char ar_mode
[8]; /* file mode, printed as octal */
2886 char ar_size
[10]; /* file size, printed as decimal */
2887 char ar_fmag
[2]; /* should contain ARFMAG */
2890 #define ARFMAG "`\n"
2892 static unsigned long long get_be(const uint8_t *b
, int n
)
2894 unsigned long long ret
= 0;
2896 ret
= (ret
<< 8) | *b
++, --n
;
2900 static int read_ar_header(int fd
, int offset
, ArchiveHeader
*hdr
)
2904 lseek(fd
, offset
, SEEK_SET
);
2905 len
= full_read(fd
, hdr
, sizeof(ArchiveHeader
));
2906 if (len
!= sizeof(ArchiveHeader
))
2907 return len
? -1 : 0;
2909 for (e
= p
+ sizeof hdr
->ar_name
; e
> p
&& e
[-1] == ' ';)
2912 hdr
->ar_size
[sizeof hdr
->ar_size
-1] = 0;
2916 /* load only the objects which resolve undefined symbols */
2917 static int tcc_load_alacarte(TCCState
*s1
, int fd
, int size
, int entrysize
)
2919 int i
, bound
, nsyms
, sym_index
, len
, ret
= -1;
2920 unsigned long long off
;
2922 const char *ar_names
, *p
;
2923 const uint8_t *ar_index
;
2927 data
= tcc_malloc(size
);
2928 if (full_read(fd
, data
, size
) != size
)
2930 nsyms
= get_be(data
, entrysize
);
2931 ar_index
= data
+ entrysize
;
2932 ar_names
= (char *) ar_index
+ nsyms
* entrysize
;
2936 for (p
= ar_names
, i
= 0; i
< nsyms
; i
++, p
+= strlen(p
)+1) {
2937 Section
*s
= symtab_section
;
2938 sym_index
= find_elf_sym(s
, p
);
2941 sym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
2942 if(sym
->st_shndx
!= SHN_UNDEF
)
2944 off
= get_be(ar_index
+ i
* entrysize
, entrysize
);
2945 len
= read_ar_header(fd
, off
, &hdr
);
2946 if (len
<= 0 || memcmp(hdr
.ar_fmag
, ARFMAG
, 2)) {
2947 tcc_error_noabort("invalid archive");
2951 if (s1
->verbose
== 2)
2952 printf(" -> %s\n", hdr
.ar_name
);
2953 if (tcc_load_object_file(s1
, fd
, off
) < 0)
2964 /* load a '.a' file */
2965 ST_FUNC
int tcc_load_archive(TCCState
*s1
, int fd
, int alacarte
)
2968 /* char magic[8]; */
2970 unsigned long file_offset
;
2973 /* skip magic which was already checked */
2974 /* full_read(fd, magic, sizeof(magic)); */
2975 file_offset
= sizeof ARMAG
- 1;
2978 len
= read_ar_header(fd
, file_offset
, &hdr
);
2982 tcc_error_noabort("invalid archive");
2986 size
= strtol(hdr
.ar_size
, NULL
, 0);
2988 size
= (size
+ 1) & ~1;
2990 /* coff symbol table : we handle it */
2991 if (!strcmp(hdr
.ar_name
, "/"))
2992 return tcc_load_alacarte(s1
, fd
, size
, 4);
2993 if (!strcmp(hdr
.ar_name
, "/SYM64/"))
2994 return tcc_load_alacarte(s1
, fd
, size
, 8);
2995 } else if (tcc_object_type(fd
, &ehdr
) == AFF_BINTYPE_REL
) {
2996 if (s1
->verbose
== 2)
2997 printf(" -> %s\n", hdr
.ar_name
);
2998 if (tcc_load_object_file(s1
, fd
, file_offset
) < 0)
3001 file_offset
+= size
;
3005 #ifndef ELF_OBJ_ONLY
3006 /* Set LV[I] to the global index of sym-version (LIB,VERSION). Maybe resizes
3007 LV, maybe create a new entry for (LIB,VERSION). */
3008 static void set_ver_to_ver(TCCState
*s1
, int *n
, int **lv
, int i
, char *lib
, char *version
)
3011 *lv
= tcc_realloc(*lv
, (*n
+ 1) * sizeof(**lv
));
3014 if ((*lv
)[i
] == -1) {
3015 int v
, prev_same_lib
= -1;
3016 for (v
= 0; v
< nb_sym_versions
; v
++) {
3017 if (strcmp(sym_versions
[v
].lib
, lib
))
3020 if (!strcmp(sym_versions
[v
].version
, version
))
3023 if (v
== nb_sym_versions
) {
3024 sym_versions
= tcc_realloc (sym_versions
,
3025 (v
+ 1) * sizeof(*sym_versions
));
3026 sym_versions
[v
].lib
= tcc_strdup(lib
);
3027 sym_versions
[v
].version
= tcc_strdup(version
);
3028 sym_versions
[v
].out_index
= 0;
3029 sym_versions
[v
].prev_same_lib
= prev_same_lib
;
3036 /* Associates symbol SYM_INDEX (in dynsymtab) with sym-version index
3039 set_sym_version(TCCState
*s1
, int sym_index
, int verndx
)
3041 if (sym_index
>= nb_sym_to_version
) {
3042 int newelems
= sym_index
? sym_index
* 2 : 1;
3043 sym_to_version
= tcc_realloc(sym_to_version
,
3044 newelems
* sizeof(*sym_to_version
));
3045 memset(sym_to_version
+ nb_sym_to_version
, -1,
3046 (newelems
- nb_sym_to_version
) * sizeof(*sym_to_version
));
3047 nb_sym_to_version
= newelems
;
3049 if (sym_to_version
[sym_index
] < 0)
3050 sym_to_version
[sym_index
] = verndx
;
3053 struct versym_info
{
3055 ElfW(Verdef
) *verdef
;
3056 ElfW(Verneed
) *verneed
;
3058 int nb_local_ver
, *local_ver
;
3062 static void store_version(TCCState
*s1
, struct versym_info
*v
, char *dynstr
)
3064 char *lib
, *version
;
3068 #define DEBUG_VERSION 0
3070 if (v
->versym
&& v
->verdef
) {
3071 ElfW(Verdef
) *vdef
= v
->verdef
;
3074 ElfW(Verdaux
) *verdaux
=
3075 (ElfW(Verdaux
) *) (((char *) vdef
) + vdef
->vd_aux
);
3078 printf ("verdef: version:%u flags:%u index:%u, hash:%u\n",
3079 vdef
->vd_version
, vdef
->vd_flags
, vdef
->vd_ndx
,
3083 version
= dynstr
+ verdaux
->vda_name
;
3088 set_ver_to_ver(s1
, &v
->nb_local_ver
, &v
->local_ver
, vdef
->vd_ndx
,
3091 printf (" verdaux(%u): %s\n", vdef
->vd_ndx
, version
);
3094 next
= vdef
->vd_next
;
3095 vdef
= (ElfW(Verdef
) *) (((char *) vdef
) + next
);
3098 if (v
->versym
&& v
->verneed
) {
3099 ElfW(Verneed
) *vneed
= v
->verneed
;
3101 ElfW(Vernaux
) *vernaux
=
3102 (ElfW(Vernaux
) *) (((char *) vneed
) + vneed
->vn_aux
);
3104 lib
= dynstr
+ vneed
->vn_file
;
3106 printf ("verneed: %u %s\n", vneed
->vn_version
, lib
);
3108 for (i
= 0; i
< vneed
->vn_cnt
; i
++) {
3109 if ((vernaux
->vna_other
& 0x8000) == 0) { /* hidden */
3110 version
= dynstr
+ vernaux
->vna_name
;
3111 set_ver_to_ver(s1
, &v
->nb_local_ver
, &v
->local_ver
, vernaux
->vna_other
,
3114 printf (" vernaux(%u): %u %u %s\n",
3115 vernaux
->vna_other
, vernaux
->vna_hash
,
3116 vernaux
->vna_flags
, version
);
3119 vernaux
= (ElfW(Vernaux
) *) (((char *) vernaux
) + vernaux
->vna_next
);
3121 next
= vneed
->vn_next
;
3122 vneed
= (ElfW(Verneed
) *) (((char *) vneed
) + next
);
3127 for (i
= 0; i
< v
->nb_local_ver
; i
++) {
3128 if (v
->local_ver
[i
] > 0) {
3129 printf ("%d: lib: %s, version %s\n",
3130 i
, sym_versions
[v
->local_ver
[i
]].lib
,
3131 sym_versions
[v
->local_ver
[i
]].version
);
3137 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
3138 is referenced by the user (so it should be added as DT_NEEDED in
3139 the generated ELF file) */
3140 ST_FUNC
int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
)
3143 ElfW(Shdr
) *shdr
, *sh
, *sh1
;
3144 int i
, j
, nb_syms
, nb_dts
, sym_bind
, ret
;
3145 ElfW(Sym
) *sym
, *dynsym
;
3146 ElfW(Dyn
) *dt
, *dynamic
;
3150 const char *name
, *soname
;
3151 DLLReference
*dllref
;
3152 struct versym_info v
;
3154 full_read(fd
, &ehdr
, sizeof(ehdr
));
3156 /* test CPU specific stuff */
3157 if (ehdr
.e_ident
[5] != ELFDATA2LSB
||
3158 ehdr
.e_machine
!= EM_TCC_TARGET
) {
3159 tcc_error_noabort("bad architecture");
3164 shdr
= load_data(fd
, ehdr
.e_shoff
, sizeof(ElfW(Shdr
)) * ehdr
.e_shnum
);
3166 /* load dynamic section and dynamic symbols */
3170 dynsym
= NULL
; /* avoid warning */
3171 dynstr
= NULL
; /* avoid warning */
3172 memset(&v
, 0, sizeof v
);
3174 for(i
= 0, sh
= shdr
; i
< ehdr
.e_shnum
; i
++, sh
++) {
3175 switch(sh
->sh_type
) {
3177 nb_dts
= sh
->sh_size
/ sizeof(ElfW(Dyn
));
3178 dynamic
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3181 nb_syms
= sh
->sh_size
/ sizeof(ElfW(Sym
));
3182 dynsym
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3183 sh1
= &shdr
[sh
->sh_link
];
3184 dynstr
= load_data(fd
, sh1
->sh_offset
, sh1
->sh_size
);
3186 case SHT_GNU_verdef
:
3187 v
.verdef
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3189 case SHT_GNU_verneed
:
3190 v
.verneed
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3192 case SHT_GNU_versym
:
3193 v
.nb_versyms
= sh
->sh_size
/ sizeof(ElfW(Half
));
3194 v
.versym
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
3201 /* compute the real library name */
3202 soname
= tcc_basename(filename
);
3204 for(i
= 0, dt
= dynamic
; i
< nb_dts
; i
++, dt
++) {
3205 if (dt
->d_tag
== DT_SONAME
) {
3206 soname
= dynstr
+ dt
->d_un
.d_val
;
3210 /* if the dll is already loaded, do not load it */
3211 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
3212 dllref
= s1
->loaded_dlls
[i
];
3213 if (!strcmp(soname
, dllref
->name
)) {
3214 /* but update level if needed */
3215 if (level
< dllref
->level
)
3216 dllref
->level
= level
;
3222 if (v
.nb_versyms
!= nb_syms
)
3223 tcc_free (v
.versym
), v
.versym
= NULL
;
3225 store_version(s1
, &v
, dynstr
);
3227 /* add the dll and its level */
3228 dllref
= tcc_mallocz(sizeof(DLLReference
) + strlen(soname
));
3229 dllref
->level
= level
;
3230 strcpy(dllref
->name
, soname
);
3231 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, dllref
);
3233 /* add dynamic symbols in dynsym_section */
3234 for(i
= 1, sym
= dynsym
+ 1; i
< nb_syms
; i
++, sym
++) {
3235 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
3236 if (sym_bind
== STB_LOCAL
)
3238 name
= dynstr
+ sym
->st_name
;
3239 sym_index
= set_elf_sym(s1
->dynsymtab_section
, sym
->st_value
, sym
->st_size
,
3240 sym
->st_info
, sym
->st_other
, sym
->st_shndx
, name
);
3242 ElfW(Half
) vsym
= v
.versym
[i
];
3243 if ((vsym
& 0x8000) == 0 && vsym
> 0 && vsym
< v
.nb_local_ver
)
3244 set_sym_version(s1
, sym_index
, v
.local_ver
[vsym
]);
3248 /* load all referenced DLLs */
3249 for(i
= 0, dt
= dynamic
; i
< nb_dts
; i
++, dt
++) {
3252 name
= dynstr
+ dt
->d_un
.d_val
;
3253 for(j
= 0; j
< s1
->nb_loaded_dlls
; j
++) {
3254 dllref
= s1
->loaded_dlls
[j
];
3255 if (!strcmp(name
, dllref
->name
))
3256 goto already_loaded
;
3258 if (tcc_add_dll(s1
, name
, AFF_REFERENCED_DLL
) < 0) {
3259 tcc_error_noabort("referenced dll '%s' not found", name
);
3273 tcc_free(v
.local_ver
);
3275 tcc_free(v
.verneed
);
3280 #define LD_TOK_NAME 256
3281 #define LD_TOK_EOF (-1)
3283 static int ld_inp(TCCState
*s1
)
3291 if (1 == read(s1
->fd
, &b
, 1))
3296 /* return next ld script token */
3297 static int ld_next(TCCState
*s1
, char *name
, int name_size
)
3314 if (ch
== '*') { /* comment */
3315 for (d
= 0;; d
= ch
) {
3317 if (ch
== CH_EOF
|| (ch
== '/' && d
== '*'))
3328 /* case 'a' ... 'z': */
3355 /* case 'A' ... 'z': */
3389 if (!((ch
>= 'a' && ch
<= 'z') ||
3390 (ch
>= 'A' && ch
<= 'Z') ||
3391 (ch
>= '0' && ch
<= '9') ||
3392 strchr("/.-_+=$:\\,~", ch
)))
3394 if ((q
- name
) < name_size
- 1) {
3413 static int ld_add_file(TCCState
*s1
, const char filename
[])
3415 if (filename
[0] == '/') {
3416 if (CONFIG_SYSROOT
[0] == '\0'
3417 && tcc_add_file_internal(s1
, filename
, AFF_TYPE_BIN
) == 0)
3419 filename
= tcc_basename(filename
);
3421 return tcc_add_dll(s1
, filename
, 0);
3424 static int ld_add_file_list(TCCState
*s1
, const char *cmd
, int as_needed
)
3426 char filename
[1024], libname
[1024];
3427 int t
, group
, nblibs
= 0, ret
= 0;
3430 group
= !strcmp(cmd
, "GROUP");
3432 s1
->new_undef_sym
= 0;
3433 t
= ld_next(s1
, filename
, sizeof(filename
));
3435 tcc_error_noabort("( expected");
3437 goto lib_parse_error
;
3439 t
= ld_next(s1
, filename
, sizeof(filename
));
3442 if (t
== LD_TOK_EOF
) {
3443 tcc_error_noabort("unexpected end of file");
3445 goto lib_parse_error
;
3446 } else if (t
== ')') {
3448 } else if (t
== '-') {
3449 t
= ld_next(s1
, filename
, sizeof(filename
));
3450 if ((t
!= LD_TOK_NAME
) || (filename
[0] != 'l')) {
3451 tcc_error_noabort("library name expected");
3453 goto lib_parse_error
;
3455 pstrcpy(libname
, sizeof libname
, &filename
[1]);
3456 if (s1
->static_link
) {
3457 snprintf(filename
, sizeof filename
, "lib%s.a", libname
);
3459 snprintf(filename
, sizeof filename
, "lib%s.so", libname
);
3461 } else if (t
!= LD_TOK_NAME
) {
3462 tcc_error_noabort("filename expected");
3464 goto lib_parse_error
;
3466 if (!strcmp(filename
, "AS_NEEDED")) {
3467 ret
= ld_add_file_list(s1
, cmd
, 1);
3469 goto lib_parse_error
;
3471 /* TODO: Implement AS_NEEDED support. Ignore it for now */
3473 ret
= ld_add_file(s1
, filename
);
3475 goto lib_parse_error
;
3477 /* Add the filename *and* the libname to avoid future conversions */
3478 dynarray_add(&libs
, &nblibs
, tcc_strdup(filename
));
3479 if (libname
[0] != '\0')
3480 dynarray_add(&libs
, &nblibs
, tcc_strdup(libname
));
3484 t
= ld_next(s1
, filename
, sizeof(filename
));
3486 t
= ld_next(s1
, filename
, sizeof(filename
));
3489 if (group
&& !as_needed
) {
3490 while (s1
->new_undef_sym
) {
3492 s1
->new_undef_sym
= 0;
3493 for (i
= 0; i
< nblibs
; i
++)
3494 ld_add_file(s1
, libs
[i
]);
3498 dynarray_reset(&libs
, &nblibs
);
3502 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
3504 ST_FUNC
int tcc_load_ldscript(TCCState
*s1
, int fd
)
3507 char filename
[1024];
3513 t
= ld_next(s1
, cmd
, sizeof(cmd
));
3514 if (t
== LD_TOK_EOF
)
3516 else if (t
!= LD_TOK_NAME
)
3518 if (!strcmp(cmd
, "INPUT") ||
3519 !strcmp(cmd
, "GROUP")) {
3520 ret
= ld_add_file_list(s1
, cmd
, 0);
3523 } else if (!strcmp(cmd
, "OUTPUT_FORMAT") ||
3524 !strcmp(cmd
, "TARGET")) {
3525 /* ignore some commands */
3526 t
= ld_next(s1
, cmd
, sizeof(cmd
));
3528 tcc_error_noabort("( expected");
3532 t
= ld_next(s1
, filename
, sizeof(filename
));
3533 if (t
== LD_TOK_EOF
) {
3534 tcc_error_noabort("unexpected end of file");
3536 } else if (t
== ')') {
3546 #endif /* !ELF_OBJ_ONLY */