blob: 63a1ba67900627aac2f877bd018ec05f6ec5d9ff [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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/mm.h>
17#include <linux/elf.h>
18#include <linux/vmalloc.h>
19#include <linux/fs.h>
20#include <linux/string.h>
21#include <linux/gfp.h>
22
23#include <asm/pgtable.h>
24#include <asm/sections.h>
25#include <asm/smp_plat.h>
26#include <asm/unwind.h>
27
28#ifdef CONFIG_XIP_KERNEL
29/*
30 * The XIP kernel text is mapped in the module area for modules and
31 * some other stuff to work without any indirect relocations.
32 * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
33 * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
34 */
35#undef MODULES_VADDR
36#define MODULES_VADDR (((unsigned long)_etext + ~PMD_MASK) & PMD_MASK)
37#endif
38
39#ifdef CONFIG_MMU
40void *module_alloc(unsigned long size)
41{
42 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
43 GFP_KERNEL, PAGE_KERNEL_EXEC, -1,
44 __builtin_return_address(0));
45}
46#endif
47
48int
49apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
50 unsigned int relindex, struct module *module)
51{
52 Elf32_Shdr *symsec = sechdrs + symindex;
53 Elf32_Shdr *relsec = sechdrs + relindex;
54 Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
55 Elf32_Rel *rel = (void *)relsec->sh_addr;
56 unsigned int i;
57
58 for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
59 unsigned long loc;
60 Elf32_Sym *sym;
61 const char *symname;
62 s32 offset;
63#if defined(CONFIG_THUMB2_KERNEL) || defined(USE_CPPS_KO)
64 u32 upper, lower, sign, j1, j2;
65#endif
66
67 offset = ELF32_R_SYM(rel->r_info);
68 if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
69 pr_err("%s: section %u reloc %u: bad relocation sym offset\n",
70 module->name, relindex, i);
71 return -ENOEXEC;
72 }
73
74 sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
75 symname = strtab + sym->st_name;
76
77 if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
78 pr_err("%s: section %u reloc %u sym '%s': out of bounds relocation, offset %d size %u\n",
79 module->name, relindex, i, symname,
80 rel->r_offset, dstsec->sh_size);
81 return -ENOEXEC;
82 }
83
84 loc = dstsec->sh_addr + rel->r_offset;
85
86 switch (ELF32_R_TYPE(rel->r_info)) {
87 case R_ARM_NONE:
88 /* ignore */
89 break;
90
91 case R_ARM_ABS32:
92 case R_ARM_TARGET1:
93 *(u32 *)loc += sym->st_value;
94 break;
95
96 case R_ARM_REL32:
97 *(u32 *)loc += sym->st_value - loc;
98 break;
99
100 case R_ARM_PC24:
101 case R_ARM_CALL:
102 case R_ARM_JUMP24:
103 offset = (*(u32 *)loc & 0x00ffffff) << 2;
104 if (offset & 0x02000000)
105 offset -= 0x04000000;
106
107 offset += sym->st_value - loc;
108 if (offset & 3 ||
109 offset <= (s32)0xfe000000 ||
110 offset >= (s32)0x02000000) {
111 pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
112 module->name, relindex, i, symname,
113 ELF32_R_TYPE(rel->r_info), loc,
114 sym->st_value);
115 return -ENOEXEC;
116 }
117
118 offset >>= 2;
119
120 *(u32 *)loc &= 0xff000000;
121 *(u32 *)loc |= offset & 0x00ffffff;
122 break;
123
124 case R_ARM_V4BX:
125 /* Preserve Rm and the condition code. Alter
126 * other bits to re-code instruction as
127 * MOV PC,Rm.
128 */
129 *(u32 *)loc &= 0xf000000f;
130 *(u32 *)loc |= 0x01a0f000;
131 break;
132
133 case R_ARM_PREL31:
134 offset = *(u32 *)loc + sym->st_value - loc;
135 *(u32 *)loc = offset & 0x7fffffff;
136 break;
137
138 case R_ARM_MOVW_ABS_NC:
139 case R_ARM_MOVT_ABS:
140 offset = *(u32 *)loc;
141 offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
142 offset = (offset ^ 0x8000) - 0x8000;
143
144 offset += sym->st_value;
145 if (ELF32_R_TYPE(rel->r_info) == R_ARM_MOVT_ABS)
146 offset >>= 16;
147
148 *(u32 *)loc &= 0xfff0f000;
149 *(u32 *)loc |= ((offset & 0xf000) << 4) |
150 (offset & 0x0fff);
151 break;
152
153#if defined(CONFIG_THUMB2_KERNEL) || defined(USE_CPPS_KO)
154 case R_ARM_THM_CALL:
155 case R_ARM_THM_JUMP24:
156 upper = *(u16 *)loc;
157 lower = *(u16 *)(loc + 2);
158
159 /*
160 * 25 bit signed address range (Thumb-2 BL and B.W
161 * instructions):
162 * S:I1:I2:imm10:imm11:0
163 * where:
164 * S = upper[10] = offset[24]
165 * I1 = ~(J1 ^ S) = offset[23]
166 * I2 = ~(J2 ^ S) = offset[22]
167 * imm10 = upper[9:0] = offset[21:12]
168 * imm11 = lower[10:0] = offset[11:1]
169 * J1 = lower[13]
170 * J2 = lower[11]
171 */
172 sign = (upper >> 10) & 1;
173 j1 = (lower >> 13) & 1;
174 j2 = (lower >> 11) & 1;
175 offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
176 ((~(j2 ^ sign) & 1) << 22) |
177 ((upper & 0x03ff) << 12) |
178 ((lower & 0x07ff) << 1);
179 if (offset & 0x01000000)
180 offset -= 0x02000000;
181 offset += sym->st_value - loc;
182
183 /*
184 * For function symbols, only Thumb addresses are
185 * allowed (no interworking).
186 *
187 * For non-function symbols, the destination
188 * has no specific ARM/Thumb disposition, so
189 * the branch is resolved under the assumption
190 * that interworking is not required.
191 */
192 if ((ELF32_ST_TYPE(sym->st_info) == STT_FUNC &&
193 !(offset & 1)) ||
194 offset <= (s32)0xff000000 ||
195 offset >= (s32)0x01000000) {
196 pr_err("%s: section thumb2 %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
197 module->name, relindex, i, symname,
198 ELF32_R_TYPE(rel->r_info), loc,
199 sym->st_value);
200 return -ENOEXEC;
201 }
202
203 sign = (offset >> 24) & 1;
204 j1 = sign ^ (~(offset >> 23) & 1);
205 j2 = sign ^ (~(offset >> 22) & 1);
206 *(u16 *)loc = (u16)((upper & 0xf800) | (sign << 10) |
207 ((offset >> 12) & 0x03ff));
208 *(u16 *)(loc + 2) = (u16)((lower & 0xd000) |
209 (j1 << 13) | (j2 << 11) |
210 ((offset >> 1) & 0x07ff));
211 break;
212
213 case R_ARM_THM_MOVW_ABS_NC:
214 case R_ARM_THM_MOVT_ABS:
215 upper = *(u16 *)loc;
216 lower = *(u16 *)(loc + 2);
217
218 /*
219 * MOVT/MOVW instructions encoding in Thumb-2:
220 *
221 * i = upper[10]
222 * imm4 = upper[3:0]
223 * imm3 = lower[14:12]
224 * imm8 = lower[7:0]
225 *
226 * imm16 = imm4:i:imm3:imm8
227 */
228 offset = ((upper & 0x000f) << 12) |
229 ((upper & 0x0400) << 1) |
230 ((lower & 0x7000) >> 4) | (lower & 0x00ff);
231 offset = (offset ^ 0x8000) - 0x8000;
232 offset += sym->st_value;
233
234 if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
235 offset >>= 16;
236
237 *(u16 *)loc = (u16)((upper & 0xfbf0) |
238 ((offset & 0xf000) >> 12) |
239 ((offset & 0x0800) >> 1));
240 *(u16 *)(loc + 2) = (u16)((lower & 0x8f00) |
241 ((offset & 0x0700) << 4) |
242 (offset & 0x00ff));
243 break;
244#endif
245
246 default:
247 printk(KERN_ERR "%s: unknown relocation: %u\n",
248 module->name, ELF32_R_TYPE(rel->r_info));
249 return -ENOEXEC;
250 }
251 }
252 return 0;
253}
254
255struct mod_unwind_map {
256 const Elf_Shdr *unw_sec;
257 const Elf_Shdr *txt_sec;
258};
259
260static const Elf_Shdr *find_mod_section(const Elf32_Ehdr *hdr,
261 const Elf_Shdr *sechdrs, const char *name)
262{
263 const Elf_Shdr *s, *se;
264 const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
265
266 for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++)
267 if (strcmp(name, secstrs + s->sh_name) == 0)
268 return s;
269
270 return NULL;
271}
272
273extern void fixup_pv_table(const void *, unsigned long);
274extern void fixup_smp(const void *, unsigned long);
275
276int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
277 struct module *mod)
278{
279 const Elf_Shdr *s = NULL;
280#ifdef CONFIG_ARM_UNWIND
281 const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
282 const Elf_Shdr *sechdrs_end = sechdrs + hdr->e_shnum;
283 struct mod_unwind_map maps[ARM_SEC_MAX];
284 int i;
285
286 memset(maps, 0, sizeof(maps));
287
288 for (s = sechdrs; s < sechdrs_end; s++) {
289 const char *secname = secstrs + s->sh_name;
290
291 if (!(s->sh_flags & SHF_ALLOC))
292 continue;
293
294 if (strcmp(".ARM.exidx.init.text", secname) == 0)
295 maps[ARM_SEC_INIT].unw_sec = s;
296 else if (strcmp(".ARM.exidx.devinit.text", secname) == 0)
297 maps[ARM_SEC_DEVINIT].unw_sec = s;
298 else if (strcmp(".ARM.exidx", secname) == 0)
299 maps[ARM_SEC_CORE].unw_sec = s;
300 else if (strcmp(".ARM.exidx.exit.text", secname) == 0)
301 maps[ARM_SEC_EXIT].unw_sec = s;
302 else if (strcmp(".ARM.exidx.devexit.text", secname) == 0)
303 maps[ARM_SEC_DEVEXIT].unw_sec = s;
304 else if (strcmp(".init.text", secname) == 0)
305 maps[ARM_SEC_INIT].txt_sec = s;
306 else if (strcmp(".devinit.text", secname) == 0)
307 maps[ARM_SEC_DEVINIT].txt_sec = s;
308 else if (strcmp(".text", secname) == 0)
309 maps[ARM_SEC_CORE].txt_sec = s;
310 else if (strcmp(".exit.text", secname) == 0)
311 maps[ARM_SEC_EXIT].txt_sec = s;
312 else if (strcmp(".devexit.text", secname) == 0)
313 maps[ARM_SEC_DEVEXIT].txt_sec = s;
314 }
315
316 for (i = 0; i < ARM_SEC_MAX; i++)
317 if (maps[i].unw_sec && maps[i].txt_sec)
318 mod->arch.unwind[i] =
319 unwind_table_add(maps[i].unw_sec->sh_addr,
320 maps[i].unw_sec->sh_size,
321 maps[i].txt_sec->sh_addr,
322 maps[i].txt_sec->sh_size);
323#endif
324#ifdef CONFIG_ARM_PATCH_PHYS_VIRT
325 s = find_mod_section(hdr, sechdrs, ".pv_table");
326 if (s)
327 fixup_pv_table((void *)s->sh_addr, s->sh_size);
328#endif
329 s = find_mod_section(hdr, sechdrs, ".alt.smp.init");
330 if (s && !is_smp())
331#ifdef CONFIG_SMP_ON_UP
332 fixup_smp((void *)s->sh_addr, s->sh_size);
333#else
334 return -EINVAL;
335#endif
336 return 0;
337}
338
339void
340module_arch_cleanup(struct module *mod)
341{
342#ifdef CONFIG_ARM_UNWIND
343 int i;
344
345 for (i = 0; i < ARM_SEC_MAX; i++)
346 if (mod->arch.unwind[i])
347 unwind_table_del(mod->arch.unwind[i]);
348#endif
349}