diff options
Diffstat (limited to 'unittests/BinaryFormat')
-rw-r--r-- | unittests/BinaryFormat/CMakeLists.txt | 1 | ||||
-rw-r--r-- | unittests/BinaryFormat/DwarfTest.cpp | 64 | ||||
-rw-r--r-- | unittests/BinaryFormat/MachOTest.cpp | 47 | ||||
-rw-r--r-- | unittests/BinaryFormat/TestFileMagic.cpp | 3 |
4 files changed, 115 insertions, 0 deletions
diff --git a/unittests/BinaryFormat/CMakeLists.txt b/unittests/BinaryFormat/CMakeLists.txt index 631936795b6c..95c672e35be8 100644 --- a/unittests/BinaryFormat/CMakeLists.txt +++ b/unittests/BinaryFormat/CMakeLists.txt @@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS add_llvm_unittest(BinaryFormatTests DwarfTest.cpp + MachOTest.cpp TestFileMagic.cpp ) diff --git a/unittests/BinaryFormat/DwarfTest.cpp b/unittests/BinaryFormat/DwarfTest.cpp index f24e029beef2..08a731f6174d 100644 --- a/unittests/BinaryFormat/DwarfTest.cpp +++ b/unittests/BinaryFormat/DwarfTest.cpp @@ -9,6 +9,7 @@ #include "llvm/BinaryFormat/Dwarf.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/FormatVariadic.h" #include "gtest/gtest.h" using namespace llvm; @@ -139,4 +140,67 @@ TEST(DwarfTest, getVirtuality) { EXPECT_EQ(DW_VIRTUALITY_invalid, getVirtuality("something else")); } +TEST(DwarfTest, FixedFormSizes) { + Optional<uint8_t> RefSize; + Optional<uint8_t> AddrSize; + + // Test 32 bit DWARF version 2 with 4 byte addresses. + FormParams Params_2_4_32 = {2, 4, DWARF32}; + RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_2_4_32); + AddrSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_2_4_32); + EXPECT_TRUE(RefSize.hasValue()); + EXPECT_TRUE(AddrSize.hasValue()); + EXPECT_EQ(*RefSize, *AddrSize); + + // Test 32 bit DWARF version 2 with 8 byte addresses. + FormParams Params_2_8_32 = {2, 8, DWARF32}; + RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_2_8_32); + AddrSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_2_8_32); + EXPECT_TRUE(RefSize.hasValue()); + EXPECT_TRUE(AddrSize.hasValue()); + EXPECT_EQ(*RefSize, *AddrSize); + + // DW_FORM_ref_addr is 4 bytes in DWARF 32 in DWARF version 3 and beyond. + FormParams Params_3_4_32 = {3, 4, DWARF32}; + RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_3_4_32); + EXPECT_TRUE(RefSize.hasValue()); + EXPECT_EQ(*RefSize, 4); + + FormParams Params_4_4_32 = {4, 4, DWARF32}; + RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_4_4_32); + EXPECT_TRUE(RefSize.hasValue()); + EXPECT_EQ(*RefSize, 4); + + FormParams Params_5_4_32 = {5, 4, DWARF32}; + RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_5_4_32); + EXPECT_TRUE(RefSize.hasValue()); + EXPECT_EQ(*RefSize, 4); + + // DW_FORM_ref_addr is 8 bytes in DWARF 64 in DWARF version 3 and beyond. + FormParams Params_3_8_64 = {3, 8, DWARF64}; + RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_3_8_64); + EXPECT_TRUE(RefSize.hasValue()); + EXPECT_EQ(*RefSize, 8); + + FormParams Params_4_8_64 = {4, 8, DWARF64}; + RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_4_8_64); + EXPECT_TRUE(RefSize.hasValue()); + EXPECT_EQ(*RefSize, 8); + + FormParams Params_5_8_64 = {5, 8, DWARF64}; + RefSize = getFixedFormByteSize(DW_FORM_ref_addr, Params_5_8_64); + EXPECT_TRUE(RefSize.hasValue()); + EXPECT_EQ(*RefSize, 8); +} + +TEST(DwarfTest, format_provider) { + EXPECT_EQ("DW_AT_name", formatv("{0}", DW_AT_name).str()); + EXPECT_EQ("DW_AT_unknown_3fff", formatv("{0}", DW_AT_hi_user).str()); + EXPECT_EQ("DW_FORM_addr", formatv("{0}", DW_FORM_addr).str()); + EXPECT_EQ("DW_FORM_unknown_1f00", formatv("{0}", DW_FORM_lo_user).str()); + EXPECT_EQ("DW_IDX_compile_unit", formatv("{0}", DW_IDX_compile_unit).str()); + EXPECT_EQ("DW_IDX_unknown_3fff", formatv("{0}", DW_IDX_hi_user).str()); + EXPECT_EQ("DW_TAG_compile_unit", formatv("{0}", DW_TAG_compile_unit).str()); + EXPECT_EQ("DW_TAG_unknown_ffff", formatv("{0}", DW_TAG_hi_user).str()); +} } // end namespace diff --git a/unittests/BinaryFormat/MachOTest.cpp b/unittests/BinaryFormat/MachOTest.cpp new file mode 100644 index 000000000000..da8e9e8c6901 --- /dev/null +++ b/unittests/BinaryFormat/MachOTest.cpp @@ -0,0 +1,47 @@ +//===- unittest/BinaryFormat/MachOTest.cpp - MachO support tests ----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/BinaryFormat/MachO.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace llvm::MachO; + +TEST(MachOTest, UnalignedLC) { + unsigned char Valid32BitMachO[] = { + 0xCE, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, + 0x85, 0x80, 0x21, 0x01, 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, + 0x5F, 0x5F, 0x50, 0x41, 0x47, 0x45, 0x5A, 0x45, 0x52, 0x4F, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x4C, 0x49, + 0x4E, 0x4B, 0x45, 0x44, 0x49, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x40, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, + 0x8C, 0x0B, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + mach_header *Header = + reinterpret_cast<mach_header *>(Valid32BitMachO); + if (!sys::IsLittleEndianHost) + swapStruct(*Header); + ASSERT_EQ(Header->magic, MH_MAGIC); + unsigned char *Current = Valid32BitMachO + sizeof(mach_header); + unsigned char *BufferEnd = + Valid32BitMachO + sizeof(mach_header) + Header->sizeofcmds; + while (Current < BufferEnd) { + macho_load_command *LC = + reinterpret_cast<macho_load_command *>(Current); + if (!sys::IsLittleEndianHost) + swapStruct(LC->load_command_data); + ASSERT_EQ(LC->load_command_data.cmd, LC_SEGMENT); + Current += LC->load_command_data.cmdsize; + } +} diff --git a/unittests/BinaryFormat/TestFileMagic.cpp b/unittests/BinaryFormat/TestFileMagic.cpp index ca4ca9a27281..5f132ba4144d 100644 --- a/unittests/BinaryFormat/TestFileMagic.cpp +++ b/unittests/BinaryFormat/TestFileMagic.cpp @@ -81,6 +81,8 @@ const char windows_resource[] = const char macho_dynamically_linked_shared_lib_stub[] = "\xfe\xed\xfa\xce........\x00\x00\x00\x09............"; const char ms_dos_stub_broken[] = "\x4d\x5a\x20\x20"; +const char pdb[] = "Microsoft C/C++ MSF 7.00\r\n\x1a" + "DS\x00\x00\x00"; TEST_F(MagicTest, Magic) { struct type { @@ -110,6 +112,7 @@ TEST_F(MagicTest, Magic) { DEFINE(macho_dsym_companion), DEFINE(macho_kext_bundle), DEFINE(windows_resource), + DEFINE(pdb), {"ms_dos_stub_broken", ms_dos_stub_broken, sizeof(ms_dos_stub_broken), file_magic::unknown}, #undef DEFINE |