Live Patching
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@kernel.org>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org, Petr Mladek <pmladek@suse.com>,
	Miroslav Benes <mbenes@suse.cz>,
	Joe Lawrence <joe.lawrence@redhat.com>,
	live-patching@vger.kernel.org, Song Liu <song@kernel.org>,
	laokz <laokz@foxmail.com>, Jiri Kosina <jikos@kernel.org>,
	Marcos Paulo de Souza <mpdesouza@suse.com>,
	Weinan Liu <wnliu@google.com>,
	Fazla Mehrab <a.mehrab@bytedance.com>,
	Chen Zhongjin <chenzhongjin@huawei.com>,
	Puranjay Mohan <puranjay@kernel.org>,
	Dylan Hatch <dylanbhatch@google.com>
Subject: [PATCH v3 40/64] objtool: Add elf_create_reloc() and elf_init_reloc()
Date: Thu, 26 Jun 2025 16:55:27 -0700	[thread overview]
Message-ID: <28472bc6f2c7e25cd034d1ba6d591ba1c0252933.1750980517.git.jpoimboe@kernel.org> (raw)
In-Reply-To: <cover.1750980516.git.jpoimboe@kernel.org>

elf_create_rela_section() is quite limited in that it requires the
caller to know how many relocations need to be allocated up front.

In preparation for the objtool klp diff subcommand, allow an arbitrary
number of relocations to be created and initialized on demand after
section creation.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 tools/objtool/elf.c                 | 151 +++++++++++++++++++++++++---
 tools/objtool/include/objtool/elf.h |   9 ++
 2 files changed, 145 insertions(+), 15 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 535bdcf077d0..0e98cf2ab533 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -22,6 +22,8 @@
 #include <objtool/warn.h>
 
 #define ALIGN_UP(x, align_to) (((x) + ((align_to)-1)) & ~((align_to)-1))
+#define ALIGN_UP_POW2(x) (1U << ((8 * sizeof(x)) - __builtin_clz((x) - 1U)))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
 
 static inline u32 str_hash(const char *str)
 {
@@ -896,10 +898,9 @@ elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, size_t size)
 				 offset, size);
 }
 
-static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
-				    unsigned int reloc_idx,
-				    unsigned long offset, struct symbol *sym,
-				    s64 addend, unsigned int type)
+struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
+			     unsigned int reloc_idx, unsigned long offset,
+			     struct symbol *sym, s64 addend, unsigned int type)
 {
 	struct reloc *reloc, empty = { 0 };
 
@@ -1001,12 +1002,14 @@ static int read_relocs(struct elf *elf)
 
 		rsec->base->rsec = rsec;
 
-		nr_reloc = 0;
-		rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
+		rsec->nr_alloc_relocs = sec_num_entries(rsec);
+		rsec->relocs = calloc(rsec->nr_alloc_relocs, sizeof(*reloc));
 		if (!rsec->relocs) {
 			ERROR_GLIBC("calloc");
 			return -1;
 		}
+
+		nr_reloc = 0;
 		for (i = 0; i < sec_num_entries(rsec); i++) {
 			reloc = &rsec->relocs[i];
 
@@ -1255,8 +1258,99 @@ struct section *elf_create_section(struct elf *elf, const char *name,
 	return sec;
 }
 
+static int elf_alloc_reloc(struct elf *elf, struct section *rsec)
+{
+	struct reloc *old_relocs, *old_relocs_end, *new_relocs;
+	unsigned int nr_relocs_old = sec_num_entries(rsec);
+	unsigned int nr_relocs_new = nr_relocs_old + 1;
+	unsigned long nr_alloc;
+	struct symbol *sym;
+
+	if (!rsec->data) {
+		rsec->data = elf_newdata(elf_getscn(elf->elf, rsec->idx));
+		if (!rsec->data) {
+			ERROR_ELF("elf_newdata");
+			return -1;
+		}
+
+		rsec->data->d_align = 1;
+		rsec->data->d_type = ELF_T_RELA;
+		rsec->data->d_buf = NULL;
+	}
+
+	rsec->data->d_size = nr_relocs_new * elf_rela_size(elf);
+	rsec->sh.sh_size   = rsec->data->d_size;
+
+	nr_alloc = MAX(64, ALIGN_UP_POW2(nr_relocs_new));
+	if (nr_alloc <= rsec->nr_alloc_relocs)
+		return 0;
+	rsec->nr_alloc_relocs = nr_alloc;
+
+	rsec->data->d_buf = realloc(rsec->data->d_buf,
+				    nr_alloc * elf_rela_size(elf));
+	if (!rsec->data->d_buf) {
+		ERROR_GLIBC("realloc");
+		return -1;
+	}
+
+	old_relocs = rsec->relocs;
+	new_relocs = calloc(nr_alloc, sizeof(struct reloc));
+	if (!new_relocs) {
+		ERROR_GLIBC("calloc");
+		return -1;
+	}
+
+	if (!old_relocs)
+		goto done;
+
+	/*
+	 * The struct reloc's address has changed.  Update all the symbols and
+	 * relocs which reference it.
+	 */
+
+	old_relocs_end = &old_relocs[nr_relocs_old];
+	for_each_sym(elf, sym) {
+		struct reloc *reloc;
+
+		reloc = sym->relocs;
+		if (!reloc)
+			continue;
+
+		if (reloc >= old_relocs && reloc < old_relocs_end)
+			sym->relocs = &new_relocs[reloc - old_relocs];
+
+		while (1) {
+			struct reloc *next_reloc = sym_next_reloc(reloc);
+
+			if (!next_reloc)
+				break;
+
+			if (next_reloc >= old_relocs && next_reloc < old_relocs_end)
+				set_sym_next_reloc(reloc, &new_relocs[next_reloc - old_relocs]);
+
+			reloc = next_reloc;
+		}
+	}
+
+	memcpy(new_relocs, old_relocs, nr_relocs_old * sizeof(struct reloc));
+
+	for (int i = 0; i < nr_relocs_old; i++) {
+		struct reloc *old = &old_relocs[i];
+		struct reloc *new = &new_relocs[i];
+		u32 key = reloc_hash(old);
+
+		elf_hash_del(reloc, &old->hash, key);
+		elf_hash_add(reloc, &new->hash, key);
+	}
+
+	free(old_relocs);
+done:
+	rsec->relocs = new_relocs;
+	return 0;
+}
+
 struct section *elf_create_rela_section(struct elf *elf, struct section *sec,
-					unsigned int reloc_nr)
+					unsigned int nr_relocs)
 {
 	struct section *rsec;
 	char *rsec_name;
@@ -1269,34 +1363,61 @@ struct section *elf_create_rela_section(struct elf *elf, struct section *sec,
 	strcpy(rsec_name, ".rela");
 	strcat(rsec_name, sec->name);
 
-	rsec = elf_create_section(elf, rsec_name, reloc_nr * elf_rela_size(elf),
+	rsec = elf_create_section(elf, rsec_name, nr_relocs * elf_rela_size(elf),
 				  elf_rela_size(elf), SHT_RELA, elf_addr_size(elf),
 				  SHF_INFO_LINK);
 	free(rsec_name);
 	if (!rsec)
 		return NULL;
 
-	rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
-	rsec->sh.sh_info = sec->idx;
-
-	if (reloc_nr) {
+	if (nr_relocs) {
 		rsec->data->d_type = ELF_T_RELA;
-		rsec->relocs = calloc(sec_num_entries(rsec), sizeof(struct reloc));
+
+		rsec->nr_alloc_relocs = nr_relocs;
+		rsec->relocs = calloc(nr_relocs, sizeof(struct reloc));
 		if (!rsec->relocs) {
 			ERROR_GLIBC("calloc");
 			return NULL;
 		}
 	}
 
+	rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
+	rsec->sh.sh_info = sec->idx;
+
 	sec->rsec = rsec;
 	rsec->base = sec;
 
 	return rsec;
 }
 
+struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
+			       unsigned long offset,
+			       struct symbol *sym, s64 addend,
+			       unsigned int type)
+{
+	struct section *rsec = sec->rsec;
+
+	if (!rsec) {
+		rsec = elf_create_rela_section(elf, sec, 0);
+		if (!rsec)
+			return NULL;
+	}
+
+	if (find_reloc_by_dest(elf, sec, offset)) {
+		ERROR_FUNC(sec, offset, "duplicate reloc");
+		return NULL;
+	}
+
+	if (elf_alloc_reloc(elf, rsec))
+		return NULL;
+
+	return elf_init_reloc(elf, rsec, sec_num_entries(rsec) - 1, offset, sym,
+			      addend, type);
+}
+
 struct section *elf_create_section_pair(struct elf *elf, const char *name,
 					size_t entsize, unsigned int nr,
-					unsigned int reloc_nr)
+					unsigned int nr_relocs)
 {
 	struct section *sec;
 
@@ -1305,7 +1426,7 @@ struct section *elf_create_section_pair(struct elf *elf, const char *name,
 	if (!sec)
 		return NULL;
 
-	if (!elf_create_rela_section(elf, sec, reloc_nr))
+	if (!elf_create_rela_section(elf, sec, nr_relocs))
 		return NULL;
 
 	return sec;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index fc00f86bedba..5c663e475890 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -47,6 +47,7 @@ struct section {
 	int idx;
 	bool _changed, text, rodata, noinstr, init, truncate;
 	struct reloc *relocs;
+	unsigned long nr_alloc_relocs;
 };
 
 struct symbol {
@@ -139,6 +140,14 @@ void *elf_add_data(struct elf *elf, struct section *sec, const void *data,
 
 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str);
 
+struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
+			       unsigned long offset, struct symbol *sym,
+			       s64 addend, unsigned int type);
+
+struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
+			     unsigned int reloc_idx, unsigned long offset,
+			     struct symbol *sym, s64 addend, unsigned int type);
+
 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
 				      unsigned long offset,
 				      unsigned int reloc_idx,
-- 
2.49.0


  parent reply	other threads:[~2025-06-26 23:56 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-26 23:54 [PATCH v3 00/64] objtool,livepatch: klp-build livepatch module generation Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 01/64] s390/vmlinux.lds.S: Prevent thunk functions from getting placed with normal text Josh Poimboeuf
2025-06-27  9:34   ` Heiko Carstens
2025-06-26 23:54 ` [PATCH v3 02/64] vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 03/64] x86/module: Improve relocation error messages Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 04/64] x86/kprobes: Remove STACK_FRAME_NON_STANDARD annotation Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 05/64] compiler: Tweak __UNIQUE_ID() naming Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 06/64] compiler.h: Make addressable symbols less of an eyesore Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 07/64] elfnote: Change ELFNOTE() to use __UNIQUE_ID() Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 08/64] kbuild: Remove 'kmod_' prefix from __KBUILD_MODNAME Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 09/64] modpost: Ignore unresolved section bounds symbols Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 10/64] x86/alternative: Refactor INT3 call emulation selftest Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 11/64] objtool: Make find_symbol_containing() less arbitrary Josh Poimboeuf
2025-06-26 23:54 ` [PATCH v3 12/64] objtool: Fix broken error handling in read_symbols() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 13/64] objtool: Propagate elf_truncate_section() error in elf_write() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 14/64] objtool: Remove error handling boilerplate Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 15/64] objtool: Add empty symbols to the symbol tree again Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 16/64] objtool: Fix interval tree insertion for zero-length symbols Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 17/64] objtool: Fix weak symbol detection Josh Poimboeuf
2025-06-27  9:13   ` Peter Zijlstra
2025-06-27 15:42     ` Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 18/64] objtool: Fix x86 addend calculation Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 19/64] objtool: Fix __pa_symbol() relocation handling Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 20/64] objtool: Fix "unexpected end of section" warning for alternatives Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 21/64] objtool: Check for missing annotation entries in read_annotate() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 22/64] objtool: Const string cleanup Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 23/64] objtool: Clean up compiler flag usage Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 24/64] objtool: Remove .parainstructions reference Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 25/64] objtool: Convert elf iterator macros to use 'struct elf' Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 26/64] objtool: Add section/symbol type helpers Josh Poimboeuf
2025-06-27 10:29   ` Peter Zijlstra
2025-06-27 16:36     ` Josh Poimboeuf
2025-06-30  7:29       ` Peter Zijlstra
2025-07-01 19:05         ` Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 27/64] objtool: Mark .cold subfunctions Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 28/64] objtool: Fix weak symbol hole detection for .cold functions Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 29/64] objtool: Mark prefix functions Josh Poimboeuf
2025-06-27 10:31   ` Peter Zijlstra
2025-06-27 16:53     ` Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 30/64] objtool: Simplify reloc offset calculation in unwind_read_hints() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 31/64] objtool: Avoid emptying lists for duplicate sections Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 32/64] objtool: Rename --Werror to --werror Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 33/64] objtool: Resurrect --backup option Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 34/64] objtool: Reindent check_options[] Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 35/64] objtool: Refactor add_jump_destinations() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 36/64] objtool: Simplify special symbol handling in elf_update_symbol() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 37/64] objtool: Generalize elf_create_symbol() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 38/64] objtool: Generalize elf_create_section() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 39/64] objtool: Add elf_create_data() Josh Poimboeuf
2025-06-26 23:55 ` Josh Poimboeuf [this message]
2025-06-26 23:55 ` [PATCH v3 41/64] objtool: Add elf_create_file() Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 42/64] kbuild,x86: Fix special section module permissions Josh Poimboeuf
2025-06-27 10:53   ` Peter Zijlstra
2025-06-27 17:34     ` Josh Poimboeuf
2025-06-30  7:31       ` Peter Zijlstra
2025-09-16 23:18         ` Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 43/64] x86/alternative: Define ELF section entry size for alternatives Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 44/64] x86/jump_label: Define ELF section entry size for jump labels Josh Poimboeuf
2025-06-27 10:48   ` Peter Zijlstra
2025-06-27 16:55     ` Josh Poimboeuf
2025-06-30  7:35       ` Peter Zijlstra
2025-06-26 23:55 ` [PATCH v3 45/64] x86/static_call: Define ELF section entry size of static calls Josh Poimboeuf
2025-06-27 10:51   ` Peter Zijlstra
2025-06-26 23:55 ` [PATCH v3 46/64] x86/extable: Define ELF section entry size for exception table Josh Poimboeuf
2025-06-27 10:52   ` Peter Zijlstra
2025-06-26 23:55 ` [PATCH v3 47/64] x86/bug: Define ELF section entry size for bug table Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 48/64] x86/orc: Define ELF section entry size for unwind hints Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 49/64] objtool: Unify STACK_FRAME_NON_STANDARD entry sizes Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 50/64] objtool/klp: Add --checksum option to generate per-function checksums Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 51/64] objtool/klp: Add --debug-checksum=<funcs> to show per-instruction checksums Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 52/64] objtool/klp: Introduce klp diff subcommand for diffing object files Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 53/64] objtool/klp: Add --debug option to show cloning decisions Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 54/64] objtool/klp: Add post-link subcommand to finalize livepatch modules Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 55/64] objtool: Disallow duplicate prefix symbols Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 56/64] objtool: Add base objtool support for livepatch modules Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 57/64] livepatch: Add CONFIG_KLP_BUILD Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 58/64] kbuild,objtool: Defer objtool validation step for CONFIG_KLP_BUILD Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 59/64] livepatch/klp-build: Introduce fix-patch-lines script to avoid __LINE__ diff noise Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 60/64] livepatch/klp-build: Add stub init code for livepatch modules Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 61/64] livepatch/klp-build: Introduce klp-build script for generating " Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 62/64] livepatch/klp-build: Add --debug option to show cloning decisions Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 63/64] livepatch/klp-build: Add --show-first-changed option to show function divergence Josh Poimboeuf
2025-06-26 23:55 ` [PATCH v3 64/64] livepatch: Introduce source code helpers for livepatch modules Josh Poimboeuf

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=28472bc6f2c7e25cd034d1ba6d591ba1c0252933.1750980517.git.jpoimboe@kernel.org \
    --to=jpoimboe@kernel.org \
    --cc=a.mehrab@bytedance.com \
    --cc=chenzhongjin@huawei.com \
    --cc=dylanbhatch@google.com \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=laokz@foxmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=mpdesouza@suse.com \
    --cc=pmladek@suse.com \
    --cc=puranjay@kernel.org \
    --cc=song@kernel.org \
    --cc=wnliu@google.com \
    --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