1 /* Kernel module help for M32R.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <linux/moduleloader.h>
19 #include <linux/elf.h>
20 #include <linux/vmalloc.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
28 #define DEBUGP(fmt...)
31 #define COPY_UNALIGNED_WORD(sw, tw, align) \
33 void *__s = &(sw), *__t = &(tw); \
34 unsigned short *__s2 = __s, *__t2 =__t; \
35 unsigned char *__s1 = __s, *__t1 =__t; \
39 *(unsigned long *) __t = *(unsigned long *) __s; \
54 #define COPY_UNALIGNED_HWORD(sw, tw, align) \
56 void *__s = &(sw), *__t = &(tw); \
57 unsigned short *__s2 = __s, *__t2 =__t; \
58 unsigned char *__s1 = __s, *__t1 =__t; \
71 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
73 unsigned int symindex
,
78 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
80 Elf32_Addr relocation
;
83 unsigned short *hlocation
;
84 unsigned short hvalue
;
88 DEBUGP("Applying relocate section %u to %u\n", relsec
,
89 sechdrs
[relsec
].sh_info
);
90 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
91 /* This is where to make the change */
92 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
94 /* This is the symbol it is referring to. Note that all
95 undefined symbols have been resolved. */
96 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
97 + ELF32_R_SYM(rel
[i
].r_info
);
98 relocation
= sym
->st_value
+ rel
[i
].r_addend
;
99 align
= (int)location
& 3;
101 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
103 COPY_UNALIGNED_WORD (*location
, value
, align
);
105 COPY_UNALIGNED_WORD (value
, *location
, align
);
107 case R_M32R_HI16_ULO_RELA
:
108 COPY_UNALIGNED_WORD (*location
, value
, align
);
109 relocation
= (relocation
>>16) & 0xffff;
110 /* RELA must has 0 at relocation field. */
112 COPY_UNALIGNED_WORD (value
, *location
, align
);
114 case R_M32R_HI16_SLO_RELA
:
115 COPY_UNALIGNED_WORD (*location
, value
, align
);
116 if (relocation
& 0x8000) relocation
+= 0x10000;
117 relocation
= (relocation
>>16) & 0xffff;
118 /* RELA must has 0 at relocation field. */
120 COPY_UNALIGNED_WORD (value
, *location
, align
);
123 hlocation
= (unsigned short *)location
;
124 relocation
= relocation
& 0xffff;
125 /* RELA must has 0 at relocation field. */
127 COPY_UNALIGNED_WORD (hvalue
, *hlocation
, align
);
129 case R_M32R_SDA16_RELA
:
130 case R_M32R_LO16_RELA
:
131 COPY_UNALIGNED_WORD (*location
, value
, align
);
132 relocation
= relocation
& 0xffff;
133 /* RELA must has 0 at relocation field. */
135 COPY_UNALIGNED_WORD (value
, *location
, align
);
138 COPY_UNALIGNED_WORD (*location
, value
, align
);
139 relocation
= relocation
& 0xffffff;
140 /* RELA must has 0 at relocation field. */
142 COPY_UNALIGNED_WORD (value
, *location
, align
);
144 case R_M32R_18_PCREL_RELA
:
145 relocation
= (relocation
- (Elf32_Addr
) location
);
146 if (relocation
< -0x20000 || 0x1fffc < relocation
)
148 printk(KERN_ERR
"module %s: relocation overflow: %u\n",
149 me
->name
, relocation
);
152 COPY_UNALIGNED_WORD (*location
, value
, align
);
155 /* RELA must has 0 at relocation field. */
156 printk(KERN_ERR
"module %s: illegal relocation field: %u\n",
160 relocation
= (relocation
>> 2) & 0xffff;
162 COPY_UNALIGNED_WORD (value
, *location
, align
);
164 case R_M32R_10_PCREL_RELA
:
165 hlocation
= (unsigned short *)location
;
166 relocation
= (relocation
- (Elf32_Addr
) location
);
167 COPY_UNALIGNED_HWORD (*hlocation
, hvalue
, align
);
168 svalue
= (int)hvalue
;
169 svalue
= (signed char)svalue
<< 2;
170 relocation
+= svalue
;
171 relocation
= (relocation
>> 2) & 0xff;
172 hvalue
= hvalue
& 0xff00;
173 hvalue
+= relocation
;
174 COPY_UNALIGNED_HWORD (hvalue
, *hlocation
, align
);
176 case R_M32R_26_PCREL_RELA
:
177 relocation
= (relocation
- (Elf32_Addr
) location
);
178 if (relocation
< -0x2000000 || 0x1fffffc < relocation
)
180 printk(KERN_ERR
"module %s: relocation overflow: %u\n",
181 me
->name
, relocation
);
184 COPY_UNALIGNED_WORD (*location
, value
, align
);
185 if (value
& 0xffffff)
187 /* RELA must has 0 at relocation field. */
188 printk(KERN_ERR
"module %s: illegal relocation field: %u\n",
192 relocation
= (relocation
>> 2) & 0xffffff;
194 COPY_UNALIGNED_WORD (value
, *location
, align
);
197 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
198 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
205 int apply_relocate(Elf32_Shdr
*sechdrs
,
207 unsigned int symindex
,
212 printk(KERN_ERR
"module %s: REL RELOCATION unsupported\n",