OpenBSD: fix typo in _ANSI_LIBRARY defined symbol
[tinycc/tcc_android_arm.git] / arm64-link.c
blobe0d6c6e32f740bb62fba75898b0b7e0806fd4557
1 #ifdef TARGET_DEFS_ONLY
3 #define EM_TCC_TARGET EM_AARCH64
5 #define R_DATA_32 R_AARCH64_ABS32
6 #define R_DATA_PTR R_AARCH64_ABS64
7 #define R_JMP_SLOT R_AARCH64_JUMP_SLOT
8 #define R_GLOB_DAT R_AARCH64_GLOB_DAT
9 #define R_COPY R_AARCH64_COPY
10 #define R_RELATIVE R_AARCH64_RELATIVE
12 #define R_NUM R_AARCH64_NUM
14 #define ELF_START_ADDR 0x00400000
15 #define ELF_PAGE_SIZE 0x10000
17 #define PCRELATIVE_DLLPLT 1
18 #define RELOCATE_DLLPLT 1
20 #else /* !TARGET_DEFS_ONLY */
22 #include "tcc.h"
24 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
25 relocations, returns -1. */
26 int code_reloc (int reloc_type)
28 switch (reloc_type) {
29 case R_AARCH64_ABS32:
30 case R_AARCH64_ABS64:
31 case R_AARCH64_PREL32:
32 case R_AARCH64_MOVW_UABS_G0_NC:
33 case R_AARCH64_MOVW_UABS_G1_NC:
34 case R_AARCH64_MOVW_UABS_G2_NC:
35 case R_AARCH64_MOVW_UABS_G3:
36 case R_AARCH64_ADR_PREL_PG_HI21:
37 case R_AARCH64_ADD_ABS_LO12_NC:
38 case R_AARCH64_ADR_GOT_PAGE:
39 case R_AARCH64_LD64_GOT_LO12_NC:
40 case R_AARCH64_LDST64_ABS_LO12_NC:
41 case R_AARCH64_GLOB_DAT:
42 case R_AARCH64_COPY:
43 return 0;
45 case R_AARCH64_JUMP26:
46 case R_AARCH64_CALL26:
47 case R_AARCH64_JUMP_SLOT:
48 return 1;
50 return -1;
53 /* Returns an enumerator to describe whether and when the relocation needs a
54 GOT and/or PLT entry to be created. See tcc.h for a description of the
55 different values. */
56 int gotplt_entry_type (int reloc_type)
58 switch (reloc_type) {
59 case R_AARCH64_PREL32:
60 case R_AARCH64_MOVW_UABS_G0_NC:
61 case R_AARCH64_MOVW_UABS_G1_NC:
62 case R_AARCH64_MOVW_UABS_G2_NC:
63 case R_AARCH64_MOVW_UABS_G3:
64 case R_AARCH64_ADR_PREL_PG_HI21:
65 case R_AARCH64_ADD_ABS_LO12_NC:
66 case R_AARCH64_LDST64_ABS_LO12_NC:
67 case R_AARCH64_GLOB_DAT:
68 case R_AARCH64_JUMP_SLOT:
69 case R_AARCH64_COPY:
70 return NO_GOTPLT_ENTRY;
72 case R_AARCH64_ABS32:
73 case R_AARCH64_ABS64:
74 case R_AARCH64_JUMP26:
75 case R_AARCH64_CALL26:
76 return AUTO_GOTPLT_ENTRY;
78 case R_AARCH64_ADR_GOT_PAGE:
79 case R_AARCH64_LD64_GOT_LO12_NC:
80 return ALWAYS_GOTPLT_ENTRY;
82 return -1;
85 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
87 Section *plt = s1->plt;
88 uint8_t *p;
89 unsigned plt_offset;
91 if (plt->data_offset == 0) {
92 section_ptr_add(plt, 32);
94 plt_offset = plt->data_offset;
96 p = section_ptr_add(plt, 16);
97 write32le(p, got_offset);
98 write32le(p + 4, (uint64_t) got_offset >> 32);
99 return plt_offset;
102 /* relocate the PLT: compute addresses and offsets in the PLT now that final
103 address for PLT and GOT are known (see fill_program_header) */
104 ST_FUNC void relocate_plt(TCCState *s1)
106 uint8_t *p, *p_end;
108 if (!s1->plt)
109 return;
111 p = s1->plt->data;
112 p_end = p + s1->plt->data_offset;
114 if (p < p_end) {
115 uint64_t plt = s1->plt->sh_addr;
116 uint64_t got = s1->got->sh_addr;
117 uint64_t off = (got >> 12) - (plt >> 12);
118 if ((off + ((uint32_t)1 << 20)) >> 21)
119 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", (long)off, (long)got, (long)plt);
120 write32le(p, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]!
121 write32le(p + 4, (0x90000010 | // adrp x16,...
122 (off & 0x1ffffc) << 3 | (off & 3) << 29));
123 write32le(p + 8, (0xf9400211 | // ldr x17,[x16,#...]
124 (got & 0xff8) << 7));
125 write32le(p + 12, (0x91000210 | // add x16,x16,#...
126 (got & 0xfff) << 10));
127 write32le(p + 16, 0xd61f0220); // br x17
128 write32le(p + 20, 0xd503201f); // nop
129 write32le(p + 24, 0xd503201f); // nop
130 write32le(p + 28, 0xd503201f); // nop
131 p += 32;
132 while (p < p_end) {
133 uint64_t pc = plt + (p - s1->plt->data);
134 uint64_t addr = got + read64le(p);
135 uint64_t off = (addr >> 12) - (pc >> 12);
136 if ((off + ((uint32_t)1 << 20)) >> 21)
137 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", (long)off, (long)addr, (long)pc);
138 write32le(p, (0x90000010 | // adrp x16,...
139 (off & 0x1ffffc) << 3 | (off & 3) << 29));
140 write32le(p + 4, (0xf9400211 | // ldr x17,[x16,#...]
141 (addr & 0xff8) << 7));
142 write32le(p + 8, (0x91000210 | // add x16,x16,#...
143 (addr & 0xfff) << 10));
144 write32le(p + 12, 0xd61f0220); // br x17
145 p += 16;
150 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
152 int sym_index = ELFW(R_SYM)(rel->r_info), esym_index;
153 #ifdef DEBUG_RELOC
154 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
155 #endif
157 switch(type) {
158 case R_AARCH64_ABS64:
159 if (s1->output_type == TCC_OUTPUT_DLL) {
160 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
161 qrel->r_offset = rel->r_offset;
162 if (esym_index) {
163 qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_ABS64);
164 qrel->r_addend = rel->r_addend;
165 qrel++;
166 break;
167 } else {
168 qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
169 qrel->r_addend = read64le(ptr) + val;
170 qrel++;
173 add64le(ptr, val);
174 return;
175 case R_AARCH64_ABS32:
176 if (s1->output_type == TCC_OUTPUT_DLL) {
177 /* XXX: this logic may depend on TCC's codegen
178 now TCC uses R_AARCH64_RELATIVE even for a 64bit pointer */
179 qrel->r_offset = rel->r_offset;
180 qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
181 /* Use sign extension! */
182 qrel->r_addend = (int)read32le(ptr) + val;
183 qrel++;
185 add32le(ptr, val);
186 return;
187 case R_AARCH64_PREL32:
188 if (s1->output_type == TCC_OUTPUT_DLL) {
189 /* DLL relocation */
190 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
191 if (esym_index) {
192 qrel->r_offset = rel->r_offset;
193 qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_PREL32);
194 /* Use sign extension! */
195 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
196 qrel++;
197 break;
200 write32le(ptr, val - addr);
201 return;
202 case R_AARCH64_MOVW_UABS_G0_NC:
203 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
204 (val & 0xffff) << 5));
205 return;
206 case R_AARCH64_MOVW_UABS_G1_NC:
207 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
208 (val >> 16 & 0xffff) << 5));
209 return;
210 case R_AARCH64_MOVW_UABS_G2_NC:
211 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
212 (val >> 32 & 0xffff) << 5));
213 return;
214 case R_AARCH64_MOVW_UABS_G3:
215 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
216 (val >> 48 & 0xffff) << 5));
217 return;
218 case R_AARCH64_ADR_PREL_PG_HI21: {
219 uint64_t off = (val >> 12) - (addr >> 12);
220 if ((off + ((uint64_t)1 << 20)) >> 21)
221 tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
222 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
223 (off & 0x1ffffc) << 3 | (off & 3) << 29));
224 return;
226 case R_AARCH64_ADD_ABS_LO12_NC:
227 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
228 (val & 0xfff) << 10));
229 return;
230 case R_AARCH64_LDST64_ABS_LO12_NC:
231 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
232 (val & 0xff8) << 7));
233 return;
234 case R_AARCH64_JUMP26:
235 case R_AARCH64_CALL26:
236 #ifdef DEBUG_RELOC
237 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
238 (char *) symtab_section->link->data + sym->st_name);
239 #endif
240 if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
241 tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed"
242 " (val=%lx, addr=%lx)", (long)val, (long)addr);
243 write32le(ptr, (0x14000000 |
244 (uint32_t)(type == R_AARCH64_CALL26) << 31 |
245 ((val - addr) >> 2 & 0x3ffffff)));
246 return;
247 case R_AARCH64_ADR_GOT_PAGE: {
248 uint64_t off =
249 (((s1->got->sh_addr +
250 get_sym_attr(s1, sym_index, 0)->got_offset) >> 12) - (addr >> 12));
251 if ((off + ((uint64_t)1 << 20)) >> 21)
252 tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed");
253 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
254 (off & 0x1ffffc) << 3 | (off & 3) << 29));
255 return;
257 case R_AARCH64_LD64_GOT_LO12_NC:
258 write32le(ptr,
259 ((read32le(ptr) & 0xfff803ff) |
260 ((s1->got->sh_addr +
261 get_sym_attr(s1, sym_index, 0)->got_offset) & 0xff8) << 7));
262 return;
263 case R_AARCH64_COPY:
264 return;
265 case R_AARCH64_GLOB_DAT:
266 case R_AARCH64_JUMP_SLOT:
267 /* They don't need addend */
268 #ifdef DEBUG_RELOC
269 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr,
270 val - rel->r_addend,
271 (char *) symtab_section->link->data + sym->st_name);
272 #endif
273 write64le(ptr, val - rel->r_addend);
274 return;
275 case R_AARCH64_RELATIVE:
276 #ifdef TCC_TARGET_PE
277 add32le(ptr, val - s1->pe_imagebase);
278 #endif
279 /* do nothing */
280 return;
281 default:
282 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
283 type, (unsigned)addr, ptr, (unsigned)val);
284 return;
288 #endif /* !TARGET_DEFS_ONLY */