Rebuild cross compilers when sources change
[tinycc.git] / i386-link.c
blobbb5029711e61bed827c7f5f89d994e59e0ecc03c
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
12 #define R_NUM R_386_NUM
14 #define ELF_START_ADDR 0x08048000
15 #define ELF_PAGE_SIZE 0x1000
17 #define PCRELATIVE_DLLPLT 0
18 #define RELOCATE_DLLPLT 0
20 #else /* !TARGET_DEFS_ONLY */
22 #include "tcc.h"
24 /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
25 relocations, returns -1. */
26 int code_reloc (int reloc_type)
28 switch (reloc_type) {
29 case R_386_RELATIVE:
30 case R_386_16:
31 case R_386_32:
32 case R_386_GOTPC:
33 case R_386_GOTOFF:
34 case R_386_GOT32:
35 case R_386_GOT32X:
36 case R_386_GLOB_DAT:
37 case R_386_COPY:
38 return 0;
40 case R_386_PC16:
41 case R_386_PC32:
42 case R_386_PLT32:
43 case R_386_JMP_SLOT:
44 return 1;
47 tcc_error ("Unknown relocation type: %d", reloc_type);
48 return -1;
51 /* Returns an enumerator to describe wether 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;
84 tcc_error ("Unknown relocation type: %d", reloc_type);
85 return -1;
88 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
90 Section *plt = s1->plt;
91 uint8_t *p;
92 int modrm;
93 unsigned plt_offset, relofs;
95 /* on i386 if we build a DLL, we add a %ebx offset */
96 if (s1->output_type == TCC_OUTPUT_DLL)
97 modrm = 0xa3;
98 else
99 modrm = 0x25;
101 /* empty PLT: create PLT0 entry that pushes the library indentifier
102 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
103 (GOT + 2 * PTR_SIZE) */
104 if (plt->data_offset == 0) {
105 p = section_ptr_add(plt, 16);
106 p[0] = 0xff; /* pushl got + PTR_SIZE */
107 p[1] = modrm + 0x10;
108 write32le(p + 2, PTR_SIZE);
109 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
110 p[7] = modrm;
111 write32le(p + 8, PTR_SIZE * 2);
113 plt_offset = plt->data_offset;
115 /* The PLT slot refers to the relocation entry it needs via offset.
116 The reloc entry is created below, so its offset is the current
117 data_offset */
118 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
120 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
121 p = section_ptr_add(plt, 16);
122 p[0] = 0xff; /* jmp *(got + x) */
123 p[1] = modrm;
124 write32le(p + 2, got_offset);
125 p[6] = 0x68; /* push $xxx */
126 write32le(p + 7, relofs);
127 p[11] = 0xe9; /* jmp plt_start */
128 write32le(p + 12, -(plt->data_offset));
129 return plt_offset;
132 /* relocate the PLT: compute addresses and offsets in the PLT now that final
133 address for PLT and GOT are known (see fill_program_header) */
134 ST_FUNC void relocate_plt(TCCState *s1)
136 uint8_t *p, *p_end;
138 if (!s1->plt)
139 return;
141 p = s1->plt->data;
142 p_end = p + s1->plt->data_offset;
144 if (p < p_end) {
145 add32le(p + 2, s1->got->sh_addr);
146 add32le(p + 8, s1->got->sh_addr);
147 p += 16;
148 while (p < p_end) {
149 add32le(p + 2, s1->got->sh_addr);
150 p += 16;
155 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
157 void relocate_init(Section *sr)
159 qrel = (ElfW_Rel *) sr->data;
162 void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
164 int sym_index, esym_index;
166 sym_index = ELFW(R_SYM)(rel->r_info);
168 switch (type) {
169 case R_386_32:
170 if (s1->output_type == TCC_OUTPUT_DLL) {
171 esym_index = s1->sym_attrs[sym_index].dyn_index;
172 qrel->r_offset = rel->r_offset;
173 if (esym_index) {
174 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
175 qrel++;
176 return;
177 } else {
178 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
179 qrel++;
182 add32le(ptr, val);
183 return;
184 case R_386_PC32:
185 if (s1->output_type == TCC_OUTPUT_DLL) {
186 /* DLL relocation */
187 esym_index = s1->sym_attrs[sym_index].dyn_index;
188 if (esym_index) {
189 qrel->r_offset = rel->r_offset;
190 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
191 qrel++;
192 return;
195 add32le(ptr, val - addr);
196 return;
197 case R_386_PLT32:
198 add32le(ptr, val - addr);
199 return;
200 case R_386_GLOB_DAT:
201 case R_386_JMP_SLOT:
202 write32le(ptr, val);
203 return;
204 case R_386_GOTPC:
205 add32le(ptr, s1->got->sh_addr - addr);
206 return;
207 case R_386_GOTOFF:
208 add32le(ptr, val - s1->got->sh_addr);
209 return;
210 case R_386_GOT32:
211 case R_386_GOT32X:
212 /* we load the got offset */
213 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
214 return;
215 case R_386_16:
216 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
217 output_file:
218 tcc_error("can only produce 16-bit binary files");
220 write16le(ptr, read16le(ptr) + val);
221 return;
222 case R_386_PC16:
223 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
224 goto output_file;
225 write16le(ptr, read16le(ptr) + val - addr);
226 return;
227 case R_386_RELATIVE:
228 /* do nothing */
229 return;
230 case R_386_COPY:
231 /* This reloction must copy initialized data from the library
232 to the program .bss segment. Currently made like for ARM
233 (to remove noise of defaukt case). Is this true?
235 return;
236 default:
237 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
238 type, (unsigned)addr, ptr, (unsigned)val);
239 return;
243 #endif /* !TARGET_DEFS_ONLY */