Fix some multi-file corner cases with asm
[tinycc.git] / tccelf.c
blob278d586c6a36095d4d4aca81a1404933cb1bb648
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 if (!nb_buckets)
303 nb_buckets = ((int*)s->hash->data)[0];
305 s->hash->data_offset = 0;
306 ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
307 ptr[0] = nb_buckets;
308 ptr[1] = nb_syms;
309 ptr += 2;
310 hash = ptr;
311 memset(hash, 0, (nb_buckets + 1) * sizeof(int));
312 ptr += nb_buckets + 1;
314 sym = (ElfW(Sym) *)s->data + 1;
315 for(sym_index = 1; sym_index < nb_syms; sym_index++) {
316 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
317 h = elf_hash(strtab + sym->st_name) % nb_buckets;
318 *ptr = hash[h];
319 hash[h] = sym_index;
320 } else {
321 *ptr = 0;
323 ptr++;
324 sym++;
328 /* return the symbol number */
329 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
330 int info, int other, int shndx, const char *name)
332 int name_offset, sym_index;
333 int nbuckets, h;
334 ElfW(Sym) *sym;
335 Section *hs;
337 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
338 if (name)
339 name_offset = put_elf_str(s->link, name);
340 else
341 name_offset = 0;
342 /* XXX: endianness */
343 sym->st_name = name_offset;
344 sym->st_value = value;
345 sym->st_size = size;
346 sym->st_info = info;
347 sym->st_other = other;
348 sym->st_shndx = shndx;
349 sym_index = sym - (ElfW(Sym) *)s->data;
350 hs = s->hash;
351 if (hs) {
352 int *ptr, *base;
353 ptr = section_ptr_add(hs, sizeof(int));
354 base = (int *)hs->data;
355 /* only add global or weak symbols. */
356 if (ELFW(ST_BIND)(info) != STB_LOCAL) {
357 /* add another hashing entry */
358 nbuckets = base[0];
359 h = elf_hash((unsigned char *) name) % nbuckets;
360 *ptr = base[2 + h];
361 base[2 + h] = sym_index;
362 base[1]++;
363 /* we resize the hash table */
364 hs->nb_hashed_syms++;
365 if (hs->nb_hashed_syms > 2 * nbuckets) {
366 rebuild_hash(s, 2 * nbuckets);
368 } else {
369 *ptr = 0;
370 base[1]++;
373 return sym_index;
376 static int find_elf_sym_1(Section *s, const char *name, int onlydef)
378 ElfW(Sym) *sym;
379 Section *hs;
380 int nbuckets, sym_index, h;
381 const char *name1;
383 hs = s->hash;
384 if (!hs)
385 return 0;
386 nbuckets = ((int *)hs->data)[0];
387 h = elf_hash((unsigned char *) name) % nbuckets;
388 sym_index = ((int *)hs->data)[2 + h];
389 while (sym_index != 0) {
390 sym = &((ElfW(Sym) *)s->data)[sym_index];
391 name1 = (char *) s->link->data + sym->st_name;
392 if (!strcmp(name, name1) && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL
393 && (!onlydef || sym->st_shndx != SHN_UNDEF))
394 return sym_index;
395 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
397 return 0;
400 /* find global ELF symbol 'name' and return its index. Return 0 if not
401 found. */
402 ST_FUNC int find_elf_sym(Section *s, const char *name)
404 return find_elf_sym_1(s, name, 0);
407 /* return elf symbol value, signal error if 'err' is nonzero */
408 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err)
410 int sym_index;
411 ElfW(Sym) *sym;
413 sym_index = find_elf_sym(s->symtab, name);
414 sym = &((ElfW(Sym) *)s->symtab->data)[sym_index];
415 if (!sym_index || sym->st_shndx == SHN_UNDEF) {
416 if (err)
417 tcc_error("%s not defined", name);
418 return 0;
420 return sym->st_value;
423 /* return elf symbol value */
424 LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
426 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0);
429 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
430 /* return elf symbol value or error */
431 ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)
433 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 1);
435 #endif
437 /* add an elf symbol : check if it is already defined and patch
438 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
439 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
440 int info, int other, int shndx, const char *name)
442 ElfW(Sym) *esym;
443 int sym_bind, sym_index, sym_type, esym_bind;
444 unsigned char sym_vis, esym_vis, new_vis;
446 sym_bind = ELFW(ST_BIND)(info);
447 sym_type = ELFW(ST_TYPE)(info);
448 sym_vis = ELFW(ST_VISIBILITY)(other);
450 sym_index = find_elf_sym(s, name);
451 esym = &((ElfW(Sym) *)s->data)[sym_index];
452 if (sym_index && esym->st_value == value && esym->st_size == size
453 && esym->st_info == info && esym->st_other == other
454 && esym->st_shndx == shndx)
455 return sym_index;
457 if (sym_bind != STB_LOCAL) {
458 /* we search global or weak symbols */
459 if (!sym_index)
460 goto do_def;
461 if (esym->st_shndx != SHN_UNDEF) {
462 esym_bind = ELFW(ST_BIND)(esym->st_info);
463 /* propagate the most constraining visibility */
464 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
465 esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
466 if (esym_vis == STV_DEFAULT) {
467 new_vis = sym_vis;
468 } else if (sym_vis == STV_DEFAULT) {
469 new_vis = esym_vis;
470 } else {
471 new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
473 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
474 | new_vis;
475 other = esym->st_other; /* in case we have to patch esym */
476 if (shndx == SHN_UNDEF) {
477 /* ignore adding of undefined symbol if the
478 corresponding symbol is already defined */
479 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
480 /* global overrides weak, so patch */
481 goto do_patch;
482 } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
483 /* weak is ignored if already global */
484 } else if (sym_bind == STB_WEAK && esym_bind == STB_WEAK) {
485 /* keep first-found weak definition, ignore subsequents */
486 } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
487 /* ignore hidden symbols after */
488 } else if ((esym->st_shndx == SHN_COMMON
489 || esym->st_shndx == bss_section->sh_num)
490 && (shndx < SHN_LORESERVE
491 && shndx != bss_section->sh_num)) {
492 /* data symbol gets precedence over common/bss */
493 goto do_patch;
494 } else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
495 /* data symbol keeps precedence over common/bss */
496 } else if (s == tcc_state->dynsymtab_section) {
497 /* we accept that two DLL define the same symbol */
498 } else if (esym->st_other & ST_ASM_SET) {
499 /* If the existing symbol came from an asm .set
500 we can override. */
501 goto do_patch;
502 } else {
503 #if 0
504 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
505 sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
506 #endif
507 tcc_error_noabort("'%s' defined twice", name);
509 } else {
510 do_patch:
511 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
512 esym->st_shndx = shndx;
513 new_undef_sym = 1;
514 esym->st_value = value;
515 esym->st_size = size;
516 esym->st_other = other;
518 } else {
519 do_def:
520 sym_index = put_elf_sym(s, value, size,
521 ELFW(ST_INFO)(sym_bind, sym_type), other,
522 shndx, name);
524 return sym_index;
527 /* put relocation */
528 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
529 int type, int symbol, addr_t addend)
531 char buf[256];
532 Section *sr;
533 ElfW_Rel *rel;
535 sr = s->reloc;
536 if (!sr) {
537 /* if no relocation section, create it */
538 snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
539 /* if the symtab is allocated, then we consider the relocation
540 are also */
541 sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
542 sr->sh_entsize = sizeof(ElfW_Rel);
543 sr->link = symtab;
544 sr->sh_info = s->sh_num;
545 s->reloc = sr;
547 rel = section_ptr_add(sr, sizeof(ElfW_Rel));
548 rel->r_offset = offset;
549 rel->r_info = ELFW(R_INFO)(symbol, type);
550 #if SHT_RELX == SHT_RELA
551 rel->r_addend = addend;
552 #else
553 if (addend)
554 tcc_error("non-zero addend on REL architecture");
555 #endif
558 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
559 int type, int symbol)
561 put_elf_reloca(symtab, s, offset, type, symbol, 0);
564 /* Remove relocations for section S->reloc starting at oldrelocoffset
565 that are to the same place, retaining the last of them. As side effect
566 the relocations are sorted. Possibly reduces the number of relocs. */
567 ST_FUNC void squeeze_multi_relocs(Section *s, size_t oldrelocoffset)
569 Section *sr = s->reloc;
570 ElfW_Rel *r, *dest;
571 ssize_t a;
572 ElfW(Addr) addr;
574 if (oldrelocoffset + sizeof(*r) >= sr->data_offset)
575 return;
576 /* The relocs we're dealing with are the result of initializer parsing.
577 So they will be mostly in order and there aren't many of them.
578 Secondly we need a stable sort (which qsort isn't). We use
579 a simple insertion sort. */
580 for (a = oldrelocoffset + sizeof(*r); a < sr->data_offset; a += sizeof(*r)) {
581 ssize_t i = a - sizeof(*r);
582 addr = ((ElfW_Rel*)(sr->data + a))->r_offset;
583 for (; i >= (ssize_t)oldrelocoffset &&
584 ((ElfW_Rel*)(sr->data + i))->r_offset > addr; i -= sizeof(*r)) {
585 ElfW_Rel tmp = *(ElfW_Rel*)(sr->data + a);
586 *(ElfW_Rel*)(sr->data + a) = *(ElfW_Rel*)(sr->data + i);
587 *(ElfW_Rel*)(sr->data + i) = tmp;
591 r = (ElfW_Rel*)(sr->data + oldrelocoffset);
592 dest = r;
593 for (; r < (ElfW_Rel*)(sr->data + sr->data_offset); r++) {
594 if (dest->r_offset != r->r_offset)
595 dest++;
596 *dest = *r;
598 sr->data_offset = (unsigned char*)dest - sr->data + sizeof(*r);
601 /* put stab debug information */
603 ST_FUNC void put_stabs(const char *str, int type, int other, int desc,
604 unsigned long value)
606 Stab_Sym *sym;
608 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
609 if (str) {
610 sym->n_strx = put_elf_str(stabstr_section, str);
611 } else {
612 sym->n_strx = 0;
614 sym->n_type = type;
615 sym->n_other = other;
616 sym->n_desc = desc;
617 sym->n_value = value;
620 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc,
621 unsigned long value, Section *sec, int sym_index)
623 put_stabs(str, type, other, desc, value);
624 put_elf_reloc(symtab_section, stab_section,
625 stab_section->data_offset - sizeof(unsigned int),
626 R_DATA_32, sym_index);
629 ST_FUNC void put_stabn(int type, int other, int desc, int value)
631 put_stabs(NULL, type, other, desc, value);
634 ST_FUNC void put_stabd(int type, int other, int desc)
636 put_stabs(NULL, type, other, desc, 0);
639 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
641 int n;
642 struct sym_attr *tab;
644 if (index >= s1->nb_sym_attrs) {
645 if (!alloc)
646 return s1->sym_attrs;
647 /* find immediately bigger power of 2 and reallocate array */
648 n = 1;
649 while (index >= n)
650 n *= 2;
651 tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
652 s1->sym_attrs = tab;
653 memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
654 (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
655 s1->nb_sym_attrs = n;
657 return &s1->sym_attrs[index];
660 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
661 using variable <elem> */
662 #define for_each_elem(sec, startoff, elem, type) \
663 for (elem = (type *) sec->data + startoff; \
664 elem < (type *) (sec->data + sec->data_offset); elem++)
666 /* In an ELF file symbol table, the local symbols must appear below
667 the global and weak ones. Since TCC cannot sort it while generating
668 the code, we must do it after. All the relocation tables are also
669 modified to take into account the symbol table sorting */
670 static void sort_syms(TCCState *s1, Section *s)
672 int *old_to_new_syms;
673 ElfW(Sym) *new_syms;
674 int nb_syms, i;
675 ElfW(Sym) *p, *q;
676 ElfW_Rel *rel;
677 Section *sr;
678 int type, sym_index;
680 nb_syms = s->data_offset / sizeof(ElfW(Sym));
681 new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
682 old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
684 /* first pass for local symbols */
685 p = (ElfW(Sym) *)s->data;
686 q = new_syms;
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++;
694 /* save the number of local symbols in section header */
695 if( s->sh_size ) /* this 'if' makes IDA happy */
696 s->sh_info = q - new_syms;
698 /* then second pass for non local symbols */
699 p = (ElfW(Sym) *)s->data;
700 for(i = 0; i < nb_syms; i++) {
701 if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
702 old_to_new_syms[i] = q - new_syms;
703 *q++ = *p;
705 p++;
708 /* we copy the new symbols to the old */
709 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
710 tcc_free(new_syms);
712 /* now we modify all the relocations */
713 for(i = 1; i < s1->nb_sections; i++) {
714 sr = s1->sections[i];
715 if (sr->sh_type == SHT_RELX && sr->link == s) {
716 for_each_elem(sr, 0, rel, ElfW_Rel) {
717 sym_index = ELFW(R_SYM)(rel->r_info);
718 type = ELFW(R_TYPE)(rel->r_info);
719 sym_index = old_to_new_syms[sym_index];
720 rel->r_info = ELFW(R_INFO)(sym_index, type);
725 tcc_free(old_to_new_syms);
728 /* relocate symbol table, resolve undefined symbols if do_resolve is
729 true and output error if undefined symbol. */
730 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
732 ElfW(Sym) *sym;
733 int sym_bind, sh_num;
734 const char *name;
736 for_each_elem(symtab, 1, sym, ElfW(Sym)) {
737 sh_num = sym->st_shndx;
738 if (sh_num == SHN_UNDEF) {
739 name = (char *) strtab_section->data + sym->st_name;
740 /* Use ld.so to resolve symbol for us (for tcc -run) */
741 if (do_resolve) {
742 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
743 void *addr = dlsym(RTLD_DEFAULT, name);
744 if (addr) {
745 sym->st_value = (addr_t) addr;
746 #ifdef DEBUG_RELOC
747 printf ("relocate_sym: %s -> 0x%lx\n", name, sym->st_value);
748 #endif
749 goto found;
751 #endif
752 /* if dynamic symbol exist, it will be used in relocate_section */
753 } else if (s1->dynsym && find_elf_sym(s1->dynsym, name))
754 goto found;
755 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
756 it */
757 if (!strcmp(name, "_fp_hw"))
758 goto found;
759 /* only weak symbols are accepted to be undefined. Their
760 value is zero */
761 sym_bind = ELFW(ST_BIND)(sym->st_info);
762 if (sym_bind == STB_WEAK)
763 sym->st_value = 0;
764 else
765 tcc_error_noabort("undefined symbol '%s'", name);
766 } else if (sh_num < SHN_LORESERVE) {
767 /* add section base */
768 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
770 found: ;
774 /* relocate a given section (CPU dependent) by applying the relocations
775 in the associated relocation section */
776 ST_FUNC void relocate_section(TCCState *s1, Section *s)
778 Section *sr = s->reloc;
779 ElfW_Rel *rel;
780 ElfW(Sym) *sym;
781 int type, sym_index;
782 unsigned char *ptr;
783 addr_t tgt, addr;
785 relocate_init(sr);
787 for_each_elem(sr, 0, rel, ElfW_Rel) {
788 ptr = s->data + rel->r_offset;
789 sym_index = ELFW(R_SYM)(rel->r_info);
790 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
791 type = ELFW(R_TYPE)(rel->r_info);
792 tgt = sym->st_value;
793 #if SHT_RELX == SHT_RELA
794 tgt += rel->r_addend;
795 #endif
796 addr = s->sh_addr + rel->r_offset;
797 relocate(s1, rel, type, ptr, addr, tgt);
799 /* if the relocation is allocated, we change its symbol table */
800 if (sr->sh_flags & SHF_ALLOC)
801 sr->link = s1->dynsym;
804 /* relocate relocation table in 'sr' */
805 static void relocate_rel(TCCState *s1, Section *sr)
807 Section *s;
808 ElfW_Rel *rel;
810 s = s1->sections[sr->sh_info];
811 for_each_elem(sr, 0, rel, ElfW_Rel)
812 rel->r_offset += s->sh_addr;
815 /* count the number of dynamic relocations so that we can reserve
816 their space */
817 static int prepare_dynamic_rel(TCCState *s1, Section *sr)
819 ElfW_Rel *rel;
820 int sym_index, type, count;
822 count = 0;
823 for_each_elem(sr, 0, rel, ElfW_Rel) {
824 sym_index = ELFW(R_SYM)(rel->r_info);
825 type = ELFW(R_TYPE)(rel->r_info);
826 switch(type) {
827 #if defined(TCC_TARGET_I386)
828 case R_386_32:
829 if (!get_sym_attr(s1, sym_index, 0)->dyn_index
830 && ((ElfW(Sym)*)symtab_section->data + sym_index)->st_shndx == SHN_UNDEF) {
831 /* don't fixup unresolved (weak) symbols */
832 rel->r_info = ELFW(R_INFO)(sym_index, R_386_RELATIVE);
833 break;
835 #elif defined(TCC_TARGET_X86_64)
836 case R_X86_64_32:
837 case R_X86_64_32S:
838 case R_X86_64_64:
839 #endif
840 count++;
841 break;
842 #if defined(TCC_TARGET_I386)
843 case R_386_PC32:
844 #elif defined(TCC_TARGET_X86_64)
845 case R_X86_64_PC32:
846 #endif
847 if (get_sym_attr(s1, sym_index, 0)->dyn_index)
848 count++;
849 break;
850 default:
851 break;
854 if (count) {
855 /* allocate the section */
856 sr->sh_flags |= SHF_ALLOC;
857 sr->sh_size = count * sizeof(ElfW_Rel);
859 return count;
862 static void build_got(TCCState *s1)
864 /* if no got, then create it */
865 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
866 s1->got->sh_entsize = 4;
867 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
868 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
869 /* keep space for _DYNAMIC pointer and two dummy got entries */
870 section_ptr_add(s1->got, 3 * PTR_SIZE);
873 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
874 in s1->symtab. When creating the dynamic symbol table entry for the GOT
875 relocation, use 'size' and 'info' for the corresponding symbol metadata.
876 Returns the offset of the GOT or (if any) PLT entry. */
877 static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
878 unsigned long size,
879 int info, int sym_index)
881 int need_plt_entry;
882 const char *name;
883 ElfW(Sym) *sym;
884 struct sym_attr *attr;
885 unsigned got_offset;
886 char plt_name[100];
887 int len;
889 need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
890 attr = get_sym_attr(s1, sym_index, 1);
892 /* In case a function is both called and its address taken 2 GOT entries
893 are created, one for taking the address (GOT) and the other for the PLT
894 entry (PLTGOT). */
895 if (need_plt_entry ? attr->plt_offset : attr->got_offset)
896 return attr;
898 /* create the GOT entry */
899 got_offset = s1->got->data_offset;
900 section_ptr_add(s1->got, PTR_SIZE);
902 /* Create the GOT relocation that will insert the address of the object or
903 function of interest in the GOT entry. This is a static relocation for
904 memory output (dlsym will give us the address of symbols) and dynamic
905 relocation otherwise (executable and DLLs). The relocation should be
906 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
907 associated to a PLT entry) but is currently done at load time for an
908 unknown reason. */
910 sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
911 name = (char *) symtab_section->link->data + sym->st_name;
913 if (s1->dynsym) {
914 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
915 /* Hack alarm. We don't want to emit dynamic symbols
916 and symbol based relocs for STB_LOCAL symbols, but rather
917 want to resolve them directly. At this point the symbol
918 values aren't final yet, so we must defer this. We will later
919 have to create a RELATIVE reloc anyway, so we misuse the
920 relocation slot to smuggle the symbol reference until
921 fill_local_got_entries. Not that the sym_index is
922 relative to symtab_section, not s1->dynsym! Nevertheless
923 we use s1->dyn_sym so that if this is the first call
924 that got->reloc is correctly created. Also note that
925 RELATIVE relocs are not normally created for the .got,
926 so the types serves as a marker for later (and is retained
927 also for the final output, which is okay because then the
928 got is just normal data). */
929 put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE,
930 sym_index);
931 } else {
932 if (0 == attr->dyn_index)
933 attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size,
934 info, 0, sym->st_shndx, name);
935 put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type,
936 attr->dyn_index);
938 } else {
939 put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
940 sym_index);
943 if (need_plt_entry) {
944 if (!s1->plt) {
945 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
946 SHF_ALLOC | SHF_EXECINSTR);
947 s1->plt->sh_entsize = 4;
950 attr->plt_offset = create_plt_entry(s1, got_offset, attr);
952 /* create a symbol 'sym@plt' for the PLT jump vector */
953 len = strlen(name);
954 if (len > sizeof plt_name - 5)
955 len = sizeof plt_name - 5;
956 memcpy(plt_name, name, len);
957 strcpy(plt_name + len, "@plt");
958 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
959 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
961 } else {
962 attr->got_offset = got_offset;
965 return attr;
968 /* build GOT and PLT entries */
969 ST_FUNC void build_got_entries(TCCState *s1)
971 Section *s;
972 ElfW_Rel *rel;
973 ElfW(Sym) *sym;
974 int i, type, gotplt_entry, reloc_type, sym_index;
975 struct sym_attr *attr;
977 for(i = 1; i < s1->nb_sections; i++) {
978 s = s1->sections[i];
979 if (s->sh_type != SHT_RELX)
980 continue;
981 /* no need to handle got relocations */
982 if (s->link != symtab_section)
983 continue;
984 for_each_elem(s, 0, rel, ElfW_Rel) {
985 type = ELFW(R_TYPE)(rel->r_info);
986 gotplt_entry = gotplt_entry_type(type);
987 sym_index = ELFW(R_SYM)(rel->r_info);
988 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
990 if (gotplt_entry == NO_GOTPLT_ENTRY) {
991 continue;
994 /* Automatically create PLT/GOT [entry] if it is an undefined
995 reference (resolved at runtime), or the symbol is absolute,
996 probably created by tcc_add_symbol, and thus on 64-bit
997 targets might be too far from application code. */
998 if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
999 if (sym->st_shndx == SHN_UNDEF) {
1000 ElfW(Sym) *esym;
1001 int dynindex;
1002 if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
1003 continue;
1004 /* Relocations for UNDEF symbols would normally need
1005 to be transferred into the executable or shared object.
1006 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1007 But TCC doesn't do that (at least for exes), so we
1008 need to resolve all such relocs locally. And that
1009 means PLT slots for functions in DLLs and COPY relocs for
1010 data symbols. COPY relocs were generated in
1011 bind_exe_dynsyms (and the symbol adjusted to be defined),
1012 and for functions we were generated a dynamic symbol
1013 of function type. */
1014 if (s1->dynsym) {
1015 /* dynsym isn't set for -run :-/ */
1016 dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index;
1017 esym = (ElfW(Sym) *)s1->dynsym->data + dynindex;
1018 if (dynindex
1019 && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
1020 || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
1021 && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC)))
1022 goto jmp_slot;
1024 } else if (!(sym->st_shndx == SHN_ABS
1025 #ifndef TCC_TARGET_ARM
1026 && PTR_SIZE == 8
1027 #endif
1029 continue;
1032 #ifdef TCC_TARGET_X86_64
1033 if ((type == R_X86_64_PLT32 || type == R_X86_64_PC32) &&
1034 (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT ||
1035 ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)) {
1036 rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
1037 continue;
1039 #endif
1040 if (code_reloc(type)) {
1041 jmp_slot:
1042 reloc_type = R_JMP_SLOT;
1043 } else
1044 reloc_type = R_GLOB_DAT;
1046 if (!s1->got)
1047 build_got(s1);
1049 if (gotplt_entry == BUILD_GOT_ONLY)
1050 continue;
1052 attr = put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1053 sym_index);
1055 if (reloc_type == R_JMP_SLOT)
1056 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
1061 /* put dynamic tag */
1062 static void put_dt(Section *dynamic, int dt, addr_t val)
1064 ElfW(Dyn) *dyn;
1065 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1066 dyn->d_tag = dt;
1067 dyn->d_un.d_val = val;
1070 #ifndef TCC_TARGET_PE
1071 static void add_init_array_defines(TCCState *s1, const char *section_name)
1073 Section *s;
1074 long end_offset;
1075 char sym_start[1024];
1076 char sym_end[1024];
1078 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1079 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1081 s = find_section(s1, section_name);
1082 if (!s) {
1083 end_offset = 0;
1084 s = data_section;
1085 } else {
1086 end_offset = s->data_offset;
1089 set_elf_sym(symtab_section,
1090 0, 0,
1091 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1092 s->sh_num, sym_start);
1093 set_elf_sym(symtab_section,
1094 end_offset, 0,
1095 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1096 s->sh_num, sym_end);
1098 #endif
1100 static int tcc_add_support(TCCState *s1, const char *filename)
1102 char buf[1024];
1103 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
1104 return tcc_add_file(s1, buf);
1107 ST_FUNC void tcc_add_bcheck(TCCState *s1)
1109 #ifdef CONFIG_TCC_BCHECK
1110 addr_t *ptr;
1111 int sym_index;
1113 if (0 == s1->do_bounds_check)
1114 return;
1115 /* XXX: add an object file to do that */
1116 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1117 *ptr = 0;
1118 set_elf_sym(symtab_section, 0, 0,
1119 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1120 bounds_section->sh_num, "__bounds_start");
1121 /* pull bcheck.o from libtcc1.a */
1122 sym_index = set_elf_sym(symtab_section, 0, 0,
1123 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1124 SHN_UNDEF, "__bound_init");
1125 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1126 /* add 'call __bound_init()' in .init section */
1127 Section *init_section = find_section(s1, ".init");
1128 unsigned char *pinit = section_ptr_add(init_section, 5);
1129 pinit[0] = 0xe8;
1130 write32le(pinit + 1, -4);
1131 put_elf_reloc(symtab_section, init_section,
1132 init_section->data_offset - 4, R_386_PC32, sym_index);
1133 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1135 #endif
1138 /* add tcc runtime libraries */
1139 ST_FUNC void tcc_add_runtime(TCCState *s1)
1141 tcc_add_bcheck(s1);
1142 tcc_add_pragma_libs(s1);
1143 /* add libc */
1144 if (!s1->nostdlib) {
1145 tcc_add_library_err(s1, "c");
1146 #ifdef TCC_LIBGCC
1147 if (!s1->static_link) {
1148 if (TCC_LIBGCC[0] == '/')
1149 tcc_add_file(s1, TCC_LIBGCC);
1150 else
1151 tcc_add_dll(s1, TCC_LIBGCC, 0);
1153 #endif
1154 tcc_add_support(s1, TCC_LIBTCC1);
1155 /* add crt end if not memory output */
1156 if (s1->output_type != TCC_OUTPUT_MEMORY)
1157 tcc_add_crt(s1, "crtn.o");
1161 /* add various standard linker symbols (must be done after the
1162 sections are filled (for example after allocating common
1163 symbols)) */
1164 static void tcc_add_linker_symbols(TCCState *s1)
1166 char buf[1024];
1167 int i;
1168 Section *s;
1170 set_elf_sym(symtab_section,
1171 text_section->data_offset, 0,
1172 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1173 text_section->sh_num, "_etext");
1174 set_elf_sym(symtab_section,
1175 data_section->data_offset, 0,
1176 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1177 data_section->sh_num, "_edata");
1178 set_elf_sym(symtab_section,
1179 bss_section->data_offset, 0,
1180 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1181 bss_section->sh_num, "_end");
1182 #ifndef TCC_TARGET_PE
1183 /* horrible new standard ldscript defines */
1184 add_init_array_defines(s1, ".preinit_array");
1185 add_init_array_defines(s1, ".init_array");
1186 add_init_array_defines(s1, ".fini_array");
1187 #endif
1189 /* add start and stop symbols for sections whose name can be
1190 expressed in C */
1191 for(i = 1; i < s1->nb_sections; i++) {
1192 s = s1->sections[i];
1193 if (s->sh_type == SHT_PROGBITS &&
1194 (s->sh_flags & SHF_ALLOC)) {
1195 const char *p;
1196 int ch;
1198 /* check if section name can be expressed in C */
1199 p = s->name;
1200 for(;;) {
1201 ch = *p;
1202 if (!ch)
1203 break;
1204 if (!isid(ch) && !isnum(ch))
1205 goto next_sec;
1206 p++;
1208 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1209 set_elf_sym(symtab_section,
1210 0, 0,
1211 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1212 s->sh_num, buf);
1213 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1214 set_elf_sym(symtab_section,
1215 s->data_offset, 0,
1216 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1217 s->sh_num, buf);
1219 next_sec: ;
1223 /* Do final regular symbol preparation (for those coming from .c/.o/.s files,
1224 not from shared libs) */
1225 ST_FUNC void resolve_regular_syms(void)
1227 int rebuild = 0;
1228 ElfW(Sym) *sym;
1230 /* Allocate common symbols in BSS. */
1231 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1232 if (sym->st_shndx == SHN_COMMON) {
1233 /* symbol alignment is in st_value for SHN_COMMONs */
1234 sym->st_value = section_add(bss_section, sym->st_size,
1235 sym->st_value);
1236 sym->st_shndx = bss_section->sh_num;
1240 /* Now assign linker provided symbols their value. */
1241 tcc_add_linker_symbols(tcc_state);
1243 /* And finally resolve still UNDEF symbols (for multi-file mode),
1244 and globalize those that are still UNDEF. */
1245 rebuild_hash(symtab_section, 0);
1246 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1247 if (sym->st_shndx == SHN_UNDEF) {
1248 const char *name = (char *) symtab_section->link->data + sym->st_name;
1249 int symndx = find_elf_sym_1(symtab_section, name, 1);
1250 if (symndx) {
1251 *sym = ((ElfSym *)symtab_section->data)[symndx];
1252 rebuild = 1;
1253 } else if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
1254 sym->st_info
1255 = ELFW(ST_INFO)(STB_GLOBAL, ELFW(ST_TYPE)(sym->st_info));
1256 rebuild = 1;
1260 if (rebuild)
1261 rebuild_hash(symtab_section, 0);
1264 static void tcc_output_binary(TCCState *s1, FILE *f,
1265 const int *sec_order)
1267 Section *s;
1268 int i, offset, size;
1270 offset = 0;
1271 for(i=1;i<s1->nb_sections;i++) {
1272 s = s1->sections[sec_order[i]];
1273 if (s->sh_type != SHT_NOBITS &&
1274 (s->sh_flags & SHF_ALLOC)) {
1275 while (offset < s->sh_offset) {
1276 fputc(0, f);
1277 offset++;
1279 size = s->sh_size;
1280 fwrite(s->data, 1, size, f);
1281 offset += size;
1286 ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1288 int sym_index = ELFW(R_SYM) (rel->r_info);
1289 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1290 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1291 unsigned offset = attr->got_offset;
1293 if (0 == offset)
1294 return;
1295 section_reserve(s1->got, offset + PTR_SIZE);
1296 #ifdef TCC_TARGET_X86_64
1297 write64le(s1->got->data + offset, sym->st_value);
1298 #else
1299 write32le(s1->got->data + offset, sym->st_value);
1300 #endif
1303 /* Perform relocation to GOT or PLT entries */
1304 ST_FUNC void fill_got(TCCState *s1)
1306 Section *s;
1307 ElfW_Rel *rel;
1308 int i;
1310 for(i = 1; i < s1->nb_sections; i++) {
1311 s = s1->sections[i];
1312 if (s->sh_type != SHT_RELX)
1313 continue;
1314 /* no need to handle got relocations */
1315 if (s->link != symtab_section)
1316 continue;
1317 for_each_elem(s, 0, rel, ElfW_Rel) {
1318 switch (ELFW(R_TYPE) (rel->r_info)) {
1319 case R_X86_64_GOT32:
1320 case R_X86_64_GOTPCREL:
1321 case R_X86_64_GOTPCRELX:
1322 case R_X86_64_REX_GOTPCRELX:
1323 case R_X86_64_PLT32:
1324 fill_got_entry(s1, rel);
1325 break;
1331 /* See put_got_entry for a description. This is the second stage
1332 where GOT references to local defined symbols are rewritten. */
1333 static void fill_local_got_entries(TCCState *s1)
1335 ElfW_Rel *rel;
1336 for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
1337 if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
1338 int sym_index = ELFW(R_SYM) (rel->r_info);
1339 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1340 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1341 unsigned offset = attr->got_offset;
1342 if (offset != rel->r_offset - s1->got->sh_addr)
1343 tcc_error_noabort("huh");
1344 rel->r_info = ELFW(R_INFO)(0, R_RELATIVE);
1345 #if SHT_RELX == SHT_RELA
1346 rel->r_addend = sym->st_value;
1347 #else
1348 /* All our REL architectures also happen to be 32bit LE. */
1349 write32le(s1->got->data + offset, sym->st_value);
1350 #endif
1355 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1356 in shared libraries and export non local defined symbols to shared libraries
1357 if -rdynamic switch was given on command line */
1358 static void bind_exe_dynsyms(TCCState *s1)
1360 const char *name;
1361 int sym_index, index;
1362 ElfW(Sym) *sym, *esym;
1363 int type;
1365 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1366 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1367 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1368 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1369 if (sym->st_shndx == SHN_UNDEF) {
1370 name = (char *) symtab_section->link->data + sym->st_name;
1371 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1372 if (sym_index) {
1373 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1374 type = ELFW(ST_TYPE)(esym->st_info);
1375 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
1376 /* Indirect functions shall have STT_FUNC type in executable
1377 * dynsym section. Indeed, a dlsym call following a lazy
1378 * resolution would pick the symbol value from the
1379 * executable dynsym entry which would contain the address
1380 * of the function wanted by the caller of dlsym instead of
1381 * the address of the function that would return that
1382 * address */
1383 int dynindex
1384 = put_elf_sym(s1->dynsym, 0, esym->st_size,
1385 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
1386 name);
1387 int index = sym - (ElfW(Sym) *) symtab_section->data;
1388 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1389 } else if (type == STT_OBJECT) {
1390 unsigned long offset;
1391 ElfW(Sym) *dynsym;
1392 offset = bss_section->data_offset;
1393 /* XXX: which alignment ? */
1394 offset = (offset + 16 - 1) & -16;
1395 set_elf_sym (s1->symtab, offset, esym->st_size,
1396 esym->st_info, 0, bss_section->sh_num, name);
1397 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1398 esym->st_info, 0, bss_section->sh_num,
1399 name);
1401 /* Ensure R_COPY works for weak symbol aliases */
1402 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1403 for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
1404 if ((dynsym->st_value == esym->st_value)
1405 && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
1406 char *dynname = (char *) s1->dynsymtab_section->link->data
1407 + dynsym->st_name;
1408 put_elf_sym(s1->dynsym, offset, dynsym->st_size,
1409 dynsym->st_info, 0,
1410 bss_section->sh_num, dynname);
1411 break;
1416 put_elf_reloc(s1->dynsym, bss_section,
1417 offset, R_COPY, index);
1418 offset += esym->st_size;
1419 bss_section->data_offset = offset;
1421 } else {
1422 /* STB_WEAK undefined symbols are accepted */
1423 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1424 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1425 !strcmp(name, "_fp_hw")) {
1426 } else {
1427 tcc_error_noabort("undefined symbol '%s'", name);
1430 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1431 /* if -rdynamic option, then export all non local symbols */
1432 name = (char *) symtab_section->link->data + sym->st_name;
1433 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1434 0, sym->st_shndx, name);
1439 /* Bind symbols of libraries: export all non local symbols of executable that
1440 are referenced by shared libraries. The reason is that the dynamic loader
1441 search symbol first in executable and then in libraries. Therefore a
1442 reference to a symbol already defined by a library can still be resolved by
1443 a symbol in the executable. */
1444 static void bind_libs_dynsyms(TCCState *s1)
1446 const char *name;
1447 int sym_index;
1448 ElfW(Sym) *sym, *esym;
1450 for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
1451 name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
1452 sym_index = find_elf_sym(symtab_section, name);
1453 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1454 if (sym_index && sym->st_shndx != SHN_UNDEF
1455 && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1456 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1457 sym->st_info, 0, sym->st_shndx, name);
1458 } else if (esym->st_shndx == SHN_UNDEF) {
1459 /* weak symbols can stay undefined */
1460 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
1461 tcc_warning("undefined dynamic symbol '%s'", name);
1466 /* Export all non local symbols. This is used by shared libraries so that the
1467 non local symbols they define can resolve a reference in another shared
1468 library or in the executable. Correspondingly, it allows undefined local
1469 symbols to be resolved by other shared libraries or by the executable. */
1470 static void export_global_syms(TCCState *s1)
1472 int dynindex, index;
1473 const char *name;
1474 ElfW(Sym) *sym;
1476 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1477 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1478 name = (char *) symtab_section->link->data + sym->st_name;
1479 dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1480 sym->st_info, 0, sym->st_shndx, name);
1481 index = sym - (ElfW(Sym) *) symtab_section->data;
1482 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1487 /* Allocate strings for section names and decide if an unallocated section
1488 should be output.
1489 NOTE: the strsec section comes last, so its size is also correct ! */
1490 static int alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
1492 int i;
1493 Section *s;
1494 int textrel = 0;
1496 /* Allocate strings for section names */
1497 for(i = 1; i < s1->nb_sections; i++) {
1498 s = s1->sections[i];
1499 /* when generating a DLL, we include relocations but we may
1500 patch them */
1501 if (file_type == TCC_OUTPUT_DLL &&
1502 s->sh_type == SHT_RELX &&
1503 !(s->sh_flags & SHF_ALLOC) &&
1504 (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) &&
1505 prepare_dynamic_rel(s1, s)) {
1506 if (s1->sections[s->sh_info]->sh_flags & SHF_EXECINSTR)
1507 textrel = 1;
1508 } else if (s1->do_debug ||
1509 file_type == TCC_OUTPUT_OBJ ||
1510 (s->sh_flags & SHF_ALLOC) ||
1511 i == (s1->nb_sections - 1)) {
1512 /* we output all sections if debug or object file */
1513 s->sh_size = s->data_offset;
1515 if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1516 s->sh_name = put_elf_str(strsec, s->name);
1518 strsec->sh_size = strsec->data_offset;
1519 return textrel;
1522 /* Info to be copied in dynamic section */
1523 struct dyn_inf {
1524 Section *dynamic;
1525 Section *dynstr;
1526 unsigned long data_offset;
1527 addr_t rel_addr;
1528 addr_t rel_size;
1529 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1530 addr_t bss_addr;
1531 addr_t bss_size;
1532 #endif
1535 /* Assign sections to segments and decide how are sections laid out when loaded
1536 in memory. This function also fills corresponding program headers. */
1537 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1538 Section *interp, Section* strsec,
1539 struct dyn_inf *dyninf, int *sec_order)
1541 int i, j, k, file_type, sh_order_index, file_offset;
1542 unsigned long s_align;
1543 long long tmp;
1544 addr_t addr;
1545 ElfW(Phdr) *ph;
1546 Section *s;
1548 file_type = s1->output_type;
1549 sh_order_index = 1;
1550 file_offset = 0;
1551 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1552 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1553 s_align = ELF_PAGE_SIZE;
1554 if (s1->section_align)
1555 s_align = s1->section_align;
1557 if (phnum > 0) {
1558 if (s1->has_text_addr) {
1559 int a_offset, p_offset;
1560 addr = s1->text_addr;
1561 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1562 ELF_PAGE_SIZE */
1563 a_offset = (int) (addr & (s_align - 1));
1564 p_offset = file_offset & (s_align - 1);
1565 if (a_offset < p_offset)
1566 a_offset += s_align;
1567 file_offset += (a_offset - p_offset);
1568 } else {
1569 if (file_type == TCC_OUTPUT_DLL)
1570 addr = 0;
1571 else
1572 addr = ELF_START_ADDR;
1573 /* compute address after headers */
1574 addr += (file_offset & (s_align - 1));
1577 ph = &phdr[0];
1578 /* Leave one program headers for the program interpreter and one for
1579 the program header table itself if needed. These are done later as
1580 they require section layout to be done first. */
1581 if (interp)
1582 ph += 2;
1584 /* dynamic relocation table information, for .dynamic section */
1585 dyninf->rel_addr = dyninf->rel_size = 0;
1586 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1587 dyninf->bss_addr = dyninf->bss_size = 0;
1588 #endif
1590 for(j = 0; j < 2; j++) {
1591 ph->p_type = PT_LOAD;
1592 if (j == 0)
1593 ph->p_flags = PF_R | PF_X;
1594 else
1595 ph->p_flags = PF_R | PF_W;
1596 ph->p_align = s_align;
1598 /* Decide the layout of sections loaded in memory. This must
1599 be done before program headers are filled since they contain
1600 info about the layout. We do the following ordering: interp,
1601 symbol tables, relocations, progbits, nobits */
1602 /* XXX: do faster and simpler sorting */
1603 for(k = 0; k < 5; k++) {
1604 for(i = 1; i < s1->nb_sections; i++) {
1605 s = s1->sections[i];
1606 /* compute if section should be included */
1607 if (j == 0) {
1608 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1609 SHF_ALLOC)
1610 continue;
1611 } else {
1612 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1613 (SHF_ALLOC | SHF_WRITE))
1614 continue;
1616 if (s == interp) {
1617 if (k != 0)
1618 continue;
1619 } else if (s->sh_type == SHT_DYNSYM ||
1620 s->sh_type == SHT_STRTAB ||
1621 s->sh_type == SHT_HASH) {
1622 if (k != 1)
1623 continue;
1624 } else if (s->sh_type == SHT_RELX) {
1625 if (k != 2)
1626 continue;
1627 } else if (s->sh_type == SHT_NOBITS) {
1628 if (k != 4)
1629 continue;
1630 } else {
1631 if (k != 3)
1632 continue;
1634 sec_order[sh_order_index++] = i;
1636 /* section matches: we align it and add its size */
1637 tmp = addr;
1638 addr = (addr + s->sh_addralign - 1) &
1639 ~(s->sh_addralign - 1);
1640 file_offset += (int) ( addr - tmp );
1641 s->sh_offset = file_offset;
1642 s->sh_addr = addr;
1644 /* update program header infos */
1645 if (ph->p_offset == 0) {
1646 ph->p_offset = file_offset;
1647 ph->p_vaddr = addr;
1648 ph->p_paddr = ph->p_vaddr;
1650 /* update dynamic relocation infos */
1651 if (s->sh_type == SHT_RELX) {
1652 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1653 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1654 dyninf->rel_addr = addr;
1655 dyninf->rel_size += s->sh_size; /* XXX only first rel. */
1657 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1658 dyninf->bss_addr = addr;
1659 dyninf->bss_size = s->sh_size; /* XXX only first rel. */
1661 #else
1662 if (dyninf->rel_size == 0)
1663 dyninf->rel_addr = addr;
1664 dyninf->rel_size += s->sh_size;
1665 #endif
1667 addr += s->sh_size;
1668 if (s->sh_type != SHT_NOBITS)
1669 file_offset += s->sh_size;
1672 if (j == 0) {
1673 /* Make the first PT_LOAD segment include the program
1674 headers itself (and the ELF header as well), it'll
1675 come out with same memory use but will make various
1676 tools like binutils strip work better. */
1677 ph->p_offset &= ~(ph->p_align - 1);
1678 ph->p_vaddr &= ~(ph->p_align - 1);
1679 ph->p_paddr &= ~(ph->p_align - 1);
1681 ph->p_filesz = file_offset - ph->p_offset;
1682 ph->p_memsz = addr - ph->p_vaddr;
1683 ph++;
1684 if (j == 0) {
1685 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1686 /* if in the middle of a page, we duplicate the page in
1687 memory so that one copy is RX and the other is RW */
1688 if ((addr & (s_align - 1)) != 0)
1689 addr += s_align;
1690 } else {
1691 addr = (addr + s_align - 1) & ~(s_align - 1);
1692 file_offset = (file_offset + s_align - 1) & ~(s_align - 1);
1698 /* all other sections come after */
1699 for(i = 1; i < s1->nb_sections; i++) {
1700 s = s1->sections[i];
1701 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1702 continue;
1703 sec_order[sh_order_index++] = i;
1705 file_offset = (file_offset + s->sh_addralign - 1) &
1706 ~(s->sh_addralign - 1);
1707 s->sh_offset = file_offset;
1708 if (s->sh_type != SHT_NOBITS)
1709 file_offset += s->sh_size;
1712 return file_offset;
1715 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
1716 Section *dynamic)
1718 ElfW(Phdr) *ph;
1720 /* if interpreter, then add corresponding program header */
1721 if (interp) {
1722 ph = &phdr[0];
1724 ph->p_type = PT_PHDR;
1725 ph->p_offset = sizeof(ElfW(Ehdr));
1726 ph->p_filesz = ph->p_memsz = phnum * sizeof(ElfW(Phdr));
1727 ph->p_vaddr = interp->sh_addr - ph->p_filesz;
1728 ph->p_paddr = ph->p_vaddr;
1729 ph->p_flags = PF_R | PF_X;
1730 ph->p_align = 4; /* interp->sh_addralign; */
1731 ph++;
1733 ph->p_type = PT_INTERP;
1734 ph->p_offset = interp->sh_offset;
1735 ph->p_vaddr = interp->sh_addr;
1736 ph->p_paddr = ph->p_vaddr;
1737 ph->p_filesz = interp->sh_size;
1738 ph->p_memsz = interp->sh_size;
1739 ph->p_flags = PF_R;
1740 ph->p_align = interp->sh_addralign;
1743 /* if dynamic section, then add corresponding program header */
1744 if (dynamic) {
1745 ph = &phdr[phnum - 1];
1747 ph->p_type = PT_DYNAMIC;
1748 ph->p_offset = dynamic->sh_offset;
1749 ph->p_vaddr = dynamic->sh_addr;
1750 ph->p_paddr = ph->p_vaddr;
1751 ph->p_filesz = dynamic->sh_size;
1752 ph->p_memsz = dynamic->sh_size;
1753 ph->p_flags = PF_R | PF_W;
1754 ph->p_align = dynamic->sh_addralign;
1758 /* Fill the dynamic section with tags describing the address and size of
1759 sections */
1760 static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf)
1762 Section *dynamic = dyninf->dynamic;
1764 /* put dynamic section entries */
1765 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1766 put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
1767 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1768 put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
1769 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1770 #if PTR_SIZE == 8
1771 put_dt(dynamic, DT_RELA, dyninf->rel_addr);
1772 put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
1773 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1774 #else
1775 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1776 put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
1777 put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size);
1778 put_dt(dynamic, DT_JMPREL, dyninf->rel_addr);
1779 put_dt(dynamic, DT_PLTREL, DT_REL);
1780 put_dt(dynamic, DT_REL, dyninf->bss_addr);
1781 put_dt(dynamic, DT_RELSZ, dyninf->bss_size);
1782 #else
1783 put_dt(dynamic, DT_REL, dyninf->rel_addr);
1784 put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
1785 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1786 #endif
1787 #endif
1788 if (s1->do_debug)
1789 put_dt(dynamic, DT_DEBUG, 0);
1790 put_dt(dynamic, DT_NULL, 0);
1793 /* Relocate remaining sections and symbols (that is those not related to
1794 dynamic linking) */
1795 static int final_sections_reloc(TCCState *s1)
1797 int i;
1798 Section *s;
1800 relocate_syms(s1, s1->symtab, 0);
1802 if (s1->nb_errors != 0)
1803 return -1;
1805 /* relocate sections */
1806 /* XXX: ignore sections with allocated relocations ? */
1807 for(i = 1; i < s1->nb_sections; i++) {
1808 s = s1->sections[i];
1809 #if defined(TCC_TARGET_I386) || defined(TCC_MUSL)
1810 if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
1811 /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC
1812 checking is removed */
1813 #else
1814 if (s->reloc && s != s1->got)
1815 /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */
1816 #endif
1817 relocate_section(s1, s);
1820 /* relocate relocation entries if the relocation tables are
1821 allocated in the executable */
1822 for(i = 1; i < s1->nb_sections; i++) {
1823 s = s1->sections[i];
1824 if ((s->sh_flags & SHF_ALLOC) &&
1825 s->sh_type == SHT_RELX) {
1826 relocate_rel(s1, s);
1829 return 0;
1832 /* Create an ELF file on disk.
1833 This function handle ELF specific layout requirements */
1834 static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
1835 int file_offset, int *sec_order)
1837 int i, shnum, offset, size, file_type;
1838 Section *s;
1839 ElfW(Ehdr) ehdr;
1840 ElfW(Shdr) shdr, *sh;
1842 file_type = s1->output_type;
1843 shnum = s1->nb_sections;
1845 memset(&ehdr, 0, sizeof(ehdr));
1847 if (phnum > 0) {
1848 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1849 ehdr.e_phnum = phnum;
1850 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1853 /* align to 4 */
1854 file_offset = (file_offset + 3) & -4;
1856 /* fill header */
1857 ehdr.e_ident[0] = ELFMAG0;
1858 ehdr.e_ident[1] = ELFMAG1;
1859 ehdr.e_ident[2] = ELFMAG2;
1860 ehdr.e_ident[3] = ELFMAG3;
1861 ehdr.e_ident[4] = ELFCLASSW;
1862 ehdr.e_ident[5] = ELFDATA2LSB;
1863 ehdr.e_ident[6] = EV_CURRENT;
1864 #if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
1865 /* FIXME: should set only for freebsd _target_, but we exclude only PE target */
1866 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1867 #endif
1868 #ifdef TCC_TARGET_ARM
1869 #ifdef TCC_ARM_EABI
1870 ehdr.e_ident[EI_OSABI] = 0;
1871 ehdr.e_flags = EF_ARM_EABI_VER4;
1872 if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
1873 ehdr.e_flags |= EF_ARM_HASENTRY;
1874 if (s1->float_abi == ARM_HARD_FLOAT)
1875 ehdr.e_flags |= EF_ARM_VFP_FLOAT;
1876 else
1877 ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
1878 #else
1879 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1880 #endif
1881 #endif
1882 switch(file_type) {
1883 default:
1884 case TCC_OUTPUT_EXE:
1885 ehdr.e_type = ET_EXEC;
1886 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
1887 break;
1888 case TCC_OUTPUT_DLL:
1889 ehdr.e_type = ET_DYN;
1890 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1891 break;
1892 case TCC_OUTPUT_OBJ:
1893 ehdr.e_type = ET_REL;
1894 break;
1896 ehdr.e_machine = EM_TCC_TARGET;
1897 ehdr.e_version = EV_CURRENT;
1898 ehdr.e_shoff = file_offset;
1899 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1900 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1901 ehdr.e_shnum = shnum;
1902 ehdr.e_shstrndx = shnum - 1;
1904 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1905 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1906 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1908 sort_syms(s1, symtab_section);
1909 for(i = 1; i < s1->nb_sections; i++) {
1910 s = s1->sections[sec_order[i]];
1911 if (s->sh_type != SHT_NOBITS) {
1912 while (offset < s->sh_offset) {
1913 fputc(0, f);
1914 offset++;
1916 size = s->sh_size;
1917 if (size)
1918 fwrite(s->data, 1, size, f);
1919 offset += size;
1923 /* output section headers */
1924 while (offset < ehdr.e_shoff) {
1925 fputc(0, f);
1926 offset++;
1929 for(i = 0; i < s1->nb_sections; i++) {
1930 sh = &shdr;
1931 memset(sh, 0, sizeof(ElfW(Shdr)));
1932 s = s1->sections[i];
1933 if (s) {
1934 sh->sh_name = s->sh_name;
1935 sh->sh_type = s->sh_type;
1936 sh->sh_flags = s->sh_flags;
1937 sh->sh_entsize = s->sh_entsize;
1938 sh->sh_info = s->sh_info;
1939 if (s->link)
1940 sh->sh_link = s->link->sh_num;
1941 sh->sh_addralign = s->sh_addralign;
1942 sh->sh_addr = s->sh_addr;
1943 sh->sh_offset = s->sh_offset;
1944 sh->sh_size = s->sh_size;
1946 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1950 /* Write an elf, coff or "binary" file */
1951 static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
1952 ElfW(Phdr) *phdr, int file_offset, int *sec_order)
1954 int fd, mode, file_type;
1955 FILE *f;
1957 file_type = s1->output_type;
1958 if (file_type == TCC_OUTPUT_OBJ)
1959 mode = 0666;
1960 else
1961 mode = 0777;
1962 unlink(filename);
1963 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1964 if (fd < 0) {
1965 tcc_error_noabort("could not write '%s'", filename);
1966 return -1;
1968 f = fdopen(fd, "wb");
1969 if (s1->verbose)
1970 printf("<- %s\n", filename);
1972 #ifdef TCC_TARGET_COFF
1973 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
1974 tcc_output_coff(s1, f);
1975 else
1976 #endif
1977 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1978 tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
1979 else
1980 tcc_output_binary(s1, f, sec_order);
1981 fclose(f);
1983 return 0;
1986 /* Sort section headers by assigned sh_addr, remove sections
1987 that we aren't going to output. */
1988 static void tidy_section_headers(TCCState *s1, int *sec_order)
1990 int i, nnew, l, *backmap;
1991 Section **snew, *s;
1992 ElfW(Sym) *sym;
1994 snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
1995 backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
1996 for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
1997 s = s1->sections[sec_order[i]];
1998 if (!i || s->sh_name) {
1999 backmap[sec_order[i]] = nnew;
2000 snew[nnew] = s;
2001 ++nnew;
2002 } else {
2003 backmap[sec_order[i]] = 0;
2004 snew[--l] = s;
2007 for (i = 0; i < nnew; i++) {
2008 s = snew[i];
2009 if (s) {
2010 s->sh_num = i;
2011 if (s->sh_type == SHT_RELX)
2012 s->sh_info = backmap[s->sh_info];
2016 for_each_elem(symtab_section, 1, sym, ElfW(Sym))
2017 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
2018 sym->st_shndx = backmap[sym->st_shndx];
2019 if( !s1->static_link ) {
2020 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
2021 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
2022 sym->st_shndx = backmap[sym->st_shndx];
2024 for (i = 0; i < s1->nb_sections; i++)
2025 sec_order[i] = i;
2026 tcc_free(s1->sections);
2027 s1->sections = snew;
2028 s1->nb_sections = nnew;
2029 tcc_free(backmap);
2032 /* Output an elf, coff or binary file */
2033 /* XXX: suppress unneeded sections */
2034 static int elf_output_file(TCCState *s1, const char *filename)
2036 int i, ret, phnum, shnum, file_type, file_offset, *sec_order;
2037 struct dyn_inf dyninf = {0};
2038 ElfW(Phdr) *phdr;
2039 ElfW(Sym) *sym;
2040 Section *strsec, *interp, *dynamic, *dynstr;
2041 int textrel;
2043 file_type = s1->output_type;
2044 s1->nb_errors = 0;
2045 ret = -1;
2046 phdr = NULL;
2047 sec_order = NULL;
2048 interp = dynamic = dynstr = NULL; /* avoid warning */
2049 textrel = 0;
2051 if (file_type != TCC_OUTPUT_OBJ) {
2052 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2053 tcc_add_runtime(s1);
2054 resolve_regular_syms();
2056 if (!s1->static_link) {
2057 if (file_type == TCC_OUTPUT_EXE) {
2058 char *ptr;
2059 /* allow override the dynamic loader */
2060 const char *elfint = getenv("LD_SO");
2061 if (elfint == NULL)
2062 elfint = DEFAULT_ELFINTERP(s1);
2063 /* add interpreter section only if executable */
2064 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
2065 interp->sh_addralign = 1;
2066 ptr = section_ptr_add(interp, 1 + strlen(elfint));
2067 strcpy(ptr, elfint);
2070 /* add dynamic symbol table */
2071 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
2072 ".dynstr",
2073 ".hash", SHF_ALLOC);
2074 dynstr = s1->dynsym->link;
2076 /* add dynamic section */
2077 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
2078 SHF_ALLOC | SHF_WRITE);
2079 dynamic->link = dynstr;
2080 dynamic->sh_entsize = sizeof(ElfW(Dyn));
2082 build_got(s1);
2084 if (file_type == TCC_OUTPUT_EXE) {
2085 bind_exe_dynsyms(s1);
2086 if (s1->nb_errors)
2087 goto the_end;
2088 bind_libs_dynsyms(s1);
2089 } else {
2090 /* shared library case: simply export all global symbols */
2091 export_global_syms(s1);
2094 build_got_entries(s1);
2095 } else {
2096 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
2097 if (sym->st_shndx == SHN_UNDEF
2098 && ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
2099 sym->st_info
2100 = ELFW(ST_INFO)(STB_GLOBAL, ELFW(ST_TYPE)(sym->st_info));
2105 /* we add a section for symbols */
2106 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
2107 put_elf_str(strsec, "");
2109 /* Allocate strings for section names */
2110 textrel = alloc_sec_names(s1, file_type, strsec);
2112 if (dynamic) {
2113 /* add a list of needed dlls */
2114 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2115 DLLReference *dllref = s1->loaded_dlls[i];
2116 if (dllref->level == 0)
2117 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
2120 if (s1->rpath)
2121 put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
2122 put_elf_str(dynstr, s1->rpath));
2124 if (file_type == TCC_OUTPUT_DLL) {
2125 if (s1->soname)
2126 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
2127 /* XXX: currently, since we do not handle PIC code, we
2128 must relocate the readonly segments */
2129 if (textrel)
2130 put_dt(dynamic, DT_TEXTREL, 0);
2133 if (s1->symbolic)
2134 put_dt(dynamic, DT_SYMBOLIC, 0);
2136 dyninf.dynamic = dynamic;
2137 dyninf.dynstr = dynstr;
2138 /* remember offset and reserve space for 2nd call below */
2139 dyninf.data_offset = dynamic->data_offset;
2140 fill_dynamic(s1, &dyninf);
2141 dynamic->sh_size = dynamic->data_offset;
2142 dynstr->sh_size = dynstr->data_offset;
2145 /* compute number of program headers */
2146 if (file_type == TCC_OUTPUT_OBJ)
2147 phnum = 0;
2148 else if (file_type == TCC_OUTPUT_DLL)
2149 phnum = 3;
2150 else if (s1->static_link)
2151 phnum = 2;
2152 else
2153 phnum = 5;
2155 /* allocate program segment headers */
2156 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
2158 /* compute number of sections */
2159 shnum = s1->nb_sections;
2161 /* this array is used to reorder sections in the output file */
2162 sec_order = tcc_malloc(sizeof(int) * shnum);
2163 sec_order[0] = 0;
2165 /* compute section to program header mapping */
2166 file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2167 sec_order);
2169 /* Fill remaining program header and finalize relocation related to dynamic
2170 linking. */
2171 if (file_type != TCC_OUTPUT_OBJ) {
2172 fill_unloadable_phdr(phdr, phnum, interp, dynamic);
2173 if (dynamic) {
2174 dynamic->data_offset = dyninf.data_offset;
2175 fill_dynamic(s1, &dyninf);
2177 /* put in GOT the dynamic section address and relocate PLT */
2178 write32le(s1->got->data, dynamic->sh_addr);
2179 if (file_type == TCC_OUTPUT_EXE
2180 || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
2181 relocate_plt(s1);
2183 /* relocate symbols in .dynsym now that final addresses are known */
2184 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
2185 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
2186 /* do symbol relocation */
2187 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
2192 /* if building executable or DLL, then relocate each section
2193 except the GOT which is already relocated */
2194 ret = final_sections_reloc(s1);
2195 if (ret)
2196 goto the_end;
2197 tidy_section_headers(s1, sec_order);
2199 /* Perform relocation to GOT or PLT entries */
2200 if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2201 fill_got(s1);
2202 else if (s1->got)
2203 fill_local_got_entries(s1);
2206 /* Create the ELF file with name 'filename' */
2207 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2208 s1->nb_sections = shnum;
2209 the_end:
2210 tcc_free(sec_order);
2211 tcc_free(phdr);
2212 return ret;
2215 LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
2217 int ret;
2218 #ifdef TCC_TARGET_PE
2219 if (s->output_type != TCC_OUTPUT_OBJ) {
2220 ret = pe_output_file(s, filename);
2221 } else
2222 #endif
2223 ret = elf_output_file(s, filename);
2224 return ret;
2227 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2229 void *data;
2231 data = tcc_malloc(size);
2232 lseek(fd, file_offset, SEEK_SET);
2233 read(fd, data, size);
2234 return data;
2237 typedef struct SectionMergeInfo {
2238 Section *s; /* corresponding existing section */
2239 unsigned long offset; /* offset of the new section in the existing section */
2240 uint8_t new_section; /* true if section 's' was added */
2241 uint8_t link_once; /* true if link once section */
2242 } SectionMergeInfo;
2244 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
2246 int size = read(fd, h, sizeof *h);
2247 if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
2248 if (h->e_type == ET_REL)
2249 return AFF_BINTYPE_REL;
2250 if (h->e_type == ET_DYN)
2251 return AFF_BINTYPE_DYN;
2252 } else if (size >= 8) {
2253 if (0 == memcmp(h, ARMAG, 8))
2254 return AFF_BINTYPE_AR;
2255 #ifdef TCC_TARGET_COFF
2256 if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC)
2257 return AFF_BINTYPE_C67;
2258 #endif
2260 return 0;
2263 /* load an object file and merge it with current files */
2264 /* XXX: handle correctly stab (debug) info */
2265 ST_FUNC int tcc_load_object_file(TCCState *s1,
2266 int fd, unsigned long file_offset)
2268 ElfW(Ehdr) ehdr;
2269 ElfW(Shdr) *shdr, *sh;
2270 int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed;
2271 unsigned char *strsec, *strtab;
2272 int *old_to_new_syms;
2273 char *sh_name, *name;
2274 SectionMergeInfo *sm_table, *sm;
2275 ElfW(Sym) *sym, *symtab;
2276 ElfW_Rel *rel;
2277 Section *s;
2279 int stab_index;
2280 int stabstr_index;
2282 stab_index = stabstr_index = 0;
2284 lseek(fd, file_offset, SEEK_SET);
2285 if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL)
2286 goto fail1;
2287 /* test CPU specific stuff */
2288 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2289 ehdr.e_machine != EM_TCC_TARGET) {
2290 fail1:
2291 tcc_error_noabort("invalid object file");
2292 return -1;
2294 /* read sections */
2295 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2296 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2297 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2299 /* load section names */
2300 sh = &shdr[ehdr.e_shstrndx];
2301 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2303 /* load symtab and strtab */
2304 old_to_new_syms = NULL;
2305 symtab = NULL;
2306 strtab = NULL;
2307 nb_syms = 0;
2308 seencompressed = 0;
2309 for(i = 1; i < ehdr.e_shnum; i++) {
2310 sh = &shdr[i];
2311 if (sh->sh_type == SHT_SYMTAB) {
2312 if (symtab) {
2313 tcc_error_noabort("object must contain only one symtab");
2314 fail:
2315 ret = -1;
2316 goto the_end;
2318 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2319 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2320 sm_table[i].s = symtab_section;
2322 /* now load strtab */
2323 sh = &shdr[sh->sh_link];
2324 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2326 if (sh->sh_flags & SHF_COMPRESSED)
2327 seencompressed = 1;
2330 /* now examine each section and try to merge its content with the
2331 ones in memory */
2332 for(i = 1; i < ehdr.e_shnum; i++) {
2333 /* no need to examine section name strtab */
2334 if (i == ehdr.e_shstrndx)
2335 continue;
2336 sh = &shdr[i];
2337 sh_name = (char *) strsec + sh->sh_name;
2338 /* ignore sections types we do not handle */
2339 if (sh->sh_type != SHT_PROGBITS &&
2340 sh->sh_type != SHT_RELX &&
2341 #ifdef TCC_ARM_EABI
2342 sh->sh_type != SHT_ARM_EXIDX &&
2343 #endif
2344 sh->sh_type != SHT_NOBITS &&
2345 sh->sh_type != SHT_PREINIT_ARRAY &&
2346 sh->sh_type != SHT_INIT_ARRAY &&
2347 sh->sh_type != SHT_FINI_ARRAY &&
2348 strcmp(sh_name, ".stabstr")
2350 continue;
2351 if (seencompressed
2352 && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1)
2353 || (sh->sh_type == SHT_RELX
2354 && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name,
2355 ".debug_", sizeof(".debug_")-1))))
2356 continue;
2357 if (sh->sh_addralign < 1)
2358 sh->sh_addralign = 1;
2359 /* find corresponding section, if any */
2360 for(j = 1; j < s1->nb_sections;j++) {
2361 s = s1->sections[j];
2362 if (!strcmp(s->name, sh_name)) {
2363 if (!strncmp(sh_name, ".gnu.linkonce",
2364 sizeof(".gnu.linkonce") - 1)) {
2365 /* if a 'linkonce' section is already present, we
2366 do not add it again. It is a little tricky as
2367 symbols can still be defined in
2368 it. */
2369 sm_table[i].link_once = 1;
2370 goto next;
2371 } else {
2372 goto found;
2376 /* not found: create new section */
2377 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
2378 /* take as much info as possible from the section. sh_link and
2379 sh_info will be updated later */
2380 s->sh_addralign = sh->sh_addralign;
2381 s->sh_entsize = sh->sh_entsize;
2382 sm_table[i].new_section = 1;
2383 found:
2384 if (sh->sh_type != s->sh_type) {
2385 tcc_error_noabort("invalid section type");
2386 goto fail;
2389 /* align start of section */
2390 offset = s->data_offset;
2392 if (0 == strcmp(sh_name, ".stab")) {
2393 stab_index = i;
2394 goto no_align;
2396 if (0 == strcmp(sh_name, ".stabstr")) {
2397 stabstr_index = i;
2398 goto no_align;
2401 size = sh->sh_addralign - 1;
2402 offset = (offset + size) & ~size;
2403 if (sh->sh_addralign > s->sh_addralign)
2404 s->sh_addralign = sh->sh_addralign;
2405 s->data_offset = offset;
2406 no_align:
2407 sm_table[i].offset = offset;
2408 sm_table[i].s = s;
2409 /* concatenate sections */
2410 size = sh->sh_size;
2411 if (sh->sh_type != SHT_NOBITS) {
2412 unsigned char *ptr;
2413 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2414 ptr = section_ptr_add(s, size);
2415 read(fd, ptr, size);
2416 } else {
2417 s->data_offset += size;
2419 next: ;
2422 /* gr relocate stab strings */
2423 if (stab_index && stabstr_index) {
2424 Stab_Sym *a, *b;
2425 unsigned o;
2426 s = sm_table[stab_index].s;
2427 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2428 b = (Stab_Sym *)(s->data + s->data_offset);
2429 o = sm_table[stabstr_index].offset;
2430 while (a < b)
2431 a->n_strx += o, a++;
2434 /* second short pass to update sh_link and sh_info fields of new
2435 sections */
2436 for(i = 1; i < ehdr.e_shnum; i++) {
2437 s = sm_table[i].s;
2438 if (!s || !sm_table[i].new_section)
2439 continue;
2440 sh = &shdr[i];
2441 if (sh->sh_link > 0)
2442 s->link = sm_table[sh->sh_link].s;
2443 if (sh->sh_type == SHT_RELX) {
2444 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2445 /* update backward link */
2446 s1->sections[s->sh_info]->reloc = s;
2449 sm = sm_table;
2451 /* resolve symbols */
2452 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2454 sym = symtab + 1;
2455 for(i = 1; i < nb_syms; i++, sym++) {
2456 if (sym->st_shndx != SHN_UNDEF &&
2457 sym->st_shndx < SHN_LORESERVE) {
2458 sm = &sm_table[sym->st_shndx];
2459 if (sm->link_once) {
2460 /* if a symbol is in a link once section, we use the
2461 already defined symbol. It is very important to get
2462 correct relocations */
2463 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2464 name = (char *) strtab + sym->st_name;
2465 sym_index = find_elf_sym(symtab_section, name);
2466 if (sym_index)
2467 old_to_new_syms[i] = sym_index;
2469 continue;
2471 /* if no corresponding section added, no need to add symbol */
2472 if (!sm->s)
2473 continue;
2474 /* convert section number */
2475 sym->st_shndx = sm->s->sh_num;
2476 /* offset value */
2477 sym->st_value += sm->offset;
2479 /* add symbol */
2480 name = (char *) strtab + sym->st_name;
2481 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
2482 sym->st_info, sym->st_other,
2483 sym->st_shndx, name);
2484 old_to_new_syms[i] = sym_index;
2487 /* third pass to patch relocation entries */
2488 for(i = 1; i < ehdr.e_shnum; i++) {
2489 s = sm_table[i].s;
2490 if (!s)
2491 continue;
2492 sh = &shdr[i];
2493 offset = sm_table[i].offset;
2494 switch(s->sh_type) {
2495 case SHT_RELX:
2496 /* take relocation offset information */
2497 offseti = sm_table[sh->sh_info].offset;
2498 for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) {
2499 int type;
2500 unsigned sym_index;
2501 /* convert symbol index */
2502 type = ELFW(R_TYPE)(rel->r_info);
2503 sym_index = ELFW(R_SYM)(rel->r_info);
2504 /* NOTE: only one symtab assumed */
2505 if (sym_index >= nb_syms)
2506 goto invalid_reloc;
2507 sym_index = old_to_new_syms[sym_index];
2508 /* ignore link_once in rel section. */
2509 if (!sym_index && !sm->link_once
2510 #ifdef TCC_TARGET_ARM
2511 && type != R_ARM_V4BX
2512 #endif
2514 invalid_reloc:
2515 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2516 i, strsec + sh->sh_name, rel->r_offset);
2517 goto fail;
2519 rel->r_info = ELFW(R_INFO)(sym_index, type);
2520 /* offset the relocation offset */
2521 rel->r_offset += offseti;
2522 #ifdef TCC_TARGET_ARM
2523 /* Jumps and branches from a Thumb code to a PLT entry need
2524 special handling since PLT entries are ARM code.
2525 Unconditional bl instructions referencing PLT entries are
2526 handled by converting these instructions into blx
2527 instructions. Other case of instructions referencing a PLT
2528 entry require to add a Thumb stub before the PLT entry to
2529 switch to ARM mode. We set bit plt_thumb_stub of the
2530 attribute of a symbol to indicate such a case. */
2531 if (type == R_ARM_THM_JUMP24)
2532 get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
2533 #endif
2535 break;
2536 default:
2537 break;
2541 ret = 0;
2542 the_end:
2543 tcc_free(symtab);
2544 tcc_free(strtab);
2545 tcc_free(old_to_new_syms);
2546 tcc_free(sm_table);
2547 tcc_free(strsec);
2548 tcc_free(shdr);
2549 return ret;
2552 typedef struct ArchiveHeader {
2553 char ar_name[16]; /* name of this member */
2554 char ar_date[12]; /* file mtime */
2555 char ar_uid[6]; /* owner uid; printed as decimal */
2556 char ar_gid[6]; /* owner gid; printed as decimal */
2557 char ar_mode[8]; /* file mode, printed as octal */
2558 char ar_size[10]; /* file size, printed as decimal */
2559 char ar_fmag[2]; /* should contain ARFMAG */
2560 } ArchiveHeader;
2562 static int get_be32(const uint8_t *b)
2564 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2567 static long get_be64(const uint8_t *b)
2569 long long ret = get_be32(b);
2570 ret = (ret << 32) | (unsigned)get_be32(b+4);
2571 return (long)ret;
2574 /* load only the objects which resolve undefined symbols */
2575 static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
2577 long i, bound, nsyms, sym_index, off, ret;
2578 uint8_t *data;
2579 const char *ar_names, *p;
2580 const uint8_t *ar_index;
2581 ElfW(Sym) *sym;
2583 data = tcc_malloc(size);
2584 if (read(fd, data, size) != size)
2585 goto fail;
2586 nsyms = entrysize == 4 ? get_be32(data) : get_be64(data);
2587 ar_index = data + entrysize;
2588 ar_names = (char *) ar_index + nsyms * entrysize;
2590 do {
2591 bound = 0;
2592 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2593 sym_index = find_elf_sym(symtab_section, p);
2594 if(sym_index) {
2595 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2596 if(sym->st_shndx == SHN_UNDEF) {
2597 off = (entrysize == 4
2598 ? get_be32(ar_index + i * 4)
2599 : get_be64(ar_index + i * 8))
2600 + sizeof(ArchiveHeader);
2601 ++bound;
2602 if(tcc_load_object_file(s1, fd, off) < 0) {
2603 fail:
2604 ret = -1;
2605 goto the_end;
2610 } while(bound);
2611 ret = 0;
2612 the_end:
2613 tcc_free(data);
2614 return ret;
2617 /* load a '.a' file */
2618 ST_FUNC int tcc_load_archive(TCCState *s1, int fd)
2620 ArchiveHeader hdr;
2621 char ar_size[11];
2622 char ar_name[17];
2623 char magic[8];
2624 int size, len, i;
2625 unsigned long file_offset;
2627 /* skip magic which was already checked */
2628 read(fd, magic, sizeof(magic));
2630 for(;;) {
2631 len = read(fd, &hdr, sizeof(hdr));
2632 if (len == 0)
2633 break;
2634 if (len != sizeof(hdr)) {
2635 tcc_error_noabort("invalid archive");
2636 return -1;
2638 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2639 ar_size[sizeof(hdr.ar_size)] = '\0';
2640 size = strtol(ar_size, NULL, 0);
2641 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2642 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2643 if (ar_name[i] != ' ')
2644 break;
2646 ar_name[i + 1] = '\0';
2647 file_offset = lseek(fd, 0, SEEK_CUR);
2648 /* align to even */
2649 size = (size + 1) & ~1;
2650 if (!strcmp(ar_name, "/")) {
2651 /* coff symbol table : we handle it */
2652 if(s1->alacarte_link)
2653 return tcc_load_alacarte(s1, fd, size, 4);
2654 } else if (!strcmp(ar_name, "/SYM64/")) {
2655 if(s1->alacarte_link)
2656 return tcc_load_alacarte(s1, fd, size, 8);
2657 } else {
2658 ElfW(Ehdr) ehdr;
2659 if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
2660 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2661 return -1;
2664 lseek(fd, file_offset + size, SEEK_SET);
2666 return 0;
2669 #ifndef TCC_TARGET_PE
2670 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2671 is referenced by the user (so it should be added as DT_NEEDED in
2672 the generated ELF file) */
2673 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2675 ElfW(Ehdr) ehdr;
2676 ElfW(Shdr) *shdr, *sh, *sh1;
2677 int i, j, nb_syms, nb_dts, sym_bind, ret;
2678 ElfW(Sym) *sym, *dynsym;
2679 ElfW(Dyn) *dt, *dynamic;
2680 unsigned char *dynstr;
2681 const char *name, *soname;
2682 DLLReference *dllref;
2684 read(fd, &ehdr, sizeof(ehdr));
2686 /* test CPU specific stuff */
2687 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2688 ehdr.e_machine != EM_TCC_TARGET) {
2689 tcc_error_noabort("bad architecture");
2690 return -1;
2693 /* read sections */
2694 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2696 /* load dynamic section and dynamic symbols */
2697 nb_syms = 0;
2698 nb_dts = 0;
2699 dynamic = NULL;
2700 dynsym = NULL; /* avoid warning */
2701 dynstr = NULL; /* avoid warning */
2702 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2703 switch(sh->sh_type) {
2704 case SHT_DYNAMIC:
2705 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2706 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2707 break;
2708 case SHT_DYNSYM:
2709 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2710 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2711 sh1 = &shdr[sh->sh_link];
2712 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2713 break;
2714 default:
2715 break;
2719 /* compute the real library name */
2720 soname = tcc_basename(filename);
2722 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2723 if (dt->d_tag == DT_SONAME) {
2724 soname = (char *) dynstr + dt->d_un.d_val;
2728 /* if the dll is already loaded, do not load it */
2729 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2730 dllref = s1->loaded_dlls[i];
2731 if (!strcmp(soname, dllref->name)) {
2732 /* but update level if needed */
2733 if (level < dllref->level)
2734 dllref->level = level;
2735 ret = 0;
2736 goto the_end;
2740 /* add the dll and its level */
2741 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2742 dllref->level = level;
2743 strcpy(dllref->name, soname);
2744 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2746 /* add dynamic symbols in dynsym_section */
2747 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2748 sym_bind = ELFW(ST_BIND)(sym->st_info);
2749 if (sym_bind == STB_LOCAL)
2750 continue;
2751 name = (char *) dynstr + sym->st_name;
2752 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2753 sym->st_info, sym->st_other, sym->st_shndx, name);
2756 /* load all referenced DLLs */
2757 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2758 switch(dt->d_tag) {
2759 case DT_NEEDED:
2760 name = (char *) dynstr + dt->d_un.d_val;
2761 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2762 dllref = s1->loaded_dlls[j];
2763 if (!strcmp(name, dllref->name))
2764 goto already_loaded;
2766 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2767 tcc_error_noabort("referenced dll '%s' not found", name);
2768 ret = -1;
2769 goto the_end;
2771 already_loaded:
2772 break;
2775 ret = 0;
2776 the_end:
2777 tcc_free(dynstr);
2778 tcc_free(dynsym);
2779 tcc_free(dynamic);
2780 tcc_free(shdr);
2781 return ret;
2784 #define LD_TOK_NAME 256
2785 #define LD_TOK_EOF (-1)
2787 /* return next ld script token */
2788 static int ld_next(TCCState *s1, char *name, int name_size)
2790 int c;
2791 char *q;
2793 redo:
2794 switch(ch) {
2795 case ' ':
2796 case '\t':
2797 case '\f':
2798 case '\v':
2799 case '\r':
2800 case '\n':
2801 inp();
2802 goto redo;
2803 case '/':
2804 minp();
2805 if (ch == '*') {
2806 file->buf_ptr = parse_comment(file->buf_ptr);
2807 ch = file->buf_ptr[0];
2808 goto redo;
2809 } else {
2810 q = name;
2811 *q++ = '/';
2812 goto parse_name;
2814 break;
2815 case '\\':
2816 ch = handle_eob();
2817 if (ch != '\\')
2818 goto redo;
2819 /* fall through */
2820 /* case 'a' ... 'z': */
2821 case 'a':
2822 case 'b':
2823 case 'c':
2824 case 'd':
2825 case 'e':
2826 case 'f':
2827 case 'g':
2828 case 'h':
2829 case 'i':
2830 case 'j':
2831 case 'k':
2832 case 'l':
2833 case 'm':
2834 case 'n':
2835 case 'o':
2836 case 'p':
2837 case 'q':
2838 case 'r':
2839 case 's':
2840 case 't':
2841 case 'u':
2842 case 'v':
2843 case 'w':
2844 case 'x':
2845 case 'y':
2846 case 'z':
2847 /* case 'A' ... 'z': */
2848 case 'A':
2849 case 'B':
2850 case 'C':
2851 case 'D':
2852 case 'E':
2853 case 'F':
2854 case 'G':
2855 case 'H':
2856 case 'I':
2857 case 'J':
2858 case 'K':
2859 case 'L':
2860 case 'M':
2861 case 'N':
2862 case 'O':
2863 case 'P':
2864 case 'Q':
2865 case 'R':
2866 case 'S':
2867 case 'T':
2868 case 'U':
2869 case 'V':
2870 case 'W':
2871 case 'X':
2872 case 'Y':
2873 case 'Z':
2874 case '_':
2875 case '.':
2876 case '$':
2877 case '~':
2878 q = name;
2879 parse_name:
2880 for(;;) {
2881 if (!((ch >= 'a' && ch <= 'z') ||
2882 (ch >= 'A' && ch <= 'Z') ||
2883 (ch >= '0' && ch <= '9') ||
2884 strchr("/.-_+=$:\\,~", ch)))
2885 break;
2886 if ((q - name) < name_size - 1) {
2887 *q++ = ch;
2889 minp();
2891 *q = '\0';
2892 c = LD_TOK_NAME;
2893 break;
2894 case CH_EOF:
2895 c = LD_TOK_EOF;
2896 break;
2897 default:
2898 c = ch;
2899 inp();
2900 break;
2902 return c;
2905 static int ld_add_file(TCCState *s1, const char filename[])
2907 if (filename[0] == '/') {
2908 if (CONFIG_SYSROOT[0] == '\0'
2909 && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0)
2910 return 0;
2911 filename = tcc_basename(filename);
2913 return tcc_add_dll(s1, filename, 0);
2916 static inline int new_undef_syms(void)
2918 int ret = 0;
2919 ret = new_undef_sym;
2920 new_undef_sym = 0;
2921 return ret;
2924 static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
2926 char filename[1024], libname[1024];
2927 int t, group, nblibs = 0, ret = 0;
2928 char **libs = NULL;
2930 group = !strcmp(cmd, "GROUP");
2931 if (!as_needed)
2932 new_undef_syms();
2933 t = ld_next(s1, filename, sizeof(filename));
2934 if (t != '(')
2935 expect("(");
2936 t = ld_next(s1, filename, sizeof(filename));
2937 for(;;) {
2938 libname[0] = '\0';
2939 if (t == LD_TOK_EOF) {
2940 tcc_error_noabort("unexpected end of file");
2941 ret = -1;
2942 goto lib_parse_error;
2943 } else if (t == ')') {
2944 break;
2945 } else if (t == '-') {
2946 t = ld_next(s1, filename, sizeof(filename));
2947 if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
2948 tcc_error_noabort("library name expected");
2949 ret = -1;
2950 goto lib_parse_error;
2952 pstrcpy(libname, sizeof libname, &filename[1]);
2953 if (s1->static_link) {
2954 snprintf(filename, sizeof filename, "lib%s.a", libname);
2955 } else {
2956 snprintf(filename, sizeof filename, "lib%s.so", libname);
2958 } else if (t != LD_TOK_NAME) {
2959 tcc_error_noabort("filename expected");
2960 ret = -1;
2961 goto lib_parse_error;
2963 if (!strcmp(filename, "AS_NEEDED")) {
2964 ret = ld_add_file_list(s1, cmd, 1);
2965 if (ret)
2966 goto lib_parse_error;
2967 } else {
2968 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2969 if (!as_needed) {
2970 ret = ld_add_file(s1, filename);
2971 if (ret)
2972 goto lib_parse_error;
2973 if (group) {
2974 /* Add the filename *and* the libname to avoid future conversions */
2975 dynarray_add(&libs, &nblibs, tcc_strdup(filename));
2976 if (libname[0] != '\0')
2977 dynarray_add(&libs, &nblibs, tcc_strdup(libname));
2981 t = ld_next(s1, filename, sizeof(filename));
2982 if (t == ',') {
2983 t = ld_next(s1, filename, sizeof(filename));
2986 if (group && !as_needed) {
2987 while (new_undef_syms()) {
2988 int i;
2990 for (i = 0; i < nblibs; i ++)
2991 ld_add_file(s1, libs[i]);
2994 lib_parse_error:
2995 dynarray_reset(&libs, &nblibs);
2996 return ret;
2999 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
3000 files */
3001 ST_FUNC int tcc_load_ldscript(TCCState *s1)
3003 char cmd[64];
3004 char filename[1024];
3005 int t, ret;
3007 ch = handle_eob();
3008 for(;;) {
3009 t = ld_next(s1, cmd, sizeof(cmd));
3010 if (t == LD_TOK_EOF)
3011 return 0;
3012 else if (t != LD_TOK_NAME)
3013 return -1;
3014 if (!strcmp(cmd, "INPUT") ||
3015 !strcmp(cmd, "GROUP")) {
3016 ret = ld_add_file_list(s1, cmd, 0);
3017 if (ret)
3018 return ret;
3019 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
3020 !strcmp(cmd, "TARGET")) {
3021 /* ignore some commands */
3022 t = ld_next(s1, cmd, sizeof(cmd));
3023 if (t != '(')
3024 expect("(");
3025 for(;;) {
3026 t = ld_next(s1, filename, sizeof(filename));
3027 if (t == LD_TOK_EOF) {
3028 tcc_error_noabort("unexpected end of file");
3029 return -1;
3030 } else if (t == ')') {
3031 break;
3034 } else {
3035 return -1;
3038 return 0;
3040 #endif /* !TCC_TARGET_PE */