• 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/alpha/kernel/
1/*  Kernel module help for Alpha.
2    Copyright (C) 2002 Richard Henderson.
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/moduleloader.h>
19#include <linux/elf.h>
20#include <linux/vmalloc.h>
21#include <linux/fs.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
25
26#define DEBUGP(fmt...)
27
28void *
29module_alloc(unsigned long size)
30{
31	if (size == 0)
32		return NULL;
33	return vmalloc(size);
34}
35
36void
37module_free(struct module *mod, void *module_region)
38{
39	vfree(module_region);
40}
41
42/* Allocate the GOT at the end of the core sections.  */
43
44struct got_entry {
45	struct got_entry *next;
46	Elf64_Sxword r_addend;
47	int got_offset;
48};
49
50static inline void
51process_reloc_for_got(Elf64_Rela *rela,
52		      struct got_entry *chains, Elf64_Xword *poffset)
53{
54	unsigned long r_sym = ELF64_R_SYM (rela->r_info);
55	unsigned long r_type = ELF64_R_TYPE (rela->r_info);
56	Elf64_Sxword r_addend = rela->r_addend;
57	struct got_entry *g;
58
59	if (r_type != R_ALPHA_LITERAL)
60		return;
61
62	for (g = chains + r_sym; g ; g = g->next)
63		if (g->r_addend == r_addend) {
64			if (g->got_offset == 0) {
65				g->got_offset = *poffset;
66				*poffset += 8;
67			}
68			goto found_entry;
69		}
70
71	g = kmalloc (sizeof (*g), GFP_KERNEL);
72	g->next = chains[r_sym].next;
73	g->r_addend = r_addend;
74	g->got_offset = *poffset;
75	*poffset += 8;
76	chains[r_sym].next = g;
77
78 found_entry:
79	/* Trick: most of the ELF64_R_TYPE field is unused.  There are
80	   42 valid relocation types, and a 32-bit field.  Co-opt the
81	   bits above 256 to store the got offset for this reloc.  */
82	rela->r_info |= g->got_offset << 8;
83}
84
85int
86module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs,
87			  char *secstrings, struct module *me)
88{
89	struct got_entry *chains;
90	Elf64_Rela *rela;
91	Elf64_Shdr *esechdrs, *symtab, *s, *got;
92	unsigned long nsyms, nrela, i;
93
94	esechdrs = sechdrs + hdr->e_shnum;
95	symtab = got = NULL;
96
97	/* Find out how large the symbol table is.  Allocate one got_entry
98	   head per symbol.  Normally this will be enough, but not always.
99	   We'll chain different offsets for the symbol down each head.  */
100	for (s = sechdrs; s < esechdrs; ++s)
101		if (s->sh_type == SHT_SYMTAB)
102			symtab = s;
103		else if (!strcmp(".got", secstrings + s->sh_name)) {
104			got = s;
105			me->arch.gotsecindex = s - sechdrs;
106		}
107
108	if (!symtab) {
109		printk(KERN_ERR "module %s: no symbol table\n", me->name);
110		return -ENOEXEC;
111	}
112	if (!got) {
113		printk(KERN_ERR "module %s: no got section\n", me->name);
114		return -ENOEXEC;
115	}
116
117	nsyms = symtab->sh_size / sizeof(Elf64_Sym);
118	chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL);
119	if (!chains) {
120		printk(KERN_ERR
121		       "module %s: no memory for symbol chain buffer\n",
122		       me->name);
123		return -ENOMEM;
124	}
125
126	got->sh_size = 0;
127	got->sh_addralign = 8;
128	got->sh_type = SHT_NOBITS;
129
130	/* Examine all LITERAL relocations to find out what GOT entries
131	   are required.  This sizes the GOT section as well.  */
132	for (s = sechdrs; s < esechdrs; ++s)
133		if (s->sh_type == SHT_RELA) {
134			nrela = s->sh_size / sizeof(Elf64_Rela);
135			rela = (void *)hdr + s->sh_offset;
136			for (i = 0; i < nrela; ++i)
137				process_reloc_for_got(rela+i, chains,
138						      &got->sh_size);
139		}
140
141	/* Free the memory we allocated.  */
142	for (i = 0; i < nsyms; ++i) {
143		struct got_entry *g, *n;
144		for (g = chains[i].next; g ; g = n) {
145			n = g->next;
146			kfree(g);
147		}
148	}
149	kfree(chains);
150
151	return 0;
152}
153
154int
155apply_relocate(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
156	       unsigned int relsec, struct module *me)
157{
158	printk(KERN_ERR "module %s: REL relocation unsupported\n", me->name);
159	return -ENOEXEC;
160}
161
162int
163apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab,
164		   unsigned int symindex, unsigned int relsec,
165		   struct module *me)
166{
167	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
168	unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela);
169	Elf64_Sym *symtab, *sym;
170	void *base, *location;
171	unsigned long got, gp;
172
173	DEBUGP("Applying relocate section %u to %u\n", relsec,
174	       sechdrs[relsec].sh_info);
175
176	base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr;
177	symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr;
178
179	/* The small sections were sorted to the end of the segment.
180	   The following should definitely cover them.  */
181	gp = (u64)me->module_core + me->core_size - 0x8000;
182	got = sechdrs[me->arch.gotsecindex].sh_addr;
183
184	for (i = 0; i < n; i++) {
185		unsigned long r_sym = ELF64_R_SYM (rela[i].r_info);
186		unsigned long r_type = ELF64_R_TYPE (rela[i].r_info);
187		unsigned long r_got_offset = r_type >> 8;
188		unsigned long value, hi, lo;
189		r_type &= 0xff;
190
191		/* This is where to make the change.  */
192		location = base + rela[i].r_offset;
193
194		/* This is the symbol it is referring to.  Note that all
195		   unresolved symbols have been resolved.  */
196		sym = symtab + r_sym;
197		value = sym->st_value + rela[i].r_addend;
198
199		switch (r_type) {
200		case R_ALPHA_NONE:
201			break;
202		case R_ALPHA_REFQUAD:
203			/* BUG() can produce misaligned relocations. */
204			((u32 *)location)[0] = value;
205			((u32 *)location)[1] = value >> 32;
206			break;
207		case R_ALPHA_GPREL32:
208			value -= gp;
209			if ((int)value != value)
210				goto reloc_overflow;
211			*(u32 *)location = value;
212			break;
213		case R_ALPHA_LITERAL:
214			hi = got + r_got_offset;
215			lo = hi - gp;
216			if ((short)lo != lo)
217				goto reloc_overflow;
218			*(u16 *)location = lo;
219			*(u64 *)hi = value;
220			break;
221		case R_ALPHA_LITUSE:
222			break;
223		case R_ALPHA_GPDISP:
224			value = gp - (u64)location;
225			lo = (short)value;
226			hi = (int)(value - lo);
227			if (hi + lo != value)
228				goto reloc_overflow;
229			*(u16 *)location = hi >> 16;
230			*(u16 *)(location + rela[i].r_addend) = lo;
231			break;
232		case R_ALPHA_BRSGP:
233			/* BRSGP is only allowed to bind to local symbols.
234			   If the section is undef, this means that the
235			   value was resolved from somewhere else.  */
236			if (sym->st_shndx == SHN_UNDEF)
237				goto reloc_overflow;
238			if ((sym->st_other & STO_ALPHA_STD_GPLOAD) ==
239			    STO_ALPHA_STD_GPLOAD)
240				/* Omit the prologue. */
241				value += 8;
242			/* FALLTHRU */
243		case R_ALPHA_BRADDR:
244			value -= (u64)location + 4;
245			if (value & 3)
246				goto reloc_overflow;
247			value = (long)value >> 2;
248			if (value + (1<<21) >= 1<<22)
249				goto reloc_overflow;
250			value &= 0x1fffff;
251			value |= *(u32 *)location & ~0x1fffff;
252			*(u32 *)location = value;
253			break;
254		case R_ALPHA_HINT:
255			break;
256		case R_ALPHA_SREL32:
257			value -= (u64)location;
258			if ((int)value != value)
259				goto reloc_overflow;
260			*(u32 *)location = value;
261			break;
262		case R_ALPHA_SREL64:
263			value -= (u64)location;
264			*(u64 *)location = value;
265			break;
266		case R_ALPHA_GPRELHIGH:
267			value = (long)(value - gp + 0x8000) >> 16;
268			if ((short) value != value)
269				goto reloc_overflow;
270			*(u16 *)location = value;
271			break;
272		case R_ALPHA_GPRELLOW:
273			value -= gp;
274			*(u16 *)location = value;
275			break;
276		case R_ALPHA_GPREL16:
277			value -= gp;
278			if ((short) value != value)
279				goto reloc_overflow;
280			*(u16 *)location = value;
281			break;
282		default:
283			printk(KERN_ERR "module %s: Unknown relocation: %lu\n",
284			       me->name, r_type);
285			return -ENOEXEC;
286		reloc_overflow:
287			if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION)
288			  printk(KERN_ERR
289			         "module %s: Relocation (type %lu) overflow vs section %d\n",
290			         me->name, r_type, sym->st_shndx);
291			else
292			  printk(KERN_ERR
293			         "module %s: Relocation (type %lu) overflow vs %s\n",
294			         me->name, r_type, strtab + sym->st_name);
295			return -ENOEXEC;
296		}
297	}
298
299	return 0;
300}
301
302int
303module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
304		struct module *me)
305{
306	return 0;
307}
308
309void
310module_arch_cleanup(struct module *mod)
311{
312}
313