Skip to contentSkip to conversation

objtool: Support implicit addend relocations

PATCH In review Matt Helsley 4 participants 8 messages May 29, 2020 – Jun 2, 2020 objtool

lore
Share & tools
lore
#0 lore raw

cover objtool: Support implicit addend relocations

Future tools that use objtool to process ELF object files may require reading or even modifying relocations with implicit addends. This series cleans up the naming of the relocation struct in objtool, variable names, and function names to be consistent with a more generic use rather than being specific to those applying explicit addends.

Changes since RFC [1]:
* Dropped the second patch and rebased on tip objtool/core with patch[2] from Sami Tolvanen [Josh Poimboeuf]
* Remade the rename patch with a sed script and added script to the commit message to aid backports. [Josh]
* Removed function pointers from last patch. [Josh]
* Added relocation type parameter to elf_create_reloc_section() to match pattern with relocation section rebuilding.

[1] https://lore.kernel.org/lkml/[email protected]
[2] https://lore.kernel.org/lkml/[email protected]/

Matt Helsley (2):
objtool: Rename rela to reloc
objtool: Add support for relocations without addends

 tools/objtool/arch.h            |   2 +-
 tools/objtool/arch/x86/decode.c |   2 +-
 tools/objtool/check.c           | 190 +++++++++++------------
 tools/objtool/check.h           |   2 +-
 tools/objtool/elf.c             | 262 +++++++++++++++++++++++---------
 tools/objtool/elf.h             |  27 ++--
 tools/objtool/orc_gen.c         |  44 +++---
 tools/objtool/special.c         |  22 +--
 8 files changed, 333 insertions(+), 218 deletions(-)

base-commit: 0decf1f8de919782b152daf9c991967a2bac54f0
prerequisite-patch-id: 83ac956b6b4a769cafcdc4abaea4a3bcc3136d6d

Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 objtool: Rename rela to reloc

Before supporting additional relocation types rename the relevant types and functions from "rela" to "reloc". This work be done with the following regex:

	sed -i -e 's/struct rela/struct reloc/g' \
		-e 's/\([_\*]\)rela\(s\{0,1\}\)/\1reloc\2/g' \
		-e 's/tmprela\(s\{0,1\}\)/tmpreloc\1/g' \
		-e 's/relasec/relocsec/g' \
		-e 's/rela_list/reloc_list/g' \
		-e 's/rela_hash/reloc_hash/g' \
		-e 's/add_rela/add_reloc/g' \
		-e 's/rela->/reloc->/g' \
		-e '/rela[,\.]/{ s/\([^\.>]\)rela\([\.,]\)/\1reloc\2/g ; }' \
		-e 's/rela =/reloc =/g' \
		-e 's/relas =/relocs =/g' \
		-e 's/relas\[/relocs[/g' \
		-e 's/relaname =/relocname =/g' \
		-e 's/= rela\;/= reloc\;/g' \
		-e 's/= relas\;/= relocs\;/g' \
		-e 's/= relaname\;/= relocname\;/g' \
		-e 's/, rela)/, reloc)/g' \
		-e 's/, relaname/, relocname/g' \
		-e 's/sec->rela/sec->reloc/g' \
		-e 's/(\(!\{0,1\}\)rela/(\1reloc/g' \
		arch.h \
		arch/x86/decode.c  \
		check.c \
		check.h \
		elf.c \
		elf.h \
		orc_gen.c \
		special.c \

Notable exceptions which complicate the regex include gelf_* library calls and standard/expected section names which still use "rela" because they encode the type of relocation expected. Also, keep "rela" in the struct because it encodes a specific type of relocation we currently expect.

It will eventually turn into a member of an anonymous union when a susequent patch adds implicit addend, or "rel", relocation support.

---
 tools/objtool/arch.h            |   2 +-
 tools/objtool/arch/x86/decode.c |   2 +-
 tools/objtool/check.c           | 190 ++++++++++++++++----------------
 tools/objtool/check.h           |   2 +-
 tools/objtool/elf.c             | 134 +++++++++++-----------
 tools/objtool/elf.h             |  22 ++--
 tools/objtool/orc_gen.c         |  44 ++++----
 tools/objtool/special.c         |  22 ++--
 8 files changed, 209 insertions(+), 209 deletions(-)
tools/objtool/arch.h +1 -1
index eda15a5a285e..d0969a9328c2 100644
@@ -82,6 +82,6 @@ bool arch_callee_saved_reg(unsigned char reg);
 
 unsigned long arch_jump_destination(struct instruction *insn);
 
-unsigned long arch_dest_rela_offset(int addend);
+unsigned long arch_dest_reloc_offset(int addend);
 
 #endif /* _ARCH_H */
tools/objtool/arch/x86/decode.c +1 -1
index 4b504fc90bbb..fe83d4c92825 100644
@@ -67,7 +67,7 @@ bool arch_callee_saved_reg(unsigned char reg)
 	}
 }
 
-unsigned long arch_dest_rela_offset(int addend)
+unsigned long arch_dest_reloc_offset(int addend)
 {
 	return addend + 4;
 }
tools/objtool/check.c +95 -95

474 lines not on this page · raw message

tools/objtool/check.h +1 -1
index 906b5210f7ca..061aa96e15d3 100644
@@ -37,7 +37,7 @@ struct instruction {
 	struct symbol *call_dest;
 	struct instruction *jump_dest;
 	struct instruction *first_jump_src;
-	struct rela *jump_table;
+	struct reloc *jump_table;
 	struct list_head alts;
 	struct symbol *func;
 	struct list_head stack_ops;
tools/objtool/elf.c +67 -67

270 lines not on this page · raw message

tools/objtool/elf.h +11 -11
index f4fe1d6ea392..6ad759fd778e 100644
@@ -32,8 +32,8 @@ struct section {
 	GElf_Shdr sh;
 	struct rb_root symbol_tree;
 	struct list_head symbol_list;
-	struct list_head rela_list;
-	struct section *base, *rela;
+	struct list_head reloc_list;
+	struct section *base, *reloc;
 	struct symbol *sym;
 	Elf_Data *data;
 	char *name;
@@ -58,7 +58,7 @@ struct symbol {
 	bool uaccess_safe;
 };
 
-struct rela {
+struct reloc {
 	struct list_head list;
 	struct hlist_node hash;
 	GElf_Rela rela;
@@ -82,7 +82,7 @@ struct elf {
 	DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS);
 	DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS);
 	DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS);
-	DECLARE_HASHTABLE(rela_hash, ELF_HASH_BITS);
+	DECLARE_HASHTABLE(reloc_hash, ELF_HASH_BITS);
 };
 
 #define OFFSET_STRIDE_BITS	4
@@ -109,15 +109,15 @@ static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
 	return ol;
 }
 
-static inline u32 rela_hash(struct rela *rela)
+static inline u32 reloc_hash(struct reloc *reloc)
 {
-	return sec_offset_hash(rela->sec, rela->offset);
+	return sec_offset_hash(reloc->sec, reloc->offset);
 }
 
 struct elf *elf_open_read(const char *name, int flags);
 struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
-struct section *elf_create_rela_section(struct elf *elf, struct section *base);
-void elf_add_rela(struct elf *elf, struct rela *rela);
+struct section *elf_create_reloc_section(struct elf *elf, struct section *base);
+void elf_add_reloc(struct elf *elf, struct reloc *reloc);
 int elf_write(const struct elf *elf);
 void elf_close(struct elf *elf);
 
@@ -126,11 +126,11 @@ struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
-struct rela *find_rela_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
-struct rela *find_rela_by_dest_range(const struct elf *elf, struct section *sec,
+struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
+struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
 				     unsigned long offset, unsigned int len);
 struct symbol *find_func_containing(struct section *sec, unsigned long offset);
-int elf_rebuild_rela_section(struct section *sec);
+int elf_rebuild_reloc_section(struct section *sec);
 
 #define for_each_sec(file, sec)						\
 	list_for_each_entry(sec, &file->elf->sections, list)
tools/objtool/orc_gen.c +22 -22

114 lines not on this page · raw message

tools/objtool/special.c +11 -11
index e74e0189de22..7facd5f659d6 100644
@@ -72,7 +72,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
 			 struct section *sec, int idx,
 			 struct special_alt *alt)
 {
-	struct rela *orig_rela, *new_rela;
+	struct reloc *orig_reloc, *new_reloc;
 	unsigned long offset;
 
 	offset = idx * entry->size;
@@ -118,30 +118,30 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
 		}
 	}
 
-	orig_rela = find_rela_by_dest(elf, sec, offset + entry->orig);
-	if (!orig_rela) {
+	orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
+	if (!orig_reloc) {
 		WARN_FUNC("can't find orig rela", sec, offset + entry->orig);
 		return -1;
 	}
-	if (orig_rela->sym->type != STT_SECTION) {
+	if (orig_reloc->sym->type != STT_SECTION) {
 		WARN_FUNC("don't know how to handle non-section rela symbol %s",
-			   sec, offset + entry->orig, orig_rela->sym->name);
+			   sec, offset + entry->orig, orig_reloc->sym->name);
 		return -1;
 	}
 
-	alt->orig_sec = orig_rela->sym->sec;
-	alt->orig_off = orig_rela->addend;
+	alt->orig_sec = orig_reloc->sym->sec;
+	alt->orig_off = orig_reloc->addend;
 
 	if (!entry->group || alt->new_len) {
-		new_rela = find_rela_by_dest(elf, sec, offset + entry->new);
-		if (!new_rela) {
+		new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
+		if (!new_reloc) {
 			WARN_FUNC("can't find new rela",
 				  sec, offset + entry->new);
 			return -1;
 		}
 
-		alt->new_sec = new_rela->sym->sec;
-		alt->new_off = (unsigned int)new_rela->addend;
+		alt->new_sec = new_reloc->sym->sec;
+		alt->new_off = (unsigned int)new_reloc->addend;
 
 		/* _ASM_EXTABLE_EX hack */
 		if (alt->new_off >= 0x7ffffff0)
Signature · 1 line
2.20.1
#5 lore raw

PATCH 2/2 objtool: Add support for relocations without addends

In reply to Matt Helsley

Currently objtool only collects information about relocations with addends. In recordmcount, which we are about to merge into objtool, some supported architectures do not use rela relocations.

---
 tools/objtool/elf.c     | 146 +++++++++++++++++++++++++++++++++++-----
 tools/objtool/elf.h     |   7 +-
 tools/objtool/orc_gen.c |   2 +-
 3 files changed, 135 insertions(+), 20 deletions(-)
tools/objtool/elf.c +129 -17
index c21e8417637b..5034918494b6 100644
@@ -490,6 +490,32 @@ void elf_add_reloc(struct elf *elf, struct reloc *reloc)
 	elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
 }
 
+static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
+{
+	if (!gelf_getrel(sec->data, i, &reloc->rel)) {
+		WARN_ELF("gelf_getrel");
+		return -1;
+	}
+	reloc->type = GELF_R_TYPE(reloc->rel.r_info);
+	reloc->addend = 0;
+	reloc->offset = reloc->rel.r_offset;
+	*symndx = GELF_R_SYM(reloc->rel.r_info);
+	return 0;
+}
+
+static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
+{
+	if (!gelf_getrela(sec->data, i, &reloc->rela)) {
+		WARN_ELF("gelf_getrela");
+		return -1;
+	}
+	reloc->type = 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);
+	return 0;
+}
+
 static int read_relocs(struct elf *elf)
 {
 	struct section *sec;
@@ -499,7 +525,8 @@ static int read_relocs(struct elf *elf)
 	unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
 
 	list_for_each_entry(sec, &elf->sections, list) {
-		if (sec->sh.sh_type != SHT_RELA)
+		if ((sec->sh.sh_type != SHT_RELA) &&
+		    (sec->sh.sh_type != SHT_REL))
 			continue;
 
 		sec->base = find_section_by_index(elf, sec->sh.sh_info);
@@ -519,16 +546,17 @@ static int read_relocs(struct elf *elf)
 				return -1;
 			}
 			memset(reloc, 0, sizeof(*reloc));
-
-			if (!gelf_getrela(sec->data, i, &reloc->rela)) {
-				WARN_ELF("gelf_getrela");
-				return -1;
+			switch(sec->sh.sh_type) {
+			case SHT_REL:
+				if (read_rel_reloc(sec, i, reloc, &symndx))
+					return -1;
+				break;
+			case SHT_RELA:
+				if (read_rela_reloc(sec, i, reloc, &symndx))
+					return -1;
+				break;
+			default: return -1;
 			}
-
-			reloc->type = 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);
 			reloc->sym = find_symbol_by_index(elf, symndx);
 			reloc->sec = sec;
 			if (!reloc->sym) {
@@ -716,7 +744,38 @@ struct section *elf_create_section(struct elf *elf, const char *name,
 	return sec;
 }
 
-struct section *elf_create_reloc_section(struct elf *elf, struct section *base)
+static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
+{
+	char *relocname;
+	struct section *sec;
+
+	relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
+	if (!relocname) {
+		perror("malloc");
+		return NULL;
+	}
+	strcpy(relocname, ".rel");
+	strcat(relocname, base->name);
+
+	sec = elf_create_section(elf, relocname, sizeof(GElf_Rel), 0);
+	free(relocname);
+	if (!sec)
+		return NULL;
+
+	base->reloc = sec;
+	sec->base = base;
+
+	sec->sh.sh_type = SHT_REL;
+	sec->sh.sh_addralign = 8;
+	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
+	sec->sh.sh_info = base->idx;
+	sec->sh.sh_flags = SHF_INFO_LINK;
+
+	return sec;
+}
+
+
+static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
 {
 	char *relocname;
 	struct section *sec;
@@ -746,16 +805,53 @@ struct section *elf_create_reloc_section(struct elf *elf, struct section *base)
 	return sec;
 }
 
-int elf_rebuild_reloc_section(struct section *sec)
+struct section *elf_create_reloc_section(struct elf *elf,
+					 struct section *base,
+					 int reltype)
+{
+	switch(reltype) {
+	case SHT_REL:  return elf_create_rel_reloc_section(elf, base);
+	case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
+	default:       return NULL;
+	}
+}
+
+static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
 {
 	struct reloc *reloc;
-	int nr, idx = 0, size;
-	GElf_Rela *relocs;
+	int idx = 0, size;
+	GElf_Rel *relocs;
 
-	nr = 0;
-	list_for_each_entry(reloc, &sec->reloc_list, list)
-		nr++;
+	/* Allocate a buffer for relocations */
+	size = nr * sizeof(*relocs);
+	relocs = malloc(size);
+	if (!relocs) {
+		perror("malloc");
+		return -1;
+	}
+
+	sec->data->d_buf = relocs;
+	sec->data->d_size = size;
 
+	sec->sh.sh_size = size;
+
+	idx = 0;
+	list_for_each_entry(reloc, &sec->reloc_list, list) {
+		relocs[idx].r_offset = reloc->offset;
+		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		idx++;
+	}
+
+	return 0;
+}
+
+static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
+{
+	struct reloc *reloc;
+	int idx = 0, size;
+	GElf_Rela *relocs;
+
+	/* Allocate a buffer for relocations with addends */
 	size = nr * sizeof(*relocs);
 	relocs = malloc(size);
 	if (!relocs) {
@@ -779,6 +875,22 @@ int elf_rebuild_reloc_section(struct section *sec)
 	return 0;
 }
 
+int elf_rebuild_reloc_section(struct section *sec)
+{
+	struct reloc *reloc;
+	int nr;
+
+	nr = 0;
+	list_for_each_entry(reloc, &sec->reloc_list, list)
+		nr++;
+
+	switch(sec->sh.sh_type) {
+	case SHT_REL:  return elf_rebuild_rel_reloc_section(sec, nr);
+	case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr);
+	default:       return -1;
+	}
+}
+
 int elf_write(const struct elf *elf)
 {
 	struct section *sec;
tools/objtool/elf.h +5 -2
index 6ad759fd778e..78a2db23b8b6 100644
@@ -61,7 +61,10 @@ struct symbol {
 struct reloc {
 	struct list_head list;
 	struct hlist_node hash;
-	GElf_Rela rela;
+	union {
+		GElf_Rela rela;
+		GElf_Rel  rel;
+	};
 	struct section *sec;
 	struct symbol *sym;
 	unsigned int type;
@@ -116,7 +119,7 @@ static inline u32 reloc_hash(struct reloc *reloc)
 
 struct elf *elf_open_read(const char *name, int flags);
 struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
-struct section *elf_create_reloc_section(struct elf *elf, struct section *base);
+struct section *elf_create_reloc_section(struct elf *elf, struct section *base, int reltype);
 void elf_add_reloc(struct elf *elf, struct reloc *reloc);
 int elf_write(const struct elf *elf);
 void elf_close(struct elf *elf);
tools/objtool/orc_gen.c +1 -1
index b538a0408cde..47875e83598b 100644
@@ -181,7 +181,7 @@ int create_orc_sections(struct objtool_file *file)
 	if (!sec)
 		return -1;
 
-	ip_relocsec = elf_create_reloc_section(file->elf, sec);
+	ip_relocsec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
 	if (!ip_relocsec)
 		return -1;
 
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH 1/2] objtool: Rename rela to reloc

In reply to Matt Helsley
Quoted 24 lines · Matt Helsley

Before supporting additional relocation types rename the relevant types and functions from "rela" to "reloc". This work be done with the following regex:

	sed -i -e 's/struct rela/struct reloc/g' \
		-e 's/\([_\*]\)rela\(s\{0,1\}\)/\1reloc\2/g' \
		-e 's/tmprela\(s\{0,1\}\)/tmpreloc\1/g' \
		-e 's/relasec/relocsec/g' \
		-e 's/rela_list/reloc_list/g' \
		-e 's/rela_hash/reloc_hash/g' \
		-e 's/add_rela/add_reloc/g' \
		-e 's/rela->/reloc->/g' \
		-e '/rela[,\.]/{ s/\([^\.>]\)rela\([\.,]\)/\1reloc\2/g ; }' \
		-e 's/rela =/reloc =/g' \
		-e 's/relas =/relocs =/g' \
		-e 's/relas\[/relocs[/g' \
		-e 's/relaname =/relocname =/g' \
		-e 's/= rela\;/= reloc\;/g' \
		-e 's/= relas\;/= relocs\;/g' \
		-e 's/= relaname\;/= relocname\;/g' \
		-e 's/, rela)/, reloc)/g' \
		-e 's/, relaname/, relocname/g' \
		-e 's/sec->rela/sec->reloc/g' \
		-e 's/(\(!\{0,1\}\)rela/(\1reloc/g' \
Quoted 8 lines
		arch.h \
		arch/x86/decode.c  \
		check.c \
		check.h \
		elf.c \
		elf.h \
		orc_gen.c \
		special.c \

Holy regex! Thanks for doing that :-)

I'll run these through testing and merge.

Signature · 1 line
Josh
#3 lore raw

Re: [PATCH 1/2] objtool: Rename rela to reloc

Quoted 34 lines · Josh Poimboeuf
Quoted 24 lines · Matt Helsley

Before supporting additional relocation types rename the relevant types and functions from "rela" to "reloc". This work be done with the following regex:

	sed -i -e 's/struct rela/struct reloc/g' \
		-e 's/\([_\*]\)rela\(s\{0,1\}\)/\1reloc\2/g' \
		-e 's/tmprela\(s\{0,1\}\)/tmpreloc\1/g' \
		-e 's/relasec/relocsec/g' \
		-e 's/rela_list/reloc_list/g' \
		-e 's/rela_hash/reloc_hash/g' \
		-e 's/add_rela/add_reloc/g' \
		-e 's/rela->/reloc->/g' \
		-e '/rela[,\.]/{ s/\([^\.>]\)rela\([\.,]\)/\1reloc\2/g ; }' \
		-e 's/rela =/reloc =/g' \
		-e 's/relas =/relocs =/g' \
		-e 's/relas\[/relocs[/g' \
		-e 's/relaname =/relocname =/g' \
		-e 's/= rela\;/= reloc\;/g' \
		-e 's/= relas\;/= relocs\;/g' \
		-e 's/= relaname\;/= relocname\;/g' \
		-e 's/, rela)/, reloc)/g' \
		-e 's/, relaname/, relocname/g' \
		-e 's/sec->rela/sec->reloc/g' \
		-e 's/(\(!\{0,1\}\)rela/(\1reloc/g' \
Quoted 8 lines
		arch.h \
		arch/x86/decode.c  \
		check.c \
		check.h \
		elf.c \
		elf.h \
		orc_gen.c \
		special.c \
Quoted 1 line

Holy regex! Thanks for doing that :-)

So I was rebasing my future patches and I found a few spots where objtool warning strings and code comments weren't fixed-up to consistent. Here's the new, complete regex -- it includes the original changes and the missed bits (e.g. note the new substitution for @rela comments):

sed -i -e 's/struct rela/struct reloc/g' \
        -e 's/\([_\*]\)rela\(s\{0,1\}\)/\1reloc\2/g' \
        -e 's/tmprela\(s\{0,1\}\)/tmpreloc\1/g' \
        -e 's/relasec/relocsec/g' \
        -e 's/rela_list/reloc_list/g' \
        -e 's/rela_hash/reloc_hash/g' \
        -e 's/add_rela/add_reloc/g' \
        -e 's/rela->/reloc->/g' \
        -e '/rela[,\.]/{ s/\([^\.>]\)rela\([\.,]\)/\1reloc\2/g ; }' \
        -e 's/rela =/reloc =/g' \
        -e 's/relas =/relocs =/g' \
        -e 's/relas\[/relocs[/g' \
        -e 's/relaname =/relocname =/g' \
        -e 's/= rela\;/= reloc\;/g' \
        -e 's/= relas\;/= relocs\;/g' \
        -e 's/= relaname\;/= relocname\;/g' \
        -e 's/, rela)/, reloc)/g' \
        -e 's/\([ @]\)rela\([ "]\)/\1reloc\2/g' \
        -e 's/ rela$/ reloc/g' \
        -e 's/, relaname/, relocname/g' \
        -e 's/sec->rela/sec->reloc/g' \
        -e 's/(\(!\{0,1\}\)rela/(\1reloc/g' \
        arch.h \
        arch/x86/decode.c  \
        check.c \
        check.h \
        elf.c \
        elf.h \
        orc_gen.c \
        special.c

If you want to fixup this commit you could just re-run the regex after applying it but before applying the next commit.

Cheers,
-Matt Helsley

#4 lore raw

Re: [PATCH 1/2] objtool: Rename rela to reloc

Quoted 5 lines · Matt Helsley

So I was rebasing my future patches and I found a few spots where objtool warning strings and code comments weren't fixed-up to consistent. Here's the new, complete regex -- it includes the original changes and the missed bits (e.g. note the new substitution for @rela comments):

Thanks, fixed.

Signature · 1 line
Josh
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] objtool: Add support for relocations without addends

In reply to Matt Helsley

Hi Matt,

Quoted 111 lines · Matt Helsley

Currently objtool only collects information about relocations with addends. In recordmcount, which we are about to merge into objtool, some supported architectures do not use rela relocations.

---
  tools/objtool/elf.c     | 146 +++++++++++++++++++++++++++++++++++-----
  tools/objtool/elf.h     |   7 +-
  tools/objtool/orc_gen.c |   2 +-
  3 files changed, 135 insertions(+), 20 deletions(-)
tools/objtool/elf.c +59 -11
index c21e8417637b..5034918494b6 100644
@@ -490,6 +490,32 @@ void elf_add_reloc(struct elf *elf, struct reloc *reloc)
  	elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
  }
 
+static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
+{
+	if (!gelf_getrel(sec->data, i, &reloc->rel)) {
+		WARN_ELF("gelf_getrel");
+		return -1;
+	}
+	reloc->type = GELF_R_TYPE(reloc->rel.r_info);
+	reloc->addend = 0;
+	reloc->offset = reloc->rel.r_offset;
+	*symndx = GELF_R_SYM(reloc->rel.r_info);
+	return 0;
+}
+
+static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
+{
+	if (!gelf_getrela(sec->data, i, &reloc->rela)) {
+		WARN_ELF("gelf_getrela");
+		return -1;
+	}
+	reloc->type = 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);
+	return 0;
+}
+
  static int read_relocs(struct elf *elf)
  {
  	struct section *sec;
@@ -499,7 +525,8 @@ static int read_relocs(struct elf *elf)
  	unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
 
  	list_for_each_entry(sec, &elf->sections, list) {
-		if (sec->sh.sh_type != SHT_RELA)
+		if ((sec->sh.sh_type != SHT_RELA) &&
+		    (sec->sh.sh_type != SHT_REL))
  			continue;
 
  		sec->base = find_section_by_index(elf, sec->sh.sh_info);
@@ -519,16 +546,17 @@ static int read_relocs(struct elf *elf)
  				return -1;
  			}
  			memset(reloc, 0, sizeof(*reloc));
-
-			if (!gelf_getrela(sec->data, i, &reloc->rela)) {
-				WARN_ELF("gelf_getrela");
-				return -1;
+			switch(sec->sh.sh_type) {
+			case SHT_REL:
+				if (read_rel_reloc(sec, i, reloc, &symndx))
+					return -1;
+				break;
+			case SHT_RELA:
+				if (read_rela_reloc(sec, i, reloc, &symndx))
+					return -1;
+				break;
+			default: return -1;
  			}
-
-			reloc->type = 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);
  			reloc->sym = find_symbol_by_index(elf, symndx);
  			reloc->sec = sec;
  			if (!reloc->sym) {
@@ -716,7 +744,38 @@ struct section *elf_create_section(struct elf *elf, const char *name,
  	return sec;
  }
 
-struct section *elf_create_reloc_section(struct elf *elf, struct section *base)
+static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
+{
+	char *relocname;
+	struct section *sec;
+
+	relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
+	if (!relocname) {
+		perror("malloc");
+		return NULL;
+	}
+	strcpy(relocname, ".rel");
+	strcat(relocname, base->name);
+
+	sec = elf_create_section(elf, relocname, sizeof(GElf_Rel), 0);
+	free(relocname);
+	if (!sec)
+		return NULL;
+
+	base->reloc = sec;
+	sec->base = base;
+

tools/objtool/elf.c struct section *elf_create_section(struct elf *elf, const char *name, · line 777

Quoted 10 lines
tools/objtool/elf.c +10 -0
+	sec->sh.sh_type = SHT_REL;
+	sec->sh.sh_addralign = 8;
+	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
+	sec->sh.sh_info = base->idx;
+	sec->sh.sh_flags = SHF_INFO_LINK;
+
+	return sec;
+}
+
+

Nit: Extra new line.

Otherwise:

Signature · 1 line
Julien Thierry
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] objtool: Add support for relocations without addends

In reply to Matt Helsley
Quoted 6 lines · Matt Helsley

Currently objtool only collects information about relocations with addends. In recordmcount, which we are about to merge into objtool, some supported architectures do not use rela relocations.

---

LGTM, minor nit-pick, checkpatch reports of missing space before '(' in the switch cases. With space issues resolved:

Signature · 1 line
Kamalesh