Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2017 Linaro, Ltd. <ard.biesheuvel@linaro.org>
0004  */
0005 
0006 #include <linux/module.h>
0007 
0008 int sym64_rel;
0009 
0010 #define SYM64_ABS_VAL       0xffff880000cccccc
0011 #define SYM32_ABS_VAL       0xf800cccc
0012 #define SYM16_ABS_VAL       0xf8cc
0013 
0014 #define __SET_ABS(name, val)    asm(".globl " #name "; .set "#name ", " #val)
0015 #define SET_ABS(name, val)  __SET_ABS(name, val)
0016 
0017 SET_ABS(sym64_abs, SYM64_ABS_VAL);
0018 SET_ABS(sym32_abs, SYM32_ABS_VAL);
0019 SET_ABS(sym16_abs, SYM16_ABS_VAL);
0020 
0021 asmlinkage u64 absolute_data64(void);
0022 asmlinkage u64 absolute_data32(void);
0023 asmlinkage u64 absolute_data16(void);
0024 asmlinkage u64 signed_movw(void);
0025 asmlinkage u64 unsigned_movw(void);
0026 asmlinkage u64 relative_adrp(void);
0027 asmlinkage u64 relative_adrp_far(void);
0028 asmlinkage u64 relative_adr(void);
0029 asmlinkage u64 relative_data64(void);
0030 asmlinkage u64 relative_data32(void);
0031 asmlinkage u64 relative_data16(void);
0032 
0033 static struct {
0034     char    name[32];
0035     u64 (*f)(void);
0036     u64 expect;
0037 } const funcs[] = {
0038     { "R_AARCH64_ABS64",        absolute_data64, UL(SYM64_ABS_VAL) },
0039     { "R_AARCH64_ABS32",        absolute_data32, UL(SYM32_ABS_VAL) },
0040     { "R_AARCH64_ABS16",        absolute_data16, UL(SYM16_ABS_VAL) },
0041     { "R_AARCH64_MOVW_SABS_Gn", signed_movw, UL(SYM64_ABS_VAL) },
0042     { "R_AARCH64_MOVW_UABS_Gn", unsigned_movw, UL(SYM64_ABS_VAL) },
0043     { "R_AARCH64_ADR_PREL_PG_HI21", relative_adrp, (u64)&sym64_rel },
0044     { "R_AARCH64_ADR_PREL_PG_HI21", relative_adrp_far, (u64)&memstart_addr },
0045     { "R_AARCH64_ADR_PREL_LO21",    relative_adr, (u64)&sym64_rel },
0046     { "R_AARCH64_PREL64",       relative_data64, (u64)&sym64_rel },
0047     { "R_AARCH64_PREL32",       relative_data32, (u64)&sym64_rel },
0048     { "R_AARCH64_PREL16",       relative_data16, (u64)&sym64_rel },
0049 };
0050 
0051 static int reloc_test_init(void)
0052 {
0053     int i;
0054 
0055     pr_info("Relocation test:\n");
0056     pr_info("-------------------------------------------------------\n");
0057 
0058     for (i = 0; i < ARRAY_SIZE(funcs); i++) {
0059         u64 ret = funcs[i].f();
0060 
0061         pr_info("%-31s 0x%016llx %s\n", funcs[i].name, ret,
0062             ret == funcs[i].expect ? "pass" : "fail");
0063         if (ret != funcs[i].expect)
0064             pr_err("Relocation failed, expected 0x%016llx, not 0x%016llx\n",
0065                    funcs[i].expect, ret);
0066     }
0067     return 0;
0068 }
0069 
0070 static void reloc_test_exit(void)
0071 {
0072 }
0073 
0074 module_init(reloc_test_init);
0075 module_exit(reloc_test_exit);
0076 
0077 MODULE_LICENSE("GPL v2");