[mach-o] Add support for reading utf16 string literal sections
authorNick Kledzik <kledzik@apple.com>
Tue, 27 May 2014 20:25:06 +0000 (20:25 +0000)
committerNick Kledzik <kledzik@apple.com>
Tue, 27 May 2014 20:25:06 +0000 (20:25 +0000)
llvm-svn: 209684

lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
lld/test/mach-o/parse-literals.yaml

index 072f40d..c6d1e2c 100644 (file)
@@ -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;
index 596a8fe..7564748 100644 (file)
@@ -113,6 +113,23 @@ static error_code processSection(MachOFile &file, const Section &section,
   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<uint8_t> 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.
index 9169ca2..46ddb90 100644 (file)
@@ -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