added examples
[tinycc.git] / tccelf.c
blob91a9a7e584b62e6669f22fe11d2d1fc9b754cdc5
1 /*
2 * ELF file handling for TCC
3 *
4 * Copyright (c) 2001, 2002 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 static int put_elf_str(Section *s, const char *sym)
23 int offset, len;
24 char *ptr;
26 len = strlen(sym) + 1;
27 offset = s->data_offset;
28 ptr = section_ptr_add(s, len);
29 memcpy(ptr, sym, len);
30 return offset;
33 /* elf symbol hashing function */
34 static unsigned long elf_hash(const unsigned char *name)
36 unsigned long h = 0, g;
38 while (*name) {
39 h = (h << 4) + *name++;
40 g = h & 0xf0000000;
41 if (g)
42 h ^= g >> 24;
43 h &= ~g;
45 return h;
48 /* rebuild hash table of section s */
49 /* NOTE: we do factorize the hash table code to go faster */
50 static void rebuild_hash(Section *s, unsigned int nb_buckets)
52 Elf32_Sym *sym;
53 int *ptr, *hash, nb_syms, sym_index, h;
54 char *strtab;
56 strtab = s->link->data;
57 nb_syms = s->data_offset / sizeof(Elf32_Sym);
59 s->hash->data_offset = 0;
60 ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
61 ptr[0] = nb_buckets;
62 ptr[1] = nb_syms;
63 ptr += 2;
64 hash = ptr;
65 memset(hash, 0, (nb_buckets + 1) * sizeof(int));
66 ptr += nb_buckets + 1;
68 sym = (Elf32_Sym *)s->data + 1;
69 for(sym_index = 1; sym_index < nb_syms; sym_index++) {
70 if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
71 h = elf_hash(strtab + sym->st_name) % nb_buckets;
72 *ptr = hash[h];
73 hash[h] = sym_index;
74 } else {
75 *ptr = 0;
77 ptr++;
78 sym++;
82 /* return the symbol number */
83 static int put_elf_sym(Section *s,
84 unsigned long value, unsigned long size,
85 int info, int other, int shndx, const char *name)
87 int name_offset, sym_index;
88 int nbuckets, h;
89 Elf32_Sym *sym;
90 Section *hs;
92 sym = section_ptr_add(s, sizeof(Elf32_Sym));
93 if (name)
94 name_offset = put_elf_str(s->link, name);
95 else
96 name_offset = 0;
97 /* XXX: endianness */
98 sym->st_name = name_offset;
99 sym->st_value = value;
100 sym->st_size = size;
101 sym->st_info = info;
102 sym->st_other = other;
103 sym->st_shndx = shndx;
104 sym_index = sym - (Elf32_Sym *)s->data;
105 hs = s->hash;
106 if (hs) {
107 int *ptr, *base;
108 ptr = section_ptr_add(hs, sizeof(int));
109 base = (int *)hs->data;
110 /* only add global or weak symbols */
111 if (ELF32_ST_BIND(info) != STB_LOCAL) {
112 /* add another hashing entry */
113 nbuckets = base[0];
114 h = elf_hash(name) % nbuckets;
115 *ptr = base[2 + h];
116 base[2 + h] = sym_index;
117 base[1]++;
118 /* we resize the hash table */
119 hs->nb_hashed_syms++;
120 if (hs->nb_hashed_syms > 2 * nbuckets) {
121 rebuild_hash(s, 2 * nbuckets);
123 } else {
124 *ptr = 0;
125 base[1]++;
128 return sym_index;
131 /* find global ELF symbol 'name' and return its index. Return 0 if not
132 found. */
133 static int find_elf_sym(Section *s, const char *name)
135 Elf32_Sym *sym;
136 Section *hs;
137 int nbuckets, sym_index, h;
138 const char *name1;
140 hs = s->hash;
141 if (!hs)
142 return 0;
143 nbuckets = ((int *)hs->data)[0];
144 h = elf_hash(name) % nbuckets;
145 sym_index = ((int *)hs->data)[2 + h];
146 while (sym_index != 0) {
147 sym = &((Elf32_Sym *)s->data)[sym_index];
148 name1 = s->link->data + sym->st_name;
149 if (!strcmp(name, name1))
150 return sym_index;
151 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
153 return 0;
156 /* return elf symbol value or error */
157 int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name)
159 int sym_index;
160 Elf32_Sym *sym;
162 sym_index = find_elf_sym(symtab_section, name);
163 if (!sym_index)
164 return -1;
165 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
166 *pval = sym->st_value;
167 return 0;
170 void *tcc_get_symbol_err(TCCState *s, const char *name)
172 unsigned long val;
173 if (tcc_get_symbol(s, &val, name) < 0)
174 error("%s not defined", name);
175 return (void *)val;
178 /* add an elf symbol : check if it is already defined and patch
179 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
180 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
181 int info, int sh_num, const char *name)
183 Elf32_Sym *esym;
184 int sym_bind, sym_index, sym_type, esym_bind;
186 sym_bind = ELF32_ST_BIND(info);
187 sym_type = ELF32_ST_TYPE(info);
189 if (sym_bind != STB_LOCAL) {
190 /* we search global or weak symbols */
191 sym_index = find_elf_sym(s, name);
192 if (!sym_index)
193 goto do_def;
194 esym = &((Elf32_Sym *)s->data)[sym_index];
195 if (esym->st_shndx != SHN_UNDEF) {
196 esym_bind = ELF32_ST_BIND(esym->st_info);
197 if (sh_num == SHN_UNDEF) {
198 /* ignore adding of undefined symbol if the
199 corresponding symbol is already defined */
200 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
201 /* global overrides weak, so patch */
202 goto do_patch;
203 } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
204 /* weak is ignored if already global */
205 } else {
206 #if 0
207 printf("new_bind=%d new_shndx=%d last_bind=%d old_shndx=%d\n",
208 sym_bind, sh_num, esym_bind, esym->st_shndx);
209 #endif
210 /* NOTE: we accept that two DLL define the same symbol */
211 if (s != tcc_state->dynsymtab_section)
212 error_noabort("'%s' defined twice", name);
214 } else {
215 do_patch:
216 esym->st_info = ELF32_ST_INFO(sym_bind, sym_type);
217 esym->st_shndx = sh_num;
218 esym->st_value = value;
219 esym->st_size = size;
221 } else {
222 do_def:
223 sym_index = put_elf_sym(s, value, size,
224 ELF32_ST_INFO(sym_bind, sym_type), 0,
225 sh_num, name);
227 return sym_index;
230 /* put relocation */
231 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
232 int type, int symbol)
234 char buf[256];
235 Section *sr;
236 Elf32_Rel *rel;
238 sr = s->reloc;
239 if (!sr) {
240 /* if no relocation section, create it */
241 snprintf(buf, sizeof(buf), ".rel%s", s->name);
242 /* if the symtab is allocated, then we consider the relocation
243 are also */
244 sr = new_section(tcc_state, buf, SHT_REL, symtab->sh_flags);
245 sr->sh_entsize = sizeof(Elf32_Rel);
246 sr->link = symtab;
247 sr->sh_info = s->sh_num;
248 s->reloc = sr;
250 rel = section_ptr_add(sr, sizeof(Elf32_Rel));
251 rel->r_offset = offset;
252 rel->r_info = ELF32_R_INFO(symbol, type);
255 /* put stab debug information */
257 typedef struct {
258 unsigned long n_strx; /* index into string table of name */
259 unsigned char n_type; /* type of symbol */
260 unsigned char n_other; /* misc info (usually empty) */
261 unsigned short n_desc; /* description field */
262 unsigned long n_value; /* value of symbol */
263 } Stab_Sym;
265 static void put_stabs(const char *str, int type, int other, int desc,
266 unsigned long value)
268 Stab_Sym *sym;
270 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
271 if (str) {
272 sym->n_strx = put_elf_str(stabstr_section, str);
273 } else {
274 sym->n_strx = 0;
276 sym->n_type = type;
277 sym->n_other = other;
278 sym->n_desc = desc;
279 sym->n_value = value;
282 static void put_stabs_r(const char *str, int type, int other, int desc,
283 unsigned long value, Section *sec, int sym_index)
285 put_stabs(str, type, other, desc, value);
286 put_elf_reloc(symtab_section, stab_section,
287 stab_section->data_offset - sizeof(unsigned long),
288 R_DATA_32, sym_index);
291 static void put_stabn(int type, int other, int desc, int value)
293 put_stabs(NULL, type, other, desc, value);
296 static void put_stabd(int type, int other, int desc)
298 put_stabs(NULL, type, other, desc, 0);
301 /* In an ELF file symbol table, the local symbols must appear below
302 the global and weak ones. Since TCC cannot sort it while generating
303 the code, we must do it after. All the relocation tables are also
304 modified to take into account the symbol table sorting */
305 static void sort_syms(TCCState *s1, Section *s)
307 int *old_to_new_syms;
308 Elf32_Sym *new_syms;
309 int nb_syms, i;
310 Elf32_Sym *p, *q;
311 Elf32_Rel *rel, *rel_end;
312 Section *sr;
313 int type, sym_index;
315 nb_syms = s->data_offset / sizeof(Elf32_Sym);
316 new_syms = tcc_malloc(nb_syms * sizeof(Elf32_Sym));
317 old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
319 /* first pass for local symbols */
320 p = (Elf32_Sym *)s->data;
321 q = new_syms;
322 for(i = 0; i < nb_syms; i++) {
323 if (ELF32_ST_BIND(p->st_info) == STB_LOCAL) {
324 old_to_new_syms[i] = q - new_syms;
325 *q++ = *p;
327 p++;
329 /* save the number of local symbols in section header */
330 s->sh_info = q - new_syms;
332 /* then second pass for non local symbols */
333 p = (Elf32_Sym *)s->data;
334 for(i = 0; i < nb_syms; i++) {
335 if (ELF32_ST_BIND(p->st_info) != STB_LOCAL) {
336 old_to_new_syms[i] = q - new_syms;
337 *q++ = *p;
339 p++;
342 /* we copy the new symbols to the old */
343 memcpy(s->data, new_syms, nb_syms * sizeof(Elf32_Sym));
344 tcc_free(new_syms);
346 /* now we modify all the relocations */
347 for(i = 1; i < s1->nb_sections; i++) {
348 sr = s1->sections[i];
349 if (sr->sh_type == SHT_REL && sr->link == s) {
350 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
351 for(rel = (Elf32_Rel *)sr->data;
352 rel < rel_end;
353 rel++) {
354 sym_index = ELF32_R_SYM(rel->r_info);
355 type = ELF32_R_TYPE(rel->r_info);
356 sym_index = old_to_new_syms[sym_index];
357 rel->r_info = ELF32_R_INFO(sym_index, type);
362 tcc_free(old_to_new_syms);
365 /* relocate common symbols in the .bss section */
366 static void relocate_common_syms(void)
368 Elf32_Sym *sym, *sym_end;
369 unsigned long offset, align;
371 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
372 for(sym = (Elf32_Sym *)symtab_section->data + 1;
373 sym < sym_end;
374 sym++) {
375 if (sym->st_shndx == SHN_COMMON) {
376 /* align symbol */
377 align = sym->st_value;
378 offset = bss_section->data_offset;
379 offset = (offset + align - 1) & -align;
380 sym->st_value = offset;
381 sym->st_shndx = bss_section->sh_num;
382 offset += sym->st_size;
383 bss_section->data_offset = offset;
388 static void *resolve_sym(const char *sym)
390 return dlsym(RTLD_DEFAULT, sym);
393 /* relocate symbol table, resolve undefined symbols if do_resolve is
394 true and output error if undefined symbol. */
395 static void relocate_syms(TCCState *s1, int do_resolve)
397 Elf32_Sym *sym, *esym, *sym_end;
398 int sym_bind, sh_num, sym_index;
399 const char *name;
400 unsigned long addr;
402 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
403 for(sym = (Elf32_Sym *)symtab_section->data + 1;
404 sym < sym_end;
405 sym++) {
406 sh_num = sym->st_shndx;
407 if (sh_num == SHN_UNDEF) {
408 name = strtab_section->data + sym->st_name;
409 if (do_resolve) {
410 name = symtab_section->link->data + sym->st_name;
411 addr = (unsigned long)resolve_sym(name);
412 if (addr) {
413 sym->st_value = addr;
414 goto found;
416 } else if (s1->dynsym) {
417 /* if dynamic symbol exist, then use it */
418 sym_index = find_elf_sym(s1->dynsym, name);
419 if (sym_index) {
420 esym = &((Elf32_Sym *)s1->dynsym->data)[sym_index];
421 sym->st_value = esym->st_value;
422 goto found;
425 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
426 it */
427 if (!strcmp(name, "_fp_hw"))
428 goto found;
429 /* only weak symbols are accepted to be undefined. Their
430 value is zero */
431 sym_bind = ELF32_ST_BIND(sym->st_info);
432 if (sym_bind == STB_WEAK) {
433 sym->st_value = 0;
434 } else {
435 error_noabort("undefined symbol '%s'", name);
437 } else if (sh_num < SHN_LORESERVE) {
438 /* add section base */
439 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
441 found: ;
445 /* relocate a given section (CPU dependent) */
446 static void relocate_section(TCCState *s1, Section *s)
448 Section *sr;
449 Elf32_Rel *rel, *rel_end, *qrel;
450 Elf32_Sym *sym;
451 int type, sym_index, esym_index;
452 unsigned char *ptr;
453 unsigned long val, addr;
455 sr = s->reloc;
456 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
457 qrel = (Elf32_Rel *)sr->data;
458 for(rel = qrel;
459 rel < rel_end;
460 rel++) {
461 ptr = s->data + rel->r_offset;
463 sym_index = ELF32_R_SYM(rel->r_info);
464 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
465 val = sym->st_value;
466 type = ELF32_R_TYPE(rel->r_info);
467 addr = s->sh_addr + rel->r_offset;
469 /* CPU specific */
470 switch(type) {
471 case R_386_32:
472 if (s1->output_type == TCC_OUTPUT_DLL) {
473 esym_index = s1->symtab_to_dynsym[sym_index];
474 qrel->r_offset = rel->r_offset;
475 if (esym_index) {
476 qrel->r_info = ELF32_R_INFO(esym_index, R_386_32);
477 qrel++;
478 break;
479 } else {
480 qrel->r_info = ELF32_R_INFO(0, R_386_RELATIVE);
481 qrel++;
484 *(int *)ptr += val;
485 break;
486 case R_386_PC32:
487 if (s1->output_type == TCC_OUTPUT_DLL) {
488 /* DLL relocation */
489 esym_index = s1->symtab_to_dynsym[sym_index];
490 if (esym_index) {
491 qrel->r_offset = rel->r_offset;
492 qrel->r_info = ELF32_R_INFO(esym_index, R_386_PC32);
493 qrel++;
494 break;
497 *(int *)ptr += val - addr;
498 break;
499 case R_386_PLT32:
500 *(int *)ptr += val - addr;
501 break;
502 case R_386_GLOB_DAT:
503 case R_386_JMP_SLOT:
504 *(int *)ptr = val;
505 break;
506 case R_386_GOTPC:
507 *(int *)ptr += s1->got->sh_addr - addr;
508 break;
509 case R_386_GOTOFF:
510 *(int *)ptr += val - s1->got->sh_addr;
511 break;
512 case R_386_GOT32:
513 /* we load the got offset */
514 *(int *)ptr += s1->got_offsets[sym_index];
515 break;
518 /* if the relocation is allocated, we change its symbol table */
519 if (sr->sh_flags & SHF_ALLOC)
520 sr->link = s1->dynsym;
523 /* relocate relocation table in 'sr' */
524 static void relocate_rel(TCCState *s1, Section *sr)
526 Section *s;
527 Elf32_Rel *rel, *rel_end;
529 s = s1->sections[sr->sh_info];
530 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
531 for(rel = (Elf32_Rel *)sr->data;
532 rel < rel_end;
533 rel++) {
534 rel->r_offset += s->sh_addr;
538 /* count the number of dynamic relocations so that we can reserve
539 their space */
540 static int prepare_dynamic_rel(TCCState *s1, Section *sr)
542 Elf32_Rel *rel, *rel_end;
543 int sym_index, esym_index, type, count;
545 count = 0;
546 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
547 for(rel = (Elf32_Rel *)sr->data; rel < rel_end; rel++) {
548 sym_index = ELF32_R_SYM(rel->r_info);
549 type = ELF32_R_TYPE(rel->r_info);
550 switch(type) {
551 case R_386_32:
552 count++;
553 break;
554 case R_386_PC32:
555 esym_index = s1->symtab_to_dynsym[sym_index];
556 if (esym_index)
557 count++;
558 break;
559 default:
560 break;
563 if (count) {
564 /* allocate the section */
565 sr->sh_flags |= SHF_ALLOC;
566 sr->sh_size = count * sizeof(Elf32_Rel);
568 return count;
571 static void put_got_offset(TCCState *s1, int index, unsigned long val)
573 int n;
574 unsigned long *tab;
576 if (index >= s1->nb_got_offsets) {
577 /* find immediately bigger power of 2 and reallocate array */
578 n = 1;
579 while (index >= n)
580 n *= 2;
581 tab = tcc_realloc(s1->got_offsets, n * sizeof(unsigned long));
582 if (!tab)
583 error("memory full");
584 s1->got_offsets = tab;
585 memset(s1->got_offsets + s1->nb_got_offsets, 0,
586 (n - s1->nb_got_offsets) * sizeof(unsigned long));
587 s1->nb_got_offsets = n;
589 s1->got_offsets[index] = val;
592 /* XXX: suppress that */
593 static void put32(unsigned char *p, uint32_t val)
595 p[0] = val;
596 p[1] = val >> 8;
597 p[2] = val >> 16;
598 p[3] = val >> 24;
601 static uint32_t get32(unsigned char *p)
603 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
606 static void build_got(TCCState *s1)
608 unsigned char *ptr;
610 /* if no got, then create it */
611 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
612 s1->got->sh_entsize = 4;
613 add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
614 s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
615 ptr = section_ptr_add(s1->got, 3 * sizeof(int));
616 /* keep space for _DYNAMIC pointer, if present */
617 put32(ptr, 0);
618 /* two dummy got entries */
619 put32(ptr + 4, 0);
620 put32(ptr + 8, 0);
623 /* put a got entry corresponding to a symbol in symtab_section. 'size'
624 and 'info' can be modifed if more precise info comes from the DLL */
625 static void put_got_entry(TCCState *s1,
626 int reloc_type, unsigned long size, int info,
627 int sym_index)
629 int index;
630 const char *name;
631 Elf32_Sym *sym;
632 unsigned long offset;
633 int *ptr;
635 if (!s1->got)
636 build_got(s1);
638 /* if a got entry already exists for that symbol, no need to add one */
639 if (sym_index < s1->nb_got_offsets &&
640 s1->got_offsets[sym_index] != 0)
641 return;
643 put_got_offset(s1, sym_index, s1->got->data_offset);
645 if (s1->dynsym) {
646 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
647 name = symtab_section->link->data + sym->st_name;
648 offset = sym->st_value;
649 if (reloc_type == R_386_JMP_SLOT) {
650 Section *plt;
651 uint8_t *p;
652 int modrm;
654 /* if we build a DLL, we add a %ebx offset */
655 if (s1->output_type == TCC_OUTPUT_DLL)
656 modrm = 0xa3;
657 else
658 modrm = 0x25;
660 /* add a PLT entry */
661 plt = s1->plt;
662 if (plt->data_offset == 0) {
663 /* first plt entry */
664 p = section_ptr_add(plt, 16);
665 p[0] = 0xff; /* pushl got + 4 */
666 p[1] = modrm + 0x10;
667 put32(p + 2, 4);
668 p[6] = 0xff; /* jmp *(got + 8) */
669 p[7] = modrm;
670 put32(p + 8, 8);
673 p = section_ptr_add(plt, 16);
674 p[0] = 0xff; /* jmp *(got + x) */
675 p[1] = modrm;
676 put32(p + 2, s1->got->data_offset);
677 p[6] = 0x68; /* push $xxx */
678 put32(p + 7, (plt->data_offset - 32) >> 1);
679 p[11] = 0xe9; /* jmp plt_start */
680 put32(p + 12, -(plt->data_offset));
682 /* the symbol is modified so that it will be relocated to
683 the PLT */
684 if (s1->output_type == TCC_OUTPUT_EXE)
685 offset = plt->data_offset - 16;
687 index = put_elf_sym(s1->dynsym, offset,
688 size, info, 0, sym->st_shndx, name);
689 /* put a got entry */
690 put_elf_reloc(s1->dynsym, s1->got,
691 s1->got->data_offset,
692 reloc_type, index);
694 ptr = section_ptr_add(s1->got, sizeof(int));
695 *ptr = 0;
698 /* build GOT and PLT entries */
699 static void build_got_entries(TCCState *s1)
701 Section *s, *symtab;
702 Elf32_Rel *rel, *rel_end;
703 Elf32_Sym *sym;
704 int i, type, reloc_type, sym_index;
706 for(i = 1; i < s1->nb_sections; i++) {
707 s = s1->sections[i];
708 if (s->sh_type != SHT_REL)
709 continue;
710 /* no need to handle got relocations */
711 if (s->link != symtab_section)
712 continue;
713 symtab = s->link;
714 rel_end = (Elf32_Rel *)(s->data + s->data_offset);
715 for(rel = (Elf32_Rel *)s->data;
716 rel < rel_end;
717 rel++) {
718 type = ELF32_R_TYPE(rel->r_info);
719 switch(type) {
720 case R_386_GOT32:
721 case R_386_GOTOFF:
722 case R_386_GOTPC:
723 case R_386_PLT32:
724 if (!s1->got)
725 build_got(s1);
726 if (type == R_386_GOT32 || type == R_386_PLT32) {
727 sym_index = ELF32_R_SYM(rel->r_info);
728 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
729 /* look at the symbol got offset. If none, then add one */
730 if (type == R_386_GOT32)
731 reloc_type = R_386_GLOB_DAT;
732 else
733 reloc_type = R_386_JMP_SLOT;
734 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
735 sym_index);
737 break;
738 default:
739 break;
745 static Section *new_symtab(TCCState *s1,
746 const char *symtab_name, int sh_type, int sh_flags,
747 const char *strtab_name,
748 const char *hash_name, int hash_sh_flags)
750 Section *symtab, *strtab, *hash;
751 int *ptr, nb_buckets;
753 symtab = new_section(s1, symtab_name, sh_type, sh_flags);
754 symtab->sh_entsize = sizeof(Elf32_Sym);
755 strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
756 put_elf_str(strtab, "");
757 symtab->link = strtab;
758 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
760 nb_buckets = 1;
762 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
763 hash->sh_entsize = sizeof(int);
764 symtab->hash = hash;
765 hash->link = symtab;
767 ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
768 ptr[0] = nb_buckets;
769 ptr[1] = 1;
770 memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
771 return symtab;
774 /* put dynamic tag */
775 static void put_dt(Section *dynamic, int dt, unsigned long val)
777 Elf32_Dyn *dyn;
778 dyn = section_ptr_add(dynamic, sizeof(Elf32_Dyn));
779 dyn->d_tag = dt;
780 dyn->d_un.d_val = val;
783 static void add_init_array_defines(TCCState *s1, const char *section_name)
785 Section *s;
786 long end_offset;
787 char sym_start[1024];
788 char sym_end[1024];
790 snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
791 snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
793 s = find_section(s1, section_name);
794 if (!s) {
795 end_offset = 0;
796 s = data_section;
797 } else {
798 end_offset = s->data_offset;
801 add_elf_sym(symtab_section,
802 0, 0,
803 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
804 s->sh_num, sym_start);
805 add_elf_sym(symtab_section,
806 end_offset, 0,
807 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
808 s->sh_num, sym_end);
811 /* add tcc runtime libraries */
812 static void tcc_add_runtime(TCCState *s1)
814 char buf[1024];
815 int i;
816 Section *s;
818 if (!s1->nostdlib) {
819 snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "libtcc1.a");
820 tcc_add_file(s1, buf);
822 #ifdef CONFIG_TCC_BCHECK
823 if (do_bounds_check) {
824 unsigned long *ptr;
825 Section *init_section;
826 unsigned char *pinit;
827 int sym_index;
829 /* XXX: add an object file to do that */
830 ptr = section_ptr_add(bounds_section, sizeof(unsigned long));
831 *ptr = 0;
832 add_elf_sym(symtab_section, 0, 0,
833 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
834 bounds_section->sh_num, "__bounds_start");
835 /* add bound check code */
836 snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o");
837 tcc_add_file(s1, buf);
838 #ifdef TCC_TARGET_I386
839 if (s1->output_type != TCC_OUTPUT_MEMORY) {
840 /* add 'call __bound_init()' in .init section */
841 init_section = find_section(s1, ".init");
842 pinit = section_ptr_add(init_section, 5);
843 pinit[0] = 0xe8;
844 put32(pinit + 1, -4);
845 sym_index = find_elf_sym(symtab_section, "__bound_init");
846 put_elf_reloc(symtab_section, init_section,
847 init_section->data_offset - 4, R_386_PC32, sym_index);
849 #endif
851 #endif
852 /* add libc */
853 if (!s1->nostdlib) {
854 tcc_add_library(s1, "c");
856 /* add crt end if not memory output */
857 if (s1->output_type != TCC_OUTPUT_MEMORY && !s1->nostdlib) {
858 tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o");
860 /* add various standard linker symbols */
861 add_elf_sym(symtab_section,
862 text_section->data_offset, 0,
863 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
864 text_section->sh_num, "_etext");
865 add_elf_sym(symtab_section,
866 data_section->data_offset, 0,
867 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
868 data_section->sh_num, "_edata");
869 add_elf_sym(symtab_section,
870 bss_section->data_offset, 0,
871 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
872 bss_section->sh_num, "_end");
873 /* horrible new standard ldscript defines */
874 add_init_array_defines(s1, ".preinit_array");
875 add_init_array_defines(s1, ".init_array");
876 add_init_array_defines(s1, ".fini_array");
878 /* add start and stop symbols for sections whose name can be
879 expressed in C */
880 for(i = 1; i < s1->nb_sections; i++) {
881 s = s1->sections[i];
882 if (s->sh_type == SHT_PROGBITS &&
883 (s->sh_flags & SHF_ALLOC)) {
884 const char *p;
885 int ch;
887 /* check if section name can be expressed in C */
888 p = s->name;
889 for(;;) {
890 ch = *p;
891 if (!ch)
892 break;
893 if (!isid(ch) && !isnum(ch))
894 goto next_sec;
895 p++;
897 snprintf(buf, sizeof(buf), "__start_%s", s->name);
898 add_elf_sym(symtab_section,
899 0, 0,
900 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
901 s->sh_num, buf);
902 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
903 add_elf_sym(symtab_section,
904 s->data_offset, 0,
905 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
906 s->sh_num, buf);
908 next_sec: ;
912 /* name of ELF interpreter */
913 #ifdef __FreeBSD__
914 static char elf_interp[] = "/usr/libexec/ld-elf.so.1";
915 #else
916 static char elf_interp[] = "/lib/ld-linux.so.2";
917 #endif
919 #define ELF_START_ADDR 0x08048000
920 #define ELF_PAGE_SIZE 0x1000
922 /* output an ELF file */
923 /* XXX: suppress unneeded sections */
924 int tcc_output_file(TCCState *s1, const char *filename)
926 Elf32_Ehdr ehdr;
927 FILE *f;
928 int fd, mode, ret;
929 int *section_order;
930 int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k;
931 unsigned long addr;
932 Section *strsec, *s;
933 Elf32_Shdr shdr, *sh;
934 Elf32_Phdr *phdr, *ph;
935 Section *interp, *dynamic, *dynstr;
936 unsigned long saved_dynamic_data_offset;
937 Elf32_Sym *sym;
938 int type, file_type;
939 unsigned long rel_addr, rel_size;
941 file_type = s1->output_type;
942 s1->nb_errors = 0;
944 if (file_type != TCC_OUTPUT_OBJ)
945 tcc_add_runtime(s1);
947 phdr = NULL;
948 section_order = NULL;
949 interp = NULL;
950 dynamic = NULL;
951 dynstr = NULL; /* avoid warning */
952 saved_dynamic_data_offset = 0; /* avoid warning */
954 if (file_type != TCC_OUTPUT_OBJ) {
956 relocate_common_syms();
958 if (!s1->static_link) {
959 const char *name;
960 int sym_index, index;
961 Elf32_Sym *esym, *sym_end;
963 if (file_type == TCC_OUTPUT_EXE) {
964 char *ptr;
965 /* add interpreter section only if executable */
966 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
967 interp->sh_addralign = 1;
968 ptr = section_ptr_add(interp, sizeof(elf_interp));
969 strcpy(ptr, elf_interp);
972 /* add dynamic symbol table */
973 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
974 ".dynstr",
975 ".hash", SHF_ALLOC);
976 dynstr = s1->dynsym->link;
978 /* add dynamic section */
979 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
980 SHF_ALLOC | SHF_WRITE);
981 dynamic->link = dynstr;
982 dynamic->sh_entsize = sizeof(Elf32_Dyn);
984 /* add PLT */
985 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
986 SHF_ALLOC | SHF_EXECINSTR);
987 s1->plt->sh_entsize = 4;
989 build_got(s1);
991 /* scan for undefined symbols and see if they are in the
992 dynamic symbols. If a symbol STT_FUNC is found, then we
993 add it in the PLT. If a symbol STT_OBJECT is found, we
994 add it in the .bss section with a suitable relocation */
995 sym_end = (Elf32_Sym *)(symtab_section->data +
996 symtab_section->data_offset);
997 if (file_type == TCC_OUTPUT_EXE) {
998 for(sym = (Elf32_Sym *)symtab_section->data + 1;
999 sym < sym_end;
1000 sym++) {
1001 if (sym->st_shndx == SHN_UNDEF) {
1002 name = symtab_section->link->data + sym->st_name;
1003 sym_index = find_elf_sym(s1->dynsymtab_section, name);
1004 if (sym_index) {
1005 esym = &((Elf32_Sym *)s1->dynsymtab_section->data)[sym_index];
1006 type = ELF32_ST_TYPE(esym->st_info);
1007 if (type == STT_FUNC) {
1008 put_got_entry(s1, R_386_JMP_SLOT, esym->st_size,
1009 esym->st_info,
1010 sym - (Elf32_Sym *)symtab_section->data);
1011 } else if (type == STT_OBJECT) {
1012 unsigned long offset;
1013 offset = bss_section->data_offset;
1014 /* XXX: which alignment ? */
1015 offset = (offset + 16 - 1) & -16;
1016 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
1017 esym->st_info, 0,
1018 bss_section->sh_num, name);
1019 put_elf_reloc(s1->dynsym, bss_section,
1020 offset, R_386_COPY, index);
1021 offset += esym->st_size;
1022 bss_section->data_offset = offset;
1024 } else {
1025 /* STB_WEAK undefined symbols are accepted */
1026 /* XXX: _fp_hw seems to be part of the ABI, so we ignore
1027 it */
1028 if (ELF32_ST_BIND(sym->st_info) == STB_WEAK ||
1029 !strcmp(name, "_fp_hw")) {
1030 } else {
1031 error_noabort("undefined symbol '%s'", name);
1034 } else if (s1->rdynamic &&
1035 ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
1036 /* if -rdynamic option, then export all non
1037 local symbols */
1038 name = symtab_section->link->data + sym->st_name;
1039 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1040 sym->st_info, 0,
1041 sym->st_shndx, name);
1045 if (s1->nb_errors)
1046 goto fail;
1048 /* now look at unresolved dynamic symbols and export
1049 corresponding symbol */
1050 sym_end = (Elf32_Sym *)(s1->dynsymtab_section->data +
1051 s1->dynsymtab_section->data_offset);
1052 for(esym = (Elf32_Sym *)s1->dynsymtab_section->data + 1;
1053 esym < sym_end;
1054 esym++) {
1055 if (esym->st_shndx == SHN_UNDEF) {
1056 name = s1->dynsymtab_section->link->data + esym->st_name;
1057 sym_index = find_elf_sym(symtab_section, name);
1058 if (sym_index) {
1059 /* XXX: avoid adding a symbol if already
1060 present because of -rdynamic ? */
1061 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
1062 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1063 sym->st_info, 0,
1064 sym->st_shndx, name);
1065 } else {
1066 if (ELF32_ST_BIND(esym->st_info) == STB_WEAK) {
1067 /* weak symbols can stay undefined */
1068 } else {
1069 warning("undefined dynamic symbol '%s'", name);
1074 } else {
1075 int nb_syms;
1076 /* shared library case : we simply export all the global symbols */
1077 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
1078 s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms);
1079 for(sym = (Elf32_Sym *)symtab_section->data + 1;
1080 sym < sym_end;
1081 sym++) {
1082 if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
1083 name = symtab_section->link->data + sym->st_name;
1084 index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
1085 sym->st_info, 0,
1086 sym->st_shndx, name);
1087 s1->symtab_to_dynsym[sym -
1088 (Elf32_Sym *)symtab_section->data] =
1089 index;
1094 build_got_entries(s1);
1096 /* add a list of needed dlls */
1097 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1098 DLLReference *dllref = s1->loaded_dlls[i];
1099 if (dllref->level == 0)
1100 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
1102 /* XXX: currently, since we do not handle PIC code, we
1103 must relocate the readonly segments */
1104 if (file_type == TCC_OUTPUT_DLL)
1105 put_dt(dynamic, DT_TEXTREL, 0);
1107 /* add necessary space for other entries */
1108 saved_dynamic_data_offset = dynamic->data_offset;
1109 dynamic->data_offset += 8 * 9;
1110 } else {
1111 /* still need to build got entries in case of static link */
1112 build_got_entries(s1);
1116 memset(&ehdr, 0, sizeof(ehdr));
1118 /* we add a section for symbols */
1119 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
1120 put_elf_str(strsec, "");
1122 /* compute number of sections */
1123 shnum = s1->nb_sections;
1125 /* this array is used to reorder sections in the output file */
1126 section_order = tcc_malloc(sizeof(int) * shnum);
1127 section_order[0] = 0;
1128 sh_order_index = 1;
1130 /* compute number of program headers */
1131 switch(file_type) {
1132 default:
1133 case TCC_OUTPUT_OBJ:
1134 phnum = 0;
1135 break;
1136 case TCC_OUTPUT_EXE:
1137 if (!s1->static_link)
1138 phnum = 4;
1139 else
1140 phnum = 2;
1141 break;
1142 case TCC_OUTPUT_DLL:
1143 phnum = 3;
1144 break;
1147 /* allocate strings for section names and decide if an unallocated
1148 section should be output */
1149 /* NOTE: the strsec section comes last, so its size is also
1150 correct ! */
1151 for(i = 1; i < s1->nb_sections; i++) {
1152 s = s1->sections[i];
1153 s->sh_name = put_elf_str(strsec, s->name);
1154 /* when generating a DLL, we include relocations but we may
1155 patch them */
1156 if (file_type == TCC_OUTPUT_DLL &&
1157 s->sh_type == SHT_REL &&
1158 !(s->sh_flags & SHF_ALLOC)) {
1159 prepare_dynamic_rel(s1, s);
1160 } else if (do_debug ||
1161 file_type == TCC_OUTPUT_OBJ ||
1162 (s->sh_flags & SHF_ALLOC) ||
1163 i == (s1->nb_sections - 1)) {
1164 /* we output all sections if debug or object file */
1165 s->sh_size = s->data_offset;
1169 /* allocate program segment headers */
1170 phdr = tcc_mallocz(phnum * sizeof(Elf32_Phdr));
1172 file_offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr);
1173 if (phnum > 0) {
1174 /* compute section to program header mapping */
1175 if (file_type == TCC_OUTPUT_DLL)
1176 addr = 0;
1177 else
1178 addr = ELF_START_ADDR;
1180 /* dynamic relocation table information, for .dynamic section */
1181 rel_size = 0;
1182 rel_addr = 0;
1184 /* compute address after headers */
1185 addr += (file_offset & (ELF_PAGE_SIZE - 1));
1187 /* leave one program header for the program interpreter */
1188 ph = &phdr[0];
1189 if (interp)
1190 ph++;
1192 for(j = 0; j < 2; j++) {
1193 ph->p_type = PT_LOAD;
1194 if (j == 0)
1195 ph->p_flags = PF_R | PF_X;
1196 else
1197 ph->p_flags = PF_R | PF_W;
1198 ph->p_align = ELF_PAGE_SIZE;
1200 /* we do the following ordering: interp, symbol tables,
1201 relocations, progbits, nobits */
1202 /* XXX: do faster and simpler sorting */
1203 for(k = 0; k < 5; k++) {
1204 for(i = 1; i < s1->nb_sections; i++) {
1205 s = s1->sections[i];
1206 /* compute if section should be included */
1207 if (j == 0) {
1208 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1209 SHF_ALLOC)
1210 continue;
1211 } else {
1212 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
1213 (SHF_ALLOC | SHF_WRITE))
1214 continue;
1216 if (s == interp) {
1217 if (k != 0)
1218 continue;
1219 } else if (s->sh_type == SHT_DYNSYM ||
1220 s->sh_type == SHT_STRTAB ||
1221 s->sh_type == SHT_HASH) {
1222 if (k != 1)
1223 continue;
1224 } else if (s->sh_type == SHT_REL) {
1225 if (k != 2)
1226 continue;
1227 } else if (s->sh_type == SHT_NOBITS) {
1228 if (k != 4)
1229 continue;
1230 } else {
1231 if (k != 3)
1232 continue;
1234 section_order[sh_order_index++] = i;
1236 /* section matches: we align it and add its size */
1237 tmp = file_offset;
1238 file_offset = (file_offset + s->sh_addralign - 1) &
1239 ~(s->sh_addralign - 1);
1240 s->sh_offset = file_offset;
1241 addr += file_offset - tmp;
1242 s->sh_addr = addr;
1244 /* update program header infos */
1245 if (ph->p_offset == 0) {
1246 ph->p_offset = file_offset;
1247 ph->p_vaddr = addr;
1248 ph->p_paddr = ph->p_vaddr;
1250 /* update dynamic relocation infos */
1251 if (s->sh_type == SHT_REL) {
1252 if (rel_size == 0)
1253 rel_addr = addr;
1254 rel_size += s->sh_size;
1256 addr += s->sh_size;
1257 if (s->sh_type != SHT_NOBITS)
1258 file_offset += s->sh_size;
1261 ph->p_filesz = file_offset - ph->p_offset;
1262 ph->p_memsz = addr - ph->p_vaddr;
1263 ph++;
1264 /* if in the middle of a page, we duplicate the page in
1265 memory so that one copy is RX and the other is RW */
1266 if ((addr & (ELF_PAGE_SIZE - 1)) != 0)
1267 addr += ELF_PAGE_SIZE;
1270 /* if interpreter, then add corresponing program header */
1271 if (interp) {
1272 ph = &phdr[0];
1274 ph->p_type = PT_INTERP;
1275 ph->p_offset = interp->sh_offset;
1276 ph->p_vaddr = interp->sh_addr;
1277 ph->p_paddr = ph->p_vaddr;
1278 ph->p_filesz = interp->sh_size;
1279 ph->p_memsz = interp->sh_size;
1280 ph->p_flags = PF_R;
1281 ph->p_align = interp->sh_addralign;
1284 /* if dynamic section, then add corresponing program header */
1285 if (dynamic) {
1286 Elf32_Sym *sym_end;
1288 ph = &phdr[phnum - 1];
1290 ph->p_type = PT_DYNAMIC;
1291 ph->p_offset = dynamic->sh_offset;
1292 ph->p_vaddr = dynamic->sh_addr;
1293 ph->p_paddr = ph->p_vaddr;
1294 ph->p_filesz = dynamic->sh_size;
1295 ph->p_memsz = dynamic->sh_size;
1296 ph->p_flags = PF_R | PF_W;
1297 ph->p_align = dynamic->sh_addralign;
1299 /* put GOT dynamic section address */
1300 put32(s1->got->data, dynamic->sh_addr);
1302 /* relocate the PLT */
1303 if (file_type == TCC_OUTPUT_EXE) {
1304 uint8_t *p, *p_end;
1306 p = s1->plt->data;
1307 p_end = p + s1->plt->data_offset;
1308 if (p < p_end) {
1309 put32(p + 2, get32(p + 2) + s1->got->sh_addr);
1310 put32(p + 8, get32(p + 8) + s1->got->sh_addr);
1311 p += 16;
1312 while (p < p_end) {
1313 put32(p + 2, get32(p + 2) + s1->got->sh_addr);
1314 p += 16;
1319 /* relocate symbols in .dynsym */
1320 sym_end = (Elf32_Sym *)(s1->dynsym->data + s1->dynsym->data_offset);
1321 for(sym = (Elf32_Sym *)s1->dynsym->data + 1;
1322 sym < sym_end;
1323 sym++) {
1324 if (sym->st_shndx == SHN_UNDEF) {
1325 /* relocate to the PLT if the symbol corresponds
1326 to a PLT entry */
1327 if (sym->st_value)
1328 sym->st_value += s1->plt->sh_addr;
1329 } else if (sym->st_shndx < SHN_LORESERVE) {
1330 /* do symbol relocation */
1331 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
1335 /* put dynamic section entries */
1336 dynamic->data_offset = saved_dynamic_data_offset;
1337 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1338 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);
1339 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1340 put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
1341 put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym));
1342 put_dt(dynamic, DT_REL, rel_addr);
1343 put_dt(dynamic, DT_RELSZ, rel_size);
1344 put_dt(dynamic, DT_RELENT, sizeof(Elf32_Rel));
1345 put_dt(dynamic, DT_NULL, 0);
1348 ehdr.e_phentsize = sizeof(Elf32_Phdr);
1349 ehdr.e_phnum = phnum;
1350 ehdr.e_phoff = sizeof(Elf32_Ehdr);
1353 /* all other sections come after */
1354 for(i = 1; i < s1->nb_sections; i++) {
1355 s = s1->sections[i];
1356 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1357 continue;
1358 section_order[sh_order_index++] = i;
1360 file_offset = (file_offset + s->sh_addralign - 1) &
1361 ~(s->sh_addralign - 1);
1362 s->sh_offset = file_offset;
1363 if (s->sh_type != SHT_NOBITS)
1364 file_offset += s->sh_size;
1367 /* if building executable or DLL, then relocate each section
1368 except the GOT which is already relocated */
1369 if (file_type != TCC_OUTPUT_OBJ) {
1370 relocate_syms(s1, 0);
1372 if (s1->nb_errors != 0) {
1373 fail:
1374 ret = -1;
1375 goto the_end;
1378 /* relocate sections */
1379 /* XXX: ignore sections with allocated relocations ? */
1380 for(i = 1; i < s1->nb_sections; i++) {
1381 s = s1->sections[i];
1382 if (s->reloc && s != s1->got)
1383 relocate_section(s1, s);
1386 /* relocate relocation entries if the relocation tables are
1387 allocated in the executable */
1388 for(i = 1; i < s1->nb_sections; i++) {
1389 s = s1->sections[i];
1390 if ((s->sh_flags & SHF_ALLOC) &&
1391 s->sh_type == SHT_REL) {
1392 relocate_rel(s1, s);
1396 /* get entry point address */
1397 if (file_type == TCC_OUTPUT_EXE)
1398 ehdr.e_entry = (unsigned long)tcc_get_symbol_err(s1, "_start");
1399 else
1400 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1403 sort_syms(s1, symtab_section);
1405 /* align to 4 */
1406 file_offset = (file_offset + 3) & -4;
1408 /* fill header */
1409 ehdr.e_ident[0] = ELFMAG0;
1410 ehdr.e_ident[1] = ELFMAG1;
1411 ehdr.e_ident[2] = ELFMAG2;
1412 ehdr.e_ident[3] = ELFMAG3;
1413 ehdr.e_ident[4] = ELFCLASS32;
1414 ehdr.e_ident[5] = ELFDATA2LSB;
1415 ehdr.e_ident[6] = EV_CURRENT;
1416 #ifdef __FreeBSD__
1417 ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1418 #endif
1419 switch(file_type) {
1420 default:
1421 case TCC_OUTPUT_EXE:
1422 ehdr.e_type = ET_EXEC;
1423 break;
1424 case TCC_OUTPUT_DLL:
1425 ehdr.e_type = ET_DYN;
1426 break;
1427 case TCC_OUTPUT_OBJ:
1428 ehdr.e_type = ET_REL;
1429 break;
1431 ehdr.e_machine = EM_386;
1432 ehdr.e_version = EV_CURRENT;
1433 ehdr.e_shoff = file_offset;
1434 ehdr.e_ehsize = sizeof(Elf32_Ehdr);
1435 ehdr.e_shentsize = sizeof(Elf32_Shdr);
1436 ehdr.e_shnum = shnum;
1437 ehdr.e_shstrndx = shnum - 1;
1439 /* write elf file */
1440 if (file_type == TCC_OUTPUT_OBJ)
1441 mode = 0666;
1442 else
1443 mode = 0777;
1444 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
1445 if (fd < 0) {
1446 error_noabort("could not write '%s'", filename);
1447 goto fail;
1449 f = fdopen(fd, "w");
1450 fwrite(&ehdr, 1, sizeof(Elf32_Ehdr), f);
1451 fwrite(phdr, 1, phnum * sizeof(Elf32_Phdr), f);
1452 offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr);
1453 for(i=1;i<s1->nb_sections;i++) {
1454 s = s1->sections[section_order[i]];
1455 if (s->sh_type != SHT_NOBITS) {
1456 while (offset < s->sh_offset) {
1457 fputc(0, f);
1458 offset++;
1460 size = s->sh_size;
1461 fwrite(s->data, 1, size, f);
1462 offset += size;
1465 while (offset < ehdr.e_shoff) {
1466 fputc(0, f);
1467 offset++;
1470 /* output section headers */
1471 for(i=0;i<s1->nb_sections;i++) {
1472 sh = &shdr;
1473 memset(sh, 0, sizeof(Elf32_Shdr));
1474 s = s1->sections[i];
1475 if (s) {
1476 sh->sh_name = s->sh_name;
1477 sh->sh_type = s->sh_type;
1478 sh->sh_flags = s->sh_flags;
1479 sh->sh_entsize = s->sh_entsize;
1480 sh->sh_info = s->sh_info;
1481 if (s->link)
1482 sh->sh_link = s->link->sh_num;
1483 sh->sh_addralign = s->sh_addralign;
1484 sh->sh_addr = s->sh_addr;
1485 sh->sh_offset = s->sh_offset;
1486 sh->sh_size = s->sh_size;
1488 fwrite(sh, 1, sizeof(Elf32_Shdr), f);
1490 fclose(f);
1492 ret = 0;
1493 the_end:
1494 tcc_free(s1->symtab_to_dynsym);
1495 tcc_free(section_order);
1496 tcc_free(phdr);
1497 tcc_free(s1->got_offsets);
1498 return ret;
1501 static void *load_data(int fd, unsigned long file_offset, unsigned long size)
1503 void *data;
1505 data = tcc_malloc(size);
1506 lseek(fd, file_offset, SEEK_SET);
1507 read(fd, data, size);
1508 return data;
1511 typedef struct SectionMergeInfo {
1512 Section *s; /* corresponding existing section */
1513 unsigned long offset; /* offset of the new section in the existing section */
1514 uint8_t new_section; /* true if section 's' was added */
1515 uint8_t link_once; /* true if link once section */
1516 } SectionMergeInfo;
1518 /* load an object file and merge it with current files */
1519 /* XXX: handle correctly stab (debug) info */
1520 static int tcc_load_object_file(TCCState *s1,
1521 int fd, unsigned long file_offset)
1523 Elf32_Ehdr ehdr;
1524 Elf32_Shdr *shdr, *sh;
1525 int size, i, j, offset, offseti, nb_syms, sym_index, ret;
1526 unsigned char *strsec, *strtab;
1527 int *old_to_new_syms;
1528 char *sh_name, *name;
1529 SectionMergeInfo *sm_table, *sm;
1530 Elf32_Sym *sym, *symtab;
1531 Elf32_Rel *rel, *rel_end;
1532 Section *s;
1534 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
1535 goto fail1;
1536 if (ehdr.e_ident[0] != ELFMAG0 ||
1537 ehdr.e_ident[1] != ELFMAG1 ||
1538 ehdr.e_ident[2] != ELFMAG2 ||
1539 ehdr.e_ident[3] != ELFMAG3)
1540 goto fail1;
1541 /* test if object file */
1542 if (ehdr.e_type != ET_REL)
1543 goto fail1;
1544 /* test CPU specific stuff */
1545 if (ehdr.e_ident[5] != ELFDATA2LSB ||
1546 ehdr.e_machine != EM_386) {
1547 fail1:
1548 error_noabort("invalid object file");
1549 return -1;
1551 /* read sections */
1552 shdr = load_data(fd, file_offset + ehdr.e_shoff,
1553 sizeof(Elf32_Shdr) * ehdr.e_shnum);
1554 sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
1556 /* load section names */
1557 sh = &shdr[ehdr.e_shstrndx];
1558 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
1560 /* load symtab and strtab */
1561 old_to_new_syms = NULL;
1562 symtab = NULL;
1563 strtab = NULL;
1564 nb_syms = 0;
1565 for(i = 1; i < ehdr.e_shnum; i++) {
1566 sh = &shdr[i];
1567 if (sh->sh_type == SHT_SYMTAB) {
1568 if (symtab) {
1569 error_noabort("object must contain only one symtab");
1570 fail:
1571 ret = -1;
1572 goto the_end;
1574 nb_syms = sh->sh_size / sizeof(Elf32_Sym);
1575 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
1576 sm_table[i].s = symtab_section;
1578 /* now load strtab */
1579 sh = &shdr[sh->sh_link];
1580 strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
1584 /* now examine each section and try to merge its content with the
1585 ones in memory */
1586 for(i = 1; i < ehdr.e_shnum; i++) {
1587 /* no need to examine section name strtab */
1588 if (i == ehdr.e_shstrndx)
1589 continue;
1590 sh = &shdr[i];
1591 sh_name = strsec + sh->sh_name;
1592 /* ignore sections types we do not handle */
1593 if (sh->sh_type != SHT_PROGBITS &&
1594 sh->sh_type != SHT_REL &&
1595 sh->sh_type != SHT_NOBITS)
1596 continue;
1597 if (sh->sh_addralign < 1)
1598 sh->sh_addralign = 1;
1599 /* find corresponding section, if any */
1600 for(j = 1; j < s1->nb_sections;j++) {
1601 s = s1->sections[j];
1602 if (!strcmp(s->name, sh_name)) {
1603 if (!strncmp(sh_name, ".gnu.linkonce",
1604 sizeof(".gnu.linkonce") - 1)) {
1605 /* if a 'linkonce' section is already present, we
1606 do not add it again. It is a little tricky as
1607 symbols can still be defined in
1608 it. */
1609 sm_table[i].link_once = 1;
1610 goto next;
1611 } else {
1612 goto found;
1616 /* not found: create new section */
1617 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags);
1618 /* take as much info as possible from the section. sh_link and
1619 sh_info will be updated later */
1620 s->sh_addralign = sh->sh_addralign;
1621 s->sh_entsize = sh->sh_entsize;
1622 sm_table[i].new_section = 1;
1623 found:
1624 if (sh->sh_type != s->sh_type) {
1625 error_noabort("invalid section type");
1626 goto fail;
1629 /* align start of section */
1630 offset = s->data_offset;
1631 size = sh->sh_addralign - 1;
1632 offset = (offset + size) & ~size;
1633 if (sh->sh_addralign > s->sh_addralign)
1634 s->sh_addralign = sh->sh_addralign;
1635 s->data_offset = offset;
1636 sm_table[i].offset = offset;
1637 sm_table[i].s = s;
1638 /* concatenate sections */
1639 size = sh->sh_size;
1640 if (sh->sh_type != SHT_NOBITS) {
1641 unsigned char *ptr;
1642 lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
1643 ptr = section_ptr_add(s, size);
1644 read(fd, ptr, size);
1645 } else {
1646 s->data_offset += size;
1648 next: ;
1651 /* second short pass to update sh_link and sh_info fields of new
1652 sections */
1653 sm = sm_table;
1654 for(i = 1; i < ehdr.e_shnum; i++) {
1655 s = sm_table[i].s;
1656 if (!s || !sm_table[i].new_section)
1657 continue;
1658 sh = &shdr[i];
1659 if (sh->sh_link > 0)
1660 s->link = sm_table[sh->sh_link].s;
1661 if (sh->sh_type == SHT_REL) {
1662 s->sh_info = sm_table[sh->sh_info].s->sh_num;
1663 /* update backward link */
1664 s1->sections[s->sh_info]->reloc = s;
1668 /* resolve symbols */
1669 old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
1671 sym = symtab + 1;
1672 for(i = 1; i < nb_syms; i++, sym++) {
1673 if (sym->st_shndx != SHN_UNDEF &&
1674 sym->st_shndx < SHN_LORESERVE) {
1675 sm = &sm_table[sym->st_shndx];
1676 if (sm->link_once) {
1677 /* if a symbol is in a link once section, we use the
1678 already defined symbol. It is very important to get
1679 correct relocations */
1680 if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
1681 name = strtab + sym->st_name;
1682 sym_index = find_elf_sym(symtab_section, name);
1683 if (sym_index)
1684 old_to_new_syms[i] = sym_index;
1686 continue;
1688 /* if no corresponding section added, no need to add symbol */
1689 if (!sm->s)
1690 continue;
1691 /* convert section number */
1692 sym->st_shndx = sm->s->sh_num;
1693 /* offset value */
1694 sym->st_value += sm->offset;
1696 /* add symbol */
1697 name = strtab + sym->st_name;
1698 sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size,
1699 sym->st_info, sym->st_shndx, name);
1700 old_to_new_syms[i] = sym_index;
1703 /* third pass to patch relocation entries */
1704 for(i = 1; i < ehdr.e_shnum; i++) {
1705 s = sm_table[i].s;
1706 if (!s)
1707 continue;
1708 sh = &shdr[i];
1709 offset = sm_table[i].offset;
1710 switch(s->sh_type) {
1711 case SHT_REL:
1712 /* take relocation offset information */
1713 offseti = sm_table[sh->sh_info].offset;
1714 rel_end = (Elf32_Rel *)(s->data + s->data_offset);
1715 for(rel = (Elf32_Rel *)(s->data + offset);
1716 rel < rel_end;
1717 rel++) {
1718 int type;
1719 unsigned sym_index;
1720 /* convert symbol index */
1721 type = ELF32_R_TYPE(rel->r_info);
1722 sym_index = ELF32_R_SYM(rel->r_info);
1723 /* NOTE: only one symtab assumed */
1724 if (sym_index >= nb_syms)
1725 goto invalid_reloc;
1726 sym_index = old_to_new_syms[sym_index];
1727 if (!sym_index) {
1728 invalid_reloc:
1729 error_noabort("Invalid relocation entry");
1730 goto fail;
1732 rel->r_info = ELF32_R_INFO(sym_index, type);
1733 /* offset the relocation offset */
1734 rel->r_offset += offseti;
1736 break;
1737 default:
1738 break;
1742 ret = 0;
1743 the_end:
1744 tcc_free(symtab);
1745 tcc_free(strtab);
1746 tcc_free(old_to_new_syms);
1747 tcc_free(sm_table);
1748 tcc_free(strsec);
1749 tcc_free(shdr);
1750 return ret;
1753 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1755 typedef struct ArchiveHeader {
1756 char ar_name[16]; /* name of this member */
1757 char ar_date[12]; /* file mtime */
1758 char ar_uid[6]; /* owner uid; printed as decimal */
1759 char ar_gid[6]; /* owner gid; printed as decimal */
1760 char ar_mode[8]; /* file mode, printed as octal */
1761 char ar_size[10]; /* file size, printed as decimal */
1762 char ar_fmag[2]; /* should contain ARFMAG */
1763 } ArchiveHeader;
1765 static int get_be32(const uint8_t *b)
1767 return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
1770 /* load only the objects which resolve undefined symbols */
1771 static int tcc_load_alacarte(TCCState *s1, int fd, int size)
1773 int i, bound, nsyms, sym_index, off, ret;
1774 uint8_t *data;
1775 const char *ar_names, *p;
1776 const uint8_t *ar_index;
1777 Elf32_Sym *sym;
1779 data = tcc_malloc(size);
1780 if (read(fd, data, size) != size)
1781 goto fail;
1782 nsyms = get_be32(data);
1783 ar_index = data + 4;
1784 ar_names = ar_index + nsyms * 4;
1786 do {
1787 bound = 0;
1788 for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
1789 sym_index = find_elf_sym(symtab_section, p);
1790 if(sym_index) {
1791 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
1792 if(sym->st_shndx == SHN_UNDEF) {
1793 off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);
1794 #if 0
1795 printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx);
1796 #endif
1797 ++bound;
1798 lseek(fd, off, SEEK_SET);
1799 if(tcc_load_object_file(s1, fd, off) < 0) {
1800 fail:
1801 ret = -1;
1802 goto the_end;
1807 } while(bound);
1808 ret = 0;
1809 the_end:
1810 tcc_free(data);
1811 return ret;
1814 /* load a '.a' file */
1815 static int tcc_load_archive(TCCState *s1, int fd)
1817 ArchiveHeader hdr;
1818 char ar_size[11];
1819 char ar_name[17];
1820 char magic[8];
1821 int size, len, i;
1822 unsigned long file_offset;
1824 /* skip magic which was already checked */
1825 read(fd, magic, sizeof(magic));
1827 for(;;) {
1828 len = read(fd, &hdr, sizeof(hdr));
1829 if (len == 0)
1830 break;
1831 if (len != sizeof(hdr)) {
1832 error_noabort("invalid archive");
1833 return -1;
1835 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
1836 ar_size[sizeof(hdr.ar_size)] = '\0';
1837 size = strtol(ar_size, NULL, 0);
1838 memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
1839 for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
1840 if (ar_name[i] != ' ')
1841 break;
1843 ar_name[i + 1] = '\0';
1844 // printf("name='%s' size=%d %s\n", ar_name, size, ar_size);
1845 file_offset = lseek(fd, 0, SEEK_CUR);
1846 /* align to even */
1847 size = (size + 1) & ~1;
1848 if (!strcmp(ar_name, "/")) {
1849 /* coff symbol table : we handle it */
1850 if(s1->alacarte_link)
1851 return tcc_load_alacarte(s1, fd, size);
1852 } else if (!strcmp(ar_name, "//") ||
1853 !strcmp(ar_name, "__.SYMDEF") ||
1854 !strcmp(ar_name, "__.SYMDEF/") ||
1855 !strcmp(ar_name, "ARFILENAMES/")) {
1856 /* skip symbol table or archive names */
1857 } else {
1858 if (tcc_load_object_file(s1, fd, file_offset) < 0)
1859 return -1;
1861 lseek(fd, file_offset + size, SEEK_SET);
1863 return 0;
1866 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
1867 is referenced by the user (so it should be added as DT_NEEDED in
1868 the generated ELF file) */
1869 static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
1871 Elf32_Ehdr ehdr;
1872 Elf32_Shdr *shdr, *sh, *sh1;
1873 int i, nb_syms, nb_dts, sym_bind, ret;
1874 Elf32_Sym *sym, *dynsym;
1875 Elf32_Dyn *dt, *dynamic;
1876 unsigned char *dynstr;
1877 const char *name, *soname, *p;
1878 DLLReference *dllref;
1880 read(fd, &ehdr, sizeof(ehdr));
1882 /* test CPU specific stuff */
1883 if (ehdr.e_ident[5] != ELFDATA2LSB ||
1884 ehdr.e_machine != EM_386) {
1885 error_noabort("bad architecture");
1886 return -1;
1889 /* read sections */
1890 shdr = load_data(fd, ehdr.e_shoff, sizeof(Elf32_Shdr) * ehdr.e_shnum);
1892 /* load dynamic section and dynamic symbols */
1893 nb_syms = 0;
1894 nb_dts = 0;
1895 dynamic = NULL;
1896 dynsym = NULL; /* avoid warning */
1897 dynstr = NULL; /* avoid warning */
1898 for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
1899 switch(sh->sh_type) {
1900 case SHT_DYNAMIC:
1901 nb_dts = sh->sh_size / sizeof(Elf32_Dyn);
1902 dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
1903 break;
1904 case SHT_DYNSYM:
1905 nb_syms = sh->sh_size / sizeof(Elf32_Sym);
1906 dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
1907 sh1 = &shdr[sh->sh_link];
1908 dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
1909 break;
1910 default:
1911 break;
1915 /* compute the real library name */
1916 soname = filename;
1917 p = strrchr(soname, '/');
1918 if (p)
1919 soname = p + 1;
1921 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
1922 if (dt->d_tag == DT_SONAME) {
1923 soname = dynstr + dt->d_un.d_val;
1927 /* if the dll is already loaded, do not load it */
1928 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1929 dllref = s1->loaded_dlls[i];
1930 if (!strcmp(soname, dllref->name)) {
1931 /* but update level if needed */
1932 if (level < dllref->level)
1933 dllref->level = level;
1934 ret = 0;
1935 goto the_end;
1939 // printf("loading dll '%s'\n", soname);
1941 /* add the dll and its level */
1942 dllref = tcc_malloc(sizeof(DLLReference) + strlen(soname));
1943 dllref->level = level;
1944 strcpy(dllref->name, soname);
1945 dynarray_add((void ***)&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
1947 /* add dynamic symbols in dynsym_section */
1948 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
1949 sym_bind = ELF32_ST_BIND(sym->st_info);
1950 if (sym_bind == STB_LOCAL)
1951 continue;
1952 name = dynstr + sym->st_name;
1953 add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
1954 sym->st_info, sym->st_shndx, name);
1957 /* load all referenced DLLs */
1958 for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
1959 switch(dt->d_tag) {
1960 case DT_NEEDED:
1961 name = dynstr + dt->d_un.d_val;
1962 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1963 dllref = s1->loaded_dlls[i];
1964 if (!strcmp(name, dllref->name))
1965 goto already_loaded;
1967 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
1968 error_noabort("referenced dll '%s' not found", name);
1969 ret = -1;
1970 goto the_end;
1972 already_loaded:
1973 break;
1976 ret = 0;
1977 the_end:
1978 tcc_free(dynstr);
1979 tcc_free(dynsym);
1980 tcc_free(dynamic);
1981 tcc_free(shdr);
1982 return ret;
1985 #define LD_TOK_NAME 256
1986 #define LD_TOK_EOF (-1)
1988 /* return next ld script token */
1989 static int ld_next(TCCState *s1, char *name, int name_size)
1991 int c;
1992 char *q;
1994 redo:
1995 switch(ch) {
1996 case ' ':
1997 case '\t':
1998 case '\f':
1999 case '\v':
2000 case '\r':
2001 case '\n':
2002 inp();
2003 goto redo;
2004 case '/':
2005 minp();
2006 if (ch == '*') {
2007 file->buf_ptr = parse_comment(file->buf_ptr);
2008 ch = file->buf_ptr[0];
2009 goto redo;
2010 } else {
2011 q = name;
2012 *q++ = '/';
2013 goto parse_name;
2015 break;
2016 case 'a' ... 'z':
2017 case 'A' ... 'Z':
2018 case '_':
2019 case '\\':
2020 case '.':
2021 case '$':
2022 case '~':
2023 q = name;
2024 parse_name:
2025 for(;;) {
2026 if (!((ch >= 'a' && ch <= 'z') ||
2027 (ch >= 'A' && ch <= 'Z') ||
2028 (ch >= '0' && ch <= '9') ||
2029 strchr("/.-_+=$:\\,~", ch)))
2030 break;
2031 if ((q - name) < name_size - 1) {
2032 *q++ = ch;
2034 minp();
2036 *q = '\0';
2037 c = LD_TOK_NAME;
2038 break;
2039 case CH_EOF:
2040 c = LD_TOK_EOF;
2041 break;
2042 default:
2043 c = ch;
2044 inp();
2045 break;
2047 #if 0
2048 printf("tok=%c %d\n", c, c);
2049 if (c == LD_TOK_NAME)
2050 printf(" name=%s\n", name);
2051 #endif
2052 return c;
2055 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
2056 files */
2057 static int tcc_load_ldscript(TCCState *s1)
2059 char cmd[64];
2060 char filename[1024];
2061 int t;
2063 ch = file->buf_ptr[0];
2064 ch = handle_eob();
2065 for(;;) {
2066 t = ld_next(s1, cmd, sizeof(cmd));
2067 if (t == LD_TOK_EOF)
2068 return 0;
2069 else if (t != LD_TOK_NAME)
2070 return -1;
2071 if (!strcmp(cmd, "INPUT") ||
2072 !strcmp(cmd, "GROUP")) {
2073 t = ld_next(s1, cmd, sizeof(cmd));
2074 if (t != '(')
2075 expect("(");
2076 t = ld_next(s1, filename, sizeof(filename));
2077 for(;;) {
2078 if (t == LD_TOK_EOF) {
2079 error_noabort("unexpected end of file");
2080 return -1;
2081 } else if (t == ')') {
2082 break;
2083 } else if (t != LD_TOK_NAME) {
2084 error_noabort("filename expected");
2085 return -1;
2087 tcc_add_file(s1, filename);
2088 t = ld_next(s1, filename, sizeof(filename));
2089 if (t == ',') {
2090 t = ld_next(s1, filename, sizeof(filename));
2093 } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
2094 !strcmp(cmd, "TARGET")) {
2095 /* ignore some commands */
2096 t = ld_next(s1, cmd, sizeof(cmd));
2097 if (t != '(')
2098 expect("(");
2099 for(;;) {
2100 t = ld_next(s1, filename, sizeof(filename));
2101 if (t == LD_TOK_EOF) {
2102 error_noabort("unexpected end of file");
2103 return -1;
2104 } else if (t == ')') {
2105 break;
2108 } else {
2109 return -1;
2112 return 0;