Fix PLT creation for i386
[tinycc/jakubkaszycki.git] / i386-link.c
bloba7ef73f21954894e737836fd5a13960ff979f876
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 HAVE_SECTION_RELOC
18 #define PCRELATIVE_DLLPLT 0
20 #else /* !TARGET_DEFS_ONLY */
22 #include "tcc.h"
24 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
25 ST_DATA struct reloc_info relocs_info[R_NUM] = {
26 INIT_RELOC_INFO (R_386_32, 0, NO_GOTPLT_ENTRY, 0)
27 INIT_RELOC_INFO (R_386_PC32, 1, AUTO_GOTPLT_ENTRY, 0)
28 INIT_RELOC_INFO (R_386_PLT32, 1, ALWAYS_GOTPLT_ENTRY, 0)
29 INIT_RELOC_INFO (R_386_GLOB_DAT, 0, NO_GOTPLT_ENTRY, 0)
30 INIT_RELOC_INFO (R_386_JMP_SLOT, 1, NO_GOTPLT_ENTRY, 0)
31 INIT_RELOC_INFO (R_386_GOTPC, 0, BUILD_GOT_ONLY, 0)
32 INIT_RELOC_INFO (R_386_GOTOFF, 0, BUILD_GOT_ONLY, 0)
33 INIT_RELOC_INFO (R_386_GOT32, 0, ALWAYS_GOTPLT_ENTRY, 0)
34 INIT_RELOC_INFO (R_386_GOT32X, 0, ALWAYS_GOTPLT_ENTRY, 0)
35 INIT_RELOC_INFO (R_386_16, 0, NO_GOTPLT_ENTRY, 0)
36 INIT_RELOC_INFO (R_386_PC16, 1, AUTO_GOTPLT_ENTRY, 0)
39 void relocate_init(Section *sr)
41 qrel = (ElfW_Rel *) sr->data;
44 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
46 int sym_index, esym_index;
48 sym_index = ELFW(R_SYM)(rel->r_info);
50 switch (type) {
51 case R_386_32:
52 if (s1->output_type == TCC_OUTPUT_DLL) {
53 esym_index = s1->symtab_to_dynsym[sym_index];
54 qrel->r_offset = rel->r_offset;
55 if (esym_index) {
56 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
57 qrel++;
58 return;
59 } else {
60 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
61 qrel++;
64 add32le(ptr, val);
65 return;
66 case R_386_PC32:
67 if (s1->output_type == TCC_OUTPUT_DLL) {
68 /* DLL relocation */
69 esym_index = s1->symtab_to_dynsym[sym_index];
70 if (esym_index) {
71 qrel->r_offset = rel->r_offset;
72 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
73 qrel++;
74 return;
77 add32le(ptr, val - addr);
78 return;
79 case R_386_PLT32:
80 add32le(ptr, val - addr);
81 return;
82 case R_386_GLOB_DAT:
83 case R_386_JMP_SLOT:
84 write32le(ptr, val);
85 return;
86 case R_386_GOTPC:
87 add32le(ptr, s1->got->sh_addr - addr);
88 return;
89 case R_386_GOTOFF:
90 add32le(ptr, val - s1->got->sh_addr);
91 return;
92 case R_386_GOT32:
93 case R_386_GOT32X:
94 /* we load the got offset */
95 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
96 return;
97 case R_386_16:
98 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
99 output_file:
100 tcc_error("can only produce 16-bit binary files");
102 write16le(ptr, read16le(ptr) + val);
103 return;
104 case R_386_PC16:
105 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
106 goto output_file;
107 write16le(ptr, read16le(ptr) + val - addr);
108 return;
109 case R_386_RELATIVE:
110 /* do nothing */
111 return;
112 case R_386_COPY:
113 /* This reloction must copy initialized data from the library
114 to the program .bss segment. Currently made like for ARM
115 (to remove noise of defaukt case). Is this true?
117 return;
118 default:
119 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
120 type, (unsigned)addr, ptr, (unsigned)val);
121 return;
125 #endif /* !TARGET_DEFS_ONLY */