Add -std=c11 option which sets __STDC_VERSION__ to 201112L and allows to implement...
[tinycc.git] / tccelf.c
blob0d372e0f747d808b222ce6a00e3155919550ed2f
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 type, count;
871 count = 0;
872 for_each_elem(sr, 0, rel, ElfW_Rel) {
873 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
874 int sym_index = ELFW(R_SYM)(rel->r_info);
875 #endif
876 type = ELFW(R_TYPE)(rel->r_info);
877 switch(type) {
878 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
879 #if defined(TCC_TARGET_I386)
880 case R_386_32:
881 if (!get_sym_attr(s1, sym_index, 0)->dyn_index
882 && ((ElfW(Sym)*)symtab_section->data + sym_index)->st_shndx == SHN_UNDEF) {
883 /* don't fixup unresolved (weak) symbols */
884 rel->r_info = ELFW(R_INFO)(sym_index, R_386_RELATIVE);
885 break;
887 #elif defined(TCC_TARGET_X86_64)
888 case R_X86_64_32:
889 case R_X86_64_32S:
890 case R_X86_64_64:
891 #endif
892 count++;
893 break;
894 #if defined(TCC_TARGET_I386)
895 case R_386_PC32:
896 #elif defined(TCC_TARGET_X86_64)
897 case R_X86_64_PC32:
898 #endif
899 if (get_sym_attr(s1, sym_index, 0)->dyn_index)
900 count++;
901 break;
902 #endif
903 default:
904 break;
907 if (count) {
908 /* allocate the section */
909 sr->sh_flags |= SHF_ALLOC;
910 sr->sh_size = count * sizeof(ElfW_Rel);
912 return count;
915 static void build_got(TCCState *s1)
917 /* if no got, then create it */
918 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
919 s1->got->sh_entsize = 4;
920 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
921 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
922 /* keep space for _DYNAMIC pointer and two dummy got entries */
923 section_ptr_add(s1->got, 3 * PTR_SIZE);
926 /* Create a GOT and (for function call) a PLT entry corresponding to a symbol
927 in s1->symtab. When creating the dynamic symbol table entry for the GOT
928 relocation, use 'size' and 'info' for the corresponding symbol metadata.
929 Returns the offset of the GOT or (if any) PLT entry. */
930 static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type,
931 unsigned long size,
932 int info, int sym_index)
934 int need_plt_entry;
935 const char *name;
936 ElfW(Sym) *sym;
937 struct sym_attr *attr;
938 unsigned got_offset;
939 char plt_name[100];
940 int len;
942 need_plt_entry = (dyn_reloc_type == R_JMP_SLOT);
943 attr = get_sym_attr(s1, sym_index, 1);
945 /* In case a function is both called and its address taken 2 GOT entries
946 are created, one for taking the address (GOT) and the other for the PLT
947 entry (PLTGOT). */
948 if (need_plt_entry ? attr->plt_offset : attr->got_offset)
949 return attr;
951 /* create the GOT entry */
952 got_offset = s1->got->data_offset;
953 section_ptr_add(s1->got, PTR_SIZE);
955 /* Create the GOT relocation that will insert the address of the object or
956 function of interest in the GOT entry. This is a static relocation for
957 memory output (dlsym will give us the address of symbols) and dynamic
958 relocation otherwise (executable and DLLs). The relocation should be
959 done lazily for GOT entry with *_JUMP_SLOT relocation type (the one
960 associated to a PLT entry) but is currently done at load time for an
961 unknown reason. */
963 sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
964 name = (char *) symtab_section->link->data + sym->st_name;
966 if (s1->dynsym) {
967 if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
968 /* Hack alarm. We don't want to emit dynamic symbols
969 and symbol based relocs for STB_LOCAL symbols, but rather
970 want to resolve them directly. At this point the symbol
971 values aren't final yet, so we must defer this. We will later
972 have to create a RELATIVE reloc anyway, so we misuse the
973 relocation slot to smuggle the symbol reference until
974 fill_local_got_entries. Not that the sym_index is
975 relative to symtab_section, not s1->dynsym! Nevertheless
976 we use s1->dyn_sym so that if this is the first call
977 that got->reloc is correctly created. Also note that
978 RELATIVE relocs are not normally created for the .got,
979 so the types serves as a marker for later (and is retained
980 also for the final output, which is okay because then the
981 got is just normal data). */
982 put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE,
983 sym_index);
984 } else {
985 if (0 == attr->dyn_index)
986 attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size,
987 info, 0, sym->st_shndx, name);
988 put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type,
989 attr->dyn_index);
991 } else {
992 put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
993 sym_index);
996 if (need_plt_entry) {
997 if (!s1->plt) {
998 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
999 SHF_ALLOC | SHF_EXECINSTR);
1000 s1->plt->sh_entsize = 4;
1003 attr->plt_offset = create_plt_entry(s1, got_offset, attr);
1005 /* create a symbol 'sym@plt' for the PLT jump vector */
1006 len = strlen(name);
1007 if (len > sizeof plt_name - 5)
1008 len = sizeof plt_name - 5;
1009 memcpy(plt_name, name, len);
1010 strcpy(plt_name + len, "@plt");
1011 attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size,
1012 ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name);
1014 } else {
1015 attr->got_offset = got_offset;
1018 return attr;
1021 /* build GOT and PLT entries */
1022 ST_FUNC void build_got_entries(TCCState *s1)
1024 Section *s;
1025 ElfW_Rel *rel;
1026 ElfW(Sym) *sym;
1027 int i, type, gotplt_entry, reloc_type, sym_index;
1028 struct sym_attr *attr;
1030 for(i = 1; i < s1->nb_sections; i++) {
1031 s = s1->sections[i];
1032 if (s->sh_type != SHT_RELX)
1033 continue;
1034 /* no need to handle got relocations */
1035 if (s->link != symtab_section)
1036 continue;
1037 for_each_elem(s, 0, rel, ElfW_Rel) {
1038 type = ELFW(R_TYPE)(rel->r_info);
1039 gotplt_entry = gotplt_entry_type(type);
1040 sym_index = ELFW(R_SYM)(rel->r_info);
1041 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1043 if (gotplt_entry == NO_GOTPLT_ENTRY) {
1044 continue;
1047 /* Automatically create PLT/GOT [entry] if it is an undefined
1048 reference (resolved at runtime), or the symbol is absolute,
1049 probably created by tcc_add_symbol, and thus on 64-bit
1050 targets might be too far from application code. */
1051 if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
1052 if (sym->st_shndx == SHN_UNDEF) {
1053 ElfW(Sym) *esym;
1054 int dynindex;
1055 if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT)
1056 continue;
1057 /* Relocations for UNDEF symbols would normally need
1058 to be transferred into the executable or shared object.
1059 If that were done AUTO_GOTPLT_ENTRY wouldn't exist.
1060 But TCC doesn't do that (at least for exes), so we
1061 need to resolve all such relocs locally. And that
1062 means PLT slots for functions in DLLs and COPY relocs for
1063 data symbols. COPY relocs were generated in
1064 bind_exe_dynsyms (and the symbol adjusted to be defined),
1065 and for functions we were generated a dynamic symbol
1066 of function type. */
1067 if (s1->dynsym) {
1068 /* dynsym isn't set for -run :-/ */
1069 dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index;
1070 esym = (ElfW(Sym) *)s1->dynsym->data + dynindex;
1071 if (dynindex
1072 && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC
1073 || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE
1074 && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC)))
1075 goto jmp_slot;
1077 } else if (!(sym->st_shndx == SHN_ABS
1078 #ifndef TCC_TARGET_ARM
1079 && PTR_SIZE == 8
1080 #endif
1082 continue;
1085 #ifdef TCC_TARGET_X86_64
1086 if ((type == R_X86_64_PLT32 || type == R_X86_64_PC32) &&
1087 (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT ||
1088 ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)) {
1089 rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32);
1090 continue;
1092 #endif
1093 if (code_reloc(type)) {
1094 jmp_slot:
1095 reloc_type = R_JMP_SLOT;
1096 } else
1097 reloc_type = R_GLOB_DAT;
1099 if (!s1->got)
1100 build_got(s1);
1102 if (gotplt_entry == BUILD_GOT_ONLY)
1103 continue;
1105 attr = put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1106 sym_index);
1108 if (reloc_type == R_JMP_SLOT)
1109 rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
1114 /* put dynamic tag */
1115 static void put_dt(Section *dynamic, int dt, addr_t val)
1117 ElfW(Dyn) *dyn;
1118 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1119 dyn->d_tag = dt;
1120 dyn->d_un.d_val = val;
1123 #ifndef TCC_TARGET_PE
1124 static void add_init_array_defines(TCCState *s1, const char *section_name)
1126 Section *s;
1127 long end_offset;
1128 char sym_start[1024];
1129 char sym_end[1024];
1131 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1132 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1134 s = find_section(s1, section_name);
1135 if (!s) {
1136 end_offset = 0;
1137 s = data_section;
1138 } else {
1139 end_offset = s->data_offset;
1142 set_elf_sym(symtab_section,
1143 0, 0,
1144 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1145 s->sh_num, sym_start);
1146 set_elf_sym(symtab_section,
1147 end_offset, 0,
1148 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1149 s->sh_num, sym_end);
1152 static int tcc_add_support(TCCState *s1, const char *filename)
1154 char buf[1024];
1155 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename);
1156 return tcc_add_file(s1, buf);
1158 #endif
1160 ST_FUNC void tcc_add_bcheck(TCCState *s1)
1162 #ifdef CONFIG_TCC_BCHECK
1163 addr_t *ptr;
1164 int sym_index;
1166 if (0 == s1->do_bounds_check)
1167 return;
1168 /* XXX: add an object file to do that */
1169 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1170 *ptr = 0;
1171 set_elf_sym(symtab_section, 0, 0,
1172 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1173 bounds_section->sh_num, "__bounds_start");
1174 /* pull bcheck.o from libtcc1.a */
1175 sym_index = set_elf_sym(symtab_section, 0, 0,
1176 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1177 SHN_UNDEF, "__bound_init");
1178 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1179 /* add 'call __bound_init()' in .init section */
1180 Section *init_section = find_section(s1, ".init");
1181 unsigned char *pinit = section_ptr_add(init_section, 5);
1182 pinit[0] = 0xe8;
1183 write32le(pinit + 1, -4);
1184 put_elf_reloc(symtab_section, init_section,
1185 init_section->data_offset - 4, R_386_PC32, sym_index);
1186 /* R_386_PC32 = R_X86_64_PC32 = 2 */
1188 #endif
1191 /* add tcc runtime libraries */
1192 ST_FUNC void tcc_add_runtime(TCCState *s1)
1194 s1->filetype = 0;
1195 tcc_add_bcheck(s1);
1196 tcc_add_pragma_libs(s1);
1197 #ifndef TCC_TARGET_PE
1198 /* add libc */
1199 if (!s1->nostdlib) {
1200 tcc_add_library_err(s1, "c");
1201 #ifdef TCC_LIBGCC
1202 if (!s1->static_link) {
1203 if (TCC_LIBGCC[0] == '/')
1204 tcc_add_file(s1, TCC_LIBGCC);
1205 else
1206 tcc_add_dll(s1, TCC_LIBGCC, 0);
1208 #endif
1209 tcc_add_support(s1, TCC_LIBTCC1);
1210 /* add crt end if not memory output */
1211 if (s1->output_type != TCC_OUTPUT_MEMORY)
1212 tcc_add_crt(s1, "crtn.o");
1214 #endif
1217 /* add various standard linker symbols (must be done after the
1218 sections are filled (for example after allocating common
1219 symbols)) */
1220 static void tcc_add_linker_symbols(TCCState *s1)
1222 char buf[1024];
1223 int i;
1224 Section *s;
1226 set_elf_sym(symtab_section,
1227 text_section->data_offset, 0,
1228 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1229 text_section->sh_num, "_etext");
1230 set_elf_sym(symtab_section,
1231 data_section->data_offset, 0,
1232 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1233 data_section->sh_num, "_edata");
1234 set_elf_sym(symtab_section,
1235 bss_section->data_offset, 0,
1236 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1237 bss_section->sh_num, "_end");
1238 #ifndef TCC_TARGET_PE
1239 /* horrible new standard ldscript defines */
1240 add_init_array_defines(s1, ".preinit_array");
1241 add_init_array_defines(s1, ".init_array");
1242 add_init_array_defines(s1, ".fini_array");
1243 #endif
1245 /* add start and stop symbols for sections whose name can be
1246 expressed in C */
1247 for(i = 1; i < s1->nb_sections; i++) {
1248 s = s1->sections[i];
1249 if (s->sh_type == SHT_PROGBITS &&
1250 (s->sh_flags & SHF_ALLOC)) {
1251 const char *p;
1252 int ch;
1254 /* check if section name can be expressed in C */
1255 p = s->name;
1256 for(;;) {
1257 ch = *p;
1258 if (!ch)
1259 break;
1260 if (!isid(ch) && !isnum(ch))
1261 goto next_sec;
1262 p++;
1264 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1265 set_elf_sym(symtab_section,
1266 0, 0,
1267 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1268 s->sh_num, buf);
1269 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1270 set_elf_sym(symtab_section,
1271 s->data_offset, 0,
1272 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1273 s->sh_num, buf);
1275 next_sec: ;
1279 ST_FUNC void resolve_common_syms(TCCState *s1)
1281 ElfW(Sym) *sym;
1283 /* Allocate common symbols in BSS. */
1284 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1285 if (sym->st_shndx == SHN_COMMON) {
1286 /* symbol alignment is in st_value for SHN_COMMONs */
1287 sym->st_value = section_add(bss_section, sym->st_size,
1288 sym->st_value);
1289 sym->st_shndx = bss_section->sh_num;
1293 /* Now assign linker provided symbols their value. */
1294 tcc_add_linker_symbols(s1);
1297 static void tcc_output_binary(TCCState *s1, FILE *f,
1298 const int *sec_order)
1300 Section *s;
1301 int i, offset, size;
1303 offset = 0;
1304 for(i=1;i<s1->nb_sections;i++) {
1305 s = s1->sections[sec_order[i]];
1306 if (s->sh_type != SHT_NOBITS &&
1307 (s->sh_flags & SHF_ALLOC)) {
1308 while (offset < s->sh_offset) {
1309 fputc(0, f);
1310 offset++;
1312 size = s->sh_size;
1313 fwrite(s->data, 1, size, f);
1314 offset += size;
1319 ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1321 int sym_index = ELFW(R_SYM) (rel->r_info);
1322 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1323 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1324 unsigned offset = attr->got_offset;
1326 if (0 == offset)
1327 return;
1328 section_reserve(s1->got, offset + PTR_SIZE);
1329 #ifdef TCC_TARGET_X86_64
1330 write64le(s1->got->data + offset, sym->st_value);
1331 #else
1332 write32le(s1->got->data + offset, sym->st_value);
1333 #endif
1336 /* Perform relocation to GOT or PLT entries */
1337 ST_FUNC void fill_got(TCCState *s1)
1339 Section *s;
1340 ElfW_Rel *rel;
1341 int i;
1343 for(i = 1; i < s1->nb_sections; i++) {
1344 s = s1->sections[i];
1345 if (s->sh_type != SHT_RELX)
1346 continue;
1347 /* no need to handle got relocations */
1348 if (s->link != symtab_section)
1349 continue;
1350 for_each_elem(s, 0, rel, ElfW_Rel) {
1351 switch (ELFW(R_TYPE) (rel->r_info)) {
1352 case R_X86_64_GOT32:
1353 case R_X86_64_GOTPCREL:
1354 case R_X86_64_GOTPCRELX:
1355 case R_X86_64_REX_GOTPCRELX:
1356 case R_X86_64_PLT32:
1357 fill_got_entry(s1, rel);
1358 break;
1364 /* See put_got_entry for a description. This is the second stage
1365 where GOT references to local defined symbols are rewritten. */
1366 static void fill_local_got_entries(TCCState *s1)
1368 ElfW_Rel *rel;
1369 if (!s1->got->reloc)
1370 return;
1371 for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
1372 if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) {
1373 int sym_index = ELFW(R_SYM) (rel->r_info);
1374 ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1375 struct sym_attr *attr = get_sym_attr(s1, sym_index, 0);
1376 unsigned offset = attr->got_offset;
1377 if (offset != rel->r_offset - s1->got->sh_addr)
1378 tcc_error_noabort("huh");
1379 rel->r_info = ELFW(R_INFO)(0, R_RELATIVE);
1380 #if SHT_RELX == SHT_RELA
1381 rel->r_addend = sym->st_value;
1382 #else
1383 /* All our REL architectures also happen to be 32bit LE. */
1384 write32le(s1->got->data + offset, sym->st_value);
1385 #endif
1390 /* Bind symbols of executable: resolve undefined symbols from exported symbols
1391 in shared libraries and export non local defined symbols to shared libraries
1392 if -rdynamic switch was given on command line */
1393 static void bind_exe_dynsyms(TCCState *s1)
1395 const char *name;
1396 int sym_index, index;
1397 ElfW(Sym) *sym, *esym;
1398 int type;
1400 /* Resolve undefined symbols from dynamic symbols. When there is a match:
1401 - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT
1402 - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */
1403 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1404 if (sym->st_shndx == SHN_UNDEF) {
1405 name = (char *) symtab_section->link->data + sym->st_name;
1406 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1407 if (sym_index) {
1408 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1409 type = ELFW(ST_TYPE)(esym->st_info);
1410 if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) {
1411 /* Indirect functions shall have STT_FUNC type in executable
1412 * dynsym section. Indeed, a dlsym call following a lazy
1413 * resolution would pick the symbol value from the
1414 * executable dynsym entry which would contain the address
1415 * of the function wanted by the caller of dlsym instead of
1416 * the address of the function that would return that
1417 * address */
1418 int dynindex
1419 = put_elf_sym(s1->dynsym, 0, esym->st_size,
1420 ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0,
1421 name);
1422 int index = sym - (ElfW(Sym) *) symtab_section->data;
1423 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1424 } else if (type == STT_OBJECT) {
1425 unsigned long offset;
1426 ElfW(Sym) *dynsym;
1427 offset = bss_section->data_offset;
1428 /* XXX: which alignment ? */
1429 offset = (offset + 16 - 1) & -16;
1430 set_elf_sym (s1->symtab, offset, esym->st_size,
1431 esym->st_info, 0, bss_section->sh_num, name);
1432 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1433 esym->st_info, 0, bss_section->sh_num,
1434 name);
1436 /* Ensure R_COPY works for weak symbol aliases */
1437 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1438 for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
1439 if ((dynsym->st_value == esym->st_value)
1440 && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) {
1441 char *dynname = (char *) s1->dynsymtab_section->link->data
1442 + dynsym->st_name;
1443 put_elf_sym(s1->dynsym, offset, dynsym->st_size,
1444 dynsym->st_info, 0,
1445 bss_section->sh_num, dynname);
1446 break;
1451 put_elf_reloc(s1->dynsym, bss_section,
1452 offset, R_COPY, index);
1453 offset += esym->st_size;
1454 bss_section->data_offset = offset;
1456 } else {
1457 /* STB_WEAK undefined symbols are accepted */
1458 /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */
1459 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1460 !strcmp(name, "_fp_hw")) {
1461 } else {
1462 tcc_error_noabort("undefined symbol '%s'", name);
1465 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1466 /* if -rdynamic option, then export all non local symbols */
1467 name = (char *) symtab_section->link->data + sym->st_name;
1468 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
1469 0, sym->st_shndx, name);
1474 /* Bind symbols of libraries: export all non local symbols of executable that
1475 are referenced by shared libraries. The reason is that the dynamic loader
1476 search symbol first in executable and then in libraries. Therefore a
1477 reference to a symbol already defined by a library can still be resolved by
1478 a symbol in the executable. */
1479 static void bind_libs_dynsyms(TCCState *s1)
1481 const char *name;
1482 int sym_index;
1483 ElfW(Sym) *sym, *esym;
1485 for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) {
1486 name = (char *) s1->dynsymtab_section->link->data + esym->st_name;
1487 sym_index = find_elf_sym(symtab_section, name);
1488 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1489 if (sym_index && sym->st_shndx != SHN_UNDEF
1490 && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1491 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1492 sym->st_info, 0, sym->st_shndx, name);
1493 } else if (esym->st_shndx == SHN_UNDEF) {
1494 /* weak symbols can stay undefined */
1495 if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
1496 tcc_warning("undefined dynamic symbol '%s'", name);
1501 /* Export all non local symbols. This is used by shared libraries so that the
1502 non local symbols they define can resolve a reference in another shared
1503 library or in the executable. Correspondingly, it allows undefined local
1504 symbols to be resolved by other shared libraries or by the executable. */
1505 static void export_global_syms(TCCState *s1)
1507 int dynindex, index;
1508 const char *name;
1509 ElfW(Sym) *sym;
1511 for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1512 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1513 name = (char *) symtab_section->link->data + sym->st_name;
1514 dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1515 sym->st_info, 0, sym->st_shndx, name);
1516 index = sym - (ElfW(Sym) *) symtab_section->data;
1517 get_sym_attr(s1, index, 1)->dyn_index = dynindex;
1522 /* Allocate strings for section names and decide if an unallocated section
1523 should be output.
1524 NOTE: the strsec section comes last, so its size is also correct ! */
1525 static int alloc_sec_names(TCCState *s1, int file_type, Section *strsec)
1527 int i;
1528 Section *s;
1529 int textrel = 0;
1531 /* Allocate strings for section names */
1532 for(i = 1; i < s1->nb_sections; i++) {
1533 s = s1->sections[i];
1534 /* when generating a DLL, we include relocations but we may
1535 patch them */
1536 if (file_type == TCC_OUTPUT_DLL &&
1537 s->sh_type == SHT_RELX &&
1538 !(s->sh_flags & SHF_ALLOC) &&
1539 (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) &&
1540 prepare_dynamic_rel(s1, s)) {
1541 if (s1->sections[s->sh_info]->sh_flags & SHF_EXECINSTR)
1542 textrel = 1;
1543 } else if ((s1->do_debug && s->sh_type != SHT_RELX) ||
1544 file_type == TCC_OUTPUT_OBJ ||
1545 (s->sh_flags & SHF_ALLOC) ||
1546 i == (s1->nb_sections - 1)) {
1547 /* we output all sections if debug or object file */
1548 s->sh_size = s->data_offset;
1550 if (s->sh_size || (s->sh_flags & SHF_ALLOC))
1551 s->sh_name = put_elf_str(strsec, s->name);
1553 strsec->sh_size = strsec->data_offset;
1554 return textrel;
1557 /* Info to be copied in dynamic section */
1558 struct dyn_inf {
1559 Section *dynamic;
1560 Section *dynstr;
1561 unsigned long data_offset;
1562 addr_t rel_addr;
1563 addr_t rel_size;
1564 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1565 addr_t bss_addr;
1566 addr_t bss_size;
1567 #endif
1570 /* Assign sections to segments and decide how are sections laid out when loaded
1571 in memory. This function also fills corresponding program headers. */
1572 static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1573 Section *interp, Section* strsec,
1574 struct dyn_inf *dyninf, int *sec_order)
1576 int i, j, k, file_type, sh_order_index, file_offset;
1577 unsigned long s_align;
1578 long long tmp;
1579 addr_t addr;
1580 ElfW(Phdr) *ph;
1581 Section *s;
1583 file_type = s1->output_type;
1584 sh_order_index = 1;
1585 file_offset = 0;
1586 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
1587 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1588 s_align = ELF_PAGE_SIZE;
1589 if (s1->section_align)
1590 s_align = s1->section_align;
1592 if (phnum > 0) {
1593 if (s1->has_text_addr) {
1594 int a_offset, p_offset;
1595 addr = s1->text_addr;
1596 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1597 ELF_PAGE_SIZE */
1598 a_offset = (int) (addr & (s_align - 1));
1599 p_offset = file_offset & (s_align - 1);
1600 if (a_offset < p_offset)
1601 a_offset += s_align;
1602 file_offset += (a_offset - p_offset);
1603 } else {
1604 if (file_type == TCC_OUTPUT_DLL)
1605 addr = 0;
1606 else
1607 addr = ELF_START_ADDR;
1608 /* compute address after headers */
1609 addr += (file_offset & (s_align - 1));
1612 ph = &phdr[0];
1613 /* Leave one program headers for the program interpreter and one for
1614 the program header table itself if needed. These are done later as
1615 they require section layout to be done first. */
1616 if (interp)
1617 ph += 2;
1619 /* dynamic relocation table information, for .dynamic section */
1620 dyninf->rel_addr = dyninf->rel_size = 0;
1621 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1622 dyninf->bss_addr = dyninf->bss_size = 0;
1623 #endif
1625 for(j = 0; j < 2; j++) {
1626 ph->p_type = PT_LOAD;
1627 if (j == 0)
1628 ph->p_flags = PF_R | PF_X;
1629 else
1630 ph->p_flags = PF_R | PF_W;
1631 ph->p_align = s_align;
1633 /* Decide the layout of sections loaded in memory. This must
1634 be done before program headers are filled since they contain
1635 info about the layout. We do the following ordering: interp,
1636 symbol tables, relocations, progbits, nobits */
1637 /* XXX: do faster and simpler sorting */
1638 for(k = 0; k < 5; k++) {
1639 for(i = 1; i < s1->nb_sections; i++) {
1640 s = s1->sections[i];
1641 /* compute if section should be included */
1642 if (j == 0) {
1643 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1644 SHF_ALLOC)
1645 continue;
1646 } else {
1647 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1648 (SHF_ALLOC | SHF_WRITE))
1649 continue;
1651 if (s == interp) {
1652 if (k != 0)
1653 continue;
1654 } else if (s->sh_type == SHT_DYNSYM ||
1655 s->sh_type == SHT_STRTAB ||
1656 s->sh_type == SHT_HASH) {
1657 if (k != 1)
1658 continue;
1659 } else if (s->sh_type == SHT_RELX) {
1660 if (k != 2)
1661 continue;
1662 } else if (s->sh_type == SHT_NOBITS) {
1663 if (k != 4)
1664 continue;
1665 } else {
1666 if (k != 3)
1667 continue;
1669 sec_order[sh_order_index++] = i;
1671 /* section matches: we align it and add its size */
1672 tmp = addr;
1673 addr = (addr + s->sh_addralign - 1) &
1674 ~(s->sh_addralign - 1);
1675 file_offset += (int) ( addr - tmp );
1676 s->sh_offset = file_offset;
1677 s->sh_addr = addr;
1679 /* update program header infos */
1680 if (ph->p_offset == 0) {
1681 ph->p_offset = file_offset;
1682 ph->p_vaddr = addr;
1683 ph->p_paddr = ph->p_vaddr;
1685 /* update dynamic relocation infos */
1686 if (s->sh_type == SHT_RELX) {
1687 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1688 if (!strcmp(strsec->data + s->sh_name, ".rel.got")) {
1689 dyninf->rel_addr = addr;
1690 dyninf->rel_size += s->sh_size; /* XXX only first rel. */
1692 if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) {
1693 dyninf->bss_addr = addr;
1694 dyninf->bss_size = s->sh_size; /* XXX only first rel. */
1696 #else
1697 if (dyninf->rel_size == 0)
1698 dyninf->rel_addr = addr;
1699 dyninf->rel_size += s->sh_size;
1700 #endif
1702 addr += s->sh_size;
1703 if (s->sh_type != SHT_NOBITS)
1704 file_offset += s->sh_size;
1707 if (j == 0) {
1708 /* Make the first PT_LOAD segment include the program
1709 headers itself (and the ELF header as well), it'll
1710 come out with same memory use but will make various
1711 tools like binutils strip work better. */
1712 ph->p_offset &= ~(ph->p_align - 1);
1713 ph->p_vaddr &= ~(ph->p_align - 1);
1714 ph->p_paddr &= ~(ph->p_align - 1);
1716 ph->p_filesz = file_offset - ph->p_offset;
1717 ph->p_memsz = addr - ph->p_vaddr;
1718 ph++;
1719 if (j == 0) {
1720 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1721 /* if in the middle of a page, we duplicate the page in
1722 memory so that one copy is RX and the other is RW */
1723 if ((addr & (s_align - 1)) != 0)
1724 addr += s_align;
1725 } else {
1726 addr = (addr + s_align - 1) & ~(s_align - 1);
1727 file_offset = (file_offset + s_align - 1) & ~(s_align - 1);
1733 /* all other sections come after */
1734 for(i = 1; i < s1->nb_sections; i++) {
1735 s = s1->sections[i];
1736 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1737 continue;
1738 sec_order[sh_order_index++] = i;
1740 file_offset = (file_offset + s->sh_addralign - 1) &
1741 ~(s->sh_addralign - 1);
1742 s->sh_offset = file_offset;
1743 if (s->sh_type != SHT_NOBITS)
1744 file_offset += s->sh_size;
1747 return file_offset;
1750 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
1751 Section *dynamic)
1753 ElfW(Phdr) *ph;
1755 /* if interpreter, then add corresponding program header */
1756 if (interp) {
1757 ph = &phdr[0];
1759 ph->p_type = PT_PHDR;
1760 ph->p_offset = sizeof(ElfW(Ehdr));
1761 ph->p_filesz = ph->p_memsz = phnum * sizeof(ElfW(Phdr));
1762 ph->p_vaddr = interp->sh_addr - ph->p_filesz;
1763 ph->p_paddr = ph->p_vaddr;
1764 ph->p_flags = PF_R | PF_X;
1765 ph->p_align = 4; /* interp->sh_addralign; */
1766 ph++;
1768 ph->p_type = PT_INTERP;
1769 ph->p_offset = interp->sh_offset;
1770 ph->p_vaddr = interp->sh_addr;
1771 ph->p_paddr = ph->p_vaddr;
1772 ph->p_filesz = interp->sh_size;
1773 ph->p_memsz = interp->sh_size;
1774 ph->p_flags = PF_R;
1775 ph->p_align = interp->sh_addralign;
1778 /* if dynamic section, then add corresponding program header */
1779 if (dynamic) {
1780 ph = &phdr[phnum - 1];
1782 ph->p_type = PT_DYNAMIC;
1783 ph->p_offset = dynamic->sh_offset;
1784 ph->p_vaddr = dynamic->sh_addr;
1785 ph->p_paddr = ph->p_vaddr;
1786 ph->p_filesz = dynamic->sh_size;
1787 ph->p_memsz = dynamic->sh_size;
1788 ph->p_flags = PF_R | PF_W;
1789 ph->p_align = dynamic->sh_addralign;
1793 /* Fill the dynamic section with tags describing the address and size of
1794 sections */
1795 static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf)
1797 Section *dynamic = dyninf->dynamic;
1799 /* put dynamic section entries */
1800 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1801 put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr);
1802 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1803 put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset);
1804 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1805 #if PTR_SIZE == 8
1806 put_dt(dynamic, DT_RELA, dyninf->rel_addr);
1807 put_dt(dynamic, DT_RELASZ, dyninf->rel_size);
1808 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1809 #else
1810 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1811 put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
1812 put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size);
1813 put_dt(dynamic, DT_JMPREL, dyninf->rel_addr);
1814 put_dt(dynamic, DT_PLTREL, DT_REL);
1815 put_dt(dynamic, DT_REL, dyninf->bss_addr);
1816 put_dt(dynamic, DT_RELSZ, dyninf->bss_size);
1817 #else
1818 put_dt(dynamic, DT_REL, dyninf->rel_addr);
1819 put_dt(dynamic, DT_RELSZ, dyninf->rel_size);
1820 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1821 #endif
1822 #endif
1823 if (s1->do_debug)
1824 put_dt(dynamic, DT_DEBUG, 0);
1825 put_dt(dynamic, DT_NULL, 0);
1828 /* Relocate remaining sections and symbols (that is those not related to
1829 dynamic linking) */
1830 static int final_sections_reloc(TCCState *s1)
1832 int i;
1833 Section *s;
1835 relocate_syms(s1, s1->symtab, 0);
1837 if (s1->nb_errors != 0)
1838 return -1;
1840 /* relocate sections */
1841 /* XXX: ignore sections with allocated relocations ? */
1842 for(i = 1; i < s1->nb_sections; i++) {
1843 s = s1->sections[i];
1844 if (s->reloc && s != s1->got)
1845 relocate_section(s1, s);
1848 /* relocate relocation entries if the relocation tables are
1849 allocated in the executable */
1850 for(i = 1; i < s1->nb_sections; i++) {
1851 s = s1->sections[i];
1852 if ((s->sh_flags & SHF_ALLOC) &&
1853 s->sh_type == SHT_RELX) {
1854 relocate_rel(s1, s);
1857 return 0;
1860 /* Create an ELF file on disk.
1861 This function handle ELF specific layout requirements */
1862 static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
1863 int file_offset, int *sec_order)
1865 int i, shnum, offset, size, file_type;
1866 Section *s;
1867 ElfW(Ehdr) ehdr;
1868 ElfW(Shdr) shdr, *sh;
1870 file_type = s1->output_type;
1871 shnum = s1->nb_sections;
1873 memset(&ehdr, 0, sizeof(ehdr));
1875 if (phnum > 0) {
1876 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1877 ehdr.e_phnum = phnum;
1878 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1881 /* align to 4 */
1882 file_offset = (file_offset + 3) & -4;
1884 /* fill header */
1885 ehdr.e_ident[0] = ELFMAG0;
1886 ehdr.e_ident[1] = ELFMAG1;
1887 ehdr.e_ident[2] = ELFMAG2;
1888 ehdr.e_ident[3] = ELFMAG3;
1889 ehdr.e_ident[4] = ELFCLASSW;
1890 ehdr.e_ident[5] = ELFDATA2LSB;
1891 ehdr.e_ident[6] = EV_CURRENT;
1892 #if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
1893 /* FIXME: should set only for freebsd _target_, but we exclude only PE target */
1894 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1895 #endif
1896 #ifdef TCC_TARGET_ARM
1897 #ifdef TCC_ARM_EABI
1898 ehdr.e_ident[EI_OSABI] = 0;
1899 ehdr.e_flags = EF_ARM_EABI_VER4;
1900 if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL)
1901 ehdr.e_flags |= EF_ARM_HASENTRY;
1902 if (s1->float_abi == ARM_HARD_FLOAT)
1903 ehdr.e_flags |= EF_ARM_VFP_FLOAT;
1904 else
1905 ehdr.e_flags |= EF_ARM_SOFT_FLOAT;
1906 #else
1907 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1908 #endif
1909 #endif
1910 switch(file_type) {
1911 default:
1912 case TCC_OUTPUT_EXE:
1913 ehdr.e_type = ET_EXEC;
1914 ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1);
1915 break;
1916 case TCC_OUTPUT_DLL:
1917 ehdr.e_type = ET_DYN;
1918 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1919 break;
1920 case TCC_OUTPUT_OBJ:
1921 ehdr.e_type = ET_REL;
1922 break;
1924 ehdr.e_machine = EM_TCC_TARGET;
1925 ehdr.e_version = EV_CURRENT;
1926 ehdr.e_shoff = file_offset;
1927 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1928 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1929 ehdr.e_shnum = shnum;
1930 ehdr.e_shstrndx = shnum - 1;
1932 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1933 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1934 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1936 sort_syms(s1, symtab_section);
1937 for(i = 1; i < s1->nb_sections; i++) {
1938 s = s1->sections[sec_order[i]];
1939 if (s->sh_type != SHT_NOBITS) {
1940 while (offset < s->sh_offset) {
1941 fputc(0, f);
1942 offset++;
1944 size = s->sh_size;
1945 if (size)
1946 fwrite(s->data, 1, size, f);
1947 offset += size;
1951 /* output section headers */
1952 while (offset < ehdr.e_shoff) {
1953 fputc(0, f);
1954 offset++;
1957 for(i = 0; i < s1->nb_sections; i++) {
1958 sh = &shdr;
1959 memset(sh, 0, sizeof(ElfW(Shdr)));
1960 s = s1->sections[i];
1961 if (s) {
1962 sh->sh_name = s->sh_name;
1963 sh->sh_type = s->sh_type;
1964 sh->sh_flags = s->sh_flags;
1965 sh->sh_entsize = s->sh_entsize;
1966 sh->sh_info = s->sh_info;
1967 if (s->link)
1968 sh->sh_link = s->link->sh_num;
1969 sh->sh_addralign = s->sh_addralign;
1970 sh->sh_addr = s->sh_addr;
1971 sh->sh_offset = s->sh_offset;
1972 sh->sh_size = s->sh_size;
1974 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1978 /* Write an elf, coff or "binary" file */
1979 static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum,
1980 ElfW(Phdr) *phdr, int file_offset, int *sec_order)
1982 int fd, mode, file_type;
1983 FILE *f;
1985 file_type = s1->output_type;
1986 if (file_type == TCC_OUTPUT_OBJ)
1987 mode = 0666;
1988 else
1989 mode = 0777;
1990 unlink(filename);
1991 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1992 if (fd < 0) {
1993 tcc_error_noabort("could not write '%s'", filename);
1994 return -1;
1996 f = fdopen(fd, "wb");
1997 if (s1->verbose)
1998 printf("<- %s\n", filename);
2000 #ifdef TCC_TARGET_COFF
2001 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF)
2002 tcc_output_coff(s1, f);
2003 else
2004 #endif
2005 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF)
2006 tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order);
2007 else
2008 tcc_output_binary(s1, f, sec_order);
2009 fclose(f);
2011 return 0;
2014 /* Sort section headers by assigned sh_addr, remove sections
2015 that we aren't going to output. */
2016 static void tidy_section_headers(TCCState *s1, int *sec_order)
2018 int i, nnew, l, *backmap;
2019 Section **snew, *s;
2020 ElfW(Sym) *sym;
2022 snew = tcc_malloc(s1->nb_sections * sizeof(snew[0]));
2023 backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0]));
2024 for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) {
2025 s = s1->sections[sec_order[i]];
2026 if (!i || s->sh_name) {
2027 backmap[sec_order[i]] = nnew;
2028 snew[nnew] = s;
2029 ++nnew;
2030 } else {
2031 backmap[sec_order[i]] = 0;
2032 snew[--l] = s;
2035 for (i = 0; i < nnew; i++) {
2036 s = snew[i];
2037 if (s) {
2038 s->sh_num = i;
2039 if (s->sh_type == SHT_RELX)
2040 s->sh_info = backmap[s->sh_info];
2044 for_each_elem(symtab_section, 1, sym, ElfW(Sym))
2045 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
2046 sym->st_shndx = backmap[sym->st_shndx];
2047 if( !s1->static_link ) {
2048 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym))
2049 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE)
2050 sym->st_shndx = backmap[sym->st_shndx];
2052 for (i = 0; i < s1->nb_sections; i++)
2053 sec_order[i] = i;
2054 tcc_free(s1->sections);
2055 s1->sections = snew;
2056 s1->nb_sections = nnew;
2057 tcc_free(backmap);
2060 /* Output an elf, coff or binary file */
2061 /* XXX: suppress unneeded sections */
2062 static int elf_output_file(TCCState *s1, const char *filename)
2064 int i, ret, phnum, shnum, file_type, file_offset, *sec_order;
2065 struct dyn_inf dyninf = {0};
2066 ElfW(Phdr) *phdr;
2067 ElfW(Sym) *sym;
2068 Section *strsec, *interp, *dynamic, *dynstr;
2069 int textrel;
2071 file_type = s1->output_type;
2072 s1->nb_errors = 0;
2073 ret = -1;
2074 phdr = NULL;
2075 sec_order = NULL;
2076 interp = dynamic = dynstr = NULL; /* avoid warning */
2077 textrel = 0;
2079 if (file_type != TCC_OUTPUT_OBJ) {
2080 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
2081 tcc_add_runtime(s1);
2082 resolve_common_syms(s1);
2084 if (!s1->static_link) {
2085 if (file_type == TCC_OUTPUT_EXE) {
2086 char *ptr;
2087 /* allow override the dynamic loader */
2088 const char *elfint = getenv("LD_SO");
2089 if (elfint == NULL)
2090 elfint = DEFAULT_ELFINTERP(s1);
2091 /* add interpreter section only if executable */
2092 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
2093 interp->sh_addralign = 1;
2094 ptr = section_ptr_add(interp, 1 + strlen(elfint));
2095 strcpy(ptr, elfint);
2098 /* add dynamic symbol table */
2099 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
2100 ".dynstr",
2101 ".hash", SHF_ALLOC);
2102 dynstr = s1->dynsym->link;
2104 /* add dynamic section */
2105 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
2106 SHF_ALLOC | SHF_WRITE);
2107 dynamic->link = dynstr;
2108 dynamic->sh_entsize = sizeof(ElfW(Dyn));
2110 build_got(s1);
2112 if (file_type == TCC_OUTPUT_EXE) {
2113 bind_exe_dynsyms(s1);
2114 if (s1->nb_errors)
2115 goto the_end;
2116 bind_libs_dynsyms(s1);
2117 } else {
2118 /* shared library case: simply export all global symbols */
2119 export_global_syms(s1);
2122 build_got_entries(s1);
2125 /* we add a section for symbols */
2126 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
2127 put_elf_str(strsec, "");
2129 /* Allocate strings for section names */
2130 textrel = alloc_sec_names(s1, file_type, strsec);
2132 if (dynamic) {
2133 /* add a list of needed dlls */
2134 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2135 DLLReference *dllref = s1->loaded_dlls[i];
2136 if (dllref->level == 0)
2137 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
2140 if (s1->rpath)
2141 put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
2142 put_elf_str(dynstr, s1->rpath));
2144 if (file_type == TCC_OUTPUT_DLL) {
2145 if (s1->soname)
2146 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
2147 /* XXX: currently, since we do not handle PIC code, we
2148 must relocate the readonly segments */
2149 if (textrel)
2150 put_dt(dynamic, DT_TEXTREL, 0);
2153 if (s1->symbolic)
2154 put_dt(dynamic, DT_SYMBOLIC, 0);
2156 dyninf.dynamic = dynamic;
2157 dyninf.dynstr = dynstr;
2158 /* remember offset and reserve space for 2nd call below */
2159 dyninf.data_offset = dynamic->data_offset;
2160 fill_dynamic(s1, &dyninf);
2161 dynamic->sh_size = dynamic->data_offset;
2162 dynstr->sh_size = dynstr->data_offset;
2165 /* compute number of program headers */
2166 if (file_type == TCC_OUTPUT_OBJ)
2167 phnum = 0;
2168 else if (file_type == TCC_OUTPUT_DLL)
2169 phnum = 3;
2170 else if (s1->static_link)
2171 phnum = 2;
2172 else
2173 phnum = 5;
2175 /* allocate program segment headers */
2176 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
2178 /* compute number of sections */
2179 shnum = s1->nb_sections;
2181 /* this array is used to reorder sections in the output file */
2182 sec_order = tcc_malloc(sizeof(int) * shnum);
2183 sec_order[0] = 0;
2185 /* compute section to program header mapping */
2186 file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2187 sec_order);
2189 /* Fill remaining program header and finalize relocation related to dynamic
2190 linking. */
2191 if (file_type != TCC_OUTPUT_OBJ) {
2192 fill_unloadable_phdr(phdr, phnum, interp, dynamic);
2193 if (dynamic) {
2194 dynamic->data_offset = dyninf.data_offset;
2195 fill_dynamic(s1, &dyninf);
2197 /* put in GOT the dynamic section address and relocate PLT */
2198 write32le(s1->got->data, dynamic->sh_addr);
2199 if (file_type == TCC_OUTPUT_EXE
2200 || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL))
2201 relocate_plt(s1);
2203 /* relocate symbols in .dynsym now that final addresses are known */
2204 for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) {
2205 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) {
2206 /* do symbol relocation */
2207 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
2212 /* if building executable or DLL, then relocate each section
2213 except the GOT which is already relocated */
2214 ret = final_sections_reloc(s1);
2215 if (ret)
2216 goto the_end;
2217 tidy_section_headers(s1, sec_order);
2219 /* Perform relocation to GOT or PLT entries */
2220 if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2221 fill_got(s1);
2222 else if (s1->got)
2223 fill_local_got_entries(s1);
2226 /* Create the ELF file with name 'filename' */
2227 ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order);
2228 s1->nb_sections = shnum;
2229 the_end:
2230 tcc_free(sec_order);
2231 tcc_free(phdr);
2232 return ret;
2235 LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename)
2237 int ret;
2238 #ifdef TCC_TARGET_PE
2239 if (s->output_type != TCC_OUTPUT_OBJ) {
2240 ret = pe_output_file(s, filename);
2241 } else
2242 #endif
2243 ret = elf_output_file(s, filename);
2244 return ret;
2247 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2249 void *data;
2251 data = tcc_malloc(size);
2252 lseek(fd, file_offset, SEEK_SET);
2253 read(fd, data, size);
2254 return data;
2257 typedef struct SectionMergeInfo {
2258 Section *s; /* corresponding existing section */
2259 unsigned long offset; /* offset of the new section in the existing section */
2260 uint8_t new_section; /* true if section 's' was added */
2261 uint8_t link_once; /* true if link once section */
2262 } SectionMergeInfo;
2264 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h)
2266 int size = read(fd, h, sizeof *h);
2267 if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
2268 if (h->e_type == ET_REL)
2269 return AFF_BINTYPE_REL;
2270 if (h->e_type == ET_DYN)
2271 return AFF_BINTYPE_DYN;
2272 } else if (size >= 8) {
2273 if (0 == memcmp(h, ARMAG, 8))
2274 return AFF_BINTYPE_AR;
2275 #ifdef TCC_TARGET_COFF
2276 if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC)
2277 return AFF_BINTYPE_C67;
2278 #endif
2280 return 0;
2283 /* load an object file and merge it with current files */
2284 /* XXX: handle correctly stab (debug) info */
2285 ST_FUNC int tcc_load_object_file(TCCState *s1,
2286 int fd, unsigned long file_offset)
2288 ElfW(Ehdr) ehdr;
2289 ElfW(Shdr) *shdr, *sh;
2290 int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed;
2291 unsigned char *strsec, *strtab;
2292 int *old_to_new_syms;
2293 char *sh_name, *name;
2294 SectionMergeInfo *sm_table, *sm;
2295 ElfW(Sym) *sym, *symtab;
2296 ElfW_Rel *rel;
2297 Section *s;
2299 int stab_index;
2300 int stabstr_index;
2302 stab_index = stabstr_index = 0;
2304 lseek(fd, file_offset, SEEK_SET);
2305 if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL)
2306 goto fail1;
2307 /* test CPU specific stuff */
2308 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2309 ehdr.e_machine != EM_TCC_TARGET) {
2310 fail1:
2311 tcc_error_noabort("invalid object file");
2312 return -1;
2314 /* read sections */
2315 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2316 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2317 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2319 /* load section names */
2320 sh = &shdr[ehdr.e_shstrndx];
2321 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2323 /* load symtab and strtab */
2324 old_to_new_syms = NULL;
2325 symtab = NULL;
2326 strtab = NULL;
2327 nb_syms = 0;
2328 seencompressed = 0;
2329 for(i = 1; i < ehdr.e_shnum; i++) {
2330 sh = &shdr[i];
2331 if (sh->sh_type == SHT_SYMTAB) {
2332 if (symtab) {
2333 tcc_error_noabort("object must contain only one symtab");
2334 fail:
2335 ret = -1;
2336 goto the_end;
2338 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2339 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2340 sm_table[i].s = symtab_section;
2342 /* now load strtab */
2343 sh = &shdr[sh->sh_link];
2344 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2346 if (sh->sh_flags & SHF_COMPRESSED)
2347 seencompressed = 1;
2350 /* now examine each section and try to merge its content with the
2351 ones in memory */
2352 for(i = 1; i < ehdr.e_shnum; i++) {
2353 /* no need to examine section name strtab */
2354 if (i == ehdr.e_shstrndx)
2355 continue;
2356 sh = &shdr[i];
2357 sh_name = (char *) strsec + sh->sh_name;
2358 /* ignore sections types we do not handle */
2359 if (sh->sh_type != SHT_PROGBITS &&
2360 sh->sh_type != SHT_RELX &&
2361 #ifdef TCC_ARM_EABI
2362 sh->sh_type != SHT_ARM_EXIDX &&
2363 #endif
2364 sh->sh_type != SHT_NOBITS &&
2365 sh->sh_type != SHT_PREINIT_ARRAY &&
2366 sh->sh_type != SHT_INIT_ARRAY &&
2367 sh->sh_type != SHT_FINI_ARRAY &&
2368 strcmp(sh_name, ".stabstr")
2370 continue;
2371 if (seencompressed
2372 && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1)
2373 || (sh->sh_type == SHT_RELX
2374 && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name,
2375 ".debug_", sizeof(".debug_")-1))))
2376 continue;
2377 if (sh->sh_addralign < 1)
2378 sh->sh_addralign = 1;
2379 /* find corresponding section, if any */
2380 for(j = 1; j < s1->nb_sections;j++) {
2381 s = s1->sections[j];
2382 if (!strcmp(s->name, sh_name)) {
2383 if (!strncmp(sh_name, ".gnu.linkonce",
2384 sizeof(".gnu.linkonce") - 1)) {
2385 /* if a 'linkonce' section is already present, we
2386 do not add it again. It is a little tricky as
2387 symbols can still be defined in
2388 it. */
2389 sm_table[i].link_once = 1;
2390 goto next;
2391 } else {
2392 goto found;
2396 /* not found: create new section */
2397 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
2398 /* take as much info as possible from the section. sh_link and
2399 sh_info will be updated later */
2400 s->sh_addralign = sh->sh_addralign;
2401 s->sh_entsize = sh->sh_entsize;
2402 sm_table[i].new_section = 1;
2403 found:
2404 if (sh->sh_type != s->sh_type) {
2405 tcc_error_noabort("invalid section type");
2406 goto fail;
2409 /* align start of section */
2410 offset = s->data_offset;
2412 if (0 == strcmp(sh_name, ".stab")) {
2413 stab_index = i;
2414 goto no_align;
2416 if (0 == strcmp(sh_name, ".stabstr")) {
2417 stabstr_index = i;
2418 goto no_align;
2421 size = sh->sh_addralign - 1;
2422 offset = (offset + size) & ~size;
2423 if (sh->sh_addralign > s->sh_addralign)
2424 s->sh_addralign = sh->sh_addralign;
2425 s->data_offset = offset;
2426 no_align:
2427 sm_table[i].offset = offset;
2428 sm_table[i].s = s;
2429 /* concatenate sections */
2430 size = sh->sh_size;
2431 if (sh->sh_type != SHT_NOBITS) {
2432 unsigned char *ptr;
2433 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2434 ptr = section_ptr_add(s, size);
2435 read(fd, ptr, size);
2436 } else {
2437 s->data_offset += size;
2439 next: ;
2442 /* gr relocate stab strings */
2443 if (stab_index && stabstr_index) {
2444 Stab_Sym *a, *b;
2445 unsigned o;
2446 s = sm_table[stab_index].s;
2447 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2448 b = (Stab_Sym *)(s->data + s->data_offset);
2449 o = sm_table[stabstr_index].offset;
2450 while (a < b) {
2451 if (a->n_strx)
2452 a->n_strx += o;
2453 a++;
2457 /* second short pass to update sh_link and sh_info fields of new
2458 sections */
2459 for(i = 1; i < ehdr.e_shnum; i++) {
2460 s = sm_table[i].s;
2461 if (!s || !sm_table[i].new_section)
2462 continue;
2463 sh = &shdr[i];
2464 if (sh->sh_link > 0)
2465 s->link = sm_table[sh->sh_link].s;
2466 if (sh->sh_type == SHT_RELX) {
2467 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2468 /* update backward link */
2469 s1->sections[s->sh_info]->reloc = s;
2472 sm = sm_table;
2474 /* resolve symbols */
2475 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2477 sym = symtab + 1;
2478 for(i = 1; i < nb_syms; i++, sym++) {
2479 if (sym->st_shndx != SHN_UNDEF &&
2480 sym->st_shndx < SHN_LORESERVE) {
2481 sm = &sm_table[sym->st_shndx];
2482 if (sm->link_once) {
2483 /* if a symbol is in a link once section, we use the
2484 already defined symbol. It is very important to get
2485 correct relocations */
2486 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2487 name = (char *) strtab + sym->st_name;
2488 sym_index = find_elf_sym(symtab_section, name);
2489 if (sym_index)
2490 old_to_new_syms[i] = sym_index;
2492 continue;
2494 /* if no corresponding section added, no need to add symbol */
2495 if (!sm->s)
2496 continue;
2497 /* convert section number */
2498 sym->st_shndx = sm->s->sh_num;
2499 /* offset value */
2500 sym->st_value += sm->offset;
2502 /* add symbol */
2503 name = (char *) strtab + sym->st_name;
2504 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
2505 sym->st_info, sym->st_other,
2506 sym->st_shndx, name);
2507 old_to_new_syms[i] = sym_index;
2510 /* third pass to patch relocation entries */
2511 for(i = 1; i < ehdr.e_shnum; i++) {
2512 s = sm_table[i].s;
2513 if (!s)
2514 continue;
2515 sh = &shdr[i];
2516 offset = sm_table[i].offset;
2517 switch(s->sh_type) {
2518 case SHT_RELX:
2519 /* take relocation offset information */
2520 offseti = sm_table[sh->sh_info].offset;
2521 for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) {
2522 int type;
2523 unsigned sym_index;
2524 /* convert symbol index */
2525 type = ELFW(R_TYPE)(rel->r_info);
2526 sym_index = ELFW(R_SYM)(rel->r_info);
2527 /* NOTE: only one symtab assumed */
2528 if (sym_index >= nb_syms)
2529 goto invalid_reloc;
2530 sym_index = old_to_new_syms[sym_index];
2531 /* ignore link_once in rel section. */
2532 if (!sym_index && !sm->link_once
2533 #ifdef TCC_TARGET_ARM
2534 && type != R_ARM_V4BX
2535 #endif
2537 invalid_reloc:
2538 tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2539 i, strsec + sh->sh_name, rel->r_offset);
2540 goto fail;
2542 rel->r_info = ELFW(R_INFO)(sym_index, type);
2543 /* offset the relocation offset */
2544 rel->r_offset += offseti;
2545 #ifdef TCC_TARGET_ARM
2546 /* Jumps and branches from a Thumb code to a PLT entry need
2547 special handling since PLT entries are ARM code.
2548 Unconditional bl instructions referencing PLT entries are
2549 handled by converting these instructions into blx
2550 instructions. Other case of instructions referencing a PLT
2551 entry require to add a Thumb stub before the PLT entry to
2552 switch to ARM mode. We set bit plt_thumb_stub of the
2553 attribute of a symbol to indicate such a case. */
2554 if (type == R_ARM_THM_JUMP24)
2555 get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1;
2556 #endif
2558 break;
2559 default:
2560 break;
2564 ret = 0;
2565 the_end:
2566 tcc_free(symtab);
2567 tcc_free(strtab);
2568 tcc_free(old_to_new_syms);
2569 tcc_free(sm_table);
2570 tcc_free(strsec);
2571 tcc_free(shdr);
2572 return ret;
2575 typedef struct ArchiveHeader {
2576 char ar_name[16]; /* name of this member */
2577 char ar_date[12]; /* file mtime */
2578 char ar_uid[6]; /* owner uid; printed as decimal */
2579 char ar_gid[6]; /* owner gid; printed as decimal */
2580 char ar_mode[8]; /* file mode, printed as octal */
2581 char ar_size[10]; /* file size, printed as decimal */
2582 char ar_fmag[2]; /* should contain ARFMAG */
2583 } ArchiveHeader;
2585 static int get_be32(const uint8_t *b)
2587 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2590 static long get_be64(const uint8_t *b)
2592 long long ret = get_be32(b);
2593 ret = (ret << 32) | (unsigned)get_be32(b+4);
2594 return (long)ret;
2597 /* load only the objects which resolve undefined symbols */
2598 static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize)
2600 long i, bound, nsyms, sym_index, off, ret;
2601 uint8_t *data;
2602 const char *ar_names, *p;
2603 const uint8_t *ar_index;
2604 ElfW(Sym) *sym;
2606 data = tcc_malloc(size);
2607 if (read(fd, data, size) != size)
2608 goto fail;
2609 nsyms = entrysize == 4 ? get_be32(data) : get_be64(data);
2610 ar_index = data + entrysize;
2611 ar_names = (char *) ar_index + nsyms * entrysize;
2613 do {
2614 bound = 0;
2615 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2616 sym_index = find_elf_sym(symtab_section, p);
2617 if(sym_index) {
2618 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2619 if(sym->st_shndx == SHN_UNDEF) {
2620 off = (entrysize == 4
2621 ? get_be32(ar_index + i * 4)
2622 : get_be64(ar_index + i * 8))
2623 + sizeof(ArchiveHeader);
2624 ++bound;
2625 if(tcc_load_object_file(s1, fd, off) < 0) {
2626 fail:
2627 ret = -1;
2628 goto the_end;
2633 } while(bound);
2634 ret = 0;
2635 the_end:
2636 tcc_free(data);
2637 return ret;
2640 /* load a '.a' file */
2641 ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte)
2643 ArchiveHeader hdr;
2644 char ar_size[11];
2645 char ar_name[17];
2646 char magic[8];
2647 int size, len, i;
2648 unsigned long file_offset;
2650 /* skip magic which was already checked */
2651 read(fd, magic, sizeof(magic));
2653 for(;;) {
2654 len = read(fd, &hdr, sizeof(hdr));
2655 if (len == 0)
2656 break;
2657 if (len != sizeof(hdr)) {
2658 tcc_error_noabort("invalid archive");
2659 return -1;
2661 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2662 ar_size[sizeof(hdr.ar_size)] = '\0';
2663 size = strtol(ar_size, NULL, 0);
2664 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2665 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2666 if (ar_name[i] != ' ')
2667 break;
2669 ar_name[i + 1] = '\0';
2670 file_offset = lseek(fd, 0, SEEK_CUR);
2671 /* align to even */
2672 size = (size + 1) & ~1;
2673 if (!strcmp(ar_name, "/")) {
2674 /* coff symbol table : we handle it */
2675 if (alacarte)
2676 return tcc_load_alacarte(s1, fd, size, 4);
2677 } else if (!strcmp(ar_name, "/SYM64/")) {
2678 if (alacarte)
2679 return tcc_load_alacarte(s1, fd, size, 8);
2680 } else {
2681 ElfW(Ehdr) ehdr;
2682 if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) {
2683 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2684 return -1;
2687 lseek(fd, file_offset + size, SEEK_SET);
2689 return 0;
2692 #ifndef TCC_TARGET_PE
2693 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2694 is referenced by the user (so it should be added as DT_NEEDED in
2695 the generated ELF file) */
2696 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2698 ElfW(Ehdr) ehdr;
2699 ElfW(Shdr) *shdr, *sh, *sh1;
2700 int i, j, nb_syms, nb_dts, sym_bind, ret;
2701 ElfW(Sym) *sym, *dynsym;
2702 ElfW(Dyn) *dt, *dynamic;
2703 unsigned char *dynstr;
2704 const char *name, *soname;
2705 DLLReference *dllref;
2707 read(fd, &ehdr, sizeof(ehdr));
2709 /* test CPU specific stuff */
2710 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2711 ehdr.e_machine != EM_TCC_TARGET) {
2712 tcc_error_noabort("bad architecture");
2713 return -1;
2716 /* read sections */
2717 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2719 /* load dynamic section and dynamic symbols */
2720 nb_syms = 0;
2721 nb_dts = 0;
2722 dynamic = NULL;
2723 dynsym = NULL; /* avoid warning */
2724 dynstr = NULL; /* avoid warning */
2725 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2726 switch(sh->sh_type) {
2727 case SHT_DYNAMIC:
2728 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2729 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2730 break;
2731 case SHT_DYNSYM:
2732 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2733 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2734 sh1 = &shdr[sh->sh_link];
2735 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2736 break;
2737 default:
2738 break;
2742 /* compute the real library name */
2743 soname = tcc_basename(filename);
2745 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2746 if (dt->d_tag == DT_SONAME) {
2747 soname = (char *) dynstr + dt->d_un.d_val;
2751 /* if the dll is already loaded, do not load it */
2752 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2753 dllref = s1->loaded_dlls[i];
2754 if (!strcmp(soname, dllref->name)) {
2755 /* but update level if needed */
2756 if (level < dllref->level)
2757 dllref->level = level;
2758 ret = 0;
2759 goto the_end;
2763 /* add the dll and its level */
2764 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2765 dllref->level = level;
2766 strcpy(dllref->name, soname);
2767 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2769 /* add dynamic symbols in dynsym_section */
2770 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2771 sym_bind = ELFW(ST_BIND)(sym->st_info);
2772 if (sym_bind == STB_LOCAL)
2773 continue;
2774 name = (char *) dynstr + sym->st_name;
2775 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2776 sym->st_info, sym->st_other, sym->st_shndx, name);
2779 /* load all referenced DLLs */
2780 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2781 switch(dt->d_tag) {
2782 case DT_NEEDED:
2783 name = (char *) dynstr + dt->d_un.d_val;
2784 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2785 dllref = s1->loaded_dlls[j];
2786 if (!strcmp(name, dllref->name))
2787 goto already_loaded;
2789 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2790 tcc_error_noabort("referenced dll '%s' not found", name);
2791 ret = -1;
2792 goto the_end;
2794 already_loaded:
2795 break;
2798 ret = 0;
2799 the_end:
2800 tcc_free(dynstr);
2801 tcc_free(dynsym);
2802 tcc_free(dynamic);
2803 tcc_free(shdr);
2804 return ret;
2807 #define LD_TOK_NAME 256
2808 #define LD_TOK_EOF (-1)
2810 /* return next ld script token */
2811 static int ld_next(TCCState *s1, char *name, int name_size)
2813 int c;
2814 char *q;
2816 redo:
2817 switch(ch) {
2818 case ' ':
2819 case '\t':
2820 case '\f':
2821 case '\v':
2822 case '\r':
2823 case '\n':
2824 inp();
2825 goto redo;
2826 case '/':
2827 minp();
2828 if (ch == '*') {
2829 file->buf_ptr = parse_comment(file->buf_ptr);
2830 ch = file->buf_ptr[0];
2831 goto redo;
2832 } else {
2833 q = name;
2834 *q++ = '/';
2835 goto parse_name;
2837 break;
2838 case '\\':
2839 ch = handle_eob();
2840 if (ch != '\\')
2841 goto redo;
2842 /* fall through */
2843 /* case 'a' ... 'z': */
2844 case 'a':
2845 case 'b':
2846 case 'c':
2847 case 'd':
2848 case 'e':
2849 case 'f':
2850 case 'g':
2851 case 'h':
2852 case 'i':
2853 case 'j':
2854 case 'k':
2855 case 'l':
2856 case 'm':
2857 case 'n':
2858 case 'o':
2859 case 'p':
2860 case 'q':
2861 case 'r':
2862 case 's':
2863 case 't':
2864 case 'u':
2865 case 'v':
2866 case 'w':
2867 case 'x':
2868 case 'y':
2869 case 'z':
2870 /* case 'A' ... 'z': */
2871 case 'A':
2872 case 'B':
2873 case 'C':
2874 case 'D':
2875 case 'E':
2876 case 'F':
2877 case 'G':
2878 case 'H':
2879 case 'I':
2880 case 'J':
2881 case 'K':
2882 case 'L':
2883 case 'M':
2884 case 'N':
2885 case 'O':
2886 case 'P':
2887 case 'Q':
2888 case 'R':
2889 case 'S':
2890 case 'T':
2891 case 'U':
2892 case 'V':
2893 case 'W':
2894 case 'X':
2895 case 'Y':
2896 case 'Z':
2897 case '_':
2898 case '.':
2899 case '$':
2900 case '~':
2901 q = name;
2902 parse_name:
2903 for(;;) {
2904 if (!((ch >= 'a' && ch <= 'z') ||
2905 (ch >= 'A' && ch <= 'Z') ||
2906 (ch >= '0' && ch <= '9') ||
2907 strchr("/.-_+=$:\\,~", ch)))
2908 break;
2909 if ((q - name) < name_size - 1) {
2910 *q++ = ch;
2912 minp();
2914 *q = '\0';
2915 c = LD_TOK_NAME;
2916 break;
2917 case CH_EOF:
2918 c = LD_TOK_EOF;
2919 break;
2920 default:
2921 c = ch;
2922 inp();
2923 break;
2925 return c;
2928 static int ld_add_file(TCCState *s1, const char filename[])
2930 if (filename[0] == '/') {
2931 if (CONFIG_SYSROOT[0] == '\0'
2932 && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0)
2933 return 0;
2934 filename = tcc_basename(filename);
2936 return tcc_add_dll(s1, filename, 0);
2939 static inline int new_undef_syms(void)
2941 int ret = 0;
2942 ret = new_undef_sym;
2943 new_undef_sym = 0;
2944 return ret;
2947 static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
2949 char filename[1024], libname[1024];
2950 int t, group, nblibs = 0, ret = 0;
2951 char **libs = NULL;
2953 group = !strcmp(cmd, "GROUP");
2954 if (!as_needed)
2955 new_undef_syms();
2956 t = ld_next(s1, filename, sizeof(filename));
2957 if (t != '(')
2958 expect("(");
2959 t = ld_next(s1, filename, sizeof(filename));
2960 for(;;) {
2961 libname[0] = '\0';
2962 if (t == LD_TOK_EOF) {
2963 tcc_error_noabort("unexpected end of file");
2964 ret = -1;
2965 goto lib_parse_error;
2966 } else if (t == ')') {
2967 break;
2968 } else if (t == '-') {
2969 t = ld_next(s1, filename, sizeof(filename));
2970 if ((t != LD_TOK_NAME) || (filename[0] != 'l')) {
2971 tcc_error_noabort("library name expected");
2972 ret = -1;
2973 goto lib_parse_error;
2975 pstrcpy(libname, sizeof libname, &filename[1]);
2976 if (s1->static_link) {
2977 snprintf(filename, sizeof filename, "lib%s.a", libname);
2978 } else {
2979 snprintf(filename, sizeof filename, "lib%s.so", libname);
2981 } else if (t != LD_TOK_NAME) {
2982 tcc_error_noabort("filename expected");
2983 ret = -1;
2984 goto lib_parse_error;
2986 if (!strcmp(filename, "AS_NEEDED")) {
2987 ret = ld_add_file_list(s1, cmd, 1);
2988 if (ret)
2989 goto lib_parse_error;
2990 } else {
2991 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2992 if (!as_needed) {
2993 ret = ld_add_file(s1, filename);
2994 if (ret)
2995 goto lib_parse_error;
2996 if (group) {
2997 /* Add the filename *and* the libname to avoid future conversions */
2998 dynarray_add(&libs, &nblibs, tcc_strdup(filename));
2999 if (libname[0] != '\0')
3000 dynarray_add(&libs, &nblibs, tcc_strdup(libname));
3004 t = ld_next(s1, filename, sizeof(filename));
3005 if (t == ',') {
3006 t = ld_next(s1, filename, sizeof(filename));
3009 if (group && !as_needed) {
3010 while (new_undef_syms()) {
3011 int i;
3013 for (i = 0; i < nblibs; i ++)
3014 ld_add_file(s1, libs[i]);
3017 lib_parse_error:
3018 dynarray_reset(&libs, &nblibs);
3019 return ret;
3022 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
3023 files */
3024 ST_FUNC int tcc_load_ldscript(TCCState *s1)
3026 char cmd[64];
3027 char filename[1024];
3028 int t, ret;
3030 ch = handle_eob();
3031 for(;;) {
3032 t = ld_next(s1, cmd, sizeof(cmd));
3033 if (t == LD_TOK_EOF)
3034 return 0;
3035 else if (t != LD_TOK_NAME)
3036 return -1;
3037 if (!strcmp(cmd, "INPUT") ||
3038 !strcmp(cmd, "GROUP")) {
3039 ret = ld_add_file_list(s1, cmd, 0);
3040 if (ret)
3041 return ret;
3042 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
3043 !strcmp(cmd, "TARGET")) {
3044 /* ignore some commands */
3045 t = ld_next(s1, cmd, sizeof(cmd));
3046 if (t != '(')
3047 expect("(");
3048 for(;;) {
3049 t = ld_next(s1, filename, sizeof(filename));
3050 if (t == LD_TOK_EOF) {
3051 tcc_error_noabort("unexpected end of file");
3052 return -1;
3053 } else if (t == ')') {
3054 break;
3057 } else {
3058 return -1;
3061 return 0;
3063 #endif /* !TCC_TARGET_PE */