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 return __vmalloc_node_range(size
, 1, MODULES_VADDR
, MODULES_END
,
42 GFP_KERNEL
, PAGE_KERNEL_EXEC
, -1,
43 __builtin_return_address(0));
45 #else /* CONFIG_MMU */
46 void *module_alloc(unsigned long size
)
48 return size
== 0 ? NULL
: vmalloc(size
);
50 #endif /* !CONFIG_MMU */
52 void module_free(struct module
*module
, void *region
)
57 int module_frob_arch_sections(Elf_Ehdr
*hdr
,
66 apply_relocate(Elf32_Shdr
*sechdrs
, const char *strtab
, unsigned int symindex
,
67 unsigned int relindex
, struct module
*module
)
69 Elf32_Shdr
*symsec
= sechdrs
+ symindex
;
70 Elf32_Shdr
*relsec
= sechdrs
+ relindex
;
71 Elf32_Shdr
*dstsec
= sechdrs
+ relsec
->sh_info
;
72 Elf32_Rel
*rel
= (void *)relsec
->sh_addr
;
75 for (i
= 0; i
< relsec
->sh_size
/ sizeof(Elf32_Rel
); i
++, rel
++) {
79 #ifdef CONFIG_THUMB2_KERNEL
80 u32 upper
, lower
, sign
, j1
, j2
;
83 offset
= ELF32_R_SYM(rel
->r_info
);
84 if (offset
< 0 || offset
> (symsec
->sh_size
/ sizeof(Elf32_Sym
))) {
85 printk(KERN_ERR
"%s: bad relocation, section %d reloc %d\n",
86 module
->name
, relindex
, i
);
90 sym
= ((Elf32_Sym
*)symsec
->sh_addr
) + offset
;
92 if (rel
->r_offset
< 0 || rel
->r_offset
> dstsec
->sh_size
- sizeof(u32
)) {
93 printk(KERN_ERR
"%s: out of bounds relocation, "
94 "section %d reloc %d offset %d size %d\n",
95 module
->name
, relindex
, i
, rel
->r_offset
,
100 loc
= dstsec
->sh_addr
+ rel
->r_offset
;
102 switch (ELF32_R_TYPE(rel
->r_info
)) {
108 *(u32
*)loc
+= sym
->st_value
;
114 offset
= (*(u32
*)loc
& 0x00ffffff) << 2;
115 if (offset
& 0x02000000)
116 offset
-= 0x04000000;
118 offset
+= sym
->st_value
- loc
;
120 offset
<= (s32
)0xfe000000 ||
121 offset
>= (s32
)0x02000000) {
123 "%s: relocation out of range, section "
124 "%d reloc %d sym '%s'\n", module
->name
,
125 relindex
, i
, strtab
+ sym
->st_name
);
131 *(u32
*)loc
&= 0xff000000;
132 *(u32
*)loc
|= offset
& 0x00ffffff;
136 /* Preserve Rm and the condition code. Alter
137 * other bits to re-code instruction as
140 *(u32
*)loc
&= 0xf000000f;
141 *(u32
*)loc
|= 0x01a0f000;
145 offset
= *(u32
*)loc
+ sym
->st_value
- loc
;
146 *(u32
*)loc
= offset
& 0x7fffffff;
149 case R_ARM_MOVW_ABS_NC
:
151 offset
= *(u32
*)loc
;
152 offset
= ((offset
& 0xf0000) >> 4) | (offset
& 0xfff);
153 offset
= (offset
^ 0x8000) - 0x8000;
155 offset
+= sym
->st_value
;
156 if (ELF32_R_TYPE(rel
->r_info
) == R_ARM_MOVT_ABS
)
159 *(u32
*)loc
&= 0xfff0f000;
160 *(u32
*)loc
|= ((offset
& 0xf000) << 4) |
164 #ifdef CONFIG_THUMB2_KERNEL
166 case R_ARM_THM_JUMP24
:
168 lower
= *(u16
*)(loc
+ 2);
171 * 25 bit signed address range (Thumb-2 BL and B.W
173 * S:I1:I2:imm10:imm11:0
175 * S = upper[10] = offset[24]
176 * I1 = ~(J1 ^ S) = offset[23]
177 * I2 = ~(J2 ^ S) = offset[22]
178 * imm10 = upper[9:0] = offset[21:12]
179 * imm11 = lower[10:0] = offset[11:1]
183 sign
= (upper
>> 10) & 1;
184 j1
= (lower
>> 13) & 1;
185 j2
= (lower
>> 11) & 1;
186 offset
= (sign
<< 24) | ((~(j1
^ sign
) & 1) << 23) |
187 ((~(j2
^ sign
) & 1) << 22) |
188 ((upper
& 0x03ff) << 12) |
189 ((lower
& 0x07ff) << 1);
190 if (offset
& 0x01000000)
191 offset
-= 0x02000000;
192 offset
+= sym
->st_value
- loc
;
194 /* only Thumb addresses allowed (no interworking) */
196 offset
<= (s32
)0xff000000 ||
197 offset
>= (s32
)0x01000000) {
199 "%s: relocation out of range, section "
200 "%d reloc %d sym '%s'\n", module
->name
,
201 relindex
, i
, strtab
+ sym
->st_name
);
205 sign
= (offset
>> 24) & 1;
206 j1
= sign
^ (~(offset
>> 23) & 1);
207 j2
= sign
^ (~(offset
>> 22) & 1);
208 *(u16
*)loc
= (u16
)((upper
& 0xf800) | (sign
<< 10) |
209 ((offset
>> 12) & 0x03ff));
210 *(u16
*)(loc
+ 2) = (u16
)((lower
& 0xd000) |
211 (j1
<< 13) | (j2
<< 11) |
212 ((offset
>> 1) & 0x07ff));
215 case R_ARM_THM_MOVW_ABS_NC
:
216 case R_ARM_THM_MOVT_ABS
:
218 lower
= *(u16
*)(loc
+ 2);
221 * MOVT/MOVW instructions encoding in Thumb-2:
225 * imm3 = lower[14:12]
228 * imm16 = imm4:i:imm3:imm8
230 offset
= ((upper
& 0x000f) << 12) |
231 ((upper
& 0x0400) << 1) |
232 ((lower
& 0x7000) >> 4) | (lower
& 0x00ff);
233 offset
= (offset
^ 0x8000) - 0x8000;
234 offset
+= sym
->st_value
;
236 if (ELF32_R_TYPE(rel
->r_info
) == R_ARM_THM_MOVT_ABS
)
239 *(u16
*)loc
= (u16
)((upper
& 0xfbf0) |
240 ((offset
& 0xf000) >> 12) |
241 ((offset
& 0x0800) >> 1));
242 *(u16
*)(loc
+ 2) = (u16
)((lower
& 0x8f00) |
243 ((offset
& 0x0700) << 4) |
249 printk(KERN_ERR
"%s: unknown relocation: %u\n",
250 module
->name
, ELF32_R_TYPE(rel
->r_info
));
258 apply_relocate_add(Elf32_Shdr
*sechdrs
, const char *strtab
,
259 unsigned int symindex
, unsigned int relsec
, struct module
*module
)
261 printk(KERN_ERR
"module %s: ADD RELOCATION unsupported\n",
266 struct mod_unwind_map
{
267 const Elf_Shdr
*unw_sec
;
268 const Elf_Shdr
*txt_sec
;
271 int module_finalize(const Elf32_Ehdr
*hdr
, const Elf_Shdr
*sechdrs
,
274 #ifdef CONFIG_ARM_UNWIND
275 const char *secstrs
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
276 const Elf_Shdr
*s
, *sechdrs_end
= sechdrs
+ hdr
->e_shnum
;
277 struct mod_unwind_map maps
[ARM_SEC_MAX
];
280 memset(maps
, 0, sizeof(maps
));
282 for (s
= sechdrs
; s
< sechdrs_end
; s
++) {
283 const char *secname
= secstrs
+ s
->sh_name
;
285 if (!(s
->sh_flags
& SHF_ALLOC
))
288 if (strcmp(".ARM.exidx.init.text", secname
) == 0)
289 maps
[ARM_SEC_INIT
].unw_sec
= s
;
290 else if (strcmp(".ARM.exidx.devinit.text", secname
) == 0)
291 maps
[ARM_SEC_DEVINIT
].unw_sec
= s
;
292 else if (strcmp(".ARM.exidx", secname
) == 0)
293 maps
[ARM_SEC_CORE
].unw_sec
= s
;
294 else if (strcmp(".ARM.exidx.exit.text", secname
) == 0)
295 maps
[ARM_SEC_EXIT
].unw_sec
= s
;
296 else if (strcmp(".ARM.exidx.devexit.text", secname
) == 0)
297 maps
[ARM_SEC_DEVEXIT
].unw_sec
= s
;
298 else if (strcmp(".init.text", secname
) == 0)
299 maps
[ARM_SEC_INIT
].txt_sec
= s
;
300 else if (strcmp(".devinit.text", secname
) == 0)
301 maps
[ARM_SEC_DEVINIT
].txt_sec
= s
;
302 else if (strcmp(".text", secname
) == 0)
303 maps
[ARM_SEC_CORE
].txt_sec
= s
;
304 else if (strcmp(".exit.text", secname
) == 0)
305 maps
[ARM_SEC_EXIT
].txt_sec
= s
;
306 else if (strcmp(".devexit.text", secname
) == 0)
307 maps
[ARM_SEC_DEVEXIT
].txt_sec
= s
;
310 for (i
= 0; i
< ARM_SEC_MAX
; i
++)
311 if (maps
[i
].unw_sec
&& maps
[i
].txt_sec
)
312 mod
->arch
.unwind
[i
] =
313 unwind_table_add(maps
[i
].unw_sec
->sh_addr
,
314 maps
[i
].unw_sec
->sh_size
,
315 maps
[i
].txt_sec
->sh_addr
,
316 maps
[i
].txt_sec
->sh_size
);
322 module_arch_cleanup(struct module
*mod
)
324 #ifdef CONFIG_ARM_UNWIND
327 for (i
= 0; i
< ARM_SEC_MAX
; i
++)
328 if (mod
->arch
.unwind
[i
])
329 unwind_table_del(mod
->arch
.unwind
[i
]);
This page took 0.038736 seconds and 5 git commands to generate.