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