diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp index d244f45b080..02504a9cefc 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp @@ -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(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(&Result) + Size - 1; + for (unsigned i = 0; i < Size; ++i) + *Dst-- = *Src++; + } return Result; } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index 986daef8a90..3e27a8b8d55 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -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(&Value) + Size - 1; + for (unsigned i = 0; i < Size; ++i) + *Dst++ = *Src--; } return false; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h index d8cc7bfc529..45954a4918b 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h @@ -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]; }