summaryrefslogtreecommitdiffstats
path: root/lld/ELF/Writer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lld/ELF/Writer.cpp')
-rw-r--r--lld/ELF/Writer.cpp46
1 files changed, 12 insertions, 34 deletions
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 99d9323d9d2..f8209e0ccf0 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -68,12 +68,11 @@ private:
void writeSections();
void writeBuildId();
- void addCommonSymbols(std::vector<DefinedCommon *> &Syms);
-
std::unique_ptr<FileOutputBuffer> Buffer;
BumpPtrAllocator Alloc;
std::vector<OutputSectionBase<ELFT> *> OutputSections;
+ std::unique_ptr<CommonInputSection<ELFT>> CommonSection;
OutputSectionFactory<ELFT> Factory;
void addRelIpltSymbols();
@@ -223,9 +222,11 @@ template <class ELFT> void Writer<ELFT>::run() {
copyLocalSymbols();
addReservedSymbols();
- OutputSections = ScriptConfig->DoLayout
- ? Script<ELFT>::X->createSections(Factory)
- : createSections();
+ CommonSection = llvm::make_unique<CommonInputSection<ELFT>>();
+ OutputSections =
+ ScriptConfig->DoLayout
+ ? Script<ELFT>::X->createSections(Factory, CommonSection.get())
+ : createSections();
finalizeSections();
if (HasError)
return;
@@ -490,30 +491,6 @@ void PhdrEntry<ELFT>::add(OutputSectionBase<ELFT> *Sec) {
H.p_align = std::max<typename ELFT::uint>(H.p_align, Sec->getAlignment());
}
-// Until this function is called, common symbols do not belong to any section.
-// This function adds them to end of BSS section.
-template <class ELFT>
-void Writer<ELFT>::addCommonSymbols(std::vector<DefinedCommon *> &Syms) {
- if (Syms.empty())
- return;
-
- // Sort the common symbols by alignment as an heuristic to pack them better.
- std::stable_sort(Syms.begin(), Syms.end(),
- [](const DefinedCommon *A, const DefinedCommon *B) {
- return A->Alignment > B->Alignment;
- });
-
- uintX_t Off = Out<ELFT>::Bss->getSize();
- for (DefinedCommon *C : Syms) {
- Off = alignTo(Off, C->Alignment);
- Out<ELFT>::Bss->updateAlignment(C->Alignment);
- C->OffsetInBss = Off;
- Off += C->Size;
- }
-
- Out<ELFT>::Bss->setSize(Off);
-}
-
template <class ELFT>
static Symbol *addOptionalSynthetic(SymbolTable<ELFT> &Table, StringRef Name,
OutputSectionBase<ELFT> *Sec,
@@ -734,7 +711,6 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// Now that we have defined all possible symbols including linker-
// synthesized ones. Visit all symbols to give the finishing touches.
- std::vector<DefinedCommon *> CommonSymbols;
for (Symbol *S : Symtab.getSymbols()) {
SymbolBody *Body = S->body();
@@ -743,9 +719,6 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
if (S->IsUsedInRegularObj && Body->isUndefined() && !S->isWeak())
reportUndefined<ELFT>(Symtab, Body);
- if (auto *C = dyn_cast<DefinedCommon>(Body))
- CommonSymbols.push_back(C);
-
if (!includeInSymtab<ELFT>(*Body))
continue;
if (Out<ELFT>::SymTab)
@@ -763,7 +736,12 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
if (HasError)
return;
- addCommonSymbols(CommonSymbols);
+ // If linker script processor hasn't added common symbol section yet,
+ // then add it to .bss now.
+ if (!CommonSection->OutSec) {
+ Out<ELFT>::Bss->addSection(CommonSection.get());
+ Out<ELFT>::Bss->assignOffsets();
+ }
// So far we have added sections from input object files.
// This function adds linker-created Out<ELFT>::* sections.
OpenPOWER on IntegriCloud