[lld-macho] Support linking against stub dylibs
authorJez Ng <jezng@fb.com>
Wed, 7 Oct 2020 21:50:42 +0000 (14:50 -0700)
committerJez Ng <jezng@fb.com>
Tue, 10 Nov 2020 20:19:27 +0000 (12:19 -0800)
Stub dylibs differ from "real" dylibs in that they lack any content in
their sections. What they do have are export tries and symbol tables,
which means we can still link against them. I am unclear how to
properly create these stub dylibs; XCode 11.3's `lipo` is able to create
stub dylibs, but those lack LC_ID_DYLIB load commands and are considered
invalid by most tooling. Newer versions of `lipo` aren't able to create
stub dylibs at all.  However, recent SDKs in XCode still come with valid
stub dylibs, so it still seems worthwhile to support them. The YAML in
this diff's test was generated by taking a non-stub dylib and editing
the appropriate fields.

Reviewed By: #lld-macho, smeenai

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

lld/MachO/Driver.cpp
lld/test/MachO/dylib-stub.yaml [new file with mode: 0644]

index 482e4a1..98bf5df 100644 (file)
@@ -312,6 +312,7 @@ static InputFile *addFile(StringRef path) {
     newFile = make<ObjFile>(mbref);
     break;
   case file_magic::macho_dynamically_linked_shared_lib:
+  case file_magic::macho_dynamically_linked_shared_lib_stub:
     newFile = make<DylibFile>(mbref);
     break;
   case file_magic::tapi_file: {
diff --git a/lld/test/MachO/dylib-stub.yaml b/lld/test/MachO/dylib-stub.yaml
new file mode 100644 (file)
index 0000000..a47b655
--- /dev/null
@@ -0,0 +1,138 @@
+## Stub dylibs differ from "real" dylibs in that they lack any content in their
+## sections. What they do have are export tries and symbol tables, which means
+## we can still link against them. I (jezng) am unclear how to properly create
+## these stub dylibs; XCode 11.3's `lipo` is able to create stub dylibs, but
+## those lack LC_ID_DYLIB load commands and are considered invalid by most
+## tooling. Newer versions of `lipo` aren't able to create stub dylibs at all.
+## However, recent SDKs in XCode still come with valid stub dylibs, so it still
+## seems worthwhile to support them. The YAML in this test was generated by
+## taking a non-stub dylib and editing the appropriate fields.
+
+# REQUIRES: x86
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
+# RUN: yaml2obj %t/fat.dylib.yaml > fat.dylib
+# RUN: %lld -lSystem %t/test.o fat.dylib -o %t/test
+# RUN: llvm-objdump --macho --lazy-bind %t/test | FileCheck %s
+# CHECK: __DATA   __la_symbol_ptr    0x100001008 foo  _foo
+
+#--- test.s
+.globl _main
+_main:
+  callq _foo
+  ret
+
+#--- fat.dylib.yaml
+--- !fat-mach-o
+FatHeader:
+  magic:           0xCAFEBABE
+  nfat_arch:       1
+FatArchs:
+  - cputype:         0x01000007
+    cpusubtype:      0x00000003
+    offset:          0x0000000000001000
+    size:            16432
+    align:           12
+Slices:
+  - !mach-o
+    FileHeader:
+      magic:           0xFEEDFACF
+      cputype:         0x01000007
+      cpusubtype:      0x00000003
+      filetype:        0x00000009
+      ncmds:           5
+      sizeofcmds:      568
+      flags:           0x00100085
+      reserved:        0x00000000
+    LoadCommands:
+      - cmd:             LC_SEGMENT_64
+        cmdsize:         232
+        segname:         __TEXT
+        vmaddr:          0
+        vmsize:          16384
+        fileoff:         0
+        filesize:        16384
+        maxprot:         5
+        initprot:        5
+        nsects:          1
+        flags:           0
+        Sections:
+          - sectname:        __text
+            segname:         __TEXT
+            addr:            0x0000000000000000
+            size:            0
+            offset:          0x00003FB7
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x80000400
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+      - cmd:             LC_SEGMENT_64
+        cmdsize:         72
+        segname:         __LINKEDIT
+        vmaddr:          16384
+        vmsize:          16384
+        fileoff:         16384
+        filesize:        48
+        maxprot:         1
+        initprot:        1
+        nsects:          0
+        flags:           0
+      - cmd:             LC_ID_DYLIB
+        cmdsize:         40
+        dylib:
+          name:            24
+          timestamp:       1
+          current_version: 0
+          compatibility_version: 0
+        PayloadString:   foo.dylib
+        ZeroPadBytes:    7
+      - cmd:             LC_DYLD_INFO_ONLY
+        cmdsize:         48
+        rebase_off:      0
+        rebase_size:     0
+        bind_off:        0
+        bind_size:       0
+        weak_bind_off:   0
+        weak_bind_size:  0
+        lazy_bind_off:   0
+        lazy_bind_size:  0
+        export_off:      16384
+        export_size:     16
+      - cmd:             LC_SYMTAB
+        cmdsize:         24
+        symoff:          16408
+        nsyms:           1
+        stroff:          16424
+        strsize:         8
+    LinkEditData:
+      ExportTrie:
+        TerminalSize:    0
+        NodeOffset:      0
+        Name:            ''
+        Flags:           0x0000000000000000
+        Address:         0x0000000000000000
+        Other:           0x0000000000000000
+        ImportName:      ''
+        Children:
+          - TerminalSize:    3
+            NodeOffset:      8
+            Name:            _foo
+            Flags:           0x0000000000000000
+            Address:         0x0000000000003FB7
+            Other:           0x0000000000000000
+            ImportName:      ''
+      NameList:
+        - n_strx:          2
+          n_type:          0x0F
+          n_sect:          1
+          n_desc:          0
+          n_value:         16311
+      StringTable:
+        - ' '
+        - _foo
+        - ''
+...