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
- 4);
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]!
161 if (s1
->got
->relocplt
) {
162 int mem
= s1
->output_type
== TCC_OUTPUT_MEMORY
;
166 for_each_elem(s1
->got
->relocplt
, 0, rel
, ElfW_Rel
) {
167 int sym_index
= ELFW(R_SYM
)(rel
->r_info
);
168 ElfW(Sym
) *sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
169 write32le(p
+ rel
->r_offset
, mem
? sym
->st_value
: s1
->plt
->sh_addr
);
175 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
178 int sym_index
, esym_index
;
180 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
181 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
189 int x
, is_thumb
, is_call
, h
, blx_avail
, is_bl
, th_ko
;
190 x
= (*(int *) ptr
) & 0xffffff;
192 printf ("reloc %d: x=0x%x val=0x%x ", type
, x
, val
);
194 (*(int *)ptr
) &= 0xff000000;
198 blx_avail
= (TCC_CPU_VERSION
>= 5);
200 is_bl
= (*(unsigned *) ptr
) >> 24 == 0xeb;
201 is_call
= (type
== R_ARM_CALL
|| (type
== R_ARM_PC24
&& is_bl
));
204 printf (" newx=0x%x name=%s\n", x
,
205 (char *) symtab_section
->link
->data
+ sym
->st_name
);
208 th_ko
= (x
& 3) && (!blx_avail
|| !is_call
);
209 if (th_ko
|| x
>= 0x2000000 || x
< -0x2000000)
210 tcc_error("can't relocate value at %x,%d",addr
, type
);
213 /* Only reached if blx is avail and it is a call */
216 (*(int *)ptr
) = 0xfa << 24; /* bl -> blx */
221 /* Since these relocations only concern Thumb-2 and blx instruction was
222 introduced before Thumb-2, we can assume blx is available and not
225 case R_ARM_THM_JUMP24
:
227 int x
, hi
, lo
, s
, j1
, j2
, i1
, i2
, imm10
, imm11
;
228 int to_thumb
, is_call
, to_plt
, blx_bit
= 1 << 12;
232 if (sym
->st_shndx
== SHN_UNDEF
&&
233 ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
)
236 /* Get initial offset */
237 hi
= (*(uint16_t *)ptr
);
238 lo
= (*(uint16_t *)(ptr
+2));
246 x
= (s
<< 24) | (i1
<< 23) | (i2
<< 22) |
247 (imm10
<< 12) | (imm11
<< 1);
251 /* Relocation infos */
254 to_plt
= (val
>= plt
->sh_addr
) &&
255 (val
< plt
->sh_addr
+ plt
->data_offset
);
256 is_call
= (type
== R_ARM_THM_PC22
);
258 if (!to_thumb
&& !to_plt
&& !is_call
) {
261 char *name
, buf
[1024];
264 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
265 text
= s1
->sections
[sym
->st_shndx
];
266 /* Modify reloc to target a thumb stub to switch to ARM */
267 snprintf(buf
, sizeof(buf
), "%s_from_thumb", name
);
268 index
= put_elf_sym(symtab_section
,
269 text
->data_offset
+ 1,
270 sym
->st_size
, sym
->st_info
, 0,
273 val
= text
->data_offset
+ 1;
274 rel
->r_info
= ELFW(R_INFO
)(index
, type
);
275 /* Create a thumb stub function to switch to ARM mode */
276 put_elf_reloc(symtab_section
, text
,
277 text
->data_offset
+ 4, R_ARM_JUMP24
,
279 p
= section_ptr_add(text
, 8);
280 write32le(p
, 0x4778); /* bx pc */
281 write32le(p
+2, 0x46c0); /* nop */
282 write32le(p
+4, 0xeafffffe); /* b $sym */
285 /* Compute final offset */
287 if (!to_thumb
&& is_call
) {
288 blx_bit
= 0; /* bl -> blx */
289 x
= (x
+ 3) & -4; /* Compute offset from aligned PC */
292 /* Check that relocation is possible
293 * offset must not be out of range
294 * if target is to be entered in arm mode:
296 - instruction must be a call (bl) or a jump to PLT */
297 if (!to_thumb
|| x
>= 0x1000000 || x
< -0x1000000)
298 if (to_thumb
|| (val
& 2) || (!is_call
&& !to_plt
))
299 tcc_error("can't relocate value at %x,%d",addr
, type
);
301 /* Compute and store final offset */
307 imm10
= (x
>> 12) & 0x3ff;
308 imm11
= (x
>> 1) & 0x7ff;
309 (*(uint16_t *)ptr
) = (uint16_t) ((hi
& 0xf800) |
311 (*(uint16_t *)(ptr
+2)) = (uint16_t) ((lo
& 0xc000) |
312 (j1
<< 13) | blx_bit
| (j2
<< 11) |
317 case R_ARM_MOVW_ABS_NC
:
320 if (type
== R_ARM_MOVT_ABS
)
323 imm4
= (val
>> 12) & 0xf;
324 x
= (imm4
<< 16) | imm12
;
325 if (type
== R_ARM_THM_MOVT_ABS
)
331 case R_ARM_THM_MOVT_ABS
:
332 case R_ARM_THM_MOVW_ABS_NC
:
334 int x
, i
, imm4
, imm3
, imm8
;
335 if (type
== R_ARM_THM_MOVT_ABS
)
338 imm3
= (val
>> 8) & 0x7;
340 imm4
= (val
>> 12) & 0xf;
341 x
= (imm3
<< 28) | (imm8
<< 16) | (i
<< 10) | imm4
;
342 if (type
== R_ARM_THM_MOVT_ABS
)
351 x
= (*(int *)ptr
) & 0x7fffffff;
352 (*(int *)ptr
) &= 0x80000000;
355 if((x
^(x
>>1))&0x40000000)
356 tcc_error("can't relocate value at %x,%d",addr
, type
);
357 (*(int *)ptr
) |= x
& 0x7fffffff;
360 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
361 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
362 qrel
->r_offset
= rel
->r_offset
;
364 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_ARM_ABS32
);
368 qrel
->r_info
= ELFW(R_INFO
)(0, R_ARM_RELATIVE
);
375 *(int *)ptr
+= val
- addr
;
378 *(int *)ptr
+= s1
->got
->sh_addr
- addr
;
381 *(int *)ptr
+= val
- s1
->got
->sh_addr
;
384 /* we load the got offset */
385 *(int *)ptr
+= get_sym_attr(s1
, sym_index
, 0)->got_offset
;
390 /* trade Thumb support for ARMv4 support */
391 if ((0x0ffffff0 & *(int*)ptr
) == 0x012FFF10)
392 *(int*)ptr
^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
395 case R_ARM_JUMP_SLOT
:
396 *(addr_t
*)ptr
= val
;
399 /* Nothing to do. Normally used to indicate a dependency
400 on a certain symbol (like for exception handling under EABI). */
404 add32le(ptr
, val
- s1
->pe_imagebase
);
409 fprintf(stderr
,"FIXME: handle reloc type %x at %x [%p] to %x\n",
410 type
, (unsigned)addr
, ptr
, (unsigned)val
);
415 #endif /* !TARGET_DEFS_ONLY */