mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: linux-kernel@vger.kernel.org, x86@kernel.org, dvyukov@google.com,
	elver@google.com, andreyknvl@google.com, mark.rutland@arm.com,
	mhelsley@vmware.com, rostedt@goodmis.org, jthierry@redhat.com,
	mbenes@suse.cz, peterz@infradead.org
Subject: [PATCH 6/7] objtool: Provide elf_write_{insn,reloc}()
Date: Thu, 18 Jun 2020 16:44:13 +0200	[thread overview]
Message-ID: <20200618144801.936847099@infradead.org> (raw)
In-Reply-To: <20200618144407.520952071@infradead.org>

This provides infrastructure to rewrite instructions; this is
immediately useful for helping out with KCOV-vs-noinstr, but will
also come in handy for a bunch of variable sized jump-label patches
that are still on ice.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 tools/objtool/elf.c |   50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 tools/objtool/elf.h |    7 ++++++-
 2 files changed, 55 insertions(+), 2 deletions(-)

--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -114,6 +114,53 @@ static int symbol_to_offset(struct rb_no
 	return 0;
 }
 
+int elf_write_insn(struct elf *elf, struct section *sec,
+		   unsigned long offset, unsigned int len,
+		   const char *insn)
+{
+	Elf_Data *data = sec->data;
+
+	if (data->d_type != ELF_T_BYTE || data->d_off) {
+		WARN("write to unexpected data for section: %s", sec->name);
+		return -1;
+	}
+
+	memcpy(data->d_buf + offset, insn, len);
+	elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
+
+	elf->changed = true;
+
+	return 0;
+}
+
+int elf_write_reloc(struct elf *elf, struct reloc *reloc)
+{
+	struct section *sec = reloc->sec;
+
+	if (sec->sh.sh_type == SHT_REL) {
+		reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		reloc->rel.r_offset = reloc->offset;
+
+		if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
+			WARN_ELF("gelf_update_rel");
+			return -1;
+		}
+	} else {
+		reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		reloc->rela.r_addend = reloc->addend;
+		reloc->rela.r_offset = reloc->offset;
+
+		if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
+			WARN_ELF("gelf_update_rela");
+			return -1;
+		}
+	}
+
+	elf->changed = true;
+
+	return 0;
+}
+
 static int symbol_by_offset(const void *key, const struct rb_node *node)
 {
 	const struct symbol *s = rb_entry(node, struct symbol, node);
@@ -563,8 +610,9 @@ static int read_relocs(struct elf *elf)
 				break;
 			default: return -1;
 			}
-			reloc->sym = find_symbol_by_index(elf, symndx);
 			reloc->sec = sec;
+			reloc->idx = i;
+			reloc->sym = find_symbol_by_index(elf, symndx);
 			if (!reloc->sym) {
 				WARN("can't find reloc entry symbol %d for %s",
 				     symndx, sec->name);
--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -67,9 +67,10 @@ struct reloc {
 	};
 	struct section *sec;
 	struct symbol *sym;
-	unsigned int type;
 	unsigned long offset;
+	unsigned int type;
 	int addend;
+	int idx;
 	bool jump_table_start;
 };
 
@@ -122,6 +123,10 @@ struct elf *elf_open_read(const char *na
 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, int reltype);
 void elf_add_reloc(struct elf *elf, struct reloc *reloc);
+int elf_write_insn(struct elf *elf, struct section *sec,
+		   unsigned long offset, unsigned int len,
+		   const char *insn);
+int elf_write_reloc(struct elf *elf, struct reloc *reloc);
 int elf_write(struct elf *elf);
 void elf_close(struct elf *elf);
 



  parent reply	other threads:[~2020-06-18 14:51 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-18 14:44 [PATCH 0/7] x86/entry: noinstr fixes Peter Zijlstra
2020-06-18 14:44 ` [PATCH 1/7] x86/entry: Fix #UD vs WARN more Peter Zijlstra
2020-06-18 14:57   ` Andy Lutomirski
2020-06-18 15:50     ` Peter Zijlstra
2020-06-18 18:36       ` Andy Lutomirski
2020-06-18 19:02         ` Peter Zijlstra
2020-06-18 19:29           ` Andy Lutomirski
2020-06-18 21:18             ` Peter Zijlstra
2020-06-22 11:47               ` Peter Zijlstra
2020-06-24 22:37                 ` Andy Lutomirski
2020-06-25 11:53                 ` [tip: x86/entry] " tip-bot2 for Peter Zijlstra
2020-06-18 14:44 ` [PATCH 2/7] objtool: Dont consider vmlinux a C-file Peter Zijlstra
2020-06-25 11:53   ` [tip: x86/entry] objtool: Don't " tip-bot2 for Peter Zijlstra
2020-06-18 14:44 ` [PATCH 3/7] x86/entry: Fixup bad_iret vs noinstr Peter Zijlstra
2020-06-18 15:13   ` Marco Elver
2020-06-25 11:53   ` [tip: x86/entry] " tip-bot2 for Peter Zijlstra
2020-06-18 14:44 ` [PATCH 4/7] x86/entry: Increase entry_stack size to a full page Peter Zijlstra
2020-06-18 15:06   ` Marco Elver
2020-06-19  3:10   ` Lai Jiangshan
2020-06-25 11:53   ` [tip: x86/entry] " tip-bot2 for Peter Zijlstra
2020-06-18 14:44 ` [PATCH 5/7] objtool: Clean up elf_write() condition Peter Zijlstra
2020-06-18 14:44 ` Peter Zijlstra [this message]
2020-06-18 14:44 ` [PATCH 7/7] objtool: Fix noinstr vs KCOV Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200618144801.936847099@infradead.org \
    --to=peterz@infradead.org \
    --cc=andreyknvl@google.com \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=jpoimboe@redhat.com \
    --cc=jthierry@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mbenes@suse.cz \
    --cc=mhelsley@vmware.com \
    --cc=rostedt@goodmis.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®