From 3e90e5f1874103a0a516f14ee8d226b2c6265446 Mon Sep 17 00:00:00 2001 From: Nick Kledzik Date: Tue, 27 May 2014 20:25:06 +0000 Subject: [PATCH] [mach-o] Add support for reading utf16 string literal sections llvm-svn: 209684 --- .../ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp | 3 +++ .../ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp | 17 +++++++++++++++++ lld/test/mach-o/parse-literals.yaml | 15 +++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp index 072f40d..c6d1e2c 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp @@ -188,6 +188,9 @@ SectionInfo *Util::makeSection(DefinedAtom::ContentType type) { case DefinedAtom::typeLiteral16: return new (_allocator) SectionInfo("__TEXT", "__literal16", S_16BYTE_LITERALS); + case DefinedAtom::typeUTF16String: + return new (_allocator) SectionInfo("__TEXT", "__ustring", + S_REGULAR); default: llvm_unreachable("TO DO: add support for more sections"); break; diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp index 596a8fe..7564748 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp @@ -113,6 +113,23 @@ static error_code processSection(MachOFile &file, const Section §ion, unsigned offset = 0; switch (section.type) { case llvm::MachO::S_REGULAR: + if (section.segmentName.equals("__TEXT") && + section.sectionName.equals("__ustring")) { + if ((section.content.size() % 4) != 0) + return make_dynamic_error_code(Twine("Section ") + section.segmentName + + "/" + section.sectionName + + " has a size that is not even"); + for (size_t i = 0, e = section.content.size(); i != e; i +=2) { + if ((section.content[i] == 0) && (section.content[i+1] == 0)) { + unsigned size = i - offset + 2; + ArrayRef utf16Content = section.content.slice(offset, size); + file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit, + DefinedAtom::typeUTF16String, utf16Content, + copyRefs); + offset = i + 2; + } + } + } case llvm::MachO::S_COALESCED: case llvm::MachO::S_ZEROFILL: // These sections are broken in to atoms based on symbols. diff --git a/lld/test/mach-o/parse-literals.yaml b/lld/test/mach-o/parse-literals.yaml index 9169ca2..46ddb90 100644 --- a/lld/test/mach-o/parse-literals.yaml +++ b/lld/test/mach-o/parse-literals.yaml @@ -43,6 +43,15 @@ sections: address: 0x0000000000000130 content: [ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00 ] + - segment: __TEXT + section: __ustring + type: S_REGULAR + attributes: [ ] + alignment: 0 + address: 0x0000000000000100 + content: [ 0x68, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, + 0x6F, 0x00, 0x00, 0x00, 0x74, 0x00, 0x68, 0x00, + 0x65, 0x00, 0x72, 0x00, 0x65, 0x00, 0x00, 0x00 ] ... @@ -57,6 +66,12 @@ sections: # CHECK: type: c-string # CHECK: content: [ 77, 6F, 72, 6C, 64, 00 ] # CHECK: - scope: hidden +# CHECK: type: utf16-string +# CHECK: content: [ 68, 00, 65, 00, 6C, 00, 6C, 00, 6F, 00, 00, 00 ] +# CHECK: - scope: hidden +# CHECK: type: utf16-string +# CHECK: content: [ 74, 00, 68, 00, 65, 00, 72, 00, 65, 00, 00, 00 ] +# CHECK: - scope: hidden # CHECK: type: const-4-byte # CHECK: content: [ 01, 02, 03, 04 ] # CHECK: - scope: hidden -- 2.7.4