[llvm] 9e78371 - [llvm-libtool-darwin] Allow flattening archives

Sameer Arora via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 21 13:54:22 PDT 2020


Author: Sameer Arora
Date: 2020-07-21T13:53:15-07:00
New Revision: 9e783716a2249e333dfe731628b72dcda7e8c2d6

URL: https://github.com/llvm/llvm-project/commit/9e783716a2249e333dfe731628b72dcda7e8c2d6
DIFF: https://github.com/llvm/llvm-project/commit/9e783716a2249e333dfe731628b72dcda7e8c2d6.diff

LOG: [llvm-libtool-darwin] Allow flattening archives

Add support for flattening archives while creating static libraries.
Hence, can now pass archives as input in addition to Mach-O binaries.
Furthermore, archives themselves must only conatain Mach-O binaries. As
per cctools' libtool's behavior, llvm-libtool-darwin does not flatten
archives recursively.

Reviewed by alexshap, smeenai, jhenderson

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

Added: 
    llvm/test/tools/llvm-libtool-darwin/archive-flattening.test

Modified: 
    llvm/tools/llvm-libtool-darwin/CMakeLists.txt
    llvm/tools/llvm-libtool-darwin/LLVMBuild.txt
    llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-libtool-darwin/archive-flattening.test b/llvm/test/tools/llvm-libtool-darwin/archive-flattening.test
new file mode 100644
index 000000000000..fa3200ec00b7
--- /dev/null
+++ b/llvm/test/tools/llvm-libtool-darwin/archive-flattening.test
@@ -0,0 +1,95 @@
+## This test checks that an archive is flattened correctly.
+
+# RUN: yaml2obj %S/Inputs/input1.yaml -o %t-input1.o
+# RUN: yaml2obj %S/Inputs/input2.yaml -o %t-input2.o
+
+## Input a correct archive:
+# RUN: rm -f %t.correct.ar
+# RUN: llvm-ar cr %t.correct.ar %t-input1.o %t-input2.o
+# RUN: llvm-libtool-darwin -static -o %t.lib %t.correct.ar
+
+## Check that binaries are present:
+# RUN: llvm-ar t %t.lib | \
+# RUN:   FileCheck %s --check-prefix=CHECK-NAMES --implicit-check-not={{.}} -DPREFIX=%basename_t.tmp
+
+# CHECK-NAMES:      [[PREFIX]]-input1.o
+# CHECK-NAMES-NEXT: [[PREFIX]]-input2.o
+
+## Check that symbols are present:
+# RUN: llvm-nm --print-armap %t.lib | \
+# RUN:   FileCheck %s --check-prefix=CHECK-SYMBOLS -DPREFIX=%basename_t.tmp --match-full-lines
+
+# CHECK-SYMBOLS:      Archive map
+# CHECK-SYMBOLS-NEXT: _symbol1 in [[PREFIX]]-input1.o
+# CHECK-SYMBOLS-NEXT: _symbol2 in [[PREFIX]]-input2.o
+# CHECK-SYMBOLS-EMPTY:
+
+## Check that output archive is in Darwin format:
+# RUN: llvm-objdump --macho --archive-headers %t.lib | \
+# RUN:   FileCheck %s --check-prefix=FORMAT -DPREFIX=%basename_t.tmp -DARCHIVE=%t.lib
+
+# FORMAT:      Archive : [[ARCHIVE]]
+# FORMAT-NEXT: __.SYMDEF
+# FORMAT-NEXT: [[PREFIX]]-input1.o
+# FORMAT-NEXT: [[PREFIX]]-input2.o
+# FORMAT-NOT:  {{.}}
+
+## Passing both archive and object file:
+# RUN: llvm-libtool-darwin -static -o %t.lib %t-input2.o %t.correct.ar %t-input1.o
+# RUN: llvm-ar t %t.lib | \
+# RUN:   FileCheck %s --check-prefix=BOTH-NAMES --implicit-check-not={{.}} -DPREFIX=%basename_t.tmp
+# RUN: llvm-nm --print-armap %t.lib | \
+# RUN:   FileCheck %s --check-prefix=BOTH-SYMBOLS -DPREFIX=%basename_t.tmp --match-full-lines
+
+# BOTH-NAMES:      [[PREFIX]]-input2.o
+# BOTH-NAMES-NEXT: [[PREFIX]]-input1.o
+# BOTH-NAMES-NEXT: [[PREFIX]]-input2.o
+# BOTH-NAMES-NEXT: [[PREFIX]]-input1.o
+
+# BOTH-SYMBOLS:      Archive map
+# BOTH-SYMBOLS-NEXT: _symbol2 in [[PREFIX]]-input2.o
+# BOTH-SYMBOLS-NEXT: _symbol1 in [[PREFIX]]-input1.o
+# BOTH-SYMBOLS-NEXT: _symbol2 in [[PREFIX]]-input2.o
+# BOTH-SYMBOLS-NEXT: _symbol1 in [[PREFIX]]-input1.o
+# BOTH-SYMBOLS-EMPTY:
+
+## Cannot read archive:
+# RUN: echo '!<arch>' >  %t-invalid-archive.lib
+# RUN: echo 'invalid' >> %t-invalid-archive.lib
+# RUN: not llvm-libtool-darwin -static -o %t.lib %t-invalid-archive.lib 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=INVALID-ARCHIVE -DARCHIVE=%t-invalid-archive.lib
+
+# INVALID-ARCHIVE: error: '[[ARCHIVE]]': truncated or malformed archive
+
+## Archive member not an object file:
+# RUN: rm -f %t.not-object.ar
+# RUN: touch %t.txt
+# RUN: llvm-ar cr %t.not-object.ar %t.txt
+# RUN: not llvm-libtool-darwin -static -o %t.lib %t.not-object.ar 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=NOT-OBJECT -DARCHIVE=%t.not-object.ar -DFILE=%basename_t.tmp.txt
+
+## Do not recursively flatten archives:
+# RUN: rm -f %t.inner
+# RUN: rm -f %t.outer
+# RUN: llvm-ar cr %t.inner %t-input1.o
+# RUN: llvm-ar cr %t.outer %t.inner
+# RUN: not llvm-libtool-darwin -static -o %t.lib %t.outer 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=NOT-OBJECT -DARCHIVE=%t.outer -DFILE=%basename_t.tmp.inner
+
+# NOT-OBJECT: error: '[[ARCHIVE]]': '[[FILE]]': The file was not recognized as a valid object file
+
+## Archive member not a Mach-O object file:
+# RUN: rm -f %t.not-macho.ar
+# RUN: yaml2obj %s -o %t.elf
+# RUN: llvm-ar cr %t.not-macho.ar %t.elf
+# RUN: not llvm-libtool-darwin -static -o %t.lib %t.not-macho.ar 2>&1 | \
+# RUN:   FileCheck %s --check-prefix=NOT-MACHO -DARCHIVE=%t.not-macho.ar -DFILE=%basename_t.tmp.elf
+
+# NOT-MACHO: error: '[[ARCHIVE]]': '[[FILE]]': format not supported
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64

diff  --git a/llvm/tools/llvm-libtool-darwin/CMakeLists.txt b/llvm/tools/llvm-libtool-darwin/CMakeLists.txt
index 75059741277e..c2073990f214 100644
--- a/llvm/tools/llvm-libtool-darwin/CMakeLists.txt
+++ b/llvm/tools/llvm-libtool-darwin/CMakeLists.txt
@@ -1,4 +1,5 @@
 set(LLVM_LINK_COMPONENTS
+  BinaryFormat
   Object
   Support
   )

diff  --git a/llvm/tools/llvm-libtool-darwin/LLVMBuild.txt b/llvm/tools/llvm-libtool-darwin/LLVMBuild.txt
index 5f7d555e6043..3c557a3aaf61 100644
--- a/llvm/tools/llvm-libtool-darwin/LLVMBuild.txt
+++ b/llvm/tools/llvm-libtool-darwin/LLVMBuild.txt
@@ -17,4 +17,4 @@
 type = Tool
 name = llvm-libtool-darwin
 parent = Tools
-required_libraries = Object Support
+required_libraries = BinaryFormat Object Support

diff  --git a/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp b/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp
index ac61ffe956db..bd1d9ac07c69 100644
--- a/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp
+++ b/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp
@@ -10,6 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Object/ArchiveWriter.h"
 #include "llvm/Object/MachO.h"
 #include "llvm/Object/ObjectFile.h"
@@ -58,11 +59,26 @@ static Error verifyMachOObject(const NewArchiveMember &Member) {
   return Error::success();
 }
 
-static Error addMember(std::vector<NewArchiveMember> &Members,
-                       StringRef FileName) {
+static Error addChildMember(std::vector<NewArchiveMember> &Members,
+                            const object::Archive::Child &M) {
   Expected<NewArchiveMember> NMOrErr =
-      NewArchiveMember::getFile(FileName, /*Deterministic=*/true);
+      NewArchiveMember::getOldMember(M, /*Deterministic=*/true);
+  if (!NMOrErr)
+    return NMOrErr.takeError();
+
+  // Verify that Member is a Mach-O object file.
+  if (Error E = verifyMachOObject(*NMOrErr))
+    return E;
+
+  Members.push_back(std::move(*NMOrErr));
+  return Error::success();
+}
 
+static Error
+addMember(std::vector<NewArchiveMember> &Members, StringRef FileName,
+          std::vector<std::unique_ptr<MemoryBuffer>> &ArchiveBuffers) {
+  Expected<NewArchiveMember> NMOrErr =
+      NewArchiveMember::getFile(FileName, /*Deterministic=*/true);
   if (!NMOrErr)
     return createFileError(FileName, NMOrErr.takeError());
 
@@ -70,6 +86,27 @@ static Error addMember(std::vector<NewArchiveMember> &Members,
   // name.
   NMOrErr->MemberName = sys::path::filename(NMOrErr->MemberName);
 
+  // Flatten archives.
+  if (identify_magic(NMOrErr->Buf->getBuffer()) == file_magic::archive) {
+    Expected<std::unique_ptr<Archive>> LibOrErr =
+        object::Archive::create(NMOrErr->Buf->getMemBufferRef());
+    if (!LibOrErr)
+      return createFileError(FileName, LibOrErr.takeError());
+    object::Archive &Lib = **LibOrErr;
+
+    Error Err = Error::success();
+    for (const object::Archive::Child &Child : Lib.children(Err))
+      if (Error E = addChildMember(Members, Child))
+        return createFileError(FileName, std::move(E));
+    if (Err)
+      return createFileError(FileName, std::move(Err));
+
+    // Update vector ArchiveBuffers with the MemoryBuffers to transfer
+    // ownership.
+    ArchiveBuffers.push_back(std::move(NMOrErr->Buf));
+    return Error::success();
+  }
+
   // Verify that Member is a Mach-O object file.
   if (Error E = verifyMachOObject(*NMOrErr))
     return E;
@@ -80,8 +117,9 @@ static Error addMember(std::vector<NewArchiveMember> &Members,
 
 static Error createStaticLibrary() {
   std::vector<NewArchiveMember> NewMembers;
+  std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
   for (StringRef Member : InputFiles)
-    if (Error E = addMember(NewMembers, Member))
+    if (Error E = addMember(NewMembers, Member, ArchiveBuffers))
       return E;
 
   if (Error E = writeArchive(OutputFile, NewMembers,


        


More information about the llvm-commits mailing list