asm-generic: make get_rtc_time overridable
[linux/fpc-iii.git] / arch / mn10300 / kernel / module.c
blob6b287f2e8e843f885c9a856df8527f862e9147a1
1 /* MN10300 Kernel module helper routines
3 * Copyright (C) 2007, 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by Mark Salter (msalter@redhat.com)
5 * - Derived from arch/i386/kernel/module.c
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public Licence as published by
9 * the Free Software Foundation; either version 2 of the Licence, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public Licence for more details.
17 * You should have received a copy of the GNU General Public Licence
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/moduleloader.h>
22 #include <linux/elf.h>
23 #include <linux/vmalloc.h>
24 #include <linux/fs.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/bug.h>
29 #if 0
30 #define DEBUGP printk
31 #else
32 #define DEBUGP(fmt, ...)
33 #endif
36 * allocate storage for a module
38 void *module_alloc(unsigned long size)
40 if (size == 0)
41 return NULL;
42 return vmalloc_exec(size);
46 * free memory returned from module_alloc()
48 void module_free(struct module *mod, void *module_region)
50 vfree(module_region);
51 /* FIXME: If module_region == mod->init_region, trim exception
52 * table entries. */
56 * allow the arch to fix up the section table
57 * - we don't need anything special
59 int module_frob_arch_sections(Elf_Ehdr *hdr,
60 Elf_Shdr *sechdrs,
61 char *secstrings,
62 struct module *mod)
64 return 0;
67 static void reloc_put16(uint8_t *p, uint32_t val)
69 p[0] = val & 0xff;
70 p[1] = (val >> 8) & 0xff;
73 static void reloc_put24(uint8_t *p, uint32_t val)
75 reloc_put16(p, val);
76 p[2] = (val >> 16) & 0xff;
79 static void reloc_put32(uint8_t *p, uint32_t val)
81 reloc_put16(p, val);
82 reloc_put16(p+2, val >> 16);
86 * apply a REL relocation
88 int apply_relocate(Elf32_Shdr *sechdrs,
89 const char *strtab,
90 unsigned int symindex,
91 unsigned int relsec,
92 struct module *me)
94 printk(KERN_ERR "module %s: RELOCATION unsupported\n",
95 me->name);
96 return -ENOEXEC;
100 * apply a RELA relocation
102 int apply_relocate_add(Elf32_Shdr *sechdrs,
103 const char *strtab,
104 unsigned int symindex,
105 unsigned int relsec,
106 struct module *me)
108 unsigned int i;
109 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
110 Elf32_Sym *sym;
111 Elf32_Addr relocation;
112 uint8_t *location;
113 uint32_t value;
115 DEBUGP("Applying relocate section %u to %u\n",
116 relsec, sechdrs[relsec].sh_info);
118 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
119 /* this is where to make the change */
120 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
121 + rel[i].r_offset;
123 /* this is the symbol the relocation is referring to (note that
124 * all undefined symbols have been resolved by the caller) */
125 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
126 + ELF32_R_SYM(rel[i].r_info);
128 /* this is the adjustment to be made */
129 relocation = sym->st_value + rel[i].r_addend;
131 switch (ELF32_R_TYPE(rel[i].r_info)) {
132 /* for the first four relocation types, we simply
133 * store the adjustment at the location given */
134 case R_MN10300_32:
135 reloc_put32(location, relocation);
136 break;
137 case R_MN10300_24:
138 reloc_put24(location, relocation);
139 break;
140 case R_MN10300_16:
141 reloc_put16(location, relocation);
142 break;
143 case R_MN10300_8:
144 *location = relocation;
145 break;
147 /* for the next three relocation types, we write the
148 * adjustment with the address subtracted over the
149 * value at the location given */
150 case R_MN10300_PCREL32:
151 value = relocation - (uint32_t) location;
152 reloc_put32(location, value);
153 break;
154 case R_MN10300_PCREL16:
155 value = relocation - (uint32_t) location;
156 reloc_put16(location, value);
157 break;
158 case R_MN10300_PCREL8:
159 *location = relocation - (uint32_t) location;
160 break;
162 default:
163 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
164 me->name, ELF32_R_TYPE(rel[i].r_info));
165 return -ENOEXEC;
168 return 0;
172 * finish loading the module
174 int module_finalize(const Elf_Ehdr *hdr,
175 const Elf_Shdr *sechdrs,
176 struct module *me)
178 return module_bug_finalize(hdr, sechdrs, me);
182 * finish clearing the module
184 void module_arch_cleanup(struct module *mod)
186 module_bug_cleanup(mod);