riscv: fp parameters
[tinycc.git] / riscv64-link.c
blobe03d70094e03c0a6219dfa3c8e3412c9ededb812
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 case R_RISCV_JUMP_SLOT:
64 return NO_GOTPLT_ENTRY;
66 case R_RISCV_BRANCH:
67 case R_RISCV_CALL:
68 case R_RISCV_PCREL_HI20:
69 case R_RISCV_PCREL_LO12_I:
70 case R_RISCV_PCREL_LO12_S:
71 case R_RISCV_32_PCREL:
72 case R_RISCV_ADD32:
73 case R_RISCV_SUB32:
74 case R_RISCV_32:
75 case R_RISCV_64:
76 case R_RISCV_JAL:
77 return AUTO_GOTPLT_ENTRY;
79 case R_RISCV_GOT_HI20:
80 case R_RISCV_CALL_PLT:
81 return ALWAYS_GOTPLT_ENTRY;
84 tcc_error ("Unknown relocation type: %d", reloc_type);
85 return -1;
88 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
90 Section *plt = s1->plt;
91 uint8_t *p;
92 unsigned plt_offset;
94 if (s1->output_type == TCC_OUTPUT_DLL)
95 tcc_error("DLLs unimplemented!");
97 if (plt->data_offset == 0)
98 section_ptr_add(plt, 32);
99 plt_offset = plt->data_offset;
101 p = section_ptr_add(plt, 16);
102 write64le(p, got_offset);
103 return plt_offset;
106 /* relocate the PLT: compute addresses and offsets in the PLT now that final
107 address for PLT and GOT are known (see fill_program_header) */
108 ST_FUNC void relocate_plt(TCCState *s1)
110 uint8_t *p, *p_end;
112 if (!s1->plt)
113 return;
115 p = s1->plt->data;
116 p_end = p + s1->plt->data_offset;
118 if (p < p_end) {
119 uint64_t plt = s1->plt->sh_addr;
120 uint64_t got = s1->got->sh_addr;
121 uint64_t off = (got - plt + 0x800) >> 12;
122 if ((off + ((uint32_t)1 << 20)) >> 21)
123 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", off, got, plt);
124 write32le(p, 0x397 | (off << 12)); // auipc t2, %pcrel_hi(got)
125 write32le(p + 4, 0x41c30333); // sub t1, t1, t3
126 write32le(p + 8, 0x0003be03 // ld t3, %pcrel_lo(got)(t2)
127 | (((got - plt) & 0xfff) << 20));
128 write32le(p + 12, 0xfd430313); // addi t1, t1, -(32+12)
129 write32le(p + 16, 0x00038293 // addi t0, t2, %pcrel_lo(got)
130 | (((got - plt) & 0xfff) << 20));
131 write32le(p + 20, 0x00135313); // srli t1, t1, log2(16/PTRSIZE)
132 write32le(p + 24, 0x0082b283); // ld t0, PTRSIZE(t0)
133 write32le(p + 28, 0x000e0067); // jr t3
134 p += 32;
135 while (p < p_end) {
136 uint64_t pc = plt + (p - s1->plt->data);
137 uint64_t addr = got + read64le(p);
138 uint64_t off = (addr - pc + 0x800) >> 12;
139 if ((off + ((uint32_t)1 << 20)) >> 21)
140 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off, addr, pc);
141 write32le(p, 0xe17 | (off << 12)); // auipc t3, %pcrel_hi(func@got)
142 write32le(p + 4, 0x000e3e03 // ld t3, %pcrel_lo(func@got)(t3)
143 | (((addr - pc) & 0xfff) << 20));
144 write32le(p + 8, 0x000e0367); // jalr t1, t3
145 write32le(p + 12, 0x00000013); // nop
146 p += 16;
151 void relocate_init(Section *sr) {}
153 void relocate_fini(Section *sr)
157 struct pcrel_hi {
158 addr_t addr, val;
160 static struct pcrel_hi last_hi;
162 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
163 addr_t addr, addr_t val)
165 uint64_t off64;
166 uint32_t off32;
167 int sym_index = ELFW(R_SYM)(rel->r_info);
168 #ifdef DEBUG_RELOC
169 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
170 #endif
172 switch(type) {
173 case R_RISCV_ALIGN:
174 case R_RISCV_RELAX:
175 return;
177 case R_RISCV_BRANCH:
178 off64 = val - addr;
179 if ((off64 + (1 << 12)) & ~(uint64_t)0x1ffe)
180 tcc_error("R_RISCV_BRANCH relocation failed"
181 " (val=%lx, addr=%lx)", val, addr);
182 off32 = off64 >> 1;
183 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
184 | ((off32 & 0x800) << 20)
185 | ((off32 & 0x3f0) << 21)
186 | ((off32 & 0x00f) << 8)
187 | ((off32 & 0x400) >> 3));
188 return;
189 case R_RISCV_JAL:
190 off64 = val - addr;
191 if ((off64 + (1 << 21)) & ~(((uint64_t)1 << 22) - 2))
192 tcc_error("R_RISCV_JAL relocation failed"
193 " (val=%lx, addr=%lx)", val, addr);
194 off32 = off64;
195 write32le(ptr, (read32le(ptr) & 0xfff)
196 | (((off32 >> 12) & 0xff) << 12)
197 | (((off32 >> 11) & 1) << 20)
198 | (((off32 >> 1) & 0x3ff) << 21)
199 | (((off32 >> 20) & 1) << 31));
200 return;
201 case R_RISCV_CALL:
202 case R_RISCV_CALL_PLT:
203 write32le(ptr, (read32le(ptr) & 0xfff)
204 | ((val - addr + 0x800) & ~0xfff));
205 write32le(ptr + 4, (read32le(ptr + 4) & 0xfffff)
206 | (((val - addr) & 0xfff) << 20));
207 return;
208 case R_RISCV_PCREL_HI20:
209 #ifdef DEBUG_RELOC
210 printf("PCREL_HI20: val=%lx addr=%lx\n", val, addr);
211 #endif
212 off64 = (int64_t)(val - addr + 0x800) >> 12;
213 if ((off64 + ((uint64_t)1 << 20)) >> 21)
214 tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx",
215 off64, ((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21));
216 write32le(ptr, (read32le(ptr) & 0xfff)
217 | ((off64 & 0xfffff) << 12));
218 last_hi.addr = addr;
219 last_hi.val = val;
220 return;
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");
226 last_hi.addr = addr;
227 last_hi.val = val;
228 write32le(ptr, (read32le(ptr) & 0xfff)
229 | ((off64 & 0xfffff) << 12));
230 return;
231 case R_RISCV_PCREL_LO12_I:
232 #ifdef DEBUG_RELOC
233 printf("PCREL_LO12_I: val=%lx addr=%lx\n", val, addr);
234 #endif
235 if (val != last_hi.addr)
236 tcc_error("unsupported hi/lo pcrel reloc scheme");
237 val = last_hi.val;
238 addr = last_hi.addr;
239 write32le(ptr, (read32le(ptr) & 0xfffff)
240 | (((val - addr) & 0xfff) << 20));
241 return;
242 case R_RISCV_PCREL_LO12_S:
243 if (val != last_hi.addr)
244 tcc_error("unsupported hi/lo pcrel reloc scheme");
245 val = last_hi.val;
246 addr = last_hi.addr;
247 off32 = val - addr;
248 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
249 | ((off32 & 0xfe0) << 20)
250 | ((off32 & 0x01f) << 7));
251 return;
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);
258 off32 = off64;
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));
265 return;
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);
271 off32 = off64;
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));
281 return;
283 case R_RISCV_32:
284 write32le(ptr, val);
285 return;
286 case R_RISCV_JUMP_SLOT:
287 case R_RISCV_64:
288 write64le(ptr, val);
289 return;
290 case R_RISCV_ADD32:
291 write32le(ptr, read32le(ptr) + val);
292 return;
293 case R_RISCV_SUB32:
294 write32le(ptr, read32le(ptr) - val);
295 return;
297 case R_RISCV_32_PCREL:
298 case R_RISCV_COPY:
299 /* XXX */
300 return;
302 default:
303 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
304 type, (unsigned)addr, ptr, (unsigned)val);
305 return;
308 #endif