tccgen: fix "Allow declared arrays to be initialized..."
[tinycc.git] / x86_64-link.c
blobe072379dbebfcc30554b6d57c69864bcdb9c5626
1 #ifdef TARGET_DEFS_ONLY
3 #define EM_TCC_TARGET EM_X86_64
5 /* relocation type for 32 bit data relocation */
6 #define R_DATA_32 R_X86_64_32S
7 #define R_DATA_PTR R_X86_64_64
8 #define R_JMP_SLOT R_X86_64_JUMP_SLOT
9 #define R_GLOB_DAT R_X86_64_GLOB_DAT
10 #define R_COPY R_X86_64_COPY
11 #define R_RELATIVE R_X86_64_RELATIVE
13 #define R_NUM R_X86_64_NUM
15 #define ELF_START_ADDR 0x400000
16 #define ELF_PAGE_SIZE 0x200000
18 #define PCRELATIVE_DLLPLT 1
19 #define RELOCATE_DLLPLT 1
21 #else /* !TARGET_DEFS_ONLY */
23 #include "tcc.h"
25 #if !defined(ELF_OBJ_ONLY) || defined(TCC_TARGET_MACHO)
26 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
27 relocations, returns -1. */
28 int code_reloc (int reloc_type)
30 switch (reloc_type) {
31 case R_X86_64_32:
32 case R_X86_64_32S:
33 case R_X86_64_64:
34 case R_X86_64_GOTPC32:
35 case R_X86_64_GOTPC64:
36 case R_X86_64_GOTPCREL:
37 case R_X86_64_GOTPCRELX:
38 case R_X86_64_REX_GOTPCRELX:
39 case R_X86_64_GOTTPOFF:
40 case R_X86_64_GOT32:
41 case R_X86_64_GOT64:
42 case R_X86_64_GLOB_DAT:
43 case R_X86_64_COPY:
44 case R_X86_64_RELATIVE:
45 case R_X86_64_GOTOFF64:
46 case R_X86_64_TLSGD:
47 case R_X86_64_TLSLD:
48 case R_X86_64_DTPOFF32:
49 case R_X86_64_TPOFF32:
50 return 0;
52 case R_X86_64_PC32:
53 case R_X86_64_PC64:
54 case R_X86_64_PLT32:
55 case R_X86_64_PLTOFF64:
56 case R_X86_64_JUMP_SLOT:
57 return 1;
59 return -1;
62 /* Returns an enumerator to describe whether and when the relocation needs a
63 GOT and/or PLT entry to be created. See tcc.h for a description of the
64 different values. */
65 int gotplt_entry_type (int reloc_type)
67 switch (reloc_type) {
68 case R_X86_64_GLOB_DAT:
69 case R_X86_64_JUMP_SLOT:
70 case R_X86_64_COPY:
71 case R_X86_64_RELATIVE:
72 return NO_GOTPLT_ENTRY;
74 /* The following relocs wouldn't normally need GOT or PLT
75 slots, but we need them for simplicity in the link
76 editor part. See our caller for comments. */
77 case R_X86_64_32:
78 case R_X86_64_32S:
79 case R_X86_64_64:
80 case R_X86_64_PC32:
81 case R_X86_64_PC64:
82 return AUTO_GOTPLT_ENTRY;
84 case R_X86_64_GOTTPOFF:
85 return BUILD_GOT_ONLY;
87 case R_X86_64_GOT32:
88 case R_X86_64_GOT64:
89 case R_X86_64_GOTPC32:
90 case R_X86_64_GOTPC64:
91 case R_X86_64_GOTOFF64:
92 case R_X86_64_GOTPCREL:
93 case R_X86_64_GOTPCRELX:
94 case R_X86_64_TLSGD:
95 case R_X86_64_TLSLD:
96 case R_X86_64_DTPOFF32:
97 case R_X86_64_TPOFF32:
98 case R_X86_64_REX_GOTPCRELX:
99 case R_X86_64_PLT32:
100 case R_X86_64_PLTOFF64:
101 return ALWAYS_GOTPLT_ENTRY;
104 return -1;
107 #if !defined(TCC_TARGET_MACHO) || defined TCC_IS_NATIVE
108 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
110 Section *plt = s1->plt;
111 uint8_t *p;
112 int modrm;
113 unsigned plt_offset, relofs;
115 modrm = 0x25;
117 /* empty PLT: create PLT0 entry that pushes the library identifier
118 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
119 (GOT + 2 * PTR_SIZE) */
120 if (plt->data_offset == 0) {
121 p = section_ptr_add(plt, 16);
122 p[0] = 0xff; /* pushl got + PTR_SIZE */
123 p[1] = modrm + 0x10;
124 write32le(p + 2, PTR_SIZE);
125 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
126 p[7] = modrm;
127 write32le(p + 8, PTR_SIZE * 2);
129 plt_offset = plt->data_offset;
131 /* The PLT slot refers to the relocation entry it needs via offset.
132 The reloc entry is created below, so its offset is the current
133 data_offset */
134 relofs = s1->got->relocplt ? s1->got->relocplt->data_offset : 0;
136 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
137 p = section_ptr_add(plt, 16);
138 p[0] = 0xff; /* jmp *(got + x) */
139 p[1] = modrm;
140 write32le(p + 2, got_offset);
141 p[6] = 0x68; /* push $xxx */
142 /* On x86-64, the relocation is referred to by _index_ */
143 write32le(p + 7, relofs / sizeof (ElfW_Rel) - 1);
144 p[11] = 0xe9; /* jmp plt_start */
145 write32le(p + 12, -(plt->data_offset));
146 return plt_offset;
149 /* relocate the PLT: compute addresses and offsets in the PLT now that final
150 address for PLT and GOT are known (see fill_program_header) */
151 ST_FUNC void relocate_plt(TCCState *s1)
153 uint8_t *p, *p_end;
155 if (!s1->plt)
156 return;
158 p = s1->plt->data;
159 p_end = p + s1->plt->data_offset;
161 if (p < p_end) {
162 int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
163 add32le(p + 2, x);
164 add32le(p + 8, x - 6);
165 p += 16;
166 while (p < p_end) {
167 add32le(p + 2, x + (s1->plt->data - p));
168 p += 16;
172 if (s1->got->relocplt) {
173 int mem = s1->output_type == TCC_OUTPUT_MEMORY;
174 ElfW_Rel *rel;
175 int x = s1->plt->sh_addr + 16 + 6;
177 p = s1->got->data;
178 for_each_elem(s1->got->relocplt, 0, rel, ElfW_Rel) {
179 int sym_index = ELFW(R_SYM)(rel->r_info);
180 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
181 write64le(p + rel->r_offset, mem ? sym->st_value : x);
182 x += 16;
186 #endif
187 #endif
189 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
191 int sym_index, esym_index;
193 sym_index = ELFW(R_SYM)(rel->r_info);
195 switch (type) {
196 case R_X86_64_64:
197 if (s1->output_type == TCC_OUTPUT_DLL) {
198 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
199 qrel->r_offset = rel->r_offset;
200 if (esym_index) {
201 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
202 qrel->r_addend = rel->r_addend;
203 qrel++;
204 break;
205 } else {
206 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
207 qrel->r_addend = read64le(ptr) + val;
208 qrel++;
211 add64le(ptr, val);
212 break;
213 case R_X86_64_32:
214 case R_X86_64_32S:
215 if (s1->output_type == TCC_OUTPUT_DLL) {
216 /* XXX: this logic may depend on TCC's codegen
217 now TCC uses R_X86_64_32 even for a 64bit pointer */
218 qrel->r_offset = rel->r_offset;
219 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
220 /* Use sign extension! */
221 qrel->r_addend = (int)read32le(ptr) + val;
222 qrel++;
224 add32le(ptr, val);
225 break;
227 case R_X86_64_PC32:
228 if (s1->output_type == TCC_OUTPUT_DLL) {
229 /* DLL relocation */
230 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
231 if (esym_index) {
232 qrel->r_offset = rel->r_offset;
233 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
234 /* Use sign extension! */
235 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
236 qrel++;
237 break;
240 goto plt32pc32;
242 case R_X86_64_PLT32:
243 /* fallthrough: val already holds the PLT slot address */
245 plt32pc32:
247 long long diff;
248 diff = (long long)val - addr;
249 if (diff < -2147483648LL || diff > 2147483647LL) {
250 tcc_error("internal error: relocation failed");
252 add32le(ptr, diff);
254 break;
256 case R_X86_64_PLTOFF64:
257 add64le(ptr, val - s1->got->sh_addr + rel->r_addend);
258 break;
260 case R_X86_64_PC64:
261 if (s1->output_type == TCC_OUTPUT_DLL) {
262 /* DLL relocation */
263 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
264 if (esym_index) {
265 qrel->r_offset = rel->r_offset;
266 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC64);
267 qrel->r_addend = read64le(ptr) + rel->r_addend;
268 qrel++;
269 break;
272 add64le(ptr, val - addr);
273 break;
275 case R_X86_64_GLOB_DAT:
276 case R_X86_64_JUMP_SLOT:
277 /* They don't need addend */
278 write64le(ptr, val - rel->r_addend);
279 break;
280 case R_X86_64_GOTPCREL:
281 case R_X86_64_GOTPCRELX:
282 case R_X86_64_REX_GOTPCRELX:
283 add32le(ptr, s1->got->sh_addr - addr +
284 get_sym_attr(s1, sym_index, 0)->got_offset - 4);
285 break;
286 case R_X86_64_GOTPC32:
287 add32le(ptr, s1->got->sh_addr - addr + rel->r_addend);
288 break;
289 case R_X86_64_GOTPC64:
290 add64le(ptr, s1->got->sh_addr - addr + rel->r_addend);
291 break;
292 case R_X86_64_GOTTPOFF:
293 add32le(ptr, val - s1->got->sh_addr);
294 break;
295 case R_X86_64_GOT32:
296 /* we load the got offset */
297 add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
298 break;
299 case R_X86_64_GOT64:
300 /* we load the got offset */
301 add64le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
302 break;
303 case R_X86_64_GOTOFF64:
304 add64le(ptr, val - s1->got->sh_addr);
305 break;
306 case R_X86_64_TLSGD:
308 static const unsigned char expect[] = {
309 /* .byte 0x66; lea 0(%rip),%rdi */
310 0x66, 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
311 /* .word 0x6666; rex64; call __tls_get_addr@PLT */
312 0x66, 0x66, 0x48, 0xe8, 0x00, 0x00, 0x00, 0x00 };
313 static const unsigned char replace[] = {
314 /* mov %fs:0,%rax */
315 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
316 /* lea -4(%rax),%rax */
317 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 };
319 if (memcmp (ptr-4, expect, sizeof(expect)) == 0) {
320 ElfW(Sym) *sym;
321 Section *sec;
322 int32_t x;
324 memcpy(ptr-4, replace, sizeof(replace));
325 rel[1].r_info = ELFW(R_INFO)(0, R_X86_64_NONE);
326 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
327 sec = s1->sections[sym->st_shndx];
328 x = sym->st_value - sec->sh_addr - sec->data_offset;
329 add32le(ptr + 8, x);
331 else
332 tcc_error("unexpected R_X86_64_TLSGD pattern");
334 break;
335 case R_X86_64_TLSLD:
337 static const unsigned char expect[] = {
338 /* lea 0(%rip),%rdi */
339 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
340 /* call __tls_get_addr@PLT */
341 0xe8, 0x00, 0x00, 0x00, 0x00 };
342 static const unsigned char replace[] = {
343 /* data16 data16 data16 mov %fs:0,%rax */
344 0x66, 0x66, 0x66, 0x64, 0x48, 0x8b, 0x04, 0x25,
345 0x00, 0x00, 0x00, 0x00 };
347 if (memcmp (ptr-3, expect, sizeof(expect)) == 0) {
348 memcpy(ptr-3, replace, sizeof(replace));
349 rel[1].r_info = ELFW(R_INFO)(0, R_X86_64_NONE);
351 else
352 tcc_error("unexpected R_X86_64_TLSLD pattern");
354 break;
355 case R_X86_64_DTPOFF32:
356 case R_X86_64_TPOFF32:
358 ElfW(Sym) *sym;
359 Section *sec;
360 int32_t x;
362 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
363 sec = s1->sections[sym->st_shndx];
364 x = val - sec->sh_addr - sec->data_offset;
365 add32le(ptr, x);
367 break;
368 case R_X86_64_NONE:
369 break;
370 case R_X86_64_RELATIVE:
371 #ifdef TCC_TARGET_PE
372 add32le(ptr, val - s1->pe_imagebase);
373 #endif
374 /* do nothing */
375 break;
379 #endif /* !TARGET_DEFS_ONLY */