[lld] r220597 - [mach-o] Support data-only dylibs

Nick Kledzik kledzik at apple.com
Fri Oct 24 15:19:23 PDT 2014


Author: kledzik
Date: Fri Oct 24 17:19:22 2014
New Revision: 220597

URL: http://llvm.org/viewvc/llvm-project?rev=220597&view=rev
Log:
[mach-o] Support data-only dylibs

In final linked shared images, the __TEXT segment contains both code and
the mach-o header/load-commands.  In the case of a data-only dylib, there is
no code, so we need to force the addition of the __TEXT segment.

Added:
    lld/trunk/test/mach-o/data-only-dylib.yaml
Modified:
    lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
    lld/trunk/test/mach-o/exe-offsets.yaml
    lld/trunk/test/mach-o/exe-segment-overlap.yaml

Modified: lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp?rev=220597&r1=220596&r2=220597&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/CompactUnwindPass.cpp Fri Oct 24 17:19:22 2014
@@ -291,6 +291,10 @@ private:
 
     collectDwarfFrameEntries(mergedFile, dwarfFrames);
 
+    // Skip rest of pass if no unwind info.
+    if (unwindLocs.empty() && dwarfFrames.empty())
+      return;
+
     // FIXME: if there are more than 4 personality functions then we need to
     // defer to DWARF info for the ones we don't put in the list. They should
     // also probably be sorted by frequency.

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp?rev=220597&r1=220596&r2=220597&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp Fri Oct 24 17:19:22 2014
@@ -500,6 +500,9 @@ void MachOFileLayout::buildFileOffsets()
                   << ", fileOffset=" << _segInfo[&sg].fileOffset << "\n");
 
     uint32_t segFileSize = 0;
+    // A segment that is not zero-fill must use a least one page of disk space.
+    if (sg.access)
+      segFileSize = _file.pageSize;
     for (const Section *s : _segInfo[&sg].sections) {
       uint32_t sectOffset = s->address - sg.address;
       uint32_t sectFileSize =

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=220597&r1=220596&r2=220597&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Fri Oct 24 17:19:22 2014
@@ -394,9 +394,19 @@ void Util::organizeSections() {
       si->finalSectionIndex = sectionIndex++;
     }
   } else {
-    // Main executables, need a zero-page segment
-    if (_context.outputMachOType() == llvm::MachO::MH_EXECUTE)
+    switch (_context.outputMachOType()){
+    case llvm::MachO::MH_EXECUTE:
+      // Main executables, need a zero-page segment
       segmentForName("__PAGEZERO");
+      // Fall into next case.
+    case llvm::MachO::MH_DYLIB:
+    case llvm::MachO::MH_BUNDLE:
+      // All dynamic code needs TEXT segment to hold the load commands.
+      segmentForName("__TEXT");
+      break;
+    default:
+      break;
+    }
     // Group sections into segments.
     for (SectionInfo *si : _sectionInfos) {
       SegmentInfo *seg = segmentForName(si->segmentName);

Added: lld/trunk/test/mach-o/data-only-dylib.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/data-only-dylib.yaml?rev=220597&view=auto
==============================================================================
--- lld/trunk/test/mach-o/data-only-dylib.yaml (added)
+++ lld/trunk/test/mach-o/data-only-dylib.yaml Fri Oct 24 17:19:22 2014
@@ -0,0 +1,27 @@
+# RUN: lld -flavor darwin -arch x86_64 -dylib %s -o %t %p/Inputs/libSystem.yaml
+# RUN: llvm-nm %t | FileCheck %s
+#
+# Test that a data-only dylib can be built.
+#
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_OBJECT
+flags:           [ MH_SUBSECTIONS_VIA_SYMBOLS ]
+sections:
+  - segment:         __DATA
+    section:         __data
+    type:            S_REGULAR
+    attributes:      [  ]
+    alignment:       2
+    address:         0x0000000000000000
+    content:         [ 0x00, 0x00, 0x00, 0x00 ]
+global-symbols:  
+  - name:            _myData
+    type:            N_SECT
+    scope:           [ N_EXT ]
+    sect:            1
+    value:           0x0000000000000000
+...
+
+# CHECK: _myData

Modified: lld/trunk/test/mach-o/exe-offsets.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/exe-offsets.yaml?rev=220597&r1=220596&r2=220597&view=diff
==============================================================================
--- lld/trunk/test/mach-o/exe-offsets.yaml (original)
+++ lld/trunk/test/mach-o/exe-offsets.yaml Fri Oct 24 17:19:22 2014
@@ -1,4 +1,4 @@
-# RUN: lld -flavor darwin -arch x86_64 %s -o %t -e start
+# RUN: lld -flavor darwin -arch x86_64 %s -o %t -e start %p/Inputs/libSystem.yaml
 # RUN: llvm-readobj -sections %t | FileCheck %s
 
 # Make sure data gets put at offset
@@ -25,27 +25,6 @@ defined-atoms:
      type:            data
      content:         [ 01 ]
 
---- !mach-o
-arch:            x86_64
-file-type:       MH_DYLIB
-flags:           [ ]
-install-name:    /usr/lib/libSystem.B.dylib
-sections:
-  - segment:         __TEXT
-    section:         __text
-    type:            S_REGULAR
-    attributes:      [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS ]
-    address:         0x0000000000000000
-    content:         [ 0x55 ]
-
-global-symbols:
-  - name:            dyld_stub_binder
-    type:            N_SECT
-    scope:           [ N_EXT ]
-    sect:            1
-    value:           0x0000000000000000
-
-
 
 # CHECK-LABEL: Section {
 # CHECK:     Name: __text
@@ -54,9 +33,6 @@ global-symbols:
 # CHECK:     Offset: 0
 
 # CHECK-LABEL: Section {
-# CHECK:     Name: __unwind_info
-
-# CHECK-LABEL: Section {
 # CHECK:     Name: __data
 # CHECK:     Segment: __DATA
 # CHECK:     Size: 0x5

Modified: lld/trunk/test/mach-o/exe-segment-overlap.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/exe-segment-overlap.yaml?rev=220597&r1=220596&r2=220597&view=diff
==============================================================================
--- lld/trunk/test/mach-o/exe-segment-overlap.yaml (original)
+++ lld/trunk/test/mach-o/exe-segment-overlap.yaml Fri Oct 24 17:19:22 2014
@@ -23,10 +23,7 @@ defined-atoms:
 # CHECK:     Name: __text
 # CHECK:     Segment: __TEXT
 # CHECK:     Size: 0x1
-# CHECK:     Offset: 4027
-
-# CHECK-LABEL: Section {
-# CHECK:     Name: __unwind_info
+# CHECK:     Offset: 4095
 
 # CHECK-LABEL: Section {
 # CHECK:     Name: __data





More information about the llvm-commits mailing list