[lld] 2e8e1bd - [lld-macho] Support linking against stub dylibs

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 10 12:19:39 PST 2020


Author: Jez Ng
Date: 2020-11-10T12:19:27-08:00
New Revision: 2e8e1bdb89392f8bbe1c3886b1f8da8746659bb2

URL: https://github.com/llvm/llvm-project/commit/2e8e1bdb89392f8bbe1c3886b1f8da8746659bb2
DIFF: https://github.com/llvm/llvm-project/commit/2e8e1bdb89392f8bbe1c3886b1f8da8746659bb2.diff

LOG: [lld-macho] Support linking against stub dylibs

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

Added: 
    lld/test/MachO/dylib-stub.yaml

Modified: 
    lld/MachO/Driver.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 482e4a13a8b4..98bf5df5adac 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -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
index 000000000000..a47b655b3233
--- /dev/null
+++ b/lld/test/MachO/dylib-stub.yaml
@@ -0,0 +1,138 @@
+## Stub dylibs 
diff er 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
+        - ''
+...


        


More information about the llvm-commits mailing list