Fix test90 for 32 bits targets
[tinycc.git] / i386-link.c
blobee344b932c03099892f3ce586893d6332cfce27d
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 #ifdef NEED_RELOC_TYPE
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 #ifdef NEED_BUILD_GOT
95 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
97 Section *plt = s1->plt;
98 uint8_t *p;
99 int modrm;
100 unsigned plt_offset, relofs;
102 /* on i386 if we build a DLL, we add a %ebx offset */
103 if (s1->output_type & TCC_OUTPUT_DYN)
104 modrm = 0xa3;
105 else
106 modrm = 0x25;
108 /* empty PLT: create PLT0 entry that pushes the library identifier
109 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
110 (GOT + 2 * PTR_SIZE) */
111 if (plt->data_offset == 0) {
112 p = section_ptr_add(plt, 16);
113 p[0] = 0xff; /* pushl got + PTR_SIZE */
114 p[1] = modrm + 0x10;
115 write32le(p + 2, PTR_SIZE);
116 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
117 p[7] = modrm;
118 write32le(p + 8, PTR_SIZE * 2);
120 plt_offset = plt->data_offset;
122 /* The PLT slot refers to the relocation entry it needs via offset.
123 The reloc entry is created below, so its offset is the current
124 data_offset */
125 relofs = s1->plt->reloc ? s1->plt->reloc->data_offset : 0;
127 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
128 p = section_ptr_add(plt, 16);
129 p[0] = 0xff; /* jmp *(got + x) */
130 p[1] = modrm;
131 write32le(p + 2, got_offset);
132 p[6] = 0x68; /* push $xxx */
133 write32le(p + 7, relofs - sizeof (ElfW_Rel));
134 p[11] = 0xe9; /* jmp plt_start */
135 write32le(p + 12, -(plt->data_offset));
136 return plt_offset;
139 /* relocate the PLT: compute addresses and offsets in the PLT now that final
140 address for PLT and GOT are known (see fill_program_header) */
141 ST_FUNC void relocate_plt(TCCState *s1)
143 uint8_t *p, *p_end;
145 if (!s1->plt)
146 return;
148 p = s1->plt->data;
149 p_end = p + s1->plt->data_offset;
151 if (!(s1->output_type & TCC_OUTPUT_DYN) && p < p_end) {
152 add32le(p + 2, s1->got->sh_addr);
153 add32le(p + 8, s1->got->sh_addr);
154 p += 16;
155 while (p < p_end) {
156 add32le(p + 2, s1->got->sh_addr);
157 p += 16;
161 if (s1->plt->reloc) {
162 ElfW_Rel *rel;
163 int x = s1->plt->sh_addr + 16 + 6;
164 p = s1->got->data;
165 for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
166 write32le(p + rel->r_offset, x);
167 x += 16;
171 #endif
172 #endif
174 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
176 int sym_index, esym_index;
178 sym_index = ELFW(R_SYM)(rel->r_info);
180 switch (type) {
181 case R_386_32:
182 if (s1->output_type & TCC_OUTPUT_DYN) {
183 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
184 qrel->r_offset = rel->r_offset;
185 if (esym_index) {
186 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
187 qrel++;
188 return;
189 } else {
190 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
191 qrel++;
194 add32le(ptr, val);
195 return;
196 case R_386_PC32:
197 if (s1->output_type == TCC_OUTPUT_DLL) {
198 /* DLL relocation */
199 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
200 if (esym_index) {
201 qrel->r_offset = rel->r_offset;
202 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
203 qrel++;
204 return;
207 add32le(ptr, val - addr);
208 return;
209 case R_386_PLT32:
210 add32le(ptr, val - addr);
211 return;
212 case R_386_GLOB_DAT:
213 case R_386_JMP_SLOT:
214 write32le(ptr, val);
215 return;
216 case R_386_GOTPC:
217 add32le(ptr, s1->got->sh_addr - addr);
218 return;
219 case R_386_GOTOFF:
220 add32le(ptr, val - s1->got->sh_addr);
221 return;
222 case R_386_GOT32:
223 case R_386_GOT32X:
224 /* we load the got offset */
225 add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
226 return;
227 case R_386_16:
228 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
229 output_file:
230 tcc_error("can only produce 16-bit binary files");
232 write16le(ptr, read16le(ptr) + val);
233 return;
234 case R_386_PC16:
235 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
236 goto output_file;
237 write16le(ptr, read16le(ptr) + val - addr);
238 return;
239 case R_386_RELATIVE:
240 #ifdef TCC_TARGET_PE
241 add32le(ptr, val - s1->pe_imagebase);
242 #endif
243 /* do nothing */
244 return;
245 case R_386_COPY:
246 /* This relocation must copy initialized data from the library
247 to the program .bss segment. Currently made like for ARM
248 (to remove noise of default case). Is this true?
250 return;
251 case R_386_TLS_GD:
253 static const unsigned char expect[] = {
254 /* lea 0(,%ebx,1),%eax */
255 0x8d, 0x04, 0x1d, 0x00, 0x00, 0x00, 0x00,
256 /* call __tls_get_addr@PLT */
257 0xe8, 0xfc, 0xff, 0xff, 0xff };
258 static const unsigned char replace[] = {
259 /* mov %gs:0,%eax */
260 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
261 /* sub 0,%eax */
262 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 };
264 if (memcmp (ptr-3, expect, sizeof(expect)) == 0) {
265 ElfW(Sym) *sym;
266 Section *sec;
267 int32_t x;
269 memcpy(ptr-3, replace, sizeof(replace));
270 rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
271 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
272 sec = s1->sections[sym->st_shndx];
273 x = sym->st_value - sec->sh_addr - sec->data_offset;
274 add32le(ptr + 5, -x);
276 else
277 tcc_error("unexpected R_386_TLS_GD pattern");
279 return;
280 case R_386_TLS_LDM:
282 static const unsigned char expect[] = {
283 /* lea 0(%ebx),%eax */
284 0x8d, 0x83, 0x00, 0x00, 0x00, 0x00,
285 /* call __tls_get_addr@PLT */
286 0xe8, 0xfc, 0xff, 0xff, 0xff };
287 static const unsigned char replace[] = {
288 /* mov %gs:0,%eax */
289 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
290 /* nop */
291 0x90,
292 /* lea 0(%esi,%eiz,1),%esi */
293 0x8d, 0x74, 0x26, 0x00 };
295 if (memcmp (ptr-2, expect, sizeof(expect)) == 0) {
296 memcpy(ptr-2, replace, sizeof(replace));
297 rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
299 else
300 tcc_error("unexpected R_386_TLS_LDM pattern");
302 return;
303 case R_386_TLS_LDO_32:
304 case R_386_TLS_LE:
306 ElfW(Sym) *sym;
307 Section *sec;
308 int32_t x;
310 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
311 sec = s1->sections[sym->st_shndx];
312 x = val - sec->sh_addr - sec->data_offset;
313 add32le(ptr, x);
315 return;
316 case R_386_NONE:
317 return;
318 default:
319 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
320 type, (unsigned)addr, ptr, (unsigned)val);
321 return;
325 #endif /* !TARGET_DEFS_ONLY */