Fix include SHT_NOTE sections everywhere
[tinycc/self_contained.git] / arm-link.c
blob51882eabab6c52445b393475b220009d057c2760
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_GOT_PREL:
45 case R_ARM_COPY:
46 case R_ARM_GLOB_DAT:
47 case R_ARM_NONE:
48 return 0;
50 case R_ARM_PC24:
51 case R_ARM_CALL:
52 case R_ARM_JUMP24:
53 case R_ARM_PLT32:
54 case R_ARM_THM_PC22:
55 case R_ARM_THM_JUMP24:
56 case R_ARM_PREL31:
57 case R_ARM_V4BX:
58 case R_ARM_JUMP_SLOT:
59 return 1;
61 return -1;
64 /* Returns an enumerator to describe whether 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 case R_ARM_GOT_PREL:
98 return ALWAYS_GOTPLT_ENTRY;
100 return -1;
103 #ifndef TCC_TARGET_PE
104 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
106 Section *plt = s1->plt;
107 uint8_t *p;
108 unsigned plt_offset;
110 /* when building a DLL, GOT entry accesses must be done relative to
111 start of GOT (see x86_64 example above) */
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, 12);
131 /* save GOT offset for relocate_plt */
132 write32le(p + 4, got_offset);
133 return plt_offset;
136 /* relocate the PLT: compute addresses and offsets in the PLT now that final
137 address for PLT and GOT are known (see fill_program_header) */
138 ST_FUNC void relocate_plt(TCCState *s1)
140 uint8_t *p, *p_end;
142 if (!s1->plt)
143 return;
145 p = s1->plt->data;
146 p_end = p + s1->plt->data_offset;
148 if (p < p_end) {
149 int x = s1->got->sh_addr - s1->plt->sh_addr - 12;
150 write32le(s1->plt->data + 16, x - 4);
151 p += 20;
152 while (p < p_end) {
153 unsigned off = x + read32le(p + 4) + (s1->plt->data - p) + 4;
154 if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
155 p += 4;
156 write32le(p, 0xe28fc600 | ((off >> 20) & 0xff)); // add ip, pc, #0xNN00000
157 write32le(p + 4, 0xe28cca00 | ((off >> 12) & 0xff)); // add ip, ip, #0xNN000
158 write32le(p + 8, 0xe5bcf000 | (off & 0xfff)); // ldr pc, [ip, #0xNNN]!
159 p += 12;
163 if (s1->got->relocplt) {
164 int mem = s1->output_type == TCC_OUTPUT_MEMORY;
165 ElfW_Rel *rel;
167 p = s1->got->data;
168 for_each_elem(s1->got->relocplt, 0, rel, ElfW_Rel) {
169 int sym_index = ELFW(R_SYM)(rel->r_info);
170 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
171 write32le(p + rel->r_offset, mem ? sym->st_value : s1->plt->sh_addr);
175 #endif
177 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
179 ElfW(Sym) *sym;
180 int sym_index, esym_index;
182 sym_index = ELFW(R_SYM)(rel->r_info);
183 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
185 switch(type) {
186 case R_ARM_PC24:
187 case R_ARM_CALL:
188 case R_ARM_JUMP24:
189 case R_ARM_PLT32:
191 int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
192 x = (*(int *) ptr) & 0xffffff;
193 #ifdef DEBUG_RELOC
194 printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
195 #endif
196 (*(int *)ptr) &= 0xff000000;
197 if (x & 0x800000)
198 x -= 0x1000000;
199 x <<= 2;
200 blx_avail = (TCC_CPU_VERSION >= 5);
201 is_thumb = val & 1;
202 is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
203 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
204 x += val - addr;
205 #ifdef DEBUG_RELOC
206 printf (" newx=0x%x name=%s\n", x,
207 (char *) symtab_section->link->data + sym->st_name);
208 #endif
209 h = x & 2;
210 th_ko = (x & 3) && (!blx_avail || !is_call);
211 if (th_ko || x >= 0x2000000 || x < -0x2000000)
212 tcc_error("can't relocate value at %x,%d",addr, type);
213 x >>= 2;
214 x &= 0xffffff;
215 /* Only reached if blx is avail and it is a call */
216 if (is_thumb) {
217 x |= h << 24;
218 (*(int *)ptr) = 0xfa << 24; /* bl -> blx */
220 (*(int *) ptr) |= x;
222 return;
223 /* Since these relocations only concern Thumb-2 and blx instruction was
224 introduced before Thumb-2, we can assume blx is available and not
225 guard its use */
226 case R_ARM_THM_PC22:
227 case R_ARM_THM_JUMP24:
229 int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11;
230 int to_thumb, is_call, to_plt, blx_bit = 1 << 12;
231 Section *plt;
233 /* weak reference */
234 if (sym->st_shndx == SHN_UNDEF &&
235 ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
236 return;
238 /* Get initial offset */
239 hi = (*(uint16_t *)ptr);
240 lo = (*(uint16_t *)(ptr+2));
241 s = (hi >> 10) & 1;
242 j1 = (lo >> 13) & 1;
243 j2 = (lo >> 11) & 1;
244 i1 = (j1 ^ s) ^ 1;
245 i2 = (j2 ^ s) ^ 1;
246 imm10 = hi & 0x3ff;
247 imm11 = lo & 0x7ff;
248 x = (s << 24) | (i1 << 23) | (i2 << 22) |
249 (imm10 << 12) | (imm11 << 1);
250 if (x & 0x01000000)
251 x -= 0x02000000;
253 /* Relocation infos */
254 to_thumb = val & 1;
255 plt = s1->plt;
256 to_plt = (val >= plt->sh_addr) &&
257 (val < plt->sh_addr + plt->data_offset);
258 is_call = (type == R_ARM_THM_PC22);
260 if (!to_thumb && !to_plt && !is_call) {
261 int index;
262 uint8_t *p;
263 char *name, buf[1024];
264 Section *text;
266 name = (char *) symtab_section->link->data + sym->st_name;
267 text = s1->sections[sym->st_shndx];
268 /* Modify reloc to target a thumb stub to switch to ARM */
269 snprintf(buf, sizeof(buf), "%s_from_thumb", name);
270 index = put_elf_sym(symtab_section,
271 text->data_offset + 1,
272 sym->st_size, sym->st_info, 0,
273 sym->st_shndx, buf);
274 to_thumb = 1;
275 val = text->data_offset + 1;
276 rel->r_info = ELFW(R_INFO)(index, type);
277 /* Create a thumb stub function to switch to ARM mode */
278 put_elf_reloc(symtab_section, text,
279 text->data_offset + 4, R_ARM_JUMP24,
280 sym_index);
281 p = section_ptr_add(text, 8);
282 write32le(p, 0x4778); /* bx pc */
283 write32le(p+2, 0x46c0); /* nop */
284 write32le(p+4, 0xeafffffe); /* b $sym */
287 /* Compute final offset */
288 x += val - addr;
289 if (!to_thumb && is_call) {
290 blx_bit = 0; /* bl -> blx */
291 x = (x + 3) & -4; /* Compute offset from aligned PC */
294 /* Check that relocation is possible
295 * offset must not be out of range
296 * if target is to be entered in arm mode:
297 - bit 1 must not set
298 - instruction must be a call (bl) or a jump to PLT */
299 if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
300 if (to_thumb || (val & 2) || (!is_call && !to_plt))
301 tcc_error("can't relocate value at %x,%d",addr, type);
303 /* Compute and store final offset */
304 s = (x >> 24) & 1;
305 i1 = (x >> 23) & 1;
306 i2 = (x >> 22) & 1;
307 j1 = s ^ (i1 ^ 1);
308 j2 = s ^ (i2 ^ 1);
309 imm10 = (x >> 12) & 0x3ff;
310 imm11 = (x >> 1) & 0x7ff;
311 (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
312 (s << 10) | imm10);
313 (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
314 (j1 << 13) | blx_bit | (j2 << 11) |
315 imm11);
317 return;
318 case R_ARM_MOVT_ABS:
319 case R_ARM_MOVW_ABS_NC:
321 int x, imm4, imm12;
322 if (type == R_ARM_MOVT_ABS)
323 val >>= 16;
324 imm12 = val & 0xfff;
325 imm4 = (val >> 12) & 0xf;
326 x = (imm4 << 16) | imm12;
327 if (type == R_ARM_THM_MOVT_ABS)
328 *(int *)ptr |= x;
329 else
330 *(int *)ptr += x;
332 return;
333 case R_ARM_THM_MOVT_ABS:
334 case R_ARM_THM_MOVW_ABS_NC:
336 int x, i, imm4, imm3, imm8;
337 if (type == R_ARM_THM_MOVT_ABS)
338 val >>= 16;
339 imm8 = val & 0xff;
340 imm3 = (val >> 8) & 0x7;
341 i = (val >> 11) & 1;
342 imm4 = (val >> 12) & 0xf;
343 x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
344 if (type == R_ARM_THM_MOVT_ABS)
345 *(int *)ptr |= x;
346 else
347 *(int *)ptr += x;
349 return;
350 case R_ARM_PREL31:
352 int x;
353 x = (*(int *)ptr) & 0x7fffffff;
354 (*(int *)ptr) &= 0x80000000;
355 x = (x * 2) / 2;
356 x += val - addr;
357 if((x^(x>>1))&0x40000000)
358 tcc_error("can't relocate value at %x,%d",addr, type);
359 (*(int *)ptr) |= x & 0x7fffffff;
361 case R_ARM_ABS32:
362 if (s1->output_type == TCC_OUTPUT_DLL) {
363 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
364 qrel->r_offset = rel->r_offset;
365 if (esym_index) {
366 qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32);
367 qrel++;
368 return;
369 } else {
370 qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE);
371 qrel++;
374 *(int *)ptr += val;
375 return;
376 case R_ARM_REL32:
377 *(int *)ptr += val - addr;
378 return;
379 case R_ARM_GOTPC:
380 *(int *)ptr += s1->got->sh_addr - addr;
381 return;
382 case R_ARM_GOTOFF:
383 *(int *)ptr += val - s1->got->sh_addr;
384 return;
385 case R_ARM_GOT32:
386 /* we load the got offset */
387 *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset;
388 return;
389 case R_ARM_GOT_PREL:
390 /* we load the pc relative got offset */
391 *(int *)ptr += s1->got->sh_addr +
392 get_sym_attr(s1, sym_index, 0)->got_offset -
393 addr;
394 return;
395 case R_ARM_COPY:
396 return;
397 case R_ARM_V4BX:
398 /* trade Thumb support for ARMv4 support */
399 if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
400 *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
401 return;
402 case R_ARM_GLOB_DAT:
403 case R_ARM_JUMP_SLOT:
404 *(addr_t *)ptr = val;
405 return;
406 case R_ARM_NONE:
407 /* Nothing to do. Normally used to indicate a dependency
408 on a certain symbol (like for exception handling under EABI). */
409 return;
410 case R_ARM_RELATIVE:
411 #ifdef TCC_TARGET_PE
412 add32le(ptr, val - s1->pe_imagebase);
413 #endif
414 /* do nothing */
415 return;
416 default:
417 fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
418 type, (unsigned)addr, ptr, (unsigned)val);
419 return;
423 #endif /* !TARGET_DEFS_ONLY */