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 */
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
)
48 tcc_error ("Unknown relocation type: %d", reloc_type
);
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
55 int gotplt_entry_type (int reloc_type
)
63 return NO_GOTPLT_ENTRY
;
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
;
73 return AUTO_GOTPLT_ENTRY
;
77 return BUILD_GOT_ONLY
;
82 return ALWAYS_GOTPLT_ENTRY
;
85 tcc_error ("Unknown relocation type: %d", reloc_type
);
89 ST_FUNC
unsigned create_plt_entry(TCCState
*s1
, unsigned got_offset
, struct sym_attr
*attr
)
91 Section
*plt
= s1
->plt
;
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
)
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 */
109 write32le(p
+ 2, PTR_SIZE
);
110 p
[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
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
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) */
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
));
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
)
143 p_end
= p
+ s1
->plt
->data_offset
;
146 add32le(p
+ 2, s1
->got
->sh_addr
);
147 add32le(p
+ 8, s1
->got
->sh_addr
);
150 add32le(p
+ 2, s1
->got
->sh_addr
);
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(TCCState
*s1
, ElfW_Rel
*rel
, int type
, unsigned char *ptr
, addr_t addr
, addr_t val
)
165 int sym_index
, esym_index
;
167 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
171 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
172 esym_index
= s1
->sym_attrs
[sym_index
].dyn_index
;
173 qrel
->r_offset
= rel
->r_offset
;
175 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_386_32
);
179 qrel
->r_info
= ELFW(R_INFO
)(0, R_386_RELATIVE
);
186 if (s1
->output_type
== TCC_OUTPUT_DLL
) {
188 esym_index
= s1
->sym_attrs
[sym_index
].dyn_index
;
190 qrel
->r_offset
= rel
->r_offset
;
191 qrel
->r_info
= ELFW(R_INFO
)(esym_index
, R_386_PC32
);
196 add32le(ptr
, val
- addr
);
199 add32le(ptr
, val
- addr
);
206 add32le(ptr
, s1
->got
->sh_addr
- addr
);
209 add32le(ptr
, val
- s1
->got
->sh_addr
);
213 /* we load the got offset */
214 add32le(ptr
, s1
->sym_attrs
[sym_index
].got_offset
);
217 if (s1
->output_format
!= TCC_OUTPUT_FORMAT_BINARY
) {
219 tcc_error("can only produce 16-bit binary files");
221 write16le(ptr
, read16le(ptr
) + val
);
224 if (s1
->output_format
!= TCC_OUTPUT_FORMAT_BINARY
)
226 write16le(ptr
, read16le(ptr
) + val
- addr
);
232 /* This relocation must copy initialized data from the library
233 to the program .bss segment. Currently made like for ARM
234 (to remove noise of default case). Is this true?
238 fprintf(stderr
,"FIXME: handle reloc type %d at %x [%p] to %x\n",
239 type
, (unsigned)addr
, ptr
, (unsigned)val
);
244 #endif /* !TARGET_DEFS_ONLY */