1 /* MN10300 Kernel module helper routines
3 * Copyright (C) 2007, 2008, 2009 Red Hat, Inc. All Rights Reserved.
4 * Written by Mark Salter (msalter@redhat.com)
5 * - Derived from arch/i386/kernel/module.c
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.
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.
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
21 #include <linux/moduleloader.h>
22 #include <linux/elf.h>
23 #include <linux/vmalloc.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/bug.h>
32 #define DEBUGP(fmt, ...)
35 static void reloc_put16(uint8_t *p
, uint32_t val
)
38 p
[1] = (val
>> 8) & 0xff;
41 static void reloc_put24(uint8_t *p
, uint32_t val
)
44 p
[2] = (val
>> 16) & 0xff;
47 static void reloc_put32(uint8_t *p
, uint32_t val
)
50 reloc_put16(p
+2, val
>> 16);
54 * apply a RELA relocation
56 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
58 unsigned int symindex
,
62 unsigned int i
, sym_diff_seen
= 0;
63 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
65 Elf32_Addr relocation
, sym_diff_val
= 0;
69 DEBUGP("Applying relocate section %u to %u\n",
70 relsec
, sechdrs
[relsec
].sh_info
);
72 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
73 /* this is where to make the change */
74 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
77 /* this is the symbol the relocation is referring to (note that
78 * all undefined symbols have been resolved by the caller) */
79 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
80 + ELF32_R_SYM(rel
[i
].r_info
);
82 /* this is the adjustment to be made */
83 relocation
= sym
->st_value
+ rel
[i
].r_addend
;
86 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
91 relocation
-= sym_diff_val
;
95 printk(KERN_ERR
"module %s: Unexpected SYM_DIFF relocation: %u\n",
96 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
101 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
102 /* for the first four relocation types, we simply
103 * store the adjustment at the location given */
105 reloc_put32(location
, relocation
);
108 reloc_put24(location
, relocation
);
111 reloc_put16(location
, relocation
);
114 *location
= relocation
;
117 /* for the next three relocation types, we write the
118 * adjustment with the address subtracted over the
119 * value at the location given */
120 case R_MN10300_PCREL32
:
121 value
= relocation
- (uint32_t) location
;
122 reloc_put32(location
, value
);
124 case R_MN10300_PCREL16
:
125 value
= relocation
- (uint32_t) location
;
126 reloc_put16(location
, value
);
128 case R_MN10300_PCREL8
:
129 *location
= relocation
- (uint32_t) location
;
132 case R_MN10300_SYM_DIFF
:
133 /* This is used to adjust the next reloc as required
136 sym_diff_val
= sym
->st_value
;
139 case R_MN10300_ALIGN
:
140 /* Just ignore the ALIGN relocs.
141 * Only interesting if kernel performed relaxation. */
145 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
146 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
151 printk(KERN_ERR
"module %s: Nothing follows SYM_DIFF relocation: %u\n",
152 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));