[RFC PATCH 3/4] objtool/LoongArch: Add support for jump table

From: Tiezhu Yang
Date: Fri Jul 12 2024 - 05:15:22 EST


Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
tools/objtool/arch/loongarch/special.c | 54 +++++++++++++++++++++++++-
1 file changed, 53 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c
index 9bba1e9318e0..3e08ad3a507c 100644
--- a/tools/objtool/arch/loongarch/special.c
+++ b/tools/objtool/arch/loongarch/special.c
@@ -1,5 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later
+#include <string.h>
+
#include <objtool/special.h>
+#include <objtool/warn.h>

bool arch_support_alt_relocation(struct special_alt *special_alt,
struct instruction *insn,
@@ -8,8 +11,57 @@ bool arch_support_alt_relocation(struct special_alt *special_alt,
return false;
}

+static struct reloc *find_reloc_by_tablejump_annotate(struct objtool_file *file,
+ struct instruction *insn)
+{
+ struct section *rsec;
+ struct reloc *reloc;
+ unsigned long offset;
+
+ rsec = find_section_by_name(file->elf, ".rela.discard.tablejump_annotate");
+ if (!rsec)
+ return NULL;
+
+ for_each_reloc(rsec, reloc) {
+ if (reloc->sym->sec->rodata)
+ continue;
+
+ if (strcmp(insn->sec->name, reloc->sym->sec->name))
+ continue;
+
+ offset = reloc->sym->offset;
+ if (insn->offset == offset) {
+ reloc++;
+ return reloc;
+ }
+ }
+
+ return NULL;
+}
+
struct reloc *arch_find_switch_table(struct objtool_file *file,
struct instruction *insn)
{
- return NULL;
+ struct reloc *annotate_reloc;
+ struct reloc *rodata_reloc;
+ struct section *table_sec;
+ unsigned long table_offset;
+
+ annotate_reloc = find_reloc_by_tablejump_annotate(file, insn);
+ if (!annotate_reloc)
+ return NULL;
+
+ table_sec = annotate_reloc->sym->sec;
+ table_offset = annotate_reloc->sym->offset;
+
+ /*
+ * Each table entry has a rela associated with it. The rela
+ * should reference text in the same function as the original
+ * instruction.
+ */
+ rodata_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);
+ if (!rodata_reloc)
+ return NULL;
+
+ return rodata_reloc;
}
--
2.42.0