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 */
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
)
42 case R_386_TLS_LDO_32
:
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
58 int gotplt_entry_type (int reloc_type
)
66 return NO_GOTPLT_ENTRY
;
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
;
76 return AUTO_GOTPLT_ENTRY
;
80 return BUILD_GOT_ONLY
;
87 case R_386_TLS_LDO_32
:
89 return ALWAYS_GOTPLT_ENTRY
;
94 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
96 Section
*plt
= s1
->plt
;
99 unsigned plt_offset
, relofs
;
101 /* on i386 if we build a DLL, we add a %ebx offset */
102 if (s1
->output_type
== TCC_OUTPUT_DLL
)
107 /* empty PLT: create PLT0 entry that pushes the library identifier
108 (GOT + PTR_SIZE) and jumps to ld.so resolution routine
109 (GOT + 2 * PTR_SIZE) */
110 if (plt
->data_offset
== 0) {
111 p
= section_ptr_add(plt
, 16);
112 p
[0] = 0xff; /* pushl got + PTR_SIZE */
114 write32le(p
+ 2, PTR_SIZE
);
115 p
[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
117 write32le(p
+ 8, PTR_SIZE
* 2);
119 plt_offset
= plt
->data_offset
;
121 /* The PLT slot refers to the relocation entry it needs via offset.
122 The reloc entry is created below, so its offset is the current
124 relofs
= s1
->plt
->reloc
? s1
->plt
->reloc
->data_offset
: 0;
126 /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
127 p
= section_ptr_add(plt
, 16);
128 p
[0] = 0xff; /* jmp *(got + x) */
130 write32le(p
+ 2, got_offset
);
131 p
[6] = 0x68; /* push $xxx */
132 write32le(p
+ 7, relofs
- sizeof (ElfW_Rel
));
133 p
[11] = 0xe9; /* jmp plt_start */
134 write32le(p
+ 12, -(plt
->data_offset
));
138 /* relocate the PLT: compute addresses and offsets in the PLT now that final
139 address for PLT and GOT are known (see fill_program_header) */
140 ST_FUNC
void relocate_plt(TCCState
*s1
)
148 p_end
= p
+ s1
->plt
->data_offset
;
150 if (s1
->output_type
!= TCC_OUTPUT_DLL
&& p
< p_end
) {
151 add32le(p
+ 2, s1
->got
->sh_addr
);
152 add32le(p
+ 8, s1
->got
->sh_addr
);
155 add32le(p
+ 2, s1
->got
->sh_addr
);
160 if (s1
->plt
->reloc
) {
162 int x
= s1
->plt
->sh_addr
+ 16 + 6;
164 for_each_elem(s1
->plt
->reloc
, 0, rel
, ElfW_Rel
) {
165 write32le(p
+ rel
->r_offset
, x
);
172 void relocate(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
174 int sym_index
, esym_index
;
176 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
180 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
181 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
182 qrel
->r_offset
= rel
->r_offset
;
184 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_386_32
);
188 qrel
->r_info
= ELFW(R_INFO
)(0, R_386_RELATIVE
);
195 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
197 esym_index
= get_sym_attr(s1
, sym_index
, 0)->dyn_index
;
199 qrel
->r_offset
= rel
->r_offset
;
200 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_386_PC32
);
205 add32le(ptr
, val
- addr
);
208 add32le(ptr
, val
- addr
);
215 add32le(ptr
, s1
->got
->sh_addr
- addr
);
218 add32le(ptr
, val
- s1
->got
->sh_addr
);
222 /* we load the got offset */
223 add32le(ptr
, get_sym_attr(s1
, sym_index
, 0)->got_offset
);
226 if (s1
->output_format
!= TCC_OUTPUT_FORMAT_BINARY
) {
228 tcc_error("can only produce 16-bit binary files");
230 write16le(ptr
, read16le(ptr
) + val
);
233 if (s1
->output_format
!= TCC_OUTPUT_FORMAT_BINARY
)
235 write16le(ptr
, read16le(ptr
) + val
- addr
);
239 add32le(ptr
, val
- s1
->pe_imagebase
);
244 /* This relocation must copy initialized data from the library
245 to the program .bss segment. Currently made like for ARM
246 (to remove noise of default case). Is this true?
251 static const unsigned char expect
[] = {
252 /* lea 0(,%ebx,1),%eax */
253 0x8d, 0x04, 0x1d, 0x00, 0x00, 0x00, 0x00,
254 /* call __tls_get_addr@PLT */
255 0xe8, 0xfc, 0xff, 0xff, 0xff };
256 static const unsigned char replace
[] = {
258 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
260 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 };
262 if (memcmp (ptr
-3, expect
, sizeof(expect
)) == 0) {
267 memcpy(ptr
-3, replace
, sizeof(replace
));
268 rel
[1].r_info
= ELFW(R_INFO
)(0, R_386_NONE
);
269 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
270 sec
= s1
->sections
[sym
->st_shndx
];
271 x
= sym
->st_value
- sec
->sh_addr
- sec
->data_offset
;
272 add32le(ptr
+ 5, -x
);
275 tcc_error("unexpected R_386_TLS_GD pattern");
280 static const unsigned char expect
[] = {
281 /* lea 0(%ebx),%eax */
282 0x8d, 0x83, 0x00, 0x00, 0x00, 0x00,
283 /* call __tls_get_addr@PLT */
284 0xe8, 0xfc, 0xff, 0xff, 0xff };
285 static const unsigned char replace
[] = {
287 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
290 /* lea 0(%esi,%eiz,1),%esi */
291 0x8d, 0x74, 0x26, 0x00 };
293 if (memcmp (ptr
-2, expect
, sizeof(expect
)) == 0) {
294 memcpy(ptr
-2, replace
, sizeof(replace
));
295 rel
[1].r_info
= ELFW(R_INFO
)(0, R_386_NONE
);
298 tcc_error("unexpected R_386_TLS_LDM pattern");
301 case R_386_TLS_LDO_32
:
308 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
309 sec
= s1
->sections
[sym
->st_shndx
];
310 x
= val
- sec
->sh_addr
- sec
->data_offset
;
317 fprintf(stderr
,"FIXME: handle reloc type %d at %x [%p] to %x\n",
318 type
, (unsigned)addr
, ptr
, (unsigned)val
);
323 #endif /* !TARGET_DEFS_ONLY */