some cleanups related to recent commits
[tinycc.git] / arm-link.c
blob9227b22941761397cb2a3958cbeaa88f76e4a311
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 0
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) */
110 if (s1->output_type == TCC_OUTPUT_DLL)
111 tcc_error("DLLs unimplemented!");
113 /* empty PLT: create PLT0 entry that push address of call site and
114 jump to ld.so resolution routine (GOT + 8) */
115 if (plt->data_offset == 0) {
116 p = section_ptr_add(plt, 20);
117 write32le(p, 0xe52de004); /* push {lr} */
118 write32le(p+4, 0xe59fe004); /* ldr lr, [pc, #4] */
119 write32le(p+8, 0xe08fe00e); /* add lr, pc, lr */
120 write32le(p+12, 0xe5bef008); /* ldr pc, [lr, #8]! */
121 /* p+16 is set in relocate_plt */
123 plt_offset = plt->data_offset;
125 if (attr->plt_thumb_stub) {
126 p = section_ptr_add(plt, 4);
127 write32le(p, 0x4778); /* bx pc */
128 write32le(p+2, 0x46c0); /* nop */
130 p = section_ptr_add(plt, 16);
131 /* Jump to GOT entry where ld.so initially put address of PLT0 */
132 write32le(p, 0xe59fc004); /* ldr ip, [pc, #4] */
133 write32le(p+4, 0xe08fc00c); /* add ip, pc, ip */
134 write32le(p+8, 0xe59cf000); /* ldr pc, [ip] */
135 /* p + 12 contains offset to GOT entry once patched by relocate_plt */
136 write32le(p+12, got_offset);
137 return plt_offset;
140 /* relocate the PLT: compute addresses and offsets in the PLT now that final
141 address for PLT and GOT are known (see fill_program_header) */
142 ST_FUNC void relocate_plt(TCCState *s1)
144 uint8_t *p, *p_end;
146 if (!s1->plt)
147 return;
149 p = s1->plt->data;
150 p_end = p + s1->plt->data_offset;
152 if (p < p_end) {
153 int x = s1->got->sh_addr - s1->plt->sh_addr - 12;
154 write32le(s1->plt->data + 16, x - 16);
155 p += 20;
156 while (p < p_end) {
157 if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
158 p += 4;
159 add32le(p + 12, x + (s1->plt->data - p));
160 p += 16;
164 #endif
166 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
168 ElfW(Sym) *sym;
169 int sym_index;
171 sym_index = ELFW(R_SYM)(rel->r_info);
172 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
174 switch(type) {
175 case R_ARM_PC24:
176 case R_ARM_CALL:
177 case R_ARM_JUMP24:
178 case R_ARM_PLT32:
180 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
181 x = (*(int *) ptr) & 0xffffff;
182 #ifdef DEBUG_RELOC
183 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
184 #endif
185 (*(int *)ptr) &= 0xff000000;
186 if (x & 0x800000)
187 x -= 0x1000000;
188 x <<= 2;
189 blx_avail = (TCC_CPU_VERSION >= 5);
190 is_thumb = val & 1;
191 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
192 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
193 x += val - addr;
194 #ifdef DEBUG_RELOC
195 printf (" newx=0x%x name=%s\n", x,
196 (char *) symtab_section->link->data + sym->st_name);
197 #endif
198 h = x & 2;
199 th_ko = (x & 3) && (!blx_avail || !is_call);
200 if (th_ko || x >= 0x2000000 || x < -0x2000000)
201 tcc_error("can't relocate value at %x,%d",addr, type);
202 x >>= 2;
203 x &= 0xffffff;
204 /* Only reached if blx is avail and it is a call */
205 if (is_thumb) {
206 x |= h << 24;
207 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
209 (*(int *) ptr) |= x;
211 return;
212 /* Since these relocations only concern Thumb-2 and blx instruction was
213 introduced before Thumb-2, we can assume blx is available and not
214 guard its use */
215 case R_ARM_THM_PC22:
216 case R_ARM_THM_JUMP24:
218 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
219 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
220 Section *plt;
222 /* weak reference */
223 if (sym->st_shndx == SHN_UNDEF &&
224 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
225 return;
227 /* Get initial offset */
228 hi = (*(uint16_t *)ptr);
229 lo = (*(uint16_t *)(ptr+2));
230 s = (hi >> 10) & 1;
231 j1 = (lo >> 13) & 1;
232 j2 = (lo >> 11) & 1;
233 i1 = (j1 ^ s) ^ 1;
234 i2 = (j2 ^ s) ^ 1;
235 imm10 = hi & 0x3ff;
236 imm11 = lo & 0x7ff;
237 x = (s << 24) | (i1 << 23) | (i2 << 22) |
238 (imm10 << 12) | (imm11 << 1);
239 if (x & 0x01000000)
240 x -= 0x02000000;
242 /* Relocation infos */
243 to_thumb = val & 1;
244 plt = s1->plt;
245 to_plt = (val >= plt->sh_addr) &&
246 (val < plt->sh_addr + plt->data_offset);
247 is_call = (type == R_ARM_THM_PC22);
249 if (!to_thumb && !to_plt && !is_call) {
250 int index;
251 uint8_t *p;
252 char *name, buf[1024];
253 Section *text;
255 name = (char *) symtab_section->link->data + sym->st_name;
256 text = s1->sections[sym->st_shndx];
257 /* Modify reloc to target a thumb stub to switch to ARM */
258 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
259 index = put_elf_sym(symtab_section,
260 text->data_offset + 1,
261 sym->st_size, sym->st_info, 0,
262 sym->st_shndx, buf);
263 to_thumb = 1;
264 val = text->data_offset + 1;
265 rel->r_info = ELFW(R_INFO)(index, type);
266 /* Create a thumb stub function to switch to ARM mode */
267 put_elf_reloc(symtab_section, text,
268 text->data_offset + 4, R_ARM_JUMP24,
269 sym_index);
270 p = section_ptr_add(text, 8);
271 write32le(p, 0x4778); /* bx pc */
272 write32le(p+2, 0x46c0); /* nop */
273 write32le(p+4, 0xeafffffe); /* b $sym */
276 /* Compute final offset */
277 x += val - addr;
278 if (!to_thumb && is_call) {
279 blx_bit = 0; /* bl -> blx */
280 x = (x + 3) & -4; /* Compute offset from aligned PC */
283 /* Check that relocation is possible
284 * offset must not be out of range
285 * if target is to be entered in arm mode:
286 - bit 1 must not set
287 - instruction must be a call (bl) or a jump to PLT */
288 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
289 if (to_thumb || (val & 2) || (!is_call && !to_plt))
290 tcc_error("can't relocate value at %x,%d",addr, type);
292 /* Compute and store final offset */
293 s = (x >> 24) & 1;
294 i1 = (x >> 23) & 1;
295 i2 = (x >> 22) & 1;
296 j1 = s ^ (i1 ^ 1);
297 j2 = s ^ (i2 ^ 1);
298 imm10 = (x >> 12) & 0x3ff;
299 imm11 = (x >> 1) & 0x7ff;
300 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
301 (s << 10) | imm10);
302 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
303 (j1 << 13) | blx_bit | (j2 << 11) |
304 imm11);
306 return;
307 case R_ARM_MOVT_ABS:
308 case R_ARM_MOVW_ABS_NC:
310 int x, imm4, imm12;
311 if (type == R_ARM_MOVT_ABS)
312 val >>= 16;
313 imm12 = val & 0xfff;
314 imm4 = (val >> 12) & 0xf;
315 x = (imm4 << 16) | imm12;
316 if (type == R_ARM_THM_MOVT_ABS)
317 *(int *)ptr |= x;
318 else
319 *(int *)ptr += x;
321 return;
322 case R_ARM_THM_MOVT_ABS:
323 case R_ARM_THM_MOVW_ABS_NC:
325 int x, i, imm4, imm3, imm8;
326 if (type == R_ARM_THM_MOVT_ABS)
327 val >>= 16;
328 imm8 = val & 0xff;
329 imm3 = (val >> 8) & 0x7;
330 i = (val >> 11) & 1;
331 imm4 = (val >> 12) & 0xf;
332 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
333 if (type == R_ARM_THM_MOVT_ABS)
334 *(int *)ptr |= x;
335 else
336 *(int *)ptr += x;
338 return;
339 case R_ARM_PREL31:
341 int x;
342 x = (*(int *)ptr) & 0x7fffffff;
343 (*(int *)ptr) &= 0x80000000;
344 x = (x * 2) / 2;
345 x += val - addr;
346 if((x^(x>>1))&0x40000000)
347 tcc_error("can't relocate value at %x,%d",addr, type);
348 (*(int *)ptr) |= x & 0x7fffffff;
350 case R_ARM_ABS32:
351 *(int *)ptr += val;
352 return;
353 case R_ARM_REL32:
354 *(int *)ptr += val - addr;
355 return;
356 case R_ARM_GOTPC:
357 *(int *)ptr += s1->got->sh_addr - addr;
358 return;
359 case R_ARM_GOTOFF:
360 *(int *)ptr += val - s1->got->sh_addr;
361 return;
362 case R_ARM_GOT32:
363 /* we load the got offset */
364 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
365 return;
366 case R_ARM_COPY:
367 return;
368 case R_ARM_V4BX:
369 /* trade Thumb support for ARMv4 support */
370 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
371 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
372 return;
373 case R_ARM_GLOB_DAT:
374 case R_ARM_JUMP_SLOT:
375 *(addr_t *)ptr = val;
376 return;
377 case R_ARM_NONE:
378 /* Nothing to do. Normally used to indicate a dependency
379 on a certain symbol (like for exception handling under EABI). */
380 return;
381 case R_ARM_RELATIVE:
382 #ifdef TCC_TARGET_PE
383 add32le(ptr, val - s1->pe_imagebase);
384 #endif
385 /* do nothing */
386 return;
387 default:
388 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
389 type, (unsigned)addr, ptr, (unsigned)val);
390 return;
394 #endif /* !TARGET_DEFS_ONLY */