blob: ac5f34993b53bb7c42341aef529ab459fb58fd6f [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* Kernel dynamically loadable module help for PARISC.
3 *
4 * The best reference for this stuff is probably the Processor-
5 * Specific ELF Supplement for PA-RISC:
6 * http://ftp.parisc-linux.org/docs/arch/elf-pa-hp.pdf
7 *
8 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
9 * Copyright (C) 2003 Randolph Chung <tausq at debian . org>
Helge Dellerc298be72009-01-01 22:25:30 +010010 * Copyright (C) 2008 Helge Deller <deller@gmx.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Notes:
Helge Dellerc298be72009-01-01 22:25:30 +010013 * - PLT stub handling
14 * On 32bit (and sometimes 64bit) and with big kernel modules like xfs or
15 * ipv6 the relocation types R_PARISC_PCREL17F and R_PARISC_PCREL22F may
16 * fail to reach their PLT stub if we only create one big stub array for
17 * all sections at the beginning of the core or init section.
18 * Instead we now insert individual PLT stub entries directly in front of
19 * of the code sections where the stubs are actually called.
20 * This reduces the distance between the PCREL location and the stub entry
21 * so that the relocations can be fulfilled.
22 * While calculating the final layout of the kernel module in memory, the
23 * kernel module loader calls arch_mod_section_prepend() to request the
24 * to be reserved amount of memory in front of each individual section.
25 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * - SEGREL32 handling
27 * We are not doing SEGREL32 handling correctly. According to the ABI, we
28 * should do a value offset, like this:
Eric Biederman959ed3402006-09-29 02:00:06 -070029 * if (in_init(me, (void *)val))
Rusty Russell7523e4d2015-11-26 09:44:08 +103030 * val -= (uint32_t)me->init_layout.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * else
Rusty Russell7523e4d2015-11-26 09:44:08 +103032 * val -= (uint32_t)me->core_layout.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * However, SEGREL32 is used only for PARISC unwind entries, and we want
34 * those entries to have an absolute address, and not just an offset.
35 *
Sven Schnelle6183d682019-06-05 22:32:20 +020036 * The unwind table mechanism has the ability to specify an offset for
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * the unwind table; however, because we split off the init functions into
Sven Schnelle6183d682019-06-05 22:32:20 +020038 * a different piece of memory, it is not possible to do this using a
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * single offset. Instead, we use the above hack for now.
40 */
41
42#include <linux/moduleloader.h>
43#include <linux/elf.h>
44#include <linux/vmalloc.h>
45#include <linux/fs.h>
46#include <linux/string.h>
47#include <linux/kernel.h>
Helge Deller6891f8a2006-12-16 16:16:50 +010048#include <linux/bug.h>
James Bottomleyd7dd2ff2011-04-14 18:25:21 -050049#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
James Bottomleyd7dd2ff2011-04-14 18:25:21 -050052#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/unwind.h>
Sergey Senozhatsky1705bd62017-11-10 08:48:28 +090054#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Helge Dellerc298be72009-01-01 22:25:30 +010056#define RELOC_REACHABLE(val, bits) \
57 (( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 ) || \
58 ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) ? \
59 0 : 1)
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define CHECK_RELOC(val, bits) \
Helge Dellerc298be72009-01-01 22:25:30 +010062 if (!RELOC_REACHABLE(val, bits)) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
64 me->name, strtab + sym->st_name, (unsigned long)val, bits); \
65 return -ENOEXEC; \
66 }
67
68/* Maximum number of GOT entries. We use a long displacement ldd from
69 * the bottom of the table, which has a maximum signed displacement of
70 * 0x3fff; however, since we're only going forward, this becomes
71 * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have
John David Anglinb4f2e2a2009-08-02 12:34:08 +020072 * at most 1023 entries.
73 * To overcome this 14bit displacement with some kernel modules, we'll
74 * use instead the unusal 16bit displacement method (see reassemble_16a)
75 * which gives us a maximum positive displacement of 0x7fff, and as such
76 * allows us to allocate up to 4095 GOT entries. */
77#define MAX_GOTS 4095
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* three functions to determine where in the module core
80 * or init pieces the location is */
Eric Biederman959ed3402006-09-29 02:00:06 -070081static inline int in_init(struct module *me, void *loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Rusty Russell7523e4d2015-11-26 09:44:08 +103083 return (loc >= me->init_layout.base &&
84 loc <= (me->init_layout.base + me->init_layout.size));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Eric Biederman959ed3402006-09-29 02:00:06 -070087static inline int in_core(struct module *me, void *loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Rusty Russell7523e4d2015-11-26 09:44:08 +103089 return (loc >= me->core_layout.base &&
90 loc <= (me->core_layout.base + me->core_layout.size));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Eric Biederman959ed3402006-09-29 02:00:06 -070093static inline int in_local(struct module *me, void *loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Eric Biederman959ed3402006-09-29 02:00:06 -070095 return in_init(me, loc) || in_core(me, loc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Helge Dellera8f44e32007-01-28 14:58:52 +010098#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070099struct got_entry {
100 Elf32_Addr addr;
101};
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103struct stub_entry {
104 Elf32_Word insns[2]; /* each stub entry has two insns */
105};
106#else
107struct got_entry {
108 Elf64_Addr addr;
109};
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct stub_entry {
112 Elf64_Word insns[4]; /* each stub entry has four insns */
113};
114#endif
115
116/* Field selection types defined by hppa */
117#define rnd(x) (((x)+0x1000)&~0x1fff)
118/* fsel: full 32 bits */
119#define fsel(v,a) ((v)+(a))
120/* lsel: select left 21 bits */
121#define lsel(v,a) (((v)+(a))>>11)
122/* rsel: select right 11 bits */
123#define rsel(v,a) (((v)+(a))&0x7ff)
124/* lrsel with rounding of addend to nearest 8k */
125#define lrsel(v,a) (((v)+rnd(a))>>11)
126/* rrsel with rounding of addend to nearest 8k */
127#define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
128
129#define mask(x,sz) ((x) & ~((1<<(sz))-1))
130
131
132/* The reassemble_* functions prepare an immediate value for
133 insertion into an opcode. pa-risc uses all sorts of weird bitfields
134 in the instruction to hold the value. */
John David Anglinb4f2e2a2009-08-02 12:34:08 +0200135static inline int sign_unext(int x, int len)
136{
137 int len_ones;
138
139 len_ones = (1 << len) - 1;
140 return x & len_ones;
141}
142
143static inline int low_sign_unext(int x, int len)
144{
145 int sign, temp;
146
147 sign = (x >> (len-1)) & 1;
148 temp = sign_unext(x, len-1);
149 return (temp << 1) | sign;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static inline int reassemble_14(int as14)
153{
154 return (((as14 & 0x1fff) << 1) |
155 ((as14 & 0x2000) >> 13));
156}
157
John David Anglinb4f2e2a2009-08-02 12:34:08 +0200158static inline int reassemble_16a(int as16)
159{
160 int s, t;
161
162 /* Unusual 16-bit encoding, for wide mode only. */
163 t = (as16 << 1) & 0xffff;
164 s = (as16 & 0x8000);
165 return (t ^ s ^ (s >> 1)) | (s >> 15);
166}
167
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static inline int reassemble_17(int as17)
170{
171 return (((as17 & 0x10000) >> 16) |
172 ((as17 & 0x0f800) << 5) |
173 ((as17 & 0x00400) >> 8) |
174 ((as17 & 0x003ff) << 3));
175}
176
177static inline int reassemble_21(int as21)
178{
179 return (((as21 & 0x100000) >> 20) |
180 ((as21 & 0x0ffe00) >> 8) |
181 ((as21 & 0x000180) << 7) |
182 ((as21 & 0x00007c) << 14) |
183 ((as21 & 0x000003) << 12));
184}
185
186static inline int reassemble_22(int as22)
187{
188 return (((as22 & 0x200000) >> 21) |
189 ((as22 & 0x1f0000) << 5) |
190 ((as22 & 0x00f800) << 5) |
191 ((as22 & 0x000400) >> 8) |
192 ((as22 & 0x0003ff) << 3));
193}
194
195void *module_alloc(unsigned long size)
196{
James Bottomleyd7dd2ff2011-04-14 18:25:21 -0500197 /* using RWX means less protection for modules, but it's
198 * easier than trying to map the text, data, init_text and
199 * init_data correctly */
200 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
Michal Hocko19809c22017-05-08 15:57:44 -0700201 GFP_KERNEL,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -0800202 PAGE_KERNEL_RWX, 0, NUMA_NO_NODE,
James Bottomleyd7dd2ff2011-04-14 18:25:21 -0500203 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Helge Dellera8f44e32007-01-28 14:58:52 +0100206#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
208{
209 return 0;
210}
211
212static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
213{
214 return 0;
215}
216
217static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
218{
219 unsigned long cnt = 0;
220
221 for (; n > 0; n--, rela++)
222 {
223 switch (ELF32_R_TYPE(rela->r_info)) {
224 case R_PARISC_PCREL17F:
225 case R_PARISC_PCREL22F:
226 cnt++;
227 }
228 }
229
230 return cnt;
231}
232#else
233static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
234{
235 unsigned long cnt = 0;
236
237 for (; n > 0; n--, rela++)
238 {
239 switch (ELF64_R_TYPE(rela->r_info)) {
240 case R_PARISC_LTOFF21L:
241 case R_PARISC_LTOFF14R:
242 case R_PARISC_PCREL22F:
243 cnt++;
244 }
245 }
246
247 return cnt;
248}
249
250static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
251{
252 unsigned long cnt = 0;
253
254 for (; n > 0; n--, rela++)
255 {
256 switch (ELF64_R_TYPE(rela->r_info)) {
257 case R_PARISC_FPTR64:
258 cnt++;
259 }
260 }
261
262 return cnt;
263}
264
265static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
266{
267 unsigned long cnt = 0;
268
269 for (; n > 0; n--, rela++)
270 {
271 switch (ELF64_R_TYPE(rela->r_info)) {
272 case R_PARISC_PCREL22F:
273 cnt++;
274 }
275 }
276
277 return cnt;
278}
279#endif
280
Rusty Russelld453cde2015-01-20 09:07:04 +1030281void module_arch_freeing_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Helge Dellerc298be72009-01-01 22:25:30 +0100283 kfree(mod->arch.section);
284 mod->arch.section = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Helge Dellerc298be72009-01-01 22:25:30 +0100287/* Additional bytes needed in front of individual sections */
288unsigned int arch_mod_section_prepend(struct module *mod,
289 unsigned int section)
290{
291 /* size needed for all stubs of this section (including
292 * one additional for correct alignment of the stubs) */
293 return (mod->arch.section[section].stub_entries + 1)
294 * sizeof(struct stub_entry);
295}
296
Sven Schnelle6183d682019-06-05 22:32:20 +0200297#define CONST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
299 CONST Elf_Shdr *sechdrs,
300 CONST char *secstrings,
301 struct module *me)
302{
Helge Dellerc298be72009-01-01 22:25:30 +0100303 unsigned long gots = 0, fdescs = 0, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned int i;
305
Helge Dellerc298be72009-01-01 22:25:30 +0100306 len = hdr->e_shnum * sizeof(me->arch.section[0]);
307 me->arch.section = kzalloc(len, GFP_KERNEL);
308 if (!me->arch.section)
309 return -ENOMEM;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 for (i = 1; i < hdr->e_shnum; i++) {
Helge Dellerc298be72009-01-01 22:25:30 +0100312 const Elf_Rela *rels = (void *)sechdrs[i].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
Helge Dellerc298be72009-01-01 22:25:30 +0100314 unsigned int count, s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (strncmp(secstrings + sechdrs[i].sh_name,
317 ".PARISC.unwind", 14) == 0)
318 me->arch.unwind_section = i;
319
320 if (sechdrs[i].sh_type != SHT_RELA)
321 continue;
322
323 /* some of these are not relevant for 32-bit/64-bit
324 * we leave them here to make the code common. the
325 * compiler will do its thing and optimize out the
326 * stuff we don't need
327 */
328 gots += count_gots(rels, nrels);
329 fdescs += count_fdescs(rels, nrels);
Helge Dellerc298be72009-01-01 22:25:30 +0100330
331 /* XXX: By sorting the relocs and finding duplicate entries
332 * we could reduce the number of necessary stubs and save
333 * some memory. */
334 count = count_stubs(rels, nrels);
335 if (!count)
336 continue;
337
338 /* so we need relocation stubs. reserve necessary memory. */
339 /* sh_info gives the section for which we need to add stubs. */
340 s = sechdrs[i].sh_info;
341
342 /* each code section should only have one relocation section */
343 WARN_ON(me->arch.section[s].stub_entries);
344
345 /* store number of stubs we need for this section */
346 me->arch.section[s].stub_entries += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 /* align things a bit */
Rusty Russell7523e4d2015-11-26 09:44:08 +1030350 me->core_layout.size = ALIGN(me->core_layout.size, 16);
351 me->arch.got_offset = me->core_layout.size;
352 me->core_layout.size += gots * sizeof(struct got_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Rusty Russell7523e4d2015-11-26 09:44:08 +1030354 me->core_layout.size = ALIGN(me->core_layout.size, 16);
355 me->arch.fdesc_offset = me->core_layout.size;
356 me->core_layout.size += fdescs * sizeof(Elf_Fdesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 me->arch.got_max = gots;
359 me->arch.fdesc_max = fdescs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 return 0;
362}
363
Helge Dellera8f44e32007-01-28 14:58:52 +0100364#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
366{
367 unsigned int i;
368 struct got_entry *got;
369
370 value += addend;
371
372 BUG_ON(value == 0);
373
Rusty Russell7523e4d2015-11-26 09:44:08 +1030374 got = me->core_layout.base + me->arch.got_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 for (i = 0; got[i].addr; i++)
376 if (got[i].addr == value)
377 goto out;
378
379 BUG_ON(++me->arch.got_count > me->arch.got_max);
380
381 got[i].addr = value;
382 out:
Sven Schnelle6183d682019-06-05 22:32:20 +0200383 pr_debug("GOT ENTRY %d[%lx] val %lx\n", i, i*sizeof(struct got_entry),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 value);
385 return i * sizeof(struct got_entry);
386}
Helge Dellera8f44e32007-01-28 14:58:52 +0100387#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Helge Dellera8f44e32007-01-28 14:58:52 +0100389#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static Elf_Addr get_fdesc(struct module *me, unsigned long value)
391{
Rusty Russell7523e4d2015-11-26 09:44:08 +1030392 Elf_Fdesc *fdesc = me->core_layout.base + me->arch.fdesc_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (!value) {
395 printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
396 return 0;
397 }
398
399 /* Look for existing fdesc entry. */
400 while (fdesc->addr) {
401 if (fdesc->addr == value)
402 return (Elf_Addr)fdesc;
403 fdesc++;
404 }
405
406 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
407
408 /* Create new one */
409 fdesc->addr = value;
Rusty Russell7523e4d2015-11-26 09:44:08 +1030410 fdesc->gp = (Elf_Addr)me->core_layout.base + me->arch.got_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return (Elf_Addr)fdesc;
412}
Helge Dellera8f44e32007-01-28 14:58:52 +0100413#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
James Bottomley6e1b95852006-06-23 14:15:20 -0600415enum elf_stub_type {
416 ELF_STUB_GOT,
417 ELF_STUB_MILLI,
418 ELF_STUB_DIRECT,
419};
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
Helge Dellerc298be72009-01-01 22:25:30 +0100422 enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct stub_entry *stub;
John David Anglinb4f2e2a2009-08-02 12:34:08 +0200425 int __maybe_unused d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Helge Dellerc298be72009-01-01 22:25:30 +0100427 /* initialize stub_offset to point in front of the section */
428 if (!me->arch.section[targetsec].stub_offset) {
429 loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
430 sizeof(struct stub_entry);
431 /* get correct alignment for the stubs */
432 loc0 = ALIGN(loc0, sizeof(struct stub_entry));
433 me->arch.section[targetsec].stub_offset = loc0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
Helge Dellerc298be72009-01-01 22:25:30 +0100436 /* get address of stub entry */
437 stub = (void *) me->arch.section[targetsec].stub_offset;
438 me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry);
439
440 /* do not write outside available stub area */
441 BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
442
443
Helge Dellera8f44e32007-01-28 14:58:52 +0100444#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/* for 32-bit the stub looks like this:
446 * ldil L'XXX,%r1
447 * be,n R'XXX(%sr4,%r1)
448 */
449 //value = *(unsigned long *)((value + addend) & ~3); /* why? */
450
451 stub->insns[0] = 0x20200000; /* ldil L'XXX,%r1 */
452 stub->insns[1] = 0xe0202002; /* be,n R'XXX(%sr4,%r1) */
453
454 stub->insns[0] |= reassemble_21(lrsel(value, addend));
455 stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
456
457#else
James Bottomley6e1b95852006-06-23 14:15:20 -0600458/* for 64-bit we have three kinds of stubs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * for normal function calls:
460 * ldd 0(%dp),%dp
461 * ldd 10(%dp), %r1
462 * bve (%r1)
463 * ldd 18(%dp), %dp
464 *
465 * for millicode:
466 * ldil 0, %r1
467 * ldo 0(%r1), %r1
468 * ldd 10(%r1), %r1
469 * bve,n (%r1)
James Bottomley6e1b95852006-06-23 14:15:20 -0600470 *
471 * for direct branches (jumps between different section of the
472 * same module):
473 * ldil 0, %r1
474 * ldo 0(%r1), %r1
475 * bve,n (%r1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
James Bottomley6e1b95852006-06-23 14:15:20 -0600477 switch (stub_type) {
478 case ELF_STUB_GOT:
John David Anglinb4f2e2a2009-08-02 12:34:08 +0200479 d = get_got(me, value, addend);
480 if (d <= 15) {
481 /* Format 5 */
482 stub->insns[0] = 0x0f6010db; /* ldd 0(%dp),%dp */
483 stub->insns[0] |= low_sign_unext(d, 5) << 16;
484 } else {
485 /* Format 3 */
486 stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp */
487 stub->insns[0] |= reassemble_16a(d);
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 stub->insns[1] = 0x53610020; /* ldd 10(%dp),%r1 */
490 stub->insns[2] = 0xe820d000; /* bve (%r1) */
491 stub->insns[3] = 0x537b0030; /* ldd 18(%dp),%dp */
James Bottomley6e1b95852006-06-23 14:15:20 -0600492 break;
493 case ELF_STUB_MILLI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
495 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
496 stub->insns[2] = 0x50210020; /* ldd 10(%r1),%r1 */
497 stub->insns[3] = 0xe820d002; /* bve,n (%r1) */
498
499 stub->insns[0] |= reassemble_21(lrsel(value, addend));
500 stub->insns[1] |= reassemble_14(rrsel(value, addend));
James Bottomley6e1b95852006-06-23 14:15:20 -0600501 break;
502 case ELF_STUB_DIRECT:
503 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
504 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
505 stub->insns[2] = 0xe820d002; /* bve,n (%r1) */
506
507 stub->insns[0] |= reassemble_21(lrsel(value, addend));
508 stub->insns[1] |= reassemble_14(rrsel(value, addend));
509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
James Bottomley6e1b95852006-06-23 14:15:20 -0600511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512#endif
513
514 return (Elf_Addr)stub;
515}
516
Helge Dellera8f44e32007-01-28 14:58:52 +0100517#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518int apply_relocate_add(Elf_Shdr *sechdrs,
519 const char *strtab,
520 unsigned int symindex,
521 unsigned int relsec,
522 struct module *me)
523{
524 int i;
525 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
526 Elf32_Sym *sym;
527 Elf32_Word *loc;
528 Elf32_Addr val;
529 Elf32_Sword addend;
530 Elf32_Addr dot;
Helge Dellerc298be72009-01-01 22:25:30 +0100531 Elf_Addr loc0;
532 unsigned int targetsec = sechdrs[relsec].sh_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 //unsigned long dp = (unsigned long)$global$;
534 register unsigned long dp asm ("r27");
535
Sven Schnelle6183d682019-06-05 22:32:20 +0200536 pr_debug("Applying relocate section %u to %u\n", relsec,
Helge Dellerc298be72009-01-01 22:25:30 +0100537 targetsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
539 /* This is where to make the change */
Helge Dellerc298be72009-01-01 22:25:30 +0100540 loc = (void *)sechdrs[targetsec].sh_addr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 + rel[i].r_offset;
Helge Dellerc298be72009-01-01 22:25:30 +0100542 /* This is the start of the target section */
543 loc0 = sechdrs[targetsec].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* This is the symbol it is referring to */
545 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
546 + ELF32_R_SYM(rel[i].r_info);
547 if (!sym->st_value) {
548 printk(KERN_WARNING "%s: Unknown symbol %s\n",
549 me->name, strtab + sym->st_name);
550 return -ENOENT;
551 }
552 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
553 dot = (Elf32_Addr)loc & ~0x03;
554
555 val = sym->st_value;
556 addend = rel[i].r_addend;
557
558#if 0
559#define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
Sven Schnelle6183d682019-06-05 22:32:20 +0200560 pr_debug("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 strtab + sym->st_name,
562 (uint32_t)loc, val, addend,
563 r(R_PARISC_PLABEL32)
564 r(R_PARISC_DIR32)
565 r(R_PARISC_DIR21L)
566 r(R_PARISC_DIR14R)
567 r(R_PARISC_SEGREL32)
568 r(R_PARISC_DPREL21L)
569 r(R_PARISC_DPREL14R)
570 r(R_PARISC_PCREL17F)
571 r(R_PARISC_PCREL22F)
572 "UNKNOWN");
573#undef r
574#endif
575
576 switch (ELF32_R_TYPE(rel[i].r_info)) {
577 case R_PARISC_PLABEL32:
578 /* 32-bit function address */
579 /* no function descriptors... */
580 *loc = fsel(val, addend);
581 break;
582 case R_PARISC_DIR32:
583 /* direct 32-bit ref */
584 *loc = fsel(val, addend);
585 break;
586 case R_PARISC_DIR21L:
587 /* left 21 bits of effective address */
588 val = lrsel(val, addend);
589 *loc = mask(*loc, 21) | reassemble_21(val);
590 break;
591 case R_PARISC_DIR14R:
592 /* right 14 bits of effective address */
593 val = rrsel(val, addend);
594 *loc = mask(*loc, 14) | reassemble_14(val);
595 break;
596 case R_PARISC_SEGREL32:
597 /* 32-bit segment relative address */
598 /* See note about special handling of SEGREL32 at
599 * the beginning of this file.
600 */
Sven Schnelle6183d682019-06-05 22:32:20 +0200601 *loc = fsel(val, addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
Mikulas Patocka5f655322017-03-14 11:47:29 -0400603 case R_PARISC_SECREL32:
604 /* 32-bit section relative address. */
605 *loc = fsel(val, addend);
606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 case R_PARISC_DPREL21L:
608 /* left 21 bit of relative address */
609 val = lrsel(val - dp, addend);
610 *loc = mask(*loc, 21) | reassemble_21(val);
611 break;
612 case R_PARISC_DPREL14R:
613 /* right 14 bit of relative address */
614 val = rrsel(val - dp, addend);
615 *loc = mask(*loc, 14) | reassemble_14(val);
616 break;
617 case R_PARISC_PCREL17F:
618 /* 17-bit PC relative address */
Helge Dellerc298be72009-01-01 22:25:30 +0100619 /* calculate direct call offset */
620 val += addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 val = (val - dot - 8)/4;
Helge Dellerc298be72009-01-01 22:25:30 +0100622 if (!RELOC_REACHABLE(val, 17)) {
623 /* direct distance too far, create
624 * stub entry instead */
625 val = get_stub(me, sym->st_value, addend,
626 ELF_STUB_DIRECT, loc0, targetsec);
627 val = (val - dot - 8)/4;
628 CHECK_RELOC(val, 17);
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
631 break;
632 case R_PARISC_PCREL22F:
633 /* 22-bit PC relative address; only defined for pa20 */
Helge Dellerc298be72009-01-01 22:25:30 +0100634 /* calculate direct call offset */
635 val += addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 val = (val - dot - 8)/4;
Helge Dellerc298be72009-01-01 22:25:30 +0100637 if (!RELOC_REACHABLE(val, 22)) {
638 /* direct distance too far, create
639 * stub entry instead */
640 val = get_stub(me, sym->st_value, addend,
641 ELF_STUB_DIRECT, loc0, targetsec);
642 val = (val - dot - 8)/4;
643 CHECK_RELOC(val, 22);
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
646 break;
Helge Deller592570c2016-04-08 22:10:35 +0200647 case R_PARISC_PCREL32:
648 /* 32-bit PC relative address */
649 *loc = val - dot - 8 + addend;
650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 default:
653 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
654 me->name, ELF32_R_TYPE(rel[i].r_info));
655 return -ENOEXEC;
656 }
657 }
658
659 return 0;
660}
661
662#else
663int apply_relocate_add(Elf_Shdr *sechdrs,
664 const char *strtab,
665 unsigned int symindex,
666 unsigned int relsec,
667 struct module *me)
668{
669 int i;
670 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
671 Elf64_Sym *sym;
672 Elf64_Word *loc;
673 Elf64_Xword *loc64;
674 Elf64_Addr val;
675 Elf64_Sxword addend;
676 Elf64_Addr dot;
Helge Dellerc298be72009-01-01 22:25:30 +0100677 Elf_Addr loc0;
678 unsigned int targetsec = sechdrs[relsec].sh_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Sven Schnelle6183d682019-06-05 22:32:20 +0200680 pr_debug("Applying relocate section %u to %u\n", relsec,
Helge Dellerc298be72009-01-01 22:25:30 +0100681 targetsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
683 /* This is where to make the change */
Helge Dellerc298be72009-01-01 22:25:30 +0100684 loc = (void *)sechdrs[targetsec].sh_addr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 + rel[i].r_offset;
Helge Dellerc298be72009-01-01 22:25:30 +0100686 /* This is the start of the target section */
687 loc0 = sechdrs[targetsec].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* This is the symbol it is referring to */
689 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
690 + ELF64_R_SYM(rel[i].r_info);
691 if (!sym->st_value) {
692 printk(KERN_WARNING "%s: Unknown symbol %s\n",
693 me->name, strtab + sym->st_name);
694 return -ENOENT;
695 }
696 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
697 dot = (Elf64_Addr)loc & ~0x03;
698 loc64 = (Elf64_Xword *)loc;
699
700 val = sym->st_value;
701 addend = rel[i].r_addend;
702
703#if 0
704#define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
705 printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
706 strtab + sym->st_name,
707 loc, val, addend,
708 r(R_PARISC_LTOFF14R)
709 r(R_PARISC_LTOFF21L)
710 r(R_PARISC_PCREL22F)
711 r(R_PARISC_DIR64)
712 r(R_PARISC_SEGREL32)
713 r(R_PARISC_FPTR64)
714 "UNKNOWN");
715#undef r
716#endif
717
718 switch (ELF64_R_TYPE(rel[i].r_info)) {
719 case R_PARISC_LTOFF21L:
720 /* LT-relative; left 21 bits */
721 val = get_got(me, val, addend);
Sven Schnelle6183d682019-06-05 22:32:20 +0200722 pr_debug("LTOFF21L Symbol %s loc %p val %llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 strtab + sym->st_name,
724 loc, val);
725 val = lrsel(val, 0);
726 *loc = mask(*loc, 21) | reassemble_21(val);
727 break;
728 case R_PARISC_LTOFF14R:
729 /* L(ltoff(val+addend)) */
730 /* LT-relative; right 14 bits */
731 val = get_got(me, val, addend);
732 val = rrsel(val, 0);
Sven Schnelle6183d682019-06-05 22:32:20 +0200733 pr_debug("LTOFF14R Symbol %s loc %p val %llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 strtab + sym->st_name,
735 loc, val);
736 *loc = mask(*loc, 14) | reassemble_14(val);
737 break;
738 case R_PARISC_PCREL22F:
739 /* PC-relative; 22 bits */
Sven Schnelle6183d682019-06-05 22:32:20 +0200740 pr_debug("PCREL22F Symbol %s loc %p val %llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 strtab + sym->st_name,
742 loc, val);
Helge Dellerc298be72009-01-01 22:25:30 +0100743 val += addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* can we reach it locally? */
Helge Dellerc298be72009-01-01 22:25:30 +0100745 if (in_local(me, (void *)val)) {
746 /* this is the case where the symbol is local
747 * to the module, but in a different section,
748 * so stub the jump in case it's more than 22
749 * bits away */
750 val = (val - dot - 8)/4;
751 if (!RELOC_REACHABLE(val, 22)) {
752 /* direct distance too far, create
753 * stub entry instead */
754 val = get_stub(me, sym->st_value,
755 addend, ELF_STUB_DIRECT,
756 loc0, targetsec);
757 } else {
758 /* Ok, we can reach it directly. */
759 val = sym->st_value;
760 val += addend;
761 }
762 } else {
763 val = sym->st_value;
764 if (strncmp(strtab + sym->st_name, "$$", 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 == 0)
James Bottomley6e1b95852006-06-23 14:15:20 -0600766 val = get_stub(me, val, addend, ELF_STUB_MILLI,
Helge Dellerc298be72009-01-01 22:25:30 +0100767 loc0, targetsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 else
James Bottomley6e1b95852006-06-23 14:15:20 -0600769 val = get_stub(me, val, addend, ELF_STUB_GOT,
Helge Dellerc298be72009-01-01 22:25:30 +0100770 loc0, targetsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Sven Schnelle6183d682019-06-05 22:32:20 +0200772 pr_debug("STUB FOR %s loc %px, val %llx+%llx at %llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 strtab + sym->st_name, loc, sym->st_value,
774 addend, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 val = (val - dot - 8)/4;
Helge Dellerc298be72009-01-01 22:25:30 +0100776 CHECK_RELOC(val, 22);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
778 break;
Helge Deller592570c2016-04-08 22:10:35 +0200779 case R_PARISC_PCREL32:
780 /* 32-bit PC relative address */
781 *loc = val - dot - 8 + addend;
782 break;
Helge Dellerd2ba3b12019-06-08 20:04:46 +0200783 case R_PARISC_PCREL64:
784 /* 64-bit PC relative address */
785 *loc64 = val - dot - 8 + addend;
786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 case R_PARISC_DIR64:
788 /* 64-bit effective address */
789 *loc64 = val + addend;
790 break;
791 case R_PARISC_SEGREL32:
792 /* 32-bit segment relative address */
793 /* See note about special handling of SEGREL32 at
794 * the beginning of this file.
795 */
Sven Schnelle6183d682019-06-05 22:32:20 +0200796 *loc = fsel(val, addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 break;
Mikulas Patocka5f655322017-03-14 11:47:29 -0400798 case R_PARISC_SECREL32:
799 /* 32-bit section relative address. */
800 *loc = fsel(val, addend);
801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 case R_PARISC_FPTR64:
803 /* 64-bit function address */
Eric Biederman959ed3402006-09-29 02:00:06 -0700804 if(in_local(me, (void *)(val + addend))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *loc64 = get_fdesc(me, val+addend);
Sven Schnelle6183d682019-06-05 22:32:20 +0200806 pr_debug("FDESC for %s at %llx points to %llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 strtab + sym->st_name, *loc64,
808 ((Elf_Fdesc *)*loc64)->addr);
809 } else {
810 /* if the symbol is not local to this
811 * module then val+addend is a pointer
812 * to the function descriptor */
Sven Schnelle6183d682019-06-05 22:32:20 +0200813 pr_debug("Non local FPTR64 Symbol %s loc %p val %llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 strtab + sym->st_name,
815 loc, val);
816 *loc64 = val + addend;
817 }
818 break;
819
820 default:
821 printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
822 me->name, ELF64_R_TYPE(rel[i].r_info));
823 return -ENOEXEC;
824 }
825 }
826 return 0;
827}
828#endif
829
830static void
831register_unwind_table(struct module *me,
832 const Elf_Shdr *sechdrs)
833{
834 unsigned char *table, *end;
835 unsigned long gp;
836
837 if (!me->arch.unwind_section)
838 return;
839
840 table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
841 end = table + sechdrs[me->arch.unwind_section].sh_size;
Rusty Russell7523e4d2015-11-26 09:44:08 +1030842 gp = (Elf_Addr)me->core_layout.base + me->arch.got_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Sven Schnelle6183d682019-06-05 22:32:20 +0200844 pr_debug("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 me->arch.unwind_section, table, end, gp);
846 me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
847}
848
849static void
850deregister_unwind_table(struct module *me)
851{
852 if (me->arch.unwind)
853 unwind_table_remove(me->arch.unwind);
854}
855
856int module_finalize(const Elf_Ehdr *hdr,
857 const Elf_Shdr *sechdrs,
858 struct module *me)
859{
860 int i;
861 unsigned long nsyms;
862 const char *strtab = NULL;
Helge Deller8cc28262018-11-10 16:14:59 +0100863 const Elf_Shdr *s;
864 char *secstrings;
Sven Schnelle6ca63662019-06-05 22:32:22 +0200865 int err, symindex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 Elf_Sym *newptr, *oldptr;
867 Elf_Shdr *symhdr = NULL;
868#ifdef DEBUG
869 Elf_Fdesc *entry;
870 u32 *addr;
871
872 entry = (Elf_Fdesc *)me->init;
873 printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
874 entry->gp, entry->addr);
875 addr = (u32 *)entry->addr;
876 printk("INSNS: %x %x %x %x\n",
877 addr[0], addr[1], addr[2], addr[3]);
Helge Dellerc298be72009-01-01 22:25:30 +0100878 printk("got entries used %ld, gots max %ld\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 "fdescs used %ld, fdescs max %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 me->arch.got_count, me->arch.got_max,
881 me->arch.fdesc_count, me->arch.fdesc_max);
882#endif
883
884 register_unwind_table(me, sechdrs);
885
886 /* haven't filled in me->symtab yet, so have to find it
887 * ourselves */
888 for (i = 1; i < hdr->e_shnum; i++) {
889 if(sechdrs[i].sh_type == SHT_SYMTAB
Julia Lawallfe579c692009-08-04 22:27:07 +0200890 && (sechdrs[i].sh_flags & SHF_ALLOC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int strindex = sechdrs[i].sh_link;
Sven Schnelle6ca63662019-06-05 22:32:22 +0200892 symindex = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* FIXME: AWFUL HACK
894 * The cast is to drop the const from
895 * the sechdrs pointer */
896 symhdr = (Elf_Shdr *)&sechdrs[i];
897 strtab = (char *)sechdrs[strindex].sh_addr;
898 break;
899 }
900 }
901
Sven Schnelle6183d682019-06-05 22:32:20 +0200902 pr_debug("module %s: strtab %p, symhdr %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 me->name, strtab, symhdr);
904
905 if(me->arch.got_count > MAX_GOTS) {
Helge Dellerf8fc18a2006-10-18 21:44:30 +0200906 printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
907 me->name, me->arch.got_count, MAX_GOTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return -EINVAL;
909 }
Helge Dellerc298be72009-01-01 22:25:30 +0100910
911 kfree(me->arch.section);
912 me->arch.section = NULL;
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* no symbol table */
915 if(symhdr == NULL)
916 return 0;
917
918 oldptr = (void *)symhdr->sh_addr;
919 newptr = oldptr + 1; /* we start counting at 1 */
920 nsyms = symhdr->sh_size / sizeof(Elf_Sym);
Sven Schnelle6183d682019-06-05 22:32:20 +0200921 pr_debug("OLD num_symtab %lu\n", nsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 for (i = 1; i < nsyms; i++) {
924 oldptr++; /* note, count starts at 1 so preincrement */
925 if(strncmp(strtab + oldptr->st_name,
926 ".L", 2) == 0)
927 continue;
928
929 if(newptr != oldptr)
930 *newptr++ = *oldptr;
931 else
932 newptr++;
933
934 }
935 nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
Sven Schnelle6183d682019-06-05 22:32:20 +0200936 pr_debug("NEW num_symtab %lu\n", nsyms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 symhdr->sh_size = nsyms * sizeof(Elf_Sym);
Helge Deller8cc28262018-11-10 16:14:59 +0100938
939 /* find .altinstructions section */
940 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
941 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
942 void *aseg = (void *) s->sh_addr;
943 char *secname = secstrings + s->sh_name;
944
945 if (!strcmp(".altinstructions", secname))
946 /* patch .altinstructions */
947 apply_alternatives(aseg, aseg + s->sh_size, me->name);
Helge Deller8cc28262018-11-10 16:14:59 +0100948
Sven Schnelle6ca63662019-06-05 22:32:22 +0200949 /* For 32 bit kernels we're compiling modules with
950 * -ffunction-sections so we must relocate the addresses in the
951 *__mcount_loc section.
952 */
953 if (symindex != -1 && !strcmp(secname, "__mcount_loc")) {
954 if (s->sh_type == SHT_REL)
955 err = apply_relocate((Elf_Shdr *)sechdrs,
956 strtab, symindex,
957 s - sechdrs, me);
958 else if (s->sh_type == SHT_RELA)
959 err = apply_relocate_add((Elf_Shdr *)sechdrs,
960 strtab, symindex,
961 s - sechdrs, me);
962 if (err)
963 return err;
964 }
965 }
Linus Torvalds53363772010-10-05 11:29:27 -0700966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969void module_arch_cleanup(struct module *mod)
970{
971 deregister_unwind_table(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Sergey Senozhatsky1705bd62017-11-10 08:48:28 +0900973
974#ifdef CONFIG_64BIT
975void *dereference_module_function_descriptor(struct module *mod, void *ptr)
976{
977 unsigned long start_opd = (Elf64_Addr)mod->core_layout.base +
978 mod->arch.fdesc_offset;
979 unsigned long end_opd = start_opd +
980 mod->arch.fdesc_count * sizeof(Elf64_Fdesc);
981
982 if (ptr < (void *)start_opd || ptr >= (void *)end_opd)
983 return ptr;
984
985 return dereference_function_descriptor(ptr);
986}
987#endif