tcc.h: Extend search path for include, lib and crt.
[tinycc.git] / arm64-link.c
blob99916693bb4461266947ce323dbaa5cb26b6c3e6
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_LDST128_ABS_LO12_NC:
41 case R_AARCH64_LDST64_ABS_LO12_NC:
42 case R_AARCH64_LDST32_ABS_LO12_NC:
43 case R_AARCH64_LDST16_ABS_LO12_NC:
44 case R_AARCH64_LDST8_ABS_LO12_NC:
45 case R_AARCH64_GLOB_DAT:
46 case R_AARCH64_COPY:
47 return 0;
49 case R_AARCH64_JUMP26:
50 case R_AARCH64_CALL26:
51 case R_AARCH64_JUMP_SLOT:
52 return 1;
54 return -1;
57 /* Returns an enumerator to describe whether and when the relocation needs a
58 GOT and/or PLT entry to be created. See tcc.h for a description of the
59 different values. */
60 int gotplt_entry_type (int reloc_type)
62 switch (reloc_type) {
63 case R_AARCH64_PREL32:
64 case R_AARCH64_MOVW_UABS_G0_NC:
65 case R_AARCH64_MOVW_UABS_G1_NC:
66 case R_AARCH64_MOVW_UABS_G2_NC:
67 case R_AARCH64_MOVW_UABS_G3:
68 case R_AARCH64_ADR_PREL_PG_HI21:
69 case R_AARCH64_ADD_ABS_LO12_NC:
70 case R_AARCH64_LDST128_ABS_LO12_NC:
71 case R_AARCH64_LDST64_ABS_LO12_NC:
72 case R_AARCH64_LDST32_ABS_LO12_NC:
73 case R_AARCH64_LDST16_ABS_LO12_NC:
74 case R_AARCH64_LDST8_ABS_LO12_NC:
75 case R_AARCH64_GLOB_DAT:
76 case R_AARCH64_JUMP_SLOT:
77 case R_AARCH64_COPY:
78 return NO_GOTPLT_ENTRY;
80 case R_AARCH64_ABS32:
81 case R_AARCH64_ABS64:
82 case R_AARCH64_JUMP26:
83 case R_AARCH64_CALL26:
84 return AUTO_GOTPLT_ENTRY;
86 case R_AARCH64_ADR_GOT_PAGE:
87 case R_AARCH64_LD64_GOT_LO12_NC:
88 return ALWAYS_GOTPLT_ENTRY;
90 return -1;
93 __attribute__((unused))
94 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
96 Section *plt = s1->plt;
97 uint8_t *p;
98 unsigned plt_offset;
100 if (plt->data_offset == 0) {
101 section_ptr_add(plt, 32);
103 plt_offset = plt->data_offset;
105 p = section_ptr_add(plt, 16);
106 write32le(p, got_offset);
107 write32le(p + 4, (uint64_t) got_offset >> 32);
108 return plt_offset;
111 /* relocate the PLT: compute addresses and offsets in the PLT now that final
112 address for PLT and GOT are known (see fill_program_header) */
113 __attribute__((unused))
114 ST_FUNC void relocate_plt(TCCState *s1)
116 uint8_t *p, *p_end;
118 if (!s1->plt)
119 return;
121 p = s1->plt->data;
122 p_end = p + s1->plt->data_offset;
124 if (p < p_end) {
125 uint64_t plt = s1->plt->sh_addr;
126 uint64_t got = s1->got->sh_addr + 16;
127 uint64_t off = (got >> 12) - (plt >> 12);
128 if ((off + ((uint32_t)1 << 20)) >> 21)
129 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", (long)off, (long)got, (long)plt);
130 write32le(p, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]!
131 write32le(p + 4, (0x90000010 | // adrp x16,...
132 (off & 0x1ffffc) << 3 | (off & 3) << 29));
133 write32le(p + 8, (0xf9400211 | // ldr x17,[x16,#...]
134 (got & 0xff8) << 7));
135 write32le(p + 12, (0x91000210 | // add x16,x16,#...
136 (got & 0xfff) << 10));
137 write32le(p + 16, 0xd61f0220); // br x17
138 write32le(p + 20, 0xd503201f); // nop
139 write32le(p + 24, 0xd503201f); // nop
140 write32le(p + 28, 0xd503201f); // nop
141 p += 32;
142 got = s1->got->sh_addr;
143 while (p < p_end) {
144 uint64_t pc = plt + (p - s1->plt->data);
145 uint64_t addr = got + read64le(p);
146 uint64_t off = (addr >> 12) - (pc >> 12);
147 if ((off + ((uint32_t)1 << 20)) >> 21)
148 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", (long)off, (long)addr, (long)pc);
149 write32le(p, (0x90000010 | // adrp x16,...
150 (off & 0x1ffffc) << 3 | (off & 3) << 29));
151 write32le(p + 4, (0xf9400211 | // ldr x17,[x16,#...]
152 (addr & 0xff8) << 7));
153 write32le(p + 8, (0x91000210 | // add x16,x16,#...
154 (addr & 0xfff) << 10));
155 write32le(p + 12, 0xd61f0220); // br x17
156 p += 16;
160 if (s1->plt->reloc) {
161 ElfW_Rel *rel;
162 p = s1->got->data;
163 for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
164 write64le(p + rel->r_offset, s1->plt->sh_addr);
169 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
171 int sym_index = ELFW(R_SYM)(rel->r_info), esym_index;
172 #ifdef DEBUG_RELOC
173 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
174 #endif
176 switch(type) {
177 case R_AARCH64_ABS64:
178 if (s1->output_type == TCC_OUTPUT_DLL) {
179 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
180 qrel->r_offset = rel->r_offset;
181 if (esym_index) {
182 qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_ABS64);
183 qrel->r_addend = rel->r_addend;
184 qrel++;
185 break;
186 } else {
187 qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
188 qrel->r_addend = read64le(ptr) + val;
189 qrel++;
192 add64le(ptr, val);
193 return;
194 case R_AARCH64_ABS32:
195 if (s1->output_type == TCC_OUTPUT_DLL) {
196 /* XXX: this logic may depend on TCC's codegen
197 now TCC uses R_AARCH64_RELATIVE even for a 64bit pointer */
198 qrel->r_offset = rel->r_offset;
199 qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
200 /* Use sign extension! */
201 qrel->r_addend = (int)read32le(ptr) + val;
202 qrel++;
204 add32le(ptr, val);
205 return;
206 case R_AARCH64_PREL32:
207 if (s1->output_type == TCC_OUTPUT_DLL) {
208 /* DLL relocation */
209 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
210 if (esym_index) {
211 qrel->r_offset = rel->r_offset;
212 qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_PREL32);
213 /* Use sign extension! */
214 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
215 qrel++;
216 break;
219 write32le(ptr, val - addr);
220 return;
221 case R_AARCH64_MOVW_UABS_G0_NC:
222 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
223 (val & 0xffff) << 5));
224 return;
225 case R_AARCH64_MOVW_UABS_G1_NC:
226 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
227 (val >> 16 & 0xffff) << 5));
228 return;
229 case R_AARCH64_MOVW_UABS_G2_NC:
230 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
231 (val >> 32 & 0xffff) << 5));
232 return;
233 case R_AARCH64_MOVW_UABS_G3:
234 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
235 (val >> 48 & 0xffff) << 5));
236 return;
237 case R_AARCH64_ADR_PREL_PG_HI21: {
238 uint64_t off = (val >> 12) - (addr >> 12);
239 if ((off + ((uint64_t)1 << 20)) >> 21)
240 tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
241 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
242 (off & 0x1ffffc) << 3 | (off & 3) << 29));
243 return;
245 case R_AARCH64_ADD_ABS_LO12_NC:
246 case R_AARCH64_LDST8_ABS_LO12_NC:
247 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
248 (val & 0xfff) << 10));
249 return;
250 case R_AARCH64_LDST16_ABS_LO12_NC:
251 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
252 (val & 0xffe) << 9));
253 return;
254 case R_AARCH64_LDST32_ABS_LO12_NC:
255 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
256 (val & 0xffc) << 8));
257 return;
258 case R_AARCH64_LDST64_ABS_LO12_NC:
259 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
260 (val & 0xff8) << 7));
261 return;
262 case R_AARCH64_LDST128_ABS_LO12_NC:
263 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
264 (val & 0xff0) << 6));
265 return;
266 case R_AARCH64_JUMP26:
267 case R_AARCH64_CALL26:
268 #ifdef DEBUG_RELOC
269 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
270 (char *) symtab_section->link->data + sym->st_name);
271 #endif
272 if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
273 tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed"
274 " (val=%lx, addr=%lx)", (long)val, (long)addr);
275 write32le(ptr, (0x14000000 |
276 (uint32_t)(type == R_AARCH64_CALL26) << 31 |
277 ((val - addr) >> 2 & 0x3ffffff)));
278 return;
279 case R_AARCH64_ADR_GOT_PAGE: {
280 uint64_t off =
281 (((s1->got->sh_addr +
282 get_sym_attr(s1, sym_index, 0)->got_offset) >> 12) - (addr >> 12));
283 if ((off + ((uint64_t)1 << 20)) >> 21)
284 tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed");
285 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
286 (off & 0x1ffffc) << 3 | (off & 3) << 29));
287 return;
289 case R_AARCH64_LD64_GOT_LO12_NC:
290 write32le(ptr,
291 ((read32le(ptr) & 0xfff803ff) |
292 ((s1->got->sh_addr +
293 get_sym_attr(s1, sym_index, 0)->got_offset) & 0xff8) << 7));
294 return;
295 case R_AARCH64_COPY:
296 return;
297 case R_AARCH64_GLOB_DAT:
298 case R_AARCH64_JUMP_SLOT:
299 /* They don't need addend */
300 #ifdef DEBUG_RELOC
301 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr,
302 val - rel->r_addend,
303 (char *) symtab_section->link->data + sym->st_name);
304 #endif
305 write64le(ptr, val - rel->r_addend);
306 return;
307 case R_AARCH64_RELATIVE:
308 #ifdef TCC_TARGET_PE
309 add32le(ptr, val - s1->pe_imagebase);
310 #endif
311 /* do nothing */
312 return;
313 default:
314 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
315 type, (unsigned)addr, ptr, (unsigned)val);
316 return;
320 #endif /* !TARGET_DEFS_ONLY */