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 = kmalloc(nsyms * sizeof(struct got_entry), GFP_KERNEL);
119	memset(chains, 0, nsyms * sizeof(struct got_entry));
120
121	got->sh_size = 0;
122	got->sh_addralign = 8;
123	got->sh_type = SHT_NOBITS;
124
125	/* Examine all LITERAL relocations to find out what GOT entries
126	   are required.  This sizes the GOT section as well.  */
127	for (s = sechdrs; s < esechdrs; ++s)
128		if (s->sh_type == SHT_RELA) {
129			nrela = s->sh_size / sizeof(Elf64_Rela);
130			rela = (void *)hdr + s->sh_offset;
131			for (i = 0; i < nrela; ++i)
132				process_reloc_for_got(rela+i, chains,
133						      &got->sh_size);
134		}
135
136	/* Free the memory we allocated.  */
137	for (i = 0; i < nsyms; ++i) {
138		struct got_entry *g, *n;
139		for (g = chains[i].next; g ; g = n) {
140			n = g->next;
141			kfree(g);
142		}
143	}
144	kfree(chains);
145
146	return 0;
147}
148
149int
150apply_relocate(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
151	       unsigned int relsec, struct module *me)
152{
153	printk(KERN_ERR "module %s: REL relocation unsupported\n", me->name);
154	return -ENOEXEC;
155}
156
157int
158apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab,
159		   unsigned int symindex, unsigned int relsec,
160		   struct module *me)
161{
162	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
163	unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela);
164	Elf64_Sym *symtab, *sym;
165	void *base, *location;
166	unsigned long got, gp;
167
168	DEBUGP("Applying relocate section %u to %u\n", relsec,
169	       sechdrs[relsec].sh_info);
170
171	base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr;
172	symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr;
173
174	/* The small sections were sorted to the end of the segment.
175	   The following should definitely cover them.  */
176	gp = (u64)me->module_core + me->core_size - 0x8000;
177	got = sechdrs[me->arch.gotsecindex].sh_addr;
178
179	for (i = 0; i < n; i++) {
180		unsigned long r_sym = ELF64_R_SYM (rela[i].r_info);
181		unsigned long r_type = ELF64_R_TYPE (rela[i].r_info);
182		unsigned long r_got_offset = r_type >> 8;
183		unsigned long value, hi, lo;
184		r_type &= 0xff;
185
186		/* This is where to make the change.  */
187		location = base + rela[i].r_offset;
188
189		/* This is the symbol it is referring to.  Note that all
190		   unresolved symbols have been resolved.  */
191		sym = symtab + r_sym;
192		value = sym->st_value + rela[i].r_addend;
193
194		switch (r_type) {
195		case R_ALPHA_NONE:
196			break;
197		case R_ALPHA_REFQUAD:
198			/* BUG() can produce misaligned relocations. */
199			((u32 *)location)[0] = value;
200			((u32 *)location)[1] = value >> 32;
201			break;
202		case R_ALPHA_GPREL32:
203			value -= gp;
204			if ((int)value != value)
205				goto reloc_overflow;
206			*(u32 *)location = value;
207			break;
208		case R_ALPHA_LITERAL:
209			hi = got + r_got_offset;
210			lo = hi - gp;
211			if ((short)lo != lo)
212				goto reloc_overflow;
213			*(u16 *)location = lo;
214			*(u64 *)hi = value;
215			break;
216		case R_ALPHA_LITUSE:
217			break;
218		case R_ALPHA_GPDISP:
219			value = gp - (u64)location;
220			lo = (short)value;
221			hi = (int)(value - lo);
222			if (hi + lo != value)
223				goto reloc_overflow;
224			*(u16 *)location = hi >> 16;
225			*(u16 *)(location + rela[i].r_addend) = lo;
226			break;
227		case R_ALPHA_BRSGP:
228			/* BRSGP is only allowed to bind to local symbols.
229			   If the section is undef, this means that the
230			   value was resolved from somewhere else.  */
231			if (sym->st_shndx == SHN_UNDEF)
232				goto reloc_overflow;
233			if ((sym->st_other & STO_ALPHA_STD_GPLOAD) ==
234			    STO_ALPHA_STD_GPLOAD)
235				/* Omit the prologue. */
236				value += 8;
237			/* FALLTHRU */
238		case R_ALPHA_BRADDR:
239			value -= (u64)location + 4;
240			if (value & 3)
241				goto reloc_overflow;
242			value = (long)value >> 2;
243			if (value + (1<<21) >= 1<<22)
244				goto reloc_overflow;
245			value &= 0x1fffff;
246			value |= *(u32 *)location & ~0x1fffff;
247			*(u32 *)location = value;
248			break;
249		case R_ALPHA_HINT:
250			break;
251		case R_ALPHA_SREL32:
252			value -= (u64)location;
253			if ((int)value != value)
254				goto reloc_overflow;
255			*(u32 *)location = value;
256			break;
257		case R_ALPHA_SREL64:
258			value -= (u64)location;
259			*(u64 *)location = value;
260			break;
261		case R_ALPHA_GPRELHIGH:
262			value = (long)(value - gp + 0x8000) >> 16;
263			if ((short) value != value)
264				goto reloc_overflow;
265			*(u16 *)location = value;
266			break;
267		case R_ALPHA_GPRELLOW:
268			value -= gp;
269			*(u16 *)location = value;
270			break;
271		case R_ALPHA_GPREL16:
272			value -= gp;
273			if ((short) value != value)
274				goto reloc_overflow;
275			*(u16 *)location = value;
276			break;
277		default:
278			printk(KERN_ERR "module %s: Unknown relocation: %lu\n",
279			       me->name, r_type);
280			return -ENOEXEC;
281		reloc_overflow:
282			if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION)
283			  printk(KERN_ERR
284			         "module %s: Relocation (type %lu) overflow vs section %d\n",
285			         me->name, r_type, sym->st_shndx);
286			else
287			  printk(KERN_ERR
288			         "module %s: Relocation (type %lu) overflow vs %s\n",
289			         me->name, r_type, strtab + sym->st_name);
290			return -ENOEXEC;
291		}
292	}
293
294	return 0;
295}
296
297int
298module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
299		struct module *me)
300{
301	return 0;
302}
303
304void
305module_arch_cleanup(struct module *mod)
306{
307}
308