Unified · Split
| @@ -199,8 +199,8 @@ static int add_elf_sym(Section *s, unsigned long value, unsigned long size, · 198 unmodified lines | ||
| 199 | 199 | sym_bind, sh_num, esym_bind, esym->st_shndx); |
| 200 | 200 | #endif |
| 201 | 201 | /* 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); | |
| 204 | 204 | } |
| 205 | 205 | } else { |
| 206 | 206 | do_patch: |
| @@ -232,7 +232,7 @@ static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, · 25 unmodified lines | ||
| 232 | 232 | snprintf(buf, sizeof(buf), ".rel%s", s->name); |
| 233 | 233 | /* if the symtab is allocated, then we consider the relocation |
| 234 | 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 | 236 | sr->sh_entsize = sizeof(Elf32_Rel); |
| 237 | 237 | sr->link = symtab; |
| 238 | 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 | 293 | the global and weak ones. Since TCC cannot sort it while generating |
| 294 | 294 | the code, we must do it after. All the relocation tables are also |
| 295 | 295 | 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) | |
| 297 | 297 | { |
| 298 | 298 | int *old_to_new_syms; |
| 299 | 299 | Elf32_Sym *new_syms; |
| @@ -335,8 +335,8 @@ static void sort_syms(Section *s) · 35 unmodified lines | ||
| 335 | 335 | tcc_free(new_syms); |
| 336 | 336 | |
| 337 | 337 | /* 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]; | |
| 340 | 340 | if (sr->sh_type == SHT_REL && sr->link == s) { |
| 341 | 341 | rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); |
| 342 | 342 | for(rel = (Elf32_Rel *)sr->data; |
| @@ -383,7 +383,7 @@ static void *resolve_sym(const char *sym) · 40 unmodified lines | ||
| 383 | 383 | |
| 384 | 384 | /* relocate symbol table, resolve undefined symbols if do_resolve is |
| 385 | 385 | 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) | |
| 387 | 387 | { |
| 388 | 388 | Elf32_Sym *sym, *esym, *sym_end; |
| 389 | 389 | int sym_bind, sh_num, sym_index; |
| @@ -404,11 +404,11 @@ static void relocate_syms(int do_resolve) · 14 unmodified lines | ||
| 404 | 404 | sym->st_value = addr; |
| 405 | 405 | goto found; |
| 406 | 406 | } |
| 407 | − } else if (dynsym) { | |
| 407 | + } else if (s1->dynsym) { | |
| 408 | 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 | 410 | if (sym_index) { |
| 411 | − esym = &((Elf32_Sym *)dynsym->data)[sym_index]; | |
| 411 | + esym = &((Elf32_Sym *)s1->dynsym->data)[sym_index]; | |
| 412 | 412 | sym->st_value = esym->st_value; |
| 413 | 413 | goto found; |
| 414 | 414 | } |
| @@ -423,11 +423,11 @@ static void relocate_syms(int do_resolve) · 8 unmodified lines | ||
| 423 | 423 | if (sym_bind == STB_WEAK) { |
| 424 | 424 | sym->st_value = 0; |
| 425 | 425 | } else { |
| 426 | − error("undefined symbol '%s'", name); | |
| 426 | + error_noabort("undefined symbol '%s'", name); | |
| 427 | 427 | } |
| 428 | 428 | } else if (sh_num < SHN_LORESERVE) { |
| 429 | 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 | 432 | found: ; |
| 433 | 433 | } |
| @@ -461,7 +461,7 @@ static void relocate_section(TCCState *s1, Section *s) · 27 unmodified lines | ||
| 461 | 461 | switch(type) { |
| 462 | 462 | case R_386_32: |
| 463 | 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 | 465 | qrel->r_offset = rel->r_offset; |
| 466 | 466 | if (esym_index) { |
| 467 | 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 | 477 | case R_386_PC32: |
| 478 | 478 | if (s1->output_type == TCC_OUTPUT_DLL) { |
| 479 | 479 | /* DLL relocation */ |
| 480 | − esym_index = symtab_to_dynsym[sym_index]; | |
| 480 | + esym_index = s1->symtab_to_dynsym[sym_index]; | |
| 481 | 481 | if (esym_index) { |
| 482 | 482 | qrel->r_offset = rel->r_offset; |
| 483 | 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 | 495 | *(int *)ptr = val; |
| 496 | 496 | break; |
| 497 | 497 | case R_386_GOTPC: |
| 498 | − *(int *)ptr += got->sh_addr - addr; | |
| 498 | + *(int *)ptr += s1->got->sh_addr - addr; | |
| 499 | 499 | break; |
| 500 | 500 | case R_386_GOTOFF: |
| 501 | − *(int *)ptr += val - got->sh_addr; | |
| 501 | + *(int *)ptr += val - s1->got->sh_addr; | |
| 502 | 502 | break; |
| 503 | 503 | case R_386_GOT32: |
| 504 | 504 | /* we load the got offset */ |
| 505 | − *(int *)ptr += got_offsets[sym_index]; | |
| 505 | + *(int *)ptr += s1->got_offsets[sym_index]; | |
| 506 | 506 | break; |
| 507 | 507 | } |
| 508 | 508 | } |
| 509 | 509 | /* if the relocation is allocated, we change its symbol table */ |
| 510 | 510 | if (sr->sh_flags & SHF_ALLOC) |
| 511 | − sr->link = dynsym; | |
| 511 | + sr->link = s1->dynsym; | |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | 514 | /* relocate relocation table in 'sr' */ |
| 515 | −static void relocate_rel(Section *sr) | |
| 515 | +static void relocate_rel(TCCState *s1, Section *sr) | |
| 516 | 516 | { |
| 517 | 517 | Section *s; |
| 518 | 518 | Elf32_Rel *rel, *rel_end; |
| 519 | 519 | |
| 520 | − s = sections[sr->sh_info]; | |
| 520 | + s = s1->sections[sr->sh_info]; | |
| 521 | 521 | rel_end = (Elf32_Rel *)(sr->data + sr->data_offset); |
| 522 | 522 | for(rel = (Elf32_Rel *)sr->data; |
| 523 | 523 | rel < rel_end; |
| @@ -528,7 +528,7 @@ static void relocate_rel(Section *sr) · 4 unmodified lines | ||
| 528 | 528 | |
| 529 | 529 | /* count the number of dynamic relocations so that we can reserve |
| 530 | 530 | their space */ |
| 531 | −static int prepare_dynamic_rel(Section *sr) | |
| 531 | +static int prepare_dynamic_rel(TCCState *s1, Section *sr) | |
| 532 | 532 | { |
| 533 | 533 | Elf32_Rel *rel, *rel_end; |
| 534 | 534 | int sym_index, esym_index, type, count; |
| @@ -543,7 +543,7 @@ static int prepare_dynamic_rel(Section *sr) · 8 unmodified lines | ||
| 543 | 543 | count++; |
| 544 | 544 | break; |
| 545 | 545 | case R_386_PC32: |
| 546 | − esym_index = symtab_to_dynsym[sym_index]; | |
| 546 | + esym_index = s1->symtab_to_dynsym[sym_index]; | |
| 547 | 547 | if (esym_index) |
| 548 | 548 | count++; |
| 549 | 549 | break; |
| @@ -559,25 +559,25 @@ static int prepare_dynamic_rel(Section *sr) · 9 unmodified lines | ||
| 559 | 559 | return count; |
| 560 | 560 | } |
| 561 | 561 | |
| 562 | −static void put_got_offset(int index, unsigned long val) | |
| 562 | +static void put_got_offset(TCCState *s1, int index, unsigned long val) | |
| 563 | 563 | { |
| 564 | 564 | int n; |
| 565 | 565 | unsigned long *tab; |
| 566 | 566 | |
| 567 | − if (index >= nb_got_offsets) { | |
| 567 | + if (index >= s1->nb_got_offsets) { | |
| 568 | 568 | /* find immediately bigger power of 2 and reallocate array */ |
| 569 | 569 | n = 1; |
| 570 | 570 | while (index >= n) |
| 571 | 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 | 573 | if (!tab) |
| 574 | 574 | 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; | |
| 579 | 579 | } |
| 580 | − got_offsets[index] = val; | |
| 580 | + s1->got_offsets[index] = val; | |
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | /* XXX: suppress that */ |
| @@ -589,16 +589,16 @@ static void put32(unsigned char *p, unsigned int val) · 5 unmodified lines | ||
| 589 | 589 | p[3] = val >> 24; |
| 590 | 590 | } |
| 591 | 591 | |
| 592 | −static void build_got(void) | |
| 592 | +static void build_got(TCCState *s1) | |
| 593 | 593 | { |
| 594 | 594 | unsigned char *ptr; |
| 595 | 595 | |
| 596 | 596 | /* 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; | |
| 599 | 599 | 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)); | |
| 602 | 602 | /* keep space for _DYNAMIC pointer, if present */ |
| 603 | 603 | put32(ptr, 0); |
| 604 | 604 | /* two dummy got entries */ |
| @@ -608,7 +608,8 @@ static void build_got(void) · 3 unmodified lines | ||
| 608 | 608 | |
| 609 | 609 | /* put a got entry corresponding to a symbol in symtab_section. 'size' |
| 610 | 610 | 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, | |
| 612 | 613 | int sym_index) |
| 613 | 614 | { |
| 614 | 615 | int index; |
| @@ -617,46 +618,46 @@ static void put_got_entry(int reloc_type, unsigned long size, int info, · 2 unmodified lines | ||
| 617 | 618 | unsigned long offset; |
| 618 | 619 | int *ptr; |
| 619 | 620 | |
| 620 | − if (!got) | |
| 621 | − build_got(); | |
| 621 | + if (!s1->got) | |
| 622 | + build_got(s1); | |
| 622 | 623 | |
| 623 | 624 | /* 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) | |
| 626 | 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 | 632 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; |
| 632 | 633 | name = symtab_section->link->data + sym->st_name; |
| 633 | 634 | offset = sym->st_value; |
| 634 | 635 | /* NOTE: we put temporarily the got offset */ |
| 635 | 636 | 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; | |
| 638 | 639 | } |
| 639 | − index = put_elf_sym(dynsym, offset, | |
| 640 | + index = put_elf_sym(s1->dynsym, offset, | |
| 640 | 641 | size, info, 0, sym->st_shndx, name); |
| 641 | 642 | /* 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, | |
| 644 | 645 | reloc_type, index); |
| 645 | 646 | } |
| 646 | − ptr = section_ptr_add(got, sizeof(int)); | |
| 647 | + ptr = section_ptr_add(s1->got, sizeof(int)); | |
| 647 | 648 | *ptr = 0; |
| 648 | 649 | } |
| 649 | 650 | |
| 650 | 651 | /* build GOT and PLT entries */ |
| 651 | −static void build_got_entries(void) | |
| 652 | +static void build_got_entries(TCCState *s1) | |
| 652 | 653 | { |
| 653 | 654 | Section *s, *symtab; |
| 654 | 655 | Elf32_Rel *rel, *rel_end; |
| 655 | 656 | Elf32_Sym *sym; |
| 656 | 657 | int i, type, reloc_type, sym_index; |
| 657 | 658 | |
| 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]; | |
| 660 | 661 | if (s->sh_type != SHT_REL) |
| 661 | 662 | continue; |
| 662 | 663 | /* no need to handle got relocations */ |
| @@ -673,8 +674,8 @@ static void build_got_entries(void) · 10 unmodified lines | ||
| 673 | 674 | case R_386_GOTOFF: |
| 674 | 675 | case R_386_GOTPC: |
| 675 | 676 | case R_386_PLT32: |
| 676 | − if (!got) | |
| 677 | − build_got(); | |
| 677 | + if (!s1->got) | |
| 678 | + build_got(s1); | |
| 678 | 679 | if (type == R_386_GOT32 || type == R_386_PLT32) { |
| 679 | 680 | sym_index = ELF32_R_SYM(rel->r_info); |
| 680 | 681 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; |
| @@ -683,7 +684,7 @@ static void build_got_entries(void) · 2 unmodified lines | ||
| 683 | 684 | reloc_type = R_386_GLOB_DAT; |
| 684 | 685 | else |
| 685 | 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 | 688 | sym_index); |
| 688 | 689 | } |
| 689 | 690 | break; |
| @@ -694,23 +695,24 @@ static void build_got_entries(void) · 4 unmodified lines | ||
| 694 | 695 | } |
| 695 | 696 | } |
| 696 | 697 | |
| 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, | |
| 698 | 700 | const char *strtab_name, |
| 699 | 701 | const char *hash_name, int hash_sh_flags) |
| 700 | 702 | { |
| 701 | 703 | Section *symtab, *strtab, *hash; |
| 702 | 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 | 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 | 709 | put_elf_str(strtab, ""); |
| 708 | 710 | symtab->link = strtab; |
| 709 | 711 | put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL); |
| 710 | 712 | |
| 711 | 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 | 716 | hash->sh_entsize = sizeof(int); |
| 715 | 717 | symtab->hash = hash; |
| 716 | 718 | hash->link = symtab; |
| @@ -759,7 +761,7 @@ static void tcc_add_runtime(TCCState *s1) · 42 unmodified lines | ||
| 759 | 761 | #ifdef TCC_TARGET_I386 |
| 760 | 762 | if (s1->output_type != TCC_OUTPUT_MEMORY) { |
| 761 | 763 | /* add 'call __bound_init()' in .init section */ |
| 762 | − init_section = find_section(".init"); | |
| 764 | + init_section = find_section(s1, ".init"); | |
| 763 | 765 | pinit = section_ptr_add(init_section, 5); |
| 764 | 766 | pinit[0] = 0xe8; |
| 765 | 767 | put32(pinit + 1, -4); |
| @@ -790,8 +792,8 @@ static void tcc_add_runtime(TCCState *s1) · 24 unmodified lines | ||
| 790 | 792 | bss_section->sh_num, "_end"); |
| 791 | 793 | /* add start and stop symbols for sections whose name can be |
| 792 | 794 | 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]; | |
| 795 | 797 | if (s->sh_type == SHT_PROGBITS && |
| 796 | 798 | (s->sh_flags & SHF_ALLOC)) { |
| 797 | 799 | const char *p; |
| @@ -834,7 +836,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 36 unmodified lines | ||
| 834 | 836 | { |
| 835 | 837 | Elf32_Ehdr ehdr; |
| 836 | 838 | FILE *f; |
| 837 | − int fd, mode; | |
| 839 | + int fd, mode, ret; | |
| 838 | 840 | int *section_order; |
| 839 | 841 | int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k; |
| 840 | 842 | unsigned long addr; |
| @@ -848,24 +850,24 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines | ||
| 848 | 850 | unsigned long rel_addr, rel_size; |
| 849 | 851 | |
| 850 | 852 | file_type = s1->output_type; |
| 853 | + s1->nb_errors = 0; | |
| 851 | 854 | |
| 852 | 855 | if (file_type != TCC_OUTPUT_OBJ) |
| 853 | 856 | tcc_add_runtime(s1); |
| 854 | 857 | |
| 858 | + phdr = NULL; | |
| 859 | + section_order = NULL; | |
| 855 | 860 | interp = NULL; |
| 856 | 861 | dynamic = NULL; |
| 857 | − dynsym = NULL; | |
| 858 | − got = NULL; | |
| 859 | − nb_plt_entries = 0; | |
| 860 | 862 | plt = NULL; /* avoid warning */ |
| 861 | 863 | dynstr = NULL; /* avoid warning */ |
| 862 | 864 | saved_dynamic_data_offset = 0; /* avoid warning */ |
| 863 | − | |
| 865 | + | |
| 864 | 866 | if (file_type != TCC_OUTPUT_OBJ) { |
| 865 | 867 | |
| 866 | 868 | relocate_common_syms(); |
| 867 | 869 | |
| 868 | − if (!static_link) { | |
| 870 | + if (!s1->static_link) { | |
| 869 | 871 | const char *name; |
| 870 | 872 | int sym_index, index; |
| 871 | 873 | Elf32_Sym *esym, *sym_end; |
| @@ -873,29 +875,29 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1 unmodified line | ||
| 873 | 875 | if (file_type == TCC_OUTPUT_EXE) { |
| 874 | 876 | char *ptr; |
| 875 | 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 | 879 | interp->sh_addralign = 1; |
| 878 | 880 | ptr = section_ptr_add(interp, sizeof(elf_interp)); |
| 879 | 881 | strcpy(ptr, elf_interp); |
| 880 | 882 | } |
| 881 | 883 | |
| 882 | 884 | /* 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; | |
| 887 | 889 | |
| 888 | 890 | /* add dynamic section */ |
| 889 | − dynamic = new_section(".dynamic", SHT_DYNAMIC, | |
| 891 | + dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC, | |
| 890 | 892 | SHF_ALLOC | SHF_WRITE); |
| 891 | 893 | dynamic->link = dynstr; |
| 892 | 894 | dynamic->sh_entsize = sizeof(Elf32_Dyn); |
| 893 | 895 | |
| 894 | 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 | 898 | plt->sh_entsize = 4; |
| 897 | 899 | |
| 898 | − build_got(); | |
| 900 | + build_got(s1); | |
| 899 | 901 | |
| 900 | 902 | /* scan for undefined symbols and see if they are in the |
| 901 | 903 | 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 | 911 | sym++) { |
| 910 | 912 | if (sym->st_shndx == SHN_UNDEF) { |
| 911 | 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 | 915 | if (sym_index) { |
| 914 | − esym = &((Elf32_Sym *)dynsymtab_section->data)[sym_index]; | |
| 916 | + esym = &((Elf32_Sym *)s1->dynsymtab_section->data)[sym_index]; | |
| 915 | 917 | type = ELF32_ST_TYPE(esym->st_info); |
| 916 | 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 | 920 | esym->st_info, |
| 919 | 921 | sym - (Elf32_Sym *)symtab_section->data); |
| 920 | 922 | } else if (type == STT_OBJECT) { |
| @@ -922,10 +924,10 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1 unmodified line | ||
| 922 | 924 | offset = bss_section->data_offset; |
| 923 | 925 | /* XXX: which alignment ? */ |
| 924 | 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 | 928 | esym->st_info, 0, |
| 927 | 929 | bss_section->sh_num, name); |
| 928 | − put_elf_reloc(dynsym, bss_section, | |
| 930 | + put_elf_reloc(s1->dynsym, bss_section, | |
| 929 | 931 | offset, R_386_COPY, index); |
| 930 | 932 | offset += esym->st_size; |
| 931 | 933 | bss_section->data_offset = offset; |
| @@ -937,25 +939,28 @@ int tcc_output_file(TCCState *s1, const char *filename) · 5 unmodified lines | ||
| 937 | 939 | if (ELF32_ST_BIND(sym->st_info) == STB_WEAK || |
| 938 | 940 | !strcmp(name, "_fp_hw")) { |
| 939 | 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 | 951 | /* now look at unresolved dynamic symbols and export |
| 947 | 952 | 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; | |
| 951 | 956 | esym < sym_end; |
| 952 | 957 | esym++) { |
| 953 | 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 | 960 | sym_index = find_elf_sym(symtab_section, name); |
| 956 | 961 | if (sym_index) { |
| 957 | 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 | 964 | sym->st_info, 0, |
| 960 | 965 | sym->st_shndx, name); |
| 961 | 966 | } else { |
| @@ -971,29 +976,30 @@ int tcc_output_file(TCCState *s1, const char *filename) · 9 unmodified lines | ||
| 971 | 976 | int nb_syms; |
| 972 | 977 | /* shared library case : we simply export all the global symbols */ |
| 973 | 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 | 980 | for(sym = (Elf32_Sym *)symtab_section->data + 1; |
| 976 | 981 | sym < sym_end; |
| 977 | 982 | sym++) { |
| 978 | 983 | if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL) { |
| 979 | 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 | 986 | sym->st_info, 0, |
| 982 | 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 | 990 | index; |
| 985 | 991 | } |
| 986 | 992 | } |
| 987 | 993 | } |
| 988 | 994 | |
| 989 | − build_got_entries(); | |
| 995 | + build_got_entries(s1); | |
| 990 | 996 | |
| 991 | 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 | 1000 | /* 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]; | |
| 997 | 1003 | if (dllref->level == 0) |
| 998 | 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 | 1013 | dynamic->data_offset += 8 * 9; |
| 1008 | 1014 | } else { |
| 1009 | 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 | 1020 | memset(&ehdr, 0, sizeof(ehdr)); |
| 1015 | 1021 | |
| 1016 | 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 | 1024 | put_elf_str(strsec, ""); |
| 1019 | 1025 | |
| 1020 | 1026 | /* compute number of sections */ |
| 1021 | − shnum = nb_sections; | |
| 1027 | + shnum = s1->nb_sections; | |
| 1022 | 1028 | |
| 1023 | 1029 | /* this array is used to reorder sections in the output file */ |
| 1024 | 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 | 1038 | phnum = 0; |
| 1033 | 1039 | break; |
| 1034 | 1040 | case TCC_OUTPUT_EXE: |
| 1035 | − if (!static_link) | |
| 1041 | + if (!s1->static_link) | |
| 1036 | 1042 | phnum = 4; |
| 1037 | 1043 | else |
| 1038 | 1044 | phnum = 2; |
| @@ -1046,19 +1052,19 @@ int tcc_output_file(TCCState *s1, const char *filename) · 7 unmodified lines | ||
| 1046 | 1052 | section should be output */ |
| 1047 | 1053 | /* NOTE: the strsec section comes last, so its size is also |
| 1048 | 1054 | 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]; | |
| 1051 | 1057 | s->sh_name = put_elf_str(strsec, s->name); |
| 1052 | 1058 | /* when generating a DLL, we include relocations but we may |
| 1053 | 1059 | patch them */ |
| 1054 | 1060 | if (file_type == TCC_OUTPUT_DLL && |
| 1055 | 1061 | s->sh_type == SHT_REL && |
| 1056 | 1062 | !(s->sh_flags & SHF_ALLOC)) { |
| 1057 | − prepare_dynamic_rel(s); | |
| 1063 | + prepare_dynamic_rel(s1, s); | |
| 1058 | 1064 | } else if (do_debug || |
| 1059 | 1065 | file_type == TCC_OUTPUT_OBJ || |
| 1060 | 1066 | (s->sh_flags & SHF_ALLOC) || |
| 1061 | − i == (nb_sections - 1)) { | |
| 1067 | + i == (s1->nb_sections - 1)) { | |
| 1062 | 1068 | /* we output all sections if debug or object file */ |
| 1063 | 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 | 1105 | relocations, progbits, nobits */ |
| 1100 | 1106 | /* XXX: do faster and simpler sorting */ |
| 1101 | 1107 | 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]; | |
| 1104 | 1110 | /* compute if section should be included */ |
| 1105 | 1111 | if (j == 0) { |
| 1106 | 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 | 1199 | ph->p_align = dynamic->sh_addralign; |
| 1194 | 1200 | |
| 1195 | 1201 | /* put GOT dynamic section address */ |
| 1196 | − put32(got->data, dynamic->sh_addr); | |
| 1202 | + put32(s1->got->data, dynamic->sh_addr); | |
| 1197 | 1203 | |
| 1198 | 1204 | /* compute the PLT */ |
| 1199 | 1205 | plt->data_offset = 0; |
| @@ -1202,15 +1208,15 @@ int tcc_output_file(TCCState *s1, const char *filename) · 2 unmodified lines | ||
| 1202 | 1208 | p = section_ptr_add(plt, 16); |
| 1203 | 1209 | p[0] = 0xff; /* pushl got + 4 */ |
| 1204 | 1210 | p[1] = 0x35; |
| 1205 | − put32(p + 2, got->sh_addr + 4); | |
| 1211 | + put32(p + 2, s1->got->sh_addr + 4); | |
| 1206 | 1212 | p[6] = 0xff; /* jmp *(got + 8) */ |
| 1207 | 1213 | p[7] = 0x25; |
| 1208 | − put32(p + 8, got->sh_addr + 8); | |
| 1214 | + put32(p + 8, s1->got->sh_addr + 8); | |
| 1209 | 1215 | |
| 1210 | 1216 | /* relocation symbols in .dynsym and build PLT. */ |
| 1211 | 1217 | 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; | |
| 1214 | 1220 | sym < sym_end; |
| 1215 | 1221 | sym++) { |
| 1216 | 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 | 1226 | p = section_ptr_add(plt, 16); |
| 1221 | 1227 | p[0] = 0xff; /* jmp *(got + x) */ |
| 1222 | 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 | 1230 | p[6] = 0x68; /* push $xxx */ |
| 1225 | 1231 | put32(p + 7, plt_offset); |
| 1226 | 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 | 1240 | } else if (sym->st_shndx < SHN_LORESERVE) { |
| 1235 | 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 | 1245 | /* put dynamic section entries */ |
| 1240 | 1246 | |
| 1241 | 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 | 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 | 1251 | put_dt(dynamic, DT_STRSZ, dynstr->data_offset); |
| 1246 | 1252 | put_dt(dynamic, DT_SYMENT, sizeof(Elf32_Sym)); |
| 1247 | 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 | 1264 | /* 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]; | |
| 1261 | 1267 | if (phnum > 0 && (s->sh_flags & SHF_ALLOC)) |
| 1262 | 1268 | continue; |
| 1263 | 1269 | section_order[sh_order_index++] = i; |
| @@ -1272,23 +1278,29 @@ int tcc_output_file(TCCState *s1, const char *filename) · 8 unmodified lines | ||
| 1272 | 1278 | /* if building executable or DLL, then relocate each section |
| 1273 | 1279 | except the GOT which is already relocated */ |
| 1274 | 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 | 1289 | /* relocate sections */ |
| 1278 | 1290 | /* 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) | |
| 1282 | 1294 | relocate_section(s1, s); |
| 1283 | 1295 | } |
| 1284 | 1296 | |
| 1285 | 1297 | /* relocate relocation entries if the relocation tables are |
| 1286 | 1298 | 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]; | |
| 1289 | 1301 | if ((s->sh_flags & SHF_ALLOC) && |
| 1290 | 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 | 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 | 1316 | /* align to 4 */ |
| 1305 | 1317 | file_offset = (file_offset + 3) & -4; |
| @@ -1338,15 +1350,16 @@ int tcc_output_file(TCCState *s1, const char *filename) · 32 unmodified lines | ||
| 1338 | 1350 | else |
| 1339 | 1351 | mode = 0777; |
| 1340 | 1352 | 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 | + } | |
| 1344 | 1357 | f = fdopen(fd, "w"); |
| 1345 | 1358 | fwrite(&ehdr, 1, sizeof(Elf32_Ehdr), f); |
| 1346 | 1359 | fwrite(phdr, 1, phnum * sizeof(Elf32_Phdr), f); |
| 1347 | 1360 | 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]]; | |
| 1350 | 1363 | if (s->sh_type != SHT_NOBITS) { |
| 1351 | 1364 | while (offset < s->sh_offset) { |
| 1352 | 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 | 1378 | /* output section headers */ |
| 1366 | − for(i=0;i<nb_sections;i++) { | |
| 1379 | + for(i=0;i<s1->nb_sections;i++) { | |
| 1367 | 1380 | sh = &shdr; |
| 1368 | 1381 | memset(sh, 0, sizeof(Elf32_Shdr)); |
| 1369 | − s = sections[i]; | |
| 1382 | + s = s1->sections[i]; | |
| 1370 | 1383 | if (s) { |
| 1371 | 1384 | sh->sh_name = s->sh_name; |
| 1372 | 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 | 1398 | fclose(f); |
| 1386 | 1399 | |
| 1400 | + ret = 0; | |
| 1401 | + the_end: | |
| 1402 | + tcc_free(s1->symtab_to_dynsym); | |
| 1387 | 1403 | tcc_free(section_order); |
| 1388 | 1404 | tcc_free(phdr); |
| 1389 | − return 0; | |
| 1405 | + tcc_free(s1->got_offsets); | |
| 1406 | + return ret; | |
| 1390 | 1407 | } |
| 1391 | 1408 | |
| 1392 | 1409 | 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 | ||
| 1412 | 1429 | { |
| 1413 | 1430 | Elf32_Ehdr ehdr; |
| 1414 | 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 | 1433 | unsigned char *strsec, *strtab; |
| 1417 | 1434 | int *old_to_new_syms; |
| 1418 | 1435 | char *sh_name, *name; |
| @@ -1422,20 +1439,21 @@ static int tcc_load_object_file(TCCState *s1, · 3 unmodified lines | ||
| 1422 | 1439 | Section *s; |
| 1423 | 1440 | |
| 1424 | 1441 | if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) |
| 1425 | − goto fail; | |
| 1442 | + goto fail1; | |
| 1426 | 1443 | if (ehdr.e_ident[0] != ELFMAG0 || |
| 1427 | 1444 | ehdr.e_ident[1] != ELFMAG1 || |
| 1428 | 1445 | ehdr.e_ident[2] != ELFMAG2 || |
| 1429 | 1446 | ehdr.e_ident[3] != ELFMAG3) |
| 1430 | − goto fail; | |
| 1447 | + goto fail1; | |
| 1431 | 1448 | /* test if object file */ |
| 1432 | 1449 | if (ehdr.e_type != ET_REL) |
| 1433 | − goto fail; | |
| 1450 | + goto fail1; | |
| 1434 | 1451 | /* test CPU specific stuff */ |
| 1435 | 1452 | if (ehdr.e_ident[5] != ELFDATA2LSB || |
| 1436 | 1453 | ehdr.e_machine != EM_386) { |
| 1437 | − fail: | |
| 1438 | − error("invalid object file"); | |
| 1454 | + fail1: | |
| 1455 | + error_noabort("invalid object file"); | |
| 1456 | + return -1; | |
| 1439 | 1457 | } |
| 1440 | 1458 | /* read sections */ |
| 1441 | 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 | 1465 | strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); |
| 1448 | 1466 | |
| 1449 | 1467 | /* load symtab and strtab */ |
| 1468 | + old_to_new_syms = NULL; | |
| 1450 | 1469 | symtab = NULL; |
| 1451 | 1470 | strtab = NULL; |
| 1452 | 1471 | nb_syms = 0; |
| 1453 | 1472 | for(i = 1; i < ehdr.e_shnum; i++) { |
| 1454 | 1473 | sh = &shdr[i]; |
| 1455 | 1474 | 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 | + } | |
| 1458 | 1481 | nb_syms = sh->sh_size / sizeof(Elf32_Sym); |
| 1459 | 1482 | symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); |
| 1460 | 1483 | sm_table[i].s = symtab_section; |
| @@ -1481,21 +1504,23 @@ static int tcc_load_object_file(TCCState *s1, · 20 unmodified lines | ||
| 1481 | 1504 | if (sh->sh_addralign < 1) |
| 1482 | 1505 | sh->sh_addralign = 1; |
| 1483 | 1506 | /* 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]; | |
| 1486 | 1509 | if (!strcmp(s->name, sh_name)) |
| 1487 | 1510 | goto found; |
| 1488 | 1511 | } |
| 1489 | 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 | 1514 | /* take as much info as possible from the section. sh_link and |
| 1492 | 1515 | sh_info will be updated later */ |
| 1493 | 1516 | s->sh_addralign = sh->sh_addralign; |
| 1494 | 1517 | s->sh_entsize = sh->sh_entsize; |
| 1495 | 1518 | sm_table[i].new_section = 1; |
| 1496 | 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 | 1522 | goto fail; |
| 1523 | + } | |
| 1499 | 1524 | |
| 1500 | 1525 | /* align start of section */ |
| 1501 | 1526 | offset = s->data_offset; |
| @@ -1531,7 +1556,7 @@ static int tcc_load_object_file(TCCState *s1, · 29 unmodified lines | ||
| 1531 | 1556 | if (sh->sh_type == SHT_REL) { |
| 1532 | 1557 | s->sh_info = sm_table[sh->sh_info].s->sh_num; |
| 1533 | 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 | 1609 | sym_index = old_to_new_syms[sym_index]; |
| 1585 | 1610 | if (!sym_index) { |
| 1586 | 1611 | invalid_reloc: |
| 1587 | − error("Invalid relocation entry"); | |
| 1612 | + error_noabort("Invalid relocation entry"); | |
| 1613 | + goto fail; | |
| 1588 | 1614 | } |
| 1589 | 1615 | rel->r_info = ELF32_R_INFO(sym_index, type); |
| 1590 | 1616 | /* offset the relocation offset */ |
| @@ -1595,12 +1621,16 @@ static int tcc_load_object_file(TCCState *s1, · 4 unmodified lines | ||
| 1595 | 1621 | break; |
| 1596 | 1622 | } |
| 1597 | 1623 | } |
| 1624 | + | |
| 1625 | + ret = 0; | |
| 1626 | + the_end: | |
| 1598 | 1627 | tcc_free(symtab); |
| 1599 | 1628 | tcc_free(strtab); |
| 1600 | 1629 | tcc_free(old_to_new_syms); |
| 1601 | 1630 | tcc_free(sm_table); |
| 1631 | + tcc_free(strsec); | |
| 1602 | 1632 | tcc_free(shdr); |
| 1603 | − return 0; | |
| 1633 | + return ret; | |
| 1604 | 1634 | } |
| 1605 | 1635 | |
| 1606 | 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 | 1662 | len = read(fd, &hdr, sizeof(hdr)); |
| 1633 | 1663 | if (len == 0) |
| 1634 | 1664 | break; |
| 1635 | − if (len != sizeof(hdr)) | |
| 1636 | − error("invalid archive"); | |
| 1665 | + if (len != sizeof(hdr)) { | |
| 1666 | + error_noabort("invalid archive"); | |
| 1667 | + return -1; | |
| 1668 | + } | |
| 1637 | 1669 | memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size)); |
| 1638 | 1670 | ar_size[sizeof(hdr.ar_size)] = '\0'; |
| 1639 | 1671 | size = strtol(ar_size, NULL, 0); |
| @@ -1652,7 +1684,8 @@ static int tcc_load_archive(TCCState *s1, int fd) · 12 unmodified lines | ||
| 1652 | 1684 | !strcmp(ar_name, "ARFILENAMES/")) { |
| 1653 | 1685 | /* skip symbol table or archive names */ |
| 1654 | 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 | 1690 | /* align to even */ |
| 1658 | 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 | 1702 | Elf32_Ehdr ehdr; |
| 1670 | 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 | 1705 | Elf32_Sym *sym, *dynsym; |
| 1673 | 1706 | Elf32_Dyn *dt, *dynamic; |
| 1674 | 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 | 1713 | /* test CPU specific stuff */ |
| 1681 | 1714 | 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 | + } | |
| 1684 | 1719 | |
| 1685 | 1720 | /* read sections */ |
| 1686 | 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 | 1758 | /* 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]; | |
| 1726 | 1761 | if (!strcmp(soname, dllref->name)) { |
| 1727 | 1762 | /* but update level if needed */ |
| 1728 | 1763 | if (level < dllref->level) |
| 1729 | 1764 | dllref->level = level; |
| 1765 | + ret = 0; | |
| 1730 | 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 | 1773 | dllref = tcc_malloc(sizeof(DLLReference) + strlen(soname)); |
| 1738 | 1774 | dllref->level = level; |
| 1739 | 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 | 1778 | /* add dynamic symbols in dynsym_section */ |
| 1743 | 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 | 1781 | if (sym_bind == STB_LOCAL) |
| 1746 | 1782 | continue; |
| 1747 | 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 | 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 | 1790 | switch(dt->d_tag) { |
| 1755 | 1791 | case DT_NEEDED: |
| 1756 | 1792 | 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]; | |
| 1759 | 1795 | if (!strcmp(name, dllref->name)) |
| 1760 | 1796 | goto already_loaded; |
| 1761 | 1797 | } |
| 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 | + } | |
| 1764 | 1803 | already_loaded: |
| 1765 | 1804 | break; |
| 1766 | 1805 | } |
| 1767 | 1806 | } |
| 1807 | + ret = 0; | |
| 1768 | 1808 | the_end: |
| 1809 | + tcc_free(dynstr); | |
| 1810 | + tcc_free(dynsym); | |
| 1811 | + tcc_free(dynamic); | |
| 1769 | 1812 | tcc_free(shdr); |
| 1770 | − return 0; | |
| 1813 | + return ret; | |
| 1771 | 1814 | } |
| 1772 | 1815 | |
| 1773 | 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 | 1876 | cinp(); |
| 1834 | 1877 | break; |
| 1835 | 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 | 1883 | } else { |
| @@ -1842,4 +1886,3 @@ static int tcc_load_ldscript(TCCState *s1) · 2 unmodified lines | ||
| 1842 | 1886 | } |
| 1843 | 1887 | return 0; |
| 1844 | 1888 | } |
| 1845 | − | |