bitfields: Implement MS compatible layout
[tinycc.git] / tccelf.c
blob003797c9ba2075dc6774bcd563850d5fffe74356
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 *cur_text_section; /* current section where function code is generated */
31 #ifdef CONFIG_TCC_ASM
32 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
33 #endif
34 #ifdef CONFIG_TCC_BCHECK
35 /* bound check related sections */
36 ST_DATA Section *bounds_section; /* contains global data bound description */
37 ST_DATA Section *lbounds_section; /* contains local data bound description */
38 #endif
39 /* symbol sections */
40 ST_DATA Section *symtab_section, *strtab_section;
41 /* debug sections */
42 ST_DATA Section *stab_section, *stabstr_section;
44 /* XXX: avoid static variable */
45 static int new_undef_sym = 0; /* Is there a new undefined sym since last new_undef_sym() */
47 /* ------------------------------------------------------------------------- */
49 ST_FUNC void tccelf_new(TCCState *s)
51 /* no section zero */
52 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
54 /* create standard sections */
55 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
56 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
57 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
59 /* symbols are always generated for linking stage */
60 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
61 ".strtab",
62 ".hashtab", SHF_PRIVATE);
63 strtab_section = symtab_section->link;
64 s->symtab = symtab_section;
66 /* private symbol table for dynamic symbols */
67 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
68 ".dynstrtab",
69 ".dynhashtab", SHF_PRIVATE);
70 get_sym_attr(s, 0, 1);
73 #ifdef CONFIG_TCC_BCHECK
74 ST_FUNC void tccelf_bounds_new(TCCState *s)
76 /* create bounds sections */
77 bounds_section = new_section(s, ".bounds",
78 SHT_PROGBITS, SHF_ALLOC);
79 lbounds_section = new_section(s, ".lbounds",
80 SHT_PROGBITS, SHF_ALLOC);
82 #endif
84 ST_FUNC void tccelf_stab_new(TCCState *s)
86 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
87 stab_section->sh_entsize = sizeof(Stab_Sym);
88 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
89 put_elf_str(stabstr_section, "");
90 stab_section->link = stabstr_section;
91 /* put first entry */
92 put_stabs("", 0, 0, 0, 0);
95 static void free_section(Section *s)
97 tcc_free(s->data);
100 ST_FUNC void tccelf_delete(TCCState *s1)
102 int i;
104 /* free all sections */
105 for(i = 1; i < s1->nb_sections; i++)
106 free_section(s1->sections[i]);
107 dynarray_reset(&s1->sections, &s1->nb_sections);
109 for(i = 0; i < s1->nb_priv_sections; i++)
110 free_section(s1->priv_sections[i]);
111 dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
113 /* free any loaded DLLs */
114 #ifdef TCC_IS_NATIVE
115 for ( i = 0; i < s1->nb_loaded_dlls; i++) {
116 DLLReference *ref = s1->loaded_dlls[i];
117 if ( ref->handle )
118 # ifdef _WIN32
119 FreeLibrary((HMODULE)ref->handle);
120 # else
121 dlclose(ref->handle);
122 # endif
124 #endif
125 /* free loaded dlls array */
126 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
127 tcc_free(s1->sym_attrs);
130 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
132 Section *sec;
134 sec = tcc_mallocz(sizeof(Section) + strlen(name));
135 strcpy(sec->name, name);
136 sec->sh_type = sh_type;
137 sec->sh_flags = sh_flags;
138 switch(sh_type) {
139 case SHT_HASH:
140 case SHT_REL:
141 case SHT_RELA:
142 case SHT_DYNSYM:
143 case SHT_SYMTAB:
144 case SHT_DYNAMIC:
145 sec->sh_addralign = 4;
146 break;
147 case SHT_STRTAB:
148 sec->sh_addralign = 1;
149 break;
150 default:
151 sec->sh_addralign = PTR_SIZE; /* gcc/pcc default aligment */
152 break;
155 if (sh_flags & SHF_PRIVATE) {
156 dynarray_add((void ***)&s1->priv_sections, &s1->nb_priv_sections, sec);
157 } else {
158 sec->sh_num = s1->nb_sections;
159 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
162 return sec;
165 ST_FUNC Section *new_symtab(TCCState *s1,
166 const char *symtab_name, int sh_type, int sh_flags,
167 const char *strtab_name,
168 const char *hash_name, int hash_sh_flags)
170 Section *symtab, *strtab, *hash;
171 int *ptr, nb_buckets;
173 symtab = new_section(s1, symtab_name, sh_type, sh_flags);
174 symtab->sh_entsize = sizeof(ElfW(Sym));
175 strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
176 put_elf_str(strtab, "");
177 symtab->link = strtab;
178 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
180 nb_buckets = 1;
182 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
183 hash->sh_entsize = sizeof(int);
184 symtab->hash = hash;
185 hash->link = symtab;
187 ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
188 ptr[0] = nb_buckets;
189 ptr[1] = 1;
190 memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
191 return symtab;
194 /* realloc section and set its content to zero */
195 ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
197 unsigned long size;
198 unsigned char *data;
200 size = sec->data_allocated;
201 if (size == 0)
202 size = 1;
203 while (size < new_size)
204 size = size * 2;
205 data = tcc_realloc(sec->data, size);
206 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
207 sec->data = data;
208 sec->data_allocated = size;
211 /* reserve at least 'size' bytes in section 'sec' from
212 sec->data_offset. */
213 ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
215 size_t offset, offset1;
217 offset = sec->data_offset;
218 offset1 = offset + size;
219 if (offset1 > sec->data_allocated)
220 section_realloc(sec, offset1);
221 sec->data_offset = offset1;
222 return sec->data + offset;
225 /* reserve at least 'size' bytes from section start */
226 ST_FUNC void section_reserve(Section *sec, unsigned long size)
228 if (size > sec->data_allocated)
229 section_realloc(sec, size);
230 if (size > sec->data_offset)
231 sec->data_offset = size;
234 /* return a reference to a section, and create it if it does not
235 exists */
236 ST_FUNC Section *find_section(TCCState *s1, const char *name)
238 Section *sec;
239 int i;
240 for(i = 1; i < s1->nb_sections; i++) {
241 sec = s1->sections[i];
242 if (!strcmp(name, sec->name))
243 return sec;
245 /* sections are created as PROGBITS */
246 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
249 /* ------------------------------------------------------------------------- */
251 ST_FUNC int put_elf_str(Section *s, const char *sym)
253 int offset, len;
254 char *ptr;
256 len = strlen(sym) + 1;
257 offset = s->data_offset;
258 ptr = section_ptr_add(s, len);
259 memcpy(ptr, sym, len);
260 return offset;
263 /* elf symbol hashing function */
264 static unsigned long elf_hash(const unsigned char *name)
266 unsigned long h = 0, g;
268 while (*name) {
269 h = (h << 4) + *name++;
270 g = h & 0xf0000000;
271 if (g)
272 h ^= g >> 24;
273 h &= ~g;
275 return h;
278 /* rebuild hash table of section s */
279 /* NOTE: we do factorize the hash table code to go faster */
280 static void rebuild_hash(Section *s, unsigned int nb_buckets)
282 ElfW(Sym) *sym;
283 int *ptr, *hash, nb_syms, sym_index, h;
284 unsigned char *strtab;
286 strtab = s->link->data;
287 nb_syms = s->data_offset / sizeof(ElfW(Sym));
289 s->hash->data_offset = 0;
290 ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
291 ptr[0] = nb_buckets;
292 ptr[1] = nb_syms;
293 ptr += 2;
294 hash = ptr;
295 memset(hash, 0, (nb_buckets + 1) * sizeof(int));
296 ptr += nb_buckets + 1;
298 sym = (ElfW(Sym) *)s->data + 1;
299 for(sym_index = 1; sym_index < nb_syms; sym_index++) {
300 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
301 h = elf_hash(strtab + sym->st_name) % nb_buckets;
302 *ptr = hash[h];
303 hash[h] = sym_index;
304 } else {
305 *ptr = 0;
307 ptr++;
308 sym++;
312 /* return the symbol number */
313 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
314 int info, int other, int shndx, const char *name)
316 int name_offset, sym_index;
317 int nbuckets, h;
318 ElfW(Sym) *sym;
319 Section *hs;
321 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
322 if (name)
323 name_offset = put_elf_str(s->link, name);
324 else
325 name_offset = 0;
326 /* XXX: endianness */
327 sym->st_name = name_offset;
328 sym->st_value = value;
329 sym->st_size = size;
330 sym->st_info = info;
331 sym->st_other = other;
332 sym->st_shndx = shndx;
333 sym_index = sym - (ElfW(Sym) *)s->data;
334 hs = s->hash;
335 if (hs) {
336 int *ptr, *base;
337 ptr = section_ptr_add(hs, sizeof(int));
338 base = (int *)hs->data;
339 /* only add global or weak symbols */
340 if (ELFW(ST_BIND)(info) != STB_LOCAL) {
341 /* add another hashing entry */
342 nbuckets = base[0];
343 h = elf_hash((unsigned char *) name) % nbuckets;
344 *ptr = base[2 + h];
345 base[2 + h] = sym_index;
346 base[1]++;
347 /* we resize the hash table */
348 hs->nb_hashed_syms++;
349 if (hs->nb_hashed_syms > 2 * nbuckets) {
350 rebuild_hash(s, 2 * nbuckets);
352 } else {
353 *ptr = 0;
354 base[1]++;
357 return sym_index;
360 /* find global ELF symbol 'name' and return its index. Return 0 if not
361 found. */
362 ST_FUNC int find_elf_sym(Section *s, const char *name)
364 ElfW(Sym) *sym;
365 Section *hs;
366 int nbuckets, sym_index, h;
367 const char *name1;
369 hs = s->hash;
370 if (!hs)
371 return 0;
372 nbuckets = ((int *)hs->data)[0];
373 h = elf_hash((unsigned char *) name) % nbuckets;
374 sym_index = ((int *)hs->data)[2 + h];
375 while (sym_index != 0) {
376 sym = &((ElfW(Sym) *)s->data)[sym_index];
377 name1 = (char *) s->link->data + sym->st_name;
378 if (!strcmp(name, name1))
379 return sym_index;
380 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
382 return 0;
385 /* return elf symbol value, signal error if 'err' is nonzero */
386 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err)
388 int sym_index;
389 ElfW(Sym) *sym;
391 sym_index = find_elf_sym(s->symtab, name);
392 sym = &((ElfW(Sym) *)s->symtab->data)[sym_index];
393 if (!sym_index || sym->st_shndx == SHN_UNDEF) {
394 if (err)
395 tcc_error("%s not defined", name);
396 return 0;
398 return sym->st_value;
401 /* return elf symbol value */
402 LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
404 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0);
407 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
408 /* return elf symbol value or error */
409 ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)
411 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 1);
413 #endif
415 /* add an elf symbol : check if it is already defined and patch
416 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
417 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
418 int info, int other, int shndx, const char *name)
420 ElfW(Sym) *esym;
421 int sym_bind, sym_index, sym_type, esym_bind;
422 unsigned char sym_vis, esym_vis, new_vis;
424 sym_bind = ELFW(ST_BIND)(info);
425 sym_type = ELFW(ST_TYPE)(info);
426 sym_vis = ELFW(ST_VISIBILITY)(other);
428 sym_index = find_elf_sym(s, name);
429 esym = &((ElfW(Sym) *)s->data)[sym_index];
430 if (sym_index && esym->st_value == value && esym->st_size == size
431 && esym->st_info == info && esym->st_other == other
432 && esym->st_shndx == shndx)
433 return sym_index;
435 if (sym_bind != STB_LOCAL) {
436 /* we search global or weak symbols */
437 if (!sym_index)
438 goto do_def;
439 if (esym->st_shndx != SHN_UNDEF) {
440 esym_bind = ELFW(ST_BIND)(esym->st_info);
441 /* propagate the most constraining visibility */
442 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
443 esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
444 if (esym_vis == STV_DEFAULT) {
445 new_vis = sym_vis;
446 } else if (sym_vis == STV_DEFAULT) {
447 new_vis = esym_vis;
448 } else {
449 new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
451 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
452 | new_vis;
453 other = esym->st_other; /* in case we have to patch esym */
454 if (shndx == SHN_UNDEF) {
455 /* ignore adding of undefined symbol if the
456 corresponding symbol is already defined */
457 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
458 /* global overrides weak, so patch */
459 goto do_patch;
460 } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
461 /* weak is ignored if already global */
462 } else if (sym_bind == STB_WEAK && esym_bind == STB_WEAK) {
463 /* keep first-found weak definition, ignore subsequents */
464 } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
465 /* ignore hidden symbols after */
466 } else if ((esym->st_shndx == SHN_COMMON
467 || esym->st_shndx == bss_section->sh_num)
468 && (shndx < SHN_LORESERVE
469 && shndx != bss_section->sh_num)) {
470 /* data symbol gets precedence over common/bss */
471 goto do_patch;
472 } else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
473 /* data symbol keeps precedence over common/bss */
474 } else if (s == tcc_state->dynsymtab_section) {
475 /* we accept that two DLL define the same symbol */
476 } else {
477 #if 0
478 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
479 sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
480 #endif
481 tcc_error_noabort("'%s' defined twice", name);
483 } else {
484 do_patch:
485 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
486 esym->st_shndx = shndx;
487 new_undef_sym = 1;
488 esym->st_value = value;
489 esym->st_size = size;
490 esym->st_other = other;
492 } else {
493 do_def:
494 sym_index = put_elf_sym(s, value, size,
495 ELFW(ST_INFO)(sym_bind, sym_type), other,
496 shndx, name);
498 return sym_index;
501 /* put relocation */
502 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
503 int type, int symbol, addr_t addend)
505 char buf[256];
506 Section *sr;
507 ElfW_Rel *rel;
509 sr = s->reloc;
510 if (!sr) {
511 /* if no relocation section, create it */
512 snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
513 /* if the symtab is allocated, then we consider the relocation
514 are also */
515 sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
516 sr->sh_entsize = sizeof(ElfW_Rel);
517 sr->link = symtab;
518 sr->sh_info = s->sh_num;
519 s->reloc = sr;
521 rel = section_ptr_add(sr, sizeof(ElfW_Rel));
522 rel->r_offset = offset;
523 rel->r_info = ELFW(R_INFO)(symbol, type);
524 #if SHT_RELX == SHT_RELA
525 rel->r_addend = addend;
526 #else
527 if (addend)
528 tcc_error("non-zero addend on REL architecture");
529 #endif
532 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
533 int type, int symbol)
535 put_elf_reloca(symtab, s, offset, type, symbol, 0);
538 /* Remove relocations for section S->reloc starting at oldrelocoffset
539 that are to the same place, retaining the last of them. As side effect
540 the relocations are sorted. Possibly reduces the number of relocs. */
541 ST_FUNC void squeeze_multi_relocs(Section *s, size_t oldrelocoffset)
543 Section *sr = s->reloc;
544 ElfW_Rel *r, *dest;
545 ssize_t a;
546 ElfW(Addr) addr;
548 if (oldrelocoffset + sizeof(*r) >= sr->data_offset)
549 return;
550 /* The relocs we're dealing with are the result of initializer parsing.
551 So they will be mostly in order and there aren't many of them.
552 Secondly we need a stable sort (which qsort isn't). We use
553 a simple insertion sort. */
554 for (a = oldrelocoffset + sizeof(*r); a < sr->data_offset; a += sizeof(*r)) {
555 ssize_t i = a - sizeof(*r);
556 addr = ((ElfW_Rel*)(sr->data + a))->r_offset;
557 for (; i >= (ssize_t)oldrelocoffset &&
558 ((ElfW_Rel*)(sr->data + i))->r_offset > addr; i -= sizeof(*r)) {
559 ElfW_Rel tmp = *(ElfW_Rel*)(sr->data + a);
560 *(ElfW_Rel*)(sr->data + a) = *(ElfW_Rel*)(sr->data + i);
561 *(ElfW_Rel*)(sr->data + i) = tmp;
565 r = (ElfW_Rel*)(sr->data + oldrelocoffset);
566 dest = r;
567 for (; r < (ElfW_Rel*)(sr->data + sr->data_offset); r++) {
568 if (dest->r_offset != r->r_offset)
569 dest++;
570 *dest = *r;
572 sr->data_offset = (unsigned char*)dest - sr->data + sizeof(*r);
575 /* put stab debug information */
577 ST_FUNC void put_stabs(const char *str, int type, int other, int desc,
578 unsigned long value)
580 Stab_Sym *sym;
582 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
583 if (str) {
584 sym->n_strx = put_elf_str(stabstr_section, str);
585 } else {
586 sym->n_strx = 0;
588 sym->n_type = type;
589 sym->n_other = other;
590 sym->n_desc = desc;
591 sym->n_value = value;
594 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc,
595 unsigned long value, Section *sec, int sym_index)
597 put_stabs(str, type, other, desc, value);
598 put_elf_reloc(symtab_section, stab_section,
599 stab_section->data_offset - sizeof(unsigned int),
600 R_DATA_32, sym_index);
603 ST_FUNC void put_stabn(int type, int other, int desc, int value)
605 put_stabs(NULL, type, other, desc, value);
608 ST_FUNC void put_stabd(int type, int other, int desc)
610 put_stabs(NULL, type, other, desc, 0);
613 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
615 int n;
616 struct sym_attr *tab;
618 if (index >= s1->nb_sym_attrs) {
619 if (!alloc)
620 return s1->sym_attrs;
621 /* find immediately bigger power of 2 and reallocate array */
622 n = 1;
623 while (index >= n)
624 n *= 2;
625 tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
626 s1->sym_attrs = tab;
627 memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
628 (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
629 s1->nb_sym_attrs = n;
631 return &s1->sym_attrs[index];
634 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
635 using variable <elem> */
636 #define for_each_elem(sec, startoff, elem, type) \
637 for (elem = (type *) sec->data + startoff; \
638 elem < (type *) (sec->data + sec->data_offset); elem++)
640 /* In an ELF file symbol table, the local symbols must appear below
641 the global and weak ones. Since TCC cannot sort it while generating
642 the code, we must do it after. All the relocation tables are also
643 modified to take into account the symbol table sorting */
644 static void sort_syms(TCCState *s1, Section *s)
646 int *old_to_new_syms;
647 ElfW(Sym) *new_syms;
648 int nb_syms, i;
649 ElfW(Sym) *p, *q;
650 ElfW_Rel *rel;
651 Section *sr;
652 int type, sym_index;
654 nb_syms = s->data_offset / sizeof(ElfW(Sym));
655 new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
656 old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
658 /* first pass for local symbols */
659 p = (ElfW(Sym) *)s->data;
660 q = new_syms;
661 for(i = 0; i < nb_syms; i++) {
662 if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
663 old_to_new_syms[i] = q - new_syms;
664 *q++ = *p;
666 p++;
668 /* save the number of local symbols in section header */
669 s->sh_info = q - new_syms;
671 /* then second pass for non local symbols */
672 p = (ElfW(Sym) *)s->data;
673 for(i = 0; i < nb_syms; i++) {
674 if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
675 old_to_new_syms[i] = q - new_syms;
676 *q++ = *p;
678 p++;
681 /* we copy the new symbols to the old */
682 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
683 tcc_free(new_syms);
685 /* now we modify all the relocations */
686 for(i = 1; i < s1->nb_sections; i++) {
687 sr = s1->sections[i];
688 if (sr->sh_type == SHT_RELX && sr->link == s) {
689 for_each_elem(sr, 0, rel, ElfW_Rel) {
690 sym_index = ELFW(R_SYM)(rel->r_info);
691 type = ELFW(R_TYPE)(rel->r_info);
692 sym_index = old_to_new_syms[sym_index];
693 rel->r_info = ELFW(R_INFO)(sym_index, type);
698 tcc_free(old_to_new_syms);
701 /* relocate common symbols in the .bss section */
702 ST_FUNC void relocate_common_syms(void)
704 ElfW(Sym) *sym;
705 unsigned long offset, align;
707 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
708 if (sym->st_shndx == SHN_COMMON) {
709 /* align symbol */
710 align = sym->st_value;
711 offset = bss_section->data_offset;
712 offset = (offset + align - 1) & -align;
713 sym->st_value = offset;
714 sym->st_shndx = bss_section->sh_num;
715 offset += sym->st_size;
716 bss_section->data_offset = offset;
721 /* relocate symbol table, resolve undefined symbols if do_resolve is
722 true and output error if undefined symbol. */
723 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
725 ElfW(Sym) *sym;
726 int sym_bind, sh_num;
727 const char *name;
729 for_each_elem(symtab, 1, sym, ElfW(Sym)) {
730 sh_num = sym->st_shndx;
731 if (sh_num == SHN_UNDEF) {
732 name = (char *) strtab_section->data + sym->st_name;
733 /* Use ld.so to resolve symbol for us (for tcc -run) */
734 if (do_resolve) {
735 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
736 void *addr = dlsym(RTLD_DEFAULT, name);
737 if (addr) {
738 sym->st_value = (addr_t) addr;
739 #ifdef DEBUG_RELOC
740 printf ("relocate_sym: %s -> 0x%lx\n", name, sym->st_value);
741 #endif
742 goto found;
744 #endif
745 /* if dynamic symbol exist, it will be used in relocate_section */
746 } else if (s1->dynsym && find_elf_sym(s1->dynsym, name))
747 goto found;
748 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
749 it */
750 if (!strcmp(name, "_fp_hw"))
751 goto found;
752 /* only weak symbols are accepted to be undefined. Their
753 value is zero */
754 sym_bind = ELFW(ST_BIND)(sym->st_info);
755 if (sym_bind == STB_WEAK)
756 sym->st_value = 0;
757 else
758 tcc_error_noabort("undefined symbol '%s'", name);
759 } else if (sh_num < SHN_LORESERVE) {
760 /* add section base */
761 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
763 found: ;
767 /* relocate a given section (CPU dependent) by applying the relocations
768 in the associated relocation section */
769 ST_FUNC void relocate_section(TCCState *s1, Section *s)
771 Section *sr = s->reloc;
772 ElfW_Rel *rel;
773 ElfW(Sym) *sym;
774 int type, sym_index;
775 unsigned char *ptr;
776 addr_t tgt, addr;
778 relocate_init(sr);
780 for_each_elem(sr, 0, rel, ElfW_Rel) {
781 ptr = s->data + rel->r_offset;
782 sym_index = ELFW(R_SYM)(rel->r_info);
783 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
784 type = ELFW(R_TYPE)(rel->r_info);
785 tgt = sym->st_value;
786 #if SHT_RELX == SHT_RELA
787 tgt += rel->r_addend;
788 #endif
789 addr = s->sh_addr + rel->r_offset;
790 relocate(s1, rel, type, ptr, addr, tgt);
792 /* if the relocation is allocated, we change its symbol table */
793 if (sr->sh_flags & SHF_ALLOC)
794 sr->link = s1->dynsym;
797 /* relocate relocation table in 'sr' */
798 static void relocate_rel(TCCState *s1, Section *sr)
800 Section *s;
801 ElfW_Rel *rel;
803 s = s1->sections[sr->sh_info];
804 for_each_elem(sr, 0, rel, ElfW_Rel)
805 rel->r_offset += s->sh_addr;
808 /* count the number of dynamic relocations so that we can reserve
809 their space */
810 static int prepare_dynamic_rel(TCCState *s1, Section *sr)
812 ElfW_Rel *rel;
813 int sym_index, type, count;
815 count = 0;
816 for_each_elem(sr, 0, rel, ElfW_Rel) {
817 sym_index = ELFW(R_SYM)(rel->r_info);
818 type = ELFW(R_TYPE)(rel->r_info);
819 switch(type) {
820 #if defined(TCC_TARGET_I386)
821 case R_386_32:
822 #elif defined(TCC_TARGET_X86_64)
823 case R_X86_64_32:
824 case R_X86_64_32S:
825 case R_X86_64_64:
826 #endif
827 count++;
828 break;
829 #if defined(TCC_TARGET_I386)
830 case R_386_PC32:
831 #elif defined(TCC_TARGET_X86_64)
832 case R_X86_64_PC32:
833 #endif
834 if (get_sym_attr(s1, sym_index, 0)->dyn_index)
835 count++;
836 break;
837 default:
838 break;
841 if (count) {
842 /* allocate the section */
843 sr->sh_flags |= SHF_ALLOC;
844 sr->sh_size = count * sizeof(ElfW_Rel);
846 return count;
849 static void build_got(TCCState *s1)
851 /* if no got, then create it */
852 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
853 s1->got->sh_entsize = 4;
854 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
855 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
856 /* keep space for _DYNAMIC pointer and two dummy got entries */
857 section_ptr_add(s1->got, 3 * PTR_SIZE);
860 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
861 in s1->symtab. When creating the dynamic symbol table entry for the GOT
862 relocation, use 'size' and 'info' for the corresponding symbol metadata.
863 Returns the offset of the GOT or (if any) PLT entry. */
864 static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
865 int reloc_type, unsigned long size,
866 int info, int sym_index)
868 int need_plt_entry;
869 const char *name;
870 ElfW(Sym) *sym;
871 struct sym_attr *attr;
872 unsigned got_offset;
873 char plt_name[100];
874 int len;
876 need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
877 attr = get_sym_attr(s1, sym_index, 1);
879 /* In case a function is both called and its address taken 2 GOT entries
880 are created, one for taking the address (GOT) and the other for the PLT
881 entry (PLTGOT). */
882 if (need_plt_entry ? attr->plt_offset : attr->got_offset)
883 return attr;
885 /* create the GOT entry */
886 got_offset = s1->got->data_offset;
887 section_ptr_add(s1->got, PTR_SIZE);
889 /* Create the GOT relocation that will insert the address of the object or
890 function of interest in the GOT entry. This is a static relocation for
891 memory output (dlsym will give us the address of symbols) and dynamic
892 relocation otherwise (executable and DLLs). The relocation should be
893 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
894 associated to a PLT entry) but is currently done at load time for an
895 unknown reason. */
897 sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
898 name = (char *) symtab_section->link->data + sym->st_name;
900 if (s1->dynsym) {
901 if (0 == attr->dyn_index)
902 attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size,
903 info, 0, sym->st_shndx, name);
904 put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type,
905 attr->dyn_index);
906 } else {
907 put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
908 sym_index);
911 if (need_plt_entry) {
912 if (!s1->plt) {
913 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
914 SHF_ALLOC | SHF_EXECINSTR);
915 s1->plt->sh_entsize = 4;
918 attr->plt_offset = create_plt_entry(s1, got_offset, attr);
920 /* create a symbol 'sym@plt' for the PLT jump vector */
921 len = strlen(name);
922 if (len > sizeof plt_name - 5)
923 len = sizeof plt_name - 5;
924 memcpy(plt_name, name, len);
925 strcpy(plt_name + len, "@plt");
926 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
927 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
929 } else {
930 attr->got_offset = got_offset;
933 return attr;
936 /* build GOT and PLT entries */
937 ST_FUNC void build_got_entries(TCCState *s1)
939 Section *s;
940 ElfW_Rel *rel;
941 ElfW(Sym) *sym;
942 int i, type, gotplt_entry, reloc_type, sym_index;
943 struct sym_attr *attr;
945 for(i = 1; i < s1->nb_sections; i++) {
946 s = s1->sections[i];
947 if (s->sh_type != SHT_RELX)
948 continue;
949 /* no need to handle got relocations */
950 if (s->link != symtab_section)
951 continue;
952 for_each_elem(s, 0, rel, ElfW_Rel) {
953 type = ELFW(R_TYPE)(rel->r_info);
954 gotplt_entry = gotplt_entry_type(type);
955 sym_index = ELFW(R_SYM)(rel->r_info);
956 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
958 if (gotplt_entry == NO_GOTPLT_ENTRY) {
959 #ifdef TCC_TARGET_I386
960 if (type == R_386_32 && sym->st_shndx == SHN_UNDEF) {
961 /* the i386 generator uses the plt address for function
962 pointers into .so. This may break pointer equality
963 but helps to keep it simple */
964 char *name = (char *)symtab_section->link->data + sym->st_name;
965 int index = find_elf_sym(s1->dynsymtab_section, name);
966 ElfW(Sym) *esym = (ElfW(Sym) *)s1->dynsymtab_section->data + index;
967 if (index
968 && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
969 || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
970 && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC)))
971 goto jmp_slot;
973 #endif
974 continue;
977 /* Automatically create PLT/GOT [entry] it is an undefined reference
978 (resolved at runtime), or the symbol is absolute, probably created
979 by tcc_add_symbol, and thus on 64-bit targets might be too far
980 from application code */
981 if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
982 if (sym->st_shndx == SHN_UNDEF) {
983 if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
984 continue;
985 } else if (!(sym->st_shndx == SHN_ABS && PTR_SIZE == 8))
986 continue;
989 #ifdef TCC_TARGET_X86_64
990 if (type == R_X86_64_PLT32 &&
991 ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT) {
992 rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
993 continue;
995 #endif
996 if (code_reloc(type)) {
997 #ifdef TCC_TARGET_I386
998 jmp_slot:
999 #endif
1000 reloc_type = R_JMP_SLOT;
1001 } else
1002 reloc_type = R_GLOB_DAT;
1004 if (!s1->got)
1005 build_got(s1);
1007 if (gotplt_entry == BUILD_GOT_ONLY)
1008 continue;
1010 attr = put_got_entry(s1, reloc_type, type, sym->st_size, sym->st_info,
1011 sym_index);
1013 if (reloc_type == R_JMP_SLOT)
1014 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
1019 /* put dynamic tag */
1020 static void put_dt(Section *dynamic, int dt, addr_t val)
1022 ElfW(Dyn) *dyn;
1023 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1024 dyn->d_tag = dt;
1025 dyn->d_un.d_val = val;
1028 #ifndef TCC_TARGET_PE
1029 static void add_init_array_defines(TCCState *s1, const char *section_name)
1031 Section *s;
1032 long end_offset;
1033 char sym_start[1024];
1034 char sym_end[1024];
1036 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1037 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1039 s = find_section(s1, section_name);
1040 if (!s) {
1041 end_offset = 0;
1042 s = data_section;
1043 } else {
1044 end_offset = s->data_offset;
1047 set_elf_sym(symtab_section,
1048 0, 0,
1049 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1050 s->sh_num, sym_start);
1051 set_elf_sym(symtab_section,
1052 end_offset, 0,
1053 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1054 s->sh_num, sym_end);
1056 #endif
1058 static int tcc_add_support(TCCState *s1, const char *filename)
1060 char buf[1024];
1061 snprintf(buf, sizeof(buf), "%s/"TCC_ARCH_DIR"%s", s1->tcc_lib_path, filename);
1062 return tcc_add_file(s1, buf);
1065 ST_FUNC void tcc_add_bcheck(TCCState *s1)
1067 #ifdef CONFIG_TCC_BCHECK
1068 addr_t *ptr;
1069 int sym_index;
1071 if (0 == s1->do_bounds_check)
1072 return;
1073 /* XXX: add an object file to do that */
1074 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1075 *ptr = 0;
1076 set_elf_sym(symtab_section, 0, 0,
1077 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1078 bounds_section->sh_num, "__bounds_start");
1079 /* pull bcheck.o from libtcc1.a */
1080 sym_index = set_elf_sym(symtab_section, 0, 0,
1081 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1082 SHN_UNDEF, "__bound_init");
1083 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1084 /* add 'call __bound_init()' in .init section */
1085 Section *init_section = find_section(s1, ".init");
1086 unsigned char *pinit = section_ptr_add(init_section, 5);
1087 pinit[0] = 0xe8;
1088 write32le(pinit + 1, -4);
1089 put_elf_reloc(symtab_section, init_section,
1090 init_section->data_offset - 4, R_386_PC32, sym_index);
1091 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1093 #endif
1096 /* add tcc runtime libraries */
1097 ST_FUNC void tcc_add_runtime(TCCState *s1)
1099 tcc_add_bcheck(s1);
1100 tcc_add_pragma_libs(s1);
1101 /* add libc */
1102 if (!s1->nostdlib) {
1103 tcc_add_library_err(s1, "c");
1104 #ifdef CONFIG_USE_LIBGCC
1105 if (!s1->static_link) {
1106 tcc_add_file(s1, TCC_LIBGCC);
1108 #endif
1109 tcc_add_support(s1, "libtcc1.a");
1110 /* add crt end if not memory output */
1111 if (s1->output_type != TCC_OUTPUT_MEMORY)
1112 tcc_add_crt(s1, "crtn.o");
1116 /* add various standard linker symbols (must be done after the
1117 sections are filled (for example after allocating common
1118 symbols)) */
1119 ST_FUNC void tcc_add_linker_symbols(TCCState *s1)
1121 char buf[1024];
1122 int i;
1123 Section *s;
1125 set_elf_sym(symtab_section,
1126 text_section->data_offset, 0,
1127 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1128 text_section->sh_num, "_etext");
1129 set_elf_sym(symtab_section,
1130 data_section->data_offset, 0,
1131 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1132 data_section->sh_num, "_edata");
1133 set_elf_sym(symtab_section,
1134 bss_section->data_offset, 0,
1135 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1136 bss_section->sh_num, "_end");
1137 #ifndef TCC_TARGET_PE
1138 /* horrible new standard ldscript defines */
1139 add_init_array_defines(s1, ".preinit_array");
1140 add_init_array_defines(s1, ".init_array");
1141 add_init_array_defines(s1, ".fini_array");
1142 #endif
1144 /* add start and stop symbols for sections whose name can be
1145 expressed in C */
1146 for(i = 1; i < s1->nb_sections; i++) {
1147 s = s1->sections[i];
1148 if (s->sh_type == SHT_PROGBITS &&
1149 (s->sh_flags & SHF_ALLOC)) {
1150 const char *p;
1151 int ch;
1153 /* check if section name can be expressed in C */
1154 p = s->name;
1155 for(;;) {
1156 ch = *p;
1157 if (!ch)
1158 break;
1159 if (!isid(ch) && !isnum(ch))
1160 goto next_sec;
1161 p++;
1163 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1164 set_elf_sym(symtab_section,
1165 0, 0,
1166 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1167 s->sh_num, buf);
1168 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1169 set_elf_sym(symtab_section,
1170 s->data_offset, 0,
1171 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1172 s->sh_num, buf);
1174 next_sec: ;
1178 static void tcc_output_binary(TCCState *s1, FILE *f,
1179 const int *sec_order)
1181 Section *s;
1182 int i, offset, size;
1184 offset = 0;
1185 for(i=1;i<s1->nb_sections;i++) {
1186 s = s1->sections[sec_order[i]];
1187 if (s->sh_type != SHT_NOBITS &&
1188 (s->sh_flags & SHF_ALLOC)) {
1189 while (offset < s->sh_offset) {
1190 fputc(0, f);
1191 offset++;
1193 size = s->sh_size;
1194 fwrite(s->data, 1, size, f);
1195 offset += size;
1200 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1201 #define HAVE_PHDR 1
1202 #define EXTRA_RELITEMS 14
1203 #else
1204 #define HAVE_PHDR 1
1205 #define EXTRA_RELITEMS 9
1206 #endif
1208 ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1210 int sym_index = ELFW(R_SYM) (rel->r_info);
1211 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1212 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1213 unsigned offset = attr->got_offset;
1215 if (0 == offset)
1216 return;
1217 section_reserve(s1->got, offset + PTR_SIZE);
1218 #ifdef TCC_TARGET_X86_64
1219 write64le(s1->got->data + offset, sym->st_value);
1220 #else
1221 write32le(s1->got->data + offset, sym->st_value);
1222 #endif
1225 /* Perform relocation to GOT or PLT entries */
1226 ST_FUNC void fill_got(TCCState *s1)
1228 Section *s;
1229 ElfW_Rel *rel;
1230 int i;
1232 for(i = 1; i < s1->nb_sections; i++) {
1233 s = s1->sections[i];
1234 if (s->sh_type != SHT_RELX)
1235 continue;
1236 /* no need to handle got relocations */
1237 if (s->link != symtab_section)
1238 continue;
1239 for_each_elem(s, 0, rel, ElfW_Rel) {
1240 switch (ELFW(R_TYPE) (rel->r_info)) {
1241 case R_X86_64_GOT32:
1242 case R_X86_64_GOTPCREL:
1243 case R_X86_64_GOTPCRELX:
1244 case R_X86_64_REX_GOTPCRELX:
1245 case R_X86_64_PLT32:
1246 fill_got_entry(s1, rel);
1247 break;
1253 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1254 in shared libraries and export non local defined symbols to shared libraries
1255 if -rdynamic switch was given on command line */
1256 static void bind_exe_dynsyms(TCCState *s1)
1258 const char *name;
1259 int sym_index, index;
1260 ElfW(Sym) *sym, *esym;
1261 int type;
1263 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1264 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1265 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1266 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1267 if (sym->st_shndx == SHN_UNDEF) {
1268 name = (char *) symtab_section->link->data + sym->st_name;
1269 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1270 if (sym_index) {
1271 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1272 type = ELFW(ST_TYPE)(esym->st_info);
1273 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
1274 /* Indirect functions shall have STT_FUNC type in executable
1275 * dynsym section. Indeed, a dlsym call following a lazy
1276 * resolution would pick the symbol value from the
1277 * executable dynsym entry which would contain the address
1278 * of the function wanted by the caller of dlsym instead of
1279 * the address of the function that would return that
1280 * address */
1281 put_elf_sym(s1->dynsym, 0, esym->st_size,
1282 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
1283 name);
1284 } else if (type == STT_OBJECT) {
1285 unsigned long offset;
1286 ElfW(Sym) *dynsym;
1287 offset = bss_section->data_offset;
1288 /* XXX: which alignment ? */
1289 offset = (offset + 16 - 1) & -16;
1290 set_elf_sym (s1->symtab, offset, esym->st_size,
1291 esym->st_info, 0, bss_section->sh_num, name);
1292 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1293 esym->st_info, 0, bss_section->sh_num,
1294 name);
1296 /* Ensure R_COPY works for weak symbol aliases */
1297 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1298 for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
1299 if ((dynsym->st_value == esym->st_value)
1300 && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
1301 char *dynname = (char *) s1->dynsymtab_section->link->data
1302 + dynsym->st_name;
1303 put_elf_sym(s1->dynsym, offset, dynsym->st_size,
1304 dynsym->st_info, 0,
1305 bss_section->sh_num, dynname);
1306 break;
1311 put_elf_reloc(s1->dynsym, bss_section,
1312 offset, R_COPY, index);
1313 offset += esym->st_size;
1314 bss_section->data_offset = offset;
1316 } else {
1317 /* STB_WEAK undefined symbols are accepted */
1318 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1319 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1320 !strcmp(name, "_fp_hw")) {
1321 } else {
1322 tcc_error_noabort("undefined symbol '%s'", name);
1325 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1326 /* if -rdynamic option, then export all non local symbols */
1327 name = (char *) symtab_section->link->data + sym->st_name;
1328 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1329 0, sym->st_shndx, name);
1334 /* Bind symbols of libraries: export all non local symbols of executable that
1335 are referenced by shared libraries. The reason is that the dynamic loader
1336 search symbol first in executable and then in libraries. Therefore a
1337 reference to a symbol already defined by a library can still be resolved by
1338 a symbol in the executable. */
1339 static void bind_libs_dynsyms(TCCState *s1)
1341 const char *name;
1342 int sym_index;
1343 ElfW(Sym) *sym, *esym;
1345 for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
1346 name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
1347 sym_index = find_elf_sym(symtab_section, name);
1348 /* XXX: avoid adding a symbol if already present because of
1349 -rdynamic ? */
1350 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1351 if (sym_index && sym->st_shndx != SHN_UNDEF)
1352 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1353 0, sym->st_shndx, name);
1354 else if (esym->st_shndx == SHN_UNDEF) {
1355 /* weak symbols can stay undefined */
1356 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
1357 tcc_warning("undefined dynamic symbol '%s'", name);
1362 /* Export all non local symbols. This is used by shared libraries so that the
1363 non local symbols they define can resolve a reference in another shared
1364 library or in the executable. Correspondingly, it allows undefined local
1365 symbols to be resolved by other shared libraries or by the executable. */
1366 static void export_global_syms(TCCState *s1)
1368 int dynindex, index;
1369 const char *name;
1370 ElfW(Sym) *sym;
1372 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1373 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1374 name = (char *) symtab_section->link->data + sym->st_name;
1375 dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1376 sym->st_info, 0, sym->st_shndx, name);
1377 index = sym - (ElfW(Sym) *) symtab_section->data;
1378 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1383 /* Allocate strings for section names and decide if an unallocated section
1384 should be output.
1385 NOTE: the strsec section comes last, so its size is also correct ! */
1386 static void alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
1388 int i;
1389 Section *s;
1391 /* Allocate strings for section names */
1392 for(i = 1; i < s1->nb_sections; i++) {
1393 s = s1->sections[i];
1394 s->sh_name = put_elf_str(strsec, s->name);
1395 /* when generating a DLL, we include relocations but we may
1396 patch them */
1397 if (file_type == TCC_OUTPUT_DLL &&
1398 s->sh_type == SHT_RELX &&
1399 !(s->sh_flags & SHF_ALLOC)) {
1400 /* gr: avoid bogus relocs for empty (debug) sections */
1401 if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
1402 prepare_dynamic_rel(s1, s);
1403 else if (s1->do_debug)
1404 s->sh_size = s->data_offset;
1405 } else if (s1->do_debug ||
1406 file_type == TCC_OUTPUT_OBJ ||
1407 (s->sh_flags & SHF_ALLOC) ||
1408 i == (s1->nb_sections - 1)) {
1409 /* we output all sections if debug or object file */
1410 s->sh_size = s->data_offset;
1415 /* Info to be copied in dynamic section */
1416 struct dyn_inf {
1417 Section *dynamic;
1418 Section *dynstr;
1419 unsigned long dyn_rel_off;
1420 addr_t rel_addr;
1421 addr_t rel_size;
1422 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1423 addr_t bss_addr;
1424 addr_t bss_size;
1425 #endif
1428 /* Assign sections to segments and decide how are sections laid out when loaded
1429 in memory. This function also fills corresponding program headers. */
1430 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1431 Section *interp, Section* strsec,
1432 struct dyn_inf *dyninf, int *sec_order)
1434 int i, j, k, file_type, sh_order_index, file_offset;
1435 unsigned long s_align;
1436 long long tmp;
1437 addr_t addr;
1438 ElfW(Phdr) *ph;
1439 Section *s;
1441 file_type = s1->output_type;
1442 sh_order_index = 1;
1443 file_offset = 0;
1444 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1445 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1446 s_align = ELF_PAGE_SIZE;
1447 if (s1->section_align)
1448 s_align = s1->section_align;
1450 if (phnum > 0) {
1451 if (s1->has_text_addr) {
1452 int a_offset, p_offset;
1453 addr = s1->text_addr;
1454 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1455 ELF_PAGE_SIZE */
1456 a_offset = (int) (addr & (s_align - 1));
1457 p_offset = file_offset & (s_align - 1);
1458 if (a_offset < p_offset)
1459 a_offset += s_align;
1460 file_offset += (a_offset - p_offset);
1461 } else {
1462 if (file_type == TCC_OUTPUT_DLL)
1463 addr = 0;
1464 else
1465 addr = ELF_START_ADDR;
1466 /* compute address after headers */
1467 addr += (file_offset & (s_align - 1));
1470 ph = &phdr[0];
1471 /* Leave one program headers for the program interpreter and one for
1472 the program header table itself if needed. These are done later as
1473 they require section layout to be done first. */
1474 if (interp)
1475 ph += 1 + HAVE_PHDR;
1477 /* dynamic relocation table information, for .dynamic section */
1478 dyninf->rel_addr = dyninf->rel_size = 0;
1479 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1480 dyninf->bss_addr = dyninf->bss_size = 0;
1481 #endif
1483 for(j = 0; j < 2; j++) {
1484 ph->p_type = PT_LOAD;
1485 if (j == 0)
1486 ph->p_flags = PF_R | PF_X;
1487 else
1488 ph->p_flags = PF_R | PF_W;
1489 ph->p_align = s_align;
1491 /* Decide the layout of sections loaded in memory. This must
1492 be done before program headers are filled since they contain
1493 info about the layout. We do the following ordering: interp,
1494 symbol tables, relocations, progbits, nobits */
1495 /* XXX: do faster and simpler sorting */
1496 for(k = 0; k < 5; k++) {
1497 for(i = 1; i < s1->nb_sections; i++) {
1498 s = s1->sections[i];
1499 /* compute if section should be included */
1500 if (j == 0) {
1501 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1502 SHF_ALLOC)
1503 continue;
1504 } else {
1505 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1506 (SHF_ALLOC | SHF_WRITE))
1507 continue;
1509 if (s == interp) {
1510 if (k != 0)
1511 continue;
1512 } else if (s->sh_type == SHT_DYNSYM ||
1513 s->sh_type == SHT_STRTAB ||
1514 s->sh_type == SHT_HASH) {
1515 if (k != 1)
1516 continue;
1517 } else if (s->sh_type == SHT_RELX) {
1518 if (k != 2)
1519 continue;
1520 } else if (s->sh_type == SHT_NOBITS) {
1521 if (k != 4)
1522 continue;
1523 } else {
1524 if (k != 3)
1525 continue;
1527 sec_order[sh_order_index++] = i;
1529 /* section matches: we align it and add its size */
1530 tmp = addr;
1531 addr = (addr + s->sh_addralign - 1) &
1532 ~(s->sh_addralign - 1);
1533 file_offset += (int) ( addr - tmp );
1534 s->sh_offset = file_offset;
1535 s->sh_addr = addr;
1537 /* update program header infos */
1538 if (ph->p_offset == 0) {
1539 ph->p_offset = file_offset;
1540 ph->p_vaddr = addr;
1541 ph->p_paddr = ph->p_vaddr;
1543 /* update dynamic relocation infos */
1544 if (s->sh_type == SHT_RELX) {
1545 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1546 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1547 dyninf->rel_addr = addr;
1548 dyninf->rel_size += s->sh_size; /* XXX only first rel. */
1550 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1551 dyninf->bss_addr = addr;
1552 dyninf->bss_size = s->sh_size; /* XXX only first rel. */
1554 #else
1555 if (dyninf->rel_size == 0)
1556 dyninf->rel_addr = addr;
1557 dyninf->rel_size += s->sh_size;
1558 #endif
1560 addr += s->sh_size;
1561 if (s->sh_type != SHT_NOBITS)
1562 file_offset += s->sh_size;
1565 if (j == 0) {
1566 /* Make the first PT_LOAD segment include the program
1567 headers itself (and the ELF header as well), it'll
1568 come out with same memory use but will make various
1569 tools like binutils strip work better. */
1570 ph->p_offset &= ~(ph->p_align - 1);
1571 ph->p_vaddr &= ~(ph->p_align - 1);
1572 ph->p_paddr &= ~(ph->p_align - 1);
1574 ph->p_filesz = file_offset - ph->p_offset;
1575 ph->p_memsz = addr - ph->p_vaddr;
1576 ph++;
1577 if (j == 0) {
1578 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1579 /* if in the middle of a page, we duplicate the page in
1580 memory so that one copy is RX and the other is RW */
1581 if ((addr & (s_align - 1)) != 0)
1582 addr += s_align;
1583 } else {
1584 addr = (addr + s_align - 1) & ~(s_align - 1);
1585 file_offset = (file_offset + s_align - 1) & ~(s_align - 1);
1591 /* all other sections come after */
1592 for(i = 1; i < s1->nb_sections; i++) {
1593 s = s1->sections[i];
1594 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1595 continue;
1596 sec_order[sh_order_index++] = i;
1598 file_offset = (file_offset + s->sh_addralign - 1) &
1599 ~(s->sh_addralign - 1);
1600 s->sh_offset = file_offset;
1601 if (s->sh_type != SHT_NOBITS)
1602 file_offset += s->sh_size;
1605 return file_offset;
1608 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
1609 Section *dynamic)
1611 ElfW(Phdr) *ph;
1613 /* if interpreter, then add corresponding program header */
1614 if (interp) {
1615 ph = &phdr[0];
1617 if (HAVE_PHDR)
1619 int len = phnum * sizeof(ElfW(Phdr));
1621 ph->p_type = PT_PHDR;
1622 ph->p_offset = sizeof(ElfW(Ehdr));
1623 ph->p_vaddr = interp->sh_addr - len;
1624 ph->p_paddr = ph->p_vaddr;
1625 ph->p_filesz = ph->p_memsz = len;
1626 ph->p_flags = PF_R | PF_X;
1627 ph->p_align = 4; /* interp->sh_addralign; */
1628 ph++;
1631 ph->p_type = PT_INTERP;
1632 ph->p_offset = interp->sh_offset;
1633 ph->p_vaddr = interp->sh_addr;
1634 ph->p_paddr = ph->p_vaddr;
1635 ph->p_filesz = interp->sh_size;
1636 ph->p_memsz = interp->sh_size;
1637 ph->p_flags = PF_R;
1638 ph->p_align = interp->sh_addralign;
1641 /* if dynamic section, then add corresponding program header */
1642 if (dynamic) {
1643 ph = &phdr[phnum - 1];
1645 ph->p_type = PT_DYNAMIC;
1646 ph->p_offset = dynamic->sh_offset;
1647 ph->p_vaddr = dynamic->sh_addr;
1648 ph->p_paddr = ph->p_vaddr;
1649 ph->p_filesz = dynamic->sh_size;
1650 ph->p_memsz = dynamic->sh_size;
1651 ph->p_flags = PF_R | PF_W;
1652 ph->p_align = dynamic->sh_addralign;
1656 /* Fill the dynamic section with tags describing the address and size of
1657 sections */
1658 static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf)
1660 Section *dynamic;
1662 dynamic = dyninf->dynamic;
1664 /* put dynamic section entries */
1665 dynamic->data_offset = dyninf->dyn_rel_off;
1666 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1667 put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
1668 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1669 put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
1670 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1671 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1672 put_dt(dynamic, DT_RELA, dyninf->rel_addr);
1673 put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
1674 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1675 #else
1676 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1677 put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
1678 put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size);
1679 put_dt(dynamic, DT_JMPREL, dyninf->rel_addr);
1680 put_dt(dynamic, DT_PLTREL, DT_REL);
1681 put_dt(dynamic, DT_REL, dyninf->bss_addr);
1682 put_dt(dynamic, DT_RELSZ, dyninf->bss_size);
1683 #else
1684 put_dt(dynamic, DT_REL, dyninf->rel_addr);
1685 put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
1686 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1687 #endif
1688 #endif
1689 if (s1->do_debug)
1690 put_dt(dynamic, DT_DEBUG, 0);
1691 put_dt(dynamic, DT_NULL, 0);
1694 /* Relocate remaining sections and symbols (that is those not related to
1695 dynamic linking) */
1696 static int final_sections_reloc(TCCState *s1)
1698 int i;
1699 Section *s;
1701 relocate_syms(s1, s1->symtab, 0);
1703 if (s1->nb_errors != 0)
1704 return -1;
1706 /* relocate sections */
1707 /* XXX: ignore sections with allocated relocations ? */
1708 for(i = 1; i < s1->nb_sections; i++) {
1709 s = s1->sections[i];
1710 #ifdef TCC_TARGET_I386
1711 if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
1712 /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC
1713 checking is removed */
1714 #else
1715 if (s->reloc && s != s1->got)
1716 /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */
1717 #endif
1718 relocate_section(s1, s);
1721 /* relocate relocation entries if the relocation tables are
1722 allocated in the executable */
1723 for(i = 1; i < s1->nb_sections; i++) {
1724 s = s1->sections[i];
1725 if ((s->sh_flags & SHF_ALLOC) &&
1726 s->sh_type == SHT_RELX) {
1727 relocate_rel(s1, s);
1730 return 0;
1733 /* Create an ELF file on disk.
1734 This function handle ELF specific layout requirements */
1735 static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
1736 int file_offset, int *sec_order)
1738 int i, shnum, offset, size, file_type;
1739 Section *s;
1740 ElfW(Ehdr) ehdr;
1741 ElfW(Shdr) shdr, *sh;
1743 file_type = s1->output_type;
1744 shnum = s1->nb_sections;
1746 memset(&ehdr, 0, sizeof(ehdr));
1748 if (phnum > 0) {
1749 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1750 ehdr.e_phnum = phnum;
1751 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1754 /* align to 4 */
1755 file_offset = (file_offset + 3) & -4;
1757 /* fill header */
1758 ehdr.e_ident[0] = ELFMAG0;
1759 ehdr.e_ident[1] = ELFMAG1;
1760 ehdr.e_ident[2] = ELFMAG2;
1761 ehdr.e_ident[3] = ELFMAG3;
1762 ehdr.e_ident[4] = ELFCLASSW;
1763 ehdr.e_ident[5] = ELFDATA2LSB;
1764 ehdr.e_ident[6] = EV_CURRENT;
1765 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1766 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1767 #endif
1768 #ifdef TCC_TARGET_ARM
1769 #ifdef TCC_ARM_EABI
1770 ehdr.e_ident[EI_OSABI] = 0;
1771 ehdr.e_flags = EF_ARM_EABI_VER4;
1772 if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
1773 ehdr.e_flags |= EF_ARM_HASENTRY;
1774 if (s1->float_abi == ARM_HARD_FLOAT)
1775 ehdr.e_flags |= EF_ARM_VFP_FLOAT;
1776 else
1777 ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
1778 #else
1779 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1780 #endif
1781 #endif
1782 switch(file_type) {
1783 default:
1784 case TCC_OUTPUT_EXE:
1785 ehdr.e_type = ET_EXEC;
1786 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
1787 break;
1788 case TCC_OUTPUT_DLL:
1789 ehdr.e_type = ET_DYN;
1790 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1791 break;
1792 case TCC_OUTPUT_OBJ:
1793 ehdr.e_type = ET_REL;
1794 break;
1796 ehdr.e_machine = EM_TCC_TARGET;
1797 ehdr.e_version = EV_CURRENT;
1798 ehdr.e_shoff = file_offset;
1799 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1800 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1801 ehdr.e_shnum = shnum;
1802 ehdr.e_shstrndx = shnum - 1;
1804 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1805 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1806 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1808 sort_syms(s1, symtab_section);
1809 for(i = 1; i < s1->nb_sections; i++) {
1810 s = s1->sections[sec_order[i]];
1811 if (s->sh_type != SHT_NOBITS) {
1812 while (offset < s->sh_offset) {
1813 fputc(0, f);
1814 offset++;
1816 size = s->sh_size;
1817 if (size)
1818 fwrite(s->data, 1, size, f);
1819 offset += size;
1823 /* output section headers */
1824 while (offset < ehdr.e_shoff) {
1825 fputc(0, f);
1826 offset++;
1829 for(i = 0; i < s1->nb_sections; i++) {
1830 sh = &shdr;
1831 memset(sh, 0, sizeof(ElfW(Shdr)));
1832 s = s1->sections[i];
1833 if (s) {
1834 sh->sh_name = s->sh_name;
1835 sh->sh_type = s->sh_type;
1836 sh->sh_flags = s->sh_flags;
1837 sh->sh_entsize = s->sh_entsize;
1838 sh->sh_info = s->sh_info;
1839 if (s->link)
1840 sh->sh_link = s->link->sh_num;
1841 sh->sh_addralign = s->sh_addralign;
1842 sh->sh_addr = s->sh_addr;
1843 sh->sh_offset = s->sh_offset;
1844 sh->sh_size = s->sh_size;
1846 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1850 /* Write an elf, coff or "binary" file */
1851 static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
1852 ElfW(Phdr) *phdr, int file_offset, int *sec_order)
1854 int fd, mode, file_type;
1855 FILE *f;
1857 file_type = s1->output_type;
1858 if (file_type == TCC_OUTPUT_OBJ)
1859 mode = 0666;
1860 else
1861 mode = 0777;
1862 unlink(filename);
1863 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1864 if (fd < 0) {
1865 tcc_error_noabort("could not write '%s'", filename);
1866 return -1;
1868 f = fdopen(fd, "wb");
1869 if (s1->verbose)
1870 printf("<- %s\n", filename);
1872 #ifdef TCC_TARGET_COFF
1873 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
1874 tcc_output_coff(s1, f);
1875 else
1876 #endif
1877 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1878 tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
1879 else
1880 tcc_output_binary(s1, f, sec_order);
1881 fclose(f);
1883 return 0;
1886 /* Output an elf, coff or binary file */
1887 /* XXX: suppress unneeded sections */
1888 static int elf_output_file(TCCState *s1, const char *filename)
1890 int i, ret, phnum, shnum, file_type, file_offset, *sec_order;
1891 struct dyn_inf dyninf;
1892 ElfW(Phdr) *phdr;
1893 ElfW(Sym) *sym;
1894 Section *strsec, *interp, *dynamic, *dynstr;
1896 file_type = s1->output_type;
1897 s1->nb_errors = 0;
1899 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
1900 if (file_type != TCC_OUTPUT_OBJ) {
1901 tcc_add_runtime(s1);
1904 phdr = NULL;
1905 sec_order = NULL;
1906 interp = dynamic = dynstr = NULL; /* avoid warning */
1907 dyninf.dyn_rel_off = 0; /* avoid warning */
1909 if (file_type != TCC_OUTPUT_OBJ) {
1910 relocate_common_syms();
1912 tcc_add_linker_symbols(s1);
1914 if (!s1->static_link) {
1915 if (file_type == TCC_OUTPUT_EXE) {
1916 char *ptr;
1917 /* allow override the dynamic loader */
1918 const char *elfint = getenv("LD_SO");
1919 if (elfint == NULL)
1920 elfint = DEFAULT_ELFINTERP(s1);
1921 /* add interpreter section only if executable */
1922 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
1923 interp->sh_addralign = 1;
1924 ptr = section_ptr_add(interp, 1 + strlen(elfint));
1925 strcpy(ptr, elfint);
1928 /* add dynamic symbol table */
1929 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
1930 ".dynstr",
1931 ".hash", SHF_ALLOC);
1932 dynstr = s1->dynsym->link;
1934 /* add dynamic section */
1935 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
1936 SHF_ALLOC | SHF_WRITE);
1937 dynamic->link = dynstr;
1938 dynamic->sh_entsize = sizeof(ElfW(Dyn));
1940 build_got(s1);
1942 if (file_type == TCC_OUTPUT_EXE) {
1943 bind_exe_dynsyms(s1);
1945 if (s1->nb_errors) {
1946 ret = -1;
1947 goto the_end;
1950 bind_libs_dynsyms(s1);
1951 } else /* shared library case: simply export all global symbols */
1952 export_global_syms(s1);
1954 build_got_entries(s1);
1956 /* add a list of needed dlls */
1957 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1958 DLLReference *dllref = s1->loaded_dlls[i];
1959 if (dllref->level == 0)
1960 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
1963 if (s1->rpath)
1964 put_dt(dynamic, DT_RPATH, put_elf_str(dynstr, s1->rpath));
1966 /* XXX: currently, since we do not handle PIC code, we
1967 must relocate the readonly segments */
1968 if (file_type == TCC_OUTPUT_DLL) {
1969 if (s1->soname)
1970 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
1971 put_dt(dynamic, DT_TEXTREL, 0);
1974 if (s1->symbolic)
1975 put_dt(dynamic, DT_SYMBOLIC, 0);
1977 /* add necessary space for other entries */
1978 dyninf.dyn_rel_off = dynamic->data_offset;
1979 dynamic->data_offset += sizeof(ElfW(Dyn)) * EXTRA_RELITEMS;
1980 } else {
1981 /* still need to build got entries in case of static link */
1982 build_got_entries(s1);
1986 /* we add a section for symbols */
1987 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
1988 put_elf_str(strsec, "");
1990 /* compute number of sections */
1991 shnum = s1->nb_sections;
1993 /* this array is used to reorder sections in the output file */
1994 sec_order = tcc_malloc(sizeof(int) * shnum);
1995 sec_order[0] = 0;
1997 /* compute number of program headers */
1998 switch(file_type) {
1999 default:
2000 case TCC_OUTPUT_OBJ:
2001 phnum = 0;
2002 break;
2003 case TCC_OUTPUT_EXE:
2004 if (!s1->static_link)
2005 phnum = 4 + HAVE_PHDR;
2006 else
2007 phnum = 2;
2008 break;
2009 case TCC_OUTPUT_DLL:
2010 phnum = 3;
2011 break;
2014 /* Allocate strings for section names */
2015 alloc_sec_names(s1, file_type, strsec);
2017 /* allocate program segment headers */
2018 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
2020 /* compute section to program header mapping */
2021 file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2022 sec_order);
2024 /* Fill remaining program header and finalize relocation related to dynamic
2025 linking. */
2026 if (phnum > 0) {
2027 fill_unloadable_phdr(phdr, phnum, interp, dynamic);
2028 if (dynamic) {
2029 dyninf.dynamic = dynamic;
2030 dyninf.dynstr = dynstr;
2032 fill_dynamic(s1, &dyninf);
2034 /* put in GOT the dynamic section address and relocate PLT */
2035 write32le(s1->got->data, dynamic->sh_addr);
2036 if (file_type == TCC_OUTPUT_EXE
2037 || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
2038 relocate_plt(s1);
2040 /* relocate symbols in .dynsym now that final addresses are known */
2041 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
2042 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
2043 /* do symbol relocation */
2044 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
2050 /* if building executable or DLL, then relocate each section
2051 except the GOT which is already relocated */
2052 if (file_type != TCC_OUTPUT_OBJ) {
2053 ret = final_sections_reloc(s1);
2054 if (ret)
2055 goto the_end;
2058 /* Perform relocation to GOT or PLT entries */
2059 if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2060 fill_got(s1);
2062 /* Create the ELF file with name 'filename' */
2063 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2064 the_end:
2065 tcc_free(sec_order);
2066 tcc_free(phdr);
2067 return ret;
2070 LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
2072 int ret;
2073 #ifdef TCC_TARGET_PE
2074 if (s->output_type != TCC_OUTPUT_OBJ) {
2075 ret = pe_output_file(s, filename);
2076 } else
2077 #endif
2078 ret = elf_output_file(s, filename);
2079 return ret;
2082 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2084 void *data;
2086 data = tcc_malloc(size);
2087 lseek(fd, file_offset, SEEK_SET);
2088 read(fd, data, size);
2089 return data;
2092 typedef struct SectionMergeInfo {
2093 Section *s; /* corresponding existing section */
2094 unsigned long offset; /* offset of the new section in the existing section */
2095 uint8_t new_section; /* true if section 's' was added */
2096 uint8_t link_once; /* true if link once section */
2097 } SectionMergeInfo;
2099 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
2101 int size = read(fd, h, sizeof *h);
2102 if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
2103 if (h->e_type == ET_REL)
2104 return AFF_BINTYPE_REL;
2105 if (h->e_type == ET_DYN)
2106 return AFF_BINTYPE_DYN;
2107 } else if (size >= 8) {
2108 if (0 == memcmp(h, ARMAG, 8))
2109 return AFF_BINTYPE_AR;
2110 #ifdef TCC_TARGET_COFF
2111 if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC)
2112 return AFF_BINTYPE_C67;
2113 #endif
2115 return 0;
2118 /* load an object file and merge it with current files */
2119 /* XXX: handle correctly stab (debug) info */
2120 ST_FUNC int tcc_load_object_file(TCCState *s1,
2121 int fd, unsigned long file_offset)
2123 ElfW(Ehdr) ehdr;
2124 ElfW(Shdr) *shdr, *sh;
2125 int size, i, j, offset, offseti, nb_syms, sym_index, ret;
2126 unsigned char *strsec, *strtab;
2127 int *old_to_new_syms;
2128 char *sh_name, *name;
2129 SectionMergeInfo *sm_table, *sm;
2130 ElfW(Sym) *sym, *symtab;
2131 ElfW_Rel *rel;
2132 Section *s;
2134 int stab_index;
2135 int stabstr_index;
2137 stab_index = stabstr_index = 0;
2139 lseek(fd, file_offset, SEEK_SET);
2140 if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL)
2141 goto fail1;
2142 /* test CPU specific stuff */
2143 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2144 ehdr.e_machine != EM_TCC_TARGET) {
2145 fail1:
2146 tcc_error_noabort("invalid object file");
2147 return -1;
2149 /* read sections */
2150 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2151 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2152 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2154 /* load section names */
2155 sh = &shdr[ehdr.e_shstrndx];
2156 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2158 /* load symtab and strtab */
2159 old_to_new_syms = NULL;
2160 symtab = NULL;
2161 strtab = NULL;
2162 nb_syms = 0;
2163 for(i = 1; i < ehdr.e_shnum; i++) {
2164 sh = &shdr[i];
2165 if (sh->sh_type == SHT_SYMTAB) {
2166 if (symtab) {
2167 tcc_error_noabort("object must contain only one symtab");
2168 fail:
2169 ret = -1;
2170 goto the_end;
2172 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2173 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2174 sm_table[i].s = symtab_section;
2176 /* now load strtab */
2177 sh = &shdr[sh->sh_link];
2178 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2182 /* now examine each section and try to merge its content with the
2183 ones in memory */
2184 for(i = 1; i < ehdr.e_shnum; i++) {
2185 /* no need to examine section name strtab */
2186 if (i == ehdr.e_shstrndx)
2187 continue;
2188 sh = &shdr[i];
2189 sh_name = (char *) strsec + sh->sh_name;
2190 /* ignore sections types we do not handle */
2191 if (sh->sh_type != SHT_PROGBITS &&
2192 sh->sh_type != SHT_RELX &&
2193 #ifdef TCC_ARM_EABI
2194 sh->sh_type != SHT_ARM_EXIDX &&
2195 #endif
2196 sh->sh_type != SHT_NOBITS &&
2197 sh->sh_type != SHT_PREINIT_ARRAY &&
2198 sh->sh_type != SHT_INIT_ARRAY &&
2199 sh->sh_type != SHT_FINI_ARRAY &&
2200 strcmp(sh_name, ".stabstr")
2202 continue;
2203 if (sh->sh_addralign < 1)
2204 sh->sh_addralign = 1;
2205 /* find corresponding section, if any */
2206 for(j = 1; j < s1->nb_sections;j++) {
2207 s = s1->sections[j];
2208 if (!strcmp(s->name, sh_name)) {
2209 if (!strncmp(sh_name, ".gnu.linkonce",
2210 sizeof(".gnu.linkonce") - 1)) {
2211 /* if a 'linkonce' section is already present, we
2212 do not add it again. It is a little tricky as
2213 symbols can still be defined in
2214 it. */
2215 sm_table[i].link_once = 1;
2216 goto next;
2217 } else {
2218 goto found;
2222 /* not found: create new section */
2223 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
2224 /* take as much info as possible from the section. sh_link and
2225 sh_info will be updated later */
2226 s->sh_addralign = sh->sh_addralign;
2227 s->sh_entsize = sh->sh_entsize;
2228 sm_table[i].new_section = 1;
2229 found:
2230 if (sh->sh_type != s->sh_type) {
2231 tcc_error_noabort("invalid section type");
2232 goto fail;
2235 /* align start of section */
2236 offset = s->data_offset;
2238 if (0 == strcmp(sh_name, ".stab")) {
2239 stab_index = i;
2240 goto no_align;
2242 if (0 == strcmp(sh_name, ".stabstr")) {
2243 stabstr_index = i;
2244 goto no_align;
2247 size = sh->sh_addralign - 1;
2248 offset = (offset + size) & ~size;
2249 if (sh->sh_addralign > s->sh_addralign)
2250 s->sh_addralign = sh->sh_addralign;
2251 s->data_offset = offset;
2252 no_align:
2253 sm_table[i].offset = offset;
2254 sm_table[i].s = s;
2255 /* concatenate sections */
2256 size = sh->sh_size;
2257 if (sh->sh_type != SHT_NOBITS) {
2258 unsigned char *ptr;
2259 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2260 ptr = section_ptr_add(s, size);
2261 read(fd, ptr, size);
2262 } else {
2263 s->data_offset += size;
2265 next: ;
2268 /* gr relocate stab strings */
2269 if (stab_index && stabstr_index) {
2270 Stab_Sym *a, *b;
2271 unsigned o;
2272 s = sm_table[stab_index].s;
2273 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2274 b = (Stab_Sym *)(s->data + s->data_offset);
2275 o = sm_table[stabstr_index].offset;
2276 while (a < b)
2277 a->n_strx += o, a++;
2280 /* second short pass to update sh_link and sh_info fields of new
2281 sections */
2282 for(i = 1; i < ehdr.e_shnum; i++) {
2283 s = sm_table[i].s;
2284 if (!s || !sm_table[i].new_section)
2285 continue;
2286 sh = &shdr[i];
2287 if (sh->sh_link > 0)
2288 s->link = sm_table[sh->sh_link].s;
2289 if (sh->sh_type == SHT_RELX) {
2290 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2291 /* update backward link */
2292 s1->sections[s->sh_info]->reloc = s;
2295 sm = sm_table;
2297 /* resolve symbols */
2298 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2300 sym = symtab + 1;
2301 for(i = 1; i < nb_syms; i++, sym++) {
2302 if (sym->st_shndx != SHN_UNDEF &&
2303 sym->st_shndx < SHN_LORESERVE) {
2304 sm = &sm_table[sym->st_shndx];
2305 if (sm->link_once) {
2306 /* if a symbol is in a link once section, we use the
2307 already defined symbol. It is very important to get
2308 correct relocations */
2309 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2310 name = (char *) strtab + sym->st_name;
2311 sym_index = find_elf_sym(symtab_section, name);
2312 if (sym_index)
2313 old_to_new_syms[i] = sym_index;
2315 continue;
2317 /* if no corresponding section added, no need to add symbol */
2318 if (!sm->s)
2319 continue;
2320 /* convert section number */
2321 sym->st_shndx = sm->s->sh_num;
2322 /* offset value */
2323 sym->st_value += sm->offset;
2325 /* add symbol */
2326 name = (char *) strtab + sym->st_name;
2327 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
2328 sym->st_info, sym->st_other,
2329 sym->st_shndx, name);
2330 old_to_new_syms[i] = sym_index;
2333 /* third pass to patch relocation entries */
2334 for(i = 1; i < ehdr.e_shnum; i++) {
2335 s = sm_table[i].s;
2336 if (!s)
2337 continue;
2338 sh = &shdr[i];
2339 offset = sm_table[i].offset;
2340 switch(s->sh_type) {
2341 case SHT_RELX:
2342 /* take relocation offset information */
2343 offseti = sm_table[sh->sh_info].offset;
2344 for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) {
2345 int type;
2346 unsigned sym_index;
2347 /* convert symbol index */
2348 type = ELFW(R_TYPE)(rel->r_info);
2349 sym_index = ELFW(R_SYM)(rel->r_info);
2350 /* NOTE: only one symtab assumed */
2351 if (sym_index >= nb_syms)
2352 goto invalid_reloc;
2353 sym_index = old_to_new_syms[sym_index];
2354 /* ignore link_once in rel section. */
2355 if (!sym_index && !sm->link_once
2356 #ifdef TCC_TARGET_ARM
2357 && type != R_ARM_V4BX
2358 #endif
2360 invalid_reloc:
2361 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2362 i, strsec + sh->sh_name, rel->r_offset);
2363 goto fail;
2365 rel->r_info = ELFW(R_INFO)(sym_index, type);
2366 /* offset the relocation offset */
2367 rel->r_offset += offseti;
2368 #ifdef TCC_TARGET_ARM
2369 /* Jumps and branches from a Thumb code to a PLT entry need
2370 special handling since PLT entries are ARM code.
2371 Unconditional bl instructions referencing PLT entries are
2372 handled by converting these instructions into blx
2373 instructions. Other case of instructions referencing a PLT
2374 entry require to add a Thumb stub before the PLT entry to
2375 switch to ARM mode. We set bit plt_thumb_stub of the
2376 attribute of a symbol to indicate such a case. */
2377 if (type == R_ARM_THM_JUMP24)
2378 get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
2379 #endif
2381 break;
2382 default:
2383 break;
2387 ret = 0;
2388 the_end:
2389 tcc_free(symtab);
2390 tcc_free(strtab);
2391 tcc_free(old_to_new_syms);
2392 tcc_free(sm_table);
2393 tcc_free(strsec);
2394 tcc_free(shdr);
2395 return ret;
2398 typedef struct ArchiveHeader {
2399 char ar_name[16]; /* name of this member */
2400 char ar_date[12]; /* file mtime */
2401 char ar_uid[6]; /* owner uid; printed as decimal */
2402 char ar_gid[6]; /* owner gid; printed as decimal */
2403 char ar_mode[8]; /* file mode, printed as octal */
2404 char ar_size[10]; /* file size, printed as decimal */
2405 char ar_fmag[2]; /* should contain ARFMAG */
2406 } ArchiveHeader;
2408 static int get_be32(const uint8_t *b)
2410 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2413 static long get_be64(const uint8_t *b)
2415 long long ret = get_be32(b);
2416 ret = (ret << 32) | (unsigned)get_be32(b+4);
2417 return (long)ret;
2420 /* load only the objects which resolve undefined symbols */
2421 static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
2423 long i, bound, nsyms, sym_index, off, ret;
2424 uint8_t *data;
2425 const char *ar_names, *p;
2426 const uint8_t *ar_index;
2427 ElfW(Sym) *sym;
2429 data = tcc_malloc(size);
2430 if (read(fd, data, size) != size)
2431 goto fail;
2432 nsyms = entrysize == 4 ? get_be32(data) : get_be64(data);
2433 ar_index = data + entrysize;
2434 ar_names = (char *) ar_index + nsyms * entrysize;
2436 do {
2437 bound = 0;
2438 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2439 sym_index = find_elf_sym(symtab_section, p);
2440 if(sym_index) {
2441 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2442 if(sym->st_shndx == SHN_UNDEF) {
2443 off = (entrysize == 4
2444 ? get_be32(ar_index + i * 4)
2445 : get_be64(ar_index + i * 8))
2446 + sizeof(ArchiveHeader);
2447 ++bound;
2448 if(tcc_load_object_file(s1, fd, off) < 0) {
2449 fail:
2450 ret = -1;
2451 goto the_end;
2456 } while(bound);
2457 ret = 0;
2458 the_end:
2459 tcc_free(data);
2460 return ret;
2463 /* load a '.a' file */
2464 ST_FUNC int tcc_load_archive(TCCState *s1, int fd)
2466 ArchiveHeader hdr;
2467 char ar_size[11];
2468 char ar_name[17];
2469 char magic[8];
2470 int size, len, i;
2471 unsigned long file_offset;
2473 /* skip magic which was already checked */
2474 read(fd, magic, sizeof(magic));
2476 for(;;) {
2477 len = read(fd, &hdr, sizeof(hdr));
2478 if (len == 0)
2479 break;
2480 if (len != sizeof(hdr)) {
2481 tcc_error_noabort("invalid archive");
2482 return -1;
2484 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2485 ar_size[sizeof(hdr.ar_size)] = '\0';
2486 size = strtol(ar_size, NULL, 0);
2487 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2488 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2489 if (ar_name[i] != ' ')
2490 break;
2492 ar_name[i + 1] = '\0';
2493 file_offset = lseek(fd, 0, SEEK_CUR);
2494 /* align to even */
2495 size = (size + 1) & ~1;
2496 if (!strcmp(ar_name, "/")) {
2497 /* coff symbol table : we handle it */
2498 if(s1->alacarte_link)
2499 return tcc_load_alacarte(s1, fd, size, 4);
2500 } else if (!strcmp(ar_name, "/SYM64/")) {
2501 if(s1->alacarte_link)
2502 return tcc_load_alacarte(s1, fd, size, 8);
2503 } else {
2504 ElfW(Ehdr) ehdr;
2505 if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
2506 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2507 return -1;
2510 lseek(fd, file_offset + size, SEEK_SET);
2512 return 0;
2515 #ifndef TCC_TARGET_PE
2516 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2517 is referenced by the user (so it should be added as DT_NEEDED in
2518 the generated ELF file) */
2519 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2521 ElfW(Ehdr) ehdr;
2522 ElfW(Shdr) *shdr, *sh, *sh1;
2523 int i, j, nb_syms, nb_dts, sym_bind, ret;
2524 ElfW(Sym) *sym, *dynsym;
2525 ElfW(Dyn) *dt, *dynamic;
2526 unsigned char *dynstr;
2527 const char *name, *soname;
2528 DLLReference *dllref;
2530 read(fd, &ehdr, sizeof(ehdr));
2532 /* test CPU specific stuff */
2533 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2534 ehdr.e_machine != EM_TCC_TARGET) {
2535 tcc_error_noabort("bad architecture");
2536 return -1;
2539 /* read sections */
2540 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2542 /* load dynamic section and dynamic symbols */
2543 nb_syms = 0;
2544 nb_dts = 0;
2545 dynamic = NULL;
2546 dynsym = NULL; /* avoid warning */
2547 dynstr = NULL; /* avoid warning */
2548 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2549 switch(sh->sh_type) {
2550 case SHT_DYNAMIC:
2551 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2552 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2553 break;
2554 case SHT_DYNSYM:
2555 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2556 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2557 sh1 = &shdr[sh->sh_link];
2558 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2559 break;
2560 default:
2561 break;
2565 /* compute the real library name */
2566 soname = tcc_basename(filename);
2568 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2569 if (dt->d_tag == DT_SONAME) {
2570 soname = (char *) dynstr + dt->d_un.d_val;
2574 /* if the dll is already loaded, do not load it */
2575 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2576 dllref = s1->loaded_dlls[i];
2577 if (!strcmp(soname, dllref->name)) {
2578 /* but update level if needed */
2579 if (level < dllref->level)
2580 dllref->level = level;
2581 ret = 0;
2582 goto the_end;
2586 /* add the dll and its level */
2587 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2588 dllref->level = level;
2589 strcpy(dllref->name, soname);
2590 dynarray_add((void ***)&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2592 /* add dynamic symbols in dynsym_section */
2593 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2594 sym_bind = ELFW(ST_BIND)(sym->st_info);
2595 if (sym_bind == STB_LOCAL)
2596 continue;
2597 name = (char *) dynstr + sym->st_name;
2598 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2599 sym->st_info, sym->st_other, sym->st_shndx, name);
2602 /* load all referenced DLLs */
2603 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2604 switch(dt->d_tag) {
2605 case DT_NEEDED:
2606 name = (char *) dynstr + dt->d_un.d_val;
2607 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2608 dllref = s1->loaded_dlls[j];
2609 if (!strcmp(name, dllref->name))
2610 goto already_loaded;
2612 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2613 tcc_error_noabort("referenced dll '%s' not found", name);
2614 ret = -1;
2615 goto the_end;
2617 already_loaded:
2618 break;
2621 ret = 0;
2622 the_end:
2623 tcc_free(dynstr);
2624 tcc_free(dynsym);
2625 tcc_free(dynamic);
2626 tcc_free(shdr);
2627 return ret;
2630 #define LD_TOK_NAME 256
2631 #define LD_TOK_EOF (-1)
2633 /* return next ld script token */
2634 static int ld_next(TCCState *s1, char *name, int name_size)
2636 int c;
2637 char *q;
2639 redo:
2640 switch(ch) {
2641 case ' ':
2642 case '\t':
2643 case '\f':
2644 case '\v':
2645 case '\r':
2646 case '\n':
2647 inp();
2648 goto redo;
2649 case '/':
2650 minp();
2651 if (ch == '*') {
2652 file->buf_ptr = parse_comment(file->buf_ptr);
2653 ch = file->buf_ptr[0];
2654 goto redo;
2655 } else {
2656 q = name;
2657 *q++ = '/';
2658 goto parse_name;
2660 break;
2661 case '\\':
2662 ch = handle_eob();
2663 if (ch != '\\')
2664 goto redo;
2665 /* fall through */
2666 /* case 'a' ... 'z': */
2667 case 'a':
2668 case 'b':
2669 case 'c':
2670 case 'd':
2671 case 'e':
2672 case 'f':
2673 case 'g':
2674 case 'h':
2675 case 'i':
2676 case 'j':
2677 case 'k':
2678 case 'l':
2679 case 'm':
2680 case 'n':
2681 case 'o':
2682 case 'p':
2683 case 'q':
2684 case 'r':
2685 case 's':
2686 case 't':
2687 case 'u':
2688 case 'v':
2689 case 'w':
2690 case 'x':
2691 case 'y':
2692 case 'z':
2693 /* case 'A' ... 'z': */
2694 case 'A':
2695 case 'B':
2696 case 'C':
2697 case 'D':
2698 case 'E':
2699 case 'F':
2700 case 'G':
2701 case 'H':
2702 case 'I':
2703 case 'J':
2704 case 'K':
2705 case 'L':
2706 case 'M':
2707 case 'N':
2708 case 'O':
2709 case 'P':
2710 case 'Q':
2711 case 'R':
2712 case 'S':
2713 case 'T':
2714 case 'U':
2715 case 'V':
2716 case 'W':
2717 case 'X':
2718 case 'Y':
2719 case 'Z':
2720 case '_':
2721 case '.':
2722 case '$':
2723 case '~':
2724 q = name;
2725 parse_name:
2726 for(;;) {
2727 if (!((ch >= 'a' && ch <= 'z') ||
2728 (ch >= 'A' && ch <= 'Z') ||
2729 (ch >= '0' && ch <= '9') ||
2730 strchr("/.-_+=$:\\,~", ch)))
2731 break;
2732 if ((q - name) < name_size - 1) {
2733 *q++ = ch;
2735 minp();
2737 *q = '\0';
2738 c = LD_TOK_NAME;
2739 break;
2740 case CH_EOF:
2741 c = LD_TOK_EOF;
2742 break;
2743 default:
2744 c = ch;
2745 inp();
2746 break;
2748 return c;
2751 static int ld_add_file(TCCState *s1, const char filename[])
2753 int ret;
2755 ret = tcc_add_file_internal(s1, filename, AFF_TYPE_BIN);
2756 if (ret)
2757 ret = tcc_add_dll(s1, filename, 0);
2758 return ret;
2761 static inline int new_undef_syms(void)
2763 int ret = 0;
2764 ret = new_undef_sym;
2765 new_undef_sym = 0;
2766 return ret;
2769 static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
2771 char filename[1024], libname[1024];
2772 int t, group, nblibs = 0, ret = 0;
2773 char **libs = NULL;
2775 group = !strcmp(cmd, "GROUP");
2776 if (!as_needed)
2777 new_undef_syms();
2778 t = ld_next(s1, filename, sizeof(filename));
2779 if (t != '(')
2780 expect("(");
2781 t = ld_next(s1, filename, sizeof(filename));
2782 for(;;) {
2783 libname[0] = '\0';
2784 if (t == LD_TOK_EOF) {
2785 tcc_error_noabort("unexpected end of file");
2786 ret = -1;
2787 goto lib_parse_error;
2788 } else if (t == ')') {
2789 break;
2790 } else if (t == '-') {
2791 t = ld_next(s1, filename, sizeof(filename));
2792 if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
2793 tcc_error_noabort("library name expected");
2794 ret = -1;
2795 goto lib_parse_error;
2797 pstrcpy(libname, sizeof libname, &filename[1]);
2798 if (s1->static_link) {
2799 snprintf(filename, sizeof filename, "lib%s.a", libname);
2800 } else {
2801 snprintf(filename, sizeof filename, "lib%s.so", libname);
2803 } else if (t != LD_TOK_NAME) {
2804 tcc_error_noabort("filename expected");
2805 ret = -1;
2806 goto lib_parse_error;
2808 if (!strcmp(filename, "AS_NEEDED")) {
2809 ret = ld_add_file_list(s1, cmd, 1);
2810 if (ret)
2811 goto lib_parse_error;
2812 } else {
2813 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2814 if (!as_needed) {
2815 ret = ld_add_file(s1, filename);
2816 if (ret)
2817 goto lib_parse_error;
2818 if (group) {
2819 /* Add the filename *and* the libname to avoid future conversions */
2820 dynarray_add((void ***) &libs, &nblibs, tcc_strdup(filename));
2821 if (libname[0] != '\0')
2822 dynarray_add((void ***) &libs, &nblibs, tcc_strdup(libname));
2826 t = ld_next(s1, filename, sizeof(filename));
2827 if (t == ',') {
2828 t = ld_next(s1, filename, sizeof(filename));
2831 if (group && !as_needed) {
2832 while (new_undef_syms()) {
2833 int i;
2835 for (i = 0; i < nblibs; i ++)
2836 ld_add_file(s1, libs[i]);
2839 lib_parse_error:
2840 dynarray_reset(&libs, &nblibs);
2841 return ret;
2844 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2845 files */
2846 ST_FUNC int tcc_load_ldscript(TCCState *s1)
2848 char cmd[64];
2849 char filename[1024];
2850 int t, ret;
2852 ch = handle_eob();
2853 for(;;) {
2854 t = ld_next(s1, cmd, sizeof(cmd));
2855 if (t == LD_TOK_EOF)
2856 return 0;
2857 else if (t != LD_TOK_NAME)
2858 return -1;
2859 if (!strcmp(cmd, "INPUT") ||
2860 !strcmp(cmd, "GROUP")) {
2861 ret = ld_add_file_list(s1, cmd, 0);
2862 if (ret)
2863 return ret;
2864 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
2865 !strcmp(cmd, "TARGET")) {
2866 /* ignore some commands */
2867 t = ld_next(s1, cmd, sizeof(cmd));
2868 if (t != '(')
2869 expect("(");
2870 for(;;) {
2871 t = ld_next(s1, filename, sizeof(filename));
2872 if (t == LD_TOK_EOF) {
2873 tcc_error_noabort("unexpected end of file");
2874 return -1;
2875 } else if (t == ')') {
2876 break;
2879 } else {
2880 return -1;
2883 return 0;
2885 #endif /* !TCC_TARGET_PE */