Use functions to get relocation info
[tinycc/jakubkaszycki.git] / i386-link.c
blob5a0511c6bbbdc951945203ebeb0f47c2c2acd1da
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 /* 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_16:
30 case R_386_32:
31 case R_386_GOTPC:
32 case R_386_GOTOFF:
33 case R_386_GOT32:
34 case R_386_GOT32X:
35 case R_386_GLOB_DAT:
36 case R_386_COPY:
37 return 0;
39 case R_386_PC16:
40 case R_386_PC32:
41 case R_386_PLT32:
42 case R_386_JMP_SLOT:
43 return 1;
46 tcc_error ("Unknown relocation type: %d", reloc_type);
47 return -1;
50 /* Returns an enumerator to describe wether and when the relocation needs a
51 GOT and/or PLT entry to be created. See tcc.h for a description of the
52 different values. */
53 int gotplt_entry_type (int reloc_type)
55 switch (reloc_type) {
56 case R_386_16:
57 case R_386_32:
58 case R_386_GLOB_DAT:
59 case R_386_JMP_SLOT:
60 case R_386_COPY:
61 return NO_GOTPLT_ENTRY;
63 case R_386_PC16:
64 case R_386_PC32:
65 return AUTO_GOTPLT_ENTRY;
67 case R_386_GOTPC:
68 case R_386_GOTOFF:
69 return BUILD_GOT_ONLY;
71 case R_386_GOT32:
72 case R_386_GOT32X:
73 case R_386_PLT32:
74 return ALWAYS_GOTPLT_ENTRY;
77 tcc_error ("Unknown relocation type: %d", reloc_type);
78 return -1;
81 static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
83 void relocate_init(Section *sr)
85 qrel = (ElfW_Rel *) sr->data;
88 void relocate(TCCState *s1, ElfW_Rel *rel, int type, char *ptr, addr_t addr, addr_t val)
90 int sym_index, esym_index;
92 sym_index = ELFW(R_SYM)(rel->r_info);
94 switch (type) {
95 case R_386_32:
96 if (s1->output_type == TCC_OUTPUT_DLL) {
97 esym_index = s1->symtab_to_dynsym[sym_index];
98 qrel->r_offset = rel->r_offset;
99 if (esym_index) {
100 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
101 qrel++;
102 return;
103 } else {
104 qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
105 qrel++;
108 add32le(ptr, val);
109 return;
110 case R_386_PC32:
111 if (s1->output_type == TCC_OUTPUT_DLL) {
112 /* DLL relocation */
113 esym_index = s1->symtab_to_dynsym[sym_index];
114 if (esym_index) {
115 qrel->r_offset = rel->r_offset;
116 qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
117 qrel++;
118 return;
121 add32le(ptr, val - addr);
122 return;
123 case R_386_PLT32:
124 add32le(ptr, val - addr);
125 return;
126 case R_386_GLOB_DAT:
127 case R_386_JMP_SLOT:
128 write32le(ptr, val);
129 return;
130 case R_386_GOTPC:
131 add32le(ptr, s1->got->sh_addr - addr);
132 return;
133 case R_386_GOTOFF:
134 add32le(ptr, val - s1->got->sh_addr);
135 return;
136 case R_386_GOT32:
137 case R_386_GOT32X:
138 /* we load the got offset */
139 add32le(ptr, s1->sym_attrs[sym_index].got_offset);
140 return;
141 case R_386_16:
142 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
143 output_file:
144 tcc_error("can only produce 16-bit binary files");
146 write16le(ptr, read16le(ptr) + val);
147 return;
148 case R_386_PC16:
149 if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY)
150 goto output_file;
151 write16le(ptr, read16le(ptr) + val - addr);
152 return;
153 case R_386_RELATIVE:
154 /* do nothing */
155 return;
156 case R_386_COPY:
157 /* This reloction must copy initialized data from the library
158 to the program .bss segment. Currently made like for ARM
159 (to remove noise of defaukt case). Is this true?
161 return;
162 default:
163 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
164 type, (unsigned)addr, ptr, (unsigned)val);
165 return;
169 #endif /* !TARGET_DEFS_ONLY */