2023-05-30 17:29:42

by Josh Poimboeuf

[permalink] [raw]
Subject: [PATCH 10/22] objtool: Add for_each_reloc()

Signed-off-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/check.c | 24 ++++++++++++------------
tools/objtool/include/objtool/elf.h | 6 ++++++
2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 88f54bb1b01c..1355e6fec7d6 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -590,7 +590,7 @@ static int add_dead_ends(struct objtool_file *file)
if (!rsec)
goto reachable;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -625,7 +625,7 @@ static int add_dead_ends(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -1018,7 +1018,7 @@ static void add_ignores(struct objtool_file *file)
if (!rsec)
return;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
switch (reloc->sym->type) {
case STT_FUNC:
func = reloc->sym;
@@ -1259,7 +1259,7 @@ static int add_ignore_alternatives(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -1990,7 +1990,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
* Each @reloc is a switch table relocation which points to the target
* instruction.
*/
- list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
+ for_each_reloc_from(table->sec, reloc) {

/* Check for the end of the table: */
if (reloc != table && reloc->jump_table_start)
@@ -2264,7 +2264,7 @@ static int read_noendbr_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend);
if (!insn) {
WARN("bad .discard.noendbr entry");
@@ -2287,7 +2287,7 @@ static int read_retpoline_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -2323,7 +2323,7 @@ static int read_instr_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -2342,7 +2342,7 @@ static int read_instr_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -2370,7 +2370,7 @@ static int read_validate_unret_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -2398,7 +2398,7 @@ static int read_intra_function_calls(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
unsigned long dest_off;

if (reloc->sym->type != STT_SECTION) {
@@ -4395,7 +4395,7 @@ static int validate_ibt(struct objtool_file *file)
strstr(sec->name, "__patchable_function_entries"))
continue;

- list_for_each_entry(reloc, &sec->rsec->reloc_list, list)
+ for_each_reloc(sec->rsec, reloc)
warnings += validate_ibt_data_reloc(file, reloc);
}

diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 74f63934afd3..a938cb1d4172 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -209,6 +209,12 @@ static inline void mark_sec_changed(struct elf *elf, struct section *sec,
for_each_sec(file, __sec) \
sec_for_each_sym(__sec, sym)

+#define for_each_reloc(rsec, reloc) \
+ list_for_each_entry(reloc, &rsec->reloc_list, list)
+
+#define for_each_reloc_from(rsec, reloc) \
+ list_for_each_entry_from(reloc, &rsec->reloc_list, list)
+
#define OFFSET_STRIDE_BITS 4
#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))
--
2.40.1



Subject: [tip: objtool/core] objtool: Add for_each_reloc()

The following commit has been merged into the objtool/core branch of tip:

Commit-ID: caa4a6b74b405ddaea40e2946cc3983aac96451d
Gitweb: https://git.kernel.org/tip/caa4a6b74b405ddaea40e2946cc3983aac96451d
Author: Josh Poimboeuf <[email protected]>
AuthorDate: Tue, 30 May 2023 10:21:02 -07:00
Committer: Josh Poimboeuf <[email protected]>
CommitterDate: Wed, 07 Jun 2023 10:03:19 -07:00

objtool: Add for_each_reloc()

Link: https://lore.kernel.org/r/dbfcb1037d8b958e52d097b67829c4c6811c24bb.1685464332.git.jpoimboe@kernel.org
Signed-off-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/check.c | 24 ++++++++++++------------
tools/objtool/include/objtool/elf.h | 6 ++++++
2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 3bc97c2..e21138d 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -591,7 +591,7 @@ static int add_dead_ends(struct objtool_file *file)
if (!rsec)
goto reachable;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -626,7 +626,7 @@ reachable:
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -1019,7 +1019,7 @@ static void add_ignores(struct objtool_file *file)
if (!rsec)
return;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
switch (reloc->sym->type) {
case STT_FUNC:
func = reloc->sym;
@@ -1260,7 +1260,7 @@ static int add_ignore_alternatives(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -1991,7 +1991,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
* Each @reloc is a switch table relocation which points to the target
* instruction.
*/
- list_for_each_entry_from(reloc, &table->sec->reloc_list, list) {
+ for_each_reloc_from(table->sec, reloc) {

/* Check for the end of the table: */
if (reloc != table && reloc->jump_table_start)
@@ -2270,7 +2270,7 @@ static int read_noendbr_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend);
if (!insn) {
WARN("bad .discard.noendbr entry");
@@ -2293,7 +2293,7 @@ static int read_retpoline_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -2329,7 +2329,7 @@ static int read_instr_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -2348,7 +2348,7 @@ static int read_instr_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -2376,7 +2376,7 @@ static int read_validate_unret_hints(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
if (reloc->sym->type != STT_SECTION) {
WARN("unexpected relocation symbol type in %s", rsec->name);
return -1;
@@ -2404,7 +2404,7 @@ static int read_intra_function_calls(struct objtool_file *file)
if (!rsec)
return 0;

- list_for_each_entry(reloc, &rsec->reloc_list, list) {
+ for_each_reloc(rsec, reloc) {
unsigned long dest_off;

if (reloc->sym->type != STT_SECTION) {
@@ -4404,7 +4404,7 @@ static int validate_ibt(struct objtool_file *file)
strstr(sec->name, "__patchable_function_entries"))
continue;

- list_for_each_entry(reloc, &sec->rsec->reloc_list, list)
+ for_each_reloc(sec->rsec, reloc)
warnings += validate_ibt_data_reloc(file, reloc);
}

diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 74f6393..a938cb1 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -209,6 +209,12 @@ static inline void mark_sec_changed(struct elf *elf, struct section *sec,
for_each_sec(file, __sec) \
sec_for_each_sym(__sec, sym)

+#define for_each_reloc(rsec, reloc) \
+ list_for_each_entry(reloc, &rsec->reloc_list, list)
+
+#define for_each_reloc_from(rsec, reloc) \
+ list_for_each_entry_from(reloc, &rsec->reloc_list, list)
+
#define OFFSET_STRIDE_BITS 4
#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))