History

PLT generation fix

bellard committed 23 years ago

Commit c2e6daa1 parent d6819c21 file · +68 −46Browse files

Unified · Split

tccelf.c+68−46
@@ -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) */
437static void relocate_section(TCCState *s1, Section *s)437static void relocate_section(TCCState *s1, Section *s)
438{438{
439 Section *sr;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/* XXX: suppress that */583/* XXX: suppress that */
584static void put32(unsigned char *p, unsigned int val)584static void put32(unsigned char *p, uint32_t val)
585{585{
586 p[0] = val;586 p[0] = val;
587 p[1] = val >> 8;587 p[1] = val >> 8;
@@ -589,6 +589,11 @@ static void put32(unsigned char *p, unsigned int val) · 1 unmodified line
589 p[3] = val >> 24;589 p[3] = val >> 24;
590}590}
591 591
592static uint32_t get32(unsigned char *p)
593{
594 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
595}
596
592static void build_got(TCCState *s1)597static void build_got(TCCState *s1)
593{598{
594 unsigned char *ptr;599 unsigned char *ptr;
@@ -632,10 +637,43 @@ static void put_got_entry(TCCState *s1, · 37 unmodified lines
632 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];637 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
633 name = symtab_section->link->data + sym->st_name;638 name = symtab_section->link->data + sym->st_name;
634 offset = sym->st_value;639 offset = sym->st_value;
635 /* NOTE: we put temporarily the got offset */
636 if (reloc_type == R_386_JMP_SLOT) {640 if (reloc_type == R_386_JMP_SLOT) {
637 s1->nb_plt_entries++;641 Section *plt;
638 offset = s1->got->data_offset;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 index = put_elf_sym(s1->dynsym, offset, 678 index = put_elf_sym(s1->dynsym, offset,
641 size, info, 0, sym->st_shndx, name);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 Section *strsec, *s;885 Section *strsec, *s;
848 Elf32_Shdr shdr, *sh;886 Elf32_Shdr shdr, *sh;
849 Elf32_Phdr *phdr, *ph;887 Elf32_Phdr *phdr, *ph;
850 Section *interp, *plt, *dynamic, *dynstr;888 Section *interp, *dynamic, *dynstr;
851 unsigned long saved_dynamic_data_offset;889 unsigned long saved_dynamic_data_offset;
852 Elf32_Sym *sym;890 Elf32_Sym *sym;
853 int type, file_type;891 int type, file_type;
@@ -863,7 +901,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 9 unmodified lines
863 section_order = NULL;901 section_order = NULL;
864 interp = NULL;902 interp = NULL;
865 dynamic = NULL;903 dynamic = NULL;
866 plt = NULL; /* avoid warning */
867 dynstr = NULL; /* avoid warning */904 dynstr = NULL; /* avoid warning */
868 saved_dynamic_data_offset = 0; /* avoid warning */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 dynamic->sh_entsize = sizeof(Elf32_Dyn);935 dynamic->sh_entsize = sizeof(Elf32_Dyn);
899 936
900 /* add PLT */937 /* add PLT */
901 plt = new_section(s1, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);938 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
902 plt->sh_entsize = 4;939 SHF_ALLOC | SHF_EXECINSTR);
940 s1->plt->sh_entsize = 4;
903 941
904 build_got(s1);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 build_got_entries(s1);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 /* add a list of needed dlls */1039 /* add a list of needed dlls */
1005 for(i = 0; i < s1->nb_loaded_dlls; i++) {1040 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1006 DLLReference *dllref = s1->loaded_dlls[i];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 /* if dynamic section, then add corresponing program header */1227 /* if dynamic section, then add corresponing program header */
1193 if (dynamic) {1228 if (dynamic) {
1194 int plt_offset;
1195 unsigned char *p;
1196 Elf32_Sym *sym_end;1229 Elf32_Sym *sym_end;
1197 1230
1198 ph = &phdr[phnum - 1];1231 ph = &phdr[phnum - 1];
@@ -1209,49 +1242,38 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines
1209 /* put GOT dynamic section address */1242 /* put GOT dynamic section address */
1210 put32(s1->got->data, dynamic->sh_addr);1243 put32(s1->got->data, dynamic->sh_addr);
1211 1244
1212 /* compute the PLT */1245 /* relocate the PLT */
1213 plt->data_offset = 0;1246 if (file_type == TCC_OUTPUT_EXE) {
1214 1247 uint8_t *p, *p_end;
1215 /* first plt entry */1248
1216 p = section_ptr_add(plt, 16);1249 p = s1->plt->data;
1217 p[0] = 0xff; /* pushl got + 4 */1250 p_end = p + s1->plt->data_offset;
1218 p[1] = 0x35;1251 put32(p + 2, get32(p + 2) + s1->got->sh_addr);
1219 put32(p + 2, s1->got->sh_addr + 4);1252 put32(p + 8, get32(p + 8) + s1->got->sh_addr);
1220 p[6] = 0xff; /* jmp *(got + 8) */1253 p += 16;
1221 p[7] = 0x25;1254 while (p < p_end) {
1222 put32(p + 8, s1->got->sh_addr + 8);1255 put32(p + 2, get32(p + 2) + s1->got->sh_addr);
1223 1256 p += 16;
1224 /* relocation symbols in .dynsym and build PLT. */1257 }
1225 plt_offset = 0;1258 }
1259
1260 /* relocate symbols in .dynsym */
1226 sym_end = (Elf32_Sym *)(s1->dynsym->data + s1->dynsym->data_offset);1261 sym_end = (Elf32_Sym *)(s1->dynsym->data + s1->dynsym->data_offset);
1227 for(sym = (Elf32_Sym *)s1->dynsym->data + 1; 1262 for(sym = (Elf32_Sym *)s1->dynsym->data + 1;
1228 sym < sym_end;1263 sym < sym_end;
1229 sym++) {1264 sym++) {
1230 type = ELF32_ST_TYPE(sym->st_info);
1231 if (sym->st_shndx == SHN_UNDEF) {1265 if (sym->st_shndx == SHN_UNDEF) {
1232 if (type == STT_FUNC) {1266 /* relocate to the PLT if the symbol corresponds
1233 /* one more entry in PLT */1267 to a PLT entry */
1234 p = section_ptr_add(plt, 16);1268 if (sym->st_value)
1235 p[0] = 0xff; /* jmp *(got + x) */1269 sym->st_value += s1->plt->sh_addr;
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 }
1248 } else if (sym->st_shndx < SHN_LORESERVE) {1270 } else if (sym->st_shndx < SHN_LORESERVE) {
1249 /* do symbol relocation */1271 /* do symbol relocation */
1250 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;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 dynamic->data_offset = saved_dynamic_data_offset;1277 dynamic->data_offset = saved_dynamic_data_offset;
1256 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);1278 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
1257 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);1279 put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);

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