1 #ifdef TARGET_DEFS_ONLY
3 #define EM_TCC_TARGET EM_AARCH64
5 #define R_DATA_32 R_AARCH64_ABS32
6 #define R_DATA_PTR R_AARCH64_ABS64
7 #define R_JMP_SLOT R_AARCH64_JUMP_SLOT
8 #define R_GLOB_DAT R_AARCH64_GLOB_DAT
9 #define R_COPY R_AARCH64_COPY
10 #define R_RELATIVE R_AARCH64_RELATIVE
12 #define R_NUM R_AARCH64_NUM
14 #define ELF_START_ADDR 0x00400000
15 #define ELF_PAGE_SIZE 0x10000
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
)
31 case R_AARCH64_PREL32
:
32 case R_AARCH64_MOVW_UABS_G0_NC
:
33 case R_AARCH64_MOVW_UABS_G1_NC
:
34 case R_AARCH64_MOVW_UABS_G2_NC
:
35 case R_AARCH64_MOVW_UABS_G3
:
36 case R_AARCH64_ADR_PREL_PG_HI21
:
37 case R_AARCH64_ADD_ABS_LO12_NC
:
38 case R_AARCH64_ADR_GOT_PAGE
:
39 case R_AARCH64_LD64_GOT_LO12_NC
:
40 case R_AARCH64_LDST128_ABS_LO12_NC
:
41 case R_AARCH64_LDST64_ABS_LO12_NC
:
42 case R_AARCH64_LDST32_ABS_LO12_NC
:
43 case R_AARCH64_LDST16_ABS_LO12_NC
:
44 case R_AARCH64_LDST8_ABS_LO12_NC
:
45 case R_AARCH64_GLOB_DAT
:
49 case R_AARCH64_JUMP26
:
50 case R_AARCH64_CALL26
:
51 case R_AARCH64_JUMP_SLOT
:
57 /* Returns an enumerator to describe whether and when the relocation needs a
58 GOT and/or PLT entry to be created. See tcc.h for a description of the
60 int gotplt_entry_type (int reloc_type
)
63 case R_AARCH64_PREL32
:
64 case R_AARCH64_MOVW_UABS_G0_NC
:
65 case R_AARCH64_MOVW_UABS_G1_NC
:
66 case R_AARCH64_MOVW_UABS_G2_NC
:
67 case R_AARCH64_MOVW_UABS_G3
:
68 case R_AARCH64_ADR_PREL_PG_HI21
:
69 case R_AARCH64_ADD_ABS_LO12_NC
:
70 case R_AARCH64_LDST128_ABS_LO12_NC
:
71 case R_AARCH64_LDST64_ABS_LO12_NC
:
72 case R_AARCH64_LDST32_ABS_LO12_NC
:
73 case R_AARCH64_LDST16_ABS_LO12_NC
:
74 case R_AARCH64_LDST8_ABS_LO12_NC
:
75 case R_AARCH64_GLOB_DAT
:
76 case R_AARCH64_JUMP_SLOT
:
78 return NO_GOTPLT_ENTRY
;
82 case R_AARCH64_JUMP26
:
83 case R_AARCH64_CALL26
:
84 return AUTO_GOTPLT_ENTRY
;
86 case R_AARCH64_ADR_GOT_PAGE
:
87 case R_AARCH64_LD64_GOT_LO12_NC
:
88 return ALWAYS_GOTPLT_ENTRY
;
93 __attribute__((unused
))
94 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
96 Section
*plt
= s1
->plt
;
100 if (plt
->data_offset
== 0) {
101 section_ptr_add(plt
, 32);
103 plt_offset
= plt
->data_offset
;
105 p
= section_ptr_add(plt
, 16);
106 write32le(p
, got_offset
);
107 write32le(p
+ 4, (uint64_t) got_offset
>> 32);
111 /* relocate the PLT: compute addresses and offsets in the PLT now that final
112 address for PLT and GOT are known (see fill_program_header) */
113 __attribute__((unused
))
114 ST_FUNC
void relocate_plt(TCCState
*s1
)
122 p_end
= p
+ s1
->plt
->data_offset
;
125 uint64_t plt
= s1
->plt
->sh_addr
;
126 uint64_t got
= s1
->got
->sh_addr
+ 16;
127 uint64_t off
= (got
>> 12) - (plt
>> 12);
128 if ((off
+ ((uint32_t)1 << 20)) >> 21)
129 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", (long)off
, (long)got
, (long)plt
);
130 write32le(p
, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]!
131 write32le(p
+ 4, (0x90000010 | // adrp x16,...
132 (off
& 0x1ffffc) << 3 | (off
& 3) << 29));
133 write32le(p
+ 8, (0xf9400211 | // ldr x17,[x16,#...]
134 (got
& 0xff8) << 7));
135 write32le(p
+ 12, (0x91000210 | // add x16,x16,#...
136 (got
& 0xfff) << 10));
137 write32le(p
+ 16, 0xd61f0220); // br x17
138 write32le(p
+ 20, 0xd503201f); // nop
139 write32le(p
+ 24, 0xd503201f); // nop
140 write32le(p
+ 28, 0xd503201f); // nop
142 got
= s1
->got
->sh_addr
;
144 uint64_t pc
= plt
+ (p
- s1
->plt
->data
);
145 uint64_t addr
= got
+ read64le(p
);
146 uint64_t off
= (addr
>> 12) - (pc
>> 12);
147 if ((off
+ ((uint32_t)1 << 20)) >> 21)
148 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", (long)off
, (long)addr
, (long)pc
);
149 write32le(p
, (0x90000010 | // adrp x16,...
150 (off
& 0x1ffffc) << 3 | (off
& 3) << 29));
151 write32le(p
+ 4, (0xf9400211 | // ldr x17,[x16,#...]
152 (addr
& 0xff8) << 7));
153 write32le(p
+ 8, (0x91000210 | // add x16,x16,#...
154 (addr
& 0xfff) << 10));
155 write32le(p
+ 12, 0xd61f0220); // br x17
160 if (s1
->plt
->reloc
) {
163 for_each_elem(s1
->plt
->reloc
, 0, rel
, ElfW_Rel
) {
164 write64le(p
+ rel
->r_offset
, s1
->plt
->sh_addr
);
169 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
171 int sym_index
= ELFW(R_SYM
)(rel
->r_info
), esym_index
;
173 ElfW(Sym
) *sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
177 case R_AARCH64_ABS64
:
178 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
179 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
180 qrel
->r_offset
= rel
->r_offset
;
182 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_AARCH64_ABS64
);
183 qrel
->r_addend
= rel
->r_addend
;
187 qrel
->r_info
= ELFW(R_INFO
)(0, R_AARCH64_RELATIVE
);
188 qrel
->r_addend
= read64le(ptr
) + val
;
194 case R_AARCH64_ABS32
:
195 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
196 /* XXX: this logic may depend on TCC's codegen
197 now TCC uses R_AARCH64_RELATIVE even for a 64bit pointer */
198 qrel
->r_offset
= rel
->r_offset
;
199 qrel
->r_info
= ELFW(R_INFO
)(0, R_AARCH64_RELATIVE
);
200 /* Use sign extension! */
201 qrel
->r_addend
= (int)read32le(ptr
) + val
;
206 case R_AARCH64_PREL32
:
207 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
209 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
211 qrel
->r_offset
= rel
->r_offset
;
212 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_AARCH64_PREL32
);
213 /* Use sign extension! */
214 qrel
->r_addend
= (int)read32le(ptr
) + rel
->r_addend
;
219 write32le(ptr
, val
- addr
);
221 case R_AARCH64_MOVW_UABS_G0_NC
:
222 write32le(ptr
, ((read32le(ptr
) & 0xffe0001f) |
223 (val
& 0xffff) << 5));
225 case R_AARCH64_MOVW_UABS_G1_NC
:
226 write32le(ptr
, ((read32le(ptr
) & 0xffe0001f) |
227 (val
>> 16 & 0xffff) << 5));
229 case R_AARCH64_MOVW_UABS_G2_NC
:
230 write32le(ptr
, ((read32le(ptr
) & 0xffe0001f) |
231 (val
>> 32 & 0xffff) << 5));
233 case R_AARCH64_MOVW_UABS_G3
:
234 write32le(ptr
, ((read32le(ptr
) & 0xffe0001f) |
235 (val
>> 48 & 0xffff) << 5));
237 case R_AARCH64_ADR_PREL_PG_HI21
: {
238 uint64_t off
= (val
>> 12) - (addr
>> 12);
239 if ((off
+ ((uint64_t)1 << 20)) >> 21)
240 tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
241 write32le(ptr
, ((read32le(ptr
) & 0x9f00001f) |
242 (off
& 0x1ffffc) << 3 | (off
& 3) << 29));
245 case R_AARCH64_ADD_ABS_LO12_NC
:
246 case R_AARCH64_LDST8_ABS_LO12_NC
:
247 write32le(ptr
, ((read32le(ptr
) & 0xffc003ff) |
248 (val
& 0xfff) << 10));
250 case R_AARCH64_LDST16_ABS_LO12_NC
:
251 write32le(ptr
, ((read32le(ptr
) & 0xffc003ff) |
252 (val
& 0xffe) << 9));
254 case R_AARCH64_LDST32_ABS_LO12_NC
:
255 write32le(ptr
, ((read32le(ptr
) & 0xffc003ff) |
256 (val
& 0xffc) << 8));
258 case R_AARCH64_LDST64_ABS_LO12_NC
:
259 write32le(ptr
, ((read32le(ptr
) & 0xffc003ff) |
260 (val
& 0xff8) << 7));
262 case R_AARCH64_LDST128_ABS_LO12_NC
:
263 write32le(ptr
, ((read32le(ptr
) & 0xffc003ff) |
264 (val
& 0xff0) << 6));
266 case R_AARCH64_JUMP26
:
267 case R_AARCH64_CALL26
:
269 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type
, addr
, val
,
270 (char *) symtab_section
->link
->data
+ sym
->st_name
);
272 if (((val
- addr
) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
273 tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed"
274 " (val=%lx, addr=%lx)", (long)val
, (long)addr
);
275 write32le(ptr
, (0x14000000 |
276 (uint32_t)(type
== R_AARCH64_CALL26
) << 31 |
277 ((val
- addr
) >> 2 & 0x3ffffff)));
279 case R_AARCH64_ADR_GOT_PAGE
: {
281 (((s1
->got
->sh_addr
+
282 get_sym_attr(s1
, sym_index
, 0)->got_offset
) >> 12) - (addr
>> 12));
283 if ((off
+ ((uint64_t)1 << 20)) >> 21)
284 tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed");
285 write32le(ptr
, ((read32le(ptr
) & 0x9f00001f) |
286 (off
& 0x1ffffc) << 3 | (off
& 3) << 29));
289 case R_AARCH64_LD64_GOT_LO12_NC
:
291 ((read32le(ptr
) & 0xfff803ff) |
293 get_sym_attr(s1
, sym_index
, 0)->got_offset
) & 0xff8) << 7));
297 case R_AARCH64_GLOB_DAT
:
298 case R_AARCH64_JUMP_SLOT
:
299 /* They don't need addend */
301 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type
, addr
,
303 (char *) symtab_section
->link
->data
+ sym
->st_name
);
305 write64le(ptr
, val
- rel
->r_addend
);
307 case R_AARCH64_RELATIVE
:
309 add32le(ptr
, val
- s1
->pe_imagebase
);
314 fprintf(stderr
, "FIXME: handle reloc type %x at %x [%p] to %x\n",
315 type
, (unsigned)addr
, ptr
, (unsigned)val
);
320 #endif /* !TARGET_DEFS_ONLY */