Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -1081,6 +1081,12 @@ if (!Config->Relocatable) InputSections.push_back(createCommentSection()); + // After this point there are no more common symbols + // unless this is a -r link and we are preserving + // common symbols. + if (Config->DefineCommon) + commonToBss(); + // Do size optimizations: garbage collection, merging of SHF_MERGE sections // and identical code folding. if (Config->GcSections) Index: ELF/MarkLive.cpp =================================================================== --- ELF/MarkLive.cpp +++ ELF/MarkLive.cpp @@ -64,11 +64,6 @@ std::function Fn) { SymbolBody &B = Sec.getFile()->getRelocTargetSym(Rel); - if (auto *Sym = dyn_cast(&B)) { - Sym->Live = true; - return; - } - if (auto *D = dyn_cast(&B)) { if (!D->Section) return; @@ -228,8 +223,6 @@ Enqueue(IS, D->Value); return; } - if (auto *S = dyn_cast_or_null(Sym)) - S->Live = true; }; // Add GC root symbols. Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -167,11 +167,6 @@ return S->kind() == SymbolBody::DefinedCommonKind; } - // True if this symbol is not GC'ed. Liveness is usually a notion of - // input sections and not of symbols, but since common symbols don't - // belong to any input section, their liveness is managed by this bit. - bool Live; - // The maximum alignment we have seen for this symbol. uint32_t Alignment; Index: ELF/Symbols.cpp =================================================================== --- ELF/Symbols.cpp +++ ELF/Symbols.cpp @@ -99,14 +99,8 @@ } return VA; } - case SymbolBody::DefinedCommonKind: { - if (!Config->DefineCommon) - return 0; - auto DC = cast(Body); - if (!DC.Live) - return 0; - return DC.Section->getParent()->Addr + DC.Section->OutSecOff; - } + case SymbolBody::DefinedCommonKind: + llvm_unreachable("unreachable: common are converted to bss"); case SymbolBody::SharedKind: { auto &SS = cast(Body); if (SS.CopyRelSec) @@ -286,7 +280,7 @@ uint8_t StOther, uint8_t Type) : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther, Type), - Live(!Config->GcSections), Alignment(Alignment), Size(Size) {} + Alignment(Alignment), Size(Size) {} // If a shared symbol is referred via a copy relocation, its alignment // becomes part of the ABI. This function returns a symbol alignment. Index: ELF/SyntheticSections.h =================================================================== --- ELF/SyntheticSections.h +++ ELF/SyntheticSections.h @@ -741,7 +741,7 @@ size_t Size = 0; }; -std::vector createCommonSections(); +template void commonToBss(); InputSection *createInterpSection(); template MergeInputSection *createCommentSection(); void decompressAndMergeSections(); Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -54,24 +54,29 @@ return 0; } -std::vector elf::createCommonSections() { - if (!Config->DefineCommon) - return {}; - - std::vector Ret; +template void elf::commonToBss() { for (Symbol *S : Symtab->getSymbols()) { auto *Sym = dyn_cast(S->body()); - if (!Sym || !Sym->Live) + + if (!Sym) continue; - Sym->Section = make("COMMON"); - size_t Pos = Sym->Section->reserveSpace(Sym->Size, Sym->Alignment); - assert(Pos == 0); - (void)Pos; - Sym->Section->File = Sym->getFile(); - Ret.push_back(Sym->Section); + // Create a synthetic section for the common data. + auto *Section = make("COMMON"); + InputSections.push_back(Section); + Section->reserveSpace(Sym->Size, Sym->Alignment); + InputFile *File = Sym->getFile(); + Section->File = File; + Section->Live = !Config->GcSections; + + // Change the symbol to be a regular definition. + Sym->Section = Section; + SymbolBody *B = S->body(); + replaceBody(S, File, B->getName(), B->IsLocal == 1, + B->StOther, B->Type, + 0u, // Value 0 - one symbol per section. + B->getSize(), Section); } - return Ret; } // Returns an LLD version string. @@ -2343,6 +2348,11 @@ template void PltSection::addEntry(SymbolBody &Sym); template void PltSection::addEntry(SymbolBody &Sym); +template void elf::commonToBss(); +template void elf::commonToBss(); +template void elf::commonToBss(); +template void elf::commonToBss(); + template MergeInputSection *elf::createCommentSection(); template MergeInputSection *elf::createCommentSection(); template MergeInputSection *elf::createCommentSection(); Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -290,9 +290,6 @@ Add(InX::BuildId); } - for (InputSection *S : createCommonSections()) - Add(S); - InX::Bss = make(".bss"); Add(InX::Bss); InX::BssRelRo = make(".bss.rel.ro"); @@ -443,9 +440,6 @@ return false; return true; } - - if (auto *Sym = dyn_cast(&B)) - return Sym->Live; return true; }