1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/unicore32/kernel/module.c
5 * Code specific to PKUnity SoC and UniCore ISA
7 * Copyright (C) 2001-2010 GUAN Xue-tao
9 #include <linux/module.h>
10 #include <linux/moduleloader.h>
11 #include <linux/kernel.h>
13 #include <linux/elf.h>
14 #include <linux/vmalloc.h>
16 #include <linux/string.h>
17 #include <linux/gfp.h>
19 #include <asm/pgtable.h>
20 #include <asm/sections.h>
22 void *module_alloc(unsigned long size
)
24 return __vmalloc_node_range(size
, 1, MODULES_VADDR
, MODULES_END
,
25 GFP_KERNEL
, PAGE_KERNEL_EXEC
, 0, NUMA_NO_NODE
,
26 __builtin_return_address(0));
30 apply_relocate(Elf32_Shdr
*sechdrs
, const char *strtab
, unsigned int symindex
,
31 unsigned int relindex
, struct module
*module
)
33 Elf32_Shdr
*symsec
= sechdrs
+ symindex
;
34 Elf32_Shdr
*relsec
= sechdrs
+ relindex
;
35 Elf32_Shdr
*dstsec
= sechdrs
+ relsec
->sh_info
;
36 Elf32_Rel
*rel
= (void *)relsec
->sh_addr
;
39 for (i
= 0; i
< relsec
->sh_size
/ sizeof(Elf32_Rel
); i
++, rel
++) {
44 offset
= ELF32_R_SYM(rel
->r_info
);
45 if (offset
< 0 || offset
>
46 (symsec
->sh_size
/ sizeof(Elf32_Sym
))) {
47 printk(KERN_ERR
"%s: bad relocation, "
48 "section %d reloc %d\n",
49 module
->name
, relindex
, i
);
53 sym
= ((Elf32_Sym
*)symsec
->sh_addr
) + offset
;
55 if (rel
->r_offset
< 0 || rel
->r_offset
>
56 dstsec
->sh_size
- sizeof(u32
)) {
57 printk(KERN_ERR
"%s: out of bounds relocation, "
58 "section %d reloc %d offset %d size %d\n",
59 module
->name
, relindex
, i
, rel
->r_offset
,
64 loc
= dstsec
->sh_addr
+ rel
->r_offset
;
66 switch (ELF32_R_TYPE(rel
->r_info
)) {
72 *(u32
*)loc
+= sym
->st_value
;
77 case R_UNICORE_JUMP24
:
78 offset
= (*(u32
*)loc
& 0x00ffffff) << 2;
79 if (offset
& 0x02000000)
82 offset
+= sym
->st_value
- loc
;
84 offset
<= (s32
)0xfe000000 ||
85 offset
>= (s32
)0x02000000) {
87 "%s: relocation out of range, section "
88 "%d reloc %d sym '%s'\n", module
->name
,
89 relindex
, i
, strtab
+ sym
->st_name
);
95 *(u32
*)loc
&= 0xff000000;
96 *(u32
*)loc
|= offset
& 0x00ffffff;
100 printk(KERN_ERR
"%s: unknown relocation: %u\n",
101 module
->name
, ELF32_R_TYPE(rel
->r_info
));