xref: /openbmc/linux/arch/arm/kernel/module.c (revision 64c70b1c)
1 /*
2  *  linux/arch/arm/kernel/module.c
3  *
4  *  Copyright (C) 2002 Russell King.
5  *  Modified for nommu by Hyok S. Choi
6  *
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.
10  *
11  * Module allocation method suggested by Andi Kleen.
12  */
13 #include <linux/module.h>
14 #include <linux/moduleloader.h>
15 #include <linux/kernel.h>
16 #include <linux/elf.h>
17 #include <linux/vmalloc.h>
18 #include <linux/slab.h>
19 #include <linux/fs.h>
20 #include <linux/string.h>
21 
22 #include <asm/pgtable.h>
23 
24 #ifdef CONFIG_XIP_KERNEL
25 /*
26  * The XIP kernel text is mapped in the module area for modules and
27  * some other stuff to work without any indirect relocations.
28  * MODULE_START is redefined here and not in asm/memory.h to avoid
29  * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
30  */
31 extern void _etext;
32 #undef MODULE_START
33 #define MODULE_START	(((unsigned long)&_etext + ~PGDIR_MASK) & PGDIR_MASK)
34 #endif
35 
36 #ifdef CONFIG_MMU
37 void *module_alloc(unsigned long size)
38 {
39 	struct vm_struct *area;
40 
41 	size = PAGE_ALIGN(size);
42 	if (!size)
43 		return NULL;
44 
45 	area = __get_vm_area(size, VM_ALLOC, MODULE_START, MODULE_END);
46 	if (!area)
47 		return NULL;
48 
49 	return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
50 }
51 #else /* CONFIG_MMU */
52 void *module_alloc(unsigned long size)
53 {
54 	return size == 0 ? NULL : vmalloc(size);
55 }
56 #endif /* !CONFIG_MMU */
57 
58 void module_free(struct module *module, void *region)
59 {
60 	vfree(region);
61 }
62 
63 int module_frob_arch_sections(Elf_Ehdr *hdr,
64 			      Elf_Shdr *sechdrs,
65 			      char *secstrings,
66 			      struct module *mod)
67 {
68 	return 0;
69 }
70 
71 int
72 apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
73 	       unsigned int relindex, struct module *module)
74 {
75 	Elf32_Shdr *symsec = sechdrs + symindex;
76 	Elf32_Shdr *relsec = sechdrs + relindex;
77 	Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
78 	Elf32_Rel *rel = (void *)relsec->sh_addr;
79 	unsigned int i;
80 
81 	for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
82 		unsigned long loc;
83 		Elf32_Sym *sym;
84 		s32 offset;
85 
86 		offset = ELF32_R_SYM(rel->r_info);
87 		if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
88 			printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
89 				module->name, relindex, i);
90 			return -ENOEXEC;
91 		}
92 
93 		sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
94 
95 		if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
96 			printk(KERN_ERR "%s: out of bounds relocation, "
97 				"section %d reloc %d offset %d size %d\n",
98 				module->name, relindex, i, rel->r_offset,
99 				dstsec->sh_size);
100 			return -ENOEXEC;
101 		}
102 
103 		loc = dstsec->sh_addr + rel->r_offset;
104 
105 		switch (ELF32_R_TYPE(rel->r_info)) {
106 		case R_ARM_ABS32:
107 			*(u32 *)loc += sym->st_value;
108 			break;
109 
110 		case R_ARM_PC24:
111 		case R_ARM_CALL:
112 		case R_ARM_JUMP24:
113 			offset = (*(u32 *)loc & 0x00ffffff) << 2;
114 			if (offset & 0x02000000)
115 				offset -= 0x04000000;
116 
117 			offset += sym->st_value - loc;
118 			if (offset & 3 ||
119 			    offset <= (s32)0xfe000000 ||
120 			    offset >= (s32)0x02000000) {
121 				printk(KERN_ERR
122 				       "%s: relocation out of range, section "
123 				       "%d reloc %d sym '%s'\n", module->name,
124 				       relindex, i, strtab + sym->st_name);
125 				return -ENOEXEC;
126 			}
127 
128 			offset >>= 2;
129 
130 			*(u32 *)loc &= 0xff000000;
131 			*(u32 *)loc |= offset & 0x00ffffff;
132 			break;
133 
134 		default:
135 			printk(KERN_ERR "%s: unknown relocation: %u\n",
136 			       module->name, ELF32_R_TYPE(rel->r_info));
137 			return -ENOEXEC;
138 		}
139 	}
140 	return 0;
141 }
142 
143 int
144 apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
145 		   unsigned int symindex, unsigned int relsec, struct module *module)
146 {
147 	printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
148 	       module->name);
149 	return -ENOEXEC;
150 }
151 
152 int
153 module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
154 		struct module *module)
155 {
156 	return 0;
157 }
158 
159 void
160 module_arch_cleanup(struct module *mod)
161 {
162 }
163