xref: /openbmc/linux/arch/riscv/kernel/module.c (revision a8e91016)
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>
13ca5999fdSMike Rapoport #include <linux/pgtable.h>
14*a8e91016SHeiko Stuebner #include <asm/alternative.h>
150cff8bffSVincent Chen #include <asm/sections.h>
16e2c0cdfbSPalmer Dabbelt 
170966d385SEmil Renner Berthing /*
180966d385SEmil Renner Berthing  * The auipc+jalr instruction pair can reach any PC-relative offset
190966d385SEmil Renner Berthing  * in the range [-2^31 - 2^11, 2^31 - 2^11)
200966d385SEmil Renner Berthing  */
210966d385SEmil Renner Berthing static bool riscv_insn_valid_32bit_offset(ptrdiff_t val)
220966d385SEmil Renner Berthing {
230966d385SEmil Renner Berthing #ifdef CONFIG_32BIT
240966d385SEmil Renner Berthing 	return true;
250966d385SEmil Renner Berthing #else
260966d385SEmil Renner Berthing 	return (-(1L << 31) - (1L << 11)) <= val && val < ((1L << 31) - (1L << 11));
270966d385SEmil Renner Berthing #endif
280966d385SEmil Renner Berthing }
290966d385SEmil Renner Berthing 
3077aa85deSAndreas Schwab static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
3177aa85deSAndreas Schwab {
3277aa85deSAndreas Schwab 	if (v != (u32)v) {
3377aa85deSAndreas Schwab 		pr_err("%s: value %016llx out of range for 32-bit field\n",
34ef3a6140SOlof Johansson 		       me->name, (long long)v);
3577aa85deSAndreas Schwab 		return -EINVAL;
3677aa85deSAndreas Schwab 	}
3777aa85deSAndreas Schwab 	*location = v;
3877aa85deSAndreas Schwab 	return 0;
3977aa85deSAndreas Schwab }
4077aa85deSAndreas Schwab 
41e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
42e2c0cdfbSPalmer Dabbelt {
43e2c0cdfbSPalmer Dabbelt 	*(u64 *)location = v;
44e2c0cdfbSPalmer Dabbelt 	return 0;
45e2c0cdfbSPalmer Dabbelt }
46e2c0cdfbSPalmer Dabbelt 
47e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
48e2c0cdfbSPalmer Dabbelt 				     Elf_Addr v)
49e2c0cdfbSPalmer Dabbelt {
507df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
51e2c0cdfbSPalmer Dabbelt 	u32 imm12 = (offset & 0x1000) << (31 - 12);
52e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) >> (11 - 7);
53e2c0cdfbSPalmer Dabbelt 	u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
54e2c0cdfbSPalmer Dabbelt 	u32 imm4_1 = (offset & 0x1e) << (11 - 4);
55e2c0cdfbSPalmer Dabbelt 
56e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
57e2c0cdfbSPalmer Dabbelt 	return 0;
58e2c0cdfbSPalmer Dabbelt }
59e2c0cdfbSPalmer Dabbelt 
60e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
61e2c0cdfbSPalmer Dabbelt 				  Elf_Addr v)
62e2c0cdfbSPalmer Dabbelt {
637df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
64e2c0cdfbSPalmer Dabbelt 	u32 imm20 = (offset & 0x100000) << (31 - 20);
65e2c0cdfbSPalmer Dabbelt 	u32 imm19_12 = (offset & 0xff000);
66e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) << (20 - 11);
67e2c0cdfbSPalmer Dabbelt 	u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
68e2c0cdfbSPalmer Dabbelt 
69e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
70e2c0cdfbSPalmer Dabbelt 	return 0;
71e2c0cdfbSPalmer Dabbelt }
72e2c0cdfbSPalmer Dabbelt 
7361748760SWu Caize static int apply_r_riscv_rvc_branch_rela(struct module *me, u32 *location,
7456ea45aeSZong Li 					 Elf_Addr v)
7556ea45aeSZong Li {
767df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
7756ea45aeSZong Li 	u16 imm8 = (offset & 0x100) << (12 - 8);
7856ea45aeSZong Li 	u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
7956ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
8056ea45aeSZong Li 	u16 imm4_3 = (offset & 0x18) << (12 - 5);
8156ea45aeSZong Li 	u16 imm2_1 = (offset & 0x6) << (12 - 10);
8256ea45aeSZong Li 
8356ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe383) |
8456ea45aeSZong Li 		    imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
8556ea45aeSZong Li 	return 0;
8656ea45aeSZong Li }
8756ea45aeSZong Li 
8856ea45aeSZong Li static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
8956ea45aeSZong Li 				       Elf_Addr v)
9056ea45aeSZong Li {
917df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
9256ea45aeSZong Li 	u16 imm11 = (offset & 0x800) << (12 - 11);
9356ea45aeSZong Li 	u16 imm10 = (offset & 0x400) >> (10 - 8);
9456ea45aeSZong Li 	u16 imm9_8 = (offset & 0x300) << (12 - 11);
9556ea45aeSZong Li 	u16 imm7 = (offset & 0x80) >> (7 - 6);
9656ea45aeSZong Li 	u16 imm6 = (offset & 0x40) << (12 - 11);
9756ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
9856ea45aeSZong Li 	u16 imm4 = (offset & 0x10) << (12 - 5);
9956ea45aeSZong Li 	u16 imm3_1 = (offset & 0xe) << (12 - 10);
10056ea45aeSZong Li 
10156ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe003) |
10256ea45aeSZong Li 		    imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
10356ea45aeSZong Li 	return 0;
10456ea45aeSZong Li }
10556ea45aeSZong Li 
106e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
107e2c0cdfbSPalmer Dabbelt 					 Elf_Addr v)
108e2c0cdfbSPalmer Dabbelt {
1097df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
110e2c0cdfbSPalmer Dabbelt 	s32 hi20;
111e2c0cdfbSPalmer Dabbelt 
1120966d385SEmil Renner Berthing 	if (!riscv_insn_valid_32bit_offset(offset)) {
113e2c0cdfbSPalmer Dabbelt 		pr_err(
114e2c0cdfbSPalmer Dabbelt 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
115ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
116e2c0cdfbSPalmer Dabbelt 		return -EINVAL;
117e2c0cdfbSPalmer Dabbelt 	}
118e2c0cdfbSPalmer Dabbelt 
119e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
120e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
121e2c0cdfbSPalmer Dabbelt 	return 0;
122e2c0cdfbSPalmer Dabbelt }
123e2c0cdfbSPalmer Dabbelt 
124e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
125e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
126e2c0cdfbSPalmer Dabbelt {
127e2c0cdfbSPalmer Dabbelt 	/*
128e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
129e2c0cdfbSPalmer Dabbelt 	 * handler.
130e2c0cdfbSPalmer Dabbelt 	 */
131e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfffff) | ((v & 0xfff) << 20);
132e2c0cdfbSPalmer Dabbelt 	return 0;
133e2c0cdfbSPalmer Dabbelt }
134e2c0cdfbSPalmer Dabbelt 
135e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
136e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
137e2c0cdfbSPalmer Dabbelt {
138e2c0cdfbSPalmer Dabbelt 	/*
139e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
140e2c0cdfbSPalmer Dabbelt 	 * handler.
141e2c0cdfbSPalmer Dabbelt 	 */
142e2c0cdfbSPalmer Dabbelt 	u32 imm11_5 = (v & 0xfe0) << (31 - 11);
143e2c0cdfbSPalmer Dabbelt 	u32 imm4_0 = (v & 0x1f) << (11 - 4);
144e2c0cdfbSPalmer Dabbelt 
145e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
146e2c0cdfbSPalmer Dabbelt 	return 0;
147e2c0cdfbSPalmer Dabbelt }
148e2c0cdfbSPalmer Dabbelt 
149e7456e69SZong Li static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
150e7456e69SZong Li 				   Elf_Addr v)
151e7456e69SZong Li {
152e7456e69SZong Li 	s32 hi20;
153e7456e69SZong Li 
154da4ed378SJoe Perches 	if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
155e7456e69SZong Li 		pr_err(
156e7456e69SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
157ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
158e7456e69SZong Li 		return -EINVAL;
159e7456e69SZong Li 	}
160e7456e69SZong Li 
161e7456e69SZong Li 	hi20 = ((s32)v + 0x800) & 0xfffff000;
162e7456e69SZong Li 	*location = (*location & 0xfff) | hi20;
163e7456e69SZong Li 	return 0;
164e7456e69SZong Li }
165e7456e69SZong Li 
166e7456e69SZong Li static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location,
167e7456e69SZong Li 				     Elf_Addr v)
168e7456e69SZong Li {
169e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
170e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
171e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
172e7456e69SZong Li 	*location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
173e7456e69SZong Li 	return 0;
174e7456e69SZong Li }
175e7456e69SZong Li 
176e7456e69SZong Li static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location,
177e7456e69SZong Li 				     Elf_Addr v)
178e7456e69SZong Li {
179e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
180e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
181e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
182e7456e69SZong Li 	u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
183e7456e69SZong Li 	u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
184e7456e69SZong Li 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
185e7456e69SZong Li 	return 0;
186e7456e69SZong Li }
187e7456e69SZong Li 
188da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
189da975dd4SZong Li 				       Elf_Addr v)
190da975dd4SZong Li {
1917df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
192da975dd4SZong Li 	s32 hi20;
193da975dd4SZong Li 
194da975dd4SZong Li 	/* Always emit the got entry */
195da975dd4SZong Li 	if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
196da975dd4SZong Li 		offset = module_emit_got_entry(me, v);
197da975dd4SZong Li 		offset = (void *)offset - (void *)location;
198da975dd4SZong Li 	} else {
199da975dd4SZong Li 		pr_err(
200da975dd4SZong Li 		  "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
201ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
202da975dd4SZong Li 		return -EINVAL;
203da975dd4SZong Li 	}
204da975dd4SZong Li 
205da975dd4SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
206da975dd4SZong Li 	*location = (*location & 0xfff) | hi20;
207da975dd4SZong Li 	return 0;
208da975dd4SZong Li }
209da975dd4SZong Li 
210e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
211e2c0cdfbSPalmer Dabbelt 				       Elf_Addr v)
212e2c0cdfbSPalmer Dabbelt {
2137df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
214e2c0cdfbSPalmer Dabbelt 	u32 hi20, lo12;
215e2c0cdfbSPalmer Dabbelt 
2160966d385SEmil Renner Berthing 	if (!riscv_insn_valid_32bit_offset(offset)) {
217da975dd4SZong Li 		/* Only emit the plt entry if offset over 32-bit range */
218da975dd4SZong Li 		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
219da975dd4SZong Li 			offset = module_emit_plt_entry(me, v);
220da975dd4SZong Li 			offset = (void *)offset - (void *)location;
221da975dd4SZong Li 		} else {
222e2c0cdfbSPalmer Dabbelt 			pr_err(
223e2c0cdfbSPalmer Dabbelt 			  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
224ef3a6140SOlof Johansson 			  me->name, (long long)v, location);
225e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
226e2c0cdfbSPalmer Dabbelt 		}
227da975dd4SZong Li 	}
228e2c0cdfbSPalmer Dabbelt 
229e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
230e2c0cdfbSPalmer Dabbelt 	lo12 = (offset - hi20) & 0xfff;
231e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
232e2c0cdfbSPalmer Dabbelt 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
233e2c0cdfbSPalmer Dabbelt 	return 0;
234e2c0cdfbSPalmer Dabbelt }
235e2c0cdfbSPalmer Dabbelt 
236e1910c72SZong Li static int apply_r_riscv_call_rela(struct module *me, u32 *location,
237e1910c72SZong Li 				   Elf_Addr v)
238e1910c72SZong Li {
2397df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
240e1910c72SZong Li 	u32 hi20, lo12;
241e1910c72SZong Li 
2420966d385SEmil Renner Berthing 	if (!riscv_insn_valid_32bit_offset(offset)) {
243e1910c72SZong Li 		pr_err(
244e1910c72SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
245ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
246e1910c72SZong Li 		return -EINVAL;
247e1910c72SZong Li 	}
248e1910c72SZong Li 
249e1910c72SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
250e1910c72SZong Li 	lo12 = (offset - hi20) & 0xfff;
251e1910c72SZong Li 	*location = (*location & 0xfff) | hi20;
252e1910c72SZong Li 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
253e1910c72SZong Li 	return 0;
254e1910c72SZong Li }
255e1910c72SZong Li 
256e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
257e2c0cdfbSPalmer Dabbelt 				    Elf_Addr v)
258e2c0cdfbSPalmer Dabbelt {
259e2c0cdfbSPalmer Dabbelt 	return 0;
260e2c0cdfbSPalmer Dabbelt }
261e2c0cdfbSPalmer Dabbelt 
26229e405cdSZong Li static int apply_r_riscv_align_rela(struct module *me, u32 *location,
26329e405cdSZong Li 				    Elf_Addr v)
26429e405cdSZong Li {
26529e405cdSZong Li 	pr_err(
26629e405cdSZong Li 	  "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
26729e405cdSZong Li 	  me->name, location);
26829e405cdSZong Li 	return -EINVAL;
26929e405cdSZong Li }
27029e405cdSZong Li 
2718e691b16SZong Li static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
2728e691b16SZong Li 				    Elf_Addr v)
2738e691b16SZong Li {
274781c8fe2SAndreas Schwab 	*(u32 *)location += (u32)v;
2758e691b16SZong Li 	return 0;
2768e691b16SZong Li }
2778e691b16SZong Li 
27811a54f42SEmil Renner Berthing static int apply_r_riscv_add64_rela(struct module *me, u32 *location,
27911a54f42SEmil Renner Berthing 				    Elf_Addr v)
28011a54f42SEmil Renner Berthing {
28111a54f42SEmil Renner Berthing 	*(u64 *)location += (u64)v;
28211a54f42SEmil Renner Berthing 	return 0;
28311a54f42SEmil Renner Berthing }
28411a54f42SEmil Renner Berthing 
2854aad074cSZong Li static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
2864aad074cSZong Li 				    Elf_Addr v)
2874aad074cSZong Li {
288781c8fe2SAndreas Schwab 	*(u32 *)location -= (u32)v;
2894aad074cSZong Li 	return 0;
2904aad074cSZong Li }
2914aad074cSZong Li 
29211a54f42SEmil Renner Berthing static int apply_r_riscv_sub64_rela(struct module *me, u32 *location,
29311a54f42SEmil Renner Berthing 				    Elf_Addr v)
29411a54f42SEmil Renner Berthing {
29511a54f42SEmil Renner Berthing 	*(u64 *)location -= (u64)v;
29611a54f42SEmil Renner Berthing 	return 0;
29711a54f42SEmil Renner Berthing }
29811a54f42SEmil Renner Berthing 
299e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
300e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
30177aa85deSAndreas Schwab 	[R_RISCV_32]			= apply_r_riscv_32_rela,
302e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
303e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
304e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
30561748760SWu Caize 	[R_RISCV_RVC_BRANCH]		= apply_r_riscv_rvc_branch_rela,
30656ea45aeSZong Li 	[R_RISCV_RVC_JUMP]		= apply_r_riscv_rvc_jump_rela,
307e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
308e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
309e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
310e7456e69SZong Li 	[R_RISCV_HI20]			= apply_r_riscv_hi20_rela,
311e7456e69SZong Li 	[R_RISCV_LO12_I]		= apply_r_riscv_lo12_i_rela,
312e7456e69SZong Li 	[R_RISCV_LO12_S]		= apply_r_riscv_lo12_s_rela,
313da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
314e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
315e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
316e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
31729e405cdSZong Li 	[R_RISCV_ALIGN]			= apply_r_riscv_align_rela,
3188e691b16SZong Li 	[R_RISCV_ADD32]			= apply_r_riscv_add32_rela,
31911a54f42SEmil Renner Berthing 	[R_RISCV_ADD64]			= apply_r_riscv_add64_rela,
3204aad074cSZong Li 	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
32111a54f42SEmil Renner Berthing 	[R_RISCV_SUB64]			= apply_r_riscv_sub64_rela,
322e2c0cdfbSPalmer Dabbelt };
323e2c0cdfbSPalmer Dabbelt 
324e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
325e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
326e2c0cdfbSPalmer Dabbelt 		       struct module *me)
327e2c0cdfbSPalmer Dabbelt {
328e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
329e2c0cdfbSPalmer Dabbelt 	int (*handler)(struct module *me, u32 *location, Elf_Addr v);
330e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
331e2c0cdfbSPalmer Dabbelt 	u32 *location;
332e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
333e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
334e2c0cdfbSPalmer Dabbelt 	int res;
335e2c0cdfbSPalmer Dabbelt 
336e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
337e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
338e2c0cdfbSPalmer Dabbelt 
339e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
340e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
341e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
342e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
343e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
344e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
345e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
346e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
347e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
348e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
349e2c0cdfbSPalmer Dabbelt 				continue;
35004ce8d3fSKefeng Wang 			pr_warn("%s: Unknown symbol %s\n",
351e2c0cdfbSPalmer Dabbelt 				me->name, strtab + sym->st_name);
352e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
353e2c0cdfbSPalmer Dabbelt 		}
354e2c0cdfbSPalmer Dabbelt 
355e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
356e2c0cdfbSPalmer Dabbelt 
357e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
358e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
359e2c0cdfbSPalmer Dabbelt 		else
360e2c0cdfbSPalmer Dabbelt 			handler = NULL;
361e2c0cdfbSPalmer Dabbelt 
362e2c0cdfbSPalmer Dabbelt 		if (!handler) {
363e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
364e2c0cdfbSPalmer Dabbelt 			       me->name, type);
365e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
366e2c0cdfbSPalmer Dabbelt 		}
367e2c0cdfbSPalmer Dabbelt 
368e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
369e2c0cdfbSPalmer Dabbelt 
370e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
371e2c0cdfbSPalmer Dabbelt 			unsigned int j;
372e2c0cdfbSPalmer Dabbelt 
373e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
3747df85002SZong Li 				unsigned long hi20_loc =
375e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
376e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
377da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
378da975dd4SZong Li 
379da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
380da975dd4SZong Li 				if (hi20_loc == sym->st_value
381da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
382da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
383da975dd4SZong Li 					s32 hi20, lo12;
384e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
385e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
386e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
3877df85002SZong Li 					unsigned long hi20_sym_val =
388e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
389e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
390da975dd4SZong Li 
391e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
3927df85002SZong Li 					size_t offset = hi20_sym_val - hi20_loc;
393da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
394da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
395da975dd4SZong Li 						offset = module_emit_got_entry(
396da975dd4SZong Li 							 me, hi20_sym_val);
397da975dd4SZong Li 						offset = offset - hi20_loc;
398da975dd4SZong Li 					}
399da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
400da975dd4SZong Li 					lo12 = offset - hi20;
401e2c0cdfbSPalmer Dabbelt 					v = lo12;
402da975dd4SZong Li 
403e2c0cdfbSPalmer Dabbelt 					break;
404e2c0cdfbSPalmer Dabbelt 				}
405e2c0cdfbSPalmer Dabbelt 			}
406e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
407e2c0cdfbSPalmer Dabbelt 				pr_err(
408da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
409e2c0cdfbSPalmer Dabbelt 				  me->name);
410e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
411e2c0cdfbSPalmer Dabbelt 			}
412e2c0cdfbSPalmer Dabbelt 		}
413e2c0cdfbSPalmer Dabbelt 
414e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
415e2c0cdfbSPalmer Dabbelt 		if (res)
416e2c0cdfbSPalmer Dabbelt 			return res;
417e2c0cdfbSPalmer Dabbelt 	}
418e2c0cdfbSPalmer Dabbelt 
419e2c0cdfbSPalmer Dabbelt 	return 0;
420e2c0cdfbSPalmer Dabbelt }
4210cff8bffSVincent Chen 
4220cff8bffSVincent Chen #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
4230cff8bffSVincent Chen void *module_alloc(unsigned long size)
4240cff8bffSVincent Chen {
4252bfc6cd8SAlexandre Ghiti 	return __vmalloc_node_range(size, 1, MODULES_VADDR,
4262bfc6cd8SAlexandre Ghiti 				    MODULES_END, GFP_KERNEL,
4275387054bSJisheng Zhang 				    PAGE_KERNEL, 0, NUMA_NO_NODE,
4280cff8bffSVincent Chen 				    __builtin_return_address(0));
4290cff8bffSVincent Chen }
4300cff8bffSVincent Chen #endif
431*a8e91016SHeiko Stuebner 
432*a8e91016SHeiko Stuebner static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
433*a8e91016SHeiko Stuebner 				    const Elf_Shdr *sechdrs,
434*a8e91016SHeiko Stuebner 				    const char *name)
435*a8e91016SHeiko Stuebner {
436*a8e91016SHeiko Stuebner 	const Elf_Shdr *s, *se;
437*a8e91016SHeiko Stuebner 	const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
438*a8e91016SHeiko Stuebner 
439*a8e91016SHeiko Stuebner 	for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
440*a8e91016SHeiko Stuebner 		if (strcmp(name, secstrs + s->sh_name) == 0)
441*a8e91016SHeiko Stuebner 			return s;
442*a8e91016SHeiko Stuebner 	}
443*a8e91016SHeiko Stuebner 
444*a8e91016SHeiko Stuebner 	return NULL;
445*a8e91016SHeiko Stuebner }
446*a8e91016SHeiko Stuebner 
447*a8e91016SHeiko Stuebner int module_finalize(const Elf_Ehdr *hdr,
448*a8e91016SHeiko Stuebner 		    const Elf_Shdr *sechdrs,
449*a8e91016SHeiko Stuebner 		    struct module *me)
450*a8e91016SHeiko Stuebner {
451*a8e91016SHeiko Stuebner 	const Elf_Shdr *s;
452*a8e91016SHeiko Stuebner 
453*a8e91016SHeiko Stuebner 	s = find_section(hdr, sechdrs, ".alternative");
454*a8e91016SHeiko Stuebner 	if (s)
455*a8e91016SHeiko Stuebner 		apply_module_alternatives((void *)s->sh_addr, s->sh_size);
456*a8e91016SHeiko Stuebner 
457*a8e91016SHeiko Stuebner 	return 0;
458*a8e91016SHeiko Stuebner }
459