xref: /linux-6.15/arch/riscv/kernel/module.c (revision 8cbe0acc)
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>
14a8e91016SHeiko 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 
30*8cbe0accSEmil Renner Berthing static int riscv_insn_rmw(void *location, u32 keep, u32 set)
31*8cbe0accSEmil Renner Berthing {
32*8cbe0accSEmil Renner Berthing 	u16 *parcel = location;
33*8cbe0accSEmil Renner Berthing 	u32 insn = (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) << 16;
34*8cbe0accSEmil Renner Berthing 
35*8cbe0accSEmil Renner Berthing 	insn &= keep;
36*8cbe0accSEmil Renner Berthing 	insn |= set;
37*8cbe0accSEmil Renner Berthing 
38*8cbe0accSEmil Renner Berthing 	parcel[0] = cpu_to_le16(insn);
39*8cbe0accSEmil Renner Berthing 	parcel[1] = cpu_to_le16(insn >> 16);
40*8cbe0accSEmil Renner Berthing 	return 0;
41*8cbe0accSEmil Renner Berthing }
42*8cbe0accSEmil Renner Berthing 
43*8cbe0accSEmil Renner Berthing static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set)
44*8cbe0accSEmil Renner Berthing {
45*8cbe0accSEmil Renner Berthing 	u16 *parcel = location;
46*8cbe0accSEmil Renner Berthing 	u16 insn = le16_to_cpu(*parcel);
47*8cbe0accSEmil Renner Berthing 
48*8cbe0accSEmil Renner Berthing 	insn &= keep;
49*8cbe0accSEmil Renner Berthing 	insn |= set;
50*8cbe0accSEmil Renner Berthing 
51*8cbe0accSEmil Renner Berthing 	*parcel = cpu_to_le16(insn);
52*8cbe0accSEmil Renner Berthing 	return 0;
53*8cbe0accSEmil Renner Berthing }
54*8cbe0accSEmil Renner Berthing 
55*8cbe0accSEmil Renner Berthing static int apply_r_riscv_32_rela(struct module *me, void *location, Elf_Addr v)
5677aa85deSAndreas Schwab {
5777aa85deSAndreas Schwab 	if (v != (u32)v) {
5877aa85deSAndreas Schwab 		pr_err("%s: value %016llx out of range for 32-bit field\n",
59ef3a6140SOlof Johansson 		       me->name, (long long)v);
6077aa85deSAndreas Schwab 		return -EINVAL;
6177aa85deSAndreas Schwab 	}
62*8cbe0accSEmil Renner Berthing 	*(u32 *)location = v;
6377aa85deSAndreas Schwab 	return 0;
6477aa85deSAndreas Schwab }
6577aa85deSAndreas Schwab 
66*8cbe0accSEmil Renner Berthing static int apply_r_riscv_64_rela(struct module *me, void *location, Elf_Addr v)
67e2c0cdfbSPalmer Dabbelt {
68e2c0cdfbSPalmer Dabbelt 	*(u64 *)location = v;
69e2c0cdfbSPalmer Dabbelt 	return 0;
70e2c0cdfbSPalmer Dabbelt }
71e2c0cdfbSPalmer Dabbelt 
72*8cbe0accSEmil Renner Berthing static int apply_r_riscv_branch_rela(struct module *me, void *location,
73e2c0cdfbSPalmer Dabbelt 				     Elf_Addr v)
74e2c0cdfbSPalmer Dabbelt {
75*8cbe0accSEmil Renner Berthing 	ptrdiff_t offset = (void *)v - location;
76e2c0cdfbSPalmer Dabbelt 	u32 imm12 = (offset & 0x1000) << (31 - 12);
77e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) >> (11 - 7);
78e2c0cdfbSPalmer Dabbelt 	u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
79e2c0cdfbSPalmer Dabbelt 	u32 imm4_1 = (offset & 0x1e) << (11 - 4);
80e2c0cdfbSPalmer Dabbelt 
81*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4_1);
82e2c0cdfbSPalmer Dabbelt }
83e2c0cdfbSPalmer Dabbelt 
84*8cbe0accSEmil Renner Berthing static int apply_r_riscv_jal_rela(struct module *me, void *location,
85e2c0cdfbSPalmer Dabbelt 				  Elf_Addr v)
86e2c0cdfbSPalmer Dabbelt {
87*8cbe0accSEmil Renner Berthing 	ptrdiff_t offset = (void *)v - location;
88e2c0cdfbSPalmer Dabbelt 	u32 imm20 = (offset & 0x100000) << (31 - 20);
89e2c0cdfbSPalmer Dabbelt 	u32 imm19_12 = (offset & 0xff000);
90e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) << (20 - 11);
91e2c0cdfbSPalmer Dabbelt 	u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
92e2c0cdfbSPalmer Dabbelt 
93*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1);
94e2c0cdfbSPalmer Dabbelt }
95e2c0cdfbSPalmer Dabbelt 
96*8cbe0accSEmil Renner Berthing static int apply_r_riscv_rvc_branch_rela(struct module *me, void *location,
9756ea45aeSZong Li 					 Elf_Addr v)
9856ea45aeSZong Li {
99*8cbe0accSEmil Renner Berthing 	ptrdiff_t offset = (void *)v - location;
10056ea45aeSZong Li 	u16 imm8 = (offset & 0x100) << (12 - 8);
10156ea45aeSZong Li 	u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
10256ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
10356ea45aeSZong Li 	u16 imm4_3 = (offset & 0x18) << (12 - 5);
10456ea45aeSZong Li 	u16 imm2_1 = (offset & 0x6) << (12 - 10);
10556ea45aeSZong Li 
106*8cbe0accSEmil Renner Berthing 	return riscv_insn_rvc_rmw(location, 0xe383,
107*8cbe0accSEmil Renner Berthing 			imm8 | imm7_6 | imm5 | imm4_3 | imm2_1);
10856ea45aeSZong Li }
10956ea45aeSZong Li 
110*8cbe0accSEmil Renner Berthing static int apply_r_riscv_rvc_jump_rela(struct module *me, void *location,
11156ea45aeSZong Li 				       Elf_Addr v)
11256ea45aeSZong Li {
113*8cbe0accSEmil Renner Berthing 	ptrdiff_t offset = (void *)v - location;
11456ea45aeSZong Li 	u16 imm11 = (offset & 0x800) << (12 - 11);
11556ea45aeSZong Li 	u16 imm10 = (offset & 0x400) >> (10 - 8);
11656ea45aeSZong Li 	u16 imm9_8 = (offset & 0x300) << (12 - 11);
11756ea45aeSZong Li 	u16 imm7 = (offset & 0x80) >> (7 - 6);
11856ea45aeSZong Li 	u16 imm6 = (offset & 0x40) << (12 - 11);
11956ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
12056ea45aeSZong Li 	u16 imm4 = (offset & 0x10) << (12 - 5);
12156ea45aeSZong Li 	u16 imm3_1 = (offset & 0xe) << (12 - 10);
12256ea45aeSZong Li 
123*8cbe0accSEmil Renner Berthing 	return riscv_insn_rvc_rmw(location, 0xe003,
124*8cbe0accSEmil Renner Berthing 			imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1);
12556ea45aeSZong Li }
12656ea45aeSZong Li 
127*8cbe0accSEmil Renner Berthing static int apply_r_riscv_pcrel_hi20_rela(struct module *me, void *location,
128e2c0cdfbSPalmer Dabbelt 					 Elf_Addr v)
129e2c0cdfbSPalmer Dabbelt {
130*8cbe0accSEmil Renner Berthing 	ptrdiff_t offset = (void *)v - location;
131e2c0cdfbSPalmer Dabbelt 
1320966d385SEmil Renner Berthing 	if (!riscv_insn_valid_32bit_offset(offset)) {
133e2c0cdfbSPalmer Dabbelt 		pr_err(
134e2c0cdfbSPalmer Dabbelt 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
135ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
136e2c0cdfbSPalmer Dabbelt 		return -EINVAL;
137e2c0cdfbSPalmer Dabbelt 	}
138e2c0cdfbSPalmer Dabbelt 
139*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000);
140e2c0cdfbSPalmer Dabbelt }
141e2c0cdfbSPalmer Dabbelt 
142*8cbe0accSEmil Renner Berthing static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *location,
143e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
144e2c0cdfbSPalmer Dabbelt {
145e2c0cdfbSPalmer Dabbelt 	/*
146e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
147e2c0cdfbSPalmer Dabbelt 	 * handler.
148e2c0cdfbSPalmer Dabbelt 	 */
149*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20);
150e2c0cdfbSPalmer Dabbelt }
151e2c0cdfbSPalmer Dabbelt 
152*8cbe0accSEmil Renner Berthing static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *location,
153e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
154e2c0cdfbSPalmer Dabbelt {
155e2c0cdfbSPalmer Dabbelt 	/*
156e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
157e2c0cdfbSPalmer Dabbelt 	 * handler.
158e2c0cdfbSPalmer Dabbelt 	 */
159e2c0cdfbSPalmer Dabbelt 	u32 imm11_5 = (v & 0xfe0) << (31 - 11);
160e2c0cdfbSPalmer Dabbelt 	u32 imm4_0 = (v & 0x1f) << (11 - 4);
161e2c0cdfbSPalmer Dabbelt 
162*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0);
163e2c0cdfbSPalmer Dabbelt }
164e2c0cdfbSPalmer Dabbelt 
165*8cbe0accSEmil Renner Berthing static int apply_r_riscv_hi20_rela(struct module *me, void *location,
166e7456e69SZong Li 				   Elf_Addr v)
167e7456e69SZong Li {
168da4ed378SJoe Perches 	if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
169e7456e69SZong Li 		pr_err(
170e7456e69SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
171ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
172e7456e69SZong Li 		return -EINVAL;
173e7456e69SZong Li 	}
174e7456e69SZong Li 
175*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000);
176e7456e69SZong Li }
177e7456e69SZong Li 
178*8cbe0accSEmil Renner Berthing static int apply_r_riscv_lo12_i_rela(struct module *me, void *location,
179e7456e69SZong Li 				     Elf_Addr v)
180e7456e69SZong Li {
181e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
182e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
183e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
184*8cbe0accSEmil Renner Berthing 
185*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 20);
186e7456e69SZong Li }
187e7456e69SZong Li 
188*8cbe0accSEmil Renner Berthing static int apply_r_riscv_lo12_s_rela(struct module *me, void *location,
189e7456e69SZong Li 				     Elf_Addr v)
190e7456e69SZong Li {
191e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
192e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
193e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
194e7456e69SZong Li 	u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
195e7456e69SZong Li 	u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
196*8cbe0accSEmil Renner Berthing 
197*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0);
198e7456e69SZong Li }
199e7456e69SZong Li 
200*8cbe0accSEmil Renner Berthing static int apply_r_riscv_got_hi20_rela(struct module *me, void *location,
201da975dd4SZong Li 				       Elf_Addr v)
202da975dd4SZong Li {
203*8cbe0accSEmil Renner Berthing 	ptrdiff_t offset = (void *)v - location;
204da975dd4SZong Li 
205da975dd4SZong Li 	/* Always emit the got entry */
206da975dd4SZong Li 	if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
207*8cbe0accSEmil Renner Berthing 		offset = (void *)module_emit_got_entry(me, v) - location;
208da975dd4SZong Li 	} else {
209da975dd4SZong Li 		pr_err(
210da975dd4SZong Li 		  "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
211ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
212da975dd4SZong Li 		return -EINVAL;
213da975dd4SZong Li 	}
214da975dd4SZong Li 
215*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000);
216da975dd4SZong Li }
217da975dd4SZong Li 
218*8cbe0accSEmil Renner Berthing static int apply_r_riscv_call_plt_rela(struct module *me, void *location,
219e2c0cdfbSPalmer Dabbelt 				       Elf_Addr v)
220e2c0cdfbSPalmer Dabbelt {
221*8cbe0accSEmil Renner Berthing 	ptrdiff_t offset = (void *)v - location;
222e2c0cdfbSPalmer Dabbelt 	u32 hi20, lo12;
223e2c0cdfbSPalmer Dabbelt 
2240966d385SEmil Renner Berthing 	if (!riscv_insn_valid_32bit_offset(offset)) {
225da975dd4SZong Li 		/* Only emit the plt entry if offset over 32-bit range */
226da975dd4SZong Li 		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
227*8cbe0accSEmil Renner Berthing 			offset = (void *)module_emit_plt_entry(me, v) - location;
228da975dd4SZong Li 		} else {
229e2c0cdfbSPalmer Dabbelt 			pr_err(
230e2c0cdfbSPalmer Dabbelt 			  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
231ef3a6140SOlof Johansson 			  me->name, (long long)v, location);
232e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
233e2c0cdfbSPalmer Dabbelt 		}
234da975dd4SZong Li 	}
235e2c0cdfbSPalmer Dabbelt 
236e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
237e2c0cdfbSPalmer Dabbelt 	lo12 = (offset - hi20) & 0xfff;
238*8cbe0accSEmil Renner Berthing 	riscv_insn_rmw(location, 0xfff, hi20);
239*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20);
240e2c0cdfbSPalmer Dabbelt }
241e2c0cdfbSPalmer Dabbelt 
242*8cbe0accSEmil Renner Berthing static int apply_r_riscv_call_rela(struct module *me, void *location,
243e1910c72SZong Li 				   Elf_Addr v)
244e1910c72SZong Li {
245*8cbe0accSEmil Renner Berthing 	ptrdiff_t offset = (void *)v - location;
246e1910c72SZong Li 	u32 hi20, lo12;
247e1910c72SZong Li 
2480966d385SEmil Renner Berthing 	if (!riscv_insn_valid_32bit_offset(offset)) {
249e1910c72SZong Li 		pr_err(
250e1910c72SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
251ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
252e1910c72SZong Li 		return -EINVAL;
253e1910c72SZong Li 	}
254e1910c72SZong Li 
255e1910c72SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
256e1910c72SZong Li 	lo12 = (offset - hi20) & 0xfff;
257*8cbe0accSEmil Renner Berthing 	riscv_insn_rmw(location, 0xfff, hi20);
258*8cbe0accSEmil Renner Berthing 	return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20);
259e1910c72SZong Li }
260e1910c72SZong Li 
261*8cbe0accSEmil Renner Berthing static int apply_r_riscv_relax_rela(struct module *me, void *location,
262e2c0cdfbSPalmer Dabbelt 				    Elf_Addr v)
263e2c0cdfbSPalmer Dabbelt {
264e2c0cdfbSPalmer Dabbelt 	return 0;
265e2c0cdfbSPalmer Dabbelt }
266e2c0cdfbSPalmer Dabbelt 
267*8cbe0accSEmil Renner Berthing static int apply_r_riscv_align_rela(struct module *me, void *location,
26829e405cdSZong Li 				    Elf_Addr v)
26929e405cdSZong Li {
27029e405cdSZong Li 	pr_err(
27129e405cdSZong Li 	  "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
27229e405cdSZong Li 	  me->name, location);
27329e405cdSZong Li 	return -EINVAL;
27429e405cdSZong Li }
27529e405cdSZong Li 
276*8cbe0accSEmil Renner Berthing static int apply_r_riscv_add16_rela(struct module *me, void *location,
2771bc400ffSAndrew Jones 				    Elf_Addr v)
2781bc400ffSAndrew Jones {
2791bc400ffSAndrew Jones 	*(u16 *)location += (u16)v;
2801bc400ffSAndrew Jones 	return 0;
2811bc400ffSAndrew Jones }
2821bc400ffSAndrew Jones 
283*8cbe0accSEmil Renner Berthing static int apply_r_riscv_add32_rela(struct module *me, void *location,
2848e691b16SZong Li 				    Elf_Addr v)
2858e691b16SZong Li {
286781c8fe2SAndreas Schwab 	*(u32 *)location += (u32)v;
2878e691b16SZong Li 	return 0;
2888e691b16SZong Li }
2898e691b16SZong Li 
290*8cbe0accSEmil Renner Berthing static int apply_r_riscv_add64_rela(struct module *me, void *location,
29111a54f42SEmil Renner Berthing 				    Elf_Addr v)
29211a54f42SEmil Renner Berthing {
29311a54f42SEmil Renner Berthing 	*(u64 *)location += (u64)v;
29411a54f42SEmil Renner Berthing 	return 0;
29511a54f42SEmil Renner Berthing }
29611a54f42SEmil Renner Berthing 
297*8cbe0accSEmil Renner Berthing static int apply_r_riscv_sub16_rela(struct module *me, void *location,
2981bc400ffSAndrew Jones 				    Elf_Addr v)
2991bc400ffSAndrew Jones {
3001bc400ffSAndrew Jones 	*(u16 *)location -= (u16)v;
3011bc400ffSAndrew Jones 	return 0;
3021bc400ffSAndrew Jones }
3031bc400ffSAndrew Jones 
304*8cbe0accSEmil Renner Berthing static int apply_r_riscv_sub32_rela(struct module *me, void *location,
3054aad074cSZong Li 				    Elf_Addr v)
3064aad074cSZong Li {
307781c8fe2SAndreas Schwab 	*(u32 *)location -= (u32)v;
3084aad074cSZong Li 	return 0;
3094aad074cSZong Li }
3104aad074cSZong Li 
311*8cbe0accSEmil Renner Berthing static int apply_r_riscv_sub64_rela(struct module *me, void *location,
31211a54f42SEmil Renner Berthing 				    Elf_Addr v)
31311a54f42SEmil Renner Berthing {
31411a54f42SEmil Renner Berthing 	*(u64 *)location -= (u64)v;
31511a54f42SEmil Renner Berthing 	return 0;
31611a54f42SEmil Renner Berthing }
31711a54f42SEmil Renner Berthing 
318*8cbe0accSEmil Renner Berthing static int (*reloc_handlers_rela[]) (struct module *me, void *location,
319e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
32077aa85deSAndreas Schwab 	[R_RISCV_32]			= apply_r_riscv_32_rela,
321e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
322e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
323e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
32461748760SWu Caize 	[R_RISCV_RVC_BRANCH]		= apply_r_riscv_rvc_branch_rela,
32556ea45aeSZong Li 	[R_RISCV_RVC_JUMP]		= apply_r_riscv_rvc_jump_rela,
326e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
327e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
328e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
329e7456e69SZong Li 	[R_RISCV_HI20]			= apply_r_riscv_hi20_rela,
330e7456e69SZong Li 	[R_RISCV_LO12_I]		= apply_r_riscv_lo12_i_rela,
331e7456e69SZong Li 	[R_RISCV_LO12_S]		= apply_r_riscv_lo12_s_rela,
332da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
333e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
334e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
335e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
33629e405cdSZong Li 	[R_RISCV_ALIGN]			= apply_r_riscv_align_rela,
3371bc400ffSAndrew Jones 	[R_RISCV_ADD16]			= apply_r_riscv_add16_rela,
3388e691b16SZong Li 	[R_RISCV_ADD32]			= apply_r_riscv_add32_rela,
33911a54f42SEmil Renner Berthing 	[R_RISCV_ADD64]			= apply_r_riscv_add64_rela,
3401bc400ffSAndrew Jones 	[R_RISCV_SUB16]			= apply_r_riscv_sub16_rela,
3414aad074cSZong Li 	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
34211a54f42SEmil Renner Berthing 	[R_RISCV_SUB64]			= apply_r_riscv_sub64_rela,
343e2c0cdfbSPalmer Dabbelt };
344e2c0cdfbSPalmer Dabbelt 
345e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
346e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
347e2c0cdfbSPalmer Dabbelt 		       struct module *me)
348e2c0cdfbSPalmer Dabbelt {
349e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
350*8cbe0accSEmil Renner Berthing 	int (*handler)(struct module *me, void *location, Elf_Addr v);
351e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
352*8cbe0accSEmil Renner Berthing 	void *location;
353e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
354e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
355e2c0cdfbSPalmer Dabbelt 	int res;
356e2c0cdfbSPalmer Dabbelt 
357e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
358e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
359e2c0cdfbSPalmer Dabbelt 
360e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
361e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
362e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
363e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
364e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
365e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
366e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
367e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
368e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
369e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
370e2c0cdfbSPalmer Dabbelt 				continue;
37104ce8d3fSKefeng Wang 			pr_warn("%s: Unknown symbol %s\n",
372e2c0cdfbSPalmer Dabbelt 				me->name, strtab + sym->st_name);
373e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
374e2c0cdfbSPalmer Dabbelt 		}
375e2c0cdfbSPalmer Dabbelt 
376e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
377e2c0cdfbSPalmer Dabbelt 
378e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
379e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
380e2c0cdfbSPalmer Dabbelt 		else
381e2c0cdfbSPalmer Dabbelt 			handler = NULL;
382e2c0cdfbSPalmer Dabbelt 
383e2c0cdfbSPalmer Dabbelt 		if (!handler) {
384e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
385e2c0cdfbSPalmer Dabbelt 			       me->name, type);
386e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
387e2c0cdfbSPalmer Dabbelt 		}
388e2c0cdfbSPalmer Dabbelt 
389e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
390e2c0cdfbSPalmer Dabbelt 
391e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
392e2c0cdfbSPalmer Dabbelt 			unsigned int j;
393e2c0cdfbSPalmer Dabbelt 
394e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
3957df85002SZong Li 				unsigned long hi20_loc =
396e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
397e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
398da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
399da975dd4SZong Li 
400da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
401da975dd4SZong Li 				if (hi20_loc == sym->st_value
402da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
403da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
404da975dd4SZong Li 					s32 hi20, lo12;
405e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
406e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
407e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
4087df85002SZong Li 					unsigned long hi20_sym_val =
409e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
410e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
411da975dd4SZong Li 
412e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
4137df85002SZong Li 					size_t offset = hi20_sym_val - hi20_loc;
414da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
415da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
416da975dd4SZong Li 						offset = module_emit_got_entry(
417da975dd4SZong Li 							 me, hi20_sym_val);
418da975dd4SZong Li 						offset = offset - hi20_loc;
419da975dd4SZong Li 					}
420da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
421da975dd4SZong Li 					lo12 = offset - hi20;
422e2c0cdfbSPalmer Dabbelt 					v = lo12;
423da975dd4SZong Li 
424e2c0cdfbSPalmer Dabbelt 					break;
425e2c0cdfbSPalmer Dabbelt 				}
426e2c0cdfbSPalmer Dabbelt 			}
427e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
428e2c0cdfbSPalmer Dabbelt 				pr_err(
429da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
430e2c0cdfbSPalmer Dabbelt 				  me->name);
431e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
432e2c0cdfbSPalmer Dabbelt 			}
433e2c0cdfbSPalmer Dabbelt 		}
434e2c0cdfbSPalmer Dabbelt 
435e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
436e2c0cdfbSPalmer Dabbelt 		if (res)
437e2c0cdfbSPalmer Dabbelt 			return res;
438e2c0cdfbSPalmer Dabbelt 	}
439e2c0cdfbSPalmer Dabbelt 
440e2c0cdfbSPalmer Dabbelt 	return 0;
441e2c0cdfbSPalmer Dabbelt }
4420cff8bffSVincent Chen 
4430cff8bffSVincent Chen #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
4440cff8bffSVincent Chen void *module_alloc(unsigned long size)
4450cff8bffSVincent Chen {
4462bfc6cd8SAlexandre Ghiti 	return __vmalloc_node_range(size, 1, MODULES_VADDR,
4472bfc6cd8SAlexandre Ghiti 				    MODULES_END, GFP_KERNEL,
4485387054bSJisheng Zhang 				    PAGE_KERNEL, 0, NUMA_NO_NODE,
4490cff8bffSVincent Chen 				    __builtin_return_address(0));
4500cff8bffSVincent Chen }
4510cff8bffSVincent Chen #endif
452a8e91016SHeiko Stuebner 
453a8e91016SHeiko Stuebner int module_finalize(const Elf_Ehdr *hdr,
454a8e91016SHeiko Stuebner 		    const Elf_Shdr *sechdrs,
455a8e91016SHeiko Stuebner 		    struct module *me)
456a8e91016SHeiko Stuebner {
457a8e91016SHeiko Stuebner 	const Elf_Shdr *s;
458a8e91016SHeiko Stuebner 
459a8e91016SHeiko Stuebner 	s = find_section(hdr, sechdrs, ".alternative");
460a8e91016SHeiko Stuebner 	if (s)
461a8e91016SHeiko Stuebner 		apply_module_alternatives((void *)s->sh_addr, s->sh_size);
462a8e91016SHeiko Stuebner 
463a8e91016SHeiko Stuebner 	return 0;
464a8e91016SHeiko Stuebner }
465