the R_X86_64_GOTOFF64 relocation was missing
[tinycc.git] / tccelf.c
blobfad23b84e3ff118cf79aa0ee0be2a40d692cbc79
1 /*
2 * ELF file handling for TCC
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /* Define this to get some debug output during relocation processing. */
24 #undef DEBUG_RELOC
26 /********************************************************/
27 /* global variables */
29 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
30 ST_DATA Section *common_section;
31 ST_DATA Section *cur_text_section; /* current section where function code is generated */
32 #ifdef CONFIG_TCC_ASM
33 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
34 #endif
35 #ifdef CONFIG_TCC_BCHECK
36 /* bound check related sections */
37 ST_DATA Section *bounds_section; /* contains global data bound description */
38 ST_DATA Section *lbounds_section; /* contains local data bound description */
39 #endif
40 /* symbol sections */
41 ST_DATA Section *symtab_section, *strtab_section;
42 /* debug sections */
43 ST_DATA Section *stab_section, *stabstr_section;
45 /* XXX: avoid static variable */
46 static int new_undef_sym = 0; /* Is there a new undefined sym since last new_undef_sym() */
48 /* ------------------------------------------------------------------------- */
50 ST_FUNC void tccelf_new(TCCState *s)
52 /* no section zero */
53 dynarray_add(&s->sections, &s->nb_sections, NULL);
55 /* create standard sections */
56 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
57 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
58 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
59 common_section = new_section(s, ".common", SHT_NOBITS, SHF_PRIVATE);
60 common_section->sh_num = SHN_COMMON;
62 /* symbols are always generated for linking stage */
63 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
64 ".strtab",
65 ".hashtab", SHF_PRIVATE);
66 strtab_section = symtab_section->link;
67 s->symtab = symtab_section;
69 /* private symbol table for dynamic symbols */
70 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
71 ".dynstrtab",
72 ".dynhashtab", SHF_PRIVATE);
73 get_sym_attr(s, 0, 1);
76 #ifdef CONFIG_TCC_BCHECK
77 ST_FUNC void tccelf_bounds_new(TCCState *s)
79 /* create bounds sections */
80 bounds_section = new_section(s, ".bounds",
81 SHT_PROGBITS, SHF_ALLOC);
82 lbounds_section = new_section(s, ".lbounds",
83 SHT_PROGBITS, SHF_ALLOC);
85 #endif
87 ST_FUNC void tccelf_stab_new(TCCState *s)
89 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
90 stab_section->sh_entsize = sizeof(Stab_Sym);
91 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
92 put_elf_str(stabstr_section, "");
93 stab_section->link = stabstr_section;
94 /* put first entry */
95 put_stabs("", 0, 0, 0, 0);
98 static void free_section(Section *s)
100 tcc_free(s->data);
103 ST_FUNC void tccelf_delete(TCCState *s1)
105 int i;
107 /* free all sections */
108 for(i = 1; i < s1->nb_sections; i++)
109 free_section(s1->sections[i]);
110 dynarray_reset(&s1->sections, &s1->nb_sections);
112 for(i = 0; i < s1->nb_priv_sections; i++)
113 free_section(s1->priv_sections[i]);
114 dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
116 /* free any loaded DLLs */
117 #ifdef TCC_IS_NATIVE
118 for ( i = 0; i < s1->nb_loaded_dlls; i++) {
119 DLLReference *ref = s1->loaded_dlls[i];
120 if ( ref->handle )
121 # ifdef _WIN32
122 FreeLibrary((HMODULE)ref->handle);
123 # else
124 dlclose(ref->handle);
125 # endif
127 #endif
128 /* free loaded dlls array */
129 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
130 tcc_free(s1->sym_attrs);
133 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
135 Section *sec;
137 sec = tcc_mallocz(sizeof(Section) + strlen(name));
138 strcpy(sec->name, name);
139 sec->sh_type = sh_type;
140 sec->sh_flags = sh_flags;
141 switch(sh_type) {
142 case SHT_HASH:
143 case SHT_REL:
144 case SHT_RELA:
145 case SHT_DYNSYM:
146 case SHT_SYMTAB:
147 case SHT_DYNAMIC:
148 sec->sh_addralign = 4;
149 break;
150 case SHT_STRTAB:
151 sec->sh_addralign = 1;
152 break;
153 default:
154 sec->sh_addralign = PTR_SIZE; /* gcc/pcc default alignment */
155 break;
158 if (sh_flags & SHF_PRIVATE) {
159 dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, sec);
160 } else {
161 sec->sh_num = s1->nb_sections;
162 dynarray_add(&s1->sections, &s1->nb_sections, sec);
165 return sec;
168 ST_FUNC Section *new_symtab(TCCState *s1,
169 const char *symtab_name, int sh_type, int sh_flags,
170 const char *strtab_name,
171 const char *hash_name, int hash_sh_flags)
173 Section *symtab, *strtab, *hash;
174 int *ptr, nb_buckets;
176 symtab = new_section(s1, symtab_name, sh_type, sh_flags);
177 symtab->sh_entsize = sizeof(ElfW(Sym));
178 strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
179 put_elf_str(strtab, "");
180 symtab->link = strtab;
181 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
183 nb_buckets = 1;
185 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
186 hash->sh_entsize = sizeof(int);
187 symtab->hash = hash;
188 hash->link = symtab;
190 ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
191 ptr[0] = nb_buckets;
192 ptr[1] = 1;
193 memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
194 return symtab;
197 /* realloc section and set its content to zero */
198 ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
200 unsigned long size;
201 unsigned char *data;
203 size = sec->data_allocated;
204 if (size == 0)
205 size = 1;
206 while (size < new_size)
207 size = size * 2;
208 data = tcc_realloc(sec->data, size);
209 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
210 sec->data = data;
211 sec->data_allocated = size;
214 /* reserve at least 'size' bytes aligned per 'align' in section
215 'sec' from current offset, and return the aligned offset */
216 ST_FUNC size_t section_add(Section *sec, addr_t size, int align)
218 size_t offset, offset1;
220 offset = (sec->data_offset + align - 1) & -align;
221 offset1 = offset + size;
222 if (sec->sh_type != SHT_NOBITS && offset1 > sec->data_allocated)
223 section_realloc(sec, offset1);
224 sec->data_offset = offset1;
225 if (align > sec->sh_addralign)
226 sec->sh_addralign = align;
227 return offset;
230 /* reserve at least 'size' bytes in section 'sec' from
231 sec->data_offset. */
232 ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
234 size_t offset = section_add(sec, size, 1);
235 return sec->data + offset;
238 /* reserve at least 'size' bytes from section start */
239 ST_FUNC void section_reserve(Section *sec, unsigned long size)
241 if (size > sec->data_allocated)
242 section_realloc(sec, size);
243 if (size > sec->data_offset)
244 sec->data_offset = size;
247 /* return a reference to a section, and create it if it does not
248 exists */
249 ST_FUNC Section *find_section(TCCState *s1, const char *name)
251 Section *sec;
252 int i;
253 for(i = 1; i < s1->nb_sections; i++) {
254 sec = s1->sections[i];
255 if (!strcmp(name, sec->name))
256 return sec;
258 /* sections are created as PROGBITS */
259 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
262 /* ------------------------------------------------------------------------- */
264 ST_FUNC int put_elf_str(Section *s, const char *sym)
266 int offset, len;
267 char *ptr;
269 len = strlen(sym) + 1;
270 offset = s->data_offset;
271 ptr = section_ptr_add(s, len);
272 memcpy(ptr, sym, len);
273 return offset;
276 /* elf symbol hashing function */
277 static unsigned long elf_hash(const unsigned char *name)
279 unsigned long h = 0, g;
281 while (*name) {
282 h = (h << 4) + *name++;
283 g = h & 0xf0000000;
284 if (g)
285 h ^= g >> 24;
286 h &= ~g;
288 return h;
291 /* rebuild hash table of section s */
292 /* NOTE: we do factorize the hash table code to go faster */
293 static void rebuild_hash(Section *s, unsigned int nb_buckets)
295 ElfW(Sym) *sym;
296 int *ptr, *hash, nb_syms, sym_index, h;
297 unsigned char *strtab;
299 strtab = s->link->data;
300 nb_syms = s->data_offset / sizeof(ElfW(Sym));
302 s->hash->data_offset = 0;
303 ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
304 ptr[0] = nb_buckets;
305 ptr[1] = nb_syms;
306 ptr += 2;
307 hash = ptr;
308 memset(hash, 0, (nb_buckets + 1) * sizeof(int));
309 ptr += nb_buckets + 1;
311 sym = (ElfW(Sym) *)s->data + 1;
312 for(sym_index = 1; sym_index < nb_syms; sym_index++) {
313 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
314 h = elf_hash(strtab + sym->st_name) % nb_buckets;
315 *ptr = hash[h];
316 hash[h] = sym_index;
317 } else {
318 *ptr = 0;
320 ptr++;
321 sym++;
325 /* return the symbol number */
326 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
327 int info, int other, int shndx, const char *name)
329 int name_offset, sym_index;
330 int nbuckets, h;
331 ElfW(Sym) *sym;
332 Section *hs;
334 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
335 if (name)
336 name_offset = put_elf_str(s->link, name);
337 else
338 name_offset = 0;
339 /* XXX: endianness */
340 sym->st_name = name_offset;
341 sym->st_value = value;
342 sym->st_size = size;
343 sym->st_info = info;
344 sym->st_other = other;
345 sym->st_shndx = shndx;
346 sym_index = sym - (ElfW(Sym) *)s->data;
347 hs = s->hash;
348 if (hs) {
349 int *ptr, *base;
350 ptr = section_ptr_add(hs, sizeof(int));
351 base = (int *)hs->data;
352 /* only add global or weak symbols */
353 if (ELFW(ST_BIND)(info) != STB_LOCAL) {
354 /* add another hashing entry */
355 nbuckets = base[0];
356 h = elf_hash((unsigned char *) name) % nbuckets;
357 *ptr = base[2 + h];
358 base[2 + h] = sym_index;
359 base[1]++;
360 /* we resize the hash table */
361 hs->nb_hashed_syms++;
362 if (hs->nb_hashed_syms > 2 * nbuckets) {
363 rebuild_hash(s, 2 * nbuckets);
365 } else {
366 *ptr = 0;
367 base[1]++;
370 return sym_index;
373 /* find global ELF symbol 'name' and return its index. Return 0 if not
374 found. */
375 ST_FUNC int find_elf_sym(Section *s, const char *name)
377 ElfW(Sym) *sym;
378 Section *hs;
379 int nbuckets, sym_index, h;
380 const char *name1;
382 hs = s->hash;
383 if (!hs)
384 return 0;
385 nbuckets = ((int *)hs->data)[0];
386 h = elf_hash((unsigned char *) name) % nbuckets;
387 sym_index = ((int *)hs->data)[2 + h];
388 while (sym_index != 0) {
389 sym = &((ElfW(Sym) *)s->data)[sym_index];
390 name1 = (char *) s->link->data + sym->st_name;
391 if (!strcmp(name, name1))
392 return sym_index;
393 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
395 return 0;
398 /* return elf symbol value, signal error if 'err' is nonzero */
399 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err)
401 int sym_index;
402 ElfW(Sym) *sym;
404 sym_index = find_elf_sym(s->symtab, name);
405 sym = &((ElfW(Sym) *)s->symtab->data)[sym_index];
406 if (!sym_index || sym->st_shndx == SHN_UNDEF) {
407 if (err)
408 tcc_error("%s not defined", name);
409 return 0;
411 return sym->st_value;
414 /* return elf symbol value */
415 LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
417 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0);
420 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
421 /* return elf symbol value or error */
422 ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)
424 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 1);
426 #endif
428 /* add an elf symbol : check if it is already defined and patch
429 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
430 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
431 int info, int other, int shndx, const char *name)
433 ElfW(Sym) *esym;
434 int sym_bind, sym_index, sym_type, esym_bind;
435 unsigned char sym_vis, esym_vis, new_vis;
437 sym_bind = ELFW(ST_BIND)(info);
438 sym_type = ELFW(ST_TYPE)(info);
439 sym_vis = ELFW(ST_VISIBILITY)(other);
441 sym_index = find_elf_sym(s, name);
442 esym = &((ElfW(Sym) *)s->data)[sym_index];
443 if (sym_index && esym->st_value == value && esym->st_size == size
444 && esym->st_info == info && esym->st_other == other
445 && esym->st_shndx == shndx)
446 return sym_index;
448 if (sym_bind != STB_LOCAL) {
449 /* we search global or weak symbols */
450 if (!sym_index)
451 goto do_def;
452 if (esym->st_shndx != SHN_UNDEF) {
453 esym_bind = ELFW(ST_BIND)(esym->st_info);
454 /* propagate the most constraining visibility */
455 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
456 esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
457 if (esym_vis == STV_DEFAULT) {
458 new_vis = sym_vis;
459 } else if (sym_vis == STV_DEFAULT) {
460 new_vis = esym_vis;
461 } else {
462 new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
464 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
465 | new_vis;
466 other = esym->st_other; /* in case we have to patch esym */
467 if (shndx == SHN_UNDEF) {
468 /* ignore adding of undefined symbol if the
469 corresponding symbol is already defined */
470 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
471 /* global overrides weak, so patch */
472 goto do_patch;
473 } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
474 /* weak is ignored if already global */
475 } else if (sym_bind == STB_WEAK && esym_bind == STB_WEAK) {
476 /* keep first-found weak definition, ignore subsequents */
477 } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
478 /* ignore hidden symbols after */
479 } else if ((esym->st_shndx == SHN_COMMON
480 || esym->st_shndx == bss_section->sh_num)
481 && (shndx < SHN_LORESERVE
482 && shndx != bss_section->sh_num)) {
483 /* data symbol gets precedence over common/bss */
484 goto do_patch;
485 } else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
486 /* data symbol keeps precedence over common/bss */
487 } else if (s == tcc_state->dynsymtab_section) {
488 /* we accept that two DLL define the same symbol */
489 } else {
490 #if 0
491 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
492 sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
493 #endif
494 tcc_error_noabort("'%s' defined twice", name);
496 } else {
497 do_patch:
498 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
499 esym->st_shndx = shndx;
500 new_undef_sym = 1;
501 esym->st_value = value;
502 esym->st_size = size;
503 esym->st_other = other;
505 } else {
506 do_def:
507 sym_index = put_elf_sym(s, value, size,
508 ELFW(ST_INFO)(sym_bind, sym_type), other,
509 shndx, name);
511 return sym_index;
514 /* put relocation */
515 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
516 int type, int symbol, addr_t addend)
518 char buf[256];
519 Section *sr;
520 ElfW_Rel *rel;
522 sr = s->reloc;
523 if (!sr) {
524 /* if no relocation section, create it */
525 snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
526 /* if the symtab is allocated, then we consider the relocation
527 are also */
528 sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
529 sr->sh_entsize = sizeof(ElfW_Rel);
530 sr->link = symtab;
531 sr->sh_info = s->sh_num;
532 s->reloc = sr;
534 rel = section_ptr_add(sr, sizeof(ElfW_Rel));
535 rel->r_offset = offset;
536 rel->r_info = ELFW(R_INFO)(symbol, type);
537 #if SHT_RELX == SHT_RELA
538 rel->r_addend = addend;
539 #else
540 if (addend)
541 tcc_error("non-zero addend on REL architecture");
542 #endif
545 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
546 int type, int symbol)
548 put_elf_reloca(symtab, s, offset, type, symbol, 0);
551 /* Remove relocations for section S->reloc starting at oldrelocoffset
552 that are to the same place, retaining the last of them. As side effect
553 the relocations are sorted. Possibly reduces the number of relocs. */
554 ST_FUNC void squeeze_multi_relocs(Section *s, size_t oldrelocoffset)
556 Section *sr = s->reloc;
557 ElfW_Rel *r, *dest;
558 ssize_t a;
559 ElfW(Addr) addr;
561 if (oldrelocoffset + sizeof(*r) >= sr->data_offset)
562 return;
563 /* The relocs we're dealing with are the result of initializer parsing.
564 So they will be mostly in order and there aren't many of them.
565 Secondly we need a stable sort (which qsort isn't). We use
566 a simple insertion sort. */
567 for (a = oldrelocoffset + sizeof(*r); a < sr->data_offset; a += sizeof(*r)) {
568 ssize_t i = a - sizeof(*r);
569 addr = ((ElfW_Rel*)(sr->data + a))->r_offset;
570 for (; i >= (ssize_t)oldrelocoffset &&
571 ((ElfW_Rel*)(sr->data + i))->r_offset > addr; i -= sizeof(*r)) {
572 ElfW_Rel tmp = *(ElfW_Rel*)(sr->data + a);
573 *(ElfW_Rel*)(sr->data + a) = *(ElfW_Rel*)(sr->data + i);
574 *(ElfW_Rel*)(sr->data + i) = tmp;
578 r = (ElfW_Rel*)(sr->data + oldrelocoffset);
579 dest = r;
580 for (; r < (ElfW_Rel*)(sr->data + sr->data_offset); r++) {
581 if (dest->r_offset != r->r_offset)
582 dest++;
583 *dest = *r;
585 sr->data_offset = (unsigned char*)dest - sr->data + sizeof(*r);
588 /* put stab debug information */
590 ST_FUNC void put_stabs(const char *str, int type, int other, int desc,
591 unsigned long value)
593 Stab_Sym *sym;
595 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
596 if (str) {
597 sym->n_strx = put_elf_str(stabstr_section, str);
598 } else {
599 sym->n_strx = 0;
601 sym->n_type = type;
602 sym->n_other = other;
603 sym->n_desc = desc;
604 sym->n_value = value;
607 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc,
608 unsigned long value, Section *sec, int sym_index)
610 put_stabs(str, type, other, desc, value);
611 put_elf_reloc(symtab_section, stab_section,
612 stab_section->data_offset - sizeof(unsigned int),
613 R_DATA_32, sym_index);
616 ST_FUNC void put_stabn(int type, int other, int desc, int value)
618 put_stabs(NULL, type, other, desc, value);
621 ST_FUNC void put_stabd(int type, int other, int desc)
623 put_stabs(NULL, type, other, desc, 0);
626 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
628 int n;
629 struct sym_attr *tab;
631 if (index >= s1->nb_sym_attrs) {
632 if (!alloc)
633 return s1->sym_attrs;
634 /* find immediately bigger power of 2 and reallocate array */
635 n = 1;
636 while (index >= n)
637 n *= 2;
638 tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
639 s1->sym_attrs = tab;
640 memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
641 (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
642 s1->nb_sym_attrs = n;
644 return &s1->sym_attrs[index];
647 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
648 using variable <elem> */
649 #define for_each_elem(sec, startoff, elem, type) \
650 for (elem = (type *) sec->data + startoff; \
651 elem < (type *) (sec->data + sec->data_offset); elem++)
653 /* In an ELF file symbol table, the local symbols must appear below
654 the global and weak ones. Since TCC cannot sort it while generating
655 the code, we must do it after. All the relocation tables are also
656 modified to take into account the symbol table sorting */
657 static void sort_syms(TCCState *s1, Section *s)
659 int *old_to_new_syms;
660 ElfW(Sym) *new_syms;
661 int nb_syms, i;
662 ElfW(Sym) *p, *q;
663 ElfW_Rel *rel;
664 Section *sr;
665 int type, sym_index;
667 nb_syms = s->data_offset / sizeof(ElfW(Sym));
668 new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
669 old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
671 /* first pass for local symbols */
672 p = (ElfW(Sym) *)s->data;
673 q = new_syms;
674 for(i = 0; i < nb_syms; i++) {
675 if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
676 old_to_new_syms[i] = q - new_syms;
677 *q++ = *p;
679 p++;
681 /* save the number of local symbols in section header */
682 s->sh_info = q - new_syms;
684 /* then second pass for non local symbols */
685 p = (ElfW(Sym) *)s->data;
686 for(i = 0; i < nb_syms; i++) {
687 if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
688 old_to_new_syms[i] = q - new_syms;
689 *q++ = *p;
691 p++;
694 /* we copy the new symbols to the old */
695 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
696 tcc_free(new_syms);
698 /* now we modify all the relocations */
699 for(i = 1; i < s1->nb_sections; i++) {
700 sr = s1->sections[i];
701 if (sr->sh_type == SHT_RELX && sr->link == s) {
702 for_each_elem(sr, 0, rel, ElfW_Rel) {
703 sym_index = ELFW(R_SYM)(rel->r_info);
704 type = ELFW(R_TYPE)(rel->r_info);
705 sym_index = old_to_new_syms[sym_index];
706 rel->r_info = ELFW(R_INFO)(sym_index, type);
711 tcc_free(old_to_new_syms);
714 /* relocate common symbols in the .bss section */
715 ST_FUNC void relocate_common_syms(void)
717 ElfW(Sym) *sym;
719 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
720 if (sym->st_shndx == SHN_COMMON) {
721 /* symbol alignment is in st_value for SHN_COMMONs */
722 sym->st_value = section_add(bss_section, sym->st_size,
723 sym->st_value);
724 sym->st_shndx = bss_section->sh_num;
729 /* relocate symbol table, resolve undefined symbols if do_resolve is
730 true and output error if undefined symbol. */
731 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
733 ElfW(Sym) *sym;
734 int sym_bind, sh_num;
735 const char *name;
737 for_each_elem(symtab, 1, sym, ElfW(Sym)) {
738 sh_num = sym->st_shndx;
739 if (sh_num == SHN_UNDEF) {
740 name = (char *) strtab_section->data + sym->st_name;
741 /* Use ld.so to resolve symbol for us (for tcc -run) */
742 if (do_resolve) {
743 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
744 void *addr = dlsym(RTLD_DEFAULT, name);
745 if (addr) {
746 sym->st_value = (addr_t) addr;
747 #ifdef DEBUG_RELOC
748 printf ("relocate_sym: %s -> 0x%lx\n", name, sym->st_value);
749 #endif
750 goto found;
752 #endif
753 /* if dynamic symbol exist, it will be used in relocate_section */
754 } else if (s1->dynsym && find_elf_sym(s1->dynsym, name))
755 goto found;
756 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
757 it */
758 if (!strcmp(name, "_fp_hw"))
759 goto found;
760 /* only weak symbols are accepted to be undefined. Their
761 value is zero */
762 sym_bind = ELFW(ST_BIND)(sym->st_info);
763 if (sym_bind == STB_WEAK)
764 sym->st_value = 0;
765 else
766 tcc_error_noabort("undefined symbol '%s'", name);
767 } else if (sh_num < SHN_LORESERVE) {
768 /* add section base */
769 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
771 found: ;
775 /* relocate a given section (CPU dependent) by applying the relocations
776 in the associated relocation section */
777 ST_FUNC void relocate_section(TCCState *s1, Section *s)
779 Section *sr = s->reloc;
780 ElfW_Rel *rel;
781 ElfW(Sym) *sym;
782 int type, sym_index;
783 unsigned char *ptr;
784 addr_t tgt, addr;
786 relocate_init(sr);
788 for_each_elem(sr, 0, rel, ElfW_Rel) {
789 ptr = s->data + rel->r_offset;
790 sym_index = ELFW(R_SYM)(rel->r_info);
791 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
792 type = ELFW(R_TYPE)(rel->r_info);
793 tgt = sym->st_value;
794 #if SHT_RELX == SHT_RELA
795 tgt += rel->r_addend;
796 #endif
797 addr = s->sh_addr + rel->r_offset;
798 relocate(s1, rel, type, ptr, addr, tgt);
800 /* if the relocation is allocated, we change its symbol table */
801 if (sr->sh_flags & SHF_ALLOC)
802 sr->link = s1->dynsym;
805 /* relocate relocation table in 'sr' */
806 static void relocate_rel(TCCState *s1, Section *sr)
808 Section *s;
809 ElfW_Rel *rel;
811 s = s1->sections[sr->sh_info];
812 for_each_elem(sr, 0, rel, ElfW_Rel)
813 rel->r_offset += s->sh_addr;
816 /* count the number of dynamic relocations so that we can reserve
817 their space */
818 static int prepare_dynamic_rel(TCCState *s1, Section *sr)
820 ElfW_Rel *rel;
821 int sym_index, type, count;
823 count = 0;
824 for_each_elem(sr, 0, rel, ElfW_Rel) {
825 sym_index = ELFW(R_SYM)(rel->r_info);
826 type = ELFW(R_TYPE)(rel->r_info);
827 switch(type) {
828 #if defined(TCC_TARGET_I386)
829 case R_386_32:
830 #elif defined(TCC_TARGET_X86_64)
831 case R_X86_64_32:
832 case R_X86_64_32S:
833 case R_X86_64_64:
834 #endif
835 count++;
836 break;
837 #if defined(TCC_TARGET_I386)
838 case R_386_PC32:
839 #elif defined(TCC_TARGET_X86_64)
840 case R_X86_64_PC32:
841 #endif
842 if (get_sym_attr(s1, sym_index, 0)->dyn_index)
843 count++;
844 break;
845 default:
846 break;
849 if (count) {
850 /* allocate the section */
851 sr->sh_flags |= SHF_ALLOC;
852 sr->sh_size = count * sizeof(ElfW_Rel);
854 return count;
857 static void build_got(TCCState *s1)
859 /* if no got, then create it */
860 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
861 s1->got->sh_entsize = 4;
862 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
863 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
864 /* keep space for _DYNAMIC pointer and two dummy got entries */
865 section_ptr_add(s1->got, 3 * PTR_SIZE);
868 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
869 in s1->symtab. When creating the dynamic symbol table entry for the GOT
870 relocation, use 'size' and 'info' for the corresponding symbol metadata.
871 Returns the offset of the GOT or (if any) PLT entry. */
872 static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
873 unsigned long size,
874 int info, int sym_index)
876 int need_plt_entry;
877 const char *name;
878 ElfW(Sym) *sym;
879 struct sym_attr *attr;
880 unsigned got_offset;
881 char plt_name[100];
882 int len;
884 need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
885 attr = get_sym_attr(s1, sym_index, 1);
887 /* In case a function is both called and its address taken 2 GOT entries
888 are created, one for taking the address (GOT) and the other for the PLT
889 entry (PLTGOT). */
890 if (need_plt_entry ? attr->plt_offset : attr->got_offset)
891 return attr;
893 /* create the GOT entry */
894 got_offset = s1->got->data_offset;
895 section_ptr_add(s1->got, PTR_SIZE);
897 /* Create the GOT relocation that will insert the address of the object or
898 function of interest in the GOT entry. This is a static relocation for
899 memory output (dlsym will give us the address of symbols) and dynamic
900 relocation otherwise (executable and DLLs). The relocation should be
901 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
902 associated to a PLT entry) but is currently done at load time for an
903 unknown reason. */
905 sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
906 name = (char *) symtab_section->link->data + sym->st_name;
908 if (s1->dynsym) {
909 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
910 /* Hack alarm. We don't want to emit dynamic symbols
911 and symbol based relocs for STB_LOCAL symbols, but rather
912 want to resolve them directly. At this point the symbol
913 values aren't final yet, so we must defer this. We will later
914 have to create a RELATIVE reloc anyway, so we misuse the
915 relocation slot to smuggle the symbol reference until
916 fill_local_got_entries. Not that the sym_index is
917 relative to symtab_section, not s1->dynsym! Nevertheless
918 we use s1->dyn_sym so that if this is the first call
919 that got->reloc is correctly created. Also note that
920 RELATIVE relocs are not normally created for the .got,
921 so the types serves as a marker for later (and is retained
922 also for the final output, which is okay because then the
923 got is just normal data). */
924 put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE,
925 sym_index);
926 } else {
927 if (0 == attr->dyn_index)
928 attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size,
929 info, 0, sym->st_shndx, name);
930 put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type,
931 attr->dyn_index);
933 } else {
934 put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
935 sym_index);
938 if (need_plt_entry) {
939 if (!s1->plt) {
940 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
941 SHF_ALLOC | SHF_EXECINSTR);
942 s1->plt->sh_entsize = 4;
945 attr->plt_offset = create_plt_entry(s1, got_offset, attr);
947 /* create a symbol 'sym@plt' for the PLT jump vector */
948 len = strlen(name);
949 if (len > sizeof plt_name - 5)
950 len = sizeof plt_name - 5;
951 memcpy(plt_name, name, len);
952 strcpy(plt_name + len, "@plt");
953 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
954 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
956 } else {
957 attr->got_offset = got_offset;
960 return attr;
963 /* build GOT and PLT entries */
964 ST_FUNC void build_got_entries(TCCState *s1)
966 Section *s;
967 ElfW_Rel *rel;
968 ElfW(Sym) *sym;
969 int i, type, gotplt_entry, reloc_type, sym_index;
970 struct sym_attr *attr;
972 for(i = 1; i < s1->nb_sections; i++) {
973 s = s1->sections[i];
974 if (s->sh_type != SHT_RELX)
975 continue;
976 /* no need to handle got relocations */
977 if (s->link != symtab_section)
978 continue;
979 for_each_elem(s, 0, rel, ElfW_Rel) {
980 type = ELFW(R_TYPE)(rel->r_info);
981 gotplt_entry = gotplt_entry_type(type);
982 sym_index = ELFW(R_SYM)(rel->r_info);
983 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
985 if (gotplt_entry == NO_GOTPLT_ENTRY) {
986 continue;
989 /* Automatically create PLT/GOT [entry] if it is an undefined
990 reference (resolved at runtime), or the symbol is absolute,
991 probably created by tcc_add_symbol, and thus on 64-bit
992 targets might be too far from application code. */
993 if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
994 if (sym->st_shndx == SHN_UNDEF) {
995 ElfW(Sym) *esym;
996 int dynindex;
997 if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
998 continue;
999 /* Relocations for UNDEF symbols would normally need
1000 to be transferred into the executable or shared object.
1001 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1002 But TCC doesn't do that (at least for exes), so we
1003 need to resolve all such relocs locally. And that
1004 means PLT slots for functions in DLLs and COPY relocs for
1005 data symbols. COPY relocs were generated in
1006 bind_exe_dynsyms (and the symbol adjusted to be defined),
1007 and for functions we were generated a dynamic symbol
1008 of function type. */
1009 if (s1->dynsym) {
1010 /* dynsym isn't set for -run :-/ */
1011 dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index;
1012 esym = (ElfW(Sym) *)s1->dynsym->data + dynindex;
1013 if (dynindex
1014 && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
1015 || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
1016 && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC)))
1017 goto jmp_slot;
1019 } else if (!(sym->st_shndx == SHN_ABS
1020 #ifndef TCC_TARGET_ARM
1021 && PTR_SIZE == 8
1022 #endif
1024 continue;
1027 #ifdef TCC_TARGET_X86_64
1028 if ((type == R_X86_64_PLT32 || type == R_X86_64_PC32) &&
1029 (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT ||
1030 ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)) {
1031 rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
1032 continue;
1034 #endif
1035 if (code_reloc(type)) {
1036 jmp_slot:
1037 reloc_type = R_JMP_SLOT;
1038 } else
1039 reloc_type = R_GLOB_DAT;
1041 if (!s1->got)
1042 build_got(s1);
1044 if (gotplt_entry == BUILD_GOT_ONLY)
1045 continue;
1047 attr = put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1048 sym_index);
1050 if (reloc_type == R_JMP_SLOT)
1051 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
1056 /* put dynamic tag */
1057 static void put_dt(Section *dynamic, int dt, addr_t val)
1059 ElfW(Dyn) *dyn;
1060 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1061 dyn->d_tag = dt;
1062 dyn->d_un.d_val = val;
1065 #ifndef TCC_TARGET_PE
1066 static void add_init_array_defines(TCCState *s1, const char *section_name)
1068 Section *s;
1069 long end_offset;
1070 char sym_start[1024];
1071 char sym_end[1024];
1073 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1074 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1076 s = find_section(s1, section_name);
1077 if (!s) {
1078 end_offset = 0;
1079 s = data_section;
1080 } else {
1081 end_offset = s->data_offset;
1084 set_elf_sym(symtab_section,
1085 0, 0,
1086 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1087 s->sh_num, sym_start);
1088 set_elf_sym(symtab_section,
1089 end_offset, 0,
1090 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1091 s->sh_num, sym_end);
1093 #endif
1095 static int tcc_add_support(TCCState *s1, const char *filename)
1097 char buf[1024];
1098 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
1099 return tcc_add_file(s1, buf);
1102 ST_FUNC void tcc_add_bcheck(TCCState *s1)
1104 #ifdef CONFIG_TCC_BCHECK
1105 addr_t *ptr;
1106 int sym_index;
1108 if (0 == s1->do_bounds_check)
1109 return;
1110 /* XXX: add an object file to do that */
1111 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1112 *ptr = 0;
1113 set_elf_sym(symtab_section, 0, 0,
1114 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1115 bounds_section->sh_num, "__bounds_start");
1116 /* pull bcheck.o from libtcc1.a */
1117 sym_index = set_elf_sym(symtab_section, 0, 0,
1118 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1119 SHN_UNDEF, "__bound_init");
1120 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1121 /* add 'call __bound_init()' in .init section */
1122 Section *init_section = find_section(s1, ".init");
1123 unsigned char *pinit = section_ptr_add(init_section, 5);
1124 pinit[0] = 0xe8;
1125 write32le(pinit + 1, -4);
1126 put_elf_reloc(symtab_section, init_section,
1127 init_section->data_offset - 4, R_386_PC32, sym_index);
1128 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1130 #endif
1133 /* add tcc runtime libraries */
1134 ST_FUNC void tcc_add_runtime(TCCState *s1)
1136 tcc_add_bcheck(s1);
1137 tcc_add_pragma_libs(s1);
1138 /* add libc */
1139 if (!s1->nostdlib) {
1140 tcc_add_library_err(s1, "c");
1141 #ifdef TCC_LIBGCC
1142 if (!s1->static_link) {
1143 if (TCC_LIBGCC[0] == '/')
1144 tcc_add_file(s1, TCC_LIBGCC);
1145 else
1146 tcc_add_dll(s1, TCC_LIBGCC, 0);
1148 #endif
1149 tcc_add_support(s1, TCC_LIBTCC1);
1150 /* add crt end if not memory output */
1151 if (s1->output_type != TCC_OUTPUT_MEMORY)
1152 tcc_add_crt(s1, "crtn.o");
1156 /* add various standard linker symbols (must be done after the
1157 sections are filled (for example after allocating common
1158 symbols)) */
1159 ST_FUNC void tcc_add_linker_symbols(TCCState *s1)
1161 char buf[1024];
1162 int i;
1163 Section *s;
1165 set_elf_sym(symtab_section,
1166 text_section->data_offset, 0,
1167 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1168 text_section->sh_num, "_etext");
1169 set_elf_sym(symtab_section,
1170 data_section->data_offset, 0,
1171 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1172 data_section->sh_num, "_edata");
1173 set_elf_sym(symtab_section,
1174 bss_section->data_offset, 0,
1175 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1176 bss_section->sh_num, "_end");
1177 #ifndef TCC_TARGET_PE
1178 /* horrible new standard ldscript defines */
1179 add_init_array_defines(s1, ".preinit_array");
1180 add_init_array_defines(s1, ".init_array");
1181 add_init_array_defines(s1, ".fini_array");
1182 #endif
1184 /* add start and stop symbols for sections whose name can be
1185 expressed in C */
1186 for(i = 1; i < s1->nb_sections; i++) {
1187 s = s1->sections[i];
1188 if (s->sh_type == SHT_PROGBITS &&
1189 (s->sh_flags & SHF_ALLOC)) {
1190 const char *p;
1191 int ch;
1193 /* check if section name can be expressed in C */
1194 p = s->name;
1195 for(;;) {
1196 ch = *p;
1197 if (!ch)
1198 break;
1199 if (!isid(ch) && !isnum(ch))
1200 goto next_sec;
1201 p++;
1203 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1204 set_elf_sym(symtab_section,
1205 0, 0,
1206 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1207 s->sh_num, buf);
1208 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1209 set_elf_sym(symtab_section,
1210 s->data_offset, 0,
1211 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1212 s->sh_num, buf);
1214 next_sec: ;
1218 static void tcc_output_binary(TCCState *s1, FILE *f,
1219 const int *sec_order)
1221 Section *s;
1222 int i, offset, size;
1224 offset = 0;
1225 for(i=1;i<s1->nb_sections;i++) {
1226 s = s1->sections[sec_order[i]];
1227 if (s->sh_type != SHT_NOBITS &&
1228 (s->sh_flags & SHF_ALLOC)) {
1229 while (offset < s->sh_offset) {
1230 fputc(0, f);
1231 offset++;
1233 size = s->sh_size;
1234 fwrite(s->data, 1, size, f);
1235 offset += size;
1240 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1241 #define HAVE_PHDR 1
1242 #define EXTRA_RELITEMS 14
1243 #else
1244 #define HAVE_PHDR 1
1245 #define EXTRA_RELITEMS 9
1246 #endif
1248 ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1250 int sym_index = ELFW(R_SYM) (rel->r_info);
1251 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1252 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1253 unsigned offset = attr->got_offset;
1255 if (0 == offset)
1256 return;
1257 section_reserve(s1->got, offset + PTR_SIZE);
1258 #ifdef TCC_TARGET_X86_64
1259 write64le(s1->got->data + offset, sym->st_value);
1260 #else
1261 write32le(s1->got->data + offset, sym->st_value);
1262 #endif
1265 /* Perform relocation to GOT or PLT entries */
1266 ST_FUNC void fill_got(TCCState *s1)
1268 Section *s;
1269 ElfW_Rel *rel;
1270 int i;
1272 for(i = 1; i < s1->nb_sections; i++) {
1273 s = s1->sections[i];
1274 if (s->sh_type != SHT_RELX)
1275 continue;
1276 /* no need to handle got relocations */
1277 if (s->link != symtab_section)
1278 continue;
1279 for_each_elem(s, 0, rel, ElfW_Rel) {
1280 switch (ELFW(R_TYPE) (rel->r_info)) {
1281 case R_X86_64_GOT32:
1282 case R_X86_64_GOTPCREL:
1283 case R_X86_64_GOTPCRELX:
1284 case R_X86_64_REX_GOTPCRELX:
1285 case R_X86_64_PLT32:
1286 fill_got_entry(s1, rel);
1287 break;
1293 /* See put_got_entry for a description. This is the second stage
1294 where GOT references to local defined symbols are rewritten. */
1295 static void fill_local_got_entries(TCCState *s1)
1297 ElfW_Rel *rel;
1298 for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
1299 if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
1300 int sym_index = ELFW(R_SYM) (rel->r_info);
1301 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1302 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1303 unsigned offset = attr->got_offset;
1304 if (offset != rel->r_offset - s1->got->sh_addr)
1305 tcc_error_noabort("huh");
1306 rel->r_info = ELFW(R_INFO)(0, R_RELATIVE);
1307 #if SHT_RELX == SHT_RELA
1308 rel->r_addend = sym->st_value;
1309 #else
1310 /* All our REL architectures also happen to be 32bit LE. */
1311 write32le(s1->got->data + offset, sym->st_value);
1312 #endif
1317 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1318 in shared libraries and export non local defined symbols to shared libraries
1319 if -rdynamic switch was given on command line */
1320 static void bind_exe_dynsyms(TCCState *s1)
1322 const char *name;
1323 int sym_index, index;
1324 ElfW(Sym) *sym, *esym;
1325 int type;
1327 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1328 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1329 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1330 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1331 if (sym->st_shndx == SHN_UNDEF) {
1332 name = (char *) symtab_section->link->data + sym->st_name;
1333 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1334 if (sym_index) {
1335 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1336 type = ELFW(ST_TYPE)(esym->st_info);
1337 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
1338 /* Indirect functions shall have STT_FUNC type in executable
1339 * dynsym section. Indeed, a dlsym call following a lazy
1340 * resolution would pick the symbol value from the
1341 * executable dynsym entry which would contain the address
1342 * of the function wanted by the caller of dlsym instead of
1343 * the address of the function that would return that
1344 * address */
1345 int dynindex
1346 = put_elf_sym(s1->dynsym, 0, esym->st_size,
1347 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
1348 name);
1349 int index = sym - (ElfW(Sym) *) symtab_section->data;
1350 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1351 } else if (type == STT_OBJECT) {
1352 unsigned long offset;
1353 ElfW(Sym) *dynsym;
1354 offset = bss_section->data_offset;
1355 /* XXX: which alignment ? */
1356 offset = (offset + 16 - 1) & -16;
1357 set_elf_sym (s1->symtab, offset, esym->st_size,
1358 esym->st_info, 0, bss_section->sh_num, name);
1359 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1360 esym->st_info, 0, bss_section->sh_num,
1361 name);
1363 /* Ensure R_COPY works for weak symbol aliases */
1364 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1365 for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
1366 if ((dynsym->st_value == esym->st_value)
1367 && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
1368 char *dynname = (char *) s1->dynsymtab_section->link->data
1369 + dynsym->st_name;
1370 put_elf_sym(s1->dynsym, offset, dynsym->st_size,
1371 dynsym->st_info, 0,
1372 bss_section->sh_num, dynname);
1373 break;
1378 put_elf_reloc(s1->dynsym, bss_section,
1379 offset, R_COPY, index);
1380 offset += esym->st_size;
1381 bss_section->data_offset = offset;
1383 } else {
1384 /* STB_WEAK undefined symbols are accepted */
1385 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1386 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1387 !strcmp(name, "_fp_hw")) {
1388 } else {
1389 tcc_error_noabort("undefined symbol '%s'", name);
1392 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1393 /* if -rdynamic option, then export all non local symbols */
1394 name = (char *) symtab_section->link->data + sym->st_name;
1395 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1396 0, sym->st_shndx, name);
1401 /* Bind symbols of libraries: export all non local symbols of executable that
1402 are referenced by shared libraries. The reason is that the dynamic loader
1403 search symbol first in executable and then in libraries. Therefore a
1404 reference to a symbol already defined by a library can still be resolved by
1405 a symbol in the executable. */
1406 static void bind_libs_dynsyms(TCCState *s1)
1408 const char *name;
1409 int sym_index;
1410 ElfW(Sym) *sym, *esym;
1412 for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
1413 name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
1414 sym_index = find_elf_sym(symtab_section, name);
1415 /* XXX: avoid adding a symbol if already present because of
1416 -rdynamic ? */
1417 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1418 if (sym_index && sym->st_shndx != SHN_UNDEF)
1419 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1420 0, sym->st_shndx, name);
1421 else if (esym->st_shndx == SHN_UNDEF) {
1422 /* weak symbols can stay undefined */
1423 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
1424 tcc_warning("undefined dynamic symbol '%s'", name);
1429 /* Export all non local symbols. This is used by shared libraries so that the
1430 non local symbols they define can resolve a reference in another shared
1431 library or in the executable. Correspondingly, it allows undefined local
1432 symbols to be resolved by other shared libraries or by the executable. */
1433 static void export_global_syms(TCCState *s1)
1435 int dynindex, index;
1436 const char *name;
1437 ElfW(Sym) *sym;
1439 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1440 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1441 name = (char *) symtab_section->link->data + sym->st_name;
1442 dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1443 sym->st_info, 0, sym->st_shndx, name);
1444 index = sym - (ElfW(Sym) *) symtab_section->data;
1445 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1450 /* Allocate strings for section names and decide if an unallocated section
1451 should be output.
1452 NOTE: the strsec section comes last, so its size is also correct ! */
1453 static void alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
1455 int i;
1456 Section *s;
1458 /* Allocate strings for section names */
1459 for(i = 1; i < s1->nb_sections; i++) {
1460 s = s1->sections[i];
1461 /* when generating a DLL, we include relocations but we may
1462 patch them */
1463 if (file_type == TCC_OUTPUT_DLL &&
1464 s->sh_type == SHT_RELX &&
1465 !(s->sh_flags & SHF_ALLOC)) {
1466 /* gr: avoid bogus relocs for empty (debug) sections */
1467 if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
1468 prepare_dynamic_rel(s1, s);
1469 else if (s1->do_debug)
1470 s->sh_size = s->data_offset;
1471 } else if (s1->do_debug ||
1472 file_type == TCC_OUTPUT_OBJ ||
1473 (s->sh_flags & SHF_ALLOC) ||
1474 i == (s1->nb_sections - 1)) {
1475 /* we output all sections if debug or object file */
1476 s->sh_size = s->data_offset;
1478 if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1479 s->sh_name = put_elf_str(strsec, s->name);
1481 strsec->sh_size = strsec->data_offset;
1484 /* Info to be copied in dynamic section */
1485 struct dyn_inf {
1486 Section *dynamic;
1487 Section *dynstr;
1488 unsigned long dyn_rel_off;
1489 addr_t rel_addr;
1490 addr_t rel_size;
1491 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1492 addr_t bss_addr;
1493 addr_t bss_size;
1494 #endif
1497 /* Assign sections to segments and decide how are sections laid out when loaded
1498 in memory. This function also fills corresponding program headers. */
1499 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1500 Section *interp, Section* strsec,
1501 struct dyn_inf *dyninf, int *sec_order)
1503 int i, j, k, file_type, sh_order_index, file_offset;
1504 unsigned long s_align;
1505 long long tmp;
1506 addr_t addr;
1507 ElfW(Phdr) *ph;
1508 Section *s;
1510 file_type = s1->output_type;
1511 sh_order_index = 1;
1512 file_offset = 0;
1513 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1514 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1515 s_align = ELF_PAGE_SIZE;
1516 if (s1->section_align)
1517 s_align = s1->section_align;
1519 if (phnum > 0) {
1520 if (s1->has_text_addr) {
1521 int a_offset, p_offset;
1522 addr = s1->text_addr;
1523 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1524 ELF_PAGE_SIZE */
1525 a_offset = (int) (addr & (s_align - 1));
1526 p_offset = file_offset & (s_align - 1);
1527 if (a_offset < p_offset)
1528 a_offset += s_align;
1529 file_offset += (a_offset - p_offset);
1530 } else {
1531 if (file_type == TCC_OUTPUT_DLL)
1532 addr = 0;
1533 else
1534 addr = ELF_START_ADDR;
1535 /* compute address after headers */
1536 addr += (file_offset & (s_align - 1));
1539 ph = &phdr[0];
1540 /* Leave one program headers for the program interpreter and one for
1541 the program header table itself if needed. These are done later as
1542 they require section layout to be done first. */
1543 if (interp)
1544 ph += 1 + HAVE_PHDR;
1546 /* dynamic relocation table information, for .dynamic section */
1547 dyninf->rel_addr = dyninf->rel_size = 0;
1548 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1549 dyninf->bss_addr = dyninf->bss_size = 0;
1550 #endif
1552 for(j = 0; j < 2; j++) {
1553 ph->p_type = PT_LOAD;
1554 if (j == 0)
1555 ph->p_flags = PF_R | PF_X;
1556 else
1557 ph->p_flags = PF_R | PF_W;
1558 ph->p_align = s_align;
1560 /* Decide the layout of sections loaded in memory. This must
1561 be done before program headers are filled since they contain
1562 info about the layout. We do the following ordering: interp,
1563 symbol tables, relocations, progbits, nobits */
1564 /* XXX: do faster and simpler sorting */
1565 for(k = 0; k < 5; k++) {
1566 for(i = 1; i < s1->nb_sections; i++) {
1567 s = s1->sections[i];
1568 /* compute if section should be included */
1569 if (j == 0) {
1570 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1571 SHF_ALLOC)
1572 continue;
1573 } else {
1574 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1575 (SHF_ALLOC | SHF_WRITE))
1576 continue;
1578 if (s == interp) {
1579 if (k != 0)
1580 continue;
1581 } else if (s->sh_type == SHT_DYNSYM ||
1582 s->sh_type == SHT_STRTAB ||
1583 s->sh_type == SHT_HASH) {
1584 if (k != 1)
1585 continue;
1586 } else if (s->sh_type == SHT_RELX) {
1587 if (k != 2)
1588 continue;
1589 } else if (s->sh_type == SHT_NOBITS) {
1590 if (k != 4)
1591 continue;
1592 } else {
1593 if (k != 3)
1594 continue;
1596 sec_order[sh_order_index++] = i;
1598 /* section matches: we align it and add its size */
1599 tmp = addr;
1600 addr = (addr + s->sh_addralign - 1) &
1601 ~(s->sh_addralign - 1);
1602 file_offset += (int) ( addr - tmp );
1603 s->sh_offset = file_offset;
1604 s->sh_addr = addr;
1606 /* update program header infos */
1607 if (ph->p_offset == 0) {
1608 ph->p_offset = file_offset;
1609 ph->p_vaddr = addr;
1610 ph->p_paddr = ph->p_vaddr;
1612 /* update dynamic relocation infos */
1613 if (s->sh_type == SHT_RELX) {
1614 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1615 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1616 dyninf->rel_addr = addr;
1617 dyninf->rel_size += s->sh_size; /* XXX only first rel. */
1619 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1620 dyninf->bss_addr = addr;
1621 dyninf->bss_size = s->sh_size; /* XXX only first rel. */
1623 #else
1624 if (dyninf->rel_size == 0)
1625 dyninf->rel_addr = addr;
1626 dyninf->rel_size += s->sh_size;
1627 #endif
1629 addr += s->sh_size;
1630 if (s->sh_type != SHT_NOBITS)
1631 file_offset += s->sh_size;
1634 if (j == 0) {
1635 /* Make the first PT_LOAD segment include the program
1636 headers itself (and the ELF header as well), it'll
1637 come out with same memory use but will make various
1638 tools like binutils strip work better. */
1639 ph->p_offset &= ~(ph->p_align - 1);
1640 ph->p_vaddr &= ~(ph->p_align - 1);
1641 ph->p_paddr &= ~(ph->p_align - 1);
1643 ph->p_filesz = file_offset - ph->p_offset;
1644 ph->p_memsz = addr - ph->p_vaddr;
1645 ph++;
1646 if (j == 0) {
1647 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1648 /* if in the middle of a page, we duplicate the page in
1649 memory so that one copy is RX and the other is RW */
1650 if ((addr & (s_align - 1)) != 0)
1651 addr += s_align;
1652 } else {
1653 addr = (addr + s_align - 1) & ~(s_align - 1);
1654 file_offset = (file_offset + s_align - 1) & ~(s_align - 1);
1660 /* all other sections come after */
1661 for(i = 1; i < s1->nb_sections; i++) {
1662 s = s1->sections[i];
1663 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1664 continue;
1665 sec_order[sh_order_index++] = i;
1667 file_offset = (file_offset + s->sh_addralign - 1) &
1668 ~(s->sh_addralign - 1);
1669 s->sh_offset = file_offset;
1670 if (s->sh_type != SHT_NOBITS)
1671 file_offset += s->sh_size;
1674 return file_offset;
1677 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
1678 Section *dynamic)
1680 ElfW(Phdr) *ph;
1682 /* if interpreter, then add corresponding program header */
1683 if (interp) {
1684 ph = &phdr[0];
1686 if (HAVE_PHDR)
1688 int len = phnum * sizeof(ElfW(Phdr));
1690 ph->p_type = PT_PHDR;
1691 ph->p_offset = sizeof(ElfW(Ehdr));
1692 ph->p_vaddr = interp->sh_addr - len;
1693 ph->p_paddr = ph->p_vaddr;
1694 ph->p_filesz = ph->p_memsz = len;
1695 ph->p_flags = PF_R | PF_X;
1696 ph->p_align = 4; /* interp->sh_addralign; */
1697 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;
1731 dynamic = dyninf->dynamic;
1733 /* put dynamic section entries */
1734 dynamic->data_offset = dyninf->dyn_rel_off;
1735 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1736 put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
1737 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1738 put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
1739 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1740 #if PTR_SIZE == 8
1741 put_dt(dynamic, DT_RELA, dyninf->rel_addr);
1742 put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
1743 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1744 #else
1745 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1746 put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
1747 put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size);
1748 put_dt(dynamic, DT_JMPREL, dyninf->rel_addr);
1749 put_dt(dynamic, DT_PLTREL, DT_REL);
1750 put_dt(dynamic, DT_REL, dyninf->bss_addr);
1751 put_dt(dynamic, DT_RELSZ, dyninf->bss_size);
1752 #else
1753 put_dt(dynamic, DT_REL, dyninf->rel_addr);
1754 put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
1755 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1756 #endif
1757 #endif
1758 if (s1->do_debug)
1759 put_dt(dynamic, DT_DEBUG, 0);
1760 put_dt(dynamic, DT_NULL, 0);
1763 /* Relocate remaining sections and symbols (that is those not related to
1764 dynamic linking) */
1765 static int final_sections_reloc(TCCState *s1)
1767 int i;
1768 Section *s;
1770 relocate_syms(s1, s1->symtab, 0);
1772 if (s1->nb_errors != 0)
1773 return -1;
1775 /* relocate sections */
1776 /* XXX: ignore sections with allocated relocations ? */
1777 for(i = 1; i < s1->nb_sections; i++) {
1778 s = s1->sections[i];
1779 #if defined(TCC_TARGET_I386) || defined(TCC_MUSL)
1780 if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
1781 /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC
1782 checking is removed */
1783 #else
1784 if (s->reloc && s != s1->got)
1785 /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */
1786 #endif
1787 relocate_section(s1, s);
1790 /* relocate relocation entries if the relocation tables are
1791 allocated in the executable */
1792 for(i = 1; i < s1->nb_sections; i++) {
1793 s = s1->sections[i];
1794 if ((s->sh_flags & SHF_ALLOC) &&
1795 s->sh_type == SHT_RELX) {
1796 relocate_rel(s1, s);
1799 return 0;
1802 /* Create an ELF file on disk.
1803 This function handle ELF specific layout requirements */
1804 static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
1805 int file_offset, int *sec_order)
1807 int i, shnum, offset, size, file_type;
1808 Section *s;
1809 ElfW(Ehdr) ehdr;
1810 ElfW(Shdr) shdr, *sh;
1812 file_type = s1->output_type;
1813 shnum = s1->nb_sections;
1815 memset(&ehdr, 0, sizeof(ehdr));
1817 if (phnum > 0) {
1818 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1819 ehdr.e_phnum = phnum;
1820 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1823 /* align to 4 */
1824 file_offset = (file_offset + 3) & -4;
1826 /* fill header */
1827 ehdr.e_ident[0] = ELFMAG0;
1828 ehdr.e_ident[1] = ELFMAG1;
1829 ehdr.e_ident[2] = ELFMAG2;
1830 ehdr.e_ident[3] = ELFMAG3;
1831 ehdr.e_ident[4] = ELFCLASSW;
1832 ehdr.e_ident[5] = ELFDATA2LSB;
1833 ehdr.e_ident[6] = EV_CURRENT;
1834 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1835 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1836 #endif
1837 #ifdef TCC_TARGET_ARM
1838 #ifdef TCC_ARM_EABI
1839 ehdr.e_ident[EI_OSABI] = 0;
1840 ehdr.e_flags = EF_ARM_EABI_VER4;
1841 if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
1842 ehdr.e_flags |= EF_ARM_HASENTRY;
1843 if (s1->float_abi == ARM_HARD_FLOAT)
1844 ehdr.e_flags |= EF_ARM_VFP_FLOAT;
1845 else
1846 ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
1847 #else
1848 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1849 #endif
1850 #endif
1851 switch(file_type) {
1852 default:
1853 case TCC_OUTPUT_EXE:
1854 ehdr.e_type = ET_EXEC;
1855 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
1856 break;
1857 case TCC_OUTPUT_DLL:
1858 ehdr.e_type = ET_DYN;
1859 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1860 break;
1861 case TCC_OUTPUT_OBJ:
1862 ehdr.e_type = ET_REL;
1863 break;
1865 ehdr.e_machine = EM_TCC_TARGET;
1866 ehdr.e_version = EV_CURRENT;
1867 ehdr.e_shoff = file_offset;
1868 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1869 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1870 ehdr.e_shnum = shnum;
1871 ehdr.e_shstrndx = shnum - 1;
1873 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1874 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1875 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1877 sort_syms(s1, symtab_section);
1878 for(i = 1; i < s1->nb_sections; i++) {
1879 s = s1->sections[sec_order[i]];
1880 if (s->sh_type != SHT_NOBITS) {
1881 while (offset < s->sh_offset) {
1882 fputc(0, f);
1883 offset++;
1885 size = s->sh_size;
1886 if (size)
1887 fwrite(s->data, 1, size, f);
1888 offset += size;
1892 /* output section headers */
1893 while (offset < ehdr.e_shoff) {
1894 fputc(0, f);
1895 offset++;
1898 for(i = 0; i < s1->nb_sections; i++) {
1899 sh = &shdr;
1900 memset(sh, 0, sizeof(ElfW(Shdr)));
1901 s = s1->sections[i];
1902 if (s) {
1903 sh->sh_name = s->sh_name;
1904 sh->sh_type = s->sh_type;
1905 sh->sh_flags = s->sh_flags;
1906 sh->sh_entsize = s->sh_entsize;
1907 sh->sh_info = s->sh_info;
1908 if (s->link)
1909 sh->sh_link = s->link->sh_num;
1910 sh->sh_addralign = s->sh_addralign;
1911 sh->sh_addr = s->sh_addr;
1912 sh->sh_offset = s->sh_offset;
1913 sh->sh_size = s->sh_size;
1915 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1919 /* Write an elf, coff or "binary" file */
1920 static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
1921 ElfW(Phdr) *phdr, int file_offset, int *sec_order)
1923 int fd, mode, file_type;
1924 FILE *f;
1926 file_type = s1->output_type;
1927 if (file_type == TCC_OUTPUT_OBJ)
1928 mode = 0666;
1929 else
1930 mode = 0777;
1931 unlink(filename);
1932 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1933 if (fd < 0) {
1934 tcc_error_noabort("could not write '%s'", filename);
1935 return -1;
1937 f = fdopen(fd, "wb");
1938 if (s1->verbose)
1939 printf("<- %s\n", filename);
1941 #ifdef TCC_TARGET_COFF
1942 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
1943 tcc_output_coff(s1, f);
1944 else
1945 #endif
1946 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1947 tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
1948 else
1949 tcc_output_binary(s1, f, sec_order);
1950 fclose(f);
1952 return 0;
1955 /* Sort section headers by assigned sh_addr, remove sections
1956 that we aren't going to output. */
1957 static void tidy_section_headers(TCCState *s1, int *sec_order)
1959 int i, nnew, l, *backmap;
1960 Section **snew, *s;
1961 ElfW(Sym) *sym;
1963 snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
1964 backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
1965 for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
1966 s = s1->sections[sec_order[i]];
1967 if (!i || s->sh_name) {
1968 backmap[sec_order[i]] = nnew;
1969 snew[nnew] = s;
1970 ++nnew;
1971 } else {
1972 backmap[sec_order[i]] = 0;
1973 snew[--l] = s;
1976 for (i = 0; i < nnew; i++) {
1977 s = snew[i];
1978 if (s) {
1979 s->sh_num = i;
1980 if (s->sh_type == SHT_RELX)
1981 s->sh_info = backmap[s->sh_info];
1985 for_each_elem(symtab_section, 1, sym, ElfW(Sym))
1986 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1987 sym->st_shndx = backmap[sym->st_shndx];
1988 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
1989 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
1990 sym->st_shndx = backmap[sym->st_shndx];
1992 for (i = 0; i < s1->nb_sections; i++)
1993 sec_order[i] = i;
1994 tcc_free(s1->sections);
1995 s1->sections = snew;
1996 s1->nb_sections = nnew;
1997 tcc_free(backmap);
2000 /* Output an elf, coff or binary file */
2001 /* XXX: suppress unneeded sections */
2002 static int elf_output_file(TCCState *s1, const char *filename)
2004 int i, ret, phnum, shnum, file_type, file_offset, *sec_order;
2005 struct dyn_inf dyninf = {0};
2006 ElfW(Phdr) *phdr;
2007 ElfW(Sym) *sym;
2008 Section *strsec, *interp, *dynamic, *dynstr;
2010 file_type = s1->output_type;
2011 s1->nb_errors = 0;
2013 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2014 if (file_type != TCC_OUTPUT_OBJ) {
2015 tcc_add_runtime(s1);
2018 phdr = NULL;
2019 sec_order = NULL;
2020 interp = dynamic = dynstr = NULL; /* avoid warning */
2022 if (file_type != TCC_OUTPUT_OBJ) {
2023 relocate_common_syms();
2025 tcc_add_linker_symbols(s1);
2027 if (!s1->static_link) {
2028 if (file_type == TCC_OUTPUT_EXE) {
2029 char *ptr;
2030 /* allow override the dynamic loader */
2031 const char *elfint = getenv("LD_SO");
2032 if (elfint == NULL)
2033 elfint = DEFAULT_ELFINTERP(s1);
2034 /* add interpreter section only if executable */
2035 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
2036 interp->sh_addralign = 1;
2037 ptr = section_ptr_add(interp, 1 + strlen(elfint));
2038 strcpy(ptr, elfint);
2041 /* add dynamic symbol table */
2042 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
2043 ".dynstr",
2044 ".hash", SHF_ALLOC);
2045 dynstr = s1->dynsym->link;
2047 /* add dynamic section */
2048 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
2049 SHF_ALLOC | SHF_WRITE);
2050 dynamic->link = dynstr;
2051 dynamic->sh_entsize = sizeof(ElfW(Dyn));
2053 build_got(s1);
2055 if (file_type == TCC_OUTPUT_EXE) {
2056 bind_exe_dynsyms(s1);
2058 if (s1->nb_errors) {
2059 ret = -1;
2060 goto the_end;
2063 bind_libs_dynsyms(s1);
2064 } else /* shared library case: simply export all global symbols */
2065 export_global_syms(s1);
2067 build_got_entries(s1);
2069 /* add a list of needed dlls */
2070 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2071 DLLReference *dllref = s1->loaded_dlls[i];
2072 if (dllref->level == 0)
2073 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
2076 if (s1->rpath)
2077 put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
2078 put_elf_str(dynstr, s1->rpath));
2080 /* XXX: currently, since we do not handle PIC code, we
2081 must relocate the readonly segments */
2082 if (file_type == TCC_OUTPUT_DLL) {
2083 if (s1->soname)
2084 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
2085 put_dt(dynamic, DT_TEXTREL, 0);
2088 if (s1->symbolic)
2089 put_dt(dynamic, DT_SYMBOLIC, 0);
2091 /* add necessary space for other entries */
2092 dyninf.dyn_rel_off = dynamic->data_offset;
2093 dynamic->data_offset += sizeof(ElfW(Dyn)) * EXTRA_RELITEMS;
2094 } else {
2095 /* still need to build got entries in case of static link */
2096 build_got_entries(s1);
2100 /* we add a section for symbols */
2101 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
2102 put_elf_str(strsec, "");
2104 /* compute number of sections */
2105 shnum = s1->nb_sections;
2107 /* this array is used to reorder sections in the output file */
2108 sec_order = tcc_malloc(sizeof(int) * shnum);
2109 sec_order[0] = 0;
2111 /* compute number of program headers */
2112 switch(file_type) {
2113 default:
2114 case TCC_OUTPUT_OBJ:
2115 phnum = 0;
2116 break;
2117 case TCC_OUTPUT_EXE:
2118 if (!s1->static_link)
2119 phnum = 4 + HAVE_PHDR;
2120 else
2121 phnum = 2;
2122 break;
2123 case TCC_OUTPUT_DLL:
2124 phnum = 3;
2125 break;
2128 /* Allocate strings for section names */
2129 alloc_sec_names(s1, file_type, strsec);
2131 /* allocate program segment headers */
2132 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
2134 /* compute section to program header mapping */
2135 file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2136 sec_order);
2138 /* Fill remaining program header and finalize relocation related to dynamic
2139 linking. */
2140 if (phnum > 0) {
2141 fill_unloadable_phdr(phdr, phnum, interp, dynamic);
2142 if (dynamic) {
2143 dyninf.dynamic = dynamic;
2144 dyninf.dynstr = dynstr;
2146 fill_dynamic(s1, &dyninf);
2148 /* put in GOT the dynamic section address and relocate PLT */
2149 write32le(s1->got->data, dynamic->sh_addr);
2150 if (file_type == TCC_OUTPUT_EXE
2151 || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
2152 relocate_plt(s1);
2154 /* relocate symbols in .dynsym now that final addresses are known */
2155 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
2156 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
2157 /* do symbol relocation */
2158 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
2164 /* if building executable or DLL, then relocate each section
2165 except the GOT which is already relocated */
2166 if (file_type != TCC_OUTPUT_OBJ) {
2167 ret = final_sections_reloc(s1);
2168 if (ret)
2169 goto the_end;
2170 tidy_section_headers(s1, sec_order);
2173 /* Perform relocation to GOT or PLT entries */
2174 if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2175 fill_got(s1);
2176 else if (s1->got)
2177 fill_local_got_entries(s1);
2179 /* Create the ELF file with name 'filename' */
2180 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2181 s1->nb_sections = shnum;
2182 the_end:
2183 tcc_free(sec_order);
2184 tcc_free(phdr);
2185 return ret;
2188 LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
2190 int ret;
2191 #ifdef TCC_TARGET_PE
2192 if (s->output_type != TCC_OUTPUT_OBJ) {
2193 ret = pe_output_file(s, filename);
2194 } else
2195 #endif
2196 ret = elf_output_file(s, filename);
2197 return ret;
2200 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2202 void *data;
2204 data = tcc_malloc(size);
2205 lseek(fd, file_offset, SEEK_SET);
2206 read(fd, data, size);
2207 return data;
2210 typedef struct SectionMergeInfo {
2211 Section *s; /* corresponding existing section */
2212 unsigned long offset; /* offset of the new section in the existing section */
2213 uint8_t new_section; /* true if section 's' was added */
2214 uint8_t link_once; /* true if link once section */
2215 } SectionMergeInfo;
2217 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
2219 int size = read(fd, h, sizeof *h);
2220 if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
2221 if (h->e_type == ET_REL)
2222 return AFF_BINTYPE_REL;
2223 if (h->e_type == ET_DYN)
2224 return AFF_BINTYPE_DYN;
2225 } else if (size >= 8) {
2226 if (0 == memcmp(h, ARMAG, 8))
2227 return AFF_BINTYPE_AR;
2228 #ifdef TCC_TARGET_COFF
2229 if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC)
2230 return AFF_BINTYPE_C67;
2231 #endif
2233 return 0;
2236 /* load an object file and merge it with current files */
2237 /* XXX: handle correctly stab (debug) info */
2238 ST_FUNC int tcc_load_object_file(TCCState *s1,
2239 int fd, unsigned long file_offset)
2241 ElfW(Ehdr) ehdr;
2242 ElfW(Shdr) *shdr, *sh;
2243 int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed;
2244 unsigned char *strsec, *strtab;
2245 int *old_to_new_syms;
2246 char *sh_name, *name;
2247 SectionMergeInfo *sm_table, *sm;
2248 ElfW(Sym) *sym, *symtab;
2249 ElfW_Rel *rel;
2250 Section *s;
2252 int stab_index;
2253 int stabstr_index;
2255 stab_index = stabstr_index = 0;
2257 lseek(fd, file_offset, SEEK_SET);
2258 if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL)
2259 goto fail1;
2260 /* test CPU specific stuff */
2261 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2262 ehdr.e_machine != EM_TCC_TARGET) {
2263 fail1:
2264 tcc_error_noabort("invalid object file");
2265 return -1;
2267 /* read sections */
2268 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2269 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2270 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2272 /* load section names */
2273 sh = &shdr[ehdr.e_shstrndx];
2274 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2276 /* load symtab and strtab */
2277 old_to_new_syms = NULL;
2278 symtab = NULL;
2279 strtab = NULL;
2280 nb_syms = 0;
2281 seencompressed = 0;
2282 for(i = 1; i < ehdr.e_shnum; i++) {
2283 sh = &shdr[i];
2284 if (sh->sh_type == SHT_SYMTAB) {
2285 if (symtab) {
2286 tcc_error_noabort("object must contain only one symtab");
2287 fail:
2288 ret = -1;
2289 goto the_end;
2291 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2292 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2293 sm_table[i].s = symtab_section;
2295 /* now load strtab */
2296 sh = &shdr[sh->sh_link];
2297 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2299 if (sh->sh_flags & SHF_COMPRESSED)
2300 seencompressed = 1;
2303 /* now examine each section and try to merge its content with the
2304 ones in memory */
2305 for(i = 1; i < ehdr.e_shnum; i++) {
2306 /* no need to examine section name strtab */
2307 if (i == ehdr.e_shstrndx)
2308 continue;
2309 sh = &shdr[i];
2310 sh_name = (char *) strsec + sh->sh_name;
2311 /* ignore sections types we do not handle */
2312 if (sh->sh_type != SHT_PROGBITS &&
2313 sh->sh_type != SHT_RELX &&
2314 #ifdef TCC_ARM_EABI
2315 sh->sh_type != SHT_ARM_EXIDX &&
2316 #endif
2317 sh->sh_type != SHT_NOBITS &&
2318 sh->sh_type != SHT_PREINIT_ARRAY &&
2319 sh->sh_type != SHT_INIT_ARRAY &&
2320 sh->sh_type != SHT_FINI_ARRAY &&
2321 strcmp(sh_name, ".stabstr")
2323 continue;
2324 if (seencompressed
2325 && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1)
2326 || (sh->sh_type == SHT_RELX
2327 && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name,
2328 ".debug_", sizeof(".debug_")-1))))
2329 continue;
2330 if (sh->sh_addralign < 1)
2331 sh->sh_addralign = 1;
2332 /* find corresponding section, if any */
2333 for(j = 1; j < s1->nb_sections;j++) {
2334 s = s1->sections[j];
2335 if (!strcmp(s->name, sh_name)) {
2336 if (!strncmp(sh_name, ".gnu.linkonce",
2337 sizeof(".gnu.linkonce") - 1)) {
2338 /* if a 'linkonce' section is already present, we
2339 do not add it again. It is a little tricky as
2340 symbols can still be defined in
2341 it. */
2342 sm_table[i].link_once = 1;
2343 goto next;
2344 } else {
2345 goto found;
2349 /* not found: create new section */
2350 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
2351 /* take as much info as possible from the section. sh_link and
2352 sh_info will be updated later */
2353 s->sh_addralign = sh->sh_addralign;
2354 s->sh_entsize = sh->sh_entsize;
2355 sm_table[i].new_section = 1;
2356 found:
2357 if (sh->sh_type != s->sh_type) {
2358 tcc_error_noabort("invalid section type");
2359 goto fail;
2362 /* align start of section */
2363 offset = s->data_offset;
2365 if (0 == strcmp(sh_name, ".stab")) {
2366 stab_index = i;
2367 goto no_align;
2369 if (0 == strcmp(sh_name, ".stabstr")) {
2370 stabstr_index = i;
2371 goto no_align;
2374 size = sh->sh_addralign - 1;
2375 offset = (offset + size) & ~size;
2376 if (sh->sh_addralign > s->sh_addralign)
2377 s->sh_addralign = sh->sh_addralign;
2378 s->data_offset = offset;
2379 no_align:
2380 sm_table[i].offset = offset;
2381 sm_table[i].s = s;
2382 /* concatenate sections */
2383 size = sh->sh_size;
2384 if (sh->sh_type != SHT_NOBITS) {
2385 unsigned char *ptr;
2386 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2387 ptr = section_ptr_add(s, size);
2388 read(fd, ptr, size);
2389 } else {
2390 s->data_offset += size;
2392 next: ;
2395 /* gr relocate stab strings */
2396 if (stab_index && stabstr_index) {
2397 Stab_Sym *a, *b;
2398 unsigned o;
2399 s = sm_table[stab_index].s;
2400 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2401 b = (Stab_Sym *)(s->data + s->data_offset);
2402 o = sm_table[stabstr_index].offset;
2403 while (a < b)
2404 a->n_strx += o, a++;
2407 /* second short pass to update sh_link and sh_info fields of new
2408 sections */
2409 for(i = 1; i < ehdr.e_shnum; i++) {
2410 s = sm_table[i].s;
2411 if (!s || !sm_table[i].new_section)
2412 continue;
2413 sh = &shdr[i];
2414 if (sh->sh_link > 0)
2415 s->link = sm_table[sh->sh_link].s;
2416 if (sh->sh_type == SHT_RELX) {
2417 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2418 /* update backward link */
2419 s1->sections[s->sh_info]->reloc = s;
2422 sm = sm_table;
2424 /* resolve symbols */
2425 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2427 sym = symtab + 1;
2428 for(i = 1; i < nb_syms; i++, sym++) {
2429 if (sym->st_shndx != SHN_UNDEF &&
2430 sym->st_shndx < SHN_LORESERVE) {
2431 sm = &sm_table[sym->st_shndx];
2432 if (sm->link_once) {
2433 /* if a symbol is in a link once section, we use the
2434 already defined symbol. It is very important to get
2435 correct relocations */
2436 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2437 name = (char *) strtab + sym->st_name;
2438 sym_index = find_elf_sym(symtab_section, name);
2439 if (sym_index)
2440 old_to_new_syms[i] = sym_index;
2442 continue;
2444 /* if no corresponding section added, no need to add symbol */
2445 if (!sm->s)
2446 continue;
2447 /* convert section number */
2448 sym->st_shndx = sm->s->sh_num;
2449 /* offset value */
2450 sym->st_value += sm->offset;
2452 /* add symbol */
2453 name = (char *) strtab + sym->st_name;
2454 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
2455 sym->st_info, sym->st_other,
2456 sym->st_shndx, name);
2457 old_to_new_syms[i] = sym_index;
2460 /* third pass to patch relocation entries */
2461 for(i = 1; i < ehdr.e_shnum; i++) {
2462 s = sm_table[i].s;
2463 if (!s)
2464 continue;
2465 sh = &shdr[i];
2466 offset = sm_table[i].offset;
2467 switch(s->sh_type) {
2468 case SHT_RELX:
2469 /* take relocation offset information */
2470 offseti = sm_table[sh->sh_info].offset;
2471 for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) {
2472 int type;
2473 unsigned sym_index;
2474 /* convert symbol index */
2475 type = ELFW(R_TYPE)(rel->r_info);
2476 sym_index = ELFW(R_SYM)(rel->r_info);
2477 /* NOTE: only one symtab assumed */
2478 if (sym_index >= nb_syms)
2479 goto invalid_reloc;
2480 sym_index = old_to_new_syms[sym_index];
2481 /* ignore link_once in rel section. */
2482 if (!sym_index && !sm->link_once
2483 #ifdef TCC_TARGET_ARM
2484 && type != R_ARM_V4BX
2485 #endif
2487 invalid_reloc:
2488 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2489 i, strsec + sh->sh_name, rel->r_offset);
2490 goto fail;
2492 rel->r_info = ELFW(R_INFO)(sym_index, type);
2493 /* offset the relocation offset */
2494 rel->r_offset += offseti;
2495 #ifdef TCC_TARGET_ARM
2496 /* Jumps and branches from a Thumb code to a PLT entry need
2497 special handling since PLT entries are ARM code.
2498 Unconditional bl instructions referencing PLT entries are
2499 handled by converting these instructions into blx
2500 instructions. Other case of instructions referencing a PLT
2501 entry require to add a Thumb stub before the PLT entry to
2502 switch to ARM mode. We set bit plt_thumb_stub of the
2503 attribute of a symbol to indicate such a case. */
2504 if (type == R_ARM_THM_JUMP24)
2505 get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
2506 #endif
2508 break;
2509 default:
2510 break;
2514 ret = 0;
2515 the_end:
2516 tcc_free(symtab);
2517 tcc_free(strtab);
2518 tcc_free(old_to_new_syms);
2519 tcc_free(sm_table);
2520 tcc_free(strsec);
2521 tcc_free(shdr);
2522 return ret;
2525 typedef struct ArchiveHeader {
2526 char ar_name[16]; /* name of this member */
2527 char ar_date[12]; /* file mtime */
2528 char ar_uid[6]; /* owner uid; printed as decimal */
2529 char ar_gid[6]; /* owner gid; printed as decimal */
2530 char ar_mode[8]; /* file mode, printed as octal */
2531 char ar_size[10]; /* file size, printed as decimal */
2532 char ar_fmag[2]; /* should contain ARFMAG */
2533 } ArchiveHeader;
2535 static int get_be32(const uint8_t *b)
2537 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2540 static long get_be64(const uint8_t *b)
2542 long long ret = get_be32(b);
2543 ret = (ret << 32) | (unsigned)get_be32(b+4);
2544 return (long)ret;
2547 /* load only the objects which resolve undefined symbols */
2548 static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
2550 long i, bound, nsyms, sym_index, off, ret;
2551 uint8_t *data;
2552 const char *ar_names, *p;
2553 const uint8_t *ar_index;
2554 ElfW(Sym) *sym;
2556 data = tcc_malloc(size);
2557 if (read(fd, data, size) != size)
2558 goto fail;
2559 nsyms = entrysize == 4 ? get_be32(data) : get_be64(data);
2560 ar_index = data + entrysize;
2561 ar_names = (char *) ar_index + nsyms * entrysize;
2563 do {
2564 bound = 0;
2565 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2566 sym_index = find_elf_sym(symtab_section, p);
2567 if(sym_index) {
2568 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2569 if(sym->st_shndx == SHN_UNDEF) {
2570 off = (entrysize == 4
2571 ? get_be32(ar_index + i * 4)
2572 : get_be64(ar_index + i * 8))
2573 + sizeof(ArchiveHeader);
2574 ++bound;
2575 if(tcc_load_object_file(s1, fd, off) < 0) {
2576 fail:
2577 ret = -1;
2578 goto the_end;
2583 } while(bound);
2584 ret = 0;
2585 the_end:
2586 tcc_free(data);
2587 return ret;
2590 /* load a '.a' file */
2591 ST_FUNC int tcc_load_archive(TCCState *s1, int fd)
2593 ArchiveHeader hdr;
2594 char ar_size[11];
2595 char ar_name[17];
2596 char magic[8];
2597 int size, len, i;
2598 unsigned long file_offset;
2600 /* skip magic which was already checked */
2601 read(fd, magic, sizeof(magic));
2603 for(;;) {
2604 len = read(fd, &hdr, sizeof(hdr));
2605 if (len == 0)
2606 break;
2607 if (len != sizeof(hdr)) {
2608 tcc_error_noabort("invalid archive");
2609 return -1;
2611 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2612 ar_size[sizeof(hdr.ar_size)] = '\0';
2613 size = strtol(ar_size, NULL, 0);
2614 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2615 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2616 if (ar_name[i] != ' ')
2617 break;
2619 ar_name[i + 1] = '\0';
2620 file_offset = lseek(fd, 0, SEEK_CUR);
2621 /* align to even */
2622 size = (size + 1) & ~1;
2623 if (!strcmp(ar_name, "/")) {
2624 /* coff symbol table : we handle it */
2625 if(s1->alacarte_link)
2626 return tcc_load_alacarte(s1, fd, size, 4);
2627 } else if (!strcmp(ar_name, "/SYM64/")) {
2628 if(s1->alacarte_link)
2629 return tcc_load_alacarte(s1, fd, size, 8);
2630 } else {
2631 ElfW(Ehdr) ehdr;
2632 if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
2633 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2634 return -1;
2637 lseek(fd, file_offset + size, SEEK_SET);
2639 return 0;
2642 #ifndef TCC_TARGET_PE
2643 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2644 is referenced by the user (so it should be added as DT_NEEDED in
2645 the generated ELF file) */
2646 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2648 ElfW(Ehdr) ehdr;
2649 ElfW(Shdr) *shdr, *sh, *sh1;
2650 int i, j, nb_syms, nb_dts, sym_bind, ret;
2651 ElfW(Sym) *sym, *dynsym;
2652 ElfW(Dyn) *dt, *dynamic;
2653 unsigned char *dynstr;
2654 const char *name, *soname;
2655 DLLReference *dllref;
2657 read(fd, &ehdr, sizeof(ehdr));
2659 /* test CPU specific stuff */
2660 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2661 ehdr.e_machine != EM_TCC_TARGET) {
2662 tcc_error_noabort("bad architecture");
2663 return -1;
2666 /* read sections */
2667 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2669 /* load dynamic section and dynamic symbols */
2670 nb_syms = 0;
2671 nb_dts = 0;
2672 dynamic = NULL;
2673 dynsym = NULL; /* avoid warning */
2674 dynstr = NULL; /* avoid warning */
2675 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2676 switch(sh->sh_type) {
2677 case SHT_DYNAMIC:
2678 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2679 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2680 break;
2681 case SHT_DYNSYM:
2682 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2683 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2684 sh1 = &shdr[sh->sh_link];
2685 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2686 break;
2687 default:
2688 break;
2692 /* compute the real library name */
2693 soname = tcc_basename(filename);
2695 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2696 if (dt->d_tag == DT_SONAME) {
2697 soname = (char *) dynstr + dt->d_un.d_val;
2701 /* if the dll is already loaded, do not load it */
2702 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2703 dllref = s1->loaded_dlls[i];
2704 if (!strcmp(soname, dllref->name)) {
2705 /* but update level if needed */
2706 if (level < dllref->level)
2707 dllref->level = level;
2708 ret = 0;
2709 goto the_end;
2713 /* add the dll and its level */
2714 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2715 dllref->level = level;
2716 strcpy(dllref->name, soname);
2717 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2719 /* add dynamic symbols in dynsym_section */
2720 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2721 sym_bind = ELFW(ST_BIND)(sym->st_info);
2722 if (sym_bind == STB_LOCAL)
2723 continue;
2724 name = (char *) dynstr + sym->st_name;
2725 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2726 sym->st_info, sym->st_other, sym->st_shndx, name);
2729 /* load all referenced DLLs */
2730 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2731 switch(dt->d_tag) {
2732 case DT_NEEDED:
2733 name = (char *) dynstr + dt->d_un.d_val;
2734 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2735 dllref = s1->loaded_dlls[j];
2736 if (!strcmp(name, dllref->name))
2737 goto already_loaded;
2739 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2740 tcc_error_noabort("referenced dll '%s' not found", name);
2741 ret = -1;
2742 goto the_end;
2744 already_loaded:
2745 break;
2748 ret = 0;
2749 the_end:
2750 tcc_free(dynstr);
2751 tcc_free(dynsym);
2752 tcc_free(dynamic);
2753 tcc_free(shdr);
2754 return ret;
2757 #define LD_TOK_NAME 256
2758 #define LD_TOK_EOF (-1)
2760 /* return next ld script token */
2761 static int ld_next(TCCState *s1, char *name, int name_size)
2763 int c;
2764 char *q;
2766 redo:
2767 switch(ch) {
2768 case ' ':
2769 case '\t':
2770 case '\f':
2771 case '\v':
2772 case '\r':
2773 case '\n':
2774 inp();
2775 goto redo;
2776 case '/':
2777 minp();
2778 if (ch == '*') {
2779 file->buf_ptr = parse_comment(file->buf_ptr);
2780 ch = file->buf_ptr[0];
2781 goto redo;
2782 } else {
2783 q = name;
2784 *q++ = '/';
2785 goto parse_name;
2787 break;
2788 case '\\':
2789 ch = handle_eob();
2790 if (ch != '\\')
2791 goto redo;
2792 /* fall through */
2793 /* case 'a' ... 'z': */
2794 case 'a':
2795 case 'b':
2796 case 'c':
2797 case 'd':
2798 case 'e':
2799 case 'f':
2800 case 'g':
2801 case 'h':
2802 case 'i':
2803 case 'j':
2804 case 'k':
2805 case 'l':
2806 case 'm':
2807 case 'n':
2808 case 'o':
2809 case 'p':
2810 case 'q':
2811 case 'r':
2812 case 's':
2813 case 't':
2814 case 'u':
2815 case 'v':
2816 case 'w':
2817 case 'x':
2818 case 'y':
2819 case 'z':
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 '_':
2848 case '.':
2849 case '$':
2850 case '~':
2851 q = name;
2852 parse_name:
2853 for(;;) {
2854 if (!((ch >= 'a' && ch <= 'z') ||
2855 (ch >= 'A' && ch <= 'Z') ||
2856 (ch >= '0' && ch <= '9') ||
2857 strchr("/.-_+=$:\\,~", ch)))
2858 break;
2859 if ((q - name) < name_size - 1) {
2860 *q++ = ch;
2862 minp();
2864 *q = '\0';
2865 c = LD_TOK_NAME;
2866 break;
2867 case CH_EOF:
2868 c = LD_TOK_EOF;
2869 break;
2870 default:
2871 c = ch;
2872 inp();
2873 break;
2875 return c;
2878 static int ld_add_file(TCCState *s1, const char filename[])
2880 if (filename[0] == '/') {
2881 if (CONFIG_SYSROOT[0] == '\0'
2882 && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0)
2883 return 0;
2884 filename = tcc_basename(filename);
2886 return tcc_add_dll(s1, filename, 0);
2889 static inline int new_undef_syms(void)
2891 int ret = 0;
2892 ret = new_undef_sym;
2893 new_undef_sym = 0;
2894 return ret;
2897 static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
2899 char filename[1024], libname[1024];
2900 int t, group, nblibs = 0, ret = 0;
2901 char **libs = NULL;
2903 group = !strcmp(cmd, "GROUP");
2904 if (!as_needed)
2905 new_undef_syms();
2906 t = ld_next(s1, filename, sizeof(filename));
2907 if (t != '(')
2908 expect("(");
2909 t = ld_next(s1, filename, sizeof(filename));
2910 for(;;) {
2911 libname[0] = '\0';
2912 if (t == LD_TOK_EOF) {
2913 tcc_error_noabort("unexpected end of file");
2914 ret = -1;
2915 goto lib_parse_error;
2916 } else if (t == ')') {
2917 break;
2918 } else if (t == '-') {
2919 t = ld_next(s1, filename, sizeof(filename));
2920 if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
2921 tcc_error_noabort("library name expected");
2922 ret = -1;
2923 goto lib_parse_error;
2925 pstrcpy(libname, sizeof libname, &filename[1]);
2926 if (s1->static_link) {
2927 snprintf(filename, sizeof filename, "lib%s.a", libname);
2928 } else {
2929 snprintf(filename, sizeof filename, "lib%s.so", libname);
2931 } else if (t != LD_TOK_NAME) {
2932 tcc_error_noabort("filename expected");
2933 ret = -1;
2934 goto lib_parse_error;
2936 if (!strcmp(filename, "AS_NEEDED")) {
2937 ret = ld_add_file_list(s1, cmd, 1);
2938 if (ret)
2939 goto lib_parse_error;
2940 } else {
2941 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2942 if (!as_needed) {
2943 ret = ld_add_file(s1, filename);
2944 if (ret)
2945 goto lib_parse_error;
2946 if (group) {
2947 /* Add the filename *and* the libname to avoid future conversions */
2948 dynarray_add(&libs, &nblibs, tcc_strdup(filename));
2949 if (libname[0] != '\0')
2950 dynarray_add(&libs, &nblibs, tcc_strdup(libname));
2954 t = ld_next(s1, filename, sizeof(filename));
2955 if (t == ',') {
2956 t = ld_next(s1, filename, sizeof(filename));
2959 if (group && !as_needed) {
2960 while (new_undef_syms()) {
2961 int i;
2963 for (i = 0; i < nblibs; i ++)
2964 ld_add_file(s1, libs[i]);
2967 lib_parse_error:
2968 dynarray_reset(&libs, &nblibs);
2969 return ret;
2972 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2973 files */
2974 ST_FUNC int tcc_load_ldscript(TCCState *s1)
2976 char cmd[64];
2977 char filename[1024];
2978 int t, ret;
2980 ch = handle_eob();
2981 for(;;) {
2982 t = ld_next(s1, cmd, sizeof(cmd));
2983 if (t == LD_TOK_EOF)
2984 return 0;
2985 else if (t != LD_TOK_NAME)
2986 return -1;
2987 if (!strcmp(cmd, "INPUT") ||
2988 !strcmp(cmd, "GROUP")) {
2989 ret = ld_add_file_list(s1, cmd, 0);
2990 if (ret)
2991 return ret;
2992 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
2993 !strcmp(cmd, "TARGET")) {
2994 /* ignore some commands */
2995 t = ld_next(s1, cmd, sizeof(cmd));
2996 if (t != '(')
2997 expect("(");
2998 for(;;) {
2999 t = ld_next(s1, filename, sizeof(filename));
3000 if (t == LD_TOK_EOF) {
3001 tcc_error_noabort("unexpected end of file");
3002 return -1;
3003 } else if (t == ')') {
3004 break;
3007 } else {
3008 return -1;
3011 return 0;
3013 #endif /* !TCC_TARGET_PE */