Skip to content

Commit

Permalink
Simplify output section ownership.
Browse files Browse the repository at this point in the history
This patch simplifies output section management by making
Factory class have ownership of sections that creates.

Differential Revision: https://reviews.llvm.org/D22575

llvm-svn: 276141
  • Loading branch information
rui314 committed Jul 20, 2016
1 parent 683170b commit a7f7884
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
7 changes: 4 additions & 3 deletions lld/ELF/LinkerScript.cpp
Expand Up @@ -208,17 +208,18 @@ bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
}

template <class ELFT>
std::vector<std::unique_ptr<OutputSectionBase<ELFT>>>
std::vector<OutputSectionBase<ELFT> *>
LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Result;
std::vector<OutputSectionBase<ELFT> *> Result;

// Add input section to output section. If there is no output section yet,
// then create it and add to output section list.
auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name) {
OutputSectionBase<ELFT> *Sec;
bool IsNew;
std::tie(Sec, IsNew) = Factory.create(C, Name);
if (IsNew)
Result.emplace_back(Sec);
Result.push_back(Sec);
Sec->addSection(C);
};

Expand Down
5 changes: 3 additions & 2 deletions lld/ELF/LinkerScript.h
Expand Up @@ -92,12 +92,13 @@ template <class ELFT> class LinkerScript {
public:
typedef PhdrEntry<ELFT> Phdr;

std::vector<OutputSectionBase<ELFT> *>
createSections(OutputSectionFactory<ELFT> &Factory);

StringRef getOutputSection(InputSectionBase<ELFT> *S);
ArrayRef<uint8_t> getFiller(StringRef Name);
bool isDiscarded(InputSectionBase<ELFT> *S);
bool shouldKeep(InputSectionBase<ELFT> *S);
std::vector<std::unique_ptr<OutputSectionBase<ELFT>>>
createSections(OutputSectionFactory<ELFT> &Factory);
void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
int compareSections(StringRef A, StringRef B);
void addScriptedSymbols();
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/OutputSections.cpp
Expand Up @@ -1770,6 +1770,7 @@ OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
Sec = new MipsOptionsOutputSection<ELFT>();
break;
}
OwningSections.emplace_back(Sec);
return {Sec, true};
}

Expand Down
1 change: 1 addition & 0 deletions lld/ELF/OutputSections.h
Expand Up @@ -685,6 +685,7 @@ template <class ELFT> class OutputSectionFactory {
Key createKey(InputSectionBase<ELFT> *C, StringRef OutsecName);

llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> OwningSections;
};

template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Expand Down
20 changes: 7 additions & 13 deletions lld/ELF/Writer.cpp
Expand Up @@ -48,7 +48,7 @@ template <class ELFT> class Writer {

void copyLocalSymbols();
void addReservedSymbols();
std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> createSections();
std::vector<OutputSectionBase<ELFT> *> createSections();
void finalizeSections();
void addPredefinedSections();
bool needsGot();
Expand Down Expand Up @@ -218,13 +218,9 @@ template <class ELFT> void Writer<ELFT>::run() {
copyLocalSymbols();
addReservedSymbols();

std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Sections =
ScriptConfig->DoLayout ? Script<ELFT>::X->createSections(Factory)
: createSections();

for (std::unique_ptr<OutputSectionBase<ELFT>> &S : Sections)
OutputSections.push_back(S.get());

OutputSections = ScriptConfig->DoLayout
? Script<ELFT>::X->createSections(Factory)
: createSections();
finalizeSections();
if (HasError)
return;
Expand Down Expand Up @@ -637,9 +633,8 @@ template <class ELFT> static void sortCtorsDtors(OutputSectionBase<ELFT> *S) {
}

template <class ELFT>
std::vector<std::unique_ptr<OutputSectionBase<ELFT>>>
Writer<ELFT>::createSections() {
std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Result;
std::vector<OutputSectionBase<ELFT> *> Writer<ELFT>::createSections() {
std::vector<OutputSectionBase<ELFT> *> Result;

for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
Symtab.getObjectFiles()) {
Expand All @@ -652,8 +647,7 @@ Writer<ELFT>::createSections() {
bool IsNew;
std::tie(Sec, IsNew) = Factory.create(C, getOutputSectionName(C));
if (IsNew)
Result.emplace_back(Sec);

Result.push_back(Sec);
Sec->addSection(C);
}
}
Expand Down

0 comments on commit a7f7884

Please sign in to comment.