perf symbol-elf: Add support for the block argument for libbfd

James Clark caught that the BUILD_NONDISTRO=1 build with libbfd was
broken due to an update to the read_build_id function adding a
blocking argument. Add support for this argument by first opening the
file blocking or non-blocking, then switching from bfd_openr to
bfd_fdopenr and passing the opened fd. bfd_fdopenr closes the fd on
error and when bfd_close are called.

Reported-by: James Clark <james.clark@linaro.org>
Closes: https://lore.kernel.org/lkml/20250903-james-perf-read-build-id-fix-v1-2-6a694d0a980f@linaro.org/
Fixes: 2c369d91d093 ("perf symbol: Add blocking argument to filename__read_build_id")
Signed-off-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Link: https://lore.kernel.org/r/20250904161731.1193729-1-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by Ian Rogers and committed by Namhyung Kim ca81e74d 744175e9

+7 -3
+7 -3
tools/perf/util/symbol-elf.c
··· 873 873 874 874 #ifdef HAVE_LIBBFD_BUILDID_SUPPORT 875 875 876 - static int read_build_id(const char *filename, struct build_id *bid) 876 + static int read_build_id(const char *filename, struct build_id *bid, bool block) 877 877 { 878 878 size_t size = sizeof(bid->data); 879 - int err = -1; 879 + int err = -1, fd; 880 880 bfd *abfd; 881 881 882 - abfd = bfd_openr(filename, NULL); 882 + fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK)); 883 + if (fd < 0) 884 + return -1; 885 + 886 + abfd = bfd_fdopenr(filename, /*target=*/NULL, fd); 883 887 if (!abfd) 884 888 return -1; 885 889