Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -286,7 +286,9 @@ case SHN_UNDEF: return new (this->Alloc) UndefinedElf(Name, *Sym); case SHN_COMMON: - return new (this->Alloc) DefinedCommon(Name, *Sym); + return new (this->Alloc) DefinedCommon( + Name, Sym->st_size, Sym->st_value, + Sym->getBinding() == llvm::ELF::STB_WEAK, Sym->getVisibility()); } switch (Sym->getBinding()) { Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -800,7 +800,7 @@ return SC->OutSec->getVA() + SC->getOffset(DR.Sym); } case SymbolBody::DefinedCommonKind: - return Out::Bss->getVA() + cast>(S).OffsetInBSS; + return Out::Bss->getVA() + cast(S).OffsetInBSS; case SymbolBody::SharedKind: { auto &SS = cast>(S); if (SS.NeedsCopy) @@ -1370,6 +1370,9 @@ if (const Elf_Sym *InputSym = getElfSym(*Body)) { Type = InputSym->getType(); Size = InputSym->st_size; + } else if (const auto *C = dyn_cast(Body)) { + Type = STT_OBJECT; + Size = C->Size; } ESym->setBindingAndType(getSymbolBinding(Body), Type); Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -57,9 +57,9 @@ enum Kind { DefinedFirst, DefinedRegularKind = DefinedFirst, - DefinedCommonKind, SharedKind, DefinedElfLast = SharedKind, + DefinedCommonKind, DefinedSyntheticKind, DefinedLast = DefinedSyntheticKind, UndefinedElfKind, @@ -154,15 +154,10 @@ } }; -template class DefinedCommon : public DefinedElf { - typedef typename llvm::object::ELFFile::Elf_Sym Elf_Sym; - +class DefinedCommon : public Defined { public: - typedef typename llvm::object::ELFFile::uintX_t uintX_t; - DefinedCommon(StringRef N, const Elf_Sym &Sym) - : DefinedElf(SymbolBody::DefinedCommonKind, N, Sym) { - MaxAlignment = Sym.st_value; - } + DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, bool IsWeak, + uint8_t Visibility); static bool classof(const SymbolBody *S) { return S->kind() == SymbolBody::DefinedCommonKind; @@ -170,10 +165,12 @@ // The output offset of this common symbol in the output bss. Computed by the // writer. - uintX_t OffsetInBSS; + uint64_t OffsetInBSS; // The maximum alignment we have seen for this symbol. - uintX_t MaxAlignment; + uint64_t MaxAlignment; + + uint64_t Size; }; // Regular defined symbols read from object file symbol tables. Index: ELF/Symbols.cpp =================================================================== --- ELF/Symbols.cpp +++ ELF/Symbols.cpp @@ -58,10 +58,10 @@ if (isCommon()) { if (!Other->isCommon()) return -1; - auto *ThisC = cast>(this); - auto *OtherC = cast>(Other); + auto *ThisC = cast(this); + auto *OtherC = cast(Other); uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment); - if (ThisC->Sym.st_size >= OtherC->Sym.st_size) { + if (ThisC->Size >= OtherC->Size) { ThisC->MaxAlignment = Align; return 1; } @@ -101,6 +101,13 @@ : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, false), Value(Value), Section(Section) {} +DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, + bool IsWeak, uint8_t Visibility) + : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility, false) { + MaxAlignment = Alignment; + this->Size = Size; +} + std::unique_ptr Lazy::getMember() { MemoryBufferRef MBRef = File->getMember(&Sym); Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -65,7 +65,7 @@ int getPhdrsNum() const; OutputSection *getBSS(); - void addCommonSymbols(std::vector *> &Syms); + void addCommonSymbols(std::vector &Syms); void addSharedCopySymbols(std::vector *> &Syms); std::unique_ptr Buffer; @@ -472,27 +472,24 @@ // Until this function is called, common symbols do not belong to any section. // This function adds them to end of BSS section. template -void Writer::addCommonSymbols(std::vector *> &Syms) { +void Writer::addCommonSymbols(std::vector &Syms) { typedef typename ELFFile::uintX_t uintX_t; - typedef typename ELFFile::Elf_Sym Elf_Sym; 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->MaxAlignment > B->MaxAlignment; - }); + std::stable_sort(Syms.begin(), Syms.end(), + [](const DefinedCommon *A, const DefinedCommon *B) { + return A->MaxAlignment > B->MaxAlignment; + }); uintX_t Off = getBSS()->getSize(); - for (DefinedCommon *C : Syms) { - const Elf_Sym &Sym = C->Sym; + for (DefinedCommon *C : Syms) { uintX_t Align = C->MaxAlignment; Off = RoundUpToAlignment(Off, Align); C->OffsetInBSS = Off; - Off += Sym.st_size; + Off += C->Size; } Out::Bss->setSize(Off); @@ -751,7 +748,7 @@ addIRelocMarkers(Symtab, isOutputDynamic()); - std::vector *> CommonSymbols; + std::vector CommonSymbols; std::vector *> SharedCopySymbols; for (auto &P : Symtab.getSymbols()) { SymbolBody *Body = P.second->Body; @@ -759,7 +756,7 @@ if (!U->isWeak() && !U->canKeepUndefined()) reportUndefined(Symtab, *Body); - if (auto *C = dyn_cast>(Body)) + if (auto *C = dyn_cast(Body)) CommonSymbols.push_back(C); if (auto *SC = dyn_cast>(Body)) if (SC->NeedsCopy)