diff options
author | Jake Ehrlich <jakehehrlich@google.com> | 2018-01-25 22:46:17 +0000 |
---|---|---|
committer | Jake Ehrlich <jakehehrlich@google.com> | 2018-01-25 22:46:17 +0000 |
commit | 76e9110f3dd1c11876793673fe491606dbaaa5d1 (patch) | |
tree | 45a0c0af60c29147d8aed61044af247d60949ae0 /llvm/tools/llvm-objcopy/llvm-objcopy.cpp | |
parent | 8abbc96c188e8fce2e21540921dd5bbb5e189b19 (diff) | |
download | bcm5719-llvm-76e9110f3dd1c11876793673fe491606dbaaa5d1.tar.gz bcm5719-llvm-76e9110f3dd1c11876793673fe491606dbaaa5d1.zip |
[llvm-objcopy] Refactor llvm-objcopy to use reader and writer objects
While writing code for input and output formats in llvm-objcopy it became
apparent that there was a code health problem. This change attempts to solve
that problem by refactoring the code to use Reader and Writer objects that can
read in different objects in different formats, convert them to a single shared
internal representation, and then write them to any other representation.
New classes:
Reader: the base class used to construct instances of the internal
representation
Writer: the base class used to write out instances of the internal
representation
ELFBuilder: a helper class for ELFWriter that takes an ELFFile and converts it
to a Object
SectionVisitor: it became necessary to remove writeSection from SectionBase
because, under the new Reader/Writer scheme, it's possible to convert between
ELF Types such as ELF32LE and ELF32BE. This isn't possible with writeSection
because it (dynamically) depends on the underlying section type *and*
(statically) depends on the ELF type. Bad things would happen if the underlying
sections for ELF32LE were used for writing to ELF64BE. To avoid this code smell
(which would have compiled, run, and output some nonsesnse) I decoupled writing
of sections from a class.
SectionWriter: This is just the ELFT templated implementation of
SectionVisitor. Many classes now have this class as a friend so that the
writing methods in this class can write out private data.
ELFWriter: This is the Writer that outputs to ELF
BinaryWriter: This is the Writer that outputs to Binary
ElfType: Because the ELF Type is not a part of the Object anymore we need a way
to construct the correct default Writer based on properties of the Reader. This
enum just keeps track of the ELF type of the input so it can be used as the
default output type as well.
Object has correspondingly undergone some serious changes as well. It now has
more generic methods for building and manipulating ELF binaries. This interface
makes ELFBuilder easy enough to use and will make the BinaryReader/Builder easy
to create as well. Most changes in this diff are cosmetic and deal with the
fact that a method has been moved from one class to another or a change from a
pointer to a reference. Almost no changes should result in a functional
difference (this is after all a refactor). One minor functional change was made
and the result can be seen in remove-shstrtab-error.test. The fact that it
fails hasn't changed but the error message has changed because that failure is
detected at a later point in the code now (because WriteSectionHeaders is a
property of the ElfWriter *not* a property of the Object). I'd say roughly
80-90% of this code is cosmetically different, 10-19% is different but
functionally the same, and 1-5% is functionally different despite not causing a
change in tests.
Differential Revision: https://reviews.llvm.org/D42222
llvm-svn: 323480
Diffstat (limited to 'llvm/tools/llvm-objcopy/llvm-objcopy.cpp')
-rw-r--r-- | llvm/tools/llvm-objcopy/llvm-objcopy.cpp | 134 |
1 files changed, 58 insertions, 76 deletions
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp index 0b09184497d..8f5243cefa0 100644 --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -130,42 +130,42 @@ using SectionPred = std::function<bool(const SectionBase &Sec)>; bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); } -template <class ELFT> -bool OnlyKeepDWOPred(const Object<ELFT> &Obj, const SectionBase &Sec) { +bool OnlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { // We can't remove the section header string table. - if (&Sec == Obj.getSectionHeaderStrTab()) + if (&Sec == Obj.SectionNames) return false; // Short of keeping the string table we want to keep everything that is a DWO // section and remove everything else. return !IsDWOSection(Sec); } -template <class ELFT> -void WriteObjectFile(const Object<ELFT> &Obj, StringRef File) { - std::unique_ptr<FileOutputBuffer> Buffer; - Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = - FileOutputBuffer::create(File, Obj.totalSize(), - FileOutputBuffer::F_executable); - handleAllErrors(BufferOrErr.takeError(), [](const ErrorInfoBase &) { - error("failed to open " + OutputFilename); - }); - Buffer = std::move(*BufferOrErr); - - Obj.write(*Buffer); - if (auto E = Buffer->commit()) - reportError(File, errorToErrorCode(std::move(E))); -} +static ElfType OutputElfType; -template <class ELFT> -void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) { - // Construct a second output file for the DWO sections. - ELFObject<ELFT> DWOFile(ObjFile); +std::unique_ptr<Writer> CreateWriter(Object &Obj, StringRef File) { + if (OutputFormat == "binary") { + return llvm::make_unique<BinaryWriter>(OutputFilename, Obj); + } + // Depending on the initial ELFT and OutputFormat we need a different Writer. + switch (OutputElfType) { + case ELFT_ELF32LE: + return llvm::make_unique<ELFWriter<ELF32LE>>(File, Obj, !StripSections); + case ELFT_ELF64LE: + return llvm::make_unique<ELFWriter<ELF64LE>>(File, Obj, !StripSections); + case ELFT_ELF32BE: + return llvm::make_unique<ELFWriter<ELF32BE>>(File, Obj, !StripSections); + case ELFT_ELF64BE: + return llvm::make_unique<ELFWriter<ELF64BE>>(File, Obj, !StripSections); + } + llvm_unreachable("Invalid output format"); +} - DWOFile.removeSections([&](const SectionBase &Sec) { - return OnlyKeepDWOPred<ELFT>(DWOFile, Sec); - }); - DWOFile.finalize(); - WriteObjectFile(DWOFile, File); +void SplitDWOToFile(const Reader &Reader, StringRef File) { + auto DWOFile = Reader.create(); + DWOFile->removeSections( + [&](const SectionBase &Sec) { return OnlyKeepDWOPred(*DWOFile, Sec); }); + auto Writer = CreateWriter(*DWOFile, File); + Writer->finalize(); + Writer->write(); } // This function handles the high level operations of GNU objcopy including @@ -175,23 +175,16 @@ void SplitDWOToFile(const ELFObjectFile<ELFT> &ObjFile, StringRef File) { // any previous removals. Lastly whether or not something is removed shouldn't // depend a) on the order the options occur in or b) on some opaque priority // system. The only priority is that keeps/copies overrule removes. -template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { - std::unique_ptr<Object<ELFT>> Obj; - - if (!OutputFormat.empty() && OutputFormat != "binary") - error("invalid output format '" + OutputFormat + "'"); - if (!OutputFormat.empty() && OutputFormat == "binary") - Obj = llvm::make_unique<BinaryObject<ELFT>>(ObjFile); - else - Obj = llvm::make_unique<ELFObject<ELFT>>(ObjFile); +void HandleArgs(Object &Obj, const Reader &Reader) { - if (!SplitDWO.empty()) - SplitDWOToFile<ELFT>(ObjFile, SplitDWO.getValue()); + if (!SplitDWO.empty()) { + SplitDWOToFile(Reader, SplitDWO); + } // Localize: if (LocalizeHidden) { - Obj->getSymTab()->localize([](const Symbol &Sym) { + Obj.SymbolTable->localize([](const Symbol &Sym) { return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL; }); } @@ -214,7 +207,7 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { if (ExtractDWO) RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { - return OnlyKeepDWOPred(*Obj, Sec) || RemovePred(Sec); + return OnlyKeepDWOPred(Obj, Sec) || RemovePred(Sec); }; if (StripAllGNU) @@ -223,7 +216,7 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { return true; if ((Sec.Flags & SHF_ALLOC) != 0) return false; - if (&Sec == Obj->getSectionHeaderStrTab()) + if (&Sec == Obj.SectionNames) return false; switch (Sec.Type) { case SHT_SYMTAB: @@ -239,7 +232,6 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { RemovePred = [RemovePred](const SectionBase &Sec) { return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; }; - Obj->WriteSectionHeaders = false; } if (StripDebug) { @@ -252,7 +244,7 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { if (RemovePred(Sec)) return true; - if (&Sec == Obj->getSectionHeaderStrTab()) + if (&Sec == Obj.SectionNames) return false; return (Sec.Flags & SHF_ALLOC) == 0; }; @@ -261,7 +253,7 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { if (RemovePred(Sec)) return true; - if (&Sec == Obj->getSectionHeaderStrTab()) + if (&Sec == Obj.SectionNames) return false; if (Sec.Name.startswith(".gnu.warning")) return false; @@ -278,17 +270,15 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { return false; // Allow all implicit removes. - if (RemovePred(Sec)) { + if (RemovePred(Sec)) return true; - } // Keep special sections. - if (Obj->getSectionHeaderStrTab() == &Sec) { + if (Obj.SectionNames == &Sec) return false; - } - if (Obj->getSymTab() == &Sec || Obj->getSymTab()->getStrTab() == &Sec) { + if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec) return false; - } + // Remove everything else. return true; }; @@ -305,7 +295,7 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { }; } - Obj->removeSections(RemovePred); + Obj.removeSections(RemovePred); if (!AddSection.empty()) { for (const auto &Flag : AddSection) { @@ -318,16 +308,22 @@ template <class ELFT> void CopyBinary(const ELFObjectFile<ELFT> &ObjFile) { auto Buf = std::move(*BufOrErr); auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart()); auto BufSize = Buf->getBufferSize(); - Obj->addSection(SecName, ArrayRef<uint8_t>(BufPtr, BufSize)); + Obj.addSection<OwnedDataSection>(SecName, + ArrayRef<uint8_t>(BufPtr, BufSize)); } } if (!AddGnuDebugLink.empty()) { - Obj->addGnuDebugLink(AddGnuDebugLink); + Obj.addSection<GnuDebugLinkSection>(StringRef(AddGnuDebugLink)); } +} - Obj->finalize(); - WriteObjectFile(*Obj, OutputFilename.getValue()); +std::unique_ptr<Reader> CreateReader() { + // Right now we can only read ELF files so there's only one reader; + auto Out = llvm::make_unique<ELFReader>(StringRef(InputFilename)); + // We need to set the default ElfType for output. + OutputElfType = Out->getElfType(); + return std::move(Out); } int main(int argc, char **argv) { @@ -341,25 +337,11 @@ int main(int argc, char **argv) { cl::PrintHelpMessage(); return 2; } - Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFilename); - if (!BinaryOrErr) - reportError(InputFilename, BinaryOrErr.takeError()); - Binary &Binary = *BinaryOrErr.get().getBinary(); - if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(&Binary)) { - CopyBinary(*o); - return 0; - } - if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(&Binary)) { - CopyBinary(*o); - return 0; - } - if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(&Binary)) { - CopyBinary(*o); - return 0; - } - if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(&Binary)) { - CopyBinary(*o); - return 0; - } - reportError(InputFilename, object_error::invalid_file_type); + + auto Reader = CreateReader(); + auto Obj = Reader->create(); + auto Writer = CreateWriter(*Obj, OutputFilename); + HandleArgs(*Obj, *Reader); + Writer->finalize(); + Writer->write(); } |