]> granicus.if.org Git - llvm/commitdiff
Allow public Triple deduction from ObjectFiles.
authorVlad Tsyrklevich <vlad@tsyrklevich.net>
Tue, 19 Sep 2017 02:22:48 +0000 (02:22 +0000)
committerVlad Tsyrklevich <vlad@tsyrklevich.net>
Tue, 19 Sep 2017 02:22:48 +0000 (02:22 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313605 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/ObjectFile.h
lib/Object/ObjectFile.cpp
tools/llvm-objdump/llvm-objdump.cpp

index afcad3090703b15901291fb9584f1735e0d0e9a5..867c218c2a9501f5194b50cde999928a18f06570 100644 (file)
@@ -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;
index 8377dd0d73fa4bfec97f3fdb920b1c277a57d940..67818409986570189dda7cc829e75ba9fb74ff73 100644 (file)
@@ -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<COFFObjectFile>(this);
+    if (COFFObj->getArch() == Triple::thumb)
+      TheTriple.setTriple("thumbv7-windows");
+  }
+
+  return TheTriple;
+}
+
 Expected<std::unique_ptr<ObjectFile>>
 ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
   StringRef Data = Object.getBuffer();
index 5b16abf25c69d6428a969f3fe3b3e34eed4b6d6d..09396466c40e5030217b800f2dead63f62422e78 100644 (file)
@@ -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<COFFObjectFile>(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();