Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] scripts/gen_lr_unwind: missing SHN_XINDEX handling in
dump_lr_unwind()
- [Low] scripts/gen_lr_unwind: broken dump_lr_unwind() on ET_EXEC/ET_DYN
with relas
--
commit 28070d4daa3cf7bb2b0ad3b96252120bc29dcba2
Author: Dylan Hatch [off-list ref]
scripts/gen_lr_unwind: add --dump option to inspect .lr_unwind tables
This commit adds a --dump (-d) option to scripts/gen_lr_unwind to print
.lr_unwind FDEs and instruction ranges in human-readable form for both
relocatable and linked ELF files.
quoted hunk ↗ jump to hunk
diff --git a/scripts/gen_lr_unwind.c b/scripts/gen_lr_unwind.c
index f65edd86ac78c..2058601ec6135 100644
--- a/scripts/gen_lr_unwind.c
+++ b/scripts/gen_lr_unwind.c
[ ... ]
quoted hunk ↗ jump to hunk
@@ -478,6 +487,225 @@ static int emit_lr_unwind_sections(const char *fname, struct lr_unwind_ctx *ctx,
+static int dump_lr_unwind(struct lr_unwind_ctx *ctx, const Elf64_Shdr *shdrs,
+ unsigned int shnum, const char *shstrtab)
+{
+ const struct lr_fde *fdes;
+ const Elf64_Rela *relas = NULL;
+ const lr_range_t *ranges = NULL;
+ size_t nr_fdes, nr_ranges = 0;
+ const Elf64_Sym *elf_syms = NULL;
+ size_t nr_elf_syms = 0;
+ const char *strtab = NULL;
+ struct dump_sym *dsyms = NULL;
+ size_t nr_dsyms = 0;
+ bool is_rel = (((const Elf64_Ehdr *)ctx->map)->e_type == ET_REL);
+
+ if (ctx->fde_sec) {
+ fdes = ctx->map + ctx->fde_sec->sh_offset;
+ nr_fdes = ctx->fde_sec->sh_size / sizeof(struct lr_fde);
+ if (ctx->rela_fde_sec)
+ relas = ctx->map + ctx->rela_fde_sec->sh_offset;
+ if (ctx->ranges_sec) {
+ ranges = ctx->map + ctx->ranges_sec->sh_offset;
+ nr_ranges = ctx->ranges_sec->sh_size / sizeof(lr_range_t);
+ }
+ } else {
[ ... ]
quoted hunk ↗ jump to hunk
+ if (ctx->symtab_shndx) {
+ const Elf64_Shdr *sym_sec = &shdrs[ctx->symtab_shndx];
+
+ elf_syms = ctx->map + sym_sec->sh_offset;
+ nr_elf_syms = sym_sec->sh_size / sizeof(Elf64_Sym);
+ strtab = ctx->map + shdrs[sym_sec->sh_link].sh_offset;
+
+ dsyms = malloc(nr_elf_syms * sizeof(*dsyms));
+ if (!dsyms)
+ return -1;
+
+ for (size_t i = 0; i < nr_elf_syms; i++) {
+ const Elf64_Sym *s = &elf_syms[i];
+ unsigned char type = ELF64_ST_TYPE(s->st_info);
+ const char *name = strtab + s->st_name;
+
+ if (s->st_shndx == SHN_UNDEF ||
+ s->st_shndx >= SHN_LORESERVE)
+ continue;
[Severity: Low]
Does skipping symbols with st_shndx >= SHN_LORESERVE here and when
formatting FDE relocations below drop all sections at or above index
0xff00?
main() and emit_lr_unwind_sections() handle objects with >= SHN_LORESERVE
sections, such as vmlinux.o built with per-function sections, where symbols
in those sections have st_shndx == SHN_XINDEX and store their 32-bit
section index in the SHT_SYMTAB_SHNDX section. Without resolving
SHN_XINDEX in both places, dump_lr_unwind() omits the section name, offset,
and function name for any FDE targeting a section >= SHN_LORESERVE.
quoted hunk ↗ jump to hunk
+ if (type != STT_FUNC && type != STT_NOTYPE)
+ continue;
+ if (!name[0] || name[0] == '$' ||
+ !strncmp(name, ".L", 2))
+ continue;
+
+ dsyms[nr_dsyms++] = (struct dump_sym){
+ .addr = s->st_value,
+ .shndx = is_rel ? s->st_shndx : 0,
+ .is_func = (type == STT_FUNC),
+ .name = name,
+ };
+ }
[ ... ]
quoted hunk ↗ jump to hunk
+ printf("FDE[%zu] ", i);
+ if (relas && elf_syms) {
+ u32 sym_idx = ELF64_R_SYM(relas[i].r_info);
+
+ if (sym_idx < nr_elf_syms) {
+ const Elf64_Sym *sym = &elf_syms[sym_idx];
+ unsigned int shndx = sym->st_shndx;
+ u64 off = sym->st_value + relas[i].r_addend;
+
+ if (shndx < shnum && shndx < SHN_LORESERVE) {
+ const char *sec_name =
+ shstrtab + shdrs[shndx].sh_name;
+ const struct dump_sym *ds =
+ find_dump_sym(dsyms, nr_dsyms,
+ shndx, off);
[Severity: Low]
On an ET_EXEC or ET_DYN binary linked with --emit-relocs so that
.rela.lr_unwind_fde is still present, does taking the relas branch break
symbol resolution and address output?
When !is_rel, dsyms entries are populated with .shndx = 0, so calling
find_dump_sym() here with non-zero sym->st_shndx never matches any symbol.
In addition, sym->st_value is already a virtual address rather than a
section-relative offset, and if .lr_unwind_fde has been sorted in place,
relas[i] no longer matches fdes[i]. Should relas only be used when is_rel
is true, falling back to the PC-relative fde->start_offset branch for
ET_EXEC and ET_DYN?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117043504.1021063-1-dylanbhatch@google.com?part=6