riscv: Start fixing float struct passing/returnig
[tinycc.git] / i386-link.c
blob8eddaa4616fac01f53d8563ba05959ca6913d61b
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 /* 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_386_RELATIVE:
31 case R_386_16:
32 case R_386_32:
33 case R_386_GOTPC:
34 case R_386_GOTOFF:
35 case R_386_GOT32:
36 case R_386_GOT32X:
37 case R_386_GLOB_DAT:
38 case R_386_COPY:
39 return 0;
41 case R_386_PC16:
42 case R_386_PC32:
43 case R_386_PLT32:
44 case R_386_JMP_SLOT:
45 return 1;
48 tcc_error ("Unknown relocation type: %d", reloc_type);
49 return -1;
52 /* Returns an enumerator to describe whether 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_386_RELATIVE:
59 case R_386_16:
60 case R_386_GLOB_DAT:
61 case R_386_JMP_SLOT:
62 case R_386_COPY:
63 return NO_GOTPLT_ENTRY;
65 case R_386_32:
66 /* This relocations shouldn't normally need GOT or PLT
67 slots if it weren't for simplicity in the code generator.
68 See our caller for comments. */
69 return AUTO_GOTPLT_ENTRY;
71 case R_386_PC16:
72 case R_386_PC32:
73 return AUTO_GOTPLT_ENTRY;
75 case R_386_GOTPC:
76 case R_386_GOTOFF:
77 return BUILD_GOT_ONLY;
79 case R_386_GOT32:
80 case R_386_GOT32X:
81 case R_386_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 /* on i386 if we build a DLL, we add a %ebx offset */
97 if (s1->output_type == TCC_OUTPUT_DLL)
98 modrm = 0xa3;
99 else
100 modrm = 0x25;
102 /* empty PLT: create PLT0 entry that pushes the library identifier
103 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
104 (GOT + 2 * PTR_SIZE) */
105 if (plt->data_offset == 0) {
106 p = section_ptr_add(plt, 16);
107 p[0] = 0xff; /* pushl got + PTR_SIZE */
108 p[1] = modrm + 0x10;
109 write32le(p + 2, PTR_SIZE);
110 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
111 p[7] = modrm;
112 write32le(p + 8, PTR_SIZE * 2);
114 plt_offset = plt->data_offset;
116 /* The PLT slot refers to the relocation entry it needs via offset.
117 The reloc entry is created below, so its offset is the current
118 data_offset */
119 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
121 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
122 p = section_ptr_add(plt, 16);
123 p[0] = 0xff; /* jmp *(got + x) */
124 p[1] = modrm;
125 write32le(p + 2, got_offset);
126 p[6] = 0x68; /* push $xxx */
127 write32le(p + 7, relofs);
128 p[11] = 0xe9; /* jmp plt_start */
129 write32le(p + 12, -(plt->data_offset));
130 return plt_offset;
133 /* relocate the PLT: compute addresses and offsets in the PLT now that final
134 address for PLT and GOT are known (see fill_program_header) */
135 ST_FUNC void relocate_plt(TCCState *s1)
137 uint8_t *p, *p_end;
139 if (!s1->plt)
140 return;
142 p = s1->plt->data;
143 p_end = p + s1->plt->data_offset;
145 if (p < p_end) {
146 add32le(p + 2, s1->got->sh_addr);
147 add32le(p + 8, s1->got->sh_addr);
148 p += 16;
149 while (p < p_end) {
150 add32le(p + 2, s1->got->sh_addr);
151 p += 16;
156 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
158 void relocate_init(Section *sr)
160 qrel = (ElfW_Rel *) sr->data;
163 void relocate_fini(Section *sr)
167 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
169 int sym_index, esym_index;
171 sym_index = ELFW(R_SYM)(rel->r_info);
173 switch (type) {
174 case R_386_32:
175 if (s1->output_type == TCC_OUTPUT_DLL) {
176 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
177 qrel->r_offset = rel->r_offset;
178 if (esym_index) {
179 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
180 qrel++;
181 return;
182 } else {
183 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
184 qrel++;
187 add32le(ptr, val);
188 return;
189 case R_386_PC32:
190 if (s1->output_type == TCC_OUTPUT_DLL) {
191 /* DLL relocation */
192 esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
193 if (esym_index) {
194 qrel->r_offset = rel->r_offset;
195 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
196 qrel++;
197 return;
200 add32le(ptr, val - addr);
201 return;
202 case R_386_PLT32:
203 add32le(ptr, val - addr);
204 return;
205 case R_386_GLOB_DAT:
206 case R_386_JMP_SLOT:
207 write32le(ptr, val);
208 return;
209 case R_386_GOTPC:
210 add32le(ptr, s1->got->sh_addr - addr);
211 return;
212 case R_386_GOTOFF:
213 add32le(ptr, val - s1->got->sh_addr);
214 return;
215 case R_386_GOT32:
216 case R_386_GOT32X:
217 /* we load the got offset */
218 add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
219 return;
220 case R_386_16:
221 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
222 output_file:
223 tcc_error("can only produce 16-bit binary files");
225 write16le(ptr, read16le(ptr) + val);
226 return;
227 case R_386_PC16:
228 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
229 goto output_file;
230 write16le(ptr, read16le(ptr) + val - addr);
231 return;
232 case R_386_RELATIVE:
233 #ifdef TCC_TARGET_PE
234 add32le(ptr, val - s1->pe_imagebase);
235 #endif
236 /* do nothing */
237 return;
238 case R_386_COPY:
239 /* This relocation must copy initialized data from the library
240 to the program .bss segment. Currently made like for ARM
241 (to remove noise of default case). Is this true?
243 return;
244 default:
245 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
246 type, (unsigned)addr, ptr, (unsigned)val);
247 return;
251 #endif /* !TARGET_DEFS_ONLY */