All other singleton instances are accessible globally.
CommonInputSection shouldn't be an exception.
Differential Revision: https://reviews.llvm.org/D22935
llvm-svn: 277034
return S->SectionKind == InputSectionBase<ELFT>::MipsOptions;
}
-// Due to initialization order in C++ memberwise initialization or
-// construction is invoked after base class construction. This helper
-// function is needed to zero initialize Elf_Shdr, before passing it
-// to InputSection<ELFT> constructor
-template <class T> static T *zero(T *Val) {
- return static_cast<T *>(memset(Val, 0, sizeof(*Val)));
-}
-
template <class ELFT>
CommonInputSection<ELFT>::CommonInputSection()
- : InputSection<ELFT>(nullptr, zero(&Hdr)) {
+ : InputSection<ELFT>(nullptr, &Hdr) {
std::vector<DefinedCommon<ELFT> *> Symbols;
Hdr.sh_size = 0;
Hdr.sh_type = SHT_NOBITS;
const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
};
-// A special kind of section used to store common symbols
+// Common symbols don't belong to any section. But it is easier for us
+// to handle them as if they belong to some input section. So we defined
+// this class. CommonInputSection is a virtual singleton class that
+// "contains" all common symbols.
template <class ELFT> class CommonInputSection : public InputSection<ELFT> {
typedef typename ELFT::uint uintX_t;
public:
CommonInputSection();
+ // The singleton instance of this class.
+ static CommonInputSection<ELFT> *X;
+
private:
- typename ELFT::Shdr Hdr;
+ static typename ELFT::Shdr Hdr;
};
+template <class ELFT> CommonInputSection<ELFT> *CommonInputSection<ELFT>::X;
+template <class ELFT> typename ELFT::Shdr CommonInputSection<ELFT>::Hdr;
+
} // namespace elf
} // namespace lld
// Returns input sections filtered by given glob patterns.
template <class ELFT>
std::vector<InputSectionBase<ELFT> *>
-LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I,
- CommonInputSection<ELFT> *Common) {
+LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
ArrayRef<StringRef> Patterns = I->Patterns;
ArrayRef<StringRef> ExcludedFiles = I->ExcludedFiles;
std::vector<InputSectionBase<ELFT> *> Ret;
Ret.push_back(S);
if ((llvm::find(Patterns, "COMMON") != Patterns.end()))
- Ret.push_back(Common);
+ Ret.push_back(CommonInputSection<ELFT>::X);
return Ret;
}
template <class ELFT>
std::vector<OutputSectionBase<ELFT> *>
-LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory,
- CommonInputSection<ELFT> *Common) {
+LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
std::vector<OutputSectionBase<ELFT> *> Ret;
// Add input section to output section. If there is no output section yet,
for (auto &P : getSectionMap()) {
StringRef OutputName = P.first;
const InputSectionDescription *I = P.second;
- for (InputSectionBase<ELFT> *S : getInputSections(I, Common)) {
+ for (InputSectionBase<ELFT> *S : getInputSections(I)) {
if (OutputName == "/DISCARD/") {
S->Live = false;
reportDiscarded(S);
template <class ELFT> class OutputSectionBase;
template <class ELFT> class OutputSectionFactory;
template <class ELFT> class DefinedCommon;
-template <class ELFT> class CommonInputSection;
typedef std::function<uint64_t(uint64_t)> Expr;
public:
std::vector<OutputSectionBase<ELFT> *>
- createSections(OutputSectionFactory<ELFT> &Factory,
- CommonInputSection<ELFT> *Common);
+ createSections(OutputSectionFactory<ELFT> &Factory);
std::vector<PhdrEntry<ELFT>>
createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
getSectionMap();
std::vector<InputSectionBase<ELFT> *>
- getInputSections(const InputSectionDescription *, CommonInputSection<ELFT> *);
+ getInputSections(const InputSectionDescription *);
// "ScriptConfig" is a bit too long, so define a short name for it.
ScriptConfiguration &Opt = *ScriptConfig;
BumpPtrAllocator Alloc;
std::vector<OutputSectionBase<ELFT> *> OutputSections;
- std::unique_ptr<CommonInputSection<ELFT>> CommonSection;
OutputSectionFactory<ELFT> Factory;
void addRelIpltSymbols();
copyLocalSymbols();
addReservedSymbols();
- CommonSection = llvm::make_unique<CommonInputSection<ELFT>>();
+ CommonInputSection<ELFT> Common;
+ CommonInputSection<ELFT>::X = &Common;
+
OutputSections =
ScriptConfig->DoLayout
- ? Script<ELFT>::X->createSections(Factory, CommonSection.get())
+ ? Script<ELFT>::X->createSections(Factory)
: createSections();
finalizeSections();
if (HasError)
// 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());
+ if (!CommonInputSection<ELFT>::X->OutSec) {
+ Out<ELFT>::Bss->addSection(CommonInputSection<ELFT>::X);
Out<ELFT>::Bss->assignOffsets();
}