1/* 2 * linux/arch/unicore32/kernel/module.c 3 * 4 * Code specific to PKUnity SoC and UniCore ISA 5 * 6 * Copyright (C) 2001-2010 GUAN Xue-tao 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12#include <linux/module.h> 13#include <linux/moduleloader.h> 14#include <linux/kernel.h> 15#include <linux/mm.h> 16#include <linux/elf.h> 17#include <linux/vmalloc.h> 18#include <linux/fs.h> 19#include <linux/string.h> 20#include <linux/gfp.h> 21 22#include <asm/pgtable.h> 23#include <asm/sections.h> 24 25void *module_alloc(unsigned long size) 26{ 27 struct vm_struct *area; 28 29 size = PAGE_ALIGN(size); 30 if (!size) 31 return NULL; 32 33 area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END); 34 if (!area) 35 return NULL; 36 37 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC); 38} 39 40int 41apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, 42 unsigned int relindex, struct module *module) 43{ 44 Elf32_Shdr *symsec = sechdrs + symindex; 45 Elf32_Shdr *relsec = sechdrs + relindex; 46 Elf32_Shdr *dstsec = sechdrs + relsec->sh_info; 47 Elf32_Rel *rel = (void *)relsec->sh_addr; 48 unsigned int i; 49 50 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) { 51 unsigned long loc; 52 Elf32_Sym *sym; 53 s32 offset; 54 55 offset = ELF32_R_SYM(rel->r_info); 56 if (offset < 0 || offset > 57 (symsec->sh_size / sizeof(Elf32_Sym))) { 58 printk(KERN_ERR "%s: bad relocation, " 59 "section %d reloc %d\n", 60 module->name, relindex, i); 61 return -ENOEXEC; 62 } 63 64 sym = ((Elf32_Sym *)symsec->sh_addr) + offset; 65 66 if (rel->r_offset < 0 || rel->r_offset > 67 dstsec->sh_size - sizeof(u32)) { 68 printk(KERN_ERR "%s: out of bounds relocation, " 69 "section %d reloc %d offset %d size %d\n", 70 module->name, relindex, i, rel->r_offset, 71 dstsec->sh_size); 72 return -ENOEXEC; 73 } 74 75 loc = dstsec->sh_addr + rel->r_offset; 76 77 switch (ELF32_R_TYPE(rel->r_info)) { 78 case R_UNICORE_NONE: 79 /* ignore */ 80 break; 81 82 case R_UNICORE_ABS32: 83 *(u32 *)loc += sym->st_value; 84 break; 85 86 case R_UNICORE_PC24: 87 case R_UNICORE_CALL: 88 case R_UNICORE_JUMP24: 89 offset = (*(u32 *)loc & 0x00ffffff) << 2; 90 if (offset & 0x02000000) 91 offset -= 0x04000000; 92 93 offset += sym->st_value - loc; 94 if (offset & 3 || 95 offset <= (s32)0xfe000000 || 96 offset >= (s32)0x02000000) { 97 printk(KERN_ERR 98 "%s: relocation out of range, section " 99 "%d reloc %d sym '%s'\n", module->name, 100 relindex, i, strtab + sym->st_name); 101 return -ENOEXEC; 102 } 103 104 offset >>= 2; 105 106 *(u32 *)loc &= 0xff000000; 107 *(u32 *)loc |= offset & 0x00ffffff; 108 break; 109 110 default: 111 printk(KERN_ERR "%s: unknown relocation: %u\n", 112 module->name, ELF32_R_TYPE(rel->r_info)); 113 return -ENOEXEC; 114 } 115 } 116 return 0; 117} 118