History

removed many memory leaks - suppressed many global variables

bellard committed 24 years ago

Commit e4c7dd51 parent c62792e1 file · +205 −162Browse files

Unified · Split

tccelf.c+205−162
@@ -199,8 +199,8 @@ static int add_elf_sym(Section *s, unsigned long value, unsigned long size, · 198 unmodified lines
199 sym_bind, sh_num, esym_bind, esym->st_shndx);199 sym_bind, sh_num, esym_bind, esym->st_shndx);
200#endif200#endif
201 /* NOTE: we accept that two DLL define the same symbol */201 /* NOTE: we accept that two DLL define the same symbol */
202 if (s != dynsymtab_section)202 if (s != tcc_state->dynsymtab_section)
203 error("'%s' defined twice", name);203 error_noabort("'%s' defined twice", name);
204 }204 }
205 } else {205 } else {
206 do_patch:206 do_patch:
@@ -232,7 +232,7 @@ static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, · 25 unmodified lines
232 snprintf(buf, sizeof(buf), ".rel%s", s->name);232 snprintf(buf, sizeof(buf), ".rel%s", s->name);
233 /* if the symtab is allocated, then we consider the relocation233 /* if the symtab is allocated, then we consider the relocation
234 are also */234 are also */
235 sr = new_section(buf, SHT_REL, symtab->sh_flags);235 sr = new_section(tcc_state, buf, SHT_REL, symtab->sh_flags);
236 sr->sh_entsize = sizeof(Elf32_Rel);236 sr->sh_entsize = sizeof(Elf32_Rel);
237 sr->link = symtab;237 sr->link = symtab;
238 sr->sh_info = s->sh_num;238 sr->sh_info = s->sh_num;
@@ -293,7 +293,7 @@ static void put_stabd(int type, int other, int desc) · 54 unmodified lines
293 the global and weak ones. Since TCC cannot sort it while generating293 the global and weak ones. Since TCC cannot sort it while generating
294 the code, we must do it after. All the relocation tables are also294 the code, we must do it after. All the relocation tables are also
295 modified to take into account the symbol table sorting */295 modified to take into account the symbol table sorting */
296static void sort_syms(Section *s)296static void sort_syms(TCCState *s1, Section *s)
297{297{
298 int *old_to_new_syms;298 int *old_to_new_syms;
299 Elf32_Sym *new_syms;299 Elf32_Sym *new_syms;
@@ -335,8 +335,8 @@ static void sort_syms(Section *s) · 35 unmodified lines
335 tcc_free(new_syms);335 tcc_free(new_syms);
336 336
337 /* now we modify all the relocations */337 /* now we modify all the relocations */
338 for(i = 1; i < nb_sections; i++) {338 for(i = 1; i < s1->nb_sections; i++) {
339 sr = sections[i];339 sr = s1->sections[i];
340 if (sr->sh_type == SHT_REL && sr->link == s) {340 if (sr->sh_type == SHT_REL && sr->link == s) {
341 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);341 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
342 for(rel = (Elf32_Rel *)sr->data;342 for(rel = (Elf32_Rel *)sr->data;
@@ -383,7 +383,7 @@ static void *resolve_sym(const char *sym) · 40 unmodified lines
383 383
384/* relocate symbol table, resolve undefined symbols if do_resolve is384/* relocate symbol table, resolve undefined symbols if do_resolve is
385 true and output error if undefined symbol. */385 true and output error if undefined symbol. */
386static void relocate_syms(int do_resolve)386static void relocate_syms(TCCState *s1, int do_resolve)
387{387{
388 Elf32_Sym *sym, *esym, *sym_end;388 Elf32_Sym *sym, *esym, *sym_end;
389 int sym_bind, sh_num, sym_index;389 int sym_bind, sh_num, sym_index;
@@ -404,11 +404,11 @@ static void relocate_syms(int do_resolve) · 14 unmodified lines
404 sym->st_value = addr;404 sym->st_value = addr;
405 goto found;405 goto found;
406 }406 }
407 } else if (dynsym) {407 } else if (s1->dynsym) {
408 /* if dynamic symbol exist, then use it */408 /* if dynamic symbol exist, then use it */
409 sym_index = find_elf_sym(dynsym, name);409 sym_index = find_elf_sym(s1->dynsym, name);
410 if (sym_index) {410 if (sym_index) {
411 esym = &((Elf32_Sym *)dynsym->data)[sym_index];411 esym = &((Elf32_Sym *)s1->dynsym->data)[sym_index];
412 sym->st_value = esym->st_value;412 sym->st_value = esym->st_value;
413 goto found;413 goto found;
414 }414 }
@@ -423,11 +423,11 @@ static void relocate_syms(int do_resolve) · 8 unmodified lines
423 if (sym_bind == STB_WEAK) {423 if (sym_bind == STB_WEAK) {
424 sym->st_value = 0;424 sym->st_value = 0;
425 } else {425 } else {
426 error("undefined symbol '%s'", name);426 error_noabort("undefined symbol '%s'", name);
427 }427 }
428 } else if (sh_num < SHN_LORESERVE) {428 } else if (sh_num < SHN_LORESERVE) {
429 /* add section base */429 /* add section base */
430 sym->st_value += sections[sym->st_shndx]->sh_addr;430 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
431 }431 }
432 found: ;432 found: ;
433 }433 }
@@ -461,7 +461,7 @@ static void relocate_section(TCCState *s1, Section *s) · 27 unmodified lines
461 switch(type) {461 switch(type) {
462 case R_386_32:462 case R_386_32:
463 if (s1->output_type == TCC_OUTPUT_DLL) {463 if (s1->output_type == TCC_OUTPUT_DLL) {
464 esym_index = symtab_to_dynsym[sym_index];464 esym_index = s1->symtab_to_dynsym[sym_index];
465 qrel->r_offset = rel->r_offset;465 qrel->r_offset = rel->r_offset;
466 if (esym_index) {466 if (esym_index) {
467 qrel->r_info = ELF32_R_INFO(esym_index, R_386_32);467 qrel->r_info = ELF32_R_INFO(esym_index, R_386_32);
@@ -477,7 +477,7 @@ static void relocate_section(TCCState *s1, Section *s) · 9 unmodified lines
477 case R_386_PC32:477 case R_386_PC32:
478 if (s1->output_type == TCC_OUTPUT_DLL) {478 if (s1->output_type == TCC_OUTPUT_DLL) {
479 /* DLL relocation */479 /* DLL relocation */
480 esym_index = symtab_to_dynsym[sym_index];480 esym_index = s1->symtab_to_dynsym[sym_index];
481 if (esym_index) {481 if (esym_index) {
482 qrel->r_offset = rel->r_offset;482 qrel->r_offset = rel->r_offset;
483 qrel->r_info = ELF32_R_INFO(esym_index, R_386_PC32);483 qrel->r_info = ELF32_R_INFO(esym_index, R_386_PC32);
@@ -495,29 +495,29 @@ static void relocate_section(TCCState *s1, Section *s) · 11 unmodified lines
495 *(int *)ptr = val;495 *(int *)ptr = val;
496 break;496 break;
497 case R_386_GOTPC:497 case R_386_GOTPC:
498 *(int *)ptr += got->sh_addr - addr;498 *(int *)ptr += s1->got->sh_addr - addr;
499 break;499 break;
500 case R_386_GOTOFF:500 case R_386_GOTOFF:
501 *(int *)ptr += val - got->sh_addr;501 *(int *)ptr += val - s1->got->sh_addr;
502 break;502 break;
503 case R_386_GOT32:503 case R_386_GOT32:
504 /* we load the got offset */504 /* we load the got offset */
505 *(int *)ptr += got_offsets[sym_index];505 *(int *)ptr += s1->got_offsets[sym_index];
506 break;506 break;
507 }507 }
508 }508 }
509 /* if the relocation is allocated, we change its symbol table */509 /* if the relocation is allocated, we change its symbol table */
510 if (sr->sh_flags & SHF_ALLOC)510 if (sr->sh_flags & SHF_ALLOC)
511 sr->link = dynsym;511 sr->link = s1->dynsym;
512}512}
513 513
514/* relocate relocation table in 'sr' */514/* relocate relocation table in 'sr' */
515static void relocate_rel(Section *sr)515static void relocate_rel(TCCState *s1, Section *sr)
516{516{
517 Section *s;517 Section *s;
518 Elf32_Rel *rel, *rel_end;518 Elf32_Rel *rel, *rel_end;
519 519
520 s = sections[sr->sh_info];520 s = s1->sections[sr->sh_info];
521 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);521 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
522 for(rel = (Elf32_Rel *)sr->data;522 for(rel = (Elf32_Rel *)sr->data;
523 rel < rel_end;523 rel < rel_end;
@@ -528,7 +528,7 @@ static void relocate_rel(Section *sr) · 4 unmodified lines
528 528
529/* count the number of dynamic relocations so that we can reserve529/* count the number of dynamic relocations so that we can reserve
530 their space */530 their space */
531static int prepare_dynamic_rel(Section *sr)531static int prepare_dynamic_rel(TCCState *s1, Section *sr)
532{532{
533 Elf32_Rel *rel, *rel_end;533 Elf32_Rel *rel, *rel_end;
534 int sym_index, esym_index, type, count;534 int sym_index, esym_index, type, count;
@@ -543,7 +543,7 @@ static int prepare_dynamic_rel(Section *sr) · 8 unmodified lines
543 count++;543 count++;
544 break;544 break;
545 case R_386_PC32:545 case R_386_PC32:
546 esym_index = symtab_to_dynsym[sym_index];546 esym_index = s1->symtab_to_dynsym[sym_index];
547 if (esym_index)547 if (esym_index)
548 count++;548 count++;
549 break;549 break;
@@ -559,25 +559,25 @@ static int prepare_dynamic_rel(Section *sr) · 9 unmodified lines
559 return count;559 return count;
560}560}
561 561
562static void put_got_offset(int index, unsigned long val)562static void put_got_offset(TCCState *s1, int index, unsigned long val)
563{563{
564 int n;564 int n;
565 unsigned long *tab;565 unsigned long *tab;
566 566
567 if (index >= nb_got_offsets) {567 if (index >= s1->nb_got_offsets) {
568 /* find immediately bigger power of 2 and reallocate array */568 /* find immediately bigger power of 2 and reallocate array */
569 n = 1;569 n = 1;
570 while (index >= n)570 while (index >= n)
571 n *= 2;571 n *= 2;
572 tab = tcc_realloc(got_offsets, n * sizeof(unsigned long));572 tab = tcc_realloc(s1->got_offsets, n * sizeof(unsigned long));
573 if (!tab)573 if (!tab)
574 error("memory full");574 error("memory full");
575 got_offsets = tab;575 s1->got_offsets = tab;
576 memset(got_offsets + nb_got_offsets, 0,576 memset(s1->got_offsets + s1->nb_got_offsets, 0,
577 (n - nb_got_offsets) * sizeof(unsigned long));577 (n - s1->nb_got_offsets) * sizeof(unsigned long));
578 nb_got_offsets = n;578 s1->nb_got_offsets = n;
579 }579 }
580 got_offsets[index] = val;580 s1->got_offsets[index] = val;
581}581}
582 582
583/* XXX: suppress that */583/* XXX: suppress that */
@@ -589,16 +589,16 @@ static void put32(unsigned char *p, unsigned int val) · 5 unmodified lines
589 p[3] = val >> 24;589 p[3] = val >> 24;
590}590}
591 591
592static void build_got(void)592static void build_got(TCCState *s1)
593{593{
594 unsigned char *ptr;594 unsigned char *ptr;
595 595
596 /* if no got, then create it */596 /* if no got, then create it */
597 got = new_section(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);597 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
598 got->sh_entsize = 4;598 s1->got->sh_entsize = 4;
599 add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT), 599 add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
600 got->sh_num, "_GLOBAL_OFFSET_TABLE_");600 s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
601 ptr = section_ptr_add(got, 3 * sizeof(int));601 ptr = section_ptr_add(s1->got, 3 * sizeof(int));
602 /* keep space for _DYNAMIC pointer, if present */602 /* keep space for _DYNAMIC pointer, if present */
603 put32(ptr, 0);603 put32(ptr, 0);
604 /* two dummy got entries */604 /* two dummy got entries */
@@ -608,7 +608,8 @@ static void build_got(void) · 3 unmodified lines
608 608
609/* put a got entry corresponding to a symbol in symtab_section. 'size'609/* put a got entry corresponding to a symbol in symtab_section. 'size'
610 and 'info' can be modifed if more precise info comes from the DLL */610 and 'info' can be modifed if more precise info comes from the DLL */
611static void put_got_entry(int reloc_type, unsigned long size, int info, 611static void put_got_entry(TCCState *s1,
612 int reloc_type, unsigned long size, int info,
612 int sym_index)613 int sym_index)
613{614{
614 int index;615 int index;
@@ -617,46 +618,46 @@ static void put_got_entry(int reloc_type, unsigned long size, int info, · 2 unmodified lines
617 unsigned long offset;618 unsigned long offset;
618 int *ptr;619 int *ptr;
619 620
620 if (!got)621 if (!s1->got)
621 build_got();622 build_got(s1);
622 623
623 /* if a got entry already exists for that symbol, no need to add one */624 /* if a got entry already exists for that symbol, no need to add one */
624 if (sym_index < nb_got_offsets &&625 if (sym_index < s1->nb_got_offsets &&
625 got_offsets[sym_index] != 0)626 s1->got_offsets[sym_index] != 0)
626 return;627 return;
627 628
628 put_got_offset(sym_index, got->data_offset);629 put_got_offset(s1, sym_index, s1->got->data_offset);
629 630
630 if (dynsym) {631 if (s1->dynsym) {
631 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];632 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
632 name = symtab_section->link->data + sym->st_name;633 name = symtab_section->link->data + sym->st_name;
633 offset = sym->st_value;634 offset = sym->st_value;
634 /* NOTE: we put temporarily the got offset */635 /* NOTE: we put temporarily the got offset */
635 if (reloc_type == R_386_JMP_SLOT) {636 if (reloc_type == R_386_JMP_SLOT) {
636 nb_plt_entries++;637 s1->nb_plt_entries++;
637 offset = got->data_offset;638 offset = s1->got->data_offset;
638 }639 }
639 index = put_elf_sym(dynsym, offset, 640 index = put_elf_sym(s1->dynsym, offset,
640 size, info, 0, sym->st_shndx, name);641 size, info, 0, sym->st_shndx, name);
641 /* put a got entry */642 /* put a got entry */
642 put_elf_reloc(dynsym, got, 643 put_elf_reloc(s1->dynsym, s1->got,
643 got->data_offset, 644 s1->got->data_offset,
644 reloc_type, index);645 reloc_type, index);
645 }646 }
646 ptr = section_ptr_add(got, sizeof(int));647 ptr = section_ptr_add(s1->got, sizeof(int));
647 *ptr = 0;648 *ptr = 0;
648}649}
649 650
650/* build GOT and PLT entries */651/* build GOT and PLT entries */
651static void build_got_entries(void)652static void build_got_entries(TCCState *s1)
652{653{
653 Section *s, *symtab;654 Section *s, *symtab;
654 Elf32_Rel *rel, *rel_end;655 Elf32_Rel *rel, *rel_end;
655 Elf32_Sym *sym;656 Elf32_Sym *sym;
656 int i, type, reloc_type, sym_index;657 int i, type, reloc_type, sym_index;
657 658
658 for(i = 1; i < nb_sections; i++) {659 for(i = 1; i < s1->nb_sections; i++) {
659 s = sections[i];660 s = s1->sections[i];
660 if (s->sh_type != SHT_REL)661 if (s->sh_type != SHT_REL)
661 continue;662 continue;
662 /* no need to handle got relocations */663 /* no need to handle got relocations */
@@ -673,8 +674,8 @@ static void build_got_entries(void) · 10 unmodified lines
673 case R_386_GOTOFF:674 case R_386_GOTOFF:
674 case R_386_GOTPC:675 case R_386_GOTPC:
675 case R_386_PLT32:676 case R_386_PLT32:
676 if (!got)677 if (!s1->got)
677 build_got();678 build_got(s1);
678 if (type == R_386_GOT32 || type == R_386_PLT32) {679 if (type == R_386_GOT32 || type == R_386_PLT32) {
679 sym_index = ELF32_R_SYM(rel->r_info);680 sym_index = ELF32_R_SYM(rel->r_info);
680 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];681 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
@@ -683,7 +684,7 @@ static void build_got_entries(void) · 2 unmodified lines
683 reloc_type = R_386_GLOB_DAT;684 reloc_type = R_386_GLOB_DAT;
684 else685 else
685 reloc_type = R_386_JMP_SLOT;686 reloc_type = R_386_JMP_SLOT;
686 put_got_entry(reloc_type, sym->st_size, sym->st_info, 687 put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
687 sym_index);688 sym_index);
688 }689 }
689 break;690 break;
@@ -694,23 +695,24 @@ static void build_got_entries(void) · 4 unmodified lines
694 }695 }
695}696}
696 697
697static Section *new_symtab(const char *symtab_name, int sh_type, int sh_flags,698static Section *new_symtab(TCCState *s1,
699 const char *symtab_name, int sh_type, int sh_flags,
698 const char *strtab_name, 700 const char *strtab_name,
699 const char *hash_name, int hash_sh_flags)701 const char *hash_name, int hash_sh_flags)
700{702{
701 Section *symtab, *strtab, *hash;703 Section *symtab, *strtab, *hash;
702 int *ptr, nb_buckets;704 int *ptr, nb_buckets;
703 705
704 symtab = new_section(symtab_name, sh_type, sh_flags);706 symtab = new_section(s1, symtab_name, sh_type, sh_flags);
705 symtab->sh_entsize = sizeof(Elf32_Sym);707 symtab->sh_entsize = sizeof(Elf32_Sym);
706 strtab = new_section(strtab_name, SHT_STRTAB, sh_flags);708 strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
707 put_elf_str(strtab, "");709 put_elf_str(strtab, "");
708 symtab->link = strtab;710 symtab->link = strtab;
709 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);711 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
710 712
711 nb_buckets = 1;713 nb_buckets = 1;
712 714
713 hash = new_section(hash_name, SHT_HASH, hash_sh_flags);715 hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
714 hash->sh_entsize = sizeof(int);716 hash->sh_entsize = sizeof(int);
715 symtab->hash = hash;717 symtab->hash = hash;
716 hash->link = symtab;718 hash->link = symtab;
@@ -759,7 +761,7 @@ static void tcc_add_runtime(TCCState *s1) · 42 unmodified lines
759#ifdef TCC_TARGET_I386761#ifdef TCC_TARGET_I386
760 if (s1->output_type != TCC_OUTPUT_MEMORY) {762 if (s1->output_type != TCC_OUTPUT_MEMORY) {
761 /* add 'call __bound_init()' in .init section */763 /* add 'call __bound_init()' in .init section */
762 init_section = find_section(".init");764 init_section = find_section(s1, ".init");
763 pinit = section_ptr_add(init_section, 5);765 pinit = section_ptr_add(init_section, 5);
764 pinit[0] = 0xe8;766 pinit[0] = 0xe8;
765 put32(pinit + 1, -4);767 put32(pinit + 1, -4);
@@ -790,8 +792,8 @@ static void tcc_add_runtime(TCCState *s1) · 24 unmodified lines
790 bss_section->sh_num, "_end");792 bss_section->sh_num, "_end");
791 /* add start and stop symbols for sections whose name can be793 /* add start and stop symbols for sections whose name can be
792 expressed in C */794 expressed in C */
793 for(i = 1; i < nb_sections; i++) {795 for(i = 1; i < s1->nb_sections; i++) {
794 s = sections[i];796 s = s1->sections[i];
795 if (s->sh_type == SHT_PROGBITS &&797 if (s->sh_type == SHT_PROGBITS &&
796 (s->sh_flags & SHF_ALLOC)) {798 (s->sh_flags & SHF_ALLOC)) {
797 const char *p;799 const char *p;
@@ -834,7 +836,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 36 unmodified lines
834{836{
835 Elf32_Ehdr ehdr;837 Elf32_Ehdr ehdr;
836 FILE *f;838 FILE *f;
837 int fd, mode;839 int fd, mode, ret;
838 int *section_order;840 int *section_order;
839 int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k;841 int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k;
840 unsigned long addr;842 unsigned long addr;
@@ -848,24 +850,24 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
848 unsigned long rel_addr, rel_size;850 unsigned long rel_addr, rel_size;
849 851
850 file_type = s1->output_type;852 file_type = s1->output_type;
853 s1->nb_errors = 0;
851 854
852 if (file_type != TCC_OUTPUT_OBJ)855 if (file_type != TCC_OUTPUT_OBJ)
853 tcc_add_runtime(s1);856 tcc_add_runtime(s1);
854 857
858 phdr = NULL;
859 section_order = NULL;
855 interp = NULL;860 interp = NULL;
856 dynamic = NULL;861 dynamic = NULL;
857 dynsym = NULL;
858 got = NULL;
859 nb_plt_entries = 0;
860 plt = NULL; /* avoid warning */862 plt = NULL; /* avoid warning */
861 dynstr = NULL; /* avoid warning */863 dynstr = NULL; /* avoid warning */
862 saved_dynamic_data_offset = 0; /* avoid warning */864 saved_dynamic_data_offset = 0; /* avoid warning */
863 865
864 if (file_type != TCC_OUTPUT_OBJ) {866 if (file_type != TCC_OUTPUT_OBJ) {
865 867
866 relocate_common_syms();868 relocate_common_syms();
867 869
868 if (!static_link) {870 if (!s1->static_link) {
869 const char *name;871 const char *name;
870 int sym_index, index;872 int sym_index, index;
871 Elf32_Sym *esym, *sym_end;873 Elf32_Sym *esym, *sym_end;
@@ -873,29 +875,29 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1 unmodified line
873 if (file_type == TCC_OUTPUT_EXE) {875 if (file_type == TCC_OUTPUT_EXE) {
874 char *ptr;876 char *ptr;
875 /* add interpreter section only if executable */877 /* add interpreter section only if executable */
876 interp = new_section(".interp", SHT_PROGBITS, SHF_ALLOC);878 interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
877 interp->sh_addralign = 1;879 interp->sh_addralign = 1;
878 ptr = section_ptr_add(interp, sizeof(elf_interp));880 ptr = section_ptr_add(interp, sizeof(elf_interp));
879 strcpy(ptr, elf_interp);881 strcpy(ptr, elf_interp);
880 }882 }
881 883
882 /* add dynamic symbol table */884 /* add dynamic symbol table */
883 dynsym = new_symtab(".dynsym", SHT_DYNSYM, SHF_ALLOC,885 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
884 ".dynstr", 886 ".dynstr",
885 ".hash", SHF_ALLOC);887 ".hash", SHF_ALLOC);
886 dynstr = dynsym->link;888 dynstr = s1->dynsym->link;
887 889
888 /* add dynamic section */890 /* add dynamic section */
889 dynamic = new_section(".dynamic", SHT_DYNAMIC, 891 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
890 SHF_ALLOC | SHF_WRITE);892 SHF_ALLOC | SHF_WRITE);
891 dynamic->link = dynstr;893 dynamic->link = dynstr;
892 dynamic->sh_entsize = sizeof(Elf32_Dyn);894 dynamic->sh_entsize = sizeof(Elf32_Dyn);
893 895
894 /* add PLT */896 /* add PLT */
895 plt = new_section(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);897 plt = new_section(s1, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
896 plt->sh_entsize = 4;898 plt->sh_entsize = 4;
897 899
898 build_got();900 build_got(s1);
899 901
900 /* scan for undefined symbols and see if they are in the902 /* scan for undefined symbols and see if they are in the
901 dynamic symbols. If a symbol STT_FUNC is found, then we903 dynamic symbols. If a symbol STT_FUNC is found, then we
@@ -909,12 +911,12 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
909 sym++) {911 sym++) {
910 if (sym->st_shndx == SHN_UNDEF) {912 if (sym->st_shndx == SHN_UNDEF) {
911 name = symtab_section->link->data + sym->st_name;913 name = symtab_section->link->data + sym->st_name;
912 sym_index = find_elf_sym(dynsymtab_section, name);914 sym_index = find_elf_sym(s1->dynsymtab_section, name);
913 if (sym_index) {915 if (sym_index) {
914 esym = &((Elf32_Sym *)dynsymtab_section->data)[sym_index];916 esym = &((Elf32_Sym *)s1->dynsymtab_section->data)[sym_index];
915 type = ELF32_ST_TYPE(esym->st_info);917 type = ELF32_ST_TYPE(esym->st_info);
916 if (type == STT_FUNC) {918 if (type == STT_FUNC) {
917 put_got_entry(R_386_JMP_SLOT, esym->st_size, 919 put_got_entry(s1, R_386_JMP_SLOT, esym->st_size,
918 esym->st_info, 920 esym->st_info,
919 sym - (Elf32_Sym *)symtab_section->data);921 sym - (Elf32_Sym *)symtab_section->data);
920 } else if (type == STT_OBJECT) {922 } else if (type == STT_OBJECT) {
@@ -922,10 +924,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1 unmodified line
922 offset = bss_section->data_offset;924 offset = bss_section->data_offset;
923 /* XXX: which alignment ? */925 /* XXX: which alignment ? */
924 offset = (offset + 8 - 1) & -8;926 offset = (offset + 8 - 1) & -8;
925 index = put_elf_sym(dynsym, offset, esym->st_size, 927 index = put_elf_sym(s1->dynsym, offset, esym->st_size,
926 esym->st_info, 0, 928 esym->st_info, 0,
927 bss_section->sh_num, name);929 bss_section->sh_num, name);
928 put_elf_reloc(dynsym, bss_section, 930 put_elf_reloc(s1->dynsym, bss_section,
929 offset, R_386_COPY, index);931 offset, R_386_COPY, index);
930 offset += esym->st_size;932 offset += esym->st_size;
931 bss_section->data_offset = offset;933 bss_section->data_offset = offset;
@@ -937,25 +939,28 @@ int tcc_output_file(TCCState *s1, const char *filename) · 5 unmodified lines
937 if (ELF32_ST_BIND(sym->st_info) == STB_WEAK ||939 if (ELF32_ST_BIND(sym->st_info) == STB_WEAK ||
938 !strcmp(name, "_fp_hw")) {940 !strcmp(name, "_fp_hw")) {
939 } else {941 } else {
940 error("undefined symbol '%s'", name);942 error_noabort("undefined symbol '%s'", name);
941 }943 }
942 }944 }
943 }945 }
944 }946 }
945 947
948 if (s1->nb_errors)
949 goto fail;
950
946 /* now look at unresolved dynamic symbols and export951 /* now look at unresolved dynamic symbols and export
947 corresponding symbol */952 corresponding symbol */
948 sym_end = (Elf32_Sym *)(dynsymtab_section->data + 953 sym_end = (Elf32_Sym *)(s1->dynsymtab_section->data +
949 dynsymtab_section->data_offset);954 s1->dynsymtab_section->data_offset);
950 for(esym = (Elf32_Sym *)dynsymtab_section->data + 1; 955 for(esym = (Elf32_Sym *)s1->dynsymtab_section->data + 1;
951 esym < sym_end;956 esym < sym_end;
952 esym++) {957 esym++) {
953 if (esym->st_shndx == SHN_UNDEF) {958 if (esym->st_shndx == SHN_UNDEF) {
954 name = dynsymtab_section->link->data + esym->st_name;959 name = s1->dynsymtab_section->link->data + esym->st_name;
955 sym_index = find_elf_sym(symtab_section, name);960 sym_index = find_elf_sym(symtab_section, name);
956 if (sym_index) {961 if (sym_index) {
957 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];962 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
958 put_elf_sym(dynsym, sym->st_value, sym->st_size, 963 put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
959 sym->st_info, 0, 964 sym->st_info, 0,
960 sym->st_shndx, name);965 sym->st_shndx, name);
961 } else {966 } else {
@@ -971,29 +976,30 @@ int tcc_output_file(TCCState *s1, const char *filename) · 9 unmodified lines
971 int nb_syms;976 int nb_syms;
972 /* shared library case : we simply export all the global symbols */977 /* shared library case : we simply export all the global symbols */
973 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);978 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
974 symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms);979 s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms);
975 for(sym = (Elf32_Sym *)symtab_section->data + 1; 980 for(sym = (Elf32_Sym *)symtab_section->data + 1;
976 sym < sym_end;981 sym < sym_end;
977 sym++) {982 sym++) {
978 if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {983 if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
979 name = symtab_section->link->data + sym->st_name;984 name = symtab_section->link->data + sym->st_name;
980 index = put_elf_sym(dynsym, sym->st_value, sym->st_size, 985 index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
981 sym->st_info, 0, 986 sym->st_info, 0,
982 sym->st_shndx, name);987 sym->st_shndx, name);
983 symtab_to_dynsym[sym - (Elf32_Sym *)symtab_section->data] = 988 s1->symtab_to_dynsym[sym -
989 (Elf32_Sym *)symtab_section->data] =
984 index;990 index;
985 }991 }
986 }992 }
987 }993 }
988 994
989 build_got_entries();995 build_got_entries(s1);
990 996
991 /* update PLT/GOT sizes so that we can allocate their space */997 /* update PLT/GOT sizes so that we can allocate their space */
992 plt->data_offset += 16 * (nb_plt_entries + 1);998 plt->data_offset += 16 * (s1->nb_plt_entries + 1);
993 999
994 /* add a list of needed dlls */1000 /* add a list of needed dlls */
995 for(i = 0; i < nb_loaded_dlls; i++) {1001 for(i = 0; i < s1->nb_loaded_dlls; i++) {
996 DLLReference *dllref = loaded_dlls[i];1002 DLLReference *dllref = s1->loaded_dlls[i];
997 if (dllref->level == 0)1003 if (dllref->level == 0)
998 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));1004 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
999 }1005 }
@@ -1007,18 +1013,18 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
1007 dynamic->data_offset += 8 * 9;1013 dynamic->data_offset += 8 * 9;
1008 } else {1014 } else {
1009 /* still need to build got entries in case of static link */1015 /* still need to build got entries in case of static link */
1010 build_got_entries();1016 build_got_entries(s1);
1011 }1017 }
1012 }1018 }
1013 1019
1014 memset(&ehdr, 0, sizeof(ehdr));1020 memset(&ehdr, 0, sizeof(ehdr));
1015 1021
1016 /* we add a section for symbols */1022 /* we add a section for symbols */
1017 strsec = new_section(".shstrtab", SHT_STRTAB, 0);1023 strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
1018 put_elf_str(strsec, "");1024 put_elf_str(strsec, "");
1019 1025
1020 /* compute number of sections */1026 /* compute number of sections */
1021 shnum = nb_sections;1027 shnum = s1->nb_sections;
1022 1028
1023 /* this array is used to reorder sections in the output file */1029 /* this array is used to reorder sections in the output file */
1024 section_order = tcc_malloc(sizeof(int) * shnum);1030 section_order = tcc_malloc(sizeof(int) * shnum);
@@ -1032,7 +1038,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
1032 phnum = 0;1038 phnum = 0;
1033 break;1039 break;
1034 case TCC_OUTPUT_EXE:1040 case TCC_OUTPUT_EXE:
1035 if (!static_link)1041 if (!s1->static_link)
1036 phnum = 4;1042 phnum = 4;
1037 else1043 else
1038 phnum = 2;1044 phnum = 2;
@@ -1046,19 +1052,19 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
1046 section should be output */1052 section should be output */
1047 /* NOTE: the strsec section comes last, so its size is also1053 /* NOTE: the strsec section comes last, so its size is also
1048 correct ! */1054 correct ! */
1049 for(i = 1; i < nb_sections; i++) {1055 for(i = 1; i < s1->nb_sections; i++) {
1050 s = sections[i];1056 s = s1->sections[i];
1051 s->sh_name = put_elf_str(strsec, s->name);1057 s->sh_name = put_elf_str(strsec, s->name);
1052 /* when generating a DLL, we include relocations but we may1058 /* when generating a DLL, we include relocations but we may
1053 patch them */1059 patch them */
1054 if (file_type == TCC_OUTPUT_DLL && 1060 if (file_type == TCC_OUTPUT_DLL &&
1055 s->sh_type == SHT_REL && 1061 s->sh_type == SHT_REL &&
1056 !(s->sh_flags & SHF_ALLOC)) {1062 !(s->sh_flags & SHF_ALLOC)) {
1057 prepare_dynamic_rel(s);1063 prepare_dynamic_rel(s1, s);
1058 } else if (do_debug || 1064 } else if (do_debug ||
1059 file_type == TCC_OUTPUT_OBJ || 1065 file_type == TCC_OUTPUT_OBJ ||
1060 (s->sh_flags & SHF_ALLOC) ||1066 (s->sh_flags & SHF_ALLOC) ||
1061 i == (nb_sections - 1)) {1067 i == (s1->nb_sections - 1)) {
1062 /* we output all sections if debug or object file */1068 /* we output all sections if debug or object file */
1063 s->sh_size = s->data_offset;1069 s->sh_size = s->data_offset;
1064 }1070 }
@@ -1099,8 +1105,8 @@ int tcc_output_file(TCCState *s1, const char *filename) · 34 unmodified lines
1099 relocations, progbits, nobits */1105 relocations, progbits, nobits */
1100 /* XXX: do faster and simpler sorting */1106 /* XXX: do faster and simpler sorting */
1101 for(k = 0; k < 5; k++) {1107 for(k = 0; k < 5; k++) {
1102 for(i = 1; i < nb_sections; i++) {1108 for(i = 1; i < s1->nb_sections; i++) {
1103 s = sections[i];1109 s = s1->sections[i];
1104 /* compute if section should be included */1110 /* compute if section should be included */
1105 if (j == 0) {1111 if (j == 0) {
1106 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != 1112 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
@@ -1193,7 +1199,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 86 unmodified lines
1193 ph->p_align = dynamic->sh_addralign;1199 ph->p_align = dynamic->sh_addralign;
1194 1200
1195 /* put GOT dynamic section address */1201 /* put GOT dynamic section address */
1196 put32(got->data, dynamic->sh_addr);1202 put32(s1->got->data, dynamic->sh_addr);
1197 1203
1198 /* compute the PLT */1204 /* compute the PLT */
1199 plt->data_offset = 0;1205 plt->data_offset = 0;
@@ -1202,15 +1208,15 @@ int tcc_output_file(TCCState *s1, const char *filename) · 2 unmodified lines
1202 p = section_ptr_add(plt, 16);1208 p = section_ptr_add(plt, 16);
1203 p[0] = 0xff; /* pushl got + 4 */1209 p[0] = 0xff; /* pushl got + 4 */
1204 p[1] = 0x35;1210 p[1] = 0x35;
1205 put32(p + 2, got->sh_addr + 4);1211 put32(p + 2, s1->got->sh_addr + 4);
1206 p[6] = 0xff; /* jmp *(got + 8) */1212 p[6] = 0xff; /* jmp *(got + 8) */
1207 p[7] = 0x25;1213 p[7] = 0x25;
1208 put32(p + 8, got->sh_addr + 8);1214 put32(p + 8, s1->got->sh_addr + 8);
1209 1215
1210 /* relocation symbols in .dynsym and build PLT. */1216 /* relocation symbols in .dynsym and build PLT. */
1211 plt_offset = 0;1217 plt_offset = 0;
1212 sym_end = (Elf32_Sym *)(dynsym->data + dynsym->data_offset);1218 sym_end = (Elf32_Sym *)(s1->dynsym->data + s1->dynsym->data_offset);
1213 for(sym = (Elf32_Sym *)dynsym->data + 1; 1219 for(sym = (Elf32_Sym *)s1->dynsym->data + 1;
1214 sym < sym_end;1220 sym < sym_end;
1215 sym++) {1221 sym++) {
1216 type = ELF32_ST_TYPE(sym->st_info);1222 type = ELF32_ST_TYPE(sym->st_info);
@@ -1220,7 +1226,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 3 unmodified lines
1220 p = section_ptr_add(plt, 16);1226 p = section_ptr_add(plt, 16);
1221 p[0] = 0xff; /* jmp *(got + x) */1227 p[0] = 0xff; /* jmp *(got + x) */
1222 p[1] = 0x25;1228 p[1] = 0x25;
1223 put32(p + 2, got->sh_addr + sym->st_value);1229 put32(p + 2, s1->got->sh_addr + sym->st_value);
1224 p[6] = 0x68; /* push $xxx */1230 p[6] = 0x68; /* push $xxx */
1225 put32(p + 7, plt_offset);1231 put32(p + 7, plt_offset);
1226 p[11] = 0xe9; /* jmp plt_start */1232 p[11] = 0xe9; /* jmp plt_start */
@@ -1233,15 +1239,15 @@ int tcc_output_file(TCCState *s1, const char *filename) · 6 unmodified lines
1233 }1239 }
1234 } else if (sym->st_shndx < SHN_LORESERVE) {1240 } else if (sym->st_shndx < SHN_LORESERVE) {
1235 /* do symbol relocation */1241 /* do symbol relocation */
1236 sym->st_value += sections[sym->st_shndx]->sh_addr;1242 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
1237 }1243 }
1238 }1244 }
1239 /* put dynamic section entries */1245 /* put dynamic section entries */
1240 1246
1241 dynamic->data_offset = saved_dynamic_data_offset;1247 dynamic->data_offset = saved_dynamic_data_offset;
1242 put_dt(dynamic, DT_HASH, dynsym->hash->sh_addr);1248 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1243 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);1249 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);
1244 put_dt(dynamic, DT_SYMTAB, dynsym->sh_addr);1250 put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
1245 put_dt(dynamic, DT_STRSZ, dynstr->data_offset);1251 put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
1246 put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym));1252 put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym));
1247 put_dt(dynamic, DT_REL, rel_addr);1253 put_dt(dynamic, DT_REL, rel_addr);
@@ -1256,8 +1262,8 @@ int tcc_output_file(TCCState *s1, const char *filename) · 8 unmodified lines
1256 }1262 }
1257 1263
1258 /* all other sections come after */1264 /* all other sections come after */
1259 for(i = 1; i < nb_sections; i++) {1265 for(i = 1; i < s1->nb_sections; i++) {
1260 s = sections[i];1266 s = s1->sections[i];
1261 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))1267 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
1262 continue;1268 continue;
1263 section_order[sh_order_index++] = i;1269 section_order[sh_order_index++] = i;
@@ -1272,23 +1278,29 @@ int tcc_output_file(TCCState *s1, const char *filename) · 8 unmodified lines
1272 /* if building executable or DLL, then relocate each section1278 /* if building executable or DLL, then relocate each section
1273 except the GOT which is already relocated */1279 except the GOT which is already relocated */
1274 if (file_type != TCC_OUTPUT_OBJ) {1280 if (file_type != TCC_OUTPUT_OBJ) {
1275 relocate_syms(0);1281 relocate_syms(s1, 0);
1282
1283 if (s1->nb_errors != 0) {
1284 fail:
1285 ret = -1;
1286 goto the_end;
1287 }
1276 1288
1277 /* relocate sections */1289 /* relocate sections */
1278 /* XXX: ignore sections with allocated relocations ? */1290 /* XXX: ignore sections with allocated relocations ? */
1279 for(i = 1; i < nb_sections; i++) {1291 for(i = 1; i < s1->nb_sections; i++) {
1280 s = sections[i];1292 s = s1->sections[i];
1281 if (s->reloc && s != got)1293 if (s->reloc && s != s1->got)
1282 relocate_section(s1, s);1294 relocate_section(s1, s);
1283 }1295 }
1284 1296
1285 /* relocate relocation entries if the relocation tables are1297 /* relocate relocation entries if the relocation tables are
1286 allocated in the executable */1298 allocated in the executable */
1287 for(i = 1; i < nb_sections; i++) {1299 for(i = 1; i < s1->nb_sections; i++) {
1288 s = sections[i];1300 s = s1->sections[i];
1289 if ((s->sh_flags & SHF_ALLOC) &&1301 if ((s->sh_flags & SHF_ALLOC) &&
1290 s->sh_type == SHT_REL) {1302 s->sh_type == SHT_REL) {
1291 relocate_rel(s);1303 relocate_rel(s1, s);
1292 }1304 }
1293 }1305 }
1294 1306
@@ -1299,7 +1311,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 4 unmodified lines
1299 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */1311 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1300 }1312 }
1301 1313
1302 sort_syms(symtab_section);1314 sort_syms(s1, symtab_section);
1303 1315
1304 /* align to 4 */1316 /* align to 4 */
1305 file_offset = (file_offset + 3) & -4;1317 file_offset = (file_offset + 3) & -4;
@@ -1338,15 +1350,16 @@ int tcc_output_file(TCCState *s1, const char *filename) · 32 unmodified lines
1338 else1350 else
1339 mode = 0777;1351 mode = 0777;
1340 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode); 1352 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
1341 if (fd < 0)1353 if (fd < 0) {
1342 error("could not write '%s'", filename);1354 error_noabort("could not write '%s'", filename);
1343 1355 goto fail;
1356 }
1344 f = fdopen(fd, "w");1357 f = fdopen(fd, "w");
1345 fwrite(&ehdr, 1, sizeof(Elf32_Ehdr), f);1358 fwrite(&ehdr, 1, sizeof(Elf32_Ehdr), f);
1346 fwrite(phdr, 1, phnum * sizeof(Elf32_Phdr), f);1359 fwrite(phdr, 1, phnum * sizeof(Elf32_Phdr), f);
1347 offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr);1360 offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr);
1348 for(i=1;i<nb_sections;i++) {1361 for(i=1;i<s1->nb_sections;i++) {
1349 s = sections[section_order[i]];1362 s = s1->sections[section_order[i]];
1350 if (s->sh_type != SHT_NOBITS) {1363 if (s->sh_type != SHT_NOBITS) {
1351 while (offset < s->sh_offset) {1364 while (offset < s->sh_offset) {
1352 fputc(0, f);1365 fputc(0, f);
@@ -1363,10 +1376,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines
1363 }1376 }
1364 1377
1365 /* output section headers */1378 /* output section headers */
1366 for(i=0;i<nb_sections;i++) {1379 for(i=0;i<s1->nb_sections;i++) {
1367 sh = &shdr;1380 sh = &shdr;
1368 memset(sh, 0, sizeof(Elf32_Shdr));1381 memset(sh, 0, sizeof(Elf32_Shdr));
1369 s = sections[i];1382 s = s1->sections[i];
1370 if (s) {1383 if (s) {
1371 sh->sh_name = s->sh_name;1384 sh->sh_name = s->sh_name;
1372 sh->sh_type = s->sh_type;1385 sh->sh_type = s->sh_type;
@@ -1384,9 +1397,13 @@ int tcc_output_file(TCCState *s1, const char *filename) · 11 unmodified lines
1384 }1397 }
1385 fclose(f);1398 fclose(f);
1386 1399
1400 ret = 0;
1401 the_end:
1402 tcc_free(s1->symtab_to_dynsym);
1387 tcc_free(section_order);1403 tcc_free(section_order);
1388 tcc_free(phdr);1404 tcc_free(phdr);
1389 return 0;1405 tcc_free(s1->got_offsets);
1406 return ret;
1390}1407}
1391 1408
1392static void *load_data(int fd, unsigned long file_offset, unsigned long size)1409static void *load_data(int fd, unsigned long file_offset, unsigned long size)
@@ -1412,7 +1429,7 @@ static int tcc_load_object_file(TCCState *s1, · 19 unmodified lines
1412{ 1429{
1413 Elf32_Ehdr ehdr;1430 Elf32_Ehdr ehdr;
1414 Elf32_Shdr *shdr, *sh;1431 Elf32_Shdr *shdr, *sh;
1415 int size, i, j, offset, offseti, nb_syms, sym_index;1432 int size, i, j, offset, offseti, nb_syms, sym_index, ret;
1416 unsigned char *strsec, *strtab;1433 unsigned char *strsec, *strtab;
1417 int *old_to_new_syms;1434 int *old_to_new_syms;
1418 char *sh_name, *name;1435 char *sh_name, *name;
@@ -1422,20 +1439,21 @@ static int tcc_load_object_file(TCCState *s1, · 3 unmodified lines
1422 Section *s;1439 Section *s;
1423 1440
1424 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))1441 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
1425 goto fail;1442 goto fail1;
1426 if (ehdr.e_ident[0] != ELFMAG0 ||1443 if (ehdr.e_ident[0] != ELFMAG0 ||
1427 ehdr.e_ident[1] != ELFMAG1 ||1444 ehdr.e_ident[1] != ELFMAG1 ||
1428 ehdr.e_ident[2] != ELFMAG2 ||1445 ehdr.e_ident[2] != ELFMAG2 ||
1429 ehdr.e_ident[3] != ELFMAG3)1446 ehdr.e_ident[3] != ELFMAG3)
1430 goto fail;1447 goto fail1;
1431 /* test if object file */1448 /* test if object file */
1432 if (ehdr.e_type != ET_REL)1449 if (ehdr.e_type != ET_REL)
1433 goto fail;1450 goto fail1;
1434 /* test CPU specific stuff */1451 /* test CPU specific stuff */
1435 if (ehdr.e_ident[5] != ELFDATA2LSB ||1452 if (ehdr.e_ident[5] != ELFDATA2LSB ||
1436 ehdr.e_machine != EM_386) {1453 ehdr.e_machine != EM_386) {
1437 fail:1454 fail1:
1438 error("invalid object file");1455 error_noabort("invalid object file");
1456 return -1;
1439 }1457 }
1440 /* read sections */1458 /* read sections */
1441 shdr = load_data(fd, file_offset + ehdr.e_shoff, 1459 shdr = load_data(fd, file_offset + ehdr.e_shoff,
@@ -1447,14 +1465,19 @@ static int tcc_load_object_file(TCCState *s1, · 5 unmodified lines
1447 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);1465 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
1448 1466
1449 /* load symtab and strtab */1467 /* load symtab and strtab */
1468 old_to_new_syms = NULL;
1450 symtab = NULL;1469 symtab = NULL;
1451 strtab = NULL;1470 strtab = NULL;
1452 nb_syms = 0;1471 nb_syms = 0;
1453 for(i = 1; i < ehdr.e_shnum; i++) {1472 for(i = 1; i < ehdr.e_shnum; i++) {
1454 sh = &shdr[i];1473 sh = &shdr[i];
1455 if (sh->sh_type == SHT_SYMTAB) {1474 if (sh->sh_type == SHT_SYMTAB) {
1456 if (symtab)1475 if (symtab) {
1457 error("object must contain only one symtab");1476 error_noabort("object must contain only one symtab");
1477 fail:
1478 ret = -1;
1479 goto the_end;
1480 }
1458 nb_syms = sh->sh_size / sizeof(Elf32_Sym);1481 nb_syms = sh->sh_size / sizeof(Elf32_Sym);
1459 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);1482 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
1460 sm_table[i].s = symtab_section;1483 sm_table[i].s = symtab_section;
@@ -1481,21 +1504,23 @@ static int tcc_load_object_file(TCCState *s1, · 20 unmodified lines
1481 if (sh->sh_addralign < 1)1504 if (sh->sh_addralign < 1)
1482 sh->sh_addralign = 1;1505 sh->sh_addralign = 1;
1483 /* find corresponding section, if any */1506 /* find corresponding section, if any */
1484 for(j = 1; j < nb_sections;j++) {1507 for(j = 1; j < s1->nb_sections;j++) {
1485 s = sections[j];1508 s = s1->sections[j];
1486 if (!strcmp(s->name, sh_name))1509 if (!strcmp(s->name, sh_name))
1487 goto found;1510 goto found;
1488 }1511 }
1489 /* not found: create new section */1512 /* not found: create new section */
1490 s = new_section(sh_name, sh->sh_type, sh->sh_flags);1513 s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags);
1491 /* take as much info as possible from the section. sh_link and1514 /* take as much info as possible from the section. sh_link and
1492 sh_info will be updated later */1515 sh_info will be updated later */
1493 s->sh_addralign = sh->sh_addralign;1516 s->sh_addralign = sh->sh_addralign;
1494 s->sh_entsize = sh->sh_entsize;1517 s->sh_entsize = sh->sh_entsize;
1495 sm_table[i].new_section = 1;1518 sm_table[i].new_section = 1;
1496 found:1519 found:
1497 if (sh->sh_type != s->sh_type)1520 if (sh->sh_type != s->sh_type) {
1521 error_noabort("invalid section type");
1498 goto fail;1522 goto fail;
1523 }
1499 1524
1500 /* align start of section */1525 /* align start of section */
1501 offset = s->data_offset;1526 offset = s->data_offset;
@@ -1531,7 +1556,7 @@ static int tcc_load_object_file(TCCState *s1, · 29 unmodified lines
1531 if (sh->sh_type == SHT_REL) {1556 if (sh->sh_type == SHT_REL) {
1532 s->sh_info = sm_table[sh->sh_info].s->sh_num;1557 s->sh_info = sm_table[sh->sh_info].s->sh_num;
1533 /* update backward link */1558 /* update backward link */
1534 sections[s->sh_info]->reloc = s;1559 s1->sections[s->sh_info]->reloc = s;
1535 }1560 }
1536 }1561 }
1537 1562
@@ -1584,7 +1609,8 @@ static int tcc_load_object_file(TCCState *s1, · 46 unmodified lines
1584 sym_index = old_to_new_syms[sym_index];1609 sym_index = old_to_new_syms[sym_index];
1585 if (!sym_index) {1610 if (!sym_index) {
1586 invalid_reloc:1611 invalid_reloc:
1587 error("Invalid relocation entry");1612 error_noabort("Invalid relocation entry");
1613 goto fail;
1588 }1614 }
1589 rel->r_info = ELF32_R_INFO(sym_index, type);1615 rel->r_info = ELF32_R_INFO(sym_index, type);
1590 /* offset the relocation offset */1616 /* offset the relocation offset */
@@ -1595,12 +1621,16 @@ static int tcc_load_object_file(TCCState *s1, · 4 unmodified lines
1595 break;1621 break;
1596 }1622 }
1597 }1623 }
1624
1625 ret = 0;
1626 the_end:
1598 tcc_free(symtab);1627 tcc_free(symtab);
1599 tcc_free(strtab);1628 tcc_free(strtab);
1600 tcc_free(old_to_new_syms);1629 tcc_free(old_to_new_syms);
1601 tcc_free(sm_table);1630 tcc_free(sm_table);
1631 tcc_free(strsec);
1602 tcc_free(shdr);1632 tcc_free(shdr);
1603 return 0;1633 return ret;
1604}1634}
1605 1635
1606#define ARMAG "!<arch>\012" /* For COFF and a.out archives */1636#define ARMAG "!<arch>\012" /* For COFF and a.out archives */
@@ -1632,8 +1662,10 @@ static int tcc_load_archive(TCCState *s1, int fd) · 25 unmodified lines
1632 len = read(fd, &hdr, sizeof(hdr));1662 len = read(fd, &hdr, sizeof(hdr));
1633 if (len == 0)1663 if (len == 0)
1634 break;1664 break;
1635 if (len != sizeof(hdr))1665 if (len != sizeof(hdr)) {
1636 error("invalid archive");1666 error_noabort("invalid archive");
1667 return -1;
1668 }
1637 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));1669 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
1638 ar_size[sizeof(hdr.ar_size)] = '\0';1670 ar_size[sizeof(hdr.ar_size)] = '\0';
1639 size = strtol(ar_size, NULL, 0);1671 size = strtol(ar_size, NULL, 0);
@@ -1652,7 +1684,8 @@ static int tcc_load_archive(TCCState *s1, int fd) · 12 unmodified lines
1652 !strcmp(ar_name, "ARFILENAMES/")) {1684 !strcmp(ar_name, "ARFILENAMES/")) {
1653 /* skip symbol table or archive names */1685 /* skip symbol table or archive names */
1654 } else {1686 } else {
1655 tcc_load_object_file(s1, fd, file_offset);1687 if (tcc_load_object_file(s1, fd, file_offset) < 0)
1688 return -1;
1656 }1689 }
1657 /* align to even */1690 /* align to even */
1658 size = (size + 1) & ~1;1691 size = (size + 1) & ~1;
@@ -1668,7 +1701,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 9 unmodified lines
1668{ 1701{
1669 Elf32_Ehdr ehdr;1702 Elf32_Ehdr ehdr;
1670 Elf32_Shdr *shdr, *sh, *sh1;1703 Elf32_Shdr *shdr, *sh, *sh1;
1671 int i, nb_syms, nb_dts, sym_bind;1704 int i, nb_syms, nb_dts, sym_bind, ret;
1672 Elf32_Sym *sym, *dynsym;1705 Elf32_Sym *sym, *dynsym;
1673 Elf32_Dyn *dt, *dynamic;1706 Elf32_Dyn *dt, *dynamic;
1674 unsigned char *dynstr;1707 unsigned char *dynstr;
@@ -1679,8 +1712,10 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 4 unmodified lines
1679 1712
1680 /* test CPU specific stuff */1713 /* test CPU specific stuff */
1681 if (ehdr.e_ident[5] != ELFDATA2LSB ||1714 if (ehdr.e_ident[5] != ELFDATA2LSB ||
1682 ehdr.e_machine != EM_386)1715 ehdr.e_machine != EM_386) {
1683 error("bad architecture");1716 error_noabort("bad architecture");
1717 return -1;
1718 }
1684 1719
1685 /* read sections */1720 /* read sections */
1686 shdr = load_data(fd, ehdr.e_shoff, sizeof(Elf32_Shdr) * ehdr.e_shnum);1721 shdr = load_data(fd, ehdr.e_shoff, sizeof(Elf32_Shdr) * ehdr.e_shnum);
@@ -1721,12 +1756,13 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 34 unmodified lines
1721 }1756 }
1722 1757
1723 /* if the dll is already loaded, do not load it */1758 /* if the dll is already loaded, do not load it */
1724 for(i = 0; i < nb_loaded_dlls; i++) {1759 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1725 dllref = loaded_dlls[i];1760 dllref = s1->loaded_dlls[i];
1726 if (!strcmp(soname, dllref->name)) {1761 if (!strcmp(soname, dllref->name)) {
1727 /* but update level if needed */1762 /* but update level if needed */
1728 if (level < dllref->level)1763 if (level < dllref->level)
1729 dllref->level = level;1764 dllref->level = level;
1765 ret = 0;
1730 goto the_end;1766 goto the_end;
1731 }1767 }
1732 }1768 }
@@ -1737,7 +1773,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 4 unmodified lines
1737 dllref = tcc_malloc(sizeof(DLLReference) + strlen(soname));1773 dllref = tcc_malloc(sizeof(DLLReference) + strlen(soname));
1738 dllref->level = level;1774 dllref->level = level;
1739 strcpy(dllref->name, soname);1775 strcpy(dllref->name, soname);
1740 dynarray_add((void ***)&loaded_dlls, &nb_loaded_dlls, dllref);1776 dynarray_add((void ***)&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
1741 1777
1742 /* add dynamic symbols in dynsym_section */1778 /* add dynamic symbols in dynsym_section */
1743 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {1779 for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
@@ -1745,7 +1781,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 1 unmodified line
1745 if (sym_bind == STB_LOCAL)1781 if (sym_bind == STB_LOCAL)
1746 continue;1782 continue;
1747 name = dynstr + sym->st_name;1783 name = dynstr + sym->st_name;
1748 add_elf_sym(dynsymtab_section, sym->st_value, sym->st_size,1784 add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
1749 sym->st_info, sym->st_shndx, name);1785 sym->st_info, sym->st_shndx, name);
1750 }1786 }
1751 1787
@@ -1754,20 +1790,27 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 2 unmodified lines
1754 switch(dt->d_tag) {1790 switch(dt->d_tag) {
1755 case DT_NEEDED:1791 case DT_NEEDED:
1756 name = dynstr + dt->d_un.d_val;1792 name = dynstr + dt->d_un.d_val;
1757 for(i = 0; i < nb_loaded_dlls; i++) {1793 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1758 dllref = loaded_dlls[i];1794 dllref = s1->loaded_dlls[i];
1759 if (!strcmp(name, dllref->name))1795 if (!strcmp(name, dllref->name))
1760 goto already_loaded;1796 goto already_loaded;
1761 }1797 }
1762 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0)1798 if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
1763 error("referenced dll '%s' not found", name);1799 error_noabort("referenced dll '%s' not found", name);
1800 ret = -1;
1801 goto the_end;
1802 }
1764 already_loaded:1803 already_loaded:
1765 break;1804 break;
1766 }1805 }
1767 }1806 }
1807 ret = 0;
1768 the_end:1808 the_end:
1809 tcc_free(dynstr);
1810 tcc_free(dynsym);
1811 tcc_free(dynamic);
1769 tcc_free(shdr);1812 tcc_free(shdr);
1770 return 0;1813 return ret;
1771}1814}
1772 1815
1773/* return -2 if error and CH_EOF if eof */1816/* return -2 if error and CH_EOF if eof */
@@ -1833,7 +1876,8 @@ static int tcc_load_ldscript(TCCState *s1) · 59 unmodified lines
1833 cinp();1876 cinp();
1834 break;1877 break;
1835 } else if (ch == CH_EOF) {1878 } else if (ch == CH_EOF) {
1836 error("unexpected end of file");1879 error_noabort("unexpected end of file");
1880 return -1;
1837 }1881 }
1838 }1882 }
1839 } else {1883 } else {
@@ -1842,4 +1886,3 @@ static int tcc_load_ldscript(TCCState *s1) · 2 unmodified lines
1842 }1886 }
1843 return 0;1887 return 0;
1844}1888}
1845

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t