xref: /linux-6.15/arch/riscv/kernel/module.c (revision ca5999fd)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e2c0cdfbSPalmer Dabbelt /*
3e2c0cdfbSPalmer Dabbelt  *
4e2c0cdfbSPalmer Dabbelt  *  Copyright (C) 2017 Zihao Yu
5e2c0cdfbSPalmer Dabbelt  */
6e2c0cdfbSPalmer Dabbelt 
7e2c0cdfbSPalmer Dabbelt #include <linux/elf.h>
8e2c0cdfbSPalmer Dabbelt #include <linux/err.h>
9e2c0cdfbSPalmer Dabbelt #include <linux/errno.h>
10e2c0cdfbSPalmer Dabbelt #include <linux/moduleloader.h>
110cff8bffSVincent Chen #include <linux/vmalloc.h>
120cff8bffSVincent Chen #include <linux/sizes.h>
13*ca5999fdSMike Rapoport #include <linux/pgtable.h>
140cff8bffSVincent Chen #include <asm/sections.h>
15e2c0cdfbSPalmer Dabbelt 
1677aa85deSAndreas Schwab static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
1777aa85deSAndreas Schwab {
1877aa85deSAndreas Schwab 	if (v != (u32)v) {
1977aa85deSAndreas Schwab 		pr_err("%s: value %016llx out of range for 32-bit field\n",
20ef3a6140SOlof Johansson 		       me->name, (long long)v);
2177aa85deSAndreas Schwab 		return -EINVAL;
2277aa85deSAndreas Schwab 	}
2377aa85deSAndreas Schwab 	*location = v;
2477aa85deSAndreas Schwab 	return 0;
2577aa85deSAndreas Schwab }
2677aa85deSAndreas Schwab 
27e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
28e2c0cdfbSPalmer Dabbelt {
29e2c0cdfbSPalmer Dabbelt 	*(u64 *)location = v;
30e2c0cdfbSPalmer Dabbelt 	return 0;
31e2c0cdfbSPalmer Dabbelt }
32e2c0cdfbSPalmer Dabbelt 
33e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
34e2c0cdfbSPalmer Dabbelt 				     Elf_Addr v)
35e2c0cdfbSPalmer Dabbelt {
367df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
37e2c0cdfbSPalmer Dabbelt 	u32 imm12 = (offset & 0x1000) << (31 - 12);
38e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) >> (11 - 7);
39e2c0cdfbSPalmer Dabbelt 	u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
40e2c0cdfbSPalmer Dabbelt 	u32 imm4_1 = (offset & 0x1e) << (11 - 4);
41e2c0cdfbSPalmer Dabbelt 
42e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
43e2c0cdfbSPalmer Dabbelt 	return 0;
44e2c0cdfbSPalmer Dabbelt }
45e2c0cdfbSPalmer Dabbelt 
46e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
47e2c0cdfbSPalmer Dabbelt 				  Elf_Addr v)
48e2c0cdfbSPalmer Dabbelt {
497df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
50e2c0cdfbSPalmer Dabbelt 	u32 imm20 = (offset & 0x100000) << (31 - 20);
51e2c0cdfbSPalmer Dabbelt 	u32 imm19_12 = (offset & 0xff000);
52e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) << (20 - 11);
53e2c0cdfbSPalmer Dabbelt 	u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
54e2c0cdfbSPalmer Dabbelt 
55e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
56e2c0cdfbSPalmer Dabbelt 	return 0;
57e2c0cdfbSPalmer Dabbelt }
58e2c0cdfbSPalmer Dabbelt 
5956ea45aeSZong Li static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location,
6056ea45aeSZong Li 					 Elf_Addr v)
6156ea45aeSZong Li {
627df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
6356ea45aeSZong Li 	u16 imm8 = (offset & 0x100) << (12 - 8);
6456ea45aeSZong Li 	u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
6556ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
6656ea45aeSZong Li 	u16 imm4_3 = (offset & 0x18) << (12 - 5);
6756ea45aeSZong Li 	u16 imm2_1 = (offset & 0x6) << (12 - 10);
6856ea45aeSZong Li 
6956ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe383) |
7056ea45aeSZong Li 		    imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
7156ea45aeSZong Li 	return 0;
7256ea45aeSZong Li }
7356ea45aeSZong Li 
7456ea45aeSZong Li static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
7556ea45aeSZong Li 				       Elf_Addr v)
7656ea45aeSZong Li {
777df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
7856ea45aeSZong Li 	u16 imm11 = (offset & 0x800) << (12 - 11);
7956ea45aeSZong Li 	u16 imm10 = (offset & 0x400) >> (10 - 8);
8056ea45aeSZong Li 	u16 imm9_8 = (offset & 0x300) << (12 - 11);
8156ea45aeSZong Li 	u16 imm7 = (offset & 0x80) >> (7 - 6);
8256ea45aeSZong Li 	u16 imm6 = (offset & 0x40) << (12 - 11);
8356ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
8456ea45aeSZong Li 	u16 imm4 = (offset & 0x10) << (12 - 5);
8556ea45aeSZong Li 	u16 imm3_1 = (offset & 0xe) << (12 - 10);
8656ea45aeSZong Li 
8756ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe003) |
8856ea45aeSZong Li 		    imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
8956ea45aeSZong Li 	return 0;
9056ea45aeSZong Li }
9156ea45aeSZong Li 
92e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
93e2c0cdfbSPalmer Dabbelt 					 Elf_Addr v)
94e2c0cdfbSPalmer Dabbelt {
957df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
96e2c0cdfbSPalmer Dabbelt 	s32 hi20;
97e2c0cdfbSPalmer Dabbelt 
98e2c0cdfbSPalmer Dabbelt 	if (offset != (s32)offset) {
99e2c0cdfbSPalmer Dabbelt 		pr_err(
100e2c0cdfbSPalmer Dabbelt 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
101ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
102e2c0cdfbSPalmer Dabbelt 		return -EINVAL;
103e2c0cdfbSPalmer Dabbelt 	}
104e2c0cdfbSPalmer Dabbelt 
105e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
106e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
107e2c0cdfbSPalmer Dabbelt 	return 0;
108e2c0cdfbSPalmer Dabbelt }
109e2c0cdfbSPalmer Dabbelt 
110e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
111e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
112e2c0cdfbSPalmer Dabbelt {
113e2c0cdfbSPalmer Dabbelt 	/*
114e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
115e2c0cdfbSPalmer Dabbelt 	 * handler.
116e2c0cdfbSPalmer Dabbelt 	 */
117e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfffff) | ((v & 0xfff) << 20);
118e2c0cdfbSPalmer Dabbelt 	return 0;
119e2c0cdfbSPalmer Dabbelt }
120e2c0cdfbSPalmer Dabbelt 
121e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
122e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
123e2c0cdfbSPalmer Dabbelt {
124e2c0cdfbSPalmer Dabbelt 	/*
125e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
126e2c0cdfbSPalmer Dabbelt 	 * handler.
127e2c0cdfbSPalmer Dabbelt 	 */
128e2c0cdfbSPalmer Dabbelt 	u32 imm11_5 = (v & 0xfe0) << (31 - 11);
129e2c0cdfbSPalmer Dabbelt 	u32 imm4_0 = (v & 0x1f) << (11 - 4);
130e2c0cdfbSPalmer Dabbelt 
131e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
132e2c0cdfbSPalmer Dabbelt 	return 0;
133e2c0cdfbSPalmer Dabbelt }
134e2c0cdfbSPalmer Dabbelt 
135e7456e69SZong Li static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
136e7456e69SZong Li 				   Elf_Addr v)
137e7456e69SZong Li {
138e7456e69SZong Li 	s32 hi20;
139e7456e69SZong Li 
140da4ed378SJoe Perches 	if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
141e7456e69SZong Li 		pr_err(
142e7456e69SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
143ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
144e7456e69SZong Li 		return -EINVAL;
145e7456e69SZong Li 	}
146e7456e69SZong Li 
147e7456e69SZong Li 	hi20 = ((s32)v + 0x800) & 0xfffff000;
148e7456e69SZong Li 	*location = (*location & 0xfff) | hi20;
149e7456e69SZong Li 	return 0;
150e7456e69SZong Li }
151e7456e69SZong Li 
152e7456e69SZong Li static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location,
153e7456e69SZong Li 				     Elf_Addr v)
154e7456e69SZong Li {
155e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
156e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
157e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
158e7456e69SZong Li 	*location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
159e7456e69SZong Li 	return 0;
160e7456e69SZong Li }
161e7456e69SZong Li 
162e7456e69SZong Li static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location,
163e7456e69SZong Li 				     Elf_Addr v)
164e7456e69SZong Li {
165e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
166e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
167e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
168e7456e69SZong Li 	u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
169e7456e69SZong Li 	u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
170e7456e69SZong Li 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
171e7456e69SZong Li 	return 0;
172e7456e69SZong Li }
173e7456e69SZong Li 
174da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
175da975dd4SZong Li 				       Elf_Addr v)
176da975dd4SZong Li {
1777df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
178da975dd4SZong Li 	s32 hi20;
179da975dd4SZong Li 
180da975dd4SZong Li 	/* Always emit the got entry */
181da975dd4SZong Li 	if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
182da975dd4SZong Li 		offset = module_emit_got_entry(me, v);
183da975dd4SZong Li 		offset = (void *)offset - (void *)location;
184da975dd4SZong Li 	} else {
185da975dd4SZong Li 		pr_err(
186da975dd4SZong Li 		  "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
187ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
188da975dd4SZong Li 		return -EINVAL;
189da975dd4SZong Li 	}
190da975dd4SZong Li 
191da975dd4SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
192da975dd4SZong Li 	*location = (*location & 0xfff) | hi20;
193da975dd4SZong Li 	return 0;
194da975dd4SZong Li }
195da975dd4SZong Li 
196e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
197e2c0cdfbSPalmer Dabbelt 				       Elf_Addr v)
198e2c0cdfbSPalmer Dabbelt {
1997df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
200e2c0cdfbSPalmer Dabbelt 	s32 fill_v = offset;
201e2c0cdfbSPalmer Dabbelt 	u32 hi20, lo12;
202e2c0cdfbSPalmer Dabbelt 
203e2c0cdfbSPalmer Dabbelt 	if (offset != fill_v) {
204da975dd4SZong Li 		/* Only emit the plt entry if offset over 32-bit range */
205da975dd4SZong Li 		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
206da975dd4SZong Li 			offset = module_emit_plt_entry(me, v);
207da975dd4SZong Li 			offset = (void *)offset - (void *)location;
208da975dd4SZong Li 		} else {
209e2c0cdfbSPalmer Dabbelt 			pr_err(
210e2c0cdfbSPalmer Dabbelt 			  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
211ef3a6140SOlof Johansson 			  me->name, (long long)v, location);
212e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
213e2c0cdfbSPalmer Dabbelt 		}
214da975dd4SZong Li 	}
215e2c0cdfbSPalmer Dabbelt 
216e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
217e2c0cdfbSPalmer Dabbelt 	lo12 = (offset - hi20) & 0xfff;
218e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
219e2c0cdfbSPalmer Dabbelt 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
220e2c0cdfbSPalmer Dabbelt 	return 0;
221e2c0cdfbSPalmer Dabbelt }
222e2c0cdfbSPalmer Dabbelt 
223e1910c72SZong Li static int apply_r_riscv_call_rela(struct module *me, u32 *location,
224e1910c72SZong Li 				   Elf_Addr v)
225e1910c72SZong Li {
2267df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
227e1910c72SZong Li 	s32 fill_v = offset;
228e1910c72SZong Li 	u32 hi20, lo12;
229e1910c72SZong Li 
230e1910c72SZong Li 	if (offset != fill_v) {
231e1910c72SZong Li 		pr_err(
232e1910c72SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
233ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
234e1910c72SZong Li 		return -EINVAL;
235e1910c72SZong Li 	}
236e1910c72SZong Li 
237e1910c72SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
238e1910c72SZong Li 	lo12 = (offset - hi20) & 0xfff;
239e1910c72SZong Li 	*location = (*location & 0xfff) | hi20;
240e1910c72SZong Li 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
241e1910c72SZong Li 	return 0;
242e1910c72SZong Li }
243e1910c72SZong Li 
244e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
245e2c0cdfbSPalmer Dabbelt 				    Elf_Addr v)
246e2c0cdfbSPalmer Dabbelt {
247e2c0cdfbSPalmer Dabbelt 	return 0;
248e2c0cdfbSPalmer Dabbelt }
249e2c0cdfbSPalmer Dabbelt 
25029e405cdSZong Li static int apply_r_riscv_align_rela(struct module *me, u32 *location,
25129e405cdSZong Li 				    Elf_Addr v)
25229e405cdSZong Li {
25329e405cdSZong Li 	pr_err(
25429e405cdSZong Li 	  "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
25529e405cdSZong Li 	  me->name, location);
25629e405cdSZong Li 	return -EINVAL;
25729e405cdSZong Li }
25829e405cdSZong Li 
2598e691b16SZong Li static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
2608e691b16SZong Li 				    Elf_Addr v)
2618e691b16SZong Li {
262781c8fe2SAndreas Schwab 	*(u32 *)location += (u32)v;
2638e691b16SZong Li 	return 0;
2648e691b16SZong Li }
2658e691b16SZong Li 
2664aad074cSZong Li static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
2674aad074cSZong Li 				    Elf_Addr v)
2684aad074cSZong Li {
269781c8fe2SAndreas Schwab 	*(u32 *)location -= (u32)v;
2704aad074cSZong Li 	return 0;
2714aad074cSZong Li }
2724aad074cSZong Li 
273e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
274e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
27577aa85deSAndreas Schwab 	[R_RISCV_32]			= apply_r_riscv_32_rela,
276e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
277e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
278e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
27956ea45aeSZong Li 	[R_RISCV_RVC_BRANCH]		= apply_r_riscv_rcv_branch_rela,
28056ea45aeSZong Li 	[R_RISCV_RVC_JUMP]		= apply_r_riscv_rvc_jump_rela,
281e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
282e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
283e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
284e7456e69SZong Li 	[R_RISCV_HI20]			= apply_r_riscv_hi20_rela,
285e7456e69SZong Li 	[R_RISCV_LO12_I]		= apply_r_riscv_lo12_i_rela,
286e7456e69SZong Li 	[R_RISCV_LO12_S]		= apply_r_riscv_lo12_s_rela,
287da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
288e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
289e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
290e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
29129e405cdSZong Li 	[R_RISCV_ALIGN]			= apply_r_riscv_align_rela,
2928e691b16SZong Li 	[R_RISCV_ADD32]			= apply_r_riscv_add32_rela,
2934aad074cSZong Li 	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
294e2c0cdfbSPalmer Dabbelt };
295e2c0cdfbSPalmer Dabbelt 
296e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
297e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
298e2c0cdfbSPalmer Dabbelt 		       struct module *me)
299e2c0cdfbSPalmer Dabbelt {
300e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
301e2c0cdfbSPalmer Dabbelt 	int (*handler)(struct module *me, u32 *location, Elf_Addr v);
302e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
303e2c0cdfbSPalmer Dabbelt 	u32 *location;
304e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
305e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
306e2c0cdfbSPalmer Dabbelt 	int res;
307e2c0cdfbSPalmer Dabbelt 
308e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
309e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
310e2c0cdfbSPalmer Dabbelt 
311e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
312e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
313e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
314e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
315e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
316e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
317e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
318e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
319e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
320e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
321e2c0cdfbSPalmer Dabbelt 				continue;
32204ce8d3fSKefeng Wang 			pr_warn("%s: Unknown symbol %s\n",
323e2c0cdfbSPalmer Dabbelt 				me->name, strtab + sym->st_name);
324e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
325e2c0cdfbSPalmer Dabbelt 		}
326e2c0cdfbSPalmer Dabbelt 
327e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
328e2c0cdfbSPalmer Dabbelt 
329e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
330e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
331e2c0cdfbSPalmer Dabbelt 		else
332e2c0cdfbSPalmer Dabbelt 			handler = NULL;
333e2c0cdfbSPalmer Dabbelt 
334e2c0cdfbSPalmer Dabbelt 		if (!handler) {
335e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
336e2c0cdfbSPalmer Dabbelt 			       me->name, type);
337e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
338e2c0cdfbSPalmer Dabbelt 		}
339e2c0cdfbSPalmer Dabbelt 
340e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
341e2c0cdfbSPalmer Dabbelt 
342e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
343e2c0cdfbSPalmer Dabbelt 			unsigned int j;
344e2c0cdfbSPalmer Dabbelt 
345e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
3467df85002SZong Li 				unsigned long hi20_loc =
347e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
348e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
349da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
350da975dd4SZong Li 
351da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
352da975dd4SZong Li 				if (hi20_loc == sym->st_value
353da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
354da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
355da975dd4SZong Li 					s32 hi20, lo12;
356e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
357e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
358e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
3597df85002SZong Li 					unsigned long hi20_sym_val =
360e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
361e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
362da975dd4SZong Li 
363e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
3647df85002SZong Li 					size_t offset = hi20_sym_val - hi20_loc;
365da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
366da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
367da975dd4SZong Li 						offset = module_emit_got_entry(
368da975dd4SZong Li 							 me, hi20_sym_val);
369da975dd4SZong Li 						offset = offset - hi20_loc;
370da975dd4SZong Li 					}
371da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
372da975dd4SZong Li 					lo12 = offset - hi20;
373e2c0cdfbSPalmer Dabbelt 					v = lo12;
374da975dd4SZong Li 
375e2c0cdfbSPalmer Dabbelt 					break;
376e2c0cdfbSPalmer Dabbelt 				}
377e2c0cdfbSPalmer Dabbelt 			}
378e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
379e2c0cdfbSPalmer Dabbelt 				pr_err(
380da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
381e2c0cdfbSPalmer Dabbelt 				  me->name);
382e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
383e2c0cdfbSPalmer Dabbelt 			}
384e2c0cdfbSPalmer Dabbelt 		}
385e2c0cdfbSPalmer Dabbelt 
386e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
387e2c0cdfbSPalmer Dabbelt 		if (res)
388e2c0cdfbSPalmer Dabbelt 			return res;
389e2c0cdfbSPalmer Dabbelt 	}
390e2c0cdfbSPalmer Dabbelt 
391e2c0cdfbSPalmer Dabbelt 	return 0;
392e2c0cdfbSPalmer Dabbelt }
3930cff8bffSVincent Chen 
3940cff8bffSVincent Chen #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
3950cff8bffSVincent Chen #define VMALLOC_MODULE_START \
3960cff8bffSVincent Chen 	 max(PFN_ALIGN((unsigned long)&_end - SZ_2G), VMALLOC_START)
3970cff8bffSVincent Chen void *module_alloc(unsigned long size)
3980cff8bffSVincent Chen {
3990cff8bffSVincent Chen 	return __vmalloc_node_range(size, 1, VMALLOC_MODULE_START,
4000cff8bffSVincent Chen 				    VMALLOC_END, GFP_KERNEL,
4010cff8bffSVincent Chen 				    PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
4020cff8bffSVincent Chen 				    __builtin_return_address(0));
4030cff8bffSVincent Chen }
4040cff8bffSVincent Chen #endif
405