1 //===- unittest/BinaryFormat/MachOTest.cpp - MachO support tests ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/BinaryFormat/MachO.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 using namespace llvm::MachO;
15 
16 TEST(MachOTest, UnalignedLC) {
17   const unsigned char Valid32BitMachO[] = {
18       0xCE, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
19       0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
20       0x85, 0x80, 0x21, 0x01, 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
21       0x5F, 0x5F, 0x50, 0x41, 0x47, 0x45, 0x5A, 0x45, 0x52, 0x4F, 0x00, 0x00,
22       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
23       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25       0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x4C, 0x49,
26       0x4E, 0x4B, 0x45, 0x44, 0x49, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27       0x00, 0x40, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
28       0x8C, 0x0B, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
29       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
30 
31   const mach_header *Header =
32       reinterpret_cast<const mach_header *>(Valid32BitMachO);
33   ASSERT_EQ(Header->magic, MH_MAGIC);
34   const unsigned char *Current = Valid32BitMachO + sizeof(mach_header);
35   const unsigned char *BufferEnd =
36       Valid32BitMachO + sizeof(mach_header) + Header->sizeofcmds;
37   while (Current < BufferEnd) {
38     const macho_load_command *LC =
39         reinterpret_cast<const macho_load_command *>(Current);
40     ASSERT_EQ(LC->load_command_data.cmd, LC_SEGMENT);
41     Current += LC->load_command_data.cmdsize;
42   }
43 }
44