14573198bSEric Christopher //===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
24573198bSEric Christopher //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64573198bSEric Christopher //
74573198bSEric Christopher //===----------------------------------------------------------------------===//
84573198bSEric Christopher //
94573198bSEric Christopher // This file contains support for DWARF4 hashing of DIEs.
104573198bSEric Christopher //
114573198bSEric Christopher //===----------------------------------------------------------------------===//
124573198bSEric Christopher 
134573198bSEric Christopher #include "DIEHash.h"
146bda14b3SChandler Carruth #include "ByteStreamer.h"
154437df8eSDavid Blaikie #include "DwarfCompileUnit.h"
164f17ee09SEric Christopher #include "DwarfDebug.h"
174573198bSEric Christopher #include "llvm/ADT/ArrayRef.h"
184573198bSEric Christopher #include "llvm/ADT/StringRef.h"
19264b5d9eSZachary Turner #include "llvm/BinaryFormat/Dwarf.h"
20420569beSEric Christopher #include "llvm/CodeGen/AsmPrinter.h"
214573198bSEric Christopher #include "llvm/Support/Debug.h"
224573198bSEric Christopher #include "llvm/Support/raw_ostream.h"
234573198bSEric Christopher 
244573198bSEric Christopher using namespace llvm;
254573198bSEric Christopher 
261b9dde08SChandler Carruth #define DEBUG_TYPE "dwarfdebug"
271b9dde08SChandler Carruth 
285f8f34e4SAdrian Prantl /// Grabs the string in whichever attribute is passed in and returns
294573198bSEric Christopher /// a reference to it.
getDIEStringAttr(const DIE & Die,uint16_t Attr)30afcb9656SDavid Blaikie static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
314573198bSEric Christopher   // Iterate through all the attributes until we find the one we're
324573198bSEric Christopher   // looking for, if we can't find it return an empty string.
3388a8fc54SDuncan P. N. Exon Smith   for (const auto &V : Die.values())
3488a8fc54SDuncan P. N. Exon Smith     if (V.getAttribute() == Attr)
3588a8fc54SDuncan P. N. Exon Smith       return V.getDIEString().getString();
3688a8fc54SDuncan P. N. Exon Smith 
374573198bSEric Christopher   return StringRef("");
384573198bSEric Christopher }
394573198bSEric Christopher 
405f8f34e4SAdrian Prantl /// Adds the string in \p Str to the hash. This also hashes
414573198bSEric Christopher /// a trailing NULL with the string.
addString(StringRef Str)424573198bSEric Christopher void DIEHash::addString(StringRef Str) {
43d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
444573198bSEric Christopher   Hash.update(Str);
454573198bSEric Christopher   Hash.update(makeArrayRef((uint8_t)'\0'));
464573198bSEric Christopher }
474573198bSEric Christopher 
484573198bSEric Christopher // FIXME: The LEB128 routines are copied and only slightly modified out of
494573198bSEric Christopher // LEB128.h.
504573198bSEric Christopher 
515f8f34e4SAdrian Prantl /// Adds the unsigned in \p Value to the hash encoded as a ULEB128.
addULEB128(uint64_t Value)524573198bSEric Christopher void DIEHash::addULEB128(uint64_t Value) {
53d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
544573198bSEric Christopher   do {
554573198bSEric Christopher     uint8_t Byte = Value & 0x7f;
564573198bSEric Christopher     Value >>= 7;
574573198bSEric Christopher     if (Value != 0)
584573198bSEric Christopher       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
594573198bSEric Christopher     Hash.update(Byte);
604573198bSEric Christopher   } while (Value != 0);
614573198bSEric Christopher }
624573198bSEric Christopher 
addSLEB128(int64_t Value)636316ca45SDavid Blaikie void DIEHash::addSLEB128(int64_t Value) {
64d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
656316ca45SDavid Blaikie   bool More;
666316ca45SDavid Blaikie   do {
676316ca45SDavid Blaikie     uint8_t Byte = Value & 0x7f;
686316ca45SDavid Blaikie     Value >>= 7;
696316ca45SDavid Blaikie     More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
706316ca45SDavid Blaikie               ((Value == -1) && ((Byte & 0x40) != 0))));
716316ca45SDavid Blaikie     if (More)
726316ca45SDavid Blaikie       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
736316ca45SDavid Blaikie     Hash.update(Byte);
746316ca45SDavid Blaikie   } while (More);
756316ca45SDavid Blaikie }
766316ca45SDavid Blaikie 
775f8f34e4SAdrian Prantl /// Including \p Parent adds the context of Parent to the hash..
addParentContext(const DIE & Parent)782aee7be8SDavid Blaikie void DIEHash::addParentContext(const DIE &Parent) {
794573198bSEric Christopher 
80d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Adding parent context to hash...\n");
814573198bSEric Christopher 
824573198bSEric Christopher   // [7.27.2] For each surrounding type or namespace beginning with the
834573198bSEric Christopher   // outermost such construct...
842aee7be8SDavid Blaikie   SmallVector<const DIE *, 1> Parents;
852aee7be8SDavid Blaikie   const DIE *Cur = &Parent;
86409dd9c3SDavid Blaikie   while (Cur->getParent()) {
872aee7be8SDavid Blaikie     Parents.push_back(Cur);
882aee7be8SDavid Blaikie     Cur = Cur->getParent();
894573198bSEric Christopher   }
90409dd9c3SDavid Blaikie   assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
91409dd9c3SDavid Blaikie          Cur->getTag() == dwarf::DW_TAG_type_unit);
924573198bSEric Christopher 
934573198bSEric Christopher   // Reverse iterate over our list to go from the outermost construct to the
944573198bSEric Christopher   // innermost.
95843d1edaSKazu Hirata   for (const DIE *Die : llvm::reverse(Parents)) {
964573198bSEric Christopher     // ... Append the letter "C" to the sequence...
974573198bSEric Christopher     addULEB128('C');
984573198bSEric Christopher 
994573198bSEric Christopher     // ... Followed by the DWARF tag of the construct...
100843d1edaSKazu Hirata     addULEB128(Die->getTag());
1014573198bSEric Christopher 
1024573198bSEric Christopher     // ... Then the name, taken from the DW_AT_name attribute.
103843d1edaSKazu Hirata     StringRef Name = getDIEStringAttr(*Die, dwarf::DW_AT_name);
104d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "... adding context: " << Name << "\n");
1054573198bSEric Christopher     if (!Name.empty())
1064573198bSEric Christopher       addString(Name);
1074573198bSEric Christopher   }
1084573198bSEric Christopher }
1094573198bSEric Christopher 
110d29614f9SEric Christopher // Collect all of the attributes for a particular DIE in single structure.
collectAttributes(const DIE & Die,DIEAttrs & Attrs)1112aee7be8SDavid Blaikie void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
112d29614f9SEric Christopher 
11388a8fc54SDuncan P. N. Exon Smith   for (const auto &V : Die.values()) {
114d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Attribute: "
11588a8fc54SDuncan P. N. Exon Smith                       << dwarf::AttributeString(V.getAttribute())
116d29614f9SEric Christopher                       << " added.\n");
11788a8fc54SDuncan P. N. Exon Smith     switch (V.getAttribute()) {
11874fa8039SDavid Blaikie #define HANDLE_DIE_HASH_ATTR(NAME)                                             \
11974fa8039SDavid Blaikie   case dwarf::NAME:                                                            \
12074fa8039SDavid Blaikie     Attrs.NAME = V;                                                            \
12174fa8039SDavid Blaikie     break;
12274fa8039SDavid Blaikie #include "DIEHashAttributes.def"
123d29614f9SEric Christopher     default:
124d29614f9SEric Christopher       break;
125d29614f9SEric Christopher     }
126d29614f9SEric Christopher   }
127d29614f9SEric Christopher }
128d29614f9SEric Christopher 
hashShallowTypeReference(dwarf::Attribute Attribute,const DIE & Entry,StringRef Name)129afcb9656SDavid Blaikie void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
130afcb9656SDavid Blaikie                                        const DIE &Entry, StringRef Name) {
131afcb9656SDavid Blaikie   // append the letter 'N'
132afcb9656SDavid Blaikie   addULEB128('N');
133d29614f9SEric Christopher 
134afcb9656SDavid Blaikie   // the DWARF attribute code (DW_AT_type or DW_AT_friend),
135afcb9656SDavid Blaikie   addULEB128(Attribute);
1366cf58c89SDavid Blaikie 
137afcb9656SDavid Blaikie   // the context of the tag,
1382aee7be8SDavid Blaikie   if (const DIE *Parent = Entry.getParent())
1392aee7be8SDavid Blaikie     addParentContext(*Parent);
140afcb9656SDavid Blaikie 
141afcb9656SDavid Blaikie   // the letter 'E',
142afcb9656SDavid Blaikie   addULEB128('E');
143afcb9656SDavid Blaikie 
144afcb9656SDavid Blaikie   // and the name of the type.
145afcb9656SDavid Blaikie   addString(Name);
146afcb9656SDavid Blaikie 
147afcb9656SDavid Blaikie   // Currently DW_TAG_friends are not used by Clang, but if they do become so,
148afcb9656SDavid Blaikie   // here's the relevant spec text to implement:
149afcb9656SDavid Blaikie   //
150afcb9656SDavid Blaikie   // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
151afcb9656SDavid Blaikie   // the context is omitted and the name to be used is the ABI-specific name
152afcb9656SDavid Blaikie   // of the subprogram (e.g., the mangled linker name).
153afcb9656SDavid Blaikie }
154afcb9656SDavid Blaikie 
hashRepeatedTypeReference(dwarf::Attribute Attribute,unsigned DieNumber)155afcb9656SDavid Blaikie void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
156afcb9656SDavid Blaikie                                         unsigned DieNumber) {
157afcb9656SDavid Blaikie   // a) If T is in the list of [previously hashed types], use the letter
158afcb9656SDavid Blaikie   // 'R' as the marker
159afcb9656SDavid Blaikie   addULEB128('R');
160afcb9656SDavid Blaikie 
161afcb9656SDavid Blaikie   addULEB128(Attribute);
162afcb9656SDavid Blaikie 
163afcb9656SDavid Blaikie   // and use the unsigned LEB128 encoding of [the index of T in the
164afcb9656SDavid Blaikie   // list] as the attribute value;
165afcb9656SDavid Blaikie   addULEB128(DieNumber);
166afcb9656SDavid Blaikie }
167afcb9656SDavid Blaikie 
hashDIEEntry(dwarf::Attribute Attribute,dwarf::Tag Tag,const DIE & Entry)168afcb9656SDavid Blaikie void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
1692aee7be8SDavid Blaikie                            const DIE &Entry) {
1705ebc54d9SDavid Blaikie   assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
1715ebc54d9SDavid Blaikie                                         "tags. Add support here when there's "
1725ebc54d9SDavid Blaikie                                         "a use case");
1736cf58c89SDavid Blaikie   // Step 5
174d70a0553SDavid Blaikie   // If the tag in Step 3 is one of [the below tags]
175d70a0553SDavid Blaikie   if ((Tag == dwarf::DW_TAG_pointer_type ||
176fe3233a5SDavid Blaikie        Tag == dwarf::DW_TAG_reference_type ||
177d70a0553SDavid Blaikie        Tag == dwarf::DW_TAG_rvalue_reference_type ||
178d70a0553SDavid Blaikie        Tag == dwarf::DW_TAG_ptr_to_member_type) &&
179d70a0553SDavid Blaikie       // and the referenced type (via the [below attributes])
180d70a0553SDavid Blaikie       // FIXME: This seems overly restrictive, and causes hash mismatches
181d70a0553SDavid Blaikie       // there's a decl/def difference in the containing type of a
182afcb9656SDavid Blaikie       // ptr_to_member_type, but it's what DWARF says, for some reason.
183d70a0553SDavid Blaikie       Attribute == dwarf::DW_AT_type) {
18432744412SDavid Blaikie     // ... has a DW_AT_name attribute,
18532744412SDavid Blaikie     StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
18632744412SDavid Blaikie     if (!Name.empty()) {
18732744412SDavid Blaikie       hashShallowTypeReference(Attribute, Entry, Name);
1886cf58c89SDavid Blaikie       return;
1896cf58c89SDavid Blaikie     }
19032744412SDavid Blaikie   }
1916cf58c89SDavid Blaikie 
192afcb9656SDavid Blaikie   unsigned &DieNumber = Numbering[&Entry];
193980d4994SDavid Blaikie   if (DieNumber) {
194afcb9656SDavid Blaikie     hashRepeatedTypeReference(Attribute, DieNumber);
195980d4994SDavid Blaikie     return;
196980d4994SDavid Blaikie   }
197980d4994SDavid Blaikie 
198039781efSRobin Morisset   // otherwise, b) use the letter 'T' as the marker, ...
199ca353be6SDavid Blaikie   addULEB128('T');
200ca353be6SDavid Blaikie 
201d70a0553SDavid Blaikie   addULEB128(Attribute);
2026316ca45SDavid Blaikie 
203ca353be6SDavid Blaikie   // ... process the type T recursively by performing Steps 2 through 7, and
204ca353be6SDavid Blaikie   // use the result as the attribute value.
205980d4994SDavid Blaikie   DieNumber = Numbering.size();
2062aee7be8SDavid Blaikie   computeHash(Entry);
207afcb9656SDavid Blaikie }
208afcb9656SDavid Blaikie 
hashRawTypeReference(const DIE & Entry)2092bddab25SDavid Blaikie void DIEHash::hashRawTypeReference(const DIE &Entry) {
2102bddab25SDavid Blaikie   unsigned &DieNumber = Numbering[&Entry];
2112bddab25SDavid Blaikie   if (DieNumber) {
2122bddab25SDavid Blaikie     addULEB128('R');
2132bddab25SDavid Blaikie     addULEB128(DieNumber);
2142bddab25SDavid Blaikie     return;
2152bddab25SDavid Blaikie   }
2162bddab25SDavid Blaikie   DieNumber = Numbering.size();
2172bddab25SDavid Blaikie   addULEB128('T');
2182bddab25SDavid Blaikie   computeHash(Entry);
2192bddab25SDavid Blaikie }
2202bddab25SDavid Blaikie 
221420569beSEric Christopher // Hash all of the values in a block like set of values. This assumes that
222420569beSEric Christopher // all of the data is going to be added as integers.
hashBlockData(const DIE::const_value_range & Values)2234fb1f9cdSDuncan P. N. Exon Smith void DIEHash::hashBlockData(const DIE::const_value_range &Values) {
22488a8fc54SDuncan P. N. Exon Smith   for (const auto &V : Values)
2254437df8eSDavid Blaikie     if (V.getType() == DIEValue::isBaseTypeRef) {
2264437df8eSDavid Blaikie       const DIE &C =
2274437df8eSDavid Blaikie           *CU->ExprRefedBaseTypes[V.getDIEBaseTypeRef().getIndex()].Die;
2284437df8eSDavid Blaikie       StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name);
2294437df8eSDavid Blaikie       assert(!Name.empty() &&
2304437df8eSDavid Blaikie              "Base types referenced from DW_OP_convert should have a name");
2314437df8eSDavid Blaikie       hashNestedType(C, Name);
2324437df8eSDavid Blaikie     } else
23388a8fc54SDuncan P. N. Exon Smith       Hash.update((uint64_t)V.getDIEInteger().getValue());
234420569beSEric Christopher }
235420569beSEric Christopher 
2364f17ee09SEric Christopher // Hash the contents of a loclistptr class.
hashLocList(const DIELocList & LocList)2374f17ee09SEric Christopher void DIEHash::hashLocList(const DIELocList &LocList) {
2384f17ee09SEric Christopher   HashingByteStreamer Streamer(*this);
2390a456de5SDavid Blaikie   DwarfDebug &DD = *AP->getDwarfDebug();
240364a3005SDuncan P. N. Exon Smith   const DebugLocStream &Locs = DD.getDebugLocs();
241031f83fbSDavid Blaikie   const DebugLocStream::List &List = Locs.getList(LocList.getValue());
242031f83fbSDavid Blaikie   for (const DebugLocStream::Entry &Entry : Locs.getEntries(List))
243031f83fbSDavid Blaikie     DD.emitDebugLocEntry(Streamer, Entry, List.CU);
2444f17ee09SEric Christopher }
2454f17ee09SEric Christopher 
246afcb9656SDavid Blaikie // Hash an individual attribute \param Attr based on the type of attribute and
247afcb9656SDavid Blaikie // the form.
hashAttribute(const DIEValue & Value,dwarf::Tag Tag)2481afc1de4SBenjamin Kramer void DIEHash::hashAttribute(const DIEValue &Value, dwarf::Tag Tag) {
249815a6eb5SDuncan P. N. Exon Smith   dwarf::Attribute Attribute = Value.getAttribute();
250afcb9656SDavid Blaikie 
2514b1cf580SEric Christopher   // Other attribute values use the letter 'A' as the marker, and the value
2524b1cf580SEric Christopher   // consists of the form code (encoded as an unsigned LEB128 value) followed by
2534b1cf580SEric Christopher   // the encoding of the value according to the form code. To ensure
2544b1cf580SEric Christopher   // reproducibility of the signature, the set of forms used in the signature
2554b1cf580SEric Christopher   // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
2564b1cf580SEric Christopher   // DW_FORM_string, and DW_FORM_block.
25719308497SEric Christopher 
258e7e1d0c7SDuncan P. N. Exon Smith   switch (Value.getType()) {
259e7e1d0c7SDuncan P. N. Exon Smith   case DIEValue::isNone:
260e7e1d0c7SDuncan P. N. Exon Smith     llvm_unreachable("Expected valid DIEValue");
261e7e1d0c7SDuncan P. N. Exon Smith 
2622bed257aSEric Christopher     // 7.27 Step 3
2632bed257aSEric Christopher     // ... An attribute that refers to another type entry T is processed as
2642bed257aSEric Christopher     // follows:
2652bed257aSEric Christopher   case DIEValue::isEntry:
266e7e1d0c7SDuncan P. N. Exon Smith     hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
2672bed257aSEric Christopher     break;
26819308497SEric Christopher   case DIEValue::isInteger: {
2694b1cf580SEric Christopher     addULEB128('A');
2704b1cf580SEric Christopher     addULEB128(Attribute);
271815a6eb5SDuncan P. N. Exon Smith     switch (Value.getForm()) {
272d033d6fbSEric Christopher     case dwarf::DW_FORM_data1:
273d033d6fbSEric Christopher     case dwarf::DW_FORM_data2:
274d033d6fbSEric Christopher     case dwarf::DW_FORM_data4:
275d033d6fbSEric Christopher     case dwarf::DW_FORM_data8:
276d033d6fbSEric Christopher     case dwarf::DW_FORM_udata:
2778192ba2aSEric Christopher     case dwarf::DW_FORM_sdata:
2786316ca45SDavid Blaikie       addULEB128(dwarf::DW_FORM_sdata);
279e7e1d0c7SDuncan P. N. Exon Smith       addSLEB128((int64_t)Value.getDIEInteger().getValue());
280d033d6fbSEric Christopher       break;
2814b1cf580SEric Christopher     // DW_FORM_flag_present is just flag with a value of one. We still give it a
2824b1cf580SEric Christopher     // value so just use the value.
2834b1cf580SEric Christopher     case dwarf::DW_FORM_flag_present:
2844b1cf580SEric Christopher     case dwarf::DW_FORM_flag:
2854b1cf580SEric Christopher       addULEB128(dwarf::DW_FORM_flag);
286e7e1d0c7SDuncan P. N. Exon Smith       addULEB128((int64_t)Value.getDIEInteger().getValue());
2874b1cf580SEric Christopher       break;
28819308497SEric Christopher     default:
28919308497SEric Christopher       llvm_unreachable("Unknown integer form!");
29019308497SEric Christopher     }
29119308497SEric Christopher     break;
29219308497SEric Christopher   }
29319308497SEric Christopher   case DIEValue::isString:
29419308497SEric Christopher     addULEB128('A');
29519308497SEric Christopher     addULEB128(Attribute);
29619308497SEric Christopher     addULEB128(dwarf::DW_FORM_string);
297e7e1d0c7SDuncan P. N. Exon Smith     addString(Value.getDIEString().getString());
29819308497SEric Christopher     break;
2993462a420SGreg Clayton   case DIEValue::isInlineString:
3003462a420SGreg Clayton     addULEB128('A');
3013462a420SGreg Clayton     addULEB128(Attribute);
3023462a420SGreg Clayton     addULEB128(dwarf::DW_FORM_string);
3033462a420SGreg Clayton     addString(Value.getDIEInlineString().getString());
3043462a420SGreg Clayton     break;
30519308497SEric Christopher   case DIEValue::isBlock:
30619308497SEric Christopher   case DIEValue::isLoc:
3074f17ee09SEric Christopher   case DIEValue::isLocList:
308420569beSEric Christopher     addULEB128('A');
309420569beSEric Christopher     addULEB128(Attribute);
310420569beSEric Christopher     addULEB128(dwarf::DW_FORM_block);
311e7e1d0c7SDuncan P. N. Exon Smith     if (Value.getType() == DIEValue::isBlock) {
31239385d4cSAlexey Lapshin       addULEB128(Value.getDIEBlock().computeSize(AP->getDwarfFormParams()));
31388a8fc54SDuncan P. N. Exon Smith       hashBlockData(Value.getDIEBlock().values());
314e7e1d0c7SDuncan P. N. Exon Smith     } else if (Value.getType() == DIEValue::isLoc) {
31539385d4cSAlexey Lapshin       addULEB128(Value.getDIELoc().computeSize(AP->getDwarfFormParams()));
31688a8fc54SDuncan P. N. Exon Smith       hashBlockData(Value.getDIELoc().values());
3174f17ee09SEric Christopher     } else {
3184f17ee09SEric Christopher       // We could add the block length, but that would take
3194f17ee09SEric Christopher       // a bit of work and not add a lot of uniqueness
3204f17ee09SEric Christopher       // to the hash in some way we could test.
321e7e1d0c7SDuncan P. N. Exon Smith       hashLocList(Value.getDIELocList());
322420569beSEric Christopher     }
323420569beSEric Christopher     break;
3246bb07f60SEric Christopher     // FIXME: It's uncertain whether or not we should handle this at the moment.
32519308497SEric Christopher   case DIEValue::isExpr:
32619308497SEric Christopher   case DIEValue::isLabel:
327b86ce219SMarkus Lavin   case DIEValue::isBaseTypeRef:
32819308497SEric Christopher   case DIEValue::isDelta:
3294318028cSDavid Blaikie   case DIEValue::isAddrOffset:
33019308497SEric Christopher     llvm_unreachable("Add support for additional value types.");
331d29614f9SEric Christopher   }
332d29614f9SEric Christopher }
333d29614f9SEric Christopher 
334d29614f9SEric Christopher // Go through the attributes from \param Attrs in the order specified in 7.27.4
335d29614f9SEric Christopher // and hash them.
hashAttributes(const DIEAttrs & Attrs,dwarf::Tag Tag)3366cf58c89SDavid Blaikie void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
33774fa8039SDavid Blaikie #define HANDLE_DIE_HASH_ATTR(NAME)                                             \
338d29614f9SEric Christopher   {                                                                            \
33974fa8039SDavid Blaikie     if (Attrs.NAME)                                                           \
34074fa8039SDavid Blaikie       hashAttribute(Attrs.NAME, Tag);                                         \
341d29614f9SEric Christopher   }
34274fa8039SDavid Blaikie #include "DIEHashAttributes.def"
343e020fa7cSEric Christopher   // FIXME: Add the extended attributes.
344d29614f9SEric Christopher }
345d29614f9SEric Christopher 
346d29614f9SEric Christopher // Add all of the attributes for \param Die to the hash.
addAttributes(const DIE & Die)3472aee7be8SDavid Blaikie void DIEHash::addAttributes(const DIE &Die) {
34894ded5f3SDavid Blaikie   DIEAttrs Attrs = {};
349d0d6fcc9SDavid Blaikie   collectAttributes(Die, Attrs);
3502aee7be8SDavid Blaikie   hashAttributes(Attrs, Die.getTag());
351d29614f9SEric Christopher }
352d29614f9SEric Christopher 
hashNestedType(const DIE & Die,StringRef Name)35365cc969fSDavid Blaikie void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
35465cc969fSDavid Blaikie   // 7.27 Step 7
35565cc969fSDavid Blaikie   // ... append the letter 'S',
35665cc969fSDavid Blaikie   addULEB128('S');
35765cc969fSDavid Blaikie 
35865cc969fSDavid Blaikie   // the tag of C,
35965cc969fSDavid Blaikie   addULEB128(Die.getTag());
36065cc969fSDavid Blaikie 
36165cc969fSDavid Blaikie   // and the name.
36265cc969fSDavid Blaikie   addString(Name);
36365cc969fSDavid Blaikie }
36465cc969fSDavid Blaikie 
365d29614f9SEric Christopher // Compute the hash of a DIE. This is based on the type signature computation
366d29614f9SEric Christopher // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
367d29614f9SEric Christopher // flattened description of the DIE.
computeHash(const DIE & Die)3682aee7be8SDavid Blaikie void DIEHash::computeHash(const DIE &Die) {
369d29614f9SEric Christopher   // Append the letter 'D', followed by the DWARF tag of the DIE.
370d29614f9SEric Christopher   addULEB128('D');
3712aee7be8SDavid Blaikie   addULEB128(Die.getTag());
372d29614f9SEric Christopher 
373d29614f9SEric Christopher   // Add each of the attributes of the DIE.
374d29614f9SEric Christopher   addAttributes(Die);
375d29614f9SEric Christopher 
376d29614f9SEric Christopher   // Then hash each of the children of the DIE.
377*9e6d1f4bSKazu Hirata   for (const auto &C : Die.children()) {
37865cc969fSDavid Blaikie     // 7.27 Step 7
37965cc969fSDavid Blaikie     // If C is a nested type entry or a member function entry, ...
380b33e5f3cSDavid Blaikie     if (isType(C.getTag()) || (C.getTag() == dwarf::DW_TAG_subprogram && isType(C.getParent()->getTag()))) {
381827200c8SDuncan P. N. Exon Smith       StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name);
38265cc969fSDavid Blaikie       // ... and has a DW_AT_name attribute
38365cc969fSDavid Blaikie       if (!Name.empty()) {
384827200c8SDuncan P. N. Exon Smith         hashNestedType(C, Name);
38565cc969fSDavid Blaikie         continue;
38665cc969fSDavid Blaikie       }
38765cc969fSDavid Blaikie     }
388827200c8SDuncan P. N. Exon Smith     computeHash(C);
38965cc969fSDavid Blaikie   }
39071a0ad66SDavid Blaikie 
39171a0ad66SDavid Blaikie   // Following the last (or if there are no children), append a zero byte.
392920bb2a7SDavid Blaikie   Hash.update(makeArrayRef((uint8_t)'\0'));
393d29614f9SEric Christopher }
394d29614f9SEric Christopher 
3954573198bSEric Christopher /// This is based on the type signature computation given in section 7.27 of the
396d29614f9SEric Christopher /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
397d29614f9SEric Christopher /// with the inclusion of the full CU and all top level CU entities.
39825b7adc8SEric Christopher // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
computeCUSignature(StringRef DWOName,const DIE & Die)39996ab48f9SMehdi Amini uint64_t DIEHash::computeCUSignature(StringRef DWOName, const DIE &Die) {
400980d4994SDavid Blaikie   Numbering.clear();
4012aee7be8SDavid Blaikie   Numbering[&Die] = 1;
402d29614f9SEric Christopher 
40396ab48f9SMehdi Amini   if (!DWOName.empty())
40496ab48f9SMehdi Amini     Hash.update(DWOName);
405d29614f9SEric Christopher   // Hash the DIE.
406d29614f9SEric Christopher   computeHash(Die);
407d29614f9SEric Christopher 
408d29614f9SEric Christopher   // Now return the result.
409d29614f9SEric Christopher   MD5::MD5Result Result;
410d29614f9SEric Christopher   Hash.final(Result);
411d29614f9SEric Christopher 
412d29614f9SEric Christopher   // ... take the least significant 8 bytes and return those. Our MD5
41382a0c97bSZachary Turner   // implementation always returns its results in little endian, so we actually
41482a0c97bSZachary Turner   // need the "high" word.
41582a0c97bSZachary Turner   return Result.high();
416d29614f9SEric Christopher }
41725b7adc8SEric Christopher 
41825b7adc8SEric Christopher /// This is based on the type signature computation given in section 7.27 of the
41925b7adc8SEric Christopher /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
42025b7adc8SEric Christopher /// with the inclusion of additional forms not specifically called out in the
42125b7adc8SEric Christopher /// standard.
computeTypeSignature(const DIE & Die)4222aee7be8SDavid Blaikie uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
423980d4994SDavid Blaikie   Numbering.clear();
4242aee7be8SDavid Blaikie   Numbering[&Die] = 1;
42525b7adc8SEric Christopher 
4262aee7be8SDavid Blaikie   if (const DIE *Parent = Die.getParent())
4272aee7be8SDavid Blaikie     addParentContext(*Parent);
4288a142aaaSDavid Blaikie 
42925b7adc8SEric Christopher   // Hash the DIE.
43025b7adc8SEric Christopher   computeHash(Die);
43125b7adc8SEric Christopher 
43225b7adc8SEric Christopher   // Now return the result.
43325b7adc8SEric Christopher   MD5::MD5Result Result;
43425b7adc8SEric Christopher   Hash.final(Result);
43525b7adc8SEric Christopher 
43625b7adc8SEric Christopher   // ... take the least significant 8 bytes and return those. Our MD5
43782a0c97bSZachary Turner   // implementation always returns its results in little endian, so we actually
43882a0c97bSZachary Turner   // need the "high" word.
43982a0c97bSZachary Turner   return Result.high();
44025b7adc8SEric Christopher }
441