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
:
53 case R_RISCV_CALL_PLT
:
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
)
67 case R_RISCV_RVC_BRANCH
:
68 case R_RISCV_RVC_JUMP
:
69 case R_RISCV_JUMP_SLOT
:
74 return NO_GOTPLT_ENTRY
;
78 case R_RISCV_PCREL_HI20
:
79 case R_RISCV_PCREL_LO12_I
:
80 case R_RISCV_PCREL_LO12_S
:
81 case R_RISCV_32_PCREL
:
89 case R_RISCV_CALL_PLT
:
90 return AUTO_GOTPLT_ENTRY
;
92 case R_RISCV_GOT_HI20
:
93 return ALWAYS_GOTPLT_ENTRY
;
98 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
100 Section
*plt
= s1
->plt
;
104 if (plt
->data_offset
== 0)
105 section_ptr_add(plt
, 32);
106 plt_offset
= plt
->data_offset
;
108 p
= section_ptr_add(plt
, 16);
109 write64le(p
, got_offset
);
113 /* relocate the PLT: compute addresses and offsets in the PLT now that final
114 address for PLT and GOT are known (see fill_program_header) */
115 ST_FUNC
void relocate_plt(TCCState
*s1
)
123 p_end
= p
+ s1
->plt
->data_offset
;
126 uint64_t plt
= s1
->plt
->sh_addr
;
127 uint64_t got
= s1
->got
->sh_addr
;
128 uint64_t off
= (got
- plt
+ 0x800) >> 12;
129 if ((off
+ ((uint32_t)1 << 20)) >> 21)
130 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", off
, got
, plt
);
131 write32le(p
, 0x397 | (off
<< 12)); // auipc t2, %pcrel_hi(got)
132 write32le(p
+ 4, 0x41c30333); // sub t1, t1, t3
133 write32le(p
+ 8, 0x0003be03 // ld t3, %pcrel_lo(got)(t2)
134 | (((got
- plt
) & 0xfff) << 20));
135 write32le(p
+ 12, 0xfd430313); // addi t1, t1, -(32+12)
136 write32le(p
+ 16, 0x00038293 // addi t0, t2, %pcrel_lo(got)
137 | (((got
- plt
) & 0xfff) << 20));
138 write32le(p
+ 20, 0x00135313); // srli t1, t1, log2(16/PTRSIZE)
139 write32le(p
+ 24, 0x0082b283); // ld t0, PTRSIZE(t0)
140 write32le(p
+ 28, 0x000e0067); // jr t3
143 uint64_t pc
= plt
+ (p
- s1
->plt
->data
);
144 uint64_t addr
= got
+ read64le(p
);
145 uint64_t off
= (addr
- pc
+ 0x800) >> 12;
146 if ((off
+ ((uint32_t)1 << 20)) >> 21)
147 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off
, addr
, pc
);
148 write32le(p
, 0xe17 | (off
<< 12)); // auipc t3, %pcrel_hi(func@got)
149 write32le(p
+ 4, 0x000e3e03 // ld t3, %pcrel_lo(func@got)(t3)
150 | (((addr
- pc
) & 0xfff) << 20));
151 write32le(p
+ 8, 0x000e0367); // jalr t1, t3
152 write32le(p
+ 12, 0x00000013); // nop
161 static struct pcrel_hi last_hi
;
163 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
,
164 addr_t addr
, addr_t val
)
168 int sym_index
= ELFW(R_SYM
)(rel
->r_info
), esym_index
;
169 ElfW(Sym
) *sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
178 if ((off64
+ (1 << 12)) & ~(uint64_t)0x1ffe)
179 tcc_error("R_RISCV_BRANCH relocation failed"
180 " (val=%lx, addr=%lx)", val
, addr
);
182 write32le(ptr
, (read32le(ptr
) & ~0xfe000f80)
183 | ((off32
& 0x800) << 20)
184 | ((off32
& 0x3f0) << 21)
185 | ((off32
& 0x00f) << 8)
186 | ((off32
& 0x400) >> 3));
190 if ((off64
+ (1 << 21)) & ~(((uint64_t)1 << 22) - 2))
191 tcc_error("R_RISCV_JAL relocation failed"
192 " (val=%lx, addr=%lx)", val
, addr
);
194 write32le(ptr
, (read32le(ptr
) & 0xfff)
195 | (((off32
>> 12) & 0xff) << 12)
196 | (((off32
>> 11) & 1) << 20)
197 | (((off32
>> 1) & 0x3ff) << 21)
198 | (((off32
>> 20) & 1) << 31));
201 case R_RISCV_CALL_PLT
:
202 write32le(ptr
, (read32le(ptr
) & 0xfff)
203 | ((val
- addr
+ 0x800) & ~0xfff));
204 write32le(ptr
+ 4, (read32le(ptr
+ 4) & 0xfffff)
205 | (((val
- addr
) & 0xfff) << 20));
207 case R_RISCV_PCREL_HI20
:
209 printf("PCREL_HI20: val=%lx addr=%lx\n", val
, addr
);
211 off64
= (int64_t)(val
- addr
+ 0x800) >> 12;
212 if ((off64
+ ((uint64_t)1 << 20)) >> 21)
213 tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx sym=%s",
214 off64
, ((int64_t)(off64
+ ((uint64_t)1 << 20)) >> 21),
215 symtab_section
->link
->data
+ sym
->st_name
);
216 write32le(ptr
, (read32le(ptr
) & 0xfff)
217 | ((off64
& 0xfffff) << 12));
221 case R_RISCV_GOT_HI20
:
222 val
= s1
->got
->sh_addr
+ get_sym_attr(s1
, sym_index
, 0)->got_offset
;
223 off64
= (int64_t)(val
- addr
+ 0x800) >> 12;
224 if ((off64
+ ((uint64_t)1 << 20)) >> 21)
225 tcc_error("R_RISCV_GOT_HI20 relocation failed");
228 write32le(ptr
, (read32le(ptr
) & 0xfff)
229 | ((off64
& 0xfffff) << 12));
231 case R_RISCV_PCREL_LO12_I
:
233 printf("PCREL_LO12_I: val=%lx addr=%lx\n", val
, addr
);
235 if (val
!= last_hi
.addr
)
236 tcc_error("unsupported hi/lo pcrel reloc scheme");
239 write32le(ptr
, (read32le(ptr
) & 0xfffff)
240 | (((val
- addr
) & 0xfff) << 20));
242 case R_RISCV_PCREL_LO12_S
:
243 if (val
!= last_hi
.addr
)
244 tcc_error("unsupported hi/lo pcrel reloc scheme");
248 write32le(ptr
, (read32le(ptr
) & ~0xfe000f80)
249 | ((off32
& 0xfe0) << 20)
250 | ((off32
& 0x01f) << 7));
253 case R_RISCV_RVC_BRANCH
:
254 off64
= (val
- addr
);
255 if ((off64
+ (1 << 8)) & ~(uint64_t)0x1fe)
256 tcc_error("R_RISCV_RVC_BRANCH relocation failed"
257 " (val=%lx, addr=%lx)", val
, addr
);
259 write16le(ptr
, (read16le(ptr
) & 0xe383)
260 | (((off32
>> 5) & 1) << 2)
261 | (((off32
>> 1) & 3) << 3)
262 | (((off32
>> 6) & 3) << 5)
263 | (((off32
>> 3) & 3) << 10)
264 | (((off32
>> 8) & 1) << 12));
266 case R_RISCV_RVC_JUMP
:
267 off64
= (val
- addr
);
268 if ((off64
+ (1 << 11)) & ~(uint64_t)0xffe)
269 tcc_error("R_RISCV_RVC_BRANCH relocation failed"
270 " (val=%lx, addr=%lx)", val
, addr
);
272 write16le(ptr
, (read16le(ptr
) & 0xe003)
273 | (((off32
>> 5) & 1) << 2)
274 | (((off32
>> 1) & 7) << 3)
275 | (((off32
>> 7) & 1) << 6)
276 | (((off32
>> 6) & 1) << 7)
277 | (((off32
>> 10) & 1) << 8)
278 | (((off32
>> 8) & 3) << 9)
279 | (((off32
>> 4) & 1) << 11)
280 | (((off32
>> 11) & 1) << 12));
284 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
285 /* XXX: this logic may depend on TCC's codegen
286 now TCC uses R_RISCV_RELATIVE even for a 64bit pointer */
287 qrel
->r_offset
= rel
->r_offset
;
288 qrel
->r_info
= ELFW(R_INFO
)(0, R_RISCV_RELATIVE
);
289 /* Use sign extension! */
290 qrel
->r_addend
= (int)read32le(ptr
) + val
;
296 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
297 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
298 qrel
->r_offset
= rel
->r_offset
;
300 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_RISCV_64
);
301 qrel
->r_addend
= rel
->r_addend
;
305 qrel
->r_info
= ELFW(R_INFO
)(0, R_RISCV_RELATIVE
);
306 qrel
->r_addend
= read64le(ptr
) + val
;
310 case R_RISCV_JUMP_SLOT
:
314 write64le(ptr
, read64le(ptr
) + val
);
317 write32le(ptr
, read32le(ptr
) + val
);
320 write64le(ptr
, read64le(ptr
) - val
);
323 write32le(ptr
, read32le(ptr
) - val
);
326 write16le(ptr
, read16le(ptr
) + val
);
329 write16le(ptr
, read16le(ptr
) - val
);
332 *ptr
= (*ptr
& ~0x3f) | (val
& 0x3f);
335 *ptr
= (*ptr
& ~0x3f) | ((*ptr
- val
) & 0x3f);
338 case R_RISCV_32_PCREL
:
344 fprintf(stderr
, "FIXME: handle reloc type %x at %x [%p] to %x\n",
345 type
, (unsigned)addr
, ptr
, (unsigned)val
);