1 #ifdef TARGET_DEFS_ONLY
3 #define EM_TCC_TARGET EM_X86_64
5 /* relocation type for 32 bit data relocation */
6 #define R_DATA_32 R_X86_64_32S
7 #define R_DATA_PTR R_X86_64_64
8 #define R_JMP_SLOT R_X86_64_JUMP_SLOT
9 #define R_GLOB_DAT R_X86_64_GLOB_DAT
10 #define R_COPY R_X86_64_COPY
12 #define R_NUM R_X86_64_NUM
14 #define ELF_START_ADDR 0x400000
15 #define ELF_PAGE_SIZE 0x200000
17 #define PCRELATIVE_DLLPLT 1
18 #define RELOCATE_DLLPLT 1
20 #else /* !TARGET_DEFS_ONLY */
24 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
25 relocations, returns -1. */
26 int code_reloc (int reloc_type
)
32 case R_X86_64_GOTPCREL
:
33 case R_X86_64_GOTPCRELX
:
34 case R_X86_64_REX_GOTPCRELX
:
35 case R_X86_64_GOTTPOFF
:
37 case R_X86_64_GLOB_DAT
:
39 case R_X86_64_RELATIVE
:
44 case R_X86_64_JUMP_SLOT
:
48 tcc_error ("Unknown relocation type: %d", reloc_type
);
52 /* Returns an enumerator to describe wether and when the relocation needs a
53 GOT and/or PLT entry to be created. See tcc.h for a description of the
55 int gotplt_entry_type (int reloc_type
)
58 case R_X86_64_GLOB_DAT
:
59 case R_X86_64_JUMP_SLOT
:
61 case R_X86_64_RELATIVE
:
62 return NO_GOTPLT_ENTRY
;
64 /* The following relocs wouldn't normally need GOT or PLT
65 slots, but we need them for simplicity in the link
66 editor part. See our caller for comments. */
71 return AUTO_GOTPLT_ENTRY
;
73 case R_X86_64_GOTTPOFF
:
74 return BUILD_GOT_ONLY
;
77 case R_X86_64_GOTPCREL
:
78 case R_X86_64_GOTPCRELX
:
79 case R_X86_64_REX_GOTPCRELX
:
81 return ALWAYS_GOTPLT_ENTRY
;
84 tcc_error ("Unknown relocation type: %d", reloc_type
);
88 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
90 Section
*plt
= s1
->plt
;
93 unsigned plt_offset
, relofs
;
97 /* empty PLT: create PLT0 entry that pushes the library indentifier
98 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
99 (GOT + 2 * PTR_SIZE) */
100 if (plt
->data_offset
== 0) {
101 p
= section_ptr_add(plt
, 16);
102 p
[0] = 0xff; /* pushl got + PTR_SIZE */
104 write32le(p
+ 2, PTR_SIZE
);
105 p
[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
107 write32le(p
+ 8, PTR_SIZE
* 2);
109 plt_offset
= plt
->data_offset
;
111 /* The PLT slot refers to the relocation entry it needs via offset.
112 The reloc entry is created below, so its offset is the current
114 relofs
= s1
->got
->reloc
? s1
->got
->reloc
->data_offset
: 0;
116 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
117 p
= section_ptr_add(plt
, 16);
118 p
[0] = 0xff; /* jmp *(got + x) */
120 write32le(p
+ 2, got_offset
);
121 p
[6] = 0x68; /* push $xxx */
122 /* On x86-64, the relocation is referred to by _index_ */
123 write32le(p
+ 7, relofs
/ sizeof (ElfW_Rel
));
124 p
[11] = 0xe9; /* jmp plt_start */
125 write32le(p
+ 12, -(plt
->data_offset
));
129 /* relocate the PLT: compute addresses and offsets in the PLT now that final
130 address for PLT and GOT are known (see fill_program_header) */
131 ST_FUNC
void relocate_plt(TCCState
*s1
)
139 p_end
= p
+ s1
->plt
->data_offset
;
142 int x
= s1
->got
->sh_addr
- s1
->plt
->sh_addr
- 6;
144 add32le(p
+ 8, x
- 6);
147 add32le(p
+ 2, x
+ s1
->plt
->data
- p
);
153 static ElfW_Rel
*qrel
; /* ptr to next reloc entry reused */
155 void relocate_init(Section
*sr
)
157 qrel
= (ElfW_Rel
*) sr
->data
;
160 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, char *ptr
, addr_t addr
, addr_t val
)
162 int sym_index
, esym_index
;
164 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
168 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
169 esym_index
= s1
->sym_attrs
[sym_index
].dyn_index
;
170 qrel
->r_offset
= rel
->r_offset
;
172 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_64
);
173 qrel
->r_addend
= rel
->r_addend
;
177 qrel
->r_info
= ELFW(R_INFO
)(0, R_X86_64_RELATIVE
);
178 qrel
->r_addend
= read64le(ptr
) + val
;
186 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
187 /* XXX: this logic may depend on TCC's codegen
188 now TCC uses R_X86_64_32 even for a 64bit pointer */
189 qrel
->r_info
= ELFW(R_INFO
)(0, R_X86_64_RELATIVE
);
190 /* Use sign extension! */
191 qrel
->r_addend
= (int)read32le(ptr
) + val
;
198 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
200 esym_index
= s1
->sym_attrs
[sym_index
].dyn_index
;
202 qrel
->r_offset
= rel
->r_offset
;
203 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_PC32
);
204 /* Use sign extension! */
205 qrel
->r_addend
= (int)read32le(ptr
) + rel
->r_addend
;
213 /* fallthrough: val already holds the PLT slot address */
218 diff
= (long long)val
- addr
;
219 if (diff
< -2147483648LL || diff
> 2147483647LL) {
220 tcc_error("internal error: relocation failed");
225 case R_X86_64_GLOB_DAT
:
226 case R_X86_64_JUMP_SLOT
:
227 /* They don't need addend */
228 write64le(ptr
, val
- rel
->r_addend
);
230 case R_X86_64_GOTPCREL
:
231 case R_X86_64_GOTPCRELX
:
232 case R_X86_64_REX_GOTPCRELX
:
233 add32le(ptr
, s1
->got
->sh_addr
- addr
+
234 s1
->sym_attrs
[sym_index
].got_offset
- 4);
236 case R_X86_64_GOTTPOFF
:
237 add32le(ptr
, val
- s1
->got
->sh_addr
);
240 /* we load the got offset */
241 add32le(ptr
, s1
->sym_attrs
[sym_index
].got_offset
);
243 case R_X86_64_RELATIVE
:
249 #endif /* !TARGET_DEFS_ONLY */