[PATCH] D112744: [llvm-objcopy] Fix misaligned access to load command data.

Daniel Rodríguez Troitiño via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 28 22:17:13 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rG8fbe1e760224: [llvm-objcopy] Fix misaligned access to load command data. (authored by drodriguez).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112744/new/

https://reviews.llvm.org/D112744

Files:
  llvm/tools/llvm-objcopy/MachO/MachOReader.cpp


Index: llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
===================================================================
--- llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
+++ llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
@@ -124,9 +124,12 @@
       O.CodeSignatureCommandIndex = O.LoadCommands.size();
       break;
     case MachO::LC_SEGMENT:
-      if (StringRef(
-              reinterpret_cast<MachO::segment_command const *>(LoadCmd.Ptr)
-                  ->segname) == TextSegmentName)
+      // LoadCmd.Ptr might not be aligned temporarily as
+      // MachO::segment_command requires, but the segname char pointer do not
+      // have alignment restrictions.
+      if (StringRef(reinterpret_cast<const char *>(
+              LoadCmd.Ptr + offsetof(MachO::segment_command, segname))) ==
+          TextSegmentName)
         O.TextSegmentCommandIndex = O.LoadCommands.size();
 
       if (Expected<std::vector<std::unique_ptr<Section>>> Sections =
@@ -137,9 +140,12 @@
         return Sections.takeError();
       break;
     case MachO::LC_SEGMENT_64:
-      if (StringRef(
-              reinterpret_cast<MachO::segment_command_64 const *>(LoadCmd.Ptr)
-                  ->segname) == TextSegmentName)
+      // LoadCmd.Ptr might not be aligned temporarily as
+      // MachO::segment_command_64 requires, but the segname char pointer do
+      // not have alignment restrictions.
+      if (StringRef(reinterpret_cast<const char *>(
+              LoadCmd.Ptr + offsetof(MachO::segment_command_64, segname))) ==
+          TextSegmentName)
         O.TextSegmentCommandIndex = O.LoadCommands.size();
 
       if (Expected<std::vector<std::unique_ptr<Section>>> Sections =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112744.383235.patch
Type: text/x-patch
Size: 1687 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211029/7f1b8a2a/attachment.bin>


More information about the llvm-commits mailing list