xref: /OK3568_Linux_fs/kernel/arch/arm64/kernel/module-plts.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2014-2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/elf.h>
7*4882a593Smuzhiyun #include <linux/ftrace.h>
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/sort.h>
12*4882a593Smuzhiyun 
__get_adrp_add_pair(u64 dst,u64 pc,enum aarch64_insn_register reg)13*4882a593Smuzhiyun static struct plt_entry __get_adrp_add_pair(u64 dst, u64 pc,
14*4882a593Smuzhiyun 					    enum aarch64_insn_register reg)
15*4882a593Smuzhiyun {
16*4882a593Smuzhiyun 	u32 adrp, add;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun 	adrp = aarch64_insn_gen_adr(pc, dst, reg, AARCH64_INSN_ADR_TYPE_ADRP);
19*4882a593Smuzhiyun 	add = aarch64_insn_gen_add_sub_imm(reg, reg, dst % SZ_4K,
20*4882a593Smuzhiyun 					   AARCH64_INSN_VARIANT_64BIT,
21*4882a593Smuzhiyun 					   AARCH64_INSN_ADSB_ADD);
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun 	return (struct plt_entry){ cpu_to_le32(adrp), cpu_to_le32(add) };
24*4882a593Smuzhiyun }
25*4882a593Smuzhiyun 
get_plt_entry(u64 dst,void * pc)26*4882a593Smuzhiyun struct plt_entry get_plt_entry(u64 dst, void *pc)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	struct plt_entry plt;
29*4882a593Smuzhiyun 	static u32 br;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	if (!br)
32*4882a593Smuzhiyun 		br = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_16,
33*4882a593Smuzhiyun 						 AARCH64_INSN_BRANCH_NOLINK);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	plt = __get_adrp_add_pair(dst, (u64)pc, AARCH64_INSN_REG_16);
36*4882a593Smuzhiyun 	plt.br = cpu_to_le32(br);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	return plt;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun 
plt_entries_equal(const struct plt_entry * a,const struct plt_entry * b)41*4882a593Smuzhiyun bool plt_entries_equal(const struct plt_entry *a, const struct plt_entry *b)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	u64 p, q;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	/*
46*4882a593Smuzhiyun 	 * Check whether both entries refer to the same target:
47*4882a593Smuzhiyun 	 * do the cheapest checks first.
48*4882a593Smuzhiyun 	 * If the 'add' or 'br' opcodes are different, then the target
49*4882a593Smuzhiyun 	 * cannot be the same.
50*4882a593Smuzhiyun 	 */
51*4882a593Smuzhiyun 	if (a->add != b->add || a->br != b->br)
52*4882a593Smuzhiyun 		return false;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	p = ALIGN_DOWN((u64)a, SZ_4K);
55*4882a593Smuzhiyun 	q = ALIGN_DOWN((u64)b, SZ_4K);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/*
58*4882a593Smuzhiyun 	 * If the 'adrp' opcodes are the same then we just need to check
59*4882a593Smuzhiyun 	 * that they refer to the same 4k region.
60*4882a593Smuzhiyun 	 */
61*4882a593Smuzhiyun 	if (a->adrp == b->adrp && p == q)
62*4882a593Smuzhiyun 		return true;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	return (p + aarch64_insn_adrp_get_offset(le32_to_cpu(a->adrp))) ==
65*4882a593Smuzhiyun 	       (q + aarch64_insn_adrp_get_offset(le32_to_cpu(b->adrp)));
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
in_init(const struct module * mod,void * loc)68*4882a593Smuzhiyun static bool in_init(const struct module *mod, void *loc)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	return (u64)loc - (u64)mod->init_layout.base < mod->init_layout.size;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
module_emit_plt_entry(struct module * mod,Elf64_Shdr * sechdrs,void * loc,const Elf64_Rela * rela,Elf64_Sym * sym)73*4882a593Smuzhiyun u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,
74*4882a593Smuzhiyun 			  void *loc, const Elf64_Rela *rela,
75*4882a593Smuzhiyun 			  Elf64_Sym *sym)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
78*4882a593Smuzhiyun 							  &mod->arch.init;
79*4882a593Smuzhiyun 	struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr;
80*4882a593Smuzhiyun 	int i = pltsec->plt_num_entries;
81*4882a593Smuzhiyun 	int j = i - 1;
82*4882a593Smuzhiyun 	u64 val = sym->st_value + rela->r_addend;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	if (is_forbidden_offset_for_adrp(&plt[i].adrp))
85*4882a593Smuzhiyun 		i++;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	plt[i] = get_plt_entry(val, &plt[i]);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/*
90*4882a593Smuzhiyun 	 * Check if the entry we just created is a duplicate. Given that the
91*4882a593Smuzhiyun 	 * relocations are sorted, this will be the last entry we allocated.
92*4882a593Smuzhiyun 	 * (if one exists).
93*4882a593Smuzhiyun 	 */
94*4882a593Smuzhiyun 	if (j >= 0 && plt_entries_equal(plt + i, plt + j))
95*4882a593Smuzhiyun 		return (u64)&plt[j];
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	pltsec->plt_num_entries += i - j;
98*4882a593Smuzhiyun 	if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
99*4882a593Smuzhiyun 		return 0;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	return (u64)&plt[i];
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun #ifdef CONFIG_ARM64_ERRATUM_843419
module_emit_veneer_for_adrp(struct module * mod,Elf64_Shdr * sechdrs,void * loc,u64 val)105*4882a593Smuzhiyun u64 module_emit_veneer_for_adrp(struct module *mod, Elf64_Shdr *sechdrs,
106*4882a593Smuzhiyun 				void *loc, u64 val)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
109*4882a593Smuzhiyun 							  &mod->arch.init;
110*4882a593Smuzhiyun 	struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr;
111*4882a593Smuzhiyun 	int i = pltsec->plt_num_entries++;
112*4882a593Smuzhiyun 	u32 br;
113*4882a593Smuzhiyun 	int rd;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
116*4882a593Smuzhiyun 		return 0;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	if (is_forbidden_offset_for_adrp(&plt[i].adrp))
119*4882a593Smuzhiyun 		i = pltsec->plt_num_entries++;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* get the destination register of the ADRP instruction */
122*4882a593Smuzhiyun 	rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD,
123*4882a593Smuzhiyun 					  le32_to_cpup((__le32 *)loc));
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	br = aarch64_insn_gen_branch_imm((u64)&plt[i].br, (u64)loc + 4,
126*4882a593Smuzhiyun 					 AARCH64_INSN_BRANCH_NOLINK);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	plt[i] = __get_adrp_add_pair(val, (u64)&plt[i], rd);
129*4882a593Smuzhiyun 	plt[i].br = cpu_to_le32(br);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	return (u64)&plt[i];
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun #endif
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun #define cmp_3way(a,b)	((a) < (b) ? -1 : (a) > (b))
136*4882a593Smuzhiyun 
cmp_rela(const void * a,const void * b)137*4882a593Smuzhiyun static int cmp_rela(const void *a, const void *b)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	const Elf64_Rela *x = a, *y = b;
140*4882a593Smuzhiyun 	int i;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* sort by type, symbol index and addend */
143*4882a593Smuzhiyun 	i = cmp_3way(ELF64_R_TYPE(x->r_info), ELF64_R_TYPE(y->r_info));
144*4882a593Smuzhiyun 	if (i == 0)
145*4882a593Smuzhiyun 		i = cmp_3way(ELF64_R_SYM(x->r_info), ELF64_R_SYM(y->r_info));
146*4882a593Smuzhiyun 	if (i == 0)
147*4882a593Smuzhiyun 		i = cmp_3way(x->r_addend, y->r_addend);
148*4882a593Smuzhiyun 	return i;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
duplicate_rel(const Elf64_Rela * rela,int num)151*4882a593Smuzhiyun static bool duplicate_rel(const Elf64_Rela *rela, int num)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	/*
154*4882a593Smuzhiyun 	 * Entries are sorted by type, symbol index and addend. That means
155*4882a593Smuzhiyun 	 * that, if a duplicate entry exists, it must be in the preceding
156*4882a593Smuzhiyun 	 * slot.
157*4882a593Smuzhiyun 	 */
158*4882a593Smuzhiyun 	return num > 0 && cmp_rela(rela + num, rela + num - 1) == 0;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
count_plts(Elf64_Sym * syms,Elf64_Rela * rela,int num,Elf64_Word dstidx,Elf_Shdr * dstsec)161*4882a593Smuzhiyun static unsigned int count_plts(Elf64_Sym *syms, Elf64_Rela *rela, int num,
162*4882a593Smuzhiyun 			       Elf64_Word dstidx, Elf_Shdr *dstsec)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	unsigned int ret = 0;
165*4882a593Smuzhiyun 	Elf64_Sym *s;
166*4882a593Smuzhiyun 	int i;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	for (i = 0; i < num; i++) {
169*4882a593Smuzhiyun 		u64 min_align;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		switch (ELF64_R_TYPE(rela[i].r_info)) {
172*4882a593Smuzhiyun 		case R_AARCH64_JUMP26:
173*4882a593Smuzhiyun 		case R_AARCH64_CALL26:
174*4882a593Smuzhiyun 			if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
175*4882a593Smuzhiyun 				break;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 			/*
178*4882a593Smuzhiyun 			 * We only have to consider branch targets that resolve
179*4882a593Smuzhiyun 			 * to symbols that are defined in a different section.
180*4882a593Smuzhiyun 			 * This is not simply a heuristic, it is a fundamental
181*4882a593Smuzhiyun 			 * limitation, since there is no guaranteed way to emit
182*4882a593Smuzhiyun 			 * PLT entries sufficiently close to the branch if the
183*4882a593Smuzhiyun 			 * section size exceeds the range of a branch
184*4882a593Smuzhiyun 			 * instruction. So ignore relocations against defined
185*4882a593Smuzhiyun 			 * symbols if they live in the same section as the
186*4882a593Smuzhiyun 			 * relocation target.
187*4882a593Smuzhiyun 			 */
188*4882a593Smuzhiyun 			s = syms + ELF64_R_SYM(rela[i].r_info);
189*4882a593Smuzhiyun 			if (s->st_shndx == dstidx)
190*4882a593Smuzhiyun 				break;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 			/*
193*4882a593Smuzhiyun 			 * Jump relocations with non-zero addends against
194*4882a593Smuzhiyun 			 * undefined symbols are supported by the ELF spec, but
195*4882a593Smuzhiyun 			 * do not occur in practice (e.g., 'jump n bytes past
196*4882a593Smuzhiyun 			 * the entry point of undefined function symbol f').
197*4882a593Smuzhiyun 			 * So we need to support them, but there is no need to
198*4882a593Smuzhiyun 			 * take them into consideration when trying to optimize
199*4882a593Smuzhiyun 			 * this code. So let's only check for duplicates when
200*4882a593Smuzhiyun 			 * the addend is zero: this allows us to record the PLT
201*4882a593Smuzhiyun 			 * entry address in the symbol table itself, rather than
202*4882a593Smuzhiyun 			 * having to search the list for duplicates each time we
203*4882a593Smuzhiyun 			 * emit one.
204*4882a593Smuzhiyun 			 */
205*4882a593Smuzhiyun 			if (rela[i].r_addend != 0 || !duplicate_rel(rela, i))
206*4882a593Smuzhiyun 				ret++;
207*4882a593Smuzhiyun 			break;
208*4882a593Smuzhiyun 		case R_AARCH64_ADR_PREL_PG_HI21_NC:
209*4882a593Smuzhiyun 		case R_AARCH64_ADR_PREL_PG_HI21:
210*4882a593Smuzhiyun 			if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_843419) ||
211*4882a593Smuzhiyun 			    !cpus_have_const_cap(ARM64_WORKAROUND_843419))
212*4882a593Smuzhiyun 				break;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 			/*
215*4882a593Smuzhiyun 			 * Determine the minimal safe alignment for this ADRP
216*4882a593Smuzhiyun 			 * instruction: the section alignment at which it is
217*4882a593Smuzhiyun 			 * guaranteed not to appear at a vulnerable offset.
218*4882a593Smuzhiyun 			 *
219*4882a593Smuzhiyun 			 * This comes down to finding the least significant zero
220*4882a593Smuzhiyun 			 * bit in bits [11:3] of the section offset, and
221*4882a593Smuzhiyun 			 * increasing the section's alignment so that the
222*4882a593Smuzhiyun 			 * resulting address of this instruction is guaranteed
223*4882a593Smuzhiyun 			 * to equal the offset in that particular bit (as well
224*4882a593Smuzhiyun 			 * as all less signficant bits). This ensures that the
225*4882a593Smuzhiyun 			 * address modulo 4 KB != 0xfff8 or 0xfffc (which would
226*4882a593Smuzhiyun 			 * have all ones in bits [11:3])
227*4882a593Smuzhiyun 			 */
228*4882a593Smuzhiyun 			min_align = 2ULL << ffz(rela[i].r_offset | 0x7);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 			/*
231*4882a593Smuzhiyun 			 * Allocate veneer space for each ADRP that may appear
232*4882a593Smuzhiyun 			 * at a vulnerable offset nonetheless. At relocation
233*4882a593Smuzhiyun 			 * time, some of these will remain unused since some
234*4882a593Smuzhiyun 			 * ADRP instructions can be patched to ADR instructions
235*4882a593Smuzhiyun 			 * instead.
236*4882a593Smuzhiyun 			 */
237*4882a593Smuzhiyun 			if (min_align > SZ_4K)
238*4882a593Smuzhiyun 				ret++;
239*4882a593Smuzhiyun 			else
240*4882a593Smuzhiyun 				dstsec->sh_addralign = max(dstsec->sh_addralign,
241*4882a593Smuzhiyun 							   min_align);
242*4882a593Smuzhiyun 			break;
243*4882a593Smuzhiyun 		}
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_ARM64_ERRATUM_843419) &&
247*4882a593Smuzhiyun 	    cpus_have_const_cap(ARM64_WORKAROUND_843419))
248*4882a593Smuzhiyun 		/*
249*4882a593Smuzhiyun 		 * Add some slack so we can skip PLT slots that may trigger
250*4882a593Smuzhiyun 		 * the erratum due to the placement of the ADRP instruction.
251*4882a593Smuzhiyun 		 */
252*4882a593Smuzhiyun 		ret += DIV_ROUND_UP(ret, (SZ_4K / sizeof(struct plt_entry)));
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	return ret;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
branch_rela_needs_plt(Elf64_Sym * syms,Elf64_Rela * rela,Elf64_Word dstidx)257*4882a593Smuzhiyun static bool branch_rela_needs_plt(Elf64_Sym *syms, Elf64_Rela *rela,
258*4882a593Smuzhiyun 				  Elf64_Word dstidx)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	Elf64_Sym *s = syms + ELF64_R_SYM(rela->r_info);
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	if (s->st_shndx == dstidx)
264*4882a593Smuzhiyun 		return false;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	return ELF64_R_TYPE(rela->r_info) == R_AARCH64_JUMP26 ||
267*4882a593Smuzhiyun 	       ELF64_R_TYPE(rela->r_info) == R_AARCH64_CALL26;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /* Group branch PLT relas at the front end of the array. */
partition_branch_plt_relas(Elf64_Sym * syms,Elf64_Rela * rela,int numrels,Elf64_Word dstidx)271*4882a593Smuzhiyun static int partition_branch_plt_relas(Elf64_Sym *syms, Elf64_Rela *rela,
272*4882a593Smuzhiyun 				      int numrels, Elf64_Word dstidx)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	int i = 0, j = numrels - 1;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
277*4882a593Smuzhiyun 		return 0;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	while (i < j) {
280*4882a593Smuzhiyun 		if (branch_rela_needs_plt(syms, &rela[i], dstidx))
281*4882a593Smuzhiyun 			i++;
282*4882a593Smuzhiyun 		else if (branch_rela_needs_plt(syms, &rela[j], dstidx))
283*4882a593Smuzhiyun 			swap(rela[i], rela[j]);
284*4882a593Smuzhiyun 		else
285*4882a593Smuzhiyun 			j--;
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	return i;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
module_frob_arch_sections(Elf_Ehdr * ehdr,Elf_Shdr * sechdrs,char * secstrings,struct module * mod)291*4882a593Smuzhiyun int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
292*4882a593Smuzhiyun 			      char *secstrings, struct module *mod)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	bool copy_rela_for_fips140 = false;
295*4882a593Smuzhiyun 	unsigned long core_plts = 0;
296*4882a593Smuzhiyun 	unsigned long init_plts = 0;
297*4882a593Smuzhiyun 	Elf64_Sym *syms = NULL;
298*4882a593Smuzhiyun 	Elf_Shdr *pltsec, *tramp = NULL;
299*4882a593Smuzhiyun 	int i;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/*
302*4882a593Smuzhiyun 	 * Find the empty .plt section so we can expand it to store the PLT
303*4882a593Smuzhiyun 	 * entries. Record the symtab address as well.
304*4882a593Smuzhiyun 	 */
305*4882a593Smuzhiyun 	for (i = 0; i < ehdr->e_shnum; i++) {
306*4882a593Smuzhiyun 		if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
307*4882a593Smuzhiyun 			mod->arch.core.plt_shndx = i;
308*4882a593Smuzhiyun 		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".init.plt"))
309*4882a593Smuzhiyun 			mod->arch.init.plt_shndx = i;
310*4882a593Smuzhiyun 		else if (!strcmp(secstrings + sechdrs[i].sh_name,
311*4882a593Smuzhiyun 				 ".text.ftrace_trampoline"))
312*4882a593Smuzhiyun 			tramp = sechdrs + i;
313*4882a593Smuzhiyun 		else if (sechdrs[i].sh_type == SHT_SYMTAB)
314*4882a593Smuzhiyun 			syms = (Elf64_Sym *)sechdrs[i].sh_addr;
315*4882a593Smuzhiyun 	}
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	if (!mod->arch.core.plt_shndx || !mod->arch.init.plt_shndx) {
318*4882a593Smuzhiyun 		pr_err("%s: module PLT section(s) missing\n", mod->name);
319*4882a593Smuzhiyun 		return -ENOEXEC;
320*4882a593Smuzhiyun 	}
321*4882a593Smuzhiyun 	if (!syms) {
322*4882a593Smuzhiyun 		pr_err("%s: module symtab section missing\n", mod->name);
323*4882a593Smuzhiyun 		return -ENOEXEC;
324*4882a593Smuzhiyun 	}
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_CRYPTO_FIPS140) &&
327*4882a593Smuzhiyun 	    !strcmp(mod->name, "fips140"))
328*4882a593Smuzhiyun 		copy_rela_for_fips140 = true;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	for (i = 0; i < ehdr->e_shnum; i++) {
331*4882a593Smuzhiyun 		Elf64_Rela *rels = (void *)ehdr + sechdrs[i].sh_offset;
332*4882a593Smuzhiyun 		int nents, numrels = sechdrs[i].sh_size / sizeof(Elf64_Rela);
333*4882a593Smuzhiyun 		Elf64_Shdr *dstsec = sechdrs + sechdrs[i].sh_info;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 		if (sechdrs[i].sh_type != SHT_RELA)
336*4882a593Smuzhiyun 			continue;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_FIPS140
339*4882a593Smuzhiyun 		if (copy_rela_for_fips140 &&
340*4882a593Smuzhiyun 		    !strcmp(secstrings + dstsec->sh_name, ".rodata")) {
341*4882a593Smuzhiyun 			void *p = kmemdup(rels, numrels * sizeof(Elf64_Rela),
342*4882a593Smuzhiyun 					  GFP_KERNEL);
343*4882a593Smuzhiyun 			if (!p) {
344*4882a593Smuzhiyun 				pr_err("fips140: failed to allocate .rodata RELA buffer\n");
345*4882a593Smuzhiyun 				return -ENOMEM;
346*4882a593Smuzhiyun 			}
347*4882a593Smuzhiyun 			mod->arch.rodata_relocations = p;
348*4882a593Smuzhiyun 			mod->arch.num_rodata_relocations = numrels;
349*4882a593Smuzhiyun 		}
350*4882a593Smuzhiyun #endif
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 		/* ignore relocations that operate on non-exec sections */
353*4882a593Smuzhiyun 		if (!(dstsec->sh_flags & SHF_EXECINSTR))
354*4882a593Smuzhiyun 			continue;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_FIPS140
357*4882a593Smuzhiyun 		if (copy_rela_for_fips140 &&
358*4882a593Smuzhiyun 		    !strcmp(secstrings + dstsec->sh_name, ".text")) {
359*4882a593Smuzhiyun 			void *p = kmemdup(rels, numrels * sizeof(Elf64_Rela),
360*4882a593Smuzhiyun 					  GFP_KERNEL);
361*4882a593Smuzhiyun 			if (!p) {
362*4882a593Smuzhiyun 				pr_err("fips140: failed to allocate .text RELA buffer\n");
363*4882a593Smuzhiyun 				return -ENOMEM;
364*4882a593Smuzhiyun 			}
365*4882a593Smuzhiyun 			mod->arch.text_relocations = p;
366*4882a593Smuzhiyun 			mod->arch.num_text_relocations = numrels;
367*4882a593Smuzhiyun 		}
368*4882a593Smuzhiyun #endif
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 		/*
371*4882a593Smuzhiyun 		 * sort branch relocations requiring a PLT by type, symbol index
372*4882a593Smuzhiyun 		 * and addend
373*4882a593Smuzhiyun 		 */
374*4882a593Smuzhiyun 		nents = partition_branch_plt_relas(syms, rels, numrels,
375*4882a593Smuzhiyun 						   sechdrs[i].sh_info);
376*4882a593Smuzhiyun 		if (nents)
377*4882a593Smuzhiyun 			sort(rels, nents, sizeof(Elf64_Rela), cmp_rela, NULL);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		if (!str_has_prefix(secstrings + dstsec->sh_name, ".init"))
380*4882a593Smuzhiyun 			core_plts += count_plts(syms, rels, numrels,
381*4882a593Smuzhiyun 						sechdrs[i].sh_info, dstsec);
382*4882a593Smuzhiyun 		else
383*4882a593Smuzhiyun 			init_plts += count_plts(syms, rels, numrels,
384*4882a593Smuzhiyun 						sechdrs[i].sh_info, dstsec);
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	pltsec = sechdrs + mod->arch.core.plt_shndx;
388*4882a593Smuzhiyun 	pltsec->sh_type = SHT_NOBITS;
389*4882a593Smuzhiyun 	pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
390*4882a593Smuzhiyun 	pltsec->sh_addralign = L1_CACHE_BYTES;
391*4882a593Smuzhiyun 	pltsec->sh_size = (core_plts  + 1) * sizeof(struct plt_entry);
392*4882a593Smuzhiyun 	mod->arch.core.plt_num_entries = 0;
393*4882a593Smuzhiyun 	mod->arch.core.plt_max_entries = core_plts;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	pltsec = sechdrs + mod->arch.init.plt_shndx;
396*4882a593Smuzhiyun 	pltsec->sh_type = SHT_NOBITS;
397*4882a593Smuzhiyun 	pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
398*4882a593Smuzhiyun 	pltsec->sh_addralign = L1_CACHE_BYTES;
399*4882a593Smuzhiyun 	pltsec->sh_size = (init_plts + 1) * sizeof(struct plt_entry);
400*4882a593Smuzhiyun 	mod->arch.init.plt_num_entries = 0;
401*4882a593Smuzhiyun 	mod->arch.init.plt_max_entries = init_plts;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	if (tramp) {
404*4882a593Smuzhiyun 		tramp->sh_type = SHT_NOBITS;
405*4882a593Smuzhiyun 		tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
406*4882a593Smuzhiyun 		tramp->sh_addralign = __alignof__(struct plt_entry);
407*4882a593Smuzhiyun 		tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry);
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	return 0;
411*4882a593Smuzhiyun }
412