Sort orphan section if --symbol-ordering-file is given.

Before this patch orphan sections were not sorted.

llvm-svn: 323779
This commit is contained in:
Rafael Espindola 2018-01-30 16:20:08 +00:00
parent 1f59ae311b
commit 22d533568b
6 changed files with 22 additions and 36 deletions

View File

@ -285,23 +285,11 @@ static void sortSections(MutableArrayRef<InputSection *> Vec,
// --sort-section is handled as an inner SORT command.
// 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
// 4. If no SORT command is given, sort according to --sort-section.
// 5. If no SORT commands are given and --sort-section is not specified,
// apply sorting provided by --symbol-ordering-file if any exist.
static void sortInputSections(
MutableArrayRef<InputSection *> Vec, const SectionPattern &Pat,
const DenseMap<SectionBase *, int> &Order) {
static void sortInputSections(MutableArrayRef<InputSection *> Vec,
const SectionPattern &Pat) {
if (Pat.SortOuter == SortSectionPolicy::None)
return;
if (Pat.SortOuter == SortSectionPolicy::Default &&
Config->SortSection == SortSectionPolicy::Default) {
// If -symbol-ordering-file was given, sort accordingly.
// Usually, Order is empty.
if (!Order.empty())
sortByOrder(Vec, [&](InputSectionBase *S) { return Order.lookup(S); });
return;
}
if (Pat.SortInner == SortSectionPolicy::Default)
sortSections(Vec, Config->SortSection);
else
@ -311,8 +299,7 @@ static void sortInputSections(
// Compute and remember which sections the InputSectionDescription matches.
std::vector<InputSection *>
LinkerScript::computeInputSections(const InputSectionDescription *Cmd,
const DenseMap<SectionBase *, int> &Order) {
LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
std::vector<InputSection *> Ret;
// Collects all sections that satisfy constraints of Cmd.
@ -343,7 +330,7 @@ LinkerScript::computeInputSections(const InputSectionDescription *Cmd,
}
sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore),
Pat, Order);
Pat);
}
return Ret;
}
@ -360,13 +347,13 @@ void LinkerScript::discard(ArrayRef<InputSection *> V) {
}
}
std::vector<InputSection *> LinkerScript::createInputSectionList(
OutputSection &OutCmd, const DenseMap<SectionBase *, int> &Order) {
std::vector<InputSection *>
LinkerScript::createInputSectionList(OutputSection &OutCmd) {
std::vector<InputSection *> Ret;
for (BaseCommand *Base : OutCmd.SectionCommands) {
if (auto *Cmd = dyn_cast<InputSectionDescription>(Base)) {
Cmd->Sections = computeInputSections(Cmd, Order);
Cmd->Sections = computeInputSections(Cmd);
Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
}
}
@ -395,7 +382,6 @@ void LinkerScript::processSectionCommands() {
Ctx->OutSec = Aether;
size_t I = 0;
DenseMap<SectionBase *, int> Order = buildSectionOrder();
// Add input sections to output sections.
for (BaseCommand *Base : SectionCommands) {
// Handle symbol assignments outside of any output section.
@ -405,7 +391,7 @@ void LinkerScript::processSectionCommands() {
}
if (auto *Sec = dyn_cast<OutputSection>(Base)) {
std::vector<InputSection *> V = createInputSectionList(*Sec, Order);
std::vector<InputSection *> V = createInputSectionList(*Sec);
// The output section name `/DISCARD/' is special.
// Any input section assigned to it is discarded.

View File

@ -217,12 +217,9 @@ class LinkerScript final {
void setDot(Expr E, const Twine &Loc, bool InSec);
std::vector<InputSection *>
computeInputSections(const InputSectionDescription *,
const llvm::DenseMap<SectionBase *, int> &Order);
computeInputSections(const InputSectionDescription *);
std::vector<InputSection *>
createInputSectionList(OutputSection &Cmd,
const llvm::DenseMap<SectionBase *, int> &Order);
std::vector<InputSection *> createInputSectionList(OutputSection &Cmd);
std::vector<size_t> getPhdrIndices(OutputSection *Sec);

View File

@ -134,7 +134,7 @@ void OutputSection::addSection(InputSection *IS) {
}
}
void elf::sortByOrder(MutableArrayRef<InputSection *> In,
static void sortByOrder(MutableArrayRef<InputSection *> In,
std::function<int(InputSectionBase *S)> Order) {
typedef std::pair<int, InputSection *> Pair;
auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };

View File

@ -143,8 +143,6 @@ namespace lld {
namespace elf {
uint64_t getHeaderSize();
void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
std::function<int(InputSectionBase *S)> Order);
extern std::vector<OutputSection *> OutputSections;
} // namespace elf

View File

@ -1018,10 +1018,8 @@ findOrphanPos(std::vector<BaseCommand *>::iterator B,
}
// If no layout was provided by linker script, we want to apply default
// sorting for special input sections and handle --symbol-ordering-file.
// sorting for special input sections. This also handles --symbol-ordering-file.
template <class ELFT> void Writer<ELFT>::sortInputSections() {
assert(!Script->HasSectionsCommand);
// Sort input sections by priority using the list provided
// by --symbol-ordering-file.
DenseMap<SectionBase *, int> Order = buildSectionOrder();
@ -1031,6 +1029,9 @@ template <class ELFT> void Writer<ELFT>::sortInputSections() {
if (Sec->Live)
Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); });
if (Script->HasSectionsCommand)
return;
// Sort input sections by section name suffixes for
// __attribute__((init_priority(N))).
if (OutputSection *Sec = findSection(".init_array"))
@ -1057,9 +1058,9 @@ template <class ELFT> void Writer<ELFT>::sortSections() {
if (auto *Sec = dyn_cast<OutputSection>(Base))
Sec->SortRank = getSectionRank(Sec);
if (!Script->HasSectionsCommand) {
sortInputSections();
if (!Script->HasSectionsCommand) {
// We know that all the OutputSections are contiguous in this case.
auto E = Script->SectionCommands.end();
auto I = Script->SectionCommands.begin();

View File

@ -14,6 +14,10 @@
# AFTER: Contents of section .foo:
# AFTER-NEXT: 2211
# RUN: echo "SECTIONS { .text : { *(.text) } }" > %t2.script
# RUN: ld.lld --symbol-ordering-file %t.ord %t.o --script %t2.script -o %t3.out
# RUN: llvm-objdump -s %t3.out| FileCheck %s --check-prefix=AFTER
.section .foo,"ax",@progbits,unique,1
_foo1:
.byte 0x11