History

x86_64: Create proper PLT and GOT also for -run

This makes us use the normal PLT/GOT codepaths also for -run, which formerly used an on-the-side blob for the jump tables. For x86_64 only for now, arm coming up.

Michael Matz committed 12 years ago

Commit 9750d0b1 parent c4427743 files · +24 −17Browse files

Unified · Split

3 files
  1. tcc.h +2 −1
  2. tccelf.c +21 −16
  3. tccrun.c +1 −0
tcc.h+2−1
@@ -712,7 +712,7 @@ struct TCCState { · 711 unmodified lines
712 void *write_mem;712 void *write_mem;
713 unsigned long mem_size;713 unsigned long mem_size;
714# endif714# endif
715# if !defined TCC_TARGET_PE && (defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM)715# if !defined TCC_TARGET_PE && (defined TCC_TARGET_ARM)
716 /* write PLT and GOT here */716 /* write PLT and GOT here */
717 char *runtime_plt_and_got;717 char *runtime_plt_and_got;
718 unsigned runtime_plt_and_got_offset;718 unsigned runtime_plt_and_got_offset;
@@ -1265,6 +1265,7 @@ ST_FUNC void put_stabd(int type, int other, int desc); · 546 unmodified lines
1265ST_FUNC void relocate_common_syms(void);1265ST_FUNC void relocate_common_syms(void);
1266ST_FUNC void relocate_syms(TCCState *s1, int do_resolve);1266ST_FUNC void relocate_syms(TCCState *s1, int do_resolve);
1267ST_FUNC void relocate_section(TCCState *s1, Section *s);1267ST_FUNC void relocate_section(TCCState *s1, Section *s);
1268ST_FUNC void relocate_plt(TCCState *s1);
1268 1269
1269ST_FUNC void tcc_add_linker_symbols(TCCState *s1);1270ST_FUNC void tcc_add_linker_symbols(TCCState *s1);
1270ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);1271ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
tccelf.c+21−16
@@ -851,8 +851,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 850 unmodified lines
851 /* We've put the PLT slot offset into r_addend when generating851 /* We've put the PLT slot offset into r_addend when generating
852 it, and that's what we must use as relocation value (adjusted852 it, and that's what we must use as relocation value (adjusted
853 by section offset of course). */853 by section offset of course). */
854 if (s1->output_type != TCC_OUTPUT_MEMORY)854 val = s1->plt->sh_addr + rel->r_addend;
855 val = s1->plt->sh_addr + rel->r_addend;
856 /* fallthrough. */855 /* fallthrough. */
857 856
858 plt32pc32:857 plt32pc32:
@@ -878,7 +877,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 19 unmodified lines
878 case R_X86_64_GLOB_DAT:877 case R_X86_64_GLOB_DAT:
879 case R_X86_64_JUMP_SLOT:878 case R_X86_64_JUMP_SLOT:
880 /* They don't need addend */879 /* They don't need addend */
881 *(int *)ptr = val - rel->r_addend;880 *(addr_t *)ptr = val - rel->r_addend;
882 break;881 break;
883 case R_X86_64_GOTPCREL:882 case R_X86_64_GOTPCREL:
884#ifdef TCC_HAS_RUNTIME_PLTGOT883#ifdef TCC_HAS_RUNTIME_PLTGOT
@@ -1042,7 +1041,7 @@ static unsigned long put_got_entry(TCCState *s1, · 157 unmodified lines
1042 if (!s1->got)1041 if (!s1->got)
1043 build_got(s1);1042 build_got(s1);
1044 1043
1045 need_plt_entry = s1->dynsym &&1044 need_plt_entry =
1046#ifdef TCC_TARGET_X86_641045#ifdef TCC_TARGET_X86_64
1047 (reloc_type == R_X86_64_JUMP_SLOT);1046 (reloc_type == R_X86_64_JUMP_SLOT);
1048#elif defined(TCC_TARGET_I386)1047#elif defined(TCC_TARGET_I386)
@@ -1053,6 +1052,13 @@ static unsigned long put_got_entry(TCCState *s1, · 4 unmodified lines
1053 0;1052 0;
1054#endif1053#endif
1055 1054
1055 if (need_plt_entry && !s1->plt) {
1056 /* add PLT */
1057 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
1058 SHF_ALLOC | SHF_EXECINSTR);
1059 s1->plt->sh_entsize = 4;
1060 }
1061
1056 /* If a got/plt entry already exists for that symbol, no need to add one */1062 /* If a got/plt entry already exists for that symbol, no need to add one */
1057 if (sym_index < s1->nb_sym_attrs) {1063 if (sym_index < s1->nb_sym_attrs) {
1058 if (need_plt_entry && s1->sym_attrs[sym_index].plt_offset)1064 if (need_plt_entry && s1->sym_attrs[sym_index].plt_offset)
@@ -1067,10 +1073,9 @@ static unsigned long put_got_entry(TCCState *s1, · 8 unmodified lines
1067 if (!need_plt_entry)1073 if (!need_plt_entry)
1068 symattr->got_offset = s1->got->data_offset;1074 symattr->got_offset = s1->got->data_offset;
1069 1075
1070 if (s1->dynsym) {1076 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1071 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];1077 name = (char *) symtab_section->link->data + sym->st_name;
1072 name = (char *) symtab_section->link->data + sym->st_name;1078 offset = sym->st_value;
1073 offset = sym->st_value;
1074#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)1079#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1075 if (need_plt_entry) {1080 if (need_plt_entry) {
1076 Section *plt;1081 Section *plt;
@@ -1165,10 +1170,13 @@ static unsigned long put_got_entry(TCCState *s1, · 88 unmodified lines
1165 offset = plt->data_offset - 16;1170 offset = plt->data_offset - 16;
1166 }1171 }
1167#elif defined(TCC_TARGET_C67)1172#elif defined(TCC_TARGET_C67)
1173 if (s1->dynsym) {
1168 tcc_error("C67 got not implemented");1174 tcc_error("C67 got not implemented");
1175 }
1169#else1176#else
1170#error unsupported CPU1177#error unsupported CPU
1171#endif1178#endif
1179 if (s1->dynsym) {
1172 /* XXX This might generate multiple syms for name. */1180 /* XXX This might generate multiple syms for name. */
1173 index = put_elf_sym(s1->dynsym, offset,1181 index = put_elf_sym(s1->dynsym, offset,
1174 size, info, 0, sym->st_shndx, name);1182 size, info, 0, sym->st_shndx, name);
@@ -1316,8 +1324,7 @@ ST_FUNC void build_got_entries(TCCState *s1) · 141 unmodified lines
1316 reloc_type = R_X86_64_JUMP_SLOT;1324 reloc_type = R_X86_64_JUMP_SLOT;
1317 ofs = put_got_entry(s1, reloc_type, sym->st_size,1325 ofs = put_got_entry(s1, reloc_type, sym->st_size,
1318 sym->st_info, sym_index);1326 sym->st_info, sym_index);
1319 if (type == R_X86_64_PLT321327 if (type == R_X86_64_PLT32)
1320 && s1->output_type != TCC_OUTPUT_MEMORY)
1321 /* We store the place of the generated PLT slot1328 /* We store the place of the generated PLT slot
1322 in our addend. */1329 in our addend. */
1323 rel->r_addend += ofs;1330 rel->r_addend += ofs;
@@ -1754,10 +1761,13 @@ static void export_global_syms(TCCState *s1) · 430 unmodified lines
1754 1761
1755/* relocate the PLT: compute addresses and offsets in the PLT now that final1762/* relocate the PLT: compute addresses and offsets in the PLT now that final
1756 address for PLT and GOT are known (see fill_program_header) */1763 address for PLT and GOT are known (see fill_program_header) */
1757static void relocate_plt(TCCState *s1)1764ST_FUNC void relocate_plt(TCCState *s1)
1758{1765{
1759 uint8_t *p, *p_end;1766 uint8_t *p, *p_end;
1760 1767
1768 if (!s1->plt)
1769 return;
1770
1761 p = s1->plt->data;1771 p = s1->plt->data;
1762 p_end = p + s1->plt->data_offset;1772 p_end = p + s1->plt->data_offset;
1763 if (p < p_end) {1773 if (p < p_end) {
@@ -2346,11 +2356,6 @@ static int elf_output_file(TCCState *s1, const char *filename) · 582 unmodified lines
2346 dynamic->link = dynstr;2356 dynamic->link = dynstr;
2347 dynamic->sh_entsize = sizeof(ElfW(Dyn));2357 dynamic->sh_entsize = sizeof(ElfW(Dyn));
2348 2358
2349 /* add PLT */
2350 s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
2351 SHF_ALLOC | SHF_EXECINSTR);
2352 s1->plt->sh_entsize = 4;
2353
2354 build_got(s1);2359 build_got(s1);
2355 2360
2356 if (file_type == TCC_OUTPUT_EXE) {2361 if (file_type == TCC_OUTPUT_EXE) {
tccrun.c+1−0
@@ -197,6 +197,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) · 196 unmodified lines
197 if (s->reloc)197 if (s->reloc)
198 relocate_section(s1, s);198 relocate_section(s1, s);
199 }199 }
200 relocate_plt(s1);
200 201
201 for(i = 1; i < s1->nb_sections; i++) {202 for(i = 1; i < s1->nb_sections; i++) {
202 s = s1->sections[i];203 s = s1->sections[i];

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