1 /* Kernel module help for Alpha. 2 Copyright (C) 2002 Richard Henderson. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 2 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program; if not, write to the Free Software 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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 void * 33 module_alloc(unsigned long size) 34 { 35 if (size == 0) 36 return NULL; 37 return vmalloc(size); 38 } 39 40 void 41 module_free(struct module *mod, void *module_region) 42 { 43 vfree(module_region); 44 } 45 46 /* Allocate the GOT at the end of the core sections. */ 47 48 struct got_entry { 49 struct got_entry *next; 50 Elf64_Sxword r_addend; 51 int got_offset; 52 }; 53 54 static inline void 55 process_reloc_for_got(Elf64_Rela *rela, 56 struct got_entry *chains, Elf64_Xword *poffset) 57 { 58 unsigned long r_sym = ELF64_R_SYM (rela->r_info); 59 unsigned long r_type = ELF64_R_TYPE (rela->r_info); 60 Elf64_Sxword r_addend = rela->r_addend; 61 struct got_entry *g; 62 63 if (r_type != R_ALPHA_LITERAL) 64 return; 65 66 for (g = chains + r_sym; g ; g = g->next) 67 if (g->r_addend == r_addend) { 68 if (g->got_offset == 0) { 69 g->got_offset = *poffset; 70 *poffset += 8; 71 } 72 goto found_entry; 73 } 74 75 g = kmalloc (sizeof (*g), GFP_KERNEL); 76 g->next = chains[r_sym].next; 77 g->r_addend = r_addend; 78 g->got_offset = *poffset; 79 *poffset += 8; 80 chains[r_sym].next = g; 81 82 found_entry: 83 /* Trick: most of the ELF64_R_TYPE field is unused. There are 84 42 valid relocation types, and a 32-bit field. Co-opt the 85 bits above 256 to store the got offset for this reloc. */ 86 rela->r_info |= g->got_offset << 8; 87 } 88 89 int 90 module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs, 91 char *secstrings, struct module *me) 92 { 93 struct got_entry *chains; 94 Elf64_Rela *rela; 95 Elf64_Shdr *esechdrs, *symtab, *s, *got; 96 unsigned long nsyms, nrela, i; 97 98 esechdrs = sechdrs + hdr->e_shnum; 99 symtab = got = NULL; 100 101 /* Find out how large the symbol table is. Allocate one got_entry 102 head per symbol. Normally this will be enough, but not always. 103 We'll chain different offsets for the symbol down each head. */ 104 for (s = sechdrs; s < esechdrs; ++s) 105 if (s->sh_type == SHT_SYMTAB) 106 symtab = s; 107 else if (!strcmp(".got", secstrings + s->sh_name)) { 108 got = s; 109 me->arch.gotsecindex = s - sechdrs; 110 } 111 112 if (!symtab) { 113 printk(KERN_ERR "module %s: no symbol table\n", me->name); 114 return -ENOEXEC; 115 } 116 if (!got) { 117 printk(KERN_ERR "module %s: no got section\n", me->name); 118 return -ENOEXEC; 119 } 120 121 nsyms = symtab->sh_size / sizeof(Elf64_Sym); 122 chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL); 123 124 got->sh_size = 0; 125 got->sh_addralign = 8; 126 got->sh_type = SHT_NOBITS; 127 128 /* Examine all LITERAL relocations to find out what GOT entries 129 are required. This sizes the GOT section as well. */ 130 for (s = sechdrs; s < esechdrs; ++s) 131 if (s->sh_type == SHT_RELA) { 132 nrela = s->sh_size / sizeof(Elf64_Rela); 133 rela = (void *)hdr + s->sh_offset; 134 for (i = 0; i < nrela; ++i) 135 process_reloc_for_got(rela+i, chains, 136 &got->sh_size); 137 } 138 139 /* Free the memory we allocated. */ 140 for (i = 0; i < nsyms; ++i) { 141 struct got_entry *g, *n; 142 for (g = chains[i].next; g ; g = n) { 143 n = g->next; 144 kfree(g); 145 } 146 } 147 kfree(chains); 148 149 return 0; 150 } 151 152 int 153 apply_relocate(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex, 154 unsigned int relsec, struct module *me) 155 { 156 printk(KERN_ERR "module %s: REL relocation unsupported\n", me->name); 157 return -ENOEXEC; 158 } 159 160 int 161 apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, 162 unsigned int symindex, unsigned int relsec, 163 struct module *me) 164 { 165 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 166 unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela); 167 Elf64_Sym *symtab, *sym; 168 void *base, *location; 169 unsigned long got, gp; 170 171 DEBUGP("Applying relocate section %u to %u\n", relsec, 172 sechdrs[relsec].sh_info); 173 174 base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr; 175 symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr; 176 177 /* The small sections were sorted to the end of the segment. 178 The following should definitely cover them. */ 179 gp = (u64)me->module_core + me->core_size - 0x8000; 180 got = sechdrs[me->arch.gotsecindex].sh_addr; 181 182 for (i = 0; i < n; i++) { 183 unsigned long r_sym = ELF64_R_SYM (rela[i].r_info); 184 unsigned long r_type = ELF64_R_TYPE (rela[i].r_info); 185 unsigned long r_got_offset = r_type >> 8; 186 unsigned long value, hi, lo; 187 r_type &= 0xff; 188 189 /* This is where to make the change. */ 190 location = base + rela[i].r_offset; 191 192 /* This is the symbol it is referring to. Note that all 193 unresolved symbols have been resolved. */ 194 sym = symtab + r_sym; 195 value = sym->st_value + rela[i].r_addend; 196 197 switch (r_type) { 198 case R_ALPHA_NONE: 199 break; 200 case R_ALPHA_REFQUAD: 201 /* BUG() can produce misaligned relocations. */ 202 ((u32 *)location)[0] = value; 203 ((u32 *)location)[1] = value >> 32; 204 break; 205 case R_ALPHA_GPREL32: 206 value -= gp; 207 if ((int)value != value) 208 goto reloc_overflow; 209 *(u32 *)location = value; 210 break; 211 case R_ALPHA_LITERAL: 212 hi = got + r_got_offset; 213 lo = hi - gp; 214 if ((short)lo != lo) 215 goto reloc_overflow; 216 *(u16 *)location = lo; 217 *(u64 *)hi = value; 218 break; 219 case R_ALPHA_LITUSE: 220 break; 221 case R_ALPHA_GPDISP: 222 value = gp - (u64)location; 223 lo = (short)value; 224 hi = (int)(value - lo); 225 if (hi + lo != value) 226 goto reloc_overflow; 227 *(u16 *)location = hi >> 16; 228 *(u16 *)(location + rela[i].r_addend) = lo; 229 break; 230 case R_ALPHA_BRSGP: 231 /* BRSGP is only allowed to bind to local symbols. 232 If the section is undef, this means that the 233 value was resolved from somewhere else. */ 234 if (sym->st_shndx == SHN_UNDEF) 235 goto reloc_overflow; 236 if ((sym->st_other & STO_ALPHA_STD_GPLOAD) == 237 STO_ALPHA_STD_GPLOAD) 238 /* Omit the prologue. */ 239 value += 8; 240 /* FALLTHRU */ 241 case R_ALPHA_BRADDR: 242 value -= (u64)location + 4; 243 if (value & 3) 244 goto reloc_overflow; 245 value = (long)value >> 2; 246 if (value + (1<<21) >= 1<<22) 247 goto reloc_overflow; 248 value &= 0x1fffff; 249 value |= *(u32 *)location & ~0x1fffff; 250 *(u32 *)location = value; 251 break; 252 case R_ALPHA_HINT: 253 break; 254 case R_ALPHA_SREL32: 255 value -= (u64)location; 256 if ((int)value != value) 257 goto reloc_overflow; 258 *(u32 *)location = value; 259 break; 260 case R_ALPHA_SREL64: 261 value -= (u64)location; 262 *(u64 *)location = value; 263 break; 264 case R_ALPHA_GPRELHIGH: 265 value = (long)(value - gp + 0x8000) >> 16; 266 if ((short) value != value) 267 goto reloc_overflow; 268 *(u16 *)location = value; 269 break; 270 case R_ALPHA_GPRELLOW: 271 value -= gp; 272 *(u16 *)location = value; 273 break; 274 case R_ALPHA_GPREL16: 275 value -= gp; 276 if ((short) value != value) 277 goto reloc_overflow; 278 *(u16 *)location = value; 279 break; 280 default: 281 printk(KERN_ERR "module %s: Unknown relocation: %lu\n", 282 me->name, r_type); 283 return -ENOEXEC; 284 reloc_overflow: 285 if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION) 286 printk(KERN_ERR 287 "module %s: Relocation (type %lu) overflow vs section %d\n", 288 me->name, r_type, sym->st_shndx); 289 else 290 printk(KERN_ERR 291 "module %s: Relocation (type %lu) overflow vs %s\n", 292 me->name, r_type, strtab + sym->st_name); 293 return -ENOEXEC; 294 } 295 } 296 297 return 0; 298 } 299 300 int 301 module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, 302 struct module *me) 303 { 304 return 0; 305 } 306 307 void 308 module_arch_cleanup(struct module *mod) 309 { 310 } 311