[PATCH] D58426: llvm-objcopy: Change sectionWithinSegment() to use virtual addresses instead of file offsets.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 19 20:51:32 PST 2019


pcc created this revision.
pcc added a reviewer: rupprecht.
Herald added subscribers: jakehehrlich, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a reviewer: alexshap.
Herald added a project: LLVM.

Without this, sectionWithinSegment() will return the wrong answer for bss
sections. This doesn't seem to matter now (for non-broken ELF files), but
it will matter with a change that I'm working on.

Also use the correct segment addresses in a test so that it continues to pass.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D58426

Files:
  llvm/test/tools/llvm-objcopy/ELF/segment-test-remove-section.test
  llvm/tools/llvm-objcopy/ELF/Object.cpp


Index: llvm/tools/llvm-objcopy/ELF/Object.cpp
===================================================================
--- llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -756,13 +756,15 @@
 // Returns true IFF a section is wholly inside the range of a segment
 static bool sectionWithinSegment(const SectionBase &Section,
                                  const Segment &Segment) {
+  if (!(Section.Flags & SHF_ALLOC))
+    return false;
   // If a section is empty it should be treated like it has a size of 1. This is
   // to clarify the case when an empty section lies on a boundary between two
   // segments and ensures that the section "belongs" to the second segment and
   // not the first.
   uint64_t SecSize = Section.Size ? Section.Size : 1;
-  return Segment.Offset <= Section.OriginalOffset &&
-         Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
+  return Segment.VAddr <= Section.Addr &&
+         Segment.VAddr + Segment.MemSize >= Section.Addr + SecSize;
 }
 
 // Returns true IFF a segment's original offset is inside of another segment's
Index: llvm/test/tools/llvm-objcopy/ELF/segment-test-remove-section.test
===================================================================
--- llvm/test/tools/llvm-objcopy/ELF/segment-test-remove-section.test
+++ llvm/test/tools/llvm-objcopy/ELF/segment-test-remove-section.test
@@ -38,6 +38,8 @@
 ProgramHeaders:
   - Type: PT_LOAD
     Flags: [ PF_X, PF_R ]
+    PAddr: 0x1000
+    VAddr: 0x1000
     Sections:
       - Section: .text
       - Section: .text2
@@ -143,8 +145,8 @@
 #CHECK-NEXT:  ProgramHeader {
 #CHECK-NEXT:    Type: PT_LOAD (0x1)
 #CHECK-NEXT:    Offset: 0x1000
-#CHECK-NEXT:    VirtualAddress: 0x0
-#CHECK-NEXT:    PhysicalAddress: 0x0
+#CHECK-NEXT:    VirtualAddress: 0x1000
+#CHECK-NEXT:    PhysicalAddress: 0x1000
 #CHECK-NEXT:    FileSize: 12288
 #CHECK-NEXT:    MemSize: 12288
 #CHECK-NEXT:    Flags [ (0x5)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58426.187513.patch
Type: text/x-patch
Size: 1956 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190220/bc9e919e/attachment.bin>


More information about the llvm-commits mailing list