win32/include/math.h: rint/trunc: pop fp stack
[tinycc.git] / arm-link.c
blobf58b8a59afca7c340cba2b720a95e5f7b9d653e3
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;
60 return -1;
63 /* Returns an enumerator to describe whether and when the relocation needs a
64 GOT and/or PLT entry to be created. See tcc.h for a description of the
65 different values. */
66 int gotplt_entry_type (int reloc_type)
68 switch (reloc_type) {
69 case R_ARM_NONE:
70 case R_ARM_COPY:
71 case R_ARM_GLOB_DAT:
72 case R_ARM_JUMP_SLOT:
73 return NO_GOTPLT_ENTRY;
75 case R_ARM_PC24:
76 case R_ARM_CALL:
77 case R_ARM_JUMP24:
78 case R_ARM_PLT32:
79 case R_ARM_THM_PC22:
80 case R_ARM_THM_JUMP24:
81 case R_ARM_MOVT_ABS:
82 case R_ARM_MOVW_ABS_NC:
83 case R_ARM_THM_MOVT_ABS:
84 case R_ARM_THM_MOVW_ABS_NC:
85 case R_ARM_PREL31:
86 case R_ARM_ABS32:
87 case R_ARM_REL32:
88 case R_ARM_V4BX:
89 return AUTO_GOTPLT_ENTRY;
91 case R_ARM_GOTPC:
92 case R_ARM_GOTOFF:
93 return BUILD_GOT_ONLY;
95 case R_ARM_GOT32:
96 return ALWAYS_GOTPLT_ENTRY;
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 /* when building a DLL, GOT entry accesses must be done relative to
108 start of GOT (see x86_64 example above) */
109 if (s1->output_type == TCC_OUTPUT_DLL)
110 tcc_error("DLLs unimplemented!");
112 /* empty PLT: create PLT0 entry that push address of call site and
113 jump to ld.so resolution routine (GOT + 8) */
114 if (plt->data_offset == 0) {
115 p = section_ptr_add(plt, 20);
116 write32le(p, 0xe52de004); /* push {lr} */
117 write32le(p+4, 0xe59fe004); /* ldr lr, [pc, #4] */
118 write32le(p+8, 0xe08fe00e); /* add lr, pc, lr */
119 write32le(p+12, 0xe5bef008); /* ldr pc, [lr, #8]! */
120 /* p+16 is set in relocate_plt */
122 plt_offset = plt->data_offset;
124 if (attr->plt_thumb_stub) {
125 p = section_ptr_add(plt, 4);
126 write32le(p, 0x4778); /* bx pc */
127 write32le(p+2, 0x46c0); /* nop */
129 p = section_ptr_add(plt, 16);
130 /* Jump to GOT entry where ld.so initially put address of PLT0 */
131 write32le(p, 0xe59fc004); /* ldr ip, [pc, #4] */
132 write32le(p+4, 0xe08fc00c); /* add ip, pc, ip */
133 write32le(p+8, 0xe59cf000); /* ldr pc, [ip] */
134 /* p + 12 contains offset to GOT entry once patched by relocate_plt */
135 write32le(p+12, got_offset);
136 return plt_offset;
139 /* relocate the PLT: compute addresses and offsets in the PLT now that final
140 address for PLT and GOT are known (see fill_program_header) */
141 ST_FUNC void relocate_plt(TCCState *s1)
143 uint8_t *p, *p_end;
145 if (!s1->plt)
146 return;
148 p = s1->plt->data;
149 p_end = p + s1->plt->data_offset;
151 if (p < p_end) {
152 int x = s1->got->sh_addr - s1->plt->sh_addr - 12;
153 write32le(s1->plt->data + 16, x - 16);
154 p += 20;
155 while (p < p_end) {
156 if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
157 p += 4;
158 add32le(p + 12, x + s1->plt->data - p);
159 p += 16;
164 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
166 ElfW(Sym) *sym;
167 int sym_index;
169 sym_index = ELFW(R_SYM)(rel->r_info);
170 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
172 switch(type) {
173 case R_ARM_PC24:
174 case R_ARM_CALL:
175 case R_ARM_JUMP24:
176 case R_ARM_PLT32:
178 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
179 x = (*(int *) ptr) & 0xffffff;
180 #ifdef DEBUG_RELOC
181 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
182 #endif
183 (*(int *)ptr) &= 0xff000000;
184 if (x & 0x800000)
185 x -= 0x1000000;
186 x <<= 2;
187 blx_avail = (TCC_CPU_VERSION >= 5);
188 is_thumb = val & 1;
189 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
190 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
191 x += val - addr;
192 #ifdef DEBUG_RELOC
193 printf (" newx=0x%x name=%s\n", x,
194 (char *) symtab_section->link->data + sym->st_name);
195 #endif
196 h = x & 2;
197 th_ko = (x & 3) && (!blx_avail || !is_call);
198 if (th_ko || x >= 0x2000000 || x < -0x2000000)
199 tcc_error("can't relocate value at %x,%d",addr, type);
200 x >>= 2;
201 x &= 0xffffff;
202 /* Only reached if blx is avail and it is a call */
203 if (is_thumb) {
204 x |= h << 24;
205 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
207 (*(int *) ptr) |= x;
209 return;
210 /* Since these relocations only concern Thumb-2 and blx instruction was
211 introduced before Thumb-2, we can assume blx is available and not
212 guard its use */
213 case R_ARM_THM_PC22:
214 case R_ARM_THM_JUMP24:
216 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
217 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
218 Section *plt;
220 /* weak reference */
221 if (sym->st_shndx == SHN_UNDEF &&
222 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
223 return;
225 /* Get initial offset */
226 hi = (*(uint16_t *)ptr);
227 lo = (*(uint16_t *)(ptr+2));
228 s = (hi >> 10) & 1;
229 j1 = (lo >> 13) & 1;
230 j2 = (lo >> 11) & 1;
231 i1 = (j1 ^ s) ^ 1;
232 i2 = (j2 ^ s) ^ 1;
233 imm10 = hi & 0x3ff;
234 imm11 = lo & 0x7ff;
235 x = (s << 24) | (i1 << 23) | (i2 << 22) |
236 (imm10 << 12) | (imm11 << 1);
237 if (x & 0x01000000)
238 x -= 0x02000000;
240 /* Relocation infos */
241 to_thumb = val & 1;
242 plt = s1->plt;
243 to_plt = (val >= plt->sh_addr) &&
244 (val < plt->sh_addr + plt->data_offset);
245 is_call = (type == R_ARM_THM_PC22);
247 if (!to_thumb && !to_plt && !is_call) {
248 int index;
249 uint8_t *p;
250 char *name, buf[1024];
251 Section *text;
253 name = (char *) symtab_section->link->data + sym->st_name;
254 text = s1->sections[sym->st_shndx];
255 /* Modify reloc to target a thumb stub to switch to ARM */
256 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
257 index = put_elf_sym(symtab_section,
258 text->data_offset + 1,
259 sym->st_size, sym->st_info, 0,
260 sym->st_shndx, buf);
261 to_thumb = 1;
262 val = text->data_offset + 1;
263 rel->r_info = ELFW(R_INFO)(index, type);
264 /* Create a thumb stub function to switch to ARM mode */
265 put_elf_reloc(symtab_section, text,
266 text->data_offset + 4, R_ARM_JUMP24,
267 sym_index);
268 p = section_ptr_add(text, 8);
269 write32le(p, 0x4778); /* bx pc */
270 write32le(p+2, 0x46c0); /* nop */
271 write32le(p+4, 0xeafffffe); /* b $sym */
274 /* Compute final offset */
275 x += val - addr;
276 if (!to_thumb && is_call) {
277 blx_bit = 0; /* bl -> blx */
278 x = (x + 3) & -4; /* Compute offset from aligned PC */
281 /* Check that relocation is possible
282 * offset must not be out of range
283 * if target is to be entered in arm mode:
284 - bit 1 must not set
285 - instruction must be a call (bl) or a jump to PLT */
286 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
287 if (to_thumb || (val & 2) || (!is_call && !to_plt))
288 tcc_error("can't relocate value at %x,%d",addr, type);
290 /* Compute and store final offset */
291 s = (x >> 24) & 1;
292 i1 = (x >> 23) & 1;
293 i2 = (x >> 22) & 1;
294 j1 = s ^ (i1 ^ 1);
295 j2 = s ^ (i2 ^ 1);
296 imm10 = (x >> 12) & 0x3ff;
297 imm11 = (x >> 1) & 0x7ff;
298 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
299 (s << 10) | imm10);
300 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
301 (j1 << 13) | blx_bit | (j2 << 11) |
302 imm11);
304 return;
305 case R_ARM_MOVT_ABS:
306 case R_ARM_MOVW_ABS_NC:
308 int x, imm4, imm12;
309 if (type == R_ARM_MOVT_ABS)
310 val >>= 16;
311 imm12 = val & 0xfff;
312 imm4 = (val >> 12) & 0xf;
313 x = (imm4 << 16) | imm12;
314 if (type == R_ARM_THM_MOVT_ABS)
315 *(int *)ptr |= x;
316 else
317 *(int *)ptr += x;
319 return;
320 case R_ARM_THM_MOVT_ABS:
321 case R_ARM_THM_MOVW_ABS_NC:
323 int x, i, imm4, imm3, imm8;
324 if (type == R_ARM_THM_MOVT_ABS)
325 val >>= 16;
326 imm8 = val & 0xff;
327 imm3 = (val >> 8) & 0x7;
328 i = (val >> 11) & 1;
329 imm4 = (val >> 12) & 0xf;
330 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
331 if (type == R_ARM_THM_MOVT_ABS)
332 *(int *)ptr |= x;
333 else
334 *(int *)ptr += x;
336 return;
337 case R_ARM_PREL31:
339 int x;
340 x = (*(int *)ptr) & 0x7fffffff;
341 (*(int *)ptr) &= 0x80000000;
342 x = (x * 2) / 2;
343 x += val - addr;
344 if((x^(x>>1))&0x40000000)
345 tcc_error("can't relocate value at %x,%d",addr, type);
346 (*(int *)ptr) |= x & 0x7fffffff;
348 case R_ARM_ABS32:
349 *(int *)ptr += val;
350 return;
351 case R_ARM_REL32:
352 *(int *)ptr += val - addr;
353 return;
354 case R_ARM_GOTPC:
355 *(int *)ptr += s1->got->sh_addr - addr;
356 return;
357 case R_ARM_GOTOFF:
358 *(int *)ptr += val - s1->got->sh_addr;
359 return;
360 case R_ARM_GOT32:
361 /* we load the got offset */
362 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
363 return;
364 case R_ARM_COPY:
365 return;
366 case R_ARM_V4BX:
367 /* trade Thumb support for ARMv4 support */
368 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
369 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
370 return;
371 case R_ARM_GLOB_DAT:
372 case R_ARM_JUMP_SLOT:
373 *(addr_t *)ptr = val;
374 return;
375 case R_ARM_NONE:
376 /* Nothing to do. Normally used to indicate a dependency
377 on a certain symbol (like for exception handling under EABI). */
378 return;
379 case R_ARM_RELATIVE:
380 #ifdef TCC_TARGET_PE
381 add32le(ptr, val - s1->pe_imagebase);
382 #endif
383 /* do nothing */
384 return;
385 default:
386 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
387 type, (unsigned)addr, ptr, (unsigned)val);
388 return;
392 #endif /* !TARGET_DEFS_ONLY */