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 size_t scratch_size = 0;
123 int i;
124
125 /*
126 * Find the empty .got and .plt sections.
127 */
128 for (i = 0; i < ehdr->e_shnum; i++) {
129 if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
130 mod->arch.plt.shdr = sechdrs + i;
131 else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
132 mod->arch.got.shdr = sechdrs + i;
133 else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got.plt"))
134 mod->arch.got_plt.shdr = sechdrs + i;
135 }
136
137 if (!mod->arch.plt.shdr) {
138 pr_err("%s: module PLT section(s) missing\n", mod->name);
139 return -ENOEXEC;
140 }
141 if (!mod->arch.got.shdr) {
142 pr_err("%s: module GOT section(s) missing\n", mod->name);
143 return -ENOEXEC;
144 }
145 if (!mod->arch.got_plt.shdr) {
146 pr_err("%s: module GOT.PLT section(s) missing\n", mod->name);
147 return -ENOEXEC;
148 }
149
150 /* Calculate the maxinum number of entries */
151 for (i = 0; i < ehdr->e_shnum; i++) {
152 size_t num_relas = sechdrs[i].sh_size / sizeof(Elf_Rela);
153 Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
154 Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
155 size_t scratch_size_needed;
156
157 if (sechdrs[i].sh_type != SHT_RELA)
158 continue;
159
160 /* ignore relocations that operate on non-exec sections */
161 if (!(dst_sec->sh_flags & SHF_EXECINSTR))
162 continue;
163
164 /*
165 * apply_relocate_add() relies on HI20 and LO12 relocation pairs being
166 * close together, so sort a copy of the section to avoid interfering.
167 */
168 scratch_size_needed = (num_scratch_relas + num_relas) * sizeof(*scratch);
169 if (scratch_size_needed > scratch_size) {
170 scratch_size = scratch_size_needed;
171 scratch = kvrealloc(scratch, scratch_size, GFP_KERNEL);
172 if (!scratch)
173 return -ENOMEM;
174 }
175
176 for (size_t j = 0; j < num_relas; j++)
177 if (rela_needs_plt_got_entry(&relas[j]))
178 scratch[num_scratch_relas++] = relas[j];
179 }
180
181 if (scratch) {
182 /* sort the accumulated PLT/GOT relocations so duplicates are adjacent */
183 sort(scratch, num_scratch_relas, sizeof(*scratch), cmp_rela, NULL);
184 count_max_entries(scratch, num_scratch_relas, &num_plts, &num_gots);
185 kvfree(scratch);
186 }
187
188 mod->arch.plt.shdr->sh_type = SHT_NOBITS;
189 mod->arch.plt.shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
190 mod->arch.plt.shdr->sh_addralign = L1_CACHE_BYTES;
191 mod->arch.plt.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_entry);
192 mod->arch.plt.num_entries = 0;
193 mod->arch.plt.max_entries = num_plts;
194
195 mod->arch.got.shdr->sh_type = SHT_NOBITS;
196 mod->arch.got.shdr->sh_flags = SHF_ALLOC;
197 mod->arch.got.shdr->sh_addralign = L1_CACHE_BYTES;
198 mod->arch.got.shdr->sh_size = (num_gots + 1) * sizeof(struct got_entry);
199 mod->arch.got.num_entries = 0;
200 mod->arch.got.max_entries = num_gots;
201
202 mod->arch.got_plt.shdr->sh_type = SHT_NOBITS;
203 mod->arch.got_plt.shdr->sh_flags = SHF_ALLOC;
204 mod->arch.got_plt.shdr->sh_addralign = L1_CACHE_BYTES;
205 mod->arch.got_plt.shdr->sh_size = (num_plts + 1) * sizeof(struct got_entry);
206 mod->arch.got_plt.num_entries = 0;
207 mod->arch.got_plt.max_entries = num_plts;
208 return 0;
209 }
210