riscv: More insns, operands and arg slots
[tinycc.git] / arm-link.c
blob54d0a10740e5e3589531a7583d82a79c280ff30b
1 #ifdef TARGET_DEFS_ONLY
3 #define EM_TCC_TARGET EM_ARM
5 /* relocation type for 32 bit data relocation */
6 #define R_DATA_32 R_ARM_ABS32
7 #define R_DATA_PTR R_ARM_ABS32
8 #define R_JMP_SLOT R_ARM_JUMP_SLOT
9 #define R_GLOB_DAT R_ARM_GLOB_DAT
10 #define R_COPY R_ARM_COPY
11 #define R_RELATIVE R_ARM_RELATIVE
13 #define R_NUM R_ARM_NUM
15 #define ELF_START_ADDR 0x00008000
16 #define ELF_PAGE_SIZE 0x1000
18 #define PCRELATIVE_DLLPLT 1
19 #define RELOCATE_DLLPLT 0
21 enum float_abi {
22 ARM_SOFTFP_FLOAT,
23 ARM_HARD_FLOAT,
26 #else /* !TARGET_DEFS_ONLY */
28 #include "tcc.h"
30 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
31 relocations, returns -1. */
32 int code_reloc (int reloc_type)
34 switch (reloc_type) {
35 case R_ARM_MOVT_ABS:
36 case R_ARM_MOVW_ABS_NC:
37 case R_ARM_THM_MOVT_ABS:
38 case R_ARM_THM_MOVW_ABS_NC:
39 case R_ARM_ABS32:
40 case R_ARM_REL32:
41 case R_ARM_GOTPC:
42 case R_ARM_GOTOFF:
43 case R_ARM_GOT32:
44 case R_ARM_COPY:
45 case R_ARM_GLOB_DAT:
46 case R_ARM_NONE:
47 return 0;
49 case R_ARM_PC24:
50 case R_ARM_CALL:
51 case R_ARM_JUMP24:
52 case R_ARM_PLT32:
53 case R_ARM_THM_PC22:
54 case R_ARM_THM_JUMP24:
55 case R_ARM_PREL31:
56 case R_ARM_V4BX:
57 case R_ARM_JUMP_SLOT:
58 return 1;
61 tcc_error ("Unknown relocation type: %d", reloc_type);
62 return -1;
65 /* Returns an enumerator to describe whether and when the relocation needs a
66 GOT and/or PLT entry to be created. See tcc.h for a description of the
67 different values. */
68 int gotplt_entry_type (int reloc_type)
70 switch (reloc_type) {
71 case R_ARM_NONE:
72 case R_ARM_COPY:
73 case R_ARM_GLOB_DAT:
74 case R_ARM_JUMP_SLOT:
75 return NO_GOTPLT_ENTRY;
77 case R_ARM_PC24:
78 case R_ARM_CALL:
79 case R_ARM_JUMP24:
80 case R_ARM_PLT32:
81 case R_ARM_THM_PC22:
82 case R_ARM_THM_JUMP24:
83 case R_ARM_MOVT_ABS:
84 case R_ARM_MOVW_ABS_NC:
85 case R_ARM_THM_MOVT_ABS:
86 case R_ARM_THM_MOVW_ABS_NC:
87 case R_ARM_PREL31:
88 case R_ARM_ABS32:
89 case R_ARM_REL32:
90 case R_ARM_V4BX:
91 return AUTO_GOTPLT_ENTRY;
93 case R_ARM_GOTPC:
94 case R_ARM_GOTOFF:
95 return BUILD_GOT_ONLY;
97 case R_ARM_GOT32:
98 return ALWAYS_GOTPLT_ENTRY;
101 tcc_error ("Unknown relocation type: %d", reloc_type);
102 return -1;
105 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
107 Section *plt = s1->plt;
108 uint8_t *p;
109 unsigned plt_offset;
111 /* when building a DLL, GOT entry accesses must be done relative to
112 start of GOT (see x86_64 example above) */
113 if (s1->output_type == TCC_OUTPUT_DLL)
114 tcc_error("DLLs unimplemented!");
116 /* empty PLT: create PLT0 entry that push address of call site and
117 jump to ld.so resolution routine (GOT + 8) */
118 if (plt->data_offset == 0) {
119 p = section_ptr_add(plt, 20);
120 write32le(p, 0xe52de004); /* push {lr} */
121 write32le(p+4, 0xe59fe004); /* ldr lr, [pc, #4] */
122 write32le(p+8, 0xe08fe00e); /* add lr, pc, lr */
123 write32le(p+12, 0xe5bef008); /* ldr pc, [lr, #8]! */
124 /* p+16 is set in relocate_plt */
126 plt_offset = plt->data_offset;
128 if (attr->plt_thumb_stub) {
129 p = section_ptr_add(plt, 4);
130 write32le(p, 0x4778); /* bx pc */
131 write32le(p+2, 0x46c0); /* nop */
133 p = section_ptr_add(plt, 16);
134 /* Jump to GOT entry where ld.so initially put address of PLT0 */
135 write32le(p, 0xe59fc004); /* ldr ip, [pc, #4] */
136 write32le(p+4, 0xe08fc00c); /* add ip, pc, ip */
137 write32le(p+8, 0xe59cf000); /* ldr pc, [ip] */
138 /* p + 12 contains offset to GOT entry once patched by relocate_plt */
139 write32le(p+12, got_offset);
140 return plt_offset;
143 /* relocate the PLT: compute addresses and offsets in the PLT now that final
144 address for PLT and GOT are known (see fill_program_header) */
145 ST_FUNC void relocate_plt(TCCState *s1)
147 uint8_t *p, *p_end;
149 if (!s1->plt)
150 return;
152 p = s1->plt->data;
153 p_end = p + s1->plt->data_offset;
155 if (p < p_end) {
156 int x = s1->got->sh_addr - s1->plt->sh_addr - 12;
157 write32le(s1->plt->data + 16, x - 16);
158 p += 20;
159 while (p < p_end) {
160 if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
161 p += 4;
162 add32le(p + 12, x + s1->plt->data - p);
163 p += 16;
168 void relocate_init(Section *sr) {}
169 void relocate_fini(Section *sr) {}
171 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
173 ElfW(Sym) *sym;
174 int sym_index;
176 sym_index = ELFW(R_SYM)(rel->r_info);
177 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
179 switch(type) {
180 case R_ARM_PC24:
181 case R_ARM_CALL:
182 case R_ARM_JUMP24:
183 case R_ARM_PLT32:
185 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
186 x = (*(int *) ptr) & 0xffffff;
187 #ifdef DEBUG_RELOC
188 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
189 #endif
190 (*(int *)ptr) &= 0xff000000;
191 if (x & 0x800000)
192 x -= 0x1000000;
193 x <<= 2;
194 blx_avail = (TCC_CPU_VERSION >= 5);
195 is_thumb = val & 1;
196 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
197 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
198 x += val - addr;
199 #ifdef DEBUG_RELOC
200 printf (" newx=0x%x name=%s\n", x,
201 (char *) symtab_section->link->data + sym->st_name);
202 #endif
203 h = x & 2;
204 th_ko = (x & 3) && (!blx_avail || !is_call);
205 if (th_ko || x >= 0x2000000 || x < -0x2000000)
206 tcc_error("can't relocate value at %x,%d",addr, type);
207 x >>= 2;
208 x &= 0xffffff;
209 /* Only reached if blx is avail and it is a call */
210 if (is_thumb) {
211 x |= h << 24;
212 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
214 (*(int *) ptr) |= x;
216 return;
217 /* Since these relocations only concern Thumb-2 and blx instruction was
218 introduced before Thumb-2, we can assume blx is available and not
219 guard its use */
220 case R_ARM_THM_PC22:
221 case R_ARM_THM_JUMP24:
223 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
224 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
225 Section *plt;
227 /* weak reference */
228 if (sym->st_shndx == SHN_UNDEF &&
229 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
230 return;
232 /* Get initial offset */
233 hi = (*(uint16_t *)ptr);
234 lo = (*(uint16_t *)(ptr+2));
235 s = (hi >> 10) & 1;
236 j1 = (lo >> 13) & 1;
237 j2 = (lo >> 11) & 1;
238 i1 = (j1 ^ s) ^ 1;
239 i2 = (j2 ^ s) ^ 1;
240 imm10 = hi & 0x3ff;
241 imm11 = lo & 0x7ff;
242 x = (s << 24) | (i1 << 23) | (i2 << 22) |
243 (imm10 << 12) | (imm11 << 1);
244 if (x & 0x01000000)
245 x -= 0x02000000;
247 /* Relocation infos */
248 to_thumb = val & 1;
249 plt = s1->plt;
250 to_plt = (val >= plt->sh_addr) &&
251 (val < plt->sh_addr + plt->data_offset);
252 is_call = (type == R_ARM_THM_PC22);
254 if (!to_thumb && !to_plt && !is_call) {
255 int index;
256 uint8_t *p;
257 char *name, buf[1024];
258 Section *text_section;
260 name = (char *) symtab_section->link->data + sym->st_name;
261 text_section = s1->sections[sym->st_shndx];
262 /* Modify reloc to target a thumb stub to switch to ARM */
263 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
264 index = put_elf_sym(symtab_section,
265 text_section->data_offset + 1,
266 sym->st_size, sym->st_info, 0,
267 sym->st_shndx, buf);
268 to_thumb = 1;
269 val = text_section->data_offset + 1;
270 rel->r_info = ELFW(R_INFO)(index, type);
271 /* Create a thumb stub function to switch to ARM mode */
272 put_elf_reloc(symtab_section, text_section,
273 text_section->data_offset + 4, R_ARM_JUMP24,
274 sym_index);
275 p = section_ptr_add(text_section, 8);
276 write32le(p, 0x4778); /* bx pc */
277 write32le(p+2, 0x46c0); /* nop */
278 write32le(p+4, 0xeafffffe); /* b $sym */
281 /* Compute final offset */
282 x += val - addr;
283 if (!to_thumb && is_call) {
284 blx_bit = 0; /* bl -> blx */
285 x = (x + 3) & -4; /* Compute offset from aligned PC */
288 /* Check that relocation is possible
289 * offset must not be out of range
290 * if target is to be entered in arm mode:
291 - bit 1 must not set
292 - instruction must be a call (bl) or a jump to PLT */
293 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
294 if (to_thumb || (val & 2) || (!is_call && !to_plt))
295 tcc_error("can't relocate value at %x,%d",addr, type);
297 /* Compute and store final offset */
298 s = (x >> 24) & 1;
299 i1 = (x >> 23) & 1;
300 i2 = (x >> 22) & 1;
301 j1 = s ^ (i1 ^ 1);
302 j2 = s ^ (i2 ^ 1);
303 imm10 = (x >> 12) & 0x3ff;
304 imm11 = (x >> 1) & 0x7ff;
305 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
306 (s << 10) | imm10);
307 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
308 (j1 << 13) | blx_bit | (j2 << 11) |
309 imm11);
311 return;
312 case R_ARM_MOVT_ABS:
313 case R_ARM_MOVW_ABS_NC:
315 int x, imm4, imm12;
316 if (type == R_ARM_MOVT_ABS)
317 val >>= 16;
318 imm12 = val & 0xfff;
319 imm4 = (val >> 12) & 0xf;
320 x = (imm4 << 16) | imm12;
321 if (type == R_ARM_THM_MOVT_ABS)
322 *(int *)ptr |= x;
323 else
324 *(int *)ptr += x;
326 return;
327 case R_ARM_THM_MOVT_ABS:
328 case R_ARM_THM_MOVW_ABS_NC:
330 int x, i, imm4, imm3, imm8;
331 if (type == R_ARM_THM_MOVT_ABS)
332 val >>= 16;
333 imm8 = val & 0xff;
334 imm3 = (val >> 8) & 0x7;
335 i = (val >> 11) & 1;
336 imm4 = (val >> 12) & 0xf;
337 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
338 if (type == R_ARM_THM_MOVT_ABS)
339 *(int *)ptr |= x;
340 else
341 *(int *)ptr += x;
343 return;
344 case R_ARM_PREL31:
346 int x;
347 x = (*(int *)ptr) & 0x7fffffff;
348 (*(int *)ptr) &= 0x80000000;
349 x = (x * 2) / 2;
350 x += val - addr;
351 if((x^(x>>1))&0x40000000)
352 tcc_error("can't relocate value at %x,%d",addr, type);
353 (*(int *)ptr) |= x & 0x7fffffff;
355 case R_ARM_ABS32:
356 *(int *)ptr += val;
357 return;
358 case R_ARM_REL32:
359 *(int *)ptr += val - addr;
360 return;
361 case R_ARM_GOTPC:
362 *(int *)ptr += s1->got->sh_addr - addr;
363 return;
364 case R_ARM_GOTOFF:
365 *(int *)ptr += val - s1->got->sh_addr;
366 return;
367 case R_ARM_GOT32:
368 /* we load the got offset */
369 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
370 return;
371 case R_ARM_COPY:
372 return;
373 case R_ARM_V4BX:
374 /* trade Thumb support for ARMv4 support */
375 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
376 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
377 return;
378 case R_ARM_GLOB_DAT:
379 case R_ARM_JUMP_SLOT:
380 *(addr_t *)ptr = val;
381 return;
382 case R_ARM_NONE:
383 /* Nothing to do. Normally used to indicate a dependency
384 on a certain symbol (like for exception handling under EABI). */
385 return;
386 case R_ARM_RELATIVE:
387 #ifdef TCC_TARGET_PE
388 add32le(ptr, val - s1->pe_imagebase);
389 #endif
390 /* do nothing */
391 return;
392 default:
393 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
394 type, (unsigned)addr, ptr, (unsigned)val);
395 return;
399 #endif /* !TARGET_DEFS_ONLY */