Feature section replacement code is copied over the original code at
boot, but its PC-relative branches are resolved for the address it is
linked at, so they point to the wrong place after the copy.  The kernel
currently corrects them at boot in patch_feature_section().

Have objtool correct them at build time instead, by rewriting each such
branch in the replacement code so it is valid at its final location.
Report an error if a branch cannot be encoded there, for example because
it is out of range.

Also make arch_pc_relative_reloc() recognise the powerpc branch
relocations so that they reach arch_support_alt_relocation().

Co-developed-by: Nicholas Piggin <[email protected]>
Signed-off-by: Nicholas Piggin <[email protected]>
Co-developed-by: Sathvika Vasireddy <[email protected]>
Signed-off-by: Sathvika Vasireddy <[email protected]>
Signed-off-by: Shivang Upadhyay <[email protected]>
---
 tools/objtool/arch/powerpc/decode.c  |  6 +----
 tools/objtool/arch/powerpc/special.c | 39 +++++++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
index e534ac1123b3..2273cab73087 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -102,11 +102,7 @@ unsigned long arch_jump_destination(struct instruction 
*insn)
 
 bool arch_pc_relative_reloc(struct reloc *reloc)
 {
-       /*
-        * The powerpc build only allows certain relocation types, see
-        * relocs_check.sh, and none of those accepted are PC relative.
-        */
-       return false;
+       return reloc_type(reloc) == R_PPC_REL24 || reloc_type(reloc) == 
R_PPC_REL14;
 }
 
 void arch_initial_func_cfi_state(struct cfi_init_state *state)
diff --git a/tools/objtool/arch/powerpc/special.c 
b/tools/objtool/arch/powerpc/special.c
index 2db320796fee..1a34b08d6118 100644
--- a/tools/objtool/arch/powerpc/special.c
+++ b/tools/objtool/arch/powerpc/special.c
@@ -3,14 +3,51 @@
 #include <stdlib.h>
 #include <objtool/special.h>
 #include <objtool/builtin.h>
+#include <objtool/warn.h>
 
+#include <asm/text-patching.h>
+#include <asm/inst.h>
+
+#include "../../../arch/powerpc/lib/code-patching-branch.c"
 
 bool arch_support_alt_relocation(struct objtool_file *file,
                                 struct special_alt *special_alt,
                                 struct instruction *insn,
                                 struct reloc *reloc)
 {
-       exit(-1);
+       struct elf *elf = file->elf;
+       u64 alt = special_alt->new_sec->sh.sh_addr + special_alt->new_off;
+       u64 orig = special_alt->orig_sec->sh.sh_addr + special_alt->orig_off;
+       u64 insn_addr = insn->sec->sh.sh_addr + insn->offset;
+       struct symbol *sym = reloc->sym;
+       u64 target = sym->sym.st_value + reloc_addend(reloc);
+       u64 dest;
+       u32 *word = insn->sec->data->d_buf + insn->offset;
+       u32 val = bswap_if_needed(elf, *word);
+       ppc_inst_t instr = ppc_inst(val);
+       int err;
+
+       /* The linker points direct calls to the local entry point */
+       if (is_func_sym(sym))
+               target += PPC64_LOCAL_ENTRY_OFFSET(sym->sym.st_other);
+
+       dest = insn_addr + orig - alt;
+
+       if (instr_is_branch_iform(instr))
+               err = create_branch(&instr, (const u32 *)dest, target, 
ppc_inst_val(instr));
+       else if (instr_is_branch_bform(instr))
+               err = create_cond_branch(&instr, (const u32 *)dest, target, 
ppc_inst_val(instr));
+       else
+               err = 1;
+
+       if (err) {
+               ERROR_INSN(insn, "alternative branch out of range");
+               return false;
+       }
+
+       val = bswap_if_needed(elf, ppc_inst_val(instr));
+       return !elf_write_insn(elf, insn->sec, insn->offset, sizeof(val),
+                              (const char *)&val);
 }
 
 struct reloc *arch_find_switch_table(struct objtool_file *file,
-- 
2.56.1


Reply via email to