1/*
2 * arch/v850/kernel/module.c -- Architecture-specific module functions
3 *
4 *  Copyright (C) 2002,03  NEC Electronics Corporation
5 *  Copyright (C) 2002,03  Miles Bader <miles@gnu.org>
6 *  Copyright (C) 2001,03  Rusty Russell
7 *
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License.  See the file COPYING in the main directory of this
10 * archive for more details.
11 *
12 * Written by Miles Bader <miles@gnu.org>
13 *
14 * Derived in part from arch/ppc/kernel/module.c
15 */
16
17#include <linux/kernel.h>
18#include <linux/vmalloc.h>
19#include <linux/moduleloader.h>
20#include <linux/elf.h>
21
22#define DEBUGP(fmt , ...)
23
24void *module_alloc (unsigned long size)
25{
26	return size == 0 ? 0 : vmalloc (size);
27}
28
29void module_free (struct module *mod, void *module_region)
30{
31	vfree (module_region);
32}
33
34int module_finalize (const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
35		     struct module *mod)
36{
37	return 0;
38}
39
40/* Count how many different relocations (different symbol, different
41   addend) */
42static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
43{
44	unsigned int i, j, ret = 0;
45
46	/* Sure, this is order(n^2), but it's usually short, and not
47           time critical */
48	for (i = 0; i < num; i++) {
49		for (j = 0; j < i; j++) {
50			/* If this addend appeared before, it's
51                           already been counted */
52			if (ELF32_R_SYM(rela[i].r_info)
53			    == ELF32_R_SYM(rela[j].r_info)
54			    && rela[i].r_addend == rela[j].r_addend)
55				break;
56		}
57		if (j == i) ret++;
58	}
59	return ret;
60}
61
62/* Get the potential trampolines size required of the init and
63   non-init sections */
64static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
65				  const Elf32_Shdr *sechdrs,
66				  const char *secstrings,
67				  int is_init)
68{
69	unsigned long ret = 0;
70	unsigned i;
71
72	/* Everything marked ALLOC (this includes the exported
73           symbols) */
74	for (i = 1; i < hdr->e_shnum; i++) {
75		/* If it's called *.init*, and we're not init, we're
76                   not interested */
77		if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
78		    != is_init)
79			continue;
80
81		if (sechdrs[i].sh_type == SHT_RELA) {
82			DEBUGP("Found relocations in section %u\n", i);
83			DEBUGP("Ptr: %p.  Number: %u\n",
84			       (void *)hdr + sechdrs[i].sh_offset,
85			       sechdrs[i].sh_size / sizeof(Elf32_Rela));
86			ret += count_relocs((void *)hdr
87					     + sechdrs[i].sh_offset,
88					     sechdrs[i].sh_size
89					     / sizeof(Elf32_Rela))
90				* sizeof(struct v850_plt_entry);
91		}
92	}
93
94	return ret;
95}
96
97int module_frob_arch_sections(Elf32_Ehdr *hdr,
98			      Elf32_Shdr *sechdrs,
99			      char *secstrings,
100			      struct module *me)
101{
102	unsigned int i;
103
104	/* Find .plt and .pltinit sections */
105	for (i = 0; i < hdr->e_shnum; i++) {
106		if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
107			me->arch.init_plt_section = i;
108		else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
109			me->arch.core_plt_section = i;
110	}
111	if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
112		printk("Module doesn't contain .plt or .plt.init sections.\n");
113		return -ENOEXEC;
114	}
115
116	/* Override their sizes */
117	sechdrs[me->arch.core_plt_section].sh_size
118		= get_plt_size(hdr, sechdrs, secstrings, 0);
119	sechdrs[me->arch.init_plt_section].sh_size
120		= get_plt_size(hdr, sechdrs, secstrings, 1);
121	return 0;
122}
123
124int apply_relocate (Elf32_Shdr *sechdrs, const char *strtab,
125		    unsigned int symindex, unsigned int relsec,
126		    struct module *mod)
127{
128	printk ("Barf\n");
129	return -ENOEXEC;
130}
131
132/* Set up a trampoline in the PLT to bounce us to the distant function */
133static uint32_t do_plt_call (void *location, Elf32_Addr val,
134			     Elf32_Shdr *sechdrs, struct module *mod)
135{
136	struct v850_plt_entry *entry;
137	/* Instructions used to do the indirect jump.  */
138	uint32_t tramp[2];
139
140	/* We have to trash a register, so we assume that any control
141	   transfer more than 21-bits away must be a function call
142	   (so we can use a call-clobbered register).  */
143	tramp[0] = 0x0621 + ((val & 0xffff) << 16);   /* mov sym, r1 ... */
144	tramp[1] = ((val >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
145
146	/* Init, or core PLT? */
147	if (location >= mod->module_core
148	    && location < mod->module_core + mod->core_size)
149		entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
150	else
151		entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
152
153	/* Find this entry, or if that fails, the next avail. entry */
154	while (entry->tramp[0])
155		if (entry->tramp[0] == tramp[0] && entry->tramp[1] == tramp[1])
156			return (uint32_t)entry;
157		else
158			entry++;
159
160	entry->tramp[0] = tramp[0];
161	entry->tramp[1] = tramp[1];
162
163	return (uint32_t)entry;
164}
165
166int apply_relocate_add (Elf32_Shdr *sechdrs, const char *strtab,
167			unsigned int symindex, unsigned int relsec,
168			struct module *mod)
169{
170	unsigned int i;
171	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
172
173	DEBUGP ("Applying relocate section %u to %u\n", relsec,
174		sechdrs[relsec].sh_info);
175
176	for (i = 0; i < sechdrs[relsec].sh_size / sizeof (*rela); i++) {
177		/* This is where to make the change */
178		uint32_t *loc
179			= ((void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
180			   + rela[i].r_offset);
181		/* This is the symbol it is referring to.  Note that all
182		   undefined symbols have been resolved.  */
183		Elf32_Sym *sym
184			= ((Elf32_Sym *)sechdrs[symindex].sh_addr
185			   + ELF32_R_SYM (rela[i].r_info));
186		uint32_t val = sym->st_value + rela[i].r_addend;
187
188		switch (ELF32_R_TYPE (rela[i].r_info)) {
189		case R_V850_32:
190			/* We write two shorts instead of a long because even
191			   32-bit insns only need half-word alignment, but
192			   32-bit data writes need to be long-word aligned.  */
193			val += ((uint16_t *)loc)[0];
194			val += ((uint16_t *)loc)[1] << 16;
195			((uint16_t *)loc)[0] = val & 0xffff;
196			((uint16_t *)loc)[1] = (val >> 16) & 0xffff;
197			break;
198
199		case R_V850_22_PCREL:
200			/* Maybe jump indirectly via a PLT table entry.  */
201			if ((int32_t)(val - (uint32_t)loc) > 0x1fffff
202			    || (int32_t)(val - (uint32_t)loc) < -0x200000)
203				val = do_plt_call (loc, val, sechdrs, mod);
204
205			val -= (uint32_t)loc;
206
207			/* We write two shorts instead of a long because
208			   even 32-bit insns only need half-word alignment,
209			   but 32-bit data writes need to be long-word
210			   aligned.  */
211			((uint16_t *)loc)[0] =
212				(*(uint16_t *)loc & 0xffc0) /* opcode + reg */
213				| ((val >> 16) & 0xffc03f); /* offs high */
214			((uint16_t *)loc)[1] =
215				(val & 0xffff);		    /* offs low */
216			break;
217
218		default:
219			printk (KERN_ERR "module %s: Unknown reloc: %u\n",
220				mod->name, ELF32_R_TYPE (rela[i].r_info));
221			return -ENOEXEC;
222		}
223	}
224
225	return 0;
226}
227
228void
229module_arch_cleanup(struct module *mod)
230{
231}
232