tccgen: introduce TOK_NEG for unary minus
[tinycc.git] / i386-link.c
blobc941469371d6b140079d63570bacd28f8ab41615
1 #ifdef TARGET_DEFS_ONLY
3 #define EM_TCC_TARGET EM_386
5 /* relocation type for 32 bit data relocation */
6 #define R_DATA_32 R_386_32
7 #define R_DATA_PTR R_386_32
8 #define R_JMP_SLOT R_386_JMP_SLOT
9 #define R_GLOB_DAT R_386_GLOB_DAT
10 #define R_COPY R_386_COPY
11 #define R_RELATIVE R_386_RELATIVE
13 #define R_NUM R_386_NUM
15 #define ELF_START_ADDR 0x08048000
16 #define ELF_PAGE_SIZE 0x1000
18 #define PCRELATIVE_DLLPLT 0
19 #define RELOCATE_DLLPLT 1
21 #else /* !TARGET_DEFS_ONLY */
23 #include "tcc.h"
25 #ifndef ELF_OBJ_ONLY
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_386_RELATIVE:
32 case R_386_16:
33 case R_386_32:
34 case R_386_GOTPC:
35 case R_386_GOTOFF:
36 case R_386_GOT32:
37 case R_386_GOT32X:
38 case R_386_GLOB_DAT:
39 case R_386_COPY:
40 case R_386_TLS_GD:
41 case R_386_TLS_LDM:
42 case R_386_TLS_LDO_32:
43 case R_386_TLS_LE:
44 return 0;
46 case R_386_PC16:
47 case R_386_PC32:
48 case R_386_PLT32:
49 case R_386_JMP_SLOT:
50 return 1;
52 return -1;
55 /* Returns an enumerator to describe whether and when the relocation needs a
56 GOT and/or PLT entry to be created. See tcc.h for a description of the
57 different values. */
58 int gotplt_entry_type (int reloc_type)
60 switch (reloc_type) {
61 case R_386_RELATIVE:
62 case R_386_16:
63 case R_386_GLOB_DAT:
64 case R_386_JMP_SLOT:
65 case R_386_COPY:
66 return NO_GOTPLT_ENTRY;
68 case R_386_32:
69 /* This relocations shouldn't normally need GOT or PLT
70 slots if it weren't for simplicity in the code generator.
71 See our caller for comments. */
72 return AUTO_GOTPLT_ENTRY;
74 case R_386_PC16:
75 case R_386_PC32:
76 return AUTO_GOTPLT_ENTRY;
78 case R_386_GOTPC:
79 case R_386_GOTOFF:
80 return BUILD_GOT_ONLY;
82 case R_386_GOT32:
83 case R_386_GOT32X:
84 case R_386_PLT32:
85 case R_386_TLS_GD:
86 case R_386_TLS_LDM:
87 case R_386_TLS_LDO_32:
88 case R_386_TLS_LE:
89 return ALWAYS_GOTPLT_ENTRY;
91 return -1;
94 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
96 Section *plt = s1->plt;
97 uint8_t *p;
98 int modrm;
99 unsigned plt_offset, relofs;
101 /* on i386 if we build a DLL, we add a %ebx offset */
102 if (s1->output_type == TCC_OUTPUT_DLL)
103 modrm = 0xa3;
104 else
105 modrm = 0x25;
107 /* empty PLT: create PLT0 entry that pushes the library identifier
108 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
109 (GOT + 2 * PTR_SIZE) */
110 if (plt->data_offset == 0) {
111 p = section_ptr_add(plt, 16);
112 p[0] = 0xff; /* pushl got + PTR_SIZE */
113 p[1] = modrm + 0x10;
114 write32le(p + 2, PTR_SIZE);
115 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
116 p[7] = modrm;
117 write32le(p + 8, PTR_SIZE * 2);
119 plt_offset = plt->data_offset;
121 /* The PLT slot refers to the relocation entry it needs via offset.
122 The reloc entry is created below, so its offset is the current
123 data_offset */
124 relofs = s1->got->relocplt ? s1->got->relocplt->data_offset : 0;
126 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
127 p = section_ptr_add(plt, 16);
128 p[0] = 0xff; /* jmp *(got + x) */
129 p[1] = modrm;
130 write32le(p + 2, got_offset);
131 p[6] = 0x68; /* push $xxx */
132 write32le(p + 7, relofs - sizeof (ElfW_Rel));
133 p[11] = 0xe9; /* jmp plt_start */
134 write32le(p + 12, -(plt->data_offset));
135 return plt_offset;
138 /* relocate the PLT: compute addresses and offsets in the PLT now that final
139 address for PLT and GOT are known (see fill_program_header) */
140 ST_FUNC void relocate_plt(TCCState *s1)
142 uint8_t *p, *p_end;
144 if (!s1->plt)
145 return;
147 p = s1->plt->data;
148 p_end = p + s1->plt->data_offset;
150 if (s1->output_type != TCC_OUTPUT_DLL && p < p_end) {
151 add32le(p + 2, s1->got->sh_addr);
152 add32le(p + 8, s1->got->sh_addr);
153 p += 16;
154 while (p < p_end) {
155 add32le(p + 2, s1->got->sh_addr);
156 p += 16;
160 if (s1->got->relocplt) {
161 int mem = s1->output_type == TCC_OUTPUT_MEMORY;
162 ElfW_Rel *rel;
163 int x = s1->plt->sh_addr + 16 + 6;
165 p = s1->got->data;
166 for_each_elem(s1->got->relocplt, 0, rel, ElfW_Rel) {
167 int sym_index = ELFW(R_SYM)(rel->r_info);
168 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
169 write32le(p + rel->r_offset, mem ? sym->st_value : x);
170 x += 16;
174 #endif
176 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
178 int sym_index, esym_index;
180 sym_index = ELFW(R_SYM)(rel->r_info);
182 switch (type) {
183 case R_386_32:
184 if (s1->output_type == TCC_OUTPUT_DLL) {
185 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
186 qrel->r_offset = rel->r_offset;
187 if (esym_index) {
188 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
189 qrel++;
190 return;
191 } else {
192 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
193 qrel++;
196 add32le(ptr, val);
197 return;
198 case R_386_PC32:
199 if (s1->output_type == TCC_OUTPUT_DLL) {
200 /* DLL relocation */
201 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
202 if (esym_index) {
203 qrel->r_offset = rel->r_offset;
204 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
205 qrel++;
206 return;
209 add32le(ptr, val - addr);
210 return;
211 case R_386_PLT32:
212 add32le(ptr, val - addr);
213 return;
214 case R_386_GLOB_DAT:
215 case R_386_JMP_SLOT:
216 write32le(ptr, val);
217 return;
218 case R_386_GOTPC:
219 add32le(ptr, s1->got->sh_addr - addr);
220 return;
221 case R_386_GOTOFF:
222 add32le(ptr, val - s1->got->sh_addr);
223 return;
224 case R_386_GOT32:
225 case R_386_GOT32X:
226 /* we load the got offset */
227 add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
228 return;
229 case R_386_16:
230 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
231 output_file:
232 tcc_error("can only produce 16-bit binary files");
234 write16le(ptr, read16le(ptr) + val);
235 return;
236 case R_386_PC16:
237 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
238 goto output_file;
239 write16le(ptr, read16le(ptr) + val - addr);
240 return;
241 case R_386_RELATIVE:
242 #ifdef TCC_TARGET_PE
243 add32le(ptr, val - s1->pe_imagebase);
244 #endif
245 /* do nothing */
246 return;
247 case R_386_COPY:
248 /* This relocation must copy initialized data from the library
249 to the program .bss segment. Currently made like for ARM
250 (to remove noise of default case). Is this true?
252 return;
253 case R_386_TLS_GD:
255 static const unsigned char expect[] = {
256 /* lea 0(,%ebx,1),%eax */
257 0x8d, 0x04, 0x1d, 0x00, 0x00, 0x00, 0x00,
258 /* call __tls_get_addr@PLT */
259 0xe8, 0xfc, 0xff, 0xff, 0xff };
260 static const unsigned char replace[] = {
261 /* mov %gs:0,%eax */
262 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
263 /* sub 0,%eax */
264 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 };
266 if (memcmp (ptr-3, expect, sizeof(expect)) == 0) {
267 ElfW(Sym) *sym;
268 Section *sec;
269 int32_t x;
271 memcpy(ptr-3, replace, sizeof(replace));
272 rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
273 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
274 sec = s1->sections[sym->st_shndx];
275 x = sym->st_value - sec->sh_addr - sec->data_offset;
276 add32le(ptr + 5, -x);
278 else
279 tcc_error("unexpected R_386_TLS_GD pattern");
281 return;
282 case R_386_TLS_LDM:
284 static const unsigned char expect[] = {
285 /* lea 0(%ebx),%eax */
286 0x8d, 0x83, 0x00, 0x00, 0x00, 0x00,
287 /* call __tls_get_addr@PLT */
288 0xe8, 0xfc, 0xff, 0xff, 0xff };
289 static const unsigned char replace[] = {
290 /* mov %gs:0,%eax */
291 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
292 /* nop */
293 0x90,
294 /* lea 0(%esi,%eiz,1),%esi */
295 0x8d, 0x74, 0x26, 0x00 };
297 if (memcmp (ptr-2, expect, sizeof(expect)) == 0) {
298 memcpy(ptr-2, replace, sizeof(replace));
299 rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
301 else
302 tcc_error("unexpected R_386_TLS_LDM pattern");
304 return;
305 case R_386_TLS_LDO_32:
306 case R_386_TLS_LE:
308 ElfW(Sym) *sym;
309 Section *sec;
310 int32_t x;
312 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
313 sec = s1->sections[sym->st_shndx];
314 x = val - sec->sh_addr - sec->data_offset;
315 add32le(ptr, x);
317 return;
318 case R_386_NONE:
319 return;
320 default:
321 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
322 type, (unsigned)addr, ptr, (unsigned)val);
323 return;
327 #endif /* !TARGET_DEFS_ONLY */