GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / arm / kernel / module-plts.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014-2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
4  */
5
6 #include <linux/elf.h>
7 #include <linux/ftrace.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/sort.h>
11
12 #include <asm/cache.h>
13 #include <asm/opcodes.h>
14
15 #ifdef CONFIG_THUMB2_KERNEL
16 #define PLT_ENT_LDR             __opcode_to_mem_thumb32(0xf8dff000 | \
17                                                         (PLT_ENT_STRIDE - 4))
18 #else
19 #define PLT_ENT_LDR             __opcode_to_mem_arm(0xe59ff000 | \
20                                                     (PLT_ENT_STRIDE - 8))
21 #endif
22
23 static const u32 fixed_plts[] = {
24 #ifdef CONFIG_DYNAMIC_FTRACE
25         FTRACE_ADDR,
26         MCOUNT_ADDR,
27 #endif
28 };
29
30 static bool in_init(const struct module *mod, unsigned long loc)
31 {
32         return loc - (u32)mod->init_layout.base < mod->init_layout.size;
33 }
34
35 static void prealloc_fixed(struct mod_plt_sec *pltsec, struct plt_entries *plt)
36 {
37         int i;
38
39         if (!ARRAY_SIZE(fixed_plts) || pltsec->plt_count)
40                 return;
41         pltsec->plt_count = ARRAY_SIZE(fixed_plts);
42
43         for (i = 0; i < ARRAY_SIZE(plt->ldr); ++i)
44                 plt->ldr[i] = PLT_ENT_LDR;
45
46         BUILD_BUG_ON(sizeof(fixed_plts) > sizeof(plt->lit));
47         memcpy(plt->lit, fixed_plts, sizeof(fixed_plts));
48 }
49
50 u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val)
51 {
52         struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
53                                                           &mod->arch.init;
54         struct plt_entries *plt;
55         int idx;
56
57         /* cache the address, ELF header is available only during module load */
58         if (!pltsec->plt_ent)
59                 pltsec->plt_ent = (struct plt_entries *)pltsec->plt->sh_addr;
60         plt = pltsec->plt_ent;
61
62         prealloc_fixed(pltsec, plt);
63
64         for (idx = 0; idx < ARRAY_SIZE(fixed_plts); ++idx)
65                 if (plt->lit[idx] == val)
66                         return (u32)&plt->ldr[idx];
67
68         idx = 0;
69         /*
70          * Look for an existing entry pointing to 'val'. Given that the
71          * relocations are sorted, this will be the last entry we allocated.
72          * (if one exists).
73          */
74         if (pltsec->plt_count > 0) {
75                 plt += (pltsec->plt_count - 1) / PLT_ENT_COUNT;
76                 idx = (pltsec->plt_count - 1) % PLT_ENT_COUNT;
77
78                 if (plt->lit[idx] == val)
79                         return (u32)&plt->ldr[idx];
80
81                 idx = (idx + 1) % PLT_ENT_COUNT;
82                 if (!idx)
83                         plt++;
84         }
85
86         pltsec->plt_count++;
87         BUG_ON(pltsec->plt_count * PLT_ENT_SIZE > pltsec->plt->sh_size);
88
89         if (!idx)
90                 /* Populate a new set of entries */
91                 *plt = (struct plt_entries){
92                         { [0 ... PLT_ENT_COUNT - 1] = PLT_ENT_LDR, },
93                         { val, }
94                 };
95         else
96                 plt->lit[idx] = val;
97
98         return (u32)&plt->ldr[idx];
99 }
100
101 #define cmp_3way(a,b)   ((a) < (b) ? -1 : (a) > (b))
102
103 static int cmp_rel(const void *a, const void *b)
104 {
105         const Elf32_Rel *x = a, *y = b;
106         int i;
107
108         /* sort by type and symbol index */
109         i = cmp_3way(ELF32_R_TYPE(x->r_info), ELF32_R_TYPE(y->r_info));
110         if (i == 0)
111                 i = cmp_3way(ELF32_R_SYM(x->r_info), ELF32_R_SYM(y->r_info));
112         return i;
113 }
114
115 static bool is_zero_addend_relocation(Elf32_Addr base, const Elf32_Rel *rel)
116 {
117         u32 *tval = (u32 *)(base + rel->r_offset);
118
119         /*
120          * Do a bitwise compare on the raw addend rather than fully decoding
121          * the offset and doing an arithmetic comparison.
122          * Note that a zero-addend jump/call relocation is encoded taking the
123          * PC bias into account, i.e., -8 for ARM and -4 for Thumb2.
124          */
125         switch (ELF32_R_TYPE(rel->r_info)) {
126                 u16 upper, lower;
127
128         case R_ARM_THM_CALL:
129         case R_ARM_THM_JUMP24:
130                 upper = __mem_to_opcode_thumb16(((u16 *)tval)[0]);
131                 lower = __mem_to_opcode_thumb16(((u16 *)tval)[1]);
132
133                 return (upper & 0x7ff) == 0x7ff && (lower & 0x2fff) == 0x2ffe;
134
135         case R_ARM_CALL:
136         case R_ARM_PC24:
137         case R_ARM_JUMP24:
138                 return (__mem_to_opcode_arm(*tval) & 0xffffff) == 0xfffffe;
139         }
140         BUG();
141 }
142
143 static bool duplicate_rel(Elf32_Addr base, const Elf32_Rel *rel, int num)
144 {
145         const Elf32_Rel *prev;
146
147         /*
148          * Entries are sorted by type and symbol index. That means that,
149          * if a duplicate entry exists, it must be in the preceding
150          * slot.
151          */
152         if (!num)
153                 return false;
154
155         prev = rel + num - 1;
156         return cmp_rel(rel + num, prev) == 0 &&
157                is_zero_addend_relocation(base, prev);
158 }
159
160 /* Count how many PLT entries we may need */
161 static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base,
162                                const Elf32_Rel *rel, int num, Elf32_Word dstidx)
163 {
164         unsigned int ret = 0;
165         const Elf32_Sym *s;
166         int i;
167
168         for (i = 0; i < num; i++) {
169                 switch (ELF32_R_TYPE(rel[i].r_info)) {
170                 case R_ARM_CALL:
171                 case R_ARM_PC24:
172                 case R_ARM_JUMP24:
173                 case R_ARM_THM_CALL:
174                 case R_ARM_THM_JUMP24:
175                         /*
176                          * We only have to consider branch targets that resolve
177                          * to symbols that are defined in a different section.
178                          * This is not simply a heuristic, it is a fundamental
179                          * limitation, since there is no guaranteed way to emit
180                          * PLT entries sufficiently close to the branch if the
181                          * section size exceeds the range of a branch
182                          * instruction. So ignore relocations against defined
183                          * symbols if they live in the same section as the
184                          * relocation target.
185                          */
186                         s = syms + ELF32_R_SYM(rel[i].r_info);
187                         if (s->st_shndx == dstidx)
188                                 break;
189
190                         /*
191                          * Jump relocations with non-zero addends against
192                          * undefined symbols are supported by the ELF spec, but
193                          * do not occur in practice (e.g., 'jump n bytes past
194                          * the entry point of undefined function symbol f').
195                          * So we need to support them, but there is no need to
196                          * take them into consideration when trying to optimize
197                          * this code. So let's only check for duplicates when
198                          * the addend is zero. (Note that calls into the core
199                          * module via init PLT entries could involve section
200                          * relative symbol references with non-zero addends, for
201                          * which we may end up emitting duplicates, but the init
202                          * PLT is released along with the rest of the .init
203                          * region as soon as module loading completes.)
204                          */
205                         if (!is_zero_addend_relocation(base, rel + i) ||
206                             !duplicate_rel(base, rel, i))
207                                 ret++;
208                 }
209         }
210         return ret;
211 }
212
213 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
214                               char *secstrings, struct module *mod)
215 {
216         unsigned long core_plts = ARRAY_SIZE(fixed_plts);
217         unsigned long init_plts = ARRAY_SIZE(fixed_plts);
218         Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
219         Elf32_Sym *syms = NULL;
220
221         /*
222          * To store the PLTs, we expand the .text section for core module code
223          * and for initialization code.
224          */
225         for (s = sechdrs; s < sechdrs_end; ++s) {
226                 if (strcmp(".plt", secstrings + s->sh_name) == 0)
227                         mod->arch.core.plt = s;
228                 else if (strcmp(".init.plt", secstrings + s->sh_name) == 0)
229                         mod->arch.init.plt = s;
230                 else if (s->sh_type == SHT_SYMTAB)
231                         syms = (Elf32_Sym *)s->sh_addr;
232         }
233
234         if (!mod->arch.core.plt || !mod->arch.init.plt) {
235                 pr_err("%s: module PLT section(s) missing\n", mod->name);
236                 return -ENOEXEC;
237         }
238         if (!syms) {
239                 pr_err("%s: module symtab section missing\n", mod->name);
240                 return -ENOEXEC;
241         }
242
243         for (s = sechdrs + 1; s < sechdrs_end; ++s) {
244                 Elf32_Rel *rels = (void *)ehdr + s->sh_offset;
245                 int numrels = s->sh_size / sizeof(Elf32_Rel);
246                 Elf32_Shdr *dstsec = sechdrs + s->sh_info;
247
248                 if (s->sh_type != SHT_REL)
249                         continue;
250
251                 /* ignore relocations that operate on non-exec sections */
252                 if (!(dstsec->sh_flags & SHF_EXECINSTR))
253                         continue;
254
255                 /* sort by type and symbol index */
256                 sort(rels, numrels, sizeof(Elf32_Rel), cmp_rel, NULL);
257
258                 if (strncmp(secstrings + dstsec->sh_name, ".init", 5) != 0)
259                         core_plts += count_plts(syms, dstsec->sh_addr, rels,
260                                                 numrels, s->sh_info);
261                 else
262                         init_plts += count_plts(syms, dstsec->sh_addr, rels,
263                                                 numrels, s->sh_info);
264         }
265
266         mod->arch.core.plt->sh_type = SHT_NOBITS;
267         mod->arch.core.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
268         mod->arch.core.plt->sh_addralign = L1_CACHE_BYTES;
269         mod->arch.core.plt->sh_size = round_up(core_plts * PLT_ENT_SIZE,
270                                                sizeof(struct plt_entries));
271         mod->arch.core.plt_count = 0;
272         mod->arch.core.plt_ent = NULL;
273
274         mod->arch.init.plt->sh_type = SHT_NOBITS;
275         mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
276         mod->arch.init.plt->sh_addralign = L1_CACHE_BYTES;
277         mod->arch.init.plt->sh_size = round_up(init_plts * PLT_ENT_SIZE,
278                                                sizeof(struct plt_entries));
279         mod->arch.init.plt_count = 0;
280         mod->arch.init.plt_ent = NULL;
281
282         pr_debug("%s: plt=%x, init.plt=%x\n", __func__,
283                  mod->arch.core.plt->sh_size, mod->arch.init.plt->sh_size);
284         return 0;
285 }