xref: /openbmc/linux/arch/riscv/kernel/module.c (revision 04ce8d3f)
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>
11e2c0cdfbSPalmer Dabbelt 
1277aa85deSAndreas Schwab static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
1377aa85deSAndreas Schwab {
1477aa85deSAndreas Schwab 	if (v != (u32)v) {
1577aa85deSAndreas Schwab 		pr_err("%s: value %016llx out of range for 32-bit field\n",
16ef3a6140SOlof Johansson 		       me->name, (long long)v);
1777aa85deSAndreas Schwab 		return -EINVAL;
1877aa85deSAndreas Schwab 	}
1977aa85deSAndreas Schwab 	*location = v;
2077aa85deSAndreas Schwab 	return 0;
2177aa85deSAndreas Schwab }
2277aa85deSAndreas Schwab 
23e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
24e2c0cdfbSPalmer Dabbelt {
25e2c0cdfbSPalmer Dabbelt 	*(u64 *)location = v;
26e2c0cdfbSPalmer Dabbelt 	return 0;
27e2c0cdfbSPalmer Dabbelt }
28e2c0cdfbSPalmer Dabbelt 
29e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
30e2c0cdfbSPalmer Dabbelt 				     Elf_Addr v)
31e2c0cdfbSPalmer Dabbelt {
327df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
33e2c0cdfbSPalmer Dabbelt 	u32 imm12 = (offset & 0x1000) << (31 - 12);
34e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) >> (11 - 7);
35e2c0cdfbSPalmer Dabbelt 	u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
36e2c0cdfbSPalmer Dabbelt 	u32 imm4_1 = (offset & 0x1e) << (11 - 4);
37e2c0cdfbSPalmer Dabbelt 
38e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
39e2c0cdfbSPalmer Dabbelt 	return 0;
40e2c0cdfbSPalmer Dabbelt }
41e2c0cdfbSPalmer Dabbelt 
42e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
43e2c0cdfbSPalmer Dabbelt 				  Elf_Addr v)
44e2c0cdfbSPalmer Dabbelt {
457df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
46e2c0cdfbSPalmer Dabbelt 	u32 imm20 = (offset & 0x100000) << (31 - 20);
47e2c0cdfbSPalmer Dabbelt 	u32 imm19_12 = (offset & 0xff000);
48e2c0cdfbSPalmer Dabbelt 	u32 imm11 = (offset & 0x800) << (20 - 11);
49e2c0cdfbSPalmer Dabbelt 	u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
50e2c0cdfbSPalmer Dabbelt 
51e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
52e2c0cdfbSPalmer Dabbelt 	return 0;
53e2c0cdfbSPalmer Dabbelt }
54e2c0cdfbSPalmer Dabbelt 
5556ea45aeSZong Li static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location,
5656ea45aeSZong Li 					 Elf_Addr v)
5756ea45aeSZong Li {
587df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
5956ea45aeSZong Li 	u16 imm8 = (offset & 0x100) << (12 - 8);
6056ea45aeSZong Li 	u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
6156ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
6256ea45aeSZong Li 	u16 imm4_3 = (offset & 0x18) << (12 - 5);
6356ea45aeSZong Li 	u16 imm2_1 = (offset & 0x6) << (12 - 10);
6456ea45aeSZong Li 
6556ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe383) |
6656ea45aeSZong Li 		    imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
6756ea45aeSZong Li 	return 0;
6856ea45aeSZong Li }
6956ea45aeSZong Li 
7056ea45aeSZong Li static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
7156ea45aeSZong Li 				       Elf_Addr v)
7256ea45aeSZong Li {
737df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
7456ea45aeSZong Li 	u16 imm11 = (offset & 0x800) << (12 - 11);
7556ea45aeSZong Li 	u16 imm10 = (offset & 0x400) >> (10 - 8);
7656ea45aeSZong Li 	u16 imm9_8 = (offset & 0x300) << (12 - 11);
7756ea45aeSZong Li 	u16 imm7 = (offset & 0x80) >> (7 - 6);
7856ea45aeSZong Li 	u16 imm6 = (offset & 0x40) << (12 - 11);
7956ea45aeSZong Li 	u16 imm5 = (offset & 0x20) >> (5 - 2);
8056ea45aeSZong Li 	u16 imm4 = (offset & 0x10) << (12 - 5);
8156ea45aeSZong Li 	u16 imm3_1 = (offset & 0xe) << (12 - 10);
8256ea45aeSZong Li 
8356ea45aeSZong Li 	*(u16 *)location = (*(u16 *)location & 0xe003) |
8456ea45aeSZong Li 		    imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
8556ea45aeSZong Li 	return 0;
8656ea45aeSZong Li }
8756ea45aeSZong Li 
88e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
89e2c0cdfbSPalmer Dabbelt 					 Elf_Addr v)
90e2c0cdfbSPalmer Dabbelt {
917df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
92e2c0cdfbSPalmer Dabbelt 	s32 hi20;
93e2c0cdfbSPalmer Dabbelt 
94e2c0cdfbSPalmer Dabbelt 	if (offset != (s32)offset) {
95e2c0cdfbSPalmer Dabbelt 		pr_err(
96e2c0cdfbSPalmer Dabbelt 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
97ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
98e2c0cdfbSPalmer Dabbelt 		return -EINVAL;
99e2c0cdfbSPalmer Dabbelt 	}
100e2c0cdfbSPalmer Dabbelt 
101e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
102e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
103e2c0cdfbSPalmer Dabbelt 	return 0;
104e2c0cdfbSPalmer Dabbelt }
105e2c0cdfbSPalmer Dabbelt 
106e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
107e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
108e2c0cdfbSPalmer Dabbelt {
109e2c0cdfbSPalmer Dabbelt 	/*
110e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
111e2c0cdfbSPalmer Dabbelt 	 * handler.
112e2c0cdfbSPalmer Dabbelt 	 */
113e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfffff) | ((v & 0xfff) << 20);
114e2c0cdfbSPalmer Dabbelt 	return 0;
115e2c0cdfbSPalmer Dabbelt }
116e2c0cdfbSPalmer Dabbelt 
117e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
118e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
119e2c0cdfbSPalmer Dabbelt {
120e2c0cdfbSPalmer Dabbelt 	/*
121e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
122e2c0cdfbSPalmer Dabbelt 	 * handler.
123e2c0cdfbSPalmer Dabbelt 	 */
124e2c0cdfbSPalmer Dabbelt 	u32 imm11_5 = (v & 0xfe0) << (31 - 11);
125e2c0cdfbSPalmer Dabbelt 	u32 imm4_0 = (v & 0x1f) << (11 - 4);
126e2c0cdfbSPalmer Dabbelt 
127e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
128e2c0cdfbSPalmer Dabbelt 	return 0;
129e2c0cdfbSPalmer Dabbelt }
130e2c0cdfbSPalmer Dabbelt 
131e7456e69SZong Li static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
132e7456e69SZong Li 				   Elf_Addr v)
133e7456e69SZong Li {
134e7456e69SZong Li 	s32 hi20;
135e7456e69SZong Li 
136da4ed378SJoe Perches 	if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
137e7456e69SZong Li 		pr_err(
138e7456e69SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
139ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
140e7456e69SZong Li 		return -EINVAL;
141e7456e69SZong Li 	}
142e7456e69SZong Li 
143e7456e69SZong Li 	hi20 = ((s32)v + 0x800) & 0xfffff000;
144e7456e69SZong Li 	*location = (*location & 0xfff) | hi20;
145e7456e69SZong Li 	return 0;
146e7456e69SZong Li }
147e7456e69SZong Li 
148e7456e69SZong Li static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location,
149e7456e69SZong Li 				     Elf_Addr v)
150e7456e69SZong Li {
151e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
152e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
153e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
154e7456e69SZong Li 	*location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
155e7456e69SZong Li 	return 0;
156e7456e69SZong Li }
157e7456e69SZong Li 
158e7456e69SZong Li static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location,
159e7456e69SZong Li 				     Elf_Addr v)
160e7456e69SZong Li {
161e7456e69SZong Li 	/* Skip medlow checking because of filtering by HI20 already */
162e7456e69SZong Li 	s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
163e7456e69SZong Li 	s32 lo12 = ((s32)v - hi20);
164e7456e69SZong Li 	u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
165e7456e69SZong Li 	u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
166e7456e69SZong Li 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
167e7456e69SZong Li 	return 0;
168e7456e69SZong Li }
169e7456e69SZong Li 
170da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
171da975dd4SZong Li 				       Elf_Addr v)
172da975dd4SZong Li {
1737df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
174da975dd4SZong Li 	s32 hi20;
175da975dd4SZong Li 
176da975dd4SZong Li 	/* Always emit the got entry */
177da975dd4SZong Li 	if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
178da975dd4SZong Li 		offset = module_emit_got_entry(me, v);
179da975dd4SZong Li 		offset = (void *)offset - (void *)location;
180da975dd4SZong Li 	} else {
181da975dd4SZong Li 		pr_err(
182da975dd4SZong Li 		  "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
183ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
184da975dd4SZong Li 		return -EINVAL;
185da975dd4SZong Li 	}
186da975dd4SZong Li 
187da975dd4SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
188da975dd4SZong Li 	*location = (*location & 0xfff) | hi20;
189da975dd4SZong Li 	return 0;
190da975dd4SZong Li }
191da975dd4SZong Li 
192e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
193e2c0cdfbSPalmer Dabbelt 				       Elf_Addr v)
194e2c0cdfbSPalmer Dabbelt {
1957df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
196e2c0cdfbSPalmer Dabbelt 	s32 fill_v = offset;
197e2c0cdfbSPalmer Dabbelt 	u32 hi20, lo12;
198e2c0cdfbSPalmer Dabbelt 
199e2c0cdfbSPalmer Dabbelt 	if (offset != fill_v) {
200da975dd4SZong Li 		/* Only emit the plt entry if offset over 32-bit range */
201da975dd4SZong Li 		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
202da975dd4SZong Li 			offset = module_emit_plt_entry(me, v);
203da975dd4SZong Li 			offset = (void *)offset - (void *)location;
204da975dd4SZong Li 		} else {
205e2c0cdfbSPalmer Dabbelt 			pr_err(
206e2c0cdfbSPalmer Dabbelt 			  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
207ef3a6140SOlof Johansson 			  me->name, (long long)v, location);
208e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
209e2c0cdfbSPalmer Dabbelt 		}
210da975dd4SZong Li 	}
211e2c0cdfbSPalmer Dabbelt 
212e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
213e2c0cdfbSPalmer Dabbelt 	lo12 = (offset - hi20) & 0xfff;
214e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
215e2c0cdfbSPalmer Dabbelt 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
216e2c0cdfbSPalmer Dabbelt 	return 0;
217e2c0cdfbSPalmer Dabbelt }
218e2c0cdfbSPalmer Dabbelt 
219e1910c72SZong Li static int apply_r_riscv_call_rela(struct module *me, u32 *location,
220e1910c72SZong Li 				   Elf_Addr v)
221e1910c72SZong Li {
2227df85002SZong Li 	ptrdiff_t offset = (void *)v - (void *)location;
223e1910c72SZong Li 	s32 fill_v = offset;
224e1910c72SZong Li 	u32 hi20, lo12;
225e1910c72SZong Li 
226e1910c72SZong Li 	if (offset != fill_v) {
227e1910c72SZong Li 		pr_err(
228e1910c72SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
229ef3a6140SOlof Johansson 		  me->name, (long long)v, location);
230e1910c72SZong Li 		return -EINVAL;
231e1910c72SZong Li 	}
232e1910c72SZong Li 
233e1910c72SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
234e1910c72SZong Li 	lo12 = (offset - hi20) & 0xfff;
235e1910c72SZong Li 	*location = (*location & 0xfff) | hi20;
236e1910c72SZong Li 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
237e1910c72SZong Li 	return 0;
238e1910c72SZong Li }
239e1910c72SZong Li 
240e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
241e2c0cdfbSPalmer Dabbelt 				    Elf_Addr v)
242e2c0cdfbSPalmer Dabbelt {
243e2c0cdfbSPalmer Dabbelt 	return 0;
244e2c0cdfbSPalmer Dabbelt }
245e2c0cdfbSPalmer Dabbelt 
24629e405cdSZong Li static int apply_r_riscv_align_rela(struct module *me, u32 *location,
24729e405cdSZong Li 				    Elf_Addr v)
24829e405cdSZong Li {
24929e405cdSZong Li 	pr_err(
25029e405cdSZong Li 	  "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
25129e405cdSZong Li 	  me->name, location);
25229e405cdSZong Li 	return -EINVAL;
25329e405cdSZong Li }
25429e405cdSZong Li 
2558e691b16SZong Li static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
2568e691b16SZong Li 				    Elf_Addr v)
2578e691b16SZong Li {
258781c8fe2SAndreas Schwab 	*(u32 *)location += (u32)v;
2598e691b16SZong Li 	return 0;
2608e691b16SZong Li }
2618e691b16SZong Li 
2624aad074cSZong Li static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
2634aad074cSZong Li 				    Elf_Addr v)
2644aad074cSZong Li {
265781c8fe2SAndreas Schwab 	*(u32 *)location -= (u32)v;
2664aad074cSZong Li 	return 0;
2674aad074cSZong Li }
2684aad074cSZong Li 
269e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
270e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
27177aa85deSAndreas Schwab 	[R_RISCV_32]			= apply_r_riscv_32_rela,
272e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
273e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
274e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
27556ea45aeSZong Li 	[R_RISCV_RVC_BRANCH]		= apply_r_riscv_rcv_branch_rela,
27656ea45aeSZong Li 	[R_RISCV_RVC_JUMP]		= apply_r_riscv_rvc_jump_rela,
277e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
278e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
279e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
280e7456e69SZong Li 	[R_RISCV_HI20]			= apply_r_riscv_hi20_rela,
281e7456e69SZong Li 	[R_RISCV_LO12_I]		= apply_r_riscv_lo12_i_rela,
282e7456e69SZong Li 	[R_RISCV_LO12_S]		= apply_r_riscv_lo12_s_rela,
283da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
284e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
285e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
286e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
28729e405cdSZong Li 	[R_RISCV_ALIGN]			= apply_r_riscv_align_rela,
2888e691b16SZong Li 	[R_RISCV_ADD32]			= apply_r_riscv_add32_rela,
2894aad074cSZong Li 	[R_RISCV_SUB32]			= apply_r_riscv_sub32_rela,
290e2c0cdfbSPalmer Dabbelt };
291e2c0cdfbSPalmer Dabbelt 
292e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
293e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
294e2c0cdfbSPalmer Dabbelt 		       struct module *me)
295e2c0cdfbSPalmer Dabbelt {
296e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
297e2c0cdfbSPalmer Dabbelt 	int (*handler)(struct module *me, u32 *location, Elf_Addr v);
298e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
299e2c0cdfbSPalmer Dabbelt 	u32 *location;
300e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
301e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
302e2c0cdfbSPalmer Dabbelt 	int res;
303e2c0cdfbSPalmer Dabbelt 
304e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
305e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
306e2c0cdfbSPalmer Dabbelt 
307e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
308e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
309e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
310e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
311e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
312e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
313e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
314e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
315e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
316e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
317e2c0cdfbSPalmer Dabbelt 				continue;
31804ce8d3fSKefeng Wang 			pr_warn("%s: Unknown symbol %s\n",
319e2c0cdfbSPalmer Dabbelt 				me->name, strtab + sym->st_name);
320e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
321e2c0cdfbSPalmer Dabbelt 		}
322e2c0cdfbSPalmer Dabbelt 
323e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
324e2c0cdfbSPalmer Dabbelt 
325e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
326e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
327e2c0cdfbSPalmer Dabbelt 		else
328e2c0cdfbSPalmer Dabbelt 			handler = NULL;
329e2c0cdfbSPalmer Dabbelt 
330e2c0cdfbSPalmer Dabbelt 		if (!handler) {
331e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
332e2c0cdfbSPalmer Dabbelt 			       me->name, type);
333e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
334e2c0cdfbSPalmer Dabbelt 		}
335e2c0cdfbSPalmer Dabbelt 
336e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
337e2c0cdfbSPalmer Dabbelt 
338e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
339e2c0cdfbSPalmer Dabbelt 			unsigned int j;
340e2c0cdfbSPalmer Dabbelt 
341e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
3427df85002SZong Li 				unsigned long hi20_loc =
343e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
344e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
345da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
346da975dd4SZong Li 
347da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
348da975dd4SZong Li 				if (hi20_loc == sym->st_value
349da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
350da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
351da975dd4SZong Li 					s32 hi20, lo12;
352e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
353e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
354e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
3557df85002SZong Li 					unsigned long hi20_sym_val =
356e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
357e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
358da975dd4SZong Li 
359e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
3607df85002SZong Li 					size_t offset = hi20_sym_val - hi20_loc;
361da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
362da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
363da975dd4SZong Li 						offset = module_emit_got_entry(
364da975dd4SZong Li 							 me, hi20_sym_val);
365da975dd4SZong Li 						offset = offset - hi20_loc;
366da975dd4SZong Li 					}
367da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
368da975dd4SZong Li 					lo12 = offset - hi20;
369e2c0cdfbSPalmer Dabbelt 					v = lo12;
370da975dd4SZong Li 
371e2c0cdfbSPalmer Dabbelt 					break;
372e2c0cdfbSPalmer Dabbelt 				}
373e2c0cdfbSPalmer Dabbelt 			}
374e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
375e2c0cdfbSPalmer Dabbelt 				pr_err(
376da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
377e2c0cdfbSPalmer Dabbelt 				  me->name);
378e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
379e2c0cdfbSPalmer Dabbelt 			}
380e2c0cdfbSPalmer Dabbelt 		}
381e2c0cdfbSPalmer Dabbelt 
382e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
383e2c0cdfbSPalmer Dabbelt 		if (res)
384e2c0cdfbSPalmer Dabbelt 			return res;
385e2c0cdfbSPalmer Dabbelt 	}
386e2c0cdfbSPalmer Dabbelt 
387e2c0cdfbSPalmer Dabbelt 	return 0;
388e2c0cdfbSPalmer Dabbelt }
389