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 #ifdef NEED_RELOC_TYPE
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
:
50 case R_X86_64_DTPOFF64
:
51 case R_X86_64_TPOFF64
:
57 case R_X86_64_PLTOFF64
:
58 case R_X86_64_JUMP_SLOT
:
64 /* Returns an enumerator to describe whether and when the relocation needs a
65 GOT and/or PLT entry to be created. See tcc.h for a description of the
67 int gotplt_entry_type (int reloc_type
)
70 case R_X86_64_GLOB_DAT
:
71 case R_X86_64_JUMP_SLOT
:
73 case R_X86_64_RELATIVE
:
74 return NO_GOTPLT_ENTRY
;
76 /* The following relocs wouldn't normally need GOT or PLT
77 slots, but we need them for simplicity in the link
78 editor part. See our caller for comments. */
84 return AUTO_GOTPLT_ENTRY
;
86 case R_X86_64_GOTTPOFF
:
87 return BUILD_GOT_ONLY
;
91 case R_X86_64_GOTPC32
:
92 case R_X86_64_GOTPC64
:
93 case R_X86_64_GOTOFF64
:
94 case R_X86_64_GOTPCREL
:
95 case R_X86_64_GOTPCRELX
:
98 case R_X86_64_DTPOFF32
:
99 case R_X86_64_TPOFF32
:
100 case R_X86_64_DTPOFF64
:
101 case R_X86_64_TPOFF64
:
102 case R_X86_64_REX_GOTPCRELX
:
104 case R_X86_64_PLTOFF64
:
105 return ALWAYS_GOTPLT_ENTRY
;
111 #ifdef NEED_BUILD_GOT
112 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
114 Section
*plt
= s1
->plt
;
117 unsigned plt_offset
, relofs
;
121 /* empty PLT: create PLT0 entry that pushes the library identifier
122 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
123 (GOT + 2 * PTR_SIZE) */
124 if (plt
->data_offset
== 0) {
125 p
= section_ptr_add(plt
, 16);
126 p
[0] = 0xff; /* pushl got + PTR_SIZE */
128 write32le(p
+ 2, PTR_SIZE
);
129 p
[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
131 write32le(p
+ 8, PTR_SIZE
* 2);
133 plt_offset
= plt
->data_offset
;
135 /* The PLT slot refers to the relocation entry it needs via offset.
136 The reloc entry is created below, so its offset is the current
138 relofs
= s1
->plt
->reloc
? s1
->plt
->reloc
->data_offset
: 0;
140 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
141 p
= section_ptr_add(plt
, 16);
142 p
[0] = 0xff; /* jmp *(got + x) */
144 write32le(p
+ 2, got_offset
);
145 p
[6] = 0x68; /* push $xxx */
146 /* On x86-64, the relocation is referred to by _index_ */
147 write32le(p
+ 7, relofs
/ sizeof (ElfW_Rel
) - 1);
148 p
[11] = 0xe9; /* jmp plt_start */
149 write32le(p
+ 12, -(plt
->data_offset
));
153 /* relocate the PLT: compute addresses and offsets in the PLT now that final
154 address for PLT and GOT are known (see fill_program_header) */
155 ST_FUNC
void relocate_plt(TCCState
*s1
)
163 p_end
= p
+ s1
->plt
->data_offset
;
166 int x
= s1
->got
->sh_addr
- s1
->plt
->sh_addr
- 6;
168 add32le(p
+ 8, x
- 6);
171 add32le(p
+ 2, x
+ (s1
->plt
->data
- p
));
176 if (s1
->plt
->reloc
) {
178 int x
= s1
->plt
->sh_addr
+ 16 + 6;
180 for_each_elem(s1
->plt
->reloc
, 0, rel
, ElfW_Rel
) {
181 write64le(p
+ rel
->r_offset
, x
);
189 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
191 int sym_index
, esym_index
;
193 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
197 if (s1
->output_type
& TCC_OUTPUT_DYN
) {
198 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
199 qrel
->r_offset
= rel
->r_offset
;
201 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_64
);
202 qrel
->r_addend
= rel
->r_addend
;
206 qrel
->r_info
= ELFW(R_INFO
)(0, R_X86_64_RELATIVE
);
207 qrel
->r_addend
= read64le(ptr
) + val
;
215 if (s1
->output_type
& TCC_OUTPUT_DYN
) {
216 /* XXX: this logic may depend on TCC's codegen
217 now TCC uses R_X86_64_32 even for a 64bit pointer */
218 qrel
->r_offset
= rel
->r_offset
;
219 qrel
->r_info
= ELFW(R_INFO
)(0, R_X86_64_RELATIVE
);
220 /* Use sign extension! */
221 qrel
->r_addend
= (int)read32le(ptr
) + val
;
228 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
230 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
232 qrel
->r_offset
= rel
->r_offset
;
233 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_PC32
);
234 /* Use sign extension! */
235 qrel
->r_addend
= (int)read32le(ptr
) + rel
->r_addend
;
243 /* fallthrough: val already holds the PLT slot address */
248 diff
= (long long)val
- addr
;
249 if (diff
< -2147483648LL || diff
> 2147483647LL) {
251 /* ignore overflow with undefined weak symbols */
252 if (((ElfW(Sym
)*)symtab_section
->data
)[sym_index
].st_shndx
!= SHN_UNDEF
)
254 tcc_error_noabort("internal error: relocation failed");
263 case R_X86_64_PLTOFF64
:
264 add64le(ptr
, val
- s1
->got
->sh_addr
+ rel
->r_addend
);
268 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
270 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
272 qrel
->r_offset
= rel
->r_offset
;
273 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_PC64
);
274 qrel
->r_addend
= read64le(ptr
) + rel
->r_addend
;
279 add64le(ptr
, val
- addr
);
282 case R_X86_64_GLOB_DAT
:
283 case R_X86_64_JUMP_SLOT
:
284 /* They don't need addend */
285 write64le(ptr
, val
- rel
->r_addend
);
287 case R_X86_64_GOTPCREL
:
288 case R_X86_64_GOTPCRELX
:
289 case R_X86_64_REX_GOTPCRELX
:
290 add32le(ptr
, s1
->got
->sh_addr
- addr
+
291 get_sym_attr(s1
, sym_index
, 0)->got_offset
- 4);
293 case R_X86_64_GOTPC32
:
294 add32le(ptr
, s1
->got
->sh_addr
- addr
+ rel
->r_addend
);
296 case R_X86_64_GOTPC64
:
297 add64le(ptr
, s1
->got
->sh_addr
- addr
+ rel
->r_addend
);
299 case R_X86_64_GOTTPOFF
:
300 add32le(ptr
, val
- s1
->got
->sh_addr
);
303 /* we load the got offset */
304 add32le(ptr
, get_sym_attr(s1
, sym_index
, 0)->got_offset
);
307 /* we load the got offset */
308 add64le(ptr
, get_sym_attr(s1
, sym_index
, 0)->got_offset
);
310 case R_X86_64_GOTOFF64
:
311 add64le(ptr
, val
- s1
->got
->sh_addr
);
315 static const unsigned char expect
[] = {
316 /* .byte 0x66; lea 0(%rip),%rdi */
317 0x66, 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
318 /* .word 0x6666; rex64; call __tls_get_addr@PLT */
319 0x66, 0x66, 0x48, 0xe8, 0x00, 0x00, 0x00, 0x00 };
320 static const unsigned char replace
[] = {
322 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
323 /* lea -4(%rax),%rax */
324 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 };
326 if (memcmp (ptr
-4, expect
, sizeof(expect
)) == 0) {
331 memcpy(ptr
-4, replace
, sizeof(replace
));
332 rel
[1].r_info
= ELFW(R_INFO
)(0, R_X86_64_NONE
);
333 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
334 sec
= s1
->sections
[sym
->st_shndx
];
335 x
= sym
->st_value
- sec
->sh_addr
- sec
->data_offset
;
339 tcc_error_noabort("unexpected R_X86_64_TLSGD pattern");
344 static const unsigned char expect
[] = {
345 /* lea 0(%rip),%rdi */
346 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
347 /* call __tls_get_addr@PLT */
348 0xe8, 0x00, 0x00, 0x00, 0x00 };
349 static const unsigned char replace
[] = {
350 /* data16 data16 data16 mov %fs:0,%rax */
351 0x66, 0x66, 0x66, 0x64, 0x48, 0x8b, 0x04, 0x25,
352 0x00, 0x00, 0x00, 0x00 };
354 if (memcmp (ptr
-3, expect
, sizeof(expect
)) == 0) {
355 memcpy(ptr
-3, replace
, sizeof(replace
));
356 rel
[1].r_info
= ELFW(R_INFO
)(0, R_X86_64_NONE
);
359 tcc_error_noabort("unexpected R_X86_64_TLSLD pattern");
362 case R_X86_64_DTPOFF32
:
363 case R_X86_64_TPOFF32
:
369 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
370 sec
= s1
->sections
[sym
->st_shndx
];
371 x
= val
- sec
->sh_addr
- sec
->data_offset
;
375 case R_X86_64_DTPOFF64
:
376 case R_X86_64_TPOFF64
:
382 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
383 sec
= s1
->sections
[sym
->st_shndx
];
384 x
= val
- sec
->sh_addr
- sec
->data_offset
;
390 case R_X86_64_RELATIVE
:
392 add32le(ptr
, val
- s1
->pe_imagebase
);
397 fprintf(stderr
,"FIXME: handle reloc type %d at %x [%p] to %x\n",
398 type
, (unsigned)addr
, ptr
, (unsigned)val
);
403 #endif /* !TARGET_DEFS_ONLY */