2 * linux/arch/arm/kernel/module.c
4 * Copyright (C) 2002 Russell King.
5 * Modified for nommu by Hyok S. Choi
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Module allocation method suggested by Andi Kleen.
13 #include <linux/module.h>
14 #include <linux/moduleloader.h>
15 #include <linux/kernel.h>
17 #include <linux/elf.h>
18 #include <linux/vmalloc.h>
20 #include <linux/string.h>
21 #include <linux/gfp.h>
23 #include <asm/pgtable.h>
24 #include <asm/sections.h>
25 #include <asm/unwind.h>
27 #ifdef CONFIG_XIP_KERNEL
29 * The XIP kernel text is mapped in the module area for modules and
30 * some other stuff to work without any indirect relocations.
31 * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
32 * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
35 #define MODULES_VADDR (((unsigned long)_etext + ~PGDIR_MASK) & PGDIR_MASK)
39 void *module_alloc(unsigned long size
)
41 struct vm_struct
*area
;
43 size
= PAGE_ALIGN(size
);
47 area
= __get_vm_area(size
, VM_ALLOC
, MODULES_VADDR
, MODULES_END
);
51 return __vmalloc_area(area
, GFP_KERNEL
, PAGE_KERNEL_EXEC
);
53 #else /* CONFIG_MMU */
54 void *module_alloc(unsigned long size
)
56 return size
== 0 ? NULL
: vmalloc(size
);
58 #endif /* !CONFIG_MMU */
60 void module_free(struct module
*module
, void *region
)
65 int module_frob_arch_sections(Elf_Ehdr
*hdr
,
70 #ifdef CONFIG_ARM_UNWIND
71 Elf_Shdr
*s
, *sechdrs_end
= sechdrs
+ hdr
->e_shnum
;
72 struct arm_unwind_mapping
*maps
= mod
->arch
.map
;
74 for (s
= sechdrs
; s
< sechdrs_end
; s
++) {
75 char const *secname
= secstrings
+ s
->sh_name
;
77 if (strcmp(".ARM.exidx.init.text", secname
) == 0)
78 maps
[ARM_SEC_INIT
].unw_sec
= s
;
79 else if (strcmp(".ARM.exidx.devinit.text", secname
) == 0)
80 maps
[ARM_SEC_DEVINIT
].unw_sec
= s
;
81 else if (strcmp(".ARM.exidx", secname
) == 0)
82 maps
[ARM_SEC_CORE
].unw_sec
= s
;
83 else if (strcmp(".ARM.exidx.exit.text", secname
) == 0)
84 maps
[ARM_SEC_EXIT
].unw_sec
= s
;
85 else if (strcmp(".ARM.exidx.devexit.text", secname
) == 0)
86 maps
[ARM_SEC_DEVEXIT
].unw_sec
= s
;
87 else if (strcmp(".init.text", secname
) == 0)
88 maps
[ARM_SEC_INIT
].sec_text
= s
;
89 else if (strcmp(".devinit.text", secname
) == 0)
90 maps
[ARM_SEC_DEVINIT
].sec_text
= s
;
91 else if (strcmp(".text", secname
) == 0)
92 maps
[ARM_SEC_CORE
].sec_text
= s
;
93 else if (strcmp(".exit.text", secname
) == 0)
94 maps
[ARM_SEC_EXIT
].sec_text
= s
;
95 else if (strcmp(".devexit.text", secname
) == 0)
96 maps
[ARM_SEC_DEVEXIT
].sec_text
= s
;
103 apply_relocate(Elf32_Shdr
*sechdrs
, const char *strtab
, unsigned int symindex
,
104 unsigned int relindex
, struct module
*module
)
106 Elf32_Shdr
*symsec
= sechdrs
+ symindex
;
107 Elf32_Shdr
*relsec
= sechdrs
+ relindex
;
108 Elf32_Shdr
*dstsec
= sechdrs
+ relsec
->sh_info
;
109 Elf32_Rel
*rel
= (void *)relsec
->sh_addr
;
112 for (i
= 0; i
< relsec
->sh_size
/ sizeof(Elf32_Rel
); i
++, rel
++) {
116 #ifdef CONFIG_THUMB2_KERNEL
117 u32 upper
, lower
, sign
, j1
, j2
;
120 offset
= ELF32_R_SYM(rel
->r_info
);
121 if (offset
< 0 || offset
> (symsec
->sh_size
/ sizeof(Elf32_Sym
))) {
122 printk(KERN_ERR
"%s: bad relocation, section %d reloc %d\n",
123 module
->name
, relindex
, i
);
127 sym
= ((Elf32_Sym
*)symsec
->sh_addr
) + offset
;
129 if (rel
->r_offset
< 0 || rel
->r_offset
> dstsec
->sh_size
- sizeof(u32
)) {
130 printk(KERN_ERR
"%s: out of bounds relocation, "
131 "section %d reloc %d offset %d size %d\n",
132 module
->name
, relindex
, i
, rel
->r_offset
,
137 loc
= dstsec
->sh_addr
+ rel
->r_offset
;
139 switch (ELF32_R_TYPE(rel
->r_info
)) {
145 *(u32
*)loc
+= sym
->st_value
;
151 offset
= (*(u32
*)loc
& 0x00ffffff) << 2;
152 if (offset
& 0x02000000)
153 offset
-= 0x04000000;
155 offset
+= sym
->st_value
- loc
;
157 offset
<= (s32
)0xfe000000 ||
158 offset
>= (s32
)0x02000000) {
160 "%s: relocation out of range, section "
161 "%d reloc %d sym '%s'\n", module
->name
,
162 relindex
, i
, strtab
+ sym
->st_name
);
168 *(u32
*)loc
&= 0xff000000;
169 *(u32
*)loc
|= offset
& 0x00ffffff;
173 /* Preserve Rm and the condition code. Alter
174 * other bits to re-code instruction as
177 *(u32
*)loc
&= 0xf000000f;
178 *(u32
*)loc
|= 0x01a0f000;
182 offset
= *(u32
*)loc
+ sym
->st_value
- loc
;
183 *(u32
*)loc
= offset
& 0x7fffffff;
186 case R_ARM_MOVW_ABS_NC
:
188 offset
= *(u32
*)loc
;
189 offset
= ((offset
& 0xf0000) >> 4) | (offset
& 0xfff);
190 offset
= (offset
^ 0x8000) - 0x8000;
192 offset
+= sym
->st_value
;
193 if (ELF32_R_TYPE(rel
->r_info
) == R_ARM_MOVT_ABS
)
196 *(u32
*)loc
&= 0xfff0f000;
197 *(u32
*)loc
|= ((offset
& 0xf000) << 4) |
201 #ifdef CONFIG_THUMB2_KERNEL
203 case R_ARM_THM_JUMP24
:
205 lower
= *(u16
*)(loc
+ 2);
208 * 25 bit signed address range (Thumb-2 BL and B.W
210 * S:I1:I2:imm10:imm11:0
212 * S = upper[10] = offset[24]
213 * I1 = ~(J1 ^ S) = offset[23]
214 * I2 = ~(J2 ^ S) = offset[22]
215 * imm10 = upper[9:0] = offset[21:12]
216 * imm11 = lower[10:0] = offset[11:1]
220 sign
= (upper
>> 10) & 1;
221 j1
= (lower
>> 13) & 1;
222 j2
= (lower
>> 11) & 1;
223 offset
= (sign
<< 24) | ((~(j1
^ sign
) & 1) << 23) |
224 ((~(j2
^ sign
) & 1) << 22) |
225 ((upper
& 0x03ff) << 12) |
226 ((lower
& 0x07ff) << 1);
227 if (offset
& 0x01000000)
228 offset
-= 0x02000000;
229 offset
+= sym
->st_value
- loc
;
231 /* only Thumb addresses allowed (no interworking) */
233 offset
<= (s32
)0xff000000 ||
234 offset
>= (s32
)0x01000000) {
236 "%s: relocation out of range, section "
237 "%d reloc %d sym '%s'\n", module
->name
,
238 relindex
, i
, strtab
+ sym
->st_name
);
242 sign
= (offset
>> 24) & 1;
243 j1
= sign
^ (~(offset
>> 23) & 1);
244 j2
= sign
^ (~(offset
>> 22) & 1);
245 *(u16
*)loc
= (u16
)((upper
& 0xf800) | (sign
<< 10) |
246 ((offset
>> 12) & 0x03ff));
247 *(u16
*)(loc
+ 2) = (u16
)((lower
& 0xd000) |
248 (j1
<< 13) | (j2
<< 11) |
249 ((offset
>> 1) & 0x07ff));
252 case R_ARM_THM_MOVW_ABS_NC
:
253 case R_ARM_THM_MOVT_ABS
:
255 lower
= *(u16
*)(loc
+ 2);
258 * MOVT/MOVW instructions encoding in Thumb-2:
262 * imm3 = lower[14:12]
265 * imm16 = imm4:i:imm3:imm8
267 offset
= ((upper
& 0x000f) << 12) |
268 ((upper
& 0x0400) << 1) |
269 ((lower
& 0x7000) >> 4) | (lower
& 0x00ff);
270 offset
= (offset
^ 0x8000) - 0x8000;
271 offset
+= sym
->st_value
;
273 if (ELF32_R_TYPE(rel
->r_info
) == R_ARM_THM_MOVT_ABS
)
276 *(u16
*)loc
= (u16
)((upper
& 0xfbf0) |
277 ((offset
& 0xf000) >> 12) |
278 ((offset
& 0x0800) >> 1));
279 *(u16
*)(loc
+ 2) = (u16
)((lower
& 0x8f00) |
280 ((offset
& 0x0700) << 4) |
286 printk(KERN_ERR
"%s: unknown relocation: %u\n",
287 module
->name
, ELF32_R_TYPE(rel
->r_info
));
295 apply_relocate_add(Elf32_Shdr
*sechdrs
, const char *strtab
,
296 unsigned int symindex
, unsigned int relsec
, struct module
*module
)
298 printk(KERN_ERR
"module %s: ADD RELOCATION unsupported\n",
303 #ifdef CONFIG_ARM_UNWIND
304 static void register_unwind_tables(struct module
*mod
)
307 for (i
= 0; i
< ARM_SEC_MAX
; ++i
) {
308 struct arm_unwind_mapping
*map
= &mod
->arch
.map
[i
];
309 if (map
->unw_sec
&& map
->sec_text
)
310 map
->unwind
= unwind_table_add(map
->unw_sec
->sh_addr
,
311 map
->unw_sec
->sh_size
,
312 map
->sec_text
->sh_addr
,
313 map
->sec_text
->sh_size
);
317 static void unregister_unwind_tables(struct module
*mod
)
321 unwind_table_del(mod
->arch
.map
[i
].unwind
);
324 static inline void register_unwind_tables(struct module
*mod
) { }
325 static inline void unregister_unwind_tables(struct module
*mod
) { }
329 module_finalize(const Elf32_Ehdr
*hdr
, const Elf_Shdr
*sechdrs
,
330 struct module
*module
)
332 register_unwind_tables(module
);
337 module_arch_cleanup(struct module
*mod
)
339 unregister_unwind_tables(mod
);