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