LLVM  9.0.0svn
DIEHash.cpp
Go to the documentation of this file.
1 //===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
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 // This file contains support for DWARF4 hashing of DIEs.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "DIEHash.h"
14 #include "ByteStreamer.h"
15 #include "DwarfDebug.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringRef.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/MD5.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "dwarfdebug"
29 
30 /// Grabs the string in whichever attribute is passed in and returns
31 /// a reference to it.
32 static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
33  // Iterate through all the attributes until we find the one we're
34  // looking for, if we can't find it return an empty string.
35  for (const auto &V : Die.values())
36  if (V.getAttribute() == Attr)
37  return V.getDIEString().getString();
38 
39  return StringRef("");
40 }
41 
42 /// Adds the string in \p Str to the hash. This also hashes
43 /// a trailing NULL with the string.
44 void DIEHash::addString(StringRef Str) {
45  LLVM_DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
46  Hash.update(Str);
47  Hash.update(makeArrayRef((uint8_t)'\0'));
48 }
49 
50 // FIXME: The LEB128 routines are copied and only slightly modified out of
51 // LEB128.h.
52 
53 /// Adds the unsigned in \p Value to the hash encoded as a ULEB128.
54 void DIEHash::addULEB128(uint64_t Value) {
55  LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
56  do {
57  uint8_t Byte = Value & 0x7f;
58  Value >>= 7;
59  if (Value != 0)
60  Byte |= 0x80; // Mark this byte to show that more bytes will follow.
61  Hash.update(Byte);
62  } while (Value != 0);
63 }
64 
65 void DIEHash::addSLEB128(int64_t Value) {
66  LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
67  bool More;
68  do {
69  uint8_t Byte = Value & 0x7f;
70  Value >>= 7;
71  More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
72  ((Value == -1) && ((Byte & 0x40) != 0))));
73  if (More)
74  Byte |= 0x80; // Mark this byte to show that more bytes will follow.
75  Hash.update(Byte);
76  } while (More);
77 }
78 
79 /// Including \p Parent adds the context of Parent to the hash..
80 void DIEHash::addParentContext(const DIE &Parent) {
81 
82  LLVM_DEBUG(dbgs() << "Adding parent context to hash...\n");
83 
84  // [7.27.2] For each surrounding type or namespace beginning with the
85  // outermost such construct...
87  const DIE *Cur = &Parent;
88  while (Cur->getParent()) {
89  Parents.push_back(Cur);
90  Cur = Cur->getParent();
91  }
92  assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
93  Cur->getTag() == dwarf::DW_TAG_type_unit);
94 
95  // Reverse iterate over our list to go from the outermost construct to the
96  // innermost.
98  E = Parents.rend();
99  I != E; ++I) {
100  const DIE &Die = **I;
101 
102  // ... Append the letter "C" to the sequence...
103  addULEB128('C');
104 
105  // ... Followed by the DWARF tag of the construct...
106  addULEB128(Die.getTag());
107 
108  // ... Then the name, taken from the DW_AT_name attribute.
109  StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
110  LLVM_DEBUG(dbgs() << "... adding context: " << Name << "\n");
111  if (!Name.empty())
112  addString(Name);
113  }
114 }
115 
116 // Collect all of the attributes for a particular DIE in single structure.
117 void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
118 
119  for (const auto &V : Die.values()) {
120  LLVM_DEBUG(dbgs() << "Attribute: "
121  << dwarf::AttributeString(V.getAttribute())
122  << " added.\n");
123  switch (V.getAttribute()) {
124 #define HANDLE_DIE_HASH_ATTR(NAME) \
125  case dwarf::NAME: \
126  Attrs.NAME = V; \
127  break;
128 #include "DIEHashAttributes.def"
129  default:
130  break;
131  }
132  }
133 }
134 
135 void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
136  const DIE &Entry, StringRef Name) {
137  // append the letter 'N'
138  addULEB128('N');
139 
140  // the DWARF attribute code (DW_AT_type or DW_AT_friend),
142 
143  // the context of the tag,
144  if (const DIE *Parent = Entry.getParent())
145  addParentContext(*Parent);
146 
147  // the letter 'E',
148  addULEB128('E');
149 
150  // and the name of the type.
151  addString(Name);
152 
153  // Currently DW_TAG_friends are not used by Clang, but if they do become so,
154  // here's the relevant spec text to implement:
155  //
156  // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
157  // the context is omitted and the name to be used is the ABI-specific name
158  // of the subprogram (e.g., the mangled linker name).
159 }
160 
161 void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
162  unsigned DieNumber) {
163  // a) If T is in the list of [previously hashed types], use the letter
164  // 'R' as the marker
165  addULEB128('R');
166 
168 
169  // and use the unsigned LEB128 encoding of [the index of T in the
170  // list] as the attribute value;
171  addULEB128(DieNumber);
172 }
173 
174 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
175  const DIE &Entry) {
176  assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
177  "tags. Add support here when there's "
178  "a use case");
179  // Step 5
180  // If the tag in Step 3 is one of [the below tags]
181  if ((Tag == dwarf::DW_TAG_pointer_type ||
182  Tag == dwarf::DW_TAG_reference_type ||
183  Tag == dwarf::DW_TAG_rvalue_reference_type ||
184  Tag == dwarf::DW_TAG_ptr_to_member_type) &&
185  // and the referenced type (via the [below attributes])
186  // FIXME: This seems overly restrictive, and causes hash mismatches
187  // there's a decl/def difference in the containing type of a
188  // ptr_to_member_type, but it's what DWARF says, for some reason.
189  Attribute == dwarf::DW_AT_type) {
190  // ... has a DW_AT_name attribute,
191  StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
192  if (!Name.empty()) {
193  hashShallowTypeReference(Attribute, Entry, Name);
194  return;
195  }
196  }
197 
198  unsigned &DieNumber = Numbering[&Entry];
199  if (DieNumber) {
200  hashRepeatedTypeReference(Attribute, DieNumber);
201  return;
202  }
203 
204  // otherwise, b) use the letter 'T' as the marker, ...
205  addULEB128('T');
206 
208 
209  // ... process the type T recursively by performing Steps 2 through 7, and
210  // use the result as the attribute value.
211  DieNumber = Numbering.size();
212  computeHash(Entry);
213 }
214 
215 // Hash all of the values in a block like set of values. This assumes that
216 // all of the data is going to be added as integers.
217 void DIEHash::hashBlockData(const DIE::const_value_range &Values) {
218  for (const auto &V : Values)
219  Hash.update((uint64_t)V.getDIEInteger().getValue());
220 }
221 
222 // Hash the contents of a loclistptr class.
223 void DIEHash::hashLocList(const DIELocList &LocList) {
224  HashingByteStreamer Streamer(*this);
225  DwarfDebug &DD = *AP->getDwarfDebug();
226  const DebugLocStream &Locs = DD.getDebugLocs();
227  for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
228  DD.emitDebugLocEntry(Streamer, Entry);
229 }
230 
231 // Hash an individual attribute \param Attr based on the type of attribute and
232 // the form.
233 void DIEHash::hashAttribute(const DIEValue &Value, dwarf::Tag Tag) {
235 
236  // Other attribute values use the letter 'A' as the marker, and the value
237  // consists of the form code (encoded as an unsigned LEB128 value) followed by
238  // the encoding of the value according to the form code. To ensure
239  // reproducibility of the signature, the set of forms used in the signature
240  // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
241  // DW_FORM_string, and DW_FORM_block.
242 
243  switch (Value.getType()) {
244  case DIEValue::isNone:
245  llvm_unreachable("Expected valid DIEValue");
246 
247  // 7.27 Step 3
248  // ... An attribute that refers to another type entry T is processed as
249  // follows:
250  case DIEValue::isEntry:
251  hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
252  break;
253  case DIEValue::isInteger: {
254  addULEB128('A');
255  addULEB128(Attribute);
256  switch (Value.getForm()) {
257  case dwarf::DW_FORM_data1:
258  case dwarf::DW_FORM_data2:
259  case dwarf::DW_FORM_data4:
260  case dwarf::DW_FORM_data8:
261  case dwarf::DW_FORM_udata:
262  case dwarf::DW_FORM_sdata:
263  addULEB128(dwarf::DW_FORM_sdata);
264  addSLEB128((int64_t)Value.getDIEInteger().getValue());
265  break;
266  // DW_FORM_flag_present is just flag with a value of one. We still give it a
267  // value so just use the value.
268  case dwarf::DW_FORM_flag_present:
269  case dwarf::DW_FORM_flag:
270  addULEB128(dwarf::DW_FORM_flag);
271  addULEB128((int64_t)Value.getDIEInteger().getValue());
272  break;
273  default:
274  llvm_unreachable("Unknown integer form!");
275  }
276  break;
277  }
278  case DIEValue::isString:
279  addULEB128('A');
280  addULEB128(Attribute);
281  addULEB128(dwarf::DW_FORM_string);
282  addString(Value.getDIEString().getString());
283  break;
284  case DIEValue::isInlineString:
285  addULEB128('A');
286  addULEB128(Attribute);
287  addULEB128(dwarf::DW_FORM_string);
288  addString(Value.getDIEInlineString().getString());
289  break;
290  case DIEValue::isBlock:
291  case DIEValue::isLoc:
292  case DIEValue::isLocList:
293  addULEB128('A');
294  addULEB128(Attribute);
295  addULEB128(dwarf::DW_FORM_block);
296  if (Value.getType() == DIEValue::isBlock) {
297  addULEB128(Value.getDIEBlock().ComputeSize(AP));
298  hashBlockData(Value.getDIEBlock().values());
299  } else if (Value.getType() == DIEValue::isLoc) {
300  addULEB128(Value.getDIELoc().ComputeSize(AP));
301  hashBlockData(Value.getDIELoc().values());
302  } else {
303  // We could add the block length, but that would take
304  // a bit of work and not add a lot of uniqueness
305  // to the hash in some way we could test.
306  hashLocList(Value.getDIELocList());
307  }
308  break;
309  // FIXME: It's uncertain whether or not we should handle this at the moment.
310  case DIEValue::isExpr:
311  case DIEValue::isLabel:
312  case DIEValue::isDelta:
313  llvm_unreachable("Add support for additional value types.");
314  }
315 }
316 
317 // Go through the attributes from \param Attrs in the order specified in 7.27.4
318 // and hash them.
319 void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
320 #define HANDLE_DIE_HASH_ATTR(NAME) \
321  { \
322  if (Attrs.NAME) \
323  hashAttribute(Attrs.NAME, Tag); \
324  }
325 #include "DIEHashAttributes.def"
326  // FIXME: Add the extended attributes.
327 }
328 
329 // Add all of the attributes for \param Die to the hash.
330 void DIEHash::addAttributes(const DIE &Die) {
331  DIEAttrs Attrs = {};
332  collectAttributes(Die, Attrs);
333  hashAttributes(Attrs, Die.getTag());
334 }
335 
336 void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
337  // 7.27 Step 7
338  // ... append the letter 'S',
339  addULEB128('S');
340 
341  // the tag of C,
342  addULEB128(Die.getTag());
343 
344  // and the name.
345  addString(Name);
346 }
347 
348 // Compute the hash of a DIE. This is based on the type signature computation
349 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
350 // flattened description of the DIE.
351 void DIEHash::computeHash(const DIE &Die) {
352  // Append the letter 'D', followed by the DWARF tag of the DIE.
353  addULEB128('D');
354  addULEB128(Die.getTag());
355 
356  // Add each of the attributes of the DIE.
357  addAttributes(Die);
358 
359  // Then hash each of the children of the DIE.
360  for (auto &C : Die.children()) {
361  // 7.27 Step 7
362  // If C is a nested type entry or a member function entry, ...
363  if (isType(C.getTag()) || C.getTag() == dwarf::DW_TAG_subprogram) {
364  StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name);
365  // ... and has a DW_AT_name attribute
366  if (!Name.empty()) {
367  hashNestedType(C, Name);
368  continue;
369  }
370  }
371  computeHash(C);
372  }
373 
374  // Following the last (or if there are no children), append a zero byte.
375  Hash.update(makeArrayRef((uint8_t)'\0'));
376 }
377 
378 /// This is based on the type signature computation given in section 7.27 of the
379 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
380 /// with the inclusion of the full CU and all top level CU entities.
381 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
382 uint64_t DIEHash::computeCUSignature(StringRef DWOName, const DIE &Die) {
383  Numbering.clear();
384  Numbering[&Die] = 1;
385 
386  if (!DWOName.empty())
387  Hash.update(DWOName);
388  // Hash the DIE.
389  computeHash(Die);
390 
391  // Now return the result.
392  MD5::MD5Result Result;
393  Hash.final(Result);
394 
395  // ... take the least significant 8 bytes and return those. Our MD5
396  // implementation always returns its results in little endian, so we actually
397  // need the "high" word.
398  return Result.high();
399 }
400 
401 /// This is based on the type signature computation given in section 7.27 of the
402 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
403 /// with the inclusion of additional forms not specifically called out in the
404 /// standard.
405 uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
406  Numbering.clear();
407  Numbering[&Die] = 1;
408 
409  if (const DIE *Parent = Die.getParent())
410  addParentContext(*Parent);
411 
412  // Hash the DIE.
413  computeHash(Die);
414 
415  // Now return the result.
416  MD5::MD5Result Result;
417  Hash.final(Result);
418 
419  // ... take the least significant 8 bytes and return those. Our MD5
420  // implementation always returns its results in little endian, so we actually
421  // need the "high" word.
422  return Result.high();
423 }
uint64_t CallInst * C
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Attribute
Attributes.
Definition: Dwarf.h:114
const DebugLocStream & getDebugLocs() const
Returns the entries for the .debug_loc section.
Definition: DwarfDebug.h:680
void addULEB128(uint64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:54
child_range children()
Definition: DIE.h:709
Collects and handles dwarf debug information.
Definition: DwarfDebug.h:280
Represents a pointer to a location list in the debug_loc section.
Definition: DIE.h:311
size_t getValue() const
Grab the current index out.
Definition: DIE.h:319
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
uint64_t computeCUSignature(StringRef DWOName, const DIE &Die)
Computes the CU signature.
Definition: DIEHash.cpp:382
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
StringRef AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:72
uint64_t computeTypeSignature(const DIE &Die)
Computes the type signature.
Definition: DIEHash.cpp:405
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
value_range values()
Definition: DIE.h:649
A structured debug information entry.
Definition: DIE.h:661
std::reverse_iterator< iterator > reverse_iterator
Definition: SmallVector.h:119
DwarfDebug * getDwarfDebug()
Definition: AsmPrinter.h:190
ArrayRef< Entry > getEntries(const List &L) const
DIE * getParent() const
Definition: DIE.cpp:182
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
const List & getList(size_t LI) const
void emitDebugLocEntry(ByteStreamer &Streamer, const DebugLocStream::Entry &Entry)
Emit an entry for the debug loc section.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
A range adaptor for a pair of iterators.
This file contains constants used for implementing Dwarf debug support.
dwarf::Tag getTag() const
Definition: DIE.h:697
#define I(x, y, z)
Definition: MD5.cpp:58
void addSLEB128(int64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:65
dwarf::Attribute getAttribute() const
Definition: DIE.h:450
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Type getType() const
Accessors.
Definition: DIE.h:449
LLVM Value Representation.
Definition: Value.h:72
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Byte stream of .debug_loc entries.
uint64_t high() const
Definition: MD5.h:72
dwarf::Form getForm() const
Definition: DIE.h:451
#define LLVM_DEBUG(X)
Definition: Debug.h:122
static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr)
Grabs the string in whichever attribute is passed in and returns a reference to it.
Definition: DIEHash.cpp:32
static bool isType(const Metadata *MD)
Definition: Verifier.cpp:857