bitfields: Implement MS compatible layout
[tinycc.git] / x86_64-link.c
blob602a8ef0e177eb381ce07bf646bc39812c35e22c
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 case R_X86_64_32:
65 case R_X86_64_32S:
66 case R_X86_64_64:
67 case R_X86_64_PC32:
68 return AUTO_GOTPLT_ENTRY;
70 case R_X86_64_GOTTPOFF:
71 return BUILD_GOT_ONLY;
73 case R_X86_64_GOT32:
74 case R_X86_64_GOTPCREL:
75 case R_X86_64_GOTPCRELX:
76 case R_X86_64_REX_GOTPCRELX:
77 case R_X86_64_PLT32:
78 return ALWAYS_GOTPLT_ENTRY;
81 tcc_error ("Unknown relocation type: %d", reloc_type);
82 return -1;
85 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
87 Section *plt = s1->plt;
88 uint8_t *p;
89 int modrm;
90 unsigned plt_offset, relofs;
92 modrm = 0x25;
94 /* empty PLT: create PLT0 entry that pushes the library indentifier
95 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
96 (GOT + 2 * PTR_SIZE) */
97 if (plt->data_offset == 0) {
98 p = section_ptr_add(plt, 16);
99 p[0] = 0xff; /* pushl got + PTR_SIZE */
100 p[1] = modrm + 0x10;
101 write32le(p + 2, PTR_SIZE);
102 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
103 p[7] = modrm;
104 write32le(p + 8, PTR_SIZE * 2);
106 plt_offset = plt->data_offset;
108 /* The PLT slot refers to the relocation entry it needs via offset.
109 The reloc entry is created below, so its offset is the current
110 data_offset */
111 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
113 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
114 p = section_ptr_add(plt, 16);
115 p[0] = 0xff; /* jmp *(got + x) */
116 p[1] = modrm;
117 write32le(p + 2, got_offset);
118 p[6] = 0x68; /* push $xxx */
119 /* On x86-64, the relocation is referred to by _index_ */
120 write32le(p + 7, relofs / sizeof (ElfW_Rel));
121 p[11] = 0xe9; /* jmp plt_start */
122 write32le(p + 12, -(plt->data_offset));
123 return plt_offset;
126 /* relocate the PLT: compute addresses and offsets in the PLT now that final
127 address for PLT and GOT are known (see fill_program_header) */
128 ST_FUNC void relocate_plt(TCCState *s1)
130 uint8_t *p, *p_end;
132 if (!s1->plt)
133 return;
135 p = s1->plt->data;
136 p_end = p + s1->plt->data_offset;
138 if (p < p_end) {
139 int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
140 add32le(p + 2, x);
141 add32le(p + 8, x - 6);
142 p += 16;
143 while (p < p_end) {
144 add32le(p + 2, x + s1->plt->data - p);
145 p += 16;
150 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
152 void relocate_init(Section *sr)
154 qrel = (ElfW_Rel *) sr->data;
157 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
159 int sym_index, esym_index;
161 sym_index = ELFW(R_SYM)(rel->r_info);
163 switch (type) {
164 case R_X86_64_64:
165 if (s1->output_type == TCC_OUTPUT_DLL) {
166 esym_index = s1->sym_attrs[sym_index].dyn_index;
167 qrel->r_offset = rel->r_offset;
168 if (esym_index) {
169 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
170 qrel->r_addend = rel->r_addend;
171 qrel++;
172 break;
173 } else {
174 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
175 qrel->r_addend = read64le(ptr) + val;
176 qrel++;
179 add64le(ptr, val);
180 break;
181 case R_X86_64_32:
182 case R_X86_64_32S:
183 if (s1->output_type == TCC_OUTPUT_DLL) {
184 /* XXX: this logic may depend on TCC's codegen
185 now TCC uses R_X86_64_32 even for a 64bit pointer */
186 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
187 /* Use sign extension! */
188 qrel->r_addend = (int)read32le(ptr) + val;
189 qrel++;
191 add32le(ptr, val);
192 break;
194 case R_X86_64_PC32:
195 if (s1->output_type == TCC_OUTPUT_DLL) {
196 /* DLL relocation */
197 esym_index = s1->sym_attrs[sym_index].dyn_index;
198 if (esym_index) {
199 qrel->r_offset = rel->r_offset;
200 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
201 /* Use sign extension! */
202 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
203 qrel++;
204 break;
207 goto plt32pc32;
209 case R_X86_64_PLT32:
210 /* fallthrough: val already holds the PLT slot address */
212 plt32pc32:
214 long long diff;
215 diff = (long long)val - addr;
216 if (diff < -2147483648LL || diff > 2147483647LL) {
217 tcc_error("internal error: relocation failed");
219 add32le(ptr, diff);
221 break;
222 case R_X86_64_GLOB_DAT:
223 case R_X86_64_JUMP_SLOT:
224 /* They don't need addend */
225 write64le(ptr, val - rel->r_addend);
226 break;
227 case R_X86_64_GOTPCREL:
228 case R_X86_64_GOTPCRELX:
229 case R_X86_64_REX_GOTPCRELX:
230 add32le(ptr, s1->got->sh_addr - addr +
231 s1->sym_attrs[sym_index].got_offset - 4);
232 break;
233 case R_X86_64_GOTTPOFF:
234 add32le(ptr, val - s1->got->sh_addr);
235 break;
236 case R_X86_64_GOT32:
237 /* we load the got offset */
238 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
239 break;
240 case R_X86_64_RELATIVE:
241 /* do nothing */
242 break;
246 #endif /* !TARGET_DEFS_ONLY */