From de62046d68083a9a39ab29194adb6258a7ac76a9 Mon Sep 17 00:00:00 2001 From: Vlad Tsyrklevich Date: Tue, 19 Sep 2017 02:22:48 +0000 Subject: [PATCH] Allow public Triple deduction from ObjectFiles. Move logic that allows for Triple deduction from an ObjectFile object out of llvm-objdump.cpp into a public factory, found in the ObjectFile class. This should allow other tools in the future to use this logic without reimplementation. Patch by Mitch Phillips Differential Revision: https://reviews.llvm.org/D37719 llvm-svn: 313605 --- llvm/include/llvm/Object/ObjectFile.h | 3 +++ llvm/lib/Object/ObjectFile.cpp | 25 ++++++++++++++++++++++++ llvm/tools/llvm-objdump/llvm-objdump.cpp | 22 ++------------------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/llvm/include/llvm/Object/ObjectFile.h b/llvm/include/llvm/Object/ObjectFile.h index afcad3090703..867c218c2a95 100644 --- a/llvm/include/llvm/Object/ObjectFile.h +++ b/llvm/include/llvm/Object/ObjectFile.h @@ -282,6 +282,9 @@ public: virtual SubtargetFeatures getFeatures() const = 0; virtual void setARMSubArch(Triple &TheTriple) const { } + /// @brief Create a triple from the data in this object file. + Triple makeTriple() const; + /// Returns platform-specific object flags, if any. virtual std::error_code getPlatformFlags(unsigned &Result) const { Result = 0; diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp index 8377dd0d73fa..678184099865 100644 --- a/llvm/lib/Object/ObjectFile.cpp +++ b/llvm/lib/Object/ObjectFile.cpp @@ -79,6 +79,31 @@ section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { return section_iterator(SectionRef(Sec, this)); } +Triple ObjectFile::makeTriple() const { + Triple TheTriple; + auto Arch = getArch(); + TheTriple.setArch(Triple::ArchType(Arch)); + + // For ARM targets, try to use the build attributes to build determine + // the build target. Target features are also added, but later during + // disassembly. + if (Arch == Triple::arm || Arch == Triple::armeb) + setARMSubArch(TheTriple); + + // TheTriple defaults to ELF, and COFF doesn't have an environment: + // the best we can do here is indicate that it is mach-o. + if (isMachO()) + TheTriple.setObjectFormat(Triple::MachO); + + if (isCOFF()) { + const auto COFFObj = dyn_cast(this); + if (COFFObj->getArch() == Triple::thumb) + TheTriple.setTriple("thumbv7-windows"); + } + + return TheTriple; +} + Expected> ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) { StringRef Data = Object.getBuffer(); diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index 5b16abf25c69..09396466c40e 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -362,29 +362,11 @@ static const Target *getTarget(const ObjectFile *Obj = nullptr) { llvm::Triple TheTriple("unknown-unknown-unknown"); if (TripleName.empty()) { if (Obj) { - auto Arch = Obj->getArch(); - TheTriple.setArch(Triple::ArchType(Arch)); - - // For ARM targets, try to use the build attributes to build determine - // the build target. Target features are also added, but later during - // disassembly. - if (Arch == Triple::arm || Arch == Triple::armeb) { - Obj->setARMSubArch(TheTriple); - } - - // TheTriple defaults to ELF, and COFF doesn't have an environment: - // the best we can do here is indicate that it is mach-o. - if (Obj->isMachO()) - TheTriple.setObjectFormat(Triple::MachO); - - if (Obj->isCOFF()) { - const auto COFFObj = dyn_cast(Obj); - if (COFFObj->getArch() == Triple::thumb) - TheTriple.setTriple("thumbv7-windows"); - } + TheTriple = Obj->makeTriple(); } } else { TheTriple.setTriple(Triple::normalize(TripleName)); + // Use the triple, but also try to combine with ARM build attributes. if (Obj) { auto Arch = Obj->getArch();