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
712712 void *write_mem;
713713 unsigned long mem_size;
714714 # 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)
716716 /* write PLT and GOT here */
717717 char *runtime_plt_and_got;
718718 unsigned runtime_plt_and_got_offset;
@@ -1265,6 +1265,7 @@ ST_FUNC void put_stabd(int type, int other, int desc); · 546 unmodified lines
12651265 ST_FUNC void relocate_common_syms(void);
12661266 ST_FUNC void relocate_syms(TCCState *s1, int do_resolve);
12671267 ST_FUNC void relocate_section(TCCState *s1, Section *s);
1268+ST_FUNC void relocate_plt(TCCState *s1);
12681269
12691270 ST_FUNC void tcc_add_linker_symbols(TCCState *s1);
12701271 ST_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
851851 /* We've put the PLT slot offset into r_addend when generating
852852 it, and that's what we must use as relocation value (adjusted
853853 by section offset of course). */
854− if (s1->output_type != TCC_OUTPUT_MEMORY)
855− val = s1->plt->sh_addr + rel->r_addend;
854+ val = s1->plt->sh_addr + rel->r_addend;
856855 /* fallthrough. */
857856
858857 plt32pc32:
@@ -878,7 +877,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 19 unmodified lines
878877 case R_X86_64_GLOB_DAT:
879878 case R_X86_64_JUMP_SLOT:
880879 /* They don't need addend */
881− *(int *)ptr = val - rel->r_addend;
880+ *(addr_t *)ptr = val - rel->r_addend;
882881 break;
883882 case R_X86_64_GOTPCREL:
884883 #ifdef TCC_HAS_RUNTIME_PLTGOT
@@ -1042,7 +1041,7 @@ static unsigned long put_got_entry(TCCState *s1, · 157 unmodified lines
10421041 if (!s1->got)
10431042 build_got(s1);
10441043
1045− need_plt_entry = s1->dynsym &&
1044+ need_plt_entry =
10461045 #ifdef TCC_TARGET_X86_64
10471046 (reloc_type == R_X86_64_JUMP_SLOT);
10481047 #elif defined(TCC_TARGET_I386)
@@ -1053,6 +1052,13 @@ static unsigned long put_got_entry(TCCState *s1, · 4 unmodified lines
10531052 0;
10541053 #endif
10551054
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+
10561062 /* If a got/plt entry already exists for that symbol, no need to add one */
10571063 if (sym_index < s1->nb_sym_attrs) {
10581064 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
10671073 if (!need_plt_entry)
10681074 symattr->got_offset = s1->got->data_offset;
10691075
1070− if (s1->dynsym) {
1071− sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1072− name = (char *) symtab_section->link->data + sym->st_name;
1073− offset = sym->st_value;
1076+ sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1077+ name = (char *) symtab_section->link->data + sym->st_name;
1078+ offset = sym->st_value;
10741079 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
10751080 if (need_plt_entry) {
10761081 Section *plt;
@@ -1165,10 +1170,13 @@ static unsigned long put_got_entry(TCCState *s1, · 88 unmodified lines
11651170 offset = plt->data_offset - 16;
11661171 }
11671172 #elif defined(TCC_TARGET_C67)
1173+ if (s1->dynsym) {
11681174 tcc_error("C67 got not implemented");
1175+ }
11691176 #else
11701177 #error unsupported CPU
11711178 #endif
1179+ if (s1->dynsym) {
11721180 /* XXX This might generate multiple syms for name. */
11731181 index = put_elf_sym(s1->dynsym, offset,
11741182 size, info, 0, sym->st_shndx, name);
@@ -1316,8 +1324,7 @@ ST_FUNC void build_got_entries(TCCState *s1) · 141 unmodified lines
13161324 reloc_type = R_X86_64_JUMP_SLOT;
13171325 ofs = put_got_entry(s1, reloc_type, sym->st_size,
13181326 sym->st_info, sym_index);
1319− if (type == R_X86_64_PLT32
1320− && s1->output_type != TCC_OUTPUT_MEMORY)
1327+ if (type == R_X86_64_PLT32)
13211328 /* We store the place of the generated PLT slot
13221329 in our addend. */
13231330 rel->r_addend += ofs;
@@ -1754,10 +1761,13 @@ static void export_global_syms(TCCState *s1) · 430 unmodified lines
17541761
17551762 /* relocate the PLT: compute addresses and offsets in the PLT now that final
17561763 address for PLT and GOT are known (see fill_program_header) */
1757−static void relocate_plt(TCCState *s1)
1764+ST_FUNC void relocate_plt(TCCState *s1)
17581765 {
17591766 uint8_t *p, *p_end;
17601767
1768+ if (!s1->plt)
1769+ return;
1770+
17611771 p = s1->plt->data;
17621772 p_end = p + s1->plt->data_offset;
17631773 if (p < p_end) {
@@ -2346,11 +2356,6 @@ static int elf_output_file(TCCState *s1, const char *filename) · 582 unmodified lines
23462356 dynamic->link = dynstr;
23472357 dynamic->sh_entsize = sizeof(ElfW(Dyn));
23482358
2349− /* add PLT */
2350− s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
2351− SHF_ALLOC | SHF_EXECINSTR);
2352− s1->plt->sh_entsize = 4;
2353−
23542359 build_got(s1);
23552360
23562361 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
197197 if (s->reloc)
198198 relocate_section(s1, s);
199199 }
200+ relocate_plt(s1);
200201
201202 for(i = 1; i < s1->nb_sections; i++) {
202203 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