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
199199 sym_bind, sh_num, esym_bind, esym->st_shndx);
200200 #endif
201201 /* NOTE: we accept that two DLL define the same symbol */
202− if (s != dynsymtab_section)
203− error("'%s' defined twice", name);
202+ if (s != tcc_state->dynsymtab_section)
203+ error_noabort("'%s' defined twice", name);
204204 }
205205 } else {
206206 do_patch:
@@ -232,7 +232,7 @@ static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, · 25 unmodified lines
232232 snprintf(buf, sizeof(buf), ".rel%s", s->name);
233233 /* if the symtab is allocated, then we consider the relocation
234234 are also */
235− sr = new_section(buf, SHT_REL, symtab->sh_flags);
235+ sr = new_section(tcc_state, buf, SHT_REL, symtab->sh_flags);
236236 sr->sh_entsize = sizeof(Elf32_Rel);
237237 sr->link = symtab;
238238 sr->sh_info = s->sh_num;
@@ -293,7 +293,7 @@ static void put_stabd(int type, int other, int desc) · 54 unmodified lines
293293 the global and weak ones. Since TCC cannot sort it while generating
294294 the code, we must do it after. All the relocation tables are also
295295 modified to take into account the symbol table sorting */
296−static void sort_syms(Section *s)
296+static void sort_syms(TCCState *s1, Section *s)
297297 {
298298 int *old_to_new_syms;
299299 Elf32_Sym *new_syms;
@@ -335,8 +335,8 @@ static void sort_syms(Section *s) · 35 unmodified lines
335335 tcc_free(new_syms);
336336
337337 /* now we modify all the relocations */
338− for(i = 1; i < nb_sections; i++) {
339− sr = sections[i];
338+ for(i = 1; i < s1->nb_sections; i++) {
339+ sr = s1->sections[i];
340340 if (sr->sh_type == SHT_REL && sr->link == s) {
341341 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
342342 for(rel = (Elf32_Rel *)sr->data;
@@ -383,7 +383,7 @@ static void *resolve_sym(const char *sym) · 40 unmodified lines
383383
384384 /* relocate symbol table, resolve undefined symbols if do_resolve is
385385 true and output error if undefined symbol. */
386−static void relocate_syms(int do_resolve)
386+static void relocate_syms(TCCState *s1, int do_resolve)
387387 {
388388 Elf32_Sym *sym, *esym, *sym_end;
389389 int sym_bind, sh_num, sym_index;
@@ -404,11 +404,11 @@ static void relocate_syms(int do_resolve) · 14 unmodified lines
404404 sym->st_value = addr;
405405 goto found;
406406 }
407− } else if (dynsym) {
407+ } else if (s1->dynsym) {
408408 /* if dynamic symbol exist, then use it */
409− sym_index = find_elf_sym(dynsym, name);
409+ sym_index = find_elf_sym(s1->dynsym, name);
410410 if (sym_index) {
411− esym = &((Elf32_Sym *)dynsym->data)[sym_index];
411+ esym = &((Elf32_Sym *)s1->dynsym->data)[sym_index];
412412 sym->st_value = esym->st_value;
413413 goto found;
414414 }
@@ -423,11 +423,11 @@ static void relocate_syms(int do_resolve) · 8 unmodified lines
423423 if (sym_bind == STB_WEAK) {
424424 sym->st_value = 0;
425425 } else {
426− error("undefined symbol '%s'", name);
426+ error_noabort("undefined symbol '%s'", name);
427427 }
428428 } else if (sh_num < SHN_LORESERVE) {
429429 /* add section base */
430− sym->st_value += sections[sym->st_shndx]->sh_addr;
430+ sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
431431 }
432432 found: ;
433433 }
@@ -461,7 +461,7 @@ static void relocate_section(TCCState *s1, Section *s) · 27 unmodified lines
461461 switch(type) {
462462 case R_386_32:
463463 if (s1->output_type == TCC_OUTPUT_DLL) {
464− esym_index = symtab_to_dynsym[sym_index];
464+ esym_index = s1->symtab_to_dynsym[sym_index];
465465 qrel->r_offset = rel->r_offset;
466466 if (esym_index) {
467467 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
477477 case R_386_PC32:
478478 if (s1->output_type == TCC_OUTPUT_DLL) {
479479 /* DLL relocation */
480− esym_index = symtab_to_dynsym[sym_index];
480+ esym_index = s1->symtab_to_dynsym[sym_index];
481481 if (esym_index) {
482482 qrel->r_offset = rel->r_offset;
483483 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
495495 *(int *)ptr = val;
496496 break;
497497 case R_386_GOTPC:
498− *(int *)ptr += got->sh_addr - addr;
498+ *(int *)ptr += s1->got->sh_addr - addr;
499499 break;
500500 case R_386_GOTOFF:
501− *(int *)ptr += val - got->sh_addr;
501+ *(int *)ptr += val - s1->got->sh_addr;
502502 break;
503503 case R_386_GOT32:
504504 /* we load the got offset */
505− *(int *)ptr += got_offsets[sym_index];
505+ *(int *)ptr += s1->got_offsets[sym_index];
506506 break;
507507 }
508508 }
509509 /* if the relocation is allocated, we change its symbol table */
510510 if (sr->sh_flags & SHF_ALLOC)
511− sr->link = dynsym;
511+ sr->link = s1->dynsym;
512512 }
513513
514514 /* relocate relocation table in 'sr' */
515−static void relocate_rel(Section *sr)
515+static void relocate_rel(TCCState *s1, Section *sr)
516516 {
517517 Section *s;
518518 Elf32_Rel *rel, *rel_end;
519519
520− s = sections[sr->sh_info];
520+ s = s1->sections[sr->sh_info];
521521 rel_end = (Elf32_Rel *)(sr->data + sr->data_offset);
522522 for(rel = (Elf32_Rel *)sr->data;
523523 rel < rel_end;
@@ -528,7 +528,7 @@ static void relocate_rel(Section *sr) · 4 unmodified lines
528528
529529 /* count the number of dynamic relocations so that we can reserve
530530 their space */
531−static int prepare_dynamic_rel(Section *sr)
531+static int prepare_dynamic_rel(TCCState *s1, Section *sr)
532532 {
533533 Elf32_Rel *rel, *rel_end;
534534 int sym_index, esym_index, type, count;
@@ -543,7 +543,7 @@ static int prepare_dynamic_rel(Section *sr) · 8 unmodified lines
543543 count++;
544544 break;
545545 case R_386_PC32:
546− esym_index = symtab_to_dynsym[sym_index];
546+ esym_index = s1->symtab_to_dynsym[sym_index];
547547 if (esym_index)
548548 count++;
549549 break;
@@ -559,25 +559,25 @@ static int prepare_dynamic_rel(Section *sr) · 9 unmodified lines
559559 return count;
560560 }
561561
562−static void put_got_offset(int index, unsigned long val)
562+static void put_got_offset(TCCState *s1, int index, unsigned long val)
563563 {
564564 int n;
565565 unsigned long *tab;
566566
567− if (index >= nb_got_offsets) {
567+ if (index >= s1->nb_got_offsets) {
568568 /* find immediately bigger power of 2 and reallocate array */
569569 n = 1;
570570 while (index >= n)
571571 n *= 2;
572− tab = tcc_realloc(got_offsets, n * sizeof(unsigned long));
572+ tab = tcc_realloc(s1->got_offsets, n * sizeof(unsigned long));
573573 if (!tab)
574574 error("memory full");
575− got_offsets = tab;
576− memset(got_offsets + nb_got_offsets, 0,
577− (n - nb_got_offsets) * sizeof(unsigned long));
578− nb_got_offsets = n;
575+ s1->got_offsets = tab;
576+ memset(s1->got_offsets + s1->nb_got_offsets, 0,
577+ (n - s1->nb_got_offsets) * sizeof(unsigned long));
578+ s1->nb_got_offsets = n;
579579 }
580− got_offsets[index] = val;
580+ s1->got_offsets[index] = val;
581581 }
582582
583583 /* XXX: suppress that */
@@ -589,16 +589,16 @@ static void put32(unsigned char *p, unsigned int val) · 5 unmodified lines
589589 p[3] = val >> 24;
590590 }
591591
592−static void build_got(void)
592+static void build_got(TCCState *s1)
593593 {
594594 unsigned char *ptr;
595595
596596 /* if no got, then create it */
597− got = new_section(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
598− got->sh_entsize = 4;
597+ s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
598+ s1->got->sh_entsize = 4;
599599 add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
600− got->sh_num, "_GLOBAL_OFFSET_TABLE_");
601− ptr = section_ptr_add(got, 3 * sizeof(int));
600+ s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
601+ ptr = section_ptr_add(s1->got, 3 * sizeof(int));
602602 /* keep space for _DYNAMIC pointer, if present */
603603 put32(ptr, 0);
604604 /* two dummy got entries */
@@ -608,7 +608,8 @@ static void build_got(void) · 3 unmodified lines
608608
609609 /* put a got entry corresponding to a symbol in symtab_section. 'size'
610610 and 'info' can be modifed if more precise info comes from the DLL */
611−static void put_got_entry(int reloc_type, unsigned long size, int info,
611+static void put_got_entry(TCCState *s1,
612+ int reloc_type, unsigned long size, int info,
612613 int sym_index)
613614 {
614615 int index;
@@ -617,46 +618,46 @@ static void put_got_entry(int reloc_type, unsigned long size, int info, · 2 unmodified lines
617618 unsigned long offset;
618619 int *ptr;
619620
620− if (!got)
621− build_got();
621+ if (!s1->got)
622+ build_got(s1);
622623
623624 /* if a got entry already exists for that symbol, no need to add one */
624− if (sym_index < nb_got_offsets &&
625− got_offsets[sym_index] != 0)
625+ if (sym_index < s1->nb_got_offsets &&
626+ s1->got_offsets[sym_index] != 0)
626627 return;
627628
628− put_got_offset(sym_index, got->data_offset);
629+ put_got_offset(s1, sym_index, s1->got->data_offset);
629630
630− if (dynsym) {
631+ if (s1->dynsym) {
631632 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
632633 name = symtab_section->link->data + sym->st_name;
633634 offset = sym->st_value;
634635 /* NOTE: we put temporarily the got offset */
635636 if (reloc_type == R_386_JMP_SLOT) {
636− nb_plt_entries++;
637− offset = got->data_offset;
637+ s1->nb_plt_entries++;
638+ offset = s1->got->data_offset;
638639 }
639− index = put_elf_sym(dynsym, offset,
640+ index = put_elf_sym(s1->dynsym, offset,
640641 size, info, 0, sym->st_shndx, name);
641642 /* put a got entry */
642− put_elf_reloc(dynsym, got,
643− got->data_offset,
643+ put_elf_reloc(s1->dynsym, s1->got,
644+ s1->got->data_offset,
644645 reloc_type, index);
645646 }
646− ptr = section_ptr_add(got, sizeof(int));
647+ ptr = section_ptr_add(s1->got, sizeof(int));
647648 *ptr = 0;
648649 }
649650
650651 /* build GOT and PLT entries */
651−static void build_got_entries(void)
652+static void build_got_entries(TCCState *s1)
652653 {
653654 Section *s, *symtab;
654655 Elf32_Rel *rel, *rel_end;
655656 Elf32_Sym *sym;
656657 int i, type, reloc_type, sym_index;
657658
658− for(i = 1; i < nb_sections; i++) {
659− s = sections[i];
659+ for(i = 1; i < s1->nb_sections; i++) {
660+ s = s1->sections[i];
660661 if (s->sh_type != SHT_REL)
661662 continue;
662663 /* no need to handle got relocations */
@@ -673,8 +674,8 @@ static void build_got_entries(void) · 10 unmodified lines
673674 case R_386_GOTOFF:
674675 case R_386_GOTPC:
675676 case R_386_PLT32:
676− if (!got)
677− build_got();
677+ if (!s1->got)
678+ build_got(s1);
678679 if (type == R_386_GOT32 || type == R_386_PLT32) {
679680 sym_index = ELF32_R_SYM(rel->r_info);
680681 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
@@ -683,7 +684,7 @@ static void build_got_entries(void) · 2 unmodified lines
683684 reloc_type = R_386_GLOB_DAT;
684685 else
685686 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,
687688 sym_index);
688689 }
689690 break;
@@ -694,23 +695,24 @@ static void build_got_entries(void) · 4 unmodified lines
694695 }
695696 }
696697
697−static Section *new_symtab(const char *symtab_name, int sh_type, int sh_flags,
698+static Section *new_symtab(TCCState *s1,
699+ const char *symtab_name, int sh_type, int sh_flags,
698700 const char *strtab_name,
699701 const char *hash_name, int hash_sh_flags)
700702 {
701703 Section *symtab, *strtab, *hash;
702704 int *ptr, nb_buckets;
703705
704− symtab = new_section(symtab_name, sh_type, sh_flags);
706+ symtab = new_section(s1, symtab_name, sh_type, sh_flags);
705707 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);
707709 put_elf_str(strtab, "");
708710 symtab->link = strtab;
709711 put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
710712
711713 nb_buckets = 1;
712714
713− hash = new_section(hash_name, SHT_HASH, hash_sh_flags);
715+ hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
714716 hash->sh_entsize = sizeof(int);
715717 symtab->hash = hash;
716718 hash->link = symtab;
@@ -759,7 +761,7 @@ static void tcc_add_runtime(TCCState *s1) · 42 unmodified lines
759761 #ifdef TCC_TARGET_I386
760762 if (s1->output_type != TCC_OUTPUT_MEMORY) {
761763 /* add 'call __bound_init()' in .init section */
762− init_section = find_section(".init");
764+ init_section = find_section(s1, ".init");
763765 pinit = section_ptr_add(init_section, 5);
764766 pinit[0] = 0xe8;
765767 put32(pinit + 1, -4);
@@ -790,8 +792,8 @@ static void tcc_add_runtime(TCCState *s1) · 24 unmodified lines
790792 bss_section->sh_num, "_end");
791793 /* add start and stop symbols for sections whose name can be
792794 expressed in C */
793− for(i = 1; i < nb_sections; i++) {
794− s = sections[i];
795+ for(i = 1; i < s1->nb_sections; i++) {
796+ s = s1->sections[i];
795797 if (s->sh_type == SHT_PROGBITS &&
796798 (s->sh_flags & SHF_ALLOC)) {
797799 const char *p;
@@ -834,7 +836,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 36 unmodified lines
834836 {
835837 Elf32_Ehdr ehdr;
836838 FILE *f;
837− int fd, mode;
839+ int fd, mode, ret;
838840 int *section_order;
839841 int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k;
840842 unsigned long addr;
@@ -848,24 +850,24 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
848850 unsigned long rel_addr, rel_size;
849851
850852 file_type = s1->output_type;
853+ s1->nb_errors = 0;
851854
852855 if (file_type != TCC_OUTPUT_OBJ)
853856 tcc_add_runtime(s1);
854857
858+ phdr = NULL;
859+ section_order = NULL;
855860 interp = NULL;
856861 dynamic = NULL;
857− dynsym = NULL;
858− got = NULL;
859− nb_plt_entries = 0;
860862 plt = NULL; /* avoid warning */
861863 dynstr = NULL; /* avoid warning */
862864 saved_dynamic_data_offset = 0; /* avoid warning */
863−
865+
864866 if (file_type != TCC_OUTPUT_OBJ) {
865867
866868 relocate_common_syms();
867869
868− if (!static_link) {
870+ if (!s1->static_link) {
869871 const char *name;
870872 int sym_index, index;
871873 Elf32_Sym *esym, *sym_end;
@@ -873,29 +875,29 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1 unmodified line
873875 if (file_type == TCC_OUTPUT_EXE) {
874876 char *ptr;
875877 /* 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);
877879 interp->sh_addralign = 1;
878880 ptr = section_ptr_add(interp, sizeof(elf_interp));
879881 strcpy(ptr, elf_interp);
880882 }
881883
882884 /* add dynamic symbol table */
883− dynsym = new_symtab(".dynsym", SHT_DYNSYM, SHF_ALLOC,
884− ".dynstr",
885− ".hash", SHF_ALLOC);
886− dynstr = dynsym->link;
885+ s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
886+ ".dynstr",
887+ ".hash", SHF_ALLOC);
888+ dynstr = s1->dynsym->link;
887889
888890 /* add dynamic section */
889− dynamic = new_section(".dynamic", SHT_DYNAMIC,
891+ dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
890892 SHF_ALLOC | SHF_WRITE);
891893 dynamic->link = dynstr;
892894 dynamic->sh_entsize = sizeof(Elf32_Dyn);
893895
894896 /* 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);
896898 plt->sh_entsize = 4;
897899
898− build_got();
900+ build_got(s1);
899901
900902 /* scan for undefined symbols and see if they are in the
901903 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
909911 sym++) {
910912 if (sym->st_shndx == SHN_UNDEF) {
911913 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);
913915 if (sym_index) {
914− esym = &((Elf32_Sym *)dynsymtab_section->data)[sym_index];
916+ esym = &((Elf32_Sym *)s1->dynsymtab_section->data)[sym_index];
915917 type = ELF32_ST_TYPE(esym->st_info);
916918 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,
918920 esym->st_info,
919921 sym - (Elf32_Sym *)symtab_section->data);
920922 } else if (type == STT_OBJECT) {
@@ -922,10 +924,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1 unmodified line
922924 offset = bss_section->data_offset;
923925 /* XXX: which alignment ? */
924926 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,
926928 esym->st_info, 0,
927929 bss_section->sh_num, name);
928− put_elf_reloc(dynsym, bss_section,
930+ put_elf_reloc(s1->dynsym, bss_section,
929931 offset, R_386_COPY, index);
930932 offset += esym->st_size;
931933 bss_section->data_offset = offset;
@@ -937,25 +939,28 @@ int tcc_output_file(TCCState *s1, const char *filename) · 5 unmodified lines
937939 if (ELF32_ST_BIND(sym->st_info) == STB_WEAK ||
938940 !strcmp(name, "_fp_hw")) {
939941 } else {
940− error("undefined symbol '%s'", name);
942+ error_noabort("undefined symbol '%s'", name);
941943 }
942944 }
943945 }
944946 }
945947
948+ if (s1->nb_errors)
949+ goto fail;
950+
946951 /* now look at unresolved dynamic symbols and export
947952 corresponding symbol */
948− sym_end = (Elf32_Sym *)(dynsymtab_section->data +
949− dynsymtab_section->data_offset);
950− for(esym = (Elf32_Sym *)dynsymtab_section->data + 1;
953+ sym_end = (Elf32_Sym *)(s1->dynsymtab_section->data +
954+ s1->dynsymtab_section->data_offset);
955+ for(esym = (Elf32_Sym *)s1->dynsymtab_section->data + 1;
951956 esym < sym_end;
952957 esym++) {
953958 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;
955960 sym_index = find_elf_sym(symtab_section, name);
956961 if (sym_index) {
957962 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,
959964 sym->st_info, 0,
960965 sym->st_shndx, name);
961966 } else {
@@ -971,29 +976,30 @@ int tcc_output_file(TCCState *s1, const char *filename) · 9 unmodified lines
971976 int nb_syms;
972977 /* shared library case : we simply export all the global symbols */
973978 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);
975980 for(sym = (Elf32_Sym *)symtab_section->data + 1;
976981 sym < sym_end;
977982 sym++) {
978983 if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
979984 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,
981986 sym->st_info, 0,
982987 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] =
984990 index;
985991 }
986992 }
987993 }
988994
989− build_got_entries();
995+ build_got_entries(s1);
990996
991997 /* 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);
993999
9941000 /* add a list of needed dlls */
995− for(i = 0; i < nb_loaded_dlls; i++) {
996− DLLReference *dllref = loaded_dlls[i];
1001+ for(i = 0; i < s1->nb_loaded_dlls; i++) {
1002+ DLLReference *dllref = s1->loaded_dlls[i];
9971003 if (dllref->level == 0)
9981004 put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
9991005 }
@@ -1007,18 +1013,18 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
10071013 dynamic->data_offset += 8 * 9;
10081014 } else {
10091015 /* still need to build got entries in case of static link */
1010− build_got_entries();
1016+ build_got_entries(s1);
10111017 }
10121018 }
10131019
10141020 memset(&ehdr, 0, sizeof(ehdr));
10151021
10161022 /* we add a section for symbols */
1017− strsec = new_section(".shstrtab", SHT_STRTAB, 0);
1023+ strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
10181024 put_elf_str(strsec, "");
10191025
10201026 /* compute number of sections */
1021− shnum = nb_sections;
1027+ shnum = s1->nb_sections;
10221028
10231029 /* this array is used to reorder sections in the output file */
10241030 section_order = tcc_malloc(sizeof(int) * shnum);
@@ -1032,7 +1038,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
10321038 phnum = 0;
10331039 break;
10341040 case TCC_OUTPUT_EXE:
1035− if (!static_link)
1041+ if (!s1->static_link)
10361042 phnum = 4;
10371043 else
10381044 phnum = 2;
@@ -1046,19 +1052,19 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines
10461052 section should be output */
10471053 /* NOTE: the strsec section comes last, so its size is also
10481054 correct ! */
1049− for(i = 1; i < nb_sections; i++) {
1050− s = sections[i];
1055+ for(i = 1; i < s1->nb_sections; i++) {
1056+ s = s1->sections[i];
10511057 s->sh_name = put_elf_str(strsec, s->name);
10521058 /* when generating a DLL, we include relocations but we may
10531059 patch them */
10541060 if (file_type == TCC_OUTPUT_DLL &&
10551061 s->sh_type == SHT_REL &&
10561062 !(s->sh_flags & SHF_ALLOC)) {
1057− prepare_dynamic_rel(s);
1063+ prepare_dynamic_rel(s1, s);
10581064 } else if (do_debug ||
10591065 file_type == TCC_OUTPUT_OBJ ||
10601066 (s->sh_flags & SHF_ALLOC) ||
1061− i == (nb_sections - 1)) {
1067+ i == (s1->nb_sections - 1)) {
10621068 /* we output all sections if debug or object file */
10631069 s->sh_size = s->data_offset;
10641070 }
@@ -1099,8 +1105,8 @@ int tcc_output_file(TCCState *s1, const char *filename) · 34 unmodified lines
10991105 relocations, progbits, nobits */
11001106 /* XXX: do faster and simpler sorting */
11011107 for(k = 0; k < 5; k++) {
1102− for(i = 1; i < nb_sections; i++) {
1103− s = sections[i];
1108+ for(i = 1; i < s1->nb_sections; i++) {
1109+ s = s1->sections[i];
11041110 /* compute if section should be included */
11051111 if (j == 0) {
11061112 if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
@@ -1193,7 +1199,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 86 unmodified lines
11931199 ph->p_align = dynamic->sh_addralign;
11941200
11951201 /* put GOT dynamic section address */
1196− put32(got->data, dynamic->sh_addr);
1202+ put32(s1->got->data, dynamic->sh_addr);
11971203
11981204 /* compute the PLT */
11991205 plt->data_offset = 0;
@@ -1202,15 +1208,15 @@ int tcc_output_file(TCCState *s1, const char *filename) · 2 unmodified lines
12021208 p = section_ptr_add(plt, 16);
12031209 p[0] = 0xff; /* pushl got + 4 */
12041210 p[1] = 0x35;
1205− put32(p + 2, got->sh_addr + 4);
1211+ put32(p + 2, s1->got->sh_addr + 4);
12061212 p[6] = 0xff; /* jmp *(got + 8) */
12071213 p[7] = 0x25;
1208− put32(p + 8, got->sh_addr + 8);
1214+ put32(p + 8, s1->got->sh_addr + 8);
12091215
12101216 /* relocation symbols in .dynsym and build PLT. */
12111217 plt_offset = 0;
1212− sym_end = (Elf32_Sym *)(dynsym->data + dynsym->data_offset);
1213− for(sym = (Elf32_Sym *)dynsym->data + 1;
1218+ sym_end = (Elf32_Sym *)(s1->dynsym->data + s1->dynsym->data_offset);
1219+ for(sym = (Elf32_Sym *)s1->dynsym->data + 1;
12141220 sym < sym_end;
12151221 sym++) {
12161222 type = ELF32_ST_TYPE(sym->st_info);
@@ -1220,7 +1226,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 3 unmodified lines
12201226 p = section_ptr_add(plt, 16);
12211227 p[0] = 0xff; /* jmp *(got + x) */
12221228 p[1] = 0x25;
1223− put32(p + 2, got->sh_addr + sym->st_value);
1229+ put32(p + 2, s1->got->sh_addr + sym->st_value);
12241230 p[6] = 0x68; /* push $xxx */
12251231 put32(p + 7, plt_offset);
12261232 p[11] = 0xe9; /* jmp plt_start */
@@ -1233,15 +1239,15 @@ int tcc_output_file(TCCState *s1, const char *filename) · 6 unmodified lines
12331239 }
12341240 } else if (sym->st_shndx < SHN_LORESERVE) {
12351241 /* do symbol relocation */
1236− sym->st_value += sections[sym->st_shndx]->sh_addr;
1242+ sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
12371243 }
12381244 }
12391245 /* put dynamic section entries */
12401246
12411247 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);
12431249 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);
12451251 put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
12461252 put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym));
12471253 put_dt(dynamic, DT_REL, rel_addr);
@@ -1256,8 +1262,8 @@ int tcc_output_file(TCCState *s1, const char *filename) · 8 unmodified lines
12561262 }
12571263
12581264 /* all other sections come after */
1259− for(i = 1; i < nb_sections; i++) {
1260− s = sections[i];
1265+ for(i = 1; i < s1->nb_sections; i++) {
1266+ s = s1->sections[i];
12611267 if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
12621268 continue;
12631269 section_order[sh_order_index++] = i;
@@ -1272,23 +1278,29 @@ int tcc_output_file(TCCState *s1, const char *filename) · 8 unmodified lines
12721278 /* if building executable or DLL, then relocate each section
12731279 except the GOT which is already relocated */
12741280 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+ }
12761288
12771289 /* relocate sections */
12781290 /* XXX: ignore sections with allocated relocations ? */
1279− for(i = 1; i < nb_sections; i++) {
1280− s = sections[i];
1281− if (s->reloc && s != got)
1291+ for(i = 1; i < s1->nb_sections; i++) {
1292+ s = s1->sections[i];
1293+ if (s->reloc && s != s1->got)
12821294 relocate_section(s1, s);
12831295 }
12841296
12851297 /* relocate relocation entries if the relocation tables are
12861298 allocated in the executable */
1287− for(i = 1; i < nb_sections; i++) {
1288− s = sections[i];
1299+ for(i = 1; i < s1->nb_sections; i++) {
1300+ s = s1->sections[i];
12891301 if ((s->sh_flags & SHF_ALLOC) &&
12901302 s->sh_type == SHT_REL) {
1291− relocate_rel(s);
1303+ relocate_rel(s1, s);
12921304 }
12931305 }
12941306
@@ -1299,7 +1311,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 4 unmodified lines
12991311 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
13001312 }
13011313
1302− sort_syms(symtab_section);
1314+ sort_syms(s1, symtab_section);
13031315
13041316 /* align to 4 */
13051317 file_offset = (file_offset + 3) & -4;
@@ -1338,15 +1350,16 @@ int tcc_output_file(TCCState *s1, const char *filename) · 32 unmodified lines
13381350 else
13391351 mode = 0777;
13401352 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
1341− if (fd < 0)
1342− error("could not write '%s'", filename);
1343−
1353+ if (fd < 0) {
1354+ error_noabort("could not write '%s'", filename);
1355+ goto fail;
1356+ }
13441357 f = fdopen(fd, "w");
13451358 fwrite(&ehdr, 1, sizeof(Elf32_Ehdr), f);
13461359 fwrite(phdr, 1, phnum * sizeof(Elf32_Phdr), f);
13471360 offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr);
1348− for(i=1;i<nb_sections;i++) {
1349− s = sections[section_order[i]];
1361+ for(i=1;i<s1->nb_sections;i++) {
1362+ s = s1->sections[section_order[i]];
13501363 if (s->sh_type != SHT_NOBITS) {
13511364 while (offset < s->sh_offset) {
13521365 fputc(0, f);
@@ -1363,10 +1376,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines
13631376 }
13641377
13651378 /* output section headers */
1366− for(i=0;i<nb_sections;i++) {
1379+ for(i=0;i<s1->nb_sections;i++) {
13671380 sh = &shdr;
13681381 memset(sh, 0, sizeof(Elf32_Shdr));
1369− s = sections[i];
1382+ s = s1->sections[i];
13701383 if (s) {
13711384 sh->sh_name = s->sh_name;
13721385 sh->sh_type = s->sh_type;
@@ -1384,9 +1397,13 @@ int tcc_output_file(TCCState *s1, const char *filename) · 11 unmodified lines
13841397 }
13851398 fclose(f);
13861399
1400+ ret = 0;
1401+ the_end:
1402+ tcc_free(s1->symtab_to_dynsym);
13871403 tcc_free(section_order);
13881404 tcc_free(phdr);
1389− return 0;
1405+ tcc_free(s1->got_offsets);
1406+ return ret;
13901407 }
13911408
13921409 static 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
14121429 {
14131430 Elf32_Ehdr ehdr;
14141431 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;
14161433 unsigned char *strsec, *strtab;
14171434 int *old_to_new_syms;
14181435 char *sh_name, *name;
@@ -1422,20 +1439,21 @@ static int tcc_load_object_file(TCCState *s1, · 3 unmodified lines
14221439 Section *s;
14231440
14241441 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
1425− goto fail;
1442+ goto fail1;
14261443 if (ehdr.e_ident[0] != ELFMAG0 ||
14271444 ehdr.e_ident[1] != ELFMAG1 ||
14281445 ehdr.e_ident[2] != ELFMAG2 ||
14291446 ehdr.e_ident[3] != ELFMAG3)
1430− goto fail;
1447+ goto fail1;
14311448 /* test if object file */
14321449 if (ehdr.e_type != ET_REL)
1433− goto fail;
1450+ goto fail1;
14341451 /* test CPU specific stuff */
14351452 if (ehdr.e_ident[5] != ELFDATA2LSB ||
14361453 ehdr.e_machine != EM_386) {
1437− fail:
1438− error("invalid object file");
1454+ fail1:
1455+ error_noabort("invalid object file");
1456+ return -1;
14391457 }
14401458 /* read sections */
14411459 shdr = load_data(fd, file_offset + ehdr.e_shoff,
@@ -1447,14 +1465,19 @@ static int tcc_load_object_file(TCCState *s1, · 5 unmodified lines
14471465 strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
14481466
14491467 /* load symtab and strtab */
1468+ old_to_new_syms = NULL;
14501469 symtab = NULL;
14511470 strtab = NULL;
14521471 nb_syms = 0;
14531472 for(i = 1; i < ehdr.e_shnum; i++) {
14541473 sh = &shdr[i];
14551474 if (sh->sh_type == SHT_SYMTAB) {
1456− if (symtab)
1457− error("object must contain only one symtab");
1475+ if (symtab) {
1476+ error_noabort("object must contain only one symtab");
1477+ fail:
1478+ ret = -1;
1479+ goto the_end;
1480+ }
14581481 nb_syms = sh->sh_size / sizeof(Elf32_Sym);
14591482 symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
14601483 sm_table[i].s = symtab_section;
@@ -1481,21 +1504,23 @@ static int tcc_load_object_file(TCCState *s1, · 20 unmodified lines
14811504 if (sh->sh_addralign < 1)
14821505 sh->sh_addralign = 1;
14831506 /* find corresponding section, if any */
1484− for(j = 1; j < nb_sections;j++) {
1485− s = sections[j];
1507+ for(j = 1; j < s1->nb_sections;j++) {
1508+ s = s1->sections[j];
14861509 if (!strcmp(s->name, sh_name))
14871510 goto found;
14881511 }
14891512 /* 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);
14911514 /* take as much info as possible from the section. sh_link and
14921515 sh_info will be updated later */
14931516 s->sh_addralign = sh->sh_addralign;
14941517 s->sh_entsize = sh->sh_entsize;
14951518 sm_table[i].new_section = 1;
14961519 found:
1497− if (sh->sh_type != s->sh_type)
1520+ if (sh->sh_type != s->sh_type) {
1521+ error_noabort("invalid section type");
14981522 goto fail;
1523+ }
14991524
15001525 /* align start of section */
15011526 offset = s->data_offset;
@@ -1531,7 +1556,7 @@ static int tcc_load_object_file(TCCState *s1, · 29 unmodified lines
15311556 if (sh->sh_type == SHT_REL) {
15321557 s->sh_info = sm_table[sh->sh_info].s->sh_num;
15331558 /* update backward link */
1534− sections[s->sh_info]->reloc = s;
1559+ s1->sections[s->sh_info]->reloc = s;
15351560 }
15361561 }
15371562
@@ -1584,7 +1609,8 @@ static int tcc_load_object_file(TCCState *s1, · 46 unmodified lines
15841609 sym_index = old_to_new_syms[sym_index];
15851610 if (!sym_index) {
15861611 invalid_reloc:
1587− error("Invalid relocation entry");
1612+ error_noabort("Invalid relocation entry");
1613+ goto fail;
15881614 }
15891615 rel->r_info = ELF32_R_INFO(sym_index, type);
15901616 /* offset the relocation offset */
@@ -1595,12 +1621,16 @@ static int tcc_load_object_file(TCCState *s1, · 4 unmodified lines
15951621 break;
15961622 }
15971623 }
1624+
1625+ ret = 0;
1626+ the_end:
15981627 tcc_free(symtab);
15991628 tcc_free(strtab);
16001629 tcc_free(old_to_new_syms);
16011630 tcc_free(sm_table);
1631+ tcc_free(strsec);
16021632 tcc_free(shdr);
1603− return 0;
1633+ return ret;
16041634 }
16051635
16061636 #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
16321662 len = read(fd, &hdr, sizeof(hdr));
16331663 if (len == 0)
16341664 break;
1635− if (len != sizeof(hdr))
1636− error("invalid archive");
1665+ if (len != sizeof(hdr)) {
1666+ error_noabort("invalid archive");
1667+ return -1;
1668+ }
16371669 memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
16381670 ar_size[sizeof(hdr.ar_size)] = '\0';
16391671 size = strtol(ar_size, NULL, 0);
@@ -1652,7 +1684,8 @@ static int tcc_load_archive(TCCState *s1, int fd) · 12 unmodified lines
16521684 !strcmp(ar_name, "ARFILENAMES/")) {
16531685 /* skip symbol table or archive names */
16541686 } else {
1655− tcc_load_object_file(s1, fd, file_offset);
1687+ if (tcc_load_object_file(s1, fd, file_offset) < 0)
1688+ return -1;
16561689 }
16571690 /* align to even */
16581691 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
16681701 {
16691702 Elf32_Ehdr ehdr;
16701703 Elf32_Shdr *shdr, *sh, *sh1;
1671− int i, nb_syms, nb_dts, sym_bind;
1704+ int i, nb_syms, nb_dts, sym_bind, ret;
16721705 Elf32_Sym *sym, *dynsym;
16731706 Elf32_Dyn *dt, *dynamic;
16741707 unsigned char *dynstr;
@@ -1679,8 +1712,10 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 4 unmodified lines
16791712
16801713 /* test CPU specific stuff */
16811714 if (ehdr.e_ident[5] != ELFDATA2LSB ||
1682− ehdr.e_machine != EM_386)
1683− error("bad architecture");
1715+ ehdr.e_machine != EM_386) {
1716+ error_noabort("bad architecture");
1717+ return -1;
1718+ }
16841719
16851720 /* read sections */
16861721 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
17211756 }
17221757
17231758 /* if the dll is already loaded, do not load it */
1724− for(i = 0; i < nb_loaded_dlls; i++) {
1725− dllref = loaded_dlls[i];
1759+ for(i = 0; i < s1->nb_loaded_dlls; i++) {
1760+ dllref = s1->loaded_dlls[i];
17261761 if (!strcmp(soname, dllref->name)) {
17271762 /* but update level if needed */
17281763 if (level < dllref->level)
17291764 dllref->level = level;
1765+ ret = 0;
17301766 goto the_end;
17311767 }
17321768 }
@@ -1737,7 +1773,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 4 unmodified lines
17371773 dllref = tcc_malloc(sizeof(DLLReference) + strlen(soname));
17381774 dllref->level = level;
17391775 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);
17411777
17421778 /* add dynamic symbols in dynsym_section */
17431779 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
17451781 if (sym_bind == STB_LOCAL)
17461782 continue;
17471783 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,
17491785 sym->st_info, sym->st_shndx, name);
17501786 }
17511787
@@ -1754,20 +1790,27 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 2 unmodified lines
17541790 switch(dt->d_tag) {
17551791 case DT_NEEDED:
17561792 name = dynstr + dt->d_un.d_val;
1757− for(i = 0; i < nb_loaded_dlls; i++) {
1758− dllref = loaded_dlls[i];
1793+ for(i = 0; i < s1->nb_loaded_dlls; i++) {
1794+ dllref = s1->loaded_dlls[i];
17591795 if (!strcmp(name, dllref->name))
17601796 goto already_loaded;
17611797 }
1762− if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0)
1763− error("referenced dll '%s' not found", name);
1798+ if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
1799+ error_noabort("referenced dll '%s' not found", name);
1800+ ret = -1;
1801+ goto the_end;
1802+ }
17641803 already_loaded:
17651804 break;
17661805 }
17671806 }
1807+ ret = 0;
17681808 the_end:
1809+ tcc_free(dynstr);
1810+ tcc_free(dynsym);
1811+ tcc_free(dynamic);
17691812 tcc_free(shdr);
1770− return 0;
1813+ return ret;
17711814 }
17721815
17731816 /* return -2 if error and CH_EOF if eof */
@@ -1833,7 +1876,8 @@ static int tcc_load_ldscript(TCCState *s1) · 59 unmodified lines
18331876 cinp();
18341877 break;
18351878 } else if (ch == CH_EOF) {
1836− error("unexpected end of file");
1879+ error_noabort("unexpected end of file");
1880+ return -1;
18371881 }
18381882 }
18391883 } else {
@@ -1842,4 +1886,3 @@ static int tcc_load_ldscript(TCCState *s1) · 2 unmodified lines
18421886 }
18431887 return 0;
18441888 }
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