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