blob: 4b224e6ae0d69e5052cb4f3c72f68b7a00f0b509 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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
38void *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
58bool 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
65bool 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
72int
73apply_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 if ((IS_ERR_VALUE(sym->st_value) || !sym->st_value) &&
110 ELF_ST_BIND(sym->st_info) == STB_WEAK)
111 continue;
112
113 loc = dstsec->sh_addr + rel->r_offset;
114
115 switch (ELF32_R_TYPE(rel->r_info)) {
116 case R_ARM_NONE:
117 /* ignore */
118 break;
119
120 case R_ARM_ABS32:
121 case R_ARM_TARGET1:
122 *(u32 *)loc += sym->st_value;
123 break;
124
125 case R_ARM_PC24:
126 case R_ARM_CALL:
127 case R_ARM_JUMP24:
128 if (sym->st_value & 3) {
129 pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (ARM -> Thumb)\n",
130 module->name, relindex, i, symname);
131 return -ENOEXEC;
132 }
133
134 offset = __mem_to_opcode_arm(*(u32 *)loc);
135 offset = (offset & 0x00ffffff) << 2;
136 if (offset & 0x02000000)
137 offset -= 0x04000000;
138
139 offset += sym->st_value - loc;
140
141 /*
142 * Route through a PLT entry if 'offset' exceeds the
143 * supported range. Note that 'offset + loc + 8'
144 * contains the absolute jump target, i.e.,
145 * @sym + addend, corrected for the +8 PC bias.
146 */
147 if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
148 (offset <= (s32)0xfe000000 ||
149 offset >= (s32)0x02000000))
150 offset = get_module_plt(module, loc,
151 offset + loc + 8)
152 - loc - 8;
153
154 if (offset <= (s32)0xfe000000 ||
155 offset >= (s32)0x02000000) {
156 pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
157 module->name, relindex, i, symname,
158 ELF32_R_TYPE(rel->r_info), loc,
159 sym->st_value);
160 return -ENOEXEC;
161 }
162
163 offset >>= 2;
164 offset &= 0x00ffffff;
165
166 *(u32 *)loc &= __opcode_to_mem_arm(0xff000000);
167 *(u32 *)loc |= __opcode_to_mem_arm(offset);
168 break;
169
170 case R_ARM_V4BX:
171 /* Preserve Rm and the condition code. Alter
172 * other bits to re-code instruction as
173 * MOV PC,Rm.
174 */
175 *(u32 *)loc &= __opcode_to_mem_arm(0xf000000f);
176 *(u32 *)loc |= __opcode_to_mem_arm(0x01a0f000);
177 break;
178
179 case R_ARM_PREL31:
180 offset = (*(s32 *)loc << 1) >> 1; /* sign extend */
181 offset += sym->st_value - loc;
182 if (offset >= 0x40000000 || offset < -0x40000000) {
183 pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
184 module->name, relindex, i, symname,
185 ELF32_R_TYPE(rel->r_info), loc,
186 sym->st_value);
187 return -ENOEXEC;
188 }
189 *(u32 *)loc &= 0x80000000;
190 *(u32 *)loc |= offset & 0x7fffffff;
191 break;
192
193 case R_ARM_MOVW_ABS_NC:
194 case R_ARM_MOVT_ABS:
195 offset = tmp = __mem_to_opcode_arm(*(u32 *)loc);
196 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
197 offset = (offset ^ 0x8000) - 0x8000;
198
199 offset += sym->st_value;
200 if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
201 offset >>= 16;
202
203 tmp &= 0xfff0f000;
204 tmp |= ((offset & 0xf000) << 4) |
205 (offset & 0x0fff);
206
207 *(u32 *)loc = __opcode_to_mem_arm(tmp);
208 break;
209
210#ifdef CONFIG_THUMB2_KERNEL
211 case R_ARM_THM_CALL:
212 case R_ARM_THM_JUMP24:
213 /*
214 * For function symbols, only Thumb addresses are
215 * allowed (no interworking).
216 *
217 * For non-function symbols, the destination
218 * has no specific ARM/Thumb disposition, so
219 * the branch is resolved under the assumption
220 * that interworking is not required.
221 */
222 if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
223 !(sym->st_value & 1)) {
224 pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (Thumb -> ARM)\n",
225 module->name, relindex, i, symname);
226 return -ENOEXEC;
227 }
228
229 upper = __mem_to_opcode_thumb16(*(u16 *)loc);
230 lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
231
232 /*
233 * 25 bit signed address range (Thumb-2 BL and B.W
234 * instructions):
235 * S:I1:I2:imm10:imm11:0
236 * where:
237 * S = upper[10] = offset[24]
238 * I1 = ~(J1 ^ S) = offset[23]
239 * I2 = ~(J2 ^ S) = offset[22]
240 * imm10 = upper[9:0] = offset[21:12]
241 * imm11 = lower[10:0] = offset[11:1]
242 * J1 = lower[13]
243 * J2 = lower[11]
244 */
245 sign = (upper >> 10) & 1;
246 j1 = (lower >> 13) & 1;
247 j2 = (lower >> 11) & 1;
248 offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
249 ((~(j2 ^ sign) & 1) << 22) |
250 ((upper & 0x03ff) << 12) |
251 ((lower & 0x07ff) << 1);
252 if (offset & 0x01000000)
253 offset -= 0x02000000;
254 offset += sym->st_value - loc;
255
256 /*
257 * Route through a PLT entry if 'offset' exceeds the
258 * supported range.
259 */
260 if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
261 (offset <= (s32)0xff000000 ||
262 offset >= (s32)0x01000000))
263 offset = get_module_plt(module, loc,
264 offset + loc + 4)
265 - loc - 4;
266
267 if (offset <= (s32)0xff000000 ||
268 offset >= (s32)0x01000000) {
269 pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
270 module->name, relindex, i, symname,
271 ELF32_R_TYPE(rel->r_info), loc,
272 sym->st_value);
273 return -ENOEXEC;
274 }
275
276 sign = (offset >> 24) & 1;
277 j1 = sign ^ (~(offset >> 23) & 1);
278 j2 = sign ^ (~(offset >> 22) & 1);
279 upper = (u16)((upper & 0xf800) | (sign << 10) |
280 ((offset >> 12) & 0x03ff));
281 lower = (u16)((lower & 0xd000) |
282 (j1 << 13) | (j2 << 11) |
283 ((offset >> 1) & 0x07ff));
284
285 *(u16 *)loc = __opcode_to_mem_thumb16(upper);
286 *(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
287 break;
288
289 case R_ARM_THM_MOVW_ABS_NC:
290 case R_ARM_THM_MOVT_ABS:
291 upper = __mem_to_opcode_thumb16(*(u16 *)loc);
292 lower = __mem_to_opcode_thumb16(*(u16 *)(loc + 2));
293
294 /*
295 * MOVT/MOVW instructions encoding in Thumb-2:
296 *
297 * i = upper[10]
298 * imm4 = upper[3:0]
299 * imm3 = lower[14:12]
300 * imm8 = lower[7:0]
301 *
302 * imm16 = imm4:i:imm3:imm8
303 */
304 offset = ((upper & 0x000f) << 12) |
305 ((upper & 0x0400) << 1) |
306 ((lower & 0x7000) >> 4) | (lower & 0x00ff);
307 offset = (offset ^ 0x8000) - 0x8000;
308 offset += sym->st_value;
309
310 if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
311 offset >>= 16;
312
313 upper = (u16)((upper & 0xfbf0) |
314 ((offset & 0xf000) >> 12) |
315 ((offset & 0x0800) >> 1));
316 lower = (u16)((lower & 0x8f00) |
317 ((offset & 0x0700) << 4) |
318 (offset & 0x00ff));
319 *(u16 *)loc = __opcode_to_mem_thumb16(upper);
320 *(u16 *)(loc + 2) = __opcode_to_mem_thumb16(lower);
321 break;
322#endif
323
324 default:
325 pr_err("%s: unknown relocation: %u\n",
326 module->name, ELF32_R_TYPE(rel->r_info));
327 return -ENOEXEC;
328 }
329 }
330 return 0;
331}
332
333static const Elf_Shdr *find_mod_section(const Elf32_Ehdr *hdr,
334 const Elf_Shdr *sechdrs, const char *name)
335{
336 const Elf_Shdr *s, *se;
337 const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
338
339 for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++)
340 if (strcmp(name, secstrs + s->sh_name) == 0)
341 return s;
342
343 return NULL;
344}
345
346extern void fixup_pv_table(const void *, unsigned long);
347extern void fixup_smp(const void *, unsigned long);
348
349int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
350 struct module *mod)
351{
352 const Elf_Shdr *s = NULL;
353#ifdef CONFIG_ARM_UNWIND
354 const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
355 const Elf_Shdr *sechdrs_end = sechdrs + hdr->e_shnum;
356 struct list_head *unwind_list = &mod->arch.unwind_list;
357
358 INIT_LIST_HEAD(unwind_list);
359 mod->arch.init_table = NULL;
360
361 for (s = sechdrs; s < sechdrs_end; s++) {
362 const char *secname = secstrs + s->sh_name;
363 const char *txtname;
364 const Elf_Shdr *txt_sec;
365
366 if (!(s->sh_flags & SHF_ALLOC) ||
367 s->sh_type != ELF_SECTION_UNWIND)
368 continue;
369
370 if (!strcmp(".ARM.exidx", secname))
371 txtname = ".text";
372 else
373 txtname = secname + strlen(".ARM.exidx");
374 txt_sec = find_mod_section(hdr, sechdrs, txtname);
375
376 if (txt_sec) {
377 struct unwind_table *table =
378 unwind_table_add(s->sh_addr,
379 s->sh_size,
380 txt_sec->sh_addr,
381 txt_sec->sh_size);
382
383 list_add(&table->mod_list, unwind_list);
384
385 /* save init table for module_arch_freeing_init */
386 if (strcmp(".ARM.exidx.init.text", secname) == 0)
387 mod->arch.init_table = table;
388 }
389 }
390#endif
391#ifdef CONFIG_ARM_PATCH_PHYS_VIRT
392 s = find_mod_section(hdr, sechdrs, ".pv_table");
393 if (s)
394 fixup_pv_table((void *)s->sh_addr, s->sh_size);
395#endif
396 s = find_mod_section(hdr, sechdrs, ".alt.smp.init");
397 if (s && !is_smp())
398#ifdef CONFIG_SMP_ON_UP
399 fixup_smp((void *)s->sh_addr, s->sh_size);
400#else
401 return -EINVAL;
402#endif
403 return 0;
404}
405
406void
407module_arch_cleanup(struct module *mod)
408{
409#ifdef CONFIG_ARM_UNWIND
410 struct unwind_table *tmp;
411 struct unwind_table *n;
412
413 list_for_each_entry_safe(tmp, n,
414 &mod->arch.unwind_list, mod_list) {
415 list_del(&tmp->mod_list);
416 unwind_table_del(tmp);
417 }
418 mod->arch.init_table = NULL;
419#endif
420}
421
422void __weak module_arch_freeing_init(struct module *mod)
423{
424#ifdef CONFIG_ARM_UNWIND
425 struct unwind_table *init = mod->arch.init_table;
426
427 if (init) {
428 mod->arch.init_table = NULL;
429 list_del(&init->mod_list);
430 unwind_table_del(init);
431 }
432#endif
433}