xref: /openbmc/linux/arch/arm64/kernel/module-plts.c (revision 5e8307b9)
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/kernel.h>
11 #include <linux/module.h>
12 #include <linux/sort.h>
13 
14 static bool in_init(const struct module *mod, void *loc)
15 {
16 	return (u64)loc - (u64)mod->init_layout.base < mod->init_layout.size;
17 }
18 
19 u64 module_emit_plt_entry(struct module *mod, void *loc, const Elf64_Rela *rela,
20 			  Elf64_Sym *sym)
21 {
22 	struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
23 							  &mod->arch.init;
24 	struct plt_entry *plt = (struct plt_entry *)pltsec->plt->sh_addr;
25 	int i = pltsec->plt_num_entries;
26 	u64 val = sym->st_value + rela->r_addend;
27 
28 	plt[i] = get_plt_entry(val);
29 
30 	/*
31 	 * Check if the entry we just created is a duplicate. Given that the
32 	 * relocations are sorted, this will be the last entry we allocated.
33 	 * (if one exists).
34 	 */
35 	if (i > 0 && plt_entries_equal(plt + i, plt + i - 1))
36 		return (u64)&plt[i - 1];
37 
38 	pltsec->plt_num_entries++;
39 	if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
40 		return 0;
41 
42 	return (u64)&plt[i];
43 }
44 
45 #define cmp_3way(a,b)	((a) < (b) ? -1 : (a) > (b))
46 
47 static int cmp_rela(const void *a, const void *b)
48 {
49 	const Elf64_Rela *x = a, *y = b;
50 	int i;
51 
52 	/* sort by type, symbol index and addend */
53 	i = cmp_3way(ELF64_R_TYPE(x->r_info), ELF64_R_TYPE(y->r_info));
54 	if (i == 0)
55 		i = cmp_3way(ELF64_R_SYM(x->r_info), ELF64_R_SYM(y->r_info));
56 	if (i == 0)
57 		i = cmp_3way(x->r_addend, y->r_addend);
58 	return i;
59 }
60 
61 static bool duplicate_rel(const Elf64_Rela *rela, int num)
62 {
63 	/*
64 	 * Entries are sorted by type, symbol index and addend. That means
65 	 * that, if a duplicate entry exists, it must be in the preceding
66 	 * slot.
67 	 */
68 	return num > 0 && cmp_rela(rela + num, rela + num - 1) == 0;
69 }
70 
71 static unsigned int count_plts(Elf64_Sym *syms, Elf64_Rela *rela, int num,
72 			       Elf64_Word dstidx)
73 {
74 	unsigned int ret = 0;
75 	Elf64_Sym *s;
76 	int i;
77 
78 	for (i = 0; i < num; i++) {
79 		switch (ELF64_R_TYPE(rela[i].r_info)) {
80 		case R_AARCH64_JUMP26:
81 		case R_AARCH64_CALL26:
82 			/*
83 			 * We only have to consider branch targets that resolve
84 			 * to symbols that are defined in a different section.
85 			 * This is not simply a heuristic, it is a fundamental
86 			 * limitation, since there is no guaranteed way to emit
87 			 * PLT entries sufficiently close to the branch if the
88 			 * section size exceeds the range of a branch
89 			 * instruction. So ignore relocations against defined
90 			 * symbols if they live in the same section as the
91 			 * relocation target.
92 			 */
93 			s = syms + ELF64_R_SYM(rela[i].r_info);
94 			if (s->st_shndx == dstidx)
95 				break;
96 
97 			/*
98 			 * Jump relocations with non-zero addends against
99 			 * undefined symbols are supported by the ELF spec, but
100 			 * do not occur in practice (e.g., 'jump n bytes past
101 			 * the entry point of undefined function symbol f').
102 			 * So we need to support them, but there is no need to
103 			 * take them into consideration when trying to optimize
104 			 * this code. So let's only check for duplicates when
105 			 * the addend is zero: this allows us to record the PLT
106 			 * entry address in the symbol table itself, rather than
107 			 * having to search the list for duplicates each time we
108 			 * emit one.
109 			 */
110 			if (rela[i].r_addend != 0 || !duplicate_rel(rela, i))
111 				ret++;
112 			break;
113 		}
114 	}
115 	return ret;
116 }
117 
118 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
119 			      char *secstrings, struct module *mod)
120 {
121 	unsigned long core_plts = 0;
122 	unsigned long init_plts = 0;
123 	Elf64_Sym *syms = NULL;
124 	Elf_Shdr *tramp = NULL;
125 	int i;
126 
127 	/*
128 	 * Find the empty .plt section so we can expand it to store the PLT
129 	 * entries. Record the symtab address as well.
130 	 */
131 	for (i = 0; i < ehdr->e_shnum; i++) {
132 		if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
133 			mod->arch.core.plt = sechdrs + i;
134 		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".init.plt"))
135 			mod->arch.init.plt = sechdrs + i;
136 		else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE) &&
137 			 !strcmp(secstrings + sechdrs[i].sh_name,
138 				 ".text.ftrace_trampoline"))
139 			tramp = sechdrs + i;
140 		else if (sechdrs[i].sh_type == SHT_SYMTAB)
141 			syms = (Elf64_Sym *)sechdrs[i].sh_addr;
142 	}
143 
144 	if (!mod->arch.core.plt || !mod->arch.init.plt) {
145 		pr_err("%s: module PLT section(s) missing\n", mod->name);
146 		return -ENOEXEC;
147 	}
148 	if (!syms) {
149 		pr_err("%s: module symtab section missing\n", mod->name);
150 		return -ENOEXEC;
151 	}
152 
153 	for (i = 0; i < ehdr->e_shnum; i++) {
154 		Elf64_Rela *rels = (void *)ehdr + sechdrs[i].sh_offset;
155 		int numrels = sechdrs[i].sh_size / sizeof(Elf64_Rela);
156 		Elf64_Shdr *dstsec = sechdrs + sechdrs[i].sh_info;
157 
158 		if (sechdrs[i].sh_type != SHT_RELA)
159 			continue;
160 
161 		/* ignore relocations that operate on non-exec sections */
162 		if (!(dstsec->sh_flags & SHF_EXECINSTR))
163 			continue;
164 
165 		/* sort by type, symbol index and addend */
166 		sort(rels, numrels, sizeof(Elf64_Rela), cmp_rela, NULL);
167 
168 		if (strncmp(secstrings + dstsec->sh_name, ".init", 5) != 0)
169 			core_plts += count_plts(syms, rels, numrels,
170 						sechdrs[i].sh_info);
171 		else
172 			init_plts += count_plts(syms, rels, numrels,
173 						sechdrs[i].sh_info);
174 	}
175 
176 	mod->arch.core.plt->sh_type = SHT_NOBITS;
177 	mod->arch.core.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
178 	mod->arch.core.plt->sh_addralign = L1_CACHE_BYTES;
179 	mod->arch.core.plt->sh_size = (core_plts  + 1) * sizeof(struct plt_entry);
180 	mod->arch.core.plt_num_entries = 0;
181 	mod->arch.core.plt_max_entries = core_plts;
182 
183 	mod->arch.init.plt->sh_type = SHT_NOBITS;
184 	mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
185 	mod->arch.init.plt->sh_addralign = L1_CACHE_BYTES;
186 	mod->arch.init.plt->sh_size = (init_plts + 1) * sizeof(struct plt_entry);
187 	mod->arch.init.plt_num_entries = 0;
188 	mod->arch.init.plt_max_entries = init_plts;
189 
190 	if (tramp) {
191 		tramp->sh_type = SHT_NOBITS;
192 		tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
193 		tramp->sh_addralign = __alignof__(struct plt_entry);
194 		tramp->sh_size = sizeof(struct plt_entry);
195 	}
196 
197 	return 0;
198 }
199