1f785676fSDimitry Andric //===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
2f785676fSDimitry Andric //
3f785676fSDimitry Andric //                     The LLVM Compiler Infrastructure
4f785676fSDimitry Andric //
5f785676fSDimitry Andric // This file is distributed under the University of Illinois Open Source
6f785676fSDimitry Andric // License. See LICENSE.TXT for details.
7f785676fSDimitry Andric //
8f785676fSDimitry Andric //===----------------------------------------------------------------------===//
9f785676fSDimitry Andric //
10f785676fSDimitry Andric // This file contains support for DWARF4 hashing of DIEs.
11f785676fSDimitry Andric //
12f785676fSDimitry Andric //===----------------------------------------------------------------------===//
13f785676fSDimitry Andric 
14f785676fSDimitry Andric #include "DIEHash.h"
15db17bf38SDimitry Andric #include "ByteStreamer.h"
1691bc56edSDimitry Andric #include "DwarfDebug.h"
17f785676fSDimitry Andric #include "llvm/ADT/ArrayRef.h"
18f785676fSDimitry Andric #include "llvm/ADT/StringRef.h"
19db17bf38SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
2091bc56edSDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
2139d628a0SDimitry Andric #include "llvm/CodeGen/DIE.h"
22f785676fSDimitry Andric #include "llvm/Support/Debug.h"
23f785676fSDimitry Andric #include "llvm/Support/Endian.h"
24f785676fSDimitry Andric #include "llvm/Support/MD5.h"
25f785676fSDimitry Andric #include "llvm/Support/raw_ostream.h"
26f785676fSDimitry Andric 
27f785676fSDimitry Andric using namespace llvm;
28f785676fSDimitry Andric 
2991bc56edSDimitry Andric #define DEBUG_TYPE "dwarfdebug"
3091bc56edSDimitry Andric 
31*4ba319b5SDimitry Andric /// Grabs the string in whichever attribute is passed in and returns
32f785676fSDimitry Andric /// a reference to it.
getDIEStringAttr(const DIE & Die,uint16_t Attr)33f785676fSDimitry Andric static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
34f785676fSDimitry Andric   // Iterate through all the attributes until we find the one we're
35f785676fSDimitry Andric   // looking for, if we can't find it return an empty string.
3697bc6c73SDimitry Andric   for (const auto &V : Die.values())
3797bc6c73SDimitry Andric     if (V.getAttribute() == Attr)
3897bc6c73SDimitry Andric       return V.getDIEString().getString();
3997bc6c73SDimitry Andric 
40f785676fSDimitry Andric   return StringRef("");
41f785676fSDimitry Andric }
42f785676fSDimitry Andric 
43*4ba319b5SDimitry Andric /// Adds the string in \p Str to the hash. This also hashes
44f785676fSDimitry Andric /// a trailing NULL with the string.
addString(StringRef Str)45f785676fSDimitry Andric void DIEHash::addString(StringRef Str) {
46*4ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
47f785676fSDimitry Andric   Hash.update(Str);
48f785676fSDimitry Andric   Hash.update(makeArrayRef((uint8_t)'\0'));
49f785676fSDimitry Andric }
50f785676fSDimitry Andric 
51f785676fSDimitry Andric // FIXME: The LEB128 routines are copied and only slightly modified out of
52f785676fSDimitry Andric // LEB128.h.
53f785676fSDimitry Andric 
54*4ba319b5SDimitry Andric /// Adds the unsigned in \p Value to the hash encoded as a ULEB128.
addULEB128(uint64_t Value)55f785676fSDimitry Andric void DIEHash::addULEB128(uint64_t Value) {
56*4ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
57f785676fSDimitry Andric   do {
58f785676fSDimitry Andric     uint8_t Byte = Value & 0x7f;
59f785676fSDimitry Andric     Value >>= 7;
60f785676fSDimitry Andric     if (Value != 0)
61f785676fSDimitry Andric       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
62f785676fSDimitry Andric     Hash.update(Byte);
63f785676fSDimitry Andric   } while (Value != 0);
64f785676fSDimitry Andric }
65f785676fSDimitry Andric 
addSLEB128(int64_t Value)66f785676fSDimitry Andric void DIEHash::addSLEB128(int64_t Value) {
67*4ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
68f785676fSDimitry Andric   bool More;
69f785676fSDimitry Andric   do {
70f785676fSDimitry Andric     uint8_t Byte = Value & 0x7f;
71f785676fSDimitry Andric     Value >>= 7;
72f785676fSDimitry Andric     More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
73f785676fSDimitry Andric               ((Value == -1) && ((Byte & 0x40) != 0))));
74f785676fSDimitry Andric     if (More)
75f785676fSDimitry Andric       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
76f785676fSDimitry Andric     Hash.update(Byte);
77f785676fSDimitry Andric   } while (More);
78f785676fSDimitry Andric }
79f785676fSDimitry Andric 
80*4ba319b5SDimitry Andric /// Including \p Parent adds the context of Parent to the hash..
addParentContext(const DIE & Parent)81f785676fSDimitry Andric void DIEHash::addParentContext(const DIE &Parent) {
82f785676fSDimitry Andric 
83*4ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "Adding parent context to hash...\n");
84f785676fSDimitry Andric 
85f785676fSDimitry Andric   // [7.27.2] For each surrounding type or namespace beginning with the
86f785676fSDimitry Andric   // outermost such construct...
87f785676fSDimitry Andric   SmallVector<const DIE *, 1> Parents;
88f785676fSDimitry Andric   const DIE *Cur = &Parent;
8991bc56edSDimitry Andric   while (Cur->getParent()) {
90f785676fSDimitry Andric     Parents.push_back(Cur);
91f785676fSDimitry Andric     Cur = Cur->getParent();
92f785676fSDimitry Andric   }
9391bc56edSDimitry Andric   assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
9491bc56edSDimitry Andric          Cur->getTag() == dwarf::DW_TAG_type_unit);
95f785676fSDimitry Andric 
96f785676fSDimitry Andric   // Reverse iterate over our list to go from the outermost construct to the
97f785676fSDimitry Andric   // innermost.
98f785676fSDimitry Andric   for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
99f785676fSDimitry Andric                                                       E = Parents.rend();
100f785676fSDimitry Andric        I != E; ++I) {
101f785676fSDimitry Andric     const DIE &Die = **I;
102f785676fSDimitry Andric 
103f785676fSDimitry Andric     // ... Append the letter "C" to the sequence...
104f785676fSDimitry Andric     addULEB128('C');
105f785676fSDimitry Andric 
106f785676fSDimitry Andric     // ... Followed by the DWARF tag of the construct...
107f785676fSDimitry Andric     addULEB128(Die.getTag());
108f785676fSDimitry Andric 
109f785676fSDimitry Andric     // ... Then the name, taken from the DW_AT_name attribute.
110f785676fSDimitry Andric     StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
111*4ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << "... adding context: " << Name << "\n");
112f785676fSDimitry Andric     if (!Name.empty())
113f785676fSDimitry Andric       addString(Name);
114f785676fSDimitry Andric   }
115f785676fSDimitry Andric }
116f785676fSDimitry Andric 
117f785676fSDimitry Andric // Collect all of the attributes for a particular DIE in single structure.
collectAttributes(const DIE & Die,DIEAttrs & Attrs)118f785676fSDimitry Andric void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
119f785676fSDimitry Andric 
12097bc6c73SDimitry Andric   for (const auto &V : Die.values()) {
121*4ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << "Attribute: "
12297bc6c73SDimitry Andric                       << dwarf::AttributeString(V.getAttribute())
123f785676fSDimitry Andric                       << " added.\n");
12497bc6c73SDimitry Andric     switch (V.getAttribute()) {
125302affcbSDimitry Andric #define HANDLE_DIE_HASH_ATTR(NAME)                                             \
126302affcbSDimitry Andric   case dwarf::NAME:                                                            \
127302affcbSDimitry Andric     Attrs.NAME = V;                                                            \
128302affcbSDimitry Andric     break;
129302affcbSDimitry Andric #include "DIEHashAttributes.def"
130f785676fSDimitry Andric     default:
131f785676fSDimitry Andric       break;
132f785676fSDimitry Andric     }
133f785676fSDimitry Andric   }
134f785676fSDimitry Andric }
135f785676fSDimitry Andric 
hashShallowTypeReference(dwarf::Attribute Attribute,const DIE & Entry,StringRef Name)136f785676fSDimitry Andric void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
137f785676fSDimitry Andric                                        const DIE &Entry, StringRef Name) {
138f785676fSDimitry Andric   // append the letter 'N'
139f785676fSDimitry Andric   addULEB128('N');
140f785676fSDimitry Andric 
141f785676fSDimitry Andric   // the DWARF attribute code (DW_AT_type or DW_AT_friend),
142f785676fSDimitry Andric   addULEB128(Attribute);
143f785676fSDimitry Andric 
144f785676fSDimitry Andric   // the context of the tag,
145f785676fSDimitry Andric   if (const DIE *Parent = Entry.getParent())
146f785676fSDimitry Andric     addParentContext(*Parent);
147f785676fSDimitry Andric 
148f785676fSDimitry Andric   // the letter 'E',
149f785676fSDimitry Andric   addULEB128('E');
150f785676fSDimitry Andric 
151f785676fSDimitry Andric   // and the name of the type.
152f785676fSDimitry Andric   addString(Name);
153f785676fSDimitry Andric 
154f785676fSDimitry Andric   // Currently DW_TAG_friends are not used by Clang, but if they do become so,
155f785676fSDimitry Andric   // here's the relevant spec text to implement:
156f785676fSDimitry Andric   //
157f785676fSDimitry Andric   // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
158f785676fSDimitry Andric   // the context is omitted and the name to be used is the ABI-specific name
159f785676fSDimitry Andric   // of the subprogram (e.g., the mangled linker name).
160f785676fSDimitry Andric }
161f785676fSDimitry Andric 
hashRepeatedTypeReference(dwarf::Attribute Attribute,unsigned DieNumber)162f785676fSDimitry Andric void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
163f785676fSDimitry Andric                                         unsigned DieNumber) {
164f785676fSDimitry Andric   // a) If T is in the list of [previously hashed types], use the letter
165f785676fSDimitry Andric   // 'R' as the marker
166f785676fSDimitry Andric   addULEB128('R');
167f785676fSDimitry Andric 
168f785676fSDimitry Andric   addULEB128(Attribute);
169f785676fSDimitry Andric 
170f785676fSDimitry Andric   // and use the unsigned LEB128 encoding of [the index of T in the
171f785676fSDimitry Andric   // list] as the attribute value;
172f785676fSDimitry Andric   addULEB128(DieNumber);
173f785676fSDimitry Andric }
174f785676fSDimitry Andric 
hashDIEEntry(dwarf::Attribute Attribute,dwarf::Tag Tag,const DIE & Entry)175f785676fSDimitry Andric void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
176f785676fSDimitry Andric                            const DIE &Entry) {
177f785676fSDimitry Andric   assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
178f785676fSDimitry Andric                                         "tags. Add support here when there's "
179f785676fSDimitry Andric                                         "a use case");
180f785676fSDimitry Andric   // Step 5
181f785676fSDimitry Andric   // If the tag in Step 3 is one of [the below tags]
182f785676fSDimitry Andric   if ((Tag == dwarf::DW_TAG_pointer_type ||
183f785676fSDimitry Andric        Tag == dwarf::DW_TAG_reference_type ||
184f785676fSDimitry Andric        Tag == dwarf::DW_TAG_rvalue_reference_type ||
185f785676fSDimitry Andric        Tag == dwarf::DW_TAG_ptr_to_member_type) &&
186f785676fSDimitry Andric       // and the referenced type (via the [below attributes])
187f785676fSDimitry Andric       // FIXME: This seems overly restrictive, and causes hash mismatches
188f785676fSDimitry Andric       // there's a decl/def difference in the containing type of a
189f785676fSDimitry Andric       // ptr_to_member_type, but it's what DWARF says, for some reason.
190f785676fSDimitry Andric       Attribute == dwarf::DW_AT_type) {
191f785676fSDimitry Andric     // ... has a DW_AT_name attribute,
192f785676fSDimitry Andric     StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
193f785676fSDimitry Andric     if (!Name.empty()) {
194f785676fSDimitry Andric       hashShallowTypeReference(Attribute, Entry, Name);
195f785676fSDimitry Andric       return;
196f785676fSDimitry Andric     }
197f785676fSDimitry Andric   }
198f785676fSDimitry Andric 
199f785676fSDimitry Andric   unsigned &DieNumber = Numbering[&Entry];
200f785676fSDimitry Andric   if (DieNumber) {
201f785676fSDimitry Andric     hashRepeatedTypeReference(Attribute, DieNumber);
202f785676fSDimitry Andric     return;
203f785676fSDimitry Andric   }
204f785676fSDimitry Andric 
20539d628a0SDimitry Andric   // otherwise, b) use the letter 'T' as the marker, ...
206f785676fSDimitry Andric   addULEB128('T');
207f785676fSDimitry Andric 
208f785676fSDimitry Andric   addULEB128(Attribute);
209f785676fSDimitry Andric 
210f785676fSDimitry Andric   // ... process the type T recursively by performing Steps 2 through 7, and
211f785676fSDimitry Andric   // use the result as the attribute value.
212f785676fSDimitry Andric   DieNumber = Numbering.size();
213f785676fSDimitry Andric   computeHash(Entry);
214f785676fSDimitry Andric }
215f785676fSDimitry Andric 
21691bc56edSDimitry Andric // Hash all of the values in a block like set of values. This assumes that
21791bc56edSDimitry Andric // all of the data is going to be added as integers.
hashBlockData(const DIE::const_value_range & Values)2183dac3a9bSDimitry Andric void DIEHash::hashBlockData(const DIE::const_value_range &Values) {
21997bc6c73SDimitry Andric   for (const auto &V : Values)
22097bc6c73SDimitry Andric     Hash.update((uint64_t)V.getDIEInteger().getValue());
22191bc56edSDimitry Andric }
22291bc56edSDimitry Andric 
22391bc56edSDimitry Andric // Hash the contents of a loclistptr class.
hashLocList(const DIELocList & LocList)22491bc56edSDimitry Andric void DIEHash::hashLocList(const DIELocList &LocList) {
22591bc56edSDimitry Andric   HashingByteStreamer Streamer(*this);
22691bc56edSDimitry Andric   DwarfDebug &DD = *AP->getDwarfDebug();
227ff0cc061SDimitry Andric   const DebugLocStream &Locs = DD.getDebugLocs();
228ff0cc061SDimitry Andric   for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
22991bc56edSDimitry Andric     DD.emitDebugLocEntry(Streamer, Entry);
23091bc56edSDimitry Andric }
23191bc56edSDimitry Andric 
232f785676fSDimitry Andric // Hash an individual attribute \param Attr based on the type of attribute and
233f785676fSDimitry Andric // the form.
hashAttribute(const DIEValue & Value,dwarf::Tag Tag)2343ca95b02SDimitry Andric void DIEHash::hashAttribute(const DIEValue &Value, dwarf::Tag Tag) {
23597bc6c73SDimitry Andric   dwarf::Attribute Attribute = Value.getAttribute();
236f785676fSDimitry Andric 
23791bc56edSDimitry Andric   // Other attribute values use the letter 'A' as the marker, and the value
23891bc56edSDimitry Andric   // consists of the form code (encoded as an unsigned LEB128 value) followed by
23991bc56edSDimitry Andric   // the encoding of the value according to the form code. To ensure
24091bc56edSDimitry Andric   // reproducibility of the signature, the set of forms used in the signature
24191bc56edSDimitry Andric   // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
24291bc56edSDimitry Andric   // DW_FORM_string, and DW_FORM_block.
24391bc56edSDimitry Andric 
24497bc6c73SDimitry Andric   switch (Value.getType()) {
24597bc6c73SDimitry Andric   case DIEValue::isNone:
24697bc6c73SDimitry Andric     llvm_unreachable("Expected valid DIEValue");
24797bc6c73SDimitry Andric 
248f785676fSDimitry Andric     // 7.27 Step 3
249f785676fSDimitry Andric     // ... An attribute that refers to another type entry T is processed as
250f785676fSDimitry Andric     // follows:
25191bc56edSDimitry Andric   case DIEValue::isEntry:
25297bc6c73SDimitry Andric     hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
253f785676fSDimitry Andric     break;
25491bc56edSDimitry Andric   case DIEValue::isInteger: {
25591bc56edSDimitry Andric     addULEB128('A');
25691bc56edSDimitry Andric     addULEB128(Attribute);
25797bc6c73SDimitry Andric     switch (Value.getForm()) {
258f785676fSDimitry Andric     case dwarf::DW_FORM_data1:
259f785676fSDimitry Andric     case dwarf::DW_FORM_data2:
260f785676fSDimitry Andric     case dwarf::DW_FORM_data4:
261f785676fSDimitry Andric     case dwarf::DW_FORM_data8:
262f785676fSDimitry Andric     case dwarf::DW_FORM_udata:
26391bc56edSDimitry Andric     case dwarf::DW_FORM_sdata:
264f785676fSDimitry Andric       addULEB128(dwarf::DW_FORM_sdata);
26597bc6c73SDimitry Andric       addSLEB128((int64_t)Value.getDIEInteger().getValue());
266f785676fSDimitry Andric       break;
26791bc56edSDimitry Andric     // DW_FORM_flag_present is just flag with a value of one. We still give it a
26891bc56edSDimitry Andric     // value so just use the value.
26991bc56edSDimitry Andric     case dwarf::DW_FORM_flag_present:
27091bc56edSDimitry Andric     case dwarf::DW_FORM_flag:
27191bc56edSDimitry Andric       addULEB128(dwarf::DW_FORM_flag);
27297bc6c73SDimitry Andric       addULEB128((int64_t)Value.getDIEInteger().getValue());
27391bc56edSDimitry Andric       break;
274f785676fSDimitry Andric     default:
27591bc56edSDimitry Andric       llvm_unreachable("Unknown integer form!");
27691bc56edSDimitry Andric     }
27791bc56edSDimitry Andric     break;
27891bc56edSDimitry Andric   }
27991bc56edSDimitry Andric   case DIEValue::isString:
28091bc56edSDimitry Andric     addULEB128('A');
28191bc56edSDimitry Andric     addULEB128(Attribute);
28291bc56edSDimitry Andric     addULEB128(dwarf::DW_FORM_string);
28397bc6c73SDimitry Andric     addString(Value.getDIEString().getString());
28491bc56edSDimitry Andric     break;
285d88c1a5aSDimitry Andric   case DIEValue::isInlineString:
286d88c1a5aSDimitry Andric     addULEB128('A');
287d88c1a5aSDimitry Andric     addULEB128(Attribute);
288d88c1a5aSDimitry Andric     addULEB128(dwarf::DW_FORM_string);
289d88c1a5aSDimitry Andric     addString(Value.getDIEInlineString().getString());
290d88c1a5aSDimitry Andric     break;
29191bc56edSDimitry Andric   case DIEValue::isBlock:
29291bc56edSDimitry Andric   case DIEValue::isLoc:
29391bc56edSDimitry Andric   case DIEValue::isLocList:
29491bc56edSDimitry Andric     addULEB128('A');
29591bc56edSDimitry Andric     addULEB128(Attribute);
29691bc56edSDimitry Andric     addULEB128(dwarf::DW_FORM_block);
29797bc6c73SDimitry Andric     if (Value.getType() == DIEValue::isBlock) {
29897bc6c73SDimitry Andric       addULEB128(Value.getDIEBlock().ComputeSize(AP));
29997bc6c73SDimitry Andric       hashBlockData(Value.getDIEBlock().values());
30097bc6c73SDimitry Andric     } else if (Value.getType() == DIEValue::isLoc) {
30197bc6c73SDimitry Andric       addULEB128(Value.getDIELoc().ComputeSize(AP));
30297bc6c73SDimitry Andric       hashBlockData(Value.getDIELoc().values());
30391bc56edSDimitry Andric     } else {
30491bc56edSDimitry Andric       // We could add the block length, but that would take
30591bc56edSDimitry Andric       // a bit of work and not add a lot of uniqueness
30691bc56edSDimitry Andric       // to the hash in some way we could test.
30797bc6c73SDimitry Andric       hashLocList(Value.getDIELocList());
30891bc56edSDimitry Andric     }
30991bc56edSDimitry Andric     break;
31091bc56edSDimitry Andric     // FIXME: It's uncertain whether or not we should handle this at the moment.
31191bc56edSDimitry Andric   case DIEValue::isExpr:
31291bc56edSDimitry Andric   case DIEValue::isLabel:
31391bc56edSDimitry Andric   case DIEValue::isDelta:
31491bc56edSDimitry Andric     llvm_unreachable("Add support for additional value types.");
315f785676fSDimitry Andric   }
316f785676fSDimitry Andric }
317f785676fSDimitry Andric 
318f785676fSDimitry Andric // Go through the attributes from \param Attrs in the order specified in 7.27.4
319f785676fSDimitry Andric // and hash them.
hashAttributes(const DIEAttrs & Attrs,dwarf::Tag Tag)320f785676fSDimitry Andric void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
321302affcbSDimitry Andric #define HANDLE_DIE_HASH_ATTR(NAME)                                             \
322f785676fSDimitry Andric   {                                                                            \
323302affcbSDimitry Andric     if (Attrs.NAME)                                                           \
324302affcbSDimitry Andric       hashAttribute(Attrs.NAME, Tag);                                         \
325f785676fSDimitry Andric   }
326302affcbSDimitry Andric #include "DIEHashAttributes.def"
327f785676fSDimitry Andric   // FIXME: Add the extended attributes.
328f785676fSDimitry Andric }
329f785676fSDimitry Andric 
330f785676fSDimitry Andric // Add all of the attributes for \param Die to the hash.
addAttributes(const DIE & Die)331f785676fSDimitry Andric void DIEHash::addAttributes(const DIE &Die) {
332f785676fSDimitry Andric   DIEAttrs Attrs = {};
333f785676fSDimitry Andric   collectAttributes(Die, Attrs);
334f785676fSDimitry Andric   hashAttributes(Attrs, Die.getTag());
335f785676fSDimitry Andric }
336f785676fSDimitry Andric 
hashNestedType(const DIE & Die,StringRef Name)337f785676fSDimitry Andric void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
338f785676fSDimitry Andric   // 7.27 Step 7
339f785676fSDimitry Andric   // ... append the letter 'S',
340f785676fSDimitry Andric   addULEB128('S');
341f785676fSDimitry Andric 
342f785676fSDimitry Andric   // the tag of C,
343f785676fSDimitry Andric   addULEB128(Die.getTag());
344f785676fSDimitry Andric 
345f785676fSDimitry Andric   // and the name.
346f785676fSDimitry Andric   addString(Name);
347f785676fSDimitry Andric }
348f785676fSDimitry Andric 
349f785676fSDimitry Andric // Compute the hash of a DIE. This is based on the type signature computation
350f785676fSDimitry Andric // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
351f785676fSDimitry Andric // flattened description of the DIE.
computeHash(const DIE & Die)352f785676fSDimitry Andric void DIEHash::computeHash(const DIE &Die) {
353f785676fSDimitry Andric   // Append the letter 'D', followed by the DWARF tag of the DIE.
354f785676fSDimitry Andric   addULEB128('D');
355f785676fSDimitry Andric   addULEB128(Die.getTag());
356f785676fSDimitry Andric 
357f785676fSDimitry Andric   // Add each of the attributes of the DIE.
358f785676fSDimitry Andric   addAttributes(Die);
359f785676fSDimitry Andric 
360f785676fSDimitry Andric   // Then hash each of the children of the DIE.
36197bc6c73SDimitry Andric   for (auto &C : Die.children()) {
362f785676fSDimitry Andric     // 7.27 Step 7
363f785676fSDimitry Andric     // If C is a nested type entry or a member function entry, ...
3643dac3a9bSDimitry Andric     if (isType(C.getTag()) || C.getTag() == dwarf::DW_TAG_subprogram) {
3653dac3a9bSDimitry Andric       StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name);
366f785676fSDimitry Andric       // ... and has a DW_AT_name attribute
367f785676fSDimitry Andric       if (!Name.empty()) {
3683dac3a9bSDimitry Andric         hashNestedType(C, Name);
369f785676fSDimitry Andric         continue;
370f785676fSDimitry Andric       }
371f785676fSDimitry Andric     }
3723dac3a9bSDimitry Andric     computeHash(C);
373f785676fSDimitry Andric   }
374f785676fSDimitry Andric 
375f785676fSDimitry Andric   // Following the last (or if there are no children), append a zero byte.
376f785676fSDimitry Andric   Hash.update(makeArrayRef((uint8_t)'\0'));
377f785676fSDimitry Andric }
378f785676fSDimitry Andric 
379f785676fSDimitry Andric /// This is based on the type signature computation given in section 7.27 of the
380f785676fSDimitry Andric /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
381f785676fSDimitry Andric /// with the inclusion of the full CU and all top level CU entities.
382f785676fSDimitry Andric // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
computeCUSignature(StringRef DWOName,const DIE & Die)383302affcbSDimitry Andric uint64_t DIEHash::computeCUSignature(StringRef DWOName, const DIE &Die) {
384f785676fSDimitry Andric   Numbering.clear();
385f785676fSDimitry Andric   Numbering[&Die] = 1;
386f785676fSDimitry Andric 
387302affcbSDimitry Andric   if (!DWOName.empty())
388302affcbSDimitry Andric     Hash.update(DWOName);
389f785676fSDimitry Andric   // Hash the DIE.
390f785676fSDimitry Andric   computeHash(Die);
391f785676fSDimitry Andric 
392f785676fSDimitry Andric   // Now return the result.
393f785676fSDimitry Andric   MD5::MD5Result Result;
394f785676fSDimitry Andric   Hash.final(Result);
395f785676fSDimitry Andric 
396f785676fSDimitry Andric   // ... take the least significant 8 bytes and return those. Our MD5
3977a7e6055SDimitry Andric   // implementation always returns its results in little endian, so we actually
3987a7e6055SDimitry Andric   // need the "high" word.
3997a7e6055SDimitry Andric   return Result.high();
400f785676fSDimitry Andric }
401f785676fSDimitry Andric 
402f785676fSDimitry Andric /// This is based on the type signature computation given in section 7.27 of the
403f785676fSDimitry Andric /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
404f785676fSDimitry Andric /// with the inclusion of additional forms not specifically called out in the
405f785676fSDimitry Andric /// standard.
computeTypeSignature(const DIE & Die)406f785676fSDimitry Andric uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
407f785676fSDimitry Andric   Numbering.clear();
408f785676fSDimitry Andric   Numbering[&Die] = 1;
409f785676fSDimitry Andric 
410f785676fSDimitry Andric   if (const DIE *Parent = Die.getParent())
411f785676fSDimitry Andric     addParentContext(*Parent);
412f785676fSDimitry Andric 
413f785676fSDimitry Andric   // Hash the DIE.
414f785676fSDimitry Andric   computeHash(Die);
415f785676fSDimitry Andric 
416f785676fSDimitry Andric   // Now return the result.
417f785676fSDimitry Andric   MD5::MD5Result Result;
418f785676fSDimitry Andric   Hash.final(Result);
419f785676fSDimitry Andric 
420f785676fSDimitry Andric   // ... take the least significant 8 bytes and return those. Our MD5
4217a7e6055SDimitry Andric   // implementation always returns its results in little endian, so we actually
4227a7e6055SDimitry Andric   // need the "high" word.
4237a7e6055SDimitry Andric   return Result.high();
424f785676fSDimitry Andric }
425