Revert "use int for ssize_t, (u)intptr_t instead of long in stddef.h"
[tinycc.git] / tccelf.c
blob9a7aa91a9aceecf9386cbcbbd0cbd9dbb360b99b
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 #elif defined(TCC_TARGET_X86_64)
832 case R_X86_64_32:
833 case R_X86_64_32S:
834 case R_X86_64_64:
835 #endif
836 count++;
837 break;
838 #if defined(TCC_TARGET_I386)
839 case R_386_PC32:
840 #elif defined(TCC_TARGET_X86_64)
841 case R_X86_64_PC32:
842 #endif
843 if (get_sym_attr(s1, sym_index, 0)->dyn_index)
844 count++;
845 break;
846 default:
847 break;
850 if (count) {
851 /* allocate the section */
852 sr->sh_flags |= SHF_ALLOC;
853 sr->sh_size = count * sizeof(ElfW_Rel);
855 return count;
858 static void build_got(TCCState *s1)
860 /* if no got, then create it */
861 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
862 s1->got->sh_entsize = 4;
863 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
864 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
865 /* keep space for _DYNAMIC pointer and two dummy got entries */
866 section_ptr_add(s1->got, 3 * PTR_SIZE);
869 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
870 in s1->symtab. When creating the dynamic symbol table entry for the GOT
871 relocation, use 'size' and 'info' for the corresponding symbol metadata.
872 Returns the offset of the GOT or (if any) PLT entry. */
873 static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
874 unsigned long size,
875 int info, int sym_index)
877 int need_plt_entry;
878 const char *name;
879 ElfW(Sym) *sym;
880 struct sym_attr *attr;
881 unsigned got_offset;
882 char plt_name[100];
883 int len;
885 need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
886 attr = get_sym_attr(s1, sym_index, 1);
888 /* In case a function is both called and its address taken 2 GOT entries
889 are created, one for taking the address (GOT) and the other for the PLT
890 entry (PLTGOT). */
891 if (need_plt_entry ? attr->plt_offset : attr->got_offset)
892 return attr;
894 /* create the GOT entry */
895 got_offset = s1->got->data_offset;
896 section_ptr_add(s1->got, PTR_SIZE);
898 /* Create the GOT relocation that will insert the address of the object or
899 function of interest in the GOT entry. This is a static relocation for
900 memory output (dlsym will give us the address of symbols) and dynamic
901 relocation otherwise (executable and DLLs). The relocation should be
902 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
903 associated to a PLT entry) but is currently done at load time for an
904 unknown reason. */
906 sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
907 name = (char *) symtab_section->link->data + sym->st_name;
909 if (s1->dynsym) {
910 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
911 /* Hack alarm. We don't want to emit dynamic symbols
912 and symbol based relocs for STB_LOCAL symbols, but rather
913 want to resolve them directly. At this point the symbol
914 values aren't final yet, so we must defer this. We will later
915 have to create a RELATIVE reloc anyway, so we misuse the
916 relocation slot to smuggle the symbol reference until
917 fill_local_got_entries. Not that the sym_index is
918 relative to symtab_section, not s1->dynsym! Nevertheless
919 we use s1->dyn_sym so that if this is the first call
920 that got->reloc is correctly created. Also note that
921 RELATIVE relocs are not normally created for the .got,
922 so the types serves as a marker for later (and is retained
923 also for the final output, which is okay because then the
924 got is just normal data). */
925 put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE,
926 sym_index);
927 } else {
928 if (0 == attr->dyn_index)
929 attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size,
930 info, 0, sym->st_shndx, name);
931 put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type,
932 attr->dyn_index);
934 } else {
935 put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
936 sym_index);
939 if (need_plt_entry) {
940 if (!s1->plt) {
941 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
942 SHF_ALLOC | SHF_EXECINSTR);
943 s1->plt->sh_entsize = 4;
946 attr->plt_offset = create_plt_entry(s1, got_offset, attr);
948 /* create a symbol 'sym@plt' for the PLT jump vector */
949 len = strlen(name);
950 if (len > sizeof plt_name - 5)
951 len = sizeof plt_name - 5;
952 memcpy(plt_name, name, len);
953 strcpy(plt_name + len, "@plt");
954 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
955 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
957 } else {
958 attr->got_offset = got_offset;
961 return attr;
964 /* build GOT and PLT entries */
965 ST_FUNC void build_got_entries(TCCState *s1)
967 Section *s;
968 ElfW_Rel *rel;
969 ElfW(Sym) *sym;
970 int i, type, gotplt_entry, reloc_type, sym_index;
971 struct sym_attr *attr;
973 for(i = 1; i < s1->nb_sections; i++) {
974 s = s1->sections[i];
975 if (s->sh_type != SHT_RELX)
976 continue;
977 /* no need to handle got relocations */
978 if (s->link != symtab_section)
979 continue;
980 for_each_elem(s, 0, rel, ElfW_Rel) {
981 type = ELFW(R_TYPE)(rel->r_info);
982 gotplt_entry = gotplt_entry_type(type);
983 sym_index = ELFW(R_SYM)(rel->r_info);
984 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
986 if (gotplt_entry == NO_GOTPLT_ENTRY) {
987 continue;
990 /* Automatically create PLT/GOT [entry] if it is an undefined
991 reference (resolved at runtime), or the symbol is absolute,
992 probably created by tcc_add_symbol, and thus on 64-bit
993 targets might be too far from application code. */
994 if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
995 if (sym->st_shndx == SHN_UNDEF) {
996 ElfW(Sym) *esym;
997 int dynindex;
998 if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
999 continue;
1000 /* Relocations for UNDEF symbols would normally need
1001 to be transferred into the executable or shared object.
1002 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1003 But TCC doesn't do that (at least for exes), so we
1004 need to resolve all such relocs locally. And that
1005 means PLT slots for functions in DLLs and COPY relocs for
1006 data symbols. COPY relocs were generated in
1007 bind_exe_dynsyms (and the symbol adjusted to be defined),
1008 and for functions we were generated a dynamic symbol
1009 of function type. */
1010 if (s1->dynsym) {
1011 /* dynsym isn't set for -run :-/ */
1012 dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index;
1013 esym = (ElfW(Sym) *)s1->dynsym->data + dynindex;
1014 if (dynindex
1015 && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
1016 || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
1017 && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC)))
1018 goto jmp_slot;
1020 } else if (!(sym->st_shndx == SHN_ABS
1021 #ifndef TCC_TARGET_ARM
1022 && PTR_SIZE == 8
1023 #endif
1025 continue;
1028 #ifdef TCC_TARGET_X86_64
1029 if ((type == R_X86_64_PLT32 || type == R_X86_64_PC32) &&
1030 (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT ||
1031 ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)) {
1032 rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
1033 continue;
1035 #endif
1036 if (code_reloc(type)) {
1037 jmp_slot:
1038 reloc_type = R_JMP_SLOT;
1039 } else
1040 reloc_type = R_GLOB_DAT;
1042 if (!s1->got)
1043 build_got(s1);
1045 if (gotplt_entry == BUILD_GOT_ONLY)
1046 continue;
1048 attr = put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1049 sym_index);
1051 if (reloc_type == R_JMP_SLOT)
1052 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
1057 /* put dynamic tag */
1058 static void put_dt(Section *dynamic, int dt, addr_t val)
1060 ElfW(Dyn) *dyn;
1061 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1062 dyn->d_tag = dt;
1063 dyn->d_un.d_val = val;
1066 #ifndef TCC_TARGET_PE
1067 static void add_init_array_defines(TCCState *s1, const char *section_name)
1069 Section *s;
1070 long end_offset;
1071 char sym_start[1024];
1072 char sym_end[1024];
1074 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1075 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1077 s = find_section(s1, section_name);
1078 if (!s) {
1079 end_offset = 0;
1080 s = data_section;
1081 } else {
1082 end_offset = s->data_offset;
1085 set_elf_sym(symtab_section,
1086 0, 0,
1087 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1088 s->sh_num, sym_start);
1089 set_elf_sym(symtab_section,
1090 end_offset, 0,
1091 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1092 s->sh_num, sym_end);
1094 #endif
1096 static int tcc_add_support(TCCState *s1, const char *filename)
1098 char buf[1024];
1099 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
1100 return tcc_add_file(s1, buf);
1103 ST_FUNC void tcc_add_bcheck(TCCState *s1)
1105 #ifdef CONFIG_TCC_BCHECK
1106 addr_t *ptr;
1107 int sym_index;
1109 if (0 == s1->do_bounds_check)
1110 return;
1111 /* XXX: add an object file to do that */
1112 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1113 *ptr = 0;
1114 set_elf_sym(symtab_section, 0, 0,
1115 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1116 bounds_section->sh_num, "__bounds_start");
1117 /* pull bcheck.o from libtcc1.a */
1118 sym_index = set_elf_sym(symtab_section, 0, 0,
1119 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1120 SHN_UNDEF, "__bound_init");
1121 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1122 /* add 'call __bound_init()' in .init section */
1123 Section *init_section = find_section(s1, ".init");
1124 unsigned char *pinit = section_ptr_add(init_section, 5);
1125 pinit[0] = 0xe8;
1126 write32le(pinit + 1, -4);
1127 put_elf_reloc(symtab_section, init_section,
1128 init_section->data_offset - 4, R_386_PC32, sym_index);
1129 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1131 #endif
1134 /* add tcc runtime libraries */
1135 ST_FUNC void tcc_add_runtime(TCCState *s1)
1137 tcc_add_bcheck(s1);
1138 tcc_add_pragma_libs(s1);
1139 /* add libc */
1140 if (!s1->nostdlib) {
1141 tcc_add_library_err(s1, "c");
1142 #ifdef TCC_LIBGCC
1143 if (!s1->static_link) {
1144 if (TCC_LIBGCC[0] == '/')
1145 tcc_add_file(s1, TCC_LIBGCC);
1146 else
1147 tcc_add_dll(s1, TCC_LIBGCC, 0);
1149 #endif
1150 tcc_add_support(s1, TCC_LIBTCC1);
1151 /* add crt end if not memory output */
1152 if (s1->output_type != TCC_OUTPUT_MEMORY)
1153 tcc_add_crt(s1, "crtn.o");
1157 /* add various standard linker symbols (must be done after the
1158 sections are filled (for example after allocating common
1159 symbols)) */
1160 ST_FUNC void tcc_add_linker_symbols(TCCState *s1)
1162 char buf[1024];
1163 int i;
1164 Section *s;
1166 set_elf_sym(symtab_section,
1167 text_section->data_offset, 0,
1168 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1169 text_section->sh_num, "_etext");
1170 set_elf_sym(symtab_section,
1171 data_section->data_offset, 0,
1172 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1173 data_section->sh_num, "_edata");
1174 set_elf_sym(symtab_section,
1175 bss_section->data_offset, 0,
1176 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1177 bss_section->sh_num, "_end");
1178 #ifndef TCC_TARGET_PE
1179 /* horrible new standard ldscript defines */
1180 add_init_array_defines(s1, ".preinit_array");
1181 add_init_array_defines(s1, ".init_array");
1182 add_init_array_defines(s1, ".fini_array");
1183 #endif
1185 /* add start and stop symbols for sections whose name can be
1186 expressed in C */
1187 for(i = 1; i < s1->nb_sections; i++) {
1188 s = s1->sections[i];
1189 if (s->sh_type == SHT_PROGBITS &&
1190 (s->sh_flags & SHF_ALLOC)) {
1191 const char *p;
1192 int ch;
1194 /* check if section name can be expressed in C */
1195 p = s->name;
1196 for(;;) {
1197 ch = *p;
1198 if (!ch)
1199 break;
1200 if (!isid(ch) && !isnum(ch))
1201 goto next_sec;
1202 p++;
1204 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1205 set_elf_sym(symtab_section,
1206 0, 0,
1207 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1208 s->sh_num, buf);
1209 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1210 set_elf_sym(symtab_section,
1211 s->data_offset, 0,
1212 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1213 s->sh_num, buf);
1215 next_sec: ;
1219 static void tcc_output_binary(TCCState *s1, FILE *f,
1220 const int *sec_order)
1222 Section *s;
1223 int i, offset, size;
1225 offset = 0;
1226 for(i=1;i<s1->nb_sections;i++) {
1227 s = s1->sections[sec_order[i]];
1228 if (s->sh_type != SHT_NOBITS &&
1229 (s->sh_flags & SHF_ALLOC)) {
1230 while (offset < s->sh_offset) {
1231 fputc(0, f);
1232 offset++;
1234 size = s->sh_size;
1235 fwrite(s->data, 1, size, f);
1236 offset += size;
1241 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1242 #define HAVE_PHDR 1
1243 #define EXTRA_RELITEMS 14
1244 #else
1245 #define HAVE_PHDR 1
1246 #define EXTRA_RELITEMS 9
1247 #endif
1249 ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1251 int sym_index = ELFW(R_SYM) (rel->r_info);
1252 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1253 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1254 unsigned offset = attr->got_offset;
1256 if (0 == offset)
1257 return;
1258 section_reserve(s1->got, offset + PTR_SIZE);
1259 #ifdef TCC_TARGET_X86_64
1260 write64le(s1->got->data + offset, sym->st_value);
1261 #else
1262 write32le(s1->got->data + offset, sym->st_value);
1263 #endif
1266 /* Perform relocation to GOT or PLT entries */
1267 ST_FUNC void fill_got(TCCState *s1)
1269 Section *s;
1270 ElfW_Rel *rel;
1271 int i;
1273 for(i = 1; i < s1->nb_sections; i++) {
1274 s = s1->sections[i];
1275 if (s->sh_type != SHT_RELX)
1276 continue;
1277 /* no need to handle got relocations */
1278 if (s->link != symtab_section)
1279 continue;
1280 for_each_elem(s, 0, rel, ElfW_Rel) {
1281 switch (ELFW(R_TYPE) (rel->r_info)) {
1282 case R_X86_64_GOT32:
1283 case R_X86_64_GOTPCREL:
1284 case R_X86_64_GOTPCRELX:
1285 case R_X86_64_REX_GOTPCRELX:
1286 case R_X86_64_PLT32:
1287 fill_got_entry(s1, rel);
1288 break;
1294 /* See put_got_entry for a description. This is the second stage
1295 where GOT references to local defined symbols are rewritten. */
1296 static void fill_local_got_entries(TCCState *s1)
1298 ElfW_Rel *rel;
1299 for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
1300 if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
1301 int sym_index = ELFW(R_SYM) (rel->r_info);
1302 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1303 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1304 unsigned offset = attr->got_offset;
1305 if (offset != rel->r_offset - s1->got->sh_addr)
1306 tcc_error_noabort("huh");
1307 rel->r_info = ELFW(R_INFO)(0, R_RELATIVE);
1308 #if SHT_RELX == SHT_RELA
1309 rel->r_addend = sym->st_value;
1310 #else
1311 /* All our REL architectures also happen to be 32bit LE. */
1312 write32le(s1->got->data + offset, sym->st_value);
1313 #endif
1318 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1319 in shared libraries and export non local defined symbols to shared libraries
1320 if -rdynamic switch was given on command line */
1321 static void bind_exe_dynsyms(TCCState *s1)
1323 const char *name;
1324 int sym_index, index;
1325 ElfW(Sym) *sym, *esym;
1326 int type;
1328 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1329 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1330 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1331 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1332 if (sym->st_shndx == SHN_UNDEF) {
1333 name = (char *) symtab_section->link->data + sym->st_name;
1334 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1335 if (sym_index) {
1336 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1337 type = ELFW(ST_TYPE)(esym->st_info);
1338 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
1339 /* Indirect functions shall have STT_FUNC type in executable
1340 * dynsym section. Indeed, a dlsym call following a lazy
1341 * resolution would pick the symbol value from the
1342 * executable dynsym entry which would contain the address
1343 * of the function wanted by the caller of dlsym instead of
1344 * the address of the function that would return that
1345 * address */
1346 int dynindex
1347 = put_elf_sym(s1->dynsym, 0, esym->st_size,
1348 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
1349 name);
1350 int index = sym - (ElfW(Sym) *) symtab_section->data;
1351 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1352 } else if (type == STT_OBJECT) {
1353 unsigned long offset;
1354 ElfW(Sym) *dynsym;
1355 offset = bss_section->data_offset;
1356 /* XXX: which alignment ? */
1357 offset = (offset + 16 - 1) & -16;
1358 set_elf_sym (s1->symtab, offset, esym->st_size,
1359 esym->st_info, 0, bss_section->sh_num, name);
1360 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1361 esym->st_info, 0, bss_section->sh_num,
1362 name);
1364 /* Ensure R_COPY works for weak symbol aliases */
1365 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1366 for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
1367 if ((dynsym->st_value == esym->st_value)
1368 && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
1369 char *dynname = (char *) s1->dynsymtab_section->link->data
1370 + dynsym->st_name;
1371 put_elf_sym(s1->dynsym, offset, dynsym->st_size,
1372 dynsym->st_info, 0,
1373 bss_section->sh_num, dynname);
1374 break;
1379 put_elf_reloc(s1->dynsym, bss_section,
1380 offset, R_COPY, index);
1381 offset += esym->st_size;
1382 bss_section->data_offset = offset;
1384 } else {
1385 /* STB_WEAK undefined symbols are accepted */
1386 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1387 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1388 !strcmp(name, "_fp_hw")) {
1389 } else {
1390 tcc_error_noabort("undefined symbol '%s'", name);
1393 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1394 /* if -rdynamic option, then export all non local symbols */
1395 name = (char *) symtab_section->link->data + sym->st_name;
1396 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1397 0, sym->st_shndx, name);
1402 /* Bind symbols of libraries: export all non local symbols of executable that
1403 are referenced by shared libraries. The reason is that the dynamic loader
1404 search symbol first in executable and then in libraries. Therefore a
1405 reference to a symbol already defined by a library can still be resolved by
1406 a symbol in the executable. */
1407 static void bind_libs_dynsyms(TCCState *s1)
1409 const char *name;
1410 int sym_index;
1411 ElfW(Sym) *sym, *esym;
1413 for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
1414 name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
1415 sym_index = find_elf_sym(symtab_section, name);
1416 /* XXX: avoid adding a symbol if already present because of
1417 -rdynamic ? */
1418 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1419 if (sym_index && sym->st_shndx != SHN_UNDEF)
1420 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1421 0, sym->st_shndx, name);
1422 else if (esym->st_shndx == SHN_UNDEF) {
1423 /* weak symbols can stay undefined */
1424 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
1425 tcc_warning("undefined dynamic symbol '%s'", name);
1430 /* Export all non local symbols. This is used by shared libraries so that the
1431 non local symbols they define can resolve a reference in another shared
1432 library or in the executable. Correspondingly, it allows undefined local
1433 symbols to be resolved by other shared libraries or by the executable. */
1434 static void export_global_syms(TCCState *s1)
1436 int dynindex, index;
1437 const char *name;
1438 ElfW(Sym) *sym;
1440 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1441 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1442 name = (char *) symtab_section->link->data + sym->st_name;
1443 dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1444 sym->st_info, 0, sym->st_shndx, name);
1445 index = sym - (ElfW(Sym) *) symtab_section->data;
1446 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1451 /* Allocate strings for section names and decide if an unallocated section
1452 should be output.
1453 NOTE: the strsec section comes last, so its size is also correct ! */
1454 static void alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
1456 int i;
1457 Section *s;
1459 /* Allocate strings for section names */
1460 for(i = 1; i < s1->nb_sections; i++) {
1461 s = s1->sections[i];
1462 /* when generating a DLL, we include relocations but we may
1463 patch them */
1464 if (file_type == TCC_OUTPUT_DLL &&
1465 s->sh_type == SHT_RELX &&
1466 !(s->sh_flags & SHF_ALLOC)) {
1467 /* gr: avoid bogus relocs for empty (debug) sections */
1468 if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
1469 prepare_dynamic_rel(s1, s);
1470 else if (s1->do_debug)
1471 s->sh_size = s->data_offset;
1472 } else if (s1->do_debug ||
1473 file_type == TCC_OUTPUT_OBJ ||
1474 (s->sh_flags & SHF_ALLOC) ||
1475 i == (s1->nb_sections - 1)) {
1476 /* we output all sections if debug or object file */
1477 s->sh_size = s->data_offset;
1479 if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1480 s->sh_name = put_elf_str(strsec, s->name);
1482 strsec->sh_size = strsec->data_offset;
1485 /* Info to be copied in dynamic section */
1486 struct dyn_inf {
1487 Section *dynamic;
1488 Section *dynstr;
1489 unsigned long dyn_rel_off;
1490 addr_t rel_addr;
1491 addr_t rel_size;
1492 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1493 addr_t bss_addr;
1494 addr_t bss_size;
1495 #endif
1498 /* Assign sections to segments and decide how are sections laid out when loaded
1499 in memory. This function also fills corresponding program headers. */
1500 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1501 Section *interp, Section* strsec,
1502 struct dyn_inf *dyninf, int *sec_order)
1504 int i, j, k, file_type, sh_order_index, file_offset;
1505 unsigned long s_align;
1506 long long tmp;
1507 addr_t addr;
1508 ElfW(Phdr) *ph;
1509 Section *s;
1511 file_type = s1->output_type;
1512 sh_order_index = 1;
1513 file_offset = 0;
1514 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1515 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1516 s_align = ELF_PAGE_SIZE;
1517 if (s1->section_align)
1518 s_align = s1->section_align;
1520 if (phnum > 0) {
1521 if (s1->has_text_addr) {
1522 int a_offset, p_offset;
1523 addr = s1->text_addr;
1524 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1525 ELF_PAGE_SIZE */
1526 a_offset = (int) (addr & (s_align - 1));
1527 p_offset = file_offset & (s_align - 1);
1528 if (a_offset < p_offset)
1529 a_offset += s_align;
1530 file_offset += (a_offset - p_offset);
1531 } else {
1532 if (file_type == TCC_OUTPUT_DLL)
1533 addr = 0;
1534 else
1535 addr = ELF_START_ADDR;
1536 /* compute address after headers */
1537 addr += (file_offset & (s_align - 1));
1540 ph = &phdr[0];
1541 /* Leave one program headers for the program interpreter and one for
1542 the program header table itself if needed. These are done later as
1543 they require section layout to be done first. */
1544 if (interp)
1545 ph += 1 + HAVE_PHDR;
1547 /* dynamic relocation table information, for .dynamic section */
1548 dyninf->rel_addr = dyninf->rel_size = 0;
1549 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1550 dyninf->bss_addr = dyninf->bss_size = 0;
1551 #endif
1553 for(j = 0; j < 2; j++) {
1554 ph->p_type = PT_LOAD;
1555 if (j == 0)
1556 ph->p_flags = PF_R | PF_X;
1557 else
1558 ph->p_flags = PF_R | PF_W;
1559 ph->p_align = s_align;
1561 /* Decide the layout of sections loaded in memory. This must
1562 be done before program headers are filled since they contain
1563 info about the layout. We do the following ordering: interp,
1564 symbol tables, relocations, progbits, nobits */
1565 /* XXX: do faster and simpler sorting */
1566 for(k = 0; k < 5; k++) {
1567 for(i = 1; i < s1->nb_sections; i++) {
1568 s = s1->sections[i];
1569 /* compute if section should be included */
1570 if (j == 0) {
1571 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1572 SHF_ALLOC)
1573 continue;
1574 } else {
1575 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1576 (SHF_ALLOC | SHF_WRITE))
1577 continue;
1579 if (s == interp) {
1580 if (k != 0)
1581 continue;
1582 } else if (s->sh_type == SHT_DYNSYM ||
1583 s->sh_type == SHT_STRTAB ||
1584 s->sh_type == SHT_HASH) {
1585 if (k != 1)
1586 continue;
1587 } else if (s->sh_type == SHT_RELX) {
1588 if (k != 2)
1589 continue;
1590 } else if (s->sh_type == SHT_NOBITS) {
1591 if (k != 4)
1592 continue;
1593 } else {
1594 if (k != 3)
1595 continue;
1597 sec_order[sh_order_index++] = i;
1599 /* section matches: we align it and add its size */
1600 tmp = addr;
1601 addr = (addr + s->sh_addralign - 1) &
1602 ~(s->sh_addralign - 1);
1603 file_offset += (int) ( addr - tmp );
1604 s->sh_offset = file_offset;
1605 s->sh_addr = addr;
1607 /* update program header infos */
1608 if (ph->p_offset == 0) {
1609 ph->p_offset = file_offset;
1610 ph->p_vaddr = addr;
1611 ph->p_paddr = ph->p_vaddr;
1613 /* update dynamic relocation infos */
1614 if (s->sh_type == SHT_RELX) {
1615 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1616 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1617 dyninf->rel_addr = addr;
1618 dyninf->rel_size += s->sh_size; /* XXX only first rel. */
1620 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1621 dyninf->bss_addr = addr;
1622 dyninf->bss_size = s->sh_size; /* XXX only first rel. */
1624 #else
1625 if (dyninf->rel_size == 0)
1626 dyninf->rel_addr = addr;
1627 dyninf->rel_size += s->sh_size;
1628 #endif
1630 addr += s->sh_size;
1631 if (s->sh_type != SHT_NOBITS)
1632 file_offset += s->sh_size;
1635 if (j == 0) {
1636 /* Make the first PT_LOAD segment include the program
1637 headers itself (and the ELF header as well), it'll
1638 come out with same memory use but will make various
1639 tools like binutils strip work better. */
1640 ph->p_offset &= ~(ph->p_align - 1);
1641 ph->p_vaddr &= ~(ph->p_align - 1);
1642 ph->p_paddr &= ~(ph->p_align - 1);
1644 ph->p_filesz = file_offset - ph->p_offset;
1645 ph->p_memsz = addr - ph->p_vaddr;
1646 ph++;
1647 if (j == 0) {
1648 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1649 /* if in the middle of a page, we duplicate the page in
1650 memory so that one copy is RX and the other is RW */
1651 if ((addr & (s_align - 1)) != 0)
1652 addr += s_align;
1653 } else {
1654 addr = (addr + s_align - 1) & ~(s_align - 1);
1655 file_offset = (file_offset + s_align - 1) & ~(s_align - 1);
1661 /* all other sections come after */
1662 for(i = 1; i < s1->nb_sections; i++) {
1663 s = s1->sections[i];
1664 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1665 continue;
1666 sec_order[sh_order_index++] = i;
1668 file_offset = (file_offset + s->sh_addralign - 1) &
1669 ~(s->sh_addralign - 1);
1670 s->sh_offset = file_offset;
1671 if (s->sh_type != SHT_NOBITS)
1672 file_offset += s->sh_size;
1675 return file_offset;
1678 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
1679 Section *dynamic)
1681 ElfW(Phdr) *ph;
1683 /* if interpreter, then add corresponding program header */
1684 if (interp) {
1685 ph = &phdr[0];
1687 if (HAVE_PHDR)
1689 int len = phnum * sizeof(ElfW(Phdr));
1691 ph->p_type = PT_PHDR;
1692 ph->p_offset = sizeof(ElfW(Ehdr));
1693 ph->p_vaddr = interp->sh_addr - len;
1694 ph->p_paddr = ph->p_vaddr;
1695 ph->p_filesz = ph->p_memsz = len;
1696 ph->p_flags = PF_R | PF_X;
1697 ph->p_align = 4; /* interp->sh_addralign; */
1698 ph++;
1701 ph->p_type = PT_INTERP;
1702 ph->p_offset = interp->sh_offset;
1703 ph->p_vaddr = interp->sh_addr;
1704 ph->p_paddr = ph->p_vaddr;
1705 ph->p_filesz = interp->sh_size;
1706 ph->p_memsz = interp->sh_size;
1707 ph->p_flags = PF_R;
1708 ph->p_align = interp->sh_addralign;
1711 /* if dynamic section, then add corresponding program header */
1712 if (dynamic) {
1713 ph = &phdr[phnum - 1];
1715 ph->p_type = PT_DYNAMIC;
1716 ph->p_offset = dynamic->sh_offset;
1717 ph->p_vaddr = dynamic->sh_addr;
1718 ph->p_paddr = ph->p_vaddr;
1719 ph->p_filesz = dynamic->sh_size;
1720 ph->p_memsz = dynamic->sh_size;
1721 ph->p_flags = PF_R | PF_W;
1722 ph->p_align = dynamic->sh_addralign;
1726 /* Fill the dynamic section with tags describing the address and size of
1727 sections */
1728 static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf)
1730 Section *dynamic;
1732 dynamic = dyninf->dynamic;
1734 /* put dynamic section entries */
1735 dynamic->data_offset = dyninf->dyn_rel_off;
1736 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1737 put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
1738 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1739 put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
1740 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1741 #if PTR_SIZE == 8
1742 put_dt(dynamic, DT_RELA, dyninf->rel_addr);
1743 put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
1744 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1745 #else
1746 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1747 put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
1748 put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size);
1749 put_dt(dynamic, DT_JMPREL, dyninf->rel_addr);
1750 put_dt(dynamic, DT_PLTREL, DT_REL);
1751 put_dt(dynamic, DT_REL, dyninf->bss_addr);
1752 put_dt(dynamic, DT_RELSZ, dyninf->bss_size);
1753 #else
1754 put_dt(dynamic, DT_REL, dyninf->rel_addr);
1755 put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
1756 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1757 #endif
1758 #endif
1759 if (s1->do_debug)
1760 put_dt(dynamic, DT_DEBUG, 0);
1761 put_dt(dynamic, DT_NULL, 0);
1764 /* Relocate remaining sections and symbols (that is those not related to
1765 dynamic linking) */
1766 static int final_sections_reloc(TCCState *s1)
1768 int i;
1769 Section *s;
1771 relocate_syms(s1, s1->symtab, 0);
1773 if (s1->nb_errors != 0)
1774 return -1;
1776 /* relocate sections */
1777 /* XXX: ignore sections with allocated relocations ? */
1778 for(i = 1; i < s1->nb_sections; i++) {
1779 s = s1->sections[i];
1780 #if defined(TCC_TARGET_I386) || defined(TCC_MUSL)
1781 if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
1782 /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC
1783 checking is removed */
1784 #else
1785 if (s->reloc && s != s1->got)
1786 /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */
1787 #endif
1788 relocate_section(s1, s);
1791 /* relocate relocation entries if the relocation tables are
1792 allocated in the executable */
1793 for(i = 1; i < s1->nb_sections; i++) {
1794 s = s1->sections[i];
1795 if ((s->sh_flags & SHF_ALLOC) &&
1796 s->sh_type == SHT_RELX) {
1797 relocate_rel(s1, s);
1800 return 0;
1803 /* Create an ELF file on disk.
1804 This function handle ELF specific layout requirements */
1805 static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
1806 int file_offset, int *sec_order)
1808 int i, shnum, offset, size, file_type;
1809 Section *s;
1810 ElfW(Ehdr) ehdr;
1811 ElfW(Shdr) shdr, *sh;
1813 file_type = s1->output_type;
1814 shnum = s1->nb_sections;
1816 memset(&ehdr, 0, sizeof(ehdr));
1818 if (phnum > 0) {
1819 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1820 ehdr.e_phnum = phnum;
1821 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1824 /* align to 4 */
1825 file_offset = (file_offset + 3) & -4;
1827 /* fill header */
1828 ehdr.e_ident[0] = ELFMAG0;
1829 ehdr.e_ident[1] = ELFMAG1;
1830 ehdr.e_ident[2] = ELFMAG2;
1831 ehdr.e_ident[3] = ELFMAG3;
1832 ehdr.e_ident[4] = ELFCLASSW;
1833 ehdr.e_ident[5] = ELFDATA2LSB;
1834 ehdr.e_ident[6] = EV_CURRENT;
1835 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1836 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1837 #endif
1838 #ifdef TCC_TARGET_ARM
1839 #ifdef TCC_ARM_EABI
1840 ehdr.e_ident[EI_OSABI] = 0;
1841 ehdr.e_flags = EF_ARM_EABI_VER4;
1842 if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
1843 ehdr.e_flags |= EF_ARM_HASENTRY;
1844 if (s1->float_abi == ARM_HARD_FLOAT)
1845 ehdr.e_flags |= EF_ARM_VFP_FLOAT;
1846 else
1847 ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
1848 #else
1849 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1850 #endif
1851 #endif
1852 switch(file_type) {
1853 default:
1854 case TCC_OUTPUT_EXE:
1855 ehdr.e_type = ET_EXEC;
1856 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
1857 break;
1858 case TCC_OUTPUT_DLL:
1859 ehdr.e_type = ET_DYN;
1860 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1861 break;
1862 case TCC_OUTPUT_OBJ:
1863 ehdr.e_type = ET_REL;
1864 break;
1866 ehdr.e_machine = EM_TCC_TARGET;
1867 ehdr.e_version = EV_CURRENT;
1868 ehdr.e_shoff = file_offset;
1869 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1870 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1871 ehdr.e_shnum = shnum;
1872 ehdr.e_shstrndx = shnum - 1;
1874 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1875 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1876 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1878 sort_syms(s1, symtab_section);
1879 for(i = 1; i < s1->nb_sections; i++) {
1880 s = s1->sections[sec_order[i]];
1881 if (s->sh_type != SHT_NOBITS) {
1882 while (offset < s->sh_offset) {
1883 fputc(0, f);
1884 offset++;
1886 size = s->sh_size;
1887 if (size)
1888 fwrite(s->data, 1, size, f);
1889 offset += size;
1893 /* output section headers */
1894 while (offset < ehdr.e_shoff) {
1895 fputc(0, f);
1896 offset++;
1899 for(i = 0; i < s1->nb_sections; i++) {
1900 sh = &shdr;
1901 memset(sh, 0, sizeof(ElfW(Shdr)));
1902 s = s1->sections[i];
1903 if (s) {
1904 sh->sh_name = s->sh_name;
1905 sh->sh_type = s->sh_type;
1906 sh->sh_flags = s->sh_flags;
1907 sh->sh_entsize = s->sh_entsize;
1908 sh->sh_info = s->sh_info;
1909 if (s->link)
1910 sh->sh_link = s->link->sh_num;
1911 sh->sh_addralign = s->sh_addralign;
1912 sh->sh_addr = s->sh_addr;
1913 sh->sh_offset = s->sh_offset;
1914 sh->sh_size = s->sh_size;
1916 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1920 /* Write an elf, coff or "binary" file */
1921 static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
1922 ElfW(Phdr) *phdr, int file_offset, int *sec_order)
1924 int fd, mode, file_type;
1925 FILE *f;
1927 file_type = s1->output_type;
1928 if (file_type == TCC_OUTPUT_OBJ)
1929 mode = 0666;
1930 else
1931 mode = 0777;
1932 unlink(filename);
1933 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1934 if (fd < 0) {
1935 tcc_error_noabort("could not write '%s'", filename);
1936 return -1;
1938 f = fdopen(fd, "wb");
1939 if (s1->verbose)
1940 printf("<- %s\n", filename);
1942 #ifdef TCC_TARGET_COFF
1943 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
1944 tcc_output_coff(s1, f);
1945 else
1946 #endif
1947 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1948 tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
1949 else
1950 tcc_output_binary(s1, f, sec_order);
1951 fclose(f);
1953 return 0;
1956 /* Sort section headers by assigned sh_addr, remove sections
1957 that we aren't going to output. */
1958 static void tidy_section_headers(TCCState *s1, int *sec_order)
1960 int i, nnew, l, *backmap;
1961 Section **snew, *s;
1962 ElfW(Sym) *sym;
1964 snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
1965 backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
1966 for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
1967 s = s1->sections[sec_order[i]];
1968 if (!i || s->sh_name) {
1969 backmap[sec_order[i]] = nnew;
1970 snew[nnew] = s;
1971 ++nnew;
1972 } else {
1973 backmap[sec_order[i]] = 0;
1974 snew[--l] = s;
1977 for (i = 0; i < nnew; i++) {
1978 s = snew[i];
1979 if (s) {
1980 s->sh_num = i;
1981 if (s->sh_type == SHT_RELX)
1982 s->sh_info = backmap[s->sh_info];
1986 for_each_elem(symtab_section, 1, sym, ElfW(Sym))
1987 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1988 sym->st_shndx = backmap[sym->st_shndx];
1989 if( !s1->static_link ) {
1990 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
1991 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1992 sym->st_shndx = backmap[sym->st_shndx];
1994 for (i = 0; i < s1->nb_sections; i++)
1995 sec_order[i] = i;
1996 tcc_free(s1->sections);
1997 s1->sections = snew;
1998 s1->nb_sections = nnew;
1999 tcc_free(backmap);
2002 /* Output an elf, coff or binary file */
2003 /* XXX: suppress unneeded sections */
2004 static int elf_output_file(TCCState *s1, const char *filename)
2006 int i, ret, phnum, shnum, file_type, file_offset, *sec_order;
2007 struct dyn_inf dyninf = {0};
2008 ElfW(Phdr) *phdr;
2009 ElfW(Sym) *sym;
2010 Section *strsec, *interp, *dynamic, *dynstr;
2012 file_type = s1->output_type;
2013 s1->nb_errors = 0;
2015 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2016 if (file_type != TCC_OUTPUT_OBJ) {
2017 tcc_add_runtime(s1);
2020 phdr = NULL;
2021 sec_order = NULL;
2022 interp = dynamic = dynstr = NULL; /* avoid warning */
2024 if (file_type != TCC_OUTPUT_OBJ) {
2025 relocate_common_syms();
2027 tcc_add_linker_symbols(s1);
2029 if (!s1->static_link) {
2030 if (file_type == TCC_OUTPUT_EXE) {
2031 char *ptr;
2032 /* allow override the dynamic loader */
2033 const char *elfint = getenv("LD_SO");
2034 if (elfint == NULL)
2035 elfint = DEFAULT_ELFINTERP(s1);
2036 /* add interpreter section only if executable */
2037 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
2038 interp->sh_addralign = 1;
2039 ptr = section_ptr_add(interp, 1 + strlen(elfint));
2040 strcpy(ptr, elfint);
2043 /* add dynamic symbol table */
2044 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
2045 ".dynstr",
2046 ".hash", SHF_ALLOC);
2047 dynstr = s1->dynsym->link;
2049 /* add dynamic section */
2050 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
2051 SHF_ALLOC | SHF_WRITE);
2052 dynamic->link = dynstr;
2053 dynamic->sh_entsize = sizeof(ElfW(Dyn));
2055 build_got(s1);
2057 if (file_type == TCC_OUTPUT_EXE) {
2058 bind_exe_dynsyms(s1);
2060 if (s1->nb_errors) {
2061 ret = -1;
2062 goto the_end;
2065 bind_libs_dynsyms(s1);
2066 } else /* shared library case: simply export all global symbols */
2067 export_global_syms(s1);
2069 build_got_entries(s1);
2071 /* add a list of needed dlls */
2072 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2073 DLLReference *dllref = s1->loaded_dlls[i];
2074 if (dllref->level == 0)
2075 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
2078 if (s1->rpath)
2079 put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
2080 put_elf_str(dynstr, s1->rpath));
2082 /* XXX: currently, since we do not handle PIC code, we
2083 must relocate the readonly segments */
2084 if (file_type == TCC_OUTPUT_DLL) {
2085 if (s1->soname)
2086 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
2087 put_dt(dynamic, DT_TEXTREL, 0);
2090 if (s1->symbolic)
2091 put_dt(dynamic, DT_SYMBOLIC, 0);
2093 /* add necessary space for other entries */
2094 dyninf.dyn_rel_off = dynamic->data_offset;
2095 dynamic->data_offset += sizeof(ElfW(Dyn)) * EXTRA_RELITEMS;
2096 } else {
2097 /* still need to build got entries in case of static link */
2098 build_got_entries(s1);
2102 /* we add a section for symbols */
2103 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
2104 put_elf_str(strsec, "");
2106 /* compute number of sections */
2107 shnum = s1->nb_sections;
2109 /* this array is used to reorder sections in the output file */
2110 sec_order = tcc_malloc(sizeof(int) * shnum);
2111 sec_order[0] = 0;
2113 /* compute number of program headers */
2114 switch(file_type) {
2115 default:
2116 case TCC_OUTPUT_OBJ:
2117 phnum = 0;
2118 break;
2119 case TCC_OUTPUT_EXE:
2120 if (!s1->static_link)
2121 phnum = 4 + HAVE_PHDR;
2122 else
2123 phnum = 2;
2124 break;
2125 case TCC_OUTPUT_DLL:
2126 phnum = 3;
2127 break;
2130 /* Allocate strings for section names */
2131 alloc_sec_names(s1, file_type, strsec);
2133 /* allocate program segment headers */
2134 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
2136 /* compute section to program header mapping */
2137 file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2138 sec_order);
2140 /* Fill remaining program header and finalize relocation related to dynamic
2141 linking. */
2142 if (phnum > 0) {
2143 fill_unloadable_phdr(phdr, phnum, interp, dynamic);
2144 if (dynamic) {
2145 dyninf.dynamic = dynamic;
2146 dyninf.dynstr = dynstr;
2148 fill_dynamic(s1, &dyninf);
2150 /* put in GOT the dynamic section address and relocate PLT */
2151 write32le(s1->got->data, dynamic->sh_addr);
2152 if (file_type == TCC_OUTPUT_EXE
2153 || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
2154 relocate_plt(s1);
2156 /* relocate symbols in .dynsym now that final addresses are known */
2157 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
2158 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
2159 /* do symbol relocation */
2160 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
2166 /* if building executable or DLL, then relocate each section
2167 except the GOT which is already relocated */
2168 if (file_type != TCC_OUTPUT_OBJ) {
2169 ret = final_sections_reloc(s1);
2170 if (ret)
2171 goto the_end;
2172 tidy_section_headers(s1, sec_order);
2175 /* Perform relocation to GOT or PLT entries */
2176 if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2177 fill_got(s1);
2178 else if (s1->got)
2179 fill_local_got_entries(s1);
2181 /* Create the ELF file with name 'filename' */
2182 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2183 s1->nb_sections = shnum;
2184 the_end:
2185 tcc_free(sec_order);
2186 tcc_free(phdr);
2187 return ret;
2190 LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
2192 int ret;
2193 #ifdef TCC_TARGET_PE
2194 if (s->output_type != TCC_OUTPUT_OBJ) {
2195 ret = pe_output_file(s, filename);
2196 } else
2197 #endif
2198 ret = elf_output_file(s, filename);
2199 return ret;
2202 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2204 void *data;
2206 data = tcc_malloc(size);
2207 lseek(fd, file_offset, SEEK_SET);
2208 read(fd, data, size);
2209 return data;
2212 typedef struct SectionMergeInfo {
2213 Section *s; /* corresponding existing section */
2214 unsigned long offset; /* offset of the new section in the existing section */
2215 uint8_t new_section; /* true if section 's' was added */
2216 uint8_t link_once; /* true if link once section */
2217 } SectionMergeInfo;
2219 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
2221 int size = read(fd, h, sizeof *h);
2222 if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
2223 if (h->e_type == ET_REL)
2224 return AFF_BINTYPE_REL;
2225 if (h->e_type == ET_DYN)
2226 return AFF_BINTYPE_DYN;
2227 } else if (size >= 8) {
2228 if (0 == memcmp(h, ARMAG, 8))
2229 return AFF_BINTYPE_AR;
2230 #ifdef TCC_TARGET_COFF
2231 if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC)
2232 return AFF_BINTYPE_C67;
2233 #endif
2235 return 0;
2238 /* load an object file and merge it with current files */
2239 /* XXX: handle correctly stab (debug) info */
2240 ST_FUNC int tcc_load_object_file(TCCState *s1,
2241 int fd, unsigned long file_offset)
2243 ElfW(Ehdr) ehdr;
2244 ElfW(Shdr) *shdr, *sh;
2245 int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed;
2246 unsigned char *strsec, *strtab;
2247 int *old_to_new_syms;
2248 char *sh_name, *name;
2249 SectionMergeInfo *sm_table, *sm;
2250 ElfW(Sym) *sym, *symtab;
2251 ElfW_Rel *rel;
2252 Section *s;
2254 int stab_index;
2255 int stabstr_index;
2257 stab_index = stabstr_index = 0;
2259 lseek(fd, file_offset, SEEK_SET);
2260 if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL)
2261 goto fail1;
2262 /* test CPU specific stuff */
2263 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2264 ehdr.e_machine != EM_TCC_TARGET) {
2265 fail1:
2266 tcc_error_noabort("invalid object file");
2267 return -1;
2269 /* read sections */
2270 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2271 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2272 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2274 /* load section names */
2275 sh = &shdr[ehdr.e_shstrndx];
2276 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2278 /* load symtab and strtab */
2279 old_to_new_syms = NULL;
2280 symtab = NULL;
2281 strtab = NULL;
2282 nb_syms = 0;
2283 seencompressed = 0;
2284 for(i = 1; i < ehdr.e_shnum; i++) {
2285 sh = &shdr[i];
2286 if (sh->sh_type == SHT_SYMTAB) {
2287 if (symtab) {
2288 tcc_error_noabort("object must contain only one symtab");
2289 fail:
2290 ret = -1;
2291 goto the_end;
2293 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2294 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2295 sm_table[i].s = symtab_section;
2297 /* now load strtab */
2298 sh = &shdr[sh->sh_link];
2299 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2301 if (sh->sh_flags & SHF_COMPRESSED)
2302 seencompressed = 1;
2305 /* now examine each section and try to merge its content with the
2306 ones in memory */
2307 for(i = 1; i < ehdr.e_shnum; i++) {
2308 /* no need to examine section name strtab */
2309 if (i == ehdr.e_shstrndx)
2310 continue;
2311 sh = &shdr[i];
2312 sh_name = (char *) strsec + sh->sh_name;
2313 /* ignore sections types we do not handle */
2314 if (sh->sh_type != SHT_PROGBITS &&
2315 sh->sh_type != SHT_RELX &&
2316 #ifdef TCC_ARM_EABI
2317 sh->sh_type != SHT_ARM_EXIDX &&
2318 #endif
2319 sh->sh_type != SHT_NOBITS &&
2320 sh->sh_type != SHT_PREINIT_ARRAY &&
2321 sh->sh_type != SHT_INIT_ARRAY &&
2322 sh->sh_type != SHT_FINI_ARRAY &&
2323 strcmp(sh_name, ".stabstr")
2325 continue;
2326 if (seencompressed
2327 && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1)
2328 || (sh->sh_type == SHT_RELX
2329 && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name,
2330 ".debug_", sizeof(".debug_")-1))))
2331 continue;
2332 if (sh->sh_addralign < 1)
2333 sh->sh_addralign = 1;
2334 /* find corresponding section, if any */
2335 for(j = 1; j < s1->nb_sections;j++) {
2336 s = s1->sections[j];
2337 if (!strcmp(s->name, sh_name)) {
2338 if (!strncmp(sh_name, ".gnu.linkonce",
2339 sizeof(".gnu.linkonce") - 1)) {
2340 /* if a 'linkonce' section is already present, we
2341 do not add it again. It is a little tricky as
2342 symbols can still be defined in
2343 it. */
2344 sm_table[i].link_once = 1;
2345 goto next;
2346 } else {
2347 goto found;
2351 /* not found: create new section */
2352 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
2353 /* take as much info as possible from the section. sh_link and
2354 sh_info will be updated later */
2355 s->sh_addralign = sh->sh_addralign;
2356 s->sh_entsize = sh->sh_entsize;
2357 sm_table[i].new_section = 1;
2358 found:
2359 if (sh->sh_type != s->sh_type) {
2360 tcc_error_noabort("invalid section type");
2361 goto fail;
2364 /* align start of section */
2365 offset = s->data_offset;
2367 if (0 == strcmp(sh_name, ".stab")) {
2368 stab_index = i;
2369 goto no_align;
2371 if (0 == strcmp(sh_name, ".stabstr")) {
2372 stabstr_index = i;
2373 goto no_align;
2376 size = sh->sh_addralign - 1;
2377 offset = (offset + size) & ~size;
2378 if (sh->sh_addralign > s->sh_addralign)
2379 s->sh_addralign = sh->sh_addralign;
2380 s->data_offset = offset;
2381 no_align:
2382 sm_table[i].offset = offset;
2383 sm_table[i].s = s;
2384 /* concatenate sections */
2385 size = sh->sh_size;
2386 if (sh->sh_type != SHT_NOBITS) {
2387 unsigned char *ptr;
2388 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2389 ptr = section_ptr_add(s, size);
2390 read(fd, ptr, size);
2391 } else {
2392 s->data_offset += size;
2394 next: ;
2397 /* gr relocate stab strings */
2398 if (stab_index && stabstr_index) {
2399 Stab_Sym *a, *b;
2400 unsigned o;
2401 s = sm_table[stab_index].s;
2402 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2403 b = (Stab_Sym *)(s->data + s->data_offset);
2404 o = sm_table[stabstr_index].offset;
2405 while (a < b)
2406 a->n_strx += o, a++;
2409 /* second short pass to update sh_link and sh_info fields of new
2410 sections */
2411 for(i = 1; i < ehdr.e_shnum; i++) {
2412 s = sm_table[i].s;
2413 if (!s || !sm_table[i].new_section)
2414 continue;
2415 sh = &shdr[i];
2416 if (sh->sh_link > 0)
2417 s->link = sm_table[sh->sh_link].s;
2418 if (sh->sh_type == SHT_RELX) {
2419 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2420 /* update backward link */
2421 s1->sections[s->sh_info]->reloc = s;
2424 sm = sm_table;
2426 /* resolve symbols */
2427 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2429 sym = symtab + 1;
2430 for(i = 1; i < nb_syms; i++, sym++) {
2431 if (sym->st_shndx != SHN_UNDEF &&
2432 sym->st_shndx < SHN_LORESERVE) {
2433 sm = &sm_table[sym->st_shndx];
2434 if (sm->link_once) {
2435 /* if a symbol is in a link once section, we use the
2436 already defined symbol. It is very important to get
2437 correct relocations */
2438 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2439 name = (char *) strtab + sym->st_name;
2440 sym_index = find_elf_sym(symtab_section, name);
2441 if (sym_index)
2442 old_to_new_syms[i] = sym_index;
2444 continue;
2446 /* if no corresponding section added, no need to add symbol */
2447 if (!sm->s)
2448 continue;
2449 /* convert section number */
2450 sym->st_shndx = sm->s->sh_num;
2451 /* offset value */
2452 sym->st_value += sm->offset;
2454 /* add symbol */
2455 name = (char *) strtab + sym->st_name;
2456 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
2457 sym->st_info, sym->st_other,
2458 sym->st_shndx, name);
2459 old_to_new_syms[i] = sym_index;
2462 /* third pass to patch relocation entries */
2463 for(i = 1; i < ehdr.e_shnum; i++) {
2464 s = sm_table[i].s;
2465 if (!s)
2466 continue;
2467 sh = &shdr[i];
2468 offset = sm_table[i].offset;
2469 switch(s->sh_type) {
2470 case SHT_RELX:
2471 /* take relocation offset information */
2472 offseti = sm_table[sh->sh_info].offset;
2473 for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) {
2474 int type;
2475 unsigned sym_index;
2476 /* convert symbol index */
2477 type = ELFW(R_TYPE)(rel->r_info);
2478 sym_index = ELFW(R_SYM)(rel->r_info);
2479 /* NOTE: only one symtab assumed */
2480 if (sym_index >= nb_syms)
2481 goto invalid_reloc;
2482 sym_index = old_to_new_syms[sym_index];
2483 /* ignore link_once in rel section. */
2484 if (!sym_index && !sm->link_once
2485 #ifdef TCC_TARGET_ARM
2486 && type != R_ARM_V4BX
2487 #endif
2489 invalid_reloc:
2490 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2491 i, strsec + sh->sh_name, rel->r_offset);
2492 goto fail;
2494 rel->r_info = ELFW(R_INFO)(sym_index, type);
2495 /* offset the relocation offset */
2496 rel->r_offset += offseti;
2497 #ifdef TCC_TARGET_ARM
2498 /* Jumps and branches from a Thumb code to a PLT entry need
2499 special handling since PLT entries are ARM code.
2500 Unconditional bl instructions referencing PLT entries are
2501 handled by converting these instructions into blx
2502 instructions. Other case of instructions referencing a PLT
2503 entry require to add a Thumb stub before the PLT entry to
2504 switch to ARM mode. We set bit plt_thumb_stub of the
2505 attribute of a symbol to indicate such a case. */
2506 if (type == R_ARM_THM_JUMP24)
2507 get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
2508 #endif
2510 break;
2511 default:
2512 break;
2516 ret = 0;
2517 the_end:
2518 tcc_free(symtab);
2519 tcc_free(strtab);
2520 tcc_free(old_to_new_syms);
2521 tcc_free(sm_table);
2522 tcc_free(strsec);
2523 tcc_free(shdr);
2524 return ret;
2527 typedef struct ArchiveHeader {
2528 char ar_name[16]; /* name of this member */
2529 char ar_date[12]; /* file mtime */
2530 char ar_uid[6]; /* owner uid; printed as decimal */
2531 char ar_gid[6]; /* owner gid; printed as decimal */
2532 char ar_mode[8]; /* file mode, printed as octal */
2533 char ar_size[10]; /* file size, printed as decimal */
2534 char ar_fmag[2]; /* should contain ARFMAG */
2535 } ArchiveHeader;
2537 static int get_be32(const uint8_t *b)
2539 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2542 static long get_be64(const uint8_t *b)
2544 long long ret = get_be32(b);
2545 ret = (ret << 32) | (unsigned)get_be32(b+4);
2546 return (long)ret;
2549 /* load only the objects which resolve undefined symbols */
2550 static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
2552 long i, bound, nsyms, sym_index, off, ret;
2553 uint8_t *data;
2554 const char *ar_names, *p;
2555 const uint8_t *ar_index;
2556 ElfW(Sym) *sym;
2558 data = tcc_malloc(size);
2559 if (read(fd, data, size) != size)
2560 goto fail;
2561 nsyms = entrysize == 4 ? get_be32(data) : get_be64(data);
2562 ar_index = data + entrysize;
2563 ar_names = (char *) ar_index + nsyms * entrysize;
2565 do {
2566 bound = 0;
2567 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2568 sym_index = find_elf_sym(symtab_section, p);
2569 if(sym_index) {
2570 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2571 if(sym->st_shndx == SHN_UNDEF) {
2572 off = (entrysize == 4
2573 ? get_be32(ar_index + i * 4)
2574 : get_be64(ar_index + i * 8))
2575 + sizeof(ArchiveHeader);
2576 ++bound;
2577 if(tcc_load_object_file(s1, fd, off) < 0) {
2578 fail:
2579 ret = -1;
2580 goto the_end;
2585 } while(bound);
2586 ret = 0;
2587 the_end:
2588 tcc_free(data);
2589 return ret;
2592 /* load a '.a' file */
2593 ST_FUNC int tcc_load_archive(TCCState *s1, int fd)
2595 ArchiveHeader hdr;
2596 char ar_size[11];
2597 char ar_name[17];
2598 char magic[8];
2599 int size, len, i;
2600 unsigned long file_offset;
2602 /* skip magic which was already checked */
2603 read(fd, magic, sizeof(magic));
2605 for(;;) {
2606 len = read(fd, &hdr, sizeof(hdr));
2607 if (len == 0)
2608 break;
2609 if (len != sizeof(hdr)) {
2610 tcc_error_noabort("invalid archive");
2611 return -1;
2613 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2614 ar_size[sizeof(hdr.ar_size)] = '\0';
2615 size = strtol(ar_size, NULL, 0);
2616 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2617 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2618 if (ar_name[i] != ' ')
2619 break;
2621 ar_name[i + 1] = '\0';
2622 file_offset = lseek(fd, 0, SEEK_CUR);
2623 /* align to even */
2624 size = (size + 1) & ~1;
2625 if (!strcmp(ar_name, "/")) {
2626 /* coff symbol table : we handle it */
2627 if(s1->alacarte_link)
2628 return tcc_load_alacarte(s1, fd, size, 4);
2629 } else if (!strcmp(ar_name, "/SYM64/")) {
2630 if(s1->alacarte_link)
2631 return tcc_load_alacarte(s1, fd, size, 8);
2632 } else {
2633 ElfW(Ehdr) ehdr;
2634 if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
2635 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2636 return -1;
2639 lseek(fd, file_offset + size, SEEK_SET);
2641 return 0;
2644 #ifndef TCC_TARGET_PE
2645 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2646 is referenced by the user (so it should be added as DT_NEEDED in
2647 the generated ELF file) */
2648 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2650 ElfW(Ehdr) ehdr;
2651 ElfW(Shdr) *shdr, *sh, *sh1;
2652 int i, j, nb_syms, nb_dts, sym_bind, ret;
2653 ElfW(Sym) *sym, *dynsym;
2654 ElfW(Dyn) *dt, *dynamic;
2655 unsigned char *dynstr;
2656 const char *name, *soname;
2657 DLLReference *dllref;
2659 read(fd, &ehdr, sizeof(ehdr));
2661 /* test CPU specific stuff */
2662 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2663 ehdr.e_machine != EM_TCC_TARGET) {
2664 tcc_error_noabort("bad architecture");
2665 return -1;
2668 /* read sections */
2669 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2671 /* load dynamic section and dynamic symbols */
2672 nb_syms = 0;
2673 nb_dts = 0;
2674 dynamic = NULL;
2675 dynsym = NULL; /* avoid warning */
2676 dynstr = NULL; /* avoid warning */
2677 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2678 switch(sh->sh_type) {
2679 case SHT_DYNAMIC:
2680 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2681 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2682 break;
2683 case SHT_DYNSYM:
2684 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2685 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2686 sh1 = &shdr[sh->sh_link];
2687 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2688 break;
2689 default:
2690 break;
2694 /* compute the real library name */
2695 soname = tcc_basename(filename);
2697 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2698 if (dt->d_tag == DT_SONAME) {
2699 soname = (char *) dynstr + dt->d_un.d_val;
2703 /* if the dll is already loaded, do not load it */
2704 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2705 dllref = s1->loaded_dlls[i];
2706 if (!strcmp(soname, dllref->name)) {
2707 /* but update level if needed */
2708 if (level < dllref->level)
2709 dllref->level = level;
2710 ret = 0;
2711 goto the_end;
2715 /* add the dll and its level */
2716 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2717 dllref->level = level;
2718 strcpy(dllref->name, soname);
2719 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2721 /* add dynamic symbols in dynsym_section */
2722 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2723 sym_bind = ELFW(ST_BIND)(sym->st_info);
2724 if (sym_bind == STB_LOCAL)
2725 continue;
2726 name = (char *) dynstr + sym->st_name;
2727 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2728 sym->st_info, sym->st_other, sym->st_shndx, name);
2731 /* load all referenced DLLs */
2732 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2733 switch(dt->d_tag) {
2734 case DT_NEEDED:
2735 name = (char *) dynstr + dt->d_un.d_val;
2736 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2737 dllref = s1->loaded_dlls[j];
2738 if (!strcmp(name, dllref->name))
2739 goto already_loaded;
2741 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2742 tcc_error_noabort("referenced dll '%s' not found", name);
2743 ret = -1;
2744 goto the_end;
2746 already_loaded:
2747 break;
2750 ret = 0;
2751 the_end:
2752 tcc_free(dynstr);
2753 tcc_free(dynsym);
2754 tcc_free(dynamic);
2755 tcc_free(shdr);
2756 return ret;
2759 #define LD_TOK_NAME 256
2760 #define LD_TOK_EOF (-1)
2762 /* return next ld script token */
2763 static int ld_next(TCCState *s1, char *name, int name_size)
2765 int c;
2766 char *q;
2768 redo:
2769 switch(ch) {
2770 case ' ':
2771 case '\t':
2772 case '\f':
2773 case '\v':
2774 case '\r':
2775 case '\n':
2776 inp();
2777 goto redo;
2778 case '/':
2779 minp();
2780 if (ch == '*') {
2781 file->buf_ptr = parse_comment(file->buf_ptr);
2782 ch = file->buf_ptr[0];
2783 goto redo;
2784 } else {
2785 q = name;
2786 *q++ = '/';
2787 goto parse_name;
2789 break;
2790 case '\\':
2791 ch = handle_eob();
2792 if (ch != '\\')
2793 goto redo;
2794 /* fall through */
2795 /* case 'a' ... 'z': */
2796 case 'a':
2797 case 'b':
2798 case 'c':
2799 case 'd':
2800 case 'e':
2801 case 'f':
2802 case 'g':
2803 case 'h':
2804 case 'i':
2805 case 'j':
2806 case 'k':
2807 case 'l':
2808 case 'm':
2809 case 'n':
2810 case 'o':
2811 case 'p':
2812 case 'q':
2813 case 'r':
2814 case 's':
2815 case 't':
2816 case 'u':
2817 case 'v':
2818 case 'w':
2819 case 'x':
2820 case 'y':
2821 case 'z':
2822 /* case 'A' ... 'z': */
2823 case 'A':
2824 case 'B':
2825 case 'C':
2826 case 'D':
2827 case 'E':
2828 case 'F':
2829 case 'G':
2830 case 'H':
2831 case 'I':
2832 case 'J':
2833 case 'K':
2834 case 'L':
2835 case 'M':
2836 case 'N':
2837 case 'O':
2838 case 'P':
2839 case 'Q':
2840 case 'R':
2841 case 'S':
2842 case 'T':
2843 case 'U':
2844 case 'V':
2845 case 'W':
2846 case 'X':
2847 case 'Y':
2848 case 'Z':
2849 case '_':
2850 case '.':
2851 case '$':
2852 case '~':
2853 q = name;
2854 parse_name:
2855 for(;;) {
2856 if (!((ch >= 'a' && ch <= 'z') ||
2857 (ch >= 'A' && ch <= 'Z') ||
2858 (ch >= '0' && ch <= '9') ||
2859 strchr("/.-_+=$:\\,~", ch)))
2860 break;
2861 if ((q - name) < name_size - 1) {
2862 *q++ = ch;
2864 minp();
2866 *q = '\0';
2867 c = LD_TOK_NAME;
2868 break;
2869 case CH_EOF:
2870 c = LD_TOK_EOF;
2871 break;
2872 default:
2873 c = ch;
2874 inp();
2875 break;
2877 return c;
2880 static int ld_add_file(TCCState *s1, const char filename[])
2882 if (filename[0] == '/') {
2883 if (CONFIG_SYSROOT[0] == '\0'
2884 && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0)
2885 return 0;
2886 filename = tcc_basename(filename);
2888 return tcc_add_dll(s1, filename, 0);
2891 static inline int new_undef_syms(void)
2893 int ret = 0;
2894 ret = new_undef_sym;
2895 new_undef_sym = 0;
2896 return ret;
2899 static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
2901 char filename[1024], libname[1024];
2902 int t, group, nblibs = 0, ret = 0;
2903 char **libs = NULL;
2905 group = !strcmp(cmd, "GROUP");
2906 if (!as_needed)
2907 new_undef_syms();
2908 t = ld_next(s1, filename, sizeof(filename));
2909 if (t != '(')
2910 expect("(");
2911 t = ld_next(s1, filename, sizeof(filename));
2912 for(;;) {
2913 libname[0] = '\0';
2914 if (t == LD_TOK_EOF) {
2915 tcc_error_noabort("unexpected end of file");
2916 ret = -1;
2917 goto lib_parse_error;
2918 } else if (t == ')') {
2919 break;
2920 } else if (t == '-') {
2921 t = ld_next(s1, filename, sizeof(filename));
2922 if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
2923 tcc_error_noabort("library name expected");
2924 ret = -1;
2925 goto lib_parse_error;
2927 pstrcpy(libname, sizeof libname, &filename[1]);
2928 if (s1->static_link) {
2929 snprintf(filename, sizeof filename, "lib%s.a", libname);
2930 } else {
2931 snprintf(filename, sizeof filename, "lib%s.so", libname);
2933 } else if (t != LD_TOK_NAME) {
2934 tcc_error_noabort("filename expected");
2935 ret = -1;
2936 goto lib_parse_error;
2938 if (!strcmp(filename, "AS_NEEDED")) {
2939 ret = ld_add_file_list(s1, cmd, 1);
2940 if (ret)
2941 goto lib_parse_error;
2942 } else {
2943 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2944 if (!as_needed) {
2945 ret = ld_add_file(s1, filename);
2946 if (ret)
2947 goto lib_parse_error;
2948 if (group) {
2949 /* Add the filename *and* the libname to avoid future conversions */
2950 dynarray_add(&libs, &nblibs, tcc_strdup(filename));
2951 if (libname[0] != '\0')
2952 dynarray_add(&libs, &nblibs, tcc_strdup(libname));
2956 t = ld_next(s1, filename, sizeof(filename));
2957 if (t == ',') {
2958 t = ld_next(s1, filename, sizeof(filename));
2961 if (group && !as_needed) {
2962 while (new_undef_syms()) {
2963 int i;
2965 for (i = 0; i < nblibs; i ++)
2966 ld_add_file(s1, libs[i]);
2969 lib_parse_error:
2970 dynarray_reset(&libs, &nblibs);
2971 return ret;
2974 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2975 files */
2976 ST_FUNC int tcc_load_ldscript(TCCState *s1)
2978 char cmd[64];
2979 char filename[1024];
2980 int t, ret;
2982 ch = handle_eob();
2983 for(;;) {
2984 t = ld_next(s1, cmd, sizeof(cmd));
2985 if (t == LD_TOK_EOF)
2986 return 0;
2987 else if (t != LD_TOK_NAME)
2988 return -1;
2989 if (!strcmp(cmd, "INPUT") ||
2990 !strcmp(cmd, "GROUP")) {
2991 ret = ld_add_file_list(s1, cmd, 0);
2992 if (ret)
2993 return ret;
2994 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
2995 !strcmp(cmd, "TARGET")) {
2996 /* ignore some commands */
2997 t = ld_next(s1, cmd, sizeof(cmd));
2998 if (t != '(')
2999 expect("(");
3000 for(;;) {
3001 t = ld_next(s1, filename, sizeof(filename));
3002 if (t == LD_TOK_EOF) {
3003 tcc_error_noabort("unexpected end of file");
3004 return -1;
3005 } else if (t == ')') {
3006 break;
3009 } else {
3010 return -1;
3013 return 0;
3015 #endif /* !TCC_TARGET_PE */