xref: /openbmc/linux/arch/riscv/kernel/module.c (revision 8633e740)
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  */
riscv_insn_valid_32bit_offset(ptrdiff_t val)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 
apply_r_riscv_32_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_64_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_branch_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_jal_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_rvc_branch_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_rvc_jump_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_pcrel_hi20_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_pcrel_lo12_i_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_pcrel_lo12_s_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_hi20_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_lo12_i_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_lo12_s_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_got_hi20_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_call_plt_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_call_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_relax_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_align_rela(struct module * me,u32 * location,Elf_Addr v)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 
apply_r_riscv_add16_rela(struct module * me,u32 * location,Elf_Addr v)2711bc400ffSAndrew Jones static int apply_r_riscv_add16_rela(struct module *me, u32 *location,
2721bc400ffSAndrew Jones 				    Elf_Addr v)
2731bc400ffSAndrew Jones {
2741bc400ffSAndrew Jones 	*(u16 *)location += (u16)v;
2751bc400ffSAndrew Jones 	return 0;
2761bc400ffSAndrew Jones }
2771bc400ffSAndrew Jones 
apply_r_riscv_add32_rela(struct module * me,u32 * location,Elf_Addr v)2788e691b16SZong Li static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
2798e691b16SZong Li 				    Elf_Addr v)
2808e691b16SZong Li {
281781c8fe2SAndreas Schwab 	*(u32 *)location += (u32)v;
2828e691b16SZong Li 	return 0;
2838e691b16SZong Li }
2848e691b16SZong Li 
apply_r_riscv_add64_rela(struct module * me,u32 * location,Elf_Addr v)28511a54f42SEmil Renner Berthing static int apply_r_riscv_add64_rela(struct module *me, u32 *location,
28611a54f42SEmil Renner Berthing 				    Elf_Addr v)
28711a54f42SEmil Renner Berthing {
28811a54f42SEmil Renner Berthing 	*(u64 *)location += (u64)v;
28911a54f42SEmil Renner Berthing 	return 0;
29011a54f42SEmil Renner Berthing }
29111a54f42SEmil Renner Berthing 
apply_r_riscv_sub16_rela(struct module * me,u32 * location,Elf_Addr v)2921bc400ffSAndrew Jones static int apply_r_riscv_sub16_rela(struct module *me, u32 *location,
2931bc400ffSAndrew Jones 				    Elf_Addr v)
2941bc400ffSAndrew Jones {
2951bc400ffSAndrew Jones 	*(u16 *)location -= (u16)v;
2961bc400ffSAndrew Jones 	return 0;
2971bc400ffSAndrew Jones }
2981bc400ffSAndrew Jones 
apply_r_riscv_sub32_rela(struct module * me,u32 * location,Elf_Addr v)2994aad074cSZong Li static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
3004aad074cSZong Li 				    Elf_Addr v)
3014aad074cSZong Li {
302781c8fe2SAndreas Schwab 	*(u32 *)location -= (u32)v;
3034aad074cSZong Li 	return 0;
3044aad074cSZong Li }
3054aad074cSZong Li 
apply_r_riscv_sub64_rela(struct module * me,u32 * location,Elf_Addr v)30611a54f42SEmil Renner Berthing static int apply_r_riscv_sub64_rela(struct module *me, u32 *location,
30711a54f42SEmil Renner Berthing 				    Elf_Addr v)
30811a54f42SEmil Renner Berthing {
30911a54f42SEmil Renner Berthing 	*(u64 *)location -= (u64)v;
31011a54f42SEmil Renner Berthing 	return 0;
31111a54f42SEmil Renner Berthing }
31211a54f42SEmil Renner Berthing 
313e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
314e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
31577aa85deSAndreas Schwab 	[R_RISCV_32]			= apply_r_riscv_32_rela,
316e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
317e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
318e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
31961748760SWu Caize 	[R_RISCV_RVC_BRANCH]		= apply_r_riscv_rvc_branch_rela,
32056ea45aeSZong Li 	[R_RISCV_RVC_JUMP]		= apply_r_riscv_rvc_jump_rela,
321e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
322e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
323e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
324e7456e69SZong Li 	[R_RISCV_HI20]			= apply_r_riscv_hi20_rela,
325e7456e69SZong Li 	[R_RISCV_LO12_I]		= apply_r_riscv_lo12_i_rela,
326e7456e69SZong Li 	[R_RISCV_LO12_S]		= apply_r_riscv_lo12_s_rela,
327da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
328e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
329e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
330e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
33129e405cdSZong Li 	[R_RISCV_ALIGN]			= apply_r_riscv_align_rela,
3321bc400ffSAndrew Jones 	[R_RISCV_ADD16]			= apply_r_riscv_add16_rela,
3338e691b16SZong Li 	[R_RISCV_ADD32]			= apply_r_riscv_add32_rela,
33411a54f42SEmil Renner Berthing 	[R_RISCV_ADD64]			= apply_r_riscv_add64_rela,
3351bc400ffSAndrew Jones 	[R_RISCV_SUB16]			= apply_r_riscv_sub16_rela,
3364aad074cSZong Li 	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
33711a54f42SEmil Renner Berthing 	[R_RISCV_SUB64]			= apply_r_riscv_sub64_rela,
338e2c0cdfbSPalmer Dabbelt };
339e2c0cdfbSPalmer Dabbelt 
apply_relocate_add(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)340e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
341e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
342e2c0cdfbSPalmer Dabbelt 		       struct module *me)
343e2c0cdfbSPalmer Dabbelt {
344e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
345e2c0cdfbSPalmer Dabbelt 	int (*handler)(struct module *me, u32 *location, Elf_Addr v);
346e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
347e2c0cdfbSPalmer Dabbelt 	u32 *location;
348e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
349e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
350e2c0cdfbSPalmer Dabbelt 	int res;
351e2c0cdfbSPalmer Dabbelt 
352e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
353e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
354e2c0cdfbSPalmer Dabbelt 
355e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
356e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
357e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
358e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
359e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
360e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
361e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
362e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
363e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
364e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
365e2c0cdfbSPalmer Dabbelt 				continue;
36604ce8d3fSKefeng Wang 			pr_warn("%s: Unknown symbol %s\n",
367e2c0cdfbSPalmer Dabbelt 				me->name, strtab + sym->st_name);
368e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
369e2c0cdfbSPalmer Dabbelt 		}
370e2c0cdfbSPalmer Dabbelt 
371e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
372e2c0cdfbSPalmer Dabbelt 
373e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
374e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
375e2c0cdfbSPalmer Dabbelt 		else
376e2c0cdfbSPalmer Dabbelt 			handler = NULL;
377e2c0cdfbSPalmer Dabbelt 
378e2c0cdfbSPalmer Dabbelt 		if (!handler) {
379e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
380e2c0cdfbSPalmer Dabbelt 			       me->name, type);
381e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
382e2c0cdfbSPalmer Dabbelt 		}
383e2c0cdfbSPalmer Dabbelt 
384e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
385e2c0cdfbSPalmer Dabbelt 
386e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
387e2c0cdfbSPalmer Dabbelt 			unsigned int j;
388e2c0cdfbSPalmer Dabbelt 
389e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
3907df85002SZong Li 				unsigned long hi20_loc =
391e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
392e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
393da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
394da975dd4SZong Li 
395da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
396da975dd4SZong Li 				if (hi20_loc == sym->st_value
397da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
398da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
399da975dd4SZong Li 					s32 hi20, lo12;
400e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
401e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
402e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
4037df85002SZong Li 					unsigned long hi20_sym_val =
404e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
405e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
406da975dd4SZong Li 
407e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
4087df85002SZong Li 					size_t offset = hi20_sym_val - hi20_loc;
409da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
410da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
411da975dd4SZong Li 						offset = module_emit_got_entry(
412da975dd4SZong Li 							 me, hi20_sym_val);
413da975dd4SZong Li 						offset = offset - hi20_loc;
414da975dd4SZong Li 					}
415da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
416da975dd4SZong Li 					lo12 = offset - hi20;
417e2c0cdfbSPalmer Dabbelt 					v = lo12;
418da975dd4SZong Li 
419e2c0cdfbSPalmer Dabbelt 					break;
420e2c0cdfbSPalmer Dabbelt 				}
421e2c0cdfbSPalmer Dabbelt 			}
422e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
423e2c0cdfbSPalmer Dabbelt 				pr_err(
424da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
425e2c0cdfbSPalmer Dabbelt 				  me->name);
426e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
427e2c0cdfbSPalmer Dabbelt 			}
428e2c0cdfbSPalmer Dabbelt 		}
429e2c0cdfbSPalmer Dabbelt 
430e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
431e2c0cdfbSPalmer Dabbelt 		if (res)
432e2c0cdfbSPalmer Dabbelt 			return res;
433e2c0cdfbSPalmer Dabbelt 	}
434e2c0cdfbSPalmer Dabbelt 
435e2c0cdfbSPalmer Dabbelt 	return 0;
436e2c0cdfbSPalmer Dabbelt }
4370cff8bffSVincent Chen 
4380cff8bffSVincent Chen #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
module_alloc(unsigned long size)4390cff8bffSVincent Chen void *module_alloc(unsigned long size)
4400cff8bffSVincent Chen {
4412bfc6cd8SAlexandre Ghiti 	return __vmalloc_node_range(size, 1, MODULES_VADDR,
4422bfc6cd8SAlexandre Ghiti 				    MODULES_END, GFP_KERNEL,
443*8633e740SAlexandre Ghiti 				    PAGE_KERNEL, VM_FLUSH_RESET_PERMS,
444*8633e740SAlexandre Ghiti 				    NUMA_NO_NODE,
4450cff8bffSVincent Chen 				    __builtin_return_address(0));
4460cff8bffSVincent Chen }
4470cff8bffSVincent Chen #endif
448a8e91016SHeiko Stuebner 
module_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * me)449a8e91016SHeiko Stuebner int module_finalize(const Elf_Ehdr *hdr,
450a8e91016SHeiko Stuebner 		    const Elf_Shdr *sechdrs,
451a8e91016SHeiko Stuebner 		    struct module *me)
452a8e91016SHeiko Stuebner {
453a8e91016SHeiko Stuebner 	const Elf_Shdr *s;
454a8e91016SHeiko Stuebner 
455a8e91016SHeiko Stuebner 	s = find_section(hdr, sechdrs, ".alternative");
456a8e91016SHeiko Stuebner 	if (s)
457a8e91016SHeiko Stuebner 		apply_module_alternatives((void *)s->sh_addr, s->sh_size);
458a8e91016SHeiko Stuebner 
459a8e91016SHeiko Stuebner 	return 0;
460a8e91016SHeiko Stuebner }
461