diff --git a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp --- a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp +++ b/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(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( + LoadCmd.Ptr + offsetof(MachO::segment_command, segname))) == + TextSegmentName) O.TextSegmentCommandIndex = O.LoadCommands.size(); if (Expected>> Sections = @@ -137,9 +140,12 @@ return Sections.takeError(); break; case MachO::LC_SEGMENT_64: - if (StringRef( - reinterpret_cast(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( + LoadCmd.Ptr + offsetof(MachO::segment_command_64, segname))) == + TextSegmentName) O.TextSegmentCommandIndex = O.LoadCommands.size(); if (Expected>> Sections =