[lld] 78976bf - [lld-macho] Support parsing of bitcode within archives

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 8 10:37:45 PST 2020


Author: Jez Ng
Date: 2020-12-08T10:34:32-08:00
New Revision: 78976bf3dae2a4fac3b7fb2ab1a8d8e986ea36ff

URL: https://github.com/llvm/llvm-project/commit/78976bf3dae2a4fac3b7fb2ab1a8d8e986ea36ff
DIFF: https://github.com/llvm/llvm-project/commit/78976bf3dae2a4fac3b7fb2ab1a8d8e986ea36ff.diff

LOG: [lld-macho] Support parsing of bitcode within archives

Also error out if we find anything other than an object or bitcode file
in the archive.

Note that we were previously inserting the symbols and sections of the
unpacked ObjFile into the containing ArchiveFile. This was actually
unnecessary -- we can just insert the ObjectFile (or BitcodeFile) into
the `inputFiles` vector. This is the approach taken by LLD-ELF.

Reviewed By: thakis

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

Added: 
    lld/test/MachO/bitcode-nodatalayout.ll
    lld/test/MachO/invalid/bad-archive-member.s
    lld/test/MachO/lto-archive.ll

Modified: 
    lld/MachO/InputFiles.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 24600090c491..c452648a1b11 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -631,15 +631,26 @@ void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
   // to it later.
   const object::Archive::Symbol sym_copy = sym;
 
-  auto file = make<ObjFile>(mb, modTime, getName());
+  InputFile *file;
+  switch (identify_magic(mb.getBuffer())) {
+  case file_magic::macho_object:
+    file = make<ObjFile>(mb, modTime, getName());
+    break;
+  case file_magic::bitcode:
+    file = make<BitcodeFile>(mb);
+    break;
+  default:
+    StringRef bufname =
+        CHECK(c.getName(), toString(this) + ": could not get buffer name");
+    error(toString(this) + ": archive member " + bufname +
+          " has unhandled file type");
+    return;
+  }
+  inputFiles.push_back(file);
 
   // ld64 doesn't demangle sym here even with -demangle. Match that, so
   // intentionally no call to toMachOString() here.
   printArchiveMemberLoad(sym_copy.getName(), file);
-
-  symbols.insert(symbols.end(), file->symbols.begin(), file->symbols.end());
-  subsections.insert(subsections.end(), file->subsections.begin(),
-                     file->subsections.end());
 }
 
 BitcodeFile::BitcodeFile(MemoryBufferRef mbref)

diff  --git a/lld/test/MachO/bitcode-nodatalayout.ll b/lld/test/MachO/bitcode-nodatalayout.ll
new file mode 100644
index 000000000000..5e1c43c928d9
--- /dev/null
+++ b/lld/test/MachO/bitcode-nodatalayout.ll
@@ -0,0 +1,13 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %t.o
+; RUN: not %lld %t.o -o /dev/null 2>&1 | FileCheck %s
+
+; CHECK: error: input module has no datalayout
+
+; This bitcode file has no datalayout.
+; Check that we error out producing a reasonable diagnostic.
+target triple = "x86_64-apple-macosx10.15.0"
+
+define void @_start() {
+  ret void
+}

diff  --git a/lld/test/MachO/invalid/bad-archive-member.s b/lld/test/MachO/invalid/bad-archive-member.s
new file mode 100644
index 000000000000..15ab524476d3
--- /dev/null
+++ b/lld/test/MachO/invalid/bad-archive-member.s
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
+# RUN: %lld -dylib -lSystem %t/foo.o -o %t/foo.dylib
+# RUN: rm -f %t/foo.a
+# RUN: llvm-ar rcs %t/foo.a %t/foo.dylib
+# RUN: not %lld %t/test.o %t/foo.a -o /dev/null 2>&1 | FileCheck %s -DFILE=%t/foo.a
+# CHECK: error: [[FILE]]: archive member foo.dylib has unhandled file type
+
+#--- foo.s
+.globl _foo
+_foo:
+  ret
+
+#--- test.s
+.globl _main
+_main:
+  callq _foo
+  ret

diff  --git a/lld/test/MachO/lto-archive.ll b/lld/test/MachO/lto-archive.ll
new file mode 100644
index 000000000000..42b2c0fb5759
--- /dev/null
+++ b/lld/test/MachO/lto-archive.ll
@@ -0,0 +1,28 @@
+; REQUIRES: x86
+; RUN: split-file %s %t
+; RUN: llvm-as %t/foo.ll -o %t/foo.o
+; RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
+; RUN: rm -f %t/foo.a
+; RUN: llvm-ar rcs %t/foo.a %t/foo.o
+; RUN: %lld -save-temps -lSystem %t/test.o %t/foo.a -o %t/test
+; RUN: llvm-objdump -d --macho --no-show-raw-insn %t/test | FileCheck %s
+
+; CHECK:      _main:
+; CHECK-NEXT: callq _foo
+; CHECK-NEXT: retq
+
+;--- foo.ll
+
+target triple = "x86_64-apple-macosx10.15.0"
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @foo() {
+  ret void
+}
+
+;--- test.s
+
+.globl _main
+_main:
+  callq _foo
+  ret


        


More information about the llvm-commits mailing list