1 #ifdef TARGET_DEFS_ONLY
3 #define EM_TCC_TARGET EM_RISCV
5 #define R_DATA_32 R_RISCV_32
6 #define R_DATA_PTR R_RISCV_64
7 #define R_JMP_SLOT R_RISCV_JUMP_SLOT
8 #define R_GLOB_DAT R_RISCV_64
9 #define R_COPY R_RISCV_COPY
10 #define R_RELATIVE R_RISCV_RELATIVE
12 #define R_NUM R_RISCV_NUM
14 #define ELF_START_ADDR 0x00010000
15 #define ELF_PAGE_SIZE 0x1000
17 #define PCRELATIVE_DLLPLT 1
18 #define RELOCATE_DLLPLT 1
20 #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
)
36 case R_RISCV_GOT_HI20
:
37 case R_RISCV_PCREL_HI20
:
38 case R_RISCV_PCREL_LO12_I
:
39 case R_RISCV_PCREL_LO12_S
:
40 case R_RISCV_32_PCREL
:
56 case R_RISCV_CALL_PLT
:
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
)
70 case R_RISCV_RVC_BRANCH
:
71 case R_RISCV_RVC_JUMP
:
72 case R_RISCV_JUMP_SLOT
:
80 return NO_GOTPLT_ENTRY
;
84 case R_RISCV_PCREL_HI20
:
85 case R_RISCV_PCREL_LO12_I
:
86 case R_RISCV_PCREL_LO12_S
:
87 case R_RISCV_32_PCREL
:
95 case R_RISCV_CALL_PLT
:
96 return AUTO_GOTPLT_ENTRY
;
98 case R_RISCV_GOT_HI20
:
99 return ALWAYS_GOTPLT_ENTRY
;
104 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
106 Section
*plt
= s1
->plt
;
110 if (plt
->data_offset
== 0)
111 section_ptr_add(plt
, 32);
112 plt_offset
= plt
->data_offset
;
114 p
= section_ptr_add(plt
, 16);
115 write64le(p
, got_offset
);
119 /* relocate the PLT: compute addresses and offsets in the PLT now that final
120 address for PLT and GOT are known (see fill_program_header) */
121 ST_FUNC
void relocate_plt(TCCState
*s1
)
129 p_end
= p
+ s1
->plt
->data_offset
;
132 uint64_t plt
= s1
->plt
->sh_addr
;
133 uint64_t got
= s1
->got
->sh_addr
;
134 uint64_t off
= (got
- plt
+ 0x800) >> 12;
135 if ((off
+ ((uint32_t)1 << 20)) >> 21)
136 tcc_error_noabort("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", (long)off
, (long)got
, (long)plt
);
137 write32le(p
, 0x397 | (off
<< 12)); // auipc t2, %pcrel_hi(got)
138 write32le(p
+ 4, 0x41c30333); // sub t1, t1, t3
139 write32le(p
+ 8, 0x0003be03 // ld t3, %pcrel_lo(got)(t2)
140 | (((got
- plt
) & 0xfff) << 20));
141 write32le(p
+ 12, 0xfd430313); // addi t1, t1, -(32+12)
142 write32le(p
+ 16, 0x00038293 // addi t0, t2, %pcrel_lo(got)
143 | (((got
- plt
) & 0xfff) << 20));
144 write32le(p
+ 20, 0x00135313); // srli t1, t1, log2(16/PTRSIZE)
145 write32le(p
+ 24, 0x0082b283); // ld t0, PTRSIZE(t0)
146 write32le(p
+ 28, 0x000e0067); // jr t3
149 uint64_t pc
= plt
+ (p
- s1
->plt
->data
);
150 uint64_t addr
= got
+ read64le(p
);
151 uint64_t off
= (addr
- pc
+ 0x800) >> 12;
152 if ((off
+ ((uint32_t)1 << 20)) >> 21)
153 tcc_error_noabort("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", (long)off
, (long)addr
, (long)pc
);
154 write32le(p
, 0xe17 | (off
<< 12)); // auipc t3, %pcrel_hi(func@got)
155 write32le(p
+ 4, 0x000e3e03 // ld t3, %pcrel_lo(func@got)(t3)
156 | (((addr
- pc
) & 0xfff) << 20));
157 write32le(p
+ 8, 0x000e0367); // jalr t1, t3
158 write32le(p
+ 12, 0x00000013); // nop
163 if (s1
->plt
->reloc
) {
166 for_each_elem(s1
->plt
->reloc
, 0, rel
, ElfW_Rel
) {
167 write64le(p
+ rel
->r_offset
, s1
->plt
->sh_addr
);
172 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
,
173 addr_t addr
, addr_t val
)
177 int sym_index
= ELFW(R_SYM
)(rel
->r_info
), esym_index
;
178 ElfW(Sym
) *sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
187 if ((off64
+ (1 << 12)) & ~(uint64_t)0x1ffe)
188 tcc_error_noabort("R_RISCV_BRANCH relocation failed"
189 " (val=%lx, addr=%lx)", (long)val
, (long)addr
);
191 write32le(ptr
, (read32le(ptr
) & ~0xfe000f80)
192 | ((off32
& 0x800) << 20)
193 | ((off32
& 0x3f0) << 21)
194 | ((off32
& 0x00f) << 8)
195 | ((off32
& 0x400) >> 3));
199 if ((off64
+ (1 << 21)) & ~(((uint64_t)1 << 22) - 2))
200 tcc_error_noabort("R_RISCV_JAL relocation failed"
201 " (val=%lx, addr=%lx)", (long)val
, (long)addr
);
203 write32le(ptr
, (read32le(ptr
) & 0xfff)
204 | (((off32
>> 12) & 0xff) << 12)
205 | (((off32
>> 11) & 1) << 20)
206 | (((off32
>> 1) & 0x3ff) << 21)
207 | (((off32
>> 20) & 1) << 31));
210 case R_RISCV_CALL_PLT
:
211 write32le(ptr
, (read32le(ptr
) & 0xfff)
212 | ((val
- addr
+ 0x800) & ~0xfff));
213 write32le(ptr
+ 4, (read32le(ptr
+ 4) & 0xfffff)
214 | (((val
- addr
) & 0xfff) << 20));
216 case R_RISCV_PCREL_HI20
:
218 printf("PCREL_HI20: val=%lx addr=%lx\n", (long)val
, (long)addr
);
220 off64
= (int64_t)(val
- addr
+ 0x800) >> 12;
221 if ((off64
+ ((uint64_t)1 << 20)) >> 21)
222 tcc_error_noabort("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx sym=%s",
223 (long)off64
, (long)((int64_t)(off64
+ ((uint64_t)1 << 20)) >> 21),
224 symtab_section
->link
->data
+ sym
->st_name
);
225 write32le(ptr
, (read32le(ptr
) & 0xfff)
226 | ((off64
& 0xfffff) << 12));
230 case R_RISCV_GOT_HI20
:
231 val
= s1
->got
->sh_addr
+ get_sym_attr(s1
, sym_index
, 0)->got_offset
;
232 off64
= (int64_t)(val
- addr
+ 0x800) >> 12;
233 if ((off64
+ ((uint64_t)1 << 20)) >> 21)
234 tcc_error_noabort("R_RISCV_GOT_HI20 relocation failed");
237 write32le(ptr
, (read32le(ptr
) & 0xfff)
238 | ((off64
& 0xfffff) << 12));
240 case R_RISCV_PCREL_LO12_I
:
242 printf("PCREL_LO12_I: val=%lx addr=%lx\n", (long)val
, (long)addr
);
244 if (val
!= last_hi
.addr
)
245 tcc_error_noabort("unsupported hi/lo pcrel reloc scheme");
248 write32le(ptr
, (read32le(ptr
) & 0xfffff)
249 | (((val
- addr
) & 0xfff) << 20));
251 case R_RISCV_PCREL_LO12_S
:
252 if (val
!= last_hi
.addr
)
253 tcc_error_noabort("unsupported hi/lo pcrel reloc scheme");
257 write32le(ptr
, (read32le(ptr
) & ~0xfe000f80)
258 | ((off32
& 0xfe0) << 20)
259 | ((off32
& 0x01f) << 7));
262 case R_RISCV_RVC_BRANCH
:
263 off64
= (val
- addr
);
264 if ((off64
+ (1 << 8)) & ~(uint64_t)0x1fe)
265 tcc_error_noabort("R_RISCV_RVC_BRANCH relocation failed"
266 " (val=%lx, addr=%lx)", (long)val
, (long)addr
);
268 write16le(ptr
, (read16le(ptr
) & 0xe383)
269 | (((off32
>> 5) & 1) << 2)
270 | (((off32
>> 1) & 3) << 3)
271 | (((off32
>> 6) & 3) << 5)
272 | (((off32
>> 3) & 3) << 10)
273 | (((off32
>> 8) & 1) << 12));
275 case R_RISCV_RVC_JUMP
:
276 off64
= (val
- addr
);
277 if ((off64
+ (1 << 11)) & ~(uint64_t)0xffe)
278 tcc_error_noabort("R_RISCV_RVC_BRANCH relocation failed"
279 " (val=%lx, addr=%lx)", (long)val
, (long)addr
);
281 write16le(ptr
, (read16le(ptr
) & 0xe003)
282 | (((off32
>> 5) & 1) << 2)
283 | (((off32
>> 1) & 7) << 3)
284 | (((off32
>> 7) & 1) << 6)
285 | (((off32
>> 6) & 1) << 7)
286 | (((off32
>> 10) & 1) << 8)
287 | (((off32
>> 8) & 3) << 9)
288 | (((off32
>> 4) & 1) << 11)
289 | (((off32
>> 11) & 1) << 12));
293 if (s1
->output_type
& TCC_OUTPUT_DYN
) {
294 /* XXX: this logic may depend on TCC's codegen
295 now TCC uses R_RISCV_RELATIVE even for a 64bit pointer */
296 qrel
->r_offset
= rel
->r_offset
;
297 qrel
->r_info
= ELFW(R_INFO
)(0, R_RISCV_RELATIVE
);
298 /* Use sign extension! */
299 qrel
->r_addend
= (int)read32le(ptr
) + val
;
305 if (s1
->output_type
& TCC_OUTPUT_DYN
) {
306 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
307 qrel
->r_offset
= rel
->r_offset
;
309 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_RISCV_64
);
310 qrel
->r_addend
= rel
->r_addend
;
314 qrel
->r_info
= ELFW(R_INFO
)(0, R_RISCV_RELATIVE
);
315 qrel
->r_addend
= read64le(ptr
) + val
;
319 case R_RISCV_JUMP_SLOT
:
323 write64le(ptr
, read64le(ptr
) + val
);
326 write32le(ptr
, read32le(ptr
) + val
);
329 write64le(ptr
, read64le(ptr
) - val
);
332 write32le(ptr
, read32le(ptr
) - val
);
335 write16le(ptr
, read16le(ptr
) + val
);
341 write16le(ptr
, read16le(ptr
) - val
);
344 *ptr
= (*ptr
& ~0x3f) | (val
& 0x3f);
347 *ptr
= (*ptr
& ~0xff) | (val
& 0xff);
350 *ptr
= (*ptr
& ~0xffff) | (val
& 0xffff);
353 *ptr
= (*ptr
& ~0x3f) | ((*ptr
- val
) & 0x3f);
356 case R_RISCV_32_PCREL
:
362 fprintf(stderr
, "FIXME: handle reloc type %x at %x [%p] to %x\n",
363 type
, (unsigned)addr
, ptr
, (unsigned)val
);