Unified · Split
| @@ -433,7 +433,7 @@ static void relocate_syms(TCCState *s1, int do_resolve) · 432 unmodified lines | ||
| 433 | 433 | } |
| 434 | 434 | } |
| 435 | 435 | |
| 436 | −/* relocate a given section (CPU dependant) */ | |
| 436 | +/* relocate a given section (CPU dependent) */ | |
| 437 | 437 | static void relocate_section(TCCState *s1, Section *s) |
| 438 | 438 | { |
| 439 | 439 | Section *sr; |
| @@ -581,7 +581,7 @@ static void put_got_offset(TCCState *s1, int index, unsigned long val) · 141 unmodified lines | ||
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | /* XXX: suppress that */ |
| 584 | −static void put32(unsigned char *p, unsigned int val) | |
| 584 | +static void put32(unsigned char *p, uint32_t val) | |
| 585 | 585 | { |
| 586 | 586 | p[0] = val; |
| 587 | 587 | p[1] = val >> 8; |
| @@ -589,6 +589,11 @@ static void put32(unsigned char *p, unsigned int val) · 1 unmodified line | ||
| 589 | 589 | p[3] = val >> 24; |
| 590 | 590 | } |
| 591 | 591 | |
| 592 | +static uint32_t get32(unsigned char *p) | |
| 593 | +{ | |
| 594 | + return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); | |
| 595 | +} | |
| 596 | + | |
| 592 | 597 | static void build_got(TCCState *s1) |
| 593 | 598 | { |
| 594 | 599 | unsigned char *ptr; |
| @@ -632,10 +637,43 @@ static void put_got_entry(TCCState *s1, · 37 unmodified lines | ||
| 632 | 637 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; |
| 633 | 638 | name = symtab_section->link->data + sym->st_name; |
| 634 | 639 | offset = sym->st_value; |
| 635 | − /* NOTE: we put temporarily the got offset */ | |
| 636 | 640 | if (reloc_type == R_386_JMP_SLOT) { |
| 637 | − s1->nb_plt_entries++; | |
| 638 | − offset = s1->got->data_offset; | |
| 641 | + Section *plt; | |
| 642 | + uint8_t *p; | |
| 643 | + int modrm; | |
| 644 | + | |
| 645 | + /* if we build a DLL, we add a %ebx offset */ | |
| 646 | + if (s1->output_type == TCC_OUTPUT_DLL) | |
| 647 | + modrm = 0xa3; | |
| 648 | + else | |
| 649 | + modrm = 0x25; | |
| 650 | + | |
| 651 | + /* add a PLT entry */ | |
| 652 | + plt = s1->plt; | |
| 653 | + if (plt->data_offset == 0) { | |
| 654 | + /* first plt entry */ | |
| 655 | + p = section_ptr_add(plt, 16); | |
| 656 | + p[0] = 0xff; /* pushl got + 4 */ | |
| 657 | + p[1] = modrm + 0x10; | |
| 658 | + put32(p + 2, 4); | |
| 659 | + p[6] = 0xff; /* jmp *(got + 8) */ | |
| 660 | + p[7] = modrm; | |
| 661 | + put32(p + 8, 8); | |
| 662 | + } | |
| 663 | + | |
| 664 | + p = section_ptr_add(plt, 16); | |
| 665 | + p[0] = 0xff; /* jmp *(got + x) */ | |
| 666 | + p[1] = modrm; | |
| 667 | + put32(p + 2, s1->got->data_offset); | |
| 668 | + p[6] = 0x68; /* push $xxx */ | |
| 669 | + put32(p + 7, (plt->data_offset - 32) >> 1); | |
| 670 | + p[11] = 0xe9; /* jmp plt_start */ | |
| 671 | + put32(p + 12, -(plt->data_offset)); | |
| 672 | + | |
| 673 | + /* the symbol is modified so that it will be relocated to | |
| 674 | + the PLT */ | |
| 675 | + if (s1->output_type == TCC_OUTPUT_EXE) | |
| 676 | + offset = plt->data_offset - 16; | |
| 639 | 677 | } |
| 640 | 678 | index = put_elf_sym(s1->dynsym, offset, |
| 641 | 679 | size, info, 0, sym->st_shndx, name); |
| @@ -847,7 +885,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 205 unmodified lines | ||
| 847 | 885 | Section *strsec, *s; |
| 848 | 886 | Elf32_Shdr shdr, *sh; |
| 849 | 887 | Elf32_Phdr *phdr, *ph; |
| 850 | − Section *interp, *plt, *dynamic, *dynstr; | |
| 888 | + Section *interp, *dynamic, *dynstr; | |
| 851 | 889 | unsigned long saved_dynamic_data_offset; |
| 852 | 890 | Elf32_Sym *sym; |
| 853 | 891 | int type, file_type; |
| @@ -863,7 +901,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 9 unmodified lines | ||
| 863 | 901 | section_order = NULL; |
| 864 | 902 | interp = NULL; |
| 865 | 903 | dynamic = NULL; |
| 866 | − plt = NULL; /* avoid warning */ | |
| 867 | 904 | dynstr = NULL; /* avoid warning */ |
| 868 | 905 | saved_dynamic_data_offset = 0; /* avoid warning */ |
| 869 | 906 | |
| @@ -898,8 +935,9 @@ int tcc_output_file(TCCState *s1, const char *filename) · 28 unmodified lines | ||
| 898 | 935 | dynamic->sh_entsize = sizeof(Elf32_Dyn); |
| 899 | 936 | |
| 900 | 937 | /* add PLT */ |
| 901 | − plt = new_section(s1, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR); | |
| 902 | − plt->sh_entsize = 4; | |
| 938 | + s1->plt = new_section(s1, ".plt", SHT_PROGBITS, | |
| 939 | + SHF_ALLOC | SHF_EXECINSTR); | |
| 940 | + s1->plt->sh_entsize = 4; | |
| 903 | 941 | |
| 904 | 942 | build_got(s1); |
| 905 | 943 | |
| @@ -998,9 +1036,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 92 unmodified lines | ||
| 998 | 1036 | |
| 999 | 1037 | build_got_entries(s1); |
| 1000 | 1038 | |
| 1001 | − /* update PLT/GOT sizes so that we can allocate their space */ | |
| 1002 | − plt->data_offset += 16 * (s1->nb_plt_entries + 1); | |
| 1003 | − | |
| 1004 | 1039 | /* add a list of needed dlls */ |
| 1005 | 1040 | for(i = 0; i < s1->nb_loaded_dlls; i++) { |
| 1006 | 1041 | DLLReference *dllref = s1->loaded_dlls[i]; |
| @@ -1191,8 +1226,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 184 unmodified lines | ||
| 1191 | 1226 | |
| 1192 | 1227 | /* if dynamic section, then add corresponing program header */ |
| 1193 | 1228 | if (dynamic) { |
| 1194 | − int plt_offset; | |
| 1195 | − unsigned char *p; | |
| 1196 | 1229 | Elf32_Sym *sym_end; |
| 1197 | 1230 | |
| 1198 | 1231 | ph = &phdr[phnum - 1]; |
| @@ -1209,49 +1242,38 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines | ||
| 1209 | 1242 | /* put GOT dynamic section address */ |
| 1210 | 1243 | put32(s1->got->data, dynamic->sh_addr); |
| 1211 | 1244 | |
| 1212 | − /* compute the PLT */ | |
| 1213 | − plt->data_offset = 0; | |
| 1214 | − | |
| 1215 | − /* first plt entry */ | |
| 1216 | − p = section_ptr_add(plt, 16); | |
| 1217 | − p[0] = 0xff; /* pushl got + 4 */ | |
| 1218 | − p[1] = 0x35; | |
| 1219 | − put32(p + 2, s1->got->sh_addr + 4); | |
| 1220 | − p[6] = 0xff; /* jmp *(got + 8) */ | |
| 1221 | − p[7] = 0x25; | |
| 1222 | − put32(p + 8, s1->got->sh_addr + 8); | |
| 1223 | − | |
| 1224 | − /* relocation symbols in .dynsym and build PLT. */ | |
| 1225 | − plt_offset = 0; | |
| 1245 | + /* relocate the PLT */ | |
| 1246 | + if (file_type == TCC_OUTPUT_EXE) { | |
| 1247 | + uint8_t *p, *p_end; | |
| 1248 | + | |
| 1249 | + p = s1->plt->data; | |
| 1250 | + p_end = p + s1->plt->data_offset; | |
| 1251 | + put32(p + 2, get32(p + 2) + s1->got->sh_addr); | |
| 1252 | + put32(p + 8, get32(p + 8) + s1->got->sh_addr); | |
| 1253 | + p += 16; | |
| 1254 | + while (p < p_end) { | |
| 1255 | + put32(p + 2, get32(p + 2) + s1->got->sh_addr); | |
| 1256 | + p += 16; | |
| 1257 | + } | |
| 1258 | + } | |
| 1259 | + | |
| 1260 | + /* relocate symbols in .dynsym */ | |
| 1226 | 1261 | sym_end = (Elf32_Sym *)(s1->dynsym->data + s1->dynsym->data_offset); |
| 1227 | 1262 | for(sym = (Elf32_Sym *)s1->dynsym->data + 1; |
| 1228 | 1263 | sym < sym_end; |
| 1229 | 1264 | sym++) { |
| 1230 | − type = ELF32_ST_TYPE(sym->st_info); | |
| 1231 | 1265 | if (sym->st_shndx == SHN_UNDEF) { |
| 1232 | − if (type == STT_FUNC) { | |
| 1233 | − /* one more entry in PLT */ | |
| 1234 | − p = section_ptr_add(plt, 16); | |
| 1235 | − p[0] = 0xff; /* jmp *(got + x) */ | |
| 1236 | − p[1] = 0x25; | |
| 1237 | − put32(p + 2, s1->got->sh_addr + sym->st_value); | |
| 1238 | − p[6] = 0x68; /* push $xxx */ | |
| 1239 | − put32(p + 7, plt_offset); | |
| 1240 | − p[11] = 0xe9; /* jmp plt_start */ | |
| 1241 | − put32(p + 12, -(plt->data_offset)); | |
| 1242 | − | |
| 1243 | − /* patch symbol value to point to plt */ | |
| 1244 | − sym->st_value = plt->sh_addr + p - plt->data; | |
| 1245 | − | |
| 1246 | − plt_offset += 8; | |
| 1247 | − } | |
| 1266 | + /* relocate to the PLT if the symbol corresponds | |
| 1267 | + to a PLT entry */ | |
| 1268 | + if (sym->st_value) | |
| 1269 | + sym->st_value += s1->plt->sh_addr; | |
| 1248 | 1270 | } else if (sym->st_shndx < SHN_LORESERVE) { |
| 1249 | 1271 | /* do symbol relocation */ |
| 1250 | 1272 | sym->st_value += s1->sections[sym->st_shndx]->sh_addr; |
| 1251 | 1273 | } |
| 1252 | 1274 | } |
| 1253 | − /* put dynamic section entries */ | |
| 1254 | 1275 | |
| 1276 | + /* put dynamic section entries */ | |
| 1255 | 1277 | dynamic->data_offset = saved_dynamic_data_offset; |
| 1256 | 1278 | put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr); |
| 1257 | 1279 | put_dt(dynamic, DT_STRTAB, dynstr->sh_addr); |