Fix a bugs in the Mach-O disassembler when disassembling from a
[oota-llvm.git] / lib / MC / MCObjectFileInfo.cpp
index ade4b496ec7f69d6fbbb19f917c89b8b8c8eb1e3..31091ef284a62782d4529bb8526eba0c7c575d77 100644 (file)
@@ -16,6 +16,8 @@
 #include "llvm/MC/MCSectionCOFF.h"
 #include "llvm/MC/MCSectionELF.h"
 #include "llvm/MC/MCSectionMachO.h"
+#include "llvm/Support/COFF.h"
+
 using namespace llvm;
 
 static bool useCompactUnwind(const Triple &T) {
@@ -258,7 +260,6 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) {
     FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
                      ((CMModel == CodeModel::Large) ? dwarf::DW_EH_PE_sdata8
                                                     : dwarf::DW_EH_PE_sdata4);
-
     break;
   default:
     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
@@ -402,7 +403,6 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) {
       EHSectionFlags |= ELF::SHF_WRITE;
   }
 
-
   // ELF
   BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
                                   ELF::SHF_WRITE | ELF::SHF_ALLOC);
@@ -577,7 +577,7 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) {
   assert(T.isOSWindows() && "Windows is the only supported COFF target");
   if (T.getArch() == Triple::x86_64) {
     // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
-    LSDASection = 0;
+    LSDASection = nullptr;
   } else {
     LSDASection = Ctx->getCOFFSection(".gcc_except_table",
                                       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
@@ -733,11 +733,11 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) {
       ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
                    COFF::IMAGE_SCN_MEM_WRITE,
       SectionKind::getDataRel());
-         
+
   StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
                                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
                                             COFF::IMAGE_SCN_MEM_READ,
-                                        SectionKind::getReadOnly());   
+                                        SectionKind::getReadOnly());
 }
 
 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple,
@@ -767,19 +767,25 @@ void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple,
 
   TT = TheTriple;
 
-  Triple::ObjectFormatType Format = TT.getObjectFormat();
-  switch (Format) {
-  case Triple::MachO:
+  Triple::ArchType Arch = TT.getArch();
+  // FIXME: Checking for Arch here to filter out bogus triples such as
+  // cellspu-apple-darwin. Perhaps we should fix in Triple?
+  if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
+       Arch == Triple::arm || Arch == Triple::thumb ||
+       Arch == Triple::aarch64 ||
+       Arch == Triple::ppc || Arch == Triple::ppc64 ||
+       Arch == Triple::UnknownArch) &&
+      TT.isOSBinFormatMachO()) {
+    Env = IsMachO;
     initMachOMCObjectFileInfo(TT);
-    break;
-  case Triple::COFF:
+  } else if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
+              Arch == Triple::arm || Arch == Triple::thumb) &&
+             (TT.isOSWindows() && TT.getObjectFormat() == Triple::COFF)) {
+    Env = IsCOFF;
     initCOFFMCObjectFileInfo(TT);
-    break;
-  case Triple::ELF:
+  } else {
+    Env = IsELF;
     initELFMCObjectFileInfo(TT);
-    break;
-  case Triple::UnknownObjectFormat:
-    break;
   }
 }
 
@@ -795,9 +801,7 @@ MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const {
 }
 
 void MCObjectFileInfo::InitEHFrameSection() {
-  Triple::ObjectFormatType Format = TT.getObjectFormat();
-  switch (Format) {
-  case Triple::MachO:
+  if (Env == IsMachO)
     EHFrameSection =
       Ctx->getMachOSection("__TEXT", "__eh_frame",
                            MachO::S_COALESCED |
@@ -805,20 +809,14 @@ void MCObjectFileInfo::InitEHFrameSection() {
                            MachO::S_ATTR_STRIP_STATIC_SYMS |
                            MachO::S_ATTR_LIVE_SUPPORT,
                            SectionKind::getReadOnly());
-    break;
-  case Triple::ELF:
+  else if (Env == IsELF)
     EHFrameSection =
         Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
-    break;
-  case Triple::COFF:
+  else
     EHFrameSection =
       Ctx->getCOFFSection(".eh_frame",
                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
                           COFF::IMAGE_SCN_MEM_READ |
                           COFF::IMAGE_SCN_MEM_WRITE,
                           SectionKind::getDataRel());
-    break;
-  case Triple::UnknownObjectFormat:
-    break;
-  }
 }