Commit | Line | Data |
---|---|---|
b920de1b DH |
1 | /* MN10300 Kernel module helper routines |
2 | * | |
1122b19b | 3 | * Copyright (C) 2007, 2008 Red Hat, Inc. All Rights Reserved. |
b920de1b DH |
4 | * Written by Mark Salter (msalter@redhat.com) |
5 | * - Derived from arch/i386/kernel/module.c | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | */ | |
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> | |
18f6db95 | 27 | #include <linux/bug.h> |
b920de1b DH |
28 | |
29 | #if 0 | |
30 | #define DEBUGP printk | |
31 | #else | |
32 | #define DEBUGP(fmt, ...) | |
33 | #endif | |
34 | ||
35 | /* | |
36 | * allocate storage for a module | |
37 | */ | |
38 | void *module_alloc(unsigned long size) | |
39 | { | |
40 | if (size == 0) | |
41 | return NULL; | |
42 | return vmalloc_exec(size); | |
43 | } | |
44 | ||
45 | /* | |
46 | * free memory returned from module_alloc() | |
47 | */ | |
48 | void module_free(struct module *mod, void *module_region) | |
49 | { | |
50 | vfree(module_region); | |
b920de1b DH |
51 | } |
52 | ||
53 | /* | |
54 | * allow the arch to fix up the section table | |
55 | * - we don't need anything special | |
56 | */ | |
57 | int module_frob_arch_sections(Elf_Ehdr *hdr, | |
58 | Elf_Shdr *sechdrs, | |
59 | char *secstrings, | |
60 | struct module *mod) | |
61 | { | |
62 | return 0; | |
63 | } | |
64 | ||
b920de1b DH |
65 | static void reloc_put16(uint8_t *p, uint32_t val) |
66 | { | |
67 | p[0] = val & 0xff; | |
68 | p[1] = (val >> 8) & 0xff; | |
69 | } | |
70 | ||
71 | static void reloc_put24(uint8_t *p, uint32_t val) | |
72 | { | |
73 | reloc_put16(p, val); | |
74 | p[2] = (val >> 16) & 0xff; | |
75 | } | |
76 | ||
77 | static void reloc_put32(uint8_t *p, uint32_t val) | |
78 | { | |
79 | reloc_put16(p, val); | |
80 | reloc_put16(p+2, val >> 16); | |
81 | } | |
82 | ||
83 | /* | |
84 | * apply a REL relocation | |
85 | */ | |
86 | int apply_relocate(Elf32_Shdr *sechdrs, | |
87 | const char *strtab, | |
88 | unsigned int symindex, | |
89 | unsigned int relsec, | |
90 | struct module *me) | |
91 | { | |
92 | printk(KERN_ERR "module %s: RELOCATION unsupported\n", | |
93 | me->name); | |
94 | return -ENOEXEC; | |
95 | } | |
96 | ||
97 | /* | |
98 | * apply a RELA relocation | |
99 | */ | |
100 | int apply_relocate_add(Elf32_Shdr *sechdrs, | |
101 | const char *strtab, | |
102 | unsigned int symindex, | |
103 | unsigned int relsec, | |
104 | struct module *me) | |
105 | { | |
106 | unsigned int i; | |
107 | Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; | |
108 | Elf32_Sym *sym; | |
109 | Elf32_Addr relocation; | |
110 | uint8_t *location; | |
111 | uint32_t value; | |
112 | ||
113 | DEBUGP("Applying relocate section %u to %u\n", | |
114 | relsec, sechdrs[relsec].sh_info); | |
115 | ||
116 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { | |
117 | /* this is where to make the change */ | |
118 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr | |
119 | + rel[i].r_offset; | |
120 | ||
121 | /* this is the symbol the relocation is referring to (note that | |
122 | * all undefined symbols have been resolved by the caller) */ | |
123 | sym = (Elf32_Sym *)sechdrs[symindex].sh_addr | |
124 | + ELF32_R_SYM(rel[i].r_info); | |
125 | ||
126 | /* this is the adjustment to be made */ | |
127 | relocation = sym->st_value + rel[i].r_addend; | |
128 | ||
129 | switch (ELF32_R_TYPE(rel[i].r_info)) { | |
1122b19b MS |
130 | /* for the first four relocation types, we simply |
131 | * store the adjustment at the location given */ | |
b920de1b | 132 | case R_MN10300_32: |
1122b19b | 133 | reloc_put32(location, relocation); |
b920de1b DH |
134 | break; |
135 | case R_MN10300_24: | |
1122b19b | 136 | reloc_put24(location, relocation); |
b920de1b DH |
137 | break; |
138 | case R_MN10300_16: | |
1122b19b | 139 | reloc_put16(location, relocation); |
b920de1b DH |
140 | break; |
141 | case R_MN10300_8: | |
1122b19b | 142 | *location = relocation; |
b920de1b DH |
143 | break; |
144 | ||
145 | /* for the next three relocation types, we write the | |
146 | * adjustment with the address subtracted over the | |
147 | * value at the location given */ | |
148 | case R_MN10300_PCREL32: | |
149 | value = relocation - (uint32_t) location; | |
150 | reloc_put32(location, value); | |
151 | break; | |
152 | case R_MN10300_PCREL16: | |
153 | value = relocation - (uint32_t) location; | |
154 | reloc_put16(location, value); | |
155 | break; | |
156 | case R_MN10300_PCREL8: | |
157 | *location = relocation - (uint32_t) location; | |
158 | break; | |
159 | ||
160 | default: | |
161 | printk(KERN_ERR "module %s: Unknown relocation: %u\n", | |
162 | me->name, ELF32_R_TYPE(rel[i].r_info)); | |
163 | return -ENOEXEC; | |
164 | } | |
165 | } | |
166 | return 0; | |
167 | } | |
168 | ||
169 | /* | |
170 | * finish loading the module | |
171 | */ | |
172 | int module_finalize(const Elf_Ehdr *hdr, | |
173 | const Elf_Shdr *sechdrs, | |
174 | struct module *me) | |
175 | { | |
18f6db95 | 176 | return module_bug_finalize(hdr, sechdrs, me); |
b920de1b DH |
177 | } |
178 | ||
179 | /* | |
180 | * finish clearing the module | |
181 | */ | |
182 | void module_arch_cleanup(struct module *mod) | |
183 | { | |
18f6db95 | 184 | module_bug_cleanup(mod); |
b920de1b | 185 | } |