From: Pete Cooper Date: Thu, 24 Mar 2016 01:05:17 +0000 (+0000) Subject: Use a memcpy to avoid unaligned store UB. X-Git-Tag: llvmorg-3.9.0-rc1~11086 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=07601d33f8349b563e1b5d8c12ec59aa49b0deca;p=platform%2Fupstream%2Fllvm.git Use a memcpy to avoid unaligned store UB. On a 32-bit output, we may write LC_MAIN (which contains a uint64_t) to an unaligned address. This changes it to use a memcpy instead which is UB safe. llvm-svn: 264232 --- diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp index a0a3df4..f85dbba 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp @@ -943,13 +943,16 @@ std::error_code MachOFileLayout::writeLoadCommands() { // If main executable, add LC_MAIN. if (_file.fileType == llvm::MachO::MH_EXECUTE) { // Build LC_MAIN load command. - entry_point_command* ep = reinterpret_cast(lc); - ep->cmd = LC_MAIN; - ep->cmdsize = sizeof(entry_point_command); - ep->entryoff = _file.entryAddress - _seg1addr; - ep->stacksize = _file.stackSize; + // Note, using a temporary here to appease UB as we may not be aligned + // enough for a struct containing a uint64_t when emitting a 32-bit binary + entry_point_command ep; + ep.cmd = LC_MAIN; + ep.cmdsize = sizeof(entry_point_command); + ep.entryoff = _file.entryAddress - _seg1addr; + ep.stacksize = _file.stackSize; if (_swap) - swapStruct(*ep); + swapStruct(ep); + memcpy(lc, &ep, sizeof(entry_point_command)); lc += sizeof(entry_point_command); }