Read from obj->offset in MaybeOpenFdFromSelfExe. This allows MaybeOpenFdFromSelfExe to symbolize unlinked executables linked with --rosegment, whose first PT_LOAD segment is read-only and whose executable PT_LOAD segment is registered at a non-zero file offset. Previously, it required obj->start_addr to begin with ELFMAG and compared it against file offset 0 of /proc/self/exe. PiperOrigin-RevId: 1017882038
diff --git a/absl/debugging/symbolize_elf.inc b/absl/debugging/symbolize_elf.inc index 07b1167..d7eb316 100644 --- a/absl/debugging/symbolize_elf.inc +++ b/absl/debugging/symbolize_elf.inc
@@ -1375,7 +1375,7 @@ } static void MaybeOpenFdFromSelfExe(ObjFile *obj) { - if (memcmp(obj->start_addr, ELFMAG, SELFMAG) != 0) { + if (obj->offset == 0 && memcmp(obj->start_addr, ELFMAG, SELFMAG) != 0) { return; } int fd = OpenReadOnlyWithHighFD("/proc/self/exe"); @@ -1384,13 +1384,15 @@ } // Verify that contents of /proc/self/exe matches in-memory image of // the binary. This can fail if the "deleted" binary is in fact not - // the main executable, or for binaries that have the first PT_LOAD - // segment smaller than 4K. We do it in four steps so that the + // the main executable, or for binaries that have the first executable + // PT_LOAD segment smaller than 4K. We do it in four steps so that the // buffer is smaller and we don't consume too much stack space. const char *mem = reinterpret_cast<const char *>(obj->start_addr); - for (int i = 0; i < 4; ++i) { + for (size_t i = 0; i < 4; ++i) { char buf[1024]; - ssize_t n = read(fd, buf, sizeof(buf)); + ssize_t n; + NO_INTR(n = pread(fd, buf, sizeof(buf), + static_cast<off_t>(obj->offset + i * sizeof(buf)))); if (n != sizeof(buf) || memcmp(buf, mem, sizeof(buf)) != 0) { close(fd); return;