From: Dylan Hatch <dylanbhatch@google.com>
To: Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
"David S . Miller" <davem@davemloft.net>,
Nathan Chancellor <nathan@kernel.org>,
Nicolas Schier <nsc@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>,
Weinan Liu <wnliu@google.com>, Jens Remus <jremus@linux.ibm.com>,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org,
linux-crypto@vger.kernel.org, llvm@lists.linux.dev,
Josh Poimboeuf <jpoimboe@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Ard Biesheuvel <ardb@kernel.org>,
Indu Bhagat <ibhagatgnu@gmail.com>,
Prasanna Kumar T S M <ptsm@linux.microsoft.com>,
live-patching@vger.kernel.org,
Dylan Hatch <dylanbhatch@google.com>
Subject: [RFC PATCH 05/12] scripts/gen_lr_unwind: emit .lr_unwind_fde and .lr_unwind_ranges
Date: Tue, 17 Nov 2026 04:34:57 +0000 [thread overview]
Message-ID: <20261117043504.1021063-6-dylanbhatch@google.com> (raw)
In-Reply-To: <20261117043504.1021063-1-dylanbhatch@google.com>
Implement emit_lr_unwind_sections() in scripts/gen_lr_unwind to append
.lr_unwind_fde, .rela.lr_unwind_fde, and .lr_unwind_ranges sections to
the target ELF relocatable object and update its .shstrtab and section
header table.
Signed-off-by: Dylan Hatch <dylanbhatch@google.com>
---
scripts/gen_lr_unwind.c | 140 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 134 insertions(+), 6 deletions(-)
diff --git a/scripts/gen_lr_unwind.c b/scripts/gen_lr_unwind.c
index 118f995b2c48b..f65edd86ac78c 100644
--- a/scripts/gen_lr_unwind.c
+++ b/scripts/gen_lr_unwind.c
@@ -54,6 +54,19 @@ struct lr_unwind_ctx {
size_t cap_runs;
};
+#define LR_SHSTRTAB_EXTRA \
+ (sizeof(".lr_unwind_fde") + sizeof(".rela.lr_unwind_fde") + \
+ sizeof(".lr_unwind_ranges"))
+
+struct elf_writer {
+ int fd;
+ u64 off;
+ Elf64_Shdr *shdrs;
+ unsigned int shnum;
+ char *shstrtab;
+ size_t str_sz;
+};
+
static int append_fde_entry(struct lr_unwind_ctx *ctx,
const Elf64_Rela *eh_rela,
s64 byte_off, u32 desc)
@@ -338,16 +351,131 @@ static int parse_eh_frame(struct lr_unwind_ctx *ctx)
return ret ? -1 : 0;
}
+static u64 align_up(u64 val, u64 align)
+{
+ return (val + align - 1) & ~(align - 1);
+}
+
+static int pwrite_all(int fd, const void *buf, size_t count, off_t offset)
+{
+ const u8 *p = buf;
+
+ while (count > 0) {
+ ssize_t n = pwrite(fd, p, count, offset);
+
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ return -1;
+ }
+ p += n;
+ count -= n;
+ offset += n;
+ }
+ return 0;
+}
+
+static int append_section(struct elf_writer *w, const char *name,
+ const void *data, u64 size, u64 align,
+ u32 type, u64 flags, u32 link, u32 info, u64 entsize)
+{
+ size_t name_len = strlen(name) + 1;
+ u64 sec_off = align_up(w->off, align);
+
+ if (size && pwrite_all(w->fd, data, size, sec_off) < 0)
+ return -1;
+
+ memcpy(w->shstrtab + w->str_sz, name, name_len);
+ w->shdrs[w->shnum++] = (Elf64_Shdr){
+ .sh_name = w->str_sz,
+ .sh_type = type,
+ .sh_flags = flags,
+ .sh_offset = sec_off,
+ .sh_size = size,
+ .sh_link = link,
+ .sh_info = info,
+ .sh_addralign = align,
+ .sh_entsize = entsize,
+ };
+ w->str_sz += name_len;
+ w->off = sec_off + size;
+ return 0;
+}
+
static int emit_lr_unwind_sections(const char *fname, struct lr_unwind_ctx *ctx,
unsigned int orig_shnum,
unsigned int shstrndx)
{
- /* TODO: Emit .lr_unwind_fde, .rela.lr_unwind_fde, and .lr_unwind_ranges */
- (void)fname;
- (void)ctx;
- (void)orig_shnum;
- (void)shstrndx;
- return 0;
+ const Elf64_Shdr *orig_shdrs =
+ ctx->map + ((const Elf64_Ehdr *)ctx->map)->e_shoff;
+ size_t orig_str_sz = orig_shdrs[shstrndx].sh_size;
+ Elf64_Ehdr ehdr = *(const Elf64_Ehdr *)ctx->map;
+ struct elf_writer w = {
+ .fd = -1,
+ .shnum = orig_shnum,
+ .str_sz = orig_str_sz,
+ .shdrs = calloc(orig_shnum + 3, sizeof(Elf64_Shdr)),
+ .shstrtab = malloc(orig_str_sz + LR_SHSTRTAB_EXTRA),
+ };
+ unsigned int fde_shndx = orig_shnum;
+ int ret = -1;
+
+ if (!w.shdrs || !w.shstrtab)
+ goto out;
+
+ memcpy(w.shdrs, orig_shdrs, orig_shnum * sizeof(Elf64_Shdr));
+ memcpy(w.shstrtab, ctx->map + orig_shdrs[shstrndx].sh_offset,
+ orig_str_sz);
+
+ w.off = (ehdr.e_shoff + (u64)orig_shnum * sizeof(Elf64_Shdr) ==
+ ctx->map_size) ? ehdr.e_shoff : ctx->map_size;
+
+ munmap((void *)ctx->map, ctx->map_size);
+ ctx->map = NULL;
+
+ w.fd = open(fname, O_RDWR);
+ if (w.fd < 0)
+ goto out;
+
+ if (append_section(&w, ".lr_unwind_fde", ctx->fdes,
+ ctx->nr_fdes * sizeof(*ctx->fdes), 4,
+ SHT_PROGBITS, SHF_ALLOC, 0, 0, 0) < 0 ||
+ append_section(&w, ".rela.lr_unwind_fde", ctx->fde_relas,
+ ctx->nr_fdes * sizeof(*ctx->fde_relas), 8,
+ SHT_RELA, SHF_INFO_LINK, ctx->symtab_shndx,
+ fde_shndx, sizeof(Elf64_Rela)) < 0 ||
+ (ctx->nr_ranges &&
+ append_section(&w, ".lr_unwind_ranges", ctx->ranges,
+ ctx->nr_ranges * sizeof(*ctx->ranges), 2,
+ SHT_PROGBITS, SHF_ALLOC, 0, 0, 0) < 0))
+ goto out;
+
+ if (pwrite_all(w.fd, w.shstrtab, w.str_sz, w.off) < 0)
+ goto out;
+ w.shdrs[shstrndx].sh_offset = w.off;
+ w.shdrs[shstrndx].sh_size = w.str_sz;
+ w.off = align_up(w.off + w.str_sz, 8);
+
+ if (ehdr.e_shnum == SHN_UNDEF || w.shnum >= SHN_LORESERVE) {
+ ehdr.e_shnum = SHN_UNDEF;
+ w.shdrs[0].sh_size = w.shnum;
+ } else {
+ ehdr.e_shnum = w.shnum;
+ }
+ ehdr.e_shoff = w.off;
+
+ if (pwrite_all(w.fd, w.shdrs, w.shnum * sizeof(Elf64_Shdr), w.off) < 0 ||
+ pwrite_all(w.fd, &ehdr, sizeof(ehdr), 0) < 0 ||
+ ftruncate(w.fd, w.off + w.shnum * sizeof(Elf64_Shdr)) < 0)
+ goto out;
+
+ ret = 0;
+out:
+ if (w.fd >= 0)
+ close(w.fd);
+ free(w.shstrtab);
+ free(w.shdrs);
+ return ret;
}
int main(int argc, char *argv[])
--
2.56.0.679.gc527c65306-goog
next prev parent reply other threads:[~2026-11-17 4:35 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 4:34 [RFC PATCH 00/12] arm64, unwind: add .lr_unwind unwinder for reliable stacktrace Dylan Hatch
2026-11-17 4:34 ` [RFC PATCH 01/12] arm64, unwind: build with .eh_frame and add gen_lr_unwind skeleton Dylan Hatch
2026-11-18 4:36 ` sashiko-bot
2026-11-17 4:34 ` [RFC PATCH 02/12] arm64: factor .eh_frame parser out of patch-scs.c into <asm/eh_frame.h> Dylan Hatch
2026-11-18 4:36 ` sashiko-bot
2026-11-17 4:34 ` [RFC PATCH 03/12] arm64: eh_frame: track CFA and x29/x30 register state in CFI parser Dylan Hatch
2026-11-17 4:34 ` [RFC PATCH 04/12] arm64, unwind: parse .eh_frame into .lr_unwind entries in gen_lr_unwind Dylan Hatch
2026-11-17 4:34 ` Dylan Hatch [this message]
2026-11-17 4:34 ` [RFC PATCH 06/12] scripts/gen_lr_unwind: add --dump option to inspect .lr_unwind tables Dylan Hatch
2026-11-18 4:36 ` sashiko-bot
2026-11-17 4:34 ` [RFC PATCH 07/12] scripts/sorttable: sort .lr_unwind_fde by start_offset Dylan Hatch
2026-11-17 4:35 ` [RFC PATCH 08/12] arm64, unwind: add in-kernel .lr_unwind lookup and validation Dylan Hatch
2026-11-17 4:35 ` [RFC PATCH 09/12] arm64/module, unwind: add .lr_unwind support for modules Dylan Hatch
2026-11-17 4:35 ` [RFC PATCH 10/12] arm64: entry: add unwind info for call_on_irq_stack() Dylan Hatch
2026-11-17 4:35 ` [RFC PATCH 11/12] arm64, crypto/lib: annotate leaf functions with CFI info Dylan Hatch
2026-11-18 5:07 ` Eric Biggers
2026-11-17 4:35 ` [RFC PATCH 12/12] unwind: arm64: use .lr_unwind to unwind interrupt frames Dylan Hatch
2026-11-18 4:36 ` sashiko-bot
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=20261117043504.1021063-6-dylanbhatch@google.com \
--to=dylanbhatch@google.com \
--cc=ardb@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=ibhagatgnu@gmail.com \
--cc=jpoimboe@kernel.org \
--cc=jremus@linux.ibm.com \
--cc=justinstitt@google.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=mark.rutland@arm.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nsc@kernel.org \
--cc=ptsm@linux.microsoft.com \
--cc=rostedt@goodmis.org \
--cc=will@kernel.org \
--cc=wnliu@google.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.