1 /*
2  * Copyright (C) 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 
10 #include <linux/module.h>
11 
12 int sym64_rel;
13 
14 #define SYM64_ABS_VAL		0xffff880000cccccc
15 #define SYM32_ABS_VAL		0xf800cccc
16 #define SYM16_ABS_VAL		0xf8cc
17 
18 #define __SET_ABS(name, val)	asm(".globl " #name "; .set "#name ", " #val)
19 #define SET_ABS(name, val)	__SET_ABS(name, val)
20 
21 SET_ABS(sym64_abs, SYM64_ABS_VAL);
22 SET_ABS(sym32_abs, SYM32_ABS_VAL);
23 SET_ABS(sym16_abs, SYM16_ABS_VAL);
24 
25 asmlinkage u64 absolute_data64(void);
26 asmlinkage u64 absolute_data32(void);
27 asmlinkage u64 absolute_data16(void);
28 asmlinkage u64 signed_movw(void);
29 asmlinkage u64 unsigned_movw(void);
30 asmlinkage u64 relative_adrp(void);
31 asmlinkage u64 relative_adrp_far(void);
32 asmlinkage u64 relative_adr(void);
33 asmlinkage u64 relative_data64(void);
34 asmlinkage u64 relative_data32(void);
35 asmlinkage u64 relative_data16(void);
36 
37 static struct {
38 	char	name[32];
39 	u64	(*f)(void);
40 	u64	expect;
41 } const funcs[] = {
42 	{ "R_AARCH64_ABS64",		absolute_data64, UL(SYM64_ABS_VAL) },
43 	{ "R_AARCH64_ABS32",		absolute_data32, UL(SYM32_ABS_VAL) },
44 	{ "R_AARCH64_ABS16",		absolute_data16, UL(SYM16_ABS_VAL) },
45 	{ "R_AARCH64_MOVW_SABS_Gn",	signed_movw, UL(SYM64_ABS_VAL) },
46 	{ "R_AARCH64_MOVW_UABS_Gn",	unsigned_movw, UL(SYM64_ABS_VAL) },
47 	{ "R_AARCH64_ADR_PREL_PG_HI21",	relative_adrp, (u64)&sym64_rel },
48 	{ "R_AARCH64_ADR_PREL_PG_HI21",	relative_adrp_far, (u64)&memstart_addr },
49 	{ "R_AARCH64_ADR_PREL_LO21",	relative_adr, (u64)&sym64_rel },
50 	{ "R_AARCH64_PREL64",		relative_data64, (u64)&sym64_rel },
51 	{ "R_AARCH64_PREL32",		relative_data32, (u64)&sym64_rel },
52 	{ "R_AARCH64_PREL16",		relative_data16, (u64)&sym64_rel },
53 };
54 
55 static int reloc_test_init(void)
56 {
57 	int i;
58 
59 	pr_info("Relocation test:\n");
60 	pr_info("-------------------------------------------------------\n");
61 
62 	for (i = 0; i < ARRAY_SIZE(funcs); i++) {
63 		u64 ret = funcs[i].f();
64 
65 		pr_info("%-31s 0x%016llx %s\n", funcs[i].name, ret,
66 			ret == funcs[i].expect ? "pass" : "fail");
67 		if (ret != funcs[i].expect)
68 			pr_err("Relocation failed, expected 0x%016llx, not 0x%016llx\n",
69 			       funcs[i].expect, ret);
70 	}
71 	return 0;
72 }
73 
74 static void reloc_test_exit(void)
75 {
76 }
77 
78 module_init(reloc_test_init);
79 module_exit(reloc_test_exit);
80 
81 MODULE_LICENSE("GPL v2");
82