[PATCH v2 4/8] objtool: introduce function elf_reloc_set_type

From: Thomas Weißschuh
Date: Tue Dec 27 2022 - 11:03:30 EST


An upcoming patch needs to perform validation when setting reloc->type
so introduce a helper to contain this validation.

Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
tools/objtool/check.c | 6 +++---
tools/objtool/elf.c | 11 ++++++++---
tools/objtool/include/objtool/elf.h | 2 ++
3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 4350be739f4f..971ee6826de7 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1355,7 +1355,7 @@ static void annotate_call_site(struct objtool_file *file,
*/
if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) {
if (reloc) {
- reloc->type = R_NONE;
+ elf_reloc_set_type(reloc, R_NONE);
elf_write_reloc(file->elf, reloc);
}

@@ -1384,7 +1384,7 @@ static void annotate_call_site(struct objtool_file *file,
WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
if (opts.mnop) {
if (reloc) {
- reloc->type = R_NONE;
+ elf_reloc_set_type(reloc, R_NONE);
elf_write_reloc(file->elf, reloc);
}

@@ -1863,7 +1863,7 @@ static int handle_jump_alt(struct objtool_file *file,
struct reloc *reloc = insn_reloc(file, orig_insn);

if (reloc) {
- reloc->type = R_NONE;
+ elf_reloc_set_type(reloc, R_NONE);
elf_write_reloc(file->elf, reloc);
}
elf_write_insn(file->elf, orig_insn->sec,
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 9c326efb8cd9..ee355beb0d82 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -555,7 +555,7 @@ int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,

reloc->sec = sec->reloc;
reloc->offset = offset;
- reloc->type = type;
+ elf_reloc_set_type(reloc, type);
reloc->sym = sym;
reloc->addend = addend;

@@ -872,7 +872,7 @@ static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsig
WARN_ELF("gelf_getrel");
return -1;
}
- reloc->type = GELF_R_TYPE(reloc->rel.r_info);
+ elf_reloc_set_type(reloc, GELF_R_TYPE(reloc->rel.r_info));
reloc->addend = 0;
reloc->offset = reloc->rel.r_offset;
*symndx = GELF_R_SYM(reloc->rel.r_info);
@@ -885,7 +885,7 @@ static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsi
WARN_ELF("gelf_getrela");
return -1;
}
- reloc->type = GELF_R_TYPE(reloc->rela.r_info);
+ elf_reloc_set_type(reloc, GELF_R_TYPE(reloc->rela.r_info));
reloc->addend = reloc->rela.r_addend;
reloc->offset = reloc->rela.r_offset;
*symndx = GELF_R_SYM(reloc->rela.r_info);
@@ -1471,3 +1471,8 @@ void elf_close(struct elf *elf)
free(elf->section_data);
free(elf);
}
+
+void elf_reloc_set_type(struct reloc *reloc, int type)
+{
+ reloc->type = type;
+}
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 1c90f0ac0d53..33ec6cf72325 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -83,6 +83,8 @@ struct reloc {
bool jump_table_start;
};

+void elf_reloc_set_type(struct reloc *reloc, int type);
+
#define ELF_HASH_BITS 20

struct elf {

--
2.39.0