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