[PATCH] D77006: [lld-macho] Support reading of universal binaries

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 30 18:03:10 PDT 2020


int3 updated this revision to Diff 253756.
int3 added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77006/new/

https://reviews.llvm.org/D77006

Files:
  lld/MachO/InputFiles.cpp
  lld/test/MachO/fat-arch.s


Index: lld/test/MachO/fat-arch.s
===================================================================
--- /dev/null
+++ lld/test/MachO/fat-arch.s
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=i386-apple-darwin %s -o %t.i386.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.x86_64.o
+# RUN: llvm-lipo %t.i386.o %t.x86_64.o -create -o %t.fat.o
+# RUN: lld -flavor darwinnew -arch x86_64 -o /dev/null %t.fat.o
+
+# RUN: llvm-lipo %t.i386.o -create -o %t.noarch.o
+# RUN: not lld -flavor darwinnew -arch x86_64 -o /dev/null %t.noarch.o 2>&1 | \
+# RUN:    FileCheck %s -DFILE=%t.noarch.o
+# CHECK: Unable to find matching architecture in [[FILE]]
+# CHECK: undefined symbol: _main
+
+.text
+.global _main
+_main:
+  mov $0, %eax
+  ret
Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -81,7 +81,25 @@
   if (read32be(&hdr->magic) != MachO::FAT_MAGIC)
     return mbref;
 
-  error("TODO: Add support for universal binaries");
+  // Object files and archive files may be fat files, which contains
+  // multiple real files for different CPU ISAs. Here, we search for a
+  // file that matches with the current link target and returns it as
+  // a MemoryBufferRef.
+  auto *arch = reinterpret_cast<const MachO::fat_arch *>(buf + sizeof(*hdr));
+
+  for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {
+    if (read32be(&arch[i].cputype) != target->cpuType ||
+        read32be(&arch[i].cpusubtype) != target->cpuSubtype)
+      continue;
+
+    uint32_t offset = read32be(&arch[i].offset);
+    uint32_t size = read32be(&arch[i].size);
+    if (offset + size > mbref.getBufferSize())
+      error(path + ": broken fat file");
+    return MemoryBufferRef(StringRef(buf + offset, size), path);
+  }
+
+  warn("Unable to find matching architecture in " + path);
   return None;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77006.253756.patch
Type: text/x-patch
Size: 1946 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200331/b5e861f6/attachment-0001.bin>


More information about the llvm-commits mailing list