MachOTest.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //===- unittest/BinaryFormat/MachOTest.cpp - MachO support tests ----------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/BinaryFormat/MachO.h"
  9. #include "gtest/gtest.h"
  10. using namespace llvm;
  11. using namespace llvm::MachO;
  12. TEST(MachOTest, UnalignedLC) {
  13. unsigned char Valid32BitMachO[] = {
  14. 0xCE, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  15. 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
  16. 0x85, 0x80, 0x21, 0x01, 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
  17. 0x5F, 0x5F, 0x50, 0x41, 0x47, 0x45, 0x5A, 0x45, 0x52, 0x4F, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  21. 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x4C, 0x49,
  22. 0x4E, 0x4B, 0x45, 0x44, 0x49, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23. 0x00, 0x40, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  24. 0x8C, 0x0B, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  26. mach_header *Header =
  27. reinterpret_cast<mach_header *>(Valid32BitMachO);
  28. if (!sys::IsLittleEndianHost)
  29. swapStruct(*Header);
  30. ASSERT_EQ(Header->magic, MH_MAGIC);
  31. unsigned char *Current = Valid32BitMachO + sizeof(mach_header);
  32. unsigned char *BufferEnd =
  33. Valid32BitMachO + sizeof(mach_header) + Header->sizeofcmds;
  34. while (Current < BufferEnd) {
  35. macho_load_command *LC =
  36. reinterpret_cast<macho_load_command *>(Current);
  37. if (!sys::IsLittleEndianHost)
  38. swapStruct(LC->load_command_data);
  39. ASSERT_EQ(LC->load_command_data.cmd, LC_SEGMENT);
  40. Current += LC->load_command_data.cmdsize;
  41. }
  42. }