tccpe: support pe32+ (x86_64) target
[tinycc/kirr.git] / tccelf.c
blobf0c12c3059b0984d69fe33f2d39001c8276902f8
1 /*
2 * ELF file handling for TCC
3 *
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 #ifdef TCC_TARGET_X86_64
22 #define ElfW_Rel ElfW(Rela)
23 #define SHT_RELX SHT_RELA
24 #define REL_SECTION_FMT ".rela%s"
25 /* x86-64 requires PLT for DLLs */
26 #define TCC_OUTPUT_DLL_WITH_PLT
27 #else
28 #define ElfW_Rel ElfW(Rel)
29 #define SHT_RELX SHT_REL
30 #define REL_SECTION_FMT ".rel%s"
31 #endif
33 /* XXX: DLL with PLT would only work with x86-64 for now */
34 //#define TCC_OUTPUT_DLL_WITH_PLT
36 static int put_elf_str(Section *s, const char *sym)
38 int offset, len;
39 char *ptr;
41 len = strlen(sym) + 1;
42 offset = s->data_offset;
43 ptr = section_ptr_add(s, len);
44 memcpy(ptr, sym, len);
45 return offset;
48 /* elf symbol hashing function */
49 static unsigned long elf_hash(const unsigned char *name)
51 unsigned long h = 0, g;
53 while (*name) {
54 h = (h << 4) + *name++;
55 g = h & 0xf0000000;
56 if (g)
57 h ^= g >> 24;
58 h &= ~g;
60 return h;
63 /* rebuild hash table of section s */
64 /* NOTE: we do factorize the hash table code to go faster */
65 static void rebuild_hash(Section *s, unsigned int nb_buckets)
67 ElfW(Sym) *sym;
68 int *ptr, *hash, nb_syms, sym_index, h;
69 char *strtab;
71 strtab = s->link->data;
72 nb_syms = s->data_offset / sizeof(ElfW(Sym));
74 s->hash->data_offset = 0;
75 ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
76 ptr[0] = nb_buckets;
77 ptr[1] = nb_syms;
78 ptr += 2;
79 hash = ptr;
80 memset(hash, 0, (nb_buckets + 1) * sizeof(int));
81 ptr += nb_buckets + 1;
83 sym = (ElfW(Sym) *)s->data + 1;
84 for(sym_index = 1; sym_index < nb_syms; sym_index++) {
85 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
86 h = elf_hash(strtab + sym->st_name) % nb_buckets;
87 *ptr = hash[h];
88 hash[h] = sym_index;
89 } else {
90 *ptr = 0;
92 ptr++;
93 sym++;
97 /* return the symbol number */
98 static int put_elf_sym(Section *s,
99 unsigned long value, unsigned long size,
100 int info, int other, int shndx, const char *name)
102 int name_offset, sym_index;
103 int nbuckets, h;
104 ElfW(Sym) *sym;
105 Section *hs;
107 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
108 if (name)
109 name_offset = put_elf_str(s->link, name);
110 else
111 name_offset = 0;
112 /* XXX: endianness */
113 sym->st_name = name_offset;
114 sym->st_value = value;
115 sym->st_size = size;
116 sym->st_info = info;
117 sym->st_other = other;
118 sym->st_shndx = shndx;
119 sym_index = sym - (ElfW(Sym) *)s->data;
120 hs = s->hash;
121 if (hs) {
122 int *ptr, *base;
123 ptr = section_ptr_add(hs, sizeof(int));
124 base = (int *)hs->data;
125 /* only add global or weak symbols */
126 if (ELFW(ST_BIND)(info) != STB_LOCAL) {
127 /* add another hashing entry */
128 nbuckets = base[0];
129 h = elf_hash(name) % nbuckets;
130 *ptr = base[2 + h];
131 base[2 + h] = sym_index;
132 base[1]++;
133 /* we resize the hash table */
134 hs->nb_hashed_syms++;
135 if (hs->nb_hashed_syms > 2 * nbuckets) {
136 rebuild_hash(s, 2 * nbuckets);
138 } else {
139 *ptr = 0;
140 base[1]++;
143 return sym_index;
146 /* find global ELF symbol 'name' and return its index. Return 0 if not
147 found. */
148 static int find_elf_sym(Section *s, const char *name)
150 ElfW(Sym) *sym;
151 Section *hs;
152 int nbuckets, sym_index, h;
153 const char *name1;
155 hs = s->hash;
156 if (!hs)
157 return 0;
158 nbuckets = ((int *)hs->data)[0];
159 h = elf_hash(name) % nbuckets;
160 sym_index = ((int *)hs->data)[2 + h];
161 while (sym_index != 0) {
162 sym = &((ElfW(Sym) *)s->data)[sym_index];
163 name1 = s->link->data + sym->st_name;
164 if (!strcmp(name, name1))
165 return sym_index;
166 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
168 return 0;
171 /* return elf symbol value or error */
172 void *tcc_get_symbol(TCCState *s, const char *name)
174 int sym_index;
175 ElfW(Sym) *sym;
176 sym_index = find_elf_sym(symtab_section, name);
177 if (!sym_index)
178 return NULL;
179 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
180 return (void*)(long)sym->st_value;
183 void *tcc_get_symbol_err(TCCState *s, const char *name)
185 void *sym;
186 sym = tcc_get_symbol(s, name);
187 if (!sym)
188 error("%s not defined", name);
189 return sym;
192 /* add an elf symbol : check if it is already defined and patch
193 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
194 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
195 int info, int other, int sh_num, const char *name)
197 ElfW(Sym) *esym;
198 int sym_bind, sym_index, sym_type, esym_bind;
199 unsigned char sym_vis, esym_vis, new_vis;
201 sym_bind = ELFW(ST_BIND)(info);
202 sym_type = ELFW(ST_TYPE)(info);
203 sym_vis = ELFW(ST_VISIBILITY)(other);
205 if (sym_bind != STB_LOCAL) {
206 /* we search global or weak symbols */
207 sym_index = find_elf_sym(s, name);
208 if (!sym_index)
209 goto do_def;
210 esym = &((ElfW(Sym) *)s->data)[sym_index];
211 if (esym->st_shndx != SHN_UNDEF) {
212 esym_bind = ELFW(ST_BIND)(esym->st_info);
213 /* propagate the most constraining visibility */
214 /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
215 esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
216 if (esym_vis == STV_DEFAULT) {
217 new_vis = sym_vis;
218 } else if (sym_vis == STV_DEFAULT) {
219 new_vis = esym_vis;
220 } else {
221 new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
223 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
224 | new_vis;
225 other = esym->st_other; /* in case we have to patch esym */
226 if (sh_num == SHN_UNDEF) {
227 /* ignore adding of undefined symbol if the
228 corresponding symbol is already defined */
229 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
230 /* global overrides weak, so patch */
231 goto do_patch;
232 } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
233 /* weak is ignored if already global */
234 } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
235 /* ignore hidden symbols after */
236 } else if (esym->st_shndx == SHN_COMMON
237 && (sh_num < SHN_LORESERVE || sh_num == SHN_COMMON)) {
238 /* gr: Happens with 'tcc ... -static tcctest.c' on e.g. Ubuntu 6.01
239 No idea if this is the correct solution ... */
240 goto do_patch;
241 } else if (s == tcc_state->dynsymtab_section) {
242 /* we accept that two DLL define the same symbol */
243 } else {
244 #if 1
245 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
246 sym_bind, sh_num, new_vis, esym_bind, esym->st_shndx, esym_vis);
247 #endif
248 error_noabort("'%s' defined twice", name);
250 } else {
251 do_patch:
252 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
253 esym->st_shndx = sh_num;
254 esym->st_value = value;
255 esym->st_size = size;
256 esym->st_other = other;
258 } else {
259 do_def:
260 sym_index = put_elf_sym(s, value, size,
261 ELFW(ST_INFO)(sym_bind, sym_type), other,
262 sh_num, name);
264 return sym_index;
267 /* put relocation */
268 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
269 int type, int symbol)
271 char buf[256];
272 Section *sr;
273 ElfW_Rel *rel;
275 sr = s->reloc;
276 if (!sr) {
277 /* if no relocation section, create it */
278 snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
279 /* if the symtab is allocated, then we consider the relocation
280 are also */
281 sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
282 sr->sh_entsize = sizeof(ElfW_Rel);
283 sr->link = symtab;
284 sr->sh_info = s->sh_num;
285 s->reloc = sr;
287 rel = section_ptr_add(sr, sizeof(ElfW_Rel));
288 rel->r_offset = offset;
289 rel->r_info = ELFW(R_INFO)(symbol, type);
290 #ifdef TCC_TARGET_X86_64
291 rel->r_addend = 0;
292 #endif
295 /* put stab debug information */
297 typedef struct {
298 unsigned int n_strx; /* index into string table of name */
299 unsigned char n_type; /* type of symbol */
300 unsigned char n_other; /* misc info (usually empty) */
301 unsigned short n_desc; /* description field */
302 unsigned int n_value; /* value of symbol */
303 } Stab_Sym;
305 static void put_stabs(const char *str, int type, int other, int desc,
306 unsigned long value)
308 Stab_Sym *sym;
310 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
311 if (str) {
312 sym->n_strx = put_elf_str(stabstr_section, str);
313 } else {
314 sym->n_strx = 0;
316 sym->n_type = type;
317 sym->n_other = other;
318 sym->n_desc = desc;
319 sym->n_value = value;
322 static void put_stabs_r(const char *str, int type, int other, int desc,
323 unsigned long value, Section *sec, int sym_index)
325 put_stabs(str, type, other, desc, value);
326 put_elf_reloc(symtab_section, stab_section,
327 stab_section->data_offset - sizeof(unsigned int),
328 R_DATA_32, sym_index);
331 static void put_stabn(int type, int other, int desc, int value)
333 put_stabs(NULL, type, other, desc, value);
336 static void put_stabd(int type, int other, int desc)
338 put_stabs(NULL, type, other, desc, 0);
341 /* In an ELF file symbol table, the local symbols must appear below
342 the global and weak ones. Since TCC cannot sort it while generating
343 the code, we must do it after. All the relocation tables are also
344 modified to take into account the symbol table sorting */
345 static void sort_syms(TCCState *s1, Section *s)
347 int *old_to_new_syms;
348 ElfW(Sym) *new_syms;
349 int nb_syms, i;
350 ElfW(Sym) *p, *q;
351 ElfW_Rel *rel, *rel_end;
352 Section *sr;
353 int type, sym_index;
355 nb_syms = s->data_offset / sizeof(ElfW(Sym));
356 new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
357 old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
359 /* first pass for local symbols */
360 p = (ElfW(Sym) *)s->data;
361 q = new_syms;
362 for(i = 0; i < nb_syms; i++) {
363 if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
364 old_to_new_syms[i] = q - new_syms;
365 *q++ = *p;
367 p++;
369 /* save the number of local symbols in section header */
370 s->sh_info = q - new_syms;
372 /* then second pass for non local symbols */
373 p = (ElfW(Sym) *)s->data;
374 for(i = 0; i < nb_syms; i++) {
375 if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
376 old_to_new_syms[i] = q - new_syms;
377 *q++ = *p;
379 p++;
382 /* we copy the new symbols to the old */
383 memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
384 tcc_free(new_syms);
386 /* now we modify all the relocations */
387 for(i = 1; i < s1->nb_sections; i++) {
388 sr = s1->sections[i];
389 if (sr->sh_type == SHT_RELX && sr->link == s) {
390 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
391 for(rel = (ElfW_Rel *)sr->data;
392 rel < rel_end;
393 rel++) {
394 sym_index = ELFW(R_SYM)(rel->r_info);
395 type = ELFW(R_TYPE)(rel->r_info);
396 sym_index = old_to_new_syms[sym_index];
397 rel->r_info = ELFW(R_INFO)(sym_index, type);
402 tcc_free(old_to_new_syms);
405 /* relocate common symbols in the .bss section */
406 static void relocate_common_syms(void)
408 ElfW(Sym) *sym, *sym_end;
409 unsigned long offset, align;
411 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
412 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
413 sym < sym_end;
414 sym++) {
415 if (sym->st_shndx == SHN_COMMON) {
416 /* align symbol */
417 align = sym->st_value;
418 offset = bss_section->data_offset;
419 offset = (offset + align - 1) & -align;
420 sym->st_value = offset;
421 sym->st_shndx = bss_section->sh_num;
422 offset += sym->st_size;
423 bss_section->data_offset = offset;
428 /* relocate symbol table, resolve undefined symbols if do_resolve is
429 true and output error if undefined symbol. */
430 static void relocate_syms(TCCState *s1, int do_resolve)
432 ElfW(Sym) *sym, *esym, *sym_end;
433 int sym_bind, sh_num, sym_index;
434 const char *name;
436 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
437 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
438 sym < sym_end;
439 sym++) {
440 sh_num = sym->st_shndx;
441 if (sh_num == SHN_UNDEF) {
442 name = strtab_section->data + sym->st_name;
443 if (do_resolve) {
444 #ifndef _WIN32
445 unsigned long addr;
446 name = symtab_section->link->data + sym->st_name;
447 addr = (unsigned long)resolve_sym(s1, name);
448 if (addr) {
449 sym->st_value = addr;
450 goto found;
452 #endif
453 } else if (s1->dynsym) {
454 /* if dynamic symbol exist, then use it */
455 sym_index = find_elf_sym(s1->dynsym, name);
456 if (sym_index) {
457 esym = &((ElfW(Sym) *)s1->dynsym->data)[sym_index];
458 sym->st_value = esym->st_value;
459 goto found;
462 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
463 it */
464 if (!strcmp(name, "_fp_hw"))
465 goto found;
466 /* only weak symbols are accepted to be undefined. Their
467 value is zero */
468 sym_bind = ELFW(ST_BIND)(sym->st_info);
469 if (sym_bind == STB_WEAK) {
470 sym->st_value = 0;
471 } else {
472 error_noabort("undefined symbol '%s'", name);
474 } else if (sh_num < SHN_LORESERVE) {
475 /* add section base */
476 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
478 found: ;
482 #ifdef TCC_TARGET_X86_64
483 #define JMP_TABLE_ENTRY_SIZE 14
484 static unsigned long add_jmp_table(TCCState *s1, unsigned long val)
486 char *p = s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset;
487 s1->runtime_plt_and_got_offset += JMP_TABLE_ENTRY_SIZE;
488 /* jmp *0x0(%rip) */
489 p[0] = 0xff;
490 p[1] = 0x25;
491 *(int *)(p + 2) = 0;
492 *(unsigned long *)(p + 6) = val;
493 return (unsigned long)p;
496 static unsigned long add_got_table(TCCState *s1, unsigned long val)
498 unsigned long *p =(unsigned long *)(s1->runtime_plt_and_got +
499 s1->runtime_plt_and_got_offset);
500 s1->runtime_plt_and_got_offset += sizeof(void *);
501 *p = val;
502 return (unsigned long)p;
504 #endif
506 /* relocate a given section (CPU dependent) */
507 static void relocate_section(TCCState *s1, Section *s)
509 Section *sr;
510 ElfW_Rel *rel, *rel_end, *qrel;
511 ElfW(Sym) *sym;
512 int type, sym_index;
513 unsigned char *ptr;
514 unsigned long val, addr;
515 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
516 int esym_index;
517 #endif
519 sr = s->reloc;
520 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
521 qrel = (ElfW_Rel *)sr->data;
522 for(rel = qrel;
523 rel < rel_end;
524 rel++) {
525 ptr = s->data + rel->r_offset;
527 sym_index = ELFW(R_SYM)(rel->r_info);
528 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
529 val = sym->st_value;
530 #ifdef TCC_TARGET_X86_64
531 /* XXX: not tested */
532 val += rel->r_addend;
533 #endif
534 type = ELFW(R_TYPE)(rel->r_info);
535 addr = s->sh_addr + rel->r_offset;
537 /* CPU specific */
538 switch(type) {
539 #if defined(TCC_TARGET_I386)
540 case R_386_32:
541 if (s1->output_type == TCC_OUTPUT_DLL) {
542 esym_index = s1->symtab_to_dynsym[sym_index];
543 qrel->r_offset = rel->r_offset;
544 if (esym_index) {
545 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
546 qrel++;
547 break;
548 } else {
549 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
550 qrel++;
553 *(int *)ptr += val;
554 break;
555 case R_386_PC32:
556 if (s1->output_type == TCC_OUTPUT_DLL) {
557 /* DLL relocation */
558 esym_index = s1->symtab_to_dynsym[sym_index];
559 if (esym_index) {
560 qrel->r_offset = rel->r_offset;
561 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
562 qrel++;
563 break;
566 *(int *)ptr += val - addr;
567 break;
568 case R_386_PLT32:
569 *(int *)ptr += val - addr;
570 break;
571 case R_386_GLOB_DAT:
572 case R_386_JMP_SLOT:
573 *(int *)ptr = val;
574 break;
575 case R_386_GOTPC:
576 *(int *)ptr += s1->got->sh_addr - addr;
577 break;
578 case R_386_GOTOFF:
579 *(int *)ptr += val - s1->got->sh_addr;
580 break;
581 case R_386_GOT32:
582 /* we load the got offset */
583 *(int *)ptr += s1->got_offsets[sym_index];
584 break;
585 #elif defined(TCC_TARGET_ARM)
586 case R_ARM_PC24:
587 case R_ARM_CALL:
588 case R_ARM_JUMP24:
589 case R_ARM_PLT32:
591 int x;
592 x = (*(int *)ptr)&0xffffff;
593 (*(int *)ptr) &= 0xff000000;
594 if (x & 0x800000)
595 x -= 0x1000000;
596 x *= 4;
597 x += val - addr;
598 if((x & 3) != 0 || x >= 0x4000000 || x < -0x4000000)
599 error("can't relocate value at %x",addr);
600 x >>= 2;
601 x &= 0xffffff;
602 (*(int *)ptr) |= x;
604 break;
605 case R_ARM_PREL31:
607 int x;
608 x = (*(int *)ptr) & 0x7fffffff;
609 (*(int *)ptr) &= 0x80000000;
610 x = (x * 2) / 2;
611 x += val - addr;
612 if((x^(x>>1))&0x40000000)
613 error("can't relocate value at %x",addr);
614 (*(int *)ptr) |= x & 0x7fffffff;
616 case R_ARM_ABS32:
617 *(int *)ptr += val;
618 break;
619 case R_ARM_BASE_PREL:
620 *(int *)ptr += s1->got->sh_addr - addr;
621 break;
622 case R_ARM_GOTOFF32:
623 *(int *)ptr += val - s1->got->sh_addr;
624 break;
625 case R_ARM_GOT_BREL:
626 /* we load the got offset */
627 *(int *)ptr += s1->got_offsets[sym_index];
628 break;
629 case R_ARM_COPY:
630 break;
631 default:
632 fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",
633 type,addr,(unsigned int )ptr,val);
634 break;
635 #elif defined(TCC_TARGET_C67)
636 case R_C60_32:
637 *(int *)ptr += val;
638 break;
639 case R_C60LO16:
641 uint32_t orig;
643 /* put the low 16 bits of the absolute address */
644 // add to what is already there
646 orig = ((*(int *)(ptr )) >> 7) & 0xffff;
647 orig |= (((*(int *)(ptr+4)) >> 7) & 0xffff) << 16;
649 //patch both at once - assumes always in pairs Low - High
651 *(int *) ptr = (*(int *) ptr & (~(0xffff << 7)) ) | (((val+orig) & 0xffff) << 7);
652 *(int *)(ptr+4) = (*(int *)(ptr+4) & (~(0xffff << 7)) ) | ((((val+orig)>>16) & 0xffff) << 7);
654 break;
655 case R_C60HI16:
656 break;
657 default:
658 fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",
659 type,addr,(unsigned int )ptr,val);
660 break;
661 #elif defined(TCC_TARGET_X86_64)
662 case R_X86_64_64:
663 if (s1->output_type == TCC_OUTPUT_DLL) {
664 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
665 qrel->r_addend = *(long long *)ptr + val;
666 qrel++;
668 *(long long *)ptr += val;
669 break;
670 case R_X86_64_32:
671 case R_X86_64_32S:
672 if (s1->output_type == TCC_OUTPUT_DLL) {
673 /* XXX: this logic may depend on TCC's codegen
674 now TCC uses R_X86_64_32 even for a 64bit pointer */
675 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
676 qrel->r_addend = *(int *)ptr + val;
677 qrel++;
679 *(int *)ptr += val;
680 break;
681 case R_X86_64_PC32: {
682 if (s1->output_type == TCC_OUTPUT_DLL) {
683 /* DLL relocation */
684 esym_index = s1->symtab_to_dynsym[sym_index];
685 if (esym_index) {
686 qrel->r_offset = rel->r_offset;
687 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
688 qrel->r_addend = *(int *)ptr;
689 qrel++;
690 break;
693 long diff = val - addr;
694 if (diff <= -2147483647 || diff > 2147483647) {
695 /* XXX: naive support for over 32bit jump */
696 if (s1->output_type == TCC_OUTPUT_MEMORY) {
697 val = add_jmp_table(s1, val);
698 diff = val - addr;
700 if (diff <= -2147483647 || diff > 2147483647) {
701 error("internal error: relocation failed");
704 *(int *)ptr += diff;
706 break;
707 case R_X86_64_PLT32:
708 *(int *)ptr += val - addr;
709 break;
710 case R_X86_64_GLOB_DAT:
711 case R_X86_64_JUMP_SLOT:
712 *(int *)ptr = val;
713 break;
714 case R_X86_64_GOTPCREL:
715 if (s1->output_type == TCC_OUTPUT_MEMORY) {
716 val = add_got_table(s1, val - rel->r_addend) + rel->r_addend;
717 *(int *)ptr += val - addr;
718 break;
720 *(int *)ptr += (s1->got->sh_addr - addr +
721 s1->got_offsets[sym_index] - 4);
722 break;
723 case R_X86_64_GOTTPOFF:
724 *(int *)ptr += val - s1->got->sh_addr;
725 break;
726 case R_X86_64_GOT32:
727 /* we load the got offset */
728 *(int *)ptr += s1->got_offsets[sym_index];
729 break;
730 #else
731 #error unsupported processor
732 #endif
735 /* if the relocation is allocated, we change its symbol table */
736 if (sr->sh_flags & SHF_ALLOC)
737 sr->link = s1->dynsym;
740 /* relocate relocation table in 'sr' */
741 static void relocate_rel(TCCState *s1, Section *sr)
743 Section *s;
744 ElfW_Rel *rel, *rel_end;
746 s = s1->sections[sr->sh_info];
747 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
748 for(rel = (ElfW_Rel *)sr->data;
749 rel < rel_end;
750 rel++) {
751 rel->r_offset += s->sh_addr;
755 /* count the number of dynamic relocations so that we can reserve
756 their space */
757 static int prepare_dynamic_rel(TCCState *s1, Section *sr)
759 ElfW_Rel *rel, *rel_end;
760 int sym_index, esym_index, type, count;
762 count = 0;
763 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
764 for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
765 sym_index = ELFW(R_SYM)(rel->r_info);
766 type = ELFW(R_TYPE)(rel->r_info);
767 switch(type) {
768 #if defined(TCC_TARGET_I386)
769 case R_386_32:
770 #elif defined(TCC_TARGET_X86_64)
771 case R_X86_64_32:
772 case R_X86_64_32S:
773 case R_X86_64_64:
774 #endif
775 count++;
776 break;
777 #if defined(TCC_TARGET_I386)
778 case R_386_PC32:
779 #elif defined(TCC_TARGET_X86_64)
780 case R_X86_64_PC32:
781 #endif
782 esym_index = s1->symtab_to_dynsym[sym_index];
783 if (esym_index)
784 count++;
785 break;
786 default:
787 break;
790 if (count) {
791 /* allocate the section */
792 sr->sh_flags |= SHF_ALLOC;
793 sr->sh_size = count * sizeof(ElfW_Rel);
795 return count;
798 static void put_got_offset(TCCState *s1, int index, unsigned long val)
800 int n;
801 unsigned long *tab;
803 if (index >= s1->nb_got_offsets) {
804 /* find immediately bigger power of 2 and reallocate array */
805 n = 1;
806 while (index >= n)
807 n *= 2;
808 tab = tcc_realloc(s1->got_offsets, n * sizeof(unsigned long));
809 if (!tab)
810 error("memory full");
811 s1->got_offsets = tab;
812 memset(s1->got_offsets + s1->nb_got_offsets, 0,
813 (n - s1->nb_got_offsets) * sizeof(unsigned long));
814 s1->nb_got_offsets = n;
816 s1->got_offsets[index] = val;
819 /* XXX: suppress that */
820 static void put32(unsigned char *p, uint32_t val)
822 p[0] = val;
823 p[1] = val >> 8;
824 p[2] = val >> 16;
825 p[3] = val >> 24;
828 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_ARM) || \
829 defined(TCC_TARGET_X86_64)
830 static uint32_t get32(unsigned char *p)
832 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
834 #endif
836 static void build_got(TCCState *s1)
838 unsigned char *ptr;
840 /* if no got, then create it */
841 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
842 s1->got->sh_entsize = 4;
843 add_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
844 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
845 ptr = section_ptr_add(s1->got, 3 * PTR_SIZE);
846 #if PTR_SIZE == 4
847 /* keep space for _DYNAMIC pointer, if present */
848 put32(ptr, 0);
849 /* two dummy got entries */
850 put32(ptr + 4, 0);
851 put32(ptr + 8, 0);
852 #else
853 /* keep space for _DYNAMIC pointer, if present */
854 put32(ptr, 0);
855 put32(ptr + 4, 0);
856 /* two dummy got entries */
857 put32(ptr + 8, 0);
858 put32(ptr + 12, 0);
859 put32(ptr + 16, 0);
860 put32(ptr + 20, 0);
861 #endif
864 /* put a got entry corresponding to a symbol in symtab_section. 'size'
865 and 'info' can be modifed if more precise info comes from the DLL */
866 static void put_got_entry(TCCState *s1,
867 int reloc_type, unsigned long size, int info,
868 int sym_index)
870 int index;
871 const char *name;
872 ElfW(Sym) *sym;
873 unsigned long offset;
874 int *ptr;
876 if (!s1->got)
877 build_got(s1);
879 /* if a got entry already exists for that symbol, no need to add one */
880 if (sym_index < s1->nb_got_offsets &&
881 s1->got_offsets[sym_index] != 0)
882 return;
884 put_got_offset(s1, sym_index, s1->got->data_offset);
886 if (s1->dynsym) {
887 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
888 name = symtab_section->link->data + sym->st_name;
889 offset = sym->st_value;
890 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
891 if (reloc_type ==
892 #ifdef TCC_TARGET_X86_64
893 R_X86_64_JUMP_SLOT
894 #else
895 R_386_JMP_SLOT
896 #endif
898 Section *plt;
899 uint8_t *p;
900 int modrm;
902 #if defined(TCC_OUTPUT_DLL_WITH_PLT)
903 modrm = 0x25;
904 #else
905 /* if we build a DLL, we add a %ebx offset */
906 if (s1->output_type == TCC_OUTPUT_DLL)
907 modrm = 0xa3;
908 else
909 modrm = 0x25;
910 #endif
912 /* add a PLT entry */
913 plt = s1->plt;
914 if (plt->data_offset == 0) {
915 /* first plt entry */
916 p = section_ptr_add(plt, 16);
917 p[0] = 0xff; /* pushl got + PTR_SIZE */
918 p[1] = modrm + 0x10;
919 put32(p + 2, PTR_SIZE);
920 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
921 p[7] = modrm;
922 put32(p + 8, PTR_SIZE * 2);
925 p = section_ptr_add(plt, 16);
926 p[0] = 0xff; /* jmp *(got + x) */
927 p[1] = modrm;
928 put32(p + 2, s1->got->data_offset);
929 p[6] = 0x68; /* push $xxx */
930 put32(p + 7, (plt->data_offset - 32) >> 1);
931 p[11] = 0xe9; /* jmp plt_start */
932 put32(p + 12, -(plt->data_offset));
934 /* the symbol is modified so that it will be relocated to
935 the PLT */
936 #if !defined(TCC_OUTPUT_DLL_WITH_PLT)
937 if (s1->output_type == TCC_OUTPUT_EXE)
938 #endif
939 offset = plt->data_offset - 16;
941 #elif defined(TCC_TARGET_ARM)
942 if (reloc_type == R_ARM_JUMP_SLOT) {
943 Section *plt;
944 uint8_t *p;
946 /* if we build a DLL, we add a %ebx offset */
947 if (s1->output_type == TCC_OUTPUT_DLL)
948 error("DLLs unimplemented!");
950 /* add a PLT entry */
951 plt = s1->plt;
952 if (plt->data_offset == 0) {
953 /* first plt entry */
954 p = section_ptr_add(plt, 16);
955 put32(p , 0xe52de004);
956 put32(p + 4, 0xe59fe010);
957 put32(p + 8, 0xe08fe00e);
958 put32(p + 12, 0xe5bef008);
961 p = section_ptr_add(plt, 16);
962 put32(p , 0xe59fc004);
963 put32(p+4, 0xe08fc00c);
964 put32(p+8, 0xe59cf000);
965 put32(p+12, s1->got->data_offset);
967 /* the symbol is modified so that it will be relocated to
968 the PLT */
969 if (s1->output_type == TCC_OUTPUT_EXE)
970 offset = plt->data_offset - 16;
972 #elif defined(TCC_TARGET_C67)
973 error("C67 got not implemented");
974 #else
975 #error unsupported CPU
976 #endif
977 index = put_elf_sym(s1->dynsym, offset,
978 size, info, 0, sym->st_shndx, name);
979 /* put a got entry */
980 put_elf_reloc(s1->dynsym, s1->got,
981 s1->got->data_offset,
982 reloc_type, index);
984 ptr = section_ptr_add(s1->got, PTR_SIZE);
985 *ptr = 0;
988 /* build GOT and PLT entries */
989 static void build_got_entries(TCCState *s1)
991 Section *s, *symtab;
992 ElfW_Rel *rel, *rel_end;
993 ElfW(Sym) *sym;
994 int i, type, reloc_type, sym_index;
996 for(i = 1; i < s1->nb_sections; i++) {
997 s = s1->sections[i];
998 if (s->sh_type != SHT_RELX)
999 continue;
1000 /* no need to handle got relocations */
1001 if (s->link != symtab_section)
1002 continue;
1003 symtab = s->link;
1004 rel_end = (ElfW_Rel *)(s->data + s->data_offset);
1005 for(rel = (ElfW_Rel *)s->data;
1006 rel < rel_end;
1007 rel++) {
1008 type = ELFW(R_TYPE)(rel->r_info);
1009 switch(type) {
1010 #if defined(TCC_TARGET_I386)
1011 case R_386_GOT32:
1012 case R_386_GOTOFF:
1013 case R_386_GOTPC:
1014 case R_386_PLT32:
1015 if (!s1->got)
1016 build_got(s1);
1017 if (type == R_386_GOT32 || type == R_386_PLT32) {
1018 sym_index = ELFW(R_SYM)(rel->r_info);
1019 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1020 /* look at the symbol got offset. If none, then add one */
1021 if (type == R_386_GOT32)
1022 reloc_type = R_386_GLOB_DAT;
1023 else
1024 reloc_type = R_386_JMP_SLOT;
1025 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1026 sym_index);
1028 break;
1029 #elif defined(TCC_TARGET_ARM)
1030 case R_ARM_GOT_BREL:
1031 case R_ARM_GOTOFF32:
1032 case R_ARM_BASE_PREL:
1033 case R_ARM_PLT32:
1034 if (!s1->got)
1035 build_got(s1);
1036 if (type == R_ARM_GOT_BREL || type == R_ARM_PLT32) {
1037 sym_index = ELFW(R_SYM)(rel->r_info);
1038 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1039 /* look at the symbol got offset. If none, then add one */
1040 if (type == R_ARM_GOT_BREL)
1041 reloc_type = R_ARM_GLOB_DAT;
1042 else
1043 reloc_type = R_ARM_JUMP_SLOT;
1044 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1045 sym_index);
1047 break;
1048 #elif defined(TCC_TARGET_C67)
1049 case R_C60_GOT32:
1050 case R_C60_GOTOFF:
1051 case R_C60_GOTPC:
1052 case R_C60_PLT32:
1053 if (!s1->got)
1054 build_got(s1);
1055 if (type == R_C60_GOT32 || type == R_C60_PLT32) {
1056 sym_index = ELFW(R_SYM)(rel->r_info);
1057 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1058 /* look at the symbol got offset. If none, then add one */
1059 if (type == R_C60_GOT32)
1060 reloc_type = R_C60_GLOB_DAT;
1061 else
1062 reloc_type = R_C60_JMP_SLOT;
1063 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1064 sym_index);
1066 break;
1067 #elif defined(TCC_TARGET_X86_64)
1068 case R_X86_64_GOT32:
1069 case R_X86_64_GOTTPOFF:
1070 case R_X86_64_GOTPCREL:
1071 case R_X86_64_PLT32:
1072 if (!s1->got)
1073 build_got(s1);
1074 if (type == R_X86_64_GOT32 || type == R_X86_64_GOTPCREL ||
1075 type == R_X86_64_PLT32) {
1076 sym_index = ELFW(R_SYM)(rel->r_info);
1077 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1078 /* look at the symbol got offset. If none, then add one */
1079 if (type == R_X86_64_GOT32 || type == R_X86_64_GOTPCREL)
1080 reloc_type = R_X86_64_GLOB_DAT;
1081 else
1082 reloc_type = R_X86_64_JUMP_SLOT;
1083 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
1084 sym_index);
1086 break;
1087 #else
1088 #error unsupported CPU
1089 #endif
1090 default:
1091 break;
1097 static Section *new_symtab(TCCState *s1,
1098 const char *symtab_name, int sh_type, int sh_flags,
1099 const char *strtab_name,
1100 const char *hash_name, int hash_sh_flags)
1102 Section *symtab, *strtab, *hash;
1103 int *ptr, nb_buckets;
1105 symtab = new_section(s1, symtab_name, sh_type, sh_flags);
1106 symtab->sh_entsize = sizeof(ElfW(Sym));
1107 strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
1108 put_elf_str(strtab, "");
1109 symtab->link = strtab;
1110 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
1112 nb_buckets = 1;
1114 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
1115 hash->sh_entsize = sizeof(int);
1116 symtab->hash = hash;
1117 hash->link = symtab;
1119 ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
1120 ptr[0] = nb_buckets;
1121 ptr[1] = 1;
1122 memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
1123 return symtab;
1126 /* put dynamic tag */
1127 static void put_dt(Section *dynamic, int dt, unsigned long val)
1129 ElfW(Dyn) *dyn;
1130 dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
1131 dyn->d_tag = dt;
1132 dyn->d_un.d_val = val;
1135 static void add_init_array_defines(TCCState *s1, const char *section_name)
1137 Section *s;
1138 long end_offset;
1139 char sym_start[1024];
1140 char sym_end[1024];
1142 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
1143 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
1145 s = find_section(s1, section_name);
1146 if (!s) {
1147 end_offset = 0;
1148 s = data_section;
1149 } else {
1150 end_offset = s->data_offset;
1153 add_elf_sym(symtab_section,
1154 0, 0,
1155 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1156 s->sh_num, sym_start);
1157 add_elf_sym(symtab_section,
1158 end_offset, 0,
1159 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1160 s->sh_num, sym_end);
1163 /* add tcc runtime libraries */
1164 static void tcc_add_runtime(TCCState *s1)
1166 #if defined(CONFIG_TCC_BCHECK) || !defined(CONFIG_USE_LIBGCC)
1167 char buf[1024];
1168 #endif
1170 #ifdef CONFIG_TCC_BCHECK
1171 if (s1->do_bounds_check) {
1172 unsigned long *ptr;
1173 Section *init_section;
1174 unsigned char *pinit;
1175 int sym_index;
1177 /* XXX: add an object file to do that */
1178 ptr = section_ptr_add(bounds_section, sizeof(unsigned long));
1179 *ptr = 0;
1180 add_elf_sym(symtab_section, 0, 0,
1181 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1182 bounds_section->sh_num, "__bounds_start");
1183 /* add bound check code */
1184 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, "bcheck.o");
1185 tcc_add_file(s1, buf);
1186 #ifdef TCC_TARGET_I386
1187 if (s1->output_type != TCC_OUTPUT_MEMORY) {
1188 /* add 'call __bound_init()' in .init section */
1189 init_section = find_section(s1, ".init");
1190 pinit = section_ptr_add(init_section, 5);
1191 pinit[0] = 0xe8;
1192 put32(pinit + 1, -4);
1193 sym_index = find_elf_sym(symtab_section, "__bound_init");
1194 put_elf_reloc(symtab_section, init_section,
1195 init_section->data_offset - 4, R_386_PC32, sym_index);
1197 #endif
1199 #endif
1200 /* add libc */
1201 if (!s1->nostdlib) {
1202 tcc_add_library(s1, "c");
1204 #ifdef CONFIG_USE_LIBGCC
1205 tcc_add_file(s1, CONFIG_SYSROOT "/lib/libgcc_s.so.1");
1206 #else
1207 snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, "libtcc1.a");
1208 tcc_add_file(s1, buf);
1209 #endif
1211 /* add crt end if not memory output */
1212 if (s1->output_type != TCC_OUTPUT_MEMORY && !s1->nostdlib) {
1213 tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o");
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 add_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 add_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 add_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 /* horrible new standard ldscript defines */
1239 add_init_array_defines(s1, ".preinit_array");
1240 add_init_array_defines(s1, ".init_array");
1241 add_init_array_defines(s1, ".fini_array");
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 add_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 add_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 /* name of ELF interpreter */
1278 #if defined __FreeBSD__
1279 static char elf_interp[] = "/usr/libexec/ld-elf.so.1";
1280 #elif defined TCC_ARM_EABI
1281 static char elf_interp[] = "/lib/ld-linux.so.3";
1282 #elif defined(TCC_TARGET_X86_64)
1283 static char elf_interp[] = "/lib/ld-linux-x86-64.so.2";
1284 #elif defined(TCC_UCLIBC)
1285 static char elf_interp[] = "/lib/ld-uClibc.so.0";
1286 #else
1287 static char elf_interp[] = "/lib/ld-linux.so.2";
1288 #endif
1290 static void tcc_output_binary(TCCState *s1, FILE *f,
1291 const int *section_order)
1293 Section *s;
1294 int i, offset, size;
1296 offset = 0;
1297 for(i=1;i<s1->nb_sections;i++) {
1298 s = s1->sections[section_order[i]];
1299 if (s->sh_type != SHT_NOBITS &&
1300 (s->sh_flags & SHF_ALLOC)) {
1301 while (offset < s->sh_offset) {
1302 fputc(0, f);
1303 offset++;
1305 size = s->sh_size;
1306 fwrite(s->data, 1, size, f);
1307 offset += size;
1312 /* output an ELF file */
1313 /* XXX: suppress unneeded sections */
1314 int elf_output_file(TCCState *s1, const char *filename)
1316 ElfW(Ehdr) ehdr;
1317 FILE *f;
1318 int fd, mode, ret;
1319 int *section_order;
1320 int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k;
1321 unsigned long addr;
1322 Section *strsec, *s;
1323 ElfW(Shdr) shdr, *sh;
1324 ElfW(Phdr) *phdr, *ph;
1325 Section *interp, *dynamic, *dynstr;
1326 unsigned long saved_dynamic_data_offset;
1327 ElfW(Sym) *sym;
1328 int type, file_type;
1329 unsigned long rel_addr, rel_size;
1331 file_type = s1->output_type;
1332 s1->nb_errors = 0;
1334 if (file_type != TCC_OUTPUT_OBJ) {
1335 tcc_add_runtime(s1);
1338 phdr = NULL;
1339 section_order = NULL;
1340 interp = NULL;
1341 dynamic = NULL;
1342 dynstr = NULL; /* avoid warning */
1343 saved_dynamic_data_offset = 0; /* avoid warning */
1345 if (file_type != TCC_OUTPUT_OBJ) {
1346 relocate_common_syms();
1348 tcc_add_linker_symbols(s1);
1350 if (!s1->static_link) {
1351 const char *name;
1352 int sym_index, index;
1353 ElfW(Sym) *esym, *sym_end;
1355 if (file_type == TCC_OUTPUT_EXE) {
1356 char *ptr;
1357 /* add interpreter section only if executable */
1358 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
1359 interp->sh_addralign = 1;
1360 ptr = section_ptr_add(interp, sizeof(elf_interp));
1361 strcpy(ptr, elf_interp);
1364 /* add dynamic symbol table */
1365 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
1366 ".dynstr",
1367 ".hash", SHF_ALLOC);
1368 dynstr = s1->dynsym->link;
1370 /* add dynamic section */
1371 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
1372 SHF_ALLOC | SHF_WRITE);
1373 dynamic->link = dynstr;
1374 dynamic->sh_entsize = sizeof(ElfW(Dyn));
1376 /* add PLT */
1377 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
1378 SHF_ALLOC | SHF_EXECINSTR);
1379 s1->plt->sh_entsize = 4;
1381 build_got(s1);
1383 /* scan for undefined symbols and see if they are in the
1384 dynamic symbols. If a symbol STT_FUNC is found, then we
1385 add it in the PLT. If a symbol STT_OBJECT is found, we
1386 add it in the .bss section with a suitable relocation */
1387 sym_end = (ElfW(Sym) *)(symtab_section->data +
1388 symtab_section->data_offset);
1389 if (file_type == TCC_OUTPUT_EXE) {
1390 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
1391 sym < sym_end;
1392 sym++) {
1393 if (sym->st_shndx == SHN_UNDEF) {
1394 name = symtab_section->link->data + sym->st_name;
1395 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1396 if (sym_index) {
1397 esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
1398 type = ELFW(ST_TYPE)(esym->st_info);
1399 if (type == STT_FUNC) {
1400 put_got_entry(s1, R_JMP_SLOT, esym->st_size,
1401 esym->st_info,
1402 sym - (ElfW(Sym) *)symtab_section->data);
1403 } else if (type == STT_OBJECT) {
1404 unsigned long offset;
1405 offset = bss_section->data_offset;
1406 /* XXX: which alignment ? */
1407 offset = (offset + 16 - 1) & -16;
1408 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1409 esym->st_info, 0,
1410 bss_section->sh_num, name);
1411 put_elf_reloc(s1->dynsym, bss_section,
1412 offset, R_COPY, index);
1413 offset += esym->st_size;
1414 bss_section->data_offset = offset;
1416 } else {
1417 /* STB_WEAK undefined symbols are accepted */
1418 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
1419 it */
1420 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
1421 !strcmp(name, "_fp_hw")) {
1422 } else {
1423 error_noabort("undefined symbol '%s'", name);
1426 } else if (s1->rdynamic &&
1427 ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1428 /* if -rdynamic option, then export all non
1429 local symbols */
1430 name = symtab_section->link->data + sym->st_name;
1431 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1432 sym->st_info, 0,
1433 sym->st_shndx, name);
1437 if (s1->nb_errors)
1438 goto fail;
1440 /* now look at unresolved dynamic symbols and export
1441 corresponding symbol */
1442 sym_end = (ElfW(Sym) *)(s1->dynsymtab_section->data +
1443 s1->dynsymtab_section->data_offset);
1444 for(esym = (ElfW(Sym) *)s1->dynsymtab_section->data + 1;
1445 esym < sym_end;
1446 esym++) {
1447 if (esym->st_shndx == SHN_UNDEF) {
1448 name = s1->dynsymtab_section->link->data + esym->st_name;
1449 sym_index = find_elf_sym(symtab_section, name);
1450 if (sym_index) {
1451 /* XXX: avoid adding a symbol if already
1452 present because of -rdynamic ? */
1453 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1454 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1455 sym->st_info, 0,
1456 sym->st_shndx, name);
1457 } else {
1458 if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
1459 /* weak symbols can stay undefined */
1460 } else {
1461 warning("undefined dynamic symbol '%s'", name);
1466 } else {
1467 int nb_syms;
1468 /* shared library case : we simply export all the global symbols */
1469 nb_syms = symtab_section->data_offset / sizeof(ElfW(Sym));
1470 s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms);
1471 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
1472 sym < sym_end;
1473 sym++) {
1474 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1475 #if defined(TCC_OUTPUT_DLL_WITH_PLT)
1476 if (ELFW(ST_TYPE)(sym->st_info) == STT_FUNC &&
1477 sym->st_shndx == SHN_UNDEF) {
1478 put_got_entry(s1, R_JMP_SLOT, sym->st_size,
1479 sym->st_info,
1480 sym - (ElfW(Sym) *)symtab_section->data);
1482 else if (ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT) {
1483 put_got_entry(s1, R_X86_64_GLOB_DAT, sym->st_size,
1484 sym->st_info,
1485 sym - (ElfW(Sym) *)symtab_section->data);
1487 else
1488 #endif
1490 name = symtab_section->link->data + sym->st_name;
1491 index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1492 sym->st_info, 0,
1493 sym->st_shndx, name);
1494 s1->symtab_to_dynsym[sym -
1495 (ElfW(Sym) *)symtab_section->data] =
1496 index;
1502 build_got_entries(s1);
1504 /* add a list of needed dlls */
1505 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1506 DLLReference *dllref = s1->loaded_dlls[i];
1507 if (dllref->level == 0)
1508 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
1510 /* XXX: currently, since we do not handle PIC code, we
1511 must relocate the readonly segments */
1512 if (file_type == TCC_OUTPUT_DLL) {
1513 if (s1->soname)
1514 put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
1515 put_dt(dynamic, DT_TEXTREL, 0);
1518 /* add necessary space for other entries */
1519 saved_dynamic_data_offset = dynamic->data_offset;
1520 dynamic->data_offset += sizeof(ElfW(Dyn)) * 9;
1521 } else {
1522 /* still need to build got entries in case of static link */
1523 build_got_entries(s1);
1527 memset(&ehdr, 0, sizeof(ehdr));
1529 /* we add a section for symbols */
1530 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
1531 put_elf_str(strsec, "");
1533 /* compute number of sections */
1534 shnum = s1->nb_sections;
1536 /* this array is used to reorder sections in the output file */
1537 section_order = tcc_malloc(sizeof(int) * shnum);
1538 section_order[0] = 0;
1539 sh_order_index = 1;
1541 /* compute number of program headers */
1542 switch(file_type) {
1543 default:
1544 case TCC_OUTPUT_OBJ:
1545 phnum = 0;
1546 break;
1547 case TCC_OUTPUT_EXE:
1548 if (!s1->static_link)
1549 phnum = 4;
1550 else
1551 phnum = 2;
1552 break;
1553 case TCC_OUTPUT_DLL:
1554 phnum = 3;
1555 break;
1558 /* allocate strings for section names and decide if an unallocated
1559 section should be output */
1560 /* NOTE: the strsec section comes last, so its size is also
1561 correct ! */
1562 for(i = 1; i < s1->nb_sections; i++) {
1563 s = s1->sections[i];
1564 s->sh_name = put_elf_str(strsec, s->name);
1565 #if 0 //gr
1566 printf("section: f=%08x t=%08x i=%08x %s %s\n",
1567 s->sh_flags,
1568 s->sh_type,
1569 s->sh_info,
1570 s->name,
1571 s->reloc ? s->reloc->name : "n"
1573 #endif
1574 /* when generating a DLL, we include relocations but we may
1575 patch them */
1576 if (file_type == TCC_OUTPUT_DLL &&
1577 s->sh_type == SHT_RELX &&
1578 !(s->sh_flags & SHF_ALLOC)) {
1579 /* //gr: avoid bogus relocs for empty (debug) sections */
1580 if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
1581 prepare_dynamic_rel(s1, s);
1582 else if (s1->do_debug)
1583 s->sh_size = s->data_offset;
1584 } else if (s1->do_debug ||
1585 file_type == TCC_OUTPUT_OBJ ||
1586 (s->sh_flags & SHF_ALLOC) ||
1587 i == (s1->nb_sections - 1)) {
1588 /* we output all sections if debug or object file */
1589 s->sh_size = s->data_offset;
1593 /* allocate program segment headers */
1594 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
1596 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1597 file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1598 } else {
1599 file_offset = 0;
1601 if (phnum > 0) {
1602 /* compute section to program header mapping */
1603 if (s1->has_text_addr) {
1604 int a_offset, p_offset;
1605 addr = s1->text_addr;
1606 /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
1607 ELF_PAGE_SIZE */
1608 a_offset = addr & (ELF_PAGE_SIZE - 1);
1609 p_offset = file_offset & (ELF_PAGE_SIZE - 1);
1610 if (a_offset < p_offset)
1611 a_offset += ELF_PAGE_SIZE;
1612 file_offset += (a_offset - p_offset);
1613 } else {
1614 if (file_type == TCC_OUTPUT_DLL)
1615 addr = 0;
1616 else
1617 addr = ELF_START_ADDR;
1618 /* compute address after headers */
1619 addr += (file_offset & (ELF_PAGE_SIZE - 1));
1622 /* dynamic relocation table information, for .dynamic section */
1623 rel_size = 0;
1624 rel_addr = 0;
1626 /* leave one program header for the program interpreter */
1627 ph = &phdr[0];
1628 if (interp)
1629 ph++;
1631 for(j = 0; j < 2; j++) {
1632 ph->p_type = PT_LOAD;
1633 if (j == 0)
1634 ph->p_flags = PF_R | PF_X;
1635 else
1636 ph->p_flags = PF_R | PF_W;
1637 ph->p_align = ELF_PAGE_SIZE;
1639 /* we do the following ordering: interp, symbol tables,
1640 relocations, progbits, nobits */
1641 /* XXX: do faster and simpler sorting */
1642 for(k = 0; k < 5; k++) {
1643 for(i = 1; i < s1->nb_sections; i++) {
1644 s = s1->sections[i];
1645 /* compute if section should be included */
1646 if (j == 0) {
1647 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1648 SHF_ALLOC)
1649 continue;
1650 } else {
1651 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1652 (SHF_ALLOC | SHF_WRITE))
1653 continue;
1655 if (s == interp) {
1656 if (k != 0)
1657 continue;
1658 } else if (s->sh_type == SHT_DYNSYM ||
1659 s->sh_type == SHT_STRTAB ||
1660 s->sh_type == SHT_HASH) {
1661 if (k != 1)
1662 continue;
1663 } else if (s->sh_type == SHT_RELX) {
1664 if (k != 2)
1665 continue;
1666 } else if (s->sh_type == SHT_NOBITS) {
1667 if (k != 4)
1668 continue;
1669 } else {
1670 if (k != 3)
1671 continue;
1673 section_order[sh_order_index++] = i;
1675 /* section matches: we align it and add its size */
1676 tmp = addr;
1677 addr = (addr + s->sh_addralign - 1) &
1678 ~(s->sh_addralign - 1);
1679 file_offset += addr - tmp;
1680 s->sh_offset = file_offset;
1681 s->sh_addr = addr;
1683 /* update program header infos */
1684 if (ph->p_offset == 0) {
1685 ph->p_offset = file_offset;
1686 ph->p_vaddr = addr;
1687 ph->p_paddr = ph->p_vaddr;
1689 /* update dynamic relocation infos */
1690 if (s->sh_type == SHT_RELX) {
1691 if (rel_size == 0)
1692 rel_addr = addr;
1693 rel_size += s->sh_size;
1695 addr += s->sh_size;
1696 if (s->sh_type != SHT_NOBITS)
1697 file_offset += s->sh_size;
1700 ph->p_filesz = file_offset - ph->p_offset;
1701 ph->p_memsz = addr - ph->p_vaddr;
1702 ph++;
1703 if (j == 0) {
1704 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1705 /* if in the middle of a page, we duplicate the page in
1706 memory so that one copy is RX and the other is RW */
1707 if ((addr & (ELF_PAGE_SIZE - 1)) != 0)
1708 addr += ELF_PAGE_SIZE;
1709 } else {
1710 addr = (addr + ELF_PAGE_SIZE - 1) & ~(ELF_PAGE_SIZE - 1);
1711 file_offset = (file_offset + ELF_PAGE_SIZE - 1) &
1712 ~(ELF_PAGE_SIZE - 1);
1717 /* if interpreter, then add corresponing program header */
1718 if (interp) {
1719 ph = &phdr[0];
1721 ph->p_type = PT_INTERP;
1722 ph->p_offset = interp->sh_offset;
1723 ph->p_vaddr = interp->sh_addr;
1724 ph->p_paddr = ph->p_vaddr;
1725 ph->p_filesz = interp->sh_size;
1726 ph->p_memsz = interp->sh_size;
1727 ph->p_flags = PF_R;
1728 ph->p_align = interp->sh_addralign;
1731 /* if dynamic section, then add corresponing program header */
1732 if (dynamic) {
1733 ElfW(Sym) *sym_end;
1735 ph = &phdr[phnum - 1];
1737 ph->p_type = PT_DYNAMIC;
1738 ph->p_offset = dynamic->sh_offset;
1739 ph->p_vaddr = dynamic->sh_addr;
1740 ph->p_paddr = ph->p_vaddr;
1741 ph->p_filesz = dynamic->sh_size;
1742 ph->p_memsz = dynamic->sh_size;
1743 ph->p_flags = PF_R | PF_W;
1744 ph->p_align = dynamic->sh_addralign;
1746 /* put GOT dynamic section address */
1747 put32(s1->got->data, dynamic->sh_addr);
1749 /* relocate the PLT */
1750 if (file_type == TCC_OUTPUT_EXE
1751 #if defined(TCC_OUTPUT_DLL_WITH_PLT)
1752 || file_type == TCC_OUTPUT_DLL
1753 #endif
1755 uint8_t *p, *p_end;
1757 p = s1->plt->data;
1758 p_end = p + s1->plt->data_offset;
1759 if (p < p_end) {
1760 #if defined(TCC_TARGET_I386)
1761 put32(p + 2, get32(p + 2) + s1->got->sh_addr);
1762 put32(p + 8, get32(p + 8) + s1->got->sh_addr);
1763 p += 16;
1764 while (p < p_end) {
1765 put32(p + 2, get32(p + 2) + s1->got->sh_addr);
1766 p += 16;
1768 #elif defined(TCC_TARGET_X86_64)
1769 int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
1770 put32(p + 2, get32(p + 2) + x);
1771 put32(p + 8, get32(p + 8) + x - 6);
1772 p += 16;
1773 while (p < p_end) {
1774 put32(p + 2, get32(p + 2) + x + s1->plt->data - p);
1775 p += 16;
1777 #elif defined(TCC_TARGET_ARM)
1778 int x;
1779 x=s1->got->sh_addr - s1->plt->sh_addr - 12;
1780 p +=16;
1781 while (p < p_end) {
1782 put32(p + 12, x + get32(p + 12) + s1->plt->data - p);
1783 p += 16;
1785 #elif defined(TCC_TARGET_C67)
1786 /* XXX: TODO */
1787 #else
1788 #error unsupported CPU
1789 #endif
1793 /* relocate symbols in .dynsym */
1794 sym_end = (ElfW(Sym) *)(s1->dynsym->data + s1->dynsym->data_offset);
1795 for(sym = (ElfW(Sym) *)s1->dynsym->data + 1;
1796 sym < sym_end;
1797 sym++) {
1798 if (sym->st_shndx == SHN_UNDEF) {
1799 /* relocate to the PLT if the symbol corresponds
1800 to a PLT entry */
1801 if (sym->st_value)
1802 sym->st_value += s1->plt->sh_addr;
1803 } else if (sym->st_shndx < SHN_LORESERVE) {
1804 /* do symbol relocation */
1805 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
1809 /* put dynamic section entries */
1810 dynamic->data_offset = saved_dynamic_data_offset;
1811 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1812 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);
1813 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1814 put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
1815 put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
1816 #ifdef TCC_TARGET_X86_64
1817 put_dt(dynamic, DT_RELA, rel_addr);
1818 put_dt(dynamic, DT_RELASZ, rel_size);
1819 put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
1820 #else
1821 put_dt(dynamic, DT_REL, rel_addr);
1822 put_dt(dynamic, DT_RELSZ, rel_size);
1823 put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
1824 #endif
1825 if (s1->do_debug)
1826 put_dt(dynamic, DT_DEBUG, 0);
1827 put_dt(dynamic, DT_NULL, 0);
1830 ehdr.e_phentsize = sizeof(ElfW(Phdr));
1831 ehdr.e_phnum = phnum;
1832 ehdr.e_phoff = sizeof(ElfW(Ehdr));
1835 /* all other sections come after */
1836 for(i = 1; i < s1->nb_sections; i++) {
1837 s = s1->sections[i];
1838 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1839 continue;
1840 section_order[sh_order_index++] = i;
1842 file_offset = (file_offset + s->sh_addralign - 1) &
1843 ~(s->sh_addralign - 1);
1844 s->sh_offset = file_offset;
1845 if (s->sh_type != SHT_NOBITS)
1846 file_offset += s->sh_size;
1849 /* if building executable or DLL, then relocate each section
1850 except the GOT which is already relocated */
1851 if (file_type != TCC_OUTPUT_OBJ) {
1852 relocate_syms(s1, 0);
1854 if (s1->nb_errors != 0) {
1855 fail:
1856 ret = -1;
1857 goto the_end;
1860 /* relocate sections */
1861 /* XXX: ignore sections with allocated relocations ? */
1862 for(i = 1; i < s1->nb_sections; i++) {
1863 s = s1->sections[i];
1864 if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
1865 relocate_section(s1, s);
1868 /* relocate relocation entries if the relocation tables are
1869 allocated in the executable */
1870 for(i = 1; i < s1->nb_sections; i++) {
1871 s = s1->sections[i];
1872 if ((s->sh_flags & SHF_ALLOC) &&
1873 s->sh_type == SHT_RELX) {
1874 relocate_rel(s1, s);
1878 /* get entry point address */
1879 if (file_type == TCC_OUTPUT_EXE)
1880 ehdr.e_entry = (unsigned long)tcc_get_symbol_err(s1, "_start");
1881 else
1882 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1885 /* write elf file */
1886 if (file_type == TCC_OUTPUT_OBJ)
1887 mode = 0666;
1888 else
1889 mode = 0777;
1890 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
1891 if (fd < 0) {
1892 error_noabort("could not write '%s'", filename);
1893 goto fail;
1895 f = fdopen(fd, "wb");
1896 if (s1->verbose)
1897 printf("<- %s\n", filename);
1899 #ifdef TCC_TARGET_COFF
1900 if (s1->output_format == TCC_OUTPUT_FORMAT_COFF) {
1901 tcc_output_coff(s1, f);
1902 } else
1903 #endif
1904 if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
1905 sort_syms(s1, symtab_section);
1907 /* align to 4 */
1908 file_offset = (file_offset + 3) & -4;
1910 /* fill header */
1911 ehdr.e_ident[0] = ELFMAG0;
1912 ehdr.e_ident[1] = ELFMAG1;
1913 ehdr.e_ident[2] = ELFMAG2;
1914 ehdr.e_ident[3] = ELFMAG3;
1915 ehdr.e_ident[4] = TCC_ELFCLASS;
1916 ehdr.e_ident[5] = ELFDATA2LSB;
1917 ehdr.e_ident[6] = EV_CURRENT;
1918 #ifdef __FreeBSD__
1919 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1920 #endif
1921 #ifdef TCC_TARGET_ARM
1922 #ifdef TCC_ARM_EABI
1923 ehdr.e_ident[EI_OSABI] = 0;
1924 ehdr.e_flags = 4 << 24;
1925 #else
1926 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1927 #endif
1928 #endif
1929 switch(file_type) {
1930 default:
1931 case TCC_OUTPUT_EXE:
1932 ehdr.e_type = ET_EXEC;
1933 break;
1934 case TCC_OUTPUT_DLL:
1935 ehdr.e_type = ET_DYN;
1936 break;
1937 case TCC_OUTPUT_OBJ:
1938 ehdr.e_type = ET_REL;
1939 break;
1941 ehdr.e_machine = EM_TCC_TARGET;
1942 ehdr.e_version = EV_CURRENT;
1943 ehdr.e_shoff = file_offset;
1944 ehdr.e_ehsize = sizeof(ElfW(Ehdr));
1945 ehdr.e_shentsize = sizeof(ElfW(Shdr));
1946 ehdr.e_shnum = shnum;
1947 ehdr.e_shstrndx = shnum - 1;
1949 fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
1950 fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
1951 offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
1953 for(i=1;i<s1->nb_sections;i++) {
1954 s = s1->sections[section_order[i]];
1955 if (s->sh_type != SHT_NOBITS) {
1956 while (offset < s->sh_offset) {
1957 fputc(0, f);
1958 offset++;
1960 size = s->sh_size;
1961 fwrite(s->data, 1, size, f);
1962 offset += size;
1966 /* output section headers */
1967 while (offset < ehdr.e_shoff) {
1968 fputc(0, f);
1969 offset++;
1972 for(i=0;i<s1->nb_sections;i++) {
1973 sh = &shdr;
1974 memset(sh, 0, sizeof(ElfW(Shdr)));
1975 s = s1->sections[i];
1976 if (s) {
1977 sh->sh_name = s->sh_name;
1978 sh->sh_type = s->sh_type;
1979 sh->sh_flags = s->sh_flags;
1980 sh->sh_entsize = s->sh_entsize;
1981 sh->sh_info = s->sh_info;
1982 if (s->link)
1983 sh->sh_link = s->link->sh_num;
1984 sh->sh_addralign = s->sh_addralign;
1985 sh->sh_addr = s->sh_addr;
1986 sh->sh_offset = s->sh_offset;
1987 sh->sh_size = s->sh_size;
1989 fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
1991 } else {
1992 tcc_output_binary(s1, f, section_order);
1994 fclose(f);
1996 ret = 0;
1997 the_end:
1998 tcc_free(s1->symtab_to_dynsym);
1999 tcc_free(section_order);
2000 tcc_free(phdr);
2001 tcc_free(s1->got_offsets);
2002 return ret;
2005 int tcc_output_file(TCCState *s, const char *filename)
2007 int ret;
2008 #ifdef TCC_TARGET_PE
2009 if (s->output_type != TCC_OUTPUT_OBJ) {
2010 ret = pe_output_file(s, filename);
2011 } else
2012 #endif
2014 ret = elf_output_file(s, filename);
2016 return ret;
2019 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
2021 void *data;
2023 data = tcc_malloc(size);
2024 lseek(fd, file_offset, SEEK_SET);
2025 read(fd, data, size);
2026 return data;
2029 typedef struct SectionMergeInfo {
2030 Section *s; /* corresponding existing section */
2031 unsigned long offset; /* offset of the new section in the existing section */
2032 uint8_t new_section; /* true if section 's' was added */
2033 uint8_t link_once; /* true if link once section */
2034 } SectionMergeInfo;
2036 /* load an object file and merge it with current files */
2037 /* XXX: handle correctly stab (debug) info */
2038 static int tcc_load_object_file(TCCState *s1,
2039 int fd, unsigned long file_offset)
2041 ElfW(Ehdr) ehdr;
2042 ElfW(Shdr) *shdr, *sh;
2043 int size, i, j, offset, offseti, nb_syms, sym_index, ret;
2044 unsigned char *strsec, *strtab;
2045 int *old_to_new_syms;
2046 char *sh_name, *name;
2047 SectionMergeInfo *sm_table, *sm;
2048 ElfW(Sym) *sym, *symtab;
2049 ElfW_Rel *rel, *rel_end;
2050 Section *s;
2052 int stab_index;
2053 int stabstr_index;
2055 stab_index = stabstr_index = 0;
2057 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
2058 goto fail1;
2059 if (ehdr.e_ident[0] != ELFMAG0 ||
2060 ehdr.e_ident[1] != ELFMAG1 ||
2061 ehdr.e_ident[2] != ELFMAG2 ||
2062 ehdr.e_ident[3] != ELFMAG3)
2063 goto fail1;
2064 /* test if object file */
2065 if (ehdr.e_type != ET_REL)
2066 goto fail1;
2067 /* test CPU specific stuff */
2068 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2069 ehdr.e_machine != EM_TCC_TARGET) {
2070 fail1:
2071 error_noabort("invalid object file");
2072 return -1;
2074 /* read sections */
2075 shdr = load_data(fd, file_offset + ehdr.e_shoff,
2076 sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2077 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
2079 /* load section names */
2080 sh = &shdr[ehdr.e_shstrndx];
2081 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2083 /* load symtab and strtab */
2084 old_to_new_syms = NULL;
2085 symtab = NULL;
2086 strtab = NULL;
2087 nb_syms = 0;
2088 for(i = 1; i < ehdr.e_shnum; i++) {
2089 sh = &shdr[i];
2090 if (sh->sh_type == SHT_SYMTAB) {
2091 if (symtab) {
2092 error_noabort("object must contain only one symtab");
2093 fail:
2094 ret = -1;
2095 goto the_end;
2097 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2098 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2099 sm_table[i].s = symtab_section;
2101 /* now load strtab */
2102 sh = &shdr[sh->sh_link];
2103 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
2107 /* now examine each section and try to merge its content with the
2108 ones in memory */
2109 for(i = 1; i < ehdr.e_shnum; i++) {
2110 /* no need to examine section name strtab */
2111 if (i == ehdr.e_shstrndx)
2112 continue;
2113 sh = &shdr[i];
2114 sh_name = strsec + sh->sh_name;
2115 /* ignore sections types we do not handle */
2116 if (sh->sh_type != SHT_PROGBITS &&
2117 sh->sh_type != SHT_RELX &&
2118 #ifdef TCC_ARM_EABI
2119 sh->sh_type != SHT_ARM_EXIDX &&
2120 #endif
2121 sh->sh_type != SHT_NOBITS &&
2122 strcmp(sh_name, ".stabstr")
2124 continue;
2125 if (sh->sh_addralign < 1)
2126 sh->sh_addralign = 1;
2127 /* find corresponding section, if any */
2128 for(j = 1; j < s1->nb_sections;j++) {
2129 s = s1->sections[j];
2130 if (!strcmp(s->name, sh_name)) {
2131 if (!strncmp(sh_name, ".gnu.linkonce",
2132 sizeof(".gnu.linkonce") - 1)) {
2133 /* if a 'linkonce' section is already present, we
2134 do not add it again. It is a little tricky as
2135 symbols can still be defined in
2136 it. */
2137 sm_table[i].link_once = 1;
2138 goto next;
2139 } else {
2140 goto found;
2144 /* not found: create new section */
2145 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags);
2146 /* take as much info as possible from the section. sh_link and
2147 sh_info will be updated later */
2148 s->sh_addralign = sh->sh_addralign;
2149 s->sh_entsize = sh->sh_entsize;
2150 sm_table[i].new_section = 1;
2151 found:
2152 if (sh->sh_type != s->sh_type) {
2153 error_noabort("invalid section type");
2154 goto fail;
2157 /* align start of section */
2158 offset = s->data_offset;
2160 if (0 == strcmp(sh_name, ".stab")) {
2161 stab_index = i;
2162 goto no_align;
2164 if (0 == strcmp(sh_name, ".stabstr")) {
2165 stabstr_index = i;
2166 goto no_align;
2169 size = sh->sh_addralign - 1;
2170 offset = (offset + size) & ~size;
2171 if (sh->sh_addralign > s->sh_addralign)
2172 s->sh_addralign = sh->sh_addralign;
2173 s->data_offset = offset;
2174 no_align:
2175 sm_table[i].offset = offset;
2176 sm_table[i].s = s;
2177 /* concatenate sections */
2178 size = sh->sh_size;
2179 if (sh->sh_type != SHT_NOBITS) {
2180 unsigned char *ptr;
2181 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
2182 ptr = section_ptr_add(s, size);
2183 read(fd, ptr, size);
2184 } else {
2185 s->data_offset += size;
2187 next: ;
2190 /* //gr relocate stab strings */
2191 if (stab_index && stabstr_index) {
2192 Stab_Sym *a, *b;
2193 unsigned o;
2194 s = sm_table[stab_index].s;
2195 a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
2196 b = (Stab_Sym *)(s->data + s->data_offset);
2197 o = sm_table[stabstr_index].offset;
2198 while (a < b)
2199 a->n_strx += o, a++;
2202 /* second short pass to update sh_link and sh_info fields of new
2203 sections */
2204 for(i = 1; i < ehdr.e_shnum; i++) {
2205 s = sm_table[i].s;
2206 if (!s || !sm_table[i].new_section)
2207 continue;
2208 sh = &shdr[i];
2209 if (sh->sh_link > 0)
2210 s->link = sm_table[sh->sh_link].s;
2211 if (sh->sh_type == SHT_RELX) {
2212 s->sh_info = sm_table[sh->sh_info].s->sh_num;
2213 /* update backward link */
2214 s1->sections[s->sh_info]->reloc = s;
2217 sm = sm_table;
2219 /* resolve symbols */
2220 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
2222 sym = symtab + 1;
2223 for(i = 1; i < nb_syms; i++, sym++) {
2224 if (sym->st_shndx != SHN_UNDEF &&
2225 sym->st_shndx < SHN_LORESERVE) {
2226 sm = &sm_table[sym->st_shndx];
2227 if (sm->link_once) {
2228 /* if a symbol is in a link once section, we use the
2229 already defined symbol. It is very important to get
2230 correct relocations */
2231 if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2232 name = strtab + sym->st_name;
2233 sym_index = find_elf_sym(symtab_section, name);
2234 if (sym_index)
2235 old_to_new_syms[i] = sym_index;
2237 continue;
2239 /* if no corresponding section added, no need to add symbol */
2240 if (!sm->s)
2241 continue;
2242 /* convert section number */
2243 sym->st_shndx = sm->s->sh_num;
2244 /* offset value */
2245 sym->st_value += sm->offset;
2247 /* add symbol */
2248 name = strtab + sym->st_name;
2249 sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size,
2250 sym->st_info, sym->st_other,
2251 sym->st_shndx, name);
2252 old_to_new_syms[i] = sym_index;
2255 /* third pass to patch relocation entries */
2256 for(i = 1; i < ehdr.e_shnum; i++) {
2257 s = sm_table[i].s;
2258 if (!s)
2259 continue;
2260 sh = &shdr[i];
2261 offset = sm_table[i].offset;
2262 switch(s->sh_type) {
2263 case SHT_RELX:
2264 /* take relocation offset information */
2265 offseti = sm_table[sh->sh_info].offset;
2266 rel_end = (ElfW_Rel *)(s->data + s->data_offset);
2267 for(rel = (ElfW_Rel *)(s->data + offset);
2268 rel < rel_end;
2269 rel++) {
2270 int type;
2271 unsigned sym_index;
2272 /* convert symbol index */
2273 type = ELFW(R_TYPE)(rel->r_info);
2274 sym_index = ELFW(R_SYM)(rel->r_info);
2275 /* NOTE: only one symtab assumed */
2276 if (sym_index >= nb_syms)
2277 goto invalid_reloc;
2278 sym_index = old_to_new_syms[sym_index];
2279 /* ignore link_once in rel section. */
2280 if (!sym_index && !sm->link_once) {
2281 invalid_reloc:
2282 error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
2283 i, strsec + sh->sh_name, rel->r_offset);
2284 goto fail;
2286 rel->r_info = ELFW(R_INFO)(sym_index, type);
2287 /* offset the relocation offset */
2288 rel->r_offset += offseti;
2290 break;
2291 default:
2292 break;
2296 ret = 0;
2297 the_end:
2298 tcc_free(symtab);
2299 tcc_free(strtab);
2300 tcc_free(old_to_new_syms);
2301 tcc_free(sm_table);
2302 tcc_free(strsec);
2303 tcc_free(shdr);
2304 return ret;
2307 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
2309 typedef struct ArchiveHeader {
2310 char ar_name[16]; /* name of this member */
2311 char ar_date[12]; /* file mtime */
2312 char ar_uid[6]; /* owner uid; printed as decimal */
2313 char ar_gid[6]; /* owner gid; printed as decimal */
2314 char ar_mode[8]; /* file mode, printed as octal */
2315 char ar_size[10]; /* file size, printed as decimal */
2316 char ar_fmag[2]; /* should contain ARFMAG */
2317 } ArchiveHeader;
2319 static int get_be32(const uint8_t *b)
2321 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
2324 /* load only the objects which resolve undefined symbols */
2325 static int tcc_load_alacarte(TCCState *s1, int fd, int size)
2327 int i, bound, nsyms, sym_index, off, ret;
2328 uint8_t *data;
2329 const char *ar_names, *p;
2330 const uint8_t *ar_index;
2331 ElfW(Sym) *sym;
2333 data = tcc_malloc(size);
2334 if (read(fd, data, size) != size)
2335 goto fail;
2336 nsyms = get_be32(data);
2337 ar_index = data + 4;
2338 ar_names = ar_index + nsyms * 4;
2340 do {
2341 bound = 0;
2342 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
2343 sym_index = find_elf_sym(symtab_section, p);
2344 if(sym_index) {
2345 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2346 if(sym->st_shndx == SHN_UNDEF) {
2347 off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);
2348 #if 0
2349 printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx);
2350 #endif
2351 ++bound;
2352 lseek(fd, off, SEEK_SET);
2353 if(tcc_load_object_file(s1, fd, off) < 0) {
2354 fail:
2355 ret = -1;
2356 goto the_end;
2361 } while(bound);
2362 ret = 0;
2363 the_end:
2364 tcc_free(data);
2365 return ret;
2368 /* load a '.a' file */
2369 static int tcc_load_archive(TCCState *s1, int fd)
2371 ArchiveHeader hdr;
2372 char ar_size[11];
2373 char ar_name[17];
2374 char magic[8];
2375 int size, len, i;
2376 unsigned long file_offset;
2378 /* skip magic which was already checked */
2379 read(fd, magic, sizeof(magic));
2381 for(;;) {
2382 len = read(fd, &hdr, sizeof(hdr));
2383 if (len == 0)
2384 break;
2385 if (len != sizeof(hdr)) {
2386 error_noabort("invalid archive");
2387 return -1;
2389 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
2390 ar_size[sizeof(hdr.ar_size)] = '\0';
2391 size = strtol(ar_size, NULL, 0);
2392 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
2393 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
2394 if (ar_name[i] != ' ')
2395 break;
2397 ar_name[i + 1] = '\0';
2398 // printf("name='%s' size=%d %s\n", ar_name, size, ar_size);
2399 file_offset = lseek(fd, 0, SEEK_CUR);
2400 /* align to even */
2401 size = (size + 1) & ~1;
2402 if (!strcmp(ar_name, "/")) {
2403 /* coff symbol table : we handle it */
2404 if(s1->alacarte_link)
2405 return tcc_load_alacarte(s1, fd, size);
2406 } else if (!strcmp(ar_name, "//") ||
2407 !strcmp(ar_name, "__.SYMDEF") ||
2408 !strcmp(ar_name, "__.SYMDEF/") ||
2409 !strcmp(ar_name, "ARFILENAMES/")) {
2410 /* skip symbol table or archive names */
2411 } else {
2412 if (tcc_load_object_file(s1, fd, file_offset) < 0)
2413 return -1;
2415 lseek(fd, file_offset + size, SEEK_SET);
2417 return 0;
2420 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
2421 is referenced by the user (so it should be added as DT_NEEDED in
2422 the generated ELF file) */
2423 static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
2425 ElfW(Ehdr) ehdr;
2426 ElfW(Shdr) *shdr, *sh, *sh1;
2427 int i, j, nb_syms, nb_dts, sym_bind, ret;
2428 ElfW(Sym) *sym, *dynsym;
2429 ElfW(Dyn) *dt, *dynamic;
2430 unsigned char *dynstr;
2431 const char *name, *soname;
2432 DLLReference *dllref;
2434 read(fd, &ehdr, sizeof(ehdr));
2436 /* test CPU specific stuff */
2437 if (ehdr.e_ident[5] != ELFDATA2LSB ||
2438 ehdr.e_machine != EM_TCC_TARGET) {
2439 error_noabort("bad architecture");
2440 return -1;
2443 /* read sections */
2444 shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
2446 /* load dynamic section and dynamic symbols */
2447 nb_syms = 0;
2448 nb_dts = 0;
2449 dynamic = NULL;
2450 dynsym = NULL; /* avoid warning */
2451 dynstr = NULL; /* avoid warning */
2452 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
2453 switch(sh->sh_type) {
2454 case SHT_DYNAMIC:
2455 nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
2456 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
2457 break;
2458 case SHT_DYNSYM:
2459 nb_syms = sh->sh_size / sizeof(ElfW(Sym));
2460 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
2461 sh1 = &shdr[sh->sh_link];
2462 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
2463 break;
2464 default:
2465 break;
2469 /* compute the real library name */
2470 soname = tcc_basename(filename);
2472 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2473 if (dt->d_tag == DT_SONAME) {
2474 soname = dynstr + dt->d_un.d_val;
2478 /* if the dll is already loaded, do not load it */
2479 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2480 dllref = s1->loaded_dlls[i];
2481 if (!strcmp(soname, dllref->name)) {
2482 /* but update level if needed */
2483 if (level < dllref->level)
2484 dllref->level = level;
2485 ret = 0;
2486 goto the_end;
2490 // printf("loading dll '%s'\n", soname);
2492 /* add the dll and its level */
2493 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
2494 dllref->level = level;
2495 strcpy(dllref->name, soname);
2496 dynarray_add((void ***)&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
2498 /* add dynamic symbols in dynsym_section */
2499 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
2500 sym_bind = ELFW(ST_BIND)(sym->st_info);
2501 if (sym_bind == STB_LOCAL)
2502 continue;
2503 name = dynstr + sym->st_name;
2504 add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2505 sym->st_info, sym->st_other, sym->st_shndx, name);
2508 /* load all referenced DLLs */
2509 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
2510 switch(dt->d_tag) {
2511 case DT_NEEDED:
2512 name = dynstr + dt->d_un.d_val;
2513 for(j = 0; j < s1->nb_loaded_dlls; j++) {
2514 dllref = s1->loaded_dlls[j];
2515 if (!strcmp(name, dllref->name))
2516 goto already_loaded;
2518 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
2519 error_noabort("referenced dll '%s' not found", name);
2520 ret = -1;
2521 goto the_end;
2523 already_loaded:
2524 break;
2527 ret = 0;
2528 the_end:
2529 tcc_free(dynstr);
2530 tcc_free(dynsym);
2531 tcc_free(dynamic);
2532 tcc_free(shdr);
2533 return ret;
2536 #define LD_TOK_NAME 256
2537 #define LD_TOK_EOF (-1)
2539 /* return next ld script token */
2540 static int ld_next(TCCState *s1, char *name, int name_size)
2542 int c;
2543 char *q;
2545 redo:
2546 switch(ch) {
2547 case ' ':
2548 case '\t':
2549 case '\f':
2550 case '\v':
2551 case '\r':
2552 case '\n':
2553 inp();
2554 goto redo;
2555 case '/':
2556 minp();
2557 if (ch == '*') {
2558 file->buf_ptr = parse_comment(file->buf_ptr);
2559 ch = file->buf_ptr[0];
2560 goto redo;
2561 } else {
2562 q = name;
2563 *q++ = '/';
2564 goto parse_name;
2566 break;
2567 /* case 'a' ... 'z': */
2568 case 'a':
2569 case 'b':
2570 case 'c':
2571 case 'd':
2572 case 'e':
2573 case 'f':
2574 case 'g':
2575 case 'h':
2576 case 'i':
2577 case 'j':
2578 case 'k':
2579 case 'l':
2580 case 'm':
2581 case 'n':
2582 case 'o':
2583 case 'p':
2584 case 'q':
2585 case 'r':
2586 case 's':
2587 case 't':
2588 case 'u':
2589 case 'v':
2590 case 'w':
2591 case 'x':
2592 case 'y':
2593 case 'z':
2594 /* case 'A' ... 'z': */
2595 case 'A':
2596 case 'B':
2597 case 'C':
2598 case 'D':
2599 case 'E':
2600 case 'F':
2601 case 'G':
2602 case 'H':
2603 case 'I':
2604 case 'J':
2605 case 'K':
2606 case 'L':
2607 case 'M':
2608 case 'N':
2609 case 'O':
2610 case 'P':
2611 case 'Q':
2612 case 'R':
2613 case 'S':
2614 case 'T':
2615 case 'U':
2616 case 'V':
2617 case 'W':
2618 case 'X':
2619 case 'Y':
2620 case 'Z':
2621 case '_':
2622 case '\\':
2623 case '.':
2624 case '$':
2625 case '~':
2626 q = name;
2627 parse_name:
2628 for(;;) {
2629 if (!((ch >= 'a' && ch <= 'z') ||
2630 (ch >= 'A' && ch <= 'Z') ||
2631 (ch >= '0' && ch <= '9') ||
2632 strchr("/.-_+=$:\\,~", ch)))
2633 break;
2634 if ((q - name) < name_size - 1) {
2635 *q++ = ch;
2637 minp();
2639 *q = '\0';
2640 c = LD_TOK_NAME;
2641 break;
2642 case CH_EOF:
2643 c = LD_TOK_EOF;
2644 break;
2645 default:
2646 c = ch;
2647 inp();
2648 break;
2650 #if 0
2651 printf("tok=%c %d\n", c, c);
2652 if (c == LD_TOK_NAME)
2653 printf(" name=%s\n", name);
2654 #endif
2655 return c;
2658 static int ld_add_file_list(TCCState *s1, int as_needed)
2660 char filename[1024];
2661 int t, ret;
2663 t = ld_next(s1, filename, sizeof(filename));
2664 if (t != '(')
2665 expect("(");
2666 t = ld_next(s1, filename, sizeof(filename));
2667 for(;;) {
2668 if (t == LD_TOK_EOF) {
2669 error_noabort("unexpected end of file");
2670 return -1;
2671 } else if (t == ')') {
2672 break;
2673 } else if (t != LD_TOK_NAME) {
2674 error_noabort("filename expected");
2675 return -1;
2677 if (!strcmp(filename, "AS_NEEDED")) {
2678 ret = ld_add_file_list(s1, 1);
2679 if (ret)
2680 return ret;
2681 } else {
2682 /* TODO: Implement AS_NEEDED support. Ignore it for now */
2683 if (!as_needed)
2684 tcc_add_file(s1, filename);
2686 t = ld_next(s1, filename, sizeof(filename));
2687 if (t == ',') {
2688 t = ld_next(s1, filename, sizeof(filename));
2691 return 0;
2694 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2695 files */
2696 static int tcc_load_ldscript(TCCState *s1)
2698 char cmd[64];
2699 char filename[1024];
2700 int t, ret;
2702 ch = file->buf_ptr[0];
2703 ch = handle_eob();
2704 for(;;) {
2705 t = ld_next(s1, cmd, sizeof(cmd));
2706 if (t == LD_TOK_EOF)
2707 return 0;
2708 else if (t != LD_TOK_NAME)
2709 return -1;
2710 if (!strcmp(cmd, "INPUT") ||
2711 !strcmp(cmd, "GROUP")) {
2712 ret = ld_add_file_list(s1, 0);
2713 if (ret)
2714 return ret;
2715 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
2716 !strcmp(cmd, "TARGET")) {
2717 /* ignore some commands */
2718 t = ld_next(s1, cmd, sizeof(cmd));
2719 if (t != '(')
2720 expect("(");
2721 for(;;) {
2722 t = ld_next(s1, filename, sizeof(filename));
2723 if (t == LD_TOK_EOF) {
2724 error_noabort("unexpected end of file");
2725 return -1;
2726 } else if (t == ')') {
2727 break;
2730 } else {
2731 return -1;
2734 return 0;