Index: include/llvm/Support/Endian.h =================================================================== --- include/llvm/Support/Endian.h +++ include/llvm/Support/Endian.h @@ -15,6 +15,7 @@ #define LLVM_SUPPORT_ENDIAN_H #include "llvm/Support/AlignOf.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Host.h" #include "llvm/Support/SwapByteOrder.h" @@ -56,6 +57,26 @@ return byte_swap(ret); } +/// Read a value of a particular endianness and size from memory. +template +inline value_type read(const void *memory, std::size_t size) { + switch (size) { + default: + llvm_unreachable("Unexpected size"); + break; + case 1: + return support::endian::read(memory); + case 2: + return support::endian::read(memory); + case 4: + return support::endian::read(memory); + case 8: + return support::endian::read(memory); + } +} + /// Read a value of a particular endianness from a buffer, and increment the /// buffer past that value. template Index: lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h =================================================================== --- lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h +++ lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h @@ -142,8 +142,9 @@ RelI->getOffset(Offset); uint8_t *LocalAddress = Section.Address + Offset; unsigned NumBytes = 1 << Size; - int64_t Addend = 0; - memcpy(&Addend, LocalAddress, NumBytes); + int64_t Addend = + support::endian::read( + LocalAddress, NumBytes); ++RelI; MachO::any_relocation_info RE2 = @@ -202,8 +203,9 @@ RelI->getOffset(Offset); uint8_t *LocalAddress = Section.Address + Offset; unsigned NumBytes = 1 << Size; - int64_t Addend = 0; - memcpy(&Addend, LocalAddress, NumBytes); + int64_t Addend = + support::endian::read( + LocalAddress, NumBytes); unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE); section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);