2 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
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.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/sort.h>
14 #include <asm/cache.h>
15 #include <asm/opcodes.h>
17 #define PLT_ENT_STRIDE L1_CACHE_BYTES
18 #define PLT_ENT_COUNT (PLT_ENT_STRIDE / sizeof(u32))
19 #define PLT_ENT_SIZE (sizeof(struct plt_entries) / PLT_ENT_COUNT)
21 #ifdef CONFIG_THUMB2_KERNEL
22 #define PLT_ENT_LDR __opcode_to_mem_thumb32(0xf8dff000 | \
25 #define PLT_ENT_LDR __opcode_to_mem_arm(0xe59ff000 | \
30 u32 ldr[PLT_ENT_COUNT];
31 u32 lit[PLT_ENT_COUNT];
34 u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val)
36 struct plt_entries *plt = (struct plt_entries *)mod->arch.plt->sh_addr;
40 * Look for an existing entry pointing to 'val'. Given that the
41 * relocations are sorted, this will be the last entry we allocated.
44 if (mod->arch.plt_count > 0) {
45 plt += (mod->arch.plt_count - 1) / PLT_ENT_COUNT;
46 idx = (mod->arch.plt_count - 1) % PLT_ENT_COUNT;
48 if (plt->lit[idx] == val)
49 return (u32)&plt->ldr[idx];
51 idx = (idx + 1) % PLT_ENT_COUNT;
56 mod->arch.plt_count++;
57 BUG_ON(mod->arch.plt_count * PLT_ENT_SIZE > mod->arch.plt->sh_size);
60 /* Populate a new set of entries */
61 *plt = (struct plt_entries){
62 { [0 ... PLT_ENT_COUNT - 1] = PLT_ENT_LDR, },
68 return (u32)&plt->ldr[idx];
71 #define cmp_3way(a,b) ((a) < (b) ? -1 : (a) > (b))
73 static int cmp_rel(const void *a, const void *b)
75 const Elf32_Rel *x = a, *y = b;
78 /* sort by type and symbol index */
79 i = cmp_3way(ELF32_R_TYPE(x->r_info), ELF32_R_TYPE(y->r_info));
81 i = cmp_3way(ELF32_R_SYM(x->r_info), ELF32_R_SYM(y->r_info));
85 static bool is_zero_addend_relocation(Elf32_Addr base, const Elf32_Rel *rel)
87 u32 *tval = (u32 *)(base + rel->r_offset);
90 * Do a bitwise compare on the raw addend rather than fully decoding
91 * the offset and doing an arithmetic comparison.
92 * Note that a zero-addend jump/call relocation is encoded taking the
93 * PC bias into account, i.e., -8 for ARM and -4 for Thumb2.
95 switch (ELF32_R_TYPE(rel->r_info)) {
99 case R_ARM_THM_JUMP24:
100 upper = __mem_to_opcode_thumb16(((u16 *)tval)[0]);
101 lower = __mem_to_opcode_thumb16(((u16 *)tval)[1]);
103 return (upper & 0x7ff) == 0x7ff && (lower & 0x2fff) == 0x2ffe;
108 return (__mem_to_opcode_arm(*tval) & 0xffffff) == 0xfffffe;
113 static bool duplicate_rel(Elf32_Addr base, const Elf32_Rel *rel, int num)
115 const Elf32_Rel *prev;
118 * Entries are sorted by type and symbol index. That means that,
119 * if a duplicate entry exists, it must be in the preceding
125 prev = rel + num - 1;
126 return cmp_rel(rel + num, prev) == 0 &&
127 is_zero_addend_relocation(base, prev);
130 /* Count how many PLT entries we may need */
131 static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base,
132 const Elf32_Rel *rel, int num)
134 unsigned int ret = 0;
138 for (i = 0; i < num; i++) {
139 switch (ELF32_R_TYPE(rel[i].r_info)) {
144 case R_ARM_THM_JUMP24:
146 * We only have to consider branch targets that resolve
147 * to undefined symbols. This is not simply a heuristic,
148 * it is a fundamental limitation, since the PLT itself
149 * is part of the module, and needs to be within range
150 * as well, so modules can never grow beyond that limit.
152 s = syms + ELF32_R_SYM(rel[i].r_info);
153 if (s->st_shndx != SHN_UNDEF)
157 * Jump relocations with non-zero addends against
158 * undefined symbols are supported by the ELF spec, but
159 * do not occur in practice (e.g., 'jump n bytes past
160 * the entry point of undefined function symbol f').
161 * So we need to support them, but there is no need to
162 * take them into consideration when trying to optimize
163 * this code. So let's only check for duplicates when
164 * the addend is zero.
166 if (!is_zero_addend_relocation(base, rel + i) ||
167 !duplicate_rel(base, rel, i))
174 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
175 char *secstrings, struct module *mod)
177 unsigned long plts = 0;
178 Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
179 Elf32_Sym *syms = NULL;
182 * To store the PLTs, we expand the .text section for core module code
183 * and for initialization code.
185 for (s = sechdrs; s < sechdrs_end; ++s) {
186 if (strcmp(".plt", secstrings + s->sh_name) == 0)
188 else if (s->sh_type == SHT_SYMTAB)
189 syms = (Elf32_Sym *)s->sh_addr;
192 if (!mod->arch.plt) {
193 pr_err("%s: module PLT section missing\n", mod->name);
197 pr_err("%s: module symtab section missing\n", mod->name);
201 for (s = sechdrs + 1; s < sechdrs_end; ++s) {
202 Elf32_Rel *rels = (void *)ehdr + s->sh_offset;
203 int numrels = s->sh_size / sizeof(Elf32_Rel);
204 Elf32_Shdr *dstsec = sechdrs + s->sh_info;
206 if (s->sh_type != SHT_REL)
209 /* ignore relocations that operate on non-exec sections */
210 if (!(dstsec->sh_flags & SHF_EXECINSTR))
213 /* sort by type and symbol index */
214 sort(rels, numrels, sizeof(Elf32_Rel), cmp_rel, NULL);
216 plts += count_plts(syms, dstsec->sh_addr, rels, numrels);
219 mod->arch.plt->sh_type = SHT_NOBITS;
220 mod->arch.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
221 mod->arch.plt->sh_addralign = L1_CACHE_BYTES;
222 mod->arch.plt->sh_size = round_up(plts * PLT_ENT_SIZE,
223 sizeof(struct plt_entries));
224 mod->arch.plt_count = 0;
226 pr_debug("%s: plt=%x\n", __func__, mod->arch.plt->sh_size);