include/tccdefs.h: moved and use it
[tinycc.git] / arm-link.c
blob207fc61a9d14b6579d7486db28c51198cca0578a
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 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_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 #ifndef TCC_TARGET_PE
102 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
104 Section *plt = s1->plt;
105 uint8_t *p;
106 unsigned plt_offset;
108 /* when building a DLL, GOT entry accesses must be done relative to
109 start of GOT (see x86_64 example above) */
111 /* empty PLT: create PLT0 entry that push address of call site and
112 jump to ld.so resolution routine (GOT + 8) */
113 if (plt->data_offset == 0) {
114 p = section_ptr_add(plt, 20);
115 write32le(p, 0xe52de004); /* push {lr} */
116 write32le(p+4, 0xe59fe004); /* ldr lr, [pc, #4] */
117 write32le(p+8, 0xe08fe00e); /* add lr, pc, lr */
118 write32le(p+12, 0xe5bef008); /* ldr pc, [lr, #8]! */
119 /* p+16 is set in relocate_plt */
121 plt_offset = plt->data_offset;
123 if (attr->plt_thumb_stub) {
124 p = section_ptr_add(plt, 4);
125 write32le(p, 0x4778); /* bx pc */
126 write32le(p+2, 0x46c0); /* nop */
128 p = section_ptr_add(plt, 12);
129 /* save GOT offset for relocate_plt */
130 write32le(p + 4, got_offset);
131 return plt_offset;
134 /* relocate the PLT: compute addresses and offsets in the PLT now that final
135 address for PLT and GOT are known (see fill_program_header) */
136 ST_FUNC void relocate_plt(TCCState *s1)
138 uint8_t *p, *p_end;
140 if (!s1->plt)
141 return;
143 p = s1->plt->data;
144 p_end = p + s1->plt->data_offset;
146 if (p < p_end) {
147 int x = s1->got->sh_addr - s1->plt->sh_addr - 12;
148 write32le(s1->plt->data + 16, x - 4);
149 p += 20;
150 while (p < p_end) {
151 unsigned off = x + read32le(p + 4) + (s1->plt->data - p) + 4;
152 if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
153 p += 4;
154 write32le(p, 0xe28fc600 | ((off >> 20) & 0xff)); // add ip, pc, #0xNN00000
155 write32le(p + 4, 0xe28cca00 | ((off >> 12) & 0xff)); // add ip, ip, #0xNN000
156 write32le(p + 8, 0xe5bcf000 | (off & 0xfff)); // ldr pc, [ip, #0xNNN]!
157 p += 12;
161 if (s1->got->relocplt) {
162 int mem = s1->output_type == TCC_OUTPUT_MEMORY;
163 ElfW_Rel *rel;
165 p = s1->got->data;
166 for_each_elem(s1->got->relocplt, 0, rel, ElfW_Rel) {
167 int sym_index = ELFW(R_SYM)(rel->r_info);
168 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
169 write32le(p + rel->r_offset, mem ? sym->st_value : s1->plt->sh_addr);
173 #endif
175 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
177 ElfW(Sym) *sym;
178 int sym_index, esym_index;
180 sym_index = ELFW(R_SYM)(rel->r_info);
181 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
183 switch(type) {
184 case R_ARM_PC24:
185 case R_ARM_CALL:
186 case R_ARM_JUMP24:
187 case R_ARM_PLT32:
189 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
190 x = (*(int *) ptr) & 0xffffff;
191 #ifdef DEBUG_RELOC
192 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
193 #endif
194 (*(int *)ptr) &= 0xff000000;
195 if (x & 0x800000)
196 x -= 0x1000000;
197 x <<= 2;
198 blx_avail = (TCC_CPU_VERSION >= 5);
199 is_thumb = val & 1;
200 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
201 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
202 x += val - addr;
203 #ifdef DEBUG_RELOC
204 printf (" newx=0x%x name=%s\n", x,
205 (char *) symtab_section->link->data + sym->st_name);
206 #endif
207 h = x & 2;
208 th_ko = (x & 3) && (!blx_avail || !is_call);
209 if (th_ko || x >= 0x2000000 || x < -0x2000000)
210 tcc_error("can't relocate value at %x,%d",addr, type);
211 x >>= 2;
212 x &= 0xffffff;
213 /* Only reached if blx is avail and it is a call */
214 if (is_thumb) {
215 x |= h << 24;
216 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
218 (*(int *) ptr) |= x;
220 return;
221 /* Since these relocations only concern Thumb-2 and blx instruction was
222 introduced before Thumb-2, we can assume blx is available and not
223 guard its use */
224 case R_ARM_THM_PC22:
225 case R_ARM_THM_JUMP24:
227 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
228 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
229 Section *plt;
231 /* weak reference */
232 if (sym->st_shndx == SHN_UNDEF &&
233 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
234 return;
236 /* Get initial offset */
237 hi = (*(uint16_t *)ptr);
238 lo = (*(uint16_t *)(ptr+2));
239 s = (hi >> 10) & 1;
240 j1 = (lo >> 13) & 1;
241 j2 = (lo >> 11) & 1;
242 i1 = (j1 ^ s) ^ 1;
243 i2 = (j2 ^ s) ^ 1;
244 imm10 = hi & 0x3ff;
245 imm11 = lo & 0x7ff;
246 x = (s << 24) | (i1 << 23) | (i2 << 22) |
247 (imm10 << 12) | (imm11 << 1);
248 if (x & 0x01000000)
249 x -= 0x02000000;
251 /* Relocation infos */
252 to_thumb = val & 1;
253 plt = s1->plt;
254 to_plt = (val >= plt->sh_addr) &&
255 (val < plt->sh_addr + plt->data_offset);
256 is_call = (type == R_ARM_THM_PC22);
258 if (!to_thumb && !to_plt && !is_call) {
259 int index;
260 uint8_t *p;
261 char *name, buf[1024];
262 Section *text;
264 name = (char *) symtab_section->link->data + sym->st_name;
265 text = s1->sections[sym->st_shndx];
266 /* Modify reloc to target a thumb stub to switch to ARM */
267 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
268 index = put_elf_sym(symtab_section,
269 text->data_offset + 1,
270 sym->st_size, sym->st_info, 0,
271 sym->st_shndx, buf);
272 to_thumb = 1;
273 val = text->data_offset + 1;
274 rel->r_info = ELFW(R_INFO)(index, type);
275 /* Create a thumb stub function to switch to ARM mode */
276 put_elf_reloc(symtab_section, text,
277 text->data_offset + 4, R_ARM_JUMP24,
278 sym_index);
279 p = section_ptr_add(text, 8);
280 write32le(p, 0x4778); /* bx pc */
281 write32le(p+2, 0x46c0); /* nop */
282 write32le(p+4, 0xeafffffe); /* b $sym */
285 /* Compute final offset */
286 x += val - addr;
287 if (!to_thumb && is_call) {
288 blx_bit = 0; /* bl -> blx */
289 x = (x + 3) & -4; /* Compute offset from aligned PC */
292 /* Check that relocation is possible
293 * offset must not be out of range
294 * if target is to be entered in arm mode:
295 - bit 1 must not set
296 - instruction must be a call (bl) or a jump to PLT */
297 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
298 if (to_thumb || (val & 2) || (!is_call && !to_plt))
299 tcc_error("can't relocate value at %x,%d",addr, type);
301 /* Compute and store final offset */
302 s = (x >> 24) & 1;
303 i1 = (x >> 23) & 1;
304 i2 = (x >> 22) & 1;
305 j1 = s ^ (i1 ^ 1);
306 j2 = s ^ (i2 ^ 1);
307 imm10 = (x >> 12) & 0x3ff;
308 imm11 = (x >> 1) & 0x7ff;
309 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
310 (s << 10) | imm10);
311 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
312 (j1 << 13) | blx_bit | (j2 << 11) |
313 imm11);
315 return;
316 case R_ARM_MOVT_ABS:
317 case R_ARM_MOVW_ABS_NC:
319 int x, imm4, imm12;
320 if (type == R_ARM_MOVT_ABS)
321 val >>= 16;
322 imm12 = val & 0xfff;
323 imm4 = (val >> 12) & 0xf;
324 x = (imm4 << 16) | imm12;
325 if (type == R_ARM_THM_MOVT_ABS)
326 *(int *)ptr |= x;
327 else
328 *(int *)ptr += x;
330 return;
331 case R_ARM_THM_MOVT_ABS:
332 case R_ARM_THM_MOVW_ABS_NC:
334 int x, i, imm4, imm3, imm8;
335 if (type == R_ARM_THM_MOVT_ABS)
336 val >>= 16;
337 imm8 = val & 0xff;
338 imm3 = (val >> 8) & 0x7;
339 i = (val >> 11) & 1;
340 imm4 = (val >> 12) & 0xf;
341 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
342 if (type == R_ARM_THM_MOVT_ABS)
343 *(int *)ptr |= x;
344 else
345 *(int *)ptr += x;
347 return;
348 case R_ARM_PREL31:
350 int x;
351 x = (*(int *)ptr) & 0x7fffffff;
352 (*(int *)ptr) &= 0x80000000;
353 x = (x * 2) / 2;
354 x += val - addr;
355 if((x^(x>>1))&0x40000000)
356 tcc_error("can't relocate value at %x,%d",addr, type);
357 (*(int *)ptr) |= x & 0x7fffffff;
359 case R_ARM_ABS32:
360 if (s1->output_type == TCC_OUTPUT_DLL) {
361 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
362 qrel->r_offset = rel->r_offset;
363 if (esym_index) {
364 qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32);
365 qrel++;
366 return;
367 } else {
368 qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE);
369 qrel++;
372 *(int *)ptr += val;
373 return;
374 case R_ARM_REL32:
375 *(int *)ptr += val - addr;
376 return;
377 case R_ARM_GOTPC:
378 *(int *)ptr += s1->got->sh_addr - addr;
379 return;
380 case R_ARM_GOTOFF:
381 *(int *)ptr += val - s1->got->sh_addr;
382 return;
383 case R_ARM_GOT32:
384 /* we load the got offset */
385 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
386 return;
387 case R_ARM_COPY:
388 return;
389 case R_ARM_V4BX:
390 /* trade Thumb support for ARMv4 support */
391 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
392 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
393 return;
394 case R_ARM_GLOB_DAT:
395 case R_ARM_JUMP_SLOT:
396 *(addr_t *)ptr = val;
397 return;
398 case R_ARM_NONE:
399 /* Nothing to do. Normally used to indicate a dependency
400 on a certain symbol (like for exception handling under EABI). */
401 return;
402 case R_ARM_RELATIVE:
403 #ifdef TCC_TARGET_PE
404 add32le(ptr, val - s1->pe_imagebase);
405 #endif
406 /* do nothing */
407 return;
408 default:
409 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
410 type, (unsigned)addr, ptr, (unsigned)val);
411 return;
415 #endif /* !TARGET_DEFS_ONLY */