xref: /linux-6.15/arch/riscv/kernel/module.c (revision 7df85002)
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 
2077aa85deSAndreas Schwab static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
2177aa85deSAndreas Schwab {
2277aa85deSAndreas Schwab 	if (v != (u32)v) {
2377aa85deSAndreas Schwab 		pr_err("%s: value %016llx out of range for 32-bit field\n",
2477aa85deSAndreas Schwab 		       me->name, v);
2577aa85deSAndreas Schwab 		return -EINVAL;
2677aa85deSAndreas Schwab 	}
2777aa85deSAndreas Schwab 	*location = v;
2877aa85deSAndreas Schwab 	return 0;
2977aa85deSAndreas Schwab }
3077aa85deSAndreas Schwab 
31e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
32e2c0cdfbSPalmer Dabbelt {
33e2c0cdfbSPalmer Dabbelt 	*(u64 *)location = v;
34e2c0cdfbSPalmer Dabbelt 	return 0;
35e2c0cdfbSPalmer Dabbelt }
36e2c0cdfbSPalmer Dabbelt 
37e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
38e2c0cdfbSPalmer Dabbelt 				     Elf_Addr v)
39e2c0cdfbSPalmer Dabbelt {
40*7df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
41e2c0cdfbSPalmer Dabbelt 	u32 imm12 = (offset & 0x1000) << (31 - 12);
42e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) >> (11 - 7);
43e2c0cdfbSPalmer Dabbelt 	u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
44e2c0cdfbSPalmer Dabbelt 	u32 imm4_1 = (offset & 0x1e) << (11 - 4);
45e2c0cdfbSPalmer Dabbelt 
46e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
47e2c0cdfbSPalmer Dabbelt 	return 0;
48e2c0cdfbSPalmer Dabbelt }
49e2c0cdfbSPalmer Dabbelt 
50e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
51e2c0cdfbSPalmer Dabbelt 				  Elf_Addr v)
52e2c0cdfbSPalmer Dabbelt {
53*7df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
54e2c0cdfbSPalmer Dabbelt 	u32 imm20 = (offset & 0x100000) << (31 - 20);
55e2c0cdfbSPalmer Dabbelt 	u32 imm19_12 = (offset & 0xff000);
56e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) << (20 - 11);
57e2c0cdfbSPalmer Dabbelt 	u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
58e2c0cdfbSPalmer Dabbelt 
59e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
60e2c0cdfbSPalmer Dabbelt 	return 0;
61e2c0cdfbSPalmer Dabbelt }
62e2c0cdfbSPalmer Dabbelt 
6356ea45aeSZong Li static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location,
6456ea45aeSZong Li 					 Elf_Addr v)
6556ea45aeSZong Li {
66*7df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
6756ea45aeSZong Li 	u16 imm8 = (offset & 0x100) << (12 - 8);
6856ea45aeSZong Li 	u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
6956ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
7056ea45aeSZong Li 	u16 imm4_3 = (offset & 0x18) << (12 - 5);
7156ea45aeSZong Li 	u16 imm2_1 = (offset & 0x6) << (12 - 10);
7256ea45aeSZong Li 
7356ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe383) |
7456ea45aeSZong Li 		    imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
7556ea45aeSZong Li 	return 0;
7656ea45aeSZong Li }
7756ea45aeSZong Li 
7856ea45aeSZong Li static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
7956ea45aeSZong Li 				       Elf_Addr v)
8056ea45aeSZong Li {
81*7df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
8256ea45aeSZong Li 	u16 imm11 = (offset & 0x800) << (12 - 11);
8356ea45aeSZong Li 	u16 imm10 = (offset & 0x400) >> (10 - 8);
8456ea45aeSZong Li 	u16 imm9_8 = (offset & 0x300) << (12 - 11);
8556ea45aeSZong Li 	u16 imm7 = (offset & 0x80) >> (7 - 6);
8656ea45aeSZong Li 	u16 imm6 = (offset & 0x40) << (12 - 11);
8756ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
8856ea45aeSZong Li 	u16 imm4 = (offset & 0x10) << (12 - 5);
8956ea45aeSZong Li 	u16 imm3_1 = (offset & 0xe) << (12 - 10);
9056ea45aeSZong Li 
9156ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe003) |
9256ea45aeSZong Li 		    imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
9356ea45aeSZong Li 	return 0;
9456ea45aeSZong Li }
9556ea45aeSZong Li 
96e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
97e2c0cdfbSPalmer Dabbelt 					 Elf_Addr v)
98e2c0cdfbSPalmer Dabbelt {
99*7df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
100e2c0cdfbSPalmer Dabbelt 	s32 hi20;
101e2c0cdfbSPalmer Dabbelt 
102e2c0cdfbSPalmer Dabbelt 	if (offset != (s32)offset) {
103e2c0cdfbSPalmer Dabbelt 		pr_err(
104e2c0cdfbSPalmer Dabbelt 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
105e2c0cdfbSPalmer Dabbelt 		  me->name, v, location);
106e2c0cdfbSPalmer Dabbelt 		return -EINVAL;
107e2c0cdfbSPalmer Dabbelt 	}
108e2c0cdfbSPalmer Dabbelt 
109e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
110e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
111e2c0cdfbSPalmer Dabbelt 	return 0;
112e2c0cdfbSPalmer Dabbelt }
113e2c0cdfbSPalmer Dabbelt 
114e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_i_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 	*location = (*location & 0xfffff) | ((v & 0xfff) << 20);
122e2c0cdfbSPalmer Dabbelt 	return 0;
123e2c0cdfbSPalmer Dabbelt }
124e2c0cdfbSPalmer Dabbelt 
125e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
126e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
127e2c0cdfbSPalmer Dabbelt {
128e2c0cdfbSPalmer Dabbelt 	/*
129e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
130e2c0cdfbSPalmer Dabbelt 	 * handler.
131e2c0cdfbSPalmer Dabbelt 	 */
132e2c0cdfbSPalmer Dabbelt 	u32 imm11_5 = (v & 0xfe0) << (31 - 11);
133e2c0cdfbSPalmer Dabbelt 	u32 imm4_0 = (v & 0x1f) << (11 - 4);
134e2c0cdfbSPalmer Dabbelt 
135e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
136e2c0cdfbSPalmer Dabbelt 	return 0;
137e2c0cdfbSPalmer Dabbelt }
138e2c0cdfbSPalmer Dabbelt 
139e7456e69SZong Li static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
140e7456e69SZong Li 				   Elf_Addr v)
141e7456e69SZong Li {
142e7456e69SZong Li 	s32 hi20;
143e7456e69SZong Li 
144e7456e69SZong Li 	if (IS_ENABLED(CMODEL_MEDLOW)) {
145e7456e69SZong Li 		pr_err(
146e7456e69SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
147e7456e69SZong Li 		  me->name, v, location);
148e7456e69SZong Li 		return -EINVAL;
149e7456e69SZong Li 	}
150e7456e69SZong Li 
151e7456e69SZong Li 	hi20 = ((s32)v + 0x800) & 0xfffff000;
152e7456e69SZong Li 	*location = (*location & 0xfff) | hi20;
153e7456e69SZong Li 	return 0;
154e7456e69SZong Li }
155e7456e69SZong Li 
156e7456e69SZong Li static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location,
157e7456e69SZong Li 				     Elf_Addr v)
158e7456e69SZong Li {
159e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
160e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
161e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
162e7456e69SZong Li 	*location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
163e7456e69SZong Li 	return 0;
164e7456e69SZong Li }
165e7456e69SZong Li 
166e7456e69SZong Li static int apply_r_riscv_lo12_s_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 	u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
173e7456e69SZong Li 	u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
174e7456e69SZong Li 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
175e7456e69SZong Li 	return 0;
176e7456e69SZong Li }
177e7456e69SZong Li 
178da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
179da975dd4SZong Li 				       Elf_Addr v)
180da975dd4SZong Li {
181*7df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
182da975dd4SZong Li 	s32 hi20;
183da975dd4SZong Li 
184da975dd4SZong Li 	/* Always emit the got entry */
185da975dd4SZong Li 	if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
186da975dd4SZong Li 		offset = module_emit_got_entry(me, v);
187da975dd4SZong Li 		offset = (void *)offset - (void *)location;
188da975dd4SZong Li 	} else {
189da975dd4SZong Li 		pr_err(
190da975dd4SZong Li 		  "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
191da975dd4SZong Li 		  me->name, v, location);
192da975dd4SZong Li 		return -EINVAL;
193da975dd4SZong Li 	}
194da975dd4SZong Li 
195da975dd4SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
196da975dd4SZong Li 	*location = (*location & 0xfff) | hi20;
197da975dd4SZong Li 	return 0;
198da975dd4SZong Li }
199da975dd4SZong Li 
200e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
201e2c0cdfbSPalmer Dabbelt 				       Elf_Addr v)
202e2c0cdfbSPalmer Dabbelt {
203*7df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
204e2c0cdfbSPalmer Dabbelt 	s32 fill_v = offset;
205e2c0cdfbSPalmer Dabbelt 	u32 hi20, lo12;
206e2c0cdfbSPalmer Dabbelt 
207e2c0cdfbSPalmer Dabbelt 	if (offset != fill_v) {
208da975dd4SZong Li 		/* Only emit the plt entry if offset over 32-bit range */
209da975dd4SZong Li 		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
210da975dd4SZong Li 			offset = module_emit_plt_entry(me, v);
211da975dd4SZong Li 			offset = (void *)offset - (void *)location;
212da975dd4SZong Li 		} else {
213e2c0cdfbSPalmer Dabbelt 			pr_err(
214e2c0cdfbSPalmer Dabbelt 			  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
215e2c0cdfbSPalmer Dabbelt 			  me->name, v, location);
216e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
217e2c0cdfbSPalmer Dabbelt 		}
218da975dd4SZong Li 	}
219e2c0cdfbSPalmer Dabbelt 
220e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
221e2c0cdfbSPalmer Dabbelt 	lo12 = (offset - hi20) & 0xfff;
222e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
223e2c0cdfbSPalmer Dabbelt 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
224e2c0cdfbSPalmer Dabbelt 	return 0;
225e2c0cdfbSPalmer Dabbelt }
226e2c0cdfbSPalmer Dabbelt 
227e1910c72SZong Li static int apply_r_riscv_call_rela(struct module *me, u32 *location,
228e1910c72SZong Li 				   Elf_Addr v)
229e1910c72SZong Li {
230*7df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
231e1910c72SZong Li 	s32 fill_v = offset;
232e1910c72SZong Li 	u32 hi20, lo12;
233e1910c72SZong Li 
234e1910c72SZong Li 	if (offset != fill_v) {
235e1910c72SZong Li 		pr_err(
236e1910c72SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
237e1910c72SZong Li 		  me->name, v, location);
238e1910c72SZong Li 		return -EINVAL;
239e1910c72SZong Li 	}
240e1910c72SZong Li 
241e1910c72SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
242e1910c72SZong Li 	lo12 = (offset - hi20) & 0xfff;
243e1910c72SZong Li 	*location = (*location & 0xfff) | hi20;
244e1910c72SZong Li 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
245e1910c72SZong Li 	return 0;
246e1910c72SZong Li }
247e1910c72SZong Li 
248e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
249e2c0cdfbSPalmer Dabbelt 				    Elf_Addr v)
250e2c0cdfbSPalmer Dabbelt {
251e2c0cdfbSPalmer Dabbelt 	return 0;
252e2c0cdfbSPalmer Dabbelt }
253e2c0cdfbSPalmer Dabbelt 
25429e405cdSZong Li static int apply_r_riscv_align_rela(struct module *me, u32 *location,
25529e405cdSZong Li 				    Elf_Addr v)
25629e405cdSZong Li {
25729e405cdSZong Li 	pr_err(
25829e405cdSZong Li 	  "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
25929e405cdSZong Li 	  me->name, location);
26029e405cdSZong Li 	return -EINVAL;
26129e405cdSZong Li }
26229e405cdSZong Li 
2638e691b16SZong Li static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
2648e691b16SZong Li 				    Elf_Addr v)
2658e691b16SZong Li {
2668e691b16SZong Li 	*(u32 *)location += (*(u32 *)v);
2678e691b16SZong Li 	return 0;
2688e691b16SZong Li }
2698e691b16SZong Li 
2704aad074cSZong Li static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
2714aad074cSZong Li 				    Elf_Addr v)
2724aad074cSZong Li {
2734aad074cSZong Li 	*(u32 *)location -= (*(u32 *)v);
2744aad074cSZong Li 	return 0;
2754aad074cSZong Li }
2764aad074cSZong Li 
277e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
278e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
27977aa85deSAndreas Schwab 	[R_RISCV_32]			= apply_r_riscv_32_rela,
280e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
281e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
282e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
28356ea45aeSZong Li 	[R_RISCV_RVC_BRANCH]		= apply_r_riscv_rcv_branch_rela,
28456ea45aeSZong Li 	[R_RISCV_RVC_JUMP]		= apply_r_riscv_rvc_jump_rela,
285e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
286e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
287e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
288e7456e69SZong Li 	[R_RISCV_HI20]			= apply_r_riscv_hi20_rela,
289e7456e69SZong Li 	[R_RISCV_LO12_I]		= apply_r_riscv_lo12_i_rela,
290e7456e69SZong Li 	[R_RISCV_LO12_S]		= apply_r_riscv_lo12_s_rela,
291da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
292e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
293e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
294e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
29529e405cdSZong Li 	[R_RISCV_ALIGN]			= apply_r_riscv_align_rela,
2968e691b16SZong Li 	[R_RISCV_ADD32]			= apply_r_riscv_add32_rela,
2974aad074cSZong Li 	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
298e2c0cdfbSPalmer Dabbelt };
299e2c0cdfbSPalmer Dabbelt 
300e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
301e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
302e2c0cdfbSPalmer Dabbelt 		       struct module *me)
303e2c0cdfbSPalmer Dabbelt {
304e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
305e2c0cdfbSPalmer Dabbelt 	int (*handler)(struct module *me, u32 *location, Elf_Addr v);
306e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
307e2c0cdfbSPalmer Dabbelt 	u32 *location;
308e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
309e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
310e2c0cdfbSPalmer Dabbelt 	int res;
311e2c0cdfbSPalmer Dabbelt 
312e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
313e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
314e2c0cdfbSPalmer Dabbelt 
315e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
316e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
317e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
318e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
319e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
320e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
321e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
322e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
323e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
324e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
325e2c0cdfbSPalmer Dabbelt 				continue;
326e2c0cdfbSPalmer Dabbelt 			pr_warning("%s: Unknown symbol %s\n",
327e2c0cdfbSPalmer Dabbelt 				   me->name, strtab + sym->st_name);
328e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
329e2c0cdfbSPalmer Dabbelt 		}
330e2c0cdfbSPalmer Dabbelt 
331e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
332e2c0cdfbSPalmer Dabbelt 
333e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
334e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
335e2c0cdfbSPalmer Dabbelt 		else
336e2c0cdfbSPalmer Dabbelt 			handler = NULL;
337e2c0cdfbSPalmer Dabbelt 
338e2c0cdfbSPalmer Dabbelt 		if (!handler) {
339e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
340e2c0cdfbSPalmer Dabbelt 			       me->name, type);
341e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
342e2c0cdfbSPalmer Dabbelt 		}
343e2c0cdfbSPalmer Dabbelt 
344e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
345e2c0cdfbSPalmer Dabbelt 
346e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
347e2c0cdfbSPalmer Dabbelt 			unsigned int j;
348e2c0cdfbSPalmer Dabbelt 
349e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
350*7df85002SZong Li 				unsigned long hi20_loc =
351e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
352e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
353da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
354da975dd4SZong Li 
355da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
356da975dd4SZong Li 				if (hi20_loc == sym->st_value
357da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
358da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
359da975dd4SZong Li 					s32 hi20, lo12;
360e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
361e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
362e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
363*7df85002SZong Li 					unsigned long hi20_sym_val =
364e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
365e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
366da975dd4SZong Li 
367e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
368*7df85002SZong Li 					size_t offset = hi20_sym_val - hi20_loc;
369da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
370da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
371da975dd4SZong Li 						offset = module_emit_got_entry(
372da975dd4SZong Li 							 me, hi20_sym_val);
373da975dd4SZong Li 						offset = offset - hi20_loc;
374da975dd4SZong Li 					}
375da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
376da975dd4SZong Li 					lo12 = offset - hi20;
377e2c0cdfbSPalmer Dabbelt 					v = lo12;
378da975dd4SZong Li 
379e2c0cdfbSPalmer Dabbelt 					break;
380e2c0cdfbSPalmer Dabbelt 				}
381e2c0cdfbSPalmer Dabbelt 			}
382e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
383e2c0cdfbSPalmer Dabbelt 				pr_err(
384da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
385e2c0cdfbSPalmer Dabbelt 				  me->name);
386e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
387e2c0cdfbSPalmer Dabbelt 			}
388e2c0cdfbSPalmer Dabbelt 		}
389e2c0cdfbSPalmer Dabbelt 
390e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
391e2c0cdfbSPalmer Dabbelt 		if (res)
392e2c0cdfbSPalmer Dabbelt 			return res;
393e2c0cdfbSPalmer Dabbelt 	}
394e2c0cdfbSPalmer Dabbelt 
395e2c0cdfbSPalmer Dabbelt 	return 0;
396e2c0cdfbSPalmer Dabbelt }
397