[MCJIT] Respect target endianness in RuntimeDyldMachO and RuntimeDyldChecker.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyldMachO.cpp
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;