Re: [PATCH v8 4/7] perf symbols: Check the file before reading lazy symbol names
From: Namhyung Kim
Date: Sun Nov 22 2026 - 19:43:14 EST
On Sat, Nov 21, 2026 at 08:41:04AM -0800, Alireza Haghdoost wrote:
> This patch makes perf check that a file is still the one it loaded
> before it reads lazy symbol names from it. A lazy name is read later
> from a file that perf opens again by path, at the offset that its
> string table had at load time. If the file was replaced in between,
> for example by a deploy, perf would read names from the wrong file and
> print wrong names without a warning. Eager loading rejects such a
> file, since symsrc__init() checks its build ID.
>
> perf checks the file each time it opens it again. dso__set_lazy_file()
> records the file at load time. Nothing calls it yet: the next patch
> makes the ELF loader call it when it creates lazy symbols.
>
> If the DSO has a build ID, symsrc__init() matched the file with it at
> load time, so accept a file with that build ID, such as a copy of the
> same build. The build ID alone is not enough, since the offsets are
> only valid if the string tables did not move: objcopy
> --add-gnu-debuglink keeps the build ID but moves .strtab, and strip
> --strip-debug removes sections. So perf also records the index, offset
> and size of the .symtab and .dynsym string tables, and compares them.
I think it's extremely rare that the symbol file is being modified while
perf is running.
>
> Reading the build ID each time is slow when most lookups load a name:
> the parity test on a 524 MiB binary, which loads 260k names one at a
> time, takes 9.3 seconds instead of 3.9. So perf also records the
> device, inode, size, mtime and ctime of the file, and reads the build
> ID only if they changed. The ctime changes when another file is copied
> over this one in place, even if its size and mtime are kept, as with
> cp -p. Then perf keeps the identity of the accepted file for later
> reads. With this, the test takes 4.1 seconds. A DSO without a build ID
> must keep the same identity.
Is ctime really needed? I think dev/inode will catch new file creation
and mtime is for in-place mofifications.
>
> If the file is not the one perf loaded, perf warns and shows the
> remaining lazy symbols of that DSO as "[unknown]" rather than print
> wrong names, without opening the file again. If the file cannot be
> opened or checked, for example because perf ran out of file descriptors
> or fstat() or a read of its ELF headers failed, perf warns once and
> shows the symbols it was reading the names of as "[unknown]", but tries
> again for later names, since the failure can be temporary. A file whose
> build ID cannot be read counts as not checked, since perf cannot tell a
> failed read from a missing build ID note; perf reads no names from it
> either.
>
> Like symsrc__init(), the check trusts the build ID: a file edited after
> linking without a new build ID, and with its string tables in place,
> gives the edited names.
Right, but I don't think it's gonna be a problem in practice.
Thanks,
Namhyung
>
> The unit tests later in this series cover these cases.
>
> No functional change, since no symbol has a lazy name yet.
>
> Signed-off-by: Alireza Haghdoost <haghdoost@xxxxxxxx>
> ---