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
11 #define R_RELATIVE R_X86_64_RELATIVE
13 #define R_NUM R_X86_64_NUM
15 #define ELF_START_ADDR 0x400000
16 #define ELF_PAGE_SIZE 0x200000
18 #define PCRELATIVE_DLLPLT 1
19 #define RELOCATE_DLLPLT 1
21 #else /* !TARGET_DEFS_ONLY */
25 #if !defined(ELF_OBJ_ONLY) || defined(TCC_TARGET_MACHO)
26 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
27 relocations, returns -1. */
28 int code_reloc (int reloc_type
)
34 case R_X86_64_GOTPC32
:
35 case R_X86_64_GOTPC64
:
36 case R_X86_64_GOTPCREL
:
37 case R_X86_64_GOTPCRELX
:
38 case R_X86_64_REX_GOTPCRELX
:
39 case R_X86_64_GOTTPOFF
:
42 case R_X86_64_GLOB_DAT
:
44 case R_X86_64_RELATIVE
:
45 case R_X86_64_GOTOFF64
:
48 case R_X86_64_DTPOFF32
:
49 case R_X86_64_TPOFF32
:
55 case R_X86_64_PLTOFF64
:
56 case R_X86_64_JUMP_SLOT
:
62 /* Returns an enumerator to describe whether and when the relocation needs a
63 GOT and/or PLT entry to be created. See tcc.h for a description of the
65 int gotplt_entry_type (int reloc_type
)
68 case R_X86_64_GLOB_DAT
:
69 case R_X86_64_JUMP_SLOT
:
71 case R_X86_64_RELATIVE
:
72 return NO_GOTPLT_ENTRY
;
74 /* The following relocs wouldn't normally need GOT or PLT
75 slots, but we need them for simplicity in the link
76 editor part. See our caller for comments. */
82 return AUTO_GOTPLT_ENTRY
;
84 case R_X86_64_GOTTPOFF
:
85 return BUILD_GOT_ONLY
;
89 case R_X86_64_GOTPC32
:
90 case R_X86_64_GOTPC64
:
91 case R_X86_64_GOTOFF64
:
92 case R_X86_64_GOTPCREL
:
93 case R_X86_64_GOTPCRELX
:
96 case R_X86_64_DTPOFF32
:
97 case R_X86_64_TPOFF32
:
98 case R_X86_64_REX_GOTPCRELX
:
100 case R_X86_64_PLTOFF64
:
101 return ALWAYS_GOTPLT_ENTRY
;
107 #if !defined(TCC_TARGET_MACHO) || defined TCC_IS_NATIVE
108 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
110 Section
*plt
= s1
->plt
;
113 unsigned plt_offset
, relofs
;
117 /* empty PLT: create PLT0 entry that pushes the library identifier
118 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
119 (GOT + 2 * PTR_SIZE) */
120 if (plt
->data_offset
== 0) {
121 p
= section_ptr_add(plt
, 16);
122 p
[0] = 0xff; /* pushl got + PTR_SIZE */
124 write32le(p
+ 2, PTR_SIZE
);
125 p
[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
127 write32le(p
+ 8, PTR_SIZE
* 2);
129 plt_offset
= plt
->data_offset
;
131 /* The PLT slot refers to the relocation entry it needs via offset.
132 The reloc entry is created below, so its offset is the current
134 relofs
= s1
->plt
->reloc
? s1
->plt
->reloc
->data_offset
: 0;
136 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
137 p
= section_ptr_add(plt
, 16);
138 p
[0] = 0xff; /* jmp *(got + x) */
140 write32le(p
+ 2, got_offset
);
141 p
[6] = 0x68; /* push $xxx */
142 /* On x86-64, the relocation is referred to by _index_ */
143 write32le(p
+ 7, relofs
/ sizeof (ElfW_Rel
) - 1);
144 p
[11] = 0xe9; /* jmp plt_start */
145 write32le(p
+ 12, -(plt
->data_offset
));
149 /* relocate the PLT: compute addresses and offsets in the PLT now that final
150 address for PLT and GOT are known (see fill_program_header) */
151 ST_FUNC
void relocate_plt(TCCState
*s1
)
159 p_end
= p
+ s1
->plt
->data_offset
;
162 int x
= s1
->got
->sh_addr
- s1
->plt
->sh_addr
- 6;
164 add32le(p
+ 8, x
- 6);
167 add32le(p
+ 2, x
+ (s1
->plt
->data
- p
));
172 if (s1
->plt
->reloc
) {
174 int x
= s1
->plt
->sh_addr
+ 16 + 6;
176 for_each_elem(s1
->plt
->reloc
, 0, rel
, ElfW_Rel
) {
177 write64le(p
+ rel
->r_offset
, x
);
185 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
187 int sym_index
, esym_index
;
189 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
193 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
194 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
195 qrel
->r_offset
= rel
->r_offset
;
197 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_64
);
198 qrel
->r_addend
= rel
->r_addend
;
202 qrel
->r_info
= ELFW(R_INFO
)(0, R_X86_64_RELATIVE
);
203 qrel
->r_addend
= read64le(ptr
) + val
;
211 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
212 /* XXX: this logic may depend on TCC's codegen
213 now TCC uses R_X86_64_32 even for a 64bit pointer */
214 qrel
->r_offset
= rel
->r_offset
;
215 qrel
->r_info
= ELFW(R_INFO
)(0, R_X86_64_RELATIVE
);
216 /* Use sign extension! */
217 qrel
->r_addend
= (int)read32le(ptr
) + val
;
224 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
226 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
228 qrel
->r_offset
= rel
->r_offset
;
229 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_PC32
);
230 /* Use sign extension! */
231 qrel
->r_addend
= (int)read32le(ptr
) + rel
->r_addend
;
239 /* fallthrough: val already holds the PLT slot address */
244 diff
= (long long)val
- addr
;
245 if (diff
< -2147483648LL || diff
> 2147483647LL) {
246 tcc_error("internal error: relocation failed");
252 case R_X86_64_PLTOFF64
:
253 add64le(ptr
, val
- s1
->got
->sh_addr
+ rel
->r_addend
);
257 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
259 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
261 qrel
->r_offset
= rel
->r_offset
;
262 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_PC64
);
263 qrel
->r_addend
= read64le(ptr
) + rel
->r_addend
;
268 add64le(ptr
, val
- addr
);
271 case R_X86_64_GLOB_DAT
:
272 case R_X86_64_JUMP_SLOT
:
273 /* They don't need addend */
274 write64le(ptr
, val
- rel
->r_addend
);
276 case R_X86_64_GOTPCREL
:
277 case R_X86_64_GOTPCRELX
:
278 case R_X86_64_REX_GOTPCRELX
:
279 add32le(ptr
, s1
->got
->sh_addr
- addr
+
280 get_sym_attr(s1
, sym_index
, 0)->got_offset
- 4);
282 case R_X86_64_GOTPC32
:
283 add32le(ptr
, s1
->got
->sh_addr
- addr
+ rel
->r_addend
);
285 case R_X86_64_GOTPC64
:
286 add64le(ptr
, s1
->got
->sh_addr
- addr
+ rel
->r_addend
);
288 case R_X86_64_GOTTPOFF
:
289 add32le(ptr
, val
- s1
->got
->sh_addr
);
292 /* we load the got offset */
293 add32le(ptr
, get_sym_attr(s1
, sym_index
, 0)->got_offset
);
296 /* we load the got offset */
297 add64le(ptr
, get_sym_attr(s1
, sym_index
, 0)->got_offset
);
299 case R_X86_64_GOTOFF64
:
300 add64le(ptr
, val
- s1
->got
->sh_addr
);
304 static const unsigned char expect
[] = {
305 /* .byte 0x66; lea 0(%rip),%rdi */
306 0x66, 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
307 /* .word 0x6666; rex64; call __tls_get_addr@PLT */
308 0x66, 0x66, 0x48, 0xe8, 0x00, 0x00, 0x00, 0x00 };
309 static const unsigned char replace
[] = {
311 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
312 /* lea -4(%rax),%rax */
313 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 };
315 if (memcmp (ptr
-4, expect
, sizeof(expect
)) == 0) {
320 memcpy(ptr
-4, replace
, sizeof(replace
));
321 rel
[1].r_info
= ELFW(R_INFO
)(0, R_X86_64_NONE
);
322 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
323 sec
= s1
->sections
[sym
->st_shndx
];
324 x
= sym
->st_value
- sec
->sh_addr
- sec
->data_offset
;
328 tcc_error("unexpected R_X86_64_TLSGD pattern");
333 static const unsigned char expect
[] = {
334 /* lea 0(%rip),%rdi */
335 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
336 /* call __tls_get_addr@PLT */
337 0xe8, 0x00, 0x00, 0x00, 0x00 };
338 static const unsigned char replace
[] = {
339 /* data16 data16 data16 mov %fs:0,%rax */
340 0x66, 0x66, 0x66, 0x64, 0x48, 0x8b, 0x04, 0x25,
341 0x00, 0x00, 0x00, 0x00 };
343 if (memcmp (ptr
-3, expect
, sizeof(expect
)) == 0) {
344 memcpy(ptr
-3, replace
, sizeof(replace
));
345 rel
[1].r_info
= ELFW(R_INFO
)(0, R_X86_64_NONE
);
348 tcc_error("unexpected R_X86_64_TLSLD pattern");
351 case R_X86_64_DTPOFF32
:
352 case R_X86_64_TPOFF32
:
358 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
359 sec
= s1
->sections
[sym
->st_shndx
];
360 x
= val
- sec
->sh_addr
- sec
->data_offset
;
366 case R_X86_64_RELATIVE
:
368 add32le(ptr
, val
- s1
->pe_imagebase
);
375 #endif /* !TARGET_DEFS_ONLY */