1/* Kernel module help for PPC. 2 Copyright (C) 2001 Rusty Russell. 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/module.h> 19#include <linux/moduleloader.h> 20#include <linux/elf.h> 21#include <linux/vmalloc.h> 22#include <linux/fs.h> 23#include <linux/string.h> 24#include <linux/kernel.h> 25#include <linux/cache.h> 26#include <linux/bug.h> 27 28#include "setup.h" 29 30#define DEBUGP(fmt , ...) 31 32LIST_HEAD(module_bug_list); 33 34void *module_alloc(unsigned long size) 35{ 36 if (size == 0) 37 return NULL; 38 return vmalloc(size); 39} 40 41/* Free memory returned from module_alloc */ 42void module_free(struct module *mod, void *module_region) 43{ 44 vfree(module_region); 45} 46 47/* Count how many different relocations (different symbol, different 48 addend) */ 49static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num) 50{ 51 unsigned int i, j, ret = 0; 52 53 /* Sure, this is order(n^2), but it's usually short, and not 54 time critical */ 55 for (i = 0; i < num; i++) { 56 for (j = 0; j < i; j++) { 57 /* If this addend appeared before, it's 58 already been counted */ 59 if (ELF32_R_SYM(rela[i].r_info) 60 == ELF32_R_SYM(rela[j].r_info) 61 && rela[i].r_addend == rela[j].r_addend) 62 break; 63 } 64 if (j == i) ret++; 65 } 66 return ret; 67} 68 69/* Get the potential trampolines size required of the init and 70 non-init sections */ 71static unsigned long get_plt_size(const Elf32_Ehdr *hdr, 72 const Elf32_Shdr *sechdrs, 73 const char *secstrings, 74 int is_init) 75{ 76 unsigned long ret = 0; 77 unsigned i; 78 79 /* Everything marked ALLOC (this includes the exported 80 symbols) */ 81 for (i = 1; i < hdr->e_shnum; i++) { 82 /* If it's called *.init*, and we're not init, we're 83 not interested */ 84 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0) 85 != is_init) 86 continue; 87 88 /* We don't want to look at debug sections. */ 89 if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0) 90 continue; 91 92 if (sechdrs[i].sh_type == SHT_RELA) { 93 DEBUGP("Found relocations in section %u\n", i); 94 DEBUGP("Ptr: %p. Number: %u\n", 95 (void *)hdr + sechdrs[i].sh_offset, 96 sechdrs[i].sh_size / sizeof(Elf32_Rela)); 97 ret += count_relocs((void *)hdr 98 + sechdrs[i].sh_offset, 99 sechdrs[i].sh_size 100 / sizeof(Elf32_Rela)) 101 * sizeof(struct ppc_plt_entry); 102 } 103 } 104 105 return ret; 106} 107 108int module_frob_arch_sections(Elf32_Ehdr *hdr, 109 Elf32_Shdr *sechdrs, 110 char *secstrings, 111 struct module *me) 112{ 113 unsigned int i; 114 115 /* Find .plt and .init.plt sections */ 116 for (i = 0; i < hdr->e_shnum; i++) { 117 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0) 118 me->arch.init_plt_section = i; 119 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0) 120 me->arch.core_plt_section = i; 121 } 122 if (!me->arch.core_plt_section || !me->arch.init_plt_section) { 123 printk("Module doesn't contain .plt or .init.plt sections.\n"); 124 return -ENOEXEC; 125 } 126 127 /* Override their sizes */ 128 sechdrs[me->arch.core_plt_section].sh_size 129 = get_plt_size(hdr, sechdrs, secstrings, 0); 130 sechdrs[me->arch.init_plt_section].sh_size 131 = get_plt_size(hdr, sechdrs, secstrings, 1); 132 return 0; 133} 134 135int apply_relocate(Elf32_Shdr *sechdrs, 136 const char *strtab, 137 unsigned int symindex, 138 unsigned int relsec, 139 struct module *module) 140{ 141 printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n", 142 module->name); 143 return -ENOEXEC; 144} 145 146static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val) 147{ 148 if (entry->jump[0] == 0x3d600000 + ((val + 0x8000) >> 16) 149 && entry->jump[1] == 0x396b0000 + (val & 0xffff)) 150 return 1; 151 return 0; 152} 153 154/* Set up a trampoline in the PLT to bounce us to the distant function */ 155static uint32_t do_plt_call(void *location, 156 Elf32_Addr val, 157 Elf32_Shdr *sechdrs, 158 struct module *mod) 159{ 160 struct ppc_plt_entry *entry; 161 162 DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location); 163 /* Init, or core PLT? */ 164 if (location >= mod->module_core 165 && location < mod->module_core + mod->core_size) 166 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr; 167 else 168 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr; 169 170 /* Find this entry, or if that fails, the next avail. entry */ 171 while (entry->jump[0]) { 172 if (entry_matches(entry, val)) return (uint32_t)entry; 173 entry++; 174 } 175 176 /* Stolen from Paul Mackerras as well... */ 177 entry->jump[0] = 0x3d600000+((val+0x8000)>>16); /* lis r11,sym@ha */ 178 entry->jump[1] = 0x396b0000 + (val&0xffff); /* addi r11,r11,sym@l*/ 179 entry->jump[2] = 0x7d6903a6; /* mtctr r11 */ 180 entry->jump[3] = 0x4e800420; /* bctr */ 181 182 DEBUGP("Initialized plt for 0x%x at %p\n", val, entry); 183 return (uint32_t)entry; 184} 185 186int apply_relocate_add(Elf32_Shdr *sechdrs, 187 const char *strtab, 188 unsigned int symindex, 189 unsigned int relsec, 190 struct module *module) 191{ 192 unsigned int i; 193 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr; 194 Elf32_Sym *sym; 195 uint32_t *location; 196 uint32_t value; 197 198 DEBUGP("Applying ADD relocate section %u to %u\n", relsec, 199 sechdrs[relsec].sh_info); 200 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { 201 /* This is where to make the change */ 202 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 203 + rela[i].r_offset; 204 /* This is the symbol it is referring to. Note that all 205 undefined symbols have been resolved. */ 206 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr 207 + ELF32_R_SYM(rela[i].r_info); 208 /* `Everything is relative'. */ 209 value = sym->st_value + rela[i].r_addend; 210 211 switch (ELF32_R_TYPE(rela[i].r_info)) { 212 case R_PPC_ADDR32: 213 /* Simply set it */ 214 *(uint32_t *)location = value; 215 break; 216 217 case R_PPC_ADDR16_LO: 218 /* Low half of the symbol */ 219 *(uint16_t *)location = value; 220 break; 221 222 case R_PPC_ADDR16_HI: 223 /* Higher half of the symbol */ 224 *(uint16_t *)location = (value >> 16); 225 break; 226 227 case R_PPC_ADDR16_HA: 228 /* Sign-adjusted lower 16 bits: PPC ELF ABI says: 229 (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF. 230 This is the same, only sane. 231 */ 232 *(uint16_t *)location = (value + 0x8000) >> 16; 233 break; 234 235 case R_PPC_REL24: 236 if ((int)(value - (uint32_t)location) < -0x02000000 237 || (int)(value - (uint32_t)location) >= 0x02000000) 238 value = do_plt_call(location, value, 239 sechdrs, module); 240 241 /* Only replace bits 2 through 26 */ 242 DEBUGP("REL24 value = %08X. location = %08X\n", 243 value, (uint32_t)location); 244 DEBUGP("Location before: %08X.\n", 245 *(uint32_t *)location); 246 *(uint32_t *)location 247 = (*(uint32_t *)location & ~0x03fffffc) 248 | ((value - (uint32_t)location) 249 & 0x03fffffc); 250 DEBUGP("Location after: %08X.\n", 251 *(uint32_t *)location); 252 DEBUGP("ie. jump to %08X+%08X = %08X\n", 253 *(uint32_t *)location & 0x03fffffc, 254 (uint32_t)location, 255 (*(uint32_t *)location & 0x03fffffc) 256 + (uint32_t)location); 257 break; 258 259 case R_PPC_REL32: 260 /* 32-bit relative jump. */ 261 *(uint32_t *)location = value - (uint32_t)location; 262 break; 263 264 default: 265 printk("%s: unknown ADD relocation: %u\n", 266 module->name, 267 ELF32_R_TYPE(rela[i].r_info)); 268 return -ENOEXEC; 269 } 270 } 271 return 0; 272} 273 274static const Elf_Shdr *find_section(const Elf_Ehdr *hdr, 275 const Elf_Shdr *sechdrs, 276 const char *name) 277{ 278 char *secstrings; 279 unsigned int i; 280 281 secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 282 for (i = 1; i < hdr->e_shnum; i++) 283 if (strcmp(secstrings+sechdrs[i].sh_name, name) == 0) 284 return &sechdrs[i]; 285 return NULL; 286} 287 288int module_finalize(const Elf_Ehdr *hdr, 289 const Elf_Shdr *sechdrs, 290 struct module *me) 291{ 292 const Elf_Shdr *sect; 293 int err; 294 295 err = module_bug_finalize(hdr, sechdrs, me); 296 if (err) /* never true, currently */ 297 return err; 298 299 /* Apply feature fixups */ 300 sect = find_section(hdr, sechdrs, "__ftr_fixup"); 301 if (sect != NULL) 302 do_feature_fixups(cur_cpu_spec->cpu_features, 303 (void *)sect->sh_addr, 304 (void *)sect->sh_addr + sect->sh_size); 305 306 return 0; 307} 308 309void module_arch_cleanup(struct module *mod) 310{ 311 module_bug_cleanup(mod); 312} 313 314struct bug_entry *module_find_bug(unsigned long bugaddr) 315{ 316 struct mod_arch_specific *mod; 317 unsigned int i; 318 struct bug_entry *bug; 319 320 list_for_each_entry(mod, &module_bug_list, bug_list) { 321 bug = mod->bug_table; 322 for (i = 0; i < mod->num_bugs; ++i, ++bug) 323 if (bugaddr == bug->bug_addr) 324 return bug; 325 } 326 return NULL; 327} 328