1/*  Kernel module help for i386.
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/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/bug.h>
25
26#define DEBUGP(fmt...)
27
28void *module_alloc(unsigned long size)
29{
30	if (size == 0)
31		return NULL;
32	return vmalloc_exec(size);
33}
34
35
36/* Free memory returned from module_alloc */
37void module_free(struct module *mod, void *module_region)
38{
39	vfree(module_region);
40}
41
42/* We don't need anything special. */
43int module_frob_arch_sections(Elf_Ehdr *hdr,
44			      Elf_Shdr *sechdrs,
45			      char *secstrings,
46			      struct module *mod)
47{
48	return 0;
49}
50
51int apply_relocate(Elf32_Shdr *sechdrs,
52		   const char *strtab,
53		   unsigned int symindex,
54		   unsigned int relsec,
55		   struct module *me)
56{
57	unsigned int i;
58	Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
59	Elf32_Sym *sym;
60	uint32_t *location;
61
62	DEBUGP("Applying relocate section %u to %u\n", relsec,
63	       sechdrs[relsec].sh_info);
64	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
65		/* This is where to make the change */
66		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
67			+ rel[i].r_offset;
68		/* This is the symbol it is referring to.  Note that all
69		   undefined symbols have been resolved.  */
70		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
71			+ ELF32_R_SYM(rel[i].r_info);
72
73		switch (ELF32_R_TYPE(rel[i].r_info)) {
74		case R_386_32:
75			/* We add the value into the location given */
76			*location += sym->st_value;
77			break;
78		case R_386_PC32:
79			/* Add the value, subtract its postition */
80			*location += sym->st_value - (uint32_t)location;
81			break;
82		default:
83			printk(KERN_ERR "module %s: Unknown relocation: %u\n",
84			       me->name, ELF32_R_TYPE(rel[i].r_info));
85			return -ENOEXEC;
86		}
87	}
88	return 0;
89}
90
91int apply_relocate_add(Elf32_Shdr *sechdrs,
92		       const char *strtab,
93		       unsigned int symindex,
94		       unsigned int relsec,
95		       struct module *me)
96{
97	printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
98	       me->name);
99	return -ENOEXEC;
100}
101
102int module_finalize(const Elf_Ehdr *hdr,
103		    const Elf_Shdr *sechdrs,
104		    struct module *me)
105{
106	const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
107		*para = NULL;
108	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
109
110	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
111		if (!strcmp(".text", secstrings + s->sh_name))
112			text = s;
113		if (!strcmp(".altinstructions", secstrings + s->sh_name))
114			alt = s;
115		if (!strcmp(".smp_locks", secstrings + s->sh_name))
116			locks= s;
117		if (!strcmp(".parainstructions", secstrings + s->sh_name))
118			para = s;
119	}
120
121	if (alt) {
122		/* patch .altinstructions */
123		void *aseg = (void *)alt->sh_addr;
124		apply_alternatives(aseg, aseg + alt->sh_size);
125	}
126	if (locks && text) {
127		void *lseg = (void *)locks->sh_addr;
128		void *tseg = (void *)text->sh_addr;
129		alternatives_smp_module_add(me, me->name,
130					    lseg, lseg + locks->sh_size,
131					    tseg, tseg + text->sh_size);
132	}
133
134	if (para) {
135		void *pseg = (void *)para->sh_addr;
136		apply_paravirt(pseg, pseg + para->sh_size);
137	}
138
139	return module_bug_finalize(hdr, sechdrs, me);
140}
141
142void module_arch_cleanup(struct module *mod)
143{
144	alternatives_smp_module_del(mod);
145	module_bug_cleanup(mod);
146}
147