tccgen.c: cleanup reg classes
[tinycc.git] / i386-link.c
blob23653b299f2e4b3a1a54aa8879f7af1bc23bcbba
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 0
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 return 0;
42 case R_386_PC16:
43 case R_386_PC32:
44 case R_386_PLT32:
45 case R_386_JMP_SLOT:
46 return 1;
48 return -1;
51 /* Returns an enumerator to describe whether and when the relocation needs a
52 GOT and/or PLT entry to be created. See tcc.h for a description of the
53 different values. */
54 int gotplt_entry_type (int reloc_type)
56 switch (reloc_type) {
57 case R_386_RELATIVE:
58 case R_386_16:
59 case R_386_GLOB_DAT:
60 case R_386_JMP_SLOT:
61 case R_386_COPY:
62 return NO_GOTPLT_ENTRY;
64 case R_386_32:
65 /* This relocations shouldn't normally need GOT or PLT
66 slots if it weren't for simplicity in the code generator.
67 See our caller for comments. */
68 return AUTO_GOTPLT_ENTRY;
70 case R_386_PC16:
71 case R_386_PC32:
72 return AUTO_GOTPLT_ENTRY;
74 case R_386_GOTPC:
75 case R_386_GOTOFF:
76 return BUILD_GOT_ONLY;
78 case R_386_GOT32:
79 case R_386_GOT32X:
80 case R_386_PLT32:
81 return ALWAYS_GOTPLT_ENTRY;
83 return -1;
86 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
88 Section *plt = s1->plt;
89 uint8_t *p;
90 int modrm;
91 unsigned plt_offset, relofs;
93 /* on i386 if we build a DLL, we add a %ebx offset */
94 if (s1->output_type == TCC_OUTPUT_DLL)
95 modrm = 0xa3;
96 else
97 modrm = 0x25;
99 /* empty PLT: create PLT0 entry that pushes the library identifier
100 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
101 (GOT + 2 * PTR_SIZE) */
102 if (plt->data_offset == 0) {
103 p = section_ptr_add(plt, 16);
104 p[0] = 0xff; /* pushl got + PTR_SIZE */
105 p[1] = modrm + 0x10;
106 write32le(p + 2, PTR_SIZE);
107 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
108 p[7] = modrm;
109 write32le(p + 8, PTR_SIZE * 2);
111 plt_offset = plt->data_offset;
113 /* The PLT slot refers to the relocation entry it needs via offset.
114 The reloc entry is created below, so its offset is the current
115 data_offset */
116 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
118 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
119 p = section_ptr_add(plt, 16);
120 p[0] = 0xff; /* jmp *(got + x) */
121 p[1] = modrm;
122 write32le(p + 2, got_offset);
123 p[6] = 0x68; /* push $xxx */
124 write32le(p + 7, relofs);
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 add32le(p + 2, s1->got->sh_addr);
144 add32le(p + 8, s1->got->sh_addr);
145 p += 16;
146 while (p < p_end) {
147 add32le(p + 2, s1->got->sh_addr);
148 p += 16;
152 #endif
154 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
156 int sym_index, esym_index;
158 sym_index = ELFW(R_SYM)(rel->r_info);
160 switch (type) {
161 case R_386_32:
162 if (s1->output_type == TCC_OUTPUT_DLL) {
163 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
164 qrel->r_offset = rel->r_offset;
165 if (esym_index) {
166 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
167 qrel++;
168 return;
169 } else {
170 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
171 qrel++;
174 add32le(ptr, val);
175 return;
176 case R_386_PC32:
177 if (s1->output_type == TCC_OUTPUT_DLL) {
178 /* DLL relocation */
179 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
180 if (esym_index) {
181 qrel->r_offset = rel->r_offset;
182 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
183 qrel++;
184 return;
187 add32le(ptr, val - addr);
188 return;
189 case R_386_PLT32:
190 add32le(ptr, val - addr);
191 return;
192 case R_386_GLOB_DAT:
193 case R_386_JMP_SLOT:
194 write32le(ptr, val);
195 return;
196 case R_386_GOTPC:
197 add32le(ptr, s1->got->sh_addr - addr);
198 return;
199 case R_386_GOTOFF:
200 add32le(ptr, val - s1->got->sh_addr);
201 return;
202 case R_386_GOT32:
203 case R_386_GOT32X:
204 /* we load the got offset */
205 add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
206 return;
207 case R_386_16:
208 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
209 output_file:
210 tcc_error("can only produce 16-bit binary files");
212 write16le(ptr, read16le(ptr) + val);
213 return;
214 case R_386_PC16:
215 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
216 goto output_file;
217 write16le(ptr, read16le(ptr) + val - addr);
218 return;
219 case R_386_RELATIVE:
220 #ifdef TCC_TARGET_PE
221 add32le(ptr, val - s1->pe_imagebase);
222 #endif
223 /* do nothing */
224 return;
225 case R_386_COPY:
226 /* This relocation must copy initialized data from the library
227 to the program .bss segment. Currently made like for ARM
228 (to remove noise of default case). Is this true?
230 return;
231 default:
232 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
233 type, (unsigned)addr, ptr, (unsigned)val);
234 return;
238 #ifdef CONFIG_TCC_BCHECK
239 ST_FUNC void tcc_add_bcheck(TCCState *s1)
241 addr_t *ptr;
242 int loc_glob;
243 int sym_index;
244 int bsym_index;
246 if (0 == s1->do_bounds_check)
247 return;
248 /* XXX: add an object file to do that */
249 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
250 *ptr = 0;
251 loc_glob = s1->output_type != TCC_OUTPUT_MEMORY ? STB_LOCAL : STB_GLOBAL;
252 bsym_index = set_elf_sym(symtab_section, 0, 0,
253 ELFW(ST_INFO)(loc_glob, STT_NOTYPE), 0,
254 bounds_section->sh_num, "__bounds_start");
255 /* pull bcheck.o from libtcc1.a */
256 sym_index = set_elf_sym(symtab_section, 0, 0,
257 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
258 SHN_UNDEF, "__bound_init");
259 if (s1->output_type != TCC_OUTPUT_MEMORY) {
260 /* add 'call __bound_init()' in .init section */
261 Section *init_section = find_section(s1, ".init");
262 unsigned char *pinit;
263 #ifdef TCC_TARGET_PE
264 pinit = section_ptr_add(init_section, 3);
265 pinit[0] = 0x55; /* push %rbp */
266 pinit[1] = 0x89; /* mov %esp,%ebp */
267 pinit[2] = 0xe5;
268 #endif
269 pinit = section_ptr_add(init_section, 5);
270 pinit[0] = 0xe8;
271 write32le(pinit + 1, -4);
272 put_elf_reloc(symtab_section, init_section,
273 init_section->data_offset - 4, R_386_PC32, sym_index);
274 /* R_386_PC32 = R_X86_64_PC32 = 2 */
275 pinit = section_ptr_add(init_section, 6);
276 pinit[0] = 0xb8; /* mov xx,%eax */
277 write32le(pinit + 1, 0);
278 pinit[5] = 0x50; /* push %eax */
279 put_elf_reloc(symtab_section, init_section,
280 init_section->data_offset - 5, R_386_32, bsym_index);
281 sym_index = set_elf_sym(symtab_section, 0, 0,
282 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
283 SHN_UNDEF, "__bounds_add_static_var");
284 pinit = section_ptr_add(init_section, 5);
285 pinit[0] = 0xe8;
286 write32le(pinit + 1, -4);
287 put_elf_reloc(symtab_section, init_section,
288 init_section->data_offset - 4, R_386_PC32, sym_index);
289 /* R_386_PC32 = R_X86_64_PC32 = 2 */
290 pinit = section_ptr_add(init_section, 3);
291 pinit[0] = 0x83; /* add $0x4,%esp */
292 pinit[1] = 0xc4;
293 pinit[2] = 0x04;
294 #ifdef TCC_TARGET_PE
296 int init_index = set_elf_sym(symtab_section,
297 0, 0,
298 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
299 init_section->sh_num, "__init_start");
300 Sym sym;
301 init_section->sh_flags |= SHF_EXECINSTR;
302 pinit = section_ptr_add(init_section, 2);
303 pinit[0] = 0xc9; /* leave */
304 pinit[1] = 0xc3; /* ret */
305 sym.c = init_index;
306 add_init_array (s1, &sym);
308 #endif
311 #endif
313 #endif /* !TARGET_DEFS_ONLY */