riscv: jalr: implement pseudo and parse like GAS
[tinycc/nsauzede.git] / x86_64-link.c
blob1b6aa092ca995a1508854323147ff25e97b5257e
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 #ifdef NEED_RELOC_TYPE
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 case R_X86_64_DTPOFF64:
51 case R_X86_64_TPOFF64:
52 return 0;
54 case R_X86_64_PC32:
55 case R_X86_64_PC64:
56 case R_X86_64_PLT32:
57 case R_X86_64_PLTOFF64:
58 case R_X86_64_JUMP_SLOT:
59 return 1;
61 return -1;
64 /* Returns an enumerator to describe whether and when the relocation needs a
65 GOT and/or PLT entry to be created. See tcc.h for a description of the
66 different values. */
67 int gotplt_entry_type (int reloc_type)
69 switch (reloc_type) {
70 case R_X86_64_GLOB_DAT:
71 case R_X86_64_JUMP_SLOT:
72 case R_X86_64_COPY:
73 case R_X86_64_RELATIVE:
74 return NO_GOTPLT_ENTRY;
76 /* The following relocs wouldn't normally need GOT or PLT
77 slots, but we need them for simplicity in the link
78 editor part. See our caller for comments. */
79 case R_X86_64_32:
80 case R_X86_64_32S:
81 case R_X86_64_64:
82 case R_X86_64_PC32:
83 case R_X86_64_PC64:
84 return AUTO_GOTPLT_ENTRY;
86 case R_X86_64_GOTTPOFF:
87 return BUILD_GOT_ONLY;
89 case R_X86_64_GOT32:
90 case R_X86_64_GOT64:
91 case R_X86_64_GOTPC32:
92 case R_X86_64_GOTPC64:
93 case R_X86_64_GOTOFF64:
94 case R_X86_64_GOTPCREL:
95 case R_X86_64_GOTPCRELX:
96 case R_X86_64_TLSGD:
97 case R_X86_64_TLSLD:
98 case R_X86_64_DTPOFF32:
99 case R_X86_64_TPOFF32:
100 case R_X86_64_DTPOFF64:
101 case R_X86_64_TPOFF64:
102 case R_X86_64_REX_GOTPCRELX:
103 case R_X86_64_PLT32:
104 case R_X86_64_PLTOFF64:
105 return ALWAYS_GOTPLT_ENTRY;
108 return -1;
111 #ifdef NEED_BUILD_GOT
112 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
114 Section *plt = s1->plt;
115 uint8_t *p;
116 int modrm;
117 unsigned plt_offset, relofs;
119 modrm = 0x25;
121 /* empty PLT: create PLT0 entry that pushes the library identifier
122 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
123 (GOT + 2 * PTR_SIZE) */
124 if (plt->data_offset == 0) {
125 p = section_ptr_add(plt, 16);
126 p[0] = 0xff; /* pushl got + PTR_SIZE */
127 p[1] = modrm + 0x10;
128 write32le(p + 2, PTR_SIZE);
129 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
130 p[7] = modrm;
131 write32le(p + 8, PTR_SIZE * 2);
133 plt_offset = plt->data_offset;
135 /* The PLT slot refers to the relocation entry it needs via offset.
136 The reloc entry is created below, so its offset is the current
137 data_offset */
138 relofs = s1->plt->reloc ? s1->plt->reloc->data_offset : 0;
140 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
141 p = section_ptr_add(plt, 16);
142 p[0] = 0xff; /* jmp *(got + x) */
143 p[1] = modrm;
144 write32le(p + 2, got_offset);
145 p[6] = 0x68; /* push $xxx */
146 /* On x86-64, the relocation is referred to by _index_ */
147 write32le(p + 7, relofs / sizeof (ElfW_Rel) - 1);
148 p[11] = 0xe9; /* jmp plt_start */
149 write32le(p + 12, -(plt->data_offset));
150 return plt_offset;
153 /* relocate the PLT: compute addresses and offsets in the PLT now that final
154 address for PLT and GOT are known (see fill_program_header) */
155 ST_FUNC void relocate_plt(TCCState *s1)
157 uint8_t *p, *p_end;
159 if (!s1->plt)
160 return;
162 p = s1->plt->data;
163 p_end = p + s1->plt->data_offset;
165 if (p < p_end) {
166 int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
167 add32le(p + 2, x);
168 add32le(p + 8, x - 6);
169 p += 16;
170 while (p < p_end) {
171 add32le(p + 2, x + (s1->plt->data - p));
172 p += 16;
176 if (s1->plt->reloc) {
177 ElfW_Rel *rel;
178 int x = s1->plt->sh_addr + 16 + 6;
179 p = s1->got->data;
180 for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
181 write64le(p + rel->r_offset, 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_DYN) {
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_DYN) {
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 #ifdef TCC_TARGET_PE
251 /* ignore overflow with undefined weak symbols */
252 if (((ElfW(Sym)*)symtab_section->data)[sym_index].st_shndx != SHN_UNDEF)
253 #endif
254 tcc_error_noabort("internal error: relocation failed");
256 add32le(ptr, diff);
258 break;
260 case R_X86_64_COPY:
261 break;
263 case R_X86_64_PLTOFF64:
264 add64le(ptr, val - s1->got->sh_addr + rel->r_addend);
265 break;
267 case R_X86_64_PC64:
268 if (s1->output_type == TCC_OUTPUT_DLL) {
269 /* DLL relocation */
270 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
271 if (esym_index) {
272 qrel->r_offset = rel->r_offset;
273 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC64);
274 qrel->r_addend = read64le(ptr) + rel->r_addend;
275 qrel++;
276 break;
279 add64le(ptr, val - addr);
280 break;
282 case R_X86_64_GLOB_DAT:
283 case R_X86_64_JUMP_SLOT:
284 /* They don't need addend */
285 write64le(ptr, val - rel->r_addend);
286 break;
287 case R_X86_64_GOTPCREL:
288 case R_X86_64_GOTPCRELX:
289 case R_X86_64_REX_GOTPCRELX:
290 add32le(ptr, s1->got->sh_addr - addr +
291 get_sym_attr(s1, sym_index, 0)->got_offset - 4);
292 break;
293 case R_X86_64_GOTPC32:
294 add32le(ptr, s1->got->sh_addr - addr + rel->r_addend);
295 break;
296 case R_X86_64_GOTPC64:
297 add64le(ptr, s1->got->sh_addr - addr + rel->r_addend);
298 break;
299 case R_X86_64_GOTTPOFF:
300 add32le(ptr, val - s1->got->sh_addr);
301 break;
302 case R_X86_64_GOT32:
303 /* we load the got offset */
304 add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
305 break;
306 case R_X86_64_GOT64:
307 /* we load the got offset */
308 add64le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
309 break;
310 case R_X86_64_GOTOFF64:
311 add64le(ptr, val - s1->got->sh_addr);
312 break;
313 case R_X86_64_TLSGD:
315 static const unsigned char expect[] = {
316 /* .byte 0x66; lea 0(%rip),%rdi */
317 0x66, 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
318 /* .word 0x6666; rex64; call __tls_get_addr@PLT */
319 0x66, 0x66, 0x48, 0xe8, 0x00, 0x00, 0x00, 0x00 };
320 static const unsigned char replace[] = {
321 /* mov %fs:0,%rax */
322 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00,
323 /* lea -4(%rax),%rax */
324 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 };
326 if (memcmp (ptr-4, expect, sizeof(expect)) == 0) {
327 ElfW(Sym) *sym;
328 Section *sec;
329 int32_t x;
331 memcpy(ptr-4, replace, sizeof(replace));
332 rel[1].r_info = ELFW(R_INFO)(0, R_X86_64_NONE);
333 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
334 sec = s1->sections[sym->st_shndx];
335 x = sym->st_value - sec->sh_addr - sec->data_offset;
336 add32le(ptr + 8, x);
338 else
339 tcc_error_noabort("unexpected R_X86_64_TLSGD pattern");
341 break;
342 case R_X86_64_TLSLD:
344 static const unsigned char expect[] = {
345 /* lea 0(%rip),%rdi */
346 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
347 /* call __tls_get_addr@PLT */
348 0xe8, 0x00, 0x00, 0x00, 0x00 };
349 static const unsigned char replace[] = {
350 /* data16 data16 data16 mov %fs:0,%rax */
351 0x66, 0x66, 0x66, 0x64, 0x48, 0x8b, 0x04, 0x25,
352 0x00, 0x00, 0x00, 0x00 };
354 if (memcmp (ptr-3, expect, sizeof(expect)) == 0) {
355 memcpy(ptr-3, replace, sizeof(replace));
356 rel[1].r_info = ELFW(R_INFO)(0, R_X86_64_NONE);
358 else
359 tcc_error_noabort("unexpected R_X86_64_TLSLD pattern");
361 break;
362 case R_X86_64_DTPOFF32:
363 case R_X86_64_TPOFF32:
365 ElfW(Sym) *sym;
366 Section *sec;
367 int32_t x;
369 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
370 sec = s1->sections[sym->st_shndx];
371 x = val - sec->sh_addr - sec->data_offset;
372 add32le(ptr, x);
374 break;
375 case R_X86_64_DTPOFF64:
376 case R_X86_64_TPOFF64:
378 ElfW(Sym) *sym;
379 Section *sec;
380 int32_t x;
382 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
383 sec = s1->sections[sym->st_shndx];
384 x = val - sec->sh_addr - sec->data_offset;
385 add64le(ptr, x);
387 break;
388 case R_X86_64_NONE:
389 break;
390 case R_X86_64_RELATIVE:
391 #ifdef TCC_TARGET_PE
392 add32le(ptr, val - s1->pe_imagebase);
393 #endif
394 /* do nothing */
395 break;
396 default:
397 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
398 type, (unsigned)addr, ptr, (unsigned)val);
399 break;
403 #endif /* !TARGET_DEFS_ONLY */