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
18 #include <linux/module.h>
19 #include <linux/moduleloader.h>
20 #include <linux/elf.h>
21 #include <linux/vmalloc.h>
23 #include <linux/string.h>
24 #include <linux/kernel.h>
25 #include <linux/ftrace.h>
26 #include <linux/cache.h>
27 #include <linux/bug.h>
28 #include <linux/sort.h>
29 #include <asm/setup.h>
34 #define DEBUGP(fmt , ...)
37 /* Count how many different relocations (different symbol, different
39 static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
41 unsigned int i, r_info, r_addend, _count_relocs;
46 for (i = 0; i < num; i++)
47 /* Only count 24-bit relocs, others don't need stubs */
48 if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
49 (r_info != ELF32_R_SYM(rela[i].r_info) ||
50 r_addend != rela[i].r_addend)) {
52 r_info = ELF32_R_SYM(rela[i].r_info);
53 r_addend = rela[i].r_addend;
56 #ifdef CONFIG_DYNAMIC_FTRACE
57 _count_relocs++; /* add one for ftrace_caller */
62 static int relacmp(const void *_x, const void *_y)
64 const Elf32_Rela *x, *y;
69 /* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
70 * make the comparison cheaper/faster. It won't affect the sorting or
71 * the counting algorithms' performance
73 if (x->r_info < y->r_info)
75 else if (x->r_info > y->r_info)
77 else if (x->r_addend < y->r_addend)
79 else if (x->r_addend > y->r_addend)
85 static void relaswap(void *_x, void *_y, int size)
93 for (i = 0; i < sizeof(Elf32_Rela) / sizeof(uint32_t); i++) {
100 /* Get the potential trampolines size required of the init and
102 static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
103 const Elf32_Shdr *sechdrs,
104 const char *secstrings,
107 unsigned long ret = 0;
110 /* Everything marked ALLOC (this includes the exported
112 for (i = 1; i < hdr->e_shnum; i++) {
113 /* If it's called *.init*, and we're not init, we're
115 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
119 /* We don't want to look at debug sections. */
120 if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
123 if (sechdrs[i].sh_type == SHT_RELA) {
124 DEBUGP("Found relocations in section %u\n", i);
125 DEBUGP("Ptr: %p. Number: %u\n",
126 (void *)hdr + sechdrs[i].sh_offset,
127 sechdrs[i].sh_size / sizeof(Elf32_Rela));
129 /* Sort the relocation information based on a symbol and
130 * addend key. This is a stable O(n*log n) complexity
131 * alogrithm but it will reduce the complexity of
132 * count_relocs() to linear complexity O(n)
134 sort((void *)hdr + sechdrs[i].sh_offset,
135 sechdrs[i].sh_size / sizeof(Elf32_Rela),
136 sizeof(Elf32_Rela), relacmp, relaswap);
138 ret += count_relocs((void *)hdr
139 + sechdrs[i].sh_offset,
141 / sizeof(Elf32_Rela))
142 * sizeof(struct ppc_plt_entry);
149 int module_frob_arch_sections(Elf32_Ehdr *hdr,
156 /* Find .plt and .init.plt sections */
157 for (i = 0; i < hdr->e_shnum; i++) {
158 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
159 me->arch.init_plt_section = i;
160 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
161 me->arch.core_plt_section = i;
163 if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
164 printk("Module doesn't contain .plt or .init.plt sections.\n");
168 /* Override their sizes */
169 sechdrs[me->arch.core_plt_section].sh_size
170 = get_plt_size(hdr, sechdrs, secstrings, 0);
171 sechdrs[me->arch.init_plt_section].sh_size
172 = get_plt_size(hdr, sechdrs, secstrings, 1);
176 static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
178 if (entry->jump[0] == 0x3d800000 + ((val + 0x8000) >> 16)
179 && entry->jump[1] == 0x398c0000 + (val & 0xffff))
184 /* Set up a trampoline in the PLT to bounce us to the distant function */
185 static uint32_t do_plt_call(void *location,
190 struct ppc_plt_entry *entry;
192 DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
193 /* Init, or core PLT? */
194 if (location >= mod->module_core
195 && location < mod->module_core + mod->core_size)
196 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
198 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
200 /* Find this entry, or if that fails, the next avail. entry */
201 while (entry->jump[0]) {
202 if (entry_matches(entry, val)) return (uint32_t)entry;
206 entry->jump[0] = 0x3d800000+((val+0x8000)>>16); /* lis r12,sym@ha */
207 entry->jump[1] = 0x398c0000 + (val&0xffff); /* addi r12,r12,sym@l*/
208 entry->jump[2] = 0x7d8903a6; /* mtctr r12 */
209 entry->jump[3] = 0x4e800420; /* bctr */
211 DEBUGP("Initialized plt for 0x%x at %p\n", val, entry);
212 return (uint32_t)entry;
215 int apply_relocate_add(Elf32_Shdr *sechdrs,
217 unsigned int symindex,
219 struct module *module)
222 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
227 DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
228 sechdrs[relsec].sh_info);
229 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
230 /* This is where to make the change */
231 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
233 /* This is the symbol it is referring to. Note that all
234 undefined symbols have been resolved. */
235 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
236 + ELF32_R_SYM(rela[i].r_info);
237 /* `Everything is relative'. */
238 value = sym->st_value + rela[i].r_addend;
240 switch (ELF32_R_TYPE(rela[i].r_info)) {
243 *(uint32_t *)location = value;
246 case R_PPC_ADDR16_LO:
247 /* Low half of the symbol */
248 *(uint16_t *)location = value;
251 case R_PPC_ADDR16_HI:
252 /* Higher half of the symbol */
253 *(uint16_t *)location = (value >> 16);
256 case R_PPC_ADDR16_HA:
257 /* Sign-adjusted lower 16 bits: PPC ELF ABI says:
258 (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
259 This is the same, only sane.
261 *(uint16_t *)location = (value + 0x8000) >> 16;
265 if ((int)(value - (uint32_t)location) < -0x02000000
266 || (int)(value - (uint32_t)location) >= 0x02000000)
267 value = do_plt_call(location, value,
270 /* Only replace bits 2 through 26 */
271 DEBUGP("REL24 value = %08X. location = %08X\n",
272 value, (uint32_t)location);
273 DEBUGP("Location before: %08X.\n",
274 *(uint32_t *)location);
275 *(uint32_t *)location
276 = (*(uint32_t *)location & ~0x03fffffc)
277 | ((value - (uint32_t)location)
279 DEBUGP("Location after: %08X.\n",
280 *(uint32_t *)location);
281 DEBUGP("ie. jump to %08X+%08X = %08X\n",
282 *(uint32_t *)location & 0x03fffffc,
284 (*(uint32_t *)location & 0x03fffffc)
285 + (uint32_t)location);
289 /* 32-bit relative jump. */
290 *(uint32_t *)location = value - (uint32_t)location;
294 printk("%s: unknown ADD relocation: %u\n",
296 ELF32_R_TYPE(rela[i].r_info));
300 #ifdef CONFIG_DYNAMIC_FTRACE
302 do_plt_call(module->module_core,
303 (unsigned long)ftrace_caller,