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

Jinjie Huang via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 25 12:55:58 PDT 2026


================
@@ -362,10 +362,11 @@ template <class ELFT>
 void ProfiledBinary::setPreferredTextSegmentAddresses(const ELFFile<ELFT> &Obj,
                                                       StringRef FileName) {
   const auto &PhdrRange = unwrapOrError(Obj.program_headers(), FileName);
-  // FIXME: This should be the page size of the system running profiling.
-  // However such info isn't available at post-processing time, assuming
-  // 4K page now. Note that we don't use EXEC_PAGESIZE from <linux/param.h>
-  // because we may build the tools on non-linux.
+  // The page size of the profiling system cannot be determined from the ELF
+  // binary alone, and using the page size of the post-processing system would
+  // be incorrect. Use 4 KiB as the binary-side baseline. PerfScriptReader
+  // reconciles executable mmap events whose mappings cover the segment start
+  // due to a larger runtime page size.
   uint64_t PageSize = 0x1000;
----------------
Jinjie-Huang wrote:

We can derive the `PreferredBaseAddress` (viewed as the static origin of the file) from `Phdr.p_vaddr - Phdr.p_offset`. Similarly, we can compute the `RuntimeBaseAddress` (viewed as the runtime origin, which is assigned to the baseline's BaseAddress) from `Event.Address - Event.Offset`. They satisfy the relationship: `PreferredBaseAddress = canonicalizeVirtualAddress(RuntimeBaseAddress)`.

This is because what canonicalize essentially does is a coordinate translation—it applies the difference between these two origins to the runtime address. Therefore, we can perform the address translation directly without relying on any PageSize assumption, while keeping the baseline logic entirely unaffected.

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


More information about the llvm-commits mailing list