Chris Bieneman | 00d8c1a | 2018-06-03 20:33:42 +0000 | [diff] [blame] | 1 | //===- unittest/BinaryFormat/MachOTest.cpp - MachO support tests ----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Bieneman | 00d8c1a | 2018-06-03 20:33:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/BinaryFormat/MachO.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | using namespace llvm; |
| 13 | using namespace llvm::MachO; |
| 14 | |
| 15 | TEST(MachOTest, UnalignedLC) { |
| 16 | unsigned char Valid32BitMachO[] = { |
| 17 | 0xCE, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, |
| 18 | 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, |
| 19 | 0x85, 0x80, 0x21, 0x01, 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, |
| 20 | 0x5F, 0x5F, 0x50, 0x41, 0x47, 0x45, 0x5A, 0x45, 0x52, 0x4F, 0x00, 0x00, |
| 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, |
| 22 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 24 | 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x4C, 0x49, |
| 25 | 0x4E, 0x4B, 0x45, 0x44, 0x49, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 26 | 0x00, 0x40, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, |
| 27 | 0x8C, 0x0B, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 28 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 29 | |
| 30 | mach_header *Header = |
| 31 | reinterpret_cast<mach_header *>(Valid32BitMachO); |
| 32 | if (!sys::IsLittleEndianHost) |
| 33 | swapStruct(*Header); |
| 34 | ASSERT_EQ(Header->magic, MH_MAGIC); |
| 35 | unsigned char *Current = Valid32BitMachO + sizeof(mach_header); |
| 36 | unsigned char *BufferEnd = |
| 37 | Valid32BitMachO + sizeof(mach_header) + Header->sizeofcmds; |
| 38 | while (Current < BufferEnd) { |
| 39 | macho_load_command *LC = |
| 40 | reinterpret_cast<macho_load_command *>(Current); |
| 41 | if (!sys::IsLittleEndianHost) |
| 42 | swapStruct(LC->load_command_data); |
| 43 | ASSERT_EQ(LC->load_command_data.cmd, LC_SEGMENT); |
| 44 | Current += LC->load_command_data.cmdsize; |
| 45 | } |
| 46 | } |