[llvm] r366772 - [llvm-lipo] Implement -info

Shoaib Meenai via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 22 17:42:04 PDT 2019


Author: smeenai
Date: Mon Jul 22 17:42:03 2019
New Revision: 366772

URL: http://llvm.org/viewvc/llvm-project?rev=366772&view=rev
Log:
[llvm-lipo] Implement -info

Prints architecture type of all input files.

Patch by Anusha Basana <anusha.basana at gmail.com>

Differential Revision: https://reviews.llvm.org/D64668

Added:
    llvm/trunk/test/tools/llvm-lipo/info-invalid.test
    llvm/trunk/test/tools/llvm-lipo/info.test
Modified:
    llvm/trunk/tools/llvm-lipo/LipoOpts.td
    llvm/trunk/tools/llvm-lipo/llvm-lipo.cpp

Added: llvm/trunk/test/tools/llvm-lipo/info-invalid.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-lipo/info-invalid.test?rev=366772&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-lipo/info-invalid.test (added)
+++ llvm/trunk/test/tools/llvm-lipo/info-invalid.test Mon Jul 22 17:42:03 2019
@@ -0,0 +1,37 @@
+# RUN: yaml2obj %s > %t
+# RUN: not llvm-lipo %t -info 2>&1 | FileCheck %s
+# CHECK: has unsupported binary format
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_DYN
+  Machine:         EM_X86_64
+  Entry:           0x0000000000001000
+Sections:
+  - Name:            .gnu.version_d
+    Type:            SHT_GNU_verdef
+    Flags:           [ SHF_ALLOC ]
+    Address:         0x0000000000000230
+    Link:            .dynstr
+    AddressAlign:    0x0000000000000004
+    Info:            0x0000000000000003
+    Entries:
+      - Version:         1
+        Flags:           1
+        VersionNdx:      1
+        Hash:            123456789
+        Names:
+          - foo
+      - Version:         1
+        Flags:           2
+        VersionNdx:      2
+        Hash:            987654321
+        Names:
+          - VERSION_1
+          - VERSION_2
+DynamicSymbols:
+  - Name:    bar
+    Binding: STB_GLOBAL
+...

Added: llvm/trunk/test/tools/llvm-lipo/info.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-lipo/info.test?rev=366772&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-lipo/info.test (added)
+++ llvm/trunk/test/tools/llvm-lipo/info.test Mon Jul 22 17:42:03 2019
@@ -0,0 +1,13 @@
+# RUN: yaml2obj %p/Inputs/i386-slice.yaml > %t-i386.o
+# RUN: yaml2obj %p/Inputs/x86_64-slice.yaml > %t-x86_64.o
+# RUN: yaml2obj %p/Inputs/i386-x86_64-universal.yaml > %t-universal.o
+
+# RUN: llvm-lipo %t-universal.o %t-i386.o %t-universal.o %t-x86_64.o -info | FileCheck %s
+# CHECK: Architectures in the fat file:
+# CHECK: i386 x86_64
+# CHECK-NEXT: Architectures in the fat file:
+# CHECK: i386 x86_64
+# CHECK-NEXT: Non-fat file:
+# CHECK:  is architecture: i386
+# CHECK-NEXT: Non-fat file:
+# CHECK:  is architecture: x86_64

Modified: llvm/trunk/tools/llvm-lipo/LipoOpts.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-lipo/LipoOpts.td?rev=366772&r1=366771&r2=366772&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-lipo/LipoOpts.td (original)
+++ llvm/trunk/tools/llvm-lipo/LipoOpts.td Mon Jul 22 17:42:03 2019
@@ -18,6 +18,12 @@ def archs : Option<["-", "--"], "archs",
             Group<action_group>,
             HelpText<"Display the arch_types present in the input file">;
 
+def info : Option<["-", "--"], "info", KIND_FLAG>,
+           Group<action_group>,
+           HelpText<"Display descriptions of each input file including "
+                    "filename and arch_types. Groups universal binaries "
+                    "together followed by thin files">;
+
 def thin : Option<["-", "--"], "thin", KIND_SEPARATE>,
            Group<action_group>,
            HelpText<"Create a thin output file of specified arch_type from the "

Modified: llvm/trunk/tools/llvm-lipo/llvm-lipo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-lipo/llvm-lipo.cpp?rev=366772&r1=366771&r2=366772&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-lipo/llvm-lipo.cpp (original)
+++ llvm/trunk/tools/llvm-lipo/llvm-lipo.cpp Mon Jul 22 17:42:03 2019
@@ -78,6 +78,7 @@ public:
 
 enum class LipoAction {
   PrintArchs,
+  PrintInfo,
   VerifyArch,
   ThinArch,
   CreateUniversal,
@@ -184,6 +185,10 @@ static Config parseLipoOptions(ArrayRef<
     C.ActionToPerform = LipoAction::PrintArchs;
     return C;
 
+  case LIPO_info:
+    C.ActionToPerform = LipoAction::PrintInfo;
+    return C;
+
   case LIPO_thin:
     if (C.InputFiles.size() > 1)
       reportError("thin expects a single input file");
@@ -269,26 +274,49 @@ static std::string getArchString(const M
       .str();
 }
 
-LLVM_ATTRIBUTE_NORETURN
-static void printArchs(ArrayRef<OwningBinary<Binary>> InputBinaries) {
+static void printBinaryArchs(const Binary *Binary, raw_ostream &OS) {
   // Prints trailing space for compatibility with cctools lipo.
-  assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
-  const Binary *InputBinary = InputBinaries.front().getBinary();
-  if (auto UO = dyn_cast<MachOUniversalBinary>(InputBinary)) {
+  if (auto UO = dyn_cast<MachOUniversalBinary>(Binary)) {
     for (const auto &O : UO->objects()) {
       Expected<std::unique_ptr<MachOObjectFile>> BinaryOrError =
           O.getAsObjectFile();
       if (!BinaryOrError)
-        reportError(InputBinary->getFileName(), BinaryOrError.takeError());
-      outs() << getArchString(*BinaryOrError.get().get()) << " ";
+        reportError(Binary->getFileName(), BinaryOrError.takeError());
+      OS << getArchString(*BinaryOrError.get().get()) << " ";
     }
-  } else if (auto O = dyn_cast<MachOObjectFile>(InputBinary)) {
-    outs() << getArchString(*O) << " ";
-  } else {
-    llvm_unreachable("Unexpected binary format");
+    OS << "\n";
+    return;
   }
+  OS << getArchString(*cast<MachOObjectFile>(Binary)) << " \n";
+}
+
+LLVM_ATTRIBUTE_NORETURN
+static void printArchs(ArrayRef<OwningBinary<Binary>> InputBinaries) {
+  assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
+  printBinaryArchs(InputBinaries.front().getBinary(), outs());
+  exit(EXIT_SUCCESS);
+}
 
-  outs() << "\n";
+LLVM_ATTRIBUTE_NORETURN
+static void printInfo(ArrayRef<OwningBinary<Binary>> InputBinaries) {
+  // Group universal and thin files together for compatibility with cctools lipo
+  for (auto &IB : InputBinaries) {
+    const Binary *Binary = IB.getBinary();
+    if (Binary->isMachOUniversalBinary()) {
+      outs() << "Architectures in the fat file: " << Binary->getFileName()
+             << " are: ";
+      printBinaryArchs(Binary, outs());
+    }
+  }
+  for (auto &IB : InputBinaries) {
+    const Binary *Binary = IB.getBinary();
+    if (!Binary->isMachOUniversalBinary()) {
+      assert(Binary->isMachO() && "expected MachO binary");
+      outs() << "Non-fat file: " << Binary->getFileName()
+             << " is architecture: ";
+      printBinaryArchs(Binary, outs());
+    }
+  }
   exit(EXIT_SUCCESS);
 }
 
@@ -470,8 +498,8 @@ static void createUniversalBinary(SmallV
               OutFile->getBufferStart() + FatArchList[Index].offset);
   }
 
-  // FatArchs written after Slices in order reduce the number of swaps for the
-  // LittleEndian case
+  // FatArchs written after Slices in order to reduce the number of swaps for
+  // the LittleEndian case
   if (sys::IsLittleEndianHost)
     for (MachO::fat_arch &FA : FatArchList)
       MachO::swapStruct(FA);
@@ -510,6 +538,9 @@ int main(int argc, char **argv) {
   case LipoAction::PrintArchs:
     printArchs(InputBinaries);
     break;
+  case LipoAction::PrintInfo:
+    printInfo(InputBinaries);
+    break;
   case LipoAction::ThinArch:
     extractSlice(InputBinaries, C.ThinArchType, C.OutputFile);
     break;




More information about the llvm-commits mailing list