xref: /openbmc/linux/arch/hexagon/kernel/module.c (revision 98fb103694b4de1b24794211405b9e0c469ca816)
1*98fb1036SLinas Vepstas /*
2*98fb1036SLinas Vepstas  * Kernel module loader for Hexagon
3*98fb1036SLinas Vepstas  *
4*98fb1036SLinas Vepstas  * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
5*98fb1036SLinas Vepstas  *
6*98fb1036SLinas Vepstas  * This program is free software; you can redistribute it and/or modify
7*98fb1036SLinas Vepstas  * it under the terms of the GNU General Public License version 2 and
8*98fb1036SLinas Vepstas  * only version 2 as published by the Free Software Foundation.
9*98fb1036SLinas Vepstas  *
10*98fb1036SLinas Vepstas  * This program is distributed in the hope that it will be useful,
11*98fb1036SLinas Vepstas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*98fb1036SLinas Vepstas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*98fb1036SLinas Vepstas  * GNU General Public License for more details.
14*98fb1036SLinas Vepstas  *
15*98fb1036SLinas Vepstas  * You should have received a copy of the GNU General Public License
16*98fb1036SLinas Vepstas  * along with this program; if not, write to the Free Software
17*98fb1036SLinas Vepstas  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18*98fb1036SLinas Vepstas  * 02110-1301, USA.
19*98fb1036SLinas Vepstas  */
20*98fb1036SLinas Vepstas 
21*98fb1036SLinas Vepstas #include <asm/module.h>
22*98fb1036SLinas Vepstas #include <linux/elf.h>
23*98fb1036SLinas Vepstas #include <linux/module.h>
24*98fb1036SLinas Vepstas #include <linux/moduleloader.h>
25*98fb1036SLinas Vepstas #include <linux/vmalloc.h>
26*98fb1036SLinas Vepstas 
27*98fb1036SLinas Vepstas #if 0
28*98fb1036SLinas Vepstas #define DEBUGP printk
29*98fb1036SLinas Vepstas #else
30*98fb1036SLinas Vepstas #define DEBUGP(fmt , ...)
31*98fb1036SLinas Vepstas #endif
32*98fb1036SLinas Vepstas 
33*98fb1036SLinas Vepstas /*
34*98fb1036SLinas Vepstas  * module_frob_arch_sections - tweak got/plt sections.
35*98fb1036SLinas Vepstas  * @hdr - pointer to elf header
36*98fb1036SLinas Vepstas  * @sechdrs - pointer to elf load section headers
37*98fb1036SLinas Vepstas  * @secstrings - symbol names
38*98fb1036SLinas Vepstas  * @mod - pointer to module
39*98fb1036SLinas Vepstas  */
40*98fb1036SLinas Vepstas int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
41*98fb1036SLinas Vepstas 				char *secstrings,
42*98fb1036SLinas Vepstas 				struct module *mod)
43*98fb1036SLinas Vepstas {
44*98fb1036SLinas Vepstas 	unsigned int i;
45*98fb1036SLinas Vepstas 	int found = 0;
46*98fb1036SLinas Vepstas 
47*98fb1036SLinas Vepstas 	/* Look for .plt and/or .got.plt and/or .init.plt sections */
48*98fb1036SLinas Vepstas 	for (i = 0; i < hdr->e_shnum; i++) {
49*98fb1036SLinas Vepstas 		DEBUGP("Section %d is %s\n", i,
50*98fb1036SLinas Vepstas 		       secstrings + sechdrs[i].sh_name);
51*98fb1036SLinas Vepstas 		if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
52*98fb1036SLinas Vepstas 			found = i+1;
53*98fb1036SLinas Vepstas 		if (strcmp(secstrings + sechdrs[i].sh_name, ".got.plt") == 0)
54*98fb1036SLinas Vepstas 			found = i+1;
55*98fb1036SLinas Vepstas 		if (strcmp(secstrings + sechdrs[i].sh_name, ".rela.plt") == 0)
56*98fb1036SLinas Vepstas 			found = i+1;
57*98fb1036SLinas Vepstas 	}
58*98fb1036SLinas Vepstas 
59*98fb1036SLinas Vepstas 	/* At this time, we don't support modules comiled with -shared */
60*98fb1036SLinas Vepstas 	if (found) {
61*98fb1036SLinas Vepstas 		printk(KERN_WARNING
62*98fb1036SLinas Vepstas 			"Module '%s' contains unexpected .plt/.got sections.\n",
63*98fb1036SLinas Vepstas 			mod->name);
64*98fb1036SLinas Vepstas 		/*  return -ENOEXEC;  */
65*98fb1036SLinas Vepstas 	}
66*98fb1036SLinas Vepstas 
67*98fb1036SLinas Vepstas 	return 0;
68*98fb1036SLinas Vepstas }
69*98fb1036SLinas Vepstas 
70*98fb1036SLinas Vepstas /*
71*98fb1036SLinas Vepstas  * apply_relocate_add - perform rela relocations.
72*98fb1036SLinas Vepstas  * @sechdrs - pointer to section headers
73*98fb1036SLinas Vepstas  * @strtab - some sort of start address?
74*98fb1036SLinas Vepstas  * @symindex - symbol index offset or something?
75*98fb1036SLinas Vepstas  * @relsec - address to relocate to?
76*98fb1036SLinas Vepstas  * @module - pointer to module
77*98fb1036SLinas Vepstas  *
78*98fb1036SLinas Vepstas  * Perform rela relocations.
79*98fb1036SLinas Vepstas  */
80*98fb1036SLinas Vepstas int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
81*98fb1036SLinas Vepstas 			unsigned int symindex, unsigned int relsec,
82*98fb1036SLinas Vepstas 			struct module *module)
83*98fb1036SLinas Vepstas {
84*98fb1036SLinas Vepstas 	unsigned int i;
85*98fb1036SLinas Vepstas 	Elf32_Sym *sym;
86*98fb1036SLinas Vepstas 	uint32_t *location;
87*98fb1036SLinas Vepstas 	uint32_t value;
88*98fb1036SLinas Vepstas 	unsigned int nrelocs = sechdrs[relsec].sh_size / sizeof(Elf32_Rela);
89*98fb1036SLinas Vepstas 	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
90*98fb1036SLinas Vepstas 	Elf32_Word sym_info = sechdrs[relsec].sh_info;
91*98fb1036SLinas Vepstas 	Elf32_Sym *sym_base = (Elf32_Sym *) sechdrs[symindex].sh_addr;
92*98fb1036SLinas Vepstas 	void *loc_base = (void *) sechdrs[sym_info].sh_addr;
93*98fb1036SLinas Vepstas 
94*98fb1036SLinas Vepstas 	DEBUGP("Applying relocations in section %u to section %u base=%p\n",
95*98fb1036SLinas Vepstas 	       relsec, sym_info, loc_base);
96*98fb1036SLinas Vepstas 
97*98fb1036SLinas Vepstas 	for (i = 0; i < nrelocs; i++) {
98*98fb1036SLinas Vepstas 
99*98fb1036SLinas Vepstas 		/* Symbol to relocate */
100*98fb1036SLinas Vepstas 		sym = sym_base + ELF32_R_SYM(rela[i].r_info);
101*98fb1036SLinas Vepstas 
102*98fb1036SLinas Vepstas 		/* Where to make the change */
103*98fb1036SLinas Vepstas 		location = loc_base + rela[i].r_offset;
104*98fb1036SLinas Vepstas 
105*98fb1036SLinas Vepstas 		/* `Everything is relative'. */
106*98fb1036SLinas Vepstas 		value = sym->st_value + rela[i].r_addend;
107*98fb1036SLinas Vepstas 
108*98fb1036SLinas Vepstas 		DEBUGP("%d: value=%08x loc=%p reloc=%d symbol=%s\n",
109*98fb1036SLinas Vepstas 		       i, value, location, ELF32_R_TYPE(rela[i].r_info),
110*98fb1036SLinas Vepstas 		       sym->st_name ?
111*98fb1036SLinas Vepstas 		       &strtab[sym->st_name] : "(anonymous)");
112*98fb1036SLinas Vepstas 
113*98fb1036SLinas Vepstas 		switch (ELF32_R_TYPE(rela[i].r_info)) {
114*98fb1036SLinas Vepstas 		case R_HEXAGON_B22_PCREL: {
115*98fb1036SLinas Vepstas 			int dist = (int)(value - (uint32_t)location);
116*98fb1036SLinas Vepstas 			if ((dist < -0x00800000) ||
117*98fb1036SLinas Vepstas 			    (dist >= 0x00800000)) {
118*98fb1036SLinas Vepstas 				printk(KERN_ERR
119*98fb1036SLinas Vepstas 				       "%s: %s: %08x=%08x-%08x %s\n",
120*98fb1036SLinas Vepstas 				       module->name,
121*98fb1036SLinas Vepstas 				       "R_HEXAGON_B22_PCREL reloc out of range",
122*98fb1036SLinas Vepstas 				       dist, value, (uint32_t)location,
123*98fb1036SLinas Vepstas 				       sym->st_name ?
124*98fb1036SLinas Vepstas 				       &strtab[sym->st_name] : "(anonymous)");
125*98fb1036SLinas Vepstas 				return -ENOEXEC;
126*98fb1036SLinas Vepstas 			}
127*98fb1036SLinas Vepstas 			DEBUGP("B22_PCREL contents: %08X.\n", *location);
128*98fb1036SLinas Vepstas 			*location &= ~0x01ff3fff;
129*98fb1036SLinas Vepstas 			*location |= 0x00003fff & dist;
130*98fb1036SLinas Vepstas 			*location |= 0x01ff0000 & (dist<<2);
131*98fb1036SLinas Vepstas 			DEBUGP("Contents after reloc: %08x\n", *location);
132*98fb1036SLinas Vepstas 			break;
133*98fb1036SLinas Vepstas 		}
134*98fb1036SLinas Vepstas 		case R_HEXAGON_HI16:
135*98fb1036SLinas Vepstas 			value = (value>>16) & 0xffff;
136*98fb1036SLinas Vepstas 			/* fallthrough */
137*98fb1036SLinas Vepstas 		case R_HEXAGON_LO16:
138*98fb1036SLinas Vepstas 			*location &= ~0x00c03fff;
139*98fb1036SLinas Vepstas 			*location |= value & 0x3fff;
140*98fb1036SLinas Vepstas 			*location |= (value & 0xc000) << 8;
141*98fb1036SLinas Vepstas 			break;
142*98fb1036SLinas Vepstas 		case R_HEXAGON_32:
143*98fb1036SLinas Vepstas 			*location = value;
144*98fb1036SLinas Vepstas 			break;
145*98fb1036SLinas Vepstas 		case R_HEXAGON_32_PCREL:
146*98fb1036SLinas Vepstas 			*location = value - (uint32_t)location;
147*98fb1036SLinas Vepstas 			break;
148*98fb1036SLinas Vepstas 		case R_HEXAGON_PLT_B22_PCREL:
149*98fb1036SLinas Vepstas 		case R_HEXAGON_GOTOFF_LO16:
150*98fb1036SLinas Vepstas 		case R_HEXAGON_GOTOFF_HI16:
151*98fb1036SLinas Vepstas 			printk(KERN_ERR "%s: GOT/PLT relocations unsupported\n",
152*98fb1036SLinas Vepstas 			       module->name);
153*98fb1036SLinas Vepstas 			return -ENOEXEC;
154*98fb1036SLinas Vepstas 		default:
155*98fb1036SLinas Vepstas 			printk(KERN_ERR "%s: unknown relocation: %u\n",
156*98fb1036SLinas Vepstas 			       module->name,
157*98fb1036SLinas Vepstas 			       ELF32_R_TYPE(rela[i].r_info));
158*98fb1036SLinas Vepstas 			return -ENOEXEC;
159*98fb1036SLinas Vepstas 		}
160*98fb1036SLinas Vepstas 	}
161*98fb1036SLinas Vepstas 	return 0;
162*98fb1036SLinas Vepstas }
163