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
433433 }
434434 }
435435
436−/* relocate a given section (CPU dependant) */
436+/* relocate a given section (CPU dependent) */
437437 static void relocate_section(TCCState *s1, Section *s)
438438 {
439439 Section *sr;
@@ -581,7 +581,7 @@ static void put_got_offset(TCCState *s1, int index, unsigned long val) · 141 unmodified lines
581581 }
582582
583583 /* XXX: suppress that */
584−static void put32(unsigned char *p, unsigned int val)
584+static void put32(unsigned char *p, uint32_t val)
585585 {
586586 p[0] = val;
587587 p[1] = val >> 8;
@@ -589,6 +589,11 @@ static void put32(unsigned char *p, unsigned int val) · 1 unmodified line
589589 p[3] = val >> 24;
590590 }
591591
592+static uint32_t get32(unsigned char *p)
593+{
594+ return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
595+}
596+
592597 static void build_got(TCCState *s1)
593598 {
594599 unsigned char *ptr;
@@ -632,10 +637,43 @@ static void put_got_entry(TCCState *s1, · 37 unmodified lines
632637 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
633638 name = symtab_section->link->data + sym->st_name;
634639 offset = sym->st_value;
635− /* NOTE: we put temporarily the got offset */
636640 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;
639677 }
640678 index = put_elf_sym(s1->dynsym, offset,
641679 size, info, 0, sym->st_shndx, name);
@@ -847,7 +885,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 205 unmodified lines
847885 Section *strsec, *s;
848886 Elf32_Shdr shdr, *sh;
849887 Elf32_Phdr *phdr, *ph;
850− Section *interp, *plt, *dynamic, *dynstr;
888+ Section *interp, *dynamic, *dynstr;
851889 unsigned long saved_dynamic_data_offset;
852890 Elf32_Sym *sym;
853891 int type, file_type;
@@ -863,7 +901,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 9 unmodified lines
863901 section_order = NULL;
864902 interp = NULL;
865903 dynamic = NULL;
866− plt = NULL; /* avoid warning */
867904 dynstr = NULL; /* avoid warning */
868905 saved_dynamic_data_offset = 0; /* avoid warning */
869906
@@ -898,8 +935,9 @@ int tcc_output_file(TCCState *s1, const char *filename) · 28 unmodified lines
898935 dynamic->sh_entsize = sizeof(Elf32_Dyn);
899936
900937 /* 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;
903941
904942 build_got(s1);
905943
@@ -998,9 +1036,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 92 unmodified lines
9981036
9991037 build_got_entries(s1);
10001038
1001− /* update PLT/GOT sizes so that we can allocate their space */
1002− plt->data_offset += 16 * (s1->nb_plt_entries + 1);
1003−
10041039 /* add a list of needed dlls */
10051040 for(i = 0; i < s1->nb_loaded_dlls; i++) {
10061041 DLLReference *dllref = s1->loaded_dlls[i];
@@ -1191,8 +1226,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 184 unmodified lines
11911226
11921227 /* if dynamic section, then add corresponing program header */
11931228 if (dynamic) {
1194− int plt_offset;
1195− unsigned char *p;
11961229 Elf32_Sym *sym_end;
11971230
11981231 ph = &phdr[phnum - 1];
@@ -1209,49 +1242,38 @@ int tcc_output_file(TCCState *s1, const char *filename) · 10 unmodified lines
12091242 /* put GOT dynamic section address */
12101243 put32(s1->got->data, dynamic->sh_addr);
12111244
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 */
12261261 sym_end = (Elf32_Sym *)(s1->dynsym->data + s1->dynsym->data_offset);
12271262 for(sym = (Elf32_Sym *)s1->dynsym->data + 1;
12281263 sym < sym_end;
12291264 sym++) {
1230− type = ELF32_ST_TYPE(sym->st_info);
12311265 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;
12481270 } else if (sym->st_shndx < SHN_LORESERVE) {
12491271 /* do symbol relocation */
12501272 sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
12511273 }
12521274 }
1253− /* put dynamic section entries */
12541275
1276+ /* put dynamic section entries */
12551277 dynamic->data_offset = saved_dynamic_data_offset;
12561278 put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
12571279 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