Fix another corner case with C/asm symtable
[tinycc.git] / tccelf.c
blob422dfd6ffe5c4c27f91a34a877940a83af43640c
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 || sym->st_shndx == SHN_UNDEF) {
315 h = elf_hash(strtab + sym->st_name) % nb_buckets;
316 *ptr = hash[h];
317 hash[h] = sym_index;
318 } else {
319 *ptr = 0;
321 ptr++;
322 sym++;
326 /* return the symbol number */
327 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
328 int info, int other, int shndx, const char *name)
330 int name_offset, sym_index;
331 int nbuckets, h;
332 ElfW(Sym) *sym;
333 Section *hs;
335 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
336 if (name)
337 name_offset = put_elf_str(s->link, name);
338 else
339 name_offset = 0;
340 /* XXX: endianness */
341 sym->st_name = name_offset;
342 sym->st_value = value;
343 sym->st_size = size;
344 sym->st_info = info;
345 sym->st_other = other;
346 sym->st_shndx = shndx;
347 sym_index = sym - (ElfW(Sym) *)s->data;
348 hs = s->hash;
349 if (hs) {
350 int *ptr, *base;
351 ptr = section_ptr_add(hs, sizeof(int));
352 base = (int *)hs->data;
353 /* only add global, weak or undef symbols. The latter might
354 become global late (from asm references). */
355 if (ELFW(ST_BIND)(info) != STB_LOCAL || shndx == SHN_UNDEF) {
356 /* add another hashing entry */
357 nbuckets = base[0];
358 h = elf_hash((unsigned char *) name) % nbuckets;
359 *ptr = base[2 + h];
360 base[2 + h] = sym_index;
361 base[1]++;
362 /* we resize the hash table */
363 hs->nb_hashed_syms++;
364 if (hs->nb_hashed_syms > 2 * nbuckets) {
365 rebuild_hash(s, 2 * nbuckets);
367 } else {
368 *ptr = 0;
369 base[1]++;
372 return sym_index;
375 /* find global ELF symbol 'name' and return its index. Return 0 if not
376 found. */
377 ST_FUNC int find_elf_sym(Section *s, const char *name)
379 ElfW(Sym) *sym;
380 Section *hs;
381 int nbuckets, sym_index, h;
382 const char *name1;
384 hs = s->hash;
385 if (!hs)
386 return 0;
387 nbuckets = ((int *)hs->data)[0];
388 h = elf_hash((unsigned char *) name) % nbuckets;
389 sym_index = ((int *)hs->data)[2 + h];
390 while (sym_index != 0) {
391 sym = &((ElfW(Sym) *)s->data)[sym_index];
392 name1 = (char *) s->link->data + sym->st_name;
393 if (!strcmp(name, name1))
394 return sym_index;
395 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
397 return 0;
400 /* return elf symbol value, signal error if 'err' is nonzero */
401 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err)
403 int sym_index;
404 ElfW(Sym) *sym;
406 sym_index = find_elf_sym(s->symtab, name);
407 sym = &((ElfW(Sym) *)s->symtab->data)[sym_index];
408 if (!sym_index || sym->st_shndx == SHN_UNDEF) {
409 if (err)
410 tcc_error("%s not defined", name);
411 return 0;
413 return sym->st_value;
416 /* return elf symbol value */
417 LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
419 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0);
422 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
423 /* return elf symbol value or error */
424 ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)
426 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 1);
428 #endif
430 /* add an elf symbol : check if it is already defined and patch
431 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
432 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
433 int info, int other, int shndx, const char *name)
435 ElfW(Sym) *esym;
436 int sym_bind, sym_index, sym_type, esym_bind;
437 unsigned char sym_vis, esym_vis, new_vis;
439 sym_bind = ELFW(ST_BIND)(info);
440 sym_type = ELFW(ST_TYPE)(info);
441 sym_vis = ELFW(ST_VISIBILITY)(other);
443 sym_index = find_elf_sym(s, name);
444 esym = &((ElfW(Sym) *)s->data)[sym_index];
445 if (sym_index && esym->st_value == value && esym->st_size == size
446 && esym->st_info == info && esym->st_other == other
447 && esym->st_shndx == shndx)
448 return sym_index;
450 if (sym_bind != STB_LOCAL) {
451 /* we search global or weak symbols */
452 if (!sym_index)
453 goto do_def;
454 if (esym->st_shndx != SHN_UNDEF) {
455 esym_bind = ELFW(ST_BIND)(esym->st_info);
456 /* propagate the most constraining visibility */
457 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
458 esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
459 if (esym_vis == STV_DEFAULT) {
460 new_vis = sym_vis;
461 } else if (sym_vis == STV_DEFAULT) {
462 new_vis = esym_vis;
463 } else {
464 new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
466 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
467 | new_vis;
468 other = esym->st_other; /* in case we have to patch esym */
469 if (shndx == SHN_UNDEF) {
470 /* ignore adding of undefined symbol if the
471 corresponding symbol is already defined */
472 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
473 /* global overrides weak, so patch */
474 goto do_patch;
475 } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
476 /* weak is ignored if already global */
477 } else if (sym_bind == STB_WEAK && esym_bind == STB_WEAK) {
478 /* keep first-found weak definition, ignore subsequents */
479 } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
480 /* ignore hidden symbols after */
481 } else if ((esym->st_shndx == SHN_COMMON
482 || esym->st_shndx == bss_section->sh_num)
483 && (shndx < SHN_LORESERVE
484 && shndx != bss_section->sh_num)) {
485 /* data symbol gets precedence over common/bss */
486 goto do_patch;
487 } else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
488 /* data symbol keeps precedence over common/bss */
489 } else if (s == tcc_state->dynsymtab_section) {
490 /* we accept that two DLL define the same symbol */
491 } else if (esym->st_other & ST_ASM_SET) {
492 /* If the existing symbol came from an asm .set
493 we can override. */
494 goto do_patch;
495 } else {
496 #if 0
497 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
498 sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
499 #endif
500 tcc_error_noabort("'%s' defined twice", name);
502 } else {
503 do_patch:
504 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
505 esym->st_shndx = shndx;
506 new_undef_sym = 1;
507 esym->st_value = value;
508 esym->st_size = size;
509 esym->st_other = other;
511 } else {
512 do_def:
513 sym_index = put_elf_sym(s, value, size,
514 ELFW(ST_INFO)(sym_bind, sym_type), other,
515 shndx, name);
517 return sym_index;
520 /* put relocation */
521 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
522 int type, int symbol, addr_t addend)
524 char buf[256];
525 Section *sr;
526 ElfW_Rel *rel;
528 sr = s->reloc;
529 if (!sr) {
530 /* if no relocation section, create it */
531 snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
532 /* if the symtab is allocated, then we consider the relocation
533 are also */
534 sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
535 sr->sh_entsize = sizeof(ElfW_Rel);
536 sr->link = symtab;
537 sr->sh_info = s->sh_num;
538 s->reloc = sr;
540 rel = section_ptr_add(sr, sizeof(ElfW_Rel));
541 rel->r_offset = offset;
542 rel->r_info = ELFW(R_INFO)(symbol, type);
543 #if SHT_RELX == SHT_RELA
544 rel->r_addend = addend;
545 #else
546 if (addend)
547 tcc_error("non-zero addend on REL architecture");
548 #endif
551 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
552 int type, int symbol)
554 put_elf_reloca(symtab, s, offset, type, symbol, 0);
557 /* Remove relocations for section S->reloc starting at oldrelocoffset
558 that are to the same place, retaining the last of them. As side effect
559 the relocations are sorted. Possibly reduces the number of relocs. */
560 ST_FUNC void squeeze_multi_relocs(Section *s, size_t oldrelocoffset)
562 Section *sr = s->reloc;
563 ElfW_Rel *r, *dest;
564 ssize_t a;
565 ElfW(Addr) addr;
567 if (oldrelocoffset + sizeof(*r) >= sr->data_offset)
568 return;
569 /* The relocs we're dealing with are the result of initializer parsing.
570 So they will be mostly in order and there aren't many of them.
571 Secondly we need a stable sort (which qsort isn't). We use
572 a simple insertion sort. */
573 for (a = oldrelocoffset + sizeof(*r); a < sr->data_offset; a += sizeof(*r)) {
574 ssize_t i = a - sizeof(*r);
575 addr = ((ElfW_Rel*)(sr->data + a))->r_offset;
576 for (; i >= (ssize_t)oldrelocoffset &&
577 ((ElfW_Rel*)(sr->data + i))->r_offset > addr; i -= sizeof(*r)) {
578 ElfW_Rel tmp = *(ElfW_Rel*)(sr->data + a);
579 *(ElfW_Rel*)(sr->data + a) = *(ElfW_Rel*)(sr->data + i);
580 *(ElfW_Rel*)(sr->data + i) = tmp;
584 r = (ElfW_Rel*)(sr->data + oldrelocoffset);
585 dest = r;
586 for (; r < (ElfW_Rel*)(sr->data + sr->data_offset); r++) {
587 if (dest->r_offset != r->r_offset)
588 dest++;
589 *dest = *r;
591 sr->data_offset = (unsigned char*)dest - sr->data + sizeof(*r);
594 /* put stab debug information */
596 ST_FUNC void put_stabs(const char *str, int type, int other, int desc,
597 unsigned long value)
599 Stab_Sym *sym;
601 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
602 if (str) {
603 sym->n_strx = put_elf_str(stabstr_section, str);
604 } else {
605 sym->n_strx = 0;
607 sym->n_type = type;
608 sym->n_other = other;
609 sym->n_desc = desc;
610 sym->n_value = value;
613 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc,
614 unsigned long value, Section *sec, int sym_index)
616 put_stabs(str, type, other, desc, value);
617 put_elf_reloc(symtab_section, stab_section,
618 stab_section->data_offset - sizeof(unsigned int),
619 R_DATA_32, sym_index);
622 ST_FUNC void put_stabn(int type, int other, int desc, int value)
624 put_stabs(NULL, type, other, desc, value);
627 ST_FUNC void put_stabd(int type, int other, int desc)
629 put_stabs(NULL, type, other, desc, 0);
632 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
634 int n;
635 struct sym_attr *tab;
637 if (index >= s1->nb_sym_attrs) {
638 if (!alloc)
639 return s1->sym_attrs;
640 /* find immediately bigger power of 2 and reallocate array */
641 n = 1;
642 while (index >= n)
643 n *= 2;
644 tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
645 s1->sym_attrs = tab;
646 memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
647 (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
648 s1->nb_sym_attrs = n;
650 return &s1->sym_attrs[index];
653 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
654 using variable <elem> */
655 #define for_each_elem(sec, startoff, elem, type) \
656 for (elem = (type *) sec->data + startoff; \
657 elem < (type *) (sec->data + sec->data_offset); elem++)
659 /* In an ELF file symbol table, the local symbols must appear below
660 the global and weak ones. Since TCC cannot sort it while generating
661 the code, we must do it after. All the relocation tables are also
662 modified to take into account the symbol table sorting */
663 static void sort_syms(TCCState *s1, Section *s)
665 int *old_to_new_syms;
666 ElfW(Sym) *new_syms;
667 int nb_syms, i;
668 ElfW(Sym) *p, *q;
669 ElfW_Rel *rel;
670 Section *sr;
671 int type, sym_index;
673 nb_syms = s->data_offset / sizeof(ElfW(Sym));
674 new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
675 old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
677 /* first pass for local symbols */
678 p = (ElfW(Sym) *)s->data;
679 q = new_syms;
680 for(i = 0; i < nb_syms; i++) {
681 if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
682 old_to_new_syms[i] = q - new_syms;
683 *q++ = *p;
685 p++;
687 /* save the number of local symbols in section header */
688 if( s->sh_size ) /* this 'if' makes IDA happy */
689 s->sh_info = q - new_syms;
691 /* then second pass for non local symbols */
692 p = (ElfW(Sym) *)s->data;
693 for(i = 0; i < nb_syms; i++) {
694 if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
695 old_to_new_syms[i] = q - new_syms;
696 *q++ = *p;
698 p++;
701 /* we copy the new symbols to the old */
702 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
703 tcc_free(new_syms);
705 /* now we modify all the relocations */
706 for(i = 1; i < s1->nb_sections; i++) {
707 sr = s1->sections[i];
708 if (sr->sh_type == SHT_RELX && sr->link == s) {
709 for_each_elem(sr, 0, rel, ElfW_Rel) {
710 sym_index = ELFW(R_SYM)(rel->r_info);
711 type = ELFW(R_TYPE)(rel->r_info);
712 sym_index = old_to_new_syms[sym_index];
713 rel->r_info = ELFW(R_INFO)(sym_index, type);
718 tcc_free(old_to_new_syms);
721 /* relocate common symbols in the .bss section */
722 ST_FUNC void relocate_common_syms(void)
724 ElfW(Sym) *sym;
726 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
727 if (sym->st_shndx == SHN_COMMON) {
728 /* symbol alignment is in st_value for SHN_COMMONs */
729 sym->st_value = section_add(bss_section, sym->st_size,
730 sym->st_value);
731 sym->st_shndx = bss_section->sh_num;
736 /* relocate symbol table, resolve undefined symbols if do_resolve is
737 true and output error if undefined symbol. */
738 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
740 ElfW(Sym) *sym;
741 int sym_bind, sh_num;
742 const char *name;
744 for_each_elem(symtab, 1, sym, ElfW(Sym)) {
745 sh_num = sym->st_shndx;
746 if (sh_num == SHN_UNDEF) {
747 name = (char *) strtab_section->data + sym->st_name;
748 /* Use ld.so to resolve symbol for us (for tcc -run) */
749 if (do_resolve) {
750 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
751 void *addr = dlsym(RTLD_DEFAULT, name);
752 if (addr) {
753 sym->st_value = (addr_t) addr;
754 #ifdef DEBUG_RELOC
755 printf ("relocate_sym: %s -> 0x%lx\n", name, sym->st_value);
756 #endif
757 goto found;
759 #endif
760 /* if dynamic symbol exist, it will be used in relocate_section */
761 } else if (s1->dynsym && find_elf_sym(s1->dynsym, name))
762 goto found;
763 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
764 it */
765 if (!strcmp(name, "_fp_hw"))
766 goto found;
767 /* only weak symbols are accepted to be undefined. Their
768 value is zero */
769 sym_bind = ELFW(ST_BIND)(sym->st_info);
770 if (sym_bind == STB_WEAK)
771 sym->st_value = 0;
772 else
773 tcc_error_noabort("undefined symbol '%s'", name);
774 } else if (sh_num < SHN_LORESERVE) {
775 /* add section base */
776 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
778 found: ;
782 /* relocate a given section (CPU dependent) by applying the relocations
783 in the associated relocation section */
784 ST_FUNC void relocate_section(TCCState *s1, Section *s)
786 Section *sr = s->reloc;
787 ElfW_Rel *rel;
788 ElfW(Sym) *sym;
789 int type, sym_index;
790 unsigned char *ptr;
791 addr_t tgt, addr;
793 relocate_init(sr);
795 for_each_elem(sr, 0, rel, ElfW_Rel) {
796 ptr = s->data + rel->r_offset;
797 sym_index = ELFW(R_SYM)(rel->r_info);
798 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
799 type = ELFW(R_TYPE)(rel->r_info);
800 tgt = sym->st_value;
801 #if SHT_RELX == SHT_RELA
802 tgt += rel->r_addend;
803 #endif
804 addr = s->sh_addr + rel->r_offset;
805 relocate(s1, rel, type, ptr, addr, tgt);
807 /* if the relocation is allocated, we change its symbol table */
808 if (sr->sh_flags & SHF_ALLOC)
809 sr->link = s1->dynsym;
812 /* relocate relocation table in 'sr' */
813 static void relocate_rel(TCCState *s1, Section *sr)
815 Section *s;
816 ElfW_Rel *rel;
818 s = s1->sections[sr->sh_info];
819 for_each_elem(sr, 0, rel, ElfW_Rel)
820 rel->r_offset += s->sh_addr;
823 /* count the number of dynamic relocations so that we can reserve
824 their space */
825 static int prepare_dynamic_rel(TCCState *s1, Section *sr)
827 ElfW_Rel *rel;
828 int sym_index, type, count;
830 count = 0;
831 for_each_elem(sr, 0, rel, ElfW_Rel) {
832 sym_index = ELFW(R_SYM)(rel->r_info);
833 type = ELFW(R_TYPE)(rel->r_info);
834 switch(type) {
835 #if defined(TCC_TARGET_I386)
836 case R_386_32:
837 if (!get_sym_attr(s1, sym_index, 0)->dyn_index
838 && ((ElfW(Sym)*)symtab_section->data + sym_index)->st_shndx == SHN_UNDEF) {
839 /* don't fixup unresolved (weak) symbols */
840 rel->r_info = ELFW(R_INFO)(sym_index, R_386_RELATIVE);
841 break;
843 #elif defined(TCC_TARGET_X86_64)
844 case R_X86_64_32:
845 case R_X86_64_32S:
846 case R_X86_64_64:
847 #endif
848 count++;
849 break;
850 #if defined(TCC_TARGET_I386)
851 case R_386_PC32:
852 #elif defined(TCC_TARGET_X86_64)
853 case R_X86_64_PC32:
854 #endif
855 if (get_sym_attr(s1, sym_index, 0)->dyn_index)
856 count++;
857 break;
858 default:
859 break;
862 if (count) {
863 /* allocate the section */
864 sr->sh_flags |= SHF_ALLOC;
865 sr->sh_size = count * sizeof(ElfW_Rel);
867 return count;
870 static void build_got(TCCState *s1)
872 /* if no got, then create it */
873 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
874 s1->got->sh_entsize = 4;
875 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
876 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
877 /* keep space for _DYNAMIC pointer and two dummy got entries */
878 section_ptr_add(s1->got, 3 * PTR_SIZE);
881 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
882 in s1->symtab. When creating the dynamic symbol table entry for the GOT
883 relocation, use 'size' and 'info' for the corresponding symbol metadata.
884 Returns the offset of the GOT or (if any) PLT entry. */
885 static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
886 unsigned long size,
887 int info, int sym_index)
889 int need_plt_entry;
890 const char *name;
891 ElfW(Sym) *sym;
892 struct sym_attr *attr;
893 unsigned got_offset;
894 char plt_name[100];
895 int len;
897 need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
898 attr = get_sym_attr(s1, sym_index, 1);
900 /* In case a function is both called and its address taken 2 GOT entries
901 are created, one for taking the address (GOT) and the other for the PLT
902 entry (PLTGOT). */
903 if (need_plt_entry ? attr->plt_offset : attr->got_offset)
904 return attr;
906 /* create the GOT entry */
907 got_offset = s1->got->data_offset;
908 section_ptr_add(s1->got, PTR_SIZE);
910 /* Create the GOT relocation that will insert the address of the object or
911 function of interest in the GOT entry. This is a static relocation for
912 memory output (dlsym will give us the address of symbols) and dynamic
913 relocation otherwise (executable and DLLs). The relocation should be
914 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
915 associated to a PLT entry) but is currently done at load time for an
916 unknown reason. */
918 sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
919 name = (char *) symtab_section->link->data + sym->st_name;
921 if (s1->dynsym) {
922 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
923 /* Hack alarm. We don't want to emit dynamic symbols
924 and symbol based relocs for STB_LOCAL symbols, but rather
925 want to resolve them directly. At this point the symbol
926 values aren't final yet, so we must defer this. We will later
927 have to create a RELATIVE reloc anyway, so we misuse the
928 relocation slot to smuggle the symbol reference until
929 fill_local_got_entries. Not that the sym_index is
930 relative to symtab_section, not s1->dynsym! Nevertheless
931 we use s1->dyn_sym so that if this is the first call
932 that got->reloc is correctly created. Also note that
933 RELATIVE relocs are not normally created for the .got,
934 so the types serves as a marker for later (and is retained
935 also for the final output, which is okay because then the
936 got is just normal data). */
937 put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE,
938 sym_index);
939 } else {
940 if (0 == attr->dyn_index)
941 attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size,
942 info, 0, sym->st_shndx, name);
943 put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type,
944 attr->dyn_index);
946 } else {
947 put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
948 sym_index);
951 if (need_plt_entry) {
952 if (!s1->plt) {
953 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
954 SHF_ALLOC | SHF_EXECINSTR);
955 s1->plt->sh_entsize = 4;
958 attr->plt_offset = create_plt_entry(s1, got_offset, attr);
960 /* create a symbol 'sym@plt' for the PLT jump vector */
961 len = strlen(name);
962 if (len > sizeof plt_name - 5)
963 len = sizeof plt_name - 5;
964 memcpy(plt_name, name, len);
965 strcpy(plt_name + len, "@plt");
966 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
967 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
969 } else {
970 attr->got_offset = got_offset;
973 return attr;
976 /* build GOT and PLT entries */
977 ST_FUNC void build_got_entries(TCCState *s1)
979 Section *s;
980 ElfW_Rel *rel;
981 ElfW(Sym) *sym;
982 int i, type, gotplt_entry, reloc_type, sym_index;
983 struct sym_attr *attr;
985 for(i = 1; i < s1->nb_sections; i++) {
986 s = s1->sections[i];
987 if (s->sh_type != SHT_RELX)
988 continue;
989 /* no need to handle got relocations */
990 if (s->link != symtab_section)
991 continue;
992 for_each_elem(s, 0, rel, ElfW_Rel) {
993 type = ELFW(R_TYPE)(rel->r_info);
994 gotplt_entry = gotplt_entry_type(type);
995 sym_index = ELFW(R_SYM)(rel->r_info);
996 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
998 if (gotplt_entry == NO_GOTPLT_ENTRY) {
999 continue;
1002 /* Automatically create PLT/GOT [entry] if it is an undefined
1003 reference (resolved at runtime), or the symbol is absolute,
1004 probably created by tcc_add_symbol, and thus on 64-bit
1005 targets might be too far from application code. */
1006 if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
1007 if (sym->st_shndx == SHN_UNDEF) {
1008 ElfW(Sym) *esym;
1009 int dynindex;
1010 if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
1011 continue;
1012 /* Relocations for UNDEF symbols would normally need
1013 to be transferred into the executable or shared object.
1014 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1015 But TCC doesn't do that (at least for exes), so we
1016 need to resolve all such relocs locally. And that
1017 means PLT slots for functions in DLLs and COPY relocs for
1018 data symbols. COPY relocs were generated in
1019 bind_exe_dynsyms (and the symbol adjusted to be defined),
1020 and for functions we were generated a dynamic symbol
1021 of function type. */
1022 if (s1->dynsym) {
1023 /* dynsym isn't set for -run :-/ */
1024 dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index;
1025 esym = (ElfW(Sym) *)s1->dynsym->data + dynindex;
1026 if (dynindex
1027 && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
1028 || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
1029 && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC)))
1030 goto jmp_slot;
1032 } else if (!(sym->st_shndx == SHN_ABS
1033 #ifndef TCC_TARGET_ARM
1034 && PTR_SIZE == 8
1035 #endif
1037 continue;
1040 #ifdef TCC_TARGET_X86_64
1041 if ((type == R_X86_64_PLT32 || type == R_X86_64_PC32) &&
1042 (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT ||
1043 ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)) {
1044 rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
1045 continue;
1047 #endif
1048 if (code_reloc(type)) {
1049 jmp_slot:
1050 reloc_type = R_JMP_SLOT;
1051 } else
1052 reloc_type = R_GLOB_DAT;
1054 if (!s1->got)
1055 build_got(s1);
1057 if (gotplt_entry == BUILD_GOT_ONLY)
1058 continue;
1060 attr = put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1061 sym_index);
1063 if (reloc_type == R_JMP_SLOT)
1064 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
1069 /* put dynamic tag */
1070 static void put_dt(Section *dynamic, int dt, addr_t val)
1072 ElfW(Dyn) *dyn;
1073 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1074 dyn->d_tag = dt;
1075 dyn->d_un.d_val = val;
1078 #ifndef TCC_TARGET_PE
1079 static void add_init_array_defines(TCCState *s1, const char *section_name)
1081 Section *s;
1082 long end_offset;
1083 char sym_start[1024];
1084 char sym_end[1024];
1086 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1087 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1089 s = find_section(s1, section_name);
1090 if (!s) {
1091 end_offset = 0;
1092 s = data_section;
1093 } else {
1094 end_offset = s->data_offset;
1097 set_elf_sym(symtab_section,
1098 0, 0,
1099 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1100 s->sh_num, sym_start);
1101 set_elf_sym(symtab_section,
1102 end_offset, 0,
1103 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1104 s->sh_num, sym_end);
1106 #endif
1108 static int tcc_add_support(TCCState *s1, const char *filename)
1110 char buf[1024];
1111 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
1112 return tcc_add_file(s1, buf);
1115 ST_FUNC void tcc_add_bcheck(TCCState *s1)
1117 #ifdef CONFIG_TCC_BCHECK
1118 addr_t *ptr;
1119 int sym_index;
1121 if (0 == s1->do_bounds_check)
1122 return;
1123 /* XXX: add an object file to do that */
1124 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1125 *ptr = 0;
1126 set_elf_sym(symtab_section, 0, 0,
1127 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1128 bounds_section->sh_num, "__bounds_start");
1129 /* pull bcheck.o from libtcc1.a */
1130 sym_index = set_elf_sym(symtab_section, 0, 0,
1131 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1132 SHN_UNDEF, "__bound_init");
1133 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1134 /* add 'call __bound_init()' in .init section */
1135 Section *init_section = find_section(s1, ".init");
1136 unsigned char *pinit = section_ptr_add(init_section, 5);
1137 pinit[0] = 0xe8;
1138 write32le(pinit + 1, -4);
1139 put_elf_reloc(symtab_section, init_section,
1140 init_section->data_offset - 4, R_386_PC32, sym_index);
1141 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1143 #endif
1146 /* add tcc runtime libraries */
1147 ST_FUNC void tcc_add_runtime(TCCState *s1)
1149 tcc_add_bcheck(s1);
1150 tcc_add_pragma_libs(s1);
1151 /* add libc */
1152 if (!s1->nostdlib) {
1153 tcc_add_library_err(s1, "c");
1154 #ifdef TCC_LIBGCC
1155 if (!s1->static_link) {
1156 if (TCC_LIBGCC[0] == '/')
1157 tcc_add_file(s1, TCC_LIBGCC);
1158 else
1159 tcc_add_dll(s1, TCC_LIBGCC, 0);
1161 #endif
1162 tcc_add_support(s1, TCC_LIBTCC1);
1163 /* add crt end if not memory output */
1164 if (s1->output_type != TCC_OUTPUT_MEMORY)
1165 tcc_add_crt(s1, "crtn.o");
1169 /* add various standard linker symbols (must be done after the
1170 sections are filled (for example after allocating common
1171 symbols)) */
1172 ST_FUNC void tcc_add_linker_symbols(TCCState *s1)
1174 char buf[1024];
1175 int i;
1176 Section *s;
1178 set_elf_sym(symtab_section,
1179 text_section->data_offset, 0,
1180 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1181 text_section->sh_num, "_etext");
1182 set_elf_sym(symtab_section,
1183 data_section->data_offset, 0,
1184 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1185 data_section->sh_num, "_edata");
1186 set_elf_sym(symtab_section,
1187 bss_section->data_offset, 0,
1188 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1189 bss_section->sh_num, "_end");
1190 #ifndef TCC_TARGET_PE
1191 /* horrible new standard ldscript defines */
1192 add_init_array_defines(s1, ".preinit_array");
1193 add_init_array_defines(s1, ".init_array");
1194 add_init_array_defines(s1, ".fini_array");
1195 #endif
1197 /* add start and stop symbols for sections whose name can be
1198 expressed in C */
1199 for(i = 1; i < s1->nb_sections; i++) {
1200 s = s1->sections[i];
1201 if (s->sh_type == SHT_PROGBITS &&
1202 (s->sh_flags & SHF_ALLOC)) {
1203 const char *p;
1204 int ch;
1206 /* check if section name can be expressed in C */
1207 p = s->name;
1208 for(;;) {
1209 ch = *p;
1210 if (!ch)
1211 break;
1212 if (!isid(ch) && !isnum(ch))
1213 goto next_sec;
1214 p++;
1216 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1217 set_elf_sym(symtab_section,
1218 0, 0,
1219 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1220 s->sh_num, buf);
1221 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1222 set_elf_sym(symtab_section,
1223 s->data_offset, 0,
1224 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1225 s->sh_num, buf);
1227 next_sec: ;
1231 static void tcc_output_binary(TCCState *s1, FILE *f,
1232 const int *sec_order)
1234 Section *s;
1235 int i, offset, size;
1237 offset = 0;
1238 for(i=1;i<s1->nb_sections;i++) {
1239 s = s1->sections[sec_order[i]];
1240 if (s->sh_type != SHT_NOBITS &&
1241 (s->sh_flags & SHF_ALLOC)) {
1242 while (offset < s->sh_offset) {
1243 fputc(0, f);
1244 offset++;
1246 size = s->sh_size;
1247 fwrite(s->data, 1, size, f);
1248 offset += size;
1253 ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1255 int sym_index = ELFW(R_SYM) (rel->r_info);
1256 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1257 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1258 unsigned offset = attr->got_offset;
1260 if (0 == offset)
1261 return;
1262 section_reserve(s1->got, offset + PTR_SIZE);
1263 #ifdef TCC_TARGET_X86_64
1264 write64le(s1->got->data + offset, sym->st_value);
1265 #else
1266 write32le(s1->got->data + offset, sym->st_value);
1267 #endif
1270 /* Perform relocation to GOT or PLT entries */
1271 ST_FUNC void fill_got(TCCState *s1)
1273 Section *s;
1274 ElfW_Rel *rel;
1275 int i;
1277 for(i = 1; i < s1->nb_sections; i++) {
1278 s = s1->sections[i];
1279 if (s->sh_type != SHT_RELX)
1280 continue;
1281 /* no need to handle got relocations */
1282 if (s->link != symtab_section)
1283 continue;
1284 for_each_elem(s, 0, rel, ElfW_Rel) {
1285 switch (ELFW(R_TYPE) (rel->r_info)) {
1286 case R_X86_64_GOT32:
1287 case R_X86_64_GOTPCREL:
1288 case R_X86_64_GOTPCRELX:
1289 case R_X86_64_REX_GOTPCRELX:
1290 case R_X86_64_PLT32:
1291 fill_got_entry(s1, rel);
1292 break;
1298 /* See put_got_entry for a description. This is the second stage
1299 where GOT references to local defined symbols are rewritten. */
1300 static void fill_local_got_entries(TCCState *s1)
1302 ElfW_Rel *rel;
1303 for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
1304 if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
1305 int sym_index = ELFW(R_SYM) (rel->r_info);
1306 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1307 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1308 unsigned offset = attr->got_offset;
1309 if (offset != rel->r_offset - s1->got->sh_addr)
1310 tcc_error_noabort("huh");
1311 rel->r_info = ELFW(R_INFO)(0, R_RELATIVE);
1312 #if SHT_RELX == SHT_RELA
1313 rel->r_addend = sym->st_value;
1314 #else
1315 /* All our REL architectures also happen to be 32bit LE. */
1316 write32le(s1->got->data + offset, sym->st_value);
1317 #endif
1322 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1323 in shared libraries and export non local defined symbols to shared libraries
1324 if -rdynamic switch was given on command line */
1325 static void bind_exe_dynsyms(TCCState *s1)
1327 const char *name;
1328 int sym_index, index;
1329 ElfW(Sym) *sym, *esym;
1330 int type;
1332 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1333 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1334 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1335 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1336 if (sym->st_shndx == SHN_UNDEF) {
1337 name = (char *) symtab_section->link->data + sym->st_name;
1338 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1339 if (sym_index) {
1340 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1341 type = ELFW(ST_TYPE)(esym->st_info);
1342 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
1343 /* Indirect functions shall have STT_FUNC type in executable
1344 * dynsym section. Indeed, a dlsym call following a lazy
1345 * resolution would pick the symbol value from the
1346 * executable dynsym entry which would contain the address
1347 * of the function wanted by the caller of dlsym instead of
1348 * the address of the function that would return that
1349 * address */
1350 int dynindex
1351 = put_elf_sym(s1->dynsym, 0, esym->st_size,
1352 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
1353 name);
1354 int index = sym - (ElfW(Sym) *) symtab_section->data;
1355 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1356 } else if (type == STT_OBJECT) {
1357 unsigned long offset;
1358 ElfW(Sym) *dynsym;
1359 offset = bss_section->data_offset;
1360 /* XXX: which alignment ? */
1361 offset = (offset + 16 - 1) & -16;
1362 set_elf_sym (s1->symtab, offset, esym->st_size,
1363 esym->st_info, 0, bss_section->sh_num, name);
1364 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1365 esym->st_info, 0, bss_section->sh_num,
1366 name);
1368 /* Ensure R_COPY works for weak symbol aliases */
1369 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1370 for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
1371 if ((dynsym->st_value == esym->st_value)
1372 && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
1373 char *dynname = (char *) s1->dynsymtab_section->link->data
1374 + dynsym->st_name;
1375 put_elf_sym(s1->dynsym, offset, dynsym->st_size,
1376 dynsym->st_info, 0,
1377 bss_section->sh_num, dynname);
1378 break;
1383 put_elf_reloc(s1->dynsym, bss_section,
1384 offset, R_COPY, index);
1385 offset += esym->st_size;
1386 bss_section->data_offset = offset;
1388 } else {
1389 /* STB_WEAK undefined symbols are accepted */
1390 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1391 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1392 !strcmp(name, "_fp_hw")) {
1393 } else {
1394 tcc_error_noabort("undefined symbol '%s'", name);
1397 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1398 /* if -rdynamic option, then export all non local symbols */
1399 name = (char *) symtab_section->link->data + sym->st_name;
1400 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1401 0, sym->st_shndx, name);
1406 /* Bind symbols of libraries: export all non local symbols of executable that
1407 are referenced by shared libraries. The reason is that the dynamic loader
1408 search symbol first in executable and then in libraries. Therefore a
1409 reference to a symbol already defined by a library can still be resolved by
1410 a symbol in the executable. */
1411 static void bind_libs_dynsyms(TCCState *s1)
1413 const char *name;
1414 int sym_index;
1415 ElfW(Sym) *sym, *esym;
1417 for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
1418 name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
1419 sym_index = find_elf_sym(symtab_section, name);
1420 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1421 if (sym_index && sym->st_shndx != SHN_UNDEF
1422 && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1423 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1424 sym->st_info, 0, sym->st_shndx, name);
1425 } else if (esym->st_shndx == SHN_UNDEF) {
1426 /* weak symbols can stay undefined */
1427 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
1428 tcc_warning("undefined dynamic symbol '%s'", name);
1433 /* Export all non local symbols. This is used by shared libraries so that the
1434 non local symbols they define can resolve a reference in another shared
1435 library or in the executable. Correspondingly, it allows undefined local
1436 symbols to be resolved by other shared libraries or by the executable. */
1437 static void export_global_syms(TCCState *s1)
1439 int dynindex, index;
1440 const char *name;
1441 ElfW(Sym) *sym;
1443 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1444 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1445 name = (char *) symtab_section->link->data + sym->st_name;
1446 dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1447 sym->st_info, 0, sym->st_shndx, name);
1448 index = sym - (ElfW(Sym) *) symtab_section->data;
1449 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1454 /* Allocate strings for section names and decide if an unallocated section
1455 should be output.
1456 NOTE: the strsec section comes last, so its size is also correct ! */
1457 static int alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
1459 int i;
1460 Section *s;
1461 int textrel = 0;
1463 /* Allocate strings for section names */
1464 for(i = 1; i < s1->nb_sections; i++) {
1465 s = s1->sections[i];
1466 /* when generating a DLL, we include relocations but we may
1467 patch them */
1468 if (file_type == TCC_OUTPUT_DLL &&
1469 s->sh_type == SHT_RELX &&
1470 !(s->sh_flags & SHF_ALLOC) &&
1471 (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) &&
1472 prepare_dynamic_rel(s1, s)) {
1473 if (s1->sections[s->sh_info]->sh_flags & SHF_EXECINSTR)
1474 textrel = 1;
1475 } else if (s1->do_debug ||
1476 file_type == TCC_OUTPUT_OBJ ||
1477 (s->sh_flags & SHF_ALLOC) ||
1478 i == (s1->nb_sections - 1)) {
1479 /* we output all sections if debug or object file */
1480 s->sh_size = s->data_offset;
1482 if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1483 s->sh_name = put_elf_str(strsec, s->name);
1485 strsec->sh_size = strsec->data_offset;
1486 return textrel;
1489 /* Info to be copied in dynamic section */
1490 struct dyn_inf {
1491 Section *dynamic;
1492 Section *dynstr;
1493 unsigned long data_offset;
1494 addr_t rel_addr;
1495 addr_t rel_size;
1496 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1497 addr_t bss_addr;
1498 addr_t bss_size;
1499 #endif
1502 /* Assign sections to segments and decide how are sections laid out when loaded
1503 in memory. This function also fills corresponding program headers. */
1504 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1505 Section *interp, Section* strsec,
1506 struct dyn_inf *dyninf, int *sec_order)
1508 int i, j, k, file_type, sh_order_index, file_offset;
1509 unsigned long s_align;
1510 long long tmp;
1511 addr_t addr;
1512 ElfW(Phdr) *ph;
1513 Section *s;
1515 file_type = s1->output_type;
1516 sh_order_index = 1;
1517 file_offset = 0;
1518 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1519 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1520 s_align = ELF_PAGE_SIZE;
1521 if (s1->section_align)
1522 s_align = s1->section_align;
1524 if (phnum > 0) {
1525 if (s1->has_text_addr) {
1526 int a_offset, p_offset;
1527 addr = s1->text_addr;
1528 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1529 ELF_PAGE_SIZE */
1530 a_offset = (int) (addr & (s_align - 1));
1531 p_offset = file_offset & (s_align - 1);
1532 if (a_offset < p_offset)
1533 a_offset += s_align;
1534 file_offset += (a_offset - p_offset);
1535 } else {
1536 if (file_type == TCC_OUTPUT_DLL)
1537 addr = 0;
1538 else
1539 addr = ELF_START_ADDR;
1540 /* compute address after headers */
1541 addr += (file_offset & (s_align - 1));
1544 ph = &phdr[0];
1545 /* Leave one program headers for the program interpreter and one for
1546 the program header table itself if needed. These are done later as
1547 they require section layout to be done first. */
1548 if (interp)
1549 ph += 2;
1551 /* dynamic relocation table information, for .dynamic section */
1552 dyninf->rel_addr = dyninf->rel_size = 0;
1553 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1554 dyninf->bss_addr = dyninf->bss_size = 0;
1555 #endif
1557 for(j = 0; j < 2; j++) {
1558 ph->p_type = PT_LOAD;
1559 if (j == 0)
1560 ph->p_flags = PF_R | PF_X;
1561 else
1562 ph->p_flags = PF_R | PF_W;
1563 ph->p_align = s_align;
1565 /* Decide the layout of sections loaded in memory. This must
1566 be done before program headers are filled since they contain
1567 info about the layout. We do the following ordering: interp,
1568 symbol tables, relocations, progbits, nobits */
1569 /* XXX: do faster and simpler sorting */
1570 for(k = 0; k < 5; k++) {
1571 for(i = 1; i < s1->nb_sections; i++) {
1572 s = s1->sections[i];
1573 /* compute if section should be included */
1574 if (j == 0) {
1575 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1576 SHF_ALLOC)
1577 continue;
1578 } else {
1579 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1580 (SHF_ALLOC | SHF_WRITE))
1581 continue;
1583 if (s == interp) {
1584 if (k != 0)
1585 continue;
1586 } else if (s->sh_type == SHT_DYNSYM ||
1587 s->sh_type == SHT_STRTAB ||
1588 s->sh_type == SHT_HASH) {
1589 if (k != 1)
1590 continue;
1591 } else if (s->sh_type == SHT_RELX) {
1592 if (k != 2)
1593 continue;
1594 } else if (s->sh_type == SHT_NOBITS) {
1595 if (k != 4)
1596 continue;
1597 } else {
1598 if (k != 3)
1599 continue;
1601 sec_order[sh_order_index++] = i;
1603 /* section matches: we align it and add its size */
1604 tmp = addr;
1605 addr = (addr + s->sh_addralign - 1) &
1606 ~(s->sh_addralign - 1);
1607 file_offset += (int) ( addr - tmp );
1608 s->sh_offset = file_offset;
1609 s->sh_addr = addr;
1611 /* update program header infos */
1612 if (ph->p_offset == 0) {
1613 ph->p_offset = file_offset;
1614 ph->p_vaddr = addr;
1615 ph->p_paddr = ph->p_vaddr;
1617 /* update dynamic relocation infos */
1618 if (s->sh_type == SHT_RELX) {
1619 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1620 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1621 dyninf->rel_addr = addr;
1622 dyninf->rel_size += s->sh_size; /* XXX only first rel. */
1624 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1625 dyninf->bss_addr = addr;
1626 dyninf->bss_size = s->sh_size; /* XXX only first rel. */
1628 #else
1629 if (dyninf->rel_size == 0)
1630 dyninf->rel_addr = addr;
1631 dyninf->rel_size += s->sh_size;
1632 #endif
1634 addr += s->sh_size;
1635 if (s->sh_type != SHT_NOBITS)
1636 file_offset += s->sh_size;
1639 if (j == 0) {
1640 /* Make the first PT_LOAD segment include the program
1641 headers itself (and the ELF header as well), it'll
1642 come out with same memory use but will make various
1643 tools like binutils strip work better. */
1644 ph->p_offset &= ~(ph->p_align - 1);
1645 ph->p_vaddr &= ~(ph->p_align - 1);
1646 ph->p_paddr &= ~(ph->p_align - 1);
1648 ph->p_filesz = file_offset - ph->p_offset;
1649 ph->p_memsz = addr - ph->p_vaddr;
1650 ph++;
1651 if (j == 0) {
1652 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1653 /* if in the middle of a page, we duplicate the page in
1654 memory so that one copy is RX and the other is RW */
1655 if ((addr & (s_align - 1)) != 0)
1656 addr += s_align;
1657 } else {
1658 addr = (addr + s_align - 1) & ~(s_align - 1);
1659 file_offset = (file_offset + s_align - 1) & ~(s_align - 1);
1665 /* all other sections come after */
1666 for(i = 1; i < s1->nb_sections; i++) {
1667 s = s1->sections[i];
1668 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1669 continue;
1670 sec_order[sh_order_index++] = i;
1672 file_offset = (file_offset + s->sh_addralign - 1) &
1673 ~(s->sh_addralign - 1);
1674 s->sh_offset = file_offset;
1675 if (s->sh_type != SHT_NOBITS)
1676 file_offset += s->sh_size;
1679 return file_offset;
1682 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
1683 Section *dynamic)
1685 ElfW(Phdr) *ph;
1687 /* if interpreter, then add corresponding program header */
1688 if (interp) {
1689 ph = &phdr[0];
1691 ph->p_type = PT_PHDR;
1692 ph->p_offset = sizeof(ElfW(Ehdr));
1693 ph->p_filesz = ph->p_memsz = phnum * sizeof(ElfW(Phdr));
1694 ph->p_vaddr = interp->sh_addr - ph->p_filesz;
1695 ph->p_paddr = ph->p_vaddr;
1696 ph->p_flags = PF_R | PF_X;
1697 ph->p_align = 4; /* interp->sh_addralign; */
1698 ph++;
1700 ph->p_type = PT_INTERP;
1701 ph->p_offset = interp->sh_offset;
1702 ph->p_vaddr = interp->sh_addr;
1703 ph->p_paddr = ph->p_vaddr;
1704 ph->p_filesz = interp->sh_size;
1705 ph->p_memsz = interp->sh_size;
1706 ph->p_flags = PF_R;
1707 ph->p_align = interp->sh_addralign;
1710 /* if dynamic section, then add corresponding program header */
1711 if (dynamic) {
1712 ph = &phdr[phnum - 1];
1714 ph->p_type = PT_DYNAMIC;
1715 ph->p_offset = dynamic->sh_offset;
1716 ph->p_vaddr = dynamic->sh_addr;
1717 ph->p_paddr = ph->p_vaddr;
1718 ph->p_filesz = dynamic->sh_size;
1719 ph->p_memsz = dynamic->sh_size;
1720 ph->p_flags = PF_R | PF_W;
1721 ph->p_align = dynamic->sh_addralign;
1725 /* Fill the dynamic section with tags describing the address and size of
1726 sections */
1727 static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf)
1729 Section *dynamic = dyninf->dynamic;
1731 /* put dynamic section entries */
1732 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1733 put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
1734 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1735 put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
1736 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1737 #if PTR_SIZE == 8
1738 put_dt(dynamic, DT_RELA, dyninf->rel_addr);
1739 put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
1740 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1741 #else
1742 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1743 put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
1744 put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size);
1745 put_dt(dynamic, DT_JMPREL, dyninf->rel_addr);
1746 put_dt(dynamic, DT_PLTREL, DT_REL);
1747 put_dt(dynamic, DT_REL, dyninf->bss_addr);
1748 put_dt(dynamic, DT_RELSZ, dyninf->bss_size);
1749 #else
1750 put_dt(dynamic, DT_REL, dyninf->rel_addr);
1751 put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
1752 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1753 #endif
1754 #endif
1755 if (s1->do_debug)
1756 put_dt(dynamic, DT_DEBUG, 0);
1757 put_dt(dynamic, DT_NULL, 0);
1760 /* Relocate remaining sections and symbols (that is those not related to
1761 dynamic linking) */
1762 static int final_sections_reloc(TCCState *s1)
1764 int i;
1765 Section *s;
1767 relocate_syms(s1, s1->symtab, 0);
1769 if (s1->nb_errors != 0)
1770 return -1;
1772 /* relocate sections */
1773 /* XXX: ignore sections with allocated relocations ? */
1774 for(i = 1; i < s1->nb_sections; i++) {
1775 s = s1->sections[i];
1776 #if defined(TCC_TARGET_I386) || defined(TCC_MUSL)
1777 if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
1778 /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC
1779 checking is removed */
1780 #else
1781 if (s->reloc && s != s1->got)
1782 /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */
1783 #endif
1784 relocate_section(s1, s);
1787 /* relocate relocation entries if the relocation tables are
1788 allocated in the executable */
1789 for(i = 1; i < s1->nb_sections; i++) {
1790 s = s1->sections[i];
1791 if ((s->sh_flags & SHF_ALLOC) &&
1792 s->sh_type == SHT_RELX) {
1793 relocate_rel(s1, s);
1796 return 0;
1799 /* Create an ELF file on disk.
1800 This function handle ELF specific layout requirements */
1801 static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
1802 int file_offset, int *sec_order)
1804 int i, shnum, offset, size, file_type;
1805 Section *s;
1806 ElfW(Ehdr) ehdr;
1807 ElfW(Shdr) shdr, *sh;
1809 file_type = s1->output_type;
1810 shnum = s1->nb_sections;
1812 memset(&ehdr, 0, sizeof(ehdr));
1814 if (phnum > 0) {
1815 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1816 ehdr.e_phnum = phnum;
1817 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1820 /* align to 4 */
1821 file_offset = (file_offset + 3) & -4;
1823 /* fill header */
1824 ehdr.e_ident[0] = ELFMAG0;
1825 ehdr.e_ident[1] = ELFMAG1;
1826 ehdr.e_ident[2] = ELFMAG2;
1827 ehdr.e_ident[3] = ELFMAG3;
1828 ehdr.e_ident[4] = ELFCLASSW;
1829 ehdr.e_ident[5] = ELFDATA2LSB;
1830 ehdr.e_ident[6] = EV_CURRENT;
1831 #if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
1832 /* FIXME: should set only for freebsd _target_, but we exclude only PE target */
1833 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1834 #endif
1835 #ifdef TCC_TARGET_ARM
1836 #ifdef TCC_ARM_EABI
1837 ehdr.e_ident[EI_OSABI] = 0;
1838 ehdr.e_flags = EF_ARM_EABI_VER4;
1839 if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
1840 ehdr.e_flags |= EF_ARM_HASENTRY;
1841 if (s1->float_abi == ARM_HARD_FLOAT)
1842 ehdr.e_flags |= EF_ARM_VFP_FLOAT;
1843 else
1844 ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
1845 #else
1846 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1847 #endif
1848 #endif
1849 switch(file_type) {
1850 default:
1851 case TCC_OUTPUT_EXE:
1852 ehdr.e_type = ET_EXEC;
1853 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
1854 break;
1855 case TCC_OUTPUT_DLL:
1856 ehdr.e_type = ET_DYN;
1857 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1858 break;
1859 case TCC_OUTPUT_OBJ:
1860 ehdr.e_type = ET_REL;
1861 break;
1863 ehdr.e_machine = EM_TCC_TARGET;
1864 ehdr.e_version = EV_CURRENT;
1865 ehdr.e_shoff = file_offset;
1866 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1867 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1868 ehdr.e_shnum = shnum;
1869 ehdr.e_shstrndx = shnum - 1;
1871 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1872 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1873 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1875 sort_syms(s1, symtab_section);
1876 for(i = 1; i < s1->nb_sections; i++) {
1877 s = s1->sections[sec_order[i]];
1878 if (s->sh_type != SHT_NOBITS) {
1879 while (offset < s->sh_offset) {
1880 fputc(0, f);
1881 offset++;
1883 size = s->sh_size;
1884 if (size)
1885 fwrite(s->data, 1, size, f);
1886 offset += size;
1890 /* output section headers */
1891 while (offset < ehdr.e_shoff) {
1892 fputc(0, f);
1893 offset++;
1896 for(i = 0; i < s1->nb_sections; i++) {
1897 sh = &shdr;
1898 memset(sh, 0, sizeof(ElfW(Shdr)));
1899 s = s1->sections[i];
1900 if (s) {
1901 sh->sh_name = s->sh_name;
1902 sh->sh_type = s->sh_type;
1903 sh->sh_flags = s->sh_flags;
1904 sh->sh_entsize = s->sh_entsize;
1905 sh->sh_info = s->sh_info;
1906 if (s->link)
1907 sh->sh_link = s->link->sh_num;
1908 sh->sh_addralign = s->sh_addralign;
1909 sh->sh_addr = s->sh_addr;
1910 sh->sh_offset = s->sh_offset;
1911 sh->sh_size = s->sh_size;
1913 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1917 /* Write an elf, coff or "binary" file */
1918 static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
1919 ElfW(Phdr) *phdr, int file_offset, int *sec_order)
1921 int fd, mode, file_type;
1922 FILE *f;
1924 file_type = s1->output_type;
1925 if (file_type == TCC_OUTPUT_OBJ)
1926 mode = 0666;
1927 else
1928 mode = 0777;
1929 unlink(filename);
1930 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1931 if (fd < 0) {
1932 tcc_error_noabort("could not write '%s'", filename);
1933 return -1;
1935 f = fdopen(fd, "wb");
1936 if (s1->verbose)
1937 printf("<- %s\n", filename);
1939 #ifdef TCC_TARGET_COFF
1940 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
1941 tcc_output_coff(s1, f);
1942 else
1943 #endif
1944 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1945 tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
1946 else
1947 tcc_output_binary(s1, f, sec_order);
1948 fclose(f);
1950 return 0;
1953 /* Sort section headers by assigned sh_addr, remove sections
1954 that we aren't going to output. */
1955 static void tidy_section_headers(TCCState *s1, int *sec_order)
1957 int i, nnew, l, *backmap;
1958 Section **snew, *s;
1959 ElfW(Sym) *sym;
1961 snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
1962 backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
1963 for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
1964 s = s1->sections[sec_order[i]];
1965 if (!i || s->sh_name) {
1966 backmap[sec_order[i]] = nnew;
1967 snew[nnew] = s;
1968 ++nnew;
1969 } else {
1970 backmap[sec_order[i]] = 0;
1971 snew[--l] = s;
1974 for (i = 0; i < nnew; i++) {
1975 s = snew[i];
1976 if (s) {
1977 s->sh_num = i;
1978 if (s->sh_type == SHT_RELX)
1979 s->sh_info = backmap[s->sh_info];
1983 for_each_elem(symtab_section, 1, sym, ElfW(Sym))
1984 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1985 sym->st_shndx = backmap[sym->st_shndx];
1986 if( !s1->static_link ) {
1987 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
1988 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1989 sym->st_shndx = backmap[sym->st_shndx];
1991 for (i = 0; i < s1->nb_sections; i++)
1992 sec_order[i] = i;
1993 tcc_free(s1->sections);
1994 s1->sections = snew;
1995 s1->nb_sections = nnew;
1996 tcc_free(backmap);
1999 /* Output an elf, coff or binary file */
2000 /* XXX: suppress unneeded sections */
2001 static int elf_output_file(TCCState *s1, const char *filename)
2003 int i, ret, phnum, shnum, file_type, file_offset, *sec_order;
2004 struct dyn_inf dyninf = {0};
2005 ElfW(Phdr) *phdr;
2006 ElfW(Sym) *sym;
2007 Section *strsec, *interp, *dynamic, *dynstr;
2008 int textrel;
2010 file_type = s1->output_type;
2011 s1->nb_errors = 0;
2012 ret = -1;
2013 phdr = NULL;
2014 sec_order = NULL;
2015 interp = dynamic = dynstr = NULL; /* avoid warning */
2016 textrel = 0;
2018 if (file_type != TCC_OUTPUT_OBJ) {
2019 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2020 tcc_add_runtime(s1);
2021 relocate_common_syms();
2022 tcc_add_linker_symbols(s1);
2024 if (!s1->static_link) {
2025 if (file_type == TCC_OUTPUT_EXE) {
2026 char *ptr;
2027 /* allow override the dynamic loader */
2028 const char *elfint = getenv("LD_SO");
2029 if (elfint == NULL)
2030 elfint = DEFAULT_ELFINTERP(s1);
2031 /* add interpreter section only if executable */
2032 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
2033 interp->sh_addralign = 1;
2034 ptr = section_ptr_add(interp, 1 + strlen(elfint));
2035 strcpy(ptr, elfint);
2038 /* add dynamic symbol table */
2039 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
2040 ".dynstr",
2041 ".hash", SHF_ALLOC);
2042 dynstr = s1->dynsym->link;
2044 /* add dynamic section */
2045 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
2046 SHF_ALLOC | SHF_WRITE);
2047 dynamic->link = dynstr;
2048 dynamic->sh_entsize = sizeof(ElfW(Dyn));
2050 build_got(s1);
2052 if (file_type == TCC_OUTPUT_EXE) {
2053 bind_exe_dynsyms(s1);
2054 if (s1->nb_errors)
2055 goto the_end;
2056 bind_libs_dynsyms(s1);
2057 } else {
2058 /* shared library case: simply export all global symbols */
2059 export_global_syms(s1);
2062 build_got_entries(s1);
2065 /* we add a section for symbols */
2066 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
2067 put_elf_str(strsec, "");
2069 /* Allocate strings for section names */
2070 textrel = alloc_sec_names(s1, file_type, strsec);
2072 if (dynamic) {
2073 /* add a list of needed dlls */
2074 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2075 DLLReference *dllref = s1->loaded_dlls[i];
2076 if (dllref->level == 0)
2077 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
2080 if (s1->rpath)
2081 put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
2082 put_elf_str(dynstr, s1->rpath));
2084 if (file_type == TCC_OUTPUT_DLL) {
2085 if (s1->soname)
2086 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
2087 /* XXX: currently, since we do not handle PIC code, we
2088 must relocate the readonly segments */
2089 if (textrel)
2090 put_dt(dynamic, DT_TEXTREL, 0);
2093 if (s1->symbolic)
2094 put_dt(dynamic, DT_SYMBOLIC, 0);
2096 dyninf.dynamic = dynamic;
2097 dyninf.dynstr = dynstr;
2098 /* remember offset and reserve space for 2nd call below */
2099 dyninf.data_offset = dynamic->data_offset;
2100 fill_dynamic(s1, &dyninf);
2101 dynamic->sh_size = dynamic->data_offset;
2102 dynstr->sh_size = dynstr->data_offset;
2105 /* compute number of program headers */
2106 if (file_type == TCC_OUTPUT_OBJ)
2107 phnum = 0;
2108 else if (file_type == TCC_OUTPUT_DLL)
2109 phnum = 3;
2110 else if (s1->static_link)
2111 phnum = 2;
2112 else
2113 phnum = 5;
2115 /* allocate program segment headers */
2116 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
2118 /* compute number of sections */
2119 shnum = s1->nb_sections;
2121 /* this array is used to reorder sections in the output file */
2122 sec_order = tcc_malloc(sizeof(int) * shnum);
2123 sec_order[0] = 0;
2125 /* compute section to program header mapping */
2126 file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2127 sec_order);
2129 /* Fill remaining program header and finalize relocation related to dynamic
2130 linking. */
2131 if (file_type != TCC_OUTPUT_OBJ) {
2132 fill_unloadable_phdr(phdr, phnum, interp, dynamic);
2133 if (dynamic) {
2134 dynamic->data_offset = dyninf.data_offset;
2135 fill_dynamic(s1, &dyninf);
2137 /* put in GOT the dynamic section address and relocate PLT */
2138 write32le(s1->got->data, dynamic->sh_addr);
2139 if (file_type == TCC_OUTPUT_EXE
2140 || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
2141 relocate_plt(s1);
2143 /* relocate symbols in .dynsym now that final addresses are known */
2144 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
2145 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
2146 /* do symbol relocation */
2147 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
2152 /* if building executable or DLL, then relocate each section
2153 except the GOT which is already relocated */
2154 ret = final_sections_reloc(s1);
2155 if (ret)
2156 goto the_end;
2157 tidy_section_headers(s1, sec_order);
2159 /* Perform relocation to GOT or PLT entries */
2160 if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2161 fill_got(s1);
2162 else if (s1->got)
2163 fill_local_got_entries(s1);
2166 /* Create the ELF file with name 'filename' */
2167 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2168 s1->nb_sections = shnum;
2169 the_end:
2170 tcc_free(sec_order);
2171 tcc_free(phdr);
2172 return ret;
2175 LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
2177 int ret;
2178 #ifdef TCC_TARGET_PE
2179 if (s->output_type != TCC_OUTPUT_OBJ) {
2180 ret = pe_output_file(s, filename);
2181 } else
2182 #endif
2183 ret = elf_output_file(s, filename);
2184 return ret;
2187 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2189 void *data;
2191 data = tcc_malloc(size);
2192 lseek(fd, file_offset, SEEK_SET);
2193 read(fd, data, size);
2194 return data;
2197 typedef struct SectionMergeInfo {
2198 Section *s; /* corresponding existing section */
2199 unsigned long offset; /* offset of the new section in the existing section */
2200 uint8_t new_section; /* true if section 's' was added */
2201 uint8_t link_once; /* true if link once section */
2202 } SectionMergeInfo;
2204 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
2206 int size = read(fd, h, sizeof *h);
2207 if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
2208 if (h->e_type == ET_REL)
2209 return AFF_BINTYPE_REL;
2210 if (h->e_type == ET_DYN)
2211 return AFF_BINTYPE_DYN;
2212 } else if (size >= 8) {
2213 if (0 == memcmp(h, ARMAG, 8))
2214 return AFF_BINTYPE_AR;
2215 #ifdef TCC_TARGET_COFF
2216 if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC)
2217 return AFF_BINTYPE_C67;
2218 #endif
2220 return 0;
2223 /* load an object file and merge it with current files */
2224 /* XXX: handle correctly stab (debug) info */
2225 ST_FUNC int tcc_load_object_file(TCCState *s1,
2226 int fd, unsigned long file_offset)
2228 ElfW(Ehdr) ehdr;
2229 ElfW(Shdr) *shdr, *sh;
2230 int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed;
2231 unsigned char *strsec, *strtab;
2232 int *old_to_new_syms;
2233 char *sh_name, *name;
2234 SectionMergeInfo *sm_table, *sm;
2235 ElfW(Sym) *sym, *symtab;
2236 ElfW_Rel *rel;
2237 Section *s;
2239 int stab_index;
2240 int stabstr_index;
2242 stab_index = stabstr_index = 0;
2244 lseek(fd, file_offset, SEEK_SET);
2245 if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL)
2246 goto fail1;
2247 /* test CPU specific stuff */
2248 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2249 ehdr.e_machine != EM_TCC_TARGET) {
2250 fail1:
2251 tcc_error_noabort("invalid object file");
2252 return -1;
2254 /* read sections */
2255 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2256 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2257 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2259 /* load section names */
2260 sh = &shdr[ehdr.e_shstrndx];
2261 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2263 /* load symtab and strtab */
2264 old_to_new_syms = NULL;
2265 symtab = NULL;
2266 strtab = NULL;
2267 nb_syms = 0;
2268 seencompressed = 0;
2269 for(i = 1; i < ehdr.e_shnum; i++) {
2270 sh = &shdr[i];
2271 if (sh->sh_type == SHT_SYMTAB) {
2272 if (symtab) {
2273 tcc_error_noabort("object must contain only one symtab");
2274 fail:
2275 ret = -1;
2276 goto the_end;
2278 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2279 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2280 sm_table[i].s = symtab_section;
2282 /* now load strtab */
2283 sh = &shdr[sh->sh_link];
2284 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2286 if (sh->sh_flags & SHF_COMPRESSED)
2287 seencompressed = 1;
2290 /* now examine each section and try to merge its content with the
2291 ones in memory */
2292 for(i = 1; i < ehdr.e_shnum; i++) {
2293 /* no need to examine section name strtab */
2294 if (i == ehdr.e_shstrndx)
2295 continue;
2296 sh = &shdr[i];
2297 sh_name = (char *) strsec + sh->sh_name;
2298 /* ignore sections types we do not handle */
2299 if (sh->sh_type != SHT_PROGBITS &&
2300 sh->sh_type != SHT_RELX &&
2301 #ifdef TCC_ARM_EABI
2302 sh->sh_type != SHT_ARM_EXIDX &&
2303 #endif
2304 sh->sh_type != SHT_NOBITS &&
2305 sh->sh_type != SHT_PREINIT_ARRAY &&
2306 sh->sh_type != SHT_INIT_ARRAY &&
2307 sh->sh_type != SHT_FINI_ARRAY &&
2308 strcmp(sh_name, ".stabstr")
2310 continue;
2311 if (seencompressed
2312 && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1)
2313 || (sh->sh_type == SHT_RELX
2314 && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name,
2315 ".debug_", sizeof(".debug_")-1))))
2316 continue;
2317 if (sh->sh_addralign < 1)
2318 sh->sh_addralign = 1;
2319 /* find corresponding section, if any */
2320 for(j = 1; j < s1->nb_sections;j++) {
2321 s = s1->sections[j];
2322 if (!strcmp(s->name, sh_name)) {
2323 if (!strncmp(sh_name, ".gnu.linkonce",
2324 sizeof(".gnu.linkonce") - 1)) {
2325 /* if a 'linkonce' section is already present, we
2326 do not add it again. It is a little tricky as
2327 symbols can still be defined in
2328 it. */
2329 sm_table[i].link_once = 1;
2330 goto next;
2331 } else {
2332 goto found;
2336 /* not found: create new section */
2337 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
2338 /* take as much info as possible from the section. sh_link and
2339 sh_info will be updated later */
2340 s->sh_addralign = sh->sh_addralign;
2341 s->sh_entsize = sh->sh_entsize;
2342 sm_table[i].new_section = 1;
2343 found:
2344 if (sh->sh_type != s->sh_type) {
2345 tcc_error_noabort("invalid section type");
2346 goto fail;
2349 /* align start of section */
2350 offset = s->data_offset;
2352 if (0 == strcmp(sh_name, ".stab")) {
2353 stab_index = i;
2354 goto no_align;
2356 if (0 == strcmp(sh_name, ".stabstr")) {
2357 stabstr_index = i;
2358 goto no_align;
2361 size = sh->sh_addralign - 1;
2362 offset = (offset + size) & ~size;
2363 if (sh->sh_addralign > s->sh_addralign)
2364 s->sh_addralign = sh->sh_addralign;
2365 s->data_offset = offset;
2366 no_align:
2367 sm_table[i].offset = offset;
2368 sm_table[i].s = s;
2369 /* concatenate sections */
2370 size = sh->sh_size;
2371 if (sh->sh_type != SHT_NOBITS) {
2372 unsigned char *ptr;
2373 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2374 ptr = section_ptr_add(s, size);
2375 read(fd, ptr, size);
2376 } else {
2377 s->data_offset += size;
2379 next: ;
2382 /* gr relocate stab strings */
2383 if (stab_index && stabstr_index) {
2384 Stab_Sym *a, *b;
2385 unsigned o;
2386 s = sm_table[stab_index].s;
2387 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2388 b = (Stab_Sym *)(s->data + s->data_offset);
2389 o = sm_table[stabstr_index].offset;
2390 while (a < b)
2391 a->n_strx += o, a++;
2394 /* second short pass to update sh_link and sh_info fields of new
2395 sections */
2396 for(i = 1; i < ehdr.e_shnum; i++) {
2397 s = sm_table[i].s;
2398 if (!s || !sm_table[i].new_section)
2399 continue;
2400 sh = &shdr[i];
2401 if (sh->sh_link > 0)
2402 s->link = sm_table[sh->sh_link].s;
2403 if (sh->sh_type == SHT_RELX) {
2404 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2405 /* update backward link */
2406 s1->sections[s->sh_info]->reloc = s;
2409 sm = sm_table;
2411 /* resolve symbols */
2412 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2414 sym = symtab + 1;
2415 for(i = 1; i < nb_syms; i++, sym++) {
2416 if (sym->st_shndx != SHN_UNDEF &&
2417 sym->st_shndx < SHN_LORESERVE) {
2418 sm = &sm_table[sym->st_shndx];
2419 if (sm->link_once) {
2420 /* if a symbol is in a link once section, we use the
2421 already defined symbol. It is very important to get
2422 correct relocations */
2423 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2424 name = (char *) strtab + sym->st_name;
2425 sym_index = find_elf_sym(symtab_section, name);
2426 if (sym_index)
2427 old_to_new_syms[i] = sym_index;
2429 continue;
2431 /* if no corresponding section added, no need to add symbol */
2432 if (!sm->s)
2433 continue;
2434 /* convert section number */
2435 sym->st_shndx = sm->s->sh_num;
2436 /* offset value */
2437 sym->st_value += sm->offset;
2439 /* add symbol */
2440 name = (char *) strtab + sym->st_name;
2441 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
2442 sym->st_info, sym->st_other,
2443 sym->st_shndx, name);
2444 old_to_new_syms[i] = sym_index;
2447 /* third pass to patch relocation entries */
2448 for(i = 1; i < ehdr.e_shnum; i++) {
2449 s = sm_table[i].s;
2450 if (!s)
2451 continue;
2452 sh = &shdr[i];
2453 offset = sm_table[i].offset;
2454 switch(s->sh_type) {
2455 case SHT_RELX:
2456 /* take relocation offset information */
2457 offseti = sm_table[sh->sh_info].offset;
2458 for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) {
2459 int type;
2460 unsigned sym_index;
2461 /* convert symbol index */
2462 type = ELFW(R_TYPE)(rel->r_info);
2463 sym_index = ELFW(R_SYM)(rel->r_info);
2464 /* NOTE: only one symtab assumed */
2465 if (sym_index >= nb_syms)
2466 goto invalid_reloc;
2467 sym_index = old_to_new_syms[sym_index];
2468 /* ignore link_once in rel section. */
2469 if (!sym_index && !sm->link_once
2470 #ifdef TCC_TARGET_ARM
2471 && type != R_ARM_V4BX
2472 #endif
2474 invalid_reloc:
2475 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2476 i, strsec + sh->sh_name, rel->r_offset);
2477 goto fail;
2479 rel->r_info = ELFW(R_INFO)(sym_index, type);
2480 /* offset the relocation offset */
2481 rel->r_offset += offseti;
2482 #ifdef TCC_TARGET_ARM
2483 /* Jumps and branches from a Thumb code to a PLT entry need
2484 special handling since PLT entries are ARM code.
2485 Unconditional bl instructions referencing PLT entries are
2486 handled by converting these instructions into blx
2487 instructions. Other case of instructions referencing a PLT
2488 entry require to add a Thumb stub before the PLT entry to
2489 switch to ARM mode. We set bit plt_thumb_stub of the
2490 attribute of a symbol to indicate such a case. */
2491 if (type == R_ARM_THM_JUMP24)
2492 get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
2493 #endif
2495 break;
2496 default:
2497 break;
2501 ret = 0;
2502 the_end:
2503 tcc_free(symtab);
2504 tcc_free(strtab);
2505 tcc_free(old_to_new_syms);
2506 tcc_free(sm_table);
2507 tcc_free(strsec);
2508 tcc_free(shdr);
2509 return ret;
2512 typedef struct ArchiveHeader {
2513 char ar_name[16]; /* name of this member */
2514 char ar_date[12]; /* file mtime */
2515 char ar_uid[6]; /* owner uid; printed as decimal */
2516 char ar_gid[6]; /* owner gid; printed as decimal */
2517 char ar_mode[8]; /* file mode, printed as octal */
2518 char ar_size[10]; /* file size, printed as decimal */
2519 char ar_fmag[2]; /* should contain ARFMAG */
2520 } ArchiveHeader;
2522 static int get_be32(const uint8_t *b)
2524 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2527 static long get_be64(const uint8_t *b)
2529 long long ret = get_be32(b);
2530 ret = (ret << 32) | (unsigned)get_be32(b+4);
2531 return (long)ret;
2534 /* load only the objects which resolve undefined symbols */
2535 static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
2537 long i, bound, nsyms, sym_index, off, ret;
2538 uint8_t *data;
2539 const char *ar_names, *p;
2540 const uint8_t *ar_index;
2541 ElfW(Sym) *sym;
2543 data = tcc_malloc(size);
2544 if (read(fd, data, size) != size)
2545 goto fail;
2546 nsyms = entrysize == 4 ? get_be32(data) : get_be64(data);
2547 ar_index = data + entrysize;
2548 ar_names = (char *) ar_index + nsyms * entrysize;
2550 do {
2551 bound = 0;
2552 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2553 sym_index = find_elf_sym(symtab_section, p);
2554 if(sym_index) {
2555 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2556 if(sym->st_shndx == SHN_UNDEF) {
2557 off = (entrysize == 4
2558 ? get_be32(ar_index + i * 4)
2559 : get_be64(ar_index + i * 8))
2560 + sizeof(ArchiveHeader);
2561 ++bound;
2562 if(tcc_load_object_file(s1, fd, off) < 0) {
2563 fail:
2564 ret = -1;
2565 goto the_end;
2570 } while(bound);
2571 ret = 0;
2572 the_end:
2573 tcc_free(data);
2574 return ret;
2577 /* load a '.a' file */
2578 ST_FUNC int tcc_load_archive(TCCState *s1, int fd)
2580 ArchiveHeader hdr;
2581 char ar_size[11];
2582 char ar_name[17];
2583 char magic[8];
2584 int size, len, i;
2585 unsigned long file_offset;
2587 /* skip magic which was already checked */
2588 read(fd, magic, sizeof(magic));
2590 for(;;) {
2591 len = read(fd, &hdr, sizeof(hdr));
2592 if (len == 0)
2593 break;
2594 if (len != sizeof(hdr)) {
2595 tcc_error_noabort("invalid archive");
2596 return -1;
2598 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2599 ar_size[sizeof(hdr.ar_size)] = '\0';
2600 size = strtol(ar_size, NULL, 0);
2601 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2602 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2603 if (ar_name[i] != ' ')
2604 break;
2606 ar_name[i + 1] = '\0';
2607 file_offset = lseek(fd, 0, SEEK_CUR);
2608 /* align to even */
2609 size = (size + 1) & ~1;
2610 if (!strcmp(ar_name, "/")) {
2611 /* coff symbol table : we handle it */
2612 if(s1->alacarte_link)
2613 return tcc_load_alacarte(s1, fd, size, 4);
2614 } else if (!strcmp(ar_name, "/SYM64/")) {
2615 if(s1->alacarte_link)
2616 return tcc_load_alacarte(s1, fd, size, 8);
2617 } else {
2618 ElfW(Ehdr) ehdr;
2619 if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
2620 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2621 return -1;
2624 lseek(fd, file_offset + size, SEEK_SET);
2626 return 0;
2629 #ifndef TCC_TARGET_PE
2630 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2631 is referenced by the user (so it should be added as DT_NEEDED in
2632 the generated ELF file) */
2633 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2635 ElfW(Ehdr) ehdr;
2636 ElfW(Shdr) *shdr, *sh, *sh1;
2637 int i, j, nb_syms, nb_dts, sym_bind, ret;
2638 ElfW(Sym) *sym, *dynsym;
2639 ElfW(Dyn) *dt, *dynamic;
2640 unsigned char *dynstr;
2641 const char *name, *soname;
2642 DLLReference *dllref;
2644 read(fd, &ehdr, sizeof(ehdr));
2646 /* test CPU specific stuff */
2647 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2648 ehdr.e_machine != EM_TCC_TARGET) {
2649 tcc_error_noabort("bad architecture");
2650 return -1;
2653 /* read sections */
2654 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2656 /* load dynamic section and dynamic symbols */
2657 nb_syms = 0;
2658 nb_dts = 0;
2659 dynamic = NULL;
2660 dynsym = NULL; /* avoid warning */
2661 dynstr = NULL; /* avoid warning */
2662 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2663 switch(sh->sh_type) {
2664 case SHT_DYNAMIC:
2665 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2666 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2667 break;
2668 case SHT_DYNSYM:
2669 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2670 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2671 sh1 = &shdr[sh->sh_link];
2672 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2673 break;
2674 default:
2675 break;
2679 /* compute the real library name */
2680 soname = tcc_basename(filename);
2682 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2683 if (dt->d_tag == DT_SONAME) {
2684 soname = (char *) dynstr + dt->d_un.d_val;
2688 /* if the dll is already loaded, do not load it */
2689 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2690 dllref = s1->loaded_dlls[i];
2691 if (!strcmp(soname, dllref->name)) {
2692 /* but update level if needed */
2693 if (level < dllref->level)
2694 dllref->level = level;
2695 ret = 0;
2696 goto the_end;
2700 /* add the dll and its level */
2701 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2702 dllref->level = level;
2703 strcpy(dllref->name, soname);
2704 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2706 /* add dynamic symbols in dynsym_section */
2707 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2708 sym_bind = ELFW(ST_BIND)(sym->st_info);
2709 if (sym_bind == STB_LOCAL)
2710 continue;
2711 name = (char *) dynstr + sym->st_name;
2712 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2713 sym->st_info, sym->st_other, sym->st_shndx, name);
2716 /* load all referenced DLLs */
2717 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2718 switch(dt->d_tag) {
2719 case DT_NEEDED:
2720 name = (char *) dynstr + dt->d_un.d_val;
2721 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2722 dllref = s1->loaded_dlls[j];
2723 if (!strcmp(name, dllref->name))
2724 goto already_loaded;
2726 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2727 tcc_error_noabort("referenced dll '%s' not found", name);
2728 ret = -1;
2729 goto the_end;
2731 already_loaded:
2732 break;
2735 ret = 0;
2736 the_end:
2737 tcc_free(dynstr);
2738 tcc_free(dynsym);
2739 tcc_free(dynamic);
2740 tcc_free(shdr);
2741 return ret;
2744 #define LD_TOK_NAME 256
2745 #define LD_TOK_EOF (-1)
2747 /* return next ld script token */
2748 static int ld_next(TCCState *s1, char *name, int name_size)
2750 int c;
2751 char *q;
2753 redo:
2754 switch(ch) {
2755 case ' ':
2756 case '\t':
2757 case '\f':
2758 case '\v':
2759 case '\r':
2760 case '\n':
2761 inp();
2762 goto redo;
2763 case '/':
2764 minp();
2765 if (ch == '*') {
2766 file->buf_ptr = parse_comment(file->buf_ptr);
2767 ch = file->buf_ptr[0];
2768 goto redo;
2769 } else {
2770 q = name;
2771 *q++ = '/';
2772 goto parse_name;
2774 break;
2775 case '\\':
2776 ch = handle_eob();
2777 if (ch != '\\')
2778 goto redo;
2779 /* fall through */
2780 /* case 'a' ... 'z': */
2781 case 'a':
2782 case 'b':
2783 case 'c':
2784 case 'd':
2785 case 'e':
2786 case 'f':
2787 case 'g':
2788 case 'h':
2789 case 'i':
2790 case 'j':
2791 case 'k':
2792 case 'l':
2793 case 'm':
2794 case 'n':
2795 case 'o':
2796 case 'p':
2797 case 'q':
2798 case 'r':
2799 case 's':
2800 case 't':
2801 case 'u':
2802 case 'v':
2803 case 'w':
2804 case 'x':
2805 case 'y':
2806 case 'z':
2807 /* case 'A' ... 'z': */
2808 case 'A':
2809 case 'B':
2810 case 'C':
2811 case 'D':
2812 case 'E':
2813 case 'F':
2814 case 'G':
2815 case 'H':
2816 case 'I':
2817 case 'J':
2818 case 'K':
2819 case 'L':
2820 case 'M':
2821 case 'N':
2822 case 'O':
2823 case 'P':
2824 case 'Q':
2825 case 'R':
2826 case 'S':
2827 case 'T':
2828 case 'U':
2829 case 'V':
2830 case 'W':
2831 case 'X':
2832 case 'Y':
2833 case 'Z':
2834 case '_':
2835 case '.':
2836 case '$':
2837 case '~':
2838 q = name;
2839 parse_name:
2840 for(;;) {
2841 if (!((ch >= 'a' && ch <= 'z') ||
2842 (ch >= 'A' && ch <= 'Z') ||
2843 (ch >= '0' && ch <= '9') ||
2844 strchr("/.-_+=$:\\,~", ch)))
2845 break;
2846 if ((q - name) < name_size - 1) {
2847 *q++ = ch;
2849 minp();
2851 *q = '\0';
2852 c = LD_TOK_NAME;
2853 break;
2854 case CH_EOF:
2855 c = LD_TOK_EOF;
2856 break;
2857 default:
2858 c = ch;
2859 inp();
2860 break;
2862 return c;
2865 static int ld_add_file(TCCState *s1, const char filename[])
2867 if (filename[0] == '/') {
2868 if (CONFIG_SYSROOT[0] == '\0'
2869 && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0)
2870 return 0;
2871 filename = tcc_basename(filename);
2873 return tcc_add_dll(s1, filename, 0);
2876 static inline int new_undef_syms(void)
2878 int ret = 0;
2879 ret = new_undef_sym;
2880 new_undef_sym = 0;
2881 return ret;
2884 static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
2886 char filename[1024], libname[1024];
2887 int t, group, nblibs = 0, ret = 0;
2888 char **libs = NULL;
2890 group = !strcmp(cmd, "GROUP");
2891 if (!as_needed)
2892 new_undef_syms();
2893 t = ld_next(s1, filename, sizeof(filename));
2894 if (t != '(')
2895 expect("(");
2896 t = ld_next(s1, filename, sizeof(filename));
2897 for(;;) {
2898 libname[0] = '\0';
2899 if (t == LD_TOK_EOF) {
2900 tcc_error_noabort("unexpected end of file");
2901 ret = -1;
2902 goto lib_parse_error;
2903 } else if (t == ')') {
2904 break;
2905 } else if (t == '-') {
2906 t = ld_next(s1, filename, sizeof(filename));
2907 if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
2908 tcc_error_noabort("library name expected");
2909 ret = -1;
2910 goto lib_parse_error;
2912 pstrcpy(libname, sizeof libname, &filename[1]);
2913 if (s1->static_link) {
2914 snprintf(filename, sizeof filename, "lib%s.a", libname);
2915 } else {
2916 snprintf(filename, sizeof filename, "lib%s.so", libname);
2918 } else if (t != LD_TOK_NAME) {
2919 tcc_error_noabort("filename expected");
2920 ret = -1;
2921 goto lib_parse_error;
2923 if (!strcmp(filename, "AS_NEEDED")) {
2924 ret = ld_add_file_list(s1, cmd, 1);
2925 if (ret)
2926 goto lib_parse_error;
2927 } else {
2928 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2929 if (!as_needed) {
2930 ret = ld_add_file(s1, filename);
2931 if (ret)
2932 goto lib_parse_error;
2933 if (group) {
2934 /* Add the filename *and* the libname to avoid future conversions */
2935 dynarray_add(&libs, &nblibs, tcc_strdup(filename));
2936 if (libname[0] != '\0')
2937 dynarray_add(&libs, &nblibs, tcc_strdup(libname));
2941 t = ld_next(s1, filename, sizeof(filename));
2942 if (t == ',') {
2943 t = ld_next(s1, filename, sizeof(filename));
2946 if (group && !as_needed) {
2947 while (new_undef_syms()) {
2948 int i;
2950 for (i = 0; i < nblibs; i ++)
2951 ld_add_file(s1, libs[i]);
2954 lib_parse_error:
2955 dynarray_reset(&libs, &nblibs);
2956 return ret;
2959 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2960 files */
2961 ST_FUNC int tcc_load_ldscript(TCCState *s1)
2963 char cmd[64];
2964 char filename[1024];
2965 int t, ret;
2967 ch = handle_eob();
2968 for(;;) {
2969 t = ld_next(s1, cmd, sizeof(cmd));
2970 if (t == LD_TOK_EOF)
2971 return 0;
2972 else if (t != LD_TOK_NAME)
2973 return -1;
2974 if (!strcmp(cmd, "INPUT") ||
2975 !strcmp(cmd, "GROUP")) {
2976 ret = ld_add_file_list(s1, cmd, 0);
2977 if (ret)
2978 return ret;
2979 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
2980 !strcmp(cmd, "TARGET")) {
2981 /* ignore some commands */
2982 t = ld_next(s1, cmd, sizeof(cmd));
2983 if (t != '(')
2984 expect("(");
2985 for(;;) {
2986 t = ld_next(s1, filename, sizeof(filename));
2987 if (t == LD_TOK_EOF) {
2988 tcc_error_noabort("unexpected end of file");
2989 return -1;
2990 } else if (t == ')') {
2991 break;
2994 } else {
2995 return -1;
2998 return 0;
3000 #endif /* !TCC_TARGET_PE */