riscv: fp parameters
[tinycc.git] / x86_64-link.c
blob1b361a3f6efada71e374048b0ef92faa21a4f205
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 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
26 relocations, returns -1. */
27 int code_reloc (int reloc_type)
29 switch (reloc_type) {
30 case R_X86_64_32:
31 case R_X86_64_32S:
32 case R_X86_64_64:
33 case R_X86_64_GOTPC32:
34 case R_X86_64_GOTPC64:
35 case R_X86_64_GOTPCREL:
36 case R_X86_64_GOTPCRELX:
37 case R_X86_64_REX_GOTPCRELX:
38 case R_X86_64_GOTTPOFF:
39 case R_X86_64_GOT32:
40 case R_X86_64_GOT64:
41 case R_X86_64_GLOB_DAT:
42 case R_X86_64_COPY:
43 case R_X86_64_RELATIVE:
44 case R_X86_64_GOTOFF64:
45 return 0;
47 case R_X86_64_PC32:
48 case R_X86_64_PC64:
49 case R_X86_64_PLT32:
50 case R_X86_64_PLTOFF64:
51 case R_X86_64_JUMP_SLOT:
52 return 1;
55 tcc_error ("Unknown relocation type: %d", reloc_type);
56 return -1;
59 /* Returns an enumerator to describe whether and when the relocation needs a
60 GOT and/or PLT entry to be created. See tcc.h for a description of the
61 different values. */
62 int gotplt_entry_type (int reloc_type)
64 switch (reloc_type) {
65 case R_X86_64_GLOB_DAT:
66 case R_X86_64_JUMP_SLOT:
67 case R_X86_64_COPY:
68 case R_X86_64_RELATIVE:
69 return NO_GOTPLT_ENTRY;
71 /* The following relocs wouldn't normally need GOT or PLT
72 slots, but we need them for simplicity in the link
73 editor part. See our caller for comments. */
74 case R_X86_64_32:
75 case R_X86_64_32S:
76 case R_X86_64_64:
77 case R_X86_64_PC32:
78 case R_X86_64_PC64:
79 return AUTO_GOTPLT_ENTRY;
81 case R_X86_64_GOTTPOFF:
82 return BUILD_GOT_ONLY;
84 case R_X86_64_GOT32:
85 case R_X86_64_GOT64:
86 case R_X86_64_GOTPC32:
87 case R_X86_64_GOTPC64:
88 case R_X86_64_GOTOFF64:
89 case R_X86_64_GOTPCREL:
90 case R_X86_64_GOTPCRELX:
91 case R_X86_64_REX_GOTPCRELX:
92 case R_X86_64_PLT32:
93 case R_X86_64_PLTOFF64:
94 return ALWAYS_GOTPLT_ENTRY;
97 tcc_error ("Unknown relocation type: %d", reloc_type);
98 return -1;
101 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
103 Section *plt = s1->plt;
104 uint8_t *p;
105 int modrm;
106 unsigned plt_offset, relofs;
108 modrm = 0x25;
110 /* empty PLT: create PLT0 entry that pushes the library identifier
111 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
112 (GOT + 2 * PTR_SIZE) */
113 if (plt->data_offset == 0) {
114 p = section_ptr_add(plt, 16);
115 p[0] = 0xff; /* pushl got + PTR_SIZE */
116 p[1] = modrm + 0x10;
117 write32le(p + 2, PTR_SIZE);
118 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
119 p[7] = modrm;
120 write32le(p + 8, PTR_SIZE * 2);
122 plt_offset = plt->data_offset;
124 /* The PLT slot refers to the relocation entry it needs via offset.
125 The reloc entry is created below, so its offset is the current
126 data_offset */
127 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
129 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
130 p = section_ptr_add(plt, 16);
131 p[0] = 0xff; /* jmp *(got + x) */
132 p[1] = modrm;
133 write32le(p + 2, got_offset);
134 p[6] = 0x68; /* push $xxx */
135 /* On x86-64, the relocation is referred to by _index_ */
136 write32le(p + 7, relofs / sizeof (ElfW_Rel));
137 p[11] = 0xe9; /* jmp plt_start */
138 write32le(p + 12, -(plt->data_offset));
139 return plt_offset;
142 /* relocate the PLT: compute addresses and offsets in the PLT now that final
143 address for PLT and GOT are known (see fill_program_header) */
144 ST_FUNC void relocate_plt(TCCState *s1)
146 uint8_t *p, *p_end;
148 if (!s1->plt)
149 return;
151 p = s1->plt->data;
152 p_end = p + s1->plt->data_offset;
154 if (p < p_end) {
155 int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
156 add32le(p + 2, x);
157 add32le(p + 8, x - 6);
158 p += 16;
159 while (p < p_end) {
160 add32le(p + 2, x + s1->plt->data - p);
161 p += 16;
166 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
168 void relocate_init(Section *sr)
170 qrel = (ElfW_Rel *) sr->data;
173 void relocate_fini(Section *sr)
177 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
179 int sym_index, esym_index;
181 sym_index = ELFW(R_SYM)(rel->r_info);
183 switch (type) {
184 case R_X86_64_64:
185 if (s1->output_type == TCC_OUTPUT_DLL) {
186 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
187 qrel->r_offset = rel->r_offset;
188 if (esym_index) {
189 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
190 qrel->r_addend = rel->r_addend;
191 qrel++;
192 break;
193 } else {
194 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
195 qrel->r_addend = read64le(ptr) + val;
196 qrel++;
199 add64le(ptr, val);
200 break;
201 case R_X86_64_32:
202 case R_X86_64_32S:
203 if (s1->output_type == TCC_OUTPUT_DLL) {
204 /* XXX: this logic may depend on TCC's codegen
205 now TCC uses R_X86_64_32 even for a 64bit pointer */
206 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
207 /* Use sign extension! */
208 qrel->r_addend = (int)read32le(ptr) + val;
209 qrel++;
211 add32le(ptr, val);
212 break;
214 case R_X86_64_PC32:
215 if (s1->output_type == TCC_OUTPUT_DLL) {
216 /* DLL relocation */
217 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
218 if (esym_index) {
219 qrel->r_offset = rel->r_offset;
220 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
221 /* Use sign extension! */
222 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
223 qrel++;
224 break;
227 goto plt32pc32;
229 case R_X86_64_PLT32:
230 /* fallthrough: val already holds the PLT slot address */
232 plt32pc32:
234 long long diff;
235 diff = (long long)val - addr;
236 if (diff < -2147483648LL || diff > 2147483647LL) {
237 tcc_error("internal error: relocation failed");
239 add32le(ptr, diff);
241 break;
243 case R_X86_64_PLTOFF64:
244 add64le(ptr, val - s1->got->sh_addr + rel->r_addend);
245 break;
247 case R_X86_64_PC64:
248 if (s1->output_type == TCC_OUTPUT_DLL) {
249 /* DLL relocation */
250 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
251 if (esym_index) {
252 qrel->r_offset = rel->r_offset;
253 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC64);
254 qrel->r_addend = read64le(ptr) + rel->r_addend;
255 qrel++;
256 break;
259 add64le(ptr, val - addr);
260 break;
262 case R_X86_64_GLOB_DAT:
263 case R_X86_64_JUMP_SLOT:
264 /* They don't need addend */
265 write64le(ptr, val - rel->r_addend);
266 break;
267 case R_X86_64_GOTPCREL:
268 case R_X86_64_GOTPCRELX:
269 case R_X86_64_REX_GOTPCRELX:
270 add32le(ptr, s1->got->sh_addr - addr +
271 get_sym_attr(s1, sym_index, 0)->got_offset - 4);
272 break;
273 case R_X86_64_GOTPC32:
274 add32le(ptr, s1->got->sh_addr - addr + rel->r_addend);
275 break;
276 case R_X86_64_GOTPC64:
277 add64le(ptr, s1->got->sh_addr - addr + rel->r_addend);
278 break;
279 case R_X86_64_GOTTPOFF:
280 add32le(ptr, val - s1->got->sh_addr);
281 break;
282 case R_X86_64_GOT32:
283 /* we load the got offset */
284 add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
285 break;
286 case R_X86_64_GOT64:
287 /* we load the got offset */
288 add64le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
289 break;
290 case R_X86_64_GOTOFF64:
291 add64le(ptr, val - s1->got->sh_addr);
292 break;
293 case R_X86_64_RELATIVE:
294 #ifdef TCC_TARGET_PE
295 add32le(ptr, val - s1->pe_imagebase);
296 #endif
297 /* do nothing */
298 break;
302 #endif /* !TARGET_DEFS_ONLY */