various stuff
[tinycc.git] / tccelf.c
blob22ce5df9c8d711cd064d99686d1aec6775d1c704
1 /*
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
21 #include "tcc.h"
23 /* Define this to get some debug output during relocation processing. */
24 #undef DEBUG_RELOC
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 */
32 #ifdef CONFIG_TCC_ASM
33 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
34 #endif
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 */
39 #endif
40 /* symbol sections */
41 ST_DATA Section *symtab_section, *strtab_section;
42 /* debug sections */
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)
52 /* no section zero */
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,
64 ".strtab",
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,
71 ".dynstrtab",
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);
85 #endif
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;
94 /* put first entry */
95 put_stabs("", 0, 0, 0, 0);
98 static void free_section(Section *s)
100 tcc_free(s->data);
103 ST_FUNC void tccelf_delete(TCCState *s1)
105 int i;
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 */
117 #ifdef TCC_IS_NATIVE
118 for ( i = 0; i < s1->nb_loaded_dlls; i++) {
119 DLLReference *ref = s1->loaded_dlls[i];
120 if ( ref->handle )
121 # ifdef _WIN32
122 FreeLibrary((HMODULE)ref->handle);
123 # else
124 dlclose(ref->handle);
125 # endif
127 #endif
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)
135 Section *sec;
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;
141 switch(sh_type) {
142 case SHT_HASH:
143 case SHT_REL:
144 case SHT_RELA:
145 case SHT_DYNSYM:
146 case SHT_SYMTAB:
147 case SHT_DYNAMIC:
148 sec->sh_addralign = 4;
149 break;
150 case SHT_STRTAB:
151 sec->sh_addralign = 1;
152 break;
153 default:
154 sec->sh_addralign = PTR_SIZE; /* gcc/pcc default alignment */
155 break;
158 if (sh_flags & SHF_PRIVATE) {
159 dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, sec);
160 } else {
161 sec->sh_num = s1->nb_sections;
162 dynarray_add(&s1->sections, &s1->nb_sections, sec);
165 return 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);
183 nb_buckets = 1;
185 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
186 hash->sh_entsize = sizeof(int);
187 symtab->hash = hash;
188 hash->link = symtab;
190 ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
191 ptr[0] = nb_buckets;
192 ptr[1] = 1;
193 memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
194 return symtab;
197 /* realloc section and set its content to zero */
198 ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
200 unsigned long size;
201 unsigned char *data;
203 size = sec->data_allocated;
204 if (size == 0)
205 size = 1;
206 while (size < new_size)
207 size = size * 2;
208 data = tcc_realloc(sec->data, size);
209 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
210 sec->data = data;
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;
227 return offset;
230 /* reserve at least 'size' bytes in section 'sec' from
231 sec->data_offset. */
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
248 exists */
249 ST_FUNC Section *find_section(TCCState *s1, const char *name)
251 Section *sec;
252 int i;
253 for(i = 1; i < s1->nb_sections; i++) {
254 sec = s1->sections[i];
255 if (!strcmp(name, sec->name))
256 return sec;
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)
266 int offset, len;
267 char *ptr;
269 len = strlen(sym) + 1;
270 offset = s->data_offset;
271 ptr = section_ptr_add(s, len);
272 memcpy(ptr, sym, len);
273 return offset;
276 /* elf symbol hashing function */
277 static unsigned long elf_hash(const unsigned char *name)
279 unsigned long h = 0, g;
281 while (*name) {
282 h = (h << 4) + *name++;
283 g = h & 0xf0000000;
284 if (g)
285 h ^= g >> 24;
286 h &= ~g;
288 return h;
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)
295 ElfW(Sym) *sym;
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));
304 ptr[0] = nb_buckets;
305 ptr[1] = nb_syms;
306 ptr += 2;
307 hash = ptr;
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 h = elf_hash(strtab + sym->st_name) % nb_buckets;
315 *ptr = hash[h];
316 hash[h] = sym_index;
317 } else {
318 *ptr = 0;
320 ptr++;
321 sym++;
325 /* return the symbol number */
326 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
327 int info, int other, int shndx, const char *name)
329 int name_offset, sym_index;
330 int nbuckets, h;
331 ElfW(Sym) *sym;
332 Section *hs;
334 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
335 if (name)
336 name_offset = put_elf_str(s->link, name);
337 else
338 name_offset = 0;
339 /* XXX: endianness */
340 sym->st_name = name_offset;
341 sym->st_value = value;
342 sym->st_size = size;
343 sym->st_info = info;
344 sym->st_other = other;
345 sym->st_shndx = shndx;
346 sym_index = sym - (ElfW(Sym) *)s->data;
347 hs = s->hash;
348 if (hs) {
349 int *ptr, *base;
350 ptr = section_ptr_add(hs, sizeof(int));
351 base = (int *)hs->data;
352 /* only add global or weak symbols */
353 if (ELFW(ST_BIND)(info) != STB_LOCAL) {
354 /* add another hashing entry */
355 nbuckets = base[0];
356 h = elf_hash((unsigned char *) name) % nbuckets;
357 *ptr = base[2 + h];
358 base[2 + h] = sym_index;
359 base[1]++;
360 /* we resize the hash table */
361 hs->nb_hashed_syms++;
362 if (hs->nb_hashed_syms > 2 * nbuckets) {
363 rebuild_hash(s, 2 * nbuckets);
365 } else {
366 *ptr = 0;
367 base[1]++;
370 return sym_index;
373 /* find global ELF symbol 'name' and return its index. Return 0 if not
374 found. */
375 ST_FUNC int find_elf_sym(Section *s, const char *name)
377 ElfW(Sym) *sym;
378 Section *hs;
379 int nbuckets, sym_index, h;
380 const char *name1;
382 hs = s->hash;
383 if (!hs)
384 return 0;
385 nbuckets = ((int *)hs->data)[0];
386 h = elf_hash((unsigned char *) name) % nbuckets;
387 sym_index = ((int *)hs->data)[2 + h];
388 while (sym_index != 0) {
389 sym = &((ElfW(Sym) *)s->data)[sym_index];
390 name1 = (char *) s->link->data + sym->st_name;
391 if (!strcmp(name, name1))
392 return sym_index;
393 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
395 return 0;
398 /* return elf symbol value, signal error if 'err' is nonzero */
399 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err)
401 int sym_index;
402 ElfW(Sym) *sym;
404 sym_index = find_elf_sym(s->symtab, name);
405 sym = &((ElfW(Sym) *)s->symtab->data)[sym_index];
406 if (!sym_index || sym->st_shndx == SHN_UNDEF) {
407 if (err)
408 tcc_error("%s not defined", name);
409 return 0;
411 return sym->st_value;
414 /* return elf symbol value */
415 LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
417 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0);
420 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
421 /* return elf symbol value or error */
422 ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)
424 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 1);
426 #endif
428 /* add an elf symbol : check if it is already defined and patch
429 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
430 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
431 int info, int other, int shndx, const char *name)
433 ElfW(Sym) *esym;
434 int sym_bind, sym_index, sym_type, esym_bind;
435 unsigned char sym_vis, esym_vis, new_vis;
437 sym_bind = ELFW(ST_BIND)(info);
438 sym_type = ELFW(ST_TYPE)(info);
439 sym_vis = ELFW(ST_VISIBILITY)(other);
441 sym_index = find_elf_sym(s, name);
442 esym = &((ElfW(Sym) *)s->data)[sym_index];
443 if (sym_index && esym->st_value == value && esym->st_size == size
444 && esym->st_info == info && esym->st_other == other
445 && esym->st_shndx == shndx)
446 return sym_index;
448 if (sym_bind != STB_LOCAL) {
449 /* we search global or weak symbols */
450 if (!sym_index)
451 goto do_def;
452 if (esym->st_shndx != SHN_UNDEF) {
453 esym_bind = ELFW(ST_BIND)(esym->st_info);
454 /* propagate the most constraining visibility */
455 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
456 esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
457 if (esym_vis == STV_DEFAULT) {
458 new_vis = sym_vis;
459 } else if (sym_vis == STV_DEFAULT) {
460 new_vis = esym_vis;
461 } else {
462 new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
464 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
465 | new_vis;
466 other = esym->st_other; /* in case we have to patch esym */
467 if (shndx == SHN_UNDEF) {
468 /* ignore adding of undefined symbol if the
469 corresponding symbol is already defined */
470 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
471 /* global overrides weak, so patch */
472 goto do_patch;
473 } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
474 /* weak is ignored if already global */
475 } else if (sym_bind == STB_WEAK && esym_bind == STB_WEAK) {
476 /* keep first-found weak definition, ignore subsequents */
477 } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
478 /* ignore hidden symbols after */
479 } else if ((esym->st_shndx == SHN_COMMON
480 || esym->st_shndx == bss_section->sh_num)
481 && (shndx < SHN_LORESERVE
482 && shndx != bss_section->sh_num)) {
483 /* data symbol gets precedence over common/bss */
484 goto do_patch;
485 } else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
486 /* data symbol keeps precedence over common/bss */
487 } else if (s == tcc_state->dynsymtab_section) {
488 /* we accept that two DLL define the same symbol */
489 } else {
490 #if 0
491 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
492 sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
493 #endif
494 tcc_error_noabort("'%s' defined twice", name);
496 } else {
497 do_patch:
498 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
499 esym->st_shndx = shndx;
500 new_undef_sym = 1;
501 esym->st_value = value;
502 esym->st_size = size;
503 esym->st_other = other;
505 } else {
506 do_def:
507 sym_index = put_elf_sym(s, value, size,
508 ELFW(ST_INFO)(sym_bind, sym_type), other,
509 shndx, name);
511 return sym_index;
514 /* put relocation */
515 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
516 int type, int symbol, addr_t addend)
518 char buf[256];
519 Section *sr;
520 ElfW_Rel *rel;
522 sr = s->reloc;
523 if (!sr) {
524 /* if no relocation section, create it */
525 snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
526 /* if the symtab is allocated, then we consider the relocation
527 are also */
528 sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
529 sr->sh_entsize = sizeof(ElfW_Rel);
530 sr->link = symtab;
531 sr->sh_info = s->sh_num;
532 s->reloc = sr;
534 rel = section_ptr_add(sr, sizeof(ElfW_Rel));
535 rel->r_offset = offset;
536 rel->r_info = ELFW(R_INFO)(symbol, type);
537 #if SHT_RELX == SHT_RELA
538 rel->r_addend = addend;
539 #else
540 if (addend)
541 tcc_error("non-zero addend on REL architecture");
542 #endif
545 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
546 int type, int symbol)
548 put_elf_reloca(symtab, s, offset, type, symbol, 0);
551 /* Remove relocations for section S->reloc starting at oldrelocoffset
552 that are to the same place, retaining the last of them. As side effect
553 the relocations are sorted. Possibly reduces the number of relocs. */
554 ST_FUNC void squeeze_multi_relocs(Section *s, size_t oldrelocoffset)
556 Section *sr = s->reloc;
557 ElfW_Rel *r, *dest;
558 ssize_t a;
559 ElfW(Addr) addr;
561 if (oldrelocoffset + sizeof(*r) >= sr->data_offset)
562 return;
563 /* The relocs we're dealing with are the result of initializer parsing.
564 So they will be mostly in order and there aren't many of them.
565 Secondly we need a stable sort (which qsort isn't). We use
566 a simple insertion sort. */
567 for (a = oldrelocoffset + sizeof(*r); a < sr->data_offset; a += sizeof(*r)) {
568 ssize_t i = a - sizeof(*r);
569 addr = ((ElfW_Rel*)(sr->data + a))->r_offset;
570 for (; i >= (ssize_t)oldrelocoffset &&
571 ((ElfW_Rel*)(sr->data + i))->r_offset > addr; i -= sizeof(*r)) {
572 ElfW_Rel tmp = *(ElfW_Rel*)(sr->data + a);
573 *(ElfW_Rel*)(sr->data + a) = *(ElfW_Rel*)(sr->data + i);
574 *(ElfW_Rel*)(sr->data + i) = tmp;
578 r = (ElfW_Rel*)(sr->data + oldrelocoffset);
579 dest = r;
580 for (; r < (ElfW_Rel*)(sr->data + sr->data_offset); r++) {
581 if (dest->r_offset != r->r_offset)
582 dest++;
583 *dest = *r;
585 sr->data_offset = (unsigned char*)dest - sr->data + sizeof(*r);
588 /* put stab debug information */
590 ST_FUNC void put_stabs(const char *str, int type, int other, int desc,
591 unsigned long value)
593 Stab_Sym *sym;
595 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
596 if (str) {
597 sym->n_strx = put_elf_str(stabstr_section, str);
598 } else {
599 sym->n_strx = 0;
601 sym->n_type = type;
602 sym->n_other = other;
603 sym->n_desc = desc;
604 sym->n_value = value;
607 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc,
608 unsigned long value, Section *sec, int sym_index)
610 put_stabs(str, type, other, desc, value);
611 put_elf_reloc(symtab_section, stab_section,
612 stab_section->data_offset - sizeof(unsigned int),
613 R_DATA_32, sym_index);
616 ST_FUNC void put_stabn(int type, int other, int desc, int value)
618 put_stabs(NULL, type, other, desc, value);
621 ST_FUNC void put_stabd(int type, int other, int desc)
623 put_stabs(NULL, type, other, desc, 0);
626 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
628 int n;
629 struct sym_attr *tab;
631 if (index >= s1->nb_sym_attrs) {
632 if (!alloc)
633 return s1->sym_attrs;
634 /* find immediately bigger power of 2 and reallocate array */
635 n = 1;
636 while (index >= n)
637 n *= 2;
638 tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
639 s1->sym_attrs = tab;
640 memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
641 (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
642 s1->nb_sym_attrs = n;
644 return &s1->sym_attrs[index];
647 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
648 using variable <elem> */
649 #define for_each_elem(sec, startoff, elem, type) \
650 for (elem = (type *) sec->data + startoff; \
651 elem < (type *) (sec->data + sec->data_offset); elem++)
653 /* In an ELF file symbol table, the local symbols must appear below
654 the global and weak ones. Since TCC cannot sort it while generating
655 the code, we must do it after. All the relocation tables are also
656 modified to take into account the symbol table sorting */
657 static void sort_syms(TCCState *s1, Section *s)
659 int *old_to_new_syms;
660 ElfW(Sym) *new_syms;
661 int nb_syms, i;
662 ElfW(Sym) *p, *q;
663 ElfW_Rel *rel;
664 Section *sr;
665 int type, sym_index;
667 nb_syms = s->data_offset / sizeof(ElfW(Sym));
668 new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
669 old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
671 /* first pass for local symbols */
672 p = (ElfW(Sym) *)s->data;
673 q = new_syms;
674 for(i = 0; i < nb_syms; i++) {
675 if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
676 old_to_new_syms[i] = q - new_syms;
677 *q++ = *p;
679 p++;
681 /* save the number of local symbols in section header */
682 if( s->sh_size ) /* this 'if' makes IDA happy */
683 s->sh_info = q - new_syms;
685 /* then second pass for non local symbols */
686 p = (ElfW(Sym) *)s->data;
687 for(i = 0; i < nb_syms; i++) {
688 if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
689 old_to_new_syms[i] = q - new_syms;
690 *q++ = *p;
692 p++;
695 /* we copy the new symbols to the old */
696 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
697 tcc_free(new_syms);
699 /* now we modify all the relocations */
700 for(i = 1; i < s1->nb_sections; i++) {
701 sr = s1->sections[i];
702 if (sr->sh_type == SHT_RELX && sr->link == s) {
703 for_each_elem(sr, 0, rel, ElfW_Rel) {
704 sym_index = ELFW(R_SYM)(rel->r_info);
705 type = ELFW(R_TYPE)(rel->r_info);
706 sym_index = old_to_new_syms[sym_index];
707 rel->r_info = ELFW(R_INFO)(sym_index, type);
712 tcc_free(old_to_new_syms);
715 /* relocate common symbols in the .bss section */
716 ST_FUNC void relocate_common_syms(void)
718 ElfW(Sym) *sym;
720 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
721 if (sym->st_shndx == SHN_COMMON) {
722 /* symbol alignment is in st_value for SHN_COMMONs */
723 sym->st_value = section_add(bss_section, sym->st_size,
724 sym->st_value);
725 sym->st_shndx = bss_section->sh_num;
730 /* relocate symbol table, resolve undefined symbols if do_resolve is
731 true and output error if undefined symbol. */
732 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
734 ElfW(Sym) *sym;
735 int sym_bind, sh_num;
736 const char *name;
738 for_each_elem(symtab, 1, sym, ElfW(Sym)) {
739 sh_num = sym->st_shndx;
740 if (sh_num == SHN_UNDEF) {
741 name = (char *) strtab_section->data + sym->st_name;
742 /* Use ld.so to resolve symbol for us (for tcc -run) */
743 if (do_resolve) {
744 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
745 void *addr = dlsym(RTLD_DEFAULT, name);
746 if (addr) {
747 sym->st_value = (addr_t) addr;
748 #ifdef DEBUG_RELOC
749 printf ("relocate_sym: %s -> 0x%lx\n", name, sym->st_value);
750 #endif
751 goto found;
753 #endif
754 /* if dynamic symbol exist, it will be used in relocate_section */
755 } else if (s1->dynsym && find_elf_sym(s1->dynsym, name))
756 goto found;
757 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
758 it */
759 if (!strcmp(name, "_fp_hw"))
760 goto found;
761 /* only weak symbols are accepted to be undefined. Their
762 value is zero */
763 sym_bind = ELFW(ST_BIND)(sym->st_info);
764 if (sym_bind == STB_WEAK)
765 sym->st_value = 0;
766 else
767 tcc_error_noabort("undefined symbol '%s'", name);
768 } else if (sh_num < SHN_LORESERVE) {
769 /* add section base */
770 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
772 found: ;
776 /* relocate a given section (CPU dependent) by applying the relocations
777 in the associated relocation section */
778 ST_FUNC void relocate_section(TCCState *s1, Section *s)
780 Section *sr = s->reloc;
781 ElfW_Rel *rel;
782 ElfW(Sym) *sym;
783 int type, sym_index;
784 unsigned char *ptr;
785 addr_t tgt, addr;
787 relocate_init(sr);
789 for_each_elem(sr, 0, rel, ElfW_Rel) {
790 ptr = s->data + rel->r_offset;
791 sym_index = ELFW(R_SYM)(rel->r_info);
792 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
793 type = ELFW(R_TYPE)(rel->r_info);
794 tgt = sym->st_value;
795 #if SHT_RELX == SHT_RELA
796 tgt += rel->r_addend;
797 #endif
798 addr = s->sh_addr + rel->r_offset;
799 relocate(s1, rel, type, ptr, addr, tgt);
801 /* if the relocation is allocated, we change its symbol table */
802 if (sr->sh_flags & SHF_ALLOC)
803 sr->link = s1->dynsym;
806 /* relocate relocation table in 'sr' */
807 static void relocate_rel(TCCState *s1, Section *sr)
809 Section *s;
810 ElfW_Rel *rel;
812 s = s1->sections[sr->sh_info];
813 for_each_elem(sr, 0, rel, ElfW_Rel)
814 rel->r_offset += s->sh_addr;
817 /* count the number of dynamic relocations so that we can reserve
818 their space */
819 static int prepare_dynamic_rel(TCCState *s1, Section *sr)
821 ElfW_Rel *rel;
822 int sym_index, type, count;
824 count = 0;
825 for_each_elem(sr, 0, rel, ElfW_Rel) {
826 sym_index = ELFW(R_SYM)(rel->r_info);
827 type = ELFW(R_TYPE)(rel->r_info);
828 switch(type) {
829 #if defined(TCC_TARGET_I386)
830 case R_386_32:
831 if (!get_sym_attr(s1, sym_index, 0)->dyn_index
832 && ((ElfW(Sym)*)symtab_section->data + sym_index)->st_shndx == SHN_UNDEF) {
833 /* don't fixup unresolved (weak) symbols */
834 rel->r_info = ELFW(R_INFO)(sym_index, R_386_RELATIVE);
835 break;
837 #elif defined(TCC_TARGET_X86_64)
838 case R_X86_64_32:
839 case R_X86_64_32S:
840 case R_X86_64_64:
841 #endif
842 count++;
843 break;
844 #if defined(TCC_TARGET_I386)
845 case R_386_PC32:
846 #elif defined(TCC_TARGET_X86_64)
847 case R_X86_64_PC32:
848 #endif
849 if (get_sym_attr(s1, sym_index, 0)->dyn_index)
850 count++;
851 break;
852 default:
853 break;
856 if (count) {
857 /* allocate the section */
858 sr->sh_flags |= SHF_ALLOC;
859 sr->sh_size = count * sizeof(ElfW_Rel);
861 return count;
864 static void build_got(TCCState *s1)
866 /* if no got, then create it */
867 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
868 s1->got->sh_entsize = 4;
869 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
870 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
871 /* keep space for _DYNAMIC pointer and two dummy got entries */
872 section_ptr_add(s1->got, 3 * PTR_SIZE);
875 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
876 in s1->symtab. When creating the dynamic symbol table entry for the GOT
877 relocation, use 'size' and 'info' for the corresponding symbol metadata.
878 Returns the offset of the GOT or (if any) PLT entry. */
879 static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
880 unsigned long size,
881 int info, int sym_index)
883 int need_plt_entry;
884 const char *name;
885 ElfW(Sym) *sym;
886 struct sym_attr *attr;
887 unsigned got_offset;
888 char plt_name[100];
889 int len;
891 need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
892 attr = get_sym_attr(s1, sym_index, 1);
894 /* In case a function is both called and its address taken 2 GOT entries
895 are created, one for taking the address (GOT) and the other for the PLT
896 entry (PLTGOT). */
897 if (need_plt_entry ? attr->plt_offset : attr->got_offset)
898 return attr;
900 /* create the GOT entry */
901 got_offset = s1->got->data_offset;
902 section_ptr_add(s1->got, PTR_SIZE);
904 /* Create the GOT relocation that will insert the address of the object or
905 function of interest in the GOT entry. This is a static relocation for
906 memory output (dlsym will give us the address of symbols) and dynamic
907 relocation otherwise (executable and DLLs). The relocation should be
908 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
909 associated to a PLT entry) but is currently done at load time for an
910 unknown reason. */
912 sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
913 name = (char *) symtab_section->link->data + sym->st_name;
915 if (s1->dynsym) {
916 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
917 /* Hack alarm. We don't want to emit dynamic symbols
918 and symbol based relocs for STB_LOCAL symbols, but rather
919 want to resolve them directly. At this point the symbol
920 values aren't final yet, so we must defer this. We will later
921 have to create a RELATIVE reloc anyway, so we misuse the
922 relocation slot to smuggle the symbol reference until
923 fill_local_got_entries. Not that the sym_index is
924 relative to symtab_section, not s1->dynsym! Nevertheless
925 we use s1->dyn_sym so that if this is the first call
926 that got->reloc is correctly created. Also note that
927 RELATIVE relocs are not normally created for the .got,
928 so the types serves as a marker for later (and is retained
929 also for the final output, which is okay because then the
930 got is just normal data). */
931 put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE,
932 sym_index);
933 } else {
934 if (0 == attr->dyn_index)
935 attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size,
936 info, 0, sym->st_shndx, name);
937 put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type,
938 attr->dyn_index);
940 } else {
941 put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
942 sym_index);
945 if (need_plt_entry) {
946 if (!s1->plt) {
947 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
948 SHF_ALLOC | SHF_EXECINSTR);
949 s1->plt->sh_entsize = 4;
952 attr->plt_offset = create_plt_entry(s1, got_offset, attr);
954 /* create a symbol 'sym@plt' for the PLT jump vector */
955 len = strlen(name);
956 if (len > sizeof plt_name - 5)
957 len = sizeof plt_name - 5;
958 memcpy(plt_name, name, len);
959 strcpy(plt_name + len, "@plt");
960 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
961 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
963 } else {
964 attr->got_offset = got_offset;
967 return attr;
970 /* build GOT and PLT entries */
971 ST_FUNC void build_got_entries(TCCState *s1)
973 Section *s;
974 ElfW_Rel *rel;
975 ElfW(Sym) *sym;
976 int i, type, gotplt_entry, reloc_type, sym_index;
977 struct sym_attr *attr;
979 for(i = 1; i < s1->nb_sections; i++) {
980 s = s1->sections[i];
981 if (s->sh_type != SHT_RELX)
982 continue;
983 /* no need to handle got relocations */
984 if (s->link != symtab_section)
985 continue;
986 for_each_elem(s, 0, rel, ElfW_Rel) {
987 type = ELFW(R_TYPE)(rel->r_info);
988 gotplt_entry = gotplt_entry_type(type);
989 sym_index = ELFW(R_SYM)(rel->r_info);
990 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
992 if (gotplt_entry == NO_GOTPLT_ENTRY) {
993 continue;
996 /* Automatically create PLT/GOT [entry] if it is an undefined
997 reference (resolved at runtime), or the symbol is absolute,
998 probably created by tcc_add_symbol, and thus on 64-bit
999 targets might be too far from application code. */
1000 if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
1001 if (sym->st_shndx == SHN_UNDEF) {
1002 ElfW(Sym) *esym;
1003 int dynindex;
1004 if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
1005 continue;
1006 /* Relocations for UNDEF symbols would normally need
1007 to be transferred into the executable or shared object.
1008 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1009 But TCC doesn't do that (at least for exes), so we
1010 need to resolve all such relocs locally. And that
1011 means PLT slots for functions in DLLs and COPY relocs for
1012 data symbols. COPY relocs were generated in
1013 bind_exe_dynsyms (and the symbol adjusted to be defined),
1014 and for functions we were generated a dynamic symbol
1015 of function type. */
1016 if (s1->dynsym) {
1017 /* dynsym isn't set for -run :-/ */
1018 dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index;
1019 esym = (ElfW(Sym) *)s1->dynsym->data + dynindex;
1020 if (dynindex
1021 && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
1022 || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
1023 && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC)))
1024 goto jmp_slot;
1026 } else if (!(sym->st_shndx == SHN_ABS
1027 #ifndef TCC_TARGET_ARM
1028 && PTR_SIZE == 8
1029 #endif
1031 continue;
1034 #ifdef TCC_TARGET_X86_64
1035 if ((type == R_X86_64_PLT32 || type == R_X86_64_PC32) &&
1036 (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT ||
1037 ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)) {
1038 rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
1039 continue;
1041 #endif
1042 if (code_reloc(type)) {
1043 jmp_slot:
1044 reloc_type = R_JMP_SLOT;
1045 } else
1046 reloc_type = R_GLOB_DAT;
1048 if (!s1->got)
1049 build_got(s1);
1051 if (gotplt_entry == BUILD_GOT_ONLY)
1052 continue;
1054 attr = put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1055 sym_index);
1057 if (reloc_type == R_JMP_SLOT)
1058 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
1063 /* put dynamic tag */
1064 static void put_dt(Section *dynamic, int dt, addr_t val)
1066 ElfW(Dyn) *dyn;
1067 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1068 dyn->d_tag = dt;
1069 dyn->d_un.d_val = val;
1072 #ifndef TCC_TARGET_PE
1073 static void add_init_array_defines(TCCState *s1, const char *section_name)
1075 Section *s;
1076 long end_offset;
1077 char sym_start[1024];
1078 char sym_end[1024];
1080 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1081 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1083 s = find_section(s1, section_name);
1084 if (!s) {
1085 end_offset = 0;
1086 s = data_section;
1087 } else {
1088 end_offset = s->data_offset;
1091 set_elf_sym(symtab_section,
1092 0, 0,
1093 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1094 s->sh_num, sym_start);
1095 set_elf_sym(symtab_section,
1096 end_offset, 0,
1097 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1098 s->sh_num, sym_end);
1100 #endif
1102 static int tcc_add_support(TCCState *s1, const char *filename)
1104 char buf[1024];
1105 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
1106 return tcc_add_file(s1, buf);
1109 ST_FUNC void tcc_add_bcheck(TCCState *s1)
1111 #ifdef CONFIG_TCC_BCHECK
1112 addr_t *ptr;
1113 int sym_index;
1115 if (0 == s1->do_bounds_check)
1116 return;
1117 /* XXX: add an object file to do that */
1118 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1119 *ptr = 0;
1120 set_elf_sym(symtab_section, 0, 0,
1121 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1122 bounds_section->sh_num, "__bounds_start");
1123 /* pull bcheck.o from libtcc1.a */
1124 sym_index = set_elf_sym(symtab_section, 0, 0,
1125 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1126 SHN_UNDEF, "__bound_init");
1127 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1128 /* add 'call __bound_init()' in .init section */
1129 Section *init_section = find_section(s1, ".init");
1130 unsigned char *pinit = section_ptr_add(init_section, 5);
1131 pinit[0] = 0xe8;
1132 write32le(pinit + 1, -4);
1133 put_elf_reloc(symtab_section, init_section,
1134 init_section->data_offset - 4, R_386_PC32, sym_index);
1135 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1137 #endif
1140 /* add tcc runtime libraries */
1141 ST_FUNC void tcc_add_runtime(TCCState *s1)
1143 tcc_add_bcheck(s1);
1144 tcc_add_pragma_libs(s1);
1145 /* add libc */
1146 if (!s1->nostdlib) {
1147 tcc_add_library_err(s1, "c");
1148 #ifdef TCC_LIBGCC
1149 if (!s1->static_link) {
1150 if (TCC_LIBGCC[0] == '/')
1151 tcc_add_file(s1, TCC_LIBGCC);
1152 else
1153 tcc_add_dll(s1, TCC_LIBGCC, 0);
1155 #endif
1156 tcc_add_support(s1, TCC_LIBTCC1);
1157 /* add crt end if not memory output */
1158 if (s1->output_type != TCC_OUTPUT_MEMORY)
1159 tcc_add_crt(s1, "crtn.o");
1163 /* add various standard linker symbols (must be done after the
1164 sections are filled (for example after allocating common
1165 symbols)) */
1166 ST_FUNC void tcc_add_linker_symbols(TCCState *s1)
1168 char buf[1024];
1169 int i;
1170 Section *s;
1172 set_elf_sym(symtab_section,
1173 text_section->data_offset, 0,
1174 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1175 text_section->sh_num, "_etext");
1176 set_elf_sym(symtab_section,
1177 data_section->data_offset, 0,
1178 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1179 data_section->sh_num, "_edata");
1180 set_elf_sym(symtab_section,
1181 bss_section->data_offset, 0,
1182 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1183 bss_section->sh_num, "_end");
1184 #ifndef TCC_TARGET_PE
1185 /* horrible new standard ldscript defines */
1186 add_init_array_defines(s1, ".preinit_array");
1187 add_init_array_defines(s1, ".init_array");
1188 add_init_array_defines(s1, ".fini_array");
1189 #endif
1191 /* add start and stop symbols for sections whose name can be
1192 expressed in C */
1193 for(i = 1; i < s1->nb_sections; i++) {
1194 s = s1->sections[i];
1195 if (s->sh_type == SHT_PROGBITS &&
1196 (s->sh_flags & SHF_ALLOC)) {
1197 const char *p;
1198 int ch;
1200 /* check if section name can be expressed in C */
1201 p = s->name;
1202 for(;;) {
1203 ch = *p;
1204 if (!ch)
1205 break;
1206 if (!isid(ch) && !isnum(ch))
1207 goto next_sec;
1208 p++;
1210 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1211 set_elf_sym(symtab_section,
1212 0, 0,
1213 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1214 s->sh_num, buf);
1215 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1216 set_elf_sym(symtab_section,
1217 s->data_offset, 0,
1218 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1219 s->sh_num, buf);
1221 next_sec: ;
1225 static void tcc_output_binary(TCCState *s1, FILE *f,
1226 const int *sec_order)
1228 Section *s;
1229 int i, offset, size;
1231 offset = 0;
1232 for(i=1;i<s1->nb_sections;i++) {
1233 s = s1->sections[sec_order[i]];
1234 if (s->sh_type != SHT_NOBITS &&
1235 (s->sh_flags & SHF_ALLOC)) {
1236 while (offset < s->sh_offset) {
1237 fputc(0, f);
1238 offset++;
1240 size = s->sh_size;
1241 fwrite(s->data, 1, size, f);
1242 offset += size;
1247 ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1249 int sym_index = ELFW(R_SYM) (rel->r_info);
1250 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1251 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1252 unsigned offset = attr->got_offset;
1254 if (0 == offset)
1255 return;
1256 section_reserve(s1->got, offset + PTR_SIZE);
1257 #ifdef TCC_TARGET_X86_64
1258 write64le(s1->got->data + offset, sym->st_value);
1259 #else
1260 write32le(s1->got->data + offset, sym->st_value);
1261 #endif
1264 /* Perform relocation to GOT or PLT entries */
1265 ST_FUNC void fill_got(TCCState *s1)
1267 Section *s;
1268 ElfW_Rel *rel;
1269 int i;
1271 for(i = 1; i < s1->nb_sections; i++) {
1272 s = s1->sections[i];
1273 if (s->sh_type != SHT_RELX)
1274 continue;
1275 /* no need to handle got relocations */
1276 if (s->link != symtab_section)
1277 continue;
1278 for_each_elem(s, 0, rel, ElfW_Rel) {
1279 switch (ELFW(R_TYPE) (rel->r_info)) {
1280 case R_X86_64_GOT32:
1281 case R_X86_64_GOTPCREL:
1282 case R_X86_64_GOTPCRELX:
1283 case R_X86_64_REX_GOTPCRELX:
1284 case R_X86_64_PLT32:
1285 fill_got_entry(s1, rel);
1286 break;
1292 /* See put_got_entry for a description. This is the second stage
1293 where GOT references to local defined symbols are rewritten. */
1294 static void fill_local_got_entries(TCCState *s1)
1296 ElfW_Rel *rel;
1297 for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
1298 if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
1299 int sym_index = ELFW(R_SYM) (rel->r_info);
1300 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1301 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1302 unsigned offset = attr->got_offset;
1303 if (offset != rel->r_offset - s1->got->sh_addr)
1304 tcc_error_noabort("huh");
1305 rel->r_info = ELFW(R_INFO)(0, R_RELATIVE);
1306 #if SHT_RELX == SHT_RELA
1307 rel->r_addend = sym->st_value;
1308 #else
1309 /* All our REL architectures also happen to be 32bit LE. */
1310 write32le(s1->got->data + offset, sym->st_value);
1311 #endif
1316 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1317 in shared libraries and export non local defined symbols to shared libraries
1318 if -rdynamic switch was given on command line */
1319 static void bind_exe_dynsyms(TCCState *s1)
1321 const char *name;
1322 int sym_index, index;
1323 ElfW(Sym) *sym, *esym;
1324 int type;
1326 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1327 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1328 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1329 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1330 if (sym->st_shndx == SHN_UNDEF) {
1331 name = (char *) symtab_section->link->data + sym->st_name;
1332 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1333 if (sym_index) {
1334 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1335 type = ELFW(ST_TYPE)(esym->st_info);
1336 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
1337 /* Indirect functions shall have STT_FUNC type in executable
1338 * dynsym section. Indeed, a dlsym call following a lazy
1339 * resolution would pick the symbol value from the
1340 * executable dynsym entry which would contain the address
1341 * of the function wanted by the caller of dlsym instead of
1342 * the address of the function that would return that
1343 * address */
1344 int dynindex
1345 = put_elf_sym(s1->dynsym, 0, esym->st_size,
1346 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
1347 name);
1348 int index = sym - (ElfW(Sym) *) symtab_section->data;
1349 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1350 } else if (type == STT_OBJECT) {
1351 unsigned long offset;
1352 ElfW(Sym) *dynsym;
1353 offset = bss_section->data_offset;
1354 /* XXX: which alignment ? */
1355 offset = (offset + 16 - 1) & -16;
1356 set_elf_sym (s1->symtab, offset, esym->st_size,
1357 esym->st_info, 0, bss_section->sh_num, name);
1358 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1359 esym->st_info, 0, bss_section->sh_num,
1360 name);
1362 /* Ensure R_COPY works for weak symbol aliases */
1363 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1364 for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
1365 if ((dynsym->st_value == esym->st_value)
1366 && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
1367 char *dynname = (char *) s1->dynsymtab_section->link->data
1368 + dynsym->st_name;
1369 put_elf_sym(s1->dynsym, offset, dynsym->st_size,
1370 dynsym->st_info, 0,
1371 bss_section->sh_num, dynname);
1372 break;
1377 put_elf_reloc(s1->dynsym, bss_section,
1378 offset, R_COPY, index);
1379 offset += esym->st_size;
1380 bss_section->data_offset = offset;
1382 } else {
1383 /* STB_WEAK undefined symbols are accepted */
1384 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1385 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1386 !strcmp(name, "_fp_hw")) {
1387 } else {
1388 tcc_error_noabort("undefined symbol '%s'", name);
1391 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1392 /* if -rdynamic option, then export all non local symbols */
1393 name = (char *) symtab_section->link->data + sym->st_name;
1394 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1395 0, sym->st_shndx, name);
1400 /* Bind symbols of libraries: export all non local symbols of executable that
1401 are referenced by shared libraries. The reason is that the dynamic loader
1402 search symbol first in executable and then in libraries. Therefore a
1403 reference to a symbol already defined by a library can still be resolved by
1404 a symbol in the executable. */
1405 static void bind_libs_dynsyms(TCCState *s1)
1407 const char *name;
1408 int sym_index;
1409 ElfW(Sym) *sym, *esym;
1411 for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
1412 name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
1413 sym_index = find_elf_sym(symtab_section, name);
1414 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1415 if (sym_index && sym->st_shndx != SHN_UNDEF
1416 && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1417 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1418 sym->st_info, 0, sym->st_shndx, name);
1419 } else if (esym->st_shndx == SHN_UNDEF) {
1420 /* weak symbols can stay undefined */
1421 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
1422 tcc_warning("undefined dynamic symbol '%s'", name);
1427 /* Export all non local symbols. This is used by shared libraries so that the
1428 non local symbols they define can resolve a reference in another shared
1429 library or in the executable. Correspondingly, it allows undefined local
1430 symbols to be resolved by other shared libraries or by the executable. */
1431 static void export_global_syms(TCCState *s1)
1433 int dynindex, index;
1434 const char *name;
1435 ElfW(Sym) *sym;
1437 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1438 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1439 name = (char *) symtab_section->link->data + sym->st_name;
1440 dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1441 sym->st_info, 0, sym->st_shndx, name);
1442 index = sym - (ElfW(Sym) *) symtab_section->data;
1443 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1448 /* Allocate strings for section names and decide if an unallocated section
1449 should be output.
1450 NOTE: the strsec section comes last, so its size is also correct ! */
1451 static int alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
1453 int i;
1454 Section *s;
1455 int textrel = 0;
1457 /* Allocate strings for section names */
1458 for(i = 1; i < s1->nb_sections; i++) {
1459 s = s1->sections[i];
1460 /* when generating a DLL, we include relocations but we may
1461 patch them */
1462 if (file_type == TCC_OUTPUT_DLL &&
1463 s->sh_type == SHT_RELX &&
1464 !(s->sh_flags & SHF_ALLOC) &&
1465 (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) &&
1466 prepare_dynamic_rel(s1, s)) {
1467 if (s1->sections[s->sh_info]->sh_flags & SHF_EXECINSTR)
1468 textrel = 1;
1469 } else if (s1->do_debug ||
1470 file_type == TCC_OUTPUT_OBJ ||
1471 (s->sh_flags & SHF_ALLOC) ||
1472 i == (s1->nb_sections - 1)) {
1473 /* we output all sections if debug or object file */
1474 s->sh_size = s->data_offset;
1476 if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1477 s->sh_name = put_elf_str(strsec, s->name);
1479 strsec->sh_size = strsec->data_offset;
1480 return textrel;
1483 /* Info to be copied in dynamic section */
1484 struct dyn_inf {
1485 Section *dynamic;
1486 Section *dynstr;
1487 unsigned long data_offset;
1488 addr_t rel_addr;
1489 addr_t rel_size;
1490 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1491 addr_t bss_addr;
1492 addr_t bss_size;
1493 #endif
1496 /* Assign sections to segments and decide how are sections laid out when loaded
1497 in memory. This function also fills corresponding program headers. */
1498 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1499 Section *interp, Section* strsec,
1500 struct dyn_inf *dyninf, int *sec_order)
1502 int i, j, k, file_type, sh_order_index, file_offset;
1503 unsigned long s_align;
1504 long long tmp;
1505 addr_t addr;
1506 ElfW(Phdr) *ph;
1507 Section *s;
1509 file_type = s1->output_type;
1510 sh_order_index = 1;
1511 file_offset = 0;
1512 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1513 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1514 s_align = ELF_PAGE_SIZE;
1515 if (s1->section_align)
1516 s_align = s1->section_align;
1518 if (phnum > 0) {
1519 if (s1->has_text_addr) {
1520 int a_offset, p_offset;
1521 addr = s1->text_addr;
1522 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1523 ELF_PAGE_SIZE */
1524 a_offset = (int) (addr & (s_align - 1));
1525 p_offset = file_offset & (s_align - 1);
1526 if (a_offset < p_offset)
1527 a_offset += s_align;
1528 file_offset += (a_offset - p_offset);
1529 } else {
1530 if (file_type == TCC_OUTPUT_DLL)
1531 addr = 0;
1532 else
1533 addr = ELF_START_ADDR;
1534 /* compute address after headers */
1535 addr += (file_offset & (s_align - 1));
1538 ph = &phdr[0];
1539 /* Leave one program headers for the program interpreter and one for
1540 the program header table itself if needed. These are done later as
1541 they require section layout to be done first. */
1542 if (interp)
1543 ph += 2;
1545 /* dynamic relocation table information, for .dynamic section */
1546 dyninf->rel_addr = dyninf->rel_size = 0;
1547 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1548 dyninf->bss_addr = dyninf->bss_size = 0;
1549 #endif
1551 for(j = 0; j < 2; j++) {
1552 ph->p_type = PT_LOAD;
1553 if (j == 0)
1554 ph->p_flags = PF_R | PF_X;
1555 else
1556 ph->p_flags = PF_R | PF_W;
1557 ph->p_align = s_align;
1559 /* Decide the layout of sections loaded in memory. This must
1560 be done before program headers are filled since they contain
1561 info about the layout. We do the following ordering: interp,
1562 symbol tables, relocations, progbits, nobits */
1563 /* XXX: do faster and simpler sorting */
1564 for(k = 0; k < 5; k++) {
1565 for(i = 1; i < s1->nb_sections; i++) {
1566 s = s1->sections[i];
1567 /* compute if section should be included */
1568 if (j == 0) {
1569 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1570 SHF_ALLOC)
1571 continue;
1572 } else {
1573 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1574 (SHF_ALLOC | SHF_WRITE))
1575 continue;
1577 if (s == interp) {
1578 if (k != 0)
1579 continue;
1580 } else if (s->sh_type == SHT_DYNSYM ||
1581 s->sh_type == SHT_STRTAB ||
1582 s->sh_type == SHT_HASH) {
1583 if (k != 1)
1584 continue;
1585 } else if (s->sh_type == SHT_RELX) {
1586 if (k != 2)
1587 continue;
1588 } else if (s->sh_type == SHT_NOBITS) {
1589 if (k != 4)
1590 continue;
1591 } else {
1592 if (k != 3)
1593 continue;
1595 sec_order[sh_order_index++] = i;
1597 /* section matches: we align it and add its size */
1598 tmp = addr;
1599 addr = (addr + s->sh_addralign - 1) &
1600 ~(s->sh_addralign - 1);
1601 file_offset += (int) ( addr - tmp );
1602 s->sh_offset = file_offset;
1603 s->sh_addr = addr;
1605 /* update program header infos */
1606 if (ph->p_offset == 0) {
1607 ph->p_offset = file_offset;
1608 ph->p_vaddr = addr;
1609 ph->p_paddr = ph->p_vaddr;
1611 /* update dynamic relocation infos */
1612 if (s->sh_type == SHT_RELX) {
1613 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1614 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1615 dyninf->rel_addr = addr;
1616 dyninf->rel_size += s->sh_size; /* XXX only first rel. */
1618 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1619 dyninf->bss_addr = addr;
1620 dyninf->bss_size = s->sh_size; /* XXX only first rel. */
1622 #else
1623 if (dyninf->rel_size == 0)
1624 dyninf->rel_addr = addr;
1625 dyninf->rel_size += s->sh_size;
1626 #endif
1628 addr += s->sh_size;
1629 if (s->sh_type != SHT_NOBITS)
1630 file_offset += s->sh_size;
1633 if (j == 0) {
1634 /* Make the first PT_LOAD segment include the program
1635 headers itself (and the ELF header as well), it'll
1636 come out with same memory use but will make various
1637 tools like binutils strip work better. */
1638 ph->p_offset &= ~(ph->p_align - 1);
1639 ph->p_vaddr &= ~(ph->p_align - 1);
1640 ph->p_paddr &= ~(ph->p_align - 1);
1642 ph->p_filesz = file_offset - ph->p_offset;
1643 ph->p_memsz = addr - ph->p_vaddr;
1644 ph++;
1645 if (j == 0) {
1646 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1647 /* if in the middle of a page, we duplicate the page in
1648 memory so that one copy is RX and the other is RW */
1649 if ((addr & (s_align - 1)) != 0)
1650 addr += s_align;
1651 } else {
1652 addr = (addr + s_align - 1) & ~(s_align - 1);
1653 file_offset = (file_offset + s_align - 1) & ~(s_align - 1);
1659 /* all other sections come after */
1660 for(i = 1; i < s1->nb_sections; i++) {
1661 s = s1->sections[i];
1662 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1663 continue;
1664 sec_order[sh_order_index++] = i;
1666 file_offset = (file_offset + s->sh_addralign - 1) &
1667 ~(s->sh_addralign - 1);
1668 s->sh_offset = file_offset;
1669 if (s->sh_type != SHT_NOBITS)
1670 file_offset += s->sh_size;
1673 return file_offset;
1676 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
1677 Section *dynamic)
1679 ElfW(Phdr) *ph;
1681 /* if interpreter, then add corresponding program header */
1682 if (interp) {
1683 ph = &phdr[0];
1685 ph->p_type = PT_PHDR;
1686 ph->p_offset = sizeof(ElfW(Ehdr));
1687 ph->p_filesz = ph->p_memsz = phnum * sizeof(ElfW(Phdr));
1688 ph->p_vaddr = interp->sh_addr - ph->p_filesz;
1689 ph->p_paddr = ph->p_vaddr;
1690 ph->p_flags = PF_R | PF_X;
1691 ph->p_align = 4; /* interp->sh_addralign; */
1692 ph++;
1694 ph->p_type = PT_INTERP;
1695 ph->p_offset = interp->sh_offset;
1696 ph->p_vaddr = interp->sh_addr;
1697 ph->p_paddr = ph->p_vaddr;
1698 ph->p_filesz = interp->sh_size;
1699 ph->p_memsz = interp->sh_size;
1700 ph->p_flags = PF_R;
1701 ph->p_align = interp->sh_addralign;
1704 /* if dynamic section, then add corresponding program header */
1705 if (dynamic) {
1706 ph = &phdr[phnum - 1];
1708 ph->p_type = PT_DYNAMIC;
1709 ph->p_offset = dynamic->sh_offset;
1710 ph->p_vaddr = dynamic->sh_addr;
1711 ph->p_paddr = ph->p_vaddr;
1712 ph->p_filesz = dynamic->sh_size;
1713 ph->p_memsz = dynamic->sh_size;
1714 ph->p_flags = PF_R | PF_W;
1715 ph->p_align = dynamic->sh_addralign;
1719 /* Fill the dynamic section with tags describing the address and size of
1720 sections */
1721 static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf)
1723 Section *dynamic = dyninf->dynamic;
1725 /* put dynamic section entries */
1726 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1727 put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
1728 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1729 put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
1730 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1731 #if PTR_SIZE == 8
1732 put_dt(dynamic, DT_RELA, dyninf->rel_addr);
1733 put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
1734 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1735 #else
1736 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1737 put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
1738 put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size);
1739 put_dt(dynamic, DT_JMPREL, dyninf->rel_addr);
1740 put_dt(dynamic, DT_PLTREL, DT_REL);
1741 put_dt(dynamic, DT_REL, dyninf->bss_addr);
1742 put_dt(dynamic, DT_RELSZ, dyninf->bss_size);
1743 #else
1744 put_dt(dynamic, DT_REL, dyninf->rel_addr);
1745 put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
1746 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1747 #endif
1748 #endif
1749 if (s1->do_debug)
1750 put_dt(dynamic, DT_DEBUG, 0);
1751 put_dt(dynamic, DT_NULL, 0);
1754 /* Relocate remaining sections and symbols (that is those not related to
1755 dynamic linking) */
1756 static int final_sections_reloc(TCCState *s1)
1758 int i;
1759 Section *s;
1761 relocate_syms(s1, s1->symtab, 0);
1763 if (s1->nb_errors != 0)
1764 return -1;
1766 /* relocate sections */
1767 /* XXX: ignore sections with allocated relocations ? */
1768 for(i = 1; i < s1->nb_sections; i++) {
1769 s = s1->sections[i];
1770 #if defined(TCC_TARGET_I386) || defined(TCC_MUSL)
1771 if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
1772 /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC
1773 checking is removed */
1774 #else
1775 if (s->reloc && s != s1->got)
1776 /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */
1777 #endif
1778 relocate_section(s1, s);
1781 /* relocate relocation entries if the relocation tables are
1782 allocated in the executable */
1783 for(i = 1; i < s1->nb_sections; i++) {
1784 s = s1->sections[i];
1785 if ((s->sh_flags & SHF_ALLOC) &&
1786 s->sh_type == SHT_RELX) {
1787 relocate_rel(s1, s);
1790 return 0;
1793 /* Create an ELF file on disk.
1794 This function handle ELF specific layout requirements */
1795 static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
1796 int file_offset, int *sec_order)
1798 int i, shnum, offset, size, file_type;
1799 Section *s;
1800 ElfW(Ehdr) ehdr;
1801 ElfW(Shdr) shdr, *sh;
1803 file_type = s1->output_type;
1804 shnum = s1->nb_sections;
1806 memset(&ehdr, 0, sizeof(ehdr));
1808 if (phnum > 0) {
1809 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1810 ehdr.e_phnum = phnum;
1811 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1814 /* align to 4 */
1815 file_offset = (file_offset + 3) & -4;
1817 /* fill header */
1818 ehdr.e_ident[0] = ELFMAG0;
1819 ehdr.e_ident[1] = ELFMAG1;
1820 ehdr.e_ident[2] = ELFMAG2;
1821 ehdr.e_ident[3] = ELFMAG3;
1822 ehdr.e_ident[4] = ELFCLASSW;
1823 ehdr.e_ident[5] = ELFDATA2LSB;
1824 ehdr.e_ident[6] = EV_CURRENT;
1825 #if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
1826 /* FIXME: should set only for freebsd _target_, but we exclude only PE target */
1827 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1828 #endif
1829 #ifdef TCC_TARGET_ARM
1830 #ifdef TCC_ARM_EABI
1831 ehdr.e_ident[EI_OSABI] = 0;
1832 ehdr.e_flags = EF_ARM_EABI_VER4;
1833 if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
1834 ehdr.e_flags |= EF_ARM_HASENTRY;
1835 if (s1->float_abi == ARM_HARD_FLOAT)
1836 ehdr.e_flags |= EF_ARM_VFP_FLOAT;
1837 else
1838 ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
1839 #else
1840 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1841 #endif
1842 #endif
1843 switch(file_type) {
1844 default:
1845 case TCC_OUTPUT_EXE:
1846 ehdr.e_type = ET_EXEC;
1847 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
1848 break;
1849 case TCC_OUTPUT_DLL:
1850 ehdr.e_type = ET_DYN;
1851 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1852 break;
1853 case TCC_OUTPUT_OBJ:
1854 ehdr.e_type = ET_REL;
1855 break;
1857 ehdr.e_machine = EM_TCC_TARGET;
1858 ehdr.e_version = EV_CURRENT;
1859 ehdr.e_shoff = file_offset;
1860 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1861 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1862 ehdr.e_shnum = shnum;
1863 ehdr.e_shstrndx = shnum - 1;
1865 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1866 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1867 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1869 sort_syms(s1, symtab_section);
1870 for(i = 1; i < s1->nb_sections; i++) {
1871 s = s1->sections[sec_order[i]];
1872 if (s->sh_type != SHT_NOBITS) {
1873 while (offset < s->sh_offset) {
1874 fputc(0, f);
1875 offset++;
1877 size = s->sh_size;
1878 if (size)
1879 fwrite(s->data, 1, size, f);
1880 offset += size;
1884 /* output section headers */
1885 while (offset < ehdr.e_shoff) {
1886 fputc(0, f);
1887 offset++;
1890 for(i = 0; i < s1->nb_sections; i++) {
1891 sh = &shdr;
1892 memset(sh, 0, sizeof(ElfW(Shdr)));
1893 s = s1->sections[i];
1894 if (s) {
1895 sh->sh_name = s->sh_name;
1896 sh->sh_type = s->sh_type;
1897 sh->sh_flags = s->sh_flags;
1898 sh->sh_entsize = s->sh_entsize;
1899 sh->sh_info = s->sh_info;
1900 if (s->link)
1901 sh->sh_link = s->link->sh_num;
1902 sh->sh_addralign = s->sh_addralign;
1903 sh->sh_addr = s->sh_addr;
1904 sh->sh_offset = s->sh_offset;
1905 sh->sh_size = s->sh_size;
1907 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1911 /* Write an elf, coff or "binary" file */
1912 static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
1913 ElfW(Phdr) *phdr, int file_offset, int *sec_order)
1915 int fd, mode, file_type;
1916 FILE *f;
1918 file_type = s1->output_type;
1919 if (file_type == TCC_OUTPUT_OBJ)
1920 mode = 0666;
1921 else
1922 mode = 0777;
1923 unlink(filename);
1924 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1925 if (fd < 0) {
1926 tcc_error_noabort("could not write '%s'", filename);
1927 return -1;
1929 f = fdopen(fd, "wb");
1930 if (s1->verbose)
1931 printf("<- %s\n", filename);
1933 #ifdef TCC_TARGET_COFF
1934 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
1935 tcc_output_coff(s1, f);
1936 else
1937 #endif
1938 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1939 tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
1940 else
1941 tcc_output_binary(s1, f, sec_order);
1942 fclose(f);
1944 return 0;
1947 /* Sort section headers by assigned sh_addr, remove sections
1948 that we aren't going to output. */
1949 static void tidy_section_headers(TCCState *s1, int *sec_order)
1951 int i, nnew, l, *backmap;
1952 Section **snew, *s;
1953 ElfW(Sym) *sym;
1955 snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
1956 backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
1957 for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
1958 s = s1->sections[sec_order[i]];
1959 if (!i || s->sh_name) {
1960 backmap[sec_order[i]] = nnew;
1961 snew[nnew] = s;
1962 ++nnew;
1963 } else {
1964 backmap[sec_order[i]] = 0;
1965 snew[--l] = s;
1968 for (i = 0; i < nnew; i++) {
1969 s = snew[i];
1970 if (s) {
1971 s->sh_num = i;
1972 if (s->sh_type == SHT_RELX)
1973 s->sh_info = backmap[s->sh_info];
1977 for_each_elem(symtab_section, 1, sym, ElfW(Sym))
1978 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1979 sym->st_shndx = backmap[sym->st_shndx];
1980 if( !s1->static_link ) {
1981 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
1982 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1983 sym->st_shndx = backmap[sym->st_shndx];
1985 for (i = 0; i < s1->nb_sections; i++)
1986 sec_order[i] = i;
1987 tcc_free(s1->sections);
1988 s1->sections = snew;
1989 s1->nb_sections = nnew;
1990 tcc_free(backmap);
1993 /* Output an elf, coff or binary file */
1994 /* XXX: suppress unneeded sections */
1995 static int elf_output_file(TCCState *s1, const char *filename)
1997 int i, ret, phnum, shnum, file_type, file_offset, *sec_order;
1998 struct dyn_inf dyninf = {0};
1999 ElfW(Phdr) *phdr;
2000 ElfW(Sym) *sym;
2001 Section *strsec, *interp, *dynamic, *dynstr;
2002 int textrel;
2004 file_type = s1->output_type;
2005 s1->nb_errors = 0;
2006 ret = -1;
2007 phdr = NULL;
2008 sec_order = NULL;
2009 interp = dynamic = dynstr = NULL; /* avoid warning */
2010 textrel = 0;
2012 if (file_type != TCC_OUTPUT_OBJ) {
2013 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2014 tcc_add_runtime(s1);
2015 relocate_common_syms();
2016 tcc_add_linker_symbols(s1);
2018 if (!s1->static_link) {
2019 if (file_type == TCC_OUTPUT_EXE) {
2020 char *ptr;
2021 /* allow override the dynamic loader */
2022 const char *elfint = getenv("LD_SO");
2023 if (elfint == NULL)
2024 elfint = DEFAULT_ELFINTERP(s1);
2025 /* add interpreter section only if executable */
2026 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
2027 interp->sh_addralign = 1;
2028 ptr = section_ptr_add(interp, 1 + strlen(elfint));
2029 strcpy(ptr, elfint);
2032 /* add dynamic symbol table */
2033 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
2034 ".dynstr",
2035 ".hash", SHF_ALLOC);
2036 dynstr = s1->dynsym->link;
2038 /* add dynamic section */
2039 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
2040 SHF_ALLOC | SHF_WRITE);
2041 dynamic->link = dynstr;
2042 dynamic->sh_entsize = sizeof(ElfW(Dyn));
2044 build_got(s1);
2046 if (file_type == TCC_OUTPUT_EXE) {
2047 bind_exe_dynsyms(s1);
2048 if (s1->nb_errors)
2049 goto the_end;
2050 bind_libs_dynsyms(s1);
2051 } else {
2052 /* shared library case: simply export all global symbols */
2053 export_global_syms(s1);
2056 build_got_entries(s1);
2059 /* we add a section for symbols */
2060 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
2061 put_elf_str(strsec, "");
2063 /* Allocate strings for section names */
2064 textrel = alloc_sec_names(s1, file_type, strsec);
2066 if (dynamic) {
2067 /* add a list of needed dlls */
2068 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2069 DLLReference *dllref = s1->loaded_dlls[i];
2070 if (dllref->level == 0)
2071 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
2074 if (s1->rpath)
2075 put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
2076 put_elf_str(dynstr, s1->rpath));
2078 if (file_type == TCC_OUTPUT_DLL) {
2079 if (s1->soname)
2080 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
2081 /* XXX: currently, since we do not handle PIC code, we
2082 must relocate the readonly segments */
2083 if (textrel)
2084 put_dt(dynamic, DT_TEXTREL, 0);
2087 if (s1->symbolic)
2088 put_dt(dynamic, DT_SYMBOLIC, 0);
2090 dyninf.dynamic = dynamic;
2091 dyninf.dynstr = dynstr;
2092 /* remember offset and reserve space for 2nd call below */
2093 dyninf.data_offset = dynamic->data_offset;
2094 fill_dynamic(s1, &dyninf);
2095 dynamic->sh_size = dynamic->data_offset;
2096 dynstr->sh_size = dynstr->data_offset;
2099 /* compute number of program headers */
2100 if (file_type == TCC_OUTPUT_OBJ)
2101 phnum = 0;
2102 else if (file_type == TCC_OUTPUT_DLL)
2103 phnum = 3;
2104 else if (s1->static_link)
2105 phnum = 2;
2106 else
2107 phnum = 5;
2109 /* allocate program segment headers */
2110 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
2112 /* compute number of sections */
2113 shnum = s1->nb_sections;
2115 /* this array is used to reorder sections in the output file */
2116 sec_order = tcc_malloc(sizeof(int) * shnum);
2117 sec_order[0] = 0;
2119 /* compute section to program header mapping */
2120 file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2121 sec_order);
2123 /* Fill remaining program header and finalize relocation related to dynamic
2124 linking. */
2125 if (file_type != TCC_OUTPUT_OBJ) {
2126 fill_unloadable_phdr(phdr, phnum, interp, dynamic);
2127 if (dynamic) {
2128 dynamic->data_offset = dyninf.data_offset;
2129 fill_dynamic(s1, &dyninf);
2131 /* put in GOT the dynamic section address and relocate PLT */
2132 write32le(s1->got->data, dynamic->sh_addr);
2133 if (file_type == TCC_OUTPUT_EXE
2134 || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
2135 relocate_plt(s1);
2137 /* relocate symbols in .dynsym now that final addresses are known */
2138 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
2139 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
2140 /* do symbol relocation */
2141 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
2146 /* if building executable or DLL, then relocate each section
2147 except the GOT which is already relocated */
2148 ret = final_sections_reloc(s1);
2149 if (ret)
2150 goto the_end;
2151 tidy_section_headers(s1, sec_order);
2153 /* Perform relocation to GOT or PLT entries */
2154 if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2155 fill_got(s1);
2156 else if (s1->got)
2157 fill_local_got_entries(s1);
2160 /* Create the ELF file with name 'filename' */
2161 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2162 s1->nb_sections = shnum;
2163 the_end:
2164 tcc_free(sec_order);
2165 tcc_free(phdr);
2166 return ret;
2169 LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
2171 int ret;
2172 #ifdef TCC_TARGET_PE
2173 if (s->output_type != TCC_OUTPUT_OBJ) {
2174 ret = pe_output_file(s, filename);
2175 } else
2176 #endif
2177 ret = elf_output_file(s, filename);
2178 return ret;
2181 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2183 void *data;
2185 data = tcc_malloc(size);
2186 lseek(fd, file_offset, SEEK_SET);
2187 read(fd, data, size);
2188 return data;
2191 typedef struct SectionMergeInfo {
2192 Section *s; /* corresponding existing section */
2193 unsigned long offset; /* offset of the new section in the existing section */
2194 uint8_t new_section; /* true if section 's' was added */
2195 uint8_t link_once; /* true if link once section */
2196 } SectionMergeInfo;
2198 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
2200 int size = read(fd, h, sizeof *h);
2201 if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
2202 if (h->e_type == ET_REL)
2203 return AFF_BINTYPE_REL;
2204 if (h->e_type == ET_DYN)
2205 return AFF_BINTYPE_DYN;
2206 } else if (size >= 8) {
2207 if (0 == memcmp(h, ARMAG, 8))
2208 return AFF_BINTYPE_AR;
2209 #ifdef TCC_TARGET_COFF
2210 if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC)
2211 return AFF_BINTYPE_C67;
2212 #endif
2214 return 0;
2217 /* load an object file and merge it with current files */
2218 /* XXX: handle correctly stab (debug) info */
2219 ST_FUNC int tcc_load_object_file(TCCState *s1,
2220 int fd, unsigned long file_offset)
2222 ElfW(Ehdr) ehdr;
2223 ElfW(Shdr) *shdr, *sh;
2224 int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed;
2225 unsigned char *strsec, *strtab;
2226 int *old_to_new_syms;
2227 char *sh_name, *name;
2228 SectionMergeInfo *sm_table, *sm;
2229 ElfW(Sym) *sym, *symtab;
2230 ElfW_Rel *rel;
2231 Section *s;
2233 int stab_index;
2234 int stabstr_index;
2236 stab_index = stabstr_index = 0;
2238 lseek(fd, file_offset, SEEK_SET);
2239 if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL)
2240 goto fail1;
2241 /* test CPU specific stuff */
2242 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2243 ehdr.e_machine != EM_TCC_TARGET) {
2244 fail1:
2245 tcc_error_noabort("invalid object file");
2246 return -1;
2248 /* read sections */
2249 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2250 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2251 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2253 /* load section names */
2254 sh = &shdr[ehdr.e_shstrndx];
2255 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2257 /* load symtab and strtab */
2258 old_to_new_syms = NULL;
2259 symtab = NULL;
2260 strtab = NULL;
2261 nb_syms = 0;
2262 seencompressed = 0;
2263 for(i = 1; i < ehdr.e_shnum; i++) {
2264 sh = &shdr[i];
2265 if (sh->sh_type == SHT_SYMTAB) {
2266 if (symtab) {
2267 tcc_error_noabort("object must contain only one symtab");
2268 fail:
2269 ret = -1;
2270 goto the_end;
2272 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2273 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2274 sm_table[i].s = symtab_section;
2276 /* now load strtab */
2277 sh = &shdr[sh->sh_link];
2278 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2280 if (sh->sh_flags & SHF_COMPRESSED)
2281 seencompressed = 1;
2284 /* now examine each section and try to merge its content with the
2285 ones in memory */
2286 for(i = 1; i < ehdr.e_shnum; i++) {
2287 /* no need to examine section name strtab */
2288 if (i == ehdr.e_shstrndx)
2289 continue;
2290 sh = &shdr[i];
2291 sh_name = (char *) strsec + sh->sh_name;
2292 /* ignore sections types we do not handle */
2293 if (sh->sh_type != SHT_PROGBITS &&
2294 sh->sh_type != SHT_RELX &&
2295 #ifdef TCC_ARM_EABI
2296 sh->sh_type != SHT_ARM_EXIDX &&
2297 #endif
2298 sh->sh_type != SHT_NOBITS &&
2299 sh->sh_type != SHT_PREINIT_ARRAY &&
2300 sh->sh_type != SHT_INIT_ARRAY &&
2301 sh->sh_type != SHT_FINI_ARRAY &&
2302 strcmp(sh_name, ".stabstr")
2304 continue;
2305 if (seencompressed
2306 && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1)
2307 || (sh->sh_type == SHT_RELX
2308 && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name,
2309 ".debug_", sizeof(".debug_")-1))))
2310 continue;
2311 if (sh->sh_addralign < 1)
2312 sh->sh_addralign = 1;
2313 /* find corresponding section, if any */
2314 for(j = 1; j < s1->nb_sections;j++) {
2315 s = s1->sections[j];
2316 if (!strcmp(s->name, sh_name)) {
2317 if (!strncmp(sh_name, ".gnu.linkonce",
2318 sizeof(".gnu.linkonce") - 1)) {
2319 /* if a 'linkonce' section is already present, we
2320 do not add it again. It is a little tricky as
2321 symbols can still be defined in
2322 it. */
2323 sm_table[i].link_once = 1;
2324 goto next;
2325 } else {
2326 goto found;
2330 /* not found: create new section */
2331 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
2332 /* take as much info as possible from the section. sh_link and
2333 sh_info will be updated later */
2334 s->sh_addralign = sh->sh_addralign;
2335 s->sh_entsize = sh->sh_entsize;
2336 sm_table[i].new_section = 1;
2337 found:
2338 if (sh->sh_type != s->sh_type) {
2339 tcc_error_noabort("invalid section type");
2340 goto fail;
2343 /* align start of section */
2344 offset = s->data_offset;
2346 if (0 == strcmp(sh_name, ".stab")) {
2347 stab_index = i;
2348 goto no_align;
2350 if (0 == strcmp(sh_name, ".stabstr")) {
2351 stabstr_index = i;
2352 goto no_align;
2355 size = sh->sh_addralign - 1;
2356 offset = (offset + size) & ~size;
2357 if (sh->sh_addralign > s->sh_addralign)
2358 s->sh_addralign = sh->sh_addralign;
2359 s->data_offset = offset;
2360 no_align:
2361 sm_table[i].offset = offset;
2362 sm_table[i].s = s;
2363 /* concatenate sections */
2364 size = sh->sh_size;
2365 if (sh->sh_type != SHT_NOBITS) {
2366 unsigned char *ptr;
2367 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2368 ptr = section_ptr_add(s, size);
2369 read(fd, ptr, size);
2370 } else {
2371 s->data_offset += size;
2373 next: ;
2376 /* gr relocate stab strings */
2377 if (stab_index && stabstr_index) {
2378 Stab_Sym *a, *b;
2379 unsigned o;
2380 s = sm_table[stab_index].s;
2381 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2382 b = (Stab_Sym *)(s->data + s->data_offset);
2383 o = sm_table[stabstr_index].offset;
2384 while (a < b)
2385 a->n_strx += o, a++;
2388 /* second short pass to update sh_link and sh_info fields of new
2389 sections */
2390 for(i = 1; i < ehdr.e_shnum; i++) {
2391 s = sm_table[i].s;
2392 if (!s || !sm_table[i].new_section)
2393 continue;
2394 sh = &shdr[i];
2395 if (sh->sh_link > 0)
2396 s->link = sm_table[sh->sh_link].s;
2397 if (sh->sh_type == SHT_RELX) {
2398 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2399 /* update backward link */
2400 s1->sections[s->sh_info]->reloc = s;
2403 sm = sm_table;
2405 /* resolve symbols */
2406 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2408 sym = symtab + 1;
2409 for(i = 1; i < nb_syms; i++, sym++) {
2410 if (sym->st_shndx != SHN_UNDEF &&
2411 sym->st_shndx < SHN_LORESERVE) {
2412 sm = &sm_table[sym->st_shndx];
2413 if (sm->link_once) {
2414 /* if a symbol is in a link once section, we use the
2415 already defined symbol. It is very important to get
2416 correct relocations */
2417 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2418 name = (char *) strtab + sym->st_name;
2419 sym_index = find_elf_sym(symtab_section, name);
2420 if (sym_index)
2421 old_to_new_syms[i] = sym_index;
2423 continue;
2425 /* if no corresponding section added, no need to add symbol */
2426 if (!sm->s)
2427 continue;
2428 /* convert section number */
2429 sym->st_shndx = sm->s->sh_num;
2430 /* offset value */
2431 sym->st_value += sm->offset;
2433 /* add symbol */
2434 name = (char *) strtab + sym->st_name;
2435 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
2436 sym->st_info, sym->st_other,
2437 sym->st_shndx, name);
2438 old_to_new_syms[i] = sym_index;
2441 /* third pass to patch relocation entries */
2442 for(i = 1; i < ehdr.e_shnum; i++) {
2443 s = sm_table[i].s;
2444 if (!s)
2445 continue;
2446 sh = &shdr[i];
2447 offset = sm_table[i].offset;
2448 switch(s->sh_type) {
2449 case SHT_RELX:
2450 /* take relocation offset information */
2451 offseti = sm_table[sh->sh_info].offset;
2452 for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) {
2453 int type;
2454 unsigned sym_index;
2455 /* convert symbol index */
2456 type = ELFW(R_TYPE)(rel->r_info);
2457 sym_index = ELFW(R_SYM)(rel->r_info);
2458 /* NOTE: only one symtab assumed */
2459 if (sym_index >= nb_syms)
2460 goto invalid_reloc;
2461 sym_index = old_to_new_syms[sym_index];
2462 /* ignore link_once in rel section. */
2463 if (!sym_index && !sm->link_once
2464 #ifdef TCC_TARGET_ARM
2465 && type != R_ARM_V4BX
2466 #endif
2468 invalid_reloc:
2469 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2470 i, strsec + sh->sh_name, rel->r_offset);
2471 goto fail;
2473 rel->r_info = ELFW(R_INFO)(sym_index, type);
2474 /* offset the relocation offset */
2475 rel->r_offset += offseti;
2476 #ifdef TCC_TARGET_ARM
2477 /* Jumps and branches from a Thumb code to a PLT entry need
2478 special handling since PLT entries are ARM code.
2479 Unconditional bl instructions referencing PLT entries are
2480 handled by converting these instructions into blx
2481 instructions. Other case of instructions referencing a PLT
2482 entry require to add a Thumb stub before the PLT entry to
2483 switch to ARM mode. We set bit plt_thumb_stub of the
2484 attribute of a symbol to indicate such a case. */
2485 if (type == R_ARM_THM_JUMP24)
2486 get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
2487 #endif
2489 break;
2490 default:
2491 break;
2495 ret = 0;
2496 the_end:
2497 tcc_free(symtab);
2498 tcc_free(strtab);
2499 tcc_free(old_to_new_syms);
2500 tcc_free(sm_table);
2501 tcc_free(strsec);
2502 tcc_free(shdr);
2503 return ret;
2506 typedef struct ArchiveHeader {
2507 char ar_name[16]; /* name of this member */
2508 char ar_date[12]; /* file mtime */
2509 char ar_uid[6]; /* owner uid; printed as decimal */
2510 char ar_gid[6]; /* owner gid; printed as decimal */
2511 char ar_mode[8]; /* file mode, printed as octal */
2512 char ar_size[10]; /* file size, printed as decimal */
2513 char ar_fmag[2]; /* should contain ARFMAG */
2514 } ArchiveHeader;
2516 static int get_be32(const uint8_t *b)
2518 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2521 static long get_be64(const uint8_t *b)
2523 long long ret = get_be32(b);
2524 ret = (ret << 32) | (unsigned)get_be32(b+4);
2525 return (long)ret;
2528 /* load only the objects which resolve undefined symbols */
2529 static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
2531 long i, bound, nsyms, sym_index, off, ret;
2532 uint8_t *data;
2533 const char *ar_names, *p;
2534 const uint8_t *ar_index;
2535 ElfW(Sym) *sym;
2537 data = tcc_malloc(size);
2538 if (read(fd, data, size) != size)
2539 goto fail;
2540 nsyms = entrysize == 4 ? get_be32(data) : get_be64(data);
2541 ar_index = data + entrysize;
2542 ar_names = (char *) ar_index + nsyms * entrysize;
2544 do {
2545 bound = 0;
2546 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2547 sym_index = find_elf_sym(symtab_section, p);
2548 if(sym_index) {
2549 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2550 if(sym->st_shndx == SHN_UNDEF) {
2551 off = (entrysize == 4
2552 ? get_be32(ar_index + i * 4)
2553 : get_be64(ar_index + i * 8))
2554 + sizeof(ArchiveHeader);
2555 ++bound;
2556 if(tcc_load_object_file(s1, fd, off) < 0) {
2557 fail:
2558 ret = -1;
2559 goto the_end;
2564 } while(bound);
2565 ret = 0;
2566 the_end:
2567 tcc_free(data);
2568 return ret;
2571 /* load a '.a' file */
2572 ST_FUNC int tcc_load_archive(TCCState *s1, int fd)
2574 ArchiveHeader hdr;
2575 char ar_size[11];
2576 char ar_name[17];
2577 char magic[8];
2578 int size, len, i;
2579 unsigned long file_offset;
2581 /* skip magic which was already checked */
2582 read(fd, magic, sizeof(magic));
2584 for(;;) {
2585 len = read(fd, &hdr, sizeof(hdr));
2586 if (len == 0)
2587 break;
2588 if (len != sizeof(hdr)) {
2589 tcc_error_noabort("invalid archive");
2590 return -1;
2592 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2593 ar_size[sizeof(hdr.ar_size)] = '\0';
2594 size = strtol(ar_size, NULL, 0);
2595 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2596 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2597 if (ar_name[i] != ' ')
2598 break;
2600 ar_name[i + 1] = '\0';
2601 file_offset = lseek(fd, 0, SEEK_CUR);
2602 /* align to even */
2603 size = (size + 1) & ~1;
2604 if (!strcmp(ar_name, "/")) {
2605 /* coff symbol table : we handle it */
2606 if(s1->alacarte_link)
2607 return tcc_load_alacarte(s1, fd, size, 4);
2608 } else if (!strcmp(ar_name, "/SYM64/")) {
2609 if(s1->alacarte_link)
2610 return tcc_load_alacarte(s1, fd, size, 8);
2611 } else {
2612 ElfW(Ehdr) ehdr;
2613 if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
2614 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2615 return -1;
2618 lseek(fd, file_offset + size, SEEK_SET);
2620 return 0;
2623 #ifndef TCC_TARGET_PE
2624 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2625 is referenced by the user (so it should be added as DT_NEEDED in
2626 the generated ELF file) */
2627 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2629 ElfW(Ehdr) ehdr;
2630 ElfW(Shdr) *shdr, *sh, *sh1;
2631 int i, j, nb_syms, nb_dts, sym_bind, ret;
2632 ElfW(Sym) *sym, *dynsym;
2633 ElfW(Dyn) *dt, *dynamic;
2634 unsigned char *dynstr;
2635 const char *name, *soname;
2636 DLLReference *dllref;
2638 read(fd, &ehdr, sizeof(ehdr));
2640 /* test CPU specific stuff */
2641 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2642 ehdr.e_machine != EM_TCC_TARGET) {
2643 tcc_error_noabort("bad architecture");
2644 return -1;
2647 /* read sections */
2648 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2650 /* load dynamic section and dynamic symbols */
2651 nb_syms = 0;
2652 nb_dts = 0;
2653 dynamic = NULL;
2654 dynsym = NULL; /* avoid warning */
2655 dynstr = NULL; /* avoid warning */
2656 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2657 switch(sh->sh_type) {
2658 case SHT_DYNAMIC:
2659 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2660 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2661 break;
2662 case SHT_DYNSYM:
2663 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2664 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2665 sh1 = &shdr[sh->sh_link];
2666 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2667 break;
2668 default:
2669 break;
2673 /* compute the real library name */
2674 soname = tcc_basename(filename);
2676 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2677 if (dt->d_tag == DT_SONAME) {
2678 soname = (char *) dynstr + dt->d_un.d_val;
2682 /* if the dll is already loaded, do not load it */
2683 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2684 dllref = s1->loaded_dlls[i];
2685 if (!strcmp(soname, dllref->name)) {
2686 /* but update level if needed */
2687 if (level < dllref->level)
2688 dllref->level = level;
2689 ret = 0;
2690 goto the_end;
2694 /* add the dll and its level */
2695 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2696 dllref->level = level;
2697 strcpy(dllref->name, soname);
2698 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2700 /* add dynamic symbols in dynsym_section */
2701 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2702 sym_bind = ELFW(ST_BIND)(sym->st_info);
2703 if (sym_bind == STB_LOCAL)
2704 continue;
2705 name = (char *) dynstr + sym->st_name;
2706 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2707 sym->st_info, sym->st_other, sym->st_shndx, name);
2710 /* load all referenced DLLs */
2711 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2712 switch(dt->d_tag) {
2713 case DT_NEEDED:
2714 name = (char *) dynstr + dt->d_un.d_val;
2715 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2716 dllref = s1->loaded_dlls[j];
2717 if (!strcmp(name, dllref->name))
2718 goto already_loaded;
2720 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2721 tcc_error_noabort("referenced dll '%s' not found", name);
2722 ret = -1;
2723 goto the_end;
2725 already_loaded:
2726 break;
2729 ret = 0;
2730 the_end:
2731 tcc_free(dynstr);
2732 tcc_free(dynsym);
2733 tcc_free(dynamic);
2734 tcc_free(shdr);
2735 return ret;
2738 #define LD_TOK_NAME 256
2739 #define LD_TOK_EOF (-1)
2741 /* return next ld script token */
2742 static int ld_next(TCCState *s1, char *name, int name_size)
2744 int c;
2745 char *q;
2747 redo:
2748 switch(ch) {
2749 case ' ':
2750 case '\t':
2751 case '\f':
2752 case '\v':
2753 case '\r':
2754 case '\n':
2755 inp();
2756 goto redo;
2757 case '/':
2758 minp();
2759 if (ch == '*') {
2760 file->buf_ptr = parse_comment(file->buf_ptr);
2761 ch = file->buf_ptr[0];
2762 goto redo;
2763 } else {
2764 q = name;
2765 *q++ = '/';
2766 goto parse_name;
2768 break;
2769 case '\\':
2770 ch = handle_eob();
2771 if (ch != '\\')
2772 goto redo;
2773 /* fall through */
2774 /* case 'a' ... 'z': */
2775 case 'a':
2776 case 'b':
2777 case 'c':
2778 case 'd':
2779 case 'e':
2780 case 'f':
2781 case 'g':
2782 case 'h':
2783 case 'i':
2784 case 'j':
2785 case 'k':
2786 case 'l':
2787 case 'm':
2788 case 'n':
2789 case 'o':
2790 case 'p':
2791 case 'q':
2792 case 'r':
2793 case 's':
2794 case 't':
2795 case 'u':
2796 case 'v':
2797 case 'w':
2798 case 'x':
2799 case 'y':
2800 case 'z':
2801 /* case 'A' ... 'z': */
2802 case 'A':
2803 case 'B':
2804 case 'C':
2805 case 'D':
2806 case 'E':
2807 case 'F':
2808 case 'G':
2809 case 'H':
2810 case 'I':
2811 case 'J':
2812 case 'K':
2813 case 'L':
2814 case 'M':
2815 case 'N':
2816 case 'O':
2817 case 'P':
2818 case 'Q':
2819 case 'R':
2820 case 'S':
2821 case 'T':
2822 case 'U':
2823 case 'V':
2824 case 'W':
2825 case 'X':
2826 case 'Y':
2827 case 'Z':
2828 case '_':
2829 case '.':
2830 case '$':
2831 case '~':
2832 q = name;
2833 parse_name:
2834 for(;;) {
2835 if (!((ch >= 'a' && ch <= 'z') ||
2836 (ch >= 'A' && ch <= 'Z') ||
2837 (ch >= '0' && ch <= '9') ||
2838 strchr("/.-_+=$:\\,~", ch)))
2839 break;
2840 if ((q - name) < name_size - 1) {
2841 *q++ = ch;
2843 minp();
2845 *q = '\0';
2846 c = LD_TOK_NAME;
2847 break;
2848 case CH_EOF:
2849 c = LD_TOK_EOF;
2850 break;
2851 default:
2852 c = ch;
2853 inp();
2854 break;
2856 return c;
2859 static int ld_add_file(TCCState *s1, const char filename[])
2861 if (filename[0] == '/') {
2862 if (CONFIG_SYSROOT[0] == '\0'
2863 && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0)
2864 return 0;
2865 filename = tcc_basename(filename);
2867 return tcc_add_dll(s1, filename, 0);
2870 static inline int new_undef_syms(void)
2872 int ret = 0;
2873 ret = new_undef_sym;
2874 new_undef_sym = 0;
2875 return ret;
2878 static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
2880 char filename[1024], libname[1024];
2881 int t, group, nblibs = 0, ret = 0;
2882 char **libs = NULL;
2884 group = !strcmp(cmd, "GROUP");
2885 if (!as_needed)
2886 new_undef_syms();
2887 t = ld_next(s1, filename, sizeof(filename));
2888 if (t != '(')
2889 expect("(");
2890 t = ld_next(s1, filename, sizeof(filename));
2891 for(;;) {
2892 libname[0] = '\0';
2893 if (t == LD_TOK_EOF) {
2894 tcc_error_noabort("unexpected end of file");
2895 ret = -1;
2896 goto lib_parse_error;
2897 } else if (t == ')') {
2898 break;
2899 } else if (t == '-') {
2900 t = ld_next(s1, filename, sizeof(filename));
2901 if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
2902 tcc_error_noabort("library name expected");
2903 ret = -1;
2904 goto lib_parse_error;
2906 pstrcpy(libname, sizeof libname, &filename[1]);
2907 if (s1->static_link) {
2908 snprintf(filename, sizeof filename, "lib%s.a", libname);
2909 } else {
2910 snprintf(filename, sizeof filename, "lib%s.so", libname);
2912 } else if (t != LD_TOK_NAME) {
2913 tcc_error_noabort("filename expected");
2914 ret = -1;
2915 goto lib_parse_error;
2917 if (!strcmp(filename, "AS_NEEDED")) {
2918 ret = ld_add_file_list(s1, cmd, 1);
2919 if (ret)
2920 goto lib_parse_error;
2921 } else {
2922 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2923 if (!as_needed) {
2924 ret = ld_add_file(s1, filename);
2925 if (ret)
2926 goto lib_parse_error;
2927 if (group) {
2928 /* Add the filename *and* the libname to avoid future conversions */
2929 dynarray_add(&libs, &nblibs, tcc_strdup(filename));
2930 if (libname[0] != '\0')
2931 dynarray_add(&libs, &nblibs, tcc_strdup(libname));
2935 t = ld_next(s1, filename, sizeof(filename));
2936 if (t == ',') {
2937 t = ld_next(s1, filename, sizeof(filename));
2940 if (group && !as_needed) {
2941 while (new_undef_syms()) {
2942 int i;
2944 for (i = 0; i < nblibs; i ++)
2945 ld_add_file(s1, libs[i]);
2948 lib_parse_error:
2949 dynarray_reset(&libs, &nblibs);
2950 return ret;
2953 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2954 files */
2955 ST_FUNC int tcc_load_ldscript(TCCState *s1)
2957 char cmd[64];
2958 char filename[1024];
2959 int t, ret;
2961 ch = handle_eob();
2962 for(;;) {
2963 t = ld_next(s1, cmd, sizeof(cmd));
2964 if (t == LD_TOK_EOF)
2965 return 0;
2966 else if (t != LD_TOK_NAME)
2967 return -1;
2968 if (!strcmp(cmd, "INPUT") ||
2969 !strcmp(cmd, "GROUP")) {
2970 ret = ld_add_file_list(s1, cmd, 0);
2971 if (ret)
2972 return ret;
2973 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
2974 !strcmp(cmd, "TARGET")) {
2975 /* ignore some commands */
2976 t = ld_next(s1, cmd, sizeof(cmd));
2977 if (t != '(')
2978 expect("(");
2979 for(;;) {
2980 t = ld_next(s1, filename, sizeof(filename));
2981 if (t == LD_TOK_EOF) {
2982 tcc_error_noabort("unexpected end of file");
2983 return -1;
2984 } else if (t == ')') {
2985 break;
2988 } else {
2989 return -1;
2992 return 0;
2994 #endif /* !TCC_TARGET_PE */