xref: /linux/arch/riscv/kernel/module-sections.c (revision d639d9fa162aadec1ae9980c4dcf6e50bd2f8290)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * Copyright (C) 2014-2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
4  *
5  * Copyright (C) 2018 Andes Technology Corporation <zong@andestech.com>
6  */
7 
8 #include <linux/elf.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/moduleloader.h>
12 #include <linux/sort.h>
13 
14 unsigned long module_emit_got_entry(struct module *mod, unsigned long val)
15 {
16 	struct mod_section *got_sec = &mod->arch.got;
17 	int i = got_sec->num_entries;
18 	struct got_entry *got = get_got_entry(val, got_sec);
19 
20 	if (got)
21 		return (unsigned long)got;
22 
23 	/* There is no duplicate entry, create a new one */
24 	got = (struct got_entry *)got_sec->shdr->sh_addr;
25 	got[i] = emit_got_entry(val);
26 
27 	got_sec->num_entries++;
28 	BUG_ON(got_sec->num_entries > got_sec->max_entries);
29 
30 	return (unsigned long)&got[i];
31 }
32 
33 unsigned long module_emit_plt_entry(struct module *mod, unsigned long val)
34 {
35 	struct mod_section *got_plt_sec = &mod->arch.got_plt;
36 	struct got_entry *got_plt;
37 	struct mod_section *plt_sec = &mod->arch.plt;
38 	struct plt_entry *plt = get_plt_entry(val, plt_sec, got_plt_sec);
39 	int i = plt_sec->num_entries;
40 
41 	if (plt)
42 		return (unsigned long)plt;
43 
44 	/* There is no duplicate entry, create a new one */
45 	got_plt = (struct got_entry *)got_plt_sec->shdr->sh_addr;
46 	got_plt[i] = emit_got_entry(val);
47 	plt = (struct plt_entry *)plt_sec->shdr->sh_addr;
48 	plt[i] = emit_plt_entry(val,
49 				(unsigned long)&plt[i],
50 				(unsigned long)&got_plt[i]);
51 
52 	plt_sec->num_entries++;
53 	got_plt_sec->num_entries++;
54 	BUG_ON(plt_sec->num_entries > plt_sec->max_entries);
55 
56 	return (unsigned long)&plt[i];
57 }
58 
59 static int cmp_rela(const void *a, const void *b)
60 {
61 	const Elf_Rela *x = a, *y = b;
62 	int i;
63 
64 	/* sort by type, symbol index and addend */
65 	i = cmp_int(x->r_info, y->r_info);
66 	if (i == 0)
67 		i = cmp_int(x->r_addend, y->r_addend);
68 	return i;
69 }
70 
71 static bool duplicate_rela(const Elf_Rela *rela, int idx)
72 {
73 	/*
74 	 * Entries are sorted by type, symbol index and addend. That means
75 	 * that, if a duplicate entry exists, it must be in the preceding slot.
76 	 */
77 	return idx > 0 && cmp_rela(rela + idx, rela + idx - 1) == 0;
78 }
79 
80 static void count_max_entries(const Elf_Rela *relas, size_t num,
81 			      unsigned int *plts, unsigned int *gots)
82 {
83 	for (size_t i = 0; i < num; i++) {
84 		if (duplicate_rela(relas, i))
85 			continue;
86 
87 		switch (ELF_R_TYPE(relas[i].r_info)) {
88 		case R_RISCV_CALL_PLT:
89 		case R_RISCV_PLT32:
90 			(*plts)++;
91 			break;
92 		case R_RISCV_GOT_HI20:
93 			(*gots)++;
94 			break;
95 		default:
96 			unreachable();
97 		}
98 	}
99 }
100 
101 static bool rela_needs_plt_got_entry(const Elf_Rela *rela)
102 {
103 	switch (ELF_R_TYPE(rela->r_info)) {
104 	case R_RISCV_CALL_PLT:
105 	case R_RISCV_GOT_HI20:
106 	case R_RISCV_PLT32:
107 		return true;
108 	default:
109 		return false;
110 	}
111 }
112 
113 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
114 			      char *secstrings, struct module *mod)
115 {
116 	size_t num_scratch_relas = 0;
117 	unsigned int num_plts = 0;
118 	unsigned int num_gots = 0;
119 	Elf_Rela *scratch = NULL;
120 	Elf_Rela *new_scratch;
121 	size_t scratch_size = 0;
122 	int i;
123 
124 	/*
125 	 * Find the empty .got and .plt sections.
126 	 */
127 	for (i = 0; i < ehdr->e_shnum; i++) {
128 		if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
129 			mod->arch.plt.shdr = sechdrs + i;
130 		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
131 			mod->arch.got.shdr = sechdrs + i;
132 		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got.plt"))
133 			mod->arch.got_plt.shdr = sechdrs + i;
134 	}
135 
136 	if (!mod->arch.plt.shdr) {
137 		pr_err("%s: module PLT section(s) missing\n", mod->name);
138 		return -ENOEXEC;
139 	}
140 	if (!mod->arch.got.shdr) {
141 		pr_err("%s: module GOT section(s) missing\n", mod->name);
142 		return -ENOEXEC;
143 	}
144 	if (!mod->arch.got_plt.shdr) {
145 		pr_err("%s: module GOT.PLT section(s) missing\n", mod->name);
146 		return -ENOEXEC;
147 	}
148 
149 	/* Calculate the maximum number of entries */
150 	for (i = 0; i < ehdr->e_shnum; i++) {
151 		size_t num_relas = sechdrs[i].sh_size / sizeof(Elf_Rela);
152 		Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
153 		Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
154 		size_t scratch_size_needed;
155 
156 		if (sechdrs[i].sh_type != SHT_RELA)
157 			continue;
158 
159 		/* ignore relocations that operate on non-exec sections */
160 		if (!(dst_sec->sh_flags & SHF_EXECINSTR))
161 			continue;
162 
163 		/*
164 		 * apply_relocate_add() relies on HI20 and LO12 relocation pairs being
165 		 * close together, so sort a copy of the section to avoid interfering.
166 		 */
167 		scratch_size_needed = (num_scratch_relas + num_relas) * sizeof(*scratch);
168 		if (scratch_size_needed > scratch_size) {
169 			scratch_size = scratch_size_needed;
170 			new_scratch = kvrealloc(scratch, scratch_size, GFP_KERNEL);
171 			if (!new_scratch) {
172 				kvfree(scratch);
173 				return -ENOMEM;
174 			}
175 			scratch = new_scratch;
176 		}
177 
178 		for (size_t j = 0; j < num_relas; j++)
179 			if (rela_needs_plt_got_entry(&relas[j]))
180 				scratch[num_scratch_relas++] = relas[j];
181 	}
182 
183 	if (scratch) {
184 		/* sort the accumulated PLT/GOT relocations so duplicates are adjacent */
185 		sort(scratch, num_scratch_relas, sizeof(*scratch), cmp_rela, NULL);
186 		count_max_entries(scratch, num_scratch_relas, &num_plts, &num_gots);
187 		kvfree(scratch);
188 	}
189 
190 	mod->arch.plt.shdr->sh_type = SHT_NOBITS;
191 	mod->arch.plt.shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
192 	mod->arch.plt.shdr->sh_addralign = L1_CACHE_BYTES;
193 	mod->arch.plt.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_entry);
194 	mod->arch.plt.num_entries = 0;
195 	mod->arch.plt.max_entries = num_plts;
196 
197 	mod->arch.got.shdr->sh_type = SHT_NOBITS;
198 	mod->arch.got.shdr->sh_flags = SHF_ALLOC;
199 	mod->arch.got.shdr->sh_addralign = L1_CACHE_BYTES;
200 	mod->arch.got.shdr->sh_size = (num_gots + 1) * sizeof(struct got_entry);
201 	mod->arch.got.num_entries = 0;
202 	mod->arch.got.max_entries = num_gots;
203 
204 	mod->arch.got_plt.shdr->sh_type = SHT_NOBITS;
205 	mod->arch.got_plt.shdr->sh_flags = SHF_ALLOC;
206 	mod->arch.got_plt.shdr->sh_addralign = L1_CACHE_BYTES;
207 	mod->arch.got_plt.shdr->sh_size = (num_plts + 1) * sizeof(struct got_entry);
208 	mod->arch.got_plt.num_entries = 0;
209 	mod->arch.got_plt.max_entries = num_plts;
210 	return 0;
211 }
212