From f48f0620ed617896cb48738b67fcc65bae993770 Mon Sep 17 00:00:00 2001 From: Tim Northover Date: Wed, 25 Jun 2014 11:21:51 +0000 Subject: [PATCH] [mach-o]: atomize zero-terminated literals correctly. When looking through sections with zero-terminated string-literals (__cstring or __ustring) we were constantly rechecking the first few bytes of the string for '\0' rather than advancing along. This obviously failed unless all strings within the section had the same length as that first one. llvm-svn: 211682 --- lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp | 10 +++++----- lld/test/mach-o/parse-literals.yaml | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp index 7de85e8..1a03ede 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp @@ -352,9 +352,9 @@ std::error_code processSection(DefinedAtom::ContentType atomType, case atomizeUTF8: // Break section up into zero terminated c-strings. size = 0; - for (unsigned int i=0; offset+i < e; ++i) { + for (unsigned int i = offset; i < e; ++i) { if (section.content[i] == 0) { - size = i+1; + size = i + 1 - offset; break; } } @@ -362,9 +362,9 @@ std::error_code processSection(DefinedAtom::ContentType atomType, case atomizeUTF16: // Break section up into zero terminated UTF16 strings. size = 0; - for (unsigned int i=0; offset+i < e; i += 2) { - if ((section.content[i] == 0) && (section.content[i+1] == 0)) { - size = i+2; + for (unsigned int i = offset; i < e; i += 2) { + if ((section.content[i] == 0) && (section.content[i + 1] == 0)) { + size = i + 2 - offset; break; } } diff --git a/lld/test/mach-o/parse-literals.yaml b/lld/test/mach-o/parse-literals.yaml index 46ddb90..24568ea 100644 --- a/lld/test/mach-o/parse-literals.yaml +++ b/lld/test/mach-o/parse-literals.yaml @@ -18,7 +18,7 @@ sections: address: 0x0000000000000100 content: [ 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x00, 0x74, 0x68, 0x65, 0x72, 0x65, 0x00, 0x77, 0x6F, 0x72, 0x6C, - 0x64, 0x00 ] + 0x00 ] - segment: __TEXT section: __literal4 type: S_4BYTE_LITERALS @@ -51,7 +51,7 @@ sections: 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 ] + 0x65, 0x00, 0x72, 0x00, 0x00, 0x00 ] ... @@ -64,13 +64,13 @@ sections: # CHECK: content: [ 74, 68, 65, 72, 65, 00 ] # CHECK: - scope: hidden # CHECK: type: c-string -# CHECK: content: [ 77, 6F, 72, 6C, 64, 00 ] +# CHECK: content: [ 77, 6F, 72, 6C, 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: content: [ 74, 00, 68, 00, 65, 00, 72, 00, 00, 00 ] # CHECK: - scope: hidden # CHECK: type: const-4-byte # CHECK: content: [ 01, 02, 03, 04 ] -- 2.7.4