[PATCH v1 5/6] objtool: Add skipped member in struct reloc

From: Tiezhu Yang
Date: Tue Jul 25 2023 - 04:15:37 EST


There exist multiple relocation types in one location, such as a pair of
R_LARCH_ADD32 and R_LARCH_SUB32 in section .rela.discard.unreachable and
.rela.discard.reachable on LoongArch.

Here is an example:

$ readelf -rW init/main.o

Relocation section '.rela.discard.unreachable' at offset 0x3e20 contains 2 entries:
Offset Info Type Symbol's Value Symbol's Name + Addend
0000000000000000 0000000a00000032 R_LARCH_ADD32 0000000000000000 .init.text + 230
0000000000000000 0000001a00000037 R_LARCH_SUB32 0000000000000000 L0^A + 0

Relocation section '.rela.discard.reachable' at offset 0x4a00 contains 6 entries:
Offset Info Type Symbol's Value Symbol's Name + Addend
0000000000000000 0000000a00000032 R_LARCH_ADD32 0000000000000000 .init.text + ae0
0000000000000000 0000002800000037 R_LARCH_SUB32 0000000000000000 L0^A + 0
0000000000000004 0000000a00000032 R_LARCH_ADD32 0000000000000000 .init.text + b88
0000000000000004 0000002b00000037 R_LARCH_SUB32 0000000000000004 L0^A + 0
0000000000000008 0000000200000032 R_LARCH_ADD32 0000000000000000 .text + 23c
0000000000000008 0000002e00000037 R_LARCH_SUB32 0000000000000008 L0^A + 0

In order to silence the related objtool warnings, add skipped member
in struct reloc to record and skip the latter relocation.

Co-developed-by: Jinyang He <hejinyang@xxxxxxxxxxx>
Signed-off-by: Jinyang He <hejinyang@xxxxxxxxxxx>
Co-developed-by: Youling Tang <tangyouling@xxxxxxxxxxx>
Signed-off-by: Youling Tang <tangyouling@xxxxxxxxxxx>
Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
tools/objtool/check.c | 4 ++++
tools/objtool/elf.c | 6 ++++++
tools/objtool/include/objtool/elf.h | 1 +
3 files changed, 11 insertions(+)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 9e5e462..602318e 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -594,6 +594,8 @@ static int add_dead_ends(struct objtool_file *file)
goto reachable;

for_each_reloc(rsec, reloc) {
+ if (reloc->skipped)
+ continue;

if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
@@ -633,6 +635,8 @@ static int add_dead_ends(struct objtool_file *file)
return 0;

for_each_reloc(rsec, reloc) {
+ if (reloc->skipped)
+ continue;

if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index d420b5d..bd950d4 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -975,6 +975,12 @@ static int read_relocs(struct elf *elf)
return -1;
}

+ if (!(strcmp(rsec->name, ".rela.discard.unreachable")) ||
+ !(strcmp(rsec->name, ".rela.discard.reachable"))) {
+ if (reloc->sym->type != STT_SECTION)
+ reloc->skipped = true;
+ }
+
elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
reloc->sym_next_reloc = sym->relocs;
sym->relocs = reloc;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index c532d70..4141837 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -75,6 +75,7 @@ struct reloc {
struct section *sec;
struct symbol *sym;
struct reloc *sym_next_reloc;
+ bool skipped;
};

struct elf {
--
2.1.0