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 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
26 relocations, returns -1. */
27 int code_reloc (int reloc_type
)
33 case R_X86_64_GOTPC32
:
34 case R_X86_64_GOTPC64
:
35 case R_X86_64_GOTPCREL
:
36 case R_X86_64_GOTPCRELX
:
37 case R_X86_64_REX_GOTPCRELX
:
38 case R_X86_64_GOTTPOFF
:
41 case R_X86_64_GLOB_DAT
:
43 case R_X86_64_RELATIVE
:
44 case R_X86_64_GOTOFF64
:
50 case R_X86_64_PLTOFF64
:
51 case R_X86_64_JUMP_SLOT
:
55 tcc_error ("Unknown relocation type: %d", reloc_type
);
59 /* Returns an enumerator to describe whether and when the relocation needs a
60 GOT and/or PLT entry to be created. See tcc.h for a description of the
62 int gotplt_entry_type (int reloc_type
)
65 case R_X86_64_GLOB_DAT
:
66 case R_X86_64_JUMP_SLOT
:
68 case R_X86_64_RELATIVE
:
69 return NO_GOTPLT_ENTRY
;
71 /* The following relocs wouldn't normally need GOT or PLT
72 slots, but we need them for simplicity in the link
73 editor part. See our caller for comments. */
79 return AUTO_GOTPLT_ENTRY
;
81 case R_X86_64_GOTTPOFF
:
82 return BUILD_GOT_ONLY
;
86 case R_X86_64_GOTPC32
:
87 case R_X86_64_GOTPC64
:
88 case R_X86_64_GOTOFF64
:
89 case R_X86_64_GOTPCREL
:
90 case R_X86_64_GOTPCRELX
:
91 case R_X86_64_REX_GOTPCRELX
:
93 case R_X86_64_PLTOFF64
:
94 return ALWAYS_GOTPLT_ENTRY
;
97 tcc_error ("Unknown relocation type: %d", reloc_type
);
101 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
103 Section
*plt
= s1
->plt
;
106 unsigned plt_offset
, relofs
;
110 /* empty PLT: create PLT0 entry that pushes the library identifier
111 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
112 (GOT + 2 * PTR_SIZE) */
113 if (plt
->data_offset
== 0) {
114 p
= section_ptr_add(plt
, 16);
115 p
[0] = 0xff; /* pushl got + PTR_SIZE */
117 write32le(p
+ 2, PTR_SIZE
);
118 p
[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
120 write32le(p
+ 8, PTR_SIZE
* 2);
122 plt_offset
= plt
->data_offset
;
124 /* The PLT slot refers to the relocation entry it needs via offset.
125 The reloc entry is created below, so its offset is the current
127 relofs
= s1
->got
->reloc
? s1
->got
->reloc
->data_offset
: 0;
129 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
130 p
= section_ptr_add(plt
, 16);
131 p
[0] = 0xff; /* jmp *(got + x) */
133 write32le(p
+ 2, got_offset
);
134 p
[6] = 0x68; /* push $xxx */
135 /* On x86-64, the relocation is referred to by _index_ */
136 write32le(p
+ 7, relofs
/ sizeof (ElfW_Rel
));
137 p
[11] = 0xe9; /* jmp plt_start */
138 write32le(p
+ 12, -(plt
->data_offset
));
142 /* relocate the PLT: compute addresses and offsets in the PLT now that final
143 address for PLT and GOT are known (see fill_program_header) */
144 ST_FUNC
void relocate_plt(TCCState
*s1
)
152 p_end
= p
+ s1
->plt
->data_offset
;
155 int x
= s1
->got
->sh_addr
- s1
->plt
->sh_addr
- 6;
157 add32le(p
+ 8, x
- 6);
160 add32le(p
+ 2, x
+ s1
->plt
->data
- p
);
166 static ElfW_Rel
*qrel
; /* ptr to next reloc entry reused */
168 void relocate_init(Section
*sr
)
170 qrel
= (ElfW_Rel
*) sr
->data
;
173 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
175 int sym_index
, esym_index
;
177 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
181 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
182 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
183 qrel
->r_offset
= rel
->r_offset
;
185 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_64
);
186 qrel
->r_addend
= rel
->r_addend
;
190 qrel
->r_info
= ELFW(R_INFO
)(0, R_X86_64_RELATIVE
);
191 qrel
->r_addend
= read64le(ptr
) + val
;
199 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
200 /* XXX: this logic may depend on TCC's codegen
201 now TCC uses R_X86_64_32 even for a 64bit pointer */
202 qrel
->r_info
= ELFW(R_INFO
)(0, R_X86_64_RELATIVE
);
203 /* Use sign extension! */
204 qrel
->r_addend
= (int)read32le(ptr
) + val
;
211 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
213 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
215 qrel
->r_offset
= rel
->r_offset
;
216 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_PC32
);
217 /* Use sign extension! */
218 qrel
->r_addend
= (int)read32le(ptr
) + rel
->r_addend
;
226 /* fallthrough: val already holds the PLT slot address */
231 diff
= (long long)val
- addr
;
232 if (diff
< -2147483648LL || diff
> 2147483647LL) {
233 tcc_error("internal error: relocation failed");
239 case R_X86_64_PLTOFF64
:
240 add64le(ptr
, val
- s1
->got
->sh_addr
+ rel
->r_addend
);
244 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
246 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
248 qrel
->r_offset
= rel
->r_offset
;
249 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_X86_64_PC64
);
250 qrel
->r_addend
= read64le(ptr
) + rel
->r_addend
;
255 add64le(ptr
, val
- addr
);
258 case R_X86_64_GLOB_DAT
:
259 case R_X86_64_JUMP_SLOT
:
260 /* They don't need addend */
261 write64le(ptr
, val
- rel
->r_addend
);
263 case R_X86_64_GOTPCREL
:
264 case R_X86_64_GOTPCRELX
:
265 case R_X86_64_REX_GOTPCRELX
:
266 add32le(ptr
, s1
->got
->sh_addr
- addr
+
267 get_sym_attr(s1
, sym_index
, 0)->got_offset
- 4);
269 case R_X86_64_GOTPC32
:
270 add32le(ptr
, s1
->got
->sh_addr
- addr
+ rel
->r_addend
);
272 case R_X86_64_GOTPC64
:
273 add64le(ptr
, s1
->got
->sh_addr
- addr
+ rel
->r_addend
);
275 case R_X86_64_GOTTPOFF
:
276 add32le(ptr
, val
- s1
->got
->sh_addr
);
279 /* we load the got offset */
280 add32le(ptr
, get_sym_attr(s1
, sym_index
, 0)->got_offset
);
283 /* we load the got offset */
284 add64le(ptr
, get_sym_attr(s1
, sym_index
, 0)->got_offset
);
286 case R_X86_64_GOTOFF64
:
287 add64le(ptr
, val
- s1
->got
->sh_addr
);
289 case R_X86_64_RELATIVE
:
291 add32le(ptr
, val
- s1
->pe_imagebase
);
298 #endif /* !TARGET_DEFS_ONLY */