Copy MachO struct to temporary to avoid unaligned load UB.
authorPete Cooper <peter_cooper@apple.com>
Wed, 23 Mar 2016 18:00:10 +0000 (18:00 +0000)
committerPete Cooper <peter_cooper@apple.com>
Wed, 23 Mar 2016 18:00:10 +0000 (18:00 +0000)
We were already copying this data to a temporary for endian swaps.  Now
we just always copy it, but still only do the endian swaps when needed.

llvm-svn: 264172

lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp

index 7a9dd4b..39860bb 100644 (file)
@@ -380,11 +380,11 @@ readBinary(std::unique_ptr<MemoryBuffer> &mb,
             reinterpret_cast<const nlist_64 *>(start + symOffset);
         // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
         for(uint32_t i=0; i < symCount; ++i) {
-          const nlist_64 *sin = &symbols[i];
           nlist_64 tempSym;
-          if (isBig != llvm::sys::IsBigEndianHost) {
-            tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
-          }
+          memcpy(&tempSym, &symbols[i], sizeof(nlist_64));
+          const nlist_64 *sin = &tempSym;
+          if (isBig != llvm::sys::IsBigEndianHost)
+            swapStruct(tempSym);
           Symbol sout;
           if (sin->n_strx > strSize)
             return true;