From 643f94bc620885c176285ccb11957e34c036a185 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 11 Dec 2014 06:22:45 +0000 Subject: [PATCH] [ELF] Remove duplicate constructor code. This piece of code was copied multiple times to each archs. llvm-svn: 224001 --- lld/lib/ReaderWriter/ELF/AArch64/AArch64ELFFile.h | 37 ++--------------- lld/lib/ReaderWriter/ELF/ELFFile.h | 36 ++++++++++------ lld/lib/ReaderWriter/ELF/Hexagon/HexagonELFFile.h | 37 ++--------------- lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h | 50 ++++++----------------- lld/lib/ReaderWriter/ELF/X86/X86ELFFile.h | 37 ++--------------- lld/lib/ReaderWriter/ELF/X86_64/X86_64ELFFile.h | 37 ++--------------- 6 files changed, 52 insertions(+), 182 deletions(-) diff --git a/lld/lib/ReaderWriter/ELF/AArch64/AArch64ELFFile.h b/lld/lib/ReaderWriter/ELF/AArch64/AArch64ELFFile.h index 6249680..09da41d 100644 --- a/lld/lib/ReaderWriter/ELF/AArch64/AArch64ELFFile.h +++ b/lld/lib/ReaderWriter/ELF/AArch64/AArch64ELFFile.h @@ -19,44 +19,15 @@ class AArch64LinkingContext; template class AArch64ELFFile : public ELFFile { public: - AArch64ELFFile(StringRef name, bool atomizeStrings) - : ELFFile(name, atomizeStrings) {} - - AArch64ELFFile(std::unique_ptr mb, bool atomizeStrings, - std::error_code &ec) - : ELFFile(std::move(mb), atomizeStrings, ec) {} + AArch64ELFFile(std::unique_ptr mb, bool atomizeStrings) + : ELFFile(std::move(mb), atomizeStrings) {} static ErrorOr> create(std::unique_ptr mb, bool atomizeStrings) { - std::error_code ec; std::unique_ptr> file( - new AArch64ELFFile(mb->getBufferIdentifier(), atomizeStrings)); - - file->_objFile.reset( - new llvm::object::ELFFile(mb.release()->getBuffer(), ec)); - - if (ec) - return ec; - - // Read input sections from the input file that need to be converted to - // atoms - if ((ec = file->createAtomizableSections())) - return ec; - - // For mergeable strings, we would need to split the section into various - // atoms - if ((ec = file->createMergeableAtoms())) - return ec; - - // Create the necessary symbols that are part of the section that we - // created in createAtomizableSections function - if ((ec = file->createSymbolsFromAtomizableSections())) + new AArch64ELFFile(std::move(mb), atomizeStrings)); + if (std::error_code ec = file->parse()) return ec; - - // Create the appropriate atoms from the file - if ((ec = file->createAtoms())) - return ec; - return std::move(file); } }; diff --git a/lld/lib/ReaderWriter/ELF/ELFFile.h b/lld/lib/ReaderWriter/ELF/ELFFile.h index c5526665..b1fc886 100644 --- a/lld/lib/ReaderWriter/ELF/ELFFile.h +++ b/lld/lib/ReaderWriter/ELF/ELFFile.h @@ -115,8 +115,14 @@ template class ELFFile : public File { typedef typename MergedSectionMapT::iterator MergedSectionMapIterT; public: - ELFFile(StringRef name, bool atomizeStrings = false) - : File(name, kindObject), _ordinal(0), _doStringsMerge(atomizeStrings) {} + ELFFile(StringRef name) + : File(name, kindObject), _ordinal(0), _doStringsMerge(false) {} + + ELFFile(std::unique_ptr mb, bool atomizeStrings = false) + : File(mb->getBufferIdentifier(), kindObject), _mb(std::move(mb)), + _ordinal(0), _doStringsMerge(atomizeStrings) {} + + virtual std::error_code parse(); static ErrorOr> create(std::unique_ptr mb, bool atomizeStrings); @@ -354,6 +360,7 @@ protected: /// \brief Sections that have merge string property std::vector _mergeStringSections; + std::unique_ptr _mb; int64_t _ordinal; /// \brief the cached options relevant while reading the ELF File @@ -412,34 +419,39 @@ ErrorOr>> ELFFile::create(std::unique_ptr mb, bool atomizeStrings) { std::error_code ec; std::unique_ptr> file( - new ELFFile(mb->getBufferIdentifier(), atomizeStrings)); - - file->_objFile.reset( - new llvm::object::ELFFile(mb.release()->getBuffer(), ec)); + new ELFFile(std::move(mb), atomizeStrings)); + if (std::error_code ec = file->parse()) + return ec; + return std::move(file); +} +template +std::error_code ELFFile::parse() { + std::error_code ec; + _objFile.reset( + new llvm::object::ELFFile(_mb.release()->getBuffer(), ec)); if (ec) return ec; // Read input sections from the input file that need to be converted to // atoms - if ((ec = file->createAtomizableSections())) + if ((ec = createAtomizableSections())) return ec; // For mergeable strings, we would need to split the section into various // atoms - if ((ec = file->createMergeableAtoms())) + if ((ec = createMergeableAtoms())) return ec; // Create the necessary symbols that are part of the section that we // created in createAtomizableSections function - if ((ec = file->createSymbolsFromAtomizableSections())) + if ((ec = createSymbolsFromAtomizableSections())) return ec; // Create the appropriate atoms from the file - if ((ec = file->createAtoms())) + if ((ec = createAtoms())) return ec; - - return std::move(file); + return std::error_code(); } template Reference::KindArch ELFFile::kindArch() { diff --git a/lld/lib/ReaderWriter/ELF/Hexagon/HexagonELFFile.h b/lld/lib/ReaderWriter/ELF/Hexagon/HexagonELFFile.h index 6d93ed4..e892a20 100644 --- a/lld/lib/ReaderWriter/ELF/Hexagon/HexagonELFFile.h +++ b/lld/lib/ReaderWriter/ELF/Hexagon/HexagonELFFile.h @@ -114,44 +114,15 @@ template class HexagonELFFile : public ELFFile { typedef llvm::object::Elf_Shdr_Impl Elf_Shdr; public: - HexagonELFFile(StringRef name, bool atomizeStrings) - : ELFFile(name, atomizeStrings) {} - - HexagonELFFile(std::unique_ptr mb, bool atomizeStrings, - std::error_code &ec) - : ELFFile(std::move(mb), atomizeStrings, ec) {} + HexagonELFFile(std::unique_ptr mb, bool atomizeStrings) + : ELFFile(std::move(mb), atomizeStrings) {} static ErrorOr> create(std::unique_ptr mb, bool atomizeStrings) { - std::error_code ec; std::unique_ptr> file( - new HexagonELFFile(mb->getBufferIdentifier(), atomizeStrings)); - - file->_objFile.reset( - new llvm::object::ELFFile(mb.release()->getBuffer(), ec)); - - if (ec) - return ec; - - // Read input sections from the input file that need to be converted to - // atoms - if ((ec = file->createAtomizableSections())) - return ec; - - // For mergeable strings, we would need to split the section into various - // atoms - if ((ec = file->createMergeableAtoms())) - return ec; - - // Create the necessary symbols that are part of the section that we - // created in createAtomizableSections function - if ((ec = file->createSymbolsFromAtomizableSections())) + new HexagonELFFile(std::move(mb), atomizeStrings)); + if (std::error_code ec = file->parse()) return ec; - - // Create the appropriate atoms from the file - if ((ec = file->createAtoms())) - return ec; - return std::move(file); } diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h b/lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h index dfd8501..6526b03 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h @@ -80,49 +80,15 @@ public: template class MipsELFFile : public ELFFile { public: - MipsELFFile(StringRef name, bool atomizeStrings) - : ELFFile(name, atomizeStrings) {} - - MipsELFFile(std::unique_ptr mb, bool atomizeStrings, - std::error_code &ec) - : ELFFile(std::move(mb), atomizeStrings, ec) {} + MipsELFFile(std::unique_ptr mb, bool atomizeStrings) + : ELFFile(std::move(mb), atomizeStrings) {} static ErrorOr> create(std::unique_ptr mb, bool atomizeStrings) { - std::error_code ec; std::unique_ptr> file( - new MipsELFFile(mb->getBufferIdentifier(), atomizeStrings)); - - file->_objFile.reset( - new llvm::object::ELFFile(mb.release()->getBuffer(), ec)); - - if (ec) - return ec; - - // Read input sections from the input file that need to be converted to - // atoms - if ((ec = file->createAtomizableSections())) - return ec; - - // For mergeable strings, we would need to split the section into various - // atoms - if ((ec = file->createMergeableAtoms())) - return ec; - - // Create the necessary symbols that are part of the section that we - // created in createAtomizableSections function - if ((ec = file->createSymbolsFromAtomizableSections())) - return ec; - - // Create the appropriate atoms from the file - if ((ec = file->createAtoms())) + new MipsELFFile(std::move(mb), atomizeStrings)); + if (std::error_code ec = file->parse()) return ec; - - // Retrieve some auxiliary data like GP value, TLS section address etc - // from the object file. - if ((ec = file->readAuxData())) - return ec; - return std::move(file); } @@ -137,6 +103,14 @@ public: uint64_t getTPOffset() const { return *_tpOff; } uint64_t getDTPOffset() const { return *_dtpOff; } + std::error_code parse() override { + if (std::error_code ec = ELFFile::parse()) + return ec; + // Retrieve some auxiliary data like GP value, TLS section address etc + // from the object file. + return readAuxData(); + } + private: typedef llvm::object::Elf_Sym_Impl Elf_Sym; typedef llvm::object::Elf_Shdr_Impl Elf_Shdr; diff --git a/lld/lib/ReaderWriter/ELF/X86/X86ELFFile.h b/lld/lib/ReaderWriter/ELF/X86/X86ELFFile.h index 9e2ecca..516e9a2 100644 --- a/lld/lib/ReaderWriter/ELF/X86/X86ELFFile.h +++ b/lld/lib/ReaderWriter/ELF/X86/X86ELFFile.h @@ -19,44 +19,15 @@ class X86LinkingContext; template class X86ELFFile : public ELFFile { public: - X86ELFFile(StringRef name, bool atomizeStrings) - : ELFFile(name, atomizeStrings) {} - - X86ELFFile(std::unique_ptr mb, bool atomizeStrings, - std::error_code &ec) - : ELFFile(std::move(mb), atomizeStrings, ec) {} + X86ELFFile(std::unique_ptr mb, bool atomizeStrings) + : ELFFile(std::move(mb), atomizeStrings) {} static ErrorOr> create(std::unique_ptr mb, bool atomizeStrings) { - std::error_code ec; std::unique_ptr> file( - new X86ELFFile(mb->getBufferIdentifier(), atomizeStrings)); - - file->_objFile.reset( - new llvm::object::ELFFile(mb.release()->getBuffer(), ec)); - - if (ec) - return ec; - - // Read input sections from the input file that need to be converted to - // atoms - if ((ec = file->createAtomizableSections())) - return ec; - - // For mergeable strings, we would need to split the section into various - // atoms - if ((ec = file->createMergeableAtoms())) - return ec; - - // Create the necessary symbols that are part of the section that we - // created in createAtomizableSections function - if ((ec = file->createSymbolsFromAtomizableSections())) + new X86ELFFile(std::move(mb), atomizeStrings)); + if (std::error_code ec = file->parse()) return ec; - - // Create the appropriate atoms from the file - if ((ec = file->createAtoms())) - return ec; - return std::move(file); } }; diff --git a/lld/lib/ReaderWriter/ELF/X86_64/X86_64ELFFile.h b/lld/lib/ReaderWriter/ELF/X86_64/X86_64ELFFile.h index e397b74..24854e5 100644 --- a/lld/lib/ReaderWriter/ELF/X86_64/X86_64ELFFile.h +++ b/lld/lib/ReaderWriter/ELF/X86_64/X86_64ELFFile.h @@ -19,44 +19,15 @@ class X86_64LinkingContext; template class X86_64ELFFile : public ELFFile { public: - X86_64ELFFile(StringRef name, bool atomizeStrings) - : ELFFile(name, atomizeStrings) {} - - X86_64ELFFile(std::unique_ptr mb, bool atomizeStrings, - std::error_code &ec) - : ELFFile(std::move(mb), atomizeStrings, ec) {} + X86_64ELFFile(std::unique_ptr mb, bool atomizeStrings) + : ELFFile(std::move(mb), atomizeStrings) {} static ErrorOr> create(std::unique_ptr mb, bool atomizeStrings) { - std::error_code ec; std::unique_ptr> file( - new X86_64ELFFile(mb->getBufferIdentifier(), atomizeStrings)); - - file->_objFile.reset( - new llvm::object::ELFFile(mb.release()->getBuffer(), ec)); - - if (ec) - return ec; - - // Read input sections from the input file that need to be converted to - // atoms - if ((ec = file->createAtomizableSections())) - return ec; - - // For mergeable strings, we would need to split the section into various - // atoms - if ((ec = file->createMergeableAtoms())) - return ec; - - // Create the necessary symbols that are part of the section that we - // created in createAtomizableSections function - if ((ec = file->createSymbolsFromAtomizableSections())) + new X86_64ELFFile(std::move(mb), atomizeStrings)); + if (std::error_code ec = file->parse()) return ec; - - // Create the appropriate atoms from the file - if ((ec = file->createAtoms())) - return ec; - return std::move(file); } }; -- 2.7.4