1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/moduleloader.h>
19#include <linux/elf.h>
20#include <linux/vmalloc.h>
21#include <linux/fs.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
25
26#if 0
27#define DEBUGP printk
28#else
29#define DEBUGP(fmt...)
30#endif
31
32
33
34struct got_entry {
35 struct got_entry *next;
36 Elf64_Sxword r_addend;
37 int got_offset;
38};
39
40static inline void
41process_reloc_for_got(Elf64_Rela *rela,
42 struct got_entry *chains, Elf64_Xword *poffset)
43{
44 unsigned long r_sym = ELF64_R_SYM (rela->r_info);
45 unsigned long r_type = ELF64_R_TYPE (rela->r_info);
46 Elf64_Sxword r_addend = rela->r_addend;
47 struct got_entry *g;
48
49 if (r_type != R_ALPHA_LITERAL)
50 return;
51
52 for (g = chains + r_sym; g ; g = g->next)
53 if (g->r_addend == r_addend) {
54 if (g->got_offset == 0) {
55 g->got_offset = *poffset;
56 *poffset += 8;
57 }
58 goto found_entry;
59 }
60
61 g = kmalloc (sizeof (*g), GFP_KERNEL);
62 g->next = chains[r_sym].next;
63 g->r_addend = r_addend;
64 g->got_offset = *poffset;
65 *poffset += 8;
66 chains[r_sym].next = g;
67
68 found_entry:
69
70
71
72 rela->r_info |= g->got_offset << 8;
73}
74
75int
76module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs,
77 char *secstrings, struct module *me)
78{
79 struct got_entry *chains;
80 Elf64_Rela *rela;
81 Elf64_Shdr *esechdrs, *symtab, *s, *got;
82 unsigned long nsyms, nrela, i;
83
84 esechdrs = sechdrs + hdr->e_shnum;
85 symtab = got = NULL;
86
87
88
89
90 for (s = sechdrs; s < esechdrs; ++s)
91 if (s->sh_type == SHT_SYMTAB)
92 symtab = s;
93 else if (!strcmp(".got", secstrings + s->sh_name)) {
94 got = s;
95 me->arch.gotsecindex = s - sechdrs;
96 }
97
98 if (!symtab) {
99 printk(KERN_ERR "module %s: no symbol table\n", me->name);
100 return -ENOEXEC;
101 }
102 if (!got) {
103 printk(KERN_ERR "module %s: no got section\n", me->name);
104 return -ENOEXEC;
105 }
106
107 nsyms = symtab->sh_size / sizeof(Elf64_Sym);
108 chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL);
109 if (!chains) {
110 printk(KERN_ERR
111 "module %s: no memory for symbol chain buffer\n",
112 me->name);
113 return -ENOMEM;
114 }
115
116 got->sh_size = 0;
117 got->sh_addralign = 8;
118 got->sh_type = SHT_NOBITS;
119
120
121
122 for (s = sechdrs; s < esechdrs; ++s)
123 if (s->sh_type == SHT_RELA) {
124 nrela = s->sh_size / sizeof(Elf64_Rela);
125 rela = (void *)hdr + s->sh_offset;
126 for (i = 0; i < nrela; ++i)
127 process_reloc_for_got(rela+i, chains,
128 &got->sh_size);
129 }
130
131
132 for (i = 0; i < nsyms; ++i) {
133 struct got_entry *g, *n;
134 for (g = chains[i].next; g ; g = n) {
135 n = g->next;
136 kfree(g);
137 }
138 }
139 kfree(chains);
140
141 return 0;
142}
143
144int
145apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab,
146 unsigned int symindex, unsigned int relsec,
147 struct module *me)
148{
149 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
150 unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela);
151 Elf64_Sym *symtab, *sym;
152 void *base, *location;
153 unsigned long got, gp;
154
155 DEBUGP("Applying relocate section %u to %u\n", relsec,
156 sechdrs[relsec].sh_info);
157
158 base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr;
159 symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr;
160
161
162
163 gp = (u64)me->module_core + me->core_size - 0x8000;
164 got = sechdrs[me->arch.gotsecindex].sh_addr;
165
166 for (i = 0; i < n; i++) {
167 unsigned long r_sym = ELF64_R_SYM (rela[i].r_info);
168 unsigned long r_type = ELF64_R_TYPE (rela[i].r_info);
169 unsigned long r_got_offset = r_type >> 8;
170 unsigned long value, hi, lo;
171 r_type &= 0xff;
172
173
174 location = base + rela[i].r_offset;
175
176
177
178 sym = symtab + r_sym;
179 value = sym->st_value + rela[i].r_addend;
180
181 switch (r_type) {
182 case R_ALPHA_NONE:
183 break;
184 case R_ALPHA_REFQUAD:
185
186 ((u32 *)location)[0] = value;
187 ((u32 *)location)[1] = value >> 32;
188 break;
189 case R_ALPHA_GPREL32:
190 value -= gp;
191 if ((int)value != value)
192 goto reloc_overflow;
193 *(u32 *)location = value;
194 break;
195 case R_ALPHA_LITERAL:
196 hi = got + r_got_offset;
197 lo = hi - gp;
198 if ((short)lo != lo)
199 goto reloc_overflow;
200 *(u16 *)location = lo;
201 *(u64 *)hi = value;
202 break;
203 case R_ALPHA_LITUSE:
204 break;
205 case R_ALPHA_GPDISP:
206 value = gp - (u64)location;
207 lo = (short)value;
208 hi = (int)(value - lo);
209 if (hi + lo != value)
210 goto reloc_overflow;
211 *(u16 *)location = hi >> 16;
212 *(u16 *)(location + rela[i].r_addend) = lo;
213 break;
214 case R_ALPHA_BRSGP:
215
216
217
218 if (sym->st_shndx == SHN_UNDEF)
219 goto reloc_overflow;
220 if ((sym->st_other & STO_ALPHA_STD_GPLOAD) ==
221 STO_ALPHA_STD_GPLOAD)
222
223 value += 8;
224
225 case R_ALPHA_BRADDR:
226 value -= (u64)location + 4;
227 if (value & 3)
228 goto reloc_overflow;
229 value = (long)value >> 2;
230 if (value + (1<<21) >= 1<<22)
231 goto reloc_overflow;
232 value &= 0x1fffff;
233 value |= *(u32 *)location & ~0x1fffff;
234 *(u32 *)location = value;
235 break;
236 case R_ALPHA_HINT:
237 break;
238 case R_ALPHA_SREL32:
239 value -= (u64)location;
240 if ((int)value != value)
241 goto reloc_overflow;
242 *(u32 *)location = value;
243 break;
244 case R_ALPHA_SREL64:
245 value -= (u64)location;
246 *(u64 *)location = value;
247 break;
248 case R_ALPHA_GPRELHIGH:
249 value = (long)(value - gp + 0x8000) >> 16;
250 if ((short) value != value)
251 goto reloc_overflow;
252 *(u16 *)location = value;
253 break;
254 case R_ALPHA_GPRELLOW:
255 value -= gp;
256 *(u16 *)location = value;
257 break;
258 case R_ALPHA_GPREL16:
259 value -= gp;
260 if ((short) value != value)
261 goto reloc_overflow;
262 *(u16 *)location = value;
263 break;
264 default:
265 printk(KERN_ERR "module %s: Unknown relocation: %lu\n",
266 me->name, r_type);
267 return -ENOEXEC;
268 reloc_overflow:
269 if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION)
270 printk(KERN_ERR
271 "module %s: Relocation (type %lu) overflow vs section %d\n",
272 me->name, r_type, sym->st_shndx);
273 else
274 printk(KERN_ERR
275 "module %s: Relocation (type %lu) overflow vs %s\n",
276 me->name, r_type, strtab + sym->st_name);
277 return -ENOEXEC;
278 }
279 }
280
281 return 0;
282}
283