win32: build-tcc.bat: figure out correct bitness of cl.exe
[tinycc.git] / x86_64-link.c
blob27cad93f549f91c3c0f29470b1bae07474368563
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
12 #define R_NUM R_X86_64_NUM
14 #define ELF_START_ADDR 0x400000
15 #define ELF_PAGE_SIZE 0x200000
17 #define PCRELATIVE_DLLPLT 1
18 #define RELOCATE_DLLPLT 1
20 #else /* !TARGET_DEFS_ONLY */
22 #include "tcc.h"
24 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
25 relocations, returns -1. */
26 int code_reloc (int reloc_type)
28 switch (reloc_type) {
29 case R_X86_64_32:
30 case R_X86_64_32S:
31 case R_X86_64_64:
32 case R_X86_64_GOTPCREL:
33 case R_X86_64_GOTPCRELX:
34 case R_X86_64_REX_GOTPCRELX:
35 case R_X86_64_GOTTPOFF:
36 case R_X86_64_GOT32:
37 case R_X86_64_GLOB_DAT:
38 case R_X86_64_COPY:
39 case R_X86_64_RELATIVE:
40 return 0;
42 case R_X86_64_PC32:
43 case R_X86_64_PLT32:
44 case R_X86_64_JUMP_SLOT:
45 return 1;
48 tcc_error ("Unknown relocation type: %d", reloc_type);
49 return -1;
52 /* Returns an enumerator to describe wether and when the relocation needs a
53 GOT and/or PLT entry to be created. See tcc.h for a description of the
54 different values. */
55 int gotplt_entry_type (int reloc_type)
57 switch (reloc_type) {
58 case R_X86_64_GLOB_DAT:
59 case R_X86_64_JUMP_SLOT:
60 case R_X86_64_COPY:
61 case R_X86_64_RELATIVE:
62 return NO_GOTPLT_ENTRY;
64 /* The following relocs wouldn't normally need GOT or PLT
65 slots, but we need them for simplicity in the link
66 editor part. See our caller for comments. */
67 case R_X86_64_32:
68 case R_X86_64_32S:
69 case R_X86_64_64:
70 case R_X86_64_PC32:
71 return AUTO_GOTPLT_ENTRY;
73 case R_X86_64_GOTTPOFF:
74 return BUILD_GOT_ONLY;
76 case R_X86_64_GOT32:
77 case R_X86_64_GOTPCREL:
78 case R_X86_64_GOTPCRELX:
79 case R_X86_64_REX_GOTPCRELX:
80 case R_X86_64_PLT32:
81 return ALWAYS_GOTPLT_ENTRY;
84 tcc_error ("Unknown relocation type: %d", reloc_type);
85 return -1;
88 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
90 Section *plt = s1->plt;
91 uint8_t *p;
92 int modrm;
93 unsigned plt_offset, relofs;
95 modrm = 0x25;
97 /* empty PLT: create PLT0 entry that pushes the library indentifier
98 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
99 (GOT + 2 * PTR_SIZE) */
100 if (plt->data_offset == 0) {
101 p = section_ptr_add(plt, 16);
102 p[0] = 0xff; /* pushl got + PTR_SIZE */
103 p[1] = modrm + 0x10;
104 write32le(p + 2, PTR_SIZE);
105 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
106 p[7] = modrm;
107 write32le(p + 8, PTR_SIZE * 2);
109 plt_offset = plt->data_offset;
111 /* The PLT slot refers to the relocation entry it needs via offset.
112 The reloc entry is created below, so its offset is the current
113 data_offset */
114 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
116 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
117 p = section_ptr_add(plt, 16);
118 p[0] = 0xff; /* jmp *(got + x) */
119 p[1] = modrm;
120 write32le(p + 2, got_offset);
121 p[6] = 0x68; /* push $xxx */
122 /* On x86-64, the relocation is referred to by _index_ */
123 write32le(p + 7, relofs / sizeof (ElfW_Rel));
124 p[11] = 0xe9; /* jmp plt_start */
125 write32le(p + 12, -(plt->data_offset));
126 return plt_offset;
129 /* relocate the PLT: compute addresses and offsets in the PLT now that final
130 address for PLT and GOT are known (see fill_program_header) */
131 ST_FUNC void relocate_plt(TCCState *s1)
133 uint8_t *p, *p_end;
135 if (!s1->plt)
136 return;
138 p = s1->plt->data;
139 p_end = p + s1->plt->data_offset;
141 if (p < p_end) {
142 int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
143 add32le(p + 2, x);
144 add32le(p + 8, x - 6);
145 p += 16;
146 while (p < p_end) {
147 add32le(p + 2, x + s1->plt->data - p);
148 p += 16;
153 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
155 void relocate_init(Section *sr)
157 qrel = (ElfW_Rel *) sr->data;
160 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
162 int sym_index, esym_index;
164 sym_index = ELFW(R_SYM)(rel->r_info);
166 switch (type) {
167 case R_X86_64_64:
168 if (s1->output_type == TCC_OUTPUT_DLL) {
169 esym_index = s1->sym_attrs[sym_index].dyn_index;
170 qrel->r_offset = rel->r_offset;
171 if (esym_index) {
172 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
173 qrel->r_addend = rel->r_addend;
174 qrel++;
175 break;
176 } else {
177 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
178 qrel->r_addend = read64le(ptr) + val;
179 qrel++;
182 add64le(ptr, val);
183 break;
184 case R_X86_64_32:
185 case R_X86_64_32S:
186 if (s1->output_type == TCC_OUTPUT_DLL) {
187 /* XXX: this logic may depend on TCC's codegen
188 now TCC uses R_X86_64_32 even for a 64bit pointer */
189 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
190 /* Use sign extension! */
191 qrel->r_addend = (int)read32le(ptr) + val;
192 qrel++;
194 add32le(ptr, val);
195 break;
197 case R_X86_64_PC32:
198 if (s1->output_type == TCC_OUTPUT_DLL) {
199 /* DLL relocation */
200 esym_index = s1->sym_attrs[sym_index].dyn_index;
201 if (esym_index) {
202 qrel->r_offset = rel->r_offset;
203 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
204 /* Use sign extension! */
205 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
206 qrel++;
207 break;
210 goto plt32pc32;
212 case R_X86_64_PLT32:
213 /* fallthrough: val already holds the PLT slot address */
215 plt32pc32:
217 long long diff;
218 diff = (long long)val - addr;
219 if (diff < -2147483648LL || diff > 2147483647LL) {
220 tcc_error("internal error: relocation failed");
222 add32le(ptr, diff);
224 break;
225 case R_X86_64_GLOB_DAT:
226 case R_X86_64_JUMP_SLOT:
227 /* They don't need addend */
228 write64le(ptr, val - rel->r_addend);
229 break;
230 case R_X86_64_GOTPCREL:
231 case R_X86_64_GOTPCRELX:
232 case R_X86_64_REX_GOTPCRELX:
233 add32le(ptr, s1->got->sh_addr - addr +
234 s1->sym_attrs[sym_index].got_offset - 4);
235 break;
236 case R_X86_64_GOTTPOFF:
237 add32le(ptr, val - s1->got->sh_addr);
238 break;
239 case R_X86_64_GOT32:
240 /* we load the got offset */
241 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
242 break;
243 case R_X86_64_RELATIVE:
244 /* do nothing */
245 break;
249 #endif /* !TARGET_DEFS_ONLY */