108dbd0f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 298fb1036SLinas Vepstas /* 398fb1036SLinas Vepstas * Kernel module loader for Hexagon 498fb1036SLinas Vepstas * 5e1858b2aSRichard Kuo * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved. 698fb1036SLinas Vepstas */ 798fb1036SLinas Vepstas 898fb1036SLinas Vepstas #include <asm/module.h> 998fb1036SLinas Vepstas #include <linux/elf.h> 1098fb1036SLinas Vepstas #include <linux/module.h> 1198fb1036SLinas Vepstas #include <linux/moduleloader.h> 1298fb1036SLinas Vepstas #include <linux/vmalloc.h> 1398fb1036SLinas Vepstas 1498fb1036SLinas Vepstas #if 0 1598fb1036SLinas Vepstas #define DEBUGP printk 1698fb1036SLinas Vepstas #else 1798fb1036SLinas Vepstas #define DEBUGP(fmt , ...) 1898fb1036SLinas Vepstas #endif 1998fb1036SLinas Vepstas 2098fb1036SLinas Vepstas /* 2198fb1036SLinas Vepstas * module_frob_arch_sections - tweak got/plt sections. 2298fb1036SLinas Vepstas * @hdr - pointer to elf header 2398fb1036SLinas Vepstas * @sechdrs - pointer to elf load section headers 2498fb1036SLinas Vepstas * @secstrings - symbol names 2598fb1036SLinas Vepstas * @mod - pointer to module 2698fb1036SLinas Vepstas */ 2798fb1036SLinas Vepstas int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 2898fb1036SLinas Vepstas char *secstrings, 2998fb1036SLinas Vepstas struct module *mod) 3098fb1036SLinas Vepstas { 3198fb1036SLinas Vepstas unsigned int i; 3298fb1036SLinas Vepstas int found = 0; 3398fb1036SLinas Vepstas 3498fb1036SLinas Vepstas /* Look for .plt and/or .got.plt and/or .init.plt sections */ 3598fb1036SLinas Vepstas for (i = 0; i < hdr->e_shnum; i++) { 3698fb1036SLinas Vepstas DEBUGP("Section %d is %s\n", i, 3798fb1036SLinas Vepstas secstrings + sechdrs[i].sh_name); 3898fb1036SLinas Vepstas if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0) 3998fb1036SLinas Vepstas found = i+1; 4098fb1036SLinas Vepstas if (strcmp(secstrings + sechdrs[i].sh_name, ".got.plt") == 0) 4198fb1036SLinas Vepstas found = i+1; 4298fb1036SLinas Vepstas if (strcmp(secstrings + sechdrs[i].sh_name, ".rela.plt") == 0) 4398fb1036SLinas Vepstas found = i+1; 4498fb1036SLinas Vepstas } 4598fb1036SLinas Vepstas 4698fb1036SLinas Vepstas /* At this time, we don't support modules comiled with -shared */ 4798fb1036SLinas Vepstas if (found) { 4898fb1036SLinas Vepstas printk(KERN_WARNING 4998fb1036SLinas Vepstas "Module '%s' contains unexpected .plt/.got sections.\n", 5098fb1036SLinas Vepstas mod->name); 5198fb1036SLinas Vepstas /* return -ENOEXEC; */ 5298fb1036SLinas Vepstas } 5398fb1036SLinas Vepstas 5498fb1036SLinas Vepstas return 0; 5598fb1036SLinas Vepstas } 5698fb1036SLinas Vepstas 5798fb1036SLinas Vepstas /* 5898fb1036SLinas Vepstas * apply_relocate_add - perform rela relocations. 5998fb1036SLinas Vepstas * @sechdrs - pointer to section headers 6098fb1036SLinas Vepstas * @strtab - some sort of start address? 6198fb1036SLinas Vepstas * @symindex - symbol index offset or something? 6298fb1036SLinas Vepstas * @relsec - address to relocate to? 6398fb1036SLinas Vepstas * @module - pointer to module 6498fb1036SLinas Vepstas * 6598fb1036SLinas Vepstas * Perform rela relocations. 6698fb1036SLinas Vepstas */ 6798fb1036SLinas Vepstas int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 6898fb1036SLinas Vepstas unsigned int symindex, unsigned int relsec, 6998fb1036SLinas Vepstas struct module *module) 7098fb1036SLinas Vepstas { 7198fb1036SLinas Vepstas unsigned int i; 7298fb1036SLinas Vepstas Elf32_Sym *sym; 7398fb1036SLinas Vepstas uint32_t *location; 7498fb1036SLinas Vepstas uint32_t value; 7598fb1036SLinas Vepstas unsigned int nrelocs = sechdrs[relsec].sh_size / sizeof(Elf32_Rela); 7698fb1036SLinas Vepstas Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr; 7798fb1036SLinas Vepstas Elf32_Word sym_info = sechdrs[relsec].sh_info; 7898fb1036SLinas Vepstas Elf32_Sym *sym_base = (Elf32_Sym *) sechdrs[symindex].sh_addr; 7998fb1036SLinas Vepstas void *loc_base = (void *) sechdrs[sym_info].sh_addr; 8098fb1036SLinas Vepstas 8198fb1036SLinas Vepstas DEBUGP("Applying relocations in section %u to section %u base=%p\n", 8298fb1036SLinas Vepstas relsec, sym_info, loc_base); 8398fb1036SLinas Vepstas 8498fb1036SLinas Vepstas for (i = 0; i < nrelocs; i++) { 8598fb1036SLinas Vepstas 8698fb1036SLinas Vepstas /* Symbol to relocate */ 8798fb1036SLinas Vepstas sym = sym_base + ELF32_R_SYM(rela[i].r_info); 8898fb1036SLinas Vepstas 8998fb1036SLinas Vepstas /* Where to make the change */ 9098fb1036SLinas Vepstas location = loc_base + rela[i].r_offset; 9198fb1036SLinas Vepstas 9298fb1036SLinas Vepstas /* `Everything is relative'. */ 9398fb1036SLinas Vepstas value = sym->st_value + rela[i].r_addend; 9498fb1036SLinas Vepstas 9598fb1036SLinas Vepstas DEBUGP("%d: value=%08x loc=%p reloc=%d symbol=%s\n", 9698fb1036SLinas Vepstas i, value, location, ELF32_R_TYPE(rela[i].r_info), 9798fb1036SLinas Vepstas sym->st_name ? 9898fb1036SLinas Vepstas &strtab[sym->st_name] : "(anonymous)"); 9998fb1036SLinas Vepstas 10098fb1036SLinas Vepstas switch (ELF32_R_TYPE(rela[i].r_info)) { 10198fb1036SLinas Vepstas case R_HEXAGON_B22_PCREL: { 10298fb1036SLinas Vepstas int dist = (int)(value - (uint32_t)location); 10398fb1036SLinas Vepstas if ((dist < -0x00800000) || 10498fb1036SLinas Vepstas (dist >= 0x00800000)) { 10598fb1036SLinas Vepstas printk(KERN_ERR 10698fb1036SLinas Vepstas "%s: %s: %08x=%08x-%08x %s\n", 10798fb1036SLinas Vepstas module->name, 10898fb1036SLinas Vepstas "R_HEXAGON_B22_PCREL reloc out of range", 10998fb1036SLinas Vepstas dist, value, (uint32_t)location, 11098fb1036SLinas Vepstas sym->st_name ? 11198fb1036SLinas Vepstas &strtab[sym->st_name] : "(anonymous)"); 11298fb1036SLinas Vepstas return -ENOEXEC; 11398fb1036SLinas Vepstas } 11498fb1036SLinas Vepstas DEBUGP("B22_PCREL contents: %08X.\n", *location); 11598fb1036SLinas Vepstas *location &= ~0x01ff3fff; 11698fb1036SLinas Vepstas *location |= 0x00003fff & dist; 11798fb1036SLinas Vepstas *location |= 0x01ff0000 & (dist<<2); 11898fb1036SLinas Vepstas DEBUGP("Contents after reloc: %08x\n", *location); 11998fb1036SLinas Vepstas break; 12098fb1036SLinas Vepstas } 12198fb1036SLinas Vepstas case R_HEXAGON_HI16: 12298fb1036SLinas Vepstas value = (value>>16) & 0xffff; 123*df561f66SGustavo A. R. Silva fallthrough; 12498fb1036SLinas Vepstas case R_HEXAGON_LO16: 12598fb1036SLinas Vepstas *location &= ~0x00c03fff; 12698fb1036SLinas Vepstas *location |= value & 0x3fff; 12798fb1036SLinas Vepstas *location |= (value & 0xc000) << 8; 12898fb1036SLinas Vepstas break; 12998fb1036SLinas Vepstas case R_HEXAGON_32: 13098fb1036SLinas Vepstas *location = value; 13198fb1036SLinas Vepstas break; 13298fb1036SLinas Vepstas case R_HEXAGON_32_PCREL: 13398fb1036SLinas Vepstas *location = value - (uint32_t)location; 13498fb1036SLinas Vepstas break; 13598fb1036SLinas Vepstas case R_HEXAGON_PLT_B22_PCREL: 13698fb1036SLinas Vepstas case R_HEXAGON_GOTOFF_LO16: 13798fb1036SLinas Vepstas case R_HEXAGON_GOTOFF_HI16: 13898fb1036SLinas Vepstas printk(KERN_ERR "%s: GOT/PLT relocations unsupported\n", 13998fb1036SLinas Vepstas module->name); 14098fb1036SLinas Vepstas return -ENOEXEC; 14198fb1036SLinas Vepstas default: 14298fb1036SLinas Vepstas printk(KERN_ERR "%s: unknown relocation: %u\n", 14398fb1036SLinas Vepstas module->name, 14498fb1036SLinas Vepstas ELF32_R_TYPE(rela[i].r_info)); 14598fb1036SLinas Vepstas return -ENOEXEC; 14698fb1036SLinas Vepstas } 14798fb1036SLinas Vepstas } 14898fb1036SLinas Vepstas return 0; 14998fb1036SLinas Vepstas } 150