1 //===-- DIERef.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 "DIERef.h"
10 #include "lldb/Utility/DataEncoder.h"
11 #include "lldb/Utility/DataExtractor.h"
12 #include "llvm/Support/Format.h"
13 #include <optional>
14
15 using namespace lldb;
16 using namespace lldb_private;
17
format(const DIERef & ref,raw_ostream & OS,StringRef Style)18 void llvm::format_provider<DIERef>::format(const DIERef &ref, raw_ostream &OS,
19 StringRef Style) {
20 if (ref.dwo_num())
21 OS << format_hex_no_prefix(*ref.dwo_num(), 8) << "/";
22 OS << (ref.section() == DIERef::DebugInfo ? "INFO" : "TYPE");
23 OS << "/" << format_hex_no_prefix(ref.die_offset(), 8);
24 }
25
26 constexpr uint32_t k_dwo_num_mask = 0x3FFFFFFF;
27 constexpr uint32_t k_dwo_num_valid_bitmask = (1u << 30);
28 constexpr uint32_t k_section_bitmask = (1u << 31);
29
Decode(const DataExtractor & data,lldb::offset_t * offset_ptr)30 std::optional<DIERef> DIERef::Decode(const DataExtractor &data,
31 lldb::offset_t *offset_ptr) {
32 const uint32_t bitfield_storage = data.GetU32(offset_ptr);
33 uint32_t dwo_num = bitfield_storage & k_dwo_num_mask;
34 bool dwo_num_valid = (bitfield_storage & (k_dwo_num_valid_bitmask)) != 0;
35 Section section = (Section)((bitfield_storage & (k_section_bitmask)) != 0);
36 // DIE offsets can't be zero and if we fail to decode something from data,
37 // it will return 0
38 dw_offset_t die_offset = data.GetU32(offset_ptr);
39 if (die_offset == 0)
40 return std::nullopt;
41 if (dwo_num_valid)
42 return DIERef(dwo_num, section, die_offset);
43 else
44 return DIERef(std::nullopt, section, die_offset);
45 }
46
Encode(DataEncoder & encoder) const47 void DIERef::Encode(DataEncoder &encoder) const {
48 uint32_t bitfield_storage = m_dwo_num;
49 if (m_dwo_num_valid)
50 bitfield_storage |= k_dwo_num_valid_bitmask;
51 if (m_section)
52 bitfield_storage |= k_section_bitmask;
53 encoder.AppendU32(bitfield_storage);
54 static_assert(sizeof(m_die_offset) == 4, "m_die_offset must be 4 bytes");
55 encoder.AppendU32(m_die_offset);
56 }
57