Add linker's --export-dynamic flag alias
[tinycc.git] / tccelf.c
blobead8eedfd14268249e5b38f057fbb5d7f746ee90
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;
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 /* special flag to indicate that the section should not be linked to the other ones */
49 #define SHF_PRIVATE 0x80000000
50 /* section is dynsymtab_section */
51 #define SHF_DYNSYM 0x40000000
53 /* ------------------------------------------------------------------------- */
55 ST_FUNC void tccelf_new(TCCState *s)
57 /* no section zero */
58 dynarray_add(&s->sections, &s->nb_sections, NULL);
60 /* create standard sections */
61 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
62 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
63 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
64 common_section = new_section(s, ".common", SHT_NOBITS, SHF_PRIVATE);
65 common_section->sh_num = SHN_COMMON;
67 /* symbols are always generated for linking stage */
68 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
69 ".strtab",
70 ".hashtab", SHF_PRIVATE);
71 s->symtab = symtab_section;
73 /* private symbol table for dynamic symbols */
74 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE|SHF_DYNSYM,
75 ".dynstrtab",
76 ".dynhashtab", SHF_PRIVATE);
77 get_sym_attr(s, 0, 1);
80 #ifdef CONFIG_TCC_BCHECK
81 ST_FUNC void tccelf_bounds_new(TCCState *s)
83 /* create bounds sections */
84 bounds_section = new_section(s, ".bounds",
85 SHT_PROGBITS, SHF_ALLOC);
86 lbounds_section = new_section(s, ".lbounds",
87 SHT_PROGBITS, SHF_ALLOC);
89 #endif
91 ST_FUNC void tccelf_stab_new(TCCState *s)
93 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
94 stab_section->sh_entsize = sizeof(Stab_Sym);
95 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
96 put_elf_str(stabstr_section, "");
97 stab_section->link = stabstr_section;
98 /* put first entry */
99 put_stabs("", 0, 0, 0, 0);
102 static void free_section(Section *s)
104 tcc_free(s->data);
107 ST_FUNC void tccelf_delete(TCCState *s1)
109 int i;
111 /* free all sections */
112 for(i = 1; i < s1->nb_sections; i++)
113 free_section(s1->sections[i]);
114 dynarray_reset(&s1->sections, &s1->nb_sections);
116 for(i = 0; i < s1->nb_priv_sections; i++)
117 free_section(s1->priv_sections[i]);
118 dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
120 /* free any loaded DLLs */
121 #ifdef TCC_IS_NATIVE
122 for ( i = 0; i < s1->nb_loaded_dlls; i++) {
123 DLLReference *ref = s1->loaded_dlls[i];
124 if ( ref->handle )
125 # ifdef _WIN32
126 FreeLibrary((HMODULE)ref->handle);
127 # else
128 dlclose(ref->handle);
129 # endif
131 #endif
132 /* free loaded dlls array */
133 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
134 tcc_free(s1->sym_attrs);
136 symtab_section = NULL; /* for tccrun.c:rt_printline() */
139 /* save section data state */
140 ST_FUNC void tccelf_begin_file(TCCState *s1)
142 Section *s; int i;
143 for (i = 1; i < s1->nb_sections; i++) {
144 s = s1->sections[i];
145 s->sh_offset = s->data_offset;
147 /* disable symbol hashing during compilation */
148 s = s1->symtab, s->reloc = s->hash, s->hash = NULL;
149 #if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE
150 s1->uw_sym = 0;
151 #endif
154 /* At the end of compilation, convert any UNDEF syms to global, and merge
155 with previously existing symbols */
156 ST_FUNC void tccelf_end_file(TCCState *s1)
158 Section *s = s1->symtab;
159 int first_sym, nb_syms, *tr, i;
161 first_sym = s->sh_offset / sizeof (ElfSym);
162 nb_syms = s->data_offset / sizeof (ElfSym) - first_sym;
163 s->data_offset = s->sh_offset;
164 s->link->data_offset = s->link->sh_offset;
165 s->hash = s->reloc, s->reloc = NULL;
166 tr = tcc_mallocz(nb_syms * sizeof *tr);
168 for (i = 0; i < nb_syms; ++i) {
169 ElfSym *sym = (ElfSym*)s->data + first_sym + i;
170 if (sym->st_shndx == SHN_UNDEF
171 && ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)
172 sym->st_info = ELFW(ST_INFO)(STB_GLOBAL, ELFW(ST_TYPE)(sym->st_info));
173 tr[i] = set_elf_sym(s, sym->st_value, sym->st_size, sym->st_info,
174 sym->st_other, sym->st_shndx, s->link->data + sym->st_name);
176 /* now update relocations */
177 for (i = 1; i < s1->nb_sections; i++) {
178 Section *sr = s1->sections[i];
179 if (sr->sh_type == SHT_RELX && sr->link == s) {
180 ElfW_Rel *rel = (ElfW_Rel*)(sr->data + sr->sh_offset);
181 ElfW_Rel *rel_end = (ElfW_Rel*)(sr->data + sr->data_offset);
182 for (; rel < rel_end; ++rel) {
183 int n = ELFW(R_SYM)(rel->r_info) - first_sym;
184 //if (n < 0) tcc_error("internal: invalid symbol index in relocation");
185 rel->r_info = ELFW(R_INFO)(tr[n], ELFW(R_TYPE)(rel->r_info));
189 tcc_free(tr);
192 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
194 Section *sec;
196 sec = tcc_mallocz(sizeof(Section) + strlen(name));
197 strcpy(sec->name, name);
198 sec->sh_type = sh_type;
199 sec->sh_flags = sh_flags;
200 switch(sh_type) {
201 case SHT_HASH:
202 case SHT_REL:
203 case SHT_RELA:
204 case SHT_DYNSYM:
205 case SHT_SYMTAB:
206 case SHT_DYNAMIC:
207 sec->sh_addralign = 4;
208 break;
209 case SHT_STRTAB:
210 sec->sh_addralign = 1;
211 break;
212 default:
213 sec->sh_addralign = PTR_SIZE; /* gcc/pcc default alignment */
214 break;
217 if (sh_flags & SHF_PRIVATE) {
218 dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, sec);
219 } else {
220 sec->sh_num = s1->nb_sections;
221 dynarray_add(&s1->sections, &s1->nb_sections, sec);
224 return sec;
227 ST_FUNC Section *new_symtab(TCCState *s1,
228 const char *symtab_name, int sh_type, int sh_flags,
229 const char *strtab_name,
230 const char *hash_name, int hash_sh_flags)
232 Section *symtab, *strtab, *hash;
233 int *ptr, nb_buckets;
235 symtab = new_section(s1, symtab_name, sh_type, sh_flags);
236 symtab->sh_entsize = sizeof(ElfW(Sym));
237 strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
238 put_elf_str(strtab, "");
239 symtab->link = strtab;
240 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
242 nb_buckets = 1;
244 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
245 hash->sh_entsize = sizeof(int);
246 symtab->hash = hash;
247 hash->link = symtab;
249 ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
250 ptr[0] = nb_buckets;
251 ptr[1] = 1;
252 memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
253 return symtab;
256 /* realloc section and set its content to zero */
257 ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
259 unsigned long size;
260 unsigned char *data;
262 size = sec->data_allocated;
263 if (size == 0)
264 size = 1;
265 while (size < new_size)
266 size = size * 2;
267 data = tcc_realloc(sec->data, size);
268 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
269 sec->data = data;
270 sec->data_allocated = size;
273 /* reserve at least 'size' bytes aligned per 'align' in section
274 'sec' from current offset, and return the aligned offset */
275 ST_FUNC size_t section_add(Section *sec, addr_t size, int align)
277 size_t offset, offset1;
279 offset = (sec->data_offset + align - 1) & -align;
280 offset1 = offset + size;
281 if (sec->sh_type != SHT_NOBITS && offset1 > sec->data_allocated)
282 section_realloc(sec, offset1);
283 sec->data_offset = offset1;
284 if (align > sec->sh_addralign)
285 sec->sh_addralign = align;
286 return offset;
289 /* reserve at least 'size' bytes in section 'sec' from
290 sec->data_offset. */
291 ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
293 size_t offset = section_add(sec, size, 1);
294 return sec->data + offset;
297 /* reserve at least 'size' bytes from section start */
298 ST_FUNC void section_reserve(Section *sec, unsigned long size)
300 if (size > sec->data_allocated)
301 section_realloc(sec, size);
302 if (size > sec->data_offset)
303 sec->data_offset = size;
306 /* return a reference to a section, and create it if it does not
307 exists */
308 ST_FUNC Section *find_section(TCCState *s1, const char *name)
310 Section *sec;
311 int i;
312 for(i = 1; i < s1->nb_sections; i++) {
313 sec = s1->sections[i];
314 if (!strcmp(name, sec->name))
315 return sec;
317 /* sections are created as PROGBITS */
318 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
321 /* ------------------------------------------------------------------------- */
323 ST_FUNC int put_elf_str(Section *s, const char *sym)
325 int offset, len;
326 char *ptr;
328 len = strlen(sym) + 1;
329 offset = s->data_offset;
330 ptr = section_ptr_add(s, len);
331 memmove(ptr, sym, len);
332 return offset;
335 /* elf symbol hashing function */
336 static unsigned long elf_hash(const unsigned char *name)
338 unsigned long h = 0, g;
340 while (*name) {
341 h = (h << 4) + *name++;
342 g = h & 0xf0000000;
343 if (g)
344 h ^= g >> 24;
345 h &= ~g;
347 return h;
350 /* rebuild hash table of section s */
351 /* NOTE: we do factorize the hash table code to go faster */
352 static void rebuild_hash(Section *s, unsigned int nb_buckets)
354 ElfW(Sym) *sym;
355 int *ptr, *hash, nb_syms, sym_index, h;
356 unsigned char *strtab;
358 strtab = s->link->data;
359 nb_syms = s->data_offset / sizeof(ElfW(Sym));
361 if (!nb_buckets)
362 nb_buckets = ((int*)s->hash->data)[0];
364 s->hash->data_offset = 0;
365 ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
366 ptr[0] = nb_buckets;
367 ptr[1] = nb_syms;
368 ptr += 2;
369 hash = ptr;
370 memset(hash, 0, (nb_buckets + 1) * sizeof(int));
371 ptr += nb_buckets + 1;
373 sym = (ElfW(Sym) *)s->data + 1;
374 for(sym_index = 1; sym_index < nb_syms; sym_index++) {
375 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
376 h = elf_hash(strtab + sym->st_name) % nb_buckets;
377 *ptr = hash[h];
378 hash[h] = sym_index;
379 } else {
380 *ptr = 0;
382 ptr++;
383 sym++;
387 /* return the symbol number */
388 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
389 int info, int other, int shndx, const char *name)
391 int name_offset, sym_index;
392 int nbuckets, h;
393 ElfW(Sym) *sym;
394 Section *hs;
396 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
397 if (name && name[0])
398 name_offset = put_elf_str(s->link, name);
399 else
400 name_offset = 0;
401 /* XXX: endianness */
402 sym->st_name = name_offset;
403 sym->st_value = value;
404 sym->st_size = size;
405 sym->st_info = info;
406 sym->st_other = other;
407 sym->st_shndx = shndx;
408 sym_index = sym - (ElfW(Sym) *)s->data;
409 hs = s->hash;
410 if (hs) {
411 int *ptr, *base;
412 ptr = section_ptr_add(hs, sizeof(int));
413 base = (int *)hs->data;
414 /* only add global or weak symbols. */
415 if (ELFW(ST_BIND)(info) != STB_LOCAL) {
416 /* add another hashing entry */
417 nbuckets = base[0];
418 h = elf_hash((unsigned char *)s->link->data + name_offset) % nbuckets;
419 *ptr = base[2 + h];
420 base[2 + h] = sym_index;
421 base[1]++;
422 /* we resize the hash table */
423 hs->nb_hashed_syms++;
424 if (hs->nb_hashed_syms > 2 * nbuckets) {
425 rebuild_hash(s, 2 * nbuckets);
427 } else {
428 *ptr = 0;
429 base[1]++;
432 return sym_index;
435 ST_FUNC int find_elf_sym(Section *s, const char *name)
437 ElfW(Sym) *sym;
438 Section *hs;
439 int nbuckets, sym_index, h;
440 const char *name1;
442 hs = s->hash;
443 if (!hs)
444 return 0;
445 nbuckets = ((int *)hs->data)[0];
446 h = elf_hash((unsigned char *) name) % nbuckets;
447 sym_index = ((int *)hs->data)[2 + h];
448 while (sym_index != 0) {
449 sym = &((ElfW(Sym) *)s->data)[sym_index];
450 name1 = (char *) s->link->data + sym->st_name;
451 if (!strcmp(name, name1))
452 return sym_index;
453 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
455 return 0;
458 /* return elf symbol value, signal error if 'err' is nonzero */
459 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err)
461 int sym_index;
462 ElfW(Sym) *sym;
464 sym_index = find_elf_sym(s->symtab, name);
465 sym = &((ElfW(Sym) *)s->symtab->data)[sym_index];
466 if (!sym_index || sym->st_shndx == SHN_UNDEF) {
467 if (err)
468 tcc_error("%s not defined", name);
469 return 0;
471 return sym->st_value;
474 /* return elf symbol value */
475 LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
477 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0);
480 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
481 /* return elf symbol value or error */
482 ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name)
484 return (void*)(uintptr_t)get_elf_sym_addr(s, name, 1);
486 #endif
488 /* add an elf symbol : check if it is already defined and patch
489 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
490 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
491 int info, int other, int shndx, const char *name)
493 ElfW(Sym) *esym;
494 int sym_bind, sym_index, sym_type, esym_bind;
495 unsigned char sym_vis, esym_vis, new_vis;
497 sym_bind = ELFW(ST_BIND)(info);
498 sym_type = ELFW(ST_TYPE)(info);
499 sym_vis = ELFW(ST_VISIBILITY)(other);
501 if (sym_bind != STB_LOCAL) {
502 /* we search global or weak symbols */
503 sym_index = find_elf_sym(s, name);
504 if (!sym_index)
505 goto do_def;
506 esym = &((ElfW(Sym) *)s->data)[sym_index];
507 if (esym->st_value == value && esym->st_size == size && esym->st_info == info
508 && esym->st_other == other && esym->st_shndx == shndx)
509 return sym_index;
510 if (esym->st_shndx != SHN_UNDEF) {
511 esym_bind = ELFW(ST_BIND)(esym->st_info);
512 /* propagate the most constraining visibility */
513 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
514 esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
515 if (esym_vis == STV_DEFAULT) {
516 new_vis = sym_vis;
517 } else if (sym_vis == STV_DEFAULT) {
518 new_vis = esym_vis;
519 } else {
520 new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
522 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
523 | new_vis;
524 other = esym->st_other; /* in case we have to patch esym */
525 if (shndx == SHN_UNDEF) {
526 /* ignore adding of undefined symbol if the
527 corresponding symbol is already defined */
528 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
529 /* global overrides weak, so patch */
530 goto do_patch;
531 } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
532 /* weak is ignored if already global */
533 } else if (sym_bind == STB_WEAK && esym_bind == STB_WEAK) {
534 /* keep first-found weak definition, ignore subsequents */
535 } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
536 /* ignore hidden symbols after */
537 } else if ((esym->st_shndx == SHN_COMMON
538 || esym->st_shndx == bss_section->sh_num)
539 && (shndx < SHN_LORESERVE
540 && shndx != bss_section->sh_num)) {
541 /* data symbol gets precedence over common/bss */
542 goto do_patch;
543 } else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
544 /* data symbol keeps precedence over common/bss */
545 } else if (s->sh_flags & SHF_DYNSYM) {
546 /* we accept that two DLL define the same symbol */
547 } else if (esym->st_other & ST_ASM_SET) {
548 /* If the existing symbol came from an asm .set
549 we can override. */
550 goto do_patch;
551 } else {
552 #if 0
553 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
554 sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
555 #endif
556 tcc_error_noabort("'%s' defined twice", name);
558 } else {
559 do_patch:
560 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
561 esym->st_shndx = shndx;
562 new_undef_sym = 1;
563 esym->st_value = value;
564 esym->st_size = size;
565 esym->st_other = other;
567 } else {
568 do_def:
569 sym_index = put_elf_sym(s, value, size,
570 ELFW(ST_INFO)(sym_bind, sym_type), other,
571 shndx, name);
573 return sym_index;
576 /* put relocation */
577 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
578 int type, int symbol, addr_t addend)
580 char buf[256];
581 Section *sr;
582 ElfW_Rel *rel;
584 sr = s->reloc;
585 if (!sr) {
586 /* if no relocation section, create it */
587 snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
588 /* if the symtab is allocated, then we consider the relocation
589 are also */
590 sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
591 sr->sh_entsize = sizeof(ElfW_Rel);
592 sr->link = symtab;
593 sr->sh_info = s->sh_num;
594 s->reloc = sr;
596 rel = section_ptr_add(sr, sizeof(ElfW_Rel));
597 rel->r_offset = offset;
598 rel->r_info = ELFW(R_INFO)(symbol, type);
599 #if SHT_RELX == SHT_RELA
600 rel->r_addend = addend;
601 #else
602 if (addend)
603 tcc_error("non-zero addend on REL architecture");
604 #endif
607 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
608 int type, int symbol)
610 put_elf_reloca(symtab, s, offset, type, symbol, 0);
613 /* Remove relocations for section S->reloc starting at oldrelocoffset
614 that are to the same place, retaining the last of them. As side effect
615 the relocations are sorted. Possibly reduces the number of relocs. */
616 ST_FUNC void squeeze_multi_relocs(Section *s, size_t oldrelocoffset)
618 Section *sr = s->reloc;
619 ElfW_Rel *r, *dest;
620 ssize_t a;
621 ElfW(Addr) addr;
623 if (oldrelocoffset + sizeof(*r) >= sr->data_offset)
624 return;
625 /* The relocs we're dealing with are the result of initializer parsing.
626 So they will be mostly in order and there aren't many of them.
627 Secondly we need a stable sort (which qsort isn't). We use
628 a simple insertion sort. */
629 for (a = oldrelocoffset + sizeof(*r); a < sr->data_offset; a += sizeof(*r)) {
630 ssize_t i = a - sizeof(*r);
631 addr = ((ElfW_Rel*)(sr->data + a))->r_offset;
632 for (; i >= (ssize_t)oldrelocoffset &&
633 ((ElfW_Rel*)(sr->data + i))->r_offset > addr; i -= sizeof(*r)) {
634 ElfW_Rel tmp = *(ElfW_Rel*)(sr->data + a);
635 *(ElfW_Rel*)(sr->data + a) = *(ElfW_Rel*)(sr->data + i);
636 *(ElfW_Rel*)(sr->data + i) = tmp;
640 r = (ElfW_Rel*)(sr->data + oldrelocoffset);
641 dest = r;
642 for (; r < (ElfW_Rel*)(sr->data + sr->data_offset); r++) {
643 if (dest->r_offset != r->r_offset)
644 dest++;
645 *dest = *r;
647 sr->data_offset = (unsigned char*)dest - sr->data + sizeof(*r);
650 /* put stab debug information */
652 ST_FUNC void put_stabs(const char *str, int type, int other, int desc,
653 unsigned long value)
655 Stab_Sym *sym;
657 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
658 if (str) {
659 sym->n_strx = put_elf_str(stabstr_section, str);
660 } else {
661 sym->n_strx = 0;
663 sym->n_type = type;
664 sym->n_other = other;
665 sym->n_desc = desc;
666 sym->n_value = value;
669 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc,
670 unsigned long value, Section *sec, int sym_index)
672 put_stabs(str, type, other, desc, value);
673 put_elf_reloc(symtab_section, stab_section,
674 stab_section->data_offset - sizeof(unsigned int),
675 R_DATA_32, sym_index);
678 ST_FUNC void put_stabn(int type, int other, int desc, int value)
680 put_stabs(NULL, type, other, desc, value);
683 ST_FUNC void put_stabd(int type, int other, int desc)
685 put_stabs(NULL, type, other, desc, 0);
688 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
690 int n;
691 struct sym_attr *tab;
693 if (index >= s1->nb_sym_attrs) {
694 if (!alloc)
695 return s1->sym_attrs;
696 /* find immediately bigger power of 2 and reallocate array */
697 n = 1;
698 while (index >= n)
699 n *= 2;
700 tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
701 s1->sym_attrs = tab;
702 memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
703 (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
704 s1->nb_sym_attrs = n;
706 return &s1->sym_attrs[index];
709 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
710 using variable <elem> */
711 #define for_each_elem(sec, startoff, elem, type) \
712 for (elem = (type *) sec->data + startoff; \
713 elem < (type *) (sec->data + sec->data_offset); elem++)
715 /* In an ELF file symbol table, the local symbols must appear below
716 the global and weak ones. Since TCC cannot sort it while generating
717 the code, we must do it after. All the relocation tables are also
718 modified to take into account the symbol table sorting */
719 static void sort_syms(TCCState *s1, Section *s)
721 int *old_to_new_syms;
722 ElfW(Sym) *new_syms;
723 int nb_syms, i;
724 ElfW(Sym) *p, *q;
725 ElfW_Rel *rel;
726 Section *sr;
727 int type, sym_index;
729 nb_syms = s->data_offset / sizeof(ElfW(Sym));
730 new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
731 old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
733 /* first pass for local symbols */
734 p = (ElfW(Sym) *)s->data;
735 q = new_syms;
736 for(i = 0; i < nb_syms; i++) {
737 if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
738 old_to_new_syms[i] = q - new_syms;
739 *q++ = *p;
741 p++;
743 /* save the number of local symbols in section header */
744 if( s->sh_size ) /* this 'if' makes IDA happy */
745 s->sh_info = q - new_syms;
747 /* then second pass for non local symbols */
748 p = (ElfW(Sym) *)s->data;
749 for(i = 0; i < nb_syms; i++) {
750 if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
751 old_to_new_syms[i] = q - new_syms;
752 *q++ = *p;
754 p++;
757 /* we copy the new symbols to the old */
758 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
759 tcc_free(new_syms);
761 /* now we modify all the relocations */
762 for(i = 1; i < s1->nb_sections; i++) {
763 sr = s1->sections[i];
764 if (sr->sh_type == SHT_RELX && sr->link == s) {
765 for_each_elem(sr, 0, rel, ElfW_Rel) {
766 sym_index = ELFW(R_SYM)(rel->r_info);
767 type = ELFW(R_TYPE)(rel->r_info);
768 sym_index = old_to_new_syms[sym_index];
769 rel->r_info = ELFW(R_INFO)(sym_index, type);
774 tcc_free(old_to_new_syms);
777 /* relocate symbol table, resolve undefined symbols if do_resolve is
778 true and output error if undefined symbol. */
779 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
781 ElfW(Sym) *sym;
782 int sym_bind, sh_num;
783 const char *name;
785 for_each_elem(symtab, 1, sym, ElfW(Sym)) {
786 sh_num = sym->st_shndx;
787 if (sh_num == SHN_UNDEF) {
788 name = (char *) s1->symtab->link->data + sym->st_name;
789 /* Use ld.so to resolve symbol for us (for tcc -run) */
790 if (do_resolve) {
791 #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
792 void *addr = dlsym(RTLD_DEFAULT, name);
793 if (addr) {
794 sym->st_value = (addr_t) addr;
795 #ifdef DEBUG_RELOC
796 printf ("relocate_sym: %s -> 0x%lx\n", name, sym->st_value);
797 #endif
798 goto found;
800 #endif
801 /* if dynamic symbol exist, it will be used in relocate_section */
802 } else if (s1->dynsym && find_elf_sym(s1->dynsym, name))
803 goto found;
804 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
805 it */
806 if (!strcmp(name, "_fp_hw"))
807 goto found;
808 /* only weak symbols are accepted to be undefined. Their
809 value is zero */
810 sym_bind = ELFW(ST_BIND)(sym->st_info);
811 if (sym_bind == STB_WEAK)
812 sym->st_value = 0;
813 else
814 tcc_error_noabort("undefined symbol '%s'", name);
815 } else if (sh_num < SHN_LORESERVE) {
816 /* add section base */
817 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
819 found: ;
823 /* relocate a given section (CPU dependent) by applying the relocations
824 in the associated relocation section */
825 ST_FUNC void relocate_section(TCCState *s1, Section *s)
827 Section *sr = s->reloc;
828 ElfW_Rel *rel;
829 ElfW(Sym) *sym;
830 int type, sym_index;
831 unsigned char *ptr;
832 addr_t tgt, addr;
834 relocate_init(sr);
836 for_each_elem(sr, 0, rel, ElfW_Rel) {
837 ptr = s->data + rel->r_offset;
838 sym_index = ELFW(R_SYM)(rel->r_info);
839 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
840 type = ELFW(R_TYPE)(rel->r_info);
841 tgt = sym->st_value;
842 #if SHT_RELX == SHT_RELA
843 tgt += rel->r_addend;
844 #endif
845 addr = s->sh_addr + rel->r_offset;
846 relocate(s1, rel, type, ptr, addr, tgt);
848 /* if the relocation is allocated, we change its symbol table */
849 if (sr->sh_flags & SHF_ALLOC)
850 sr->link = s1->dynsym;
853 /* relocate relocation table in 'sr' */
854 static void relocate_rel(TCCState *s1, Section *sr)
856 Section *s;
857 ElfW_Rel *rel;
859 s = s1->sections[sr->sh_info];
860 for_each_elem(sr, 0, rel, ElfW_Rel)
861 rel->r_offset += s->sh_addr;
864 /* count the number of dynamic relocations so that we can reserve
865 their space */
866 static int prepare_dynamic_rel(TCCState *s1, Section *sr)
868 ElfW_Rel *rel;
869 int sym_index, type, count;
871 count = 0;
872 for_each_elem(sr, 0, rel, ElfW_Rel) {
873 sym_index = ELFW(R_SYM)(rel->r_info);
874 type = ELFW(R_TYPE)(rel->r_info);
875 switch(type) {
876 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
877 #if defined(TCC_TARGET_I386)
878 case R_386_32:
879 if (!get_sym_attr(s1, sym_index, 0)->dyn_index
880 && ((ElfW(Sym)*)symtab_section->data + sym_index)->st_shndx == SHN_UNDEF) {
881 /* don't fixup unresolved (weak) symbols */
882 rel->r_info = ELFW(R_INFO)(sym_index, R_386_RELATIVE);
883 break;
885 #elif defined(TCC_TARGET_X86_64)
886 case R_X86_64_32:
887 case R_X86_64_32S:
888 case R_X86_64_64:
889 #endif
890 count++;
891 break;
892 #if defined(TCC_TARGET_I386)
893 case R_386_PC32:
894 #elif defined(TCC_TARGET_X86_64)
895 case R_X86_64_PC32:
896 #endif
897 if (get_sym_attr(s1, sym_index, 0)->dyn_index)
898 count++;
899 break;
900 #endif
901 default:
902 break;
905 if (count) {
906 /* allocate the section */
907 sr->sh_flags |= SHF_ALLOC;
908 sr->sh_size = count * sizeof(ElfW_Rel);
910 return count;
913 static void build_got(TCCState *s1)
915 /* if no got, then create it */
916 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
917 s1->got->sh_entsize = 4;
918 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
919 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
920 /* keep space for _DYNAMIC pointer and two dummy got entries */
921 section_ptr_add(s1->got, 3 * PTR_SIZE);
924 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
925 in s1->symtab. When creating the dynamic symbol table entry for the GOT
926 relocation, use 'size' and 'info' for the corresponding symbol metadata.
927 Returns the offset of the GOT or (if any) PLT entry. */
928 static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
929 unsigned long size,
930 int info, int sym_index)
932 int need_plt_entry;
933 const char *name;
934 ElfW(Sym) *sym;
935 struct sym_attr *attr;
936 unsigned got_offset;
937 char plt_name[100];
938 int len;
940 need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
941 attr = get_sym_attr(s1, sym_index, 1);
943 /* In case a function is both called and its address taken 2 GOT entries
944 are created, one for taking the address (GOT) and the other for the PLT
945 entry (PLTGOT). */
946 if (need_plt_entry ? attr->plt_offset : attr->got_offset)
947 return attr;
949 /* create the GOT entry */
950 got_offset = s1->got->data_offset;
951 section_ptr_add(s1->got, PTR_SIZE);
953 /* Create the GOT relocation that will insert the address of the object or
954 function of interest in the GOT entry. This is a static relocation for
955 memory output (dlsym will give us the address of symbols) and dynamic
956 relocation otherwise (executable and DLLs). The relocation should be
957 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
958 associated to a PLT entry) but is currently done at load time for an
959 unknown reason. */
961 sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
962 name = (char *) symtab_section->link->data + sym->st_name;
964 if (s1->dynsym) {
965 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
966 /* Hack alarm. We don't want to emit dynamic symbols
967 and symbol based relocs for STB_LOCAL symbols, but rather
968 want to resolve them directly. At this point the symbol
969 values aren't final yet, so we must defer this. We will later
970 have to create a RELATIVE reloc anyway, so we misuse the
971 relocation slot to smuggle the symbol reference until
972 fill_local_got_entries. Not that the sym_index is
973 relative to symtab_section, not s1->dynsym! Nevertheless
974 we use s1->dyn_sym so that if this is the first call
975 that got->reloc is correctly created. Also note that
976 RELATIVE relocs are not normally created for the .got,
977 so the types serves as a marker for later (and is retained
978 also for the final output, which is okay because then the
979 got is just normal data). */
980 put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE,
981 sym_index);
982 } else {
983 if (0 == attr->dyn_index)
984 attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size,
985 info, 0, sym->st_shndx, name);
986 put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type,
987 attr->dyn_index);
989 } else {
990 put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
991 sym_index);
994 if (need_plt_entry) {
995 if (!s1->plt) {
996 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
997 SHF_ALLOC | SHF_EXECINSTR);
998 s1->plt->sh_entsize = 4;
1001 attr->plt_offset = create_plt_entry(s1, got_offset, attr);
1003 /* create a symbol 'sym@plt' for the PLT jump vector */
1004 len = strlen(name);
1005 if (len > sizeof plt_name - 5)
1006 len = sizeof plt_name - 5;
1007 memcpy(plt_name, name, len);
1008 strcpy(plt_name + len, "@plt");
1009 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
1010 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
1012 } else {
1013 attr->got_offset = got_offset;
1016 return attr;
1019 /* build GOT and PLT entries */
1020 ST_FUNC void build_got_entries(TCCState *s1)
1022 Section *s;
1023 ElfW_Rel *rel;
1024 ElfW(Sym) *sym;
1025 int i, type, gotplt_entry, reloc_type, sym_index;
1026 struct sym_attr *attr;
1028 for(i = 1; i < s1->nb_sections; i++) {
1029 s = s1->sections[i];
1030 if (s->sh_type != SHT_RELX)
1031 continue;
1032 /* no need to handle got relocations */
1033 if (s->link != symtab_section)
1034 continue;
1035 for_each_elem(s, 0, rel, ElfW_Rel) {
1036 type = ELFW(R_TYPE)(rel->r_info);
1037 gotplt_entry = gotplt_entry_type(type);
1038 sym_index = ELFW(R_SYM)(rel->r_info);
1039 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1041 if (gotplt_entry == NO_GOTPLT_ENTRY) {
1042 continue;
1045 /* Automatically create PLT/GOT [entry] if it is an undefined
1046 reference (resolved at runtime), or the symbol is absolute,
1047 probably created by tcc_add_symbol, and thus on 64-bit
1048 targets might be too far from application code. */
1049 if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
1050 if (sym->st_shndx == SHN_UNDEF) {
1051 ElfW(Sym) *esym;
1052 int dynindex;
1053 if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
1054 continue;
1055 /* Relocations for UNDEF symbols would normally need
1056 to be transferred into the executable or shared object.
1057 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1058 But TCC doesn't do that (at least for exes), so we
1059 need to resolve all such relocs locally. And that
1060 means PLT slots for functions in DLLs and COPY relocs for
1061 data symbols. COPY relocs were generated in
1062 bind_exe_dynsyms (and the symbol adjusted to be defined),
1063 and for functions we were generated a dynamic symbol
1064 of function type. */
1065 if (s1->dynsym) {
1066 /* dynsym isn't set for -run :-/ */
1067 dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index;
1068 esym = (ElfW(Sym) *)s1->dynsym->data + dynindex;
1069 if (dynindex
1070 && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
1071 || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
1072 && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC)))
1073 goto jmp_slot;
1075 } else if (!(sym->st_shndx == SHN_ABS
1076 #ifndef TCC_TARGET_ARM
1077 && PTR_SIZE == 8
1078 #endif
1080 continue;
1083 #ifdef TCC_TARGET_X86_64
1084 if ((type == R_X86_64_PLT32 || type == R_X86_64_PC32) &&
1085 (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT ||
1086 ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)) {
1087 rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
1088 continue;
1090 #endif
1091 if (code_reloc(type)) {
1092 jmp_slot:
1093 reloc_type = R_JMP_SLOT;
1094 } else
1095 reloc_type = R_GLOB_DAT;
1097 if (!s1->got)
1098 build_got(s1);
1100 if (gotplt_entry == BUILD_GOT_ONLY)
1101 continue;
1103 attr = put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1104 sym_index);
1106 if (reloc_type == R_JMP_SLOT)
1107 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
1112 /* put dynamic tag */
1113 static void put_dt(Section *dynamic, int dt, addr_t val)
1115 ElfW(Dyn) *dyn;
1116 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1117 dyn->d_tag = dt;
1118 dyn->d_un.d_val = val;
1121 #ifndef TCC_TARGET_PE
1122 static void add_init_array_defines(TCCState *s1, const char *section_name)
1124 Section *s;
1125 long end_offset;
1126 char sym_start[1024];
1127 char sym_end[1024];
1129 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1130 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1132 s = find_section(s1, section_name);
1133 if (!s) {
1134 end_offset = 0;
1135 s = data_section;
1136 } else {
1137 end_offset = s->data_offset;
1140 set_elf_sym(symtab_section,
1141 0, 0,
1142 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1143 s->sh_num, sym_start);
1144 set_elf_sym(symtab_section,
1145 end_offset, 0,
1146 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1147 s->sh_num, sym_end);
1150 static int tcc_add_support(TCCState *s1, const char *filename)
1152 char buf[1024];
1153 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
1154 return tcc_add_file(s1, buf);
1156 #endif
1158 ST_FUNC void tcc_add_bcheck(TCCState *s1)
1160 #ifdef CONFIG_TCC_BCHECK
1161 addr_t *ptr;
1162 int sym_index;
1164 if (0 == s1->do_bounds_check)
1165 return;
1166 /* XXX: add an object file to do that */
1167 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1168 *ptr = 0;
1169 set_elf_sym(symtab_section, 0, 0,
1170 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1171 bounds_section->sh_num, "__bounds_start");
1172 /* pull bcheck.o from libtcc1.a */
1173 sym_index = set_elf_sym(symtab_section, 0, 0,
1174 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1175 SHN_UNDEF, "__bound_init");
1176 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1177 /* add 'call __bound_init()' in .init section */
1178 Section *init_section = find_section(s1, ".init");
1179 unsigned char *pinit = section_ptr_add(init_section, 5);
1180 pinit[0] = 0xe8;
1181 write32le(pinit + 1, -4);
1182 put_elf_reloc(symtab_section, init_section,
1183 init_section->data_offset - 4, R_386_PC32, sym_index);
1184 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1186 #endif
1189 /* add tcc runtime libraries */
1190 ST_FUNC void tcc_add_runtime(TCCState *s1)
1192 s1->filetype = 0;
1193 tcc_add_bcheck(s1);
1194 tcc_add_pragma_libs(s1);
1195 #ifndef TCC_TARGET_PE
1196 /* add libc */
1197 if (!s1->nostdlib) {
1198 tcc_add_library_err(s1, "c");
1199 #ifdef TCC_LIBGCC
1200 if (!s1->static_link) {
1201 if (TCC_LIBGCC[0] == '/')
1202 tcc_add_file(s1, TCC_LIBGCC);
1203 else
1204 tcc_add_dll(s1, TCC_LIBGCC, 0);
1206 #endif
1207 tcc_add_support(s1, TCC_LIBTCC1);
1208 /* add crt end if not memory output */
1209 if (s1->output_type != TCC_OUTPUT_MEMORY)
1210 tcc_add_crt(s1, "crtn.o");
1212 #endif
1215 /* add various standard linker symbols (must be done after the
1216 sections are filled (for example after allocating common
1217 symbols)) */
1218 static void tcc_add_linker_symbols(TCCState *s1)
1220 char buf[1024];
1221 int i;
1222 Section *s;
1224 set_elf_sym(symtab_section,
1225 text_section->data_offset, 0,
1226 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1227 text_section->sh_num, "_etext");
1228 set_elf_sym(symtab_section,
1229 data_section->data_offset, 0,
1230 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1231 data_section->sh_num, "_edata");
1232 set_elf_sym(symtab_section,
1233 bss_section->data_offset, 0,
1234 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1235 bss_section->sh_num, "_end");
1236 #ifndef TCC_TARGET_PE
1237 /* horrible new standard ldscript defines */
1238 add_init_array_defines(s1, ".preinit_array");
1239 add_init_array_defines(s1, ".init_array");
1240 add_init_array_defines(s1, ".fini_array");
1241 #endif
1243 /* add start and stop symbols for sections whose name can be
1244 expressed in C */
1245 for(i = 1; i < s1->nb_sections; i++) {
1246 s = s1->sections[i];
1247 if (s->sh_type == SHT_PROGBITS &&
1248 (s->sh_flags & SHF_ALLOC)) {
1249 const char *p;
1250 int ch;
1252 /* check if section name can be expressed in C */
1253 p = s->name;
1254 for(;;) {
1255 ch = *p;
1256 if (!ch)
1257 break;
1258 if (!isid(ch) && !isnum(ch))
1259 goto next_sec;
1260 p++;
1262 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1263 set_elf_sym(symtab_section,
1264 0, 0,
1265 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1266 s->sh_num, buf);
1267 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1268 set_elf_sym(symtab_section,
1269 s->data_offset, 0,
1270 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1271 s->sh_num, buf);
1273 next_sec: ;
1277 ST_FUNC void resolve_common_syms(TCCState *s1)
1279 ElfW(Sym) *sym;
1281 /* Allocate common symbols in BSS. */
1282 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1283 if (sym->st_shndx == SHN_COMMON) {
1284 /* symbol alignment is in st_value for SHN_COMMONs */
1285 sym->st_value = section_add(bss_section, sym->st_size,
1286 sym->st_value);
1287 sym->st_shndx = bss_section->sh_num;
1291 /* Now assign linker provided symbols their value. */
1292 tcc_add_linker_symbols(s1);
1295 static void tcc_output_binary(TCCState *s1, FILE *f,
1296 const int *sec_order)
1298 Section *s;
1299 int i, offset, size;
1301 offset = 0;
1302 for(i=1;i<s1->nb_sections;i++) {
1303 s = s1->sections[sec_order[i]];
1304 if (s->sh_type != SHT_NOBITS &&
1305 (s->sh_flags & SHF_ALLOC)) {
1306 while (offset < s->sh_offset) {
1307 fputc(0, f);
1308 offset++;
1310 size = s->sh_size;
1311 fwrite(s->data, 1, size, f);
1312 offset += size;
1317 ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1319 int sym_index = ELFW(R_SYM) (rel->r_info);
1320 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1321 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1322 unsigned offset = attr->got_offset;
1324 if (0 == offset)
1325 return;
1326 section_reserve(s1->got, offset + PTR_SIZE);
1327 #ifdef TCC_TARGET_X86_64
1328 write64le(s1->got->data + offset, sym->st_value);
1329 #else
1330 write32le(s1->got->data + offset, sym->st_value);
1331 #endif
1334 /* Perform relocation to GOT or PLT entries */
1335 ST_FUNC void fill_got(TCCState *s1)
1337 Section *s;
1338 ElfW_Rel *rel;
1339 int i;
1341 for(i = 1; i < s1->nb_sections; i++) {
1342 s = s1->sections[i];
1343 if (s->sh_type != SHT_RELX)
1344 continue;
1345 /* no need to handle got relocations */
1346 if (s->link != symtab_section)
1347 continue;
1348 for_each_elem(s, 0, rel, ElfW_Rel) {
1349 switch (ELFW(R_TYPE) (rel->r_info)) {
1350 case R_X86_64_GOT32:
1351 case R_X86_64_GOTPCREL:
1352 case R_X86_64_GOTPCRELX:
1353 case R_X86_64_REX_GOTPCRELX:
1354 case R_X86_64_PLT32:
1355 fill_got_entry(s1, rel);
1356 break;
1362 /* See put_got_entry for a description. This is the second stage
1363 where GOT references to local defined symbols are rewritten. */
1364 static void fill_local_got_entries(TCCState *s1)
1366 ElfW_Rel *rel;
1367 if (!s1->got->reloc)
1368 return;
1369 for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
1370 if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
1371 int sym_index = ELFW(R_SYM) (rel->r_info);
1372 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1373 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1374 unsigned offset = attr->got_offset;
1375 if (offset != rel->r_offset - s1->got->sh_addr)
1376 tcc_error_noabort("huh");
1377 rel->r_info = ELFW(R_INFO)(0, R_RELATIVE);
1378 #if SHT_RELX == SHT_RELA
1379 rel->r_addend = sym->st_value;
1380 #else
1381 /* All our REL architectures also happen to be 32bit LE. */
1382 write32le(s1->got->data + offset, sym->st_value);
1383 #endif
1388 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1389 in shared libraries and export non local defined symbols to shared libraries
1390 if -rdynamic switch was given on command line */
1391 static void bind_exe_dynsyms(TCCState *s1)
1393 const char *name;
1394 int sym_index, index;
1395 ElfW(Sym) *sym, *esym;
1396 int type;
1398 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1399 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1400 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1401 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1402 if (sym->st_shndx == SHN_UNDEF) {
1403 name = (char *) symtab_section->link->data + sym->st_name;
1404 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1405 if (sym_index) {
1406 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1407 type = ELFW(ST_TYPE)(esym->st_info);
1408 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
1409 /* Indirect functions shall have STT_FUNC type in executable
1410 * dynsym section. Indeed, a dlsym call following a lazy
1411 * resolution would pick the symbol value from the
1412 * executable dynsym entry which would contain the address
1413 * of the function wanted by the caller of dlsym instead of
1414 * the address of the function that would return that
1415 * address */
1416 int dynindex
1417 = put_elf_sym(s1->dynsym, 0, esym->st_size,
1418 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
1419 name);
1420 int index = sym - (ElfW(Sym) *) symtab_section->data;
1421 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1422 } else if (type == STT_OBJECT) {
1423 unsigned long offset;
1424 ElfW(Sym) *dynsym;
1425 offset = bss_section->data_offset;
1426 /* XXX: which alignment ? */
1427 offset = (offset + 16 - 1) & -16;
1428 set_elf_sym (s1->symtab, offset, esym->st_size,
1429 esym->st_info, 0, bss_section->sh_num, name);
1430 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1431 esym->st_info, 0, bss_section->sh_num,
1432 name);
1434 /* Ensure R_COPY works for weak symbol aliases */
1435 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1436 for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
1437 if ((dynsym->st_value == esym->st_value)
1438 && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
1439 char *dynname = (char *) s1->dynsymtab_section->link->data
1440 + dynsym->st_name;
1441 put_elf_sym(s1->dynsym, offset, dynsym->st_size,
1442 dynsym->st_info, 0,
1443 bss_section->sh_num, dynname);
1444 break;
1449 put_elf_reloc(s1->dynsym, bss_section,
1450 offset, R_COPY, index);
1451 offset += esym->st_size;
1452 bss_section->data_offset = offset;
1454 } else {
1455 /* STB_WEAK undefined symbols are accepted */
1456 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1457 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1458 !strcmp(name, "_fp_hw")) {
1459 } else {
1460 tcc_error_noabort("undefined symbol '%s'", name);
1463 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1464 /* if -rdynamic option, then export all non local symbols */
1465 name = (char *) symtab_section->link->data + sym->st_name;
1466 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1467 0, sym->st_shndx, name);
1472 /* Bind symbols of libraries: export all non local symbols of executable that
1473 are referenced by shared libraries. The reason is that the dynamic loader
1474 search symbol first in executable and then in libraries. Therefore a
1475 reference to a symbol already defined by a library can still be resolved by
1476 a symbol in the executable. */
1477 static void bind_libs_dynsyms(TCCState *s1)
1479 const char *name;
1480 int sym_index;
1481 ElfW(Sym) *sym, *esym;
1483 for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
1484 name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
1485 sym_index = find_elf_sym(symtab_section, name);
1486 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1487 if (sym_index && sym->st_shndx != SHN_UNDEF
1488 && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1489 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1490 sym->st_info, 0, sym->st_shndx, name);
1491 } else if (esym->st_shndx == SHN_UNDEF) {
1492 /* weak symbols can stay undefined */
1493 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
1494 tcc_warning("undefined dynamic symbol '%s'", name);
1499 /* Export all non local symbols. This is used by shared libraries so that the
1500 non local symbols they define can resolve a reference in another shared
1501 library or in the executable. Correspondingly, it allows undefined local
1502 symbols to be resolved by other shared libraries or by the executable. */
1503 static void export_global_syms(TCCState *s1)
1505 int dynindex, index;
1506 const char *name;
1507 ElfW(Sym) *sym;
1509 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1510 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1511 name = (char *) symtab_section->link->data + sym->st_name;
1512 dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1513 sym->st_info, 0, sym->st_shndx, name);
1514 index = sym - (ElfW(Sym) *) symtab_section->data;
1515 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1520 /* Allocate strings for section names and decide if an unallocated section
1521 should be output.
1522 NOTE: the strsec section comes last, so its size is also correct ! */
1523 static int alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
1525 int i;
1526 Section *s;
1527 int textrel = 0;
1529 /* Allocate strings for section names */
1530 for(i = 1; i < s1->nb_sections; i++) {
1531 s = s1->sections[i];
1532 /* when generating a DLL, we include relocations but we may
1533 patch them */
1534 if (file_type == TCC_OUTPUT_DLL &&
1535 s->sh_type == SHT_RELX &&
1536 !(s->sh_flags & SHF_ALLOC) &&
1537 (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) &&
1538 prepare_dynamic_rel(s1, s)) {
1539 if (s1->sections[s->sh_info]->sh_flags & SHF_EXECINSTR)
1540 textrel = 1;
1541 } else if ((s1->do_debug && s->sh_type != SHT_RELX) ||
1542 file_type == TCC_OUTPUT_OBJ ||
1543 (s->sh_flags & SHF_ALLOC) ||
1544 i == (s1->nb_sections - 1)) {
1545 /* we output all sections if debug or object file */
1546 s->sh_size = s->data_offset;
1548 if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1549 s->sh_name = put_elf_str(strsec, s->name);
1551 strsec->sh_size = strsec->data_offset;
1552 return textrel;
1555 /* Info to be copied in dynamic section */
1556 struct dyn_inf {
1557 Section *dynamic;
1558 Section *dynstr;
1559 unsigned long data_offset;
1560 addr_t rel_addr;
1561 addr_t rel_size;
1562 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1563 addr_t bss_addr;
1564 addr_t bss_size;
1565 #endif
1568 /* Assign sections to segments and decide how are sections laid out when loaded
1569 in memory. This function also fills corresponding program headers. */
1570 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1571 Section *interp, Section* strsec,
1572 struct dyn_inf *dyninf, int *sec_order)
1574 int i, j, k, file_type, sh_order_index, file_offset;
1575 unsigned long s_align;
1576 long long tmp;
1577 addr_t addr;
1578 ElfW(Phdr) *ph;
1579 Section *s;
1581 file_type = s1->output_type;
1582 sh_order_index = 1;
1583 file_offset = 0;
1584 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1585 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1586 s_align = ELF_PAGE_SIZE;
1587 if (s1->section_align)
1588 s_align = s1->section_align;
1590 if (phnum > 0) {
1591 if (s1->has_text_addr) {
1592 int a_offset, p_offset;
1593 addr = s1->text_addr;
1594 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1595 ELF_PAGE_SIZE */
1596 a_offset = (int) (addr & (s_align - 1));
1597 p_offset = file_offset & (s_align - 1);
1598 if (a_offset < p_offset)
1599 a_offset += s_align;
1600 file_offset += (a_offset - p_offset);
1601 } else {
1602 if (file_type == TCC_OUTPUT_DLL)
1603 addr = 0;
1604 else
1605 addr = ELF_START_ADDR;
1606 /* compute address after headers */
1607 addr += (file_offset & (s_align - 1));
1610 ph = &phdr[0];
1611 /* Leave one program headers for the program interpreter and one for
1612 the program header table itself if needed. These are done later as
1613 they require section layout to be done first. */
1614 if (interp)
1615 ph += 2;
1617 /* dynamic relocation table information, for .dynamic section */
1618 dyninf->rel_addr = dyninf->rel_size = 0;
1619 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1620 dyninf->bss_addr = dyninf->bss_size = 0;
1621 #endif
1623 for(j = 0; j < 2; j++) {
1624 ph->p_type = PT_LOAD;
1625 if (j == 0)
1626 ph->p_flags = PF_R | PF_X;
1627 else
1628 ph->p_flags = PF_R | PF_W;
1629 ph->p_align = s_align;
1631 /* Decide the layout of sections loaded in memory. This must
1632 be done before program headers are filled since they contain
1633 info about the layout. We do the following ordering: interp,
1634 symbol tables, relocations, progbits, nobits */
1635 /* XXX: do faster and simpler sorting */
1636 for(k = 0; k < 5; k++) {
1637 for(i = 1; i < s1->nb_sections; i++) {
1638 s = s1->sections[i];
1639 /* compute if section should be included */
1640 if (j == 0) {
1641 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1642 SHF_ALLOC)
1643 continue;
1644 } else {
1645 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1646 (SHF_ALLOC | SHF_WRITE))
1647 continue;
1649 if (s == interp) {
1650 if (k != 0)
1651 continue;
1652 } else if (s->sh_type == SHT_DYNSYM ||
1653 s->sh_type == SHT_STRTAB ||
1654 s->sh_type == SHT_HASH) {
1655 if (k != 1)
1656 continue;
1657 } else if (s->sh_type == SHT_RELX) {
1658 if (k != 2)
1659 continue;
1660 } else if (s->sh_type == SHT_NOBITS) {
1661 if (k != 4)
1662 continue;
1663 } else {
1664 if (k != 3)
1665 continue;
1667 sec_order[sh_order_index++] = i;
1669 /* section matches: we align it and add its size */
1670 tmp = addr;
1671 addr = (addr + s->sh_addralign - 1) &
1672 ~(s->sh_addralign - 1);
1673 file_offset += (int) ( addr - tmp );
1674 s->sh_offset = file_offset;
1675 s->sh_addr = addr;
1677 /* update program header infos */
1678 if (ph->p_offset == 0) {
1679 ph->p_offset = file_offset;
1680 ph->p_vaddr = addr;
1681 ph->p_paddr = ph->p_vaddr;
1683 /* update dynamic relocation infos */
1684 if (s->sh_type == SHT_RELX) {
1685 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1686 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1687 dyninf->rel_addr = addr;
1688 dyninf->rel_size += s->sh_size; /* XXX only first rel. */
1690 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1691 dyninf->bss_addr = addr;
1692 dyninf->bss_size = s->sh_size; /* XXX only first rel. */
1694 #else
1695 if (dyninf->rel_size == 0)
1696 dyninf->rel_addr = addr;
1697 dyninf->rel_size += s->sh_size;
1698 #endif
1700 addr += s->sh_size;
1701 if (s->sh_type != SHT_NOBITS)
1702 file_offset += s->sh_size;
1705 if (j == 0) {
1706 /* Make the first PT_LOAD segment include the program
1707 headers itself (and the ELF header as well), it'll
1708 come out with same memory use but will make various
1709 tools like binutils strip work better. */
1710 ph->p_offset &= ~(ph->p_align - 1);
1711 ph->p_vaddr &= ~(ph->p_align - 1);
1712 ph->p_paddr &= ~(ph->p_align - 1);
1714 ph->p_filesz = file_offset - ph->p_offset;
1715 ph->p_memsz = addr - ph->p_vaddr;
1716 ph++;
1717 if (j == 0) {
1718 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1719 /* if in the middle of a page, we duplicate the page in
1720 memory so that one copy is RX and the other is RW */
1721 if ((addr & (s_align - 1)) != 0)
1722 addr += s_align;
1723 } else {
1724 addr = (addr + s_align - 1) & ~(s_align - 1);
1725 file_offset = (file_offset + s_align - 1) & ~(s_align - 1);
1731 /* all other sections come after */
1732 for(i = 1; i < s1->nb_sections; i++) {
1733 s = s1->sections[i];
1734 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1735 continue;
1736 sec_order[sh_order_index++] = i;
1738 file_offset = (file_offset + s->sh_addralign - 1) &
1739 ~(s->sh_addralign - 1);
1740 s->sh_offset = file_offset;
1741 if (s->sh_type != SHT_NOBITS)
1742 file_offset += s->sh_size;
1745 return file_offset;
1748 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
1749 Section *dynamic)
1751 ElfW(Phdr) *ph;
1753 /* if interpreter, then add corresponding program header */
1754 if (interp) {
1755 ph = &phdr[0];
1757 ph->p_type = PT_PHDR;
1758 ph->p_offset = sizeof(ElfW(Ehdr));
1759 ph->p_filesz = ph->p_memsz = phnum * sizeof(ElfW(Phdr));
1760 ph->p_vaddr = interp->sh_addr - ph->p_filesz;
1761 ph->p_paddr = ph->p_vaddr;
1762 ph->p_flags = PF_R | PF_X;
1763 ph->p_align = 4; /* interp->sh_addralign; */
1764 ph++;
1766 ph->p_type = PT_INTERP;
1767 ph->p_offset = interp->sh_offset;
1768 ph->p_vaddr = interp->sh_addr;
1769 ph->p_paddr = ph->p_vaddr;
1770 ph->p_filesz = interp->sh_size;
1771 ph->p_memsz = interp->sh_size;
1772 ph->p_flags = PF_R;
1773 ph->p_align = interp->sh_addralign;
1776 /* if dynamic section, then add corresponding program header */
1777 if (dynamic) {
1778 ph = &phdr[phnum - 1];
1780 ph->p_type = PT_DYNAMIC;
1781 ph->p_offset = dynamic->sh_offset;
1782 ph->p_vaddr = dynamic->sh_addr;
1783 ph->p_paddr = ph->p_vaddr;
1784 ph->p_filesz = dynamic->sh_size;
1785 ph->p_memsz = dynamic->sh_size;
1786 ph->p_flags = PF_R | PF_W;
1787 ph->p_align = dynamic->sh_addralign;
1791 /* Fill the dynamic section with tags describing the address and size of
1792 sections */
1793 static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf)
1795 Section *dynamic = dyninf->dynamic;
1797 /* put dynamic section entries */
1798 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1799 put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
1800 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1801 put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
1802 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1803 #if PTR_SIZE == 8
1804 put_dt(dynamic, DT_RELA, dyninf->rel_addr);
1805 put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
1806 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1807 #else
1808 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1809 put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
1810 put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size);
1811 put_dt(dynamic, DT_JMPREL, dyninf->rel_addr);
1812 put_dt(dynamic, DT_PLTREL, DT_REL);
1813 put_dt(dynamic, DT_REL, dyninf->bss_addr);
1814 put_dt(dynamic, DT_RELSZ, dyninf->bss_size);
1815 #else
1816 put_dt(dynamic, DT_REL, dyninf->rel_addr);
1817 put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
1818 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1819 #endif
1820 #endif
1821 if (s1->do_debug)
1822 put_dt(dynamic, DT_DEBUG, 0);
1823 put_dt(dynamic, DT_NULL, 0);
1826 /* Relocate remaining sections and symbols (that is those not related to
1827 dynamic linking) */
1828 static int final_sections_reloc(TCCState *s1)
1830 int i;
1831 Section *s;
1833 relocate_syms(s1, s1->symtab, 0);
1835 if (s1->nb_errors != 0)
1836 return -1;
1838 /* relocate sections */
1839 /* XXX: ignore sections with allocated relocations ? */
1840 for(i = 1; i < s1->nb_sections; i++) {
1841 s = s1->sections[i];
1842 if (s->reloc && s != s1->got)
1843 relocate_section(s1, s);
1846 /* relocate relocation entries if the relocation tables are
1847 allocated in the executable */
1848 for(i = 1; i < s1->nb_sections; i++) {
1849 s = s1->sections[i];
1850 if ((s->sh_flags & SHF_ALLOC) &&
1851 s->sh_type == SHT_RELX) {
1852 relocate_rel(s1, s);
1855 return 0;
1858 /* Create an ELF file on disk.
1859 This function handle ELF specific layout requirements */
1860 static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
1861 int file_offset, int *sec_order)
1863 int i, shnum, offset, size, file_type;
1864 Section *s;
1865 ElfW(Ehdr) ehdr;
1866 ElfW(Shdr) shdr, *sh;
1868 file_type = s1->output_type;
1869 shnum = s1->nb_sections;
1871 memset(&ehdr, 0, sizeof(ehdr));
1873 if (phnum > 0) {
1874 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1875 ehdr.e_phnum = phnum;
1876 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1879 /* align to 4 */
1880 file_offset = (file_offset + 3) & -4;
1882 /* fill header */
1883 ehdr.e_ident[0] = ELFMAG0;
1884 ehdr.e_ident[1] = ELFMAG1;
1885 ehdr.e_ident[2] = ELFMAG2;
1886 ehdr.e_ident[3] = ELFMAG3;
1887 ehdr.e_ident[4] = ELFCLASSW;
1888 ehdr.e_ident[5] = ELFDATA2LSB;
1889 ehdr.e_ident[6] = EV_CURRENT;
1890 #if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
1891 /* FIXME: should set only for freebsd _target_, but we exclude only PE target */
1892 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1893 #endif
1894 #ifdef TCC_TARGET_ARM
1895 #ifdef TCC_ARM_EABI
1896 ehdr.e_ident[EI_OSABI] = 0;
1897 ehdr.e_flags = EF_ARM_EABI_VER4;
1898 if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
1899 ehdr.e_flags |= EF_ARM_HASENTRY;
1900 if (s1->float_abi == ARM_HARD_FLOAT)
1901 ehdr.e_flags |= EF_ARM_VFP_FLOAT;
1902 else
1903 ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
1904 #else
1905 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1906 #endif
1907 #endif
1908 switch(file_type) {
1909 default:
1910 case TCC_OUTPUT_EXE:
1911 ehdr.e_type = ET_EXEC;
1912 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
1913 break;
1914 case TCC_OUTPUT_DLL:
1915 ehdr.e_type = ET_DYN;
1916 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1917 break;
1918 case TCC_OUTPUT_OBJ:
1919 ehdr.e_type = ET_REL;
1920 break;
1922 ehdr.e_machine = EM_TCC_TARGET;
1923 ehdr.e_version = EV_CURRENT;
1924 ehdr.e_shoff = file_offset;
1925 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1926 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1927 ehdr.e_shnum = shnum;
1928 ehdr.e_shstrndx = shnum - 1;
1930 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1931 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1932 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1934 sort_syms(s1, symtab_section);
1935 for(i = 1; i < s1->nb_sections; i++) {
1936 s = s1->sections[sec_order[i]];
1937 if (s->sh_type != SHT_NOBITS) {
1938 while (offset < s->sh_offset) {
1939 fputc(0, f);
1940 offset++;
1942 size = s->sh_size;
1943 if (size)
1944 fwrite(s->data, 1, size, f);
1945 offset += size;
1949 /* output section headers */
1950 while (offset < ehdr.e_shoff) {
1951 fputc(0, f);
1952 offset++;
1955 for(i = 0; i < s1->nb_sections; i++) {
1956 sh = &shdr;
1957 memset(sh, 0, sizeof(ElfW(Shdr)));
1958 s = s1->sections[i];
1959 if (s) {
1960 sh->sh_name = s->sh_name;
1961 sh->sh_type = s->sh_type;
1962 sh->sh_flags = s->sh_flags;
1963 sh->sh_entsize = s->sh_entsize;
1964 sh->sh_info = s->sh_info;
1965 if (s->link)
1966 sh->sh_link = s->link->sh_num;
1967 sh->sh_addralign = s->sh_addralign;
1968 sh->sh_addr = s->sh_addr;
1969 sh->sh_offset = s->sh_offset;
1970 sh->sh_size = s->sh_size;
1972 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1976 /* Write an elf, coff or "binary" file */
1977 static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
1978 ElfW(Phdr) *phdr, int file_offset, int *sec_order)
1980 int fd, mode, file_type;
1981 FILE *f;
1983 file_type = s1->output_type;
1984 if (file_type == TCC_OUTPUT_OBJ)
1985 mode = 0666;
1986 else
1987 mode = 0777;
1988 unlink(filename);
1989 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1990 if (fd < 0) {
1991 tcc_error_noabort("could not write '%s'", filename);
1992 return -1;
1994 f = fdopen(fd, "wb");
1995 if (s1->verbose)
1996 printf("<- %s\n", filename);
1998 #ifdef TCC_TARGET_COFF
1999 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
2000 tcc_output_coff(s1, f);
2001 else
2002 #endif
2003 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
2004 tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
2005 else
2006 tcc_output_binary(s1, f, sec_order);
2007 fclose(f);
2009 return 0;
2012 /* Sort section headers by assigned sh_addr, remove sections
2013 that we aren't going to output. */
2014 static void tidy_section_headers(TCCState *s1, int *sec_order)
2016 int i, nnew, l, *backmap;
2017 Section **snew, *s;
2018 ElfW(Sym) *sym;
2020 snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
2021 backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
2022 for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
2023 s = s1->sections[sec_order[i]];
2024 if (!i || s->sh_name) {
2025 backmap[sec_order[i]] = nnew;
2026 snew[nnew] = s;
2027 ++nnew;
2028 } else {
2029 backmap[sec_order[i]] = 0;
2030 snew[--l] = s;
2033 for (i = 0; i < nnew; i++) {
2034 s = snew[i];
2035 if (s) {
2036 s->sh_num = i;
2037 if (s->sh_type == SHT_RELX)
2038 s->sh_info = backmap[s->sh_info];
2042 for_each_elem(symtab_section, 1, sym, ElfW(Sym))
2043 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
2044 sym->st_shndx = backmap[sym->st_shndx];
2045 if( !s1->static_link ) {
2046 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
2047 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
2048 sym->st_shndx = backmap[sym->st_shndx];
2050 for (i = 0; i < s1->nb_sections; i++)
2051 sec_order[i] = i;
2052 tcc_free(s1->sections);
2053 s1->sections = snew;
2054 s1->nb_sections = nnew;
2055 tcc_free(backmap);
2058 /* Output an elf, coff or binary file */
2059 /* XXX: suppress unneeded sections */
2060 static int elf_output_file(TCCState *s1, const char *filename)
2062 int i, ret, phnum, shnum, file_type, file_offset, *sec_order;
2063 struct dyn_inf dyninf = {0};
2064 ElfW(Phdr) *phdr;
2065 ElfW(Sym) *sym;
2066 Section *strsec, *interp, *dynamic, *dynstr;
2067 int textrel;
2069 file_type = s1->output_type;
2070 s1->nb_errors = 0;
2071 ret = -1;
2072 phdr = NULL;
2073 sec_order = NULL;
2074 interp = dynamic = dynstr = NULL; /* avoid warning */
2075 textrel = 0;
2077 if (file_type != TCC_OUTPUT_OBJ) {
2078 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2079 tcc_add_runtime(s1);
2080 resolve_common_syms(s1);
2082 if (!s1->static_link) {
2083 if (file_type == TCC_OUTPUT_EXE) {
2084 char *ptr;
2085 /* allow override the dynamic loader */
2086 const char *elfint = getenv("LD_SO");
2087 if (elfint == NULL)
2088 elfint = DEFAULT_ELFINTERP(s1);
2089 /* add interpreter section only if executable */
2090 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
2091 interp->sh_addralign = 1;
2092 ptr = section_ptr_add(interp, 1 + strlen(elfint));
2093 strcpy(ptr, elfint);
2096 /* add dynamic symbol table */
2097 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
2098 ".dynstr",
2099 ".hash", SHF_ALLOC);
2100 dynstr = s1->dynsym->link;
2102 /* add dynamic section */
2103 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
2104 SHF_ALLOC | SHF_WRITE);
2105 dynamic->link = dynstr;
2106 dynamic->sh_entsize = sizeof(ElfW(Dyn));
2108 build_got(s1);
2110 if (file_type == TCC_OUTPUT_EXE) {
2111 bind_exe_dynsyms(s1);
2112 if (s1->nb_errors)
2113 goto the_end;
2114 bind_libs_dynsyms(s1);
2115 } else {
2116 /* shared library case: simply export all global symbols */
2117 export_global_syms(s1);
2120 build_got_entries(s1);
2123 /* we add a section for symbols */
2124 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
2125 put_elf_str(strsec, "");
2127 /* Allocate strings for section names */
2128 textrel = alloc_sec_names(s1, file_type, strsec);
2130 if (dynamic) {
2131 /* add a list of needed dlls */
2132 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2133 DLLReference *dllref = s1->loaded_dlls[i];
2134 if (dllref->level == 0)
2135 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
2138 if (s1->rpath)
2139 put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
2140 put_elf_str(dynstr, s1->rpath));
2142 if (file_type == TCC_OUTPUT_DLL) {
2143 if (s1->soname)
2144 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
2145 /* XXX: currently, since we do not handle PIC code, we
2146 must relocate the readonly segments */
2147 if (textrel)
2148 put_dt(dynamic, DT_TEXTREL, 0);
2151 if (s1->symbolic)
2152 put_dt(dynamic, DT_SYMBOLIC, 0);
2154 dyninf.dynamic = dynamic;
2155 dyninf.dynstr = dynstr;
2156 /* remember offset and reserve space for 2nd call below */
2157 dyninf.data_offset = dynamic->data_offset;
2158 fill_dynamic(s1, &dyninf);
2159 dynamic->sh_size = dynamic->data_offset;
2160 dynstr->sh_size = dynstr->data_offset;
2163 /* compute number of program headers */
2164 if (file_type == TCC_OUTPUT_OBJ)
2165 phnum = 0;
2166 else if (file_type == TCC_OUTPUT_DLL)
2167 phnum = 3;
2168 else if (s1->static_link)
2169 phnum = 2;
2170 else
2171 phnum = 5;
2173 /* allocate program segment headers */
2174 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
2176 /* compute number of sections */
2177 shnum = s1->nb_sections;
2179 /* this array is used to reorder sections in the output file */
2180 sec_order = tcc_malloc(sizeof(int) * shnum);
2181 sec_order[0] = 0;
2183 /* compute section to program header mapping */
2184 file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2185 sec_order);
2187 /* Fill remaining program header and finalize relocation related to dynamic
2188 linking. */
2189 if (file_type != TCC_OUTPUT_OBJ) {
2190 fill_unloadable_phdr(phdr, phnum, interp, dynamic);
2191 if (dynamic) {
2192 dynamic->data_offset = dyninf.data_offset;
2193 fill_dynamic(s1, &dyninf);
2195 /* put in GOT the dynamic section address and relocate PLT */
2196 write32le(s1->got->data, dynamic->sh_addr);
2197 if (file_type == TCC_OUTPUT_EXE
2198 || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
2199 relocate_plt(s1);
2201 /* relocate symbols in .dynsym now that final addresses are known */
2202 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
2203 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
2204 /* do symbol relocation */
2205 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
2210 /* if building executable or DLL, then relocate each section
2211 except the GOT which is already relocated */
2212 ret = final_sections_reloc(s1);
2213 if (ret)
2214 goto the_end;
2215 tidy_section_headers(s1, sec_order);
2217 /* Perform relocation to GOT or PLT entries */
2218 if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2219 fill_got(s1);
2220 else if (s1->got)
2221 fill_local_got_entries(s1);
2224 /* Create the ELF file with name 'filename' */
2225 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2226 s1->nb_sections = shnum;
2227 the_end:
2228 tcc_free(sec_order);
2229 tcc_free(phdr);
2230 return ret;
2233 LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
2235 int ret;
2236 #ifdef TCC_TARGET_PE
2237 if (s->output_type != TCC_OUTPUT_OBJ) {
2238 ret = pe_output_file(s, filename);
2239 } else
2240 #endif
2241 ret = elf_output_file(s, filename);
2242 return ret;
2245 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2247 void *data;
2249 data = tcc_malloc(size);
2250 lseek(fd, file_offset, SEEK_SET);
2251 read(fd, data, size);
2252 return data;
2255 typedef struct SectionMergeInfo {
2256 Section *s; /* corresponding existing section */
2257 unsigned long offset; /* offset of the new section in the existing section */
2258 uint8_t new_section; /* true if section 's' was added */
2259 uint8_t link_once; /* true if link once section */
2260 } SectionMergeInfo;
2262 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
2264 int size = read(fd, h, sizeof *h);
2265 if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
2266 if (h->e_type == ET_REL)
2267 return AFF_BINTYPE_REL;
2268 if (h->e_type == ET_DYN)
2269 return AFF_BINTYPE_DYN;
2270 } else if (size >= 8) {
2271 if (0 == memcmp(h, ARMAG, 8))
2272 return AFF_BINTYPE_AR;
2273 #ifdef TCC_TARGET_COFF
2274 if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC)
2275 return AFF_BINTYPE_C67;
2276 #endif
2278 return 0;
2281 /* load an object file and merge it with current files */
2282 /* XXX: handle correctly stab (debug) info */
2283 ST_FUNC int tcc_load_object_file(TCCState *s1,
2284 int fd, unsigned long file_offset)
2286 ElfW(Ehdr) ehdr;
2287 ElfW(Shdr) *shdr, *sh;
2288 int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed;
2289 unsigned char *strsec, *strtab;
2290 int *old_to_new_syms;
2291 char *sh_name, *name;
2292 SectionMergeInfo *sm_table, *sm;
2293 ElfW(Sym) *sym, *symtab;
2294 ElfW_Rel *rel;
2295 Section *s;
2297 int stab_index;
2298 int stabstr_index;
2300 stab_index = stabstr_index = 0;
2302 lseek(fd, file_offset, SEEK_SET);
2303 if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL)
2304 goto fail1;
2305 /* test CPU specific stuff */
2306 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2307 ehdr.e_machine != EM_TCC_TARGET) {
2308 fail1:
2309 tcc_error_noabort("invalid object file");
2310 return -1;
2312 /* read sections */
2313 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2314 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2315 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2317 /* load section names */
2318 sh = &shdr[ehdr.e_shstrndx];
2319 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2321 /* load symtab and strtab */
2322 old_to_new_syms = NULL;
2323 symtab = NULL;
2324 strtab = NULL;
2325 nb_syms = 0;
2326 seencompressed = 0;
2327 for(i = 1; i < ehdr.e_shnum; i++) {
2328 sh = &shdr[i];
2329 if (sh->sh_type == SHT_SYMTAB) {
2330 if (symtab) {
2331 tcc_error_noabort("object must contain only one symtab");
2332 fail:
2333 ret = -1;
2334 goto the_end;
2336 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2337 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2338 sm_table[i].s = symtab_section;
2340 /* now load strtab */
2341 sh = &shdr[sh->sh_link];
2342 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2344 if (sh->sh_flags & SHF_COMPRESSED)
2345 seencompressed = 1;
2348 /* now examine each section and try to merge its content with the
2349 ones in memory */
2350 for(i = 1; i < ehdr.e_shnum; i++) {
2351 /* no need to examine section name strtab */
2352 if (i == ehdr.e_shstrndx)
2353 continue;
2354 sh = &shdr[i];
2355 sh_name = (char *) strsec + sh->sh_name;
2356 /* ignore sections types we do not handle */
2357 if (sh->sh_type != SHT_PROGBITS &&
2358 sh->sh_type != SHT_RELX &&
2359 #ifdef TCC_ARM_EABI
2360 sh->sh_type != SHT_ARM_EXIDX &&
2361 #endif
2362 sh->sh_type != SHT_NOBITS &&
2363 sh->sh_type != SHT_PREINIT_ARRAY &&
2364 sh->sh_type != SHT_INIT_ARRAY &&
2365 sh->sh_type != SHT_FINI_ARRAY &&
2366 strcmp(sh_name, ".stabstr")
2368 continue;
2369 if (seencompressed
2370 && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1)
2371 || (sh->sh_type == SHT_RELX
2372 && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name,
2373 ".debug_", sizeof(".debug_")-1))))
2374 continue;
2375 if (sh->sh_addralign < 1)
2376 sh->sh_addralign = 1;
2377 /* find corresponding section, if any */
2378 for(j = 1; j < s1->nb_sections;j++) {
2379 s = s1->sections[j];
2380 if (!strcmp(s->name, sh_name)) {
2381 if (!strncmp(sh_name, ".gnu.linkonce",
2382 sizeof(".gnu.linkonce") - 1)) {
2383 /* if a 'linkonce' section is already present, we
2384 do not add it again. It is a little tricky as
2385 symbols can still be defined in
2386 it. */
2387 sm_table[i].link_once = 1;
2388 goto next;
2389 } else {
2390 goto found;
2394 /* not found: create new section */
2395 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
2396 /* take as much info as possible from the section. sh_link and
2397 sh_info will be updated later */
2398 s->sh_addralign = sh->sh_addralign;
2399 s->sh_entsize = sh->sh_entsize;
2400 sm_table[i].new_section = 1;
2401 found:
2402 if (sh->sh_type != s->sh_type) {
2403 tcc_error_noabort("invalid section type");
2404 goto fail;
2407 /* align start of section */
2408 offset = s->data_offset;
2410 if (0 == strcmp(sh_name, ".stab")) {
2411 stab_index = i;
2412 goto no_align;
2414 if (0 == strcmp(sh_name, ".stabstr")) {
2415 stabstr_index = i;
2416 goto no_align;
2419 size = sh->sh_addralign - 1;
2420 offset = (offset + size) & ~size;
2421 if (sh->sh_addralign > s->sh_addralign)
2422 s->sh_addralign = sh->sh_addralign;
2423 s->data_offset = offset;
2424 no_align:
2425 sm_table[i].offset = offset;
2426 sm_table[i].s = s;
2427 /* concatenate sections */
2428 size = sh->sh_size;
2429 if (sh->sh_type != SHT_NOBITS) {
2430 unsigned char *ptr;
2431 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2432 ptr = section_ptr_add(s, size);
2433 read(fd, ptr, size);
2434 } else {
2435 s->data_offset += size;
2437 next: ;
2440 /* gr relocate stab strings */
2441 if (stab_index && stabstr_index) {
2442 Stab_Sym *a, *b;
2443 unsigned o;
2444 s = sm_table[stab_index].s;
2445 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2446 b = (Stab_Sym *)(s->data + s->data_offset);
2447 o = sm_table[stabstr_index].offset;
2448 while (a < b) {
2449 if (a->n_strx)
2450 a->n_strx += o;
2451 a++;
2455 /* second short pass to update sh_link and sh_info fields of new
2456 sections */
2457 for(i = 1; i < ehdr.e_shnum; i++) {
2458 s = sm_table[i].s;
2459 if (!s || !sm_table[i].new_section)
2460 continue;
2461 sh = &shdr[i];
2462 if (sh->sh_link > 0)
2463 s->link = sm_table[sh->sh_link].s;
2464 if (sh->sh_type == SHT_RELX) {
2465 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2466 /* update backward link */
2467 s1->sections[s->sh_info]->reloc = s;
2470 sm = sm_table;
2472 /* resolve symbols */
2473 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2475 sym = symtab + 1;
2476 for(i = 1; i < nb_syms; i++, sym++) {
2477 if (sym->st_shndx != SHN_UNDEF &&
2478 sym->st_shndx < SHN_LORESERVE) {
2479 sm = &sm_table[sym->st_shndx];
2480 if (sm->link_once) {
2481 /* if a symbol is in a link once section, we use the
2482 already defined symbol. It is very important to get
2483 correct relocations */
2484 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2485 name = (char *) strtab + sym->st_name;
2486 sym_index = find_elf_sym(symtab_section, name);
2487 if (sym_index)
2488 old_to_new_syms[i] = sym_index;
2490 continue;
2492 /* if no corresponding section added, no need to add symbol */
2493 if (!sm->s)
2494 continue;
2495 /* convert section number */
2496 sym->st_shndx = sm->s->sh_num;
2497 /* offset value */
2498 sym->st_value += sm->offset;
2500 /* add symbol */
2501 name = (char *) strtab + sym->st_name;
2502 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
2503 sym->st_info, sym->st_other,
2504 sym->st_shndx, name);
2505 old_to_new_syms[i] = sym_index;
2508 /* third pass to patch relocation entries */
2509 for(i = 1; i < ehdr.e_shnum; i++) {
2510 s = sm_table[i].s;
2511 if (!s)
2512 continue;
2513 sh = &shdr[i];
2514 offset = sm_table[i].offset;
2515 switch(s->sh_type) {
2516 case SHT_RELX:
2517 /* take relocation offset information */
2518 offseti = sm_table[sh->sh_info].offset;
2519 for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) {
2520 int type;
2521 unsigned sym_index;
2522 /* convert symbol index */
2523 type = ELFW(R_TYPE)(rel->r_info);
2524 sym_index = ELFW(R_SYM)(rel->r_info);
2525 /* NOTE: only one symtab assumed */
2526 if (sym_index >= nb_syms)
2527 goto invalid_reloc;
2528 sym_index = old_to_new_syms[sym_index];
2529 /* ignore link_once in rel section. */
2530 if (!sym_index && !sm->link_once
2531 #ifdef TCC_TARGET_ARM
2532 && type != R_ARM_V4BX
2533 #endif
2535 invalid_reloc:
2536 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2537 i, strsec + sh->sh_name, rel->r_offset);
2538 goto fail;
2540 rel->r_info = ELFW(R_INFO)(sym_index, type);
2541 /* offset the relocation offset */
2542 rel->r_offset += offseti;
2543 #ifdef TCC_TARGET_ARM
2544 /* Jumps and branches from a Thumb code to a PLT entry need
2545 special handling since PLT entries are ARM code.
2546 Unconditional bl instructions referencing PLT entries are
2547 handled by converting these instructions into blx
2548 instructions. Other case of instructions referencing a PLT
2549 entry require to add a Thumb stub before the PLT entry to
2550 switch to ARM mode. We set bit plt_thumb_stub of the
2551 attribute of a symbol to indicate such a case. */
2552 if (type == R_ARM_THM_JUMP24)
2553 get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
2554 #endif
2556 break;
2557 default:
2558 break;
2562 ret = 0;
2563 the_end:
2564 tcc_free(symtab);
2565 tcc_free(strtab);
2566 tcc_free(old_to_new_syms);
2567 tcc_free(sm_table);
2568 tcc_free(strsec);
2569 tcc_free(shdr);
2570 return ret;
2573 typedef struct ArchiveHeader {
2574 char ar_name[16]; /* name of this member */
2575 char ar_date[12]; /* file mtime */
2576 char ar_uid[6]; /* owner uid; printed as decimal */
2577 char ar_gid[6]; /* owner gid; printed as decimal */
2578 char ar_mode[8]; /* file mode, printed as octal */
2579 char ar_size[10]; /* file size, printed as decimal */
2580 char ar_fmag[2]; /* should contain ARFMAG */
2581 } ArchiveHeader;
2583 static int get_be32(const uint8_t *b)
2585 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2588 static long get_be64(const uint8_t *b)
2590 long long ret = get_be32(b);
2591 ret = (ret << 32) | (unsigned)get_be32(b+4);
2592 return (long)ret;
2595 /* load only the objects which resolve undefined symbols */
2596 static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
2598 long i, bound, nsyms, sym_index, off, ret;
2599 uint8_t *data;
2600 const char *ar_names, *p;
2601 const uint8_t *ar_index;
2602 ElfW(Sym) *sym;
2604 data = tcc_malloc(size);
2605 if (read(fd, data, size) != size)
2606 goto fail;
2607 nsyms = entrysize == 4 ? get_be32(data) : get_be64(data);
2608 ar_index = data + entrysize;
2609 ar_names = (char *) ar_index + nsyms * entrysize;
2611 do {
2612 bound = 0;
2613 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2614 sym_index = find_elf_sym(symtab_section, p);
2615 if(sym_index) {
2616 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2617 if(sym->st_shndx == SHN_UNDEF) {
2618 off = (entrysize == 4
2619 ? get_be32(ar_index + i * 4)
2620 : get_be64(ar_index + i * 8))
2621 + sizeof(ArchiveHeader);
2622 ++bound;
2623 if(tcc_load_object_file(s1, fd, off) < 0) {
2624 fail:
2625 ret = -1;
2626 goto the_end;
2631 } while(bound);
2632 ret = 0;
2633 the_end:
2634 tcc_free(data);
2635 return ret;
2638 /* load a '.a' file */
2639 ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte)
2641 ArchiveHeader hdr;
2642 char ar_size[11];
2643 char ar_name[17];
2644 char magic[8];
2645 int size, len, i;
2646 unsigned long file_offset;
2648 /* skip magic which was already checked */
2649 read(fd, magic, sizeof(magic));
2651 for(;;) {
2652 len = read(fd, &hdr, sizeof(hdr));
2653 if (len == 0)
2654 break;
2655 if (len != sizeof(hdr)) {
2656 tcc_error_noabort("invalid archive");
2657 return -1;
2659 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2660 ar_size[sizeof(hdr.ar_size)] = '\0';
2661 size = strtol(ar_size, NULL, 0);
2662 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2663 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2664 if (ar_name[i] != ' ')
2665 break;
2667 ar_name[i + 1] = '\0';
2668 file_offset = lseek(fd, 0, SEEK_CUR);
2669 /* align to even */
2670 size = (size + 1) & ~1;
2671 if (!strcmp(ar_name, "/")) {
2672 /* coff symbol table : we handle it */
2673 if (alacarte)
2674 return tcc_load_alacarte(s1, fd, size, 4);
2675 } else if (!strcmp(ar_name, "/SYM64/")) {
2676 if (alacarte)
2677 return tcc_load_alacarte(s1, fd, size, 8);
2678 } else {
2679 ElfW(Ehdr) ehdr;
2680 if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
2681 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2682 return -1;
2685 lseek(fd, file_offset + size, SEEK_SET);
2687 return 0;
2690 #ifndef TCC_TARGET_PE
2691 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2692 is referenced by the user (so it should be added as DT_NEEDED in
2693 the generated ELF file) */
2694 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2696 ElfW(Ehdr) ehdr;
2697 ElfW(Shdr) *shdr, *sh, *sh1;
2698 int i, j, nb_syms, nb_dts, sym_bind, ret;
2699 ElfW(Sym) *sym, *dynsym;
2700 ElfW(Dyn) *dt, *dynamic;
2701 unsigned char *dynstr;
2702 const char *name, *soname;
2703 DLLReference *dllref;
2705 read(fd, &ehdr, sizeof(ehdr));
2707 /* test CPU specific stuff */
2708 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2709 ehdr.e_machine != EM_TCC_TARGET) {
2710 tcc_error_noabort("bad architecture");
2711 return -1;
2714 /* read sections */
2715 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2717 /* load dynamic section and dynamic symbols */
2718 nb_syms = 0;
2719 nb_dts = 0;
2720 dynamic = NULL;
2721 dynsym = NULL; /* avoid warning */
2722 dynstr = NULL; /* avoid warning */
2723 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2724 switch(sh->sh_type) {
2725 case SHT_DYNAMIC:
2726 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2727 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2728 break;
2729 case SHT_DYNSYM:
2730 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2731 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2732 sh1 = &shdr[sh->sh_link];
2733 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2734 break;
2735 default:
2736 break;
2740 /* compute the real library name */
2741 soname = tcc_basename(filename);
2743 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2744 if (dt->d_tag == DT_SONAME) {
2745 soname = (char *) dynstr + dt->d_un.d_val;
2749 /* if the dll is already loaded, do not load it */
2750 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2751 dllref = s1->loaded_dlls[i];
2752 if (!strcmp(soname, dllref->name)) {
2753 /* but update level if needed */
2754 if (level < dllref->level)
2755 dllref->level = level;
2756 ret = 0;
2757 goto the_end;
2761 /* add the dll and its level */
2762 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2763 dllref->level = level;
2764 strcpy(dllref->name, soname);
2765 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2767 /* add dynamic symbols in dynsym_section */
2768 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2769 sym_bind = ELFW(ST_BIND)(sym->st_info);
2770 if (sym_bind == STB_LOCAL)
2771 continue;
2772 name = (char *) dynstr + sym->st_name;
2773 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2774 sym->st_info, sym->st_other, sym->st_shndx, name);
2777 /* load all referenced DLLs */
2778 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2779 switch(dt->d_tag) {
2780 case DT_NEEDED:
2781 name = (char *) dynstr + dt->d_un.d_val;
2782 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2783 dllref = s1->loaded_dlls[j];
2784 if (!strcmp(name, dllref->name))
2785 goto already_loaded;
2787 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2788 tcc_error_noabort("referenced dll '%s' not found", name);
2789 ret = -1;
2790 goto the_end;
2792 already_loaded:
2793 break;
2796 ret = 0;
2797 the_end:
2798 tcc_free(dynstr);
2799 tcc_free(dynsym);
2800 tcc_free(dynamic);
2801 tcc_free(shdr);
2802 return ret;
2805 #define LD_TOK_NAME 256
2806 #define LD_TOK_EOF (-1)
2808 /* return next ld script token */
2809 static int ld_next(TCCState *s1, char *name, int name_size)
2811 int c;
2812 char *q;
2814 redo:
2815 switch(ch) {
2816 case ' ':
2817 case '\t':
2818 case '\f':
2819 case '\v':
2820 case '\r':
2821 case '\n':
2822 inp();
2823 goto redo;
2824 case '/':
2825 minp();
2826 if (ch == '*') {
2827 file->buf_ptr = parse_comment(file->buf_ptr);
2828 ch = file->buf_ptr[0];
2829 goto redo;
2830 } else {
2831 q = name;
2832 *q++ = '/';
2833 goto parse_name;
2835 break;
2836 case '\\':
2837 ch = handle_eob();
2838 if (ch != '\\')
2839 goto redo;
2840 /* fall through */
2841 /* case 'a' ... 'z': */
2842 case 'a':
2843 case 'b':
2844 case 'c':
2845 case 'd':
2846 case 'e':
2847 case 'f':
2848 case 'g':
2849 case 'h':
2850 case 'i':
2851 case 'j':
2852 case 'k':
2853 case 'l':
2854 case 'm':
2855 case 'n':
2856 case 'o':
2857 case 'p':
2858 case 'q':
2859 case 'r':
2860 case 's':
2861 case 't':
2862 case 'u':
2863 case 'v':
2864 case 'w':
2865 case 'x':
2866 case 'y':
2867 case 'z':
2868 /* case 'A' ... 'z': */
2869 case 'A':
2870 case 'B':
2871 case 'C':
2872 case 'D':
2873 case 'E':
2874 case 'F':
2875 case 'G':
2876 case 'H':
2877 case 'I':
2878 case 'J':
2879 case 'K':
2880 case 'L':
2881 case 'M':
2882 case 'N':
2883 case 'O':
2884 case 'P':
2885 case 'Q':
2886 case 'R':
2887 case 'S':
2888 case 'T':
2889 case 'U':
2890 case 'V':
2891 case 'W':
2892 case 'X':
2893 case 'Y':
2894 case 'Z':
2895 case '_':
2896 case '.':
2897 case '$':
2898 case '~':
2899 q = name;
2900 parse_name:
2901 for(;;) {
2902 if (!((ch >= 'a' && ch <= 'z') ||
2903 (ch >= 'A' && ch <= 'Z') ||
2904 (ch >= '0' && ch <= '9') ||
2905 strchr("/.-_+=$:\\,~", ch)))
2906 break;
2907 if ((q - name) < name_size - 1) {
2908 *q++ = ch;
2910 minp();
2912 *q = '\0';
2913 c = LD_TOK_NAME;
2914 break;
2915 case CH_EOF:
2916 c = LD_TOK_EOF;
2917 break;
2918 default:
2919 c = ch;
2920 inp();
2921 break;
2923 return c;
2926 static int ld_add_file(TCCState *s1, const char filename[])
2928 if (filename[0] == '/') {
2929 if (CONFIG_SYSROOT[0] == '\0'
2930 && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0)
2931 return 0;
2932 filename = tcc_basename(filename);
2934 return tcc_add_dll(s1, filename, 0);
2937 static inline int new_undef_syms(void)
2939 int ret = 0;
2940 ret = new_undef_sym;
2941 new_undef_sym = 0;
2942 return ret;
2945 static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
2947 char filename[1024], libname[1024];
2948 int t, group, nblibs = 0, ret = 0;
2949 char **libs = NULL;
2951 group = !strcmp(cmd, "GROUP");
2952 if (!as_needed)
2953 new_undef_syms();
2954 t = ld_next(s1, filename, sizeof(filename));
2955 if (t != '(')
2956 expect("(");
2957 t = ld_next(s1, filename, sizeof(filename));
2958 for(;;) {
2959 libname[0] = '\0';
2960 if (t == LD_TOK_EOF) {
2961 tcc_error_noabort("unexpected end of file");
2962 ret = -1;
2963 goto lib_parse_error;
2964 } else if (t == ')') {
2965 break;
2966 } else if (t == '-') {
2967 t = ld_next(s1, filename, sizeof(filename));
2968 if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
2969 tcc_error_noabort("library name expected");
2970 ret = -1;
2971 goto lib_parse_error;
2973 pstrcpy(libname, sizeof libname, &filename[1]);
2974 if (s1->static_link) {
2975 snprintf(filename, sizeof filename, "lib%s.a", libname);
2976 } else {
2977 snprintf(filename, sizeof filename, "lib%s.so", libname);
2979 } else if (t != LD_TOK_NAME) {
2980 tcc_error_noabort("filename expected");
2981 ret = -1;
2982 goto lib_parse_error;
2984 if (!strcmp(filename, "AS_NEEDED")) {
2985 ret = ld_add_file_list(s1, cmd, 1);
2986 if (ret)
2987 goto lib_parse_error;
2988 } else {
2989 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2990 if (!as_needed) {
2991 ret = ld_add_file(s1, filename);
2992 if (ret)
2993 goto lib_parse_error;
2994 if (group) {
2995 /* Add the filename *and* the libname to avoid future conversions */
2996 dynarray_add(&libs, &nblibs, tcc_strdup(filename));
2997 if (libname[0] != '\0')
2998 dynarray_add(&libs, &nblibs, tcc_strdup(libname));
3002 t = ld_next(s1, filename, sizeof(filename));
3003 if (t == ',') {
3004 t = ld_next(s1, filename, sizeof(filename));
3007 if (group && !as_needed) {
3008 while (new_undef_syms()) {
3009 int i;
3011 for (i = 0; i < nblibs; i ++)
3012 ld_add_file(s1, libs[i]);
3015 lib_parse_error:
3016 dynarray_reset(&libs, &nblibs);
3017 return ret;
3020 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
3021 files */
3022 ST_FUNC int tcc_load_ldscript(TCCState *s1)
3024 char cmd[64];
3025 char filename[1024];
3026 int t, ret;
3028 ch = handle_eob();
3029 for(;;) {
3030 t = ld_next(s1, cmd, sizeof(cmd));
3031 if (t == LD_TOK_EOF)
3032 return 0;
3033 else if (t != LD_TOK_NAME)
3034 return -1;
3035 if (!strcmp(cmd, "INPUT") ||
3036 !strcmp(cmd, "GROUP")) {
3037 ret = ld_add_file_list(s1, cmd, 0);
3038 if (ret)
3039 return ret;
3040 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
3041 !strcmp(cmd, "TARGET")) {
3042 /* ignore some commands */
3043 t = ld_next(s1, cmd, sizeof(cmd));
3044 if (t != '(')
3045 expect("(");
3046 for(;;) {
3047 t = ld_next(s1, filename, sizeof(filename));
3048 if (t == LD_TOK_EOF) {
3049 tcc_error_noabort("unexpected end of file");
3050 return -1;
3051 } else if (t == ')') {
3052 break;
3055 } else {
3056 return -1;
3059 return 0;
3061 #endif /* !TCC_TARGET_PE */