Use functions to get relocation info
[tinycc/jakubkaszycki.git] / arm-link.c
bloba92342afcf4d57dde4bef3a65958bb834de325cc
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
12 #define R_NUM R_ARM_NUM
14 #define ELF_START_ADDR 0x00008000
15 #define ELF_PAGE_SIZE 0x1000
17 #define HAVE_SECTION_RELOC
18 #define PCRELATIVE_DLLPLT 1
20 enum float_abi {
21 ARM_SOFTFP_FLOAT,
22 ARM_HARD_FLOAT,
25 #else /* !TARGET_DEFS_ONLY */
27 #include "tcc.h"
29 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
30 relocations, returns -1. */
31 int code_reloc (int reloc_type)
33 switch (reloc_type) {
34 case R_ARM_MOVT_ABS:
35 case R_ARM_MOVW_ABS_NC:
36 case R_ARM_THM_MOVT_ABS:
37 case R_ARM_THM_MOVW_ABS_NC:
38 case R_ARM_ABS32:
39 case R_ARM_REL32:
40 case R_ARM_GOTPC:
41 case R_ARM_GOTOFF:
42 case R_ARM_GOT32:
43 case R_ARM_COPY:
44 case R_ARM_GLOB_DAT:
45 case R_ARM_NONE:
46 return 0;
48 case R_ARM_PC24:
49 case R_ARM_CALL:
50 case R_ARM_JUMP24:
51 case R_ARM_PLT32:
52 case R_ARM_THM_PC22:
53 case R_ARM_THM_JUMP24:
54 case R_ARM_PREL31:
55 case R_ARM_V4BX:
56 case R_ARM_JUMP_SLOT:
57 return 1;
60 tcc_error ("Unknown relocation type: %d", reloc_type);
61 return -1;
64 /* Returns an enumerator to describe wether and when the relocation needs a
65 GOT and/or PLT entry to be created. See tcc.h for a description of the
66 different values. */
67 int gotplt_entry_type (int reloc_type)
69 switch (reloc_type) {
70 case R_ARM_NONE:
71 case R_ARM_COPY:
72 case R_ARM_GLOB_DAT:
73 case R_ARM_JUMP_SLOT:
74 return NO_GOTPLT_ENTRY;
76 case R_ARM_PC24:
77 case R_ARM_CALL:
78 case R_ARM_JUMP24:
79 case R_ARM_PLT32:
80 case R_ARM_THM_PC22:
81 case R_ARM_THM_JUMP24:
82 case R_ARM_MOVT_ABS:
83 case R_ARM_MOVW_ABS_NC:
84 case R_ARM_THM_MOVT_ABS:
85 case R_ARM_THM_MOVW_ABS_NC:
86 case R_ARM_PREL31:
87 case R_ARM_ABS32:
88 case R_ARM_REL32:
89 case R_ARM_V4BX:
90 return AUTO_GOTPLT_ENTRY;
92 case R_ARM_GOTPC:
93 case R_ARM_GOTOFF:
94 return BUILD_GOT_ONLY;
96 case R_ARM_GOT32:
97 return ALWAYS_GOTPLT_ENTRY;
100 tcc_error ("Unknown relocation type: %d", reloc_type);
101 return -1;
104 void relocate_init(Section *sr) {}
106 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
108 ElfW(Sym) *sym;
109 int sym_index;
111 sym_index = ELFW(R_SYM)(rel->r_info);
112 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
114 switch(type) {
115 case R_ARM_PC24:
116 case R_ARM_CALL:
117 case R_ARM_JUMP24:
118 case R_ARM_PLT32:
120 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
121 x = (*(int *) ptr) & 0xffffff;
122 #ifdef DEBUG_RELOC
123 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
124 #endif
125 (*(int *)ptr) &= 0xff000000;
126 if (x & 0x800000)
127 x -= 0x1000000;
128 x <<= 2;
129 blx_avail = (TCC_ARM_VERSION >= 5);
130 is_thumb = val & 1;
131 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
132 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
133 x += val - addr;
134 #ifdef DEBUG_RELOC
135 printf (" newx=0x%x name=%s\n", x,
136 (char *) symtab_section->link->data + sym->st_name);
137 #endif
138 h = x & 2;
139 th_ko = (x & 3) && (!blx_avail || !is_call);
140 if (th_ko || x >= 0x2000000 || x < -0x2000000)
141 tcc_error("can't relocate value at %x,%d",addr, type);
142 x >>= 2;
143 x &= 0xffffff;
144 /* Only reached if blx is avail and it is a call */
145 if (is_thumb) {
146 x |= h << 24;
147 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
149 (*(int *) ptr) |= x;
151 return;
152 /* Since these relocations only concern Thumb-2 and blx instruction was
153 introduced before Thumb-2, we can assume blx is available and not
154 guard its use */
155 case R_ARM_THM_PC22:
156 case R_ARM_THM_JUMP24:
158 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
159 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
160 Section *plt;
162 /* weak reference */
163 if (sym->st_shndx == SHN_UNDEF &&
164 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
165 return;
167 /* Get initial offset */
168 hi = (*(uint16_t *)ptr);
169 lo = (*(uint16_t *)(ptr+2));
170 s = (hi >> 10) & 1;
171 j1 = (lo >> 13) & 1;
172 j2 = (lo >> 11) & 1;
173 i1 = (j1 ^ s) ^ 1;
174 i2 = (j2 ^ s) ^ 1;
175 imm10 = hi & 0x3ff;
176 imm11 = lo & 0x7ff;
177 x = (s << 24) | (i1 << 23) | (i2 << 22) |
178 (imm10 << 12) | (imm11 << 1);
179 if (x & 0x01000000)
180 x -= 0x02000000;
182 /* Relocation infos */
183 to_thumb = val & 1;
184 plt = s1->plt;
185 to_plt = (val >= plt->sh_addr) &&
186 (val < plt->sh_addr + plt->data_offset);
187 is_call = (type == R_ARM_THM_PC22);
189 if (!to_thumb && !to_plt && !is_call) {
190 int index;
191 uint8_t *p;
192 char *name, buf[1024];
193 Section *text_section;
195 name = (char *) symtab_section->link->data + sym->st_name;
196 text_section = s1->sections[sym->st_shndx];
197 /* Modify reloc to target a thumb stub to switch to ARM */
198 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
199 index = put_elf_sym(symtab_section,
200 text_section->data_offset + 1,
201 sym->st_size, sym->st_info, 0,
202 sym->st_shndx, buf);
203 to_thumb = 1;
204 val = text_section->data_offset + 1;
205 rel->r_info = ELFW(R_INFO)(index, type);
206 /* Create a thumb stub function to switch to ARM mode */
207 put_elf_reloc(symtab_section, text_section,
208 text_section->data_offset + 4, R_ARM_JUMP24,
209 sym_index);
210 p = section_ptr_add(text_section, 8);
211 write32le(p, 0x4778); /* bx pc */
212 write32le(p+2, 0x46c0); /* nop */
213 write32le(p+4, 0xeafffffe); /* b $sym */
216 /* Compute final offset */
217 x += val - addr;
218 if (!to_thumb && is_call) {
219 blx_bit = 0; /* bl -> blx */
220 x = (x + 3) & -4; /* Compute offset from aligned PC */
223 /* Check that relocation is possible
224 * offset must not be out of range
225 * if target is to be entered in arm mode:
226 - bit 1 must not set
227 - instruction must be a call (bl) or a jump to PLT */
228 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
229 if (to_thumb || (val & 2) || (!is_call && !to_plt))
230 tcc_error("can't relocate value at %x,%d",addr, type);
232 /* Compute and store final offset */
233 s = (x >> 24) & 1;
234 i1 = (x >> 23) & 1;
235 i2 = (x >> 22) & 1;
236 j1 = s ^ (i1 ^ 1);
237 j2 = s ^ (i2 ^ 1);
238 imm10 = (x >> 12) & 0x3ff;
239 imm11 = (x >> 1) & 0x7ff;
240 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
241 (s << 10) | imm10);
242 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
243 (j1 << 13) | blx_bit | (j2 << 11) |
244 imm11);
246 return;
247 case R_ARM_MOVT_ABS:
248 case R_ARM_MOVW_ABS_NC:
250 int x, imm4, imm12;
251 if (type == R_ARM_MOVT_ABS)
252 val >>= 16;
253 imm12 = val & 0xfff;
254 imm4 = (val >> 12) & 0xf;
255 x = (imm4 << 16) | imm12;
256 if (type == R_ARM_THM_MOVT_ABS)
257 *(int *)ptr |= x;
258 else
259 *(int *)ptr += x;
261 return;
262 case R_ARM_THM_MOVT_ABS:
263 case R_ARM_THM_MOVW_ABS_NC:
265 int x, i, imm4, imm3, imm8;
266 if (type == R_ARM_THM_MOVT_ABS)
267 val >>= 16;
268 imm8 = val & 0xff;
269 imm3 = (val >> 8) & 0x7;
270 i = (val >> 11) & 1;
271 imm4 = (val >> 12) & 0xf;
272 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
273 if (type == R_ARM_THM_MOVT_ABS)
274 *(int *)ptr |= x;
275 else
276 *(int *)ptr += x;
278 return;
279 case R_ARM_PREL31:
281 int x;
282 x = (*(int *)ptr) & 0x7fffffff;
283 (*(int *)ptr) &= 0x80000000;
284 x = (x * 2) / 2;
285 x += val - addr;
286 if((x^(x>>1))&0x40000000)
287 tcc_error("can't relocate value at %x,%d",addr, type);
288 (*(int *)ptr) |= x & 0x7fffffff;
290 case R_ARM_ABS32:
291 *(int *)ptr += val;
292 return;
293 case R_ARM_REL32:
294 *(int *)ptr += val - addr;
295 return;
296 case R_ARM_GOTPC:
297 *(int *)ptr += s1->got->sh_addr - addr;
298 return;
299 case R_ARM_GOTOFF:
300 *(int *)ptr += val - s1->got->sh_addr;
301 return;
302 case R_ARM_GOT32:
303 /* we load the got offset */
304 *(int *)ptr += s1->sym_attrs[sym_index].got_offset;
305 return;
306 case R_ARM_COPY:
307 return;
308 case R_ARM_V4BX:
309 /* trade Thumb support for ARMv4 support */
310 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
311 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
312 return;
313 case R_ARM_GLOB_DAT:
314 case R_ARM_JUMP_SLOT:
315 *(addr_t *)ptr = val;
316 return;
317 case R_ARM_NONE:
318 /* Nothing to do. Normally used to indicate a dependency
319 on a certain symbol (like for exception handling under EABI). */
320 return;
321 default:
322 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
323 type, (unsigned)addr, ptr, (unsigned)val);
324 return;
328 #endif /* !TARGET_DEFS_ONLY */