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 int apply_relocate(Elf64_Shdr
*sechdrs
,
64 unsigned int symindex
,
69 * We don't want to deal with REL relocations - RELA is so much saner.
71 if (!sechdrs
[relsec
].sh_size
)
74 printk(KERN_ERR
"module %s: REL relocation unsupported\n",
79 static int apply_r_mips_none(struct module
*me
, uint32_t *location
,
85 static int apply_r_mips_32(struct module
*me
, uint32_t *location
,
93 static int apply_r_mips_26(struct module
*me
, uint32_t *location
,
97 printk(KERN_ERR
"module %s: dangerous relocation\n", me
->name
);
101 if ((v
& 0xf0000000) != (((unsigned long)location
+ 4) & 0xf0000000)) {
103 "module %s: relocation overflow\n",
108 *location
= (*location
& ~0x03ffffff) | ((v
>> 2) & 0x03ffffff);
113 static int apply_r_mips_hi16(struct module
*me
, uint32_t *location
,
119 * We cannot relocate this one now because we don't know the value of
120 * the carry we need to add. Save the information, and let LO16 do the
123 n
= kmalloc(sizeof *n
, GFP_KERNEL
);
129 n
->next
= mips_hi16_list
;
135 static int apply_r_mips_lo16(struct module
*me
, uint32_t *location
,
138 unsigned long insnlo
= *location
;
139 Elf32_Addr val
, vallo
;
141 /* Sign extend the addend we extract from the lo insn. */
142 vallo
= ((insnlo
& 0xffff) ^ 0x8000) - 0x8000;
144 if (mips_hi16_list
!= NULL
) {
149 struct mips_hi16
*next
;
153 * The value for the HI16 had best be the same.
159 * Do the HI16 relocation. Note that we actually don't
160 * need to know anything about the LO16 itself, except
161 * where to find the low 16 bits of the addend needed
165 val
= ((insn
& 0xffff) << 16) + vallo
;
169 * Account for the sign extension that will happen in
172 val
= ((val
>> 16) + ((val
& 0x8000) != 0)) & 0xffff;
174 insn
= (insn
& ~0xffff) | val
;
182 mips_hi16_list
= NULL
;
186 * Ok, we're done with the HI16 relocs. Now deal with the LO16.
188 insnlo
= (insnlo
& ~0xffff) | (v
& 0xffff);
194 printk(KERN_ERR
"module %s: dangerous " "relocation\n", me
->name
);
199 static int apply_r_mips_64(struct module
*me
, uint32_t *location
,
202 *(uint64_t *) location
= v
;
208 static int apply_r_mips_higher(struct module
*me
, uint32_t *location
,
211 *location
= (*location
& 0xffff0000) |
212 ((((long long) v
+ 0x80008000LL
) >> 32) & 0xffff);
217 static int apply_r_mips_highest(struct module
*me
, uint32_t *location
,
220 *location
= (*location
& 0xffff0000) |
221 ((((long long) v
+ 0x800080008000LL
) >> 48) & 0xffff);
226 static int (*reloc_handlers
[]) (struct module
*me
, uint32_t *location
,
228 [R_MIPS_NONE
] = apply_r_mips_none
,
229 [R_MIPS_32
] = apply_r_mips_32
,
230 [R_MIPS_26
] = apply_r_mips_26
,
231 [R_MIPS_HI16
] = apply_r_mips_hi16
,
232 [R_MIPS_LO16
] = apply_r_mips_lo16
,
233 [R_MIPS_64
] = apply_r_mips_64
,
234 [R_MIPS_HIGHER
] = apply_r_mips_higher
,
235 [R_MIPS_HIGHEST
] = apply_r_mips_highest
238 int apply_relocate_add(Elf64_Shdr
*sechdrs
,
240 unsigned int symindex
,
244 Elf64_Mips_Rela
*rel
= (void *) sechdrs
[relsec
].sh_addr
;
251 pr_debug("Applying relocate section %u to %u\n", relsec
,
252 sechdrs
[relsec
].sh_info
);
254 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
255 /* This is where to make the change */
256 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
258 /* This is the symbol it is referring to */
259 sym
= (Elf64_Sym
*)sechdrs
[symindex
].sh_addr
+ rel
[i
].r_sym
;
260 if (!sym
->st_value
) {
261 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
262 me
->name
, strtab
+ sym
->st_name
);
268 res
= reloc_handlers
[rel
[i
].r_type
](me
, location
, v
);