Skip to content

Commit

Permalink
Fix a comparison function to actually be a SWO so that it conforms to
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chandlerc committed Mar 11, 2015
1 parent df41a30 commit c9cdc50
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
33 changes: 18 additions & 15 deletions lld/lib/ReaderWriter/ELF/SegmentChunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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>
Expand Down
24 changes: 12 additions & 12 deletions lld/test/elf/phdr.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions lld/test/elf/rosegment.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit c9cdc50

Please sign in to comment.