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
*cur_text_section
; /* current section where function code is generated */
32 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
34 #ifdef CONFIG_TCC_BCHECK
35 /* bound check related sections */
36 ST_DATA Section
*bounds_section
; /* contains global data bound description */
37 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
40 ST_DATA Section
*symtab_section
, *strtab_section
;
42 ST_DATA Section
*stab_section
, *stabstr_section
;
44 /* XXX: avoid static variable */
45 static int new_undef_sym
= 0; /* Is there a new undefined sym since last new_undef_sym() */
47 /* ------------------------------------------------------------------------- */
49 ST_FUNC
void tccelf_new(TCCState
*s
)
52 dynarray_add(&s
->sections
, &s
->nb_sections
, NULL
);
54 /* create standard sections */
55 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
56 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
57 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
59 /* symbols are always generated for linking stage */
60 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
62 ".hashtab", SHF_PRIVATE
);
63 strtab_section
= symtab_section
->link
;
64 s
->symtab
= symtab_section
;
66 /* private symbol table for dynamic symbols */
67 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
69 ".dynhashtab", SHF_PRIVATE
);
70 get_sym_attr(s
, 0, 1);
73 #ifdef CONFIG_TCC_BCHECK
74 ST_FUNC
void tccelf_bounds_new(TCCState
*s
)
76 /* create bounds sections */
77 bounds_section
= new_section(s
, ".bounds",
78 SHT_PROGBITS
, SHF_ALLOC
);
79 lbounds_section
= new_section(s
, ".lbounds",
80 SHT_PROGBITS
, SHF_ALLOC
);
84 ST_FUNC
void tccelf_stab_new(TCCState
*s
)
86 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
87 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
88 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
89 put_elf_str(stabstr_section
, "");
90 stab_section
->link
= stabstr_section
;
92 put_stabs("", 0, 0, 0, 0);
95 static void free_section(Section
*s
)
100 ST_FUNC
void tccelf_delete(TCCState
*s1
)
104 /* free all sections */
105 for(i
= 1; i
< s1
->nb_sections
; i
++)
106 free_section(s1
->sections
[i
]);
107 dynarray_reset(&s1
->sections
, &s1
->nb_sections
);
109 for(i
= 0; i
< s1
->nb_priv_sections
; i
++)
110 free_section(s1
->priv_sections
[i
]);
111 dynarray_reset(&s1
->priv_sections
, &s1
->nb_priv_sections
);
113 /* free any loaded DLLs */
115 for ( i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
116 DLLReference
*ref
= s1
->loaded_dlls
[i
];
119 FreeLibrary((HMODULE
)ref
->handle
);
121 dlclose(ref
->handle
);
125 /* free loaded dlls array */
126 dynarray_reset(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
);
127 tcc_free(s1
->sym_attrs
);
130 ST_FUNC Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
134 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
135 strcpy(sec
->name
, name
);
136 sec
->sh_type
= sh_type
;
137 sec
->sh_flags
= sh_flags
;
145 sec
->sh_addralign
= 4;
148 sec
->sh_addralign
= 1;
151 sec
->sh_addralign
= PTR_SIZE
; /* gcc/pcc default aligment */
155 if (sh_flags
& SHF_PRIVATE
) {
156 dynarray_add(&s1
->priv_sections
, &s1
->nb_priv_sections
, sec
);
158 sec
->sh_num
= s1
->nb_sections
;
159 dynarray_add(&s1
->sections
, &s1
->nb_sections
, sec
);
165 ST_FUNC Section
*new_symtab(TCCState
*s1
,
166 const char *symtab_name
, int sh_type
, int sh_flags
,
167 const char *strtab_name
,
168 const char *hash_name
, int hash_sh_flags
)
170 Section
*symtab
, *strtab
, *hash
;
171 int *ptr
, nb_buckets
;
173 symtab
= new_section(s1
, symtab_name
, sh_type
, sh_flags
);
174 symtab
->sh_entsize
= sizeof(ElfW(Sym
));
175 strtab
= new_section(s1
, strtab_name
, SHT_STRTAB
, sh_flags
);
176 put_elf_str(strtab
, "");
177 symtab
->link
= strtab
;
178 put_elf_sym(symtab
, 0, 0, 0, 0, 0, NULL
);
182 hash
= new_section(s1
, hash_name
, SHT_HASH
, hash_sh_flags
);
183 hash
->sh_entsize
= sizeof(int);
187 ptr
= section_ptr_add(hash
, (2 + nb_buckets
+ 1) * sizeof(int));
190 memset(ptr
+ 2, 0, (nb_buckets
+ 1) * sizeof(int));
194 /* realloc section and set its content to zero */
195 ST_FUNC
void section_realloc(Section
*sec
, unsigned long new_size
)
200 size
= sec
->data_allocated
;
203 while (size
< new_size
)
205 data
= tcc_realloc(sec
->data
, size
);
206 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
208 sec
->data_allocated
= size
;
211 /* reserve at least 'size' bytes in section 'sec' from
213 ST_FUNC
void *section_ptr_add(Section
*sec
, addr_t size
)
215 size_t offset
, offset1
;
217 offset
= sec
->data_offset
;
218 offset1
= offset
+ size
;
219 if (offset1
> sec
->data_allocated
)
220 section_realloc(sec
, offset1
);
221 sec
->data_offset
= offset1
;
222 return sec
->data
+ offset
;
225 /* reserve at least 'size' bytes from section start */
226 ST_FUNC
void section_reserve(Section
*sec
, unsigned long size
)
228 if (size
> sec
->data_allocated
)
229 section_realloc(sec
, size
);
230 if (size
> sec
->data_offset
)
231 sec
->data_offset
= size
;
234 /* return a reference to a section, and create it if it does not
236 ST_FUNC Section
*find_section(TCCState
*s1
, const char *name
)
240 for(i
= 1; i
< s1
->nb_sections
; i
++) {
241 sec
= s1
->sections
[i
];
242 if (!strcmp(name
, sec
->name
))
245 /* sections are created as PROGBITS */
246 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
249 /* ------------------------------------------------------------------------- */
251 ST_FUNC
int put_elf_str(Section
*s
, const char *sym
)
256 len
= strlen(sym
) + 1;
257 offset
= s
->data_offset
;
258 ptr
= section_ptr_add(s
, len
);
259 memcpy(ptr
, sym
, len
);
263 /* elf symbol hashing function */
264 static unsigned long elf_hash(const unsigned char *name
)
266 unsigned long h
= 0, g
;
269 h
= (h
<< 4) + *name
++;
278 /* rebuild hash table of section s */
279 /* NOTE: we do factorize the hash table code to go faster */
280 static void rebuild_hash(Section
*s
, unsigned int nb_buckets
)
283 int *ptr
, *hash
, nb_syms
, sym_index
, h
;
284 unsigned char *strtab
;
286 strtab
= s
->link
->data
;
287 nb_syms
= s
->data_offset
/ sizeof(ElfW(Sym
));
289 s
->hash
->data_offset
= 0;
290 ptr
= section_ptr_add(s
->hash
, (2 + nb_buckets
+ nb_syms
) * sizeof(int));
295 memset(hash
, 0, (nb_buckets
+ 1) * sizeof(int));
296 ptr
+= nb_buckets
+ 1;
298 sym
= (ElfW(Sym
) *)s
->data
+ 1;
299 for(sym_index
= 1; sym_index
< nb_syms
; sym_index
++) {
300 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
301 h
= elf_hash(strtab
+ sym
->st_name
) % nb_buckets
;
312 /* return the symbol number */
313 ST_FUNC
int put_elf_sym(Section
*s
, addr_t value
, unsigned long size
,
314 int info
, int other
, int shndx
, const char *name
)
316 int name_offset
, sym_index
;
321 sym
= section_ptr_add(s
, sizeof(ElfW(Sym
)));
323 name_offset
= put_elf_str(s
->link
, name
);
326 /* XXX: endianness */
327 sym
->st_name
= name_offset
;
328 sym
->st_value
= value
;
331 sym
->st_other
= other
;
332 sym
->st_shndx
= shndx
;
333 sym_index
= sym
- (ElfW(Sym
) *)s
->data
;
337 ptr
= section_ptr_add(hs
, sizeof(int));
338 base
= (int *)hs
->data
;
339 /* only add global or weak symbols */
340 if (ELFW(ST_BIND
)(info
) != STB_LOCAL
) {
341 /* add another hashing entry */
343 h
= elf_hash((unsigned char *) name
) % nbuckets
;
345 base
[2 + h
] = sym_index
;
347 /* we resize the hash table */
348 hs
->nb_hashed_syms
++;
349 if (hs
->nb_hashed_syms
> 2 * nbuckets
) {
350 rebuild_hash(s
, 2 * nbuckets
);
360 /* find global ELF symbol 'name' and return its index. Return 0 if not
362 ST_FUNC
int find_elf_sym(Section
*s
, const char *name
)
366 int nbuckets
, sym_index
, h
;
372 nbuckets
= ((int *)hs
->data
)[0];
373 h
= elf_hash((unsigned char *) name
) % nbuckets
;
374 sym_index
= ((int *)hs
->data
)[2 + h
];
375 while (sym_index
!= 0) {
376 sym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
377 name1
= (char *) s
->link
->data
+ sym
->st_name
;
378 if (!strcmp(name
, name1
))
380 sym_index
= ((int *)hs
->data
)[2 + nbuckets
+ sym_index
];
385 /* return elf symbol value, signal error if 'err' is nonzero */
386 ST_FUNC addr_t
get_elf_sym_addr(TCCState
*s
, const char *name
, int err
)
391 sym_index
= find_elf_sym(s
->symtab
, name
);
392 sym
= &((ElfW(Sym
) *)s
->symtab
->data
)[sym_index
];
393 if (!sym_index
|| sym
->st_shndx
== SHN_UNDEF
) {
395 tcc_error("%s not defined", name
);
398 return sym
->st_value
;
401 /* return elf symbol value */
402 LIBTCCAPI
void *tcc_get_symbol(TCCState
*s
, const char *name
)
404 return (void*)(uintptr_t)get_elf_sym_addr(s
, name
, 0);
407 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
408 /* return elf symbol value or error */
409 ST_FUNC
void* tcc_get_symbol_err(TCCState
*s
, const char *name
)
411 return (void*)(uintptr_t)get_elf_sym_addr(s
, name
, 1);
415 /* add an elf symbol : check if it is already defined and patch
416 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
417 ST_FUNC
int set_elf_sym(Section
*s
, addr_t value
, unsigned long size
,
418 int info
, int other
, int shndx
, const char *name
)
421 int sym_bind
, sym_index
, sym_type
, esym_bind
;
422 unsigned char sym_vis
, esym_vis
, new_vis
;
424 sym_bind
= ELFW(ST_BIND
)(info
);
425 sym_type
= ELFW(ST_TYPE
)(info
);
426 sym_vis
= ELFW(ST_VISIBILITY
)(other
);
428 sym_index
= find_elf_sym(s
, name
);
429 esym
= &((ElfW(Sym
) *)s
->data
)[sym_index
];
430 if (sym_index
&& esym
->st_value
== value
&& esym
->st_size
== size
431 && esym
->st_info
== info
&& esym
->st_other
== other
432 && esym
->st_shndx
== shndx
)
435 if (sym_bind
!= STB_LOCAL
) {
436 /* we search global or weak symbols */
439 if (esym
->st_shndx
!= SHN_UNDEF
) {
440 esym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
441 /* propagate the most constraining visibility */
442 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
443 esym_vis
= ELFW(ST_VISIBILITY
)(esym
->st_other
);
444 if (esym_vis
== STV_DEFAULT
) {
446 } else if (sym_vis
== STV_DEFAULT
) {
449 new_vis
= (esym_vis
< sym_vis
) ? esym_vis
: sym_vis
;
451 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
453 other
= esym
->st_other
; /* in case we have to patch esym */
454 if (shndx
== SHN_UNDEF
) {
455 /* ignore adding of undefined symbol if the
456 corresponding symbol is already defined */
457 } else if (sym_bind
== STB_GLOBAL
&& esym_bind
== STB_WEAK
) {
458 /* global overrides weak, so patch */
460 } else if (sym_bind
== STB_WEAK
&& esym_bind
== STB_GLOBAL
) {
461 /* weak is ignored if already global */
462 } else if (sym_bind
== STB_WEAK
&& esym_bind
== STB_WEAK
) {
463 /* keep first-found weak definition, ignore subsequents */
464 } else if (sym_vis
== STV_HIDDEN
|| sym_vis
== STV_INTERNAL
) {
465 /* ignore hidden symbols after */
466 } else if ((esym
->st_shndx
== SHN_COMMON
467 || esym
->st_shndx
== bss_section
->sh_num
)
468 && (shndx
< SHN_LORESERVE
469 && shndx
!= bss_section
->sh_num
)) {
470 /* data symbol gets precedence over common/bss */
472 } else if (shndx
== SHN_COMMON
|| shndx
== bss_section
->sh_num
) {
473 /* data symbol keeps precedence over common/bss */
474 } else if (s
== tcc_state
->dynsymtab_section
) {
475 /* we accept that two DLL define the same symbol */
478 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
479 sym_bind
, shndx
, new_vis
, esym_bind
, esym
->st_shndx
, esym_vis
);
481 tcc_error_noabort("'%s' defined twice", name
);
485 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
486 esym
->st_shndx
= shndx
;
488 esym
->st_value
= value
;
489 esym
->st_size
= size
;
490 esym
->st_other
= other
;
494 sym_index
= put_elf_sym(s
, value
, size
,
495 ELFW(ST_INFO
)(sym_bind
, sym_type
), other
,
502 ST_FUNC
void put_elf_reloca(Section
*symtab
, Section
*s
, unsigned long offset
,
503 int type
, int symbol
, addr_t addend
)
511 /* if no relocation section, create it */
512 snprintf(buf
, sizeof(buf
), REL_SECTION_FMT
, s
->name
);
513 /* if the symtab is allocated, then we consider the relocation
515 sr
= new_section(tcc_state
, buf
, SHT_RELX
, symtab
->sh_flags
);
516 sr
->sh_entsize
= sizeof(ElfW_Rel
);
518 sr
->sh_info
= s
->sh_num
;
521 rel
= section_ptr_add(sr
, sizeof(ElfW_Rel
));
522 rel
->r_offset
= offset
;
523 rel
->r_info
= ELFW(R_INFO
)(symbol
, type
);
524 #if SHT_RELX == SHT_RELA
525 rel
->r_addend
= addend
;
528 tcc_error("non-zero addend on REL architecture");
532 ST_FUNC
void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
533 int type
, int symbol
)
535 put_elf_reloca(symtab
, s
, offset
, type
, symbol
, 0);
538 /* Remove relocations for section S->reloc starting at oldrelocoffset
539 that are to the same place, retaining the last of them. As side effect
540 the relocations are sorted. Possibly reduces the number of relocs. */
541 ST_FUNC
void squeeze_multi_relocs(Section
*s
, size_t oldrelocoffset
)
543 Section
*sr
= s
->reloc
;
548 if (oldrelocoffset
+ sizeof(*r
) >= sr
->data_offset
)
550 /* The relocs we're dealing with are the result of initializer parsing.
551 So they will be mostly in order and there aren't many of them.
552 Secondly we need a stable sort (which qsort isn't). We use
553 a simple insertion sort. */
554 for (a
= oldrelocoffset
+ sizeof(*r
); a
< sr
->data_offset
; a
+= sizeof(*r
)) {
555 ssize_t i
= a
- sizeof(*r
);
556 addr
= ((ElfW_Rel
*)(sr
->data
+ a
))->r_offset
;
557 for (; i
>= (ssize_t
)oldrelocoffset
&&
558 ((ElfW_Rel
*)(sr
->data
+ i
))->r_offset
> addr
; i
-= sizeof(*r
)) {
559 ElfW_Rel tmp
= *(ElfW_Rel
*)(sr
->data
+ a
);
560 *(ElfW_Rel
*)(sr
->data
+ a
) = *(ElfW_Rel
*)(sr
->data
+ i
);
561 *(ElfW_Rel
*)(sr
->data
+ i
) = tmp
;
565 r
= (ElfW_Rel
*)(sr
->data
+ oldrelocoffset
);
567 for (; r
< (ElfW_Rel
*)(sr
->data
+ sr
->data_offset
); r
++) {
568 if (dest
->r_offset
!= r
->r_offset
)
572 sr
->data_offset
= (unsigned char*)dest
- sr
->data
+ sizeof(*r
);
575 /* put stab debug information */
577 ST_FUNC
void put_stabs(const char *str
, int type
, int other
, int desc
,
582 sym
= section_ptr_add(stab_section
, sizeof(Stab_Sym
));
584 sym
->n_strx
= put_elf_str(stabstr_section
, str
);
589 sym
->n_other
= other
;
591 sym
->n_value
= value
;
594 ST_FUNC
void put_stabs_r(const char *str
, int type
, int other
, int desc
,
595 unsigned long value
, Section
*sec
, int sym_index
)
597 put_stabs(str
, type
, other
, desc
, value
);
598 put_elf_reloc(symtab_section
, stab_section
,
599 stab_section
->data_offset
- sizeof(unsigned int),
600 R_DATA_32
, sym_index
);
603 ST_FUNC
void put_stabn(int type
, int other
, int desc
, int value
)
605 put_stabs(NULL
, type
, other
, desc
, value
);
608 ST_FUNC
void put_stabd(int type
, int other
, int desc
)
610 put_stabs(NULL
, type
, other
, desc
, 0);
613 ST_FUNC
struct sym_attr
*get_sym_attr(TCCState
*s1
, int index
, int alloc
)
616 struct sym_attr
*tab
;
618 if (index
>= s1
->nb_sym_attrs
) {
620 return s1
->sym_attrs
;
621 /* find immediately bigger power of 2 and reallocate array */
625 tab
= tcc_realloc(s1
->sym_attrs
, n
* sizeof(*s1
->sym_attrs
));
627 memset(s1
->sym_attrs
+ s1
->nb_sym_attrs
, 0,
628 (n
- s1
->nb_sym_attrs
) * sizeof(*s1
->sym_attrs
));
629 s1
->nb_sym_attrs
= n
;
631 return &s1
->sym_attrs
[index
];
634 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
635 using variable <elem> */
636 #define for_each_elem(sec, startoff, elem, type) \
637 for (elem = (type *) sec->data + startoff; \
638 elem < (type *) (sec->data + sec->data_offset); elem++)
640 /* In an ELF file symbol table, the local symbols must appear below
641 the global and weak ones. Since TCC cannot sort it while generating
642 the code, we must do it after. All the relocation tables are also
643 modified to take into account the symbol table sorting */
644 static void sort_syms(TCCState
*s1
, Section
*s
)
646 int *old_to_new_syms
;
654 nb_syms
= s
->data_offset
/ sizeof(ElfW(Sym
));
655 new_syms
= tcc_malloc(nb_syms
* sizeof(ElfW(Sym
)));
656 old_to_new_syms
= tcc_malloc(nb_syms
* sizeof(int));
658 /* first pass for local symbols */
659 p
= (ElfW(Sym
) *)s
->data
;
661 for(i
= 0; i
< nb_syms
; i
++) {
662 if (ELFW(ST_BIND
)(p
->st_info
) == STB_LOCAL
) {
663 old_to_new_syms
[i
] = q
- new_syms
;
668 /* save the number of local symbols in section header */
669 s
->sh_info
= q
- new_syms
;
671 /* then second pass for non local symbols */
672 p
= (ElfW(Sym
) *)s
->data
;
673 for(i
= 0; i
< nb_syms
; i
++) {
674 if (ELFW(ST_BIND
)(p
->st_info
) != STB_LOCAL
) {
675 old_to_new_syms
[i
] = q
- new_syms
;
681 /* we copy the new symbols to the old */
682 memcpy(s
->data
, new_syms
, nb_syms
* sizeof(ElfW(Sym
)));
685 /* now we modify all the relocations */
686 for(i
= 1; i
< s1
->nb_sections
; i
++) {
687 sr
= s1
->sections
[i
];
688 if (sr
->sh_type
== SHT_RELX
&& sr
->link
== s
) {
689 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
690 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
691 type
= ELFW(R_TYPE
)(rel
->r_info
);
692 sym_index
= old_to_new_syms
[sym_index
];
693 rel
->r_info
= ELFW(R_INFO
)(sym_index
, type
);
698 tcc_free(old_to_new_syms
);
701 /* relocate common symbols in the .bss section */
702 ST_FUNC
void relocate_common_syms(void)
705 unsigned long offset
, align
;
707 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
708 if (sym
->st_shndx
== SHN_COMMON
) {
710 align
= sym
->st_value
;
711 offset
= bss_section
->data_offset
;
712 offset
= (offset
+ align
- 1) & -align
;
713 sym
->st_value
= offset
;
714 sym
->st_shndx
= bss_section
->sh_num
;
715 offset
+= sym
->st_size
;
716 bss_section
->data_offset
= offset
;
721 /* relocate symbol table, resolve undefined symbols if do_resolve is
722 true and output error if undefined symbol. */
723 ST_FUNC
void relocate_syms(TCCState
*s1
, Section
*symtab
, int do_resolve
)
726 int sym_bind
, sh_num
;
729 for_each_elem(symtab
, 1, sym
, ElfW(Sym
)) {
730 sh_num
= sym
->st_shndx
;
731 if (sh_num
== SHN_UNDEF
) {
732 name
= (char *) strtab_section
->data
+ sym
->st_name
;
733 /* Use ld.so to resolve symbol for us (for tcc -run) */
735 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
736 void *addr
= dlsym(RTLD_DEFAULT
, name
);
738 sym
->st_value
= (addr_t
) addr
;
740 printf ("relocate_sym: %s -> 0x%lx\n", name
, sym
->st_value
);
745 /* if dynamic symbol exist, it will be used in relocate_section */
746 } else if (s1
->dynsym
&& find_elf_sym(s1
->dynsym
, name
))
748 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
750 if (!strcmp(name
, "_fp_hw"))
752 /* only weak symbols are accepted to be undefined. Their
754 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
755 if (sym_bind
== STB_WEAK
)
758 tcc_error_noabort("undefined symbol '%s'", name
);
759 } else if (sh_num
< SHN_LORESERVE
) {
760 /* add section base */
761 sym
->st_value
+= s1
->sections
[sym
->st_shndx
]->sh_addr
;
767 /* relocate a given section (CPU dependent) by applying the relocations
768 in the associated relocation section */
769 ST_FUNC
void relocate_section(TCCState
*s1
, Section
*s
)
771 Section
*sr
= s
->reloc
;
780 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
781 ptr
= s
->data
+ rel
->r_offset
;
782 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
783 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
784 type
= ELFW(R_TYPE
)(rel
->r_info
);
786 #if SHT_RELX == SHT_RELA
787 tgt
+= rel
->r_addend
;
789 addr
= s
->sh_addr
+ rel
->r_offset
;
790 relocate(s1
, rel
, type
, ptr
, addr
, tgt
);
792 /* if the relocation is allocated, we change its symbol table */
793 if (sr
->sh_flags
& SHF_ALLOC
)
794 sr
->link
= s1
->dynsym
;
797 /* relocate relocation table in 'sr' */
798 static void relocate_rel(TCCState
*s1
, Section
*sr
)
803 s
= s1
->sections
[sr
->sh_info
];
804 for_each_elem(sr
, 0, rel
, ElfW_Rel
)
805 rel
->r_offset
+= s
->sh_addr
;
808 /* count the number of dynamic relocations so that we can reserve
810 static int prepare_dynamic_rel(TCCState
*s1
, Section
*sr
)
813 int sym_index
, type
, count
;
816 for_each_elem(sr
, 0, rel
, ElfW_Rel
) {
817 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
818 type
= ELFW(R_TYPE
)(rel
->r_info
);
820 #if defined(TCC_TARGET_I386)
822 #elif defined(TCC_TARGET_X86_64)
829 #if defined(TCC_TARGET_I386)
831 #elif defined(TCC_TARGET_X86_64)
834 if (get_sym_attr(s1
, sym_index
, 0)->dyn_index
)
842 /* allocate the section */
843 sr
->sh_flags
|= SHF_ALLOC
;
844 sr
->sh_size
= count
* sizeof(ElfW_Rel
);
849 static void build_got(TCCState
*s1
)
851 /* if no got, then create it */
852 s1
->got
= new_section(s1
, ".got", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
853 s1
->got
->sh_entsize
= 4;
854 set_elf_sym(symtab_section
, 0, 4, ELFW(ST_INFO
)(STB_GLOBAL
, STT_OBJECT
),
855 0, s1
->got
->sh_num
, "_GLOBAL_OFFSET_TABLE_");
856 /* keep space for _DYNAMIC pointer and two dummy got entries */
857 section_ptr_add(s1
->got
, 3 * PTR_SIZE
);
860 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
861 in s1->symtab. When creating the dynamic symbol table entry for the GOT
862 relocation, use 'size' and 'info' for the corresponding symbol metadata.
863 Returns the offset of the GOT or (if any) PLT entry. */
864 static struct sym_attr
* put_got_entry(TCCState
*s1
, int dyn_reloc_type
,
865 int reloc_type
, unsigned long size
,
866 int info
, int sym_index
)
871 struct sym_attr
*attr
;
876 need_plt_entry
= (dyn_reloc_type
== R_JMP_SLOT
);
877 attr
= get_sym_attr(s1
, sym_index
, 1);
879 /* In case a function is both called and its address taken 2 GOT entries
880 are created, one for taking the address (GOT) and the other for the PLT
882 if (need_plt_entry
? attr
->plt_offset
: attr
->got_offset
)
885 /* create the GOT entry */
886 got_offset
= s1
->got
->data_offset
;
887 section_ptr_add(s1
->got
, PTR_SIZE
);
889 /* Create the GOT relocation that will insert the address of the object or
890 function of interest in the GOT entry. This is a static relocation for
891 memory output (dlsym will give us the address of symbols) and dynamic
892 relocation otherwise (executable and DLLs). The relocation should be
893 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
894 associated to a PLT entry) but is currently done at load time for an
897 sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
898 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
901 if (0 == attr
->dyn_index
)
902 attr
->dyn_index
= set_elf_sym(s1
->dynsym
, sym
->st_value
, size
,
903 info
, 0, sym
->st_shndx
, name
);
904 put_elf_reloc(s1
->dynsym
, s1
->got
, got_offset
, dyn_reloc_type
,
907 put_elf_reloc(symtab_section
, s1
->got
, got_offset
, dyn_reloc_type
,
911 if (need_plt_entry
) {
913 s1
->plt
= new_section(s1
, ".plt", SHT_PROGBITS
,
914 SHF_ALLOC
| SHF_EXECINSTR
);
915 s1
->plt
->sh_entsize
= 4;
918 attr
->plt_offset
= create_plt_entry(s1
, got_offset
, attr
);
920 /* create a symbol 'sym@plt' for the PLT jump vector */
922 if (len
> sizeof plt_name
- 5)
923 len
= sizeof plt_name
- 5;
924 memcpy(plt_name
, name
, len
);
925 strcpy(plt_name
+ len
, "@plt");
926 attr
->plt_sym
= put_elf_sym(s1
->symtab
, attr
->plt_offset
, sym
->st_size
,
927 ELFW(ST_INFO
)(STB_GLOBAL
, STT_FUNC
), 0, s1
->plt
->sh_num
, plt_name
);
930 attr
->got_offset
= got_offset
;
936 /* build GOT and PLT entries */
937 ST_FUNC
void build_got_entries(TCCState
*s1
)
942 int i
, type
, gotplt_entry
, reloc_type
, sym_index
;
943 struct sym_attr
*attr
;
945 for(i
= 1; i
< s1
->nb_sections
; i
++) {
947 if (s
->sh_type
!= SHT_RELX
)
949 /* no need to handle got relocations */
950 if (s
->link
!= symtab_section
)
952 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
953 type
= ELFW(R_TYPE
)(rel
->r_info
);
954 gotplt_entry
= gotplt_entry_type(type
);
955 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
956 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
958 if (gotplt_entry
== NO_GOTPLT_ENTRY
) {
962 /* Automatically create PLT/GOT [entry] if it is an undefined
963 reference (resolved at runtime), or the symbol is absolute,
964 probably created by tcc_add_symbol, and thus on 64-bit
965 targets might be too far from application code. */
966 if (gotplt_entry
== AUTO_GOTPLT_ENTRY
) {
967 if (sym
->st_shndx
== SHN_UNDEF
) {
970 if (s1
->output_type
== TCC_OUTPUT_DLL
&& ! PCRELATIVE_DLLPLT
)
972 /* Relocations for UNDEF symbols would normally need
973 to be transferred into the executable or shared object.
974 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
975 But TCC doesn't do that (at least for exes), so we
976 need to resolve all such relocs locally. And that
977 means PLT slots for functions in DLLs and COPY relocs for
978 data symbols. COPY relocs were generated in
979 bind_exe_dynsyms (and the symbol adjusted to be defined),
980 and for functions we were generated a dynamic symbol
983 /* dynsym isn't set for -run :-/ */
984 dynindex
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
985 esym
= (ElfW(Sym
) *)s1
->dynsym
->data
+ dynindex
;
987 && (ELFW(ST_TYPE
)(esym
->st_info
) == STT_FUNC
988 || (ELFW(ST_TYPE
)(esym
->st_info
) == STT_NOTYPE
989 && ELFW(ST_TYPE
)(sym
->st_info
) == STT_FUNC
)))
992 } else if (!(sym
->st_shndx
== SHN_ABS
993 #ifndef TCC_TARGET_ARM
1000 #ifdef TCC_TARGET_X86_64
1001 if (type
== R_X86_64_PLT32
&&
1002 ELFW(ST_VISIBILITY
)(sym
->st_other
) != STV_DEFAULT
) {
1003 rel
->r_info
= ELFW(R_INFO
)(sym_index
, R_X86_64_PC32
);
1007 if (code_reloc(type
)) {
1009 reloc_type
= R_JMP_SLOT
;
1011 reloc_type
= R_GLOB_DAT
;
1016 if (gotplt_entry
== BUILD_GOT_ONLY
)
1019 attr
= put_got_entry(s1
, reloc_type
, type
, sym
->st_size
, sym
->st_info
,
1022 if (reloc_type
== R_JMP_SLOT
)
1023 rel
->r_info
= ELFW(R_INFO
)(attr
->plt_sym
, type
);
1028 /* put dynamic tag */
1029 static void put_dt(Section
*dynamic
, int dt
, addr_t val
)
1032 dyn
= section_ptr_add(dynamic
, sizeof(ElfW(Dyn
)));
1034 dyn
->d_un
.d_val
= val
;
1037 #ifndef TCC_TARGET_PE
1038 static void add_init_array_defines(TCCState
*s1
, const char *section_name
)
1042 char sym_start
[1024];
1045 snprintf(sym_start
, sizeof(sym_start
), "__%s_start", section_name
+ 1);
1046 snprintf(sym_end
, sizeof(sym_end
), "__%s_end", section_name
+ 1);
1048 s
= find_section(s1
, section_name
);
1053 end_offset
= s
->data_offset
;
1056 set_elf_sym(symtab_section
,
1058 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1059 s
->sh_num
, sym_start
);
1060 set_elf_sym(symtab_section
,
1062 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1063 s
->sh_num
, sym_end
);
1067 static int tcc_add_support(TCCState
*s1
, const char *filename
)
1070 snprintf(buf
, sizeof(buf
), "%s/%s", s1
->tcc_lib_path
, filename
);
1071 return tcc_add_file(s1
, buf
);
1074 ST_FUNC
void tcc_add_bcheck(TCCState
*s1
)
1076 #ifdef CONFIG_TCC_BCHECK
1080 if (0 == s1
->do_bounds_check
)
1082 /* XXX: add an object file to do that */
1083 ptr
= section_ptr_add(bounds_section
, sizeof(*ptr
));
1085 set_elf_sym(symtab_section
, 0, 0,
1086 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1087 bounds_section
->sh_num
, "__bounds_start");
1088 /* pull bcheck.o from libtcc1.a */
1089 sym_index
= set_elf_sym(symtab_section
, 0, 0,
1090 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1091 SHN_UNDEF
, "__bound_init");
1092 if (s1
->output_type
!= TCC_OUTPUT_MEMORY
) {
1093 /* add 'call __bound_init()' in .init section */
1094 Section
*init_section
= find_section(s1
, ".init");
1095 unsigned char *pinit
= section_ptr_add(init_section
, 5);
1097 write32le(pinit
+ 1, -4);
1098 put_elf_reloc(symtab_section
, init_section
,
1099 init_section
->data_offset
- 4, R_386_PC32
, sym_index
);
1100 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1105 /* add tcc runtime libraries */
1106 ST_FUNC
void tcc_add_runtime(TCCState
*s1
)
1109 tcc_add_pragma_libs(s1
);
1111 if (!s1
->nostdlib
) {
1112 tcc_add_library_err(s1
, "c");
1114 if (!s1
->static_link
) {
1115 if (TCC_LIBGCC
[0] == '/')
1116 tcc_add_file(s1
, TCC_LIBGCC
);
1118 tcc_add_dll(s1
, TCC_LIBGCC
, 0);
1121 tcc_add_support(s1
, TCC_LIBTCC1
);
1122 /* add crt end if not memory output */
1123 if (s1
->output_type
!= TCC_OUTPUT_MEMORY
)
1124 tcc_add_crt(s1
, "crtn.o");
1128 /* add various standard linker symbols (must be done after the
1129 sections are filled (for example after allocating common
1131 ST_FUNC
void tcc_add_linker_symbols(TCCState
*s1
)
1137 set_elf_sym(symtab_section
,
1138 text_section
->data_offset
, 0,
1139 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1140 text_section
->sh_num
, "_etext");
1141 set_elf_sym(symtab_section
,
1142 data_section
->data_offset
, 0,
1143 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1144 data_section
->sh_num
, "_edata");
1145 set_elf_sym(symtab_section
,
1146 bss_section
->data_offset
, 0,
1147 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1148 bss_section
->sh_num
, "_end");
1149 #ifndef TCC_TARGET_PE
1150 /* horrible new standard ldscript defines */
1151 add_init_array_defines(s1
, ".preinit_array");
1152 add_init_array_defines(s1
, ".init_array");
1153 add_init_array_defines(s1
, ".fini_array");
1156 /* add start and stop symbols for sections whose name can be
1158 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1159 s
= s1
->sections
[i
];
1160 if (s
->sh_type
== SHT_PROGBITS
&&
1161 (s
->sh_flags
& SHF_ALLOC
)) {
1165 /* check if section name can be expressed in C */
1171 if (!isid(ch
) && !isnum(ch
))
1175 snprintf(buf
, sizeof(buf
), "__start_%s", s
->name
);
1176 set_elf_sym(symtab_section
,
1178 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1180 snprintf(buf
, sizeof(buf
), "__stop_%s", s
->name
);
1181 set_elf_sym(symtab_section
,
1183 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1190 static void tcc_output_binary(TCCState
*s1
, FILE *f
,
1191 const int *sec_order
)
1194 int i
, offset
, size
;
1197 for(i
=1;i
<s1
->nb_sections
;i
++) {
1198 s
= s1
->sections
[sec_order
[i
]];
1199 if (s
->sh_type
!= SHT_NOBITS
&&
1200 (s
->sh_flags
& SHF_ALLOC
)) {
1201 while (offset
< s
->sh_offset
) {
1206 fwrite(s
->data
, 1, size
, f
);
1212 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1214 #define EXTRA_RELITEMS 14
1217 #define EXTRA_RELITEMS 9
1220 ST_FUNC
void fill_got_entry(TCCState
*s1
, ElfW_Rel
*rel
)
1222 int sym_index
= ELFW(R_SYM
) (rel
->r_info
);
1223 ElfW(Sym
) *sym
= &((ElfW(Sym
) *) symtab_section
->data
)[sym_index
];
1224 struct sym_attr
*attr
= get_sym_attr(s1
, sym_index
, 0);
1225 unsigned offset
= attr
->got_offset
;
1229 section_reserve(s1
->got
, offset
+ PTR_SIZE
);
1230 #ifdef TCC_TARGET_X86_64
1231 write64le(s1
->got
->data
+ offset
, sym
->st_value
);
1233 write32le(s1
->got
->data
+ offset
, sym
->st_value
);
1237 /* Perform relocation to GOT or PLT entries */
1238 ST_FUNC
void fill_got(TCCState
*s1
)
1244 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1245 s
= s1
->sections
[i
];
1246 if (s
->sh_type
!= SHT_RELX
)
1248 /* no need to handle got relocations */
1249 if (s
->link
!= symtab_section
)
1251 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
1252 switch (ELFW(R_TYPE
) (rel
->r_info
)) {
1253 case R_X86_64_GOT32
:
1254 case R_X86_64_GOTPCREL
:
1255 case R_X86_64_GOTPCRELX
:
1256 case R_X86_64_REX_GOTPCRELX
:
1257 case R_X86_64_PLT32
:
1258 fill_got_entry(s1
, rel
);
1265 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1266 in shared libraries and export non local defined symbols to shared libraries
1267 if -rdynamic switch was given on command line */
1268 static void bind_exe_dynsyms(TCCState
*s1
)
1271 int sym_index
, index
;
1272 ElfW(Sym
) *sym
, *esym
;
1275 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1276 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1277 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1278 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1279 if (sym
->st_shndx
== SHN_UNDEF
) {
1280 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1281 sym_index
= find_elf_sym(s1
->dynsymtab_section
, name
);
1283 esym
= &((ElfW(Sym
) *)s1
->dynsymtab_section
->data
)[sym_index
];
1284 type
= ELFW(ST_TYPE
)(esym
->st_info
);
1285 if ((type
== STT_FUNC
) || (type
== STT_GNU_IFUNC
)) {
1286 /* Indirect functions shall have STT_FUNC type in executable
1287 * dynsym section. Indeed, a dlsym call following a lazy
1288 * resolution would pick the symbol value from the
1289 * executable dynsym entry which would contain the address
1290 * of the function wanted by the caller of dlsym instead of
1291 * the address of the function that would return that
1294 = put_elf_sym(s1
->dynsym
, 0, esym
->st_size
,
1295 ELFW(ST_INFO
)(STB_GLOBAL
,STT_FUNC
), 0, 0,
1297 int index
= sym
- (ElfW(Sym
) *) symtab_section
->data
;
1298 get_sym_attr(s1
, index
, 1)->dyn_index
= dynindex
;
1299 } else if (type
== STT_OBJECT
) {
1300 unsigned long offset
;
1302 offset
= bss_section
->data_offset
;
1303 /* XXX: which alignment ? */
1304 offset
= (offset
+ 16 - 1) & -16;
1305 set_elf_sym (s1
->symtab
, offset
, esym
->st_size
,
1306 esym
->st_info
, 0, bss_section
->sh_num
, name
);
1307 index
= put_elf_sym(s1
->dynsym
, offset
, esym
->st_size
,
1308 esym
->st_info
, 0, bss_section
->sh_num
,
1311 /* Ensure R_COPY works for weak symbol aliases */
1312 if (ELFW(ST_BIND
)(esym
->st_info
) == STB_WEAK
) {
1313 for_each_elem(s1
->dynsymtab_section
, 1, dynsym
, ElfW(Sym
)) {
1314 if ((dynsym
->st_value
== esym
->st_value
)
1315 && (ELFW(ST_BIND
)(dynsym
->st_info
) == STB_GLOBAL
)) {
1316 char *dynname
= (char *) s1
->dynsymtab_section
->link
->data
1318 put_elf_sym(s1
->dynsym
, offset
, dynsym
->st_size
,
1320 bss_section
->sh_num
, dynname
);
1326 put_elf_reloc(s1
->dynsym
, bss_section
,
1327 offset
, R_COPY
, index
);
1328 offset
+= esym
->st_size
;
1329 bss_section
->data_offset
= offset
;
1332 /* STB_WEAK undefined symbols are accepted */
1333 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1334 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
||
1335 !strcmp(name
, "_fp_hw")) {
1337 tcc_error_noabort("undefined symbol '%s'", name
);
1340 } else if (s1
->rdynamic
&& ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1341 /* if -rdynamic option, then export all non local symbols */
1342 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1343 set_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
, sym
->st_info
,
1344 0, sym
->st_shndx
, name
);
1349 /* Bind symbols of libraries: export all non local symbols of executable that
1350 are referenced by shared libraries. The reason is that the dynamic loader
1351 search symbol first in executable and then in libraries. Therefore a
1352 reference to a symbol already defined by a library can still be resolved by
1353 a symbol in the executable. */
1354 static void bind_libs_dynsyms(TCCState
*s1
)
1358 ElfW(Sym
) *sym
, *esym
;
1360 for_each_elem(s1
->dynsymtab_section
, 1, esym
, ElfW(Sym
)) {
1361 name
= (char *) s1
->dynsymtab_section
->link
->data
+ esym
->st_name
;
1362 sym_index
= find_elf_sym(symtab_section
, name
);
1363 /* XXX: avoid adding a symbol if already present because of
1365 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1366 if (sym_index
&& sym
->st_shndx
!= SHN_UNDEF
)
1367 set_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
, sym
->st_info
,
1368 0, sym
->st_shndx
, name
);
1369 else if (esym
->st_shndx
== SHN_UNDEF
) {
1370 /* weak symbols can stay undefined */
1371 if (ELFW(ST_BIND
)(esym
->st_info
) != STB_WEAK
)
1372 tcc_warning("undefined dynamic symbol '%s'", name
);
1377 /* Export all non local symbols. This is used by shared libraries so that the
1378 non local symbols they define can resolve a reference in another shared
1379 library or in the executable. Correspondingly, it allows undefined local
1380 symbols to be resolved by other shared libraries or by the executable. */
1381 static void export_global_syms(TCCState
*s1
)
1383 int dynindex
, index
;
1387 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
)) {
1388 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
1389 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1390 dynindex
= put_elf_sym(s1
->dynsym
, sym
->st_value
, sym
->st_size
,
1391 sym
->st_info
, 0, sym
->st_shndx
, name
);
1392 index
= sym
- (ElfW(Sym
) *) symtab_section
->data
;
1393 get_sym_attr(s1
, index
, 1)->dyn_index
= dynindex
;
1398 /* Allocate strings for section names and decide if an unallocated section
1400 NOTE: the strsec section comes last, so its size is also correct ! */
1401 static void alloc_sec_names(TCCState
*s1
, int file_type
, Section
*strsec
)
1406 /* Allocate strings for section names */
1407 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1408 s
= s1
->sections
[i
];
1409 /* when generating a DLL, we include relocations but we may
1411 if (file_type
== TCC_OUTPUT_DLL
&&
1412 s
->sh_type
== SHT_RELX
&&
1413 !(s
->sh_flags
& SHF_ALLOC
)) {
1414 /* gr: avoid bogus relocs for empty (debug) sections */
1415 if (s1
->sections
[s
->sh_info
]->sh_flags
& SHF_ALLOC
)
1416 prepare_dynamic_rel(s1
, s
);
1417 else if (s1
->do_debug
)
1418 s
->sh_size
= s
->data_offset
;
1419 } else if (s1
->do_debug
||
1420 file_type
== TCC_OUTPUT_OBJ
||
1421 (s
->sh_flags
& SHF_ALLOC
) ||
1422 i
== (s1
->nb_sections
- 1)) {
1423 /* we output all sections if debug or object file */
1424 s
->sh_size
= s
->data_offset
;
1426 if (s
->sh_size
|| (s
->sh_flags
& SHF_ALLOC
))
1427 s
->sh_name
= put_elf_str(strsec
, s
->name
);
1429 strsec
->sh_size
= strsec
->data_offset
;
1432 /* Info to be copied in dynamic section */
1436 unsigned long dyn_rel_off
;
1439 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1445 /* Assign sections to segments and decide how are sections laid out when loaded
1446 in memory. This function also fills corresponding program headers. */
1447 static int layout_sections(TCCState
*s1
, ElfW(Phdr
) *phdr
, int phnum
,
1448 Section
*interp
, Section
* strsec
,
1449 struct dyn_inf
*dyninf
, int *sec_order
)
1451 int i
, j
, k
, file_type
, sh_order_index
, file_offset
;
1452 unsigned long s_align
;
1458 file_type
= s1
->output_type
;
1461 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
)
1462 file_offset
= sizeof(ElfW(Ehdr
)) + phnum
* sizeof(ElfW(Phdr
));
1463 s_align
= ELF_PAGE_SIZE
;
1464 if (s1
->section_align
)
1465 s_align
= s1
->section_align
;
1468 if (s1
->has_text_addr
) {
1469 int a_offset
, p_offset
;
1470 addr
= s1
->text_addr
;
1471 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1473 a_offset
= (int) (addr
& (s_align
- 1));
1474 p_offset
= file_offset
& (s_align
- 1);
1475 if (a_offset
< p_offset
)
1476 a_offset
+= s_align
;
1477 file_offset
+= (a_offset
- p_offset
);
1479 if (file_type
== TCC_OUTPUT_DLL
)
1482 addr
= ELF_START_ADDR
;
1483 /* compute address after headers */
1484 addr
+= (file_offset
& (s_align
- 1));
1488 /* Leave one program headers for the program interpreter and one for
1489 the program header table itself if needed. These are done later as
1490 they require section layout to be done first. */
1492 ph
+= 1 + HAVE_PHDR
;
1494 /* dynamic relocation table information, for .dynamic section */
1495 dyninf
->rel_addr
= dyninf
->rel_size
= 0;
1496 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1497 dyninf
->bss_addr
= dyninf
->bss_size
= 0;
1500 for(j
= 0; j
< 2; j
++) {
1501 ph
->p_type
= PT_LOAD
;
1503 ph
->p_flags
= PF_R
| PF_X
;
1505 ph
->p_flags
= PF_R
| PF_W
;
1506 ph
->p_align
= s_align
;
1508 /* Decide the layout of sections loaded in memory. This must
1509 be done before program headers are filled since they contain
1510 info about the layout. We do the following ordering: interp,
1511 symbol tables, relocations, progbits, nobits */
1512 /* XXX: do faster and simpler sorting */
1513 for(k
= 0; k
< 5; k
++) {
1514 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1515 s
= s1
->sections
[i
];
1516 /* compute if section should be included */
1518 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_WRITE
)) !=
1522 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_WRITE
)) !=
1523 (SHF_ALLOC
| SHF_WRITE
))
1529 } else if (s
->sh_type
== SHT_DYNSYM
||
1530 s
->sh_type
== SHT_STRTAB
||
1531 s
->sh_type
== SHT_HASH
) {
1534 } else if (s
->sh_type
== SHT_RELX
) {
1537 } else if (s
->sh_type
== SHT_NOBITS
) {
1544 sec_order
[sh_order_index
++] = i
;
1546 /* section matches: we align it and add its size */
1548 addr
= (addr
+ s
->sh_addralign
- 1) &
1549 ~(s
->sh_addralign
- 1);
1550 file_offset
+= (int) ( addr
- tmp
);
1551 s
->sh_offset
= file_offset
;
1554 /* update program header infos */
1555 if (ph
->p_offset
== 0) {
1556 ph
->p_offset
= file_offset
;
1558 ph
->p_paddr
= ph
->p_vaddr
;
1560 /* update dynamic relocation infos */
1561 if (s
->sh_type
== SHT_RELX
) {
1562 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1563 if (!strcmp(strsec
->data
+ s
->sh_name
, ".rel.got")) {
1564 dyninf
->rel_addr
= addr
;
1565 dyninf
->rel_size
+= s
->sh_size
; /* XXX only first rel. */
1567 if (!strcmp(strsec
->data
+ s
->sh_name
, ".rel.bss")) {
1568 dyninf
->bss_addr
= addr
;
1569 dyninf
->bss_size
= s
->sh_size
; /* XXX only first rel. */
1572 if (dyninf
->rel_size
== 0)
1573 dyninf
->rel_addr
= addr
;
1574 dyninf
->rel_size
+= s
->sh_size
;
1578 if (s
->sh_type
!= SHT_NOBITS
)
1579 file_offset
+= s
->sh_size
;
1583 /* Make the first PT_LOAD segment include the program
1584 headers itself (and the ELF header as well), it'll
1585 come out with same memory use but will make various
1586 tools like binutils strip work better. */
1587 ph
->p_offset
&= ~(ph
->p_align
- 1);
1588 ph
->p_vaddr
&= ~(ph
->p_align
- 1);
1589 ph
->p_paddr
&= ~(ph
->p_align
- 1);
1591 ph
->p_filesz
= file_offset
- ph
->p_offset
;
1592 ph
->p_memsz
= addr
- ph
->p_vaddr
;
1595 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
) {
1596 /* if in the middle of a page, we duplicate the page in
1597 memory so that one copy is RX and the other is RW */
1598 if ((addr
& (s_align
- 1)) != 0)
1601 addr
= (addr
+ s_align
- 1) & ~(s_align
- 1);
1602 file_offset
= (file_offset
+ s_align
- 1) & ~(s_align
- 1);
1608 /* all other sections come after */
1609 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1610 s
= s1
->sections
[i
];
1611 if (phnum
> 0 && (s
->sh_flags
& SHF_ALLOC
))
1613 sec_order
[sh_order_index
++] = i
;
1615 file_offset
= (file_offset
+ s
->sh_addralign
- 1) &
1616 ~(s
->sh_addralign
- 1);
1617 s
->sh_offset
= file_offset
;
1618 if (s
->sh_type
!= SHT_NOBITS
)
1619 file_offset
+= s
->sh_size
;
1625 static void fill_unloadable_phdr(ElfW(Phdr
) *phdr
, int phnum
, Section
*interp
,
1630 /* if interpreter, then add corresponding program header */
1636 int len
= phnum
* sizeof(ElfW(Phdr
));
1638 ph
->p_type
= PT_PHDR
;
1639 ph
->p_offset
= sizeof(ElfW(Ehdr
));
1640 ph
->p_vaddr
= interp
->sh_addr
- len
;
1641 ph
->p_paddr
= ph
->p_vaddr
;
1642 ph
->p_filesz
= ph
->p_memsz
= len
;
1643 ph
->p_flags
= PF_R
| PF_X
;
1644 ph
->p_align
= 4; /* interp->sh_addralign; */
1648 ph
->p_type
= PT_INTERP
;
1649 ph
->p_offset
= interp
->sh_offset
;
1650 ph
->p_vaddr
= interp
->sh_addr
;
1651 ph
->p_paddr
= ph
->p_vaddr
;
1652 ph
->p_filesz
= interp
->sh_size
;
1653 ph
->p_memsz
= interp
->sh_size
;
1655 ph
->p_align
= interp
->sh_addralign
;
1658 /* if dynamic section, then add corresponding program header */
1660 ph
= &phdr
[phnum
- 1];
1662 ph
->p_type
= PT_DYNAMIC
;
1663 ph
->p_offset
= dynamic
->sh_offset
;
1664 ph
->p_vaddr
= dynamic
->sh_addr
;
1665 ph
->p_paddr
= ph
->p_vaddr
;
1666 ph
->p_filesz
= dynamic
->sh_size
;
1667 ph
->p_memsz
= dynamic
->sh_size
;
1668 ph
->p_flags
= PF_R
| PF_W
;
1669 ph
->p_align
= dynamic
->sh_addralign
;
1673 /* Fill the dynamic section with tags describing the address and size of
1675 static void fill_dynamic(TCCState
*s1
, struct dyn_inf
*dyninf
)
1679 dynamic
= dyninf
->dynamic
;
1681 /* put dynamic section entries */
1682 dynamic
->data_offset
= dyninf
->dyn_rel_off
;
1683 put_dt(dynamic
, DT_HASH
, s1
->dynsym
->hash
->sh_addr
);
1684 put_dt(dynamic
, DT_STRTAB
, dyninf
->dynstr
->sh_addr
);
1685 put_dt(dynamic
, DT_SYMTAB
, s1
->dynsym
->sh_addr
);
1686 put_dt(dynamic
, DT_STRSZ
, dyninf
->dynstr
->data_offset
);
1687 put_dt(dynamic
, DT_SYMENT
, sizeof(ElfW(Sym
)));
1688 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1689 put_dt(dynamic
, DT_RELA
, dyninf
->rel_addr
);
1690 put_dt(dynamic
, DT_RELASZ
, dyninf
->rel_size
);
1691 put_dt(dynamic
, DT_RELAENT
, sizeof(ElfW_Rel
));
1693 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1694 put_dt(dynamic
, DT_PLTGOT
, s1
->got
->sh_addr
);
1695 put_dt(dynamic
, DT_PLTRELSZ
, dyninf
->rel_size
);
1696 put_dt(dynamic
, DT_JMPREL
, dyninf
->rel_addr
);
1697 put_dt(dynamic
, DT_PLTREL
, DT_REL
);
1698 put_dt(dynamic
, DT_REL
, dyninf
->bss_addr
);
1699 put_dt(dynamic
, DT_RELSZ
, dyninf
->bss_size
);
1701 put_dt(dynamic
, DT_REL
, dyninf
->rel_addr
);
1702 put_dt(dynamic
, DT_RELSZ
, dyninf
->rel_size
);
1703 put_dt(dynamic
, DT_RELENT
, sizeof(ElfW_Rel
));
1707 put_dt(dynamic
, DT_DEBUG
, 0);
1708 put_dt(dynamic
, DT_NULL
, 0);
1711 /* Relocate remaining sections and symbols (that is those not related to
1713 static int final_sections_reloc(TCCState
*s1
)
1718 relocate_syms(s1
, s1
->symtab
, 0);
1720 if (s1
->nb_errors
!= 0)
1723 /* relocate sections */
1724 /* XXX: ignore sections with allocated relocations ? */
1725 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1726 s
= s1
->sections
[i
];
1727 #if defined(TCC_TARGET_I386) || defined(TCC_MUSL)
1728 if (s
->reloc
&& s
!= s1
->got
&& (s
->sh_flags
& SHF_ALLOC
)) //gr
1729 /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC
1730 checking is removed */
1732 if (s
->reloc
&& s
!= s1
->got
)
1733 /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */
1735 relocate_section(s1
, s
);
1738 /* relocate relocation entries if the relocation tables are
1739 allocated in the executable */
1740 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1741 s
= s1
->sections
[i
];
1742 if ((s
->sh_flags
& SHF_ALLOC
) &&
1743 s
->sh_type
== SHT_RELX
) {
1744 relocate_rel(s1
, s
);
1750 /* Create an ELF file on disk.
1751 This function handle ELF specific layout requirements */
1752 static void tcc_output_elf(TCCState
*s1
, FILE *f
, int phnum
, ElfW(Phdr
) *phdr
,
1753 int file_offset
, int *sec_order
)
1755 int i
, shnum
, offset
, size
, file_type
;
1758 ElfW(Shdr
) shdr
, *sh
;
1760 file_type
= s1
->output_type
;
1761 shnum
= s1
->nb_sections
;
1763 memset(&ehdr
, 0, sizeof(ehdr
));
1766 ehdr
.e_phentsize
= sizeof(ElfW(Phdr
));
1767 ehdr
.e_phnum
= phnum
;
1768 ehdr
.e_phoff
= sizeof(ElfW(Ehdr
));
1772 file_offset
= (file_offset
+ 3) & -4;
1775 ehdr
.e_ident
[0] = ELFMAG0
;
1776 ehdr
.e_ident
[1] = ELFMAG1
;
1777 ehdr
.e_ident
[2] = ELFMAG2
;
1778 ehdr
.e_ident
[3] = ELFMAG3
;
1779 ehdr
.e_ident
[4] = ELFCLASSW
;
1780 ehdr
.e_ident
[5] = ELFDATA2LSB
;
1781 ehdr
.e_ident
[6] = EV_CURRENT
;
1782 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1783 ehdr
.e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
1785 #ifdef TCC_TARGET_ARM
1787 ehdr
.e_ident
[EI_OSABI
] = 0;
1788 ehdr
.e_flags
= EF_ARM_EABI_VER4
;
1789 if (file_type
== TCC_OUTPUT_EXE
|| file_type
== TCC_OUTPUT_DLL
)
1790 ehdr
.e_flags
|= EF_ARM_HASENTRY
;
1791 if (s1
->float_abi
== ARM_HARD_FLOAT
)
1792 ehdr
.e_flags
|= EF_ARM_VFP_FLOAT
;
1794 ehdr
.e_flags
|= EF_ARM_SOFT_FLOAT
;
1796 ehdr
.e_ident
[EI_OSABI
] = ELFOSABI_ARM
;
1801 case TCC_OUTPUT_EXE
:
1802 ehdr
.e_type
= ET_EXEC
;
1803 ehdr
.e_entry
= get_elf_sym_addr(s1
, "_start", 1);
1805 case TCC_OUTPUT_DLL
:
1806 ehdr
.e_type
= ET_DYN
;
1807 ehdr
.e_entry
= text_section
->sh_addr
; /* XXX: is it correct ? */
1809 case TCC_OUTPUT_OBJ
:
1810 ehdr
.e_type
= ET_REL
;
1813 ehdr
.e_machine
= EM_TCC_TARGET
;
1814 ehdr
.e_version
= EV_CURRENT
;
1815 ehdr
.e_shoff
= file_offset
;
1816 ehdr
.e_ehsize
= sizeof(ElfW(Ehdr
));
1817 ehdr
.e_shentsize
= sizeof(ElfW(Shdr
));
1818 ehdr
.e_shnum
= shnum
;
1819 ehdr
.e_shstrndx
= shnum
- 1;
1821 fwrite(&ehdr
, 1, sizeof(ElfW(Ehdr
)), f
);
1822 fwrite(phdr
, 1, phnum
* sizeof(ElfW(Phdr
)), f
);
1823 offset
= sizeof(ElfW(Ehdr
)) + phnum
* sizeof(ElfW(Phdr
));
1825 sort_syms(s1
, symtab_section
);
1826 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1827 s
= s1
->sections
[sec_order
[i
]];
1828 if (s
->sh_type
!= SHT_NOBITS
) {
1829 while (offset
< s
->sh_offset
) {
1835 fwrite(s
->data
, 1, size
, f
);
1840 /* output section headers */
1841 while (offset
< ehdr
.e_shoff
) {
1846 for(i
= 0; i
< s1
->nb_sections
; i
++) {
1848 memset(sh
, 0, sizeof(ElfW(Shdr
)));
1849 s
= s1
->sections
[i
];
1851 sh
->sh_name
= s
->sh_name
;
1852 sh
->sh_type
= s
->sh_type
;
1853 sh
->sh_flags
= s
->sh_flags
;
1854 sh
->sh_entsize
= s
->sh_entsize
;
1855 sh
->sh_info
= s
->sh_info
;
1857 sh
->sh_link
= s
->link
->sh_num
;
1858 sh
->sh_addralign
= s
->sh_addralign
;
1859 sh
->sh_addr
= s
->sh_addr
;
1860 sh
->sh_offset
= s
->sh_offset
;
1861 sh
->sh_size
= s
->sh_size
;
1863 fwrite(sh
, 1, sizeof(ElfW(Shdr
)), f
);
1867 /* Write an elf, coff or "binary" file */
1868 static int tcc_write_elf_file(TCCState
*s1
, const char *filename
, int phnum
,
1869 ElfW(Phdr
) *phdr
, int file_offset
, int *sec_order
)
1871 int fd
, mode
, file_type
;
1874 file_type
= s1
->output_type
;
1875 if (file_type
== TCC_OUTPUT_OBJ
)
1880 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, mode
);
1882 tcc_error_noabort("could not write '%s'", filename
);
1885 f
= fdopen(fd
, "wb");
1887 printf("<- %s\n", filename
);
1889 #ifdef TCC_TARGET_COFF
1890 if (s1
->output_format
== TCC_OUTPUT_FORMAT_COFF
)
1891 tcc_output_coff(s1
, f
);
1894 if (s1
->output_format
== TCC_OUTPUT_FORMAT_ELF
)
1895 tcc_output_elf(s1
, f
, phnum
, phdr
, file_offset
, sec_order
);
1897 tcc_output_binary(s1
, f
, sec_order
);
1903 /* Sort section headers by assigned sh_addr, remove sections
1904 that we aren't going to output. */
1905 static void tidy_section_headers(TCCState
*s1
, int *sec_order
)
1907 int i
, nnew
, l
, *backmap
;
1911 snew
= tcc_malloc(s1
->nb_sections
* sizeof(snew
[0]));
1912 backmap
= tcc_malloc(s1
->nb_sections
* sizeof(backmap
[0]));
1913 for (i
= 0, nnew
= 0, l
= s1
->nb_sections
; i
< s1
->nb_sections
; i
++) {
1914 s
= s1
->sections
[sec_order
[i
]];
1915 if (!i
|| s
->sh_name
) {
1916 backmap
[sec_order
[i
]] = nnew
;
1920 backmap
[sec_order
[i
]] = 0;
1924 for (i
= 0; i
< nnew
; i
++) {
1928 if (s
->sh_type
== SHT_RELX
)
1929 s
->sh_info
= backmap
[s
->sh_info
];
1933 for_each_elem(symtab_section
, 1, sym
, ElfW(Sym
))
1934 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
)
1935 sym
->st_shndx
= backmap
[sym
->st_shndx
];
1936 for_each_elem(s1
->dynsym
, 1, sym
, ElfW(Sym
))
1937 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
)
1938 sym
->st_shndx
= backmap
[sym
->st_shndx
];
1940 for (i
= 0; i
< s1
->nb_sections
; i
++)
1942 tcc_free(s1
->sections
);
1943 s1
->sections
= snew
;
1944 s1
->nb_sections
= nnew
;
1948 /* Output an elf, coff or binary file */
1949 /* XXX: suppress unneeded sections */
1950 static int elf_output_file(TCCState
*s1
, const char *filename
)
1952 int i
, ret
, phnum
, shnum
, file_type
, file_offset
, *sec_order
;
1953 struct dyn_inf dyninf
= {0};
1956 Section
*strsec
, *interp
, *dynamic
, *dynstr
;
1958 file_type
= s1
->output_type
;
1961 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
1962 if (file_type
!= TCC_OUTPUT_OBJ
) {
1963 tcc_add_runtime(s1
);
1968 interp
= dynamic
= dynstr
= NULL
; /* avoid warning */
1970 if (file_type
!= TCC_OUTPUT_OBJ
) {
1971 relocate_common_syms();
1973 tcc_add_linker_symbols(s1
);
1975 if (!s1
->static_link
) {
1976 if (file_type
== TCC_OUTPUT_EXE
) {
1978 /* allow override the dynamic loader */
1979 const char *elfint
= getenv("LD_SO");
1981 elfint
= DEFAULT_ELFINTERP(s1
);
1982 /* add interpreter section only if executable */
1983 interp
= new_section(s1
, ".interp", SHT_PROGBITS
, SHF_ALLOC
);
1984 interp
->sh_addralign
= 1;
1985 ptr
= section_ptr_add(interp
, 1 + strlen(elfint
));
1986 strcpy(ptr
, elfint
);
1989 /* add dynamic symbol table */
1990 s1
->dynsym
= new_symtab(s1
, ".dynsym", SHT_DYNSYM
, SHF_ALLOC
,
1992 ".hash", SHF_ALLOC
);
1993 dynstr
= s1
->dynsym
->link
;
1995 /* add dynamic section */
1996 dynamic
= new_section(s1
, ".dynamic", SHT_DYNAMIC
,
1997 SHF_ALLOC
| SHF_WRITE
);
1998 dynamic
->link
= dynstr
;
1999 dynamic
->sh_entsize
= sizeof(ElfW(Dyn
));
2003 if (file_type
== TCC_OUTPUT_EXE
) {
2004 bind_exe_dynsyms(s1
);
2006 if (s1
->nb_errors
) {
2011 bind_libs_dynsyms(s1
);
2012 } else /* shared library case: simply export all global symbols */
2013 export_global_syms(s1
);
2015 build_got_entries(s1
);
2017 /* add a list of needed dlls */
2018 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
2019 DLLReference
*dllref
= s1
->loaded_dlls
[i
];
2020 if (dllref
->level
== 0)
2021 put_dt(dynamic
, DT_NEEDED
, put_elf_str(dynstr
, dllref
->name
));
2025 put_dt(dynamic
, s1
->enable_new_dtags
? DT_RUNPATH
: DT_RPATH
,
2026 put_elf_str(dynstr
, s1
->rpath
));
2028 /* XXX: currently, since we do not handle PIC code, we
2029 must relocate the readonly segments */
2030 if (file_type
== TCC_OUTPUT_DLL
) {
2032 put_dt(dynamic
, DT_SONAME
, put_elf_str(dynstr
, s1
->soname
));
2033 put_dt(dynamic
, DT_TEXTREL
, 0);
2037 put_dt(dynamic
, DT_SYMBOLIC
, 0);
2039 /* add necessary space for other entries */
2040 dyninf
.dyn_rel_off
= dynamic
->data_offset
;
2041 dynamic
->data_offset
+= sizeof(ElfW(Dyn
)) * EXTRA_RELITEMS
;
2043 /* still need to build got entries in case of static link */
2044 build_got_entries(s1
);
2048 /* we add a section for symbols */
2049 strsec
= new_section(s1
, ".shstrtab", SHT_STRTAB
, 0);
2050 put_elf_str(strsec
, "");
2052 /* compute number of sections */
2053 shnum
= s1
->nb_sections
;
2055 /* this array is used to reorder sections in the output file */
2056 sec_order
= tcc_malloc(sizeof(int) * shnum
);
2059 /* compute number of program headers */
2062 case TCC_OUTPUT_OBJ
:
2065 case TCC_OUTPUT_EXE
:
2066 if (!s1
->static_link
)
2067 phnum
= 4 + HAVE_PHDR
;
2071 case TCC_OUTPUT_DLL
:
2076 /* Allocate strings for section names */
2077 alloc_sec_names(s1
, file_type
, strsec
);
2079 /* allocate program segment headers */
2080 phdr
= tcc_mallocz(phnum
* sizeof(ElfW(Phdr
)));
2082 /* compute section to program header mapping */
2083 file_offset
= layout_sections(s1
, phdr
, phnum
, interp
, strsec
, &dyninf
,
2086 /* Fill remaining program header and finalize relocation related to dynamic
2089 fill_unloadable_phdr(phdr
, phnum
, interp
, dynamic
);
2091 dyninf
.dynamic
= dynamic
;
2092 dyninf
.dynstr
= dynstr
;
2094 fill_dynamic(s1
, &dyninf
);
2096 /* put in GOT the dynamic section address and relocate PLT */
2097 write32le(s1
->got
->data
, dynamic
->sh_addr
);
2098 if (file_type
== TCC_OUTPUT_EXE
2099 || (RELOCATE_DLLPLT
&& file_type
== TCC_OUTPUT_DLL
))
2102 /* relocate symbols in .dynsym now that final addresses are known */
2103 for_each_elem(s1
->dynsym
, 1, sym
, ElfW(Sym
)) {
2104 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
) {
2105 /* do symbol relocation */
2106 sym
->st_value
+= s1
->sections
[sym
->st_shndx
]->sh_addr
;
2112 /* if building executable or DLL, then relocate each section
2113 except the GOT which is already relocated */
2114 if (file_type
!= TCC_OUTPUT_OBJ
) {
2115 ret
= final_sections_reloc(s1
);
2118 tidy_section_headers(s1
, sec_order
);
2121 /* Perform relocation to GOT or PLT entries */
2122 if (file_type
== TCC_OUTPUT_EXE
&& s1
->static_link
)
2125 /* Create the ELF file with name 'filename' */
2126 ret
= tcc_write_elf_file(s1
, filename
, phnum
, phdr
, file_offset
, sec_order
);
2127 s1
->nb_sections
= shnum
;
2129 tcc_free(sec_order
);
2134 LIBTCCAPI
int tcc_output_file(TCCState
*s
, const char *filename
)
2137 #ifdef TCC_TARGET_PE
2138 if (s
->output_type
!= TCC_OUTPUT_OBJ
) {
2139 ret
= pe_output_file(s
, filename
);
2142 ret
= elf_output_file(s
, filename
);
2146 static void *load_data(int fd
, unsigned long file_offset
, unsigned long size
)
2150 data
= tcc_malloc(size
);
2151 lseek(fd
, file_offset
, SEEK_SET
);
2152 read(fd
, data
, size
);
2156 typedef struct SectionMergeInfo
{
2157 Section
*s
; /* corresponding existing section */
2158 unsigned long offset
; /* offset of the new section in the existing section */
2159 uint8_t new_section
; /* true if section 's' was added */
2160 uint8_t link_once
; /* true if link once section */
2163 ST_FUNC
int tcc_object_type(int fd
, ElfW(Ehdr
) *h
)
2165 int size
= read(fd
, h
, sizeof *h
);
2166 if (size
== sizeof *h
&& 0 == memcmp(h
, ELFMAG
, 4)) {
2167 if (h
->e_type
== ET_REL
)
2168 return AFF_BINTYPE_REL
;
2169 if (h
->e_type
== ET_DYN
)
2170 return AFF_BINTYPE_DYN
;
2171 } else if (size
>= 8) {
2172 if (0 == memcmp(h
, ARMAG
, 8))
2173 return AFF_BINTYPE_AR
;
2174 #ifdef TCC_TARGET_COFF
2175 if (((struct filehdr
*)h
)->f_magic
== COFF_C67_MAGIC
)
2176 return AFF_BINTYPE_C67
;
2182 /* load an object file and merge it with current files */
2183 /* XXX: handle correctly stab (debug) info */
2184 ST_FUNC
int tcc_load_object_file(TCCState
*s1
,
2185 int fd
, unsigned long file_offset
)
2188 ElfW(Shdr
) *shdr
, *sh
;
2189 int size
, i
, j
, offset
, offseti
, nb_syms
, sym_index
, ret
;
2190 unsigned char *strsec
, *strtab
;
2191 int *old_to_new_syms
;
2192 char *sh_name
, *name
;
2193 SectionMergeInfo
*sm_table
, *sm
;
2194 ElfW(Sym
) *sym
, *symtab
;
2201 stab_index
= stabstr_index
= 0;
2203 lseek(fd
, file_offset
, SEEK_SET
);
2204 if (tcc_object_type(fd
, &ehdr
) != AFF_BINTYPE_REL
)
2206 /* test CPU specific stuff */
2207 if (ehdr
.e_ident
[5] != ELFDATA2LSB
||
2208 ehdr
.e_machine
!= EM_TCC_TARGET
) {
2210 tcc_error_noabort("invalid object file");
2214 shdr
= load_data(fd
, file_offset
+ ehdr
.e_shoff
,
2215 sizeof(ElfW(Shdr
)) * ehdr
.e_shnum
);
2216 sm_table
= tcc_mallocz(sizeof(SectionMergeInfo
) * ehdr
.e_shnum
);
2218 /* load section names */
2219 sh
= &shdr
[ehdr
.e_shstrndx
];
2220 strsec
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2222 /* load symtab and strtab */
2223 old_to_new_syms
= NULL
;
2227 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2229 if (sh
->sh_type
== SHT_SYMTAB
) {
2231 tcc_error_noabort("object must contain only one symtab");
2236 nb_syms
= sh
->sh_size
/ sizeof(ElfW(Sym
));
2237 symtab
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2238 sm_table
[i
].s
= symtab_section
;
2240 /* now load strtab */
2241 sh
= &shdr
[sh
->sh_link
];
2242 strtab
= load_data(fd
, file_offset
+ sh
->sh_offset
, sh
->sh_size
);
2246 /* now examine each section and try to merge its content with the
2248 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2249 /* no need to examine section name strtab */
2250 if (i
== ehdr
.e_shstrndx
)
2253 sh_name
= (char *) strsec
+ sh
->sh_name
;
2254 /* ignore sections types we do not handle */
2255 if (sh
->sh_type
!= SHT_PROGBITS
&&
2256 sh
->sh_type
!= SHT_RELX
&&
2258 sh
->sh_type
!= SHT_ARM_EXIDX
&&
2260 sh
->sh_type
!= SHT_NOBITS
&&
2261 sh
->sh_type
!= SHT_PREINIT_ARRAY
&&
2262 sh
->sh_type
!= SHT_INIT_ARRAY
&&
2263 sh
->sh_type
!= SHT_FINI_ARRAY
&&
2264 strcmp(sh_name
, ".stabstr")
2267 if (sh
->sh_addralign
< 1)
2268 sh
->sh_addralign
= 1;
2269 /* find corresponding section, if any */
2270 for(j
= 1; j
< s1
->nb_sections
;j
++) {
2271 s
= s1
->sections
[j
];
2272 if (!strcmp(s
->name
, sh_name
)) {
2273 if (!strncmp(sh_name
, ".gnu.linkonce",
2274 sizeof(".gnu.linkonce") - 1)) {
2275 /* if a 'linkonce' section is already present, we
2276 do not add it again. It is a little tricky as
2277 symbols can still be defined in
2279 sm_table
[i
].link_once
= 1;
2286 /* not found: create new section */
2287 s
= new_section(s1
, sh_name
, sh
->sh_type
, sh
->sh_flags
& ~SHF_GROUP
);
2288 /* take as much info as possible from the section. sh_link and
2289 sh_info will be updated later */
2290 s
->sh_addralign
= sh
->sh_addralign
;
2291 s
->sh_entsize
= sh
->sh_entsize
;
2292 sm_table
[i
].new_section
= 1;
2294 if (sh
->sh_type
!= s
->sh_type
) {
2295 tcc_error_noabort("invalid section type");
2299 /* align start of section */
2300 offset
= s
->data_offset
;
2302 if (0 == strcmp(sh_name
, ".stab")) {
2306 if (0 == strcmp(sh_name
, ".stabstr")) {
2311 size
= sh
->sh_addralign
- 1;
2312 offset
= (offset
+ size
) & ~size
;
2313 if (sh
->sh_addralign
> s
->sh_addralign
)
2314 s
->sh_addralign
= sh
->sh_addralign
;
2315 s
->data_offset
= offset
;
2317 sm_table
[i
].offset
= offset
;
2319 /* concatenate sections */
2321 if (sh
->sh_type
!= SHT_NOBITS
) {
2323 lseek(fd
, file_offset
+ sh
->sh_offset
, SEEK_SET
);
2324 ptr
= section_ptr_add(s
, size
);
2325 read(fd
, ptr
, size
);
2327 s
->data_offset
+= size
;
2332 /* gr relocate stab strings */
2333 if (stab_index
&& stabstr_index
) {
2336 s
= sm_table
[stab_index
].s
;
2337 a
= (Stab_Sym
*)(s
->data
+ sm_table
[stab_index
].offset
);
2338 b
= (Stab_Sym
*)(s
->data
+ s
->data_offset
);
2339 o
= sm_table
[stabstr_index
].offset
;
2341 a
->n_strx
+= o
, a
++;
2344 /* second short pass to update sh_link and sh_info fields of new
2346 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2348 if (!s
|| !sm_table
[i
].new_section
)
2351 if (sh
->sh_link
> 0)
2352 s
->link
= sm_table
[sh
->sh_link
].s
;
2353 if (sh
->sh_type
== SHT_RELX
) {
2354 s
->sh_info
= sm_table
[sh
->sh_info
].s
->sh_num
;
2355 /* update backward link */
2356 s1
->sections
[s
->sh_info
]->reloc
= s
;
2361 /* resolve symbols */
2362 old_to_new_syms
= tcc_mallocz(nb_syms
* sizeof(int));
2365 for(i
= 1; i
< nb_syms
; i
++, sym
++) {
2366 if (sym
->st_shndx
!= SHN_UNDEF
&&
2367 sym
->st_shndx
< SHN_LORESERVE
) {
2368 sm
= &sm_table
[sym
->st_shndx
];
2369 if (sm
->link_once
) {
2370 /* if a symbol is in a link once section, we use the
2371 already defined symbol. It is very important to get
2372 correct relocations */
2373 if (ELFW(ST_BIND
)(sym
->st_info
) != STB_LOCAL
) {
2374 name
= (char *) strtab
+ sym
->st_name
;
2375 sym_index
= find_elf_sym(symtab_section
, name
);
2377 old_to_new_syms
[i
] = sym_index
;
2381 /* if no corresponding section added, no need to add symbol */
2384 /* convert section number */
2385 sym
->st_shndx
= sm
->s
->sh_num
;
2387 sym
->st_value
+= sm
->offset
;
2390 name
= (char *) strtab
+ sym
->st_name
;
2391 sym_index
= set_elf_sym(symtab_section
, sym
->st_value
, sym
->st_size
,
2392 sym
->st_info
, sym
->st_other
,
2393 sym
->st_shndx
, name
);
2394 old_to_new_syms
[i
] = sym_index
;
2397 /* third pass to patch relocation entries */
2398 for(i
= 1; i
< ehdr
.e_shnum
; i
++) {
2403 offset
= sm_table
[i
].offset
;
2404 switch(s
->sh_type
) {
2406 /* take relocation offset information */
2407 offseti
= sm_table
[sh
->sh_info
].offset
;
2408 for_each_elem(s
, (offset
/ sizeof(*rel
)), rel
, ElfW_Rel
) {
2411 /* convert symbol index */
2412 type
= ELFW(R_TYPE
)(rel
->r_info
);
2413 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
2414 /* NOTE: only one symtab assumed */
2415 if (sym_index
>= nb_syms
)
2417 sym_index
= old_to_new_syms
[sym_index
];
2418 /* ignore link_once in rel section. */
2419 if (!sym_index
&& !sm
->link_once
2420 #ifdef TCC_TARGET_ARM
2421 && type
!= R_ARM_V4BX
2425 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2426 i
, strsec
+ sh
->sh_name
, rel
->r_offset
);
2429 rel
->r_info
= ELFW(R_INFO
)(sym_index
, type
);
2430 /* offset the relocation offset */
2431 rel
->r_offset
+= offseti
;
2432 #ifdef TCC_TARGET_ARM
2433 /* Jumps and branches from a Thumb code to a PLT entry need
2434 special handling since PLT entries are ARM code.
2435 Unconditional bl instructions referencing PLT entries are
2436 handled by converting these instructions into blx
2437 instructions. Other case of instructions referencing a PLT
2438 entry require to add a Thumb stub before the PLT entry to
2439 switch to ARM mode. We set bit plt_thumb_stub of the
2440 attribute of a symbol to indicate such a case. */
2441 if (type
== R_ARM_THM_JUMP24
)
2442 get_sym_attr(s1
, sym_index
, 1)->plt_thumb_stub
= 1;
2455 tcc_free(old_to_new_syms
);
2462 typedef struct ArchiveHeader
{
2463 char ar_name
[16]; /* name of this member */
2464 char ar_date
[12]; /* file mtime */
2465 char ar_uid
[6]; /* owner uid; printed as decimal */
2466 char ar_gid
[6]; /* owner gid; printed as decimal */
2467 char ar_mode
[8]; /* file mode, printed as octal */
2468 char ar_size
[10]; /* file size, printed as decimal */
2469 char ar_fmag
[2]; /* should contain ARFMAG */
2472 static int get_be32(const uint8_t *b
)
2474 return b
[3] | (b
[2] << 8) | (b
[1] << 16) | (b
[0] << 24);
2477 static long get_be64(const uint8_t *b
)
2479 long long ret
= get_be32(b
);
2480 ret
= (ret
<< 32) | (unsigned)get_be32(b
+4);
2484 /* load only the objects which resolve undefined symbols */
2485 static int tcc_load_alacarte(TCCState
*s1
, int fd
, int size
, int entrysize
)
2487 long i
, bound
, nsyms
, sym_index
, off
, ret
;
2489 const char *ar_names
, *p
;
2490 const uint8_t *ar_index
;
2493 data
= tcc_malloc(size
);
2494 if (read(fd
, data
, size
) != size
)
2496 nsyms
= entrysize
== 4 ? get_be32(data
) : get_be64(data
);
2497 ar_index
= data
+ entrysize
;
2498 ar_names
= (char *) ar_index
+ nsyms
* entrysize
;
2502 for(p
= ar_names
, i
= 0; i
< nsyms
; i
++, p
+= strlen(p
)+1) {
2503 sym_index
= find_elf_sym(symtab_section
, p
);
2505 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
2506 if(sym
->st_shndx
== SHN_UNDEF
) {
2507 off
= (entrysize
== 4
2508 ? get_be32(ar_index
+ i
* 4)
2509 : get_be64(ar_index
+ i
* 8))
2510 + sizeof(ArchiveHeader
);
2512 if(tcc_load_object_file(s1
, fd
, off
) < 0) {
2527 /* load a '.a' file */
2528 ST_FUNC
int tcc_load_archive(TCCState
*s1
, int fd
)
2535 unsigned long file_offset
;
2537 /* skip magic which was already checked */
2538 read(fd
, magic
, sizeof(magic
));
2541 len
= read(fd
, &hdr
, sizeof(hdr
));
2544 if (len
!= sizeof(hdr
)) {
2545 tcc_error_noabort("invalid archive");
2548 memcpy(ar_size
, hdr
.ar_size
, sizeof(hdr
.ar_size
));
2549 ar_size
[sizeof(hdr
.ar_size
)] = '\0';
2550 size
= strtol(ar_size
, NULL
, 0);
2551 memcpy(ar_name
, hdr
.ar_name
, sizeof(hdr
.ar_name
));
2552 for(i
= sizeof(hdr
.ar_name
) - 1; i
>= 0; i
--) {
2553 if (ar_name
[i
] != ' ')
2556 ar_name
[i
+ 1] = '\0';
2557 file_offset
= lseek(fd
, 0, SEEK_CUR
);
2559 size
= (size
+ 1) & ~1;
2560 if (!strcmp(ar_name
, "/")) {
2561 /* coff symbol table : we handle it */
2562 if(s1
->alacarte_link
)
2563 return tcc_load_alacarte(s1
, fd
, size
, 4);
2564 } else if (!strcmp(ar_name
, "/SYM64/")) {
2565 if(s1
->alacarte_link
)
2566 return tcc_load_alacarte(s1
, fd
, size
, 8);
2569 if (tcc_object_type(fd
, &ehdr
) == AFF_BINTYPE_REL
) {
2570 if (tcc_load_object_file(s1
, fd
, file_offset
) < 0)
2574 lseek(fd
, file_offset
+ size
, SEEK_SET
);
2579 #ifndef TCC_TARGET_PE
2580 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2581 is referenced by the user (so it should be added as DT_NEEDED in
2582 the generated ELF file) */
2583 ST_FUNC
int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
)
2586 ElfW(Shdr
) *shdr
, *sh
, *sh1
;
2587 int i
, j
, nb_syms
, nb_dts
, sym_bind
, ret
;
2588 ElfW(Sym
) *sym
, *dynsym
;
2589 ElfW(Dyn
) *dt
, *dynamic
;
2590 unsigned char *dynstr
;
2591 const char *name
, *soname
;
2592 DLLReference
*dllref
;
2594 read(fd
, &ehdr
, sizeof(ehdr
));
2596 /* test CPU specific stuff */
2597 if (ehdr
.e_ident
[5] != ELFDATA2LSB
||
2598 ehdr
.e_machine
!= EM_TCC_TARGET
) {
2599 tcc_error_noabort("bad architecture");
2604 shdr
= load_data(fd
, ehdr
.e_shoff
, sizeof(ElfW(Shdr
)) * ehdr
.e_shnum
);
2606 /* load dynamic section and dynamic symbols */
2610 dynsym
= NULL
; /* avoid warning */
2611 dynstr
= NULL
; /* avoid warning */
2612 for(i
= 0, sh
= shdr
; i
< ehdr
.e_shnum
; i
++, sh
++) {
2613 switch(sh
->sh_type
) {
2615 nb_dts
= sh
->sh_size
/ sizeof(ElfW(Dyn
));
2616 dynamic
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
2619 nb_syms
= sh
->sh_size
/ sizeof(ElfW(Sym
));
2620 dynsym
= load_data(fd
, sh
->sh_offset
, sh
->sh_size
);
2621 sh1
= &shdr
[sh
->sh_link
];
2622 dynstr
= load_data(fd
, sh1
->sh_offset
, sh1
->sh_size
);
2629 /* compute the real library name */
2630 soname
= tcc_basename(filename
);
2632 for(i
= 0, dt
= dynamic
; i
< nb_dts
; i
++, dt
++) {
2633 if (dt
->d_tag
== DT_SONAME
) {
2634 soname
= (char *) dynstr
+ dt
->d_un
.d_val
;
2638 /* if the dll is already loaded, do not load it */
2639 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
2640 dllref
= s1
->loaded_dlls
[i
];
2641 if (!strcmp(soname
, dllref
->name
)) {
2642 /* but update level if needed */
2643 if (level
< dllref
->level
)
2644 dllref
->level
= level
;
2650 /* add the dll and its level */
2651 dllref
= tcc_mallocz(sizeof(DLLReference
) + strlen(soname
));
2652 dllref
->level
= level
;
2653 strcpy(dllref
->name
, soname
);
2654 dynarray_add(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, dllref
);
2656 /* add dynamic symbols in dynsym_section */
2657 for(i
= 1, sym
= dynsym
+ 1; i
< nb_syms
; i
++, sym
++) {
2658 sym_bind
= ELFW(ST_BIND
)(sym
->st_info
);
2659 if (sym_bind
== STB_LOCAL
)
2661 name
= (char *) dynstr
+ sym
->st_name
;
2662 set_elf_sym(s1
->dynsymtab_section
, sym
->st_value
, sym
->st_size
,
2663 sym
->st_info
, sym
->st_other
, sym
->st_shndx
, name
);
2666 /* load all referenced DLLs */
2667 for(i
= 0, dt
= dynamic
; i
< nb_dts
; i
++, dt
++) {
2670 name
= (char *) dynstr
+ dt
->d_un
.d_val
;
2671 for(j
= 0; j
< s1
->nb_loaded_dlls
; j
++) {
2672 dllref
= s1
->loaded_dlls
[j
];
2673 if (!strcmp(name
, dllref
->name
))
2674 goto already_loaded
;
2676 if (tcc_add_dll(s1
, name
, AFF_REFERENCED_DLL
) < 0) {
2677 tcc_error_noabort("referenced dll '%s' not found", name
);
2694 #define LD_TOK_NAME 256
2695 #define LD_TOK_EOF (-1)
2697 /* return next ld script token */
2698 static int ld_next(TCCState
*s1
, char *name
, int name_size
)
2716 file
->buf_ptr
= parse_comment(file
->buf_ptr
);
2717 ch
= file
->buf_ptr
[0];
2730 /* case 'a' ... 'z': */
2757 /* case 'A' ... 'z': */
2791 if (!((ch
>= 'a' && ch
<= 'z') ||
2792 (ch
>= 'A' && ch
<= 'Z') ||
2793 (ch
>= '0' && ch
<= '9') ||
2794 strchr("/.-_+=$:\\,~", ch
)))
2796 if ((q
- name
) < name_size
- 1) {
2815 static int ld_add_file(TCCState
*s1
, const char filename
[])
2817 if (filename
[0] == '/') {
2818 if (CONFIG_SYSROOT
[0] == '\0'
2819 && tcc_add_file_internal(s1
, filename
, AFF_TYPE_BIN
) == 0)
2821 filename
= tcc_basename(filename
);
2823 return tcc_add_dll(s1
, filename
, 0);
2826 static inline int new_undef_syms(void)
2829 ret
= new_undef_sym
;
2834 static int ld_add_file_list(TCCState
*s1
, const char *cmd
, int as_needed
)
2836 char filename
[1024], libname
[1024];
2837 int t
, group
, nblibs
= 0, ret
= 0;
2840 group
= !strcmp(cmd
, "GROUP");
2843 t
= ld_next(s1
, filename
, sizeof(filename
));
2846 t
= ld_next(s1
, filename
, sizeof(filename
));
2849 if (t
== LD_TOK_EOF
) {
2850 tcc_error_noabort("unexpected end of file");
2852 goto lib_parse_error
;
2853 } else if (t
== ')') {
2855 } else if (t
== '-') {
2856 t
= ld_next(s1
, filename
, sizeof(filename
));
2857 if ((t
!= LD_TOK_NAME
) || (filename
[0] != 'l')) {
2858 tcc_error_noabort("library name expected");
2860 goto lib_parse_error
;
2862 pstrcpy(libname
, sizeof libname
, &filename
[1]);
2863 if (s1
->static_link
) {
2864 snprintf(filename
, sizeof filename
, "lib%s.a", libname
);
2866 snprintf(filename
, sizeof filename
, "lib%s.so", libname
);
2868 } else if (t
!= LD_TOK_NAME
) {
2869 tcc_error_noabort("filename expected");
2871 goto lib_parse_error
;
2873 if (!strcmp(filename
, "AS_NEEDED")) {
2874 ret
= ld_add_file_list(s1
, cmd
, 1);
2876 goto lib_parse_error
;
2878 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2880 ret
= ld_add_file(s1
, filename
);
2882 goto lib_parse_error
;
2884 /* Add the filename *and* the libname to avoid future conversions */
2885 dynarray_add(&libs
, &nblibs
, tcc_strdup(filename
));
2886 if (libname
[0] != '\0')
2887 dynarray_add(&libs
, &nblibs
, tcc_strdup(libname
));
2891 t
= ld_next(s1
, filename
, sizeof(filename
));
2893 t
= ld_next(s1
, filename
, sizeof(filename
));
2896 if (group
&& !as_needed
) {
2897 while (new_undef_syms()) {
2900 for (i
= 0; i
< nblibs
; i
++)
2901 ld_add_file(s1
, libs
[i
]);
2905 dynarray_reset(&libs
, &nblibs
);
2909 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2911 ST_FUNC
int tcc_load_ldscript(TCCState
*s1
)
2914 char filename
[1024];
2919 t
= ld_next(s1
, cmd
, sizeof(cmd
));
2920 if (t
== LD_TOK_EOF
)
2922 else if (t
!= LD_TOK_NAME
)
2924 if (!strcmp(cmd
, "INPUT") ||
2925 !strcmp(cmd
, "GROUP")) {
2926 ret
= ld_add_file_list(s1
, cmd
, 0);
2929 } else if (!strcmp(cmd
, "OUTPUT_FORMAT") ||
2930 !strcmp(cmd
, "TARGET")) {
2931 /* ignore some commands */
2932 t
= ld_next(s1
, cmd
, sizeof(cmd
));
2936 t
= ld_next(s1
, filename
, sizeof(filename
));
2937 if (t
== LD_TOK_EOF
) {
2938 tcc_error_noabort("unexpected end of file");
2940 } else if (t
== ')') {
2950 #endif /* !TCC_TARGET_PE */