tcc.h: Extend search path for include, lib and crt.
[tinycc.git] / arm-link.c
blob5d40f84309f86bbd8ccfa662fd7406a2601e1e3d
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 0x00010000
16 #define ELF_PAGE_SIZE 0x10000
18 #define PCRELATIVE_DLLPLT 1
19 #define RELOCATE_DLLPLT 1
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_GOT_PREL:
45 case R_ARM_COPY:
46 case R_ARM_GLOB_DAT:
47 case R_ARM_NONE:
48 case R_ARM_TARGET1:
49 case R_ARM_MOVT_PREL:
50 case R_ARM_MOVW_PREL_NC:
51 return 0;
53 case R_ARM_PC24:
54 case R_ARM_CALL:
55 case R_ARM_JUMP24:
56 case R_ARM_PLT32:
57 case R_ARM_THM_PC22:
58 case R_ARM_THM_JUMP24:
59 case R_ARM_PREL31:
60 case R_ARM_V4BX:
61 case R_ARM_JUMP_SLOT:
62 return 1;
64 return -1;
67 /* Returns an enumerator to describe whether and when the relocation needs a
68 GOT and/or PLT entry to be created. See tcc.h for a description of the
69 different values. */
70 int gotplt_entry_type (int reloc_type)
72 switch (reloc_type) {
73 case R_ARM_NONE:
74 case R_ARM_COPY:
75 case R_ARM_GLOB_DAT:
76 case R_ARM_JUMP_SLOT:
77 return NO_GOTPLT_ENTRY;
79 case R_ARM_PC24:
80 case R_ARM_CALL:
81 case R_ARM_JUMP24:
82 case R_ARM_PLT32:
83 case R_ARM_THM_PC22:
84 case R_ARM_THM_JUMP24:
85 case R_ARM_MOVT_ABS:
86 case R_ARM_MOVW_ABS_NC:
87 case R_ARM_THM_MOVT_ABS:
88 case R_ARM_THM_MOVW_ABS_NC:
89 case R_ARM_PREL31:
90 case R_ARM_ABS32:
91 case R_ARM_REL32:
92 case R_ARM_V4BX:
93 case R_ARM_TARGET1:
94 case R_ARM_MOVT_PREL:
95 case R_ARM_MOVW_PREL_NC:
96 return AUTO_GOTPLT_ENTRY;
98 case R_ARM_GOTPC:
99 case R_ARM_GOTOFF:
100 return BUILD_GOT_ONLY;
102 case R_ARM_GOT32:
103 case R_ARM_GOT_PREL:
104 return ALWAYS_GOTPLT_ENTRY;
106 return -1;
109 #ifndef TCC_TARGET_PE
110 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
112 Section *plt = s1->plt;
113 uint8_t *p;
114 unsigned plt_offset;
116 /* when building a DLL, GOT entry accesses must be done relative to
117 start of GOT (see x86_64 example above) */
119 /* empty PLT: create PLT0 entry that push address of call site and
120 jump to ld.so resolution routine (GOT + 8) */
121 if (plt->data_offset == 0) {
122 p = section_ptr_add(plt, 20);
123 write32le(p, 0xe52de004); /* push {lr} */
124 write32le(p+4, 0xe59fe004); /* ldr lr, [pc, #4] */
125 write32le(p+8, 0xe08fe00e); /* add lr, pc, lr */
126 write32le(p+12, 0xe5bef008); /* ldr pc, [lr, #8]! */
127 /* p+16 is set in relocate_plt */
129 plt_offset = plt->data_offset;
131 if (attr->plt_thumb_stub) {
132 p = section_ptr_add(plt, 4);
133 write32le(p, 0x4778); /* bx pc */
134 write32le(p+2, 0x46c0); /* nop */
136 p = section_ptr_add(plt, 16);
137 /* save GOT offset for relocate_plt */
138 write32le(p + 4, 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 - 4);
157 p += 20;
158 while (p < p_end) {
159 unsigned off = x + read32le(p + 4) + (s1->plt->data - p) + 4;
160 if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
161 p += 4;
162 write32le(p, 0xe28fc200 | ((off >> 28) & 0xf)); // add ip, pc, #0xN0000000
163 write32le(p + 4, 0xe28cc600 | ((off >> 20) & 0xff)); // add ip, pc, #0xNN00000
164 write32le(p + 8, 0xe28cca00 | ((off >> 12) & 0xff)); // add ip, ip, #0xNN000
165 write32le(p + 12, 0xe5bcf000 | (off & 0xfff)); // ldr pc, [ip, #0xNNN]!
166 p += 16;
170 if (s1->plt->reloc) {
171 ElfW_Rel *rel;
172 p = s1->got->data;
173 for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
174 write32le(p + rel->r_offset, s1->plt->sh_addr);
178 #endif
180 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
182 ElfW(Sym) *sym;
183 int sym_index, esym_index;
185 sym_index = ELFW(R_SYM)(rel->r_info);
186 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
188 switch(type) {
189 case R_ARM_PC24:
190 case R_ARM_CALL:
191 case R_ARM_JUMP24:
192 case R_ARM_PLT32:
194 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
195 x = (*(int *) ptr) & 0xffffff;
196 #ifdef DEBUG_RELOC
197 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
198 #endif
199 (*(int *)ptr) &= 0xff000000;
200 if (x & 0x800000)
201 x -= 0x1000000;
202 x <<= 2;
203 blx_avail = (TCC_CPU_VERSION >= 5);
204 is_thumb = val & 1;
205 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
206 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
207 x += val - addr;
208 #ifdef DEBUG_RELOC
209 printf (" newx=0x%x name=%s\n", x,
210 (char *) symtab_section->link->data + sym->st_name);
211 #endif
212 h = x & 2;
213 th_ko = (x & 3) && (!blx_avail || !is_call);
214 if (th_ko || x >= 0x2000000 || x < -0x2000000)
215 tcc_error("can't relocate value at %x,%d",addr, type);
216 x >>= 2;
217 x &= 0xffffff;
218 /* Only reached if blx is avail and it is a call */
219 if (is_thumb) {
220 x |= h << 24;
221 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
223 (*(int *) ptr) |= x;
225 return;
226 /* Since these relocations only concern Thumb-2 and blx instruction was
227 introduced before Thumb-2, we can assume blx is available and not
228 guard its use */
229 case R_ARM_THM_PC22:
230 case R_ARM_THM_JUMP24:
232 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
233 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
234 Section *plt;
236 /* weak reference */
237 if (sym->st_shndx == SHN_UNDEF &&
238 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
239 return;
241 /* Get initial offset */
242 hi = (*(uint16_t *)ptr);
243 lo = (*(uint16_t *)(ptr+2));
244 s = (hi >> 10) & 1;
245 j1 = (lo >> 13) & 1;
246 j2 = (lo >> 11) & 1;
247 i1 = (j1 ^ s) ^ 1;
248 i2 = (j2 ^ s) ^ 1;
249 imm10 = hi & 0x3ff;
250 imm11 = lo & 0x7ff;
251 x = (s << 24) | (i1 << 23) | (i2 << 22) |
252 (imm10 << 12) | (imm11 << 1);
253 if (x & 0x01000000)
254 x -= 0x02000000;
256 /* Relocation infos */
257 to_thumb = val & 1;
258 plt = s1->plt;
259 to_plt = (val >= plt->sh_addr) &&
260 (val < plt->sh_addr + plt->data_offset);
261 is_call = (type == R_ARM_THM_PC22);
263 if (!to_thumb && !to_plt && !is_call) {
264 int index;
265 uint8_t *p;
266 char *name, buf[1024];
267 Section *text;
269 name = (char *) symtab_section->link->data + sym->st_name;
270 text = s1->sections[sym->st_shndx];
271 /* Modify reloc to target a thumb stub to switch to ARM */
272 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
273 index = put_elf_sym(symtab_section,
274 text->data_offset + 1,
275 sym->st_size, sym->st_info, 0,
276 sym->st_shndx, buf);
277 to_thumb = 1;
278 val = text->data_offset + 1;
279 rel->r_info = ELFW(R_INFO)(index, type);
280 /* Create a thumb stub function to switch to ARM mode */
281 put_elf_reloc(symtab_section, text,
282 text->data_offset + 4, R_ARM_JUMP24,
283 sym_index);
284 p = section_ptr_add(text, 8);
285 write32le(p, 0x4778); /* bx pc */
286 write32le(p+2, 0x46c0); /* nop */
287 write32le(p+4, 0xeafffffe); /* b $sym */
290 /* Compute final offset */
291 x += val - addr;
292 if (!to_thumb && is_call) {
293 blx_bit = 0; /* bl -> blx */
294 x = (x + 3) & -4; /* Compute offset from aligned PC */
297 /* Check that relocation is possible
298 * offset must not be out of range
299 * if target is to be entered in arm mode:
300 - bit 1 must not set
301 - instruction must be a call (bl) or a jump to PLT */
302 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
303 if (to_thumb || (val & 2) || (!is_call && !to_plt))
304 tcc_error("can't relocate value at %x,%d",addr, type);
306 /* Compute and store final offset */
307 s = (x >> 24) & 1;
308 i1 = (x >> 23) & 1;
309 i2 = (x >> 22) & 1;
310 j1 = s ^ (i1 ^ 1);
311 j2 = s ^ (i2 ^ 1);
312 imm10 = (x >> 12) & 0x3ff;
313 imm11 = (x >> 1) & 0x7ff;
314 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
315 (s << 10) | imm10);
316 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
317 (j1 << 13) | blx_bit | (j2 << 11) |
318 imm11);
320 return;
321 case R_ARM_MOVT_ABS:
322 case R_ARM_MOVW_ABS_NC:
324 int x, imm4, imm12;
325 if (type == R_ARM_MOVT_ABS)
326 val >>= 16;
327 imm12 = val & 0xfff;
328 imm4 = (val >> 12) & 0xf;
329 x = (imm4 << 16) | imm12;
330 if (type == R_ARM_THM_MOVT_ABS)
331 *(int *)ptr |= x;
332 else
333 *(int *)ptr += x;
335 return;
336 case R_ARM_MOVT_PREL:
337 case R_ARM_MOVW_PREL_NC:
339 int insn = *(int *)ptr;
340 int addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
342 addend = (addend ^ 0x8000) - 0x8000;
343 val += addend - addr;
344 if (type == R_ARM_MOVT_PREL)
345 val >>= 16;
346 *(int *)ptr = (insn & 0xfff0f000) |
347 ((val & 0xf000) << 4) | (val & 0xfff);
349 return;
350 case R_ARM_THM_MOVT_ABS:
351 case R_ARM_THM_MOVW_ABS_NC:
353 int x, i, imm4, imm3, imm8;
354 if (type == R_ARM_THM_MOVT_ABS)
355 val >>= 16;
356 imm8 = val & 0xff;
357 imm3 = (val >> 8) & 0x7;
358 i = (val >> 11) & 1;
359 imm4 = (val >> 12) & 0xf;
360 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
361 if (type == R_ARM_THM_MOVT_ABS)
362 *(int *)ptr |= x;
363 else
364 *(int *)ptr += x;
366 return;
367 case R_ARM_PREL31:
369 int x;
370 x = (*(int *)ptr) & 0x7fffffff;
371 (*(int *)ptr) &= 0x80000000;
372 x = (x * 2) / 2;
373 x += val - addr;
374 if((x^(x>>1))&0x40000000)
375 tcc_error("can't relocate value at %x,%d",addr, type);
376 (*(int *)ptr) |= x & 0x7fffffff;
378 return;
379 case R_ARM_ABS32:
380 case R_ARM_TARGET1:
381 if (s1->output_type == TCC_OUTPUT_DLL) {
382 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
383 qrel->r_offset = rel->r_offset;
384 if (esym_index) {
385 qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32);
386 qrel++;
387 return;
388 } else {
389 qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE);
390 qrel++;
393 *(int *)ptr += val;
394 return;
395 case R_ARM_REL32:
396 *(int *)ptr += val - addr;
397 return;
398 case R_ARM_GOTPC:
399 *(int *)ptr += s1->got->sh_addr - addr;
400 return;
401 case R_ARM_GOTOFF:
402 *(int *)ptr += val - s1->got->sh_addr;
403 return;
404 case R_ARM_GOT32:
405 /* we load the got offset */
406 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
407 return;
408 case R_ARM_GOT_PREL:
409 /* we load the pc relative got offset */
410 *(int *)ptr += s1->got->sh_addr +
411 get_sym_attr(s1, sym_index, 0)->got_offset -
412 addr;
413 return;
414 case R_ARM_COPY:
415 return;
416 case R_ARM_V4BX:
417 /* trade Thumb support for ARMv4 support */
418 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
419 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
420 return;
421 case R_ARM_GLOB_DAT:
422 case R_ARM_JUMP_SLOT:
423 *(addr_t *)ptr = val;
424 return;
425 case R_ARM_NONE:
426 /* Nothing to do. Normally used to indicate a dependency
427 on a certain symbol (like for exception handling under EABI). */
428 return;
429 case R_ARM_RELATIVE:
430 #ifdef TCC_TARGET_PE
431 add32le(ptr, val - s1->pe_imagebase);
432 #endif
433 /* do nothing */
434 return;
435 default:
436 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
437 type, (unsigned)addr, ptr, (unsigned)val);
438 return;
442 #endif /* !TARGET_DEFS_ONLY */