Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* Kernel module help for PPC. |
3 | Copyright (C) 2001 Rusty Russell. | |
4 | ||
1da177e4 | 5 | */ |
c7d1f6af AB |
6 | |
7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
8 | ||
1da177e4 LT |
9 | #include <linux/module.h> |
10 | #include <linux/moduleloader.h> | |
11 | #include <linux/elf.h> | |
12 | #include <linux/vmalloc.h> | |
13 | #include <linux/fs.h> | |
14 | #include <linux/string.h> | |
15 | #include <linux/kernel.h> | |
7cc45e64 | 16 | #include <linux/ftrace.h> |
1da177e4 | 17 | #include <linux/cache.h> |
73c9ceab | 18 | #include <linux/bug.h> |
eda09fbd | 19 | #include <linux/sort.h> |
b88c4767 | 20 | #include <asm/setup.h> |
21c4ff80 | 21 | |
1da177e4 LT |
22 | /* Count how many different relocations (different symbol, different |
23 | addend) */ | |
24 | static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num) | |
25 | { | |
eda09fbd EM |
26 | unsigned int i, r_info, r_addend, _count_relocs; |
27 | ||
28 | _count_relocs = 0; | |
29 | r_info = 0; | |
30 | r_addend = 0; | |
31 | for (i = 0; i < num; i++) | |
32 | /* Only count 24-bit relocs, others don't need stubs */ | |
33 | if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 && | |
34 | (r_info != ELF32_R_SYM(rela[i].r_info) || | |
35 | r_addend != rela[i].r_addend)) { | |
36 | _count_relocs++; | |
37 | r_info = ELF32_R_SYM(rela[i].r_info); | |
38 | r_addend = rela[i].r_addend; | |
1da177e4 | 39 | } |
eda09fbd | 40 | |
7cc45e64 SR |
41 | #ifdef CONFIG_DYNAMIC_FTRACE |
42 | _count_relocs++; /* add one for ftrace_caller */ | |
43 | #endif | |
eda09fbd EM |
44 | return _count_relocs; |
45 | } | |
46 | ||
47 | static int relacmp(const void *_x, const void *_y) | |
48 | { | |
49 | const Elf32_Rela *x, *y; | |
50 | ||
51 | y = (Elf32_Rela *)_x; | |
52 | x = (Elf32_Rela *)_y; | |
53 | ||
54 | /* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to | |
55 | * make the comparison cheaper/faster. It won't affect the sorting or | |
56 | * the counting algorithms' performance | |
57 | */ | |
58 | if (x->r_info < y->r_info) | |
59 | return -1; | |
60 | else if (x->r_info > y->r_info) | |
61 | return 1; | |
62 | else if (x->r_addend < y->r_addend) | |
63 | return -1; | |
64 | else if (x->r_addend > y->r_addend) | |
65 | return 1; | |
66 | else | |
67 | return 0; | |
68 | } | |
69 | ||
70 | static void relaswap(void *_x, void *_y, int size) | |
71 | { | |
72 | uint32_t *x, *y, tmp; | |
73 | int i; | |
74 | ||
75 | y = (uint32_t *)_x; | |
76 | x = (uint32_t *)_y; | |
77 | ||
78 | for (i = 0; i < sizeof(Elf32_Rela) / sizeof(uint32_t); i++) { | |
79 | tmp = x[i]; | |
80 | x[i] = y[i]; | |
81 | y[i] = tmp; | |
1da177e4 | 82 | } |
1da177e4 LT |
83 | } |
84 | ||
85 | /* Get the potential trampolines size required of the init and | |
86 | non-init sections */ | |
87 | static unsigned long get_plt_size(const Elf32_Ehdr *hdr, | |
88 | const Elf32_Shdr *sechdrs, | |
89 | const char *secstrings, | |
90 | int is_init) | |
91 | { | |
92 | unsigned long ret = 0; | |
93 | unsigned i; | |
94 | ||
95 | /* Everything marked ALLOC (this includes the exported | |
96 | symbols) */ | |
97 | for (i = 1; i < hdr->e_shnum; i++) { | |
98 | /* If it's called *.init*, and we're not init, we're | |
99 | not interested */ | |
d8731527 | 100 | if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != NULL) |
1da177e4 LT |
101 | != is_init) |
102 | continue; | |
103 | ||
104 | /* We don't want to look at debug sections. */ | |
d8731527 | 105 | if (strstr(secstrings + sechdrs[i].sh_name, ".debug")) |
1da177e4 LT |
106 | continue; |
107 | ||
108 | if (sechdrs[i].sh_type == SHT_RELA) { | |
c7d1f6af AB |
109 | pr_debug("Found relocations in section %u\n", i); |
110 | pr_debug("Ptr: %p. Number: %u\n", | |
1da177e4 LT |
111 | (void *)hdr + sechdrs[i].sh_offset, |
112 | sechdrs[i].sh_size / sizeof(Elf32_Rela)); | |
eda09fbd EM |
113 | |
114 | /* Sort the relocation information based on a symbol and | |
115 | * addend key. This is a stable O(n*log n) complexity | |
116 | * alogrithm but it will reduce the complexity of | |
117 | * count_relocs() to linear complexity O(n) | |
118 | */ | |
119 | sort((void *)hdr + sechdrs[i].sh_offset, | |
120 | sechdrs[i].sh_size / sizeof(Elf32_Rela), | |
121 | sizeof(Elf32_Rela), relacmp, relaswap); | |
122 | ||
1da177e4 LT |
123 | ret += count_relocs((void *)hdr |
124 | + sechdrs[i].sh_offset, | |
125 | sechdrs[i].sh_size | |
126 | / sizeof(Elf32_Rela)) | |
127 | * sizeof(struct ppc_plt_entry); | |
128 | } | |
129 | } | |
130 | ||
131 | return ret; | |
132 | } | |
133 | ||
134 | int module_frob_arch_sections(Elf32_Ehdr *hdr, | |
135 | Elf32_Shdr *sechdrs, | |
136 | char *secstrings, | |
137 | struct module *me) | |
138 | { | |
139 | unsigned int i; | |
140 | ||
141 | /* Find .plt and .init.plt sections */ | |
142 | for (i = 0; i < hdr->e_shnum; i++) { | |
143 | if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0) | |
144 | me->arch.init_plt_section = i; | |
145 | else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0) | |
146 | me->arch.core_plt_section = i; | |
147 | } | |
148 | if (!me->arch.core_plt_section || !me->arch.init_plt_section) { | |
c7d1f6af | 149 | pr_err("Module doesn't contain .plt or .init.plt sections.\n"); |
1da177e4 LT |
150 | return -ENOEXEC; |
151 | } | |
152 | ||
153 | /* Override their sizes */ | |
154 | sechdrs[me->arch.core_plt_section].sh_size | |
155 | = get_plt_size(hdr, sechdrs, secstrings, 0); | |
156 | sechdrs[me->arch.init_plt_section].sh_size | |
157 | = get_plt_size(hdr, sechdrs, secstrings, 1); | |
158 | return 0; | |
159 | } | |
160 | ||
1da177e4 LT |
161 | static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val) |
162 | { | |
3c752965 SR |
163 | if (entry->jump[0] == 0x3d800000 + ((val + 0x8000) >> 16) |
164 | && entry->jump[1] == 0x398c0000 + (val & 0xffff)) | |
1da177e4 LT |
165 | return 1; |
166 | return 0; | |
167 | } | |
168 | ||
169 | /* Set up a trampoline in the PLT to bounce us to the distant function */ | |
170 | static uint32_t do_plt_call(void *location, | |
171 | Elf32_Addr val, | |
136cd345 | 172 | const Elf32_Shdr *sechdrs, |
1da177e4 LT |
173 | struct module *mod) |
174 | { | |
175 | struct ppc_plt_entry *entry; | |
176 | ||
c7d1f6af | 177 | pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location); |
1da177e4 | 178 | /* Init, or core PLT? */ |
7523e4dc RR |
179 | if (location >= mod->core_layout.base |
180 | && location < mod->core_layout.base + mod->core_layout.size) | |
1da177e4 LT |
181 | entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr; |
182 | else | |
183 | entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr; | |
184 | ||
185 | /* Find this entry, or if that fails, the next avail. entry */ | |
186 | while (entry->jump[0]) { | |
187 | if (entry_matches(entry, val)) return (uint32_t)entry; | |
188 | entry++; | |
189 | } | |
190 | ||
3c752965 SR |
191 | entry->jump[0] = 0x3d800000+((val+0x8000)>>16); /* lis r12,sym@ha */ |
192 | entry->jump[1] = 0x398c0000 + (val&0xffff); /* addi r12,r12,sym@l*/ | |
193 | entry->jump[2] = 0x7d8903a6; /* mtctr r12 */ | |
1da177e4 LT |
194 | entry->jump[3] = 0x4e800420; /* bctr */ |
195 | ||
c7d1f6af | 196 | pr_debug("Initialized plt for 0x%x at %p\n", val, entry); |
1da177e4 LT |
197 | return (uint32_t)entry; |
198 | } | |
199 | ||
200 | int apply_relocate_add(Elf32_Shdr *sechdrs, | |
201 | const char *strtab, | |
202 | unsigned int symindex, | |
203 | unsigned int relsec, | |
204 | struct module *module) | |
205 | { | |
206 | unsigned int i; | |
207 | Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr; | |
208 | Elf32_Sym *sym; | |
209 | uint32_t *location; | |
210 | uint32_t value; | |
211 | ||
c7d1f6af | 212 | pr_debug("Applying ADD relocate section %u to %u\n", relsec, |
1da177e4 LT |
213 | sechdrs[relsec].sh_info); |
214 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { | |
215 | /* This is where to make the change */ | |
216 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr | |
217 | + rela[i].r_offset; | |
218 | /* This is the symbol it is referring to. Note that all | |
219 | undefined symbols have been resolved. */ | |
220 | sym = (Elf32_Sym *)sechdrs[symindex].sh_addr | |
221 | + ELF32_R_SYM(rela[i].r_info); | |
222 | /* `Everything is relative'. */ | |
223 | value = sym->st_value + rela[i].r_addend; | |
224 | ||
225 | switch (ELF32_R_TYPE(rela[i].r_info)) { | |
226 | case R_PPC_ADDR32: | |
227 | /* Simply set it */ | |
228 | *(uint32_t *)location = value; | |
229 | break; | |
230 | ||
231 | case R_PPC_ADDR16_LO: | |
232 | /* Low half of the symbol */ | |
233 | *(uint16_t *)location = value; | |
234 | break; | |
9a3d6458 SV |
235 | |
236 | case R_PPC_ADDR16_HI: | |
237 | /* Higher half of the symbol */ | |
238 | *(uint16_t *)location = (value >> 16); | |
239 | break; | |
240 | ||
1da177e4 LT |
241 | case R_PPC_ADDR16_HA: |
242 | /* Sign-adjusted lower 16 bits: PPC ELF ABI says: | |
243 | (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF. | |
244 | This is the same, only sane. | |
245 | */ | |
246 | *(uint16_t *)location = (value + 0x8000) >> 16; | |
247 | break; | |
248 | ||
249 | case R_PPC_REL24: | |
250 | if ((int)(value - (uint32_t)location) < -0x02000000 | |
251 | || (int)(value - (uint32_t)location) >= 0x02000000) | |
252 | value = do_plt_call(location, value, | |
253 | sechdrs, module); | |
254 | ||
255 | /* Only replace bits 2 through 26 */ | |
c7d1f6af | 256 | pr_debug("REL24 value = %08X. location = %08X\n", |
1da177e4 | 257 | value, (uint32_t)location); |
c7d1f6af | 258 | pr_debug("Location before: %08X.\n", |
1da177e4 LT |
259 | *(uint32_t *)location); |
260 | *(uint32_t *)location | |
261 | = (*(uint32_t *)location & ~0x03fffffc) | |
262 | | ((value - (uint32_t)location) | |
263 | & 0x03fffffc); | |
c7d1f6af | 264 | pr_debug("Location after: %08X.\n", |
1da177e4 | 265 | *(uint32_t *)location); |
c7d1f6af | 266 | pr_debug("ie. jump to %08X+%08X = %08X\n", |
1da177e4 LT |
267 | *(uint32_t *)location & 0x03fffffc, |
268 | (uint32_t)location, | |
269 | (*(uint32_t *)location & 0x03fffffc) | |
270 | + (uint32_t)location); | |
271 | break; | |
272 | ||
273 | case R_PPC_REL32: | |
274 | /* 32-bit relative jump. */ | |
275 | *(uint32_t *)location = value - (uint32_t)location; | |
276 | break; | |
277 | ||
278 | default: | |
c7d1f6af | 279 | pr_err("%s: unknown ADD relocation: %u\n", |
1da177e4 LT |
280 | module->name, |
281 | ELF32_R_TYPE(rela[i].r_info)); | |
282 | return -ENOEXEC; | |
283 | } | |
284 | } | |
136cd345 ME |
285 | |
286 | return 0; | |
287 | } | |
288 | ||
7cc45e64 | 289 | #ifdef CONFIG_DYNAMIC_FTRACE |
136cd345 ME |
290 | int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs) |
291 | { | |
292 | module->arch.tramp = do_plt_call(module->core_layout.base, | |
293 | (unsigned long)ftrace_caller, | |
294 | sechdrs, module); | |
295 | if (!module->arch.tramp) | |
296 | return -ENOENT; | |
297 | ||
1da177e4 LT |
298 | return 0; |
299 | } | |
136cd345 | 300 | #endif |