xref: /linux-6.15/arch/riscv/kernel/module.c (revision e1910c72)
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 
52e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
53e2c0cdfbSPalmer Dabbelt 					 Elf_Addr v)
54e2c0cdfbSPalmer Dabbelt {
55e2c0cdfbSPalmer Dabbelt 	s64 offset = (void *)v - (void *)location;
56e2c0cdfbSPalmer Dabbelt 	s32 hi20;
57e2c0cdfbSPalmer Dabbelt 
58e2c0cdfbSPalmer Dabbelt 	if (offset != (s32)offset) {
59e2c0cdfbSPalmer Dabbelt 		pr_err(
60e2c0cdfbSPalmer Dabbelt 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
61e2c0cdfbSPalmer Dabbelt 		  me->name, v, location);
62e2c0cdfbSPalmer Dabbelt 		return -EINVAL;
63e2c0cdfbSPalmer Dabbelt 	}
64e2c0cdfbSPalmer Dabbelt 
65e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
66e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
67e2c0cdfbSPalmer Dabbelt 	return 0;
68e2c0cdfbSPalmer Dabbelt }
69e2c0cdfbSPalmer Dabbelt 
70e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
71e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
72e2c0cdfbSPalmer Dabbelt {
73e2c0cdfbSPalmer Dabbelt 	/*
74e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
75e2c0cdfbSPalmer Dabbelt 	 * handler.
76e2c0cdfbSPalmer Dabbelt 	 */
77e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfffff) | ((v & 0xfff) << 20);
78e2c0cdfbSPalmer Dabbelt 	return 0;
79e2c0cdfbSPalmer Dabbelt }
80e2c0cdfbSPalmer Dabbelt 
81e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
82e2c0cdfbSPalmer Dabbelt 					   Elf_Addr v)
83e2c0cdfbSPalmer Dabbelt {
84e2c0cdfbSPalmer Dabbelt 	/*
85e2c0cdfbSPalmer Dabbelt 	 * v is the lo12 value to fill. It is calculated before calling this
86e2c0cdfbSPalmer Dabbelt 	 * handler.
87e2c0cdfbSPalmer Dabbelt 	 */
88e2c0cdfbSPalmer Dabbelt 	u32 imm11_5 = (v & 0xfe0) << (31 - 11);
89e2c0cdfbSPalmer Dabbelt 	u32 imm4_0 = (v & 0x1f) << (11 - 4);
90e2c0cdfbSPalmer Dabbelt 
91e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
92e2c0cdfbSPalmer Dabbelt 	return 0;
93e2c0cdfbSPalmer Dabbelt }
94e2c0cdfbSPalmer Dabbelt 
95da975dd4SZong Li static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
96da975dd4SZong Li 				       Elf_Addr v)
97da975dd4SZong Li {
98da975dd4SZong Li 	s64 offset = (void *)v - (void *)location;
99da975dd4SZong Li 	s32 hi20;
100da975dd4SZong Li 
101da975dd4SZong Li 	/* Always emit the got entry */
102da975dd4SZong Li 	if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
103da975dd4SZong Li 		offset = module_emit_got_entry(me, v);
104da975dd4SZong Li 		offset = (void *)offset - (void *)location;
105da975dd4SZong Li 	} else {
106da975dd4SZong Li 		pr_err(
107da975dd4SZong Li 		  "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
108da975dd4SZong Li 		  me->name, v, location);
109da975dd4SZong Li 		return -EINVAL;
110da975dd4SZong Li 	}
111da975dd4SZong Li 
112da975dd4SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
113da975dd4SZong Li 	*location = (*location & 0xfff) | hi20;
114da975dd4SZong Li 	return 0;
115da975dd4SZong Li }
116da975dd4SZong Li 
117e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
118e2c0cdfbSPalmer Dabbelt 				       Elf_Addr v)
119e2c0cdfbSPalmer Dabbelt {
120e2c0cdfbSPalmer Dabbelt 	s64 offset = (void *)v - (void *)location;
121e2c0cdfbSPalmer Dabbelt 	s32 fill_v = offset;
122e2c0cdfbSPalmer Dabbelt 	u32 hi20, lo12;
123e2c0cdfbSPalmer Dabbelt 
124e2c0cdfbSPalmer Dabbelt 	if (offset != fill_v) {
125da975dd4SZong Li 		/* Only emit the plt entry if offset over 32-bit range */
126da975dd4SZong Li 		if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
127da975dd4SZong Li 			offset = module_emit_plt_entry(me, v);
128da975dd4SZong Li 			offset = (void *)offset - (void *)location;
129da975dd4SZong Li 		} else {
130e2c0cdfbSPalmer Dabbelt 			pr_err(
131e2c0cdfbSPalmer Dabbelt 			  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
132e2c0cdfbSPalmer Dabbelt 			  me->name, v, location);
133e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
134e2c0cdfbSPalmer Dabbelt 		}
135da975dd4SZong Li 	}
136e2c0cdfbSPalmer Dabbelt 
137e2c0cdfbSPalmer Dabbelt 	hi20 = (offset + 0x800) & 0xfffff000;
138e2c0cdfbSPalmer Dabbelt 	lo12 = (offset - hi20) & 0xfff;
139e2c0cdfbSPalmer Dabbelt 	*location = (*location & 0xfff) | hi20;
140e2c0cdfbSPalmer Dabbelt 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
141e2c0cdfbSPalmer Dabbelt 	return 0;
142e2c0cdfbSPalmer Dabbelt }
143e2c0cdfbSPalmer Dabbelt 
144*e1910c72SZong Li static int apply_r_riscv_call_rela(struct module *me, u32 *location,
145*e1910c72SZong Li 				   Elf_Addr v)
146*e1910c72SZong Li {
147*e1910c72SZong Li 	s64 offset = (void *)v - (void *)location;
148*e1910c72SZong Li 	s32 fill_v = offset;
149*e1910c72SZong Li 	u32 hi20, lo12;
150*e1910c72SZong Li 
151*e1910c72SZong Li 	if (offset != fill_v) {
152*e1910c72SZong Li 		pr_err(
153*e1910c72SZong Li 		  "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
154*e1910c72SZong Li 		  me->name, v, location);
155*e1910c72SZong Li 		return -EINVAL;
156*e1910c72SZong Li 	}
157*e1910c72SZong Li 
158*e1910c72SZong Li 	hi20 = (offset + 0x800) & 0xfffff000;
159*e1910c72SZong Li 	lo12 = (offset - hi20) & 0xfff;
160*e1910c72SZong Li 	*location = (*location & 0xfff) | hi20;
161*e1910c72SZong Li 	*(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
162*e1910c72SZong Li 	return 0;
163*e1910c72SZong Li }
164*e1910c72SZong Li 
165e2c0cdfbSPalmer Dabbelt static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
166e2c0cdfbSPalmer Dabbelt 				    Elf_Addr v)
167e2c0cdfbSPalmer Dabbelt {
168e2c0cdfbSPalmer Dabbelt 	return 0;
169e2c0cdfbSPalmer Dabbelt }
170e2c0cdfbSPalmer Dabbelt 
171e2c0cdfbSPalmer Dabbelt static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
172e2c0cdfbSPalmer Dabbelt 				Elf_Addr v) = {
173e2c0cdfbSPalmer Dabbelt 	[R_RISCV_64]			= apply_r_riscv_64_rela,
174e2c0cdfbSPalmer Dabbelt 	[R_RISCV_BRANCH]		= apply_r_riscv_branch_rela,
175e2c0cdfbSPalmer Dabbelt 	[R_RISCV_JAL]			= apply_r_riscv_jal_rela,
176e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_HI20]		= apply_r_riscv_pcrel_hi20_rela,
177e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_I]		= apply_r_riscv_pcrel_lo12_i_rela,
178e2c0cdfbSPalmer Dabbelt 	[R_RISCV_PCREL_LO12_S]		= apply_r_riscv_pcrel_lo12_s_rela,
179da975dd4SZong Li 	[R_RISCV_GOT_HI20]		= apply_r_riscv_got_hi20_rela,
180e2c0cdfbSPalmer Dabbelt 	[R_RISCV_CALL_PLT]		= apply_r_riscv_call_plt_rela,
181*e1910c72SZong Li 	[R_RISCV_CALL]			= apply_r_riscv_call_rela,
182e2c0cdfbSPalmer Dabbelt 	[R_RISCV_RELAX]			= apply_r_riscv_relax_rela,
183e2c0cdfbSPalmer Dabbelt };
184e2c0cdfbSPalmer Dabbelt 
185e2c0cdfbSPalmer Dabbelt int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
186e2c0cdfbSPalmer Dabbelt 		       unsigned int symindex, unsigned int relsec,
187e2c0cdfbSPalmer Dabbelt 		       struct module *me)
188e2c0cdfbSPalmer Dabbelt {
189e2c0cdfbSPalmer Dabbelt 	Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
190e2c0cdfbSPalmer Dabbelt 	int (*handler)(struct module *me, u32 *location, Elf_Addr v);
191e2c0cdfbSPalmer Dabbelt 	Elf_Sym *sym;
192e2c0cdfbSPalmer Dabbelt 	u32 *location;
193e2c0cdfbSPalmer Dabbelt 	unsigned int i, type;
194e2c0cdfbSPalmer Dabbelt 	Elf_Addr v;
195e2c0cdfbSPalmer Dabbelt 	int res;
196e2c0cdfbSPalmer Dabbelt 
197e2c0cdfbSPalmer Dabbelt 	pr_debug("Applying relocate section %u to %u\n", relsec,
198e2c0cdfbSPalmer Dabbelt 	       sechdrs[relsec].sh_info);
199e2c0cdfbSPalmer Dabbelt 
200e2c0cdfbSPalmer Dabbelt 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
201e2c0cdfbSPalmer Dabbelt 		/* This is where to make the change */
202e2c0cdfbSPalmer Dabbelt 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
203e2c0cdfbSPalmer Dabbelt 			+ rel[i].r_offset;
204e2c0cdfbSPalmer Dabbelt 		/* This is the symbol it is referring to */
205e2c0cdfbSPalmer Dabbelt 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
206e2c0cdfbSPalmer Dabbelt 			+ ELF_RISCV_R_SYM(rel[i].r_info);
207e2c0cdfbSPalmer Dabbelt 		if (IS_ERR_VALUE(sym->st_value)) {
208e2c0cdfbSPalmer Dabbelt 			/* Ignore unresolved weak symbol */
209e2c0cdfbSPalmer Dabbelt 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
210e2c0cdfbSPalmer Dabbelt 				continue;
211e2c0cdfbSPalmer Dabbelt 			pr_warning("%s: Unknown symbol %s\n",
212e2c0cdfbSPalmer Dabbelt 				   me->name, strtab + sym->st_name);
213e2c0cdfbSPalmer Dabbelt 			return -ENOENT;
214e2c0cdfbSPalmer Dabbelt 		}
215e2c0cdfbSPalmer Dabbelt 
216e2c0cdfbSPalmer Dabbelt 		type = ELF_RISCV_R_TYPE(rel[i].r_info);
217e2c0cdfbSPalmer Dabbelt 
218e2c0cdfbSPalmer Dabbelt 		if (type < ARRAY_SIZE(reloc_handlers_rela))
219e2c0cdfbSPalmer Dabbelt 			handler = reloc_handlers_rela[type];
220e2c0cdfbSPalmer Dabbelt 		else
221e2c0cdfbSPalmer Dabbelt 			handler = NULL;
222e2c0cdfbSPalmer Dabbelt 
223e2c0cdfbSPalmer Dabbelt 		if (!handler) {
224e2c0cdfbSPalmer Dabbelt 			pr_err("%s: Unknown relocation type %u\n",
225e2c0cdfbSPalmer Dabbelt 			       me->name, type);
226e2c0cdfbSPalmer Dabbelt 			return -EINVAL;
227e2c0cdfbSPalmer Dabbelt 		}
228e2c0cdfbSPalmer Dabbelt 
229e2c0cdfbSPalmer Dabbelt 		v = sym->st_value + rel[i].r_addend;
230e2c0cdfbSPalmer Dabbelt 
231e2c0cdfbSPalmer Dabbelt 		if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
232e2c0cdfbSPalmer Dabbelt 			unsigned int j;
233e2c0cdfbSPalmer Dabbelt 
234e2c0cdfbSPalmer Dabbelt 			for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
235e2c0cdfbSPalmer Dabbelt 				u64 hi20_loc =
236e2c0cdfbSPalmer Dabbelt 					sechdrs[sechdrs[relsec].sh_info].sh_addr
237e2c0cdfbSPalmer Dabbelt 					+ rel[j].r_offset;
238da975dd4SZong Li 				u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
239da975dd4SZong Li 
240da975dd4SZong Li 				/* Find the corresponding HI20 relocation entry */
241da975dd4SZong Li 				if (hi20_loc == sym->st_value
242da975dd4SZong Li 				    && (hi20_type == R_RISCV_PCREL_HI20
243da975dd4SZong Li 					|| hi20_type == R_RISCV_GOT_HI20)) {
244da975dd4SZong Li 					s32 hi20, lo12;
245e2c0cdfbSPalmer Dabbelt 					Elf_Sym *hi20_sym =
246e2c0cdfbSPalmer Dabbelt 						(Elf_Sym *)sechdrs[symindex].sh_addr
247e2c0cdfbSPalmer Dabbelt 						+ ELF_RISCV_R_SYM(rel[j].r_info);
248e2c0cdfbSPalmer Dabbelt 					u64 hi20_sym_val =
249e2c0cdfbSPalmer Dabbelt 						hi20_sym->st_value
250e2c0cdfbSPalmer Dabbelt 						+ rel[j].r_addend;
251da975dd4SZong Li 
252e2c0cdfbSPalmer Dabbelt 					/* Calculate lo12 */
253da975dd4SZong Li 					u64 offset = hi20_sym_val - hi20_loc;
254da975dd4SZong Li 					if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
255da975dd4SZong Li 					    && hi20_type == R_RISCV_GOT_HI20) {
256da975dd4SZong Li 						offset = module_emit_got_entry(
257da975dd4SZong Li 							 me, hi20_sym_val);
258da975dd4SZong Li 						offset = offset - hi20_loc;
259da975dd4SZong Li 					}
260da975dd4SZong Li 					hi20 = (offset + 0x800) & 0xfffff000;
261da975dd4SZong Li 					lo12 = offset - hi20;
262e2c0cdfbSPalmer Dabbelt 					v = lo12;
263da975dd4SZong Li 
264e2c0cdfbSPalmer Dabbelt 					break;
265e2c0cdfbSPalmer Dabbelt 				}
266e2c0cdfbSPalmer Dabbelt 			}
267e2c0cdfbSPalmer Dabbelt 			if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
268e2c0cdfbSPalmer Dabbelt 				pr_err(
269da975dd4SZong Li 				  "%s: Can not find HI20 relocation information\n",
270e2c0cdfbSPalmer Dabbelt 				  me->name);
271e2c0cdfbSPalmer Dabbelt 				return -EINVAL;
272e2c0cdfbSPalmer Dabbelt 			}
273e2c0cdfbSPalmer Dabbelt 		}
274e2c0cdfbSPalmer Dabbelt 
275e2c0cdfbSPalmer Dabbelt 		res = handler(me, location, v);
276e2c0cdfbSPalmer Dabbelt 		if (res)
277e2c0cdfbSPalmer Dabbelt 			return res;
278e2c0cdfbSPalmer Dabbelt 	}
279e2c0cdfbSPalmer Dabbelt 
280e2c0cdfbSPalmer Dabbelt 	return 0;
281e2c0cdfbSPalmer Dabbelt }
282