Add -static to be in sync with Windows bat.
[tinycc.git] / arm64-link.c
blob179fb1469b27f8ed987f46c4de7cdf4455fa0006
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_PREL32:
31 case R_AARCH64_MOVW_UABS_G0_NC:
32 case R_AARCH64_MOVW_UABS_G1_NC:
33 case R_AARCH64_MOVW_UABS_G2_NC:
34 case R_AARCH64_MOVW_UABS_G3:
35 case R_AARCH64_ADR_PREL_PG_HI21:
36 case R_AARCH64_ADD_ABS_LO12_NC:
37 case R_AARCH64_ADR_GOT_PAGE:
38 case R_AARCH64_LD64_GOT_LO12_NC:
39 case R_AARCH64_GLOB_DAT:
40 case R_AARCH64_COPY:
41 return 0;
43 case R_AARCH64_JUMP26:
44 case R_AARCH64_CALL26:
45 case R_AARCH64_JUMP_SLOT:
46 return 1;
49 tcc_error ("Unknown relocation type: %d", reloc_type);
50 return -1;
53 /* Returns an enumerator to describe wether 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_GLOB_DAT:
67 case R_AARCH64_JUMP_SLOT:
68 case R_AARCH64_COPY:
69 return NO_GOTPLT_ENTRY;
71 case R_AARCH64_ABS32:
72 case R_AARCH64_ABS64:
73 case R_AARCH64_JUMP26:
74 case R_AARCH64_CALL26:
75 return AUTO_GOTPLT_ENTRY;
77 case R_AARCH64_ADR_GOT_PAGE:
78 case R_AARCH64_LD64_GOT_LO12_NC:
79 return ALWAYS_GOTPLT_ENTRY;
82 tcc_error ("Unknown relocation type: %d", reloc_type);
83 return -1;
86 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
88 Section *plt = s1->plt;
89 uint8_t *p;
90 unsigned plt_offset;
92 if (s1->output_type == TCC_OUTPUT_DLL)
93 tcc_error("DLLs unimplemented!");
95 if (plt->data_offset == 0) {
96 section_ptr_add(plt, 32);
98 plt_offset = plt->data_offset;
100 p = section_ptr_add(plt, 16);
101 write32le(p, got_offset);
102 write32le(p + 4, (uint64_t) got_offset >> 32);
103 return plt_offset;
106 /* relocate the PLT: compute addresses and offsets in the PLT now that final
107 address for PLT and GOT are known (see fill_program_header) */
108 ST_FUNC void relocate_plt(TCCState *s1)
110 uint8_t *p, *p_end;
112 if (!s1->plt)
113 return;
115 p = s1->plt->data;
116 p_end = p + s1->plt->data_offset;
118 if (p < p_end) {
119 uint64_t plt = s1->plt->sh_addr;
120 uint64_t got = s1->got->sh_addr;
121 uint64_t off = (got >> 12) - (plt >> 12);
122 if ((off + ((uint32_t)1 << 20)) >> 21)
123 tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", off, got, plt);
124 write32le(p, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]!
125 write32le(p + 4, (0x90000010 | // adrp x16,...
126 (off & 0x1ffffc) << 3 | (off & 3) << 29));
127 write32le(p + 8, (0xf9400211 | // ldr x17,[x16,#...]
128 (got & 0xff8) << 7));
129 write32le(p + 12, (0x91000210 | // add x16,x16,#...
130 (got & 0xfff) << 10));
131 write32le(p + 16, 0xd61f0220); // br x17
132 write32le(p + 20, 0xd503201f); // nop
133 write32le(p + 24, 0xd503201f); // nop
134 write32le(p + 28, 0xd503201f); // nop
135 p += 32;
136 while (p < p_end) {
137 uint64_t pc = plt + (p - s1->plt->data);
138 uint64_t addr = got + read64le(p);
139 uint64_t off = (addr >> 12) - (pc >> 12);
140 if ((off + ((uint32_t)1 << 20)) >> 21)
141 tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off, addr, pc);
142 write32le(p, (0x90000010 | // adrp x16,...
143 (off & 0x1ffffc) << 3 | (off & 3) << 29));
144 write32le(p + 4, (0xf9400211 | // ldr x17,[x16,#...]
145 (addr & 0xff8) << 7));
146 write32le(p + 8, (0x91000210 | // add x16,x16,#...
147 (addr & 0xfff) << 10));
148 write32le(p + 12, 0xd61f0220); // br x17
149 p += 16;
154 void relocate_init(Section *sr) {}
156 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
158 int sym_index = ELFW(R_SYM)(rel->r_info);
159 #ifdef DEBUG_RELOC
160 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
161 #endif
163 switch(type) {
164 case R_AARCH64_ABS64:
165 write64le(ptr, val);
166 return;
167 case R_AARCH64_ABS32:
168 write32le(ptr, val);
169 return;
170 case R_AARCH64_PREL32:
171 write32le(ptr, val - addr);
172 return;
173 case R_AARCH64_MOVW_UABS_G0_NC:
174 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
175 (val & 0xffff) << 5));
176 return;
177 case R_AARCH64_MOVW_UABS_G1_NC:
178 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
179 (val >> 16 & 0xffff) << 5));
180 return;
181 case R_AARCH64_MOVW_UABS_G2_NC:
182 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
183 (val >> 32 & 0xffff) << 5));
184 return;
185 case R_AARCH64_MOVW_UABS_G3:
186 write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
187 (val >> 48 & 0xffff) << 5));
188 return;
189 case R_AARCH64_ADR_PREL_PG_HI21: {
190 uint64_t off = (val >> 12) - (addr >> 12);
191 if ((off + ((uint64_t)1 << 20)) >> 21)
192 tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
193 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
194 (off & 0x1ffffc) << 3 | (off & 3) << 29));
195 return;
197 case R_AARCH64_ADD_ABS_LO12_NC:
198 write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
199 (val & 0xfff) << 10));
200 return;
201 case R_AARCH64_JUMP26:
202 case R_AARCH64_CALL26:
203 #ifdef DEBUG_RELOC
204 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
205 (char *) symtab_section->link->data + sym->st_name);
206 #endif
207 if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
208 tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed"
209 " (val=%lx, addr=%lx)", val, addr);
210 write32le(ptr, (0x14000000 |
211 (uint32_t)(type == R_AARCH64_CALL26) << 31 |
212 ((val - addr) >> 2 & 0x3ffffff)));
213 return;
214 case R_AARCH64_ADR_GOT_PAGE: {
215 uint64_t off =
216 (((s1->got->sh_addr +
217 s1->sym_attrs[sym_index].got_offset) >> 12) - (addr >> 12));
218 if ((off + ((uint64_t)1 << 20)) >> 21)
219 tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed");
220 write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
221 (off & 0x1ffffc) << 3 | (off & 3) << 29));
222 return;
224 case R_AARCH64_LD64_GOT_LO12_NC:
225 write32le(ptr,
226 ((read32le(ptr) & 0xfff803ff) |
227 ((s1->got->sh_addr +
228 s1->sym_attrs[sym_index].got_offset) & 0xff8) << 7));
229 return;
230 case R_AARCH64_COPY:
231 return;
232 case R_AARCH64_GLOB_DAT:
233 case R_AARCH64_JUMP_SLOT:
234 /* They don't need addend */
235 #ifdef DEBUG_RELOC
236 printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr,
237 val - rel->r_addend,
238 (char *) symtab_section->link->data + sym->st_name);
239 #endif
240 write64le(ptr, val - rel->r_addend);
241 return;
242 default:
243 fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
244 type, (unsigned)addr, ptr, (unsigned)val);
245 return;
249 #endif /* !TARGET_DEFS_ONLY */