* [PATCH v3 00/10] Add jump table support for objtool on LoongArch
@ 2024-11-19 6:56 Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 01/10] objtool: Handle various symbol types of rodata Tiezhu Yang
` (10 more replies)
0 siblings, 11 replies; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
This series is based on 6.12-rc7, tested with the latest upstream
mainline Binutils, GCC and Clang, most of the patches are aim to
handle the special cases compiled with Clang on LoongArch.
Tiezhu Yang (10):
objtool: Handle various symbol types of rodata
objtool: Handle special cases of dead end insn
objtool: Handle different entry size of rodata
objtool: Handle PC relative relocation type
objtool: Handle unreachable entry of rodata
objtool: Handle unsorted table offset of rodata
objtool/LoongArch: Get each table size of rodata
objtool/LoongArch: Add support for switch table
objtool/LoongArch: Add support for goto table
LoongArch: Enable jump table for objtool
arch/loongarch/Kconfig | 3 +
arch/loongarch/Makefile | 4 +
tools/objtool/arch/loongarch/special.c | 156 ++++++++++++++++++++++++-
tools/objtool/check.c | 68 ++++++++++-
tools/objtool/include/objtool/check.h | 1 +
5 files changed, 226 insertions(+), 6 deletions(-)
--
2.42.0
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v3 01/10] objtool: Handle various symbol types of rodata
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
2024-11-19 6:56 ` [PATCH v3 02/10] objtool: Handle special cases of dead end insn Tiezhu Yang
` (9 subsequent siblings)
10 siblings, 1 reply; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
In the relocation section ".rela.rodata" of each .o file compiled with
LoongArch toolchain, there are various symbol types such as STT_NOTYPE,
STT_OBJECT, STT_FUNC in addition to the usual STT_SECTION, it needs to
use reloc symbol offset instead of reloc addend to find the destination
instruction in find_jump_table() and add_jump_table().
This is preparation for later patch on LoongArch, there is no effect for
the other archs with this patch.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/check.c | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 6604f5d038aa..9601235e908d 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2079,6 +2079,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
unsigned int prev_offset = 0;
struct reloc *reloc = table;
struct alternative *alt;
+ unsigned long offset;
/*
* Each @reloc is a switch table relocation which points to the target
@@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
break;
+ if (reloc->sym->type == STT_SECTION) {
+ /* Addend field in the relocation entry associated with the symbol */
+ offset = reloc_addend(reloc);
+ } else {
+ /* The address of the symbol in the relocation entry */
+ offset = reloc->sym->offset;
+ }
+
/* Detect function pointers from contiguous objects: */
- if (reloc->sym->sec == pfunc->sec &&
- reloc_addend(reloc) == pfunc->offset)
+ if (reloc->sym->sec == pfunc->sec && offset == pfunc->offset)
break;
- dest_insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
+ dest_insn = find_insn(file, reloc->sym->sec, offset);
if (!dest_insn)
break;
@@ -2137,6 +2145,7 @@ static struct reloc *find_jump_table(struct objtool_file *file,
{
struct reloc *table_reloc;
struct instruction *dest_insn, *orig_insn = insn;
+ unsigned long offset;
/*
* Backward search using the @first_jump_src links, these help avoid
@@ -2160,7 +2169,16 @@ static struct reloc *find_jump_table(struct objtool_file *file,
table_reloc = arch_find_switch_table(file, insn);
if (!table_reloc)
continue;
- dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc));
+
+ if (table_reloc->sym->type == STT_SECTION) {
+ /* Addend field in the relocation entry associated with the symbol */
+ offset = reloc_addend(table_reloc);
+ } else {
+ /* The address of the symbol in the relocation entry */
+ offset = table_reloc->sym->offset;
+ }
+
+ dest_insn = find_insn(file, table_reloc->sym->sec, offset);
if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func)
continue;
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 02/10] objtool: Handle special cases of dead end insn
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 01/10] objtool: Handle various symbol types of rodata Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
2024-11-19 6:56 ` [PATCH v3 03/10] objtool: Handle different entry size of rodata Tiezhu Yang
` (8 subsequent siblings)
10 siblings, 1 reply; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
There are some "unreachable instruction" objtool warnings when compling
with Clang on LoongArch, this is because the "break" instruction is set
as dead end due to its type is INSN_BUG in decode_instructions() at the
beginning, and it does not set insn->dead_end of the "break" instruction
as false after checking ".rela.discard.reachable" in add_dead_ends(), so
the next instruction of "break" is marked as unreachable.
Actually, it can find the reachable instruction after parsing the section
".rela.discard.reachable", in some cases, the "break" instruction may not
be the first previous instruction with scheduling by Machine Instruction
Scheduler of LLVM, it should find more times and then set insn->dead_end
of the "break" instruction as false.
This is preparation for later patch on LoongArch, there is no effect for
the other archs with this patch.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/check.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 9601235e908d..6607cd56459b 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -711,6 +711,18 @@ static int add_dead_ends(struct objtool_file *file)
}
insn->dead_end = false;
+
+ /* Handle the special cases compiled with Clang on LoongArch */
+ if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
+ reloc->sym->type == STT_SECTION) {
+ while (insn && insn_func(insn)) {
+ insn = prev_insn_same_sym(file, insn);
+ if (insn && insn->dead_end) {
+ insn->dead_end = false;
+ break;
+ }
+ }
+ }
}
return 0;
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 03/10] objtool: Handle different entry size of rodata
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 01/10] objtool: Handle various symbol types of rodata Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 02/10] objtool: Handle special cases of dead end insn Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
` (2 more replies)
2024-11-19 6:56 ` [PATCH v3 04/10] objtool: Handle PC relative relocation type Tiezhu Yang
` (7 subsequent siblings)
10 siblings, 3 replies; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
In the most cases, the entry size of rodata is 8 bytes because the
relocation type is 64 bit, but when compling with Clang on LoongArch,
there exists 32 bit relocation type, the entry size of rodata should
be 4 bytes in this case.
This is preparation for later patch on LoongArch, there is no effect
for the other archs with this patch.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/check.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 6607cd56459b..af824bfd0973 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2092,6 +2092,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
struct reloc *reloc = table;
struct alternative *alt;
unsigned long offset;
+ unsigned long rodata_entry_size;
/*
* Each @reloc is a switch table relocation which points to the target
@@ -2103,8 +2104,15 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
if (reloc != table && reloc == next_table)
break;
+ /* Handle the special cases compiled with Clang on LoongArch */
+ if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
+ reloc_type(reloc) == R_LARCH_32_PCREL)
+ rodata_entry_size = 4;
+ else
+ rodata_entry_size = 8;
+
/* Make sure the table entries are consecutive: */
- if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
+ if (prev_offset && reloc_offset(reloc) != prev_offset + rodata_entry_size)
break;
if (reloc->sym->type == STT_SECTION) {
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 04/10] objtool: Handle PC relative relocation type
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
` (2 preceding siblings ...)
2024-11-19 6:56 ` [PATCH v3 03/10] objtool: Handle different entry size of rodata Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 05/10] objtool: Handle unreachable entry of rodata Tiezhu Yang
` (6 subsequent siblings)
10 siblings, 0 replies; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
When compling with Clang on LoongArch, there exists 32 bit PC relative
relocation type, it needs to get the offset with "S + A - PC" according
to the spec of "ELF for the LoongArch Architecture".
This is preparation for later patch on LoongArch, there is no effect for
the other archs with this patch.
Link: https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/check.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index af824bfd0973..eb4c89501493 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2118,6 +2118,11 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
if (reloc->sym->type == STT_SECTION) {
/* Addend field in the relocation entry associated with the symbol */
offset = reloc_addend(reloc);
+ /* Handle the special cases compiled with Clang on LoongArch */
+ if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
+ reloc_type(reloc) == R_LARCH_32_PCREL)
+ offset = reloc->sym->offset + reloc_addend(reloc) -
+ (reloc_offset(reloc) - reloc_offset(table));
} else {
/* The address of the symbol in the relocation entry */
offset = reloc->sym->offset;
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 05/10] objtool: Handle unreachable entry of rodata
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
` (3 preceding siblings ...)
2024-11-19 6:56 ` [PATCH v3 04/10] objtool: Handle PC relative relocation type Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
2024-11-19 6:56 ` [PATCH v3 06/10] objtool: Handle unsorted table offset " Tiezhu Yang
` (5 subsequent siblings)
10 siblings, 1 reply; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
When compling with Clang on LoongArch, there exists unreachable entry
of rodata which points to a nop instruction after the function return
instruction, this is generated by compiler to fill the non-existent
switch case, just skip the entry when parsing the relocation section
of rodata.
This is preparation for later patch on LoongArch, there is no effect
for the other archs with this patch.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/check.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index eb4c89501493..17df3738e087 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2136,6 +2136,14 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
if (!dest_insn)
break;
+ /* Handle the special cases compiled with Clang on LoongArch */
+ if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
+ reloc->sym->type == STT_SECTION && dest_insn->type == INSN_NOP &&
+ (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)) {
+ prev_offset = reloc_offset(reloc);
+ continue;
+ }
+
/* Make sure the destination is in the same function: */
if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)
break;
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 06/10] objtool: Handle unsorted table offset of rodata
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
` (4 preceding siblings ...)
2024-11-19 6:56 ` [PATCH v3 05/10] objtool: Handle unreachable entry of rodata Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 07/10] objtool/LoongArch: Get each table size " Tiezhu Yang
` (4 subsequent siblings)
10 siblings, 0 replies; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
When compling with Clang on LoongArch, there are unsorted table offset
of rodata if there exist many jump tables, it will get the wrong table
end and find the wrong jump destination instructions in add_jump_table(),
so it needs to check the table size of rodata to break the process when
parsing the relocation section of rodata.
This is preparation for later patch on LoongArch, there is no effect for
the other archs with this patch.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/check.c | 7 +++++++
tools/objtool/include/objtool/check.h | 1 +
2 files changed, 8 insertions(+)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 17df3738e087..cad9e0c21123 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2093,6 +2093,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
struct alternative *alt;
unsigned long offset;
unsigned long rodata_entry_size;
+ unsigned long rodata_table_size = insn->table_size;
/*
* Each @reloc is a switch table relocation which points to the target
@@ -2104,6 +2105,12 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
if (reloc != table && reloc == next_table)
break;
+ /* Handle the special cases compiled with Clang on LoongArch */
+ if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
+ reloc->sym->type == STT_SECTION && reloc != table &&
+ reloc_offset(reloc) == reloc_offset(table) + rodata_table_size)
+ break;
+
/* Handle the special cases compiled with Clang on LoongArch */
if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
reloc_type(reloc) == R_LARCH_32_PCREL)
diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/objtool/check.h
index daa46f1f0965..11aafcc7b0c7 100644
--- a/tools/objtool/include/objtool/check.h
+++ b/tools/objtool/include/objtool/check.h
@@ -46,6 +46,7 @@ struct instruction {
struct section *sec;
unsigned long offset;
unsigned long immediate;
+ unsigned long table_size;
u8 len;
u8 prev_len;
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 07/10] objtool/LoongArch: Get each table size of rodata
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
` (5 preceding siblings ...)
2024-11-19 6:56 ` [PATCH v3 06/10] objtool: Handle unsorted table offset " Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 08/10] objtool/LoongArch: Add support for switch table Tiezhu Yang
` (3 subsequent siblings)
10 siblings, 0 replies; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
When compling with Clang on LoongArch, there are unsorted table offset
of rodata if there exist many jump tables, it will get the wrong table
end and find the wrong jump destination instructions in add_jump_table().
Sort the rodata table offset by parsing ".rela.discard.tablejump_annotate"
and then get each table size of rodata corresponded with each table jump
instruction, it is used to check the table end and will break the process
when parsing ".rela.rodata" to avoid getting the wrong jump destination
instructions.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/arch/loongarch/special.c | 72 ++++++++++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c
index 9bba1e9318e0..454bd01226a4 100644
--- a/tools/objtool/arch/loongarch/special.c
+++ b/tools/objtool/arch/loongarch/special.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <objtool/special.h>
+#include <objtool/warn.h>
bool arch_support_alt_relocation(struct special_alt *special_alt,
struct instruction *insn,
@@ -8,6 +9,77 @@ bool arch_support_alt_relocation(struct special_alt *special_alt,
return false;
}
+struct table_info {
+ struct list_head jump_info;
+ unsigned long insn_offset;
+ unsigned long rodata_offset;
+};
+
+static void get_rodata_table_size_by_table_annotate(struct objtool_file *file,
+ struct instruction *insn)
+{
+ struct section *rsec;
+ struct reloc *reloc;
+ struct list_head table_list;
+ struct table_info *orig_table;
+ struct table_info *next_table;
+ unsigned long tmp_insn_offset;
+ unsigned long tmp_rodata_offset;
+
+ rsec = find_section_by_name(file->elf, ".rela.discard.tablejump_annotate");
+ if (!rsec)
+ return;
+
+ INIT_LIST_HEAD(&table_list);
+
+ for_each_reloc(rsec, reloc) {
+ if (reloc->sym->type != STT_SECTION)
+ return;
+
+ orig_table = malloc(sizeof(struct table_info));
+ if (!orig_table) {
+ WARN("malloc failed");
+ return;
+ }
+
+ orig_table->insn_offset = reloc_addend(reloc);
+ reloc++;
+ orig_table->rodata_offset = reloc_addend(reloc);
+
+ list_add_tail(&orig_table->jump_info, &table_list);
+
+ if (reloc_idx(reloc) + 1 == sec_num_entries(rsec))
+ break;
+ }
+
+ list_for_each_entry(orig_table, &table_list, jump_info) {
+ next_table = list_next_entry(orig_table, jump_info);
+ list_for_each_entry_from(next_table, &table_list, jump_info) {
+ if (next_table->rodata_offset < orig_table->rodata_offset) {
+ tmp_insn_offset = next_table->insn_offset;
+ tmp_rodata_offset = next_table->rodata_offset;
+ next_table->insn_offset = orig_table->insn_offset;
+ next_table->rodata_offset = orig_table->rodata_offset;
+ orig_table->insn_offset = tmp_insn_offset;
+ orig_table->rodata_offset = tmp_rodata_offset;
+ }
+ }
+ }
+
+ list_for_each_entry(orig_table, &table_list, jump_info) {
+ if (insn->offset == orig_table->insn_offset) {
+ next_table = list_next_entry(orig_table, jump_info);
+ if (&next_table->jump_info == &table_list)
+ break;
+
+ while (next_table->rodata_offset == orig_table->rodata_offset)
+ next_table = list_next_entry(next_table, jump_info);
+
+ insn->table_size = next_table->rodata_offset - orig_table->rodata_offset;
+ }
+ }
+}
+
struct reloc *arch_find_switch_table(struct objtool_file *file,
struct instruction *insn)
{
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 08/10] objtool/LoongArch: Add support for switch table
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
` (6 preceding siblings ...)
2024-11-19 6:56 ` [PATCH v3 07/10] objtool/LoongArch: Get each table size " Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 09/10] objtool/LoongArch: Add support for goto table Tiezhu Yang
` (2 subsequent siblings)
10 siblings, 0 replies; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
The objtool program needs to analysis the control flow of each object file
generated by compiler toolchain, it needs to know all the locations that a
branch instruction may jump into, if a jump table is used, objtool has to
correlate the jump instruction with the table.
On x86 which is the only port supported by objtool before LoongArch, there
is a relocation on the jump instruction and to the table directly. But on
LoongArch, the relocation is on some kind of instruction prior to the jump
instruction, and also with scheduling it is not easy to tell the offset of
that instruction from the jump instruction. Furthermore, because LoongArch
has -fsection-anchors (often enabled at -O1 or above) the relocation may
actually points to a section anchor instead of the table itself.
The good news is that after continuous analysis and discussions, at last a
GCC patch "LoongArch: Add support to annotate tablejump" and a Clang patch
"[LoongArch] Add options for annotate tablejump" have been merged into the
upstream mainline, the compiler changes make life much easier for switch
table support of objtool on LoongArch.
By now, there is an additional section ".discard.tablejump_annotate" to
store the jump info as pairs of addresses, each pair contains the address
of jump instruction and the address of jump table.
In order to find switch table, it is easy to parse the relocation section
".rela.discard.tablejump_annotate" to get table_sec and table_offset, the
rest process is somehow like x86.
Link: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=0ee028f55640
Link: https://github.com/llvm/llvm-project/commit/4c2c17756739
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/arch/loongarch/special.c | 60 +++++++++++++++++++++++++-
1 file changed, 59 insertions(+), 1 deletion(-)
diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c
index 454bd01226a4..366517deb35b 100644
--- a/tools/objtool/arch/loongarch/special.c
+++ b/tools/objtool/arch/loongarch/special.c
@@ -1,4 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-or-later
+#include <string.h>
#include <objtool/special.h>
#include <objtool/warn.h>
@@ -80,8 +81,65 @@ static void get_rodata_table_size_by_table_annotate(struct objtool_file *file,
}
}
+static struct reloc *find_reloc_by_table_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;
+
+ if (reloc->sym->type == STT_SECTION)
+ offset = reloc_addend(reloc);
+ else
+ offset = reloc->sym->offset;
+
+ if (insn->offset == offset) {
+ get_rodata_table_size_by_table_annotate(file, insn);
+ 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_table_annotate(file, insn);
+ if (!annotate_reloc)
+ return NULL;
+
+ table_sec = annotate_reloc->sym->sec;
+ if (annotate_reloc->sym->type == STT_SECTION)
+ table_offset = reloc_addend(annotate_reloc);
+ else
+ 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
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 09/10] objtool/LoongArch: Add support for goto table
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
` (7 preceding siblings ...)
2024-11-19 6:56 ` [PATCH v3 08/10] objtool/LoongArch: Add support for switch table Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 10/10] LoongArch: Enable jump table for objtool Tiezhu Yang
2024-11-19 8:48 ` [PATCH v3 00/10] Add jump table support for objtool on LoongArch Huacai Chen
10 siblings, 0 replies; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
The objtool program needs to analysis the control flow of each object file
generated by compiler toolchain, it needs to know all the locations that a
branch instruction may jump into, if a jump table is used, objtool has to
correlate the jump instruction with the table.
On x86 which is the only port supported by objtool before LoongArch, there
is a relocation on the jump instruction and to the table directly. But on
LoongArch, the relocation is on some kind of instruction prior to the jump
instruction, and also with scheduling it is not easy to tell the offset of
that instruction from the jump instruction. Furthermore, because LoongArch
has -fsection-anchors (often enabled at -O1 or above) the relocation may
actually points to a section anchor instead of the table itself.
For the jump table of switch cases, a GCC patch "LoongArch: Add support to
annotate tablejump" and a Clang patch "[LoongArch] Add options for annotate
tablejump" have been merged into the upstream mainline, it can parse the
additional section ".discard.tablejump_annotate" which stores the jump info
as pairs of addresses, each pair contains the address of jump instruction
and the address of jump table.
For the jump table of computed gotos, it is indeed not easy to implement
in the compiler, especially if there is more than one computed goto in a
function such as ___bpf_prog_run(). objdump kernel/bpf/core.o shows that
there are many table jump instructions in ___bpf_prog_run(), but there are
no relocations on the table jump instructions and to the table directly on
LoongArch.
Without the help of compiler, in order to figure out the address of goto
table for the special case of ___bpf_prog_run(), since the instruction
sequence is relatively single and stable, it makes sense to add a helper
find_reloc_of_rodata_c_jump_table() to find the relocation which points
to the section ".rodata..c_jump_table".
If find_reloc_by_table_annotate() failed, it means there is no relocation
info of switch table address in ".rela.discard.tablejump_annotate", then
objtool may find the relocation info of goto table ".rodata..c_jump_table"
with find_reloc_of_rodata_c_jump_table().
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
tools/objtool/arch/loongarch/special.c | 28 ++++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/loongarch/special.c
index 366517deb35b..c5f4217df3a7 100644
--- a/tools/objtool/arch/loongarch/special.c
+++ b/tools/objtool/arch/loongarch/special.c
@@ -114,6 +114,27 @@ static struct reloc *find_reloc_by_table_annotate(struct objtool_file *file,
return NULL;
}
+static struct reloc *find_reloc_of_rodata_c_jump_table(struct section *sec,
+ unsigned long offset)
+{
+ struct section *rsec;
+ struct reloc *reloc;
+
+ rsec = sec->rsec;
+ if (!rsec)
+ return NULL;
+
+ for_each_reloc(rsec, reloc) {
+ if (reloc_offset(reloc) > offset)
+ break;
+
+ if (!strncmp(reloc->sym->sec->name, ".rodata..c_jump_table", 21))
+ return reloc;
+ }
+
+ return NULL;
+}
+
struct reloc *arch_find_switch_table(struct objtool_file *file,
struct instruction *insn)
{
@@ -123,8 +144,11 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
unsigned long table_offset;
annotate_reloc = find_reloc_by_table_annotate(file, insn);
- if (!annotate_reloc)
- return NULL;
+ if (!annotate_reloc) {
+ annotate_reloc = find_reloc_of_rodata_c_jump_table(insn->sec, insn->offset);
+ if (!annotate_reloc)
+ return NULL;
+ }
table_sec = annotate_reloc->sym->sec;
if (annotate_reloc->sym->type == STT_SECTION)
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v3 10/10] LoongArch: Enable jump table for objtool
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
` (8 preceding siblings ...)
2024-11-19 6:56 ` [PATCH v3 09/10] objtool/LoongArch: Add support for goto table Tiezhu Yang
@ 2024-11-19 6:56 ` Tiezhu Yang
2024-11-19 8:48 ` [PATCH v3 00/10] Add jump table support for objtool on LoongArch Huacai Chen
10 siblings, 0 replies; 19+ messages in thread
From: Tiezhu Yang @ 2024-11-19 6:56 UTC (permalink / raw)
To: Huacai Chen, Josh Poimboeuf, Peter Zijlstra; +Cc: loongarch, linux-kernel
For now, it is time to remove -fno-jump-tables to enable jump table for
objtool if the compiler has -mannotate-tablejump, otherwise it is better
to remain -fno-jump-tables to keep compatibility with the older compilers.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
arch/loongarch/Kconfig | 3 +++
arch/loongarch/Makefile | 4 ++++
2 files changed, 7 insertions(+)
diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index bb35c34f86d2..500ee9b2cd88 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -284,6 +284,9 @@ config AS_HAS_LBT_EXTENSION
config AS_HAS_LVZ_EXTENSION
def_bool $(as-instr,hvcl 0)
+config CC_HAS_ANNOTATE_TABLEJUMP
+ def_bool $(cc-option,-mannotate-tablejump)
+
menu "Kernel type and options"
source "kernel/Kconfig.hz"
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index ae3f80622f4c..6b84e633b8ce 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -101,8 +101,12 @@ KBUILD_AFLAGS += $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)
KBUILD_CFLAGS += $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub)
ifdef CONFIG_OBJTOOL
+ifdef CONFIG_CC_HAS_ANNOTATE_TABLEJUMP
+KBUILD_CFLAGS += $(call cc-option,-mannotate-tablejump)
+else
KBUILD_CFLAGS += -fno-jump-tables
endif
+endif
KBUILD_RUSTFLAGS += --target=loongarch64-unknown-none-softfloat
KBUILD_RUSTFLAGS_KERNEL += -Zdirect-access-external-data=yes
--
2.42.0
^ permalink raw reply related [flat|nested] 19+ messages in thread
* Re: [PATCH v3 00/10] Add jump table support for objtool on LoongArch
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
` (9 preceding siblings ...)
2024-11-19 6:56 ` [PATCH v3 10/10] LoongArch: Enable jump table for objtool Tiezhu Yang
@ 2024-11-19 8:48 ` Huacai Chen
10 siblings, 0 replies; 19+ messages in thread
From: Huacai Chen @ 2024-11-19 8:48 UTC (permalink / raw)
To: Tiezhu Yang, Jinyang He
Cc: Josh Poimboeuf, Peter Zijlstra, loongarch, linux-kernel
Hi, Jinyang,
If you have time please review this series, thank you.
Huacai
On Tue, Nov 19, 2024 at 2:57 PM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
>
> This series is based on 6.12-rc7, tested with the latest upstream
> mainline Binutils, GCC and Clang, most of the patches are aim to
> handle the special cases compiled with Clang on LoongArch.
>
> Tiezhu Yang (10):
> objtool: Handle various symbol types of rodata
> objtool: Handle special cases of dead end insn
> objtool: Handle different entry size of rodata
> objtool: Handle PC relative relocation type
> objtool: Handle unreachable entry of rodata
> objtool: Handle unsorted table offset of rodata
> objtool/LoongArch: Get each table size of rodata
> objtool/LoongArch: Add support for switch table
> objtool/LoongArch: Add support for goto table
> LoongArch: Enable jump table for objtool
>
> arch/loongarch/Kconfig | 3 +
> arch/loongarch/Makefile | 4 +
> tools/objtool/arch/loongarch/special.c | 156 ++++++++++++++++++++++++-
> tools/objtool/check.c | 68 ++++++++++-
> tools/objtool/include/objtool/check.h | 1 +
> 5 files changed, 226 insertions(+), 6 deletions(-)
>
> --
> 2.42.0
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v3 01/10] objtool: Handle various symbol types of rodata
2024-11-19 6:56 ` [PATCH v3 01/10] objtool: Handle various symbol types of rodata Tiezhu Yang
@ 2024-11-19 10:47 ` Jinyang He
0 siblings, 0 replies; 19+ messages in thread
From: Jinyang He @ 2024-11-19 10:47 UTC (permalink / raw)
To: Tiezhu Yang, Huacai Chen, Josh Poimboeuf, Peter Zijlstra
Cc: loongarch, linux-kernel
On 2024-11-19 14:56, Tiezhu Yang wrote:
> In the relocation section ".rela.rodata" of each .o file compiled with
> LoongArch toolchain, there are various symbol types such as STT_NOTYPE,
> STT_OBJECT, STT_FUNC in addition to the usual STT_SECTION, it needs to
> use reloc symbol offset instead of reloc addend to find the destination
> instruction in find_jump_table() and add_jump_table().
>
> This is preparation for later patch on LoongArch, there is no effect for
> the other archs with this patch.
Which patch it prepares for? Please merge some patches if it is not
independent. Otherwise I cannot find what problem it solves.
According to the commit message I think it solve some problems independent?
>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
> tools/objtool/check.c | 26 ++++++++++++++++++++++----
> 1 file changed, 22 insertions(+), 4 deletions(-)
>
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index 6604f5d038aa..9601235e908d 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -2079,6 +2079,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
> unsigned int prev_offset = 0;
> struct reloc *reloc = table;
> struct alternative *alt;
> + unsigned long offset;
>
> /*
> * Each @reloc is a switch table relocation which points to the target
> @@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
> if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
> break;
>
> + if (reloc->sym->type == STT_SECTION) {
> + /* Addend field in the relocation entry associated with the symbol */
> + offset = reloc_addend(reloc);
> + } else {
> + /* The address of the symbol in the relocation entry */
> + offset = reloc->sym->offset;
> + }
Generally rela relocations use S + A.
> +
> /* Detect function pointers from contiguous objects: */
> - if (reloc->sym->sec == pfunc->sec &&
> - reloc_addend(reloc) == pfunc->offset)
> + if (reloc->sym->sec == pfunc->sec && offset == pfunc->offset)
> break;
>
> - dest_insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
> + dest_insn = find_insn(file, reloc->sym->sec, offset);
> if (!dest_insn)
> break;
>
> @@ -2137,6 +2145,7 @@ static struct reloc *find_jump_table(struct objtool_file *file,
> {
> struct reloc *table_reloc;
> struct instruction *dest_insn, *orig_insn = insn;
> + unsigned long offset;
>
> /*
> * Backward search using the @first_jump_src links, these help avoid
> @@ -2160,7 +2169,16 @@ static struct reloc *find_jump_table(struct objtool_file *file,
> table_reloc = arch_find_switch_table(file, insn);
> if (!table_reloc)
> continue;
> - dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc));
> +
> + if (table_reloc->sym->type == STT_SECTION) {
> + /* Addend field in the relocation entry associated with the symbol */
> + offset = reloc_addend(table_reloc);
> + } else {
> + /* The address of the symbol in the relocation entry */
> + offset = table_reloc->sym->offset;
> + }
> +
> + dest_insn = find_insn(file, table_reloc->sym->sec, offset);
> if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func)
> continue;
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v3 02/10] objtool: Handle special cases of dead end insn
2024-11-19 6:56 ` [PATCH v3 02/10] objtool: Handle special cases of dead end insn Tiezhu Yang
@ 2024-11-19 10:47 ` Jinyang He
0 siblings, 0 replies; 19+ messages in thread
From: Jinyang He @ 2024-11-19 10:47 UTC (permalink / raw)
To: Tiezhu Yang, Huacai Chen, Josh Poimboeuf, Peter Zijlstra
Cc: loongarch, linux-kernel
On 2024-11-19 14:56, Tiezhu Yang wrote:
> There are some "unreachable instruction" objtool warnings when compling
> with Clang on LoongArch, this is because the "break" instruction is set
> as dead end due to its type is INSN_BUG in decode_instructions() at the
> beginning, and it does not set insn->dead_end of the "break" instruction
> as false after checking ".rela.discard.reachable" in add_dead_ends(), so
> the next instruction of "break" is marked as unreachable.
>
> Actually, it can find the reachable instruction after parsing the section
> ".rela.discard.reachable", in some cases, the "break" instruction may not
> be the first previous instruction with scheduling by Machine Instruction
> Scheduler of LLVM, it should find more times and then set insn->dead_end
> of the "break" instruction as false.
>
> This is preparation for later patch on LoongArch, there is no effect for
> the other archs with this patch.
>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
> tools/objtool/check.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index 9601235e908d..6607cd56459b 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -711,6 +711,18 @@ static int add_dead_ends(struct objtool_file *file)
> }
>
> insn->dead_end = false;
> +
> + /* Handle the special cases compiled with Clang on LoongArch */
> + if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
> + reloc->sym->type == STT_SECTION) {
> + while (insn && insn_func(insn)) {
> + insn = prev_insn_same_sym(file, insn);
> + if (insn && insn->dead_end) {
> + insn->dead_end = false;
> + break;
> + }
> + }
> + }
Does it cancel the previous instruction which has insn->dead_end?
Why the previous, and why just for LoongArch?
IMO if we annotate reachable, we should cancel all instructions which
are from the sym head to current insn, not only previous. Otherwise,
why it is previous but not the previous of previous.
> }
>
> return 0;
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v3 03/10] objtool: Handle different entry size of rodata
2024-11-19 6:56 ` [PATCH v3 03/10] objtool: Handle different entry size of rodata Tiezhu Yang
@ 2024-11-19 10:47 ` Jinyang He
2024-11-21 21:33 ` kernel test robot
2024-11-22 1:24 ` kernel test robot
2 siblings, 0 replies; 19+ messages in thread
From: Jinyang He @ 2024-11-19 10:47 UTC (permalink / raw)
To: Tiezhu Yang, Huacai Chen, Josh Poimboeuf, Peter Zijlstra
Cc: loongarch, linux-kernel
On 2024-11-19 14:56, Tiezhu Yang wrote:
> In the most cases, the entry size of rodata is 8 bytes because the
> relocation type is 64 bit, but when compling with Clang on LoongArch,
> there exists 32 bit relocation type, the entry size of rodata should
> be 4 bytes in this case.
>
> This is preparation for later patch on LoongArch, there is no effect
> for the other archs with this patch.
>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
> tools/objtool/check.c | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index 6607cd56459b..af824bfd0973 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -2092,6 +2092,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
> struct reloc *reloc = table;
> struct alternative *alt;
> unsigned long offset;
> + unsigned long rodata_entry_size;
>
> /*
> * Each @reloc is a switch table relocation which points to the target
> @@ -2103,8 +2104,15 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
> if (reloc != table && reloc == next_table)
> break;
>
> + /* Handle the special cases compiled with Clang on LoongArch */
> + if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
> + reloc_type(reloc) == R_LARCH_32_PCREL)
It is arch-specific. I'd prefer to rewrite an interface in arch directory
to get the entry size in rodata by arch different relocation types.
And I think this patch should merge to other patches e.g. [07/10].
> + rodata_entry_size = 4;
> + else
> + rodata_entry_size = 8;
> +
> /* Make sure the table entries are consecutive: */
> - if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
> + if (prev_offset && reloc_offset(reloc) != prev_offset + rodata_entry_size)
> break;
>
> if (reloc->sym->type == STT_SECTION) {
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v3 05/10] objtool: Handle unreachable entry of rodata
2024-11-19 6:56 ` [PATCH v3 05/10] objtool: Handle unreachable entry of rodata Tiezhu Yang
@ 2024-11-19 10:47 ` Jinyang He
2024-11-21 12:56 ` Huacai Chen
0 siblings, 1 reply; 19+ messages in thread
From: Jinyang He @ 2024-11-19 10:47 UTC (permalink / raw)
To: Tiezhu Yang, Huacai Chen, Josh Poimboeuf, Peter Zijlstra
Cc: loongarch, linux-kernel
On 2024-11-19 14:56, Tiezhu Yang wrote:
> When compling with Clang on LoongArch, there exists unreachable entry
> of rodata which points to a nop instruction after the function return
> instruction, this is generated by compiler to fill the non-existent
> switch case, just skip the entry when parsing the relocation section
> of rodata.
>
> This is preparation for later patch on LoongArch, there is no effect
> for the other archs with this patch.
>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
> tools/objtool/check.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index eb4c89501493..17df3738e087 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -2136,6 +2136,14 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
> if (!dest_insn)
> break;
>
> + /* Handle the special cases compiled with Clang on LoongArch */
> + if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
> + reloc->sym->type == STT_SECTION && dest_insn->type == INSN_NOP &&
Why it points to NOP, and whether it could be some other instruction
(e.g. break)?
It seems fragile. I think it should fix in Clang.
> + (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)) {
> + prev_offset = reloc_offset(reloc);
> + continue;
> + }
> +
> /* Make sure the destination is in the same function: */
> if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)
> break;
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v3 05/10] objtool: Handle unreachable entry of rodata
2024-11-19 10:47 ` Jinyang He
@ 2024-11-21 12:56 ` Huacai Chen
0 siblings, 0 replies; 19+ messages in thread
From: Huacai Chen @ 2024-11-21 12:56 UTC (permalink / raw)
To: Jinyang He, wanglei
Cc: Tiezhu Yang, Josh Poimboeuf, Peter Zijlstra, loongarch,
linux-kernel
Hi, Jinyang,
On Tue, Nov 19, 2024 at 6:47 PM Jinyang He <hejinyang@loongson.cn> wrote:
>
> On 2024-11-19 14:56, Tiezhu Yang wrote:
>
> > When compling with Clang on LoongArch, there exists unreachable entry
> > of rodata which points to a nop instruction after the function return
> > instruction, this is generated by compiler to fill the non-existent
> > switch case, just skip the entry when parsing the relocation section
> > of rodata.
> >
> > This is preparation for later patch on LoongArch, there is no effect
> > for the other archs with this patch.
> >
> > Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> > ---
> > tools/objtool/check.c | 8 ++++++++
> > 1 file changed, 8 insertions(+)
> >
> > diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> > index eb4c89501493..17df3738e087 100644
> > --- a/tools/objtool/check.c
> > +++ b/tools/objtool/check.c
> > @@ -2136,6 +2136,14 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
> > if (!dest_insn)
> > break;
> >
> > + /* Handle the special cases compiled with Clang on LoongArch */
> > + if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
> > + reloc->sym->type == STT_SECTION && dest_insn->type == INSN_NOP &&
> Why it points to NOP, and whether it could be some other instruction
> (e.g. break)?
> It seems fragile. I think it should fix in Clang.
As discussed with Tiezhu and the developer of Clang compiler (Wang
Lei), this is just the implementation of Clang, the non-existent
switch case is filled in the jump table, the table entry points to a
position after the function return instruction, this is not an issue
for Clang.
Currently, objdump the object files of Linux kernel shows that the
position is a nop instruction.
But it is not necessary to check the position is a nop instruction, it
only needs to check whether the position is after the function return
instruction, so "dest_insn->type == INSN_NOP" can be removed. I will
modify it when applying the patch.
Other patches seem to have no fundamental issues.
Huacai
> > + (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)) {
> > + prev_offset = reloc_offset(reloc);
> > + continue;
> > + }
> > +
> > /* Make sure the destination is in the same function: */
> > if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)
> > break;
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v3 03/10] objtool: Handle different entry size of rodata
2024-11-19 6:56 ` [PATCH v3 03/10] objtool: Handle different entry size of rodata Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
@ 2024-11-21 21:33 ` kernel test robot
2024-11-22 1:24 ` kernel test robot
2 siblings, 0 replies; 19+ messages in thread
From: kernel test robot @ 2024-11-21 21:33 UTC (permalink / raw)
To: Tiezhu Yang, Huacai Chen, Josh Poimboeuf, Peter Zijlstra
Cc: llvm, oe-kbuild-all, loongarch, linux-kernel
Hi Tiezhu,
kernel test robot noticed the following build errors:
[auto build test ERROR on v6.12-rc7]
[also build test ERROR on next-20241121]
[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/Tiezhu-Yang/objtool-Handle-various-symbol-types-of-rodata/20241121-131412
base: v6.12-rc7
patch link: https://lore.kernel.org/r/20241119065655.21123-4-yangtiezhu%40loongson.cn
patch subject: [PATCH v3 03/10] objtool: Handle different entry size of rodata
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20241122/202411220519.OK6FYVwq-lkp@intel.com/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241122/202411220519.OK6FYVwq-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202411220519.OK6FYVwq-lkp@intel.com/
All errors (new ones prefixed by >>):
>> check.c:2109:28: error: use of undeclared identifier 'R_LARCH_32_PCREL'
2109 | reloc_type(reloc) == R_LARCH_32_PCREL)
| ^
1 error generated.
make[5]: *** [tools/build/Makefile.build:105: tools/objtool/check.o] Error 1
make[5]: *** Waiting for unfinished jobs....
make[4]: *** [Makefile:70: tools/objtool/objtool-in.o] Error 2
make[3]: *** [Makefile:73: objtool] Error 2
make[2]: *** [Makefile:1370: tools/objtool] Error 2
In file included from arch/x86/kernel/asm-offsets.c:14:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:21:
In file included from include/linux/mm.h:2213:
include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
518 | return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
| ~~~~~~~~~~~ ^ ~~~
1 warning generated.
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:224: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:224: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v3 03/10] objtool: Handle different entry size of rodata
2024-11-19 6:56 ` [PATCH v3 03/10] objtool: Handle different entry size of rodata Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
2024-11-21 21:33 ` kernel test robot
@ 2024-11-22 1:24 ` kernel test robot
2 siblings, 0 replies; 19+ messages in thread
From: kernel test robot @ 2024-11-22 1:24 UTC (permalink / raw)
To: Tiezhu Yang, Huacai Chen, Josh Poimboeuf, Peter Zijlstra
Cc: oe-kbuild-all, loongarch, linux-kernel
Hi Tiezhu,
kernel test robot noticed the following build errors:
[auto build test ERROR on v6.12-rc7]
[also build test ERROR on next-20241121]
[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/Tiezhu-Yang/objtool-Handle-various-symbol-types-of-rodata/20241121-131412
base: v6.12-rc7
patch link: https://lore.kernel.org/r/20241119065655.21123-4-yangtiezhu%40loongson.cn
patch subject: [PATCH v3 03/10] objtool: Handle different entry size of rodata
config: x86_64-defconfig (https://download.01.org/0day-ci/archive/20241122/202411220727.UfLx4PU8-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.3.0-12) 11.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241122/202411220727.UfLx4PU8-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202411220727.UfLx4PU8-lkp@intel.com/
All errors (new ones prefixed by >>):
check.c: In function 'add_jump_table':
>> check.c:2109:42: error: 'R_LARCH_32_PCREL' undeclared (first use in this function); did you mean 'R_ARC_B22_PCREL'?
2109 | reloc_type(reloc) == R_LARCH_32_PCREL)
| ^~~~~~~~~~~~~~~~
| R_ARC_B22_PCREL
check.c:2109:42: note: each undeclared identifier is reported only once for each function it appears in
make[5]: *** [tools/build/Makefile.build:105: tools/objtool/check.o] Error 1
make[5]: *** Waiting for unfinished jobs....
make[4]: *** [Makefile:70: tools/objtool/objtool-in.o] Error 2
make[3]: *** [Makefile:73: objtool] Error 2
make[2]: *** [Makefile:1370: tools/objtool] Error 2
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:224: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:224: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2024-11-22 1:25 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-19 6:56 [PATCH v3 00/10] Add jump table support for objtool on LoongArch Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 01/10] objtool: Handle various symbol types of rodata Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
2024-11-19 6:56 ` [PATCH v3 02/10] objtool: Handle special cases of dead end insn Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
2024-11-19 6:56 ` [PATCH v3 03/10] objtool: Handle different entry size of rodata Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
2024-11-21 21:33 ` kernel test robot
2024-11-22 1:24 ` kernel test robot
2024-11-19 6:56 ` [PATCH v3 04/10] objtool: Handle PC relative relocation type Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 05/10] objtool: Handle unreachable entry of rodata Tiezhu Yang
2024-11-19 10:47 ` Jinyang He
2024-11-21 12:56 ` Huacai Chen
2024-11-19 6:56 ` [PATCH v3 06/10] objtool: Handle unsorted table offset " Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 07/10] objtool/LoongArch: Get each table size " Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 08/10] objtool/LoongArch: Add support for switch table Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 09/10] objtool/LoongArch: Add support for goto table Tiezhu Yang
2024-11-19 6:56 ` [PATCH v3 10/10] LoongArch: Enable jump table for objtool Tiezhu Yang
2024-11-19 8:48 ` [PATCH v3 00/10] Add jump table support for objtool on LoongArch Huacai Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox