summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
diff options
context:
space:
mode:
authorPete Cooper <peter_cooper@apple.com>2016-03-24 01:05:17 +0000
committerPete Cooper <peter_cooper@apple.com>2016-03-24 01:05:17 +0000
commit07601d33f8349b563e1b5d8c12ec59aa49b0deca (patch)
treee349da697a6c2d926737f53722098aaaca89a233 /lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
parenta13f62f5f822aa7ea0e394c267b25400f8b88976 (diff)
downloadbcm5719-llvm-07601d33f8349b563e1b5d8c12ec59aa49b0deca.tar.gz
bcm5719-llvm-07601d33f8349b563e1b5d8c12ec59aa49b0deca.zip
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
Diffstat (limited to 'lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp')
-rw-r--r--lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp15
1 files changed, 9 insertions, 6 deletions
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
index a0a3df44ccd..f85dbba9d6e 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<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);
}
OpenPOWER on IntegriCloud