[llvm] r221915 - Object, Mach-O: Refactor and clean code up

David Majnemer david.majnemer at gmail.com
Thu Nov 13 11:48:56 PST 2014


Author: majnemer
Date: Thu Nov 13 13:48:56 2014
New Revision: 221915

URL: http://llvm.org/viewvc/llvm-project?rev=221915&view=rev
Log:
Object, Mach-O: Refactor and clean code up

Don't assert if we can return an error code, reuse existing
functionality like is64Bit().

Modified:
    llvm/trunk/lib/Object/MachOObjectFile.cpp
    llvm/trunk/tools/llvm-objdump/MachODump.cpp

Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=221915&r1=221914&r2=221915&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Thu Nov 13 13:48:56 2014
@@ -242,20 +242,40 @@ MachOObjectFile::MachOObjectFile(MemoryB
   MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
   for (unsigned I = 0; ; ++I) {
     if (Load.C.cmd == MachO::LC_SYMTAB) {
-      assert(!SymtabLoadCmd && "Multiple symbol tables");
+      // Multiple symbol tables
+      if (SymtabLoadCmd) {
+        EC = object_error::parse_failed;
+        return;
+      }
       SymtabLoadCmd = Load.Ptr;
     } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
-      assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
+      // Multiple dynamic symbol tables
+      if (DysymtabLoadCmd) {
+        EC = object_error::parse_failed;
+        return;
+      }
       DysymtabLoadCmd = Load.Ptr;
     } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
-      assert(!DataInCodeLoadCmd && "Multiple data in code tables");
+      // Multiple data in code tables
+      if (DataInCodeLoadCmd) {
+        EC = object_error::parse_failed;
+        return;
+      }
       DataInCodeLoadCmd = Load.Ptr;
     } else if (Load.C.cmd == MachO::LC_DYLD_INFO || 
                Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
-      assert(!DyldInfoLoadCmd && "Multiple dyldinfo load commands");
+      // Multiple dyldinfo load commands
+      if (DyldInfoLoadCmd) {
+        EC = object_error::parse_failed;
+        return;
+      }
       DyldInfoLoadCmd = Load.Ptr;
     } else if (Load.C.cmd == MachO::LC_UUID) {
-      assert(!UuidLoadCmd && "Multiple UUID load commands");
+      // Multiple UUID load commands
+      if (UuidLoadCmd) {
+        EC = object_error::parse_failed;
+        return;
+      }
       UuidLoadCmd = Load.Ptr;
     } else if (Load.C.cmd == SegmentLoadType) {
       uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
@@ -1227,17 +1247,10 @@ StringRef MachOObjectFile::getFileFormat
     case llvm::MachO::CPU_TYPE_POWERPC:
       return "Mach-O 32-bit ppc";
     default:
-      assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
-             "64-bit object file when we're not 64-bit?");
       return "Mach-O 32-bit unknown";
     }
   }
 
-  // Make sure the cpu type has the correct mask.
-  assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
-         == llvm::MachO::CPU_ARCH_ABI64 &&
-         "32-bit object file when we're 64-bit?");
-
   switch (CPUType) {
   case llvm::MachO::CPU_TYPE_X86_64:
     return "Mach-O 64-bit x86-64";

Modified: llvm/trunk/tools/llvm-objdump/MachODump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/MachODump.cpp?rev=221915&r1=221914&r2=221915&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/MachODump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/MachODump.cpp Thu Nov 13 13:48:56 2014
@@ -2826,7 +2826,7 @@ static void PrintSection(const char *sec
     outs() << "\n";
 }
 
-static void PrintSymtabLoadCommand(MachO::symtab_command st, uint32_t cputype,
+static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
                                    uint32_t object_size) {
   outs() << "     cmd LC_SYMTAB\n";
   outs() << " cmdsize " << st.cmdsize;
@@ -2841,7 +2841,7 @@ static void PrintSymtabLoadCommand(MachO
     outs() << "\n";
   outs() << "   nsyms " << st.nsyms;
   uint64_t big_size;
-  if (cputype & MachO::CPU_ARCH_ABI64) {
+  if (Is64Bit) {
     big_size = st.nsyms;
     big_size *= sizeof(struct MachO::nlist_64);
     big_size += st.symoff;
@@ -2874,7 +2874,7 @@ static void PrintSymtabLoadCommand(MachO
 
 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
                                      uint32_t nsyms, uint32_t object_size,
-                                     uint32_t cputype) {
+                                     bool Is64Bit) {
   outs() << "            cmd LC_DYSYMTAB\n";
   outs() << "        cmdsize " << dyst.cmdsize;
   if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
@@ -2938,7 +2938,7 @@ static void PrintDysymtabLoadCommand(Mac
     outs() << "\n";
   outs() << "        nmodtab " << dyst.nmodtab;
   uint64_t modtabend;
-  if (cputype & MachO::CPU_ARCH_ABI64) {
+  if (Is64Bit) {
     modtabend = dyst.nmodtab;
     modtabend *= sizeof(struct MachO::dylib_module_64);
     modtabend += dyst.modtaboff;
@@ -3310,11 +3310,12 @@ static void PrintLoadCommands(const Mach
       }
     } else if (Command.C.cmd == MachO::LC_SYMTAB) {
       MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
-      PrintSymtabLoadCommand(Symtab, cputype, Buf.size());
+      PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
     } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
       MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
       MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
-      PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), cputype);
+      PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
+                               Obj->is64Bit());
     } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
                Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
       MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);





More information about the llvm-commits mailing list