[llvm] [llvm-profgen] Handle perf mmaps with page sizes larger than 4 KiB (PR #217887)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 22:20:49 PDT 2026


================
@@ -594,30 +594,48 @@ void PerfScriptReader::updateBinaryAddress(const MMapEvent &Event) {
   if (PIDFilter && Event.PID != *PIDFilter)
     return;
 
-  // Drop the event if its image is loaded at the same address
-  if (Event.Address == Binary->getBaseAddress()) {
+  auto MMapContainsFileOffset = [&](uint64_t FileOffset) {
+    return Event.Offset == FileOffset ||
+           (Event.MemProtectionFlag.contains("x") &&
+            Event.Offset < FileOffset &&
+            FileOffset - Event.Offset < Event.Size);
+  };
+  const bool MMapContainsTextSegment =
+      MMapContainsFileOffset(Binary->getTextSegmentOffset());
+
+  // For user-space ELF, subtract the mmap file offset to get the runtime
+  // address corresponding to file offset zero. Kernel and COFF retain their
+  // existing mmap address semantics.
+  const uint64_t RuntimeBaseAddress = IsKernel || Binary->isCOFF()
+                                          ? Event.Address
+                                          : Event.Address - Event.Offset;
+
+  // Drop the event if its image has the same base address.
+  if ((IsKernel || MMapContainsTextSegment) &&
+      RuntimeBaseAddress == Binary->getBaseAddress()) {
     Binary->setIsLoadedByMMap(true);
     return;
   }
 
-  if (IsKernel || Event.Offset == Binary->getTextSegmentOffset()) {
+  if (IsKernel || MMapContainsTextSegment) {
     // A binary image could be unloaded and then reloaded at different
     // place, so update binary load address.
     // Only update for the first executable segment and assume all other
     // segments are loaded at consecutive memory addresses, which is the case on
     // X64.
-    Binary->setBaseAddress(Event.Address);
+    Binary->setBaseAddress(RuntimeBaseAddress);
     Binary->setIsLoadedByMMap(true);
   } else {
     // Verify segments are loaded consecutively.
     const auto &Offsets = Binary->getTextSegmentOffsets();
     auto It = llvm::lower_bound(Offsets, Event.Offset);
-    if (It != Offsets.end() && *It == Event.Offset) {
+    if (It != Offsets.end() && MMapContainsFileOffset(*It)) {
----------------
HighW4y2H3ll wrote:

Can you check if `*It == Event.Offset` still holds since both shall be file offsets?

https://github.com/llvm/llvm-project/pull/217887


More information about the llvm-commits mailing list