xref: /linux/arch/arm64/kernel/reloc_test_core.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2214fad55SArd Biesheuvel /*
3214fad55SArd Biesheuvel  * Copyright (C) 2017 Linaro, Ltd. <ard.biesheuvel@linaro.org>
4214fad55SArd Biesheuvel  */
5214fad55SArd Biesheuvel 
6214fad55SArd Biesheuvel #include <linux/module.h>
7214fad55SArd Biesheuvel 
8214fad55SArd Biesheuvel int sym64_rel;
9214fad55SArd Biesheuvel 
10214fad55SArd Biesheuvel #define SYM64_ABS_VAL		0xffff880000cccccc
11214fad55SArd Biesheuvel #define SYM32_ABS_VAL		0xf800cccc
12214fad55SArd Biesheuvel #define SYM16_ABS_VAL		0xf8cc
13214fad55SArd Biesheuvel 
14214fad55SArd Biesheuvel #define __SET_ABS(name, val)	asm(".globl " #name "; .set "#name ", " #val)
15214fad55SArd Biesheuvel #define SET_ABS(name, val)	__SET_ABS(name, val)
16214fad55SArd Biesheuvel 
17214fad55SArd Biesheuvel SET_ABS(sym64_abs, SYM64_ABS_VAL);
18214fad55SArd Biesheuvel SET_ABS(sym32_abs, SYM32_ABS_VAL);
19214fad55SArd Biesheuvel SET_ABS(sym16_abs, SYM16_ABS_VAL);
20214fad55SArd Biesheuvel 
21214fad55SArd Biesheuvel asmlinkage u64 absolute_data64(void);
22214fad55SArd Biesheuvel asmlinkage u64 absolute_data32(void);
23214fad55SArd Biesheuvel asmlinkage u64 absolute_data16(void);
24214fad55SArd Biesheuvel asmlinkage u64 signed_movw(void);
25214fad55SArd Biesheuvel asmlinkage u64 unsigned_movw(void);
26214fad55SArd Biesheuvel asmlinkage u64 relative_adrp(void);
27a257e025SArd Biesheuvel asmlinkage u64 relative_adrp_far(void);
28214fad55SArd Biesheuvel asmlinkage u64 relative_adr(void);
29214fad55SArd Biesheuvel asmlinkage u64 relative_data64(void);
30214fad55SArd Biesheuvel asmlinkage u64 relative_data32(void);
31214fad55SArd Biesheuvel asmlinkage u64 relative_data16(void);
32214fad55SArd Biesheuvel 
33214fad55SArd Biesheuvel static struct {
34214fad55SArd Biesheuvel 	char	name[32];
35214fad55SArd Biesheuvel 	u64	(*f)(void);
36214fad55SArd Biesheuvel 	u64	expect;
37214fad55SArd Biesheuvel } const funcs[] = {
38214fad55SArd Biesheuvel 	{ "R_AARCH64_ABS64",		absolute_data64, UL(SYM64_ABS_VAL) },
39214fad55SArd Biesheuvel 	{ "R_AARCH64_ABS32",		absolute_data32, UL(SYM32_ABS_VAL) },
40214fad55SArd Biesheuvel 	{ "R_AARCH64_ABS16",		absolute_data16, UL(SYM16_ABS_VAL) },
41214fad55SArd Biesheuvel 	{ "R_AARCH64_MOVW_SABS_Gn",	signed_movw, UL(SYM64_ABS_VAL) },
42214fad55SArd Biesheuvel 	{ "R_AARCH64_MOVW_UABS_Gn",	unsigned_movw, UL(SYM64_ABS_VAL) },
43214fad55SArd Biesheuvel 	{ "R_AARCH64_ADR_PREL_PG_HI21",	relative_adrp, (u64)&sym64_rel },
44bd99f9a1SArnd Bergmann 	{ "R_AARCH64_ADR_PREL_PG_HI21",	relative_adrp_far, (u64)&memstart_addr },
45214fad55SArd Biesheuvel 	{ "R_AARCH64_ADR_PREL_LO21",	relative_adr, (u64)&sym64_rel },
46214fad55SArd Biesheuvel 	{ "R_AARCH64_PREL64",		relative_data64, (u64)&sym64_rel },
47214fad55SArd Biesheuvel 	{ "R_AARCH64_PREL32",		relative_data32, (u64)&sym64_rel },
48214fad55SArd Biesheuvel 	{ "R_AARCH64_PREL16",		relative_data16, (u64)&sym64_rel },
49214fad55SArd Biesheuvel };
50214fad55SArd Biesheuvel 
reloc_test_init(void)518c6e3657SXiu Jianfeng static int __init reloc_test_init(void)
52214fad55SArd Biesheuvel {
53214fad55SArd Biesheuvel 	int i;
54214fad55SArd Biesheuvel 
55214fad55SArd Biesheuvel 	pr_info("Relocation test:\n");
56214fad55SArd Biesheuvel 	pr_info("-------------------------------------------------------\n");
57214fad55SArd Biesheuvel 
58214fad55SArd Biesheuvel 	for (i = 0; i < ARRAY_SIZE(funcs); i++) {
59214fad55SArd Biesheuvel 		u64 ret = funcs[i].f();
60214fad55SArd Biesheuvel 
61214fad55SArd Biesheuvel 		pr_info("%-31s 0x%016llx %s\n", funcs[i].name, ret,
62214fad55SArd Biesheuvel 			ret == funcs[i].expect ? "pass" : "fail");
63214fad55SArd Biesheuvel 		if (ret != funcs[i].expect)
64214fad55SArd Biesheuvel 			pr_err("Relocation failed, expected 0x%016llx, not 0x%016llx\n",
65214fad55SArd Biesheuvel 			       funcs[i].expect, ret);
66214fad55SArd Biesheuvel 	}
67214fad55SArd Biesheuvel 	return 0;
68214fad55SArd Biesheuvel }
69214fad55SArd Biesheuvel 
reloc_test_exit(void)708c6e3657SXiu Jianfeng static void __exit reloc_test_exit(void)
71214fad55SArd Biesheuvel {
72214fad55SArd Biesheuvel }
73214fad55SArd Biesheuvel 
74214fad55SArd Biesheuvel module_init(reloc_test_init);
75214fad55SArd Biesheuvel module_exit(reloc_test_exit);
76214fad55SArd Biesheuvel 
77*2a805201SJeff Johnson MODULE_DESCRIPTION("Relocation testing module");
78214fad55SArd Biesheuvel MODULE_LICENSE("GPL v2");
79