riscv: Fix 73_arm.c
[tinycc.git] / riscv64-link.c
blob6e850aefa9678916d14482f56586fbc4889fb3dc
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_SUB6:
43 case R_RISCV_ADD16:
44 case R_RISCV_ADD32:
45 case R_RISCV_ADD64:
46 case R_RISCV_SUB16:
47 case R_RISCV_SUB32:
48 case R_RISCV_SUB64:
49 case R_RISCV_32:
50 case R_RISCV_64:
51 return 0;
53 case R_RISCV_CALL_PLT:
54 return 1;
56 tcc_error ("Unknown relocation type in code_reloc: %d", reloc_type);
57 return -1;
60 /* Returns an enumerator to describe whether and when the relocation needs a
61 GOT and/or PLT entry to be created. See tcc.h for a description of the
62 different values. */
63 int gotplt_entry_type (int reloc_type)
65 switch (reloc_type) {
66 case R_RISCV_ALIGN:
67 case R_RISCV_RELAX:
68 case R_RISCV_RVC_BRANCH:
69 case R_RISCV_RVC_JUMP:
70 case R_RISCV_JUMP_SLOT:
71 case R_RISCV_SET6:
72 case R_RISCV_SUB6:
73 case R_RISCV_ADD16:
74 case R_RISCV_SUB16:
75 return NO_GOTPLT_ENTRY;
77 case R_RISCV_BRANCH:
78 case R_RISCV_CALL:
79 case R_RISCV_PCREL_HI20:
80 case R_RISCV_PCREL_LO12_I:
81 case R_RISCV_PCREL_LO12_S:
82 case R_RISCV_32_PCREL:
83 case R_RISCV_ADD32:
84 case R_RISCV_ADD64:
85 case R_RISCV_SUB32:
86 case R_RISCV_SUB64:
87 case R_RISCV_32:
88 case R_RISCV_64:
89 case R_RISCV_JAL:
90 return AUTO_GOTPLT_ENTRY;
92 case R_RISCV_GOT_HI20:
93 case R_RISCV_CALL_PLT:
94 return ALWAYS_GOTPLT_ENTRY;
97 tcc_error ("Unknown relocation type: %d", reloc_type);
98 return -1;
101 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
103 Section *plt = s1->plt;
104 uint8_t *p;
105 unsigned plt_offset;
107 if (s1->output_type == TCC_OUTPUT_DLL)
108 tcc_error("DLLs unimplemented!");
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("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", off, got, 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("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off, addr, 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;
164 void relocate_init(Section *sr) {}
166 void relocate_fini(Section *sr)
170 struct pcrel_hi {
171 addr_t addr, val;
173 static struct pcrel_hi last_hi;
175 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
176 addr_t addr, addr_t val)
178 uint64_t off64;
179 uint32_t off32;
180 int sym_index = ELFW(R_SYM)(rel->r_info);
181 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
183 switch(type) {
184 case R_RISCV_ALIGN:
185 case R_RISCV_RELAX:
186 return;
188 case R_RISCV_BRANCH:
189 off64 = val - addr;
190 if ((off64 + (1 << 12)) & ~(uint64_t)0x1ffe)
191 tcc_error("R_RISCV_BRANCH relocation failed"
192 " (val=%lx, addr=%lx)", val, addr);
193 off32 = off64 >> 1;
194 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
195 | ((off32 & 0x800) << 20)
196 | ((off32 & 0x3f0) << 21)
197 | ((off32 & 0x00f) << 8)
198 | ((off32 & 0x400) >> 3));
199 return;
200 case R_RISCV_JAL:
201 off64 = val - addr;
202 if ((off64 + (1 << 21)) & ~(((uint64_t)1 << 22) - 2))
203 tcc_error("R_RISCV_JAL relocation failed"
204 " (val=%lx, addr=%lx)", val, addr);
205 off32 = off64;
206 write32le(ptr, (read32le(ptr) & 0xfff)
207 | (((off32 >> 12) & 0xff) << 12)
208 | (((off32 >> 11) & 1) << 20)
209 | (((off32 >> 1) & 0x3ff) << 21)
210 | (((off32 >> 20) & 1) << 31));
211 return;
212 case R_RISCV_CALL:
213 case R_RISCV_CALL_PLT:
214 write32le(ptr, (read32le(ptr) & 0xfff)
215 | ((val - addr + 0x800) & ~0xfff));
216 write32le(ptr + 4, (read32le(ptr + 4) & 0xfffff)
217 | (((val - addr) & 0xfff) << 20));
218 return;
219 case R_RISCV_PCREL_HI20:
220 #ifdef DEBUG_RELOC
221 printf("PCREL_HI20: val=%lx addr=%lx\n", val, addr);
222 #endif
223 off64 = (int64_t)(val - addr + 0x800) >> 12;
224 if ((off64 + ((uint64_t)1 << 20)) >> 21)
225 tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx sym=%s",
226 off64, ((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21),
227 symtab_section->link->data + sym->st_name);
228 write32le(ptr, (read32le(ptr) & 0xfff)
229 | ((off64 & 0xfffff) << 12));
230 last_hi.addr = addr;
231 last_hi.val = val;
232 return;
233 case R_RISCV_GOT_HI20:
234 val = s1->got->sh_addr + get_sym_attr(s1, sym_index, 0)->got_offset;
235 off64 = (int64_t)(val - addr + 0x800) >> 12;
236 if ((off64 + ((uint64_t)1 << 20)) >> 21)
237 tcc_error("R_RISCV_GOT_HI20 relocation failed");
238 last_hi.addr = addr;
239 last_hi.val = val;
240 write32le(ptr, (read32le(ptr) & 0xfff)
241 | ((off64 & 0xfffff) << 12));
242 return;
243 case R_RISCV_PCREL_LO12_I:
244 #ifdef DEBUG_RELOC
245 printf("PCREL_LO12_I: val=%lx addr=%lx\n", val, addr);
246 #endif
247 if (val != last_hi.addr)
248 tcc_error("unsupported hi/lo pcrel reloc scheme");
249 val = last_hi.val;
250 addr = last_hi.addr;
251 write32le(ptr, (read32le(ptr) & 0xfffff)
252 | (((val - addr) & 0xfff) << 20));
253 return;
254 case R_RISCV_PCREL_LO12_S:
255 if (val != last_hi.addr)
256 tcc_error("unsupported hi/lo pcrel reloc scheme");
257 val = last_hi.val;
258 addr = last_hi.addr;
259 off32 = val - addr;
260 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
261 | ((off32 & 0xfe0) << 20)
262 | ((off32 & 0x01f) << 7));
263 return;
265 case R_RISCV_RVC_BRANCH:
266 off64 = (val - addr);
267 if ((off64 + (1 << 8)) & ~(uint64_t)0x1fe)
268 tcc_error("R_RISCV_RVC_BRANCH relocation failed"
269 " (val=%lx, addr=%lx)", val, addr);
270 off32 = off64;
271 write16le(ptr, (read16le(ptr) & 0xe383)
272 | (((off32 >> 5) & 1) << 2)
273 | (((off32 >> 1) & 3) << 3)
274 | (((off32 >> 6) & 3) << 5)
275 | (((off32 >> 3) & 3) << 10)
276 | (((off32 >> 8) & 1) << 12));
277 return;
278 case R_RISCV_RVC_JUMP:
279 off64 = (val - addr);
280 if ((off64 + (1 << 11)) & ~(uint64_t)0xffe)
281 tcc_error("R_RISCV_RVC_BRANCH relocation failed"
282 " (val=%lx, addr=%lx)", val, addr);
283 off32 = off64;
284 write16le(ptr, (read16le(ptr) & 0xe003)
285 | (((off32 >> 5) & 1) << 2)
286 | (((off32 >> 1) & 7) << 3)
287 | (((off32 >> 7) & 1) << 6)
288 | (((off32 >> 6) & 1) << 7)
289 | (((off32 >> 10) & 1) << 8)
290 | (((off32 >> 8) & 3) << 9)
291 | (((off32 >> 4) & 1) << 11)
292 | (((off32 >> 11) & 1) << 12));
293 return;
295 case R_RISCV_32:
296 write32le(ptr, val);
297 return;
298 case R_RISCV_JUMP_SLOT:
299 case R_RISCV_64:
300 write64le(ptr, val);
301 return;
302 case R_RISCV_ADD64:
303 write64le(ptr, read64le(ptr) + val);
304 return;
305 case R_RISCV_ADD32:
306 write32le(ptr, read32le(ptr) + val);
307 return;
308 case R_RISCV_SUB64:
309 write64le(ptr, read64le(ptr) - val);
310 return;
311 case R_RISCV_SUB32:
312 write32le(ptr, read32le(ptr) - val);
313 return;
314 case R_RISCV_ADD16:
315 write16le(ptr, read16le(ptr) + val);
316 return;
317 case R_RISCV_SUB16:
318 write16le(ptr, read16le(ptr) - val);
319 return;
320 case R_RISCV_SET6:
321 *ptr = (*ptr & ~0x3f) | (val & 0x3f);
322 return;
323 case R_RISCV_SUB6:
324 *ptr = (*ptr & ~0x3f) | ((*ptr - val) & 0x3f);
325 return;
327 case R_RISCV_32_PCREL:
328 case R_RISCV_COPY:
329 /* XXX */
330 return;
332 default:
333 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
334 type, (unsigned)addr, ptr, (unsigned)val);
335 return;
338 #endif