xref: /linux/arch/loongarch/kernel/module-sections.c (revision 23c48a124b469cee2eb0c75e6d22d366d1caa118)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 
6 #include <linux/elf.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 
10 Elf_Addr module_emit_plt_entry(struct module *mod, unsigned long val)
11 {
12 	int nr;
13 	struct mod_section *plt_sec = &mod->arch.plt;
14 	struct mod_section *plt_idx_sec = &mod->arch.plt_idx;
15 	struct plt_entry *plt = get_plt_entry(val, plt_sec, plt_idx_sec);
16 	struct plt_idx_entry *plt_idx;
17 
18 	if (plt)
19 		return (Elf_Addr)plt;
20 
21 	nr = plt_sec->num_entries;
22 
23 	/* There is no duplicate entry, create a new one */
24 	plt = (struct plt_entry *)plt_sec->shdr->sh_addr;
25 	plt[nr] = emit_plt_entry(val);
26 	plt_idx = (struct plt_idx_entry *)plt_idx_sec->shdr->sh_addr;
27 	plt_idx[nr] = emit_plt_idx_entry(val);
28 
29 	plt_sec->num_entries++;
30 	plt_idx_sec->num_entries++;
31 	BUG_ON(plt_sec->num_entries > plt_sec->max_entries);
32 
33 	return (Elf_Addr)&plt[nr];
34 }
35 
36 static int is_rela_equal(const Elf_Rela *x, const Elf_Rela *y)
37 {
38 	return x->r_info == y->r_info && x->r_addend == y->r_addend;
39 }
40 
41 static bool duplicate_rela(const Elf_Rela *rela, int idx)
42 {
43 	int i;
44 
45 	for (i = 0; i < idx; i++) {
46 		if (is_rela_equal(&rela[i], &rela[idx]))
47 			return true;
48 	}
49 
50 	return false;
51 }
52 
53 static void count_max_entries(Elf_Rela *relas, int num, unsigned int *plts)
54 {
55 	unsigned int i, type;
56 
57 	for (i = 0; i < num; i++) {
58 		type = ELF_R_TYPE(relas[i].r_info);
59 		if (type == R_LARCH_SOP_PUSH_PLT_PCREL) {
60 			if (!duplicate_rela(relas, i))
61 				(*plts)++;
62 		}
63 	}
64 }
65 
66 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
67 			      char *secstrings, struct module *mod)
68 {
69 	unsigned int i, num_plts = 0;
70 
71 	/*
72 	 * Find the empty .plt sections.
73 	 */
74 	for (i = 0; i < ehdr->e_shnum; i++) {
75 		if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
76 			mod->arch.plt.shdr = sechdrs + i;
77 		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt.idx"))
78 			mod->arch.plt_idx.shdr = sechdrs + i;
79 	}
80 
81 	if (!mod->arch.plt.shdr) {
82 		pr_err("%s: module PLT section(s) missing\n", mod->name);
83 		return -ENOEXEC;
84 	}
85 	if (!mod->arch.plt_idx.shdr) {
86 		pr_err("%s: module PLT.IDX section(s) missing\n", mod->name);
87 		return -ENOEXEC;
88 	}
89 
90 	/* Calculate the maxinum number of entries */
91 	for (i = 0; i < ehdr->e_shnum; i++) {
92 		int num_rela = sechdrs[i].sh_size / sizeof(Elf_Rela);
93 		Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
94 		Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
95 
96 		if (sechdrs[i].sh_type != SHT_RELA)
97 			continue;
98 
99 		/* ignore relocations that operate on non-exec sections */
100 		if (!(dst_sec->sh_flags & SHF_EXECINSTR))
101 			continue;
102 
103 		count_max_entries(relas, num_rela, &num_plts);
104 	}
105 
106 	mod->arch.plt.shdr->sh_type = SHT_NOBITS;
107 	mod->arch.plt.shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
108 	mod->arch.plt.shdr->sh_addralign = L1_CACHE_BYTES;
109 	mod->arch.plt.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_entry);
110 	mod->arch.plt.num_entries = 0;
111 	mod->arch.plt.max_entries = num_plts;
112 
113 	mod->arch.plt_idx.shdr->sh_type = SHT_NOBITS;
114 	mod->arch.plt_idx.shdr->sh_flags = SHF_ALLOC;
115 	mod->arch.plt_idx.shdr->sh_addralign = L1_CACHE_BYTES;
116 	mod->arch.plt_idx.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_idx_entry);
117 	mod->arch.plt_idx.num_entries = 0;
118 	mod->arch.plt_idx.max_entries = num_plts;
119 
120 	return 0;
121 }
122