diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index f092691517b9..7101c7fa4332 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -663,17 +663,6 @@ template int LinkerScript::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 -int LinkerScript::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 bool LinkerScript::hasPhdrsCommands() { return !Opt.PhdrsCommands.empty(); } diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index edd098da6e16..af26ac0120c2 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -193,7 +193,6 @@ public: bool shouldKeep(InputSectionBase *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 *> *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 getPhdrIndices(StringRef SectionName); size_t getPhdrIndex(StringRef PhdrName); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 372dd42e0d77..b3148a4845a5 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -451,14 +451,19 @@ static bool compareSections(OutputSectionBase *A, if (AIsAlloc != BIsAlloc) return AIsAlloc; - int Comp = Script::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::X->getSectionIndex(A->getName()); + int BIndex = Script::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 *A, return getPPC64SectionRank(A->getName()) < getPPC64SectionRank(B->getName()); - return false; + // Just put linker script sections first to satisfy strict weak ordering. + return AInScript; } template static bool isDiscarded(InputSectionBase *S) { diff --git a/lld/test/ELF/linkerscript/orphans.s b/lld/test/ELF/linkerscript/orphans.s index fa7d30bf7f7c..0a8c7ab1129b 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 f0f0d827129b..f7401413c53c 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 000000000000..ac990b9a658b --- /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