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
26 #else /* !TARGET_DEFS_ONLY */
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
)
36 case R_ARM_MOVW_ABS_NC
:
37 case R_ARM_THM_MOVT_ABS
:
38 case R_ARM_THM_MOVW_ABS_NC
:
54 case R_ARM_THM_JUMP24
:
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
66 int gotplt_entry_type (int reloc_type
)
73 return NO_GOTPLT_ENTRY
;
80 case R_ARM_THM_JUMP24
:
82 case R_ARM_MOVW_ABS_NC
:
83 case R_ARM_THM_MOVT_ABS
:
84 case R_ARM_THM_MOVW_ABS_NC
:
89 return AUTO_GOTPLT_ENTRY
;
93 return BUILD_GOT_ONLY
;
96 return ALWAYS_GOTPLT_ENTRY
;
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
;
108 /* when building a DLL, GOT entry accesses must be done relative to
109 start of GOT (see x86_64 example above) */
111 /* empty PLT: create PLT0 entry that push address of call site and
112 jump to ld.so resolution routine (GOT + 8) */
113 if (plt
->data_offset
== 0) {
114 p
= section_ptr_add(plt
, 20);
115 write32le(p
, 0xe52de004); /* push {lr} */
116 write32le(p
+4, 0xe59fe004); /* ldr lr, [pc, #4] */
117 write32le(p
+8, 0xe08fe00e); /* add lr, pc, lr */
118 write32le(p
+12, 0xe5bef008); /* ldr pc, [lr, #8]! */
119 /* p+16 is set in relocate_plt */
121 plt_offset
= plt
->data_offset
;
123 if (attr
->plt_thumb_stub
) {
124 p
= section_ptr_add(plt
, 4);
125 write32le(p
, 0x4778); /* bx pc */
126 write32le(p
+2, 0x46c0); /* nop */
128 p
= section_ptr_add(plt
, 12);
129 /* save GOT offset for relocate_plt */
130 write32le(p
+ 4, got_offset
);
134 /* relocate the PLT: compute addresses and offsets in the PLT now that final
135 address for PLT and GOT are known (see fill_program_header) */
136 ST_FUNC
void relocate_plt(TCCState
*s1
)
144 p_end
= p
+ s1
->plt
->data_offset
;
147 int x
= s1
->got
->sh_addr
- s1
->plt
->sh_addr
- 12;
148 write32le(s1
->plt
->data
+ 16, x
- 16);
151 unsigned off
= x
+ read32le(p
+ 4) + (s1
->plt
->data
- p
) + 4;
152 if (read32le(p
) == 0x46c04778) /* PLT Thumb stub present */
154 write32le(p
, 0xe28fc600 | ((off
>> 20) & 0xff)); // add ip, pc, #0xNN00000
155 write32le(p
+ 4, 0xe28cca00 | ((off
>> 12) & 0xff)); // add ip, ip, #0xNN000
156 write32le(p
+ 8, 0xe5bcf000 | (off
& 0xfff)); // ldr pc, [ip, #0xNNN]!
163 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
166 int sym_index
, esym_index
;
168 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
169 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
177 int x
, is_thumb
, is_call
, h
, blx_avail
, is_bl
, th_ko
;
178 x
= (*(int *) ptr
) & 0xffffff;
180 printf ("reloc %d: x=0x%x val=0x%x ", type
, x
, val
);
182 (*(int *)ptr
) &= 0xff000000;
186 blx_avail
= (TCC_CPU_VERSION
>= 5);
188 is_bl
= (*(unsigned *) ptr
) >> 24 == 0xeb;
189 is_call
= (type
== R_ARM_CALL
|| (type
== R_ARM_PC24
&& is_bl
));
192 printf (" newx=0x%x name=%s\n", x
,
193 (char *) symtab_section
->link
->data
+ sym
->st_name
);
196 th_ko
= (x
& 3) && (!blx_avail
|| !is_call
);
197 if (th_ko
|| x
>= 0x2000000 || x
< -0x2000000)
198 tcc_error("can't relocate value at %x,%d",addr
, type
);
201 /* Only reached if blx is avail and it is a call */
204 (*(int *)ptr
) = 0xfa << 24; /* bl -> blx */
209 /* Since these relocations only concern Thumb-2 and blx instruction was
210 introduced before Thumb-2, we can assume blx is available and not
213 case R_ARM_THM_JUMP24
:
215 int x
, hi
, lo
, s
, j1
, j2
, i1
, i2
, imm10
, imm11
;
216 int to_thumb
, is_call
, to_plt
, blx_bit
= 1 << 12;
220 if (sym
->st_shndx
== SHN_UNDEF
&&
221 ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
)
224 /* Get initial offset */
225 hi
= (*(uint16_t *)ptr
);
226 lo
= (*(uint16_t *)(ptr
+2));
234 x
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) |
235 (imm10
<< 12) | (imm11
<< 1);
239 /* Relocation infos */
242 to_plt
= (val
>= plt
->sh_addr
) &&
243 (val
< plt
->sh_addr
+ plt
->data_offset
);
244 is_call
= (type
== R_ARM_THM_PC22
);
246 if (!to_thumb
&& !to_plt
&& !is_call
) {
249 char *name
, buf
[1024];
252 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
253 text
= s1
->sections
[sym
->st_shndx
];
254 /* Modify reloc to target a thumb stub to switch to ARM */
255 snprintf(buf
, sizeof(buf
), "%s_from_thumb", name
);
256 index
= put_elf_sym(symtab_section
,
257 text
->data_offset
+ 1,
258 sym
->st_size
, sym
->st_info
, 0,
261 val
= text
->data_offset
+ 1;
262 rel
->r_info
= ELFW(R_INFO
)(index
, type
);
263 /* Create a thumb stub function to switch to ARM mode */
264 put_elf_reloc(symtab_section
, text
,
265 text
->data_offset
+ 4, R_ARM_JUMP24
,
267 p
= section_ptr_add(text
, 8);
268 write32le(p
, 0x4778); /* bx pc */
269 write32le(p
+2, 0x46c0); /* nop */
270 write32le(p
+4, 0xeafffffe); /* b $sym */
273 /* Compute final offset */
275 if (!to_thumb
&& is_call
) {
276 blx_bit
= 0; /* bl -> blx */
277 x
= (x
+ 3) & -4; /* Compute offset from aligned PC */
280 /* Check that relocation is possible
281 * offset must not be out of range
282 * if target is to be entered in arm mode:
284 - instruction must be a call (bl) or a jump to PLT */
285 if (!to_thumb
|| x
>= 0x1000000 || x
< -0x1000000)
286 if (to_thumb
|| (val
& 2) || (!is_call
&& !to_plt
))
287 tcc_error("can't relocate value at %x,%d",addr
, type
);
289 /* Compute and store final offset */
295 imm10
= (x
>> 12) & 0x3ff;
296 imm11
= (x
>> 1) & 0x7ff;
297 (*(uint16_t *)ptr
) = (uint16_t) ((hi
& 0xf800) |
299 (*(uint16_t *)(ptr
+2)) = (uint16_t) ((lo
& 0xc000) |
300 (j1
<< 13) | blx_bit
| (j2
<< 11) |
305 case R_ARM_MOVW_ABS_NC
:
308 if (type
== R_ARM_MOVT_ABS
)
311 imm4
= (val
>> 12) & 0xf;
312 x
= (imm4
<< 16) | imm12
;
313 if (type
== R_ARM_THM_MOVT_ABS
)
319 case R_ARM_THM_MOVT_ABS
:
320 case R_ARM_THM_MOVW_ABS_NC
:
322 int x
, i
, imm4
, imm3
, imm8
;
323 if (type
== R_ARM_THM_MOVT_ABS
)
326 imm3
= (val
>> 8) & 0x7;
328 imm4
= (val
>> 12) & 0xf;
329 x
= (imm3
<< 28) | (imm8
<< 16) | (i
<< 10) | imm4
;
330 if (type
== R_ARM_THM_MOVT_ABS
)
339 x
= (*(int *)ptr
) & 0x7fffffff;
340 (*(int *)ptr
) &= 0x80000000;
343 if((x
^(x
>>1))&0x40000000)
344 tcc_error("can't relocate value at %x,%d",addr
, type
);
345 (*(int *)ptr
) |= x
& 0x7fffffff;
348 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
349 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
350 qrel
->r_offset
= rel
->r_offset
;
352 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_ARM_ABS32
);
356 qrel
->r_info
= ELFW(R_INFO
)(0, R_ARM_RELATIVE
);
363 *(int *)ptr
+= val
- addr
;
366 *(int *)ptr
+= s1
->got
->sh_addr
- addr
;
369 *(int *)ptr
+= val
- s1
->got
->sh_addr
;
372 /* we load the got offset */
373 *(int *)ptr
+= get_sym_attr(s1
, sym_index
, 0)->got_offset
;
378 /* trade Thumb support for ARMv4 support */
379 if ((0x0ffffff0 & *(int*)ptr
) == 0x012FFF10)
380 *(int*)ptr
^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
383 case R_ARM_JUMP_SLOT
:
384 *(addr_t
*)ptr
= val
;
387 /* Nothing to do. Normally used to indicate a dependency
388 on a certain symbol (like for exception handling under EABI). */
392 add32le(ptr
, val
- s1
->pe_imagebase
);
397 fprintf(stderr
,"FIXME: handle reloc type %x at %x [%p] to %x\n",
398 type
, (unsigned)addr
, ptr
, (unsigned)val
);
403 #endif /* !TARGET_DEFS_ONLY */