diff options
| author | Nick Kledzik <kledzik@apple.com> | 2014-10-02 17:27:20 +0000 |
|---|---|---|
| committer | Nick Kledzik <kledzik@apple.com> | 2014-10-02 17:27:20 +0000 |
| commit | 7efd054479b0df398af773c415f5f8aa5052f8cf (patch) | |
| tree | a3681456d6c41ae62a4c7a987f332aa9004dbd7b | |
| parent | 32d0d09bf888c09f469d3239f9f3344c55055e45 (diff) | |
| download | bcm5719-llvm-7efd054479b0df398af773c415f5f8aa5052f8cf.tar.gz bcm5719-llvm-7efd054479b0df398af773c415f5f8aa5052f8cf.zip | |
[mach-o] preserve custom section names on coalesable strings
llvm-svn: 218894
| -rw-r--r-- | lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp | 28 | ||||
| -rw-r--r-- | lld/test/mach-o/cstring-sections.yaml | 91 |
2 files changed, 114 insertions, 5 deletions
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp index 7a5c3c31235..8f83672cc35 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp @@ -88,8 +88,10 @@ const MachORelocatableSectionToAtomType sectsToAtomType[] = { /// Figures out ContentType of a mach-o section. -DefinedAtom::ContentType atomTypeFromSection(const Section §ion) { +DefinedAtom::ContentType atomTypeFromSection(const Section §ion, + 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 atomTypeFromSection(const Section §ion) { 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(DefinedAtom::ContentType atomType, std::error_code processSection(DefinedAtom::ContentType atomType, const Section §ion, + bool customSectionName, const NormalizedFile &normalizedFile, MachOFile &file, bool copyRefs) { const bool is64 = MachOLinkingContext::is64Bit(normalizedFile.arch); @@ -432,8 +436,19 @@ std::error_code processSection(DefinedAtom::ContentType atomType, + " is malformed. The last atom is " "not zero terminated."); } - file.addDefinedAtom(StringRef(), scope, atomType, merge, offset, size, - false, false, copyRefs, §ion); + 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, §ion); + } else { + file.addDefinedAtom(StringRef(), scope, atomType, merge, offset, size, + false, false, copyRefs, §ion); + } offset += size; } } @@ -599,9 +614,12 @@ normalizedObjectToAtoms(const NormalizedFile &normalizedFile, StringRef path, for (auto § : 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. diff --git a/lld/test/mach-o/cstring-sections.yaml b/lld/test/mach-o/cstring-sections.yaml new file mode 100644 index 00000000000..940f048b5c6 --- /dev/null +++ b/lld/test/mach-o/cstring-sections.yaml @@ -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 |

