NetBSD: reintroduce NetBSD support. Currently, -run works but executable has an incor...
[tinycc/tcc_android_arm.git] / arm-link.c
blobc75eeb7067ddfb7310916ecd17eb17ca01678ac5
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 - 16);
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 #endif
163 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
165 ElfW(Sym) *sym;
166 int sym_index, esym_index;
168 sym_index = ELFW(R_SYM)(rel->r_info);
169 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
171 switch(type) {
172 case R_ARM_PC24:
173 case R_ARM_CALL:
174 case R_ARM_JUMP24:
175 case R_ARM_PLT32:
177 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
178 x = (*(int *) ptr) & 0xffffff;
179 #ifdef DEBUG_RELOC
180 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
181 #endif
182 (*(int *)ptr) &= 0xff000000;
183 if (x & 0x800000)
184 x -= 0x1000000;
185 x <<= 2;
186 blx_avail = (TCC_CPU_VERSION >= 5);
187 is_thumb = val & 1;
188 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
189 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
190 x += val - addr;
191 #ifdef DEBUG_RELOC
192 printf (" newx=0x%x name=%s\n", x,
193 (char *) symtab_section->link->data + sym->st_name);
194 #endif
195 h = x & 2;
196 th_ko = (x & 3) && (!blx_avail || !is_call);
197 if (th_ko || x >= 0x2000000 || x < -0x2000000)
198 tcc_error("can't relocate value at %x,%d",addr, type);
199 x >>= 2;
200 x &= 0xffffff;
201 /* Only reached if blx is avail and it is a call */
202 if (is_thumb) {
203 x |= h << 24;
204 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
206 (*(int *) ptr) |= x;
208 return;
209 /* Since these relocations only concern Thumb-2 and blx instruction was
210 introduced before Thumb-2, we can assume blx is available and not
211 guard its use */
212 case R_ARM_THM_PC22:
213 case R_ARM_THM_JUMP24:
215 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
216 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
217 Section *plt;
219 /* weak reference */
220 if (sym->st_shndx == SHN_UNDEF &&
221 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
222 return;
224 /* Get initial offset */
225 hi = (*(uint16_t *)ptr);
226 lo = (*(uint16_t *)(ptr+2));
227 s = (hi >> 10) & 1;
228 j1 = (lo >> 13) & 1;
229 j2 = (lo >> 11) & 1;
230 i1 = (j1 ^ s) ^ 1;
231 i2 = (j2 ^ s) ^ 1;
232 imm10 = hi & 0x3ff;
233 imm11 = lo & 0x7ff;
234 x = (s << 24) | (i1 << 23) | (i2 << 22) |
235 (imm10 << 12) | (imm11 << 1);
236 if (x & 0x01000000)
237 x -= 0x02000000;
239 /* Relocation infos */
240 to_thumb = val & 1;
241 plt = s1->plt;
242 to_plt = (val >= plt->sh_addr) &&
243 (val < plt->sh_addr + plt->data_offset);
244 is_call = (type == R_ARM_THM_PC22);
246 if (!to_thumb && !to_plt && !is_call) {
247 int index;
248 uint8_t *p;
249 char *name, buf[1024];
250 Section *text;
252 name = (char *) symtab_section->link->data + sym->st_name;
253 text = s1->sections[sym->st_shndx];
254 /* Modify reloc to target a thumb stub to switch to ARM */
255 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
256 index = put_elf_sym(symtab_section,
257 text->data_offset + 1,
258 sym->st_size, sym->st_info, 0,
259 sym->st_shndx, buf);
260 to_thumb = 1;
261 val = text->data_offset + 1;
262 rel->r_info = ELFW(R_INFO)(index, type);
263 /* Create a thumb stub function to switch to ARM mode */
264 put_elf_reloc(symtab_section, text,
265 text->data_offset + 4, R_ARM_JUMP24,
266 sym_index);
267 p = section_ptr_add(text, 8);
268 write32le(p, 0x4778); /* bx pc */
269 write32le(p+2, 0x46c0); /* nop */
270 write32le(p+4, 0xeafffffe); /* b $sym */
273 /* Compute final offset */
274 x += val - addr;
275 if (!to_thumb && is_call) {
276 blx_bit = 0; /* bl -> blx */
277 x = (x + 3) & -4; /* Compute offset from aligned PC */
280 /* Check that relocation is possible
281 * offset must not be out of range
282 * if target is to be entered in arm mode:
283 - bit 1 must not set
284 - instruction must be a call (bl) or a jump to PLT */
285 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
286 if (to_thumb || (val & 2) || (!is_call && !to_plt))
287 tcc_error("can't relocate value at %x,%d",addr, type);
289 /* Compute and store final offset */
290 s = (x >> 24) & 1;
291 i1 = (x >> 23) & 1;
292 i2 = (x >> 22) & 1;
293 j1 = s ^ (i1 ^ 1);
294 j2 = s ^ (i2 ^ 1);
295 imm10 = (x >> 12) & 0x3ff;
296 imm11 = (x >> 1) & 0x7ff;
297 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
298 (s << 10) | imm10);
299 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
300 (j1 << 13) | blx_bit | (j2 << 11) |
301 imm11);
303 return;
304 case R_ARM_MOVT_ABS:
305 case R_ARM_MOVW_ABS_NC:
307 int x, imm4, imm12;
308 if (type == R_ARM_MOVT_ABS)
309 val >>= 16;
310 imm12 = val & 0xfff;
311 imm4 = (val >> 12) & 0xf;
312 x = (imm4 << 16) | imm12;
313 if (type == R_ARM_THM_MOVT_ABS)
314 *(int *)ptr |= x;
315 else
316 *(int *)ptr += x;
318 return;
319 case R_ARM_THM_MOVT_ABS:
320 case R_ARM_THM_MOVW_ABS_NC:
322 int x, i, imm4, imm3, imm8;
323 if (type == R_ARM_THM_MOVT_ABS)
324 val >>= 16;
325 imm8 = val & 0xff;
326 imm3 = (val >> 8) & 0x7;
327 i = (val >> 11) & 1;
328 imm4 = (val >> 12) & 0xf;
329 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
330 if (type == R_ARM_THM_MOVT_ABS)
331 *(int *)ptr |= x;
332 else
333 *(int *)ptr += x;
335 return;
336 case R_ARM_PREL31:
338 int x;
339 x = (*(int *)ptr) & 0x7fffffff;
340 (*(int *)ptr) &= 0x80000000;
341 x = (x * 2) / 2;
342 x += val - addr;
343 if((x^(x>>1))&0x40000000)
344 tcc_error("can't relocate value at %x,%d",addr, type);
345 (*(int *)ptr) |= x & 0x7fffffff;
347 case R_ARM_ABS32:
348 if (s1->output_type == TCC_OUTPUT_DLL) {
349 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
350 qrel->r_offset = rel->r_offset;
351 if (esym_index) {
352 qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32);
353 qrel++;
354 return;
355 } else {
356 qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE);
357 qrel++;
360 *(int *)ptr += val;
361 return;
362 case R_ARM_REL32:
363 *(int *)ptr += val - addr;
364 return;
365 case R_ARM_GOTPC:
366 *(int *)ptr += s1->got->sh_addr - addr;
367 return;
368 case R_ARM_GOTOFF:
369 *(int *)ptr += val - s1->got->sh_addr;
370 return;
371 case R_ARM_GOT32:
372 /* we load the got offset */
373 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
374 return;
375 case R_ARM_COPY:
376 return;
377 case R_ARM_V4BX:
378 /* trade Thumb support for ARMv4 support */
379 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
380 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
381 return;
382 case R_ARM_GLOB_DAT:
383 case R_ARM_JUMP_SLOT:
384 *(addr_t *)ptr = val;
385 return;
386 case R_ARM_NONE:
387 /* Nothing to do. Normally used to indicate a dependency
388 on a certain symbol (like for exception handling under EABI). */
389 return;
390 case R_ARM_RELATIVE:
391 #ifdef TCC_TARGET_PE
392 add32le(ptr, val - s1->pe_imagebase);
393 #endif
394 /* do nothing */
395 return;
396 default:
397 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
398 type, (unsigned)addr, ptr, (unsigned)val);
399 return;
403 #endif /* !TARGET_DEFS_ONLY */