[MCJIT] Respect target endianness in RuntimeDyldMachO and RuntimeDyldChecker.
authorLang Hames <lhames@gmail.com>
Mon, 18 Aug 2014 21:43:16 +0000 (21:43 +0000)
committerLang Hames <lhames@gmail.com>
Mon, 18 Aug 2014 21:43:16 +0000 (21:43 +0000)
This patch may address some of the issues described in http://llvm.org/PR20640.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215938 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp
lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp
lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h

index d244f45b0803b767481b5d703acfca9aaa865ca0..02504a9cefcd3d3c7b9a3e995cee908bad9c6560 100644 (file)
@@ -705,7 +705,16 @@ uint64_t RuntimeDyldCheckerImpl::readMemoryAtAddr(uint64_t SrcAddr,
   assert(PtrSizedAddr == SrcAddr && "Linker memory pointer out-of-range.");
   uint8_t *Src = reinterpret_cast<uint8_t *>(PtrSizedAddr);
   uint64_t Result = 0;
-  memcpy(&Result, Src, Size);
+
+  // If host and target endianness match use memcpy, otherwise copy in reverse
+  // order.
+  if (getRTDyld().IsTargetLittleEndian == sys::IsLittleEndianHost)
+    memcpy(&Result, Src, Size);
+  else {
+    uint8_t *Dst = reinterpret_cast<uint8_t*>(&Result) + Size - 1;
+    for (unsigned i = 0; i < Size; ++i)
+      *Dst-- = *Src++;
+  }
   return Result;
 }
 
index 986daef8a907f97b8ddb5262abb7c3b486f57c37..3e27a8b8d5553fe2b2c3c1d2b54395d6df0d7858 100644 (file)
@@ -110,11 +110,18 @@ void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
          << " Size: " << (1 << RE.Size) << "\n";
 }
 
-bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Addr, uint64_t Value,
+bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Dst, uint64_t Value,
                                            unsigned Size) {
-  for (unsigned i = 0; i < Size; ++i) {
-    *Addr++ = (uint8_t)Value;
-    Value >>= 8;
+
+
+  // If host and target endianness match use memcpy, otherwise copy in reverse
+  // order.
+  if (IsTargetLittleEndian == sys::IsLittleEndianHost)
+    memcpy(Dst, &Value, Size);
+  else {
+    uint8_t *Src = reinterpret_cast<uint8_t*>(&Value) + Size - 1;
+    for (unsigned i = 0; i < Size; ++i)
+      *Dst++ = *Src--;
   }
 
   return false;
index d8cc7bfc529ed1fbd0670bf5889de6f4cc169e44..45954a4918b65cf01b8fe86241324dbe0bd803b0 100644 (file)
@@ -119,7 +119,7 @@ public:
 
   /// Write the least significant 'Size' bytes in 'Value' out at the address
   /// pointed to by Addr. Check for overflow.
-  bool writeBytesUnaligned(uint8_t *Addr, uint64_t Value, unsigned Size);
+  bool writeBytesUnaligned(uint8_t *Dst, uint64_t Value, unsigned Size);
 
   SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }