Pavel Labath | a03435e | 2019-11-08 15:24:11 +0100 | [diff] [blame] | 1 | //===- llvm/unittest/DebugInfo/DWARFDieTest.cpp ---------------------------===// |
| 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 | |
| 9 | #include "llvm/BinaryFormat/Dwarf.h" |
| 10 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 11 | #include "llvm/ObjectYAML/DWARFEmitter.h" |
| 12 | #include "llvm/Testing/Support/Error.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | using namespace llvm::dwarf; |
| 17 | using object::SectionedAddress; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | TEST(DWARFDie, getLocations) { |
| 22 | const char *yamldata = R"( |
| 23 | debug_abbrev: |
Xing GUO | 290e399 | 2020-08-21 10:11:33 +0800 | [diff] [blame] | 24 | - Table: |
| 25 | - Code: 0x00000001 |
| 26 | Tag: DW_TAG_compile_unit |
| 27 | Children: DW_CHILDREN_no |
| 28 | Attributes: |
| 29 | - Attribute: DW_AT_location |
| 30 | Form: DW_FORM_sec_offset |
| 31 | - Attribute: DW_AT_data_member_location |
| 32 | Form: DW_FORM_exprloc |
| 33 | - Attribute: DW_AT_vtable_elem_location |
| 34 | Form: DW_FORM_sec_offset |
| 35 | - Attribute: DW_AT_call_data_location |
| 36 | Form: DW_FORM_sec_offset |
Pavel Labath | a03435e | 2019-11-08 15:24:11 +0100 | [diff] [blame] | 37 | debug_info: |
Xing GUO | 8632931 | 2020-07-22 12:14:16 +0800 | [diff] [blame] | 38 | - Version: 5 |
Pavel Labath | a03435e | 2019-11-08 15:24:11 +0100 | [diff] [blame] | 39 | UnitType: DW_UT_compile |
Pavel Labath | a03435e | 2019-11-08 15:24:11 +0100 | [diff] [blame] | 40 | AddrSize: 4 |
| 41 | Entries: |
| 42 | - AbbrCode: 0x00000001 |
| 43 | Values: |
| 44 | - Value: 12 |
| 45 | - Value: 0x0000000000000001 |
| 46 | BlockData: [ 0x47 ] |
| 47 | - Value: 20 |
| 48 | - Value: 25 |
Xing GUO | 5d597e2 | 2020-08-04 16:37:20 +0800 | [diff] [blame] | 49 | debug_loclists: |
| 50 | - AddressSize: 4 |
| 51 | OffsetEntryCount: 0 |
| 52 | Lists: |
| 53 | - Entries: |
| 54 | - Operator: DW_LLE_start_length |
| 55 | Values: [ 0x01, 0x02 ] |
| 56 | - Operator: DW_LLE_end_of_list |
| 57 | - Entries: |
| 58 | - Operator: DW_LLE_startx_length |
| 59 | Values: [ 0x01, 0x02 ] |
| 60 | - Operator: DW_LLE_end_of_list |
| 61 | - Entries: |
| 62 | - Operator: DW_LLE_start_length |
| 63 | Values: [ 0x01, 0x02 ] |
| 64 | ## end_of_list intentionally missing. |
Pavel Labath | a03435e | 2019-11-08 15:24:11 +0100 | [diff] [blame] | 65 | )"; |
| 66 | Expected<StringMap<std::unique_ptr<MemoryBuffer>>> Sections = |
Xing GUO | 92874d2 | 2020-07-23 23:00:19 +0800 | [diff] [blame] | 67 | DWARFYAML::emitDebugSections(StringRef(yamldata), |
Xing GUO | 12605bf | 2020-08-05 00:09:12 +0800 | [diff] [blame] | 68 | /*IsLittleEndian=*/true, |
| 69 | /*Is64BitAddrSize=*/false); |
Pavel Labath | a03435e | 2019-11-08 15:24:11 +0100 | [diff] [blame] | 70 | ASSERT_THAT_EXPECTED(Sections, Succeeded()); |
Pavel Labath | a03435e | 2019-11-08 15:24:11 +0100 | [diff] [blame] | 71 | std::unique_ptr<DWARFContext> Ctx = |
| 72 | DWARFContext::create(*Sections, 4, /*isLittleEndian=*/true); |
| 73 | DWARFCompileUnit *CU = Ctx->getCompileUnitForOffset(0); |
| 74 | ASSERT_NE(nullptr, CU); |
| 75 | DWARFDie Die = CU->getUnitDIE(); |
| 76 | ASSERT_TRUE(Die.isValid()); |
| 77 | |
| 78 | EXPECT_THAT_EXPECTED(Die.getLocations(DW_AT_location), |
| 79 | HasValue(testing::ElementsAre(DWARFLocationExpression{ |
| 80 | DWARFAddressRange{1, 3}, {}}))); |
| 81 | |
| 82 | EXPECT_THAT_EXPECTED( |
| 83 | Die.getLocations(DW_AT_data_member_location), |
| 84 | HasValue(testing::ElementsAre(DWARFLocationExpression{None, {0x47}}))); |
| 85 | |
| 86 | EXPECT_THAT_EXPECTED( |
| 87 | Die.getLocations(DW_AT_vtable_elem_location), |
| 88 | Failed<ErrorInfoBase>(testing::Property( |
| 89 | &ErrorInfoBase::message, |
| 90 | "Unable to resolve indirect address 1 for: DW_LLE_startx_length"))); |
| 91 | |
Pavel Labath | 5754a61 | 2020-02-27 16:22:12 +0100 | [diff] [blame] | 92 | EXPECT_THAT_EXPECTED( |
| 93 | Die.getLocations(DW_AT_call_data_location), |
Pavel Labath | 04aea76 | 2020-04-20 17:28:15 +0200 | [diff] [blame] | 94 | FailedWithMessage( |
| 95 | "unexpected end of data at offset 0x20 while reading [0x20, 0x21)")); |
Pavel Labath | a03435e | 2019-11-08 15:24:11 +0100 | [diff] [blame] | 96 | |
| 97 | EXPECT_THAT_EXPECTED( |
| 98 | Die.getLocations(DW_AT_call_data_value), |
| 99 | Failed<ErrorInfoBase>(testing::Property(&ErrorInfoBase::message, |
| 100 | "No DW_AT_call_data_value"))); |
| 101 | } |
| 102 | |
| 103 | } // end anonymous namespace |