C string litteral is const, fix return type of default_elfinterp. Remove #ifndef...
[tinycc.git] / x86_64-link.c
blob1485e055e6e34949f1a54baf1fb32c2d5f9126c0
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_GOTPCREL:
34 case R_X86_64_GOTPCRELX:
35 case R_X86_64_REX_GOTPCRELX:
36 case R_X86_64_GOTTPOFF:
37 case R_X86_64_GOT32:
38 case R_X86_64_GLOB_DAT:
39 case R_X86_64_COPY:
40 case R_X86_64_RELATIVE:
41 return 0;
43 case R_X86_64_PC32:
44 case R_X86_64_PLT32:
45 case R_X86_64_JUMP_SLOT:
46 return 1;
49 tcc_error ("Unknown relocation type: %d", reloc_type);
50 return -1;
53 /* Returns an enumerator to describe whether and when the relocation needs a
54 GOT and/or PLT entry to be created. See tcc.h for a description of the
55 different values. */
56 int gotplt_entry_type (int reloc_type)
58 switch (reloc_type) {
59 case R_X86_64_GLOB_DAT:
60 case R_X86_64_JUMP_SLOT:
61 case R_X86_64_COPY:
62 case R_X86_64_RELATIVE:
63 return NO_GOTPLT_ENTRY;
65 /* The following relocs wouldn't normally need GOT or PLT
66 slots, but we need them for simplicity in the link
67 editor part. See our caller for comments. */
68 case R_X86_64_32:
69 case R_X86_64_32S:
70 case R_X86_64_64:
71 case R_X86_64_PC32:
72 return AUTO_GOTPLT_ENTRY;
74 case R_X86_64_GOTTPOFF:
75 return BUILD_GOT_ONLY;
77 case R_X86_64_GOT32:
78 case R_X86_64_GOTPCREL:
79 case R_X86_64_GOTPCRELX:
80 case R_X86_64_REX_GOTPCRELX:
81 case R_X86_64_PLT32:
82 return ALWAYS_GOTPLT_ENTRY;
85 tcc_error ("Unknown relocation type: %d", reloc_type);
86 return -1;
89 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
91 Section *plt = s1->plt;
92 uint8_t *p;
93 int modrm;
94 unsigned plt_offset, relofs;
96 modrm = 0x25;
98 /* empty PLT: create PLT0 entry that pushes the library identifier
99 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
100 (GOT + 2 * PTR_SIZE) */
101 if (plt->data_offset == 0) {
102 p = section_ptr_add(plt, 16);
103 p[0] = 0xff; /* pushl got + PTR_SIZE */
104 p[1] = modrm + 0x10;
105 write32le(p + 2, PTR_SIZE);
106 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
107 p[7] = modrm;
108 write32le(p + 8, PTR_SIZE * 2);
110 plt_offset = plt->data_offset;
112 /* The PLT slot refers to the relocation entry it needs via offset.
113 The reloc entry is created below, so its offset is the current
114 data_offset */
115 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
117 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
118 p = section_ptr_add(plt, 16);
119 p[0] = 0xff; /* jmp *(got + x) */
120 p[1] = modrm;
121 write32le(p + 2, got_offset);
122 p[6] = 0x68; /* push $xxx */
123 /* On x86-64, the relocation is referred to by _index_ */
124 write32le(p + 7, relofs / sizeof (ElfW_Rel));
125 p[11] = 0xe9; /* jmp plt_start */
126 write32le(p + 12, -(plt->data_offset));
127 return plt_offset;
130 /* relocate the PLT: compute addresses and offsets in the PLT now that final
131 address for PLT and GOT are known (see fill_program_header) */
132 ST_FUNC void relocate_plt(TCCState *s1)
134 uint8_t *p, *p_end;
136 if (!s1->plt)
137 return;
139 p = s1->plt->data;
140 p_end = p + s1->plt->data_offset;
142 if (p < p_end) {
143 int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
144 add32le(p + 2, x);
145 add32le(p + 8, x - 6);
146 p += 16;
147 while (p < p_end) {
148 add32le(p + 2, x + s1->plt->data - p);
149 p += 16;
154 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
156 void relocate_init(Section *sr)
158 qrel = (ElfW_Rel *) sr->data;
161 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
163 int sym_index, esym_index;
165 sym_index = ELFW(R_SYM)(rel->r_info);
167 switch (type) {
168 case R_X86_64_64:
169 if (s1->output_type == TCC_OUTPUT_DLL) {
170 esym_index = s1->sym_attrs[sym_index].dyn_index;
171 qrel->r_offset = rel->r_offset;
172 if (esym_index) {
173 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
174 qrel->r_addend = rel->r_addend;
175 qrel++;
176 break;
177 } else {
178 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
179 qrel->r_addend = read64le(ptr) + val;
180 qrel++;
183 add64le(ptr, val);
184 break;
185 case R_X86_64_32:
186 case R_X86_64_32S:
187 if (s1->output_type == TCC_OUTPUT_DLL) {
188 /* XXX: this logic may depend on TCC's codegen
189 now TCC uses R_X86_64_32 even for a 64bit pointer */
190 qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
191 /* Use sign extension! */
192 qrel->r_addend = (int)read32le(ptr) + val;
193 qrel++;
195 add32le(ptr, val);
196 break;
198 case R_X86_64_PC32:
199 if (s1->output_type == TCC_OUTPUT_DLL) {
200 /* DLL relocation */
201 esym_index = s1->sym_attrs[sym_index].dyn_index;
202 if (esym_index) {
203 qrel->r_offset = rel->r_offset;
204 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
205 /* Use sign extension! */
206 qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
207 qrel++;
208 break;
211 goto plt32pc32;
213 case R_X86_64_PLT32:
214 /* fallthrough: val already holds the PLT slot address */
216 plt32pc32:
218 long long diff;
219 diff = (long long)val - addr;
220 if (diff < -2147483648LL || diff > 2147483647LL) {
221 tcc_error("internal error: relocation failed");
223 add32le(ptr, diff);
225 break;
226 case R_X86_64_GLOB_DAT:
227 case R_X86_64_JUMP_SLOT:
228 /* They don't need addend */
229 write64le(ptr, val - rel->r_addend);
230 break;
231 case R_X86_64_GOTPCREL:
232 case R_X86_64_GOTPCRELX:
233 case R_X86_64_REX_GOTPCRELX:
234 add32le(ptr, s1->got->sh_addr - addr +
235 s1->sym_attrs[sym_index].got_offset - 4);
236 break;
237 case R_X86_64_GOTTPOFF:
238 add32le(ptr, val - s1->got->sh_addr);
239 break;
240 case R_X86_64_GOT32:
241 /* we load the got offset */
242 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
243 break;
244 case R_X86_64_RELATIVE:
245 /* do nothing */
246 break;
250 #endif /* !TARGET_DEFS_ONLY */