riscv: Handle some usual relocs
[tinycc.git] / riscv64-link.c
blob5265acb8adb6af04ae5a9bb51d37481b00f729a6
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 #include "tcc.h"
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)
28 switch (reloc_type) {
30 case R_RISCV_BRANCH:
31 case R_RISCV_CALL:
32 case R_RISCV_JAL:
33 return 1;
35 case R_RISCV_GOT_HI20:
36 case R_RISCV_PCREL_HI20:
37 case R_RISCV_PCREL_LO12_I:
38 case R_RISCV_PCREL_LO12_S:
39 case R_RISCV_32_PCREL:
40 case R_RISCV_ADD32:
41 case R_RISCV_SUB32:
42 case R_RISCV_32:
43 case R_RISCV_64:
44 return 0;
46 case R_RISCV_CALL_PLT:
47 return 1;
49 tcc_error ("Unknown relocation type in code_reloc: %d", reloc_type);
50 return -1;
53 /* Returns an enumerator to describe whether and when the relocation needs a
54 GOT and/or PLT entry to be created. See tcc.h for a description of the
55 different values. */
56 int gotplt_entry_type (int reloc_type)
58 switch (reloc_type) {
59 case R_RISCV_ALIGN:
60 case R_RISCV_RELAX:
61 case R_RISCV_RVC_BRANCH:
62 case R_RISCV_RVC_JUMP:
63 return NO_GOTPLT_ENTRY;
65 case R_RISCV_BRANCH:
66 case R_RISCV_CALL:
67 case R_RISCV_PCREL_HI20:
68 case R_RISCV_PCREL_LO12_I:
69 case R_RISCV_PCREL_LO12_S:
70 case R_RISCV_32_PCREL:
71 case R_RISCV_ADD32:
72 case R_RISCV_SUB32:
73 case R_RISCV_32:
74 case R_RISCV_64:
75 case R_RISCV_JAL:
76 return AUTO_GOTPLT_ENTRY;
78 case R_RISCV_GOT_HI20:
79 case R_RISCV_CALL_PLT:
80 return ALWAYS_GOTPLT_ENTRY;
83 tcc_error ("Unknown relocation type: %d", reloc_type);
84 return -1;
87 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
89 Section *plt = s1->plt;
90 uint8_t *p;
91 unsigned plt_offset;
93 if (s1->output_type == TCC_OUTPUT_DLL)
94 tcc_error("DLLs unimplemented!");
96 if (plt->data_offset == 0)
97 section_ptr_add(plt, 32);
98 plt_offset = plt->data_offset;
100 p = section_ptr_add(plt, 16);
101 write64le(p, got_offset);
102 return plt_offset;
105 /* relocate the PLT: compute addresses and offsets in the PLT now that final
106 address for PLT and GOT are known (see fill_program_header) */
107 ST_FUNC void relocate_plt(TCCState *s1)
109 uint8_t *p, *p_end;
111 if (!s1->plt)
112 return;
114 p = s1->plt->data;
115 p_end = p + s1->plt->data_offset;
117 if (p < p_end) {
118 uint64_t plt = s1->plt->sh_addr;
119 uint64_t got = s1->got->sh_addr;
120 uint64_t off = (got - plt + 0x800) >> 12;
121 if ((off + ((uint32_t)1 << 20)) >> 21)
122 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", off, got, plt);
123 write32le(p, 0x397 | (off << 12)); // auipc t2, %pcrel_hi(got)
124 write32le(p + 4, 0x41c30333); // sub t1, t1, t3
125 write32le(p + 8, 0x0003be03 // ld t3, %pcrel_lo(got)(t2)
126 | (((got - plt) & 0xfff) << 20));
127 write32le(p + 12, 0xfd430313); // addi t1, t1, -(32+12)
128 write32le(p + 16, 0x00038293 // addi t0, t2, %pcrel_lo(got)
129 | (((got - plt) & 0xfff) << 20));
130 write32le(p + 20, 0x00135313); // srli t1, t1, log2(16/PTRSIZE)
131 write32le(p + 24, 0x0082b283); // ld t0, PTRSIZE(t0)
132 write32le(p + 28, 0x000e0067); // jr t3
133 p += 32;
134 while (p < p_end) {
135 uint64_t pc = plt + (p - s1->plt->data);
136 uint64_t addr = got + read64le(p);
137 uint64_t off = (addr - pc + 0x800) >> 12;
138 if ((off + ((uint32_t)1 << 20)) >> 21)
139 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off, addr, pc);
140 write32le(p, 0xe17 | (off << 12)); // auipc t3, %pcrel_hi(func@got)
141 write32le(p + 4, 0x000e3e03 // ld t3, %pcrel_lo(func@got)(t3)
142 | (((addr - pc) & 0xfff) << 20));
143 write32le(p + 8, 0x000e0367); // jalr t1, t3
144 write32le(p + 12, 0x00000013); // nop
145 p += 16;
150 void relocate_init(Section *sr) {}
152 void relocate_fini(Section *sr)
156 struct pcrel_hi {
157 addr_t addr, val;
159 static struct pcrel_hi last_hi;
161 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
162 addr_t addr, addr_t val)
164 uint64_t off64;
165 uint32_t off32;
166 int sym_index = ELFW(R_SYM)(rel->r_info);
167 #ifdef DEBUG_RELOC
168 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
169 #endif
171 switch(type) {
172 case R_RISCV_ALIGN:
173 case R_RISCV_RELAX:
174 return;
176 case R_RISCV_BRANCH:
177 off64 = val - addr;
178 if ((off64 + (1 << 12)) & ~(uint64_t)0x1ffe)
179 tcc_error("R_RISCV_BRANCH relocation failed"
180 " (val=%lx, addr=%lx)", val, addr);
181 off32 = off64 >> 1;
182 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
183 | ((off32 & 0x800) << 20)
184 | ((off32 & 0x3f0) << 21)
185 | ((off32 & 0x00f) << 8)
186 | ((off32 & 0x400) >> 3));
187 return;
188 case R_RISCV_JAL:
189 off64 = val - addr;
190 if ((off64 + (1 << 21)) & ~(((uint64_t)1 << 22) - 2))
191 tcc_error("R_RISCV_BRANCH relocation failed"
192 " (val=%lx, addr=%lx)", val, addr);
193 off32 = off64;
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));
199 return;
200 case R_RISCV_CALL:
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));
206 return;
207 case R_RISCV_PCREL_HI20:
208 printf("PCREL_HI20: val=%lx addr=%lx\n", val, addr);
209 off64 = (int64_t)(val - addr + 0x800) >> 12;
210 if ((off64 + ((uint64_t)1 << 20)) >> 21)
211 tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx",
212 off64, ((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21));
213 write32le(ptr, (read32le(ptr) & 0xfff)
214 | ((off64 & 0xfffff) << 12));
215 last_hi.addr = addr;
216 last_hi.val = val;
217 return;
218 case R_RISCV_GOT_HI20:
219 val = s1->got->sh_addr + get_sym_attr(s1, sym_index, 0)->got_offset;
220 off64 = (int64_t)(val - addr + 0x800) >> 12;
221 if ((off64 + ((uint64_t)1 << 20)) >> 21)
222 tcc_error("R_RISCV_GOT_HI20 relocation failed");
223 last_hi.addr = addr;
224 last_hi.val = val;
225 write32le(ptr, (read32le(ptr) & 0xfff)
226 | ((off64 & 0xfffff) << 12));
227 return;
228 case R_RISCV_PCREL_LO12_I:
229 printf("PCREL_LO12_I: val=%lx addr=%lx\n", val, addr);
230 if (val != last_hi.addr)
231 tcc_error("unsupported hi/lo pcrel reloc scheme");
232 val = last_hi.val;
233 addr = last_hi.addr;
234 write32le(ptr, (read32le(ptr) & 0xfffff)
235 | (((val - addr) & 0xfff) << 20));
236 return;
237 case R_RISCV_PCREL_LO12_S:
238 if (val != last_hi.addr)
239 tcc_error("unsupported hi/lo pcrel reloc scheme");
240 val = last_hi.val;
241 addr = last_hi.addr;
242 off32 = val - addr;
243 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
244 | ((off32 & 0xfe0) << 20)
245 | ((off32 & 0x01f) << 7));
246 return;
248 case R_RISCV_RVC_BRANCH:
249 off64 = (val - addr);
250 if ((off64 + (1 << 8)) & ~(uint64_t)0x1fe)
251 tcc_error("R_RISCV_RVC_BRANCH relocation failed"
252 " (val=%lx, addr=%lx)", val, addr);
253 off32 = off64;
254 write16le(ptr, (read16le(ptr) & 0xe383)
255 | (((off32 >> 5) & 1) << 2)
256 | (((off32 >> 1) & 3) << 3)
257 | (((off32 >> 6) & 3) << 5)
258 | (((off32 >> 3) & 3) << 10)
259 | (((off32 >> 8) & 1) << 12));
260 return;
261 case R_RISCV_RVC_JUMP:
262 off64 = (val - addr);
263 if ((off64 + (1 << 11)) & ~(uint64_t)0xffe)
264 tcc_error("R_RISCV_RVC_BRANCH relocation failed"
265 " (val=%lx, addr=%lx)", val, addr);
266 off32 = off64;
267 write16le(ptr, (read16le(ptr) & 0xe003)
268 | (((off32 >> 5) & 1) << 2)
269 | (((off32 >> 1) & 7) << 3)
270 | (((off32 >> 7) & 1) << 6)
271 | (((off32 >> 6) & 1) << 7)
272 | (((off32 >> 10) & 1) << 8)
273 | (((off32 >> 8) & 3) << 9)
274 | (((off32 >> 4) & 1) << 11)
275 | (((off32 >> 11) & 1) << 12));
276 return;
278 case R_RISCV_32:
279 write32le(ptr, val);
280 return;
281 case R_RISCV_64:
282 write64le(ptr, val);
283 return;
284 case R_RISCV_ADD32:
285 write32le(ptr, read32le(ptr) + val);
286 return;
287 case R_RISCV_SUB32:
288 write32le(ptr, read32le(ptr) - val);
289 return;
291 case R_RISCV_32_PCREL:
292 case R_RISCV_COPY:
293 /* XXX */
294 return;
296 default:
297 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
298 type, (unsigned)addr, ptr, (unsigned)val);
299 return;
302 #endif