LIBTCCAPI tcc_relocate(s) : REMOVED 2nd argument
[tinycc.git] / arm64-link.c
blob568ac1fef90394ff0bb3d2c5c3b9d442a31c8330
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 #ifdef NEED_RELOC_TYPE
25 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
26 relocations, returns -1. */
27 int code_reloc (int reloc_type)
29 switch (reloc_type) {
30 case R_AARCH64_ABS32:
31 case R_AARCH64_ABS64:
32 case R_AARCH64_PREL32:
33 case R_AARCH64_MOVW_UABS_G0_NC:
34 case R_AARCH64_MOVW_UABS_G1_NC:
35 case R_AARCH64_MOVW_UABS_G2_NC:
36 case R_AARCH64_MOVW_UABS_G3:
37 case R_AARCH64_ADR_PREL_PG_HI21:
38 case R_AARCH64_ADD_ABS_LO12_NC:
39 case R_AARCH64_ADR_GOT_PAGE:
40 case R_AARCH64_LD64_GOT_LO12_NC:
41 case R_AARCH64_LDST128_ABS_LO12_NC:
42 case R_AARCH64_LDST64_ABS_LO12_NC:
43 case R_AARCH64_LDST32_ABS_LO12_NC:
44 case R_AARCH64_LDST16_ABS_LO12_NC:
45 case R_AARCH64_LDST8_ABS_LO12_NC:
46 case R_AARCH64_GLOB_DAT:
47 case R_AARCH64_COPY:
48 return 0;
50 case R_AARCH64_JUMP26:
51 case R_AARCH64_CALL26:
52 case R_AARCH64_JUMP_SLOT:
53 return 1;
55 return -1;
58 /* Returns an enumerator to describe whether and when the relocation needs a
59 GOT and/or PLT entry to be created. See tcc.h for a description of the
60 different values. */
61 int gotplt_entry_type (int reloc_type)
63 switch (reloc_type) {
64 case R_AARCH64_PREL32:
65 case R_AARCH64_MOVW_UABS_G0_NC:
66 case R_AARCH64_MOVW_UABS_G1_NC:
67 case R_AARCH64_MOVW_UABS_G2_NC:
68 case R_AARCH64_MOVW_UABS_G3:
69 case R_AARCH64_ADR_PREL_PG_HI21:
70 case R_AARCH64_ADD_ABS_LO12_NC:
71 case R_AARCH64_LDST128_ABS_LO12_NC:
72 case R_AARCH64_LDST64_ABS_LO12_NC:
73 case R_AARCH64_LDST32_ABS_LO12_NC:
74 case R_AARCH64_LDST16_ABS_LO12_NC:
75 case R_AARCH64_LDST8_ABS_LO12_NC:
76 case R_AARCH64_GLOB_DAT:
77 case R_AARCH64_JUMP_SLOT:
78 case R_AARCH64_COPY:
79 return NO_GOTPLT_ENTRY;
81 case R_AARCH64_ABS32:
82 case R_AARCH64_ABS64:
83 case R_AARCH64_JUMP26:
84 case R_AARCH64_CALL26:
85 return AUTO_GOTPLT_ENTRY;
87 case R_AARCH64_ADR_GOT_PAGE:
88 case R_AARCH64_LD64_GOT_LO12_NC:
89 return ALWAYS_GOTPLT_ENTRY;
91 return -1;
94 #ifdef NEED_BUILD_GOT
95 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
97 Section *plt = s1->plt;
98 uint8_t *p;
99 unsigned plt_offset;
101 if (plt->data_offset == 0) {
102 section_ptr_add(plt, 32);
104 plt_offset = plt->data_offset;
106 p = section_ptr_add(plt, 16);
107 write32le(p, got_offset);
108 write32le(p + 4, (uint64_t) got_offset >> 32);
109 return plt_offset;
112 /* relocate the PLT: compute addresses and offsets in the PLT now that final
113 address for PLT and GOT are known (see fill_program_header) */
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_noabort("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_noabort("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);
168 #endif
169 #endif
171 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
173 int sym_index = ELFW(R_SYM)(rel->r_info), esym_index;
174 #ifdef DEBUG_RELOC
175 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
176 #endif
178 switch(type) {
179 case R_AARCH64_ABS64:
180 if ((s1->output_type & TCC_OUTPUT_DYN)) {
181 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
182 qrel->r_offset = rel->r_offset;
183 if (esym_index) {
184 qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_ABS64);
185 qrel->r_addend = rel->r_addend;
186 qrel++;
187 break;
188 } else {
189 qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
190 qrel->r_addend = read64le(ptr) + val;
191 qrel++;
194 add64le(ptr, val);
195 return;
196 case R_AARCH64_ABS32:
197 if (s1->output_type & TCC_OUTPUT_DYN) {
198 /* XXX: this logic may depend on TCC's codegen
199 now TCC uses R_AARCH64_RELATIVE even for a 64bit pointer */
200 qrel->r_offset = rel->r_offset;
201 qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
202 /* Use sign extension! */
203 qrel->r_addend = (int)read32le(ptr) + val;
204 qrel++;
206 add32le(ptr, val);
207 return;
208 case R_AARCH64_PREL32:
209 if (s1->output_type == TCC_OUTPUT_DLL) {
210 /* DLL relocation */
211 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
212 if (esym_index) {
213 qrel->r_offset = rel->r_offset;
214 qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_PREL32);
215 /* Use sign extension! */
216 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
217 qrel++;
218 break;
221 write32le(ptr, val - addr);
222 return;
223 case R_AARCH64_MOVW_UABS_G0_NC:
224 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
225 (val & 0xffff) << 5));
226 return;
227 case R_AARCH64_MOVW_UABS_G1_NC:
228 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
229 (val >> 16 & 0xffff) << 5));
230 return;
231 case R_AARCH64_MOVW_UABS_G2_NC:
232 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
233 (val >> 32 & 0xffff) << 5));
234 return;
235 case R_AARCH64_MOVW_UABS_G3:
236 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
237 (val >> 48 & 0xffff) << 5));
238 return;
239 case R_AARCH64_ADR_PREL_PG_HI21: {
240 uint64_t off = (val >> 12) - (addr >> 12);
241 if ((off + ((uint64_t)1 << 20)) >> 21)
242 tcc_error_noabort("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
243 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
244 (off & 0x1ffffc) << 3 | (off & 3) << 29));
245 return;
247 case R_AARCH64_ADD_ABS_LO12_NC:
248 case R_AARCH64_LDST8_ABS_LO12_NC:
249 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
250 (val & 0xfff) << 10));
251 return;
252 case R_AARCH64_LDST16_ABS_LO12_NC:
253 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
254 (val & 0xffe) << 9));
255 return;
256 case R_AARCH64_LDST32_ABS_LO12_NC:
257 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
258 (val & 0xffc) << 8));
259 return;
260 case R_AARCH64_LDST64_ABS_LO12_NC:
261 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
262 (val & 0xff8) << 7));
263 return;
264 case R_AARCH64_LDST128_ABS_LO12_NC:
265 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
266 (val & 0xff0) << 6));
267 return;
268 case R_AARCH64_JUMP26:
269 case R_AARCH64_CALL26:
270 #ifdef DEBUG_RELOC
271 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
272 (char *) symtab_section->link->data + sym->st_name);
273 #endif
274 if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
275 tcc_error_noabort("R_AARCH64_(JUMP|CALL)26 relocation failed"
276 " (val=%lx, addr=%lx)", (long)val, (long)addr);
277 write32le(ptr, (0x14000000 |
278 (uint32_t)(type == R_AARCH64_CALL26) << 31 |
279 ((val - addr) >> 2 & 0x3ffffff)));
280 return;
281 case R_AARCH64_ADR_GOT_PAGE: {
282 uint64_t off =
283 (((s1->got->sh_addr +
284 get_sym_attr(s1, sym_index, 0)->got_offset) >> 12) - (addr >> 12));
285 if ((off + ((uint64_t)1 << 20)) >> 21)
286 tcc_error_noabort("R_AARCH64_ADR_GOT_PAGE relocation failed");
287 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
288 (off & 0x1ffffc) << 3 | (off & 3) << 29));
289 return;
291 case R_AARCH64_LD64_GOT_LO12_NC:
292 write32le(ptr,
293 ((read32le(ptr) & 0xfff803ff) |
294 ((s1->got->sh_addr +
295 get_sym_attr(s1, sym_index, 0)->got_offset) & 0xff8) << 7));
296 return;
297 case R_AARCH64_COPY:
298 return;
299 case R_AARCH64_GLOB_DAT:
300 case R_AARCH64_JUMP_SLOT:
301 /* They don't need addend */
302 #ifdef DEBUG_RELOC
303 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr,
304 val - rel->r_addend,
305 (char *) symtab_section->link->data + sym->st_name);
306 #endif
307 write64le(ptr, val - rel->r_addend);
308 return;
309 case R_AARCH64_RELATIVE:
310 #ifdef TCC_TARGET_PE
311 add32le(ptr, val - s1->pe_imagebase);
312 #endif
313 /* do nothing */
314 return;
315 default:
316 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
317 type, (unsigned)addr, ptr, (unsigned)val);
318 return;
322 #endif /* !TARGET_DEFS_ONLY */