re-add accidentally deleted printf("\n"); to tests2/97*.c
[tinycc.git] / arm-link.c
blob420639a03e3bb5f466adbb168d1a63e6d54a8c9f
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->got->relocplt) {
171 int mem = s1->output_type == TCC_OUTPUT_MEMORY;
172 ElfW_Rel *rel;
174 p = s1->got->data;
175 for_each_elem(s1->got->relocplt, 0, rel, ElfW_Rel) {
176 int sym_index = ELFW(R_SYM)(rel->r_info);
177 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
178 write32le(p + rel->r_offset, mem ? sym->st_value : s1->plt->sh_addr);
182 #endif
184 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
186 ElfW(Sym) *sym;
187 int sym_index, esym_index;
189 sym_index = ELFW(R_SYM)(rel->r_info);
190 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
192 switch(type) {
193 case R_ARM_PC24:
194 case R_ARM_CALL:
195 case R_ARM_JUMP24:
196 case R_ARM_PLT32:
198 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
199 x = (*(int *) ptr) & 0xffffff;
200 #ifdef DEBUG_RELOC
201 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
202 #endif
203 (*(int *)ptr) &= 0xff000000;
204 if (x & 0x800000)
205 x -= 0x1000000;
206 x <<= 2;
207 blx_avail = (TCC_CPU_VERSION >= 5);
208 is_thumb = val & 1;
209 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
210 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
211 x += val - addr;
212 #ifdef DEBUG_RELOC
213 printf (" newx=0x%x name=%s\n", x,
214 (char *) symtab_section->link->data + sym->st_name);
215 #endif
216 h = x & 2;
217 th_ko = (x & 3) && (!blx_avail || !is_call);
218 if (th_ko || x >= 0x2000000 || x < -0x2000000)
219 tcc_error("can't relocate value at %x,%d",addr, type);
220 x >>= 2;
221 x &= 0xffffff;
222 /* Only reached if blx is avail and it is a call */
223 if (is_thumb) {
224 x |= h << 24;
225 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
227 (*(int *) ptr) |= x;
229 return;
230 /* Since these relocations only concern Thumb-2 and blx instruction was
231 introduced before Thumb-2, we can assume blx is available and not
232 guard its use */
233 case R_ARM_THM_PC22:
234 case R_ARM_THM_JUMP24:
236 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
237 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
238 Section *plt;
240 /* weak reference */
241 if (sym->st_shndx == SHN_UNDEF &&
242 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
243 return;
245 /* Get initial offset */
246 hi = (*(uint16_t *)ptr);
247 lo = (*(uint16_t *)(ptr+2));
248 s = (hi >> 10) & 1;
249 j1 = (lo >> 13) & 1;
250 j2 = (lo >> 11) & 1;
251 i1 = (j1 ^ s) ^ 1;
252 i2 = (j2 ^ s) ^ 1;
253 imm10 = hi & 0x3ff;
254 imm11 = lo & 0x7ff;
255 x = (s << 24) | (i1 << 23) | (i2 << 22) |
256 (imm10 << 12) | (imm11 << 1);
257 if (x & 0x01000000)
258 x -= 0x02000000;
260 /* Relocation infos */
261 to_thumb = val & 1;
262 plt = s1->plt;
263 to_plt = (val >= plt->sh_addr) &&
264 (val < plt->sh_addr + plt->data_offset);
265 is_call = (type == R_ARM_THM_PC22);
267 if (!to_thumb && !to_plt && !is_call) {
268 int index;
269 uint8_t *p;
270 char *name, buf[1024];
271 Section *text;
273 name = (char *) symtab_section->link->data + sym->st_name;
274 text = s1->sections[sym->st_shndx];
275 /* Modify reloc to target a thumb stub to switch to ARM */
276 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
277 index = put_elf_sym(symtab_section,
278 text->data_offset + 1,
279 sym->st_size, sym->st_info, 0,
280 sym->st_shndx, buf);
281 to_thumb = 1;
282 val = text->data_offset + 1;
283 rel->r_info = ELFW(R_INFO)(index, type);
284 /* Create a thumb stub function to switch to ARM mode */
285 put_elf_reloc(symtab_section, text,
286 text->data_offset + 4, R_ARM_JUMP24,
287 sym_index);
288 p = section_ptr_add(text, 8);
289 write32le(p, 0x4778); /* bx pc */
290 write32le(p+2, 0x46c0); /* nop */
291 write32le(p+4, 0xeafffffe); /* b $sym */
294 /* Compute final offset */
295 x += val - addr;
296 if (!to_thumb && is_call) {
297 blx_bit = 0; /* bl -> blx */
298 x = (x + 3) & -4; /* Compute offset from aligned PC */
301 /* Check that relocation is possible
302 * offset must not be out of range
303 * if target is to be entered in arm mode:
304 - bit 1 must not set
305 - instruction must be a call (bl) or a jump to PLT */
306 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
307 if (to_thumb || (val & 2) || (!is_call && !to_plt))
308 tcc_error("can't relocate value at %x,%d",addr, type);
310 /* Compute and store final offset */
311 s = (x >> 24) & 1;
312 i1 = (x >> 23) & 1;
313 i2 = (x >> 22) & 1;
314 j1 = s ^ (i1 ^ 1);
315 j2 = s ^ (i2 ^ 1);
316 imm10 = (x >> 12) & 0x3ff;
317 imm11 = (x >> 1) & 0x7ff;
318 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
319 (s << 10) | imm10);
320 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
321 (j1 << 13) | blx_bit | (j2 << 11) |
322 imm11);
324 return;
325 case R_ARM_MOVT_ABS:
326 case R_ARM_MOVW_ABS_NC:
328 int x, imm4, imm12;
329 if (type == R_ARM_MOVT_ABS)
330 val >>= 16;
331 imm12 = val & 0xfff;
332 imm4 = (val >> 12) & 0xf;
333 x = (imm4 << 16) | imm12;
334 if (type == R_ARM_THM_MOVT_ABS)
335 *(int *)ptr |= x;
336 else
337 *(int *)ptr += x;
339 return;
340 case R_ARM_MOVT_PREL:
341 case R_ARM_MOVW_PREL_NC:
343 int insn = *(int *)ptr;
344 int addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
346 addend = (addend ^ 0x8000) - 0x8000;
347 val += addend - addr;
348 if (type == R_ARM_MOVT_PREL)
349 val >>= 16;
350 *(int *)ptr = (insn & 0xfff0f000) |
351 ((val & 0xf000) << 4) | (val & 0xfff);
353 return;
354 case R_ARM_THM_MOVT_ABS:
355 case R_ARM_THM_MOVW_ABS_NC:
357 int x, i, imm4, imm3, imm8;
358 if (type == R_ARM_THM_MOVT_ABS)
359 val >>= 16;
360 imm8 = val & 0xff;
361 imm3 = (val >> 8) & 0x7;
362 i = (val >> 11) & 1;
363 imm4 = (val >> 12) & 0xf;
364 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
365 if (type == R_ARM_THM_MOVT_ABS)
366 *(int *)ptr |= x;
367 else
368 *(int *)ptr += x;
370 return;
371 case R_ARM_PREL31:
373 int x;
374 x = (*(int *)ptr) & 0x7fffffff;
375 (*(int *)ptr) &= 0x80000000;
376 x = (x * 2) / 2;
377 x += val - addr;
378 if((x^(x>>1))&0x40000000)
379 tcc_error("can't relocate value at %x,%d",addr, type);
380 (*(int *)ptr) |= x & 0x7fffffff;
382 return;
383 case R_ARM_ABS32:
384 case R_ARM_TARGET1:
385 if (s1->output_type == TCC_OUTPUT_DLL) {
386 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
387 qrel->r_offset = rel->r_offset;
388 if (esym_index) {
389 qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32);
390 qrel++;
391 return;
392 } else {
393 qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE);
394 qrel++;
397 *(int *)ptr += val;
398 return;
399 case R_ARM_REL32:
400 *(int *)ptr += val - addr;
401 return;
402 case R_ARM_GOTPC:
403 *(int *)ptr += s1->got->sh_addr - addr;
404 return;
405 case R_ARM_GOTOFF:
406 *(int *)ptr += val - s1->got->sh_addr;
407 return;
408 case R_ARM_GOT32:
409 /* we load the got offset */
410 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
411 return;
412 case R_ARM_GOT_PREL:
413 /* we load the pc relative got offset */
414 *(int *)ptr += s1->got->sh_addr +
415 get_sym_attr(s1, sym_index, 0)->got_offset -
416 addr;
417 return;
418 case R_ARM_COPY:
419 return;
420 case R_ARM_V4BX:
421 /* trade Thumb support for ARMv4 support */
422 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
423 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
424 return;
425 case R_ARM_GLOB_DAT:
426 case R_ARM_JUMP_SLOT:
427 *(addr_t *)ptr = val;
428 return;
429 case R_ARM_NONE:
430 /* Nothing to do. Normally used to indicate a dependency
431 on a certain symbol (like for exception handling under EABI). */
432 return;
433 case R_ARM_RELATIVE:
434 #ifdef TCC_TARGET_PE
435 add32le(ptr, val - s1->pe_imagebase);
436 #endif
437 /* do nothing */
438 return;
439 default:
440 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
441 type, (unsigned)addr, ptr, (unsigned)val);
442 return;
446 #endif /* !TARGET_DEFS_ONLY */