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 0x00010000
16 #define ELF_PAGE_SIZE 0x10000
18 #define PCRELATIVE_DLLPLT 1
19 #define RELOCATE_DLLPLT 1
26 #else /* !TARGET_DEFS_ONLY */
30 #ifdef NEED_RELOC_TYPE
31 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
32 relocations, returns -1. */
33 int code_reloc (int reloc_type
)
37 case R_ARM_MOVW_ABS_NC
:
38 case R_ARM_THM_MOVT_ABS
:
39 case R_ARM_THM_MOVW_ABS_NC
:
51 case R_ARM_MOVW_PREL_NC
:
59 case R_ARM_THM_JUMP24
:
68 /* Returns an enumerator to describe whether and when the relocation needs a
69 GOT and/or PLT entry to be created. See tcc.h for a description of the
71 int gotplt_entry_type (int reloc_type
)
78 return NO_GOTPLT_ENTRY
;
85 case R_ARM_THM_JUMP24
:
87 case R_ARM_MOVW_ABS_NC
:
88 case R_ARM_THM_MOVT_ABS
:
89 case R_ARM_THM_MOVW_ABS_NC
:
96 case R_ARM_MOVW_PREL_NC
:
97 return AUTO_GOTPLT_ENTRY
;
101 return BUILD_GOT_ONLY
;
105 return ALWAYS_GOTPLT_ENTRY
;
110 #ifdef NEED_BUILD_GOT
111 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
113 Section
*plt
= s1
->plt
;
117 /* when building a DLL, GOT entry accesses must be done relative to
118 start of GOT (see x86_64 example above) */
120 /* empty PLT: create PLT0 entry that push address of call site and
121 jump to ld.so resolution routine (GOT + 8) */
122 if (plt
->data_offset
== 0) {
123 p
= section_ptr_add(plt
, 20);
124 write32le(p
, 0xe52de004); /* push {lr} */
125 write32le(p
+4, 0xe59fe004); /* ldr lr, [pc, #4] */
126 write32le(p
+8, 0xe08fe00e); /* add lr, pc, lr */
127 write32le(p
+12, 0xe5bef008); /* ldr pc, [lr, #8]! */
128 /* p+16 is set in relocate_plt */
130 plt_offset
= plt
->data_offset
;
132 if (attr
->plt_thumb_stub
) {
133 p
= section_ptr_add(plt
, 4);
134 write32le(p
, 0x4778); /* bx pc */
135 write32le(p
+2, 0x46c0); /* nop */
137 p
= section_ptr_add(plt
, 16);
138 /* save GOT offset for relocate_plt */
139 write32le(p
+ 4, got_offset
);
143 /* relocate the PLT: compute addresses and offsets in the PLT now that final
144 address for PLT and GOT are known (see fill_program_header) */
145 ST_FUNC
void relocate_plt(TCCState
*s1
)
153 p_end
= p
+ s1
->plt
->data_offset
;
156 int x
= s1
->got
->sh_addr
- s1
->plt
->sh_addr
- 12;
157 write32le(s1
->plt
->data
+ 16, x
- 4);
160 unsigned off
= x
+ read32le(p
+ 4) + (s1
->plt
->data
- p
) + 4;
161 if (read32le(p
) == 0x46c04778) /* PLT Thumb stub present */
163 write32le(p
, 0xe28fc200 | ((off
>> 28) & 0xf)); // add ip, pc, #0xN0000000
164 write32le(p
+ 4, 0xe28cc600 | ((off
>> 20) & 0xff)); // add ip, pc, #0xNN00000
165 write32le(p
+ 8, 0xe28cca00 | ((off
>> 12) & 0xff)); // add ip, ip, #0xNN000
166 write32le(p
+ 12, 0xe5bcf000 | (off
& 0xfff)); // ldr pc, [ip, #0xNNN]!
171 if (s1
->plt
->reloc
) {
174 for_each_elem(s1
->plt
->reloc
, 0, rel
, ElfW_Rel
) {
175 write32le(p
+ rel
->r_offset
, s1
->plt
->sh_addr
);
182 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
185 int sym_index
, esym_index
;
187 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
188 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
196 int x
, is_thumb
, is_call
, h
, blx_avail
, is_bl
, th_ko
;
197 x
= (*(int *) ptr
) & 0xffffff;
199 printf ("reloc %d: x=0x%x val=0x%x ", type
, x
, val
);
201 (*(int *)ptr
) &= 0xff000000;
205 blx_avail
= (TCC_CPU_VERSION
>= 5);
207 is_bl
= (*(unsigned *) ptr
) >> 24 == 0xeb;
208 is_call
= (type
== R_ARM_CALL
|| (type
== R_ARM_PC24
&& is_bl
));
211 printf (" newx=0x%x name=%s\n", x
,
212 (char *) symtab_section
->link
->data
+ sym
->st_name
);
215 th_ko
= (x
& 3) && (!blx_avail
|| !is_call
);
216 if (th_ko
|| x
>= 0x2000000 || x
< -0x2000000)
217 tcc_error("can't relocate value at %x,%d",addr
, type
);
220 /* Only reached if blx is avail and it is a call */
223 (*(int *)ptr
) = 0xfa << 24; /* bl -> blx */
228 /* Since these relocations only concern Thumb-2 and blx instruction was
229 introduced before Thumb-2, we can assume blx is available and not
232 case R_ARM_THM_JUMP24
:
234 int x
, hi
, lo
, s
, j1
, j2
, i1
, i2
, imm10
, imm11
;
235 int to_thumb
, is_call
, to_plt
, blx_bit
= 1 << 12;
239 if (sym
->st_shndx
== SHN_UNDEF
&&
240 ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
)
243 /* Get initial offset */
244 hi
= (*(uint16_t *)ptr
);
245 lo
= (*(uint16_t *)(ptr
+2));
253 x
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) |
254 (imm10
<< 12) | (imm11
<< 1);
258 /* Relocation infos */
261 to_plt
= (val
>= plt
->sh_addr
) &&
262 (val
< plt
->sh_addr
+ plt
->data_offset
);
263 is_call
= (type
== R_ARM_THM_PC22
);
265 if (!to_thumb
&& !to_plt
&& !is_call
) {
268 char *name
, buf
[1024];
271 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
272 text
= s1
->sections
[sym
->st_shndx
];
273 /* Modify reloc to target a thumb stub to switch to ARM */
274 snprintf(buf
, sizeof(buf
), "%s_from_thumb", name
);
275 index
= put_elf_sym(symtab_section
,
276 text
->data_offset
+ 1,
277 sym
->st_size
, sym
->st_info
, 0,
280 val
= text
->data_offset
+ 1;
281 rel
->r_info
= ELFW(R_INFO
)(index
, type
);
282 /* Create a thumb stub function to switch to ARM mode */
283 put_elf_reloc(symtab_section
, text
,
284 text
->data_offset
+ 4, R_ARM_JUMP24
,
286 p
= section_ptr_add(text
, 8);
287 write32le(p
, 0x4778); /* bx pc */
288 write32le(p
+2, 0x46c0); /* nop */
289 write32le(p
+4, 0xeafffffe); /* b $sym */
292 /* Compute final offset */
294 if (!to_thumb
&& is_call
) {
295 blx_bit
= 0; /* bl -> blx */
296 x
= (x
+ 3) & -4; /* Compute offset from aligned PC */
299 /* Check that relocation is possible
300 * offset must not be out of range
301 * if target is to be entered in arm mode:
303 - instruction must be a call (bl) or a jump to PLT */
304 if (!to_thumb
|| x
>= 0x1000000 || x
< -0x1000000)
305 if (to_thumb
|| (val
& 2) || (!is_call
&& !to_plt
))
306 tcc_error("can't relocate value at %x,%d",addr
, type
);
308 /* Compute and store final offset */
314 imm10
= (x
>> 12) & 0x3ff;
315 imm11
= (x
>> 1) & 0x7ff;
316 (*(uint16_t *)ptr
) = (uint16_t) ((hi
& 0xf800) |
318 (*(uint16_t *)(ptr
+2)) = (uint16_t) ((lo
& 0xc000) |
319 (j1
<< 13) | blx_bit
| (j2
<< 11) |
324 case R_ARM_MOVW_ABS_NC
:
327 if (type
== R_ARM_MOVT_ABS
)
330 imm4
= (val
>> 12) & 0xf;
331 x
= (imm4
<< 16) | imm12
;
332 if (type
== R_ARM_THM_MOVT_ABS
)
338 case R_ARM_MOVT_PREL
:
339 case R_ARM_MOVW_PREL_NC
:
341 int insn
= *(int *)ptr
;
342 int addend
= ((insn
>> 4) & 0xf000) | (insn
& 0xfff);
344 addend
= (addend
^ 0x8000) - 0x8000;
345 val
+= addend
- addr
;
346 if (type
== R_ARM_MOVT_PREL
)
348 *(int *)ptr
= (insn
& 0xfff0f000) |
349 ((val
& 0xf000) << 4) | (val
& 0xfff);
352 case R_ARM_THM_MOVT_ABS
:
353 case R_ARM_THM_MOVW_ABS_NC
:
355 int x
, i
, imm4
, imm3
, imm8
;
356 if (type
== R_ARM_THM_MOVT_ABS
)
359 imm3
= (val
>> 8) & 0x7;
361 imm4
= (val
>> 12) & 0xf;
362 x
= (imm3
<< 28) | (imm8
<< 16) | (i
<< 10) | imm4
;
363 if (type
== R_ARM_THM_MOVT_ABS
)
372 x
= (*(int *)ptr
) & 0x7fffffff;
373 (*(int *)ptr
) &= 0x80000000;
376 if((x
^(x
>>1))&0x40000000)
377 tcc_error("can't relocate value at %x,%d",addr
, type
);
378 (*(int *)ptr
) |= x
& 0x7fffffff;
383 if (s1
->output_type
& TCC_OUTPUT_DYN
) {
384 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
385 qrel
->r_offset
= rel
->r_offset
;
387 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_ARM_ABS32
);
391 qrel
->r_info
= ELFW(R_INFO
)(0, R_ARM_RELATIVE
);
398 *(int *)ptr
+= val
- addr
;
401 *(int *)ptr
+= s1
->got
->sh_addr
- addr
;
404 *(int *)ptr
+= val
- s1
->got
->sh_addr
;
407 /* we load the got offset */
408 *(int *)ptr
+= get_sym_attr(s1
, sym_index
, 0)->got_offset
;
411 /* we load the pc relative got offset */
412 *(int *)ptr
+= s1
->got
->sh_addr
+
413 get_sym_attr(s1
, sym_index
, 0)->got_offset
-
419 /* trade Thumb support for ARMv4 support */
420 if ((0x0ffffff0 & *(int*)ptr
) == 0x012FFF10)
421 *(int*)ptr
^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
424 case R_ARM_JUMP_SLOT
:
425 *(addr_t
*)ptr
= val
;
428 /* Nothing to do. Normally used to indicate a dependency
429 on a certain symbol (like for exception handling under EABI). */
433 add32le(ptr
, val
- s1
->pe_imagebase
);
438 fprintf(stderr
,"FIXME: handle reloc type %d at %x [%p] to %x\n",
439 type
, (unsigned)addr
, ptr
, (unsigned)val
);
444 #endif /* !TARGET_DEFS_ONLY */