Add relocs_info array to c67 backend
[tinycc/jakubkaszycki.git] / arm-link.c
blob02f8196ac967a19a80f4ea74b1b2f43f3f4a19f8
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
19 enum float_abi {
20 ARM_SOFTFP_FLOAT,
21 ARM_HARD_FLOAT,
24 #else /* !TARGET_DEFS_ONLY */
26 #include "tcc.h"
28 ST_DATA struct reloc_info relocs_info[R_NUM] = {
29 INIT_RELOC_INFO (R_ARM_PC24, 1, AUTO_GOTPLT_ENTRY, 0)
30 INIT_RELOC_INFO (R_ARM_CALL, 1, AUTO_GOTPLT_ENTRY, 0)
31 INIT_RELOC_INFO (R_ARM_JUMP24, 1, AUTO_GOTPLT_ENTRY, 0)
32 INIT_RELOC_INFO (R_ARM_PLT32, 1, ALWAYS_GOTPLT_ENTRY, 0)
33 INIT_RELOC_INFO (R_ARM_THM_PC22, 1, AUTO_GOTPLT_ENTRY, 0)
34 INIT_RELOC_INFO (R_ARM_THM_JUMP24, 1, AUTO_GOTPLT_ENTRY, 0)
35 INIT_RELOC_INFO (R_ARM_MOVT_ABS, 0, AUTO_GOTPLT_ENTRY, 0)
36 INIT_RELOC_INFO (R_ARM_MOVW_ABS_NC, 0, AUTO_GOTPLT_ENTRY, 0)
37 INIT_RELOC_INFO (R_ARM_THM_MOVT_ABS, 0, AUTO_GOTPLT_ENTRY, 0)
38 INIT_RELOC_INFO (R_ARM_THM_MOVW_ABS_NC, 0, AUTO_GOTPLT_ENTRY, 0)
39 INIT_RELOC_INFO (R_ARM_PREL31, 1, AUTO_GOTPLT_ENTRY, 0)
40 INIT_RELOC_INFO (R_ARM_ABS32, 0, AUTO_GOTPLT_ENTRY, 0)
41 INIT_RELOC_INFO (R_ARM_REL32, 0, AUTO_GOTPLT_ENTRY, 0)
42 INIT_RELOC_INFO (R_ARM_GOTPC, 0, BUILD_GOT_ONLY, 0)
43 INIT_RELOC_INFO (R_ARM_GOTOFF, 0, BUILD_GOT_ONLY, 0)
44 INIT_RELOC_INFO (R_ARM_GOT32, 0, ALWAYS_GOTPLT_ENTRY, 0)
45 INIT_RELOC_INFO (R_ARM_COPY, 0, NO_GOTPLT_ENTRY, 0)
46 INIT_RELOC_INFO (R_ARM_V4BX, 1, AUTO_GOTPLT_ENTRY, 0)
47 INIT_RELOC_INFO (R_ARM_GLOB_DAT, 0, NO_GOTPLT_ENTRY, 0)
48 INIT_RELOC_INFO (R_ARM_JUMP_SLOT, 1, NO_GOTPLT_ENTRY, 0)
49 INIT_RELOC_INFO (R_ARM_NONE, 0, NO_GOTPLT_ENTRY, 0)
52 void relocate_init(Section *sr) {}
54 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
56 ElfW(Sym) *sym;
57 int sym_index;
59 sym_index = ELFW(R_SYM)(rel->r_info);
60 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
62 switch(type) {
63 case R_ARM_PC24:
64 case R_ARM_CALL:
65 case R_ARM_JUMP24:
66 case R_ARM_PLT32:
68 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
69 x = (*(int *) ptr) & 0xffffff;
70 #ifdef DEBUG_RELOC
71 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
72 #endif
73 (*(int *)ptr) &= 0xff000000;
74 if (x & 0x800000)
75 x -= 0x1000000;
76 x <<= 2;
77 blx_avail = (TCC_ARM_VERSION >= 5);
78 is_thumb = val & 1;
79 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
80 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
81 x += val - addr;
82 #ifdef DEBUG_RELOC
83 printf (" newx=0x%x name=%s\n", x,
84 (char *) symtab_section->link->data + sym->st_name);
85 #endif
86 h = x & 2;
87 th_ko = (x & 3) && (!blx_avail || !is_call);
88 if (th_ko || x >= 0x2000000 || x < -0x2000000)
89 tcc_error("can't relocate value at %x,%d",addr, type);
90 x >>= 2;
91 x &= 0xffffff;
92 /* Only reached if blx is avail and it is a call */
93 if (is_thumb) {
94 x |= h << 24;
95 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
97 (*(int *) ptr) |= x;
99 return;
100 /* Since these relocations only concern Thumb-2 and blx instruction was
101 introduced before Thumb-2, we can assume blx is available and not
102 guard its use */
103 case R_ARM_THM_PC22:
104 case R_ARM_THM_JUMP24:
106 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
107 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
108 Section *plt;
110 /* weak reference */
111 if (sym->st_shndx == SHN_UNDEF &&
112 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
113 return;
115 /* Get initial offset */
116 hi = (*(uint16_t *)ptr);
117 lo = (*(uint16_t *)(ptr+2));
118 s = (hi >> 10) & 1;
119 j1 = (lo >> 13) & 1;
120 j2 = (lo >> 11) & 1;
121 i1 = (j1 ^ s) ^ 1;
122 i2 = (j2 ^ s) ^ 1;
123 imm10 = hi & 0x3ff;
124 imm11 = lo & 0x7ff;
125 x = (s << 24) | (i1 << 23) | (i2 << 22) |
126 (imm10 << 12) | (imm11 << 1);
127 if (x & 0x01000000)
128 x -= 0x02000000;
130 /* Relocation infos */
131 to_thumb = val & 1;
132 plt = s1->plt;
133 to_plt = (val >= plt->sh_addr) &&
134 (val < plt->sh_addr + plt->data_offset);
135 is_call = (type == R_ARM_THM_PC22);
137 if (!to_thumb && !to_plt && !is_call) {
138 int index;
139 uint8_t *p;
140 char *name, buf[1024];
141 Section *text_section;
143 name = (char *) symtab_section->link->data + sym->st_name;
144 text_section = s1->sections[sym->st_shndx];
145 /* Modify reloc to target a thumb stub to switch to ARM */
146 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
147 index = put_elf_sym(symtab_section,
148 text_section->data_offset + 1,
149 sym->st_size, sym->st_info, 0,
150 sym->st_shndx, buf);
151 to_thumb = 1;
152 val = text_section->data_offset + 1;
153 rel->r_info = ELFW(R_INFO)(index, type);
154 /* Create a thumb stub function to switch to ARM mode */
155 put_elf_reloc(symtab_section, text_section,
156 text_section->data_offset + 4, R_ARM_JUMP24,
157 sym_index);
158 p = section_ptr_add(text_section, 8);
159 write32le(p, 0x4778); /* bx pc */
160 write32le(p+2, 0x46c0); /* nop */
161 write32le(p+4, 0xeafffffe); /* b $sym */
164 /* Compute final offset */
165 x += val - addr;
166 if (!to_thumb && is_call) {
167 blx_bit = 0; /* bl -> blx */
168 x = (x + 3) & -4; /* Compute offset from aligned PC */
171 /* Check that relocation is possible
172 * offset must not be out of range
173 * if target is to be entered in arm mode:
174 - bit 1 must not set
175 - instruction must be a call (bl) or a jump to PLT */
176 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
177 if (to_thumb || (val & 2) || (!is_call && !to_plt))
178 tcc_error("can't relocate value at %x,%d",addr, type);
180 /* Compute and store final offset */
181 s = (x >> 24) & 1;
182 i1 = (x >> 23) & 1;
183 i2 = (x >> 22) & 1;
184 j1 = s ^ (i1 ^ 1);
185 j2 = s ^ (i2 ^ 1);
186 imm10 = (x >> 12) & 0x3ff;
187 imm11 = (x >> 1) & 0x7ff;
188 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
189 (s << 10) | imm10);
190 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
191 (j1 << 13) | blx_bit | (j2 << 11) |
192 imm11);
194 return;
195 case R_ARM_MOVT_ABS:
196 case R_ARM_MOVW_ABS_NC:
198 int x, imm4, imm12;
199 if (type == R_ARM_MOVT_ABS)
200 val >>= 16;
201 imm12 = val & 0xfff;
202 imm4 = (val >> 12) & 0xf;
203 x = (imm4 << 16) | imm12;
204 if (type == R_ARM_THM_MOVT_ABS)
205 *(int *)ptr |= x;
206 else
207 *(int *)ptr += x;
209 return;
210 case R_ARM_THM_MOVT_ABS:
211 case R_ARM_THM_MOVW_ABS_NC:
213 int x, i, imm4, imm3, imm8;
214 if (type == R_ARM_THM_MOVT_ABS)
215 val >>= 16;
216 imm8 = val & 0xff;
217 imm3 = (val >> 8) & 0x7;
218 i = (val >> 11) & 1;
219 imm4 = (val >> 12) & 0xf;
220 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
221 if (type == R_ARM_THM_MOVT_ABS)
222 *(int *)ptr |= x;
223 else
224 *(int *)ptr += x;
226 return;
227 case R_ARM_PREL31:
229 int x;
230 x = (*(int *)ptr) & 0x7fffffff;
231 (*(int *)ptr) &= 0x80000000;
232 x = (x * 2) / 2;
233 x += val - addr;
234 if((x^(x>>1))&0x40000000)
235 tcc_error("can't relocate value at %x,%d",addr, type);
236 (*(int *)ptr) |= x & 0x7fffffff;
238 case R_ARM_ABS32:
239 *(int *)ptr += val;
240 return;
241 case R_ARM_REL32:
242 *(int *)ptr += val - addr;
243 return;
244 case R_ARM_GOTPC:
245 *(int *)ptr += s1->got->sh_addr - addr;
246 return;
247 case R_ARM_GOTOFF:
248 *(int *)ptr += val - s1->got->sh_addr;
249 return;
250 case R_ARM_GOT32:
251 /* we load the got offset */
252 *(int *)ptr += s1->sym_attrs[sym_index].got_offset;
253 return;
254 case R_ARM_COPY:
255 return;
256 case R_ARM_V4BX:
257 /* trade Thumb support for ARMv4 support */
258 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
259 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
260 return;
261 case R_ARM_GLOB_DAT:
262 case R_ARM_JUMP_SLOT:
263 *(addr_t *)ptr = val;
264 return;
265 case R_ARM_NONE:
266 /* Nothing to do. Normally used to indicate a dependency
267 on a certain symbol (like for exception handling under EABI). */
268 return;
269 default:
270 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
271 type, (unsigned)addr, ptr, (unsigned)val);
272 return;
276 #endif /* !TARGET_DEFS_ONLY */