diff --git a/llvm/test/tools/llvm-objcopy/ELF/segment-test-remove-section.test b/llvm/test/tools/llvm-objcopy/ELF/segment-test-remove-section.test --- a/llvm/test/tools/llvm-objcopy/ELF/segment-test-remove-section.test +++ b/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) diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp --- a/llvm/tools/llvm-objcopy/ELF/Object.cpp +++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp @@ -770,13 +770,21 @@ // 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; + + bool SectionIsTLS = Section.Flags & SHF_TLS; + bool SegmentIsTLS = Segment.Type == PT_TLS; + if (SectionIsTLS != SegmentIsTLS) + 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