[lld] r218894 - [mach-o] preserve custom section names on coalesable strings

Nick Kledzik kledzik at apple.com
Thu Oct 2 10:27:20 PDT 2014


Author: kledzik
Date: Thu Oct  2 12:27:20 2014
New Revision: 218894

URL: http://llvm.org/viewvc/llvm-project?rev=218894&view=rev
Log:
[mach-o] preserve custom section names on coalesable strings

Added:
    lld/trunk/test/mach-o/cstring-sections.yaml
Modified:
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp?rev=218894&r1=218893&r2=218894&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp Thu Oct  2 12:27:20 2014
@@ -88,8 +88,10 @@ const MachORelocatableSectionToAtomType
 
 
 /// Figures out ContentType of a mach-o section.
-DefinedAtom::ContentType atomTypeFromSection(const Section &section) {
+DefinedAtom::ContentType atomTypeFromSection(const Section &section,
+                                             bool &customSectionName) {
   // First look for match of name and type. Empty names in table are wildcards.
+  customSectionName = false;
   for (const MachORelocatableSectionToAtomType *p = sectsToAtomType ;
                                  p->atomType != DefinedAtom::typeUnknown; ++p) {
     if (p->sectionType != section.type)
@@ -98,6 +100,7 @@ DefinedAtom::ContentType atomTypeFromSec
       continue;
     if (!p->sectionName.equals(section.sectionName) && !p->sectionName.empty())
       continue;
+    customSectionName = p->segmentName.empty() && p->sectionName.empty();
     return p->atomType;
   }
   // Look for code denoted by section attributes
@@ -343,6 +346,7 @@ std::error_code processSymboledSection(D
 
 std::error_code processSection(DefinedAtom::ContentType atomType,
                                const Section &section,
+                               bool customSectionName,
                                const NormalizedFile &normalizedFile,
                                MachOFile &file, bool copyRefs) {
   const bool is64 = MachOLinkingContext::is64Bit(normalizedFile.arch);
@@ -432,8 +436,19 @@ std::error_code processSection(DefinedAt
                                      + " is malformed.  The last atom is "
                                      "not zero terminated.");
       }
-      file.addDefinedAtom(StringRef(), scope, atomType, merge, offset, size,
-                          false, false, copyRefs, &section);
+      if (customSectionName) {
+        // Mach-O needs a segment and section name.  Concatentate those two
+        // with a / separator (e.g. "seg/sect") to fit into the lld model
+        // of just a section name.
+        std::string segSectName = section.segmentName.str()
+                                  + "/" + section.sectionName.str();
+        file.addDefinedAtomInCustomSection(StringRef(), scope, atomType,
+                                           merge, false, false, offset,
+                                           size, segSectName, true, &section);
+      } else {
+        file.addDefinedAtom(StringRef(), scope, atomType, merge, offset, size,
+                            false, false, copyRefs, &section);
+      }
       offset += size;
     }
   }
@@ -599,9 +614,12 @@ normalizedObjectToAtoms(const Normalized
   for (auto &sect : normalizedFile.sections) {
     if (isDebugInfoSection(sect))
       continue;
-    DefinedAtom::ContentType atomType = atomTypeFromSection(sect);
+    bool customSectionName;
+    DefinedAtom::ContentType atomType = atomTypeFromSection(sect,
+                                                            customSectionName);
     if (std::error_code ec =
-            processSection(atomType, sect, normalizedFile, *file, copyRefs))
+            processSection(atomType, sect, customSectionName, normalizedFile,
+                           *file, copyRefs))
       return ec;
   }
   // Create atoms from undefined symbols.

Added: lld/trunk/test/mach-o/cstring-sections.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/cstring-sections.yaml?rev=218894&view=auto
==============================================================================
--- lld/trunk/test/mach-o/cstring-sections.yaml (added)
+++ lld/trunk/test/mach-o/cstring-sections.yaml Thu Oct  2 12:27:20 2014
@@ -0,0 +1,91 @@
+# RUN: lld -flavor darwin -arch x86_64 -r  %s -o %t -print_atoms | FileCheck %s
+#
+# Test -keep_private_externs in -r mode.
+#
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_OBJECT
+flags:           [ MH_SUBSECTIONS_VIA_SYMBOLS ]
+sections:
+  - segment:         __TEXT
+    section:         __objc_methname
+    type:            S_CSTRING_LITERALS
+    attributes:      [  ]
+    address:         0x0000000000000000
+    content:         [ 0x61, 0x62, 0x63, 0x00, 0x64, 0x65, 0x66, 0x00 ]
+  - segment:         __TEXT
+    section:         __objc_classname
+    type:            S_CSTRING_LITERALS
+    attributes:      [  ]
+    address:         0x0000000000000006
+    content:         [ 0x61, 0x62, 0x63, 0x00, 0x67, 0x68, 0x69, 0x00 ]
+  - segment:         __TEXT
+    section:         __cstring
+    type:            S_CSTRING_LITERALS
+    attributes:      [  ]
+    address:         0x000000000000000A
+    content:         [ 0x61, 0x62, 0x63, 0x00, 0x6A, 0x6B, 0x6C, 0x00 ]
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_OBJECT
+flags:           [ MH_SUBSECTIONS_VIA_SYMBOLS ]
+has-UUID:        false
+OS:              unknown
+sections:
+  - segment:         __TEXT
+    section:         __objc_methname
+    type:            S_CSTRING_LITERALS
+    attributes:      [  ]
+    address:         0x0000000000000000
+    content:         [ 0x61, 0x62, 0x63, 0x00 ]
+  - segment:         __TEXT
+    section:         __objc_classname
+    type:            S_CSTRING_LITERALS
+    attributes:      [  ]
+    address:         0x0000000000000006
+    content:         [ 0x61, 0x62, 0x63, 0x00 ]
+  - segment:         __TEXT
+    section:         __cstring
+    type:            S_CSTRING_LITERALS
+    attributes:      [  ]
+    address:         0x000000000000000A
+    content:         [ 0x61, 0x62, 0x63, 0x00 ]
+
+
+...
+
+# CHECK: defined-atoms:
+# CHECK:   - scope:           hidden
+# CHECK:     type:            c-string
+# CHECK:     content:         [ 61, 62, 63, 00 ]
+# CHECK:     merge:           by-content
+# CHECK:     section-choice:  custom-required
+# CHECK:     section-name:    __TEXT/__objc_methname
+# CHECK:   - scope:           hidden
+# CHECK:     type:            c-string
+# CHECK:     content:         [ 64, 65, 66, 00 ]
+# CHECK:     merge:           by-content
+# CHECK:     section-choice:  custom-required
+# CHECK:     section-name:    __TEXT/__objc_methname
+# CHECK:   - scope:           hidden
+# CHECK:     type:            c-string
+# CHECK:     content:         [ 61, 62, 63, 00 ]
+# CHECK:     merge:           by-content
+# CHECK:     section-choice:  custom-required
+# CHECK:     section-name:    __TEXT/__objc_classname
+# CHECK:   - scope:           hidden
+# CHECK:     type:            c-string
+# CHECK:     content:         [ 67, 68, 69, 00 ]
+# CHECK:     merge:           by-content
+# CHECK:     section-choice:  custom-required
+# CHECK:     section-name:    __TEXT/__objc_classname
+# CHECK:   - scope:           hidden
+# CHECK:     type:            c-string
+# CHECK:     content:         [ 61, 62, 63, 00 ]
+# CHECK:     merge:           by-content
+# CHECK:   - scope:           hidden
+# CHECK:     type:            c-string
+# CHECK:     content:         [ 6A, 6B, 6C, 00 ]
+# CHECK:     merge:           by-content





More information about the llvm-commits mailing list