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 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
30 ST_DATA Section
*common_section
;
31 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
33 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
35 #ifdef CONFIG_TCC_BCHECK
36 /* bound check related sections */
37 ST_DATA Section
*bounds_section
; /* contains global data bound description */
38 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
41 ST_DATA Section
*symtab_section
, *strtab_section
;
43 ST_DATA Section
*stab_section
, *stabstr_section
;
45 /* XXX: avoid static variable */
46 static int new_undef_sym
= 0; /* Is there a new undefined sym since last new_undef_sym() */
48 /* ------------------------------------------------------------------------- */
50 ST_FUNC
void tccelf_new(TCCState
*s
)
53 dynarray_add(&s
->sections
, &s
->nb_sections
, NULL
);
55 /* create standard sections */
56 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
57 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
58 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
59 common_section
= new_section(s
, ".common", SHT_NOBITS
, SHF_PRIVATE
);
60 common_section
->sh_num
= SHN_COMMON
;
62 /* symbols are always generated for linking stage */
63 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
65 ".hashtab", SHF_PRIVATE
);
66 strtab_section
= symtab_section
->link
;
67 s
->symtab
= symtab_section
;
69 /* private symbol table for dynamic symbols */
70 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
72 ".dynhashtab", SHF_PRIVATE
);
73 get_sym_attr(s
, 0, 1);
76 #ifdef CONFIG_TCC_BCHECK
77 ST_FUNC
void tccelf_bounds_new(TCCState
*s
)
79 /* create bounds sections */
80 bounds_section
= new_section(s
, ".bounds",
81 SHT_PROGBITS
, SHF_ALLOC
);
82 lbounds_section
= new_section(s
, ".lbounds",
83 SHT_PROGBITS
, SHF_ALLOC
);
87 ST_FUNC
void tccelf_stab_new(TCCState
*s
)
89 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
90 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
91 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
92 put_elf_str(stabstr_section
, "");
93 stab_section
->link
= stabstr_section
;
95 put_stabs("", 0, 0, 0, 0);
98 static void free_section(Section
*s
)
103 ST_FUNC
void tccelf_delete(TCCState
*s1
)
107 /* free all sections */
108 for(i
= 1; i
< s1
->nb_sections
; i
++)
109 free_section(s1
->sections
[i
]);
110 dynarray_reset(&s1
->sections
, &s1
->nb_sections
);
112 for(i
= 0; i
< s1
->nb_priv_sections
; i
++)
113 free_section(s1
->priv_sections
[i
]);
114 dynarray_reset(&s1
->priv_sections
, &s1
->nb_priv_sections
);
116 /* free any loaded DLLs */
118 for ( i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
119 DLLReference
*ref
= s1
->loaded_dlls
[i
];
122 FreeLibrary((HMODULE
)ref
->handle
);
124 dlclose(ref
->handle
);
128 /* free loaded dlls array */
129 dynarray_reset(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
);
130 tcc_free(s1
->sym_attrs
);
133 ST_FUNC Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
137 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
138 strcpy(sec
->name
, name
);
139 sec
->sh_type
= sh_type
;
140 sec
->sh_flags
= sh_flags
;
148 sec
->sh_addralign
= 4;
151 sec
->sh_addralign
= 1;
154 sec
->sh_addralign
= PTR_SIZE
; /* gcc/pcc default alignment */
158 if (sh_flags
& SHF_PRIVATE
) {
159 dynarray_add(&s1
->priv_sections
, &s1
->nb_priv_sections
, sec
);
161 sec
->sh_num
= s1
->nb_sections
;
162 dynarray_add(&s1
->sections
, &s1
->nb_sections
, sec
);
168 ST_FUNC Section
*new_symtab(TCCState
*s1
,
169 const char *symtab_name
, int sh_type
, int sh_flags
,
170 const char *strtab_name
,
171 const char *hash_name
, int hash_sh_flags
)
173 Section
*symtab
, *strtab
, *hash
;
174 int *ptr
, nb_buckets
;
176 symtab
= new_section(s1
, symtab_name
, sh_type
, sh_flags
);
177 symtab
->sh_entsize
= sizeof(ElfW(Sym
));
178 strtab
= new_section(s1
, strtab_name
, SHT_STRTAB
, sh_flags
);
179 put_elf_str(strtab
, "");
180 symtab
->link
= strtab
;
181 put_elf_sym(symtab
, 0, 0, 0, 0, 0, NULL
);
185 hash
= new_section(s1
, hash_name
, SHT_HASH
, hash_sh_flags
);
186 hash
->sh_entsize
= sizeof(int);
190 ptr
= section_ptr_add(hash
, (2 + nb_buckets
+ 1) * sizeof(int));
193 memset(ptr
+ 2, 0, (nb_buckets
+ 1) * sizeof(int));
197 /* realloc section and set its content to zero */
198 ST_FUNC
void section_realloc(Section
*sec
, unsigned long new_size
)
203 size
= sec
->data_allocated
;
206 while (size
< new_size
)
208 data
= tcc_realloc(sec
->data
, size
);
209 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
211 sec
->data_allocated
= size
;
214 /* reserve at least 'size' bytes aligned per 'align' in section
215 'sec' from current offset, and return the aligned offset */
216 ST_FUNC
size_t section_add(Section
*sec
, addr_t size
, int align
)
218 size_t offset
, offset1
;
220 offset
= (sec
->data_offset
+ align
- 1) & -align
;
221 offset1
= offset
+ size
;
222 if (sec
->sh_type
!= SHT_NOBITS
&& offset1
> sec
->data_allocated
)
223 section_realloc(sec
, offset1
);
224 sec
->data_offset
= offset1
;
225 if (align
> sec
->sh_addralign
)
226 sec
->sh_addralign
= align
;
230 /* reserve at least 'size' bytes in section 'sec' from
232 ST_FUNC
void *section_ptr_add(Section
*sec
, addr_t size
)
234 size_t offset
= section_add(sec
, size
, 1);
235 return sec
->data
+ offset
;
238 /* reserve at least 'size' bytes from section start */
239 ST_FUNC
void section_reserve(Section
*sec
, unsigned long size
)
241 if (size
> sec
->data_allocated
)
242 section_realloc(sec
, size
);
243 if (size
> sec
->data_offset
)
244 sec
->data_offset
= size
;
247 /* return a reference to a section, and create it if it does not
249 ST_FUNC Section
*find_section(TCCState
*s1
, const char *name
)
253 for(i
= 1; i
< s1
->nb_sections
; i
++) {
254 sec
= s1
->sections
[i
];
255 if (!strcmp(name
, sec
->name
))
258 /* sections are created as PROGBITS */
259 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
262 /* ------------------------------------------------------------------------- */
264 ST_FUNC
int put_elf_str(Section
*s
, const char *sym
)
269 len
= strlen(sym
) + 1;
270 offset
= s
->data_offset
;
271 ptr
= section_ptr_add(s
, len
);
272 memcpy(ptr
, sym
, len
);
276 /* elf symbol hashing function */
277 static unsigned long elf_hash(const unsigned char *name
)
279 unsigned long h
= 0, g
;
282 h
= (h
<< 4) + *name
++;
291 /* rebuild hash table of section s */
292 /* NOTE: we do factorize the hash table code to go faster */
293 static void rebuild_hash(Section
*s
, unsigned int nb_buckets
)
296 int *ptr
, *hash
, nb_syms
, sym_index
, h
;
297 unsigned char *strtab
;
299 strtab
= s
->link
->data
;
300 nb_syms
= s
->data_offset
/ sizeof(ElfW(Sym
));
302 s
->hash
->data_offset
= 0;
303 ptr
= section_ptr_add(s
->hash
, (2 + nb_buckets
+ nb_syms
) * sizeof(int));
308 memset(hash
, 0, (nb_buckets
+ 1) * sizeof(int));
309 ptr
+= nb_buckets
+ 1;
311 sym
= (ElfW(Sym
) *)s
->data
+ 1;
312 for(sym_index
= 1; sym_index
< nb_syms
; sym_index
++) {
313 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
314 || sym
->st_shndx
== SHN_UNDEF
) {
315 h
= elf_hash(strtab
+ sym
->st_name
) % nb_buckets
;
326 /* return the symbol number */
327 ST_FUNC
int put_elf_sym(Section
*s
, addr_t value
, unsigned long size
,
328 int info
, int other
, int shndx
, const char *name
)
330 int name_offset
, sym_index
;
335 sym
= section_ptr_add(s
, sizeof(ElfW(Sym
)));
337 name_offset
= put_elf_str(s
->link
, name
);
340 /* XXX: endianness */
341 sym
->st_name
= name_offset
;
342 sym
->st_value
= value
;
345 sym
->st_other
= other
;
346 sym
->st_shndx
= shndx
;
347 sym_index
= sym
- (ElfW(Sym
) *)s
->data
;
351 ptr
= section_ptr_add(hs
, sizeof(int));
352 base
= (int *)hs
->data
;
353 /* only add global, weak or undef symbols. The latter might
354 become global late (from asm references). */
355 if (ELFW(ST_BIND
)(info
) != STB_LOCAL
|| shndx
== SHN_UNDEF
) {
356 /* add another hashing entry */
358 h
= elf_hash((unsigned char *) name
) % nbuckets
;
360 base
[2 + h
] = sym_index
;
362 /* we resize the hash table */
363 hs
->nb_hashed_syms
++;
364 if (hs
->nb_hashed_syms
> 2 * nbuckets
) {
365 rebuild_hash(s
, 2 * nbuckets
);
375 /* find global ELF symbol 'name' and return its index. Return 0 if not
377 ST_FUNC
int find_elf_sym(Section
*s
, const char *name
)
381 int nbuckets
, sym_index
, h
;
387 nbuckets
= ((int *)hs
->data
)[0];
388 h
= elf_hash((unsigned char *) name
) % nbuckets
;
389 sym_index
= ((int *)hs
->data
)[2 + h
];
390 while (sym_index
!= 0) {
391 sym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
392 name1
= (char *) s
->link
->data
+ sym
->st_name
;
393 if (!strcmp(name
, name1
))
395 sym_index
= ((int *)hs
->data
)[2 + nbuckets
+ sym_index
];
400 /* return elf symbol value, signal error if 'err' is nonzero */
401 ST_FUNC addr_t
get_elf_sym_addr(TCCState
*s
, const char *name
, int err
)
406 sym_index
= find_elf_sym(s
->symtab
, name
);
407 sym
= &((ElfW(Sym
) *)s
->symtab
->data
)[sym_index
];
408 if (!sym_index
|| sym
->st_shndx
== SHN_UNDEF
) {
410 tcc_error("%s not defined", name
);
413 return sym
->st_value
;
416 /* return elf symbol value */
417 LIBTCCAPI
void *tcc_get_symbol(TCCState
*s
, const char *name
)
419 return (void*)(uintptr_t)get_elf_sym_addr(s
, name
, 0);
422 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
423 /* return elf symbol value or error */
424 ST_FUNC
void* tcc_get_symbol_err(TCCState
*s
, const char *name
)
426 return (void*)(uintptr_t)get_elf_sym_addr(s
, name
, 1);
430 /* add an elf symbol : check if it is already defined and patch
431 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
432 ST_FUNC
int set_elf_sym(Section
*s
, addr_t value
, unsigned long size
,
433 int info
, int other
, int shndx
, const char *name
)
436 int sym_bind
, sym_index
, sym_type
, esym_bind
;
437 unsigned char sym_vis
, esym_vis
, new_vis
;
439 sym_bind
= ELFW(ST_BIND
)(info
);
440 sym_type
= ELFW(ST_TYPE
)(info
);
441 sym_vis
= ELFW(ST_VISIBILITY
)(other
);
443 sym_index
= find_elf_sym(s
, name
);
444 esym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
445 if (sym_index
&& esym
->st_value
== value
&& esym
->st_size
== size
446 && esym
->st_info
== info
&& esym
->st_other
== other
447 && esym
->st_shndx
== shndx
)
450 if (sym_bind
!= STB_LOCAL
) {
451 /* we search global or weak symbols */
454 if (esym
->st_shndx
!= SHN_UNDEF
) {
455 esym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
456 /* propagate the most constraining visibility */
457 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
458 esym_vis
= ELFW(ST_VISIBILITY
)(esym
->st_other
);
459 if (esym_vis
== STV_DEFAULT
) {
461 } else if (sym_vis
== STV_DEFAULT
) {
464 new_vis
= (esym_vis
< sym_vis
) ? esym_vis
: sym_vis
;
466 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
468 other
= esym
->st_other
; /* in case we have to patch esym */
469 if (shndx
== SHN_UNDEF
) {
470 /* ignore adding of undefined symbol if the
471 corresponding symbol is already defined */
472 } else if (sym_bind
== STB_GLOBAL
&& esym_bind
== STB_WEAK
) {
473 /* global overrides weak, so patch */
475 } else if (sym_bind
== STB_WEAK
&& esym_bind
== STB_GLOBAL
) {
476 /* weak is ignored if already global */
477 } else if (sym_bind
== STB_WEAK
&& esym_bind
== STB_WEAK
) {
478 /* keep first-found weak definition, ignore subsequents */
479 } else if (sym_vis
== STV_HIDDEN
|| sym_vis
== STV_INTERNAL
) {
480 /* ignore hidden symbols after */
481 } else if ((esym
->st_shndx
== SHN_COMMON
482 || esym
->st_shndx
== bss_section
->sh_num
)
483 && (shndx
< SHN_LORESERVE
484 && shndx
!= bss_section
->sh_num
)) {
485 /* data symbol gets precedence over common/bss */
487 } else if (shndx
== SHN_COMMON
|| shndx
== bss_section
->sh_num
) {
488 /* data symbol keeps precedence over common/bss */
489 } else if (s
== tcc_state
->dynsymtab_section
) {
490 /* we accept that two DLL define the same symbol */
491 } else if (esym
->st_other
& ST_ASM_SET
) {
492 /* If the existing symbol came from an asm .set
497 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
498 sym_bind
, shndx
, new_vis
, esym_bind
, esym
->st_shndx
, esym_vis
);
500 tcc_error_noabort("'%s' defined twice", name
);
504 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
505 esym
->st_shndx
= shndx
;
507 esym
->st_value
= value
;
508 esym
->st_size
= size
;
509 esym
->st_other
= other
;
513 sym_index
= put_elf_sym(s
, value
, size
,
514 ELFW(ST_INFO
)(sym_bind
, sym_type
), other
,
521 ST_FUNC
void put_elf_reloca(Section
*symtab
, Section
*s
, unsigned long offset
,
522 int type
, int symbol
, addr_t addend
)
530 /* if no relocation section, create it */
531 snprintf(buf
, sizeof(buf
), REL_SECTION_FMT
, s
->name
);
532 /* if the symtab is allocated, then we consider the relocation
534 sr
= new_section(tcc_state
, buf
, SHT_RELX
, symtab
->sh_flags
);
535 sr
->sh_entsize
= sizeof(ElfW_Rel
);
537 sr
->sh_info
= s
->sh_num
;
540 rel
= section_ptr_add(sr
, sizeof(ElfW_Rel
));
541 rel
->r_offset
= offset
;
542 rel
->r_info
= ELFW(R_INFO
)(symbol
, type
);
543 #if SHT_RELX == SHT_RELA
544 rel
->r_addend
= addend
;
547 tcc_error("non-zero addend on REL architecture");
551 ST_FUNC
void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
552 int type
, int symbol
)
554 put_elf_reloca(symtab
, s
, offset
, type
, symbol
, 0);
557 /* Remove relocations for section S->reloc starting at oldrelocoffset
558 that are to the same place, retaining the last of them. As side effect
559 the relocations are sorted. Possibly reduces the number of relocs. */
560 ST_FUNC
void squeeze_multi_relocs(Section
*s
, size_t oldrelocoffset
)
562 Section
*sr
= s
->reloc
;
567 if (oldrelocoffset
+ sizeof(*r
) >= sr
->data_offset
)
569 /* The relocs we're dealing with are the result of initializer parsing.
570 So they will be mostly in order and there aren't many of them.
571 Secondly we need a stable sort (which qsort isn't). We use
572 a simple insertion sort. */
573 for (a
= oldrelocoffset
+ sizeof(*r
); a
< sr
->data_offset
; a
+= sizeof(*r
)) {
574 ssize_t i
= a
- sizeof(*r
);
575 addr
= ((ElfW_Rel
*)(sr
->data
+ a
))->r_offset
;
576 for (; i
>= (ssize_t
)oldrelocoffset
&&
577 ((ElfW_Rel
*)(sr
->data
+ i
))->r_offset
> addr
; i
-= sizeof(*r
)) {
578 ElfW_Rel tmp
= *(ElfW_Rel
*)(sr
->data
+ a
);
579 *(ElfW_Rel
*)(sr
->data
+ a
) = *(ElfW_Rel
*)(sr
->data
+ i
);
580 *(ElfW_Rel
*)(sr
->data
+ i
) = tmp
;
584 r
= (ElfW_Rel
*)(sr
->data
+ oldrelocoffset
);
586 for (; r
< (ElfW_Rel
*)(sr
->data
+ sr
->data_offset
); r
++) {
587 if (dest
->r_offset
!= r
->r_offset
)
591 sr
->data_offset
= (unsigned char*)dest
- sr
->data
+ sizeof(*r
);
594 /* put stab debug information */
596 ST_FUNC
void put_stabs(const char *str
, int type
, int other
, int desc
,
601 sym
= section_ptr_add(stab_section
, sizeof(Stab_Sym
));
603 sym
->n_strx
= put_elf_str(stabstr_section
, str
);
608 sym
->n_other
= other
;
610 sym
->n_value
= value
;
613 ST_FUNC
void put_stabs_r(const char *str
, int type
, int other
, int desc
,
614 unsigned long value
, Section
*sec
, int sym_index
)
616 put_stabs(str
, type
, other
, desc
, value
);
617 put_elf_reloc(symtab_section
, stab_section
,
618 stab_section
->data_offset
- sizeof(unsigned int),
619 R_DATA_32
, sym_index
);
622 ST_FUNC
void put_stabn(int type
, int other
, int desc
, int value
)
624 put_stabs(NULL
, type
, other
, desc
, value
);
627 ST_FUNC
void put_stabd(int type
, int other
, int desc
)
629 put_stabs(NULL
, type
, other
, desc
, 0);
632 ST_FUNC
struct sym_attr
*get_sym_attr(TCCState
*s1
, int index
, int alloc
)
635 struct sym_attr
*tab
;
637 if (index
>= s1
->nb_sym_attrs
) {
639 return s1
->sym_attrs
;
640 /* find immediately bigger power of 2 and reallocate array */
644 tab
= tcc_realloc(s1
->sym_attrs
, n
* sizeof(*s1
->sym_attrs
));
646 memset(s1
->sym_attrs
+ s1
->nb_sym_attrs
, 0,
647 (n
- s1
->nb_sym_attrs
) * sizeof(*s1
->sym_attrs
));
648 s1
->nb_sym_attrs
= n
;
650 return &s1
->sym_attrs
[index
];
653 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
654 using variable <elem> */
655 #define for_each_elem(sec, startoff, elem, type) \
656 for (elem = (type *) sec->data + startoff; \
657 elem < (type *) (sec->data + sec->data_offset); elem++)
659 /* In an ELF file symbol table, the local symbols must appear below
660 the global and weak ones. Since TCC cannot sort it while generating
661 the code, we must do it after. All the relocation tables are also
662 modified to take into account the symbol table sorting */
663 static void sort_syms(TCCState
*s1
, Section
*s
)
665 int *old_to_new_syms
;
673 nb_syms
= s
->data_offset
/ sizeof(ElfW(Sym
));
674 new_syms
= tcc_malloc(nb_syms
* sizeof(ElfW(Sym
)));
675 old_to_new_syms
= tcc_malloc(nb_syms
* sizeof(int));
677 /* first pass for local symbols */
678 p
= (ElfW(Sym
) *)s
->data
;
680 for(i
= 0; i
< nb_syms
; i
++) {
681 if (ELFW(ST_BIND
)(p
->st_info
) == STB_LOCAL
) {
682 old_to_new_syms
[i
] = q
- new_syms
;
687 /* save the number of local symbols in section header */
688 if( s
->sh_size
) /* this 'if' makes IDA happy */
689 s
->sh_info
= q
- new_syms
;
691 /* then second pass for non local symbols */
692 p
= (ElfW(Sym
) *)s
->data
;
693 for(i
= 0; i
< nb_syms
; i
++) {
694 if (ELFW(ST_BIND
)(p
->st_info
) != STB_LOCAL
) {
695 old_to_new_syms
[i
] = q
- new_syms
;
701 /* we copy the new symbols to the old */
702 memcpy(s
->data
, new_syms
, nb_syms
* sizeof(ElfW(Sym
)));
705 /* now we modify all the relocations */
706 for(i
= 1; i
< s1
->nb_sections
; i
++) {
707 sr
= s1
->sections
[i
];
708 if (sr
->sh_type
== SHT_RELX
&& sr
->link
== s
) {
709 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
710 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
711 type
= ELFW(R_TYPE
)(rel
->r_info
);
712 sym_index
= old_to_new_syms
[sym_index
];
713 rel
->r_info
= ELFW(R_INFO
)(sym_index
, type
);
718 tcc_free(old_to_new_syms
);
721 /* relocate common symbols in the .bss section */
722 ST_FUNC
void relocate_common_syms(void)
726 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
727 if (sym
->st_shndx
== SHN_COMMON
) {
728 /* symbol alignment is in st_value for SHN_COMMONs */
729 sym
->st_value
= section_add(bss_section
, sym
->st_size
,
731 sym
->st_shndx
= bss_section
->sh_num
;
736 /* relocate symbol table, resolve undefined symbols if do_resolve is
737 true and output error if undefined symbol. */
738 ST_FUNC
void relocate_syms(TCCState
*s1
, Section
*symtab
, int do_resolve
)
741 int sym_bind
, sh_num
;
744 for_each_elem(symtab
, 1, sym
, ElfW(Sym
)) {
745 sh_num
= sym
->st_shndx
;
746 if (sh_num
== SHN_UNDEF
) {
747 name
= (char *) strtab_section
->data
+ sym
->st_name
;
748 /* Use ld.so to resolve symbol for us (for tcc -run) */
750 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
751 void *addr
= dlsym(RTLD_DEFAULT
, name
);
753 sym
->st_value
= (addr_t
) addr
;
755 printf ("relocate_sym: %s -> 0x%lx\n", name
, sym
->st_value
);
760 /* if dynamic symbol exist, it will be used in relocate_section */
761 } else if (s1
->dynsym
&& find_elf_sym(s1
->dynsym
, name
))
763 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
765 if (!strcmp(name
, "_fp_hw"))
767 /* only weak symbols are accepted to be undefined. Their
769 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
770 if (sym_bind
== STB_WEAK
)
773 tcc_error_noabort("undefined symbol '%s'", name
);
774 } else if (sh_num
< SHN_LORESERVE
) {
775 /* add section base */
776 sym
->st_value
+= s1
->sections
[sym
->st_shndx
]->sh_addr
;
782 /* relocate a given section (CPU dependent) by applying the relocations
783 in the associated relocation section */
784 ST_FUNC
void relocate_section(TCCState
*s1
, Section
*s
)
786 Section
*sr
= s
->reloc
;
795 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
796 ptr
= s
->data
+ rel
->r_offset
;
797 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
798 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
799 type
= ELFW(R_TYPE
)(rel
->r_info
);
801 #if SHT_RELX == SHT_RELA
802 tgt
+= rel
->r_addend
;
804 addr
= s
->sh_addr
+ rel
->r_offset
;
805 relocate(s1
, rel
, type
, ptr
, addr
, tgt
);
807 /* if the relocation is allocated, we change its symbol table */
808 if (sr
->sh_flags
& SHF_ALLOC
)
809 sr
->link
= s1
->dynsym
;
812 /* relocate relocation table in 'sr' */
813 static void relocate_rel(TCCState
*s1
, Section
*sr
)
818 s
= s1
->sections
[sr
->sh_info
];
819 for_each_elem(sr
, 0, rel
, ElfW_Rel
)
820 rel
->r_offset
+= s
->sh_addr
;
823 /* count the number of dynamic relocations so that we can reserve
825 static int prepare_dynamic_rel(TCCState
*s1
, Section
*sr
)
828 int sym_index
, type
, count
;
831 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
832 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
833 type
= ELFW(R_TYPE
)(rel
->r_info
);
835 #if defined(TCC_TARGET_I386)
837 if (!get_sym_attr(s1
, sym_index
, 0)->dyn_index
838 && ((ElfW(Sym
)*)symtab_section
->data
+ sym_index
)->st_shndx
== SHN_UNDEF
) {
839 /* don't fixup unresolved (weak) symbols */
840 rel
->r_info
= ELFW(R_INFO
)(sym_index
, R_386_RELATIVE
);
843 #elif defined(TCC_TARGET_X86_64)
850 #if defined(TCC_TARGET_I386)
852 #elif defined(TCC_TARGET_X86_64)
855 if (get_sym_attr(s1
, sym_index
, 0)->dyn_index
)
863 /* allocate the section */
864 sr
->sh_flags
|= SHF_ALLOC
;
865 sr
->sh_size
= count
* sizeof(ElfW_Rel
);
870 static void build_got(TCCState
*s1
)
872 /* if no got, then create it */
873 s1
->got
= new_section(s1
, ".got", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
874 s1
->got
->sh_entsize
= 4;
875 set_elf_sym(symtab_section
, 0, 4, ELFW(ST_INFO
)(STB_GLOBAL
, STT_OBJECT
),
876 0, s1
->got
->sh_num
, "_GLOBAL_OFFSET_TABLE_");
877 /* keep space for _DYNAMIC pointer and two dummy got entries */
878 section_ptr_add(s1
->got
, 3 * PTR_SIZE
);
881 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
882 in s1->symtab. When creating the dynamic symbol table entry for the GOT
883 relocation, use 'size' and 'info' for the corresponding symbol metadata.
884 Returns the offset of the GOT or (if any) PLT entry. */
885 static struct sym_attr
* put_got_entry(TCCState
*s1
, int dyn_reloc_type
,
887 int info
, int sym_index
)
892 struct sym_attr
*attr
;
897 need_plt_entry
= (dyn_reloc_type
== R_JMP_SLOT
);
898 attr
= get_sym_attr(s1
, sym_index
, 1);
900 /* In case a function is both called and its address taken 2 GOT entries
901 are created, one for taking the address (GOT) and the other for the PLT
903 if (need_plt_entry
? attr
->plt_offset
: attr
->got_offset
)
906 /* create the GOT entry */
907 got_offset
= s1
->got
->data_offset
;
908 section_ptr_add(s1
->got
, PTR_SIZE
);
910 /* Create the GOT relocation that will insert the address of the object or
911 function of interest in the GOT entry. This is a static relocation for
912 memory output (dlsym will give us the address of symbols) and dynamic
913 relocation otherwise (executable and DLLs). The relocation should be
914 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
915 associated to a PLT entry) but is currently done at load time for an
918 sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
919 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
922 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
) {
923 /* Hack alarm. We don't want to emit dynamic symbols
924 and symbol based relocs for STB_LOCAL symbols, but rather
925 want to resolve them directly. At this point the symbol
926 values aren't final yet, so we must defer this. We will later
927 have to create a RELATIVE reloc anyway, so we misuse the
928 relocation slot to smuggle the symbol reference until
929 fill_local_got_entries. Not that the sym_index is
930 relative to symtab_section, not s1->dynsym! Nevertheless
931 we use s1->dyn_sym so that if this is the first call
932 that got->reloc is correctly created. Also note that
933 RELATIVE relocs are not normally created for the .got,
934 so the types serves as a marker for later (and is retained
935 also for the final output, which is okay because then the
936 got is just normal data). */
937 put_elf_reloc(s1
->dynsym
, s1
->got
, got_offset
, R_RELATIVE
,
940 if (0 == attr
->dyn_index
)
941 attr
->dyn_index
= set_elf_sym(s1
->dynsym
, sym
->st_value
, size
,
942 info
, 0, sym
->st_shndx
, name
);
943 put_elf_reloc(s1
->dynsym
, s1
->got
, got_offset
, dyn_reloc_type
,
947 put_elf_reloc(symtab_section
, s1
->got
, got_offset
, dyn_reloc_type
,
951 if (need_plt_entry
) {
953 s1
->plt
= new_section(s1
, ".plt", SHT_PROGBITS
,
954 SHF_ALLOC
| SHF_EXECINSTR
);
955 s1
->plt
->sh_entsize
= 4;
958 attr
->plt_offset
= create_plt_entry(s1
, got_offset
, attr
);
960 /* create a symbol 'sym@plt' for the PLT jump vector */
962 if (len
> sizeof plt_name
- 5)
963 len
= sizeof plt_name
- 5;
964 memcpy(plt_name
, name
, len
);
965 strcpy(plt_name
+ len
, "@plt");
966 attr
->plt_sym
= put_elf_sym(s1
->symtab
, attr
->plt_offset
, sym
->st_size
,
967 ELFW(ST_INFO
)(STB_GLOBAL
, STT_FUNC
), 0, s1
->plt
->sh_num
, plt_name
);
970 attr
->got_offset
= got_offset
;
976 /* build GOT and PLT entries */
977 ST_FUNC
void build_got_entries(TCCState
*s1
)
982 int i
, type
, gotplt_entry
, reloc_type
, sym_index
;
983 struct sym_attr
*attr
;
985 for(i
= 1; i
< s1
->nb_sections
; i
++) {
987 if (s
->sh_type
!= SHT_RELX
)
989 /* no need to handle got relocations */
990 if (s
->link
!= symtab_section
)
992 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
993 type
= ELFW(R_TYPE
)(rel
->r_info
);
994 gotplt_entry
= gotplt_entry_type(type
);
995 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
996 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
998 if (gotplt_entry
== NO_GOTPLT_ENTRY
) {
1002 /* Automatically create PLT/GOT [entry] if it is an undefined
1003 reference (resolved at runtime), or the symbol is absolute,
1004 probably created by tcc_add_symbol, and thus on 64-bit
1005 targets might be too far from application code. */
1006 if (gotplt_entry
== AUTO_GOTPLT_ENTRY
) {
1007 if (sym
->st_shndx
== SHN_UNDEF
) {
1010 if (s1
->output_type
== TCC_OUTPUT_DLL
&& ! PCRELATIVE_DLLPLT
)
1012 /* Relocations for UNDEF symbols would normally need
1013 to be transferred into the executable or shared object.
1014 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1015 But TCC doesn't do that (at least for exes), so we
1016 need to resolve all such relocs locally. And that
1017 means PLT slots for functions in DLLs and COPY relocs for
1018 data symbols. COPY relocs were generated in
1019 bind_exe_dynsyms (and the symbol adjusted to be defined),
1020 and for functions we were generated a dynamic symbol
1021 of function type. */
1023 /* dynsym isn't set for -run :-/ */
1024 dynindex
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
1025 esym
= (ElfW(Sym
) *)s1
->dynsym
->data
+ dynindex
;
1027 && (ELFW(ST_TYPE
)(esym
->st_info
) == STT_FUNC
1028 || (ELFW(ST_TYPE
)(esym
->st_info
) == STT_NOTYPE
1029 && ELFW(ST_TYPE
)(sym
->st_info
) == STT_FUNC
)))
1032 } else if (!(sym
->st_shndx
== SHN_ABS
1033 #ifndef TCC_TARGET_ARM
1040 #ifdef TCC_TARGET_X86_64
1041 if ((type
== R_X86_64_PLT32
|| type
== R_X86_64_PC32
) &&
1042 (ELFW(ST_VISIBILITY
)(sym
->st_other
) != STV_DEFAULT
||
1043 ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
)) {
1044 rel
->r_info
= ELFW(R_INFO
)(sym_index
, R_X86_64_PC32
);
1048 if (code_reloc(type
)) {
1050 reloc_type
= R_JMP_SLOT
;
1052 reloc_type
= R_GLOB_DAT
;
1057 if (gotplt_entry
== BUILD_GOT_ONLY
)
1060 attr
= put_got_entry(s1
, reloc_type
, sym
->st_size
, sym
->st_info
,
1063 if (reloc_type
== R_JMP_SLOT
)
1064 rel
->r_info
= ELFW(R_INFO
)(attr
->plt_sym
, type
);
1069 /* put dynamic tag */
1070 static void put_dt(Section
*dynamic
, int dt
, addr_t val
)
1073 dyn
= section_ptr_add(dynamic
, sizeof(ElfW(Dyn
)));
1075 dyn
->d_un
.d_val
= val
;
1078 #ifndef TCC_TARGET_PE
1079 static void add_init_array_defines(TCCState
*s1
, const char *section_name
)
1083 char sym_start
[1024];
1086 snprintf(sym_start
, sizeof(sym_start
), "__%s_start", section_name
+ 1);
1087 snprintf(sym_end
, sizeof(sym_end
), "__%s_end", section_name
+ 1);
1089 s
= find_section(s1
, section_name
);
1094 end_offset
= s
->data_offset
;
1097 set_elf_sym(symtab_section
,
1099 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1100 s
->sh_num
, sym_start
);
1101 set_elf_sym(symtab_section
,
1103 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1104 s
->sh_num
, sym_end
);
1108 static int tcc_add_support(TCCState
*s1
, const char *filename
)
1111 snprintf(buf
, sizeof(buf
), "%s/%s", s1
->tcc_lib_path
, filename
);
1112 return tcc_add_file(s1
, buf
);
1115 ST_FUNC
void tcc_add_bcheck(TCCState
*s1
)
1117 #ifdef CONFIG_TCC_BCHECK
1121 if (0 == s1
->do_bounds_check
)
1123 /* XXX: add an object file to do that */
1124 ptr
= section_ptr_add(bounds_section
, sizeof(*ptr
));
1126 set_elf_sym(symtab_section
, 0, 0,
1127 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1128 bounds_section
->sh_num
, "__bounds_start");
1129 /* pull bcheck.o from libtcc1.a */
1130 sym_index
= set_elf_sym(symtab_section
, 0, 0,
1131 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1132 SHN_UNDEF
, "__bound_init");
1133 if (s1
->output_type
!= TCC_OUTPUT_MEMORY
) {
1134 /* add 'call __bound_init()' in .init section */
1135 Section
*init_section
= find_section(s1
, ".init");
1136 unsigned char *pinit
= section_ptr_add(init_section
, 5);
1138 write32le(pinit
+ 1, -4);
1139 put_elf_reloc(symtab_section
, init_section
,
1140 init_section
->data_offset
- 4, R_386_PC32
, sym_index
);
1141 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1146 /* add tcc runtime libraries */
1147 ST_FUNC
void tcc_add_runtime(TCCState
*s1
)
1150 tcc_add_pragma_libs(s1
);
1152 if (!s1
->nostdlib
) {
1153 tcc_add_library_err(s1
, "c");
1155 if (!s1
->static_link
) {
1156 if (TCC_LIBGCC
[0] == '/')
1157 tcc_add_file(s1
, TCC_LIBGCC
);
1159 tcc_add_dll(s1
, TCC_LIBGCC
, 0);
1162 tcc_add_support(s1
, TCC_LIBTCC1
);
1163 /* add crt end if not memory output */
1164 if (s1
->output_type
!= TCC_OUTPUT_MEMORY
)
1165 tcc_add_crt(s1
, "crtn.o");
1169 /* add various standard linker symbols (must be done after the
1170 sections are filled (for example after allocating common
1172 ST_FUNC
void tcc_add_linker_symbols(TCCState
*s1
)
1178 set_elf_sym(symtab_section
,
1179 text_section
->data_offset
, 0,
1180 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1181 text_section
->sh_num
, "_etext");
1182 set_elf_sym(symtab_section
,
1183 data_section
->data_offset
, 0,
1184 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1185 data_section
->sh_num
, "_edata");
1186 set_elf_sym(symtab_section
,
1187 bss_section
->data_offset
, 0,
1188 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1189 bss_section
->sh_num
, "_end");
1190 #ifndef TCC_TARGET_PE
1191 /* horrible new standard ldscript defines */
1192 add_init_array_defines(s1
, ".preinit_array");
1193 add_init_array_defines(s1
, ".init_array");
1194 add_init_array_defines(s1
, ".fini_array");
1197 /* add start and stop symbols for sections whose name can be
1199 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1200 s
= s1
->sections
[i
];
1201 if (s
->sh_type
== SHT_PROGBITS
&&
1202 (s
->sh_flags
& SHF_ALLOC
)) {
1206 /* check if section name can be expressed in C */
1212 if (!isid(ch
) && !isnum(ch
))
1216 snprintf(buf
, sizeof(buf
), "__start_%s", s
->name
);
1217 set_elf_sym(symtab_section
,
1219 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1221 snprintf(buf
, sizeof(buf
), "__stop_%s", s
->name
);
1222 set_elf_sym(symtab_section
,
1224 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1231 static void tcc_output_binary(TCCState
*s1
, FILE *f
,
1232 const int *sec_order
)
1235 int i
, offset
, size
;
1238 for(i
=1;i
<s1
->nb_sections
;i
++) {
1239 s
= s1
->sections
[sec_order
[i
]];
1240 if (s
->sh_type
!= SHT_NOBITS
&&
1241 (s
->sh_flags
& SHF_ALLOC
)) {
1242 while (offset
< s
->sh_offset
) {
1247 fwrite(s
->data
, 1, size
, f
);
1253 ST_FUNC
void fill_got_entry(TCCState
*s1
, ElfW_Rel
*rel
)
1255 int sym_index
= ELFW(R_SYM
) (rel
->r_info
);
1256 ElfW(Sym
) *sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1257 struct sym_attr
*attr
= get_sym_attr(s1
, sym_index
, 0);
1258 unsigned offset
= attr
->got_offset
;
1262 section_reserve(s1
->got
, offset
+ PTR_SIZE
);
1263 #ifdef TCC_TARGET_X86_64
1264 write64le(s1
->got
->data
+ offset
, sym
->st_value
);
1266 write32le(s1
->got
->data
+ offset
, sym
->st_value
);
1270 /* Perform relocation to GOT or PLT entries */
1271 ST_FUNC
void fill_got(TCCState
*s1
)
1277 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1278 s
= s1
->sections
[i
];
1279 if (s
->sh_type
!= SHT_RELX
)
1281 /* no need to handle got relocations */
1282 if (s
->link
!= symtab_section
)
1284 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
1285 switch (ELFW(R_TYPE
) (rel
->r_info
)) {
1286 case R_X86_64_GOT32
:
1287 case R_X86_64_GOTPCREL
:
1288 case R_X86_64_GOTPCRELX
:
1289 case R_X86_64_REX_GOTPCRELX
:
1290 case R_X86_64_PLT32
:
1291 fill_got_entry(s1
, rel
);
1298 /* See put_got_entry for a description. This is the second stage
1299 where GOT references to local defined symbols are rewritten. */
1300 static void fill_local_got_entries(TCCState
*s1
)
1303 for_each_elem(s1
->got
->reloc
, 0, rel
, ElfW_Rel
) {
1304 if (ELFW(R_TYPE
)(rel
->r_info
) == R_RELATIVE
) {
1305 int sym_index
= ELFW(R_SYM
) (rel
->r_info
);
1306 ElfW(Sym
) *sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1307 struct sym_attr
*attr
= get_sym_attr(s1
, sym_index
, 0);
1308 unsigned offset
= attr
->got_offset
;
1309 if (offset
!= rel
->r_offset
- s1
->got
->sh_addr
)
1310 tcc_error_noabort("huh");
1311 rel
->r_info
= ELFW(R_INFO
)(0, R_RELATIVE
);
1312 #if SHT_RELX == SHT_RELA
1313 rel
->r_addend
= sym
->st_value
;
1315 /* All our REL architectures also happen to be 32bit LE. */
1316 write32le(s1
->got
->data
+ offset
, sym
->st_value
);
1322 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1323 in shared libraries and export non local defined symbols to shared libraries
1324 if -rdynamic switch was given on command line */
1325 static void bind_exe_dynsyms(TCCState
*s1
)
1328 int sym_index
, index
;
1329 ElfW(Sym
) *sym
, *esym
;
1332 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1333 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1334 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1335 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1336 if (sym
->st_shndx
== SHN_UNDEF
) {
1337 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1338 sym_index
= find_elf_sym(s1
->dynsymtab_section
, name
);
1340 esym
= &((ElfW(Sym
) *)s1
->dynsymtab_section
->data
)[sym_index
];
1341 type
= ELFW(ST_TYPE
)(esym
->st_info
);
1342 if ((type
== STT_FUNC
) || (type
== STT_GNU_IFUNC
)) {
1343 /* Indirect functions shall have STT_FUNC type in executable
1344 * dynsym section. Indeed, a dlsym call following a lazy
1345 * resolution would pick the symbol value from the
1346 * executable dynsym entry which would contain the address
1347 * of the function wanted by the caller of dlsym instead of
1348 * the address of the function that would return that
1351 = put_elf_sym(s1
->dynsym
, 0, esym
->st_size
,
1352 ELFW(ST_INFO
)(STB_GLOBAL
,STT_FUNC
), 0, 0,
1354 int index
= sym
- (ElfW(Sym
) *) symtab_section
->data
;
1355 get_sym_attr(s1
, index
, 1)->dyn_index
= dynindex
;
1356 } else if (type
== STT_OBJECT
) {
1357 unsigned long offset
;
1359 offset
= bss_section
->data_offset
;
1360 /* XXX: which alignment ? */
1361 offset
= (offset
+ 16 - 1) & -16;
1362 set_elf_sym (s1
->symtab
, offset
, esym
->st_size
,
1363 esym
->st_info
, 0, bss_section
->sh_num
, name
);
1364 index
= put_elf_sym(s1
->dynsym
, offset
, esym
->st_size
,
1365 esym
->st_info
, 0, bss_section
->sh_num
,
1368 /* Ensure R_COPY works for weak symbol aliases */
1369 if (ELFW(ST_BIND
)(esym
->st_info
) == STB_WEAK
) {
1370 for_each_elem(s1
->dynsymtab_section
, 1, dynsym
, ElfW(Sym
)) {
1371 if ((dynsym
->st_value
== esym
->st_value
)
1372 && (ELFW(ST_BIND
)(dynsym
->st_info
) == STB_GLOBAL
)) {
1373 char *dynname
= (char *) s1
->dynsymtab_section
->link
->data
1375 put_elf_sym(s1
->dynsym
, offset
, dynsym
->st_size
,
1377 bss_section
->sh_num
, dynname
);
1383 put_elf_reloc(s1
->dynsym
, bss_section
,
1384 offset
, R_COPY
, index
);
1385 offset
+= esym
->st_size
;
1386 bss_section
->data_offset
= offset
;
1389 /* STB_WEAK undefined symbols are accepted */
1390 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1391 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
||
1392 !strcmp(name
, "_fp_hw")) {
1394 tcc_error_noabort("undefined symbol '%s'", name
);
1397 } else if (s1
->rdynamic
&& ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1398 /* if -rdynamic option, then export all non local symbols */
1399 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1400 set_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
, sym
->st_info
,
1401 0, sym
->st_shndx
, name
);
1406 /* Bind symbols of libraries: export all non local symbols of executable that
1407 are referenced by shared libraries. The reason is that the dynamic loader
1408 search symbol first in executable and then in libraries. Therefore a
1409 reference to a symbol already defined by a library can still be resolved by
1410 a symbol in the executable. */
1411 static void bind_libs_dynsyms(TCCState
*s1
)
1415 ElfW(Sym
) *sym
, *esym
;
1417 for_each_elem(s1
->dynsymtab_section
, 1, esym
, ElfW(Sym
)) {
1418 name
= (char *) s1
->dynsymtab_section
->link
->data
+ esym
->st_name
;
1419 sym_index
= find_elf_sym(symtab_section
, name
);
1420 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1421 if (sym_index
&& sym
->st_shndx
!= SHN_UNDEF
1422 && ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1423 set_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
,
1424 sym
->st_info
, 0, sym
->st_shndx
, name
);
1425 } else if (esym
->st_shndx
== SHN_UNDEF
) {
1426 /* weak symbols can stay undefined */
1427 if (ELFW(ST_BIND
)(esym
->st_info
) != STB_WEAK
)
1428 tcc_warning("undefined dynamic symbol '%s'", name
);
1433 /* Export all non local symbols. This is used by shared libraries so that the
1434 non local symbols they define can resolve a reference in another shared
1435 library or in the executable. Correspondingly, it allows undefined local
1436 symbols to be resolved by other shared libraries or by the executable. */
1437 static void export_global_syms(TCCState
*s1
)
1439 int dynindex
, index
;
1443 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1444 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1445 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1446 dynindex
= put_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
,
1447 sym
->st_info
, 0, sym
->st_shndx
, name
);
1448 index
= sym
- (ElfW(Sym
) *) symtab_section
->data
;
1449 get_sym_attr(s1
, index
, 1)->dyn_index
= dynindex
;
1454 /* Allocate strings for section names and decide if an unallocated section
1456 NOTE: the strsec section comes last, so its size is also correct ! */
1457 static int alloc_sec_names(TCCState
*s1
, int file_type
, Section
*strsec
)
1463 /* Allocate strings for section names */
1464 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1465 s
= s1
->sections
[i
];
1466 /* when generating a DLL, we include relocations but we may
1468 if (file_type
== TCC_OUTPUT_DLL
&&
1469 s
->sh_type
== SHT_RELX
&&
1470 !(s
->sh_flags
& SHF_ALLOC
) &&
1471 (s1
->sections
[s
->sh_info
]->sh_flags
& SHF_ALLOC
) &&
1472 prepare_dynamic_rel(s1
, s
)) {
1473 if (s1
->sections
[s
->sh_info
]->sh_flags
& SHF_EXECINSTR
)
1475 } else if (s1
->do_debug
||
1476 file_type
== TCC_OUTPUT_OBJ
||
1477 (s
->sh_flags
& SHF_ALLOC
) ||
1478 i
== (s1
->nb_sections
- 1)) {
1479 /* we output all sections if debug or object file */
1480 s
->sh_size
= s
->data_offset
;
1482 if (s
->sh_size
|| (s
->sh_flags
& SHF_ALLOC
))
1483 s
->sh_name
= put_elf_str(strsec
, s
->name
);
1485 strsec
->sh_size
= strsec
->data_offset
;
1489 /* Info to be copied in dynamic section */
1493 unsigned long data_offset
;
1496 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1502 /* Assign sections to segments and decide how are sections laid out when loaded
1503 in memory. This function also fills corresponding program headers. */
1504 static int layout_sections(TCCState
*s1
, ElfW(Phdr
) *phdr
, int phnum
,
1505 Section
*interp
, Section
* strsec
,
1506 struct dyn_inf
*dyninf
, int *sec_order
)
1508 int i
, j
, k
, file_type
, sh_order_index
, file_offset
;
1509 unsigned long s_align
;
1515 file_type
= s1
->output_type
;
1518 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
)
1519 file_offset
= sizeof(ElfW(Ehdr
)) + phnum
* sizeof(ElfW(Phdr
));
1520 s_align
= ELF_PAGE_SIZE
;
1521 if (s1
->section_align
)
1522 s_align
= s1
->section_align
;
1525 if (s1
->has_text_addr
) {
1526 int a_offset
, p_offset
;
1527 addr
= s1
->text_addr
;
1528 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1530 a_offset
= (int) (addr
& (s_align
- 1));
1531 p_offset
= file_offset
& (s_align
- 1);
1532 if (a_offset
< p_offset
)
1533 a_offset
+= s_align
;
1534 file_offset
+= (a_offset
- p_offset
);
1536 if (file_type
== TCC_OUTPUT_DLL
)
1539 addr
= ELF_START_ADDR
;
1540 /* compute address after headers */
1541 addr
+= (file_offset
& (s_align
- 1));
1545 /* Leave one program headers for the program interpreter and one for
1546 the program header table itself if needed. These are done later as
1547 they require section layout to be done first. */
1551 /* dynamic relocation table information, for .dynamic section */
1552 dyninf
->rel_addr
= dyninf
->rel_size
= 0;
1553 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1554 dyninf
->bss_addr
= dyninf
->bss_size
= 0;
1557 for(j
= 0; j
< 2; j
++) {
1558 ph
->p_type
= PT_LOAD
;
1560 ph
->p_flags
= PF_R
| PF_X
;
1562 ph
->p_flags
= PF_R
| PF_W
;
1563 ph
->p_align
= s_align
;
1565 /* Decide the layout of sections loaded in memory. This must
1566 be done before program headers are filled since they contain
1567 info about the layout. We do the following ordering: interp,
1568 symbol tables, relocations, progbits, nobits */
1569 /* XXX: do faster and simpler sorting */
1570 for(k
= 0; k
< 5; k
++) {
1571 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1572 s
= s1
->sections
[i
];
1573 /* compute if section should be included */
1575 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_WRITE
)) !=
1579 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_WRITE
)) !=
1580 (SHF_ALLOC
| SHF_WRITE
))
1586 } else if (s
->sh_type
== SHT_DYNSYM
||
1587 s
->sh_type
== SHT_STRTAB
||
1588 s
->sh_type
== SHT_HASH
) {
1591 } else if (s
->sh_type
== SHT_RELX
) {
1594 } else if (s
->sh_type
== SHT_NOBITS
) {
1601 sec_order
[sh_order_index
++] = i
;
1603 /* section matches: we align it and add its size */
1605 addr
= (addr
+ s
->sh_addralign
- 1) &
1606 ~(s
->sh_addralign
- 1);
1607 file_offset
+= (int) ( addr
- tmp
);
1608 s
->sh_offset
= file_offset
;
1611 /* update program header infos */
1612 if (ph
->p_offset
== 0) {
1613 ph
->p_offset
= file_offset
;
1615 ph
->p_paddr
= ph
->p_vaddr
;
1617 /* update dynamic relocation infos */
1618 if (s
->sh_type
== SHT_RELX
) {
1619 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1620 if (!strcmp(strsec
->data
+ s
->sh_name
, ".rel.got")) {
1621 dyninf
->rel_addr
= addr
;
1622 dyninf
->rel_size
+= s
->sh_size
; /* XXX only first rel. */
1624 if (!strcmp(strsec
->data
+ s
->sh_name
, ".rel.bss")) {
1625 dyninf
->bss_addr
= addr
;
1626 dyninf
->bss_size
= s
->sh_size
; /* XXX only first rel. */
1629 if (dyninf
->rel_size
== 0)
1630 dyninf
->rel_addr
= addr
;
1631 dyninf
->rel_size
+= s
->sh_size
;
1635 if (s
->sh_type
!= SHT_NOBITS
)
1636 file_offset
+= s
->sh_size
;
1640 /* Make the first PT_LOAD segment include the program
1641 headers itself (and the ELF header as well), it'll
1642 come out with same memory use but will make various
1643 tools like binutils strip work better. */
1644 ph
->p_offset
&= ~(ph
->p_align
- 1);
1645 ph
->p_vaddr
&= ~(ph
->p_align
- 1);
1646 ph
->p_paddr
&= ~(ph
->p_align
- 1);
1648 ph
->p_filesz
= file_offset
- ph
->p_offset
;
1649 ph
->p_memsz
= addr
- ph
->p_vaddr
;
1652 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
) {
1653 /* if in the middle of a page, we duplicate the page in
1654 memory so that one copy is RX and the other is RW */
1655 if ((addr
& (s_align
- 1)) != 0)
1658 addr
= (addr
+ s_align
- 1) & ~(s_align
- 1);
1659 file_offset
= (file_offset
+ s_align
- 1) & ~(s_align
- 1);
1665 /* all other sections come after */
1666 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1667 s
= s1
->sections
[i
];
1668 if (phnum
> 0 && (s
->sh_flags
& SHF_ALLOC
))
1670 sec_order
[sh_order_index
++] = i
;
1672 file_offset
= (file_offset
+ s
->sh_addralign
- 1) &
1673 ~(s
->sh_addralign
- 1);
1674 s
->sh_offset
= file_offset
;
1675 if (s
->sh_type
!= SHT_NOBITS
)
1676 file_offset
+= s
->sh_size
;
1682 static void fill_unloadable_phdr(ElfW(Phdr
) *phdr
, int phnum
, Section
*interp
,
1687 /* if interpreter, then add corresponding program header */
1691 ph
->p_type
= PT_PHDR
;
1692 ph
->p_offset
= sizeof(ElfW(Ehdr
));
1693 ph
->p_filesz
= ph
->p_memsz
= phnum
* sizeof(ElfW(Phdr
));
1694 ph
->p_vaddr
= interp
->sh_addr
- ph
->p_filesz
;
1695 ph
->p_paddr
= ph
->p_vaddr
;
1696 ph
->p_flags
= PF_R
| PF_X
;
1697 ph
->p_align
= 4; /* interp->sh_addralign; */
1700 ph
->p_type
= PT_INTERP
;
1701 ph
->p_offset
= interp
->sh_offset
;
1702 ph
->p_vaddr
= interp
->sh_addr
;
1703 ph
->p_paddr
= ph
->p_vaddr
;
1704 ph
->p_filesz
= interp
->sh_size
;
1705 ph
->p_memsz
= interp
->sh_size
;
1707 ph
->p_align
= interp
->sh_addralign
;
1710 /* if dynamic section, then add corresponding program header */
1712 ph
= &phdr
[phnum
- 1];
1714 ph
->p_type
= PT_DYNAMIC
;
1715 ph
->p_offset
= dynamic
->sh_offset
;
1716 ph
->p_vaddr
= dynamic
->sh_addr
;
1717 ph
->p_paddr
= ph
->p_vaddr
;
1718 ph
->p_filesz
= dynamic
->sh_size
;
1719 ph
->p_memsz
= dynamic
->sh_size
;
1720 ph
->p_flags
= PF_R
| PF_W
;
1721 ph
->p_align
= dynamic
->sh_addralign
;
1725 /* Fill the dynamic section with tags describing the address and size of
1727 static void fill_dynamic(TCCState
*s1
, struct dyn_inf
*dyninf
)
1729 Section
*dynamic
= dyninf
->dynamic
;
1731 /* put dynamic section entries */
1732 put_dt(dynamic
, DT_HASH
, s1
->dynsym
->hash
->sh_addr
);
1733 put_dt(dynamic
, DT_STRTAB
, dyninf
->dynstr
->sh_addr
);
1734 put_dt(dynamic
, DT_SYMTAB
, s1
->dynsym
->sh_addr
);
1735 put_dt(dynamic
, DT_STRSZ
, dyninf
->dynstr
->data_offset
);
1736 put_dt(dynamic
, DT_SYMENT
, sizeof(ElfW(Sym
)));
1738 put_dt(dynamic
, DT_RELA
, dyninf
->rel_addr
);
1739 put_dt(dynamic
, DT_RELASZ
, dyninf
->rel_size
);
1740 put_dt(dynamic
, DT_RELAENT
, sizeof(ElfW_Rel
));
1742 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1743 put_dt(dynamic
, DT_PLTGOT
, s1
->got
->sh_addr
);
1744 put_dt(dynamic
, DT_PLTRELSZ
, dyninf
->rel_size
);
1745 put_dt(dynamic
, DT_JMPREL
, dyninf
->rel_addr
);
1746 put_dt(dynamic
, DT_PLTREL
, DT_REL
);
1747 put_dt(dynamic
, DT_REL
, dyninf
->bss_addr
);
1748 put_dt(dynamic
, DT_RELSZ
, dyninf
->bss_size
);
1750 put_dt(dynamic
, DT_REL
, dyninf
->rel_addr
);
1751 put_dt(dynamic
, DT_RELSZ
, dyninf
->rel_size
);
1752 put_dt(dynamic
, DT_RELENT
, sizeof(ElfW_Rel
));
1756 put_dt(dynamic
, DT_DEBUG
, 0);
1757 put_dt(dynamic
, DT_NULL
, 0);
1760 /* Relocate remaining sections and symbols (that is those not related to
1762 static int final_sections_reloc(TCCState
*s1
)
1767 relocate_syms(s1
, s1
->symtab
, 0);
1769 if (s1
->nb_errors
!= 0)
1772 /* relocate sections */
1773 /* XXX: ignore sections with allocated relocations ? */
1774 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1775 s
= s1
->sections
[i
];
1776 #if defined(TCC_TARGET_I386) || defined(TCC_MUSL)
1777 if (s
->reloc
&& s
!= s1
->got
&& (s
->sh_flags
& SHF_ALLOC
)) //gr
1778 /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC
1779 checking is removed */
1781 if (s
->reloc
&& s
!= s1
->got
)
1782 /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */
1784 relocate_section(s1
, s
);
1787 /* relocate relocation entries if the relocation tables are
1788 allocated in the executable */
1789 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1790 s
= s1
->sections
[i
];
1791 if ((s
->sh_flags
& SHF_ALLOC
) &&
1792 s
->sh_type
== SHT_RELX
) {
1793 relocate_rel(s1
, s
);
1799 /* Create an ELF file on disk.
1800 This function handle ELF specific layout requirements */
1801 static void tcc_output_elf(TCCState
*s1
, FILE *f
, int phnum
, ElfW(Phdr
) *phdr
,
1802 int file_offset
, int *sec_order
)
1804 int i
, shnum
, offset
, size
, file_type
;
1807 ElfW(Shdr
) shdr
, *sh
;
1809 file_type
= s1
->output_type
;
1810 shnum
= s1
->nb_sections
;
1812 memset(&ehdr
, 0, sizeof(ehdr
));
1815 ehdr
.e_phentsize
= sizeof(ElfW(Phdr
));
1816 ehdr
.e_phnum
= phnum
;
1817 ehdr
.e_phoff
= sizeof(ElfW(Ehdr
));
1821 file_offset
= (file_offset
+ 3) & -4;
1824 ehdr
.e_ident
[0] = ELFMAG0
;
1825 ehdr
.e_ident
[1] = ELFMAG1
;
1826 ehdr
.e_ident
[2] = ELFMAG2
;
1827 ehdr
.e_ident
[3] = ELFMAG3
;
1828 ehdr
.e_ident
[4] = ELFCLASSW
;
1829 ehdr
.e_ident
[5] = ELFDATA2LSB
;
1830 ehdr
.e_ident
[6] = EV_CURRENT
;
1831 #if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
1832 /* FIXME: should set only for freebsd _target_, but we exclude only PE target */
1833 ehdr
.e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
1835 #ifdef TCC_TARGET_ARM
1837 ehdr
.e_ident
[EI_OSABI
] = 0;
1838 ehdr
.e_flags
= EF_ARM_EABI_VER4
;
1839 if (file_type
== TCC_OUTPUT_EXE
|| file_type
== TCC_OUTPUT_DLL
)
1840 ehdr
.e_flags
|= EF_ARM_HASENTRY
;
1841 if (s1
->float_abi
== ARM_HARD_FLOAT
)
1842 ehdr
.e_flags
|= EF_ARM_VFP_FLOAT
;
1844 ehdr
.e_flags
|= EF_ARM_SOFT_FLOAT
;
1846 ehdr
.e_ident
[EI_OSABI
] = ELFOSABI_ARM
;
1851 case TCC_OUTPUT_EXE
:
1852 ehdr
.e_type
= ET_EXEC
;
1853 ehdr
.e_entry
= get_elf_sym_addr(s1
, "_start", 1);
1855 case TCC_OUTPUT_DLL
:
1856 ehdr
.e_type
= ET_DYN
;
1857 ehdr
.e_entry
= text_section
->sh_addr
; /* XXX: is it correct ? */
1859 case TCC_OUTPUT_OBJ
:
1860 ehdr
.e_type
= ET_REL
;
1863 ehdr
.e_machine
= EM_TCC_TARGET
;
1864 ehdr
.e_version
= EV_CURRENT
;
1865 ehdr
.e_shoff
= file_offset
;
1866 ehdr
.e_ehsize
= sizeof(ElfW(Ehdr
));
1867 ehdr
.e_shentsize
= sizeof(ElfW(Shdr
));
1868 ehdr
.e_shnum
= shnum
;
1869 ehdr
.e_shstrndx
= shnum
- 1;
1871 fwrite(&ehdr
, 1, sizeof(ElfW(Ehdr
)), f
);
1872 fwrite(phdr
, 1, phnum
* sizeof(ElfW(Phdr
)), f
);
1873 offset
= sizeof(ElfW(Ehdr
)) + phnum
* sizeof(ElfW(Phdr
));
1875 sort_syms(s1
, symtab_section
);
1876 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1877 s
= s1
->sections
[sec_order
[i
]];
1878 if (s
->sh_type
!= SHT_NOBITS
) {
1879 while (offset
< s
->sh_offset
) {
1885 fwrite(s
->data
, 1, size
, f
);
1890 /* output section headers */
1891 while (offset
< ehdr
.e_shoff
) {
1896 for(i
= 0; i
< s1
->nb_sections
; i
++) {
1898 memset(sh
, 0, sizeof(ElfW(Shdr
)));
1899 s
= s1
->sections
[i
];
1901 sh
->sh_name
= s
->sh_name
;
1902 sh
->sh_type
= s
->sh_type
;
1903 sh
->sh_flags
= s
->sh_flags
;
1904 sh
->sh_entsize
= s
->sh_entsize
;
1905 sh
->sh_info
= s
->sh_info
;
1907 sh
->sh_link
= s
->link
->sh_num
;
1908 sh
->sh_addralign
= s
->sh_addralign
;
1909 sh
->sh_addr
= s
->sh_addr
;
1910 sh
->sh_offset
= s
->sh_offset
;
1911 sh
->sh_size
= s
->sh_size
;
1913 fwrite(sh
, 1, sizeof(ElfW(Shdr
)), f
);
1917 /* Write an elf, coff or "binary" file */
1918 static int tcc_write_elf_file(TCCState
*s1
, const char *filename
, int phnum
,
1919 ElfW(Phdr
) *phdr
, int file_offset
, int *sec_order
)
1921 int fd
, mode
, file_type
;
1924 file_type
= s1
->output_type
;
1925 if (file_type
== TCC_OUTPUT_OBJ
)
1930 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, mode
);
1932 tcc_error_noabort("could not write '%s'", filename
);
1935 f
= fdopen(fd
, "wb");
1937 printf("<- %s\n", filename
);
1939 #ifdef TCC_TARGET_COFF
1940 if (s1
->output_format
== TCC_OUTPUT_FORMAT_COFF
)
1941 tcc_output_coff(s1
, f
);
1944 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
)
1945 tcc_output_elf(s1
, f
, phnum
, phdr
, file_offset
, sec_order
);
1947 tcc_output_binary(s1
, f
, sec_order
);
1953 /* Sort section headers by assigned sh_addr, remove sections
1954 that we aren't going to output. */
1955 static void tidy_section_headers(TCCState
*s1
, int *sec_order
)
1957 int i
, nnew
, l
, *backmap
;
1961 snew
= tcc_malloc(s1
->nb_sections
* sizeof(snew
[0]));
1962 backmap
= tcc_malloc(s1
->nb_sections
* sizeof(backmap
[0]));
1963 for (i
= 0, nnew
= 0, l
= s1
->nb_sections
; i
< s1
->nb_sections
; i
++) {
1964 s
= s1
->sections
[sec_order
[i
]];
1965 if (!i
|| s
->sh_name
) {
1966 backmap
[sec_order
[i
]] = nnew
;
1970 backmap
[sec_order
[i
]] = 0;
1974 for (i
= 0; i
< nnew
; i
++) {
1978 if (s
->sh_type
== SHT_RELX
)
1979 s
->sh_info
= backmap
[s
->sh_info
];
1983 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
))
1984 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
)
1985 sym
->st_shndx
= backmap
[sym
->st_shndx
];
1986 if( !s1
->static_link
) {
1987 for_each_elem(s1
->dynsym
, 1, sym
, ElfW(Sym
))
1988 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
)
1989 sym
->st_shndx
= backmap
[sym
->st_shndx
];
1991 for (i
= 0; i
< s1
->nb_sections
; i
++)
1993 tcc_free(s1
->sections
);
1994 s1
->sections
= snew
;
1995 s1
->nb_sections
= nnew
;
1999 /* Output an elf, coff or binary file */
2000 /* XXX: suppress unneeded sections */
2001 static int elf_output_file(TCCState
*s1
, const char *filename
)
2003 int i
, ret
, phnum
, shnum
, file_type
, file_offset
, *sec_order
;
2004 struct dyn_inf dyninf
= {0};
2007 Section
*strsec
, *interp
, *dynamic
, *dynstr
;
2010 file_type
= s1
->output_type
;
2015 interp
= dynamic
= dynstr
= NULL
; /* avoid warning */
2018 if (file_type
!= TCC_OUTPUT_OBJ
) {
2019 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2020 tcc_add_runtime(s1
);
2021 relocate_common_syms();
2022 tcc_add_linker_symbols(s1
);
2024 if (!s1
->static_link
) {
2025 if (file_type
== TCC_OUTPUT_EXE
) {
2027 /* allow override the dynamic loader */
2028 const char *elfint
= getenv("LD_SO");
2030 elfint
= DEFAULT_ELFINTERP(s1
);
2031 /* add interpreter section only if executable */
2032 interp
= new_section(s1
, ".interp", SHT_PROGBITS
, SHF_ALLOC
);
2033 interp
->sh_addralign
= 1;
2034 ptr
= section_ptr_add(interp
, 1 + strlen(elfint
));
2035 strcpy(ptr
, elfint
);
2038 /* add dynamic symbol table */
2039 s1
->dynsym
= new_symtab(s1
, ".dynsym", SHT_DYNSYM
, SHF_ALLOC
,
2041 ".hash", SHF_ALLOC
);
2042 dynstr
= s1
->dynsym
->link
;
2044 /* add dynamic section */
2045 dynamic
= new_section(s1
, ".dynamic", SHT_DYNAMIC
,
2046 SHF_ALLOC
| SHF_WRITE
);
2047 dynamic
->link
= dynstr
;
2048 dynamic
->sh_entsize
= sizeof(ElfW(Dyn
));
2052 if (file_type
== TCC_OUTPUT_EXE
) {
2053 bind_exe_dynsyms(s1
);
2056 bind_libs_dynsyms(s1
);
2058 /* shared library case: simply export all global symbols */
2059 export_global_syms(s1
);
2062 build_got_entries(s1
);
2065 /* we add a section for symbols */
2066 strsec
= new_section(s1
, ".shstrtab", SHT_STRTAB
, 0);
2067 put_elf_str(strsec
, "");
2069 /* Allocate strings for section names */
2070 textrel
= alloc_sec_names(s1
, file_type
, strsec
);
2073 /* add a list of needed dlls */
2074 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
2075 DLLReference
*dllref
= s1
->loaded_dlls
[i
];
2076 if (dllref
->level
== 0)
2077 put_dt(dynamic
, DT_NEEDED
, put_elf_str(dynstr
, dllref
->name
));
2081 put_dt(dynamic
, s1
->enable_new_dtags
? DT_RUNPATH
: DT_RPATH
,
2082 put_elf_str(dynstr
, s1
->rpath
));
2084 if (file_type
== TCC_OUTPUT_DLL
) {
2086 put_dt(dynamic
, DT_SONAME
, put_elf_str(dynstr
, s1
->soname
));
2087 /* XXX: currently, since we do not handle PIC code, we
2088 must relocate the readonly segments */
2090 put_dt(dynamic
, DT_TEXTREL
, 0);
2094 put_dt(dynamic
, DT_SYMBOLIC
, 0);
2096 dyninf
.dynamic
= dynamic
;
2097 dyninf
.dynstr
= dynstr
;
2098 /* remember offset and reserve space for 2nd call below */
2099 dyninf
.data_offset
= dynamic
->data_offset
;
2100 fill_dynamic(s1
, &dyninf
);
2101 dynamic
->sh_size
= dynamic
->data_offset
;
2102 dynstr
->sh_size
= dynstr
->data_offset
;
2105 /* compute number of program headers */
2106 if (file_type
== TCC_OUTPUT_OBJ
)
2108 else if (file_type
== TCC_OUTPUT_DLL
)
2110 else if (s1
->static_link
)
2115 /* allocate program segment headers */
2116 phdr
= tcc_mallocz(phnum
* sizeof(ElfW(Phdr
)));
2118 /* compute number of sections */
2119 shnum
= s1
->nb_sections
;
2121 /* this array is used to reorder sections in the output file */
2122 sec_order
= tcc_malloc(sizeof(int) * shnum
);
2125 /* compute section to program header mapping */
2126 file_offset
= layout_sections(s1
, phdr
, phnum
, interp
, strsec
, &dyninf
,
2129 /* Fill remaining program header and finalize relocation related to dynamic
2131 if (file_type
!= TCC_OUTPUT_OBJ
) {
2132 fill_unloadable_phdr(phdr
, phnum
, interp
, dynamic
);
2134 dynamic
->data_offset
= dyninf
.data_offset
;
2135 fill_dynamic(s1
, &dyninf
);
2137 /* put in GOT the dynamic section address and relocate PLT */
2138 write32le(s1
->got
->data
, dynamic
->sh_addr
);
2139 if (file_type
== TCC_OUTPUT_EXE
2140 || (RELOCATE_DLLPLT
&& file_type
== TCC_OUTPUT_DLL
))
2143 /* relocate symbols in .dynsym now that final addresses are known */
2144 for_each_elem(s1
->dynsym
, 1, sym
, ElfW(Sym
)) {
2145 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
) {
2146 /* do symbol relocation */
2147 sym
->st_value
+= s1
->sections
[sym
->st_shndx
]->sh_addr
;
2152 /* if building executable or DLL, then relocate each section
2153 except the GOT which is already relocated */
2154 ret
= final_sections_reloc(s1
);
2157 tidy_section_headers(s1
, sec_order
);
2159 /* Perform relocation to GOT or PLT entries */
2160 if (file_type
== TCC_OUTPUT_EXE
&& s1
->static_link
)
2163 fill_local_got_entries(s1
);
2166 /* Create the ELF file with name 'filename' */
2167 ret
= tcc_write_elf_file(s1
, filename
, phnum
, phdr
, file_offset
, sec_order
);
2168 s1
->nb_sections
= shnum
;
2170 tcc_free(sec_order
);
2175 LIBTCCAPI
int tcc_output_file(TCCState
*s
, const char *filename
)
2178 #ifdef TCC_TARGET_PE
2179 if (s
->output_type
!= TCC_OUTPUT_OBJ
) {
2180 ret
= pe_output_file(s
, filename
);
2183 ret
= elf_output_file(s
, filename
);
2187 static void *load_data(int fd
, unsigned long file_offset
, unsigned long size
)
2191 data
= tcc_malloc(size
);
2192 lseek(fd
, file_offset
, SEEK_SET
);
2193 read(fd
, data
, size
);
2197 typedef struct SectionMergeInfo
{
2198 Section
*s
; /* corresponding existing section */
2199 unsigned long offset
; /* offset of the new section in the existing section */
2200 uint8_t new_section
; /* true if section 's' was added */
2201 uint8_t link_once
; /* true if link once section */
2204 ST_FUNC
int tcc_object_type(int fd
, ElfW(Ehdr
) *h
)
2206 int size
= read(fd
, h
, sizeof *h
);
2207 if (size
== sizeof *h
&& 0 == memcmp(h
, ELFMAG
, 4)) {
2208 if (h
->e_type
== ET_REL
)
2209 return AFF_BINTYPE_REL
;
2210 if (h
->e_type
== ET_DYN
)
2211 return AFF_BINTYPE_DYN
;
2212 } else if (size
>= 8) {
2213 if (0 == memcmp(h
, ARMAG
, 8))
2214 return AFF_BINTYPE_AR
;
2215 #ifdef TCC_TARGET_COFF
2216 if (((struct filehdr
*)h
)->f_magic
== COFF_C67_MAGIC
)
2217 return AFF_BINTYPE_C67
;
2223 /* load an object file and merge it with current files */
2224 /* XXX: handle correctly stab (debug) info */
2225 ST_FUNC
int tcc_load_object_file(TCCState
*s1
,
2226 int fd
, unsigned long file_offset
)
2229 ElfW(Shdr
) *shdr
, *sh
;
2230 int size
, i
, j
, offset
, offseti
, nb_syms
, sym_index
, ret
, seencompressed
;
2231 unsigned char *strsec
, *strtab
;
2232 int *old_to_new_syms
;
2233 char *sh_name
, *name
;
2234 SectionMergeInfo
*sm_table
, *sm
;
2235 ElfW(Sym
) *sym
, *symtab
;
2242 stab_index
= stabstr_index
= 0;
2244 lseek(fd
, file_offset
, SEEK_SET
);
2245 if (tcc_object_type(fd
, &ehdr
) != AFF_BINTYPE_REL
)
2247 /* test CPU specific stuff */
2248 if (ehdr
.e_ident
[5] != ELFDATA2LSB
||
2249 ehdr
.e_machine
!= EM_TCC_TARGET
) {
2251 tcc_error_noabort("invalid object file");
2255 shdr
= load_data(fd
, file_offset
+ ehdr
.e_shoff
,
2256 sizeof(ElfW(Shdr
)) * ehdr
.e_shnum
);
2257 sm_table
= tcc_mallocz(sizeof(SectionMergeInfo
) * ehdr
.e_shnum
);
2259 /* load section names */
2260 sh
= &shdr
[ehdr
.e_shstrndx
];
2261 strsec
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2263 /* load symtab and strtab */
2264 old_to_new_syms
= NULL
;
2269 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2271 if (sh
->sh_type
== SHT_SYMTAB
) {
2273 tcc_error_noabort("object must contain only one symtab");
2278 nb_syms
= sh
->sh_size
/ sizeof(ElfW(Sym
));
2279 symtab
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2280 sm_table
[i
].s
= symtab_section
;
2282 /* now load strtab */
2283 sh
= &shdr
[sh
->sh_link
];
2284 strtab
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2286 if (sh
->sh_flags
& SHF_COMPRESSED
)
2290 /* now examine each section and try to merge its content with the
2292 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2293 /* no need to examine section name strtab */
2294 if (i
== ehdr
.e_shstrndx
)
2297 sh_name
= (char *) strsec
+ sh
->sh_name
;
2298 /* ignore sections types we do not handle */
2299 if (sh
->sh_type
!= SHT_PROGBITS
&&
2300 sh
->sh_type
!= SHT_RELX
&&
2302 sh
->sh_type
!= SHT_ARM_EXIDX
&&
2304 sh
->sh_type
!= SHT_NOBITS
&&
2305 sh
->sh_type
!= SHT_PREINIT_ARRAY
&&
2306 sh
->sh_type
!= SHT_INIT_ARRAY
&&
2307 sh
->sh_type
!= SHT_FINI_ARRAY
&&
2308 strcmp(sh_name
, ".stabstr")
2312 && (!strncmp(sh_name
, ".debug_", sizeof(".debug_")-1)
2313 || (sh
->sh_type
== SHT_RELX
2314 && !strncmp((char*)strsec
+ shdr
[sh
->sh_info
].sh_name
,
2315 ".debug_", sizeof(".debug_")-1))))
2317 if (sh
->sh_addralign
< 1)
2318 sh
->sh_addralign
= 1;
2319 /* find corresponding section, if any */
2320 for(j
= 1; j
< s1
->nb_sections
;j
++) {
2321 s
= s1
->sections
[j
];
2322 if (!strcmp(s
->name
, sh_name
)) {
2323 if (!strncmp(sh_name
, ".gnu.linkonce",
2324 sizeof(".gnu.linkonce") - 1)) {
2325 /* if a 'linkonce' section is already present, we
2326 do not add it again. It is a little tricky as
2327 symbols can still be defined in
2329 sm_table
[i
].link_once
= 1;
2336 /* not found: create new section */
2337 s
= new_section(s1
, sh_name
, sh
->sh_type
, sh
->sh_flags
& ~SHF_GROUP
);
2338 /* take as much info as possible from the section. sh_link and
2339 sh_info will be updated later */
2340 s
->sh_addralign
= sh
->sh_addralign
;
2341 s
->sh_entsize
= sh
->sh_entsize
;
2342 sm_table
[i
].new_section
= 1;
2344 if (sh
->sh_type
!= s
->sh_type
) {
2345 tcc_error_noabort("invalid section type");
2349 /* align start of section */
2350 offset
= s
->data_offset
;
2352 if (0 == strcmp(sh_name
, ".stab")) {
2356 if (0 == strcmp(sh_name
, ".stabstr")) {
2361 size
= sh
->sh_addralign
- 1;
2362 offset
= (offset
+ size
) & ~size
;
2363 if (sh
->sh_addralign
> s
->sh_addralign
)
2364 s
->sh_addralign
= sh
->sh_addralign
;
2365 s
->data_offset
= offset
;
2367 sm_table
[i
].offset
= offset
;
2369 /* concatenate sections */
2371 if (sh
->sh_type
!= SHT_NOBITS
) {
2373 lseek(fd
, file_offset
+ sh
->sh_offset
, SEEK_SET
);
2374 ptr
= section_ptr_add(s
, size
);
2375 read(fd
, ptr
, size
);
2377 s
->data_offset
+= size
;
2382 /* gr relocate stab strings */
2383 if (stab_index
&& stabstr_index
) {
2386 s
= sm_table
[stab_index
].s
;
2387 a
= (Stab_Sym
*)(s
->data
+ sm_table
[stab_index
].offset
);
2388 b
= (Stab_Sym
*)(s
->data
+ s
->data_offset
);
2389 o
= sm_table
[stabstr_index
].offset
;
2391 a
->n_strx
+= o
, a
++;
2394 /* second short pass to update sh_link and sh_info fields of new
2396 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2398 if (!s
|| !sm_table
[i
].new_section
)
2401 if (sh
->sh_link
> 0)
2402 s
->link
= sm_table
[sh
->sh_link
].s
;
2403 if (sh
->sh_type
== SHT_RELX
) {
2404 s
->sh_info
= sm_table
[sh
->sh_info
].s
->sh_num
;
2405 /* update backward link */
2406 s1
->sections
[s
->sh_info
]->reloc
= s
;
2411 /* resolve symbols */
2412 old_to_new_syms
= tcc_mallocz(nb_syms
* sizeof(int));
2415 for(i
= 1; i
< nb_syms
; i
++, sym
++) {
2416 if (sym
->st_shndx
!= SHN_UNDEF
&&
2417 sym
->st_shndx
< SHN_LORESERVE
) {
2418 sm
= &sm_table
[sym
->st_shndx
];
2419 if (sm
->link_once
) {
2420 /* if a symbol is in a link once section, we use the
2421 already defined symbol. It is very important to get
2422 correct relocations */
2423 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
2424 name
= (char *) strtab
+ sym
->st_name
;
2425 sym_index
= find_elf_sym(symtab_section
, name
);
2427 old_to_new_syms
[i
] = sym_index
;
2431 /* if no corresponding section added, no need to add symbol */
2434 /* convert section number */
2435 sym
->st_shndx
= sm
->s
->sh_num
;
2437 sym
->st_value
+= sm
->offset
;
2440 name
= (char *) strtab
+ sym
->st_name
;
2441 sym_index
= set_elf_sym(symtab_section
, sym
->st_value
, sym
->st_size
,
2442 sym
->st_info
, sym
->st_other
,
2443 sym
->st_shndx
, name
);
2444 old_to_new_syms
[i
] = sym_index
;
2447 /* third pass to patch relocation entries */
2448 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2453 offset
= sm_table
[i
].offset
;
2454 switch(s
->sh_type
) {
2456 /* take relocation offset information */
2457 offseti
= sm_table
[sh
->sh_info
].offset
;
2458 for_each_elem(s
, (offset
/ sizeof(*rel
)), rel
, ElfW_Rel
) {
2461 /* convert symbol index */
2462 type
= ELFW(R_TYPE
)(rel
->r_info
);
2463 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
2464 /* NOTE: only one symtab assumed */
2465 if (sym_index
>= nb_syms
)
2467 sym_index
= old_to_new_syms
[sym_index
];
2468 /* ignore link_once in rel section. */
2469 if (!sym_index
&& !sm
->link_once
2470 #ifdef TCC_TARGET_ARM
2471 && type
!= R_ARM_V4BX
2475 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2476 i
, strsec
+ sh
->sh_name
, rel
->r_offset
);
2479 rel
->r_info
= ELFW(R_INFO
)(sym_index
, type
);
2480 /* offset the relocation offset */
2481 rel
->r_offset
+= offseti
;
2482 #ifdef TCC_TARGET_ARM
2483 /* Jumps and branches from a Thumb code to a PLT entry need
2484 special handling since PLT entries are ARM code.
2485 Unconditional bl instructions referencing PLT entries are
2486 handled by converting these instructions into blx
2487 instructions. Other case of instructions referencing a PLT
2488 entry require to add a Thumb stub before the PLT entry to
2489 switch to ARM mode. We set bit plt_thumb_stub of the
2490 attribute of a symbol to indicate such a case. */
2491 if (type
== R_ARM_THM_JUMP24
)
2492 get_sym_attr(s1
, sym_index
, 1)->plt_thumb_stub
= 1;
2505 tcc_free(old_to_new_syms
);
2512 typedef struct ArchiveHeader
{
2513 char ar_name
[16]; /* name of this member */
2514 char ar_date
[12]; /* file mtime */
2515 char ar_uid
[6]; /* owner uid; printed as decimal */
2516 char ar_gid
[6]; /* owner gid; printed as decimal */
2517 char ar_mode
[8]; /* file mode, printed as octal */
2518 char ar_size
[10]; /* file size, printed as decimal */
2519 char ar_fmag
[2]; /* should contain ARFMAG */
2522 static int get_be32(const uint8_t *b
)
2524 return b
[3] | (b
[2] << 8) | (b
[1] << 16) | (b
[0] << 24);
2527 static long get_be64(const uint8_t *b
)
2529 long long ret
= get_be32(b
);
2530 ret
= (ret
<< 32) | (unsigned)get_be32(b
+4);
2534 /* load only the objects which resolve undefined symbols */
2535 static int tcc_load_alacarte(TCCState
*s1
, int fd
, int size
, int entrysize
)
2537 long i
, bound
, nsyms
, sym_index
, off
, ret
;
2539 const char *ar_names
, *p
;
2540 const uint8_t *ar_index
;
2543 data
= tcc_malloc(size
);
2544 if (read(fd
, data
, size
) != size
)
2546 nsyms
= entrysize
== 4 ? get_be32(data
) : get_be64(data
);
2547 ar_index
= data
+ entrysize
;
2548 ar_names
= (char *) ar_index
+ nsyms
* entrysize
;
2552 for(p
= ar_names
, i
= 0; i
< nsyms
; i
++, p
+= strlen(p
)+1) {
2553 sym_index
= find_elf_sym(symtab_section
, p
);
2555 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
2556 if(sym
->st_shndx
== SHN_UNDEF
) {
2557 off
= (entrysize
== 4
2558 ? get_be32(ar_index
+ i
* 4)
2559 : get_be64(ar_index
+ i
* 8))
2560 + sizeof(ArchiveHeader
);
2562 if(tcc_load_object_file(s1
, fd
, off
) < 0) {
2577 /* load a '.a' file */
2578 ST_FUNC
int tcc_load_archive(TCCState
*s1
, int fd
)
2585 unsigned long file_offset
;
2587 /* skip magic which was already checked */
2588 read(fd
, magic
, sizeof(magic
));
2591 len
= read(fd
, &hdr
, sizeof(hdr
));
2594 if (len
!= sizeof(hdr
)) {
2595 tcc_error_noabort("invalid archive");
2598 memcpy(ar_size
, hdr
.ar_size
, sizeof(hdr
.ar_size
));
2599 ar_size
[sizeof(hdr
.ar_size
)] = '\0';
2600 size
= strtol(ar_size
, NULL
, 0);
2601 memcpy(ar_name
, hdr
.ar_name
, sizeof(hdr
.ar_name
));
2602 for(i
= sizeof(hdr
.ar_name
) - 1; i
>= 0; i
--) {
2603 if (ar_name
[i
] != ' ')
2606 ar_name
[i
+ 1] = '\0';
2607 file_offset
= lseek(fd
, 0, SEEK_CUR
);
2609 size
= (size
+ 1) & ~1;
2610 if (!strcmp(ar_name
, "/")) {
2611 /* coff symbol table : we handle it */
2612 if(s1
->alacarte_link
)
2613 return tcc_load_alacarte(s1
, fd
, size
, 4);
2614 } else if (!strcmp(ar_name
, "/SYM64/")) {
2615 if(s1
->alacarte_link
)
2616 return tcc_load_alacarte(s1
, fd
, size
, 8);
2619 if (tcc_object_type(fd
, &ehdr
) == AFF_BINTYPE_REL
) {
2620 if (tcc_load_object_file(s1
, fd
, file_offset
) < 0)
2624 lseek(fd
, file_offset
+ size
, SEEK_SET
);
2629 #ifndef TCC_TARGET_PE
2630 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2631 is referenced by the user (so it should be added as DT_NEEDED in
2632 the generated ELF file) */
2633 ST_FUNC
int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
)
2636 ElfW(Shdr
) *shdr
, *sh
, *sh1
;
2637 int i
, j
, nb_syms
, nb_dts
, sym_bind
, ret
;
2638 ElfW(Sym
) *sym
, *dynsym
;
2639 ElfW(Dyn
) *dt
, *dynamic
;
2640 unsigned char *dynstr
;
2641 const char *name
, *soname
;
2642 DLLReference
*dllref
;
2644 read(fd
, &ehdr
, sizeof(ehdr
));
2646 /* test CPU specific stuff */
2647 if (ehdr
.e_ident
[5] != ELFDATA2LSB
||
2648 ehdr
.e_machine
!= EM_TCC_TARGET
) {
2649 tcc_error_noabort("bad architecture");
2654 shdr
= load_data(fd
, ehdr
.e_shoff
, sizeof(ElfW(Shdr
)) * ehdr
.e_shnum
);
2656 /* load dynamic section and dynamic symbols */
2660 dynsym
= NULL
; /* avoid warning */
2661 dynstr
= NULL
; /* avoid warning */
2662 for(i
= 0, sh
= shdr
; i
< ehdr
.e_shnum
; i
++, sh
++) {
2663 switch(sh
->sh_type
) {
2665 nb_dts
= sh
->sh_size
/ sizeof(ElfW(Dyn
));
2666 dynamic
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
2669 nb_syms
= sh
->sh_size
/ sizeof(ElfW(Sym
));
2670 dynsym
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
2671 sh1
= &shdr
[sh
->sh_link
];
2672 dynstr
= load_data(fd
, sh1
->sh_offset
, sh1
->sh_size
);
2679 /* compute the real library name */
2680 soname
= tcc_basename(filename
);
2682 for(i
= 0, dt
= dynamic
; i
< nb_dts
; i
++, dt
++) {
2683 if (dt
->d_tag
== DT_SONAME
) {
2684 soname
= (char *) dynstr
+ dt
->d_un
.d_val
;
2688 /* if the dll is already loaded, do not load it */
2689 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
2690 dllref
= s1
->loaded_dlls
[i
];
2691 if (!strcmp(soname
, dllref
->name
)) {
2692 /* but update level if needed */
2693 if (level
< dllref
->level
)
2694 dllref
->level
= level
;
2700 /* add the dll and its level */
2701 dllref
= tcc_mallocz(sizeof(DLLReference
) + strlen(soname
));
2702 dllref
->level
= level
;
2703 strcpy(dllref
->name
, soname
);
2704 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, dllref
);
2706 /* add dynamic symbols in dynsym_section */
2707 for(i
= 1, sym
= dynsym
+ 1; i
< nb_syms
; i
++, sym
++) {
2708 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
2709 if (sym_bind
== STB_LOCAL
)
2711 name
= (char *) dynstr
+ sym
->st_name
;
2712 set_elf_sym(s1
->dynsymtab_section
, sym
->st_value
, sym
->st_size
,
2713 sym
->st_info
, sym
->st_other
, sym
->st_shndx
, name
);
2716 /* load all referenced DLLs */
2717 for(i
= 0, dt
= dynamic
; i
< nb_dts
; i
++, dt
++) {
2720 name
= (char *) dynstr
+ dt
->d_un
.d_val
;
2721 for(j
= 0; j
< s1
->nb_loaded_dlls
; j
++) {
2722 dllref
= s1
->loaded_dlls
[j
];
2723 if (!strcmp(name
, dllref
->name
))
2724 goto already_loaded
;
2726 if (tcc_add_dll(s1
, name
, AFF_REFERENCED_DLL
) < 0) {
2727 tcc_error_noabort("referenced dll '%s' not found", name
);
2744 #define LD_TOK_NAME 256
2745 #define LD_TOK_EOF (-1)
2747 /* return next ld script token */
2748 static int ld_next(TCCState
*s1
, char *name
, int name_size
)
2766 file
->buf_ptr
= parse_comment(file
->buf_ptr
);
2767 ch
= file
->buf_ptr
[0];
2780 /* case 'a' ... 'z': */
2807 /* case 'A' ... 'z': */
2841 if (!((ch
>= 'a' && ch
<= 'z') ||
2842 (ch
>= 'A' && ch
<= 'Z') ||
2843 (ch
>= '0' && ch
<= '9') ||
2844 strchr("/.-_+=$:\\,~", ch
)))
2846 if ((q
- name
) < name_size
- 1) {
2865 static int ld_add_file(TCCState
*s1
, const char filename
[])
2867 if (filename
[0] == '/') {
2868 if (CONFIG_SYSROOT
[0] == '\0'
2869 && tcc_add_file_internal(s1
, filename
, AFF_TYPE_BIN
) == 0)
2871 filename
= tcc_basename(filename
);
2873 return tcc_add_dll(s1
, filename
, 0);
2876 static inline int new_undef_syms(void)
2879 ret
= new_undef_sym
;
2884 static int ld_add_file_list(TCCState
*s1
, const char *cmd
, int as_needed
)
2886 char filename
[1024], libname
[1024];
2887 int t
, group
, nblibs
= 0, ret
= 0;
2890 group
= !strcmp(cmd
, "GROUP");
2893 t
= ld_next(s1
, filename
, sizeof(filename
));
2896 t
= ld_next(s1
, filename
, sizeof(filename
));
2899 if (t
== LD_TOK_EOF
) {
2900 tcc_error_noabort("unexpected end of file");
2902 goto lib_parse_error
;
2903 } else if (t
== ')') {
2905 } else if (t
== '-') {
2906 t
= ld_next(s1
, filename
, sizeof(filename
));
2907 if ((t
!= LD_TOK_NAME
) || (filename
[0] != 'l')) {
2908 tcc_error_noabort("library name expected");
2910 goto lib_parse_error
;
2912 pstrcpy(libname
, sizeof libname
, &filename
[1]);
2913 if (s1
->static_link
) {
2914 snprintf(filename
, sizeof filename
, "lib%s.a", libname
);
2916 snprintf(filename
, sizeof filename
, "lib%s.so", libname
);
2918 } else if (t
!= LD_TOK_NAME
) {
2919 tcc_error_noabort("filename expected");
2921 goto lib_parse_error
;
2923 if (!strcmp(filename
, "AS_NEEDED")) {
2924 ret
= ld_add_file_list(s1
, cmd
, 1);
2926 goto lib_parse_error
;
2928 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2930 ret
= ld_add_file(s1
, filename
);
2932 goto lib_parse_error
;
2934 /* Add the filename *and* the libname to avoid future conversions */
2935 dynarray_add(&libs
, &nblibs
, tcc_strdup(filename
));
2936 if (libname
[0] != '\0')
2937 dynarray_add(&libs
, &nblibs
, tcc_strdup(libname
));
2941 t
= ld_next(s1
, filename
, sizeof(filename
));
2943 t
= ld_next(s1
, filename
, sizeof(filename
));
2946 if (group
&& !as_needed
) {
2947 while (new_undef_syms()) {
2950 for (i
= 0; i
< nblibs
; i
++)
2951 ld_add_file(s1
, libs
[i
]);
2955 dynarray_reset(&libs
, &nblibs
);
2959 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2961 ST_FUNC
int tcc_load_ldscript(TCCState
*s1
)
2964 char filename
[1024];
2969 t
= ld_next(s1
, cmd
, sizeof(cmd
));
2970 if (t
== LD_TOK_EOF
)
2972 else if (t
!= LD_TOK_NAME
)
2974 if (!strcmp(cmd
, "INPUT") ||
2975 !strcmp(cmd
, "GROUP")) {
2976 ret
= ld_add_file_list(s1
, cmd
, 0);
2979 } else if (!strcmp(cmd
, "OUTPUT_FORMAT") ||
2980 !strcmp(cmd
, "TARGET")) {
2981 /* ignore some commands */
2982 t
= ld_next(s1
, cmd
, sizeof(cmd
));
2986 t
= ld_next(s1
, filename
, sizeof(filename
));
2987 if (t
== LD_TOK_EOF
) {
2988 tcc_error_noabort("unexpected end of file");
2990 } else if (t
== ')') {
3000 #endif /* !TCC_TARGET_PE */