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