Revert "Revert "Only restrict order if both sections are in the script.""

This reverts commit r282021, bringing back r282015.

The problem was that the comparison function was not a strict weak
ordering anymore, which this patch fixes.

Original message:

Only restrict order if both sections are in the script.

This matches gold and bfd behavior and is required to handle some scripts.

The script has to assume where PT_LOADs start in order to align that
spot. If we don't allow section it doesn't know about to move to the
middle, we can need more PT_LOADs and those will not be aligned.

llvm-svn: 282035
This commit is contained in:
Rafael Espindola 2016-09-20 22:43:15 +00:00
parent 9780fc1451
commit b6b8f6c308
6 changed files with 36 additions and 25 deletions

View File

@ -663,17 +663,6 @@ template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
return INT_MAX;
}
// A compartor to sort output sections. Returns -1 or 1 if
// A or B are mentioned in linker script. Otherwise, returns 0.
template <class ELFT>
int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
int I = getSectionIndex(A);
int J = getSectionIndex(B);
if (I == INT_MAX && J == INT_MAX)
return 0;
return I < J ? -1 : 1;
}
template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
return !Opt.PhdrsCommands.empty();
}

View File

@ -193,7 +193,6 @@ public:
bool shouldKeep(InputSectionBase<ELFT> *S);
void assignOffsets(OutputSectionCommand *Cmd);
void assignAddresses();
int compareSections(StringRef A, StringRef B);
bool hasPhdrsCommands();
uint64_t getOutputSectionAddress(StringRef Name) override;
uint64_t getOutputSectionSize(StringRef Name) override;
@ -203,6 +202,8 @@ public:
std::vector<OutputSectionBase<ELFT> *> *OutputSections;
int getSectionIndex(StringRef Name);
private:
void computeInputSections(InputSectionDescription *);
@ -216,7 +217,6 @@ private:
// "ScriptConfig" is a bit too long, so define a short name for it.
ScriptConfiguration &Opt = *ScriptConfig;
int getSectionIndex(StringRef Name);
std::vector<size_t> getPhdrIndices(StringRef SectionName);
size_t getPhdrIndex(StringRef PhdrName);

View File

@ -451,14 +451,19 @@ static bool compareSections(OutputSectionBase<ELFT> *A,
if (AIsAlloc != BIsAlloc)
return AIsAlloc;
int Comp = Script<ELFT>::X->compareSections(A->getName(), B->getName());
if (Comp != 0)
return Comp < 0;
// If A and B are mentioned in linker script, use that order.
int AIndex = Script<ELFT>::X->getSectionIndex(A->getName());
int BIndex = Script<ELFT>::X->getSectionIndex(B->getName());
bool AInScript = AIndex != INT_MAX;
bool BInScript = BIndex != INT_MAX;
if (AInScript && BInScript)
return AIndex < BIndex;
// We don't have any special requirements for the relative order of
// two non allocatable sections.
// We don't have any special requirements for the relative order of two non
// allocatable sections.
// Just put linker script sections first to satisfy strict weak ordering.
if (!AIsAlloc)
return false;
return AInScript;
// We want the read only sections first so that they go in the PT_LOAD
// covering the program headers at the start of the file.
@ -507,7 +512,8 @@ static bool compareSections(OutputSectionBase<ELFT> *A,
return getPPC64SectionRank(A->getName()) <
getPPC64SectionRank(B->getName());
return false;
// Just put linker script sections first to satisfy strict weak ordering.
return AInScript;
}
template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {

View File

@ -14,8 +14,8 @@
# TEXTORPHAN: Sections:
# TEXTORPHAN-NEXT: Idx Name
# TEXTORPHAN-NEXT: 0
# TEXTORPHAN-NEXT: 1 .writable
# TEXTORPHAN-NEXT: 2 .text
# TEXTORPHAN-NEXT: 1 .text
# TEXTORPHAN-NEXT: 2 .writable
# WRITABLEORPHAN: Sections:
# WRITABLEORPHAN-NEXT: Idx Name

View File

@ -6,13 +6,13 @@
# RUN: llvm-readobj -t %t1 | FileCheck %s
# CHECK: Name: foo1
# CHECK-NEXT: Value: 0x200
# CHECK-NEXT: Value: 0x288
# CHECK: Name: foo2
# CHECK-NEXT: Value: 0x208
# CHECK-NEXT: Value: 0x290
# CHECK: Name: foo3
# CHECK-NEXT: Value: 0x20C
# CHECK-NEXT: Value: 0x294
.section .foo.1,"a"
.long 1

View File

@ -0,0 +1,16 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
# RUN: echo "SECTIONS { foo : {*(foo)} }" > %t.script
# RUN: ld.lld -o %t1 --script %t.script %t -shared
# RUN: llvm-readobj -elf-output-style=GNU -s %t1 | FileCheck %s
# CHECK: .dynsym {{.*}} A
# CHECK-NEXT: .hash {{.*}} A
# CHECK-NEXT: .dynstr {{.*}} A
# CHECK-NEXT: .text {{.*}} AX
# CHECK-NEXT: .dynamic {{.*}} WA
# CHECK-NEXT: foo {{.*}} WA
.section foo, "aw"
.byte 0