Fix cast_test for clang.
[tinycc.git] / arm64-link.c
blobb8c9c0c8889ad3151cf2329cab4ff31546904b0e
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 (s1->output_type == TCC_OUTPUT_DLL)
92 tcc_error("DLLs unimplemented!");
94 if (plt->data_offset == 0) {
95 section_ptr_add(plt, 32);
97 plt_offset = plt->data_offset;
99 p = section_ptr_add(plt, 16);
100 write32le(p, got_offset);
101 write32le(p + 4, (uint64_t) got_offset >> 32);
102 return plt_offset;
105 /* relocate the PLT: compute addresses and offsets in the PLT now that final
106 address for PLT and GOT are known (see fill_program_header) */
107 ST_FUNC void relocate_plt(TCCState *s1)
109 uint8_t *p, *p_end;
111 if (!s1->plt)
112 return;
114 p = s1->plt->data;
115 p_end = p + s1->plt->data_offset;
117 if (p < p_end) {
118 uint64_t plt = s1->plt->sh_addr;
119 uint64_t got = s1->got->sh_addr;
120 uint64_t off = (got >> 12) - (plt >> 12);
121 if ((off + ((uint32_t)1 << 20)) >> 21)
122 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", off, got, plt);
123 write32le(p, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]!
124 write32le(p + 4, (0x90000010 | // adrp x16,...
125 (off & 0x1ffffc) << 3 | (off & 3) << 29));
126 write32le(p + 8, (0xf9400211 | // ldr x17,[x16,#...]
127 (got & 0xff8) << 7));
128 write32le(p + 12, (0x91000210 | // add x16,x16,#...
129 (got & 0xfff) << 10));
130 write32le(p + 16, 0xd61f0220); // br x17
131 write32le(p + 20, 0xd503201f); // nop
132 write32le(p + 24, 0xd503201f); // nop
133 write32le(p + 28, 0xd503201f); // nop
134 p += 32;
135 while (p < p_end) {
136 uint64_t pc = plt + (p - s1->plt->data);
137 uint64_t addr = got + read64le(p);
138 uint64_t off = (addr >> 12) - (pc >> 12);
139 if ((off + ((uint32_t)1 << 20)) >> 21)
140 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off, addr, pc);
141 write32le(p, (0x90000010 | // adrp x16,...
142 (off & 0x1ffffc) << 3 | (off & 3) << 29));
143 write32le(p + 4, (0xf9400211 | // ldr x17,[x16,#...]
144 (addr & 0xff8) << 7));
145 write32le(p + 8, (0x91000210 | // add x16,x16,#...
146 (addr & 0xfff) << 10));
147 write32le(p + 12, 0xd61f0220); // br x17
148 p += 16;
153 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
155 int sym_index = ELFW(R_SYM)(rel->r_info);
156 #ifdef DEBUG_RELOC
157 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
158 #endif
160 switch(type) {
161 case R_AARCH64_ABS64:
162 add64le(ptr, val);
163 return;
164 case R_AARCH64_ABS32:
165 add32le(ptr, val);
166 return;
167 case R_AARCH64_PREL32:
168 write32le(ptr, val - addr);
169 return;
170 case R_AARCH64_MOVW_UABS_G0_NC:
171 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
172 (val & 0xffff) << 5));
173 return;
174 case R_AARCH64_MOVW_UABS_G1_NC:
175 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
176 (val >> 16 & 0xffff) << 5));
177 return;
178 case R_AARCH64_MOVW_UABS_G2_NC:
179 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
180 (val >> 32 & 0xffff) << 5));
181 return;
182 case R_AARCH64_MOVW_UABS_G3:
183 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
184 (val >> 48 & 0xffff) << 5));
185 return;
186 case R_AARCH64_ADR_PREL_PG_HI21: {
187 uint64_t off = (val >> 12) - (addr >> 12);
188 if ((off + ((uint64_t)1 << 20)) >> 21)
189 tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
190 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
191 (off & 0x1ffffc) << 3 | (off & 3) << 29));
192 return;
194 case R_AARCH64_ADD_ABS_LO12_NC:
195 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
196 (val & 0xfff) << 10));
197 return;
198 case R_AARCH64_LDST64_ABS_LO12_NC:
199 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
200 (val & 0xff8) << 7));
201 return;
202 case R_AARCH64_JUMP26:
203 case R_AARCH64_CALL26:
204 #ifdef DEBUG_RELOC
205 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
206 (char *) symtab_section->link->data + sym->st_name);
207 #endif
208 if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
209 tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed"
210 " (val=%lx, addr=%lx)", val, addr);
211 write32le(ptr, (0x14000000 |
212 (uint32_t)(type == R_AARCH64_CALL26) << 31 |
213 ((val - addr) >> 2 & 0x3ffffff)));
214 return;
215 case R_AARCH64_ADR_GOT_PAGE: {
216 uint64_t off =
217 (((s1->got->sh_addr +
218 get_sym_attr(s1, sym_index, 0)->got_offset) >> 12) - (addr >> 12));
219 if ((off + ((uint64_t)1 << 20)) >> 21)
220 tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed");
221 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
222 (off & 0x1ffffc) << 3 | (off & 3) << 29));
223 return;
225 case R_AARCH64_LD64_GOT_LO12_NC:
226 write32le(ptr,
227 ((read32le(ptr) & 0xfff803ff) |
228 ((s1->got->sh_addr +
229 get_sym_attr(s1, sym_index, 0)->got_offset) & 0xff8) << 7));
230 return;
231 case R_AARCH64_COPY:
232 return;
233 case R_AARCH64_GLOB_DAT:
234 case R_AARCH64_JUMP_SLOT:
235 /* They don't need addend */
236 #ifdef DEBUG_RELOC
237 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr,
238 val - rel->r_addend,
239 (char *) symtab_section->link->data + sym->st_name);
240 #endif
241 write64le(ptr, val - rel->r_addend);
242 return;
243 case R_AARCH64_RELATIVE:
244 #ifdef TCC_TARGET_PE
245 add32le(ptr, val - s1->pe_imagebase);
246 #endif
247 /* do nothing */
248 return;
249 default:
250 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
251 type, (unsigned)addr, ptr, (unsigned)val);
252 return;
256 #endif /* !TARGET_DEFS_ONLY */