win32/tccpe: use full dll-path with -run
[tinycc.git] / x86_64-link.c
blobec67d740c446b9e7a415563cc2ac1bb657df232e
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 return 0;
48 case R_X86_64_PC32:
49 case R_X86_64_PC64:
50 case R_X86_64_PLT32:
51 case R_X86_64_PLTOFF64:
52 case R_X86_64_JUMP_SLOT:
53 return 1;
55 return -1;
58 /* Returns an enumerator to describe whether and when the relocation needs a
59 GOT and/or PLT entry to be created. See tcc.h for a description of the
60 different values. */
61 int gotplt_entry_type (int reloc_type)
63 switch (reloc_type) {
64 case R_X86_64_GLOB_DAT:
65 case R_X86_64_JUMP_SLOT:
66 case R_X86_64_COPY:
67 case R_X86_64_RELATIVE:
68 return NO_GOTPLT_ENTRY;
70 /* The following relocs wouldn't normally need GOT or PLT
71 slots, but we need them for simplicity in the link
72 editor part. See our caller for comments. */
73 case R_X86_64_32:
74 case R_X86_64_32S:
75 case R_X86_64_64:
76 case R_X86_64_PC32:
77 case R_X86_64_PC64:
78 return AUTO_GOTPLT_ENTRY;
80 case R_X86_64_GOTTPOFF:
81 return BUILD_GOT_ONLY;
83 case R_X86_64_GOT32:
84 case R_X86_64_GOT64:
85 case R_X86_64_GOTPC32:
86 case R_X86_64_GOTPC64:
87 case R_X86_64_GOTOFF64:
88 case R_X86_64_GOTPCREL:
89 case R_X86_64_GOTPCRELX:
90 case R_X86_64_REX_GOTPCRELX:
91 case R_X86_64_PLT32:
92 case R_X86_64_PLTOFF64:
93 return ALWAYS_GOTPLT_ENTRY;
96 return -1;
99 #if !defined(TCC_TARGET_MACHO) || defined TCC_IS_NATIVE
100 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
102 Section *plt = s1->plt;
103 uint8_t *p;
104 int modrm;
105 unsigned plt_offset, relofs;
107 modrm = 0x25;
109 /* empty PLT: create PLT0 entry that pushes the library identifier
110 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
111 (GOT + 2 * PTR_SIZE) */
112 if (plt->data_offset == 0) {
113 p = section_ptr_add(plt, 16);
114 p[0] = 0xff; /* pushl got + PTR_SIZE */
115 p[1] = modrm + 0x10;
116 write32le(p + 2, PTR_SIZE);
117 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
118 p[7] = modrm;
119 write32le(p + 8, PTR_SIZE * 2);
121 plt_offset = plt->data_offset;
123 /* The PLT slot refers to the relocation entry it needs via offset.
124 The reloc entry is created below, so its offset is the current
125 data_offset */
126 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
128 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
129 p = section_ptr_add(plt, 16);
130 p[0] = 0xff; /* jmp *(got + x) */
131 p[1] = modrm;
132 write32le(p + 2, got_offset);
133 p[6] = 0x68; /* push $xxx */
134 /* On x86-64, the relocation is referred to by _index_ */
135 write32le(p + 7, relofs / sizeof (ElfW_Rel));
136 p[11] = 0xe9; /* jmp plt_start */
137 write32le(p + 12, -(plt->data_offset));
138 return plt_offset;
141 /* relocate the PLT: compute addresses and offsets in the PLT now that final
142 address for PLT and GOT are known (see fill_program_header) */
143 ST_FUNC void relocate_plt(TCCState *s1)
145 uint8_t *p, *p_end;
147 if (!s1->plt)
148 return;
150 p = s1->plt->data;
151 p_end = p + s1->plt->data_offset;
153 if (p < p_end) {
154 int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
155 add32le(p + 2, x);
156 add32le(p + 8, x - 6);
157 p += 16;
158 while (p < p_end) {
159 add32le(p + 2, x + (s1->plt->data - p));
160 p += 16;
164 #endif
165 #endif
167 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
169 int sym_index, esym_index;
171 sym_index = ELFW(R_SYM)(rel->r_info);
173 switch (type) {
174 case R_X86_64_64:
175 if (s1->output_type == TCC_OUTPUT_DLL) {
176 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
177 qrel->r_offset = rel->r_offset;
178 if (esym_index) {
179 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
180 qrel->r_addend = rel->r_addend;
181 qrel++;
182 break;
183 } else {
184 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
185 qrel->r_addend = read64le(ptr) + val;
186 qrel++;
189 add64le(ptr, val);
190 break;
191 case R_X86_64_32:
192 case R_X86_64_32S:
193 if (s1->output_type == TCC_OUTPUT_DLL) {
194 /* XXX: this logic may depend on TCC's codegen
195 now TCC uses R_X86_64_32 even for a 64bit pointer */
196 qrel->r_offset = rel->r_offset;
197 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
198 /* Use sign extension! */
199 qrel->r_addend = (int)read32le(ptr) + val;
200 qrel++;
202 add32le(ptr, val);
203 break;
205 case R_X86_64_PC32:
206 if (s1->output_type == TCC_OUTPUT_DLL) {
207 /* DLL relocation */
208 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
209 if (esym_index) {
210 qrel->r_offset = rel->r_offset;
211 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
212 /* Use sign extension! */
213 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
214 qrel++;
215 break;
218 goto plt32pc32;
220 case R_X86_64_PLT32:
221 /* fallthrough: val already holds the PLT slot address */
223 plt32pc32:
225 long long diff;
226 diff = (long long)val - addr;
227 if (diff < -2147483648LL || diff > 2147483647LL) {
228 tcc_error("internal error: relocation failed");
230 add32le(ptr, diff);
232 break;
234 case R_X86_64_PLTOFF64:
235 add64le(ptr, val - s1->got->sh_addr + rel->r_addend);
236 break;
238 case R_X86_64_PC64:
239 if (s1->output_type == TCC_OUTPUT_DLL) {
240 /* DLL relocation */
241 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
242 if (esym_index) {
243 qrel->r_offset = rel->r_offset;
244 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC64);
245 qrel->r_addend = read64le(ptr) + rel->r_addend;
246 qrel++;
247 break;
250 add64le(ptr, val - addr);
251 break;
253 case R_X86_64_GLOB_DAT:
254 case R_X86_64_JUMP_SLOT:
255 /* They don't need addend */
256 write64le(ptr, val - rel->r_addend);
257 break;
258 case R_X86_64_GOTPCREL:
259 case R_X86_64_GOTPCRELX:
260 case R_X86_64_REX_GOTPCRELX:
261 add32le(ptr, s1->got->sh_addr - addr +
262 get_sym_attr(s1, sym_index, 0)->got_offset - 4);
263 break;
264 case R_X86_64_GOTPC32:
265 add32le(ptr, s1->got->sh_addr - addr + rel->r_addend);
266 break;
267 case R_X86_64_GOTPC64:
268 add64le(ptr, s1->got->sh_addr - addr + rel->r_addend);
269 break;
270 case R_X86_64_GOTTPOFF:
271 add32le(ptr, val - s1->got->sh_addr);
272 break;
273 case R_X86_64_GOT32:
274 /* we load the got offset */
275 add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
276 break;
277 case R_X86_64_GOT64:
278 /* we load the got offset */
279 add64le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
280 break;
281 case R_X86_64_GOTOFF64:
282 add64le(ptr, val - s1->got->sh_addr);
283 break;
284 case R_X86_64_RELATIVE:
285 #ifdef TCC_TARGET_PE
286 add32le(ptr, val - s1->pe_imagebase);
287 #endif
288 /* do nothing */
289 break;
293 #endif /* !TARGET_DEFS_ONLY */