1/* 2 * AVR32-specific kernel module loader 3 * 4 * Copyright (C) 2005-2006 Atmel Corporation 5 * 6 * GOT initialization parts are based on the s390 version 7 * Copyright (C) 2002, 2003 IBM Deutschland Entwicklung GmbH, 8 * IBM Corporation 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 */ 14 15#include <linux/bug.h> 16#include <linux/elf.h> 17#include <linux/kernel.h> 18#include <linux/module.h> 19#include <linux/moduleloader.h> 20#include <linux/vmalloc.h> 21 22void module_arch_freeing_init(struct module *mod) 23{ 24 vfree(mod->arch.syminfo); 25 mod->arch.syminfo = NULL; 26} 27 28static inline int check_rela(Elf32_Rela *rela, struct module *module, 29 char *strings, Elf32_Sym *symbols) 30{ 31 struct mod_arch_syminfo *info; 32 33 info = module->arch.syminfo + ELF32_R_SYM(rela->r_info); 34 switch (ELF32_R_TYPE(rela->r_info)) { 35 case R_AVR32_GOT32: 36 case R_AVR32_GOT16: 37 case R_AVR32_GOT8: 38 case R_AVR32_GOT21S: 39 case R_AVR32_GOT18SW: /* mcall */ 40 case R_AVR32_GOT16S: /* ld.w */ 41 if (rela->r_addend != 0) { 42 printk(KERN_ERR 43 "GOT relocation against %s at offset %u with addend\n", 44 strings + symbols[ELF32_R_SYM(rela->r_info)].st_name, 45 rela->r_offset); 46 return -ENOEXEC; 47 } 48 if (info->got_offset == -1UL) { 49 info->got_offset = module->arch.got_size; 50 module->arch.got_size += sizeof(void *); 51 } 52 pr_debug("GOT[%3lu] %s\n", info->got_offset, 53 strings + symbols[ELF32_R_SYM(rela->r_info)].st_name); 54 break; 55 } 56 57 return 0; 58} 59 60int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 61 char *secstrings, struct module *module) 62{ 63 Elf32_Shdr *symtab; 64 Elf32_Sym *symbols; 65 Elf32_Rela *rela; 66 char *strings; 67 int nrela, i, j; 68 int ret; 69 70 /* Find the symbol table */ 71 symtab = NULL; 72 for (i = 0; i < hdr->e_shnum; i++) 73 switch (sechdrs[i].sh_type) { 74 case SHT_SYMTAB: 75 symtab = &sechdrs[i]; 76 break; 77 } 78 if (!symtab) { 79 printk(KERN_ERR "module %s: no symbol table\n", module->name); 80 return -ENOEXEC; 81 } 82 83 /* Allocate room for one syminfo structure per symbol. */ 84 module->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym); 85 module->arch.syminfo = vmalloc(module->arch.nsyms 86 * sizeof(struct mod_arch_syminfo)); 87 if (!module->arch.syminfo) 88 return -ENOMEM; 89 90 symbols = (void *)hdr + symtab->sh_offset; 91 strings = (void *)hdr + sechdrs[symtab->sh_link].sh_offset; 92 for (i = 0; i < module->arch.nsyms; i++) { 93 if (symbols[i].st_shndx == SHN_UNDEF && 94 strcmp(strings + symbols[i].st_name, 95 "_GLOBAL_OFFSET_TABLE_") == 0) 96 /* "Define" it as absolute. */ 97 symbols[i].st_shndx = SHN_ABS; 98 module->arch.syminfo[i].got_offset = -1UL; 99 module->arch.syminfo[i].got_initialized = 0; 100 } 101 102 /* Allocate GOT entries for symbols that need it. */ 103 module->arch.got_size = 0; 104 for (i = 0; i < hdr->e_shnum; i++) { 105 if (sechdrs[i].sh_type != SHT_RELA) 106 continue; 107 nrela = sechdrs[i].sh_size / sizeof(Elf32_Rela); 108 rela = (void *)hdr + sechdrs[i].sh_offset; 109 for (j = 0; j < nrela; j++) { 110 ret = check_rela(rela + j, module, 111 strings, symbols); 112 if (ret) 113 goto out_free_syminfo; 114 } 115 } 116 117 /* 118 * Increase core size to make room for GOT and set start 119 * offset for GOT. 120 */ 121 module->core_size = ALIGN(module->core_size, 4); 122 module->arch.got_offset = module->core_size; 123 module->core_size += module->arch.got_size; 124 125 return 0; 126 127out_free_syminfo: 128 vfree(module->arch.syminfo); 129 module->arch.syminfo = NULL; 130 131 return ret; 132} 133 134static inline int reloc_overflow(struct module *module, const char *reloc_name, 135 Elf32_Addr relocation) 136{ 137 printk(KERN_ERR "module %s: Value %lx does not fit relocation %s\n", 138 module->name, (unsigned long)relocation, reloc_name); 139 return -ENOEXEC; 140} 141 142#define get_u16(loc) (*((uint16_t *)loc)) 143#define put_u16(loc, val) (*((uint16_t *)loc) = (val)) 144 145int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab, 146 unsigned int symindex, unsigned int relindex, 147 struct module *module) 148{ 149 Elf32_Shdr *symsec = sechdrs + symindex; 150 Elf32_Shdr *relsec = sechdrs + relindex; 151 Elf32_Shdr *dstsec = sechdrs + relsec->sh_info; 152 Elf32_Rela *rel = (void *)relsec->sh_addr; 153 unsigned int i; 154 int ret = 0; 155 156 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rela); i++, rel++) { 157 struct mod_arch_syminfo *info; 158 Elf32_Sym *sym; 159 Elf32_Addr relocation; 160 uint32_t *location; 161 uint32_t value; 162 163 location = (void *)dstsec->sh_addr + rel->r_offset; 164 sym = (Elf32_Sym *)symsec->sh_addr + ELF32_R_SYM(rel->r_info); 165 relocation = sym->st_value + rel->r_addend; 166 167 info = module->arch.syminfo + ELF32_R_SYM(rel->r_info); 168 169 /* Initialize GOT entry if necessary */ 170 switch (ELF32_R_TYPE(rel->r_info)) { 171 case R_AVR32_GOT32: 172 case R_AVR32_GOT16: 173 case R_AVR32_GOT8: 174 case R_AVR32_GOT21S: 175 case R_AVR32_GOT18SW: 176 case R_AVR32_GOT16S: 177 if (!info->got_initialized) { 178 Elf32_Addr *gotent; 179 180 gotent = (module->module_core 181 + module->arch.got_offset 182 + info->got_offset); 183 *gotent = relocation; 184 info->got_initialized = 1; 185 } 186 187 relocation = info->got_offset; 188 break; 189 } 190 191 switch (ELF32_R_TYPE(rel->r_info)) { 192 case R_AVR32_32: 193 case R_AVR32_32_CPENT: 194 *location = relocation; 195 break; 196 case R_AVR32_22H_PCREL: 197 relocation -= (Elf32_Addr)location; 198 if ((relocation & 0xffe00001) != 0 199 && (relocation & 0xffc00001) != 0xffc00000) 200 return reloc_overflow(module, 201 "R_AVR32_22H_PCREL", 202 relocation); 203 relocation >>= 1; 204 205 value = *location; 206 value = ((value & 0xe1ef0000) 207 | (relocation & 0xffff) 208 | ((relocation & 0x10000) << 4) 209 | ((relocation & 0x1e0000) << 8)); 210 *location = value; 211 break; 212 case R_AVR32_11H_PCREL: 213 relocation -= (Elf32_Addr)location; 214 if ((relocation & 0xfffffc01) != 0 215 && (relocation & 0xfffff801) != 0xfffff800) 216 return reloc_overflow(module, 217 "R_AVR32_11H_PCREL", 218 relocation); 219 value = get_u16(location); 220 value = ((value & 0xf00c) 221 | ((relocation & 0x1fe) << 3) 222 | ((relocation & 0x600) >> 9)); 223 put_u16(location, value); 224 break; 225 case R_AVR32_9H_PCREL: 226 relocation -= (Elf32_Addr)location; 227 if ((relocation & 0xffffff01) != 0 228 && (relocation & 0xfffffe01) != 0xfffffe00) 229 return reloc_overflow(module, 230 "R_AVR32_9H_PCREL", 231 relocation); 232 value = get_u16(location); 233 value = ((value & 0xf00f) 234 | ((relocation & 0x1fe) << 3)); 235 put_u16(location, value); 236 break; 237 case R_AVR32_9UW_PCREL: 238 relocation -= ((Elf32_Addr)location) & 0xfffffffc; 239 if ((relocation & 0xfffffc03) != 0) 240 return reloc_overflow(module, 241 "R_AVR32_9UW_PCREL", 242 relocation); 243 value = get_u16(location); 244 value = ((value & 0xf80f) 245 | ((relocation & 0x1fc) << 2)); 246 put_u16(location, value); 247 break; 248 case R_AVR32_GOTPC: 249 /* 250 * R6 = PC - (PC - GOT) 251 * 252 * At this point, relocation contains the 253 * value of PC. Just subtract the value of 254 * GOT, and we're done. 255 */ 256 pr_debug("GOTPC: PC=0x%x, got_offset=0x%lx, core=0x%p\n", 257 relocation, module->arch.got_offset, 258 module->module_core); 259 relocation -= ((unsigned long)module->module_core 260 + module->arch.got_offset); 261 *location = relocation; 262 break; 263 case R_AVR32_GOT18SW: 264 if ((relocation & 0xfffe0003) != 0 265 && (relocation & 0xfffc0000) != 0xfffc0000) 266 return reloc_overflow(module, "R_AVR32_GOT18SW", 267 relocation); 268 relocation >>= 2; 269 /* fall through */ 270 case R_AVR32_GOT16S: 271 if ((relocation & 0xffff8000) != 0 272 && (relocation & 0xffff0000) != 0xffff0000) 273 return reloc_overflow(module, "R_AVR32_GOT16S", 274 relocation); 275 pr_debug("GOT reloc @ 0x%x -> %u\n", 276 rel->r_offset, relocation); 277 value = *location; 278 value = ((value & 0xffff0000) 279 | (relocation & 0xffff)); 280 *location = value; 281 break; 282 283 default: 284 printk(KERN_ERR "module %s: Unknown relocation: %u\n", 285 module->name, ELF32_R_TYPE(rel->r_info)); 286 return -ENOEXEC; 287 } 288 } 289 290 return ret; 291} 292