Fix a comparison function to actually be a SWO so that it conforms to
authorChandler Carruth <chandlerc@gmail.com>
Wed, 11 Mar 2015 21:34:33 +0000 (21:34 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Wed, 11 Mar 2015 21:34:33 +0000 (21:34 +0000)
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

llvm-svn: 231968

lld/lib/ReaderWriter/ELF/SegmentChunks.h
lld/test/elf/phdr.test
lld/test/elf/rosegment.test

index 515813c..b91d979 100644 (file)
@@ -346,6 +346,9 @@ bool Segment<ELFT>::compareSegments(Segment<ELFT> *sega, Segment<ELFT> *segb) {
   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(Segment<ELFT> *sega, Segment<ELFT> *segb) {
     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>
index 755cb87..c8ab73d 100644 (file)
@@ -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
@@ -94,6 +82,18 @@ 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:   }
 
 X86_64: LOAD off    0x0000000000000000
 X86_64: LOAD off    0x0000000000001000
index eab2a90..32638d8 100644 (file)
@@ -11,8 +11,8 @@ RUN: llvm-readobj -program-headers %t2.elf | FileCheck %s -check-prefix=RO-SEGME
 #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.elf | FileCheck %s -check-prefix=RO-SEGME
 #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