198fb1036SLinas Vepstas /* 298fb1036SLinas Vepstas * Kernel module loader for Hexagon 398fb1036SLinas Vepstas * 4*e1858b2aSRichard Kuo * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved. 598fb1036SLinas Vepstas * 698fb1036SLinas Vepstas * This program is free software; you can redistribute it and/or modify 798fb1036SLinas Vepstas * it under the terms of the GNU General Public License version 2 and 898fb1036SLinas Vepstas * only version 2 as published by the Free Software Foundation. 998fb1036SLinas Vepstas * 1098fb1036SLinas Vepstas * This program is distributed in the hope that it will be useful, 1198fb1036SLinas Vepstas * but WITHOUT ANY WARRANTY; without even the implied warranty of 1298fb1036SLinas Vepstas * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1398fb1036SLinas Vepstas * GNU General Public License for more details. 1498fb1036SLinas Vepstas * 1598fb1036SLinas Vepstas * You should have received a copy of the GNU General Public License 1698fb1036SLinas Vepstas * along with this program; if not, write to the Free Software 1798fb1036SLinas Vepstas * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 1898fb1036SLinas Vepstas * 02110-1301, USA. 1998fb1036SLinas Vepstas */ 2098fb1036SLinas Vepstas 2198fb1036SLinas Vepstas #include <asm/module.h> 2298fb1036SLinas Vepstas #include <linux/elf.h> 2398fb1036SLinas Vepstas #include <linux/module.h> 2498fb1036SLinas Vepstas #include <linux/moduleloader.h> 2598fb1036SLinas Vepstas #include <linux/vmalloc.h> 2698fb1036SLinas Vepstas 2798fb1036SLinas Vepstas #if 0 2898fb1036SLinas Vepstas #define DEBUGP printk 2998fb1036SLinas Vepstas #else 3098fb1036SLinas Vepstas #define DEBUGP(fmt , ...) 3198fb1036SLinas Vepstas #endif 3298fb1036SLinas Vepstas 3398fb1036SLinas Vepstas /* 3498fb1036SLinas Vepstas * module_frob_arch_sections - tweak got/plt sections. 3598fb1036SLinas Vepstas * @hdr - pointer to elf header 3698fb1036SLinas Vepstas * @sechdrs - pointer to elf load section headers 3798fb1036SLinas Vepstas * @secstrings - symbol names 3898fb1036SLinas Vepstas * @mod - pointer to module 3998fb1036SLinas Vepstas */ 4098fb1036SLinas Vepstas int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 4198fb1036SLinas Vepstas char *secstrings, 4298fb1036SLinas Vepstas struct module *mod) 4398fb1036SLinas Vepstas { 4498fb1036SLinas Vepstas unsigned int i; 4598fb1036SLinas Vepstas int found = 0; 4698fb1036SLinas Vepstas 4798fb1036SLinas Vepstas /* Look for .plt and/or .got.plt and/or .init.plt sections */ 4898fb1036SLinas Vepstas for (i = 0; i < hdr->e_shnum; i++) { 4998fb1036SLinas Vepstas DEBUGP("Section %d is %s\n", i, 5098fb1036SLinas Vepstas secstrings + sechdrs[i].sh_name); 5198fb1036SLinas Vepstas if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0) 5298fb1036SLinas Vepstas found = i+1; 5398fb1036SLinas Vepstas if (strcmp(secstrings + sechdrs[i].sh_name, ".got.plt") == 0) 5498fb1036SLinas Vepstas found = i+1; 5598fb1036SLinas Vepstas if (strcmp(secstrings + sechdrs[i].sh_name, ".rela.plt") == 0) 5698fb1036SLinas Vepstas found = i+1; 5798fb1036SLinas Vepstas } 5898fb1036SLinas Vepstas 5998fb1036SLinas Vepstas /* At this time, we don't support modules comiled with -shared */ 6098fb1036SLinas Vepstas if (found) { 6198fb1036SLinas Vepstas printk(KERN_WARNING 6298fb1036SLinas Vepstas "Module '%s' contains unexpected .plt/.got sections.\n", 6398fb1036SLinas Vepstas mod->name); 6498fb1036SLinas Vepstas /* return -ENOEXEC; */ 6598fb1036SLinas Vepstas } 6698fb1036SLinas Vepstas 6798fb1036SLinas Vepstas return 0; 6898fb1036SLinas Vepstas } 6998fb1036SLinas Vepstas 7098fb1036SLinas Vepstas /* 7198fb1036SLinas Vepstas * apply_relocate_add - perform rela relocations. 7298fb1036SLinas Vepstas * @sechdrs - pointer to section headers 7398fb1036SLinas Vepstas * @strtab - some sort of start address? 7498fb1036SLinas Vepstas * @symindex - symbol index offset or something? 7598fb1036SLinas Vepstas * @relsec - address to relocate to? 7698fb1036SLinas Vepstas * @module - pointer to module 7798fb1036SLinas Vepstas * 7898fb1036SLinas Vepstas * Perform rela relocations. 7998fb1036SLinas Vepstas */ 8098fb1036SLinas Vepstas int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 8198fb1036SLinas Vepstas unsigned int symindex, unsigned int relsec, 8298fb1036SLinas Vepstas struct module *module) 8398fb1036SLinas Vepstas { 8498fb1036SLinas Vepstas unsigned int i; 8598fb1036SLinas Vepstas Elf32_Sym *sym; 8698fb1036SLinas Vepstas uint32_t *location; 8798fb1036SLinas Vepstas uint32_t value; 8898fb1036SLinas Vepstas unsigned int nrelocs = sechdrs[relsec].sh_size / sizeof(Elf32_Rela); 8998fb1036SLinas Vepstas Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr; 9098fb1036SLinas Vepstas Elf32_Word sym_info = sechdrs[relsec].sh_info; 9198fb1036SLinas Vepstas Elf32_Sym *sym_base = (Elf32_Sym *) sechdrs[symindex].sh_addr; 9298fb1036SLinas Vepstas void *loc_base = (void *) sechdrs[sym_info].sh_addr; 9398fb1036SLinas Vepstas 9498fb1036SLinas Vepstas DEBUGP("Applying relocations in section %u to section %u base=%p\n", 9598fb1036SLinas Vepstas relsec, sym_info, loc_base); 9698fb1036SLinas Vepstas 9798fb1036SLinas Vepstas for (i = 0; i < nrelocs; i++) { 9898fb1036SLinas Vepstas 9998fb1036SLinas Vepstas /* Symbol to relocate */ 10098fb1036SLinas Vepstas sym = sym_base + ELF32_R_SYM(rela[i].r_info); 10198fb1036SLinas Vepstas 10298fb1036SLinas Vepstas /* Where to make the change */ 10398fb1036SLinas Vepstas location = loc_base + rela[i].r_offset; 10498fb1036SLinas Vepstas 10598fb1036SLinas Vepstas /* `Everything is relative'. */ 10698fb1036SLinas Vepstas value = sym->st_value + rela[i].r_addend; 10798fb1036SLinas Vepstas 10898fb1036SLinas Vepstas DEBUGP("%d: value=%08x loc=%p reloc=%d symbol=%s\n", 10998fb1036SLinas Vepstas i, value, location, ELF32_R_TYPE(rela[i].r_info), 11098fb1036SLinas Vepstas sym->st_name ? 11198fb1036SLinas Vepstas &strtab[sym->st_name] : "(anonymous)"); 11298fb1036SLinas Vepstas 11398fb1036SLinas Vepstas switch (ELF32_R_TYPE(rela[i].r_info)) { 11498fb1036SLinas Vepstas case R_HEXAGON_B22_PCREL: { 11598fb1036SLinas Vepstas int dist = (int)(value - (uint32_t)location); 11698fb1036SLinas Vepstas if ((dist < -0x00800000) || 11798fb1036SLinas Vepstas (dist >= 0x00800000)) { 11898fb1036SLinas Vepstas printk(KERN_ERR 11998fb1036SLinas Vepstas "%s: %s: %08x=%08x-%08x %s\n", 12098fb1036SLinas Vepstas module->name, 12198fb1036SLinas Vepstas "R_HEXAGON_B22_PCREL reloc out of range", 12298fb1036SLinas Vepstas dist, value, (uint32_t)location, 12398fb1036SLinas Vepstas sym->st_name ? 12498fb1036SLinas Vepstas &strtab[sym->st_name] : "(anonymous)"); 12598fb1036SLinas Vepstas return -ENOEXEC; 12698fb1036SLinas Vepstas } 12798fb1036SLinas Vepstas DEBUGP("B22_PCREL contents: %08X.\n", *location); 12898fb1036SLinas Vepstas *location &= ~0x01ff3fff; 12998fb1036SLinas Vepstas *location |= 0x00003fff & dist; 13098fb1036SLinas Vepstas *location |= 0x01ff0000 & (dist<<2); 13198fb1036SLinas Vepstas DEBUGP("Contents after reloc: %08x\n", *location); 13298fb1036SLinas Vepstas break; 13398fb1036SLinas Vepstas } 13498fb1036SLinas Vepstas case R_HEXAGON_HI16: 13598fb1036SLinas Vepstas value = (value>>16) & 0xffff; 13698fb1036SLinas Vepstas /* fallthrough */ 13798fb1036SLinas Vepstas case R_HEXAGON_LO16: 13898fb1036SLinas Vepstas *location &= ~0x00c03fff; 13998fb1036SLinas Vepstas *location |= value & 0x3fff; 14098fb1036SLinas Vepstas *location |= (value & 0xc000) << 8; 14198fb1036SLinas Vepstas break; 14298fb1036SLinas Vepstas case R_HEXAGON_32: 14398fb1036SLinas Vepstas *location = value; 14498fb1036SLinas Vepstas break; 14598fb1036SLinas Vepstas case R_HEXAGON_32_PCREL: 14698fb1036SLinas Vepstas *location = value - (uint32_t)location; 14798fb1036SLinas Vepstas break; 14898fb1036SLinas Vepstas case R_HEXAGON_PLT_B22_PCREL: 14998fb1036SLinas Vepstas case R_HEXAGON_GOTOFF_LO16: 15098fb1036SLinas Vepstas case R_HEXAGON_GOTOFF_HI16: 15198fb1036SLinas Vepstas printk(KERN_ERR "%s: GOT/PLT relocations unsupported\n", 15298fb1036SLinas Vepstas module->name); 15398fb1036SLinas Vepstas return -ENOEXEC; 15498fb1036SLinas Vepstas default: 15598fb1036SLinas Vepstas printk(KERN_ERR "%s: unknown relocation: %u\n", 15698fb1036SLinas Vepstas module->name, 15798fb1036SLinas Vepstas ELF32_R_TYPE(rela[i].r_info)); 15898fb1036SLinas Vepstas return -ENOEXEC; 15998fb1036SLinas Vepstas } 16098fb1036SLinas Vepstas } 16198fb1036SLinas Vepstas return 0; 16298fb1036SLinas Vepstas } 163