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