Fix include SHT_NOTE sections everywhere
[tinycc/self_contained.git] / arm64-link.c
blob5fec77d396150f85dc04deacad9888086a46d25b
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_LDST8_ABS_LO12_NC:
42 case R_AARCH64_GLOB_DAT:
43 case R_AARCH64_COPY:
44 return 0;
46 case R_AARCH64_JUMP26:
47 case R_AARCH64_CALL26:
48 case R_AARCH64_JUMP_SLOT:
49 return 1;
51 return -1;
54 /* Returns an enumerator to describe whether and when the relocation needs a
55 GOT and/or PLT entry to be created. See tcc.h for a description of the
56 different values. */
57 int gotplt_entry_type (int reloc_type)
59 switch (reloc_type) {
60 case R_AARCH64_PREL32:
61 case R_AARCH64_MOVW_UABS_G0_NC:
62 case R_AARCH64_MOVW_UABS_G1_NC:
63 case R_AARCH64_MOVW_UABS_G2_NC:
64 case R_AARCH64_MOVW_UABS_G3:
65 case R_AARCH64_ADR_PREL_PG_HI21:
66 case R_AARCH64_ADD_ABS_LO12_NC:
67 case R_AARCH64_LDST64_ABS_LO12_NC:
68 case R_AARCH64_LDST8_ABS_LO12_NC:
69 case R_AARCH64_GLOB_DAT:
70 case R_AARCH64_JUMP_SLOT:
71 case R_AARCH64_COPY:
72 return NO_GOTPLT_ENTRY;
74 case R_AARCH64_ABS32:
75 case R_AARCH64_ABS64:
76 case R_AARCH64_JUMP26:
77 case R_AARCH64_CALL26:
78 return AUTO_GOTPLT_ENTRY;
80 case R_AARCH64_ADR_GOT_PAGE:
81 case R_AARCH64_LD64_GOT_LO12_NC:
82 return ALWAYS_GOTPLT_ENTRY;
84 return -1;
87 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
89 Section *plt = s1->plt;
90 uint8_t *p;
91 unsigned plt_offset;
93 if (plt->data_offset == 0) {
94 section_ptr_add(plt, 32);
96 plt_offset = plt->data_offset;
98 p = section_ptr_add(plt, 16);
99 write32le(p, got_offset);
100 write32le(p + 4, (uint64_t) got_offset >> 32);
101 return plt_offset;
104 /* relocate the PLT: compute addresses and offsets in the PLT now that final
105 address for PLT and GOT are known (see fill_program_header) */
106 ST_FUNC void relocate_plt(TCCState *s1)
108 uint8_t *p, *p_end;
110 if (!s1->plt)
111 return;
113 p = s1->plt->data;
114 p_end = p + s1->plt->data_offset;
116 if (p < p_end) {
117 uint64_t plt = s1->plt->sh_addr;
118 uint64_t got = s1->got->sh_addr + 16;
119 uint64_t off = (got >> 12) - (plt >> 12);
120 if ((off + ((uint32_t)1 << 20)) >> 21)
121 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", (long)off, (long)got, (long)plt);
122 write32le(p, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]!
123 write32le(p + 4, (0x90000010 | // adrp x16,...
124 (off & 0x1ffffc) << 3 | (off & 3) << 29));
125 write32le(p + 8, (0xf9400211 | // ldr x17,[x16,#...]
126 (got & 0xff8) << 7));
127 write32le(p + 12, (0x91000210 | // add x16,x16,#...
128 (got & 0xfff) << 10));
129 write32le(p + 16, 0xd61f0220); // br x17
130 write32le(p + 20, 0xd503201f); // nop
131 write32le(p + 24, 0xd503201f); // nop
132 write32le(p + 28, 0xd503201f); // nop
133 p += 32;
134 got = s1->got->sh_addr;
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)", (long)off, (long)addr, (long)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;
152 if (s1->got->relocplt) {
153 int mem = s1->output_type == TCC_OUTPUT_MEMORY;
154 ElfW_Rel *rel;
156 p = s1->got->data;
157 for_each_elem(s1->got->relocplt, 0, rel, ElfW_Rel) {
158 int sym_index = ELFW(R_SYM)(rel->r_info);
159 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
160 write64le(p + rel->r_offset, mem ? sym->st_value : s1->plt->sh_addr);
165 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
167 int sym_index = ELFW(R_SYM)(rel->r_info), esym_index;
168 #ifdef DEBUG_RELOC
169 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
170 #endif
172 switch(type) {
173 case R_AARCH64_ABS64:
174 if (s1->output_type == TCC_OUTPUT_DLL) {
175 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
176 qrel->r_offset = rel->r_offset;
177 if (esym_index) {
178 qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_ABS64);
179 qrel->r_addend = rel->r_addend;
180 qrel++;
181 break;
182 } else {
183 qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
184 qrel->r_addend = read64le(ptr) + val;
185 qrel++;
188 add64le(ptr, val);
189 return;
190 case R_AARCH64_ABS32:
191 if (s1->output_type == TCC_OUTPUT_DLL) {
192 /* XXX: this logic may depend on TCC's codegen
193 now TCC uses R_AARCH64_RELATIVE even for a 64bit pointer */
194 qrel->r_offset = rel->r_offset;
195 qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
196 /* Use sign extension! */
197 qrel->r_addend = (int)read32le(ptr) + val;
198 qrel++;
200 add32le(ptr, val);
201 return;
202 case R_AARCH64_PREL32:
203 if (s1->output_type == TCC_OUTPUT_DLL) {
204 /* DLL relocation */
205 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
206 if (esym_index) {
207 qrel->r_offset = rel->r_offset;
208 qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_PREL32);
209 /* Use sign extension! */
210 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
211 qrel++;
212 break;
215 write32le(ptr, val - addr);
216 return;
217 case R_AARCH64_MOVW_UABS_G0_NC:
218 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
219 (val & 0xffff) << 5));
220 return;
221 case R_AARCH64_MOVW_UABS_G1_NC:
222 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
223 (val >> 16 & 0xffff) << 5));
224 return;
225 case R_AARCH64_MOVW_UABS_G2_NC:
226 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
227 (val >> 32 & 0xffff) << 5));
228 return;
229 case R_AARCH64_MOVW_UABS_G3:
230 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
231 (val >> 48 & 0xffff) << 5));
232 return;
233 case R_AARCH64_ADR_PREL_PG_HI21: {
234 uint64_t off = (val >> 12) - (addr >> 12);
235 if ((off + ((uint64_t)1 << 20)) >> 21)
236 tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
237 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
238 (off & 0x1ffffc) << 3 | (off & 3) << 29));
239 return;
241 case R_AARCH64_ADD_ABS_LO12_NC:
242 case R_AARCH64_LDST8_ABS_LO12_NC:
243 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
244 (val & 0xfff) << 10));
245 return;
246 case R_AARCH64_LDST64_ABS_LO12_NC:
247 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
248 (val & 0xff8) << 7));
249 return;
250 case R_AARCH64_JUMP26:
251 case R_AARCH64_CALL26:
252 #ifdef DEBUG_RELOC
253 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
254 (char *) symtab_section->link->data + sym->st_name);
255 #endif
256 if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
257 tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed"
258 " (val=%lx, addr=%lx)", (long)val, (long)addr);
259 write32le(ptr, (0x14000000 |
260 (uint32_t)(type == R_AARCH64_CALL26) << 31 |
261 ((val - addr) >> 2 & 0x3ffffff)));
262 return;
263 case R_AARCH64_ADR_GOT_PAGE: {
264 uint64_t off =
265 (((s1->got->sh_addr +
266 get_sym_attr(s1, sym_index, 0)->got_offset) >> 12) - (addr >> 12));
267 if ((off + ((uint64_t)1 << 20)) >> 21)
268 tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed");
269 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
270 (off & 0x1ffffc) << 3 | (off & 3) << 29));
271 return;
273 case R_AARCH64_LD64_GOT_LO12_NC:
274 write32le(ptr,
275 ((read32le(ptr) & 0xfff803ff) |
276 ((s1->got->sh_addr +
277 get_sym_attr(s1, sym_index, 0)->got_offset) & 0xff8) << 7));
278 return;
279 case R_AARCH64_COPY:
280 return;
281 case R_AARCH64_GLOB_DAT:
282 case R_AARCH64_JUMP_SLOT:
283 /* They don't need addend */
284 #ifdef DEBUG_RELOC
285 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr,
286 val - rel->r_addend,
287 (char *) symtab_section->link->data + sym->st_name);
288 #endif
289 write64le(ptr, val - rel->r_addend);
290 return;
291 case R_AARCH64_RELATIVE:
292 #ifdef TCC_TARGET_PE
293 add32le(ptr, val - s1->pe_imagebase);
294 #endif
295 /* do nothing */
296 return;
297 default:
298 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
299 type, (unsigned)addr, ptr, (unsigned)val);
300 return;
304 #endif /* !TARGET_DEFS_ONLY */