2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * Copyright (C) 2001 Rusty Russell.
17 * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
22 #include <linux/moduleloader.h>
23 #include <linux/elf.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/kernel.h>
31 struct mips_hi16 *next;
36 static struct mips_hi16 *mips_hi16_list;
38 void *module_alloc(unsigned long size)
46 /* Free memory returned from module_alloc */
47 void module_free(struct module *mod, void *module_region)
50 /* FIXME: If module_region == mod->init_region, trim exception
54 int module_frob_arch_sections(Elf_Ehdr *hdr,
62 static int apply_r_mips_none(struct module *me, uint32_t *location,
68 static int apply_r_mips_32(struct module *me, uint32_t *location,
76 static int apply_r_mips_26(struct module *me, uint32_t *location,
80 printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
84 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
86 "module %s: relocation overflow\n",
91 *location = (*location & ~0x03ffffff) |
92 ((*location + (v >> 2)) & 0x03ffffff);
97 static int apply_r_mips_hi16(struct module *me, uint32_t *location,
103 * We cannot relocate this one now because we don't know the value of
104 * the carry we need to add. Save the information, and let LO16 do the
107 n = kmalloc(sizeof *n, GFP_KERNEL);
113 n->next = mips_hi16_list;
119 static int apply_r_mips_lo16(struct module *me, uint32_t *location,
122 unsigned long insnlo = *location;
123 Elf32_Addr val, vallo;
125 /* Sign extend the addend we extract from the lo insn. */
126 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
128 if (mips_hi16_list != NULL) {
133 struct mips_hi16 *next;
137 * The value for the HI16 had best be the same.
143 * Do the HI16 relocation. Note that we actually don't
144 * need to know anything about the LO16 itself, except
145 * where to find the low 16 bits of the addend needed
149 val = ((insn & 0xffff) << 16) + vallo;
153 * Account for the sign extension that will happen in
156 val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
158 insn = (insn & ~0xffff) | val;
166 mips_hi16_list = NULL;
170 * Ok, we're done with the HI16 relocs. Now deal with the LO16.
173 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
179 printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
184 static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
186 [R_MIPS_NONE] = apply_r_mips_none,
187 [R_MIPS_32] = apply_r_mips_32,
188 [R_MIPS_26] = apply_r_mips_26,
189 [R_MIPS_HI16] = apply_r_mips_hi16,
190 [R_MIPS_LO16] = apply_r_mips_lo16
193 int apply_relocate(Elf32_Shdr *sechdrs,
195 unsigned int symindex,
199 Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
206 pr_debug("Applying relocate section %u to %u\n", relsec,
207 sechdrs[relsec].sh_info);
209 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
210 Elf32_Word r_info = rel[i].r_info;
212 /* This is where to make the change */
213 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
215 /* This is the symbol it is referring to */
216 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
217 + ELF32_R_SYM(r_info);
218 if (!sym->st_value) {
219 printk(KERN_WARNING "%s: Unknown symbol %s\n",
220 me->name, strtab + sym->st_name);
226 res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
234 int apply_relocate_add(Elf32_Shdr *sechdrs,
236 unsigned int symindex,
241 * Current binutils always generate .rela relocations. Keep smiling
242 * if it's empty, abort otherwise.
244 if (!sechdrs[relsec].sh_size)
247 printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",