riscv: fix jal: fix reloc and parsing
[tinycc.git] / riscv64-link.c
blob39c0c5d201f6127578d4afd9c1be7b95f2feeca2
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 */
22 //#define DEBUG_RELOC
23 #include "tcc.h"
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)
29 switch (reloc_type) {
31 case R_RISCV_BRANCH:
32 case R_RISCV_CALL:
33 case R_RISCV_JAL:
34 return 1;
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:
41 case R_RISCV_SET6:
42 case R_RISCV_SET8:
43 case R_RISCV_SET16:
44 case R_RISCV_SUB6:
45 case R_RISCV_ADD16:
46 case R_RISCV_ADD32:
47 case R_RISCV_ADD64:
48 case R_RISCV_SUB8:
49 case R_RISCV_SUB16:
50 case R_RISCV_SUB32:
51 case R_RISCV_SUB64:
52 case R_RISCV_32:
53 case R_RISCV_64:
54 return 0;
56 case R_RISCV_CALL_PLT:
57 return 1;
59 return -1;
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
64 different values. */
65 int gotplt_entry_type (int reloc_type)
67 switch (reloc_type) {
68 case R_RISCV_ALIGN:
69 case R_RISCV_RELAX:
70 case R_RISCV_RVC_BRANCH:
71 case R_RISCV_RVC_JUMP:
72 case R_RISCV_JUMP_SLOT:
73 case R_RISCV_SET6:
74 case R_RISCV_SET8:
75 case R_RISCV_SET16:
76 case R_RISCV_SUB6:
77 case R_RISCV_ADD16:
78 case R_RISCV_SUB8:
79 case R_RISCV_SUB16:
80 return NO_GOTPLT_ENTRY;
82 case R_RISCV_BRANCH:
83 case R_RISCV_CALL:
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:
88 case R_RISCV_ADD32:
89 case R_RISCV_ADD64:
90 case R_RISCV_SUB32:
91 case R_RISCV_SUB64:
92 case R_RISCV_32:
93 case R_RISCV_64:
94 case R_RISCV_JAL:
95 case R_RISCV_CALL_PLT:
96 return AUTO_GOTPLT_ENTRY;
98 case R_RISCV_GOT_HI20:
99 return ALWAYS_GOTPLT_ENTRY;
101 return -1;
104 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
106 Section *plt = s1->plt;
107 uint8_t *p;
108 unsigned plt_offset;
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);
116 return plt_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)
123 uint8_t *p, *p_end;
125 if (!s1->plt)
126 return;
128 p = s1->plt->data;
129 p_end = p + s1->plt->data_offset;
131 if (p < p_end) {
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
147 p += 32;
148 while (p < p_end) {
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
159 p += 16;
163 if (s1->plt->reloc) {
164 ElfW_Rel *rel;
165 p = s1->got->data;
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)
175 uint64_t off64;
176 uint32_t off32;
177 int sym_index = ELFW(R_SYM)(rel->r_info), esym_index;
178 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
180 switch(type) {
181 case R_RISCV_ALIGN:
182 case R_RISCV_RELAX:
183 return;
185 case R_RISCV_BRANCH:
186 off64 = val - addr;
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);
190 off32 = off64 >> 1;
191 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
192 | ((off32 & 0x800) << 20)
193 | ((off32 & 0x3f0) << 21)
194 | ((off32 & 0x00f) << 8)
195 | ((off32 & 0x400) >> 3));
196 return;
197 case R_RISCV_JAL:
198 off64 = val - addr;
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);
202 off32 = off64;
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));
208 return;
209 case R_RISCV_CALL:
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));
215 return;
216 case R_RISCV_PCREL_HI20:
217 #ifdef DEBUG_RELOC
218 printf("PCREL_HI20: val=%lx addr=%lx\n", (long)val, (long)addr);
219 #endif
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));
227 last_hi.addr = addr;
228 last_hi.val = val;
229 return;
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");
235 last_hi.addr = addr;
236 last_hi.val = val;
237 write32le(ptr, (read32le(ptr) & 0xfff)
238 | ((off64 & 0xfffff) << 12));
239 return;
240 case R_RISCV_PCREL_LO12_I:
241 #ifdef DEBUG_RELOC
242 printf("PCREL_LO12_I: val=%lx addr=%lx\n", (long)val, (long)addr);
243 #endif
244 if (val != last_hi.addr)
245 tcc_error_noabort("unsupported hi/lo pcrel reloc scheme");
246 val = last_hi.val;
247 addr = last_hi.addr;
248 write32le(ptr, (read32le(ptr) & 0xfffff)
249 | (((val - addr) & 0xfff) << 20));
250 return;
251 case R_RISCV_PCREL_LO12_S:
252 if (val != last_hi.addr)
253 tcc_error_noabort("unsupported hi/lo pcrel reloc scheme");
254 val = last_hi.val;
255 addr = last_hi.addr;
256 off32 = val - addr;
257 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
258 | ((off32 & 0xfe0) << 20)
259 | ((off32 & 0x01f) << 7));
260 return;
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);
267 off32 = off64;
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));
274 return;
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);
280 off32 = off64;
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));
290 return;
292 case R_RISCV_32:
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;
300 qrel++;
302 add32le(ptr, val);
303 return;
304 case R_RISCV_64:
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;
308 if (esym_index) {
309 qrel->r_info = ELFW(R_INFO)(esym_index, R_RISCV_64);
310 qrel->r_addend = rel->r_addend;
311 qrel++;
312 break;
313 } else {
314 qrel->r_info = ELFW(R_INFO)(0, R_RISCV_RELATIVE);
315 qrel->r_addend = read64le(ptr) + val;
316 qrel++;
319 case R_RISCV_JUMP_SLOT:
320 add64le(ptr, val);
321 return;
322 case R_RISCV_ADD64:
323 write64le(ptr, read64le(ptr) + val);
324 return;
325 case R_RISCV_ADD32:
326 write32le(ptr, read32le(ptr) + val);
327 return;
328 case R_RISCV_SUB64:
329 write64le(ptr, read64le(ptr) - val);
330 return;
331 case R_RISCV_SUB32:
332 write32le(ptr, read32le(ptr) - val);
333 return;
334 case R_RISCV_ADD16:
335 write16le(ptr, read16le(ptr) + val);
336 return;
337 case R_RISCV_SUB8:
338 *ptr -= val;
339 return;
340 case R_RISCV_SUB16:
341 write16le(ptr, read16le(ptr) - val);
342 return;
343 case R_RISCV_SET6:
344 *ptr = (*ptr & ~0x3f) | (val & 0x3f);
345 return;
346 case R_RISCV_SET8:
347 *ptr = (*ptr & ~0xff) | (val & 0xff);
348 return;
349 case R_RISCV_SET16:
350 *ptr = (*ptr & ~0xffff) | (val & 0xffff);
351 return;
352 case R_RISCV_SUB6:
353 *ptr = (*ptr & ~0x3f) | ((*ptr - val) & 0x3f);
354 return;
356 case R_RISCV_32_PCREL:
357 case R_RISCV_COPY:
358 /* XXX */
359 return;
361 default:
362 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
363 type, (unsigned)addr, ptr, (unsigned)val);
364 return;
367 #endif