• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/arch/powerpc/kernel/
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/ftrace.h>
26#include <linux/cache.h>
27#include <linux/bug.h>
28#include <linux/sort.h>
29
30#include "setup.h"
31
32#define DEBUGP(fmt , ...)
33
34/* Count how many different relocations (different symbol, different
35   addend) */
36static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
37{
38	unsigned int i, r_info, r_addend, _count_relocs;
39
40	_count_relocs = 0;
41	r_info = 0;
42	r_addend = 0;
43	for (i = 0; i < num; i++)
44		/* Only count 24-bit relocs, others don't need stubs */
45		if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
46		    (r_info != ELF32_R_SYM(rela[i].r_info) ||
47		     r_addend != rela[i].r_addend)) {
48			_count_relocs++;
49			r_info = ELF32_R_SYM(rela[i].r_info);
50			r_addend = rela[i].r_addend;
51		}
52
53#ifdef CONFIG_DYNAMIC_FTRACE
54	_count_relocs++;	/* add one for ftrace_caller */
55#endif
56	return _count_relocs;
57}
58
59static int relacmp(const void *_x, const void *_y)
60{
61	const Elf32_Rela *x, *y;
62
63	y = (Elf32_Rela *)_x;
64	x = (Elf32_Rela *)_y;
65
66	/* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
67	 * make the comparison cheaper/faster. It won't affect the sorting or
68	 * the counting algorithms' performance
69	 */
70	if (x->r_info < y->r_info)
71		return -1;
72	else if (x->r_info > y->r_info)
73		return 1;
74	else if (x->r_addend < y->r_addend)
75		return -1;
76	else if (x->r_addend > y->r_addend)
77		return 1;
78	else
79		return 0;
80}
81
82static void relaswap(void *_x, void *_y, int size)
83{
84	uint32_t *x, *y, tmp;
85	int i;
86
87	y = (uint32_t *)_x;
88	x = (uint32_t *)_y;
89
90	for (i = 0; i < sizeof(Elf32_Rela) / sizeof(uint32_t); i++) {
91		tmp = x[i];
92		x[i] = y[i];
93		y[i] = tmp;
94	}
95}
96
97/* Get the potential trampolines size required of the init and
98   non-init sections */
99static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
100				  const Elf32_Shdr *sechdrs,
101				  const char *secstrings,
102				  int is_init)
103{
104	unsigned long ret = 0;
105	unsigned i;
106
107	/* Everything marked ALLOC (this includes the exported
108           symbols) */
109	for (i = 1; i < hdr->e_shnum; i++) {
110		/* If it's called *.init*, and we're not init, we're
111                   not interested */
112		if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
113		    != is_init)
114			continue;
115
116		/* We don't want to look at debug sections. */
117		if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
118			continue;
119
120		if (sechdrs[i].sh_type == SHT_RELA) {
121			DEBUGP("Found relocations in section %u\n", i);
122			DEBUGP("Ptr: %p.  Number: %u\n",
123			       (void *)hdr + sechdrs[i].sh_offset,
124			       sechdrs[i].sh_size / sizeof(Elf32_Rela));
125
126			/* Sort the relocation information based on a symbol and
127			 * addend key. This is a stable O(n*log n) complexity
128			 * alogrithm but it will reduce the complexity of
129			 * count_relocs() to linear complexity O(n)
130			 */
131			sort((void *)hdr + sechdrs[i].sh_offset,
132			     sechdrs[i].sh_size / sizeof(Elf32_Rela),
133			     sizeof(Elf32_Rela), relacmp, relaswap);
134
135			ret += count_relocs((void *)hdr
136					     + sechdrs[i].sh_offset,
137					     sechdrs[i].sh_size
138					     / sizeof(Elf32_Rela))
139				* sizeof(struct ppc_plt_entry);
140		}
141	}
142
143	return ret;
144}
145
146int module_frob_arch_sections(Elf32_Ehdr *hdr,
147			      Elf32_Shdr *sechdrs,
148			      char *secstrings,
149			      struct module *me)
150{
151	unsigned int i;
152
153	/* Find .plt and .init.plt sections */
154	for (i = 0; i < hdr->e_shnum; i++) {
155		if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
156			me->arch.init_plt_section = i;
157		else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
158			me->arch.core_plt_section = i;
159	}
160	if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
161		printk("Module doesn't contain .plt or .init.plt sections.\n");
162		return -ENOEXEC;
163	}
164
165	/* Override their sizes */
166	sechdrs[me->arch.core_plt_section].sh_size
167		= get_plt_size(hdr, sechdrs, secstrings, 0);
168	sechdrs[me->arch.init_plt_section].sh_size
169		= get_plt_size(hdr, sechdrs, secstrings, 1);
170	return 0;
171}
172
173int apply_relocate(Elf32_Shdr *sechdrs,
174		   const char *strtab,
175		   unsigned int symindex,
176		   unsigned int relsec,
177		   struct module *module)
178{
179	printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n",
180	       module->name);
181	return -ENOEXEC;
182}
183
184static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
185{
186	if (entry->jump[0] == 0x3d600000 + ((val + 0x8000) >> 16)
187	    && entry->jump[1] == 0x396b0000 + (val & 0xffff))
188		return 1;
189	return 0;
190}
191
192/* Set up a trampoline in the PLT to bounce us to the distant function */
193static uint32_t do_plt_call(void *location,
194			    Elf32_Addr val,
195			    Elf32_Shdr *sechdrs,
196			    struct module *mod)
197{
198	struct ppc_plt_entry *entry;
199
200	DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
201	/* Init, or core PLT? */
202	if (location >= mod->module_core
203	    && location < mod->module_core + mod->core_size)
204		entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
205	else
206		entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
207
208	/* Find this entry, or if that fails, the next avail. entry */
209	while (entry->jump[0]) {
210		if (entry_matches(entry, val)) return (uint32_t)entry;
211		entry++;
212	}
213
214	/* Stolen from Paul Mackerras as well... */
215	entry->jump[0] = 0x3d600000+((val+0x8000)>>16);	/* lis r11,sym@ha */
216	entry->jump[1] = 0x396b0000 + (val&0xffff);	/* addi r11,r11,sym@l*/
217	entry->jump[2] = 0x7d6903a6;			/* mtctr r11 */
218	entry->jump[3] = 0x4e800420;			/* bctr */
219
220	DEBUGP("Initialized plt for 0x%x at %p\n", val, entry);
221	return (uint32_t)entry;
222}
223
224int apply_relocate_add(Elf32_Shdr *sechdrs,
225		       const char *strtab,
226		       unsigned int symindex,
227		       unsigned int relsec,
228		       struct module *module)
229{
230	unsigned int i;
231	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
232	Elf32_Sym *sym;
233	uint32_t *location;
234	uint32_t value;
235
236	DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
237	       sechdrs[relsec].sh_info);
238	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
239		/* This is where to make the change */
240		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
241			+ rela[i].r_offset;
242		/* This is the symbol it is referring to.  Note that all
243		   undefined symbols have been resolved.  */
244		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
245			+ ELF32_R_SYM(rela[i].r_info);
246		/* `Everything is relative'. */
247		value = sym->st_value + rela[i].r_addend;
248
249		switch (ELF32_R_TYPE(rela[i].r_info)) {
250		case R_PPC_ADDR32:
251			/* Simply set it */
252			*(uint32_t *)location = value;
253			break;
254
255		case R_PPC_ADDR16_LO:
256			/* Low half of the symbol */
257			*(uint16_t *)location = value;
258			break;
259
260		case R_PPC_ADDR16_HI:
261			/* Higher half of the symbol */
262			*(uint16_t *)location = (value >> 16);
263			break;
264
265		case R_PPC_ADDR16_HA:
266			/* Sign-adjusted lower 16 bits: PPC ELF ABI says:
267			   (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
268			   This is the same, only sane.
269			 */
270			*(uint16_t *)location = (value + 0x8000) >> 16;
271			break;
272
273		case R_PPC_REL24:
274			if ((int)(value - (uint32_t)location) < -0x02000000
275			    || (int)(value - (uint32_t)location) >= 0x02000000)
276				value = do_plt_call(location, value,
277						    sechdrs, module);
278
279			/* Only replace bits 2 through 26 */
280			DEBUGP("REL24 value = %08X. location = %08X\n",
281			       value, (uint32_t)location);
282			DEBUGP("Location before: %08X.\n",
283			       *(uint32_t *)location);
284			*(uint32_t *)location
285				= (*(uint32_t *)location & ~0x03fffffc)
286				| ((value - (uint32_t)location)
287				   & 0x03fffffc);
288			DEBUGP("Location after: %08X.\n",
289			       *(uint32_t *)location);
290			DEBUGP("ie. jump to %08X+%08X = %08X\n",
291			       *(uint32_t *)location & 0x03fffffc,
292			       (uint32_t)location,
293			       (*(uint32_t *)location & 0x03fffffc)
294			       + (uint32_t)location);
295			break;
296
297		case R_PPC_REL32:
298			/* 32-bit relative jump. */
299			*(uint32_t *)location = value - (uint32_t)location;
300			break;
301
302		default:
303			printk("%s: unknown ADD relocation: %u\n",
304			       module->name,
305			       ELF32_R_TYPE(rela[i].r_info));
306			return -ENOEXEC;
307		}
308	}
309#ifdef CONFIG_DYNAMIC_FTRACE
310	module->arch.tramp =
311		do_plt_call(module->module_core,
312			    (unsigned long)ftrace_caller,
313			    sechdrs, module);
314#endif
315	return 0;
316}
317