Index: lld/ELF/Writer.cpp =================================================================== --- lld/ELF/Writer.cpp +++ lld/ELF/Writer.cpp @@ -1085,6 +1085,47 @@ return SectionOrder; } +static void +sortISDBySectionOrder(InputSectionDescription *ISD, + const DenseMap &Order) { + std::vector UnorderedSections, OrderedSections; + uint64_t UnorderedSize = 0; + + for (InputSection *IS : ISD->Sections) { + if (!Order.count(IS)) { + UnorderedSections.push_back(IS); + UnorderedSize += IS->getSize(); + continue; + } + OrderedSections.push_back(IS); + } + std::sort(OrderedSections.begin(), OrderedSections.end(), + [&](InputSection *A, InputSection *B) { + return Order.lookup(A) < Order.lookup(B); + }); + + // Find an insertion point for the ordered section list in the unordered + // section list. On targets with limited-range branches, this is the mid-point + // of the unordered section list. This decreases the likelihood that a range + // extension thunk will be needed to enter or exit the ordered region. + size_t UnorderedInsPt = 0; + if (Target->ThunkSectionSpacing && !OrderedSections.empty()) { + uint64_t UnorderedPos = 0; + for (; UnorderedInsPt != UnorderedSections.size(); ++UnorderedInsPt) { + UnorderedPos += UnorderedSections[UnorderedInsPt]->getSize(); + if (UnorderedPos > UnorderedSize / 2) + break; + } + } + + std::copy(UnorderedSections.begin(), + UnorderedSections.begin() + UnorderedInsPt, ISD->Sections.begin()); + std::copy(OrderedSections.begin(), OrderedSections.end(), + ISD->Sections.begin() + UnorderedInsPt); + std::copy(UnorderedSections.begin() + UnorderedInsPt, UnorderedSections.end(), + ISD->Sections.begin() + UnorderedInsPt + OrderedSections.size()); +} + static void sortSection(OutputSection *Sec, const DenseMap &Order) { StringRef Name = Sec->Name; @@ -1111,7 +1152,9 @@ // Sort input sections by priority using the list provided // by --symbol-ordering-file. if (!Order.empty()) - Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); }); + for (BaseCommand *B : Sec->SectionCommands) + if (auto *ISD = dyn_cast(B)) + sortISDBySectionOrder(ISD, Order); } // If no layout was provided by linker script, we want to apply default Index: lld/test/ELF/arm-symbol-ordering-file.s =================================================================== --- /dev/null +++ lld/test/ELF/arm-symbol-ordering-file.s @@ -0,0 +1,31 @@ +# RUN: llvm-mc -filetype=obj -triple=armv7-unknown-linux %s -o %t.o + +# RUN: echo ordered > %t_order.txt +# RUN: ld.lld --symbol-ordering-file %t_order.txt %t.o -o %t2.out +# RUN: llvm-nm -n %t2.out | FileCheck %s + +# CHECK: unordered1 +# CHECK-NEXT: unordered2 +# CHECK-NEXT: unordered3 +# CHECK-NEXT: ordered +# CHECK-NEXT: unordered4 + +.section .foo,"ax",%progbits,unique,1 +unordered1: +.zero 1 + +.section .foo,"ax",%progbits,unique,2 +unordered2: +.zero 1 + +.section .foo,"ax",%progbits,unique,3 +unordered3: +.zero 2 + +.section .foo,"ax",%progbits,unique,4 +unordered4: +.zero 4 + +.section .foo,"ax",%progbits,unique,5 +ordered: +.zero 1