xref: /linux/arch/riscv/kernel/module-sections.c (revision 225a97d6d45456a7627633da09cb842a43ef1b85)
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 
module_emit_got_entry(struct module * mod,unsigned long val)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 
module_emit_plt_entry(struct module * mod,unsigned long val)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 #define cmp_3way(a, b)	((a) < (b) ? -1 : (a) > (b))
60 
cmp_rela(const void * a,const void * b)61 static int cmp_rela(const void *a, const void *b)
62 {
63 	const Elf_Rela *x = a, *y = b;
64 	int i;
65 
66 	/* sort by type, symbol index and addend */
67 	i = cmp_3way(x->r_info, y->r_info);
68 	if (i == 0)
69 		i = cmp_3way(x->r_addend, y->r_addend);
70 	return i;
71 }
72 
duplicate_rela(const Elf_Rela * rela,int idx)73 static bool duplicate_rela(const Elf_Rela *rela, int idx)
74 {
75 	/*
76 	 * Entries are sorted by type, symbol index and addend. That means
77 	 * that, if a duplicate entry exists, it must be in the preceding slot.
78 	 */
79 	return idx > 0 && cmp_rela(rela + idx, rela + idx - 1) == 0;
80 }
81 
count_max_entries(const Elf_Rela * relas,size_t num,unsigned int * plts,unsigned int * gots)82 static void count_max_entries(const Elf_Rela *relas, size_t num,
83 			      unsigned int *plts, unsigned int *gots)
84 {
85 	for (size_t i = 0; i < num; i++) {
86 		if (duplicate_rela(relas, i))
87 			continue;
88 
89 		switch (ELF_R_TYPE(relas[i].r_info)) {
90 		case R_RISCV_CALL_PLT:
91 		case R_RISCV_PLT32:
92 			(*plts)++;
93 			break;
94 		case R_RISCV_GOT_HI20:
95 			(*gots)++;
96 			break;
97 		default:
98 			unreachable();
99 		}
100 	}
101 }
102 
rela_needs_plt_got_entry(const Elf_Rela * rela)103 static bool rela_needs_plt_got_entry(const Elf_Rela *rela)
104 {
105 	switch (ELF_R_TYPE(rela->r_info)) {
106 	case R_RISCV_CALL_PLT:
107 	case R_RISCV_GOT_HI20:
108 	case R_RISCV_PLT32:
109 		return true;
110 	default:
111 		return false;
112 	}
113 }
114 
module_frob_arch_sections(Elf_Ehdr * ehdr,Elf_Shdr * sechdrs,char * secstrings,struct module * mod)115 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
116 			      char *secstrings, struct module *mod)
117 {
118 	size_t num_scratch_relas = 0;
119 	unsigned int num_plts = 0;
120 	unsigned int num_gots = 0;
121 	Elf_Rela *scratch = NULL;
122 	Elf_Rela *new_scratch;
123 	size_t scratch_size = 0;
124 	int i;
125 
126 	/*
127 	 * Find the empty .got and .plt sections.
128 	 */
129 	for (i = 0; i < ehdr->e_shnum; i++) {
130 		if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
131 			mod->arch.plt.shdr = sechdrs + i;
132 		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
133 			mod->arch.got.shdr = sechdrs + i;
134 		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got.plt"))
135 			mod->arch.got_plt.shdr = sechdrs + i;
136 	}
137 
138 	if (!mod->arch.plt.shdr) {
139 		pr_err("%s: module PLT section(s) missing\n", mod->name);
140 		return -ENOEXEC;
141 	}
142 	if (!mod->arch.got.shdr) {
143 		pr_err("%s: module GOT section(s) missing\n", mod->name);
144 		return -ENOEXEC;
145 	}
146 	if (!mod->arch.got_plt.shdr) {
147 		pr_err("%s: module GOT.PLT section(s) missing\n", mod->name);
148 		return -ENOEXEC;
149 	}
150 
151 	/* Calculate the maxinum number of entries */
152 	for (i = 0; i < ehdr->e_shnum; i++) {
153 		size_t num_relas = sechdrs[i].sh_size / sizeof(Elf_Rela);
154 		Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
155 		Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
156 		size_t scratch_size_needed;
157 
158 		if (sechdrs[i].sh_type != SHT_RELA)
159 			continue;
160 
161 		/* ignore relocations that operate on non-exec sections */
162 		if (!(dst_sec->sh_flags & SHF_EXECINSTR))
163 			continue;
164 
165 		/*
166 		 * apply_relocate_add() relies on HI20 and LO12 relocation pairs being
167 		 * close together, so sort a copy of the section to avoid interfering.
168 		 */
169 		scratch_size_needed = (num_scratch_relas + num_relas) * sizeof(*scratch);
170 		if (scratch_size_needed > scratch_size) {
171 			scratch_size = scratch_size_needed;
172 			new_scratch = kvrealloc(scratch, scratch_size, GFP_KERNEL);
173 			if (!new_scratch) {
174 				kvfree(scratch);
175 				return -ENOMEM;
176 			}
177 			scratch = new_scratch;
178 		}
179 
180 		for (size_t j = 0; j < num_relas; j++)
181 			if (rela_needs_plt_got_entry(&relas[j]))
182 				scratch[num_scratch_relas++] = relas[j];
183 	}
184 
185 	if (scratch) {
186 		/* sort the accumulated PLT/GOT relocations so duplicates are adjacent */
187 		sort(scratch, num_scratch_relas, sizeof(*scratch), cmp_rela, NULL);
188 		count_max_entries(scratch, num_scratch_relas, &num_plts, &num_gots);
189 		kvfree(scratch);
190 	}
191 
192 	mod->arch.plt.shdr->sh_type = SHT_NOBITS;
193 	mod->arch.plt.shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
194 	mod->arch.plt.shdr->sh_addralign = L1_CACHE_BYTES;
195 	mod->arch.plt.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_entry);
196 	mod->arch.plt.num_entries = 0;
197 	mod->arch.plt.max_entries = num_plts;
198 
199 	mod->arch.got.shdr->sh_type = SHT_NOBITS;
200 	mod->arch.got.shdr->sh_flags = SHF_ALLOC;
201 	mod->arch.got.shdr->sh_addralign = L1_CACHE_BYTES;
202 	mod->arch.got.shdr->sh_size = (num_gots + 1) * sizeof(struct got_entry);
203 	mod->arch.got.num_entries = 0;
204 	mod->arch.got.max_entries = num_gots;
205 
206 	mod->arch.got_plt.shdr->sh_type = SHT_NOBITS;
207 	mod->arch.got_plt.shdr->sh_flags = SHF_ALLOC;
208 	mod->arch.got_plt.shdr->sh_addralign = L1_CACHE_BYTES;
209 	mod->arch.got_plt.shdr->sh_size = (num_plts + 1) * sizeof(struct got_entry);
210 	mod->arch.got_plt.num_entries = 0;
211 	mod->arch.got_plt.max_entries = num_plts;
212 	return 0;
213 }
214