OpenBSD: use portable strtoll instead of strtonum to allow cross-compilation test
[tinycc.git] / arm-link.c
blobbad4e8b914159c9b643a638cdebbbeb77e5f4ad2
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_GOT_PREL:
45 case R_ARM_COPY:
46 case R_ARM_GLOB_DAT:
47 case R_ARM_NONE:
48 case R_ARM_TARGET1:
49 return 0;
51 case R_ARM_PC24:
52 case R_ARM_CALL:
53 case R_ARM_JUMP24:
54 case R_ARM_PLT32:
55 case R_ARM_THM_PC22:
56 case R_ARM_THM_JUMP24:
57 case R_ARM_PREL31:
58 case R_ARM_V4BX:
59 case R_ARM_JUMP_SLOT:
60 return 1;
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 case R_ARM_TARGET1:
92 return AUTO_GOTPLT_ENTRY;
94 case R_ARM_GOTPC:
95 case R_ARM_GOTOFF:
96 return BUILD_GOT_ONLY;
98 case R_ARM_GOT32:
99 case R_ARM_GOT_PREL:
100 return ALWAYS_GOTPLT_ENTRY;
102 return -1;
105 #ifndef TCC_TARGET_PE
106 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
108 Section *plt = s1->plt;
109 uint8_t *p;
110 unsigned plt_offset;
112 /* when building a DLL, GOT entry accesses must be done relative to
113 start of GOT (see x86_64 example above) */
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, 12);
133 /* save GOT offset for relocate_plt */
134 write32le(p + 4, got_offset);
135 return plt_offset;
138 /* relocate the PLT: compute addresses and offsets in the PLT now that final
139 address for PLT and GOT are known (see fill_program_header) */
140 ST_FUNC void relocate_plt(TCCState *s1)
142 uint8_t *p, *p_end;
144 if (!s1->plt)
145 return;
147 p = s1->plt->data;
148 p_end = p + s1->plt->data_offset;
150 if (p < p_end) {
151 int x = s1->got->sh_addr - s1->plt->sh_addr - 12;
152 write32le(s1->plt->data + 16, x - 4);
153 p += 20;
154 while (p < p_end) {
155 unsigned off = x + read32le(p + 4) + (s1->plt->data - p) + 4;
156 if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
157 p += 4;
158 write32le(p, 0xe28fc600 | ((off >> 20) & 0xff)); // add ip, pc, #0xNN00000
159 write32le(p + 4, 0xe28cca00 | ((off >> 12) & 0xff)); // add ip, ip, #0xNN000
160 write32le(p + 8, 0xe5bcf000 | (off & 0xfff)); // ldr pc, [ip, #0xNNN]!
161 p += 12;
165 if (s1->got->relocplt) {
166 int mem = s1->output_type == TCC_OUTPUT_MEMORY;
167 ElfW_Rel *rel;
169 p = s1->got->data;
170 for_each_elem(s1->got->relocplt, 0, rel, ElfW_Rel) {
171 int sym_index = ELFW(R_SYM)(rel->r_info);
172 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
173 write32le(p + rel->r_offset, mem ? sym->st_value : s1->plt->sh_addr);
177 #endif
179 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
181 ElfW(Sym) *sym;
182 int sym_index, esym_index;
184 sym_index = ELFW(R_SYM)(rel->r_info);
185 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
187 switch(type) {
188 case R_ARM_PC24:
189 case R_ARM_CALL:
190 case R_ARM_JUMP24:
191 case R_ARM_PLT32:
193 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
194 x = (*(int *) ptr) & 0xffffff;
195 #ifdef DEBUG_RELOC
196 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
197 #endif
198 (*(int *)ptr) &= 0xff000000;
199 if (x & 0x800000)
200 x -= 0x1000000;
201 x <<= 2;
202 blx_avail = (TCC_CPU_VERSION >= 5);
203 is_thumb = val & 1;
204 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
205 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
206 x += val - addr;
207 #ifdef DEBUG_RELOC
208 printf (" newx=0x%x name=%s\n", x,
209 (char *) symtab_section->link->data + sym->st_name);
210 #endif
211 h = x & 2;
212 th_ko = (x & 3) && (!blx_avail || !is_call);
213 if (th_ko || x >= 0x2000000 || x < -0x2000000)
214 tcc_error("can't relocate value at %x,%d",addr, type);
215 x >>= 2;
216 x &= 0xffffff;
217 /* Only reached if blx is avail and it is a call */
218 if (is_thumb) {
219 x |= h << 24;
220 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
222 (*(int *) ptr) |= x;
224 return;
225 /* Since these relocations only concern Thumb-2 and blx instruction was
226 introduced before Thumb-2, we can assume blx is available and not
227 guard its use */
228 case R_ARM_THM_PC22:
229 case R_ARM_THM_JUMP24:
231 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
232 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
233 Section *plt;
235 /* weak reference */
236 if (sym->st_shndx == SHN_UNDEF &&
237 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
238 return;
240 /* Get initial offset */
241 hi = (*(uint16_t *)ptr);
242 lo = (*(uint16_t *)(ptr+2));
243 s = (hi >> 10) & 1;
244 j1 = (lo >> 13) & 1;
245 j2 = (lo >> 11) & 1;
246 i1 = (j1 ^ s) ^ 1;
247 i2 = (j2 ^ s) ^ 1;
248 imm10 = hi & 0x3ff;
249 imm11 = lo & 0x7ff;
250 x = (s << 24) | (i1 << 23) | (i2 << 22) |
251 (imm10 << 12) | (imm11 << 1);
252 if (x & 0x01000000)
253 x -= 0x02000000;
255 /* Relocation infos */
256 to_thumb = val & 1;
257 plt = s1->plt;
258 to_plt = (val >= plt->sh_addr) &&
259 (val < plt->sh_addr + plt->data_offset);
260 is_call = (type == R_ARM_THM_PC22);
262 if (!to_thumb && !to_plt && !is_call) {
263 int index;
264 uint8_t *p;
265 char *name, buf[1024];
266 Section *text;
268 name = (char *) symtab_section->link->data + sym->st_name;
269 text = s1->sections[sym->st_shndx];
270 /* Modify reloc to target a thumb stub to switch to ARM */
271 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
272 index = put_elf_sym(symtab_section,
273 text->data_offset + 1,
274 sym->st_size, sym->st_info, 0,
275 sym->st_shndx, buf);
276 to_thumb = 1;
277 val = text->data_offset + 1;
278 rel->r_info = ELFW(R_INFO)(index, type);
279 /* Create a thumb stub function to switch to ARM mode */
280 put_elf_reloc(symtab_section, text,
281 text->data_offset + 4, R_ARM_JUMP24,
282 sym_index);
283 p = section_ptr_add(text, 8);
284 write32le(p, 0x4778); /* bx pc */
285 write32le(p+2, 0x46c0); /* nop */
286 write32le(p+4, 0xeafffffe); /* b $sym */
289 /* Compute final offset */
290 x += val - addr;
291 if (!to_thumb && is_call) {
292 blx_bit = 0; /* bl -> blx */
293 x = (x + 3) & -4; /* Compute offset from aligned PC */
296 /* Check that relocation is possible
297 * offset must not be out of range
298 * if target is to be entered in arm mode:
299 - bit 1 must not set
300 - instruction must be a call (bl) or a jump to PLT */
301 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
302 if (to_thumb || (val & 2) || (!is_call && !to_plt))
303 tcc_error("can't relocate value at %x,%d",addr, type);
305 /* Compute and store final offset */
306 s = (x >> 24) & 1;
307 i1 = (x >> 23) & 1;
308 i2 = (x >> 22) & 1;
309 j1 = s ^ (i1 ^ 1);
310 j2 = s ^ (i2 ^ 1);
311 imm10 = (x >> 12) & 0x3ff;
312 imm11 = (x >> 1) & 0x7ff;
313 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
314 (s << 10) | imm10);
315 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
316 (j1 << 13) | blx_bit | (j2 << 11) |
317 imm11);
319 return;
320 case R_ARM_MOVT_ABS:
321 case R_ARM_MOVW_ABS_NC:
323 int x, imm4, imm12;
324 if (type == R_ARM_MOVT_ABS)
325 val >>= 16;
326 imm12 = val & 0xfff;
327 imm4 = (val >> 12) & 0xf;
328 x = (imm4 << 16) | imm12;
329 if (type == R_ARM_THM_MOVT_ABS)
330 *(int *)ptr |= x;
331 else
332 *(int *)ptr += x;
334 return;
335 case R_ARM_THM_MOVT_ABS:
336 case R_ARM_THM_MOVW_ABS_NC:
338 int x, i, imm4, imm3, imm8;
339 if (type == R_ARM_THM_MOVT_ABS)
340 val >>= 16;
341 imm8 = val & 0xff;
342 imm3 = (val >> 8) & 0x7;
343 i = (val >> 11) & 1;
344 imm4 = (val >> 12) & 0xf;
345 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
346 if (type == R_ARM_THM_MOVT_ABS)
347 *(int *)ptr |= x;
348 else
349 *(int *)ptr += x;
351 return;
352 case R_ARM_PREL31:
354 int x;
355 x = (*(int *)ptr) & 0x7fffffff;
356 (*(int *)ptr) &= 0x80000000;
357 x = (x * 2) / 2;
358 x += val - addr;
359 if((x^(x>>1))&0x40000000)
360 tcc_error("can't relocate value at %x,%d",addr, type);
361 (*(int *)ptr) |= x & 0x7fffffff;
363 case R_ARM_ABS32:
364 // case R_ARM_TARGET1: /* ??? as seen on NetBSD - FIXME! */
365 if (s1->output_type == TCC_OUTPUT_DLL) {
366 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
367 qrel->r_offset = rel->r_offset;
368 if (esym_index) {
369 qrel->r_info = ELFW(R_INFO)(esym_index, type);
370 qrel++;
371 return;
372 } else {
373 qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE);
374 qrel++;
377 *(int *)ptr += val;
378 return;
379 case R_ARM_REL32:
380 *(int *)ptr += val - addr;
381 return;
382 case R_ARM_GOTPC:
383 *(int *)ptr += s1->got->sh_addr - addr;
384 return;
385 case R_ARM_GOTOFF:
386 *(int *)ptr += val - s1->got->sh_addr;
387 return;
388 case R_ARM_GOT32:
389 /* we load the got offset */
390 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
391 return;
392 case R_ARM_GOT_PREL:
393 /* we load the pc relative got offset */
394 *(int *)ptr += s1->got->sh_addr +
395 get_sym_attr(s1, sym_index, 0)->got_offset -
396 addr;
397 return;
398 case R_ARM_COPY:
399 return;
400 case R_ARM_V4BX:
401 /* trade Thumb support for ARMv4 support */
402 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
403 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
404 return;
405 case R_ARM_GLOB_DAT:
406 case R_ARM_JUMP_SLOT:
407 *(addr_t *)ptr = val;
408 return;
409 case R_ARM_NONE:
410 /* Nothing to do. Normally used to indicate a dependency
411 on a certain symbol (like for exception handling under EABI). */
412 return;
413 case R_ARM_RELATIVE:
414 #ifdef TCC_TARGET_PE
415 add32le(ptr, val - s1->pe_imagebase);
416 #endif
417 /* do nothing */
418 return;
419 default:
420 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
421 type, (unsigned)addr, ptr, (unsigned)val);
422 return;
426 #endif /* !TARGET_DEFS_ONLY */