1 /* Kernel module help for PPC.
2 Copyright (C) 2001 Rusty Russell.
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.
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/moduleloader.h>
23 #include <linux/elf.h>
24 #include <linux/vmalloc.h>
26 #include <linux/string.h>
27 #include <linux/kernel.h>
28 #include <linux/ftrace.h>
29 #include <linux/cache.h>
30 #include <linux/bug.h>
31 #include <linux/sort.h>
32 #include <asm/setup.h>
34 /* Count how many different relocations (different symbol, different
36 static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
38 unsigned int i, r_info, r_addend, _count_relocs;
43 for (i = 0; i < num; i++)
44 /* Only count 24-bit relocs, others don't need stubs */
45 if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
46 (r_info != ELF32_R_SYM(rela[i].r_info) ||
47 r_addend != rela[i].r_addend)) {
49 r_info = ELF32_R_SYM(rela[i].r_info);
50 r_addend = rela[i].r_addend;
53 #ifdef CONFIG_DYNAMIC_FTRACE
54 _count_relocs++; /* add one for ftrace_caller */
59 static int relacmp(const void *_x, const void *_y)
61 const Elf32_Rela *x, *y;
66 /* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
67 * make the comparison cheaper/faster. It won't affect the sorting or
68 * the counting algorithms' performance
70 if (x->r_info < y->r_info)
72 else if (x->r_info > y->r_info)
74 else if (x->r_addend < y->r_addend)
76 else if (x->r_addend > y->r_addend)
82 static void relaswap(void *_x, void *_y, int size)
90 for (i = 0; i < sizeof(Elf32_Rela) / sizeof(uint32_t); i++) {
97 /* Get the potential trampolines size required of the init and
99 static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
100 const Elf32_Shdr *sechdrs,
101 const char *secstrings,
104 unsigned long ret = 0;
107 /* Everything marked ALLOC (this includes the exported
109 for (i = 1; i < hdr->e_shnum; i++) {
110 /* If it's called *.init*, and we're not init, we're
112 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
116 /* We don't want to look at debug sections. */
117 if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
120 if (sechdrs[i].sh_type == SHT_RELA) {
121 pr_debug("Found relocations in section %u\n", i);
122 pr_debug("Ptr: %p. Number: %u\n",
123 (void *)hdr + sechdrs[i].sh_offset,
124 sechdrs[i].sh_size / sizeof(Elf32_Rela));
126 /* Sort the relocation information based on a symbol and
127 * addend key. This is a stable O(n*log n) complexity
128 * alogrithm but it will reduce the complexity of
129 * count_relocs() to linear complexity O(n)
131 sort((void *)hdr + sechdrs[i].sh_offset,
132 sechdrs[i].sh_size / sizeof(Elf32_Rela),
133 sizeof(Elf32_Rela), relacmp, relaswap);
135 ret += count_relocs((void *)hdr
136 + sechdrs[i].sh_offset,
138 / sizeof(Elf32_Rela))
139 * sizeof(struct ppc_plt_entry);
146 int module_frob_arch_sections(Elf32_Ehdr *hdr,
153 /* Find .plt and .init.plt sections */
154 for (i = 0; i < hdr->e_shnum; i++) {
155 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
156 me->arch.init_plt_section = i;
157 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
158 me->arch.core_plt_section = i;
160 if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
161 pr_err("Module doesn't contain .plt or .init.plt sections.\n");
165 /* Override their sizes */
166 sechdrs[me->arch.core_plt_section].sh_size
167 = get_plt_size(hdr, sechdrs, secstrings, 0);
168 sechdrs[me->arch.init_plt_section].sh_size
169 = get_plt_size(hdr, sechdrs, secstrings, 1);
173 static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
175 if (entry->jump[0] == 0x3d800000 + ((val + 0x8000) >> 16)
176 && entry->jump[1] == 0x398c0000 + (val & 0xffff))
181 /* Set up a trampoline in the PLT to bounce us to the distant function */
182 static uint32_t do_plt_call(void *location,
187 struct ppc_plt_entry *entry;
189 pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
190 /* Init, or core PLT? */
191 if (location >= mod->module_core
192 && location < mod->module_core + mod->core_size)
193 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
195 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
197 /* Find this entry, or if that fails, the next avail. entry */
198 while (entry->jump[0]) {
199 if (entry_matches(entry, val)) return (uint32_t)entry;
203 entry->jump[0] = 0x3d800000+((val+0x8000)>>16); /* lis r12,sym@ha */
204 entry->jump[1] = 0x398c0000 + (val&0xffff); /* addi r12,r12,sym@l*/
205 entry->jump[2] = 0x7d8903a6; /* mtctr r12 */
206 entry->jump[3] = 0x4e800420; /* bctr */
208 pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
209 return (uint32_t)entry;
212 int apply_relocate_add(Elf32_Shdr *sechdrs,
214 unsigned int symindex,
216 struct module *module)
219 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
224 pr_debug("Applying ADD relocate section %u to %u\n", relsec,
225 sechdrs[relsec].sh_info);
226 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
227 /* This is where to make the change */
228 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
230 /* This is the symbol it is referring to. Note that all
231 undefined symbols have been resolved. */
232 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
233 + ELF32_R_SYM(rela[i].r_info);
234 /* `Everything is relative'. */
235 value = sym->st_value + rela[i].r_addend;
237 switch (ELF32_R_TYPE(rela[i].r_info)) {
240 *(uint32_t *)location = value;
243 case R_PPC_ADDR16_LO:
244 /* Low half of the symbol */
245 *(uint16_t *)location = value;
248 case R_PPC_ADDR16_HI:
249 /* Higher half of the symbol */
250 *(uint16_t *)location = (value >> 16);
253 case R_PPC_ADDR16_HA:
254 /* Sign-adjusted lower 16 bits: PPC ELF ABI says:
255 (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
256 This is the same, only sane.
258 *(uint16_t *)location = (value + 0x8000) >> 16;
262 if ((int)(value - (uint32_t)location) < -0x02000000
263 || (int)(value - (uint32_t)location) >= 0x02000000)
264 value = do_plt_call(location, value,
267 /* Only replace bits 2 through 26 */
268 pr_debug("REL24 value = %08X. location = %08X\n",
269 value, (uint32_t)location);
270 pr_debug("Location before: %08X.\n",
271 *(uint32_t *)location);
272 *(uint32_t *)location
273 = (*(uint32_t *)location & ~0x03fffffc)
274 | ((value - (uint32_t)location)
276 pr_debug("Location after: %08X.\n",
277 *(uint32_t *)location);
278 pr_debug("ie. jump to %08X+%08X = %08X\n",
279 *(uint32_t *)location & 0x03fffffc,
281 (*(uint32_t *)location & 0x03fffffc)
282 + (uint32_t)location);
286 /* 32-bit relative jump. */
287 *(uint32_t *)location = value - (uint32_t)location;
291 pr_err("%s: unknown ADD relocation: %u\n",
293 ELF32_R_TYPE(rela[i].r_info));
297 #ifdef CONFIG_DYNAMIC_FTRACE
299 do_plt_call(module->module_core,
300 (unsigned long)ftrace_caller,