xref: /linux-6.15/arch/riscv/kernel/module.c (revision 29e405cd)
1e2c0cdfbSPalmer Dabbelt /*
2e2c0cdfbSPalmer Dabbelt  *  This program is free software; you can redistribute it and/or modify
3e2c0cdfbSPalmer Dabbelt  *  it under the terms of the GNU General Public License as published by
4e2c0cdfbSPalmer Dabbelt  *  the Free Software Foundation; either version 2 of the License, or
5e2c0cdfbSPalmer Dabbelt  *  (at your option) any later version.
6e2c0cdfbSPalmer Dabbelt  *
7e2c0cdfbSPalmer Dabbelt  *  This program is distributed in the hope that it will be useful,
8e2c0cdfbSPalmer Dabbelt  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9e2c0cdfbSPalmer Dabbelt  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10e2c0cdfbSPalmer Dabbelt  *  GNU General Public License for more details.
11e2c0cdfbSPalmer Dabbelt  *
12e2c0cdfbSPalmer Dabbelt  *  Copyright (C) 2017 Zihao Yu
13e2c0cdfbSPalmer Dabbelt  */
14e2c0cdfbSPalmer Dabbelt 
15e2c0cdfbSPalmer Dabbelt #include <linux/elf.h>
16e2c0cdfbSPalmer Dabbelt #include <linux/err.h>
17e2c0cdfbSPalmer Dabbelt #include <linux/errno.h>
18e2c0cdfbSPalmer Dabbelt #include <linux/moduleloader.h>
19e2c0cdfbSPalmer Dabbelt 
20e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
21e2c0cdfbSPalmer Dabbelt {
22e2c0cdfbSPalmer Dabbelt 	*(u64 *)location = v;
23e2c0cdfbSPalmer Dabbelt 	return 0;
24e2c0cdfbSPalmer Dabbelt }
25e2c0cdfbSPalmer Dabbelt 
26e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
27e2c0cdfbSPalmer Dabbelt 				     Elf_Addr v)
28e2c0cdfbSPalmer Dabbelt {
29e2c0cdfbSPalmer Dabbelt 	s64 offset = (void *)v - (void *)location;
30e2c0cdfbSPalmer Dabbelt 	u32 imm12 = (offset & 0x1000) << (31 - 12);
31e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) >> (11 - 7);
32e2c0cdfbSPalmer Dabbelt 	u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
33e2c0cdfbSPalmer Dabbelt 	u32 imm4_1 = (offset & 0x1e) << (11 - 4);
34e2c0cdfbSPalmer Dabbelt 
35e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
36e2c0cdfbSPalmer Dabbelt 	return 0;
37e2c0cdfbSPalmer Dabbelt }
38e2c0cdfbSPalmer Dabbelt 
39e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
40e2c0cdfbSPalmer Dabbelt 				  Elf_Addr v)
41e2c0cdfbSPalmer Dabbelt {
42e2c0cdfbSPalmer Dabbelt 	s64 offset = (void *)v - (void *)location;
43e2c0cdfbSPalmer Dabbelt 	u32 imm20 = (offset & 0x100000) << (31 - 20);
44e2c0cdfbSPalmer Dabbelt 	u32 imm19_12 = (offset & 0xff000);
45e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) << (20 - 11);
46e2c0cdfbSPalmer Dabbelt 	u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
47e2c0cdfbSPalmer Dabbelt 
48e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
49e2c0cdfbSPalmer Dabbelt 	return 0;
50e2c0cdfbSPalmer Dabbelt }
51e2c0cdfbSPalmer Dabbelt 
5256ea45aeSZong Li static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location,
5356ea45aeSZong Li 					 Elf_Addr v)
5456ea45aeSZong Li {
5556ea45aeSZong Li 	s64 offset = (void *)v - (void *)location;
5656ea45aeSZong Li 	u16 imm8 = (offset & 0x100) << (12 - 8);
5756ea45aeSZong Li 	u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
5856ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
5956ea45aeSZong Li 	u16 imm4_3 = (offset & 0x18) << (12 - 5);
6056ea45aeSZong Li 	u16 imm2_1 = (offset & 0x6) << (12 - 10);
6156ea45aeSZong Li 
6256ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe383) |
6356ea45aeSZong Li 		    imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
6456ea45aeSZong Li 	return 0;
6556ea45aeSZong Li }
6656ea45aeSZong Li 
6756ea45aeSZong Li static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
6856ea45aeSZong Li 				       Elf_Addr v)
6956ea45aeSZong Li {
7056ea45aeSZong Li 	s64 offset = (void *)v - (void *)location;
7156ea45aeSZong Li 	u16 imm11 = (offset & 0x800) << (12 - 11);
7256ea45aeSZong Li 	u16 imm10 = (offset & 0x400) >> (10 - 8);
7356ea45aeSZong Li 	u16 imm9_8 = (offset & 0x300) << (12 - 11);
7456ea45aeSZong Li 	u16 imm7 = (offset & 0x80) >> (7 - 6);
7556ea45aeSZong Li 	u16 imm6 = (offset & 0x40) << (12 - 11);
7656ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
7756ea45aeSZong Li 	u16 imm4 = (offset & 0x10) << (12 - 5);
7856ea45aeSZong Li 	u16 imm3_1 = (offset & 0xe) << (12 - 10);
7956ea45aeSZong Li 
8056ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe003) |
8156ea45aeSZong Li 		    imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
8256ea45aeSZong Li 	return 0;
8356ea45aeSZong Li }
8456ea45aeSZong Li 
85e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
86e2c0cdfbSPalmer Dabbelt 					 Elf_Addr v)
87e2c0cdfbSPalmer Dabbelt {
88e2c0cdfbSPalmer Dabbelt 	s64 offset = (void *)v - (void *)location;
89e2c0cdfbSPalmer Dabbelt 	s32 hi20;
90e2c0cdfbSPalmer Dabbelt 
91e2c0cdfbSPalmer Dabbelt 	if (offset != (s32)offset) {
92e2c0cdfbSPalmer Dabbelt 		pr_err(
93e2c0cdfbSPalmer Dabbelt 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
94e2c0cdfbSPalmer Dabbelt 		  me->name, v, location);
95e2c0cdfbSPalmer Dabbelt 		return -EINVAL;
96e2c0cdfbSPalmer Dabbelt 	}
97e2c0cdfbSPalmer Dabbelt 
98e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
99e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
100e2c0cdfbSPalmer Dabbelt 	return 0;
101e2c0cdfbSPalmer Dabbelt }
102e2c0cdfbSPalmer Dabbelt 
103e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
104e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
105e2c0cdfbSPalmer Dabbelt {
106e2c0cdfbSPalmer Dabbelt 	/*
107e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
108e2c0cdfbSPalmer Dabbelt 	 * handler.
109e2c0cdfbSPalmer Dabbelt 	 */
110e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfffff) | ((v & 0xfff) << 20);
111e2c0cdfbSPalmer Dabbelt 	return 0;
112e2c0cdfbSPalmer Dabbelt }
113e2c0cdfbSPalmer Dabbelt 
114e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
115e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
116e2c0cdfbSPalmer Dabbelt {
117e2c0cdfbSPalmer Dabbelt 	/*
118e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
119e2c0cdfbSPalmer Dabbelt 	 * handler.
120e2c0cdfbSPalmer Dabbelt 	 */
121e2c0cdfbSPalmer Dabbelt 	u32 imm11_5 = (v & 0xfe0) << (31 - 11);
122e2c0cdfbSPalmer Dabbelt 	u32 imm4_0 = (v & 0x1f) << (11 - 4);
123e2c0cdfbSPalmer Dabbelt 
124e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
125e2c0cdfbSPalmer Dabbelt 	return 0;
126e2c0cdfbSPalmer Dabbelt }
127e2c0cdfbSPalmer Dabbelt 
128e7456e69SZong Li static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
129e7456e69SZong Li 				   Elf_Addr v)
130e7456e69SZong Li {
131e7456e69SZong Li 	s32 hi20;
132e7456e69SZong Li 
133e7456e69SZong Li 	if (IS_ENABLED(CMODEL_MEDLOW)) {
134e7456e69SZong Li 		pr_err(
135e7456e69SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
136e7456e69SZong Li 		  me->name, v, location);
137e7456e69SZong Li 		return -EINVAL;
138e7456e69SZong Li 	}
139e7456e69SZong Li 
140e7456e69SZong Li 	hi20 = ((s32)v + 0x800) & 0xfffff000;
141e7456e69SZong Li 	*location = (*location & 0xfff) | hi20;
142e7456e69SZong Li 	return 0;
143e7456e69SZong Li }
144e7456e69SZong Li 
145e7456e69SZong Li static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location,
146e7456e69SZong Li 				     Elf_Addr v)
147e7456e69SZong Li {
148e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
149e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
150e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
151e7456e69SZong Li 	*location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
152e7456e69SZong Li 	return 0;
153e7456e69SZong Li }
154e7456e69SZong Li 
155e7456e69SZong Li static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location,
156e7456e69SZong Li 				     Elf_Addr v)
157e7456e69SZong Li {
158e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
159e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
160e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
161e7456e69SZong Li 	u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
162e7456e69SZong Li 	u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
163e7456e69SZong Li 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
164e7456e69SZong Li 	return 0;
165e7456e69SZong Li }
166e7456e69SZong Li 
167da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
168da975dd4SZong Li 				       Elf_Addr v)
169da975dd4SZong Li {
170da975dd4SZong Li 	s64 offset = (void *)v - (void *)location;
171da975dd4SZong Li 	s32 hi20;
172da975dd4SZong Li 
173da975dd4SZong Li 	/* Always emit the got entry */
174da975dd4SZong Li 	if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
175da975dd4SZong Li 		offset = module_emit_got_entry(me, v);
176da975dd4SZong Li 		offset = (void *)offset - (void *)location;
177da975dd4SZong Li 	} else {
178da975dd4SZong Li 		pr_err(
179da975dd4SZong Li 		  "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
180da975dd4SZong Li 		  me->name, v, location);
181da975dd4SZong Li 		return -EINVAL;
182da975dd4SZong Li 	}
183da975dd4SZong Li 
184da975dd4SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
185da975dd4SZong Li 	*location = (*location & 0xfff) | hi20;
186da975dd4SZong Li 	return 0;
187da975dd4SZong Li }
188da975dd4SZong Li 
189e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
190e2c0cdfbSPalmer Dabbelt 				       Elf_Addr v)
191e2c0cdfbSPalmer Dabbelt {
192e2c0cdfbSPalmer Dabbelt 	s64 offset = (void *)v - (void *)location;
193e2c0cdfbSPalmer Dabbelt 	s32 fill_v = offset;
194e2c0cdfbSPalmer Dabbelt 	u32 hi20, lo12;
195e2c0cdfbSPalmer Dabbelt 
196e2c0cdfbSPalmer Dabbelt 	if (offset != fill_v) {
197da975dd4SZong Li 		/* Only emit the plt entry if offset over 32-bit range */
198da975dd4SZong Li 		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
199da975dd4SZong Li 			offset = module_emit_plt_entry(me, v);
200da975dd4SZong Li 			offset = (void *)offset - (void *)location;
201da975dd4SZong Li 		} else {
202e2c0cdfbSPalmer Dabbelt 			pr_err(
203e2c0cdfbSPalmer Dabbelt 			  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
204e2c0cdfbSPalmer Dabbelt 			  me->name, v, location);
205e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
206e2c0cdfbSPalmer Dabbelt 		}
207da975dd4SZong Li 	}
208e2c0cdfbSPalmer Dabbelt 
209e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
210e2c0cdfbSPalmer Dabbelt 	lo12 = (offset - hi20) & 0xfff;
211e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
212e2c0cdfbSPalmer Dabbelt 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
213e2c0cdfbSPalmer Dabbelt 	return 0;
214e2c0cdfbSPalmer Dabbelt }
215e2c0cdfbSPalmer Dabbelt 
216e1910c72SZong Li static int apply_r_riscv_call_rela(struct module *me, u32 *location,
217e1910c72SZong Li 				   Elf_Addr v)
218e1910c72SZong Li {
219e1910c72SZong Li 	s64 offset = (void *)v - (void *)location;
220e1910c72SZong Li 	s32 fill_v = offset;
221e1910c72SZong Li 	u32 hi20, lo12;
222e1910c72SZong Li 
223e1910c72SZong Li 	if (offset != fill_v) {
224e1910c72SZong Li 		pr_err(
225e1910c72SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
226e1910c72SZong Li 		  me->name, v, location);
227e1910c72SZong Li 		return -EINVAL;
228e1910c72SZong Li 	}
229e1910c72SZong Li 
230e1910c72SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
231e1910c72SZong Li 	lo12 = (offset - hi20) & 0xfff;
232e1910c72SZong Li 	*location = (*location & 0xfff) | hi20;
233e1910c72SZong Li 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
234e1910c72SZong Li 	return 0;
235e1910c72SZong Li }
236e1910c72SZong Li 
237e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
238e2c0cdfbSPalmer Dabbelt 				    Elf_Addr v)
239e2c0cdfbSPalmer Dabbelt {
240e2c0cdfbSPalmer Dabbelt 	return 0;
241e2c0cdfbSPalmer Dabbelt }
242e2c0cdfbSPalmer Dabbelt 
243*29e405cdSZong Li static int apply_r_riscv_align_rela(struct module *me, u32 *location,
244*29e405cdSZong Li 				    Elf_Addr v)
245*29e405cdSZong Li {
246*29e405cdSZong Li 	pr_err(
247*29e405cdSZong Li 	  "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
248*29e405cdSZong Li 	  me->name, location);
249*29e405cdSZong Li 	return -EINVAL;
250*29e405cdSZong Li }
251*29e405cdSZong Li 
252e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
253e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
254e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
255e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
256e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
25756ea45aeSZong Li 	[R_RISCV_RVC_BRANCH]		= apply_r_riscv_rcv_branch_rela,
25856ea45aeSZong Li 	[R_RISCV_RVC_JUMP]		= apply_r_riscv_rvc_jump_rela,
259e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
260e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
261e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
262e7456e69SZong Li 	[R_RISCV_HI20]			= apply_r_riscv_hi20_rela,
263e7456e69SZong Li 	[R_RISCV_LO12_I]		= apply_r_riscv_lo12_i_rela,
264e7456e69SZong Li 	[R_RISCV_LO12_S]		= apply_r_riscv_lo12_s_rela,
265da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
266e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
267e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
268e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
269*29e405cdSZong Li 	[R_RISCV_ALIGN]			= apply_r_riscv_align_rela,
270e2c0cdfbSPalmer Dabbelt };
271e2c0cdfbSPalmer Dabbelt 
272e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
273e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
274e2c0cdfbSPalmer Dabbelt 		       struct module *me)
275e2c0cdfbSPalmer Dabbelt {
276e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
277e2c0cdfbSPalmer Dabbelt 	int (*handler)(struct module *me, u32 *location, Elf_Addr v);
278e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
279e2c0cdfbSPalmer Dabbelt 	u32 *location;
280e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
281e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
282e2c0cdfbSPalmer Dabbelt 	int res;
283e2c0cdfbSPalmer Dabbelt 
284e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
285e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
286e2c0cdfbSPalmer Dabbelt 
287e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
288e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
289e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
290e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
291e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
292e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
293e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
294e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
295e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
296e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
297e2c0cdfbSPalmer Dabbelt 				continue;
298e2c0cdfbSPalmer Dabbelt 			pr_warning("%s: Unknown symbol %s\n",
299e2c0cdfbSPalmer Dabbelt 				   me->name, strtab + sym->st_name);
300e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
301e2c0cdfbSPalmer Dabbelt 		}
302e2c0cdfbSPalmer Dabbelt 
303e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
304e2c0cdfbSPalmer Dabbelt 
305e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
306e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
307e2c0cdfbSPalmer Dabbelt 		else
308e2c0cdfbSPalmer Dabbelt 			handler = NULL;
309e2c0cdfbSPalmer Dabbelt 
310e2c0cdfbSPalmer Dabbelt 		if (!handler) {
311e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
312e2c0cdfbSPalmer Dabbelt 			       me->name, type);
313e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
314e2c0cdfbSPalmer Dabbelt 		}
315e2c0cdfbSPalmer Dabbelt 
316e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
317e2c0cdfbSPalmer Dabbelt 
318e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
319e2c0cdfbSPalmer Dabbelt 			unsigned int j;
320e2c0cdfbSPalmer Dabbelt 
321e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
322e2c0cdfbSPalmer Dabbelt 				u64 hi20_loc =
323e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
324e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
325da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
326da975dd4SZong Li 
327da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
328da975dd4SZong Li 				if (hi20_loc == sym->st_value
329da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
330da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
331da975dd4SZong Li 					s32 hi20, lo12;
332e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
333e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
334e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
335e2c0cdfbSPalmer Dabbelt 					u64 hi20_sym_val =
336e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
337e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
338da975dd4SZong Li 
339e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
340da975dd4SZong Li 					u64 offset = hi20_sym_val - hi20_loc;
341da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
342da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
343da975dd4SZong Li 						offset = module_emit_got_entry(
344da975dd4SZong Li 							 me, hi20_sym_val);
345da975dd4SZong Li 						offset = offset - hi20_loc;
346da975dd4SZong Li 					}
347da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
348da975dd4SZong Li 					lo12 = offset - hi20;
349e2c0cdfbSPalmer Dabbelt 					v = lo12;
350da975dd4SZong Li 
351e2c0cdfbSPalmer Dabbelt 					break;
352e2c0cdfbSPalmer Dabbelt 				}
353e2c0cdfbSPalmer Dabbelt 			}
354e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
355e2c0cdfbSPalmer Dabbelt 				pr_err(
356da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
357e2c0cdfbSPalmer Dabbelt 				  me->name);
358e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
359e2c0cdfbSPalmer Dabbelt 			}
360e2c0cdfbSPalmer Dabbelt 		}
361e2c0cdfbSPalmer Dabbelt 
362e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
363e2c0cdfbSPalmer Dabbelt 		if (res)
364e2c0cdfbSPalmer Dabbelt 			return res;
365e2c0cdfbSPalmer Dabbelt 	}
366e2c0cdfbSPalmer Dabbelt 
367e2c0cdfbSPalmer Dabbelt 	return 0;
368e2c0cdfbSPalmer Dabbelt }
369