From nobody Tue Dec 16 12:42:39 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8FA03298998; Fri, 9 May 2025 20:18:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746821892; cv=none; b=kva6unK4skBPQuva4MSR751CMiNlE6cr/HBrT1OgSkbZZvcYIJO33r3Ylf7WdUGyyxHOOvwz2n9Th7c2RvBOna1FDReWjgLbFjNmVJTdLv1KmGE9izgCxu7Md9RR7LYHtAx53W0gfTJ9uGkKaHsZ8S1v4mauLBJEwmpkbizTGiw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746821892; c=relaxed/simple; bh=zsZfauCU6GgJ9tzyyCAWCfFGv5Mky/g31a415oAov7Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UlLh/dK8l5URWKEl9+ZSG3abL+6/+f+vcIo3TK4H2RNlQN4VOrvtVRYRJU2q1F1ihAm98XQee0BJ5Gw69ExKZHWnZRQIlfoEAW+OvBydZRxk9z0JFCO+swkbHz5bNF89GQCqfJBbjAllYezp0Eraz2p6qWbKLDs/P/pyxMbCqdU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pzdCprVN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pzdCprVN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4544C4CEE9; Fri, 9 May 2025 20:18:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746821892; bh=zsZfauCU6GgJ9tzyyCAWCfFGv5Mky/g31a415oAov7Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pzdCprVNi4eZPUsvPKMacTyN6dpgv1uyU01kh4ag1+4qiqiVIjqmuiOQtsvTZlLBa fvDEpoG5hlUt52OuBL6dwfHRpEBuaAksphi2TG+wEbYG/LjQ+BgarhcgJ2WA9nHrmL JVsEkEqvSU8zFNucIhTgVeSxXdhfRNgPszHxet4YXqRVucCKTZGHB5mtFUHVX+26Q0 dUGS4BHGgCg7AjR9DkaYbv4E4zrngpC3KDgU1EUiDJbWNt67G1JmEYSI1Gfn219kMw uC2HLt5XJNFeWWal2rUQqNeDJYhLVQcwN+Q4n8DINwpvIfElY6ZdjKEhTFBm0f8yMZ dIcg1Ea10Vjlg== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Petr Mladek , Miroslav Benes , Joe Lawrence , live-patching@vger.kernel.org, Song Liu , laokz , Jiri Kosina , Marcos Paulo de Souza , Weinan Liu , Fazla Mehrab , Chen Zhongjin , Puranjay Mohan Subject: [PATCH v2 39/62] objtool: Add elf_create_data() Date: Fri, 9 May 2025 13:17:03 -0700 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for the objtool klp diff subcommand, refactor elf_add_string() by adding a new elf_add_data() helper which allows the adding of arbitrary data to a section. Make both interfaces public so they can be used by the upcoming klp diff code. Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 66 ++++++++++++++++++++--------- tools/objtool/include/objtool/elf.h | 10 +++-- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index c38b109f441f..1b5528065df7 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -18,10 +18,11 @@ #include #include #include - #include #include =20 +#define ALIGN_UP(x, align_to) (((x) + ((align_to)-1)) & ~((align_to)-1)) + static inline u32 str_hash(const char *str) { return jhash(str, strlen(str), 0); @@ -761,8 +762,6 @@ static int elf_update_symbol(struct elf *elf, struct se= ction *symtab, return 0; } =20 -static int elf_add_string(struct elf *elf, struct section *strtab, const c= har *str); - struct symbol *elf_create_symbol(struct elf *elf, const char *name, struct section *sec, unsigned int bind, unsigned int type, unsigned long offset, @@ -1098,11 +1097,9 @@ struct elf *elf_open_read(const char *name, int flag= s) return NULL; } =20 -static int elf_add_string(struct elf *elf, struct section *strtab, const c= har *str) +unsigned int elf_add_string(struct elf *elf, struct section *strtab, const= char *str) { - Elf_Data *data; - Elf_Scn *s; - int len; + unsigned int offset; =20 if (!strtab) strtab =3D find_section_by_name(elf, ".strtab"); @@ -1111,28 +1108,59 @@ static int elf_add_string(struct elf *elf, struct s= ection *strtab, const char *s return -1; } =20 - s =3D elf_getscn(elf->elf, strtab->idx); + if (!strtab->sh.sh_addralign) { + ERROR("'%s': invalid sh_addralign", strtab->name); + return -1; + } + + offset =3D ALIGN_UP(strtab->sh.sh_size, strtab->sh.sh_addralign); + + if (!elf_add_data(elf, strtab, str, strlen(str) + 1)) + return -1; + + return offset; +} + +void *elf_add_data(struct elf *elf, struct section *sec, const void *data,= size_t size) +{ + unsigned long offset; + Elf_Scn *s; + + if (!sec->sh.sh_addralign) { + ERROR("'%s': invalid sh_addralign", sec->name); + return NULL; + } + + s =3D elf_getscn(elf->elf, sec->idx); if (!s) { ERROR_ELF("elf_getscn"); - return -1; + return NULL; } =20 - data =3D elf_newdata(s); - if (!data) { + sec->data =3D elf_newdata(s); + if (!sec->data) { ERROR_ELF("elf_newdata"); - return -1; + return NULL; } =20 - data->d_buf =3D strdup(str); - data->d_size =3D strlen(str) + 1; - data->d_align =3D 1; + sec->data->d_buf =3D calloc(1, size); + if (!sec->data->d_buf) { + ERROR_GLIBC("calloc"); + return NULL; + } =20 - len =3D strtab->sh.sh_size; - strtab->sh.sh_size +=3D data->d_size; + if (data) + memcpy(sec->data->d_buf, data, size); =20 - mark_sec_changed(elf, strtab, true); + sec->data->d_size =3D size; + sec->data->d_align =3D 1; =20 - return len; + offset =3D ALIGN_UP(sec->sh.sh_size, sec->sh.sh_addralign); + sec->sh.sh_size =3D offset + size; + + mark_sec_changed(elf, sec, true); + + return sec->data->d_buf; } =20 struct section *elf_create_section(struct elf *elf, const char *name, diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index b366516b119d..fc00f86bedba 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -134,6 +134,10 @@ struct symbol *elf_create_section_symbol(struct elf *e= lf, struct section *sec); struct symbol *elf_create_prefix_symbol(struct elf *elf, struct symbol *or= ig, size_t size); =20 +void *elf_add_data(struct elf *elf, struct section *sec, const void *data, + size_t size); + +unsigned int elf_add_string(struct elf *elf, struct section *strtab, const= char *str); =20 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec, unsigned long offset, @@ -147,9 +151,9 @@ struct reloc *elf_init_reloc_data_sym(struct elf *elf, = struct section *sec, struct symbol *sym, s64 addend); =20 -int elf_write_insn(struct elf *elf, struct section *sec, - unsigned long offset, unsigned int len, - const char *insn); +int elf_write_insn(struct elf *elf, struct section *sec, unsigned long off= set, + unsigned int len, const char *insn); + int elf_write(struct elf *elf); void elf_close(struct elf *elf); =20 --=20 2.49.0