[lld] r264232 - Use a memcpy to avoid unaligned store UB.
Pete Cooper via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 23 18:05:17 PDT 2016
Author: pete
Date: Wed Mar 23 20:05:17 2016
New Revision: 264232
URL: http://llvm.org/viewvc/llvm-project?rev=264232&view=rev
Log:
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.
Modified:
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp?rev=264232&r1=264231&r2=264232&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp Wed Mar 23 20:05:17 2016
@@ -943,13 +943,16 @@ std::error_code MachOFileLayout::writeLo
// If main executable, add LC_MAIN.
if (_file.fileType == llvm::MachO::MH_EXECUTE) {
// Build LC_MAIN load command.
- entry_point_command* ep = reinterpret_cast<entry_point_command*>(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);
}
More information about the llvm-commits
mailing list