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
:
56 case R_ARM_THM_JUMP24
:
65 /* Returns an enumerator to describe whether and when the relocation needs a
66 GOT and/or PLT entry to be created. See tcc.h for a description of the
68 int gotplt_entry_type (int reloc_type
)
75 return NO_GOTPLT_ENTRY
;
82 case R_ARM_THM_JUMP24
:
84 case R_ARM_MOVW_ABS_NC
:
85 case R_ARM_THM_MOVT_ABS
:
86 case R_ARM_THM_MOVW_ABS_NC
:
92 return AUTO_GOTPLT_ENTRY
;
96 return BUILD_GOT_ONLY
;
100 return ALWAYS_GOTPLT_ENTRY
;
105 #ifndef TCC_TARGET_PE
106 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
108 Section
*plt
= s1
->plt
;
112 /* when building a DLL, GOT entry accesses must be done relative to
113 start of GOT (see x86_64 example above) */
115 /* empty PLT: create PLT0 entry that push address of call site and
116 jump to ld.so resolution routine (GOT + 8) */
117 if (plt
->data_offset
== 0) {
118 p
= section_ptr_add(plt
, 20);
119 write32le(p
, 0xe52de004); /* push {lr} */
120 write32le(p
+4, 0xe59fe004); /* ldr lr, [pc, #4] */
121 write32le(p
+8, 0xe08fe00e); /* add lr, pc, lr */
122 write32le(p
+12, 0xe5bef008); /* ldr pc, [lr, #8]! */
123 /* p+16 is set in relocate_plt */
125 plt_offset
= plt
->data_offset
;
127 if (attr
->plt_thumb_stub
) {
128 p
= section_ptr_add(plt
, 4);
129 write32le(p
, 0x4778); /* bx pc */
130 write32le(p
+2, 0x46c0); /* nop */
132 p
= section_ptr_add(plt
, 12);
133 /* save GOT offset for relocate_plt */
134 write32le(p
+ 4, got_offset
);
138 /* relocate the PLT: compute addresses and offsets in the PLT now that final
139 address for PLT and GOT are known (see fill_program_header) */
140 ST_FUNC
void relocate_plt(TCCState
*s1
)
148 p_end
= p
+ s1
->plt
->data_offset
;
151 int x
= s1
->got
->sh_addr
- s1
->plt
->sh_addr
- 12;
152 write32le(s1
->plt
->data
+ 16, x
- 4);
155 unsigned off
= x
+ read32le(p
+ 4) + (s1
->plt
->data
- p
) + 4;
156 if (read32le(p
) == 0x46c04778) /* PLT Thumb stub present */
158 write32le(p
, 0xe28fc600 | ((off
>> 20) & 0xff)); // add ip, pc, #0xNN00000
159 write32le(p
+ 4, 0xe28cca00 | ((off
>> 12) & 0xff)); // add ip, ip, #0xNN000
160 write32le(p
+ 8, 0xe5bcf000 | (off
& 0xfff)); // ldr pc, [ip, #0xNNN]!
165 if (s1
->got
->relocplt
) {
166 int mem
= s1
->output_type
== TCC_OUTPUT_MEMORY
;
170 for_each_elem(s1
->got
->relocplt
, 0, rel
, ElfW_Rel
) {
171 int sym_index
= ELFW(R_SYM
)(rel
->r_info
);
172 ElfW(Sym
) *sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
173 write32le(p
+ rel
->r_offset
, mem
? sym
->st_value
: s1
->plt
->sh_addr
);
179 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
182 int sym_index
, esym_index
;
184 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
185 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
193 int x
, is_thumb
, is_call
, h
, blx_avail
, is_bl
, th_ko
;
194 x
= (*(int *) ptr
) & 0xffffff;
196 printf ("reloc %d: x=0x%x val=0x%x ", type
, x
, val
);
198 (*(int *)ptr
) &= 0xff000000;
202 blx_avail
= (TCC_CPU_VERSION
>= 5);
204 is_bl
= (*(unsigned *) ptr
) >> 24 == 0xeb;
205 is_call
= (type
== R_ARM_CALL
|| (type
== R_ARM_PC24
&& is_bl
));
208 printf (" newx=0x%x name=%s\n", x
,
209 (char *) symtab_section
->link
->data
+ sym
->st_name
);
212 th_ko
= (x
& 3) && (!blx_avail
|| !is_call
);
213 if (th_ko
|| x
>= 0x2000000 || x
< -0x2000000)
214 tcc_error("can't relocate value at %x,%d",addr
, type
);
217 /* Only reached if blx is avail and it is a call */
220 (*(int *)ptr
) = 0xfa << 24; /* bl -> blx */
225 /* Since these relocations only concern Thumb-2 and blx instruction was
226 introduced before Thumb-2, we can assume blx is available and not
229 case R_ARM_THM_JUMP24
:
231 int x
, hi
, lo
, s
, j1
, j2
, i1
, i2
, imm10
, imm11
;
232 int to_thumb
, is_call
, to_plt
, blx_bit
= 1 << 12;
236 if (sym
->st_shndx
== SHN_UNDEF
&&
237 ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
)
240 /* Get initial offset */
241 hi
= (*(uint16_t *)ptr
);
242 lo
= (*(uint16_t *)(ptr
+2));
250 x
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) |
251 (imm10
<< 12) | (imm11
<< 1);
255 /* Relocation infos */
258 to_plt
= (val
>= plt
->sh_addr
) &&
259 (val
< plt
->sh_addr
+ plt
->data_offset
);
260 is_call
= (type
== R_ARM_THM_PC22
);
262 if (!to_thumb
&& !to_plt
&& !is_call
) {
265 char *name
, buf
[1024];
268 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
269 text
= s1
->sections
[sym
->st_shndx
];
270 /* Modify reloc to target a thumb stub to switch to ARM */
271 snprintf(buf
, sizeof(buf
), "%s_from_thumb", name
);
272 index
= put_elf_sym(symtab_section
,
273 text
->data_offset
+ 1,
274 sym
->st_size
, sym
->st_info
, 0,
277 val
= text
->data_offset
+ 1;
278 rel
->r_info
= ELFW(R_INFO
)(index
, type
);
279 /* Create a thumb stub function to switch to ARM mode */
280 put_elf_reloc(symtab_section
, text
,
281 text
->data_offset
+ 4, R_ARM_JUMP24
,
283 p
= section_ptr_add(text
, 8);
284 write32le(p
, 0x4778); /* bx pc */
285 write32le(p
+2, 0x46c0); /* nop */
286 write32le(p
+4, 0xeafffffe); /* b $sym */
289 /* Compute final offset */
291 if (!to_thumb
&& is_call
) {
292 blx_bit
= 0; /* bl -> blx */
293 x
= (x
+ 3) & -4; /* Compute offset from aligned PC */
296 /* Check that relocation is possible
297 * offset must not be out of range
298 * if target is to be entered in arm mode:
300 - instruction must be a call (bl) or a jump to PLT */
301 if (!to_thumb
|| x
>= 0x1000000 || x
< -0x1000000)
302 if (to_thumb
|| (val
& 2) || (!is_call
&& !to_plt
))
303 tcc_error("can't relocate value at %x,%d",addr
, type
);
305 /* Compute and store final offset */
311 imm10
= (x
>> 12) & 0x3ff;
312 imm11
= (x
>> 1) & 0x7ff;
313 (*(uint16_t *)ptr
) = (uint16_t) ((hi
& 0xf800) |
315 (*(uint16_t *)(ptr
+2)) = (uint16_t) ((lo
& 0xc000) |
316 (j1
<< 13) | blx_bit
| (j2
<< 11) |
321 case R_ARM_MOVW_ABS_NC
:
324 if (type
== R_ARM_MOVT_ABS
)
327 imm4
= (val
>> 12) & 0xf;
328 x
= (imm4
<< 16) | imm12
;
329 if (type
== R_ARM_THM_MOVT_ABS
)
335 case R_ARM_THM_MOVT_ABS
:
336 case R_ARM_THM_MOVW_ABS_NC
:
338 int x
, i
, imm4
, imm3
, imm8
;
339 if (type
== R_ARM_THM_MOVT_ABS
)
342 imm3
= (val
>> 8) & 0x7;
344 imm4
= (val
>> 12) & 0xf;
345 x
= (imm3
<< 28) | (imm8
<< 16) | (i
<< 10) | imm4
;
346 if (type
== R_ARM_THM_MOVT_ABS
)
355 x
= (*(int *)ptr
) & 0x7fffffff;
356 (*(int *)ptr
) &= 0x80000000;
359 if((x
^(x
>>1))&0x40000000)
360 tcc_error("can't relocate value at %x,%d",addr
, type
);
361 (*(int *)ptr
) |= x
& 0x7fffffff;
364 // case R_ARM_TARGET1: /* ??? as seen on NetBSD - FIXME! */
365 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
366 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
367 qrel
->r_offset
= rel
->r_offset
;
369 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, type
);
373 qrel
->r_info
= ELFW(R_INFO
)(0, R_ARM_RELATIVE
);
380 *(int *)ptr
+= val
- addr
;
383 *(int *)ptr
+= s1
->got
->sh_addr
- addr
;
386 *(int *)ptr
+= val
- s1
->got
->sh_addr
;
389 /* we load the got offset */
390 *(int *)ptr
+= get_sym_attr(s1
, sym_index
, 0)->got_offset
;
393 /* we load the pc relative got offset */
394 *(int *)ptr
+= s1
->got
->sh_addr
+
395 get_sym_attr(s1
, sym_index
, 0)->got_offset
-
401 /* trade Thumb support for ARMv4 support */
402 if ((0x0ffffff0 & *(int*)ptr
) == 0x012FFF10)
403 *(int*)ptr
^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
406 case R_ARM_JUMP_SLOT
:
407 *(addr_t
*)ptr
= val
;
410 /* Nothing to do. Normally used to indicate a dependency
411 on a certain symbol (like for exception handling under EABI). */
415 add32le(ptr
, val
- s1
->pe_imagebase
);
420 fprintf(stderr
,"FIXME: handle reloc type %x at %x [%p] to %x\n",
421 type
, (unsigned)addr
, ptr
, (unsigned)val
);
426 #endif /* !TARGET_DEFS_ONLY */