struct-init: Correctly parse unnamed member initializers
[tinycc.git] / i386-link.c
blobc0981727a1c519b706ac0e20d4eac75f5bff6319
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_32:
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_PC16:
66 case R_386_PC32:
67 return AUTO_GOTPLT_ENTRY;
69 case R_386_GOTPC:
70 case R_386_GOTOFF:
71 return BUILD_GOT_ONLY;
73 case R_386_GOT32:
74 case R_386_GOT32X:
75 case R_386_PLT32:
76 return ALWAYS_GOTPLT_ENTRY;
79 tcc_error ("Unknown relocation type: %d", reloc_type);
80 return -1;
83 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
85 Section *plt = s1->plt;
86 uint8_t *p;
87 int modrm;
88 unsigned plt_offset, relofs;
90 /* on i386 if we build a DLL, we add a %ebx offset */
91 if (s1->output_type == TCC_OUTPUT_DLL)
92 modrm = 0xa3;
93 else
94 modrm = 0x25;
96 /* empty PLT: create PLT0 entry that pushes the library indentifier
97 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
98 (GOT + 2 * PTR_SIZE) */
99 if (plt->data_offset == 0) {
100 p = section_ptr_add(plt, 16);
101 p[0] = 0xff; /* pushl got + PTR_SIZE */
102 p[1] = modrm + 0x10;
103 write32le(p + 2, PTR_SIZE);
104 p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
105 p[7] = modrm;
106 write32le(p + 8, PTR_SIZE * 2);
108 plt_offset = plt->data_offset;
110 /* The PLT slot refers to the relocation entry it needs via offset.
111 The reloc entry is created below, so its offset is the current
112 data_offset */
113 relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
115 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
116 p = section_ptr_add(plt, 16);
117 p[0] = 0xff; /* jmp *(got + x) */
118 p[1] = modrm;
119 write32le(p + 2, got_offset);
120 p[6] = 0x68; /* push $xxx */
121 write32le(p + 7, relofs);
122 p[11] = 0xe9; /* jmp plt_start */
123 write32le(p + 12, -(plt->data_offset));
124 return plt_offset;
127 /* relocate the PLT: compute addresses and offsets in the PLT now that final
128 address for PLT and GOT are known (see fill_program_header) */
129 ST_FUNC void relocate_plt(TCCState *s1)
131 uint8_t *p, *p_end;
133 if (!s1->plt)
134 return;
136 p = s1->plt->data;
137 p_end = p + s1->plt->data_offset;
139 if (p < p_end) {
140 add32le(p + 2, s1->got->sh_addr);
141 add32le(p + 8, s1->got->sh_addr);
142 p += 16;
143 while (p < p_end) {
144 add32le(p + 2, s1->got->sh_addr);
145 p += 16;
150 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
152 void relocate_init(Section *sr)
154 qrel = (ElfW_Rel *) sr->data;
157 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
159 int sym_index, esym_index;
161 sym_index = ELFW(R_SYM)(rel->r_info);
163 switch (type) {
164 case R_386_32:
165 if (s1->output_type == TCC_OUTPUT_DLL) {
166 esym_index = s1->sym_attrs[sym_index].dyn_index;
167 qrel->r_offset = rel->r_offset;
168 if (esym_index) {
169 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
170 qrel++;
171 return;
172 } else {
173 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
174 qrel++;
177 add32le(ptr, val);
178 return;
179 case R_386_PC32:
180 if (s1->output_type == TCC_OUTPUT_DLL) {
181 /* DLL relocation */
182 esym_index = s1->sym_attrs[sym_index].dyn_index;
183 if (esym_index) {
184 qrel->r_offset = rel->r_offset;
185 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
186 qrel++;
187 return;
190 add32le(ptr, val - addr);
191 return;
192 case R_386_PLT32:
193 add32le(ptr, val - addr);
194 return;
195 case R_386_GLOB_DAT:
196 case R_386_JMP_SLOT:
197 write32le(ptr, val);
198 return;
199 case R_386_GOTPC:
200 add32le(ptr, s1->got->sh_addr - addr);
201 return;
202 case R_386_GOTOFF:
203 add32le(ptr, val - s1->got->sh_addr);
204 return;
205 case R_386_GOT32:
206 case R_386_GOT32X:
207 /* we load the got offset */
208 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
209 return;
210 case R_386_16:
211 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
212 output_file:
213 tcc_error("can only produce 16-bit binary files");
215 write16le(ptr, read16le(ptr) + val);
216 return;
217 case R_386_PC16:
218 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
219 goto output_file;
220 write16le(ptr, read16le(ptr) + val - addr);
221 return;
222 case R_386_RELATIVE:
223 /* do nothing */
224 return;
225 case R_386_COPY:
226 /* This reloction must copy initialized data from the library
227 to the program .bss segment. Currently made like for ARM
228 (to remove noise of defaukt 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 #endif /* !TARGET_DEFS_ONLY */