NetBSD: reintroduce NetBSD support. Currently, -run works but executable has an incor...
[tinycc/tcc_android_arm.git] / riscv64-link.c
blob02c5718ac950e899dc8c94f999c4906e33fa2a86
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 return -1;
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
61 different values. */
62 int gotplt_entry_type (int reloc_type)
64 switch (reloc_type) {
65 case R_RISCV_ALIGN:
66 case R_RISCV_RELAX:
67 case R_RISCV_RVC_BRANCH:
68 case R_RISCV_RVC_JUMP:
69 case R_RISCV_JUMP_SLOT:
70 case R_RISCV_SET6:
71 case R_RISCV_SUB6:
72 case R_RISCV_ADD16:
73 case R_RISCV_SUB16:
74 return NO_GOTPLT_ENTRY;
76 case R_RISCV_BRANCH:
77 case R_RISCV_CALL:
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:
82 case R_RISCV_ADD32:
83 case R_RISCV_ADD64:
84 case R_RISCV_SUB32:
85 case R_RISCV_SUB64:
86 case R_RISCV_32:
87 case R_RISCV_64:
88 case R_RISCV_JAL:
89 case R_RISCV_CALL_PLT:
90 return AUTO_GOTPLT_ENTRY;
92 case R_RISCV_GOT_HI20:
93 return ALWAYS_GOTPLT_ENTRY;
95 return -1;
98 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
100 Section *plt = s1->plt;
101 uint8_t *p;
102 unsigned plt_offset;
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);
110 return plt_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)
117 uint8_t *p, *p_end;
119 if (!s1->plt)
120 return;
122 p = s1->plt->data;
123 p_end = p + s1->plt->data_offset;
125 if (p < p_end) {
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)", (long)off, (long)got, (long)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
141 p += 32;
142 while (p < p_end) {
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)", (long)off, (long)addr, (long)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
153 p += 16;
158 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
159 addr_t addr, addr_t val)
161 uint64_t off64;
162 uint32_t off32;
163 int sym_index = ELFW(R_SYM)(rel->r_info), esym_index;
164 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
166 switch(type) {
167 case R_RISCV_ALIGN:
168 case R_RISCV_RELAX:
169 return;
171 case R_RISCV_BRANCH:
172 off64 = val - addr;
173 if ((off64 + (1 << 12)) & ~(uint64_t)0x1ffe)
174 tcc_error("R_RISCV_BRANCH relocation failed"
175 " (val=%lx, addr=%lx)", (long)val, (long)addr);
176 off32 = off64 >> 1;
177 write32le(ptr, (read32le(ptr) & ~0xfe000f80)
178 | ((off32 & 0x800) << 20)
179 | ((off32 & 0x3f0) << 21)
180 | ((off32 & 0x00f) << 8)
181 | ((off32 & 0x400) >> 3));
182 return;
183 case R_RISCV_JAL:
184 off64 = val - addr;
185 if ((off64 + (1 << 21)) & ~(((uint64_t)1 << 22) - 2))
186 tcc_error("R_RISCV_JAL relocation failed"
187 " (val=%lx, addr=%lx)", (long)val, (long)addr);
188 off32 = off64;
189 write32le(ptr, (read32le(ptr) & 0xfff)
190 | (((off32 >> 12) & 0xff) << 12)
191 | (((off32 >> 11) & 1) << 20)
192 | (((off32 >> 1) & 0x3ff) << 21)
193 | (((off32 >> 20) & 1) << 31));
194 return;
195 case R_RISCV_CALL:
196 case R_RISCV_CALL_PLT:
197 write32le(ptr, (read32le(ptr) & 0xfff)
198 | ((val - addr + 0x800) & ~0xfff));
199 write32le(ptr + 4, (read32le(ptr + 4) & 0xfffff)
200 | (((val - addr) & 0xfff) << 20));
201 return;
202 case R_RISCV_PCREL_HI20:
203 #ifdef DEBUG_RELOC
204 printf("PCREL_HI20: val=%lx addr=%lx\n", (long)val, (long)addr);
205 #endif
206 off64 = (int64_t)(val - addr + 0x800) >> 12;
207 if ((off64 + ((uint64_t)1 << 20)) >> 21)
208 tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx sym=%s",
209 (long)off64, (long)((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21),
210 symtab_section->link->data + sym->st_name);
211 write32le(ptr, (read32le(ptr) & 0xfff)
212 | ((off64 & 0xfffff) << 12));
213 last_hi.addr = addr;
214 last_hi.val = val;
215 return;
216 case R_RISCV_GOT_HI20:
217 val = s1->got->sh_addr + get_sym_attr(s1, sym_index, 0)->got_offset;
218 off64 = (int64_t)(val - addr + 0x800) >> 12;
219 if ((off64 + ((uint64_t)1 << 20)) >> 21)
220 tcc_error("R_RISCV_GOT_HI20 relocation failed");
221 last_hi.addr = addr;
222 last_hi.val = val;
223 write32le(ptr, (read32le(ptr) & 0xfff)
224 | ((off64 & 0xfffff) << 12));
225 return;
226 case R_RISCV_PCREL_LO12_I:
227 #ifdef DEBUG_RELOC
228 printf("PCREL_LO12_I: val=%lx addr=%lx\n", (long)val, (long)addr);
229 #endif
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)", (long)val, (long)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)", (long)val, (long)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 if (s1->output_type == TCC_OUTPUT_DLL) {
280 /* XXX: this logic may depend on TCC's codegen
281 now TCC uses R_RISCV_RELATIVE even for a 64bit pointer */
282 qrel->r_offset = rel->r_offset;
283 qrel->r_info = ELFW(R_INFO)(0, R_RISCV_RELATIVE);
284 /* Use sign extension! */
285 qrel->r_addend = (int)read32le(ptr) + val;
286 qrel++;
288 add32le(ptr, val);
289 return;
290 case R_RISCV_64:
291 if (s1->output_type == TCC_OUTPUT_DLL) {
292 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
293 qrel->r_offset = rel->r_offset;
294 if (esym_index) {
295 qrel->r_info = ELFW(R_INFO)(esym_index, R_RISCV_64);
296 qrel->r_addend = rel->r_addend;
297 qrel++;
298 break;
299 } else {
300 qrel->r_info = ELFW(R_INFO)(0, R_RISCV_RELATIVE);
301 qrel->r_addend = read64le(ptr) + val;
302 qrel++;
305 case R_RISCV_JUMP_SLOT:
306 add64le(ptr, val);
307 return;
308 case R_RISCV_ADD64:
309 write64le(ptr, read64le(ptr) + val);
310 return;
311 case R_RISCV_ADD32:
312 write32le(ptr, read32le(ptr) + val);
313 return;
314 case R_RISCV_SUB64:
315 write64le(ptr, read64le(ptr) - val);
316 return;
317 case R_RISCV_SUB32:
318 write32le(ptr, read32le(ptr) - val);
319 return;
320 case R_RISCV_ADD16:
321 write16le(ptr, read16le(ptr) + val);
322 return;
323 case R_RISCV_SUB16:
324 write16le(ptr, read16le(ptr) - val);
325 return;
326 case R_RISCV_SET6:
327 *ptr = (*ptr & ~0x3f) | (val & 0x3f);
328 return;
329 case R_RISCV_SUB6:
330 *ptr = (*ptr & ~0x3f) | ((*ptr - val) & 0x3f);
331 return;
333 case R_RISCV_32_PCREL:
334 case R_RISCV_COPY:
335 /* XXX */
336 return;
338 default:
339 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
340 type, (unsigned)addr, ptr, (unsigned)val);
341 return;
344 #endif