[lld] r231968 - Fix a comparison function to actually be a SWO so that it conforms to
Chandler Carruth
chandlerc at gmail.com
Wed Mar 11 14:34:34 PDT 2015
Author: chandlerc
Date: Wed Mar 11 16:34:33 2015
New Revision: 231968
URL: http://llvm.org/viewvc/llvm-project?rev=231968&view=rev
Log:
Fix a comparison function to actually be a SWO so that it conforms to
the spec required by std::sort and friends.
Ordering things this way also dramatically simplifies the code as
short-circuit ensures we can skip all of the negative tests.
I've left one FIXME where we're establishing a fairly arbitrary
ordering. Previously, the function compared all types as equal except
for the ones it explicitly handled, but it didn't delegate correctly to
the atomflags when doing so, and so it would fail to be a SWO. The two
possible fixes are to stop comparing the atom flags entirely, or to
establish some arbitrary ordering of the types.
Since it was pure luck which ordering of unequal types we ended up with
previously (the caller was std::sort, not std::stable_sort) I chose to
make the ordering explicit and guaranteed. This seems like the best
conservative approach as I suspect we would want to switch to
stable_sort otherwise in order to have deterministic output.
Differential Revision: http://reviews.llvm.org/D8266
Modified:
lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h
lld/trunk/test/elf/phdr.test
lld/trunk/test/elf/rosegment.test
Modified: lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h?rev=231968&r1=231967&r2=231968&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h Wed Mar 11 16:34:33 2015
@@ -346,6 +346,9 @@ bool Segment<ELFT>::compareSegments(Segm
int64_t type1 = sega->segmentType();
int64_t type2 = segb->segmentType();
+ if (type1 == type2)
+ return sega->atomflags() < segb->atomflags();
+
// The single PT_PHDR segment is required to precede any loadable
// segment. We simply make it always first.
if (type1 == llvm::ELF::PT_PHDR)
@@ -361,30 +364,30 @@ bool Segment<ELFT>::compareSegments(Segm
return false;
// We then put PT_LOAD segments before any other segments.
- if (type1 == llvm::ELF::PT_LOAD && type2 != llvm::ELF::PT_LOAD)
+ if (type1 == llvm::ELF::PT_LOAD)
return true;
- if (type2 == llvm::ELF::PT_LOAD && type1 != llvm::ELF::PT_LOAD)
+ if (type2 == llvm::ELF::PT_LOAD)
return false;
- // We put the PT_TLS segment last except for the PT_GNU_RELRO
- // segment, because that is where the dynamic linker expects to find
- if (type1 == llvm::ELF::PT_TLS && type2 != llvm::ELF::PT_TLS &&
- type2 != llvm::ELF::PT_GNU_RELRO)
+ // We put the PT_GNU_RELRO segment last, because that is where the
+ // dynamic linker expects to find it
+ if (type1 == llvm::ELF::PT_GNU_RELRO)
return false;
- if (type2 == llvm::ELF::PT_TLS && type1 != llvm::ELF::PT_TLS &&
- type1 != llvm::ELF::PT_GNU_RELRO)
+ if (type2 == llvm::ELF::PT_GNU_RELRO)
return true;
- // We put the PT_GNU_RELRO segment last, because that is where the
- // dynamic linker expects to find it
- if (type1 == llvm::ELF::PT_GNU_RELRO && type2 != llvm::ELF::PT_GNU_RELRO)
+ // We put the PT_TLS segment last except for the PT_GNU_RELRO
+ // segment, because that is where the dynamic linker expects to find
+ if (type1 == llvm::ELF::PT_TLS)
return false;
- if (type2 == llvm::ELF::PT_GNU_RELRO && type1 != llvm::ELF::PT_GNU_RELRO)
+ if (type2 == llvm::ELF::PT_TLS)
return true;
- if (type1 == type2)
- return sega->atomflags() < segb->atomflags();
- return false;
+ // Otherwise compare the types to establish an arbitrary ordering.
+ // FIXME: Should figure out if we should just make all other types compare
+ // equal, but if so, we should probably do the same for atom flags and change
+ // users of this to use stable_sort.
+ return type1 < type2;
}
template <class ELFT>
Modified: lld/trunk/test/elf/phdr.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/phdr.test?rev=231968&r1=231967&r2=231968&view=diff
==============================================================================
--- lld/trunk/test/elf/phdr.test (original)
+++ lld/trunk/test/elf/phdr.test Wed Mar 11 16:34:33 2015
@@ -71,18 +71,6 @@ I386-NEXT: ]
I386-NEXT: Alignment: 16384
I386-NEXT: }
I386-NEXT: ProgramHeader {
-I386-NEXT: Type: PT_GNU_EH_FRAME (0x6474E550)
-I386-NEXT: Offset: 0x1F4
-I386-NEXT: VirtualAddress: 0x1F4
-I386-NEXT: PhysicalAddress: 0x1F4
-I386-NEXT: FileSize: 8
-I386-NEXT: MemSize: 8
-I386-NEXT: Flags [ (0x4)
-I386-NEXT: PF_R (0x4)
-I386-NEXT: ]
-I386-NEXT: Alignment: 4
-I386-NEXT: }
-I386-NEXT: ProgramHeader {
I386-NEXT: Type: PT_DYNAMIC (0x2)
I386-NEXT: Offset: 0x1FC
I386-NEXT: VirtualAddress: 0x1FC
@@ -92,6 +80,18 @@ I386-NEXT: MemSize: 48
I386-NEXT: Flags [ (0x4)
I386-NEXT: PF_R (0x4)
I386-NEXT: ]
+I386-NEXT: Alignment: 4
+I386-NEXT: }
+I386-NEXT: ProgramHeader {
+I386-NEXT: Type: PT_GNU_EH_FRAME (0x6474E550)
+I386-NEXT: Offset: 0x1F4
+I386-NEXT: VirtualAddress: 0x1F4
+I386-NEXT: PhysicalAddress: 0x1F4
+I386-NEXT: FileSize: 8
+I386-NEXT: MemSize: 8
+I386-NEXT: Flags [ (0x4)
+I386-NEXT: PF_R (0x4)
+I386-NEXT: ]
I386-NEXT: Alignment: 4
I386-NEXT: }
Modified: lld/trunk/test/elf/rosegment.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/rosegment.test?rev=231968&r1=231967&r2=231968&view=diff
==============================================================================
--- lld/trunk/test/elf/rosegment.test (original)
+++ lld/trunk/test/elf/rosegment.test Wed Mar 11 16:34:33 2015
@@ -11,8 +11,8 @@ RUN: llvm-readobj -program-headers %t2.e
#NORO-SEGMENT: Type: PT_INTERP
#NORO-SEGMENT: Type: PT_LOAD
#NORO-SEGMENT: Type: PT_LOAD
-#NORO-SEGMENT: Type: PT_GNU_EH_FRAME
#NORO-SEGMENT: Type: PT_DYNAMIC
+#NORO-SEGMENT: Type: PT_GNU_EH_FRAME
#RO-SEGMENT: Type: PT_PHDR
#RO-SEGMENT: Type: PT_INTERP
@@ -22,5 +22,5 @@ RUN: llvm-readobj -program-headers %t2.e
#RO-SEGMENT: PF_R (0x4)
#RO-SEGMENT: ]
#RO-SEGMENT: Type: PT_LOAD
-#RO-SEGMENT: Type: PT_GNU_EH_FRAME
#RO-SEGMENT: Type: PT_DYNAMIC
+#RO-SEGMENT: Type: PT_GNU_EH_FRAME
More information about the llvm-commits
mailing list