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

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 2 20:58:53 PST 2020


int3 created this revision.
int3 added a reviewer: lld-macho.
Herald added subscribers: llvm-commits, steven_wu, hiraditya.
Herald added a project: LLVM.
int3 requested review of this revision.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92539

Files:
  lld/MachO/InputFiles.cpp
  lld/test/MachO/invalid/bad-archive-member.s
  lld/test/MachO/lto-archive.ll


Index: lld/test/MachO/lto-archive.ll
===================================================================
--- /dev/null
+++ 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
Index: lld/test/MachO/invalid/bad-archive-member.s
===================================================================
--- /dev/null
+++ 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
Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -619,7 +619,22 @@
                                      "for the member defining symbol " +
                                      toMachOString(sym)));
 
-  inputFiles.push_back(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);
 }
 
 BitcodeFile::BitcodeFile(MemoryBufferRef mbref)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92539.309143.patch
Type: text/x-patch
Size: 2569 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201203/94e57a84/attachment.bin>


More information about the llvm-commits mailing list