bitfields: Implement MS compatible layout
[tinycc.git] / arm64-link.c
blobe8f810d5b935033626f02e479b98a988dd0639d0
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
11 #define R_NUM R_AARCH64_NUM
13 #define ELF_START_ADDR 0x00400000
14 #define ELF_PAGE_SIZE 0x1000
16 #define PCRELATIVE_DLLPLT 1
17 #define RELOCATE_DLLPLT 1
19 #else /* !TARGET_DEFS_ONLY */
21 #include "tcc.h"
23 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
24 relocations, returns -1. */
25 int code_reloc (int reloc_type)
27 switch (reloc_type) {
28 case R_AARCH64_ABS32:
29 case R_AARCH64_ABS64:
30 case R_AARCH64_MOVW_UABS_G0_NC:
31 case R_AARCH64_MOVW_UABS_G1_NC:
32 case R_AARCH64_MOVW_UABS_G2_NC:
33 case R_AARCH64_MOVW_UABS_G3:
34 case R_AARCH64_ADR_PREL_PG_HI21:
35 case R_AARCH64_ADD_ABS_LO12_NC:
36 case R_AARCH64_ADR_GOT_PAGE:
37 case R_AARCH64_LD64_GOT_LO12_NC:
38 case R_AARCH64_GLOB_DAT:
39 case R_AARCH64_COPY:
40 return 0;
42 case R_AARCH64_JUMP26:
43 case R_AARCH64_CALL26:
44 case R_AARCH64_JUMP_SLOT:
45 return 1;
48 tcc_error ("Unknown relocation type: %d", reloc_type);
49 return -1;
52 /* Returns an enumerator to describe wether and when the relocation needs a
53 GOT and/or PLT entry to be created. See tcc.h for a description of the
54 different values. */
55 int gotplt_entry_type (int reloc_type)
57 switch (reloc_type) {
58 case R_AARCH64_ABS32:
59 case R_AARCH64_ABS64:
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_GLOB_DAT:
67 case R_AARCH64_JUMP_SLOT:
68 case R_AARCH64_COPY:
69 return NO_GOTPLT_ENTRY;
71 case R_AARCH64_JUMP26:
72 case R_AARCH64_CALL26:
73 return AUTO_GOTPLT_ENTRY;
75 case R_AARCH64_ADR_GOT_PAGE:
76 case R_AARCH64_LD64_GOT_LO12_NC:
77 return ALWAYS_GOTPLT_ENTRY;
80 tcc_error ("Unknown relocation type: %d", reloc_type);
81 return -1;
84 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
86 Section *plt = s1->plt;
87 uint8_t *p;
88 unsigned plt_offset;
90 if (s1->output_type == TCC_OUTPUT_DLL)
91 tcc_error("DLLs unimplemented!");
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;
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)", off, got, 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 while (p < p_end) {
135 uint64_t pc = plt + (p - s1->plt->data);
136 uint64_t addr = got + read64le(p);
137 uint64_t off = (addr >> 12) - (pc >> 12);
138 if ((off + ((uint32_t)1 << 20)) >> 21)
139 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off, addr, pc);
140 write32le(p, (0x90000010 | // adrp x16,...
141 (off & 0x1ffffc) << 3 | (off & 3) << 29));
142 write32le(p + 4, (0xf9400211 | // ldr x17,[x16,#...]
143 (addr & 0xff8) << 7));
144 write32le(p + 8, (0x91000210 | // add x16,x16,#...
145 (addr & 0xfff) << 10));
146 write32le(p + 12, 0xd61f0220); // br x17
147 p += 16;
152 void relocate_init(Section *sr) {}
154 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
156 int sym_index = ELFW(R_SYM)(rel->r_info);
157 #ifdef DEBUG_RELOC
158 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
159 #endif
161 switch(type) {
162 case R_AARCH64_ABS64:
163 write64le(ptr, val);
164 return;
165 case R_AARCH64_ABS32:
166 write32le(ptr, val);
167 return;
168 case R_AARCH64_MOVW_UABS_G0_NC:
169 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
170 (val & 0xffff) << 5));
171 return;
172 case R_AARCH64_MOVW_UABS_G1_NC:
173 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
174 (val >> 16 & 0xffff) << 5));
175 return;
176 case R_AARCH64_MOVW_UABS_G2_NC:
177 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
178 (val >> 32 & 0xffff) << 5));
179 return;
180 case R_AARCH64_MOVW_UABS_G3:
181 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
182 (val >> 48 & 0xffff) << 5));
183 return;
184 case R_AARCH64_ADR_PREL_PG_HI21: {
185 uint64_t off = (val >> 12) - (addr >> 12);
186 if ((off + ((uint64_t)1 << 20)) >> 21)
187 tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
188 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
189 (off & 0x1ffffc) << 3 | (off & 3) << 29));
190 return;
192 case R_AARCH64_ADD_ABS_LO12_NC:
193 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
194 (val & 0xfff) << 10));
195 return;
196 case R_AARCH64_JUMP26:
197 case R_AARCH64_CALL26:
198 #ifdef DEBUG_RELOC
199 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
200 (char *) symtab_section->link->data + sym->st_name);
201 #endif
202 if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
203 tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed"
204 " (val=%lx, addr=%lx)", val, addr);
205 write32le(ptr, (0x14000000 |
206 (uint32_t)(type == R_AARCH64_CALL26) << 31 |
207 ((val - addr) >> 2 & 0x3ffffff)));
208 return;
209 case R_AARCH64_ADR_GOT_PAGE: {
210 uint64_t off =
211 (((s1->got->sh_addr +
212 s1->sym_attrs[sym_index].got_offset) >> 12) - (addr >> 12));
213 if ((off + ((uint64_t)1 << 20)) >> 21)
214 tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed");
215 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
216 (off & 0x1ffffc) << 3 | (off & 3) << 29));
217 return;
219 case R_AARCH64_LD64_GOT_LO12_NC:
220 write32le(ptr,
221 ((read32le(ptr) & 0xfff803ff) |
222 ((s1->got->sh_addr +
223 s1->sym_attrs[sym_index].got_offset) & 0xff8) << 7));
224 return;
225 case R_AARCH64_COPY:
226 return;
227 case R_AARCH64_GLOB_DAT:
228 case R_AARCH64_JUMP_SLOT:
229 /* They don't need addend */
230 #ifdef DEBUG_RELOC
231 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr,
232 val - rel->r_addend,
233 (char *) symtab_section->link->data + sym->st_name);
234 #endif
235 write64le(ptr, val - rel->r_addend);
236 return;
237 default:
238 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
239 type, (unsigned)addr, ptr, (unsigned)val);
240 return;
244 #endif /* !TARGET_DEFS_ONLY */