1 // SPDX-License-Identifier: GPL-2.0+
2 /* Kernel module help for SH.
4 SHcompact version by Kaz Kojima and Paul Mundt.
8 Copyright 2004 SuperH (UK) Ltd
11 Based on the sh version, and on code from the sh64-specific parts of
12 modutils, originally written by Richard Curnow and Ben Gaster.
14 #include <linux/moduleloader.h>
15 #include <linux/elf.h>
16 #include <linux/vmalloc.h>
17 #include <linux/bug.h>
19 #include <linux/string.h>
20 #include <linux/kernel.h>
21 #include <asm/unaligned.h>
22 #include <asm/dwarf.h>
24 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
26 unsigned int symindex
,
31 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
33 Elf32_Addr relocation
;
37 pr_debug("Applying relocate section %u to %u\n", relsec
,
38 sechdrs
[relsec
].sh_info
);
39 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
40 /* This is where to make the change */
41 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
43 /* This is the symbol it is referring to. Note that all
44 undefined symbols have been resolved. */
45 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
46 + ELF32_R_SYM(rel
[i
].r_info
);
47 relocation
= sym
->st_value
+ rel
[i
].r_addend
;
49 #ifdef CONFIG_SUPERH64
50 /* For text addresses, bit2 of the st_other field indicates
51 * whether the symbol is SHmedia (1) or SHcompact (0). If
52 * SHmedia, the LSB of the symbol needs to be asserted
53 * for the CPU to be in SHmedia mode when it starts executing
54 * the branch target. */
55 relocation
|= !!(sym
->st_other
& 4);
58 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
62 value
= get_unaligned(location
);
64 put_unaligned(value
, location
);
67 relocation
= (relocation
- (Elf32_Addr
) location
);
68 value
= get_unaligned(location
);
70 put_unaligned(value
, location
);
73 *location
= (*location
& ~0x3fffc00) |
74 ((relocation
& 0xffff) << 10);
76 case R_SH_IMM_MEDLOW16
:
77 *location
= (*location
& ~0x3fffc00) |
78 (((relocation
>> 16) & 0xffff) << 10);
80 case R_SH_IMM_LOW16_PCREL
:
81 relocation
-= (Elf32_Addr
) location
;
82 *location
= (*location
& ~0x3fffc00) |
83 ((relocation
& 0xffff) << 10);
85 case R_SH_IMM_MEDLOW16_PCREL
:
86 relocation
-= (Elf32_Addr
) location
;
87 *location
= (*location
& ~0x3fffc00) |
88 (((relocation
>> 16) & 0xffff) << 10);
91 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
92 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
99 int module_finalize(const Elf_Ehdr
*hdr
,
100 const Elf_Shdr
*sechdrs
,
105 ret
|= module_dwarf_finalize(hdr
, sechdrs
, me
);
110 void module_arch_cleanup(struct module
*mod
)
112 module_dwarf_cleanup(mod
);