[llvm] [llvm-objcopy] Fix file offsets when PT_INTERP/PT_LOAD offsets are equal (PR #80562)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 16 13:02:11 PST 2024


================
@@ -1234,6 +1234,13 @@ static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
     return true;
   if (A->OriginalOffset > B->OriginalOffset)
     return false;
+  // If one is PT_LOAD and the other isn't (e.g. PT_INTERP, PT_GNU_RELRO,
+  // PT_TLS), order the PT_LOAD first to ensure ParentSegment relationship will
+  // be correct.
+  bool AIsLOAD = A->Type == PT_LOAD;
+  bool BIsLOAD = B->Type == PT_LOAD;
+  if (AIsLOAD != BIsLOAD)
----------------
MaskRay wrote:

I think I special cased `PT_LOAD` because in practice, for all segments that may overlap with a `PT_LOAD`, treating `PT_LOAD` as the parent/ancestor will work and the parent relationship is conceptually treated as such. Giving it another thought, when a `PT_LOAD` and a `PT_TLS` have the same offset and alignment, it doesn't matter if we make `PT_TLS` the parent. The algorithm will still work as intended.

Switched to the `return A->Align > B->Align;` rule.

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


More information about the llvm-commits mailing list