summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lld/ELF/LinkerScript.cpp11
-rw-r--r--lld/ELF/LinkerScript.h4
-rw-r--r--lld/ELF/Writer.cpp22
-rw-r--r--lld/test/ELF/linkerscript/orphans.s4
-rw-r--r--lld/test/ELF/linkerscript/repsection-symbol.s6
-rw-r--r--lld/test/ELF/linkerscript/sort-non-script.s16
6 files changed, 37 insertions, 26 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index f092691517b..7101c7fa433 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -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();
}
diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index edd098da6e1..af26ac0120c 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -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);
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 372dd42e0d7..b3148a4845a 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -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;
-
- // We don't have any special requirements for the relative order of
- // two non allocatable sections.
+ // 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.
+ // 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) {
diff --git a/lld/test/ELF/linkerscript/orphans.s b/lld/test/ELF/linkerscript/orphans.s
index fa7d30bf7f7..0a8c7ab1129 100644
--- a/lld/test/ELF/linkerscript/orphans.s
+++ b/lld/test/ELF/linkerscript/orphans.s
@@ -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
diff --git a/lld/test/ELF/linkerscript/repsection-symbol.s b/lld/test/ELF/linkerscript/repsection-symbol.s
index f0f0d827129..f7401413c53 100644
--- a/lld/test/ELF/linkerscript/repsection-symbol.s
+++ b/lld/test/ELF/linkerscript/repsection-symbol.s
@@ -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
diff --git a/lld/test/ELF/linkerscript/sort-non-script.s b/lld/test/ELF/linkerscript/sort-non-script.s
new file mode 100644
index 00000000000..ac990b9a658
--- /dev/null
+++ b/lld/test/ELF/linkerscript/sort-non-script.s
@@ -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
OpenPOWER on IntegriCloud