[PATCH v7 4/5] LoongArch: Support PC-relative relocations in modules

Xi Ruoyao posted 5 patches 3 years, 7 months ago
[PATCH v7 4/5] LoongArch: Support PC-relative relocations in modules
Posted by Xi Ruoyao 3 years, 7 months ago
Binutils >= 2.40 uses R_LARCH_B26 instead of R_LARCH_SOP_PUSH_PLT_PCREL,
and R_LARCH_PCALA* instead of R_LARCH_SOP_PUSH_PCREL.

Handle R_LARCH_B26 and R_LARCH_PCALA* in the module loader.  For
R_LARCH_B26, also create a PLT entry as needed.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 arch/loongarch/kernel/module-sections.c |  7 ++-
 arch/loongarch/kernel/module.c          | 67 +++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/kernel/module-sections.c b/arch/loongarch/kernel/module-sections.c
index 6d498288977d..c67b9cb220eb 100644
--- a/arch/loongarch/kernel/module-sections.c
+++ b/arch/loongarch/kernel/module-sections.c
@@ -56,9 +56,14 @@ static void count_max_entries(Elf_Rela *relas, int num, unsigned int *plts)
 
 	for (i = 0; i < num; i++) {
 		type = ELF_R_TYPE(relas[i].r_info);
-		if (type == R_LARCH_SOP_PUSH_PLT_PCREL) {
+		switch (type) {
+		case R_LARCH_SOP_PUSH_PLT_PCREL:
+		case R_LARCH_B26:
 			if (!duplicate_rela(relas, i))
 				(*plts)++;
+			break;
+		default:
+			/* Do nothing. */
 		}
 	}
 }
diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c
index 755d91ef8d85..c09ddbe5ed8b 100644
--- a/arch/loongarch/kernel/module.c
+++ b/arch/loongarch/kernel/module.c
@@ -281,6 +281,71 @@ static int apply_r_larch_add_sub(struct module *mod, u32 *location, Elf_Addr v,
 	}
 }
 
+static int apply_r_larch_b26(struct module *mod, u32 *location, Elf_Addr v,
+			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
+{
+	ptrdiff_t offset = (void *)v - (void *)location;
+	union loongarch_instruction *insn = (union loongarch_instruction *)location;
+
+	if (offset >= SZ_128M)
+		v = module_emit_plt_entry(mod, v);
+
+	if (offset < -SZ_128M)
+		v = module_emit_plt_entry(mod, v);
+
+	offset = (void *)v - (void *)location;
+
+	if (offset & 3) {
+		pr_err("module %s: jump offset = 0x%llx unaligned! dangerous R_LARCH_B26 (%u) relocation\n",
+				mod->name, (long long)offset, type);
+		return -ENOEXEC;
+	}
+
+	if (!signed_imm_check(offset, 28)) {
+		pr_err("module %s: jump offset = 0x%llx overflow! dangerous R_LARCH_B26 (%u) relocation\n",
+				mod->name, (long long)offset, type);
+		return -ENOEXEC;
+	}
+
+	offset >>= 2;
+	insn->reg0i26_format.immediate_l = offset & 0xffff;
+	insn->reg0i26_format.immediate_h = (offset >> 16) & 0x3ff;
+	return 0;
+}
+
+static int apply_r_larch_pcala(struct module *mod, u32 *location, Elf_Addr v,
+			s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
+{
+	union loongarch_instruction *insn = (union loongarch_instruction *)location;
+	/* Use s32 for a sign-extension deliberately. */
+	s32 offset_hi20 = (void *)((v + 0x800) & ~0xfff) -
+		(void *)((Elf_Addr)location & ~0xfff);
+	Elf_Addr anchor = (((Elf_Addr)location) & ~0xfff) + offset_hi20;
+	ptrdiff_t offset_rem = (void *)v - (void *)anchor;
+
+	switch (type) {
+	case R_LARCH_PCALA_HI20:
+		v = offset_hi20 >> 12;
+		insn->reg1i20_format.immediate = v & 0xfffff;
+		break;
+	case R_LARCH_PCALA64_LO20:
+		v = offset_rem >> 32;
+		insn->reg1i20_format.immediate = v & 0xfffff;
+		break;
+	case R_LARCH_PCALA64_HI12:
+		v = offset_rem >> 52;
+		/* fall through */
+	case R_LARCH_PCALA_LO12:
+		insn->reg2i12_format.immediate = v & 0xfff;
+		break;
+	default:
+		pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 /*
  * reloc_handlers_rela() - Apply a particular relocation to a module
  * @mod: the module to apply the reloc to
@@ -310,6 +375,8 @@ static reloc_rela_handler reloc_rela_handlers[] = {
 	[R_LARCH_SOP_SUB ... R_LARCH_SOP_IF_ELSE] 	     = apply_r_larch_sop,
 	[R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] = apply_r_larch_sop_imm_field,
 	[R_LARCH_ADD32 ... R_LARCH_SUB64]		     = apply_r_larch_add_sub,
+	[R_LARCH_B26]					     = apply_r_larch_b26,
+	[R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12]	     = apply_r_larch_pcala,
 };
 
 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
-- 
2.37.0
Re: [PATCH v7 4/5] LoongArch: Support PC-relative relocations in modules
Posted by kernel test robot 3 years, 7 months ago
Hi Xi,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on kees/for-next/execve]
[also build test WARNING on linus/master v6.0-rc3 next-20220830]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Xi-Ruoyao/LoongArch-Support-toolchain-with-new-relocation-types/20220830-185350
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/execve
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20220830/202208302041.ANqQ0hQd-lkp@intel.com/config)
compiler: loongarch64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/529c0f36d2dad7dd4bcec3815f821547d9e9643c
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Xi-Ruoyao/LoongArch-Support-toolchain-with-new-relocation-types/20220830-185350
        git checkout 529c0f36d2dad7dd4bcec3815f821547d9e9643c
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch SHELL=/bin/bash arch/loongarch/kernel/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

     366 |         [R_LARCH_NONE]                                       = apply_r_larch_none,
         |                                                                ^~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:366:64: note: (near initialization for 'reloc_rela_handlers[0]')
   arch/loongarch/kernel/module.c:367:64: warning: initialized field overwritten [-Woverride-init]
     367 |         [R_LARCH_32]                                         = apply_r_larch_32,
         |                                                                ^~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:367:64: note: (near initialization for 'reloc_rela_handlers[1]')
   arch/loongarch/kernel/module.c:368:64: warning: initialized field overwritten [-Woverride-init]
     368 |         [R_LARCH_64]                                         = apply_r_larch_64,
         |                                                                ^~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:368:64: note: (near initialization for 'reloc_rela_handlers[2]')
   arch/loongarch/kernel/module.c:369:64: warning: initialized field overwritten [-Woverride-init]
     369 |         [R_LARCH_MARK_LA]                                    = apply_r_larch_none,
         |                                                                ^~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:369:64: note: (near initialization for 'reloc_rela_handlers[20]')
   arch/loongarch/kernel/module.c:370:64: warning: initialized field overwritten [-Woverride-init]
     370 |         [R_LARCH_MARK_PCREL]                                 = apply_r_larch_none,
         |                                                                ^~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:370:64: note: (near initialization for 'reloc_rela_handlers[21]')
   arch/loongarch/kernel/module.c:371:64: warning: initialized field overwritten [-Woverride-init]
     371 |         [R_LARCH_SOP_PUSH_PCREL]                             = apply_r_larch_sop_push_pcrel,
         |                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:371:64: note: (near initialization for 'reloc_rela_handlers[22]')
   arch/loongarch/kernel/module.c:372:64: warning: initialized field overwritten [-Woverride-init]
     372 |         [R_LARCH_SOP_PUSH_ABSOLUTE]                          = apply_r_larch_sop_push_absolute,
         |                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:372:64: note: (near initialization for 'reloc_rela_handlers[23]')
   arch/loongarch/kernel/module.c:373:64: warning: initialized field overwritten [-Woverride-init]
     373 |         [R_LARCH_SOP_PUSH_DUP]                               = apply_r_larch_sop_push_dup,
         |                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:373:64: note: (near initialization for 'reloc_rela_handlers[24]')
   arch/loongarch/kernel/module.c:374:64: warning: initialized field overwritten [-Woverride-init]
     374 |         [R_LARCH_SOP_PUSH_PLT_PCREL]                         = apply_r_larch_sop_push_plt_pcrel,
         |                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:374:64: note: (near initialization for 'reloc_rela_handlers[29]')
   arch/loongarch/kernel/module.c:375:64: warning: initialized field overwritten [-Woverride-init]
     375 |         [R_LARCH_SOP_SUB ... R_LARCH_SOP_IF_ELSE]            = apply_r_larch_sop,
         |                                                                ^~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:375:64: note: (near initialization for 'reloc_rela_handlers[32]')
   arch/loongarch/kernel/module.c:375:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:375:64: note: (near initialization for 'reloc_rela_handlers[33]')
   arch/loongarch/kernel/module.c:375:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:375:64: note: (near initialization for 'reloc_rela_handlers[34]')
   arch/loongarch/kernel/module.c:375:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:375:64: note: (near initialization for 'reloc_rela_handlers[35]')
   arch/loongarch/kernel/module.c:375:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:375:64: note: (near initialization for 'reloc_rela_handlers[36]')
   arch/loongarch/kernel/module.c:375:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:375:64: note: (near initialization for 'reloc_rela_handlers[37]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
     376 |         [R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] = apply_r_larch_sop_imm_field,
         |                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[38]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[39]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[40]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[41]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[42]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[43]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[44]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[45]')
   arch/loongarch/kernel/module.c:376:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:376:64: note: (near initialization for 'reloc_rela_handlers[46]')
   arch/loongarch/kernel/module.c:377:64: warning: initialized field overwritten [-Woverride-init]
     377 |         [R_LARCH_ADD32 ... R_LARCH_SUB64]                    = apply_r_larch_add_sub,
         |                                                                ^~~~~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:377:64: note: (near initialization for 'reloc_rela_handlers[50]')
   arch/loongarch/kernel/module.c:377:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:377:64: note: (near initialization for 'reloc_rela_handlers[51]')
   arch/loongarch/kernel/module.c:377:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:377:64: note: (near initialization for 'reloc_rela_handlers[52]')
   arch/loongarch/kernel/module.c:377:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:377:64: note: (near initialization for 'reloc_rela_handlers[53]')
   arch/loongarch/kernel/module.c:377:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:377:64: note: (near initialization for 'reloc_rela_handlers[54]')
   arch/loongarch/kernel/module.c:377:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:377:64: note: (near initialization for 'reloc_rela_handlers[55]')
   arch/loongarch/kernel/module.c:377:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:377:64: note: (near initialization for 'reloc_rela_handlers[56]')
   arch/loongarch/kernel/module.c:378:64: warning: initialized field overwritten [-Woverride-init]
     378 |         [R_LARCH_B26]                                        = apply_r_larch_b26,
         |                                                                ^~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:378:64: note: (near initialization for 'reloc_rela_handlers[66]')
   arch/loongarch/kernel/module.c:379:64: warning: initialized field overwritten [-Woverride-init]
     379 |         [R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12]          = apply_r_larch_pcala,
         |                                                                ^~~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:379:64: note: (near initialization for 'reloc_rela_handlers[71]')
   arch/loongarch/kernel/module.c:379:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:379:64: note: (near initialization for 'reloc_rela_handlers[72]')
   arch/loongarch/kernel/module.c:379:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:379:64: note: (near initialization for 'reloc_rela_handlers[73]')
   arch/loongarch/kernel/module.c:379:64: warning: initialized field overwritten [-Woverride-init]
   arch/loongarch/kernel/module.c:379:64: note: (near initialization for 'reloc_rela_handlers[74]')
   arch/loongarch/kernel/module.c: In function 'apply_r_larch_pcala':
>> arch/loongarch/kernel/module.c:336:19: warning: this statement may fall through [-Wimplicit-fallthrough=]
     336 |                 v = offset_rem >> 52;
         |                 ~~^~~~~~~~~~~~~~~~~~
   arch/loongarch/kernel/module.c:338:9: note: here
     338 |         case R_LARCH_PCALA_LO12:
         |         ^~~~


vim +336 arch/loongarch/kernel/module.c

   315	
   316	static int apply_r_larch_pcala(struct module *mod, u32 *location, Elf_Addr v,
   317				s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
   318	{
   319		union loongarch_instruction *insn = (union loongarch_instruction *)location;
   320		/* Use s32 for a sign-extension deliberately. */
   321		s32 offset_hi20 = (void *)((v + 0x800) & ~0xfff) -
   322			(void *)((Elf_Addr)location & ~0xfff);
   323		Elf_Addr anchor = (((Elf_Addr)location) & ~0xfff) + offset_hi20;
   324		ptrdiff_t offset_rem = (void *)v - (void *)anchor;
   325	
   326		switch (type) {
   327		case R_LARCH_PCALA_HI20:
   328			v = offset_hi20 >> 12;
   329			insn->reg1i20_format.immediate = v & 0xfffff;
   330			break;
   331		case R_LARCH_PCALA64_LO20:
   332			v = offset_rem >> 32;
   333			insn->reg1i20_format.immediate = v & 0xfffff;
   334			break;
   335		case R_LARCH_PCALA64_HI12:
 > 336			v = offset_rem >> 52;
   337			/* fall through */
   338		case R_LARCH_PCALA_LO12:
   339			insn->reg2i12_format.immediate = v & 0xfff;
   340			break;
   341		default:
   342			pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
   343			return -EINVAL;
   344		}
   345	
   346		return 0;
   347	}
   348	

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp