xref: /linux/arch/arm/kernel/module.c (revision d9afbb3509900a953f5cf90bc57e793ee80c1108)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/arch/arm/kernel/module.c
4  *
5  *  Copyright (C) 2002 Russell King.
6  *  Modified for nommu by Hyok S. Choi
7  *
8  * Module allocation method suggested by Andi Kleen.
9  */
10 #include <linux/module.h>
11 #include <linux/moduleloader.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/elf.h>
15 #include <linux/vmalloc.h>
16 #include <linux/fs.h>
17 #include <linux/string.h>
18 #include <linux/gfp.h>
19 
20 #include <asm/pgtable.h>
21 #include <asm/sections.h>
22 #include <asm/smp_plat.h>
23 #include <asm/unwind.h>
24 #include <asm/opcodes.h>
25 
26 #ifdef CONFIG_XIP_KERNEL
27 /*
28  * The XIP kernel text is mapped in the module area for modules and
29  * some other stuff to work without any indirect relocations.
30  * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
31  * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
32  */
33 #undef MODULES_VADDR
34 #define MODULES_VADDR	(((unsigned long)_exiprom + ~PMD_MASK) & PMD_MASK)
35 #endif
36 
37 #ifdef CONFIG_MMU
38 void *module_alloc(unsigned long size)
39 {
40 	gfp_t gfp_mask = GFP_KERNEL;
41 	void *p;
42 
43 	/* Silence the initial allocation */
44 	if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS))
45 		gfp_mask |= __GFP_NOWARN;
46 
47 	p = __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
48 				gfp_mask, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
49 				__builtin_return_address(0));
50 	if (!IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || p)
51 		return p;
52 	return __vmalloc_node_range(size, 1,  VMALLOC_START, VMALLOC_END,
53 				GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
54 				__builtin_return_address(0));
55 }
56 #endif
57 
58 bool module_init_section(const char *name)
59 {
60 	return strstarts(name, ".init") ||
61 		strstarts(name, ".ARM.extab.init") ||
62 		strstarts(name, ".ARM.exidx.init");
63 }
64 
65 bool module_exit_section(const char *name)
66 {
67 	return strstarts(name, ".exit") ||
68 		strstarts(name, ".ARM.extab.exit") ||
69 		strstarts(name, ".ARM.exidx.exit");
70 }
71 
72 int
73 apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
74 	       unsigned int relindex, struct module *module)
75 {
76 	Elf32_Shdr *symsec = sechdrs + symindex;
77 	Elf32_Shdr *relsec = sechdrs + relindex;
78 	Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
79 	Elf32_Rel *rel = (void *)relsec->sh_addr;
80 	unsigned int i;
81 
82 	for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
83 		unsigned long loc;
84 		Elf32_Sym *sym;
85 		const char *symname;
86 		s32 offset;
87 		u32 tmp;
88 #ifdef CONFIG_THUMB2_KERNEL
89 		u32 upper, lower, sign, j1, j2;
90 #endif
91 
92 		offset = ELF32_R_SYM(rel->r_info);
93 		if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
94 			pr_err("%s: section %u reloc %u: bad relocation sym offset\n",
95 				module->name, relindex, i);
96 			return -ENOEXEC;
97 		}
98 
99 		sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
100 		symname = strtab + sym->st_name;
101 
102 		if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
103 			pr_err("%s: section %u reloc %u sym '%s': out of bounds relocation, offset %d size %u\n",
104 			       module->name, relindex, i, symname,
105 			       rel->r_offset, dstsec->sh_size);
106 			return -ENOEXEC;
107 		}
108 
109 		loc = dstsec->sh_addr + rel->r_offset;
110 
111 		switch (ELF32_R_TYPE(rel->r_info)) {
112 		case R_ARM_NONE:
113 			/* ignore */
114 			break;
115 
116 		case R_ARM_ABS32:
117 		case R_ARM_TARGET1:
118 			*(u32 *)loc += sym->st_value;
119 			break;
120 
121 		case R_ARM_PC24:
122 		case R_ARM_CALL:
123 		case R_ARM_JUMP24:
124 			if (sym->st_value & 3) {
125 				pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (ARM -> Thumb)\n",
126 				       module->name, relindex, i, symname);
127 				return -ENOEXEC;
128 			}
129 
130 			offset = __mem_to_opcode_arm(*(u32 *)loc);
131 			offset = (offset & 0x00ffffff) << 2;
132 			if (offset & 0x02000000)
133 				offset -= 0x04000000;
134 
135 			offset += sym->st_value - loc;
136 
137 			/*
138 			 * Route through a PLT entry if 'offset' exceeds the
139 			 * supported range. Note that 'offset + loc + 8'
140 			 * contains the absolute jump target, i.e.,
141 			 * @sym + addend, corrected for the +8 PC bias.
142 			 */
143 			if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
144 			    (offset <= (s32)0xfe000000 ||
145 			     offset >= (s32)0x02000000))
146 				offset = get_module_plt(module, loc,
147 							offset + loc + 8)
148 					 - loc - 8;
149 
150 			if (offset <= (s32)0xfe000000 ||
151 			    offset >= (s32)0x02000000) {
152 				pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
153 				       module->name, relindex, i, symname,
154 				       ELF32_R_TYPE(rel->r_info), loc,
155 				       sym->st_value);
156 				return -ENOEXEC;
157 			}
158 
159 			offset >>= 2;
160 			offset &= 0x00ffffff;
161 
162 			*(u32 *)loc &= __opcode_to_mem_arm(0xff000000);
163 			*(u32 *)loc |= __opcode_to_mem_arm(offset);
164 			break;
165 
166 	       case R_ARM_V4BX:
167 		       /* Preserve Rm and the condition code. Alter
168 			* other bits to re-code instruction as
169 			* MOV PC,Rm.
170 			*/
171 		       *(u32 *)loc &= __opcode_to_mem_arm(0xf000000f);
172 		       *(u32 *)loc |= __opcode_to_mem_arm(0x01a0f000);
173 		       break;
174 
175 		case R_ARM_PREL31:
176 			offset = (*(s32 *)loc << 1) >> 1; /* sign extend */
177 			offset += sym->st_value - loc;
178 			if (offset >= 0x40000000 || offset < -0x40000000) {
179 				pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
180 				       module->name, relindex, i, symname,
181 				       ELF32_R_TYPE(rel->r_info), loc,
182 				       sym->st_value);
183 				return -ENOEXEC;
184 			}
185 			*(u32 *)loc &= 0x80000000;
186 			*(u32 *)loc |= offset & 0x7fffffff;
187 			break;
188 
189 		case R_ARM_MOVW_ABS_NC:
190 		case R_ARM_MOVT_ABS:
191 			offset = tmp = __mem_to_opcode_arm(*(u32 *)loc);
192 			offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
193 			offset = (offset ^ 0x8000) - 0x8000;
194 
195 			offset += sym->st_value;
196 			if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
197 				offset >>= 16;
198 
199 			tmp &= 0xfff0f000;
200 			tmp |= ((offset & 0xf000) << 4) |
201 				(offset & 0x0fff);
202 
203 			*(u32 *)loc = __opcode_to_mem_arm(tmp);
204 			break;
205 
206 #ifdef CONFIG_THUMB2_KERNEL
207 		case R_ARM_THM_CALL:
208 		case R_ARM_THM_JUMP24:
209 			/*
210 			 * For function symbols, only Thumb addresses are
211 			 * allowed (no interworking).
212 			 *
213 			 * For non-function symbols, the destination
214 			 * has no specific ARM/Thumb disposition, so
215 			 * the branch is resolved under the assumption
216 			 * that interworking is not required.
217 			 */
218 			if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
219 			    !(sym->st_value & 1)) {
220 				pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (Thumb -> ARM)\n",
221 				       module->name, relindex, i, symname);
222 				return -ENOEXEC;
223 			}
224 
225 			upper = __mem_to_opcode_thumb16(*(u16 *)loc);
226 			lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
227 
228 			/*
229 			 * 25 bit signed address range (Thumb-2 BL and B.W
230 			 * instructions):
231 			 *   S:I1:I2:imm10:imm11:0
232 			 * where:
233 			 *   S     = upper[10]   = offset[24]
234 			 *   I1    = ~(J1 ^ S)   = offset[23]
235 			 *   I2    = ~(J2 ^ S)   = offset[22]
236 			 *   imm10 = upper[9:0]  = offset[21:12]
237 			 *   imm11 = lower[10:0] = offset[11:1]
238 			 *   J1    = lower[13]
239 			 *   J2    = lower[11]
240 			 */
241 			sign = (upper >> 10) & 1;
242 			j1 = (lower >> 13) & 1;
243 			j2 = (lower >> 11) & 1;
244 			offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
245 				((~(j2 ^ sign) & 1) << 22) |
246 				((upper & 0x03ff) << 12) |
247 				((lower & 0x07ff) << 1);
248 			if (offset & 0x01000000)
249 				offset -= 0x02000000;
250 			offset += sym->st_value - loc;
251 
252 			/*
253 			 * Route through a PLT entry if 'offset' exceeds the
254 			 * supported range.
255 			 */
256 			if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
257 			    (offset <= (s32)0xff000000 ||
258 			     offset >= (s32)0x01000000))
259 				offset = get_module_plt(module, loc,
260 							offset + loc + 4)
261 					 - loc - 4;
262 
263 			if (offset <= (s32)0xff000000 ||
264 			    offset >= (s32)0x01000000) {
265 				pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
266 				       module->name, relindex, i, symname,
267 				       ELF32_R_TYPE(rel->r_info), loc,
268 				       sym->st_value);
269 				return -ENOEXEC;
270 			}
271 
272 			sign = (offset >> 24) & 1;
273 			j1 = sign ^ (~(offset >> 23) & 1);
274 			j2 = sign ^ (~(offset >> 22) & 1);
275 			upper = (u16)((upper & 0xf800) | (sign << 10) |
276 					    ((offset >> 12) & 0x03ff));
277 			lower = (u16)((lower & 0xd000) |
278 				      (j1 << 13) | (j2 << 11) |
279 				      ((offset >> 1) & 0x07ff));
280 
281 			*(u16 *)loc = __opcode_to_mem_thumb16(upper);
282 			*(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
283 			break;
284 
285 		case R_ARM_THM_MOVW_ABS_NC:
286 		case R_ARM_THM_MOVT_ABS:
287 			upper = __mem_to_opcode_thumb16(*(u16 *)loc);
288 			lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
289 
290 			/*
291 			 * MOVT/MOVW instructions encoding in Thumb-2:
292 			 *
293 			 * i	= upper[10]
294 			 * imm4	= upper[3:0]
295 			 * imm3	= lower[14:12]
296 			 * imm8	= lower[7:0]
297 			 *
298 			 * imm16 = imm4:i:imm3:imm8
299 			 */
300 			offset = ((upper & 0x000f) << 12) |
301 				((upper & 0x0400) << 1) |
302 				((lower & 0x7000) >> 4) | (lower & 0x00ff);
303 			offset = (offset ^ 0x8000) - 0x8000;
304 			offset += sym->st_value;
305 
306 			if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
307 				offset >>= 16;
308 
309 			upper = (u16)((upper & 0xfbf0) |
310 				      ((offset & 0xf000) >> 12) |
311 				      ((offset & 0x0800) >> 1));
312 			lower = (u16)((lower & 0x8f00) |
313 				      ((offset & 0x0700) << 4) |
314 				      (offset & 0x00ff));
315 			*(u16 *)loc = __opcode_to_mem_thumb16(upper);
316 			*(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
317 			break;
318 #endif
319 
320 		default:
321 			pr_err("%s: unknown relocation: %u\n",
322 			       module->name, ELF32_R_TYPE(rel->r_info));
323 			return -ENOEXEC;
324 		}
325 	}
326 	return 0;
327 }
328 
329 struct mod_unwind_map {
330 	const Elf_Shdr *unw_sec;
331 	const Elf_Shdr *txt_sec;
332 };
333 
334 static const Elf_Shdr *find_mod_section(const Elf32_Ehdr *hdr,
335 	const Elf_Shdr *sechdrs, const char *name)
336 {
337 	const Elf_Shdr *s, *se;
338 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
339 
340 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++)
341 		if (strcmp(name, secstrs + s->sh_name) == 0)
342 			return s;
343 
344 	return NULL;
345 }
346 
347 extern void fixup_pv_table(const void *, unsigned long);
348 extern void fixup_smp(const void *, unsigned long);
349 
350 int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
351 		    struct module *mod)
352 {
353 	const Elf_Shdr *s = NULL;
354 #ifdef CONFIG_ARM_UNWIND
355 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
356 	const Elf_Shdr *sechdrs_end = sechdrs + hdr->e_shnum;
357 	struct mod_unwind_map maps[ARM_SEC_MAX];
358 	int i;
359 
360 	memset(maps, 0, sizeof(maps));
361 
362 	for (s = sechdrs; s < sechdrs_end; s++) {
363 		const char *secname = secstrs + s->sh_name;
364 
365 		if (!(s->sh_flags & SHF_ALLOC))
366 			continue;
367 
368 		if (strcmp(".ARM.exidx.init.text", secname) == 0)
369 			maps[ARM_SEC_INIT].unw_sec = s;
370 		else if (strcmp(".ARM.exidx", secname) == 0)
371 			maps[ARM_SEC_CORE].unw_sec = s;
372 		else if (strcmp(".ARM.exidx.exit.text", secname) == 0)
373 			maps[ARM_SEC_EXIT].unw_sec = s;
374 		else if (strcmp(".ARM.exidx.text.unlikely", secname) == 0)
375 			maps[ARM_SEC_UNLIKELY].unw_sec = s;
376 		else if (strcmp(".ARM.exidx.text.hot", secname) == 0)
377 			maps[ARM_SEC_HOT].unw_sec = s;
378 		else if (strcmp(".init.text", secname) == 0)
379 			maps[ARM_SEC_INIT].txt_sec = s;
380 		else if (strcmp(".text", secname) == 0)
381 			maps[ARM_SEC_CORE].txt_sec = s;
382 		else if (strcmp(".exit.text", secname) == 0)
383 			maps[ARM_SEC_EXIT].txt_sec = s;
384 		else if (strcmp(".text.unlikely", secname) == 0)
385 			maps[ARM_SEC_UNLIKELY].txt_sec = s;
386 		else if (strcmp(".text.hot", secname) == 0)
387 			maps[ARM_SEC_HOT].txt_sec = s;
388 	}
389 
390 	for (i = 0; i < ARM_SEC_MAX; i++)
391 		if (maps[i].unw_sec && maps[i].txt_sec)
392 			mod->arch.unwind[i] =
393 				unwind_table_add(maps[i].unw_sec->sh_addr,
394 					         maps[i].unw_sec->sh_size,
395 					         maps[i].txt_sec->sh_addr,
396 					         maps[i].txt_sec->sh_size);
397 #endif
398 #ifdef CONFIG_ARM_PATCH_PHYS_VIRT
399 	s = find_mod_section(hdr, sechdrs, ".pv_table");
400 	if (s)
401 		fixup_pv_table((void *)s->sh_addr, s->sh_size);
402 #endif
403 	s = find_mod_section(hdr, sechdrs, ".alt.smp.init");
404 	if (s && !is_smp())
405 #ifdef CONFIG_SMP_ON_UP
406 		fixup_smp((void *)s->sh_addr, s->sh_size);
407 #else
408 		return -EINVAL;
409 #endif
410 	return 0;
411 }
412 
413 void
414 module_arch_cleanup(struct module *mod)
415 {
416 #ifdef CONFIG_ARM_UNWIND
417 	int i;
418 
419 	for (i = 0; i < ARM_SEC_MAX; i++) {
420 		unwind_table_del(mod->arch.unwind[i]);
421 		mod->arch.unwind[i] = NULL;
422 	}
423 #endif
424 }
425 
426 void __weak module_arch_freeing_init(struct module *mod)
427 {
428 #ifdef CONFIG_ARM_UNWIND
429 	unwind_table_del(mod->arch.unwind[ARM_SEC_INIT]);
430 	mod->arch.unwind[ARM_SEC_INIT] = NULL;
431 #endif
432 }
433