xref: /linux-6.15/arch/riscv/kernel/module.c (revision 4aad074c)
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 
24329e405cdSZong Li static int apply_r_riscv_align_rela(struct module *me, u32 *location,
24429e405cdSZong Li 				    Elf_Addr v)
24529e405cdSZong Li {
24629e405cdSZong Li 	pr_err(
24729e405cdSZong Li 	  "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
24829e405cdSZong Li 	  me->name, location);
24929e405cdSZong Li 	return -EINVAL;
25029e405cdSZong Li }
25129e405cdSZong Li 
2528e691b16SZong Li static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
2538e691b16SZong Li 				    Elf_Addr v)
2548e691b16SZong Li {
2558e691b16SZong Li 	*(u32 *)location += (*(u32 *)v);
2568e691b16SZong Li 	return 0;
2578e691b16SZong Li }
2588e691b16SZong Li 
259*4aad074cSZong Li static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
260*4aad074cSZong Li 				    Elf_Addr v)
261*4aad074cSZong Li {
262*4aad074cSZong Li 	*(u32 *)location -= (*(u32 *)v);
263*4aad074cSZong Li 	return 0;
264*4aad074cSZong Li }
265*4aad074cSZong Li 
266e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
267e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
268e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
269e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
270e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
27156ea45aeSZong Li 	[R_RISCV_RVC_BRANCH]		= apply_r_riscv_rcv_branch_rela,
27256ea45aeSZong Li 	[R_RISCV_RVC_JUMP]		= apply_r_riscv_rvc_jump_rela,
273e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
274e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
275e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
276e7456e69SZong Li 	[R_RISCV_HI20]			= apply_r_riscv_hi20_rela,
277e7456e69SZong Li 	[R_RISCV_LO12_I]		= apply_r_riscv_lo12_i_rela,
278e7456e69SZong Li 	[R_RISCV_LO12_S]		= apply_r_riscv_lo12_s_rela,
279da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
280e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
281e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
282e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
28329e405cdSZong Li 	[R_RISCV_ALIGN]			= apply_r_riscv_align_rela,
2848e691b16SZong Li 	[R_RISCV_ADD32]			= apply_r_riscv_add32_rela,
285*4aad074cSZong Li 	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
286e2c0cdfbSPalmer Dabbelt };
287e2c0cdfbSPalmer Dabbelt 
288e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
289e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
290e2c0cdfbSPalmer Dabbelt 		       struct module *me)
291e2c0cdfbSPalmer Dabbelt {
292e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
293e2c0cdfbSPalmer Dabbelt 	int (*handler)(struct module *me, u32 *location, Elf_Addr v);
294e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
295e2c0cdfbSPalmer Dabbelt 	u32 *location;
296e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
297e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
298e2c0cdfbSPalmer Dabbelt 	int res;
299e2c0cdfbSPalmer Dabbelt 
300e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
301e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
302e2c0cdfbSPalmer Dabbelt 
303e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
304e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
305e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
306e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
307e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
308e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
309e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
310e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
311e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
312e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
313e2c0cdfbSPalmer Dabbelt 				continue;
314e2c0cdfbSPalmer Dabbelt 			pr_warning("%s: Unknown symbol %s\n",
315e2c0cdfbSPalmer Dabbelt 				   me->name, strtab + sym->st_name);
316e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
317e2c0cdfbSPalmer Dabbelt 		}
318e2c0cdfbSPalmer Dabbelt 
319e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
320e2c0cdfbSPalmer Dabbelt 
321e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
322e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
323e2c0cdfbSPalmer Dabbelt 		else
324e2c0cdfbSPalmer Dabbelt 			handler = NULL;
325e2c0cdfbSPalmer Dabbelt 
326e2c0cdfbSPalmer Dabbelt 		if (!handler) {
327e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
328e2c0cdfbSPalmer Dabbelt 			       me->name, type);
329e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
330e2c0cdfbSPalmer Dabbelt 		}
331e2c0cdfbSPalmer Dabbelt 
332e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
333e2c0cdfbSPalmer Dabbelt 
334e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
335e2c0cdfbSPalmer Dabbelt 			unsigned int j;
336e2c0cdfbSPalmer Dabbelt 
337e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
338e2c0cdfbSPalmer Dabbelt 				u64 hi20_loc =
339e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
340e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
341da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
342da975dd4SZong Li 
343da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
344da975dd4SZong Li 				if (hi20_loc == sym->st_value
345da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
346da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
347da975dd4SZong Li 					s32 hi20, lo12;
348e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
349e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
350e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
351e2c0cdfbSPalmer Dabbelt 					u64 hi20_sym_val =
352e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
353e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
354da975dd4SZong Li 
355e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
356da975dd4SZong Li 					u64 offset = hi20_sym_val - hi20_loc;
357da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
358da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
359da975dd4SZong Li 						offset = module_emit_got_entry(
360da975dd4SZong Li 							 me, hi20_sym_val);
361da975dd4SZong Li 						offset = offset - hi20_loc;
362da975dd4SZong Li 					}
363da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
364da975dd4SZong Li 					lo12 = offset - hi20;
365e2c0cdfbSPalmer Dabbelt 					v = lo12;
366da975dd4SZong Li 
367e2c0cdfbSPalmer Dabbelt 					break;
368e2c0cdfbSPalmer Dabbelt 				}
369e2c0cdfbSPalmer Dabbelt 			}
370e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
371e2c0cdfbSPalmer Dabbelt 				pr_err(
372da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
373e2c0cdfbSPalmer Dabbelt 				  me->name);
374e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
375e2c0cdfbSPalmer Dabbelt 			}
376e2c0cdfbSPalmer Dabbelt 		}
377e2c0cdfbSPalmer Dabbelt 
378e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
379e2c0cdfbSPalmer Dabbelt 		if (res)
380e2c0cdfbSPalmer Dabbelt 			return res;
381e2c0cdfbSPalmer Dabbelt 	}
382e2c0cdfbSPalmer Dabbelt 
383e2c0cdfbSPalmer Dabbelt 	return 0;
384e2c0cdfbSPalmer Dabbelt }
385