1 //===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for DWARF4 hashing of DIEs.
12 //===----------------------------------------------------------------------===//
14 #include "ByteStreamer.h"
16 #include "DwarfDebug.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Dwarf.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/MD5.h"
25 #include "llvm/Support/raw_ostream.h"
29 #define DEBUG_TYPE "dwarfdebug"
31 /// \brief Grabs the string in whichever attribute is passed in and returns
32 /// a reference to it.
33 static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
34 const auto &Values = Die.getValues();
36 // Iterate through all the attributes until we find the one we're
37 // looking for, if we can't find it return an empty string.
38 for (size_t i = 0; i < Values.size(); ++i) {
39 if (Values[i].getAttribute() == Attr)
40 return Values[i].getDIEString().getString();
45 /// \brief Adds the string in \p Str to the hash. This also hashes
46 /// a trailing NULL with the string.
47 void DIEHash::addString(StringRef Str) {
48 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
50 Hash.update(makeArrayRef((uint8_t)'\0'));
53 // FIXME: The LEB128 routines are copied and only slightly modified out of
56 /// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
57 void DIEHash::addULEB128(uint64_t Value) {
58 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
60 uint8_t Byte = Value & 0x7f;
63 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
68 void DIEHash::addSLEB128(int64_t Value) {
69 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
72 uint8_t Byte = Value & 0x7f;
74 More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
75 ((Value == -1) && ((Byte & 0x40) != 0))));
77 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
82 /// \brief Including \p Parent adds the context of Parent to the hash..
83 void DIEHash::addParentContext(const DIE &Parent) {
85 DEBUG(dbgs() << "Adding parent context to hash...\n");
87 // [7.27.2] For each surrounding type or namespace beginning with the
88 // outermost such construct...
89 SmallVector<const DIE *, 1> Parents;
90 const DIE *Cur = &Parent;
91 while (Cur->getParent()) {
92 Parents.push_back(Cur);
93 Cur = Cur->getParent();
95 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
96 Cur->getTag() == dwarf::DW_TAG_type_unit);
98 // Reverse iterate over our list to go from the outermost construct to the
100 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
103 const DIE &Die = **I;
105 // ... Append the letter "C" to the sequence...
108 // ... Followed by the DWARF tag of the construct...
109 addULEB128(Die.getTag());
111 // ... Then the name, taken from the DW_AT_name attribute.
112 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
113 DEBUG(dbgs() << "... adding context: " << Name << "\n");
119 // Collect all of the attributes for a particular DIE in single structure.
120 void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
121 const SmallVectorImpl<DIEValue> &Values = Die.getValues();
123 #define COLLECT_ATTR(NAME) \
125 Attrs.NAME.Val = Values[i]; \
128 for (size_t i = 0, e = Values.size(); i != e; ++i) {
129 DEBUG(dbgs() << "Attribute: "
130 << dwarf::AttributeString(Values[i].getAttribute())
132 switch (Values[i].getAttribute()) {
133 COLLECT_ATTR(DW_AT_name);
134 COLLECT_ATTR(DW_AT_accessibility);
135 COLLECT_ATTR(DW_AT_address_class);
136 COLLECT_ATTR(DW_AT_allocated);
137 COLLECT_ATTR(DW_AT_artificial);
138 COLLECT_ATTR(DW_AT_associated);
139 COLLECT_ATTR(DW_AT_binary_scale);
140 COLLECT_ATTR(DW_AT_bit_offset);
141 COLLECT_ATTR(DW_AT_bit_size);
142 COLLECT_ATTR(DW_AT_bit_stride);
143 COLLECT_ATTR(DW_AT_byte_size);
144 COLLECT_ATTR(DW_AT_byte_stride);
145 COLLECT_ATTR(DW_AT_const_expr);
146 COLLECT_ATTR(DW_AT_const_value);
147 COLLECT_ATTR(DW_AT_containing_type);
148 COLLECT_ATTR(DW_AT_count);
149 COLLECT_ATTR(DW_AT_data_bit_offset);
150 COLLECT_ATTR(DW_AT_data_location);
151 COLLECT_ATTR(DW_AT_data_member_location);
152 COLLECT_ATTR(DW_AT_decimal_scale);
153 COLLECT_ATTR(DW_AT_decimal_sign);
154 COLLECT_ATTR(DW_AT_default_value);
155 COLLECT_ATTR(DW_AT_digit_count);
156 COLLECT_ATTR(DW_AT_discr);
157 COLLECT_ATTR(DW_AT_discr_list);
158 COLLECT_ATTR(DW_AT_discr_value);
159 COLLECT_ATTR(DW_AT_encoding);
160 COLLECT_ATTR(DW_AT_enum_class);
161 COLLECT_ATTR(DW_AT_endianity);
162 COLLECT_ATTR(DW_AT_explicit);
163 COLLECT_ATTR(DW_AT_is_optional);
164 COLLECT_ATTR(DW_AT_location);
165 COLLECT_ATTR(DW_AT_lower_bound);
166 COLLECT_ATTR(DW_AT_mutable);
167 COLLECT_ATTR(DW_AT_ordering);
168 COLLECT_ATTR(DW_AT_picture_string);
169 COLLECT_ATTR(DW_AT_prototyped);
170 COLLECT_ATTR(DW_AT_small);
171 COLLECT_ATTR(DW_AT_segment);
172 COLLECT_ATTR(DW_AT_string_length);
173 COLLECT_ATTR(DW_AT_threads_scaled);
174 COLLECT_ATTR(DW_AT_upper_bound);
175 COLLECT_ATTR(DW_AT_use_location);
176 COLLECT_ATTR(DW_AT_use_UTF8);
177 COLLECT_ATTR(DW_AT_variable_parameter);
178 COLLECT_ATTR(DW_AT_virtuality);
179 COLLECT_ATTR(DW_AT_visibility);
180 COLLECT_ATTR(DW_AT_vtable_elem_location);
181 COLLECT_ATTR(DW_AT_type);
188 void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
189 const DIE &Entry, StringRef Name) {
190 // append the letter 'N'
193 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
194 addULEB128(Attribute);
196 // the context of the tag,
197 if (const DIE *Parent = Entry.getParent())
198 addParentContext(*Parent);
203 // and the name of the type.
206 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
207 // here's the relevant spec text to implement:
209 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
210 // the context is omitted and the name to be used is the ABI-specific name
211 // of the subprogram (e.g., the mangled linker name).
214 void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
215 unsigned DieNumber) {
216 // a) If T is in the list of [previously hashed types], use the letter
220 addULEB128(Attribute);
222 // and use the unsigned LEB128 encoding of [the index of T in the
223 // list] as the attribute value;
224 addULEB128(DieNumber);
227 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
229 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
230 "tags. Add support here when there's "
233 // If the tag in Step 3 is one of [the below tags]
234 if ((Tag == dwarf::DW_TAG_pointer_type ||
235 Tag == dwarf::DW_TAG_reference_type ||
236 Tag == dwarf::DW_TAG_rvalue_reference_type ||
237 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
238 // and the referenced type (via the [below attributes])
239 // FIXME: This seems overly restrictive, and causes hash mismatches
240 // there's a decl/def difference in the containing type of a
241 // ptr_to_member_type, but it's what DWARF says, for some reason.
242 Attribute == dwarf::DW_AT_type) {
243 // ... has a DW_AT_name attribute,
244 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
246 hashShallowTypeReference(Attribute, Entry, Name);
251 unsigned &DieNumber = Numbering[&Entry];
253 hashRepeatedTypeReference(Attribute, DieNumber);
257 // otherwise, b) use the letter 'T' as the marker, ...
260 addULEB128(Attribute);
262 // ... process the type T recursively by performing Steps 2 through 7, and
263 // use the result as the attribute value.
264 DieNumber = Numbering.size();
268 // Hash all of the values in a block like set of values. This assumes that
269 // all of the data is going to be added as integers.
270 void DIEHash::hashBlockData(const SmallVectorImpl<DIEValue> &Values) {
271 for (auto I = Values.begin(), E = Values.end(); I != E; ++I)
272 Hash.update((uint64_t)I->getDIEInteger().getValue());
275 // Hash the contents of a loclistptr class.
276 void DIEHash::hashLocList(const DIELocList &LocList) {
277 HashingByteStreamer Streamer(*this);
278 DwarfDebug &DD = *AP->getDwarfDebug();
279 const DebugLocStream &Locs = DD.getDebugLocs();
280 for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
281 DD.emitDebugLocEntry(Streamer, Entry);
284 // Hash an individual attribute \param Attr based on the type of attribute and
286 void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
287 const DIEValue &Value = Attr.Val;
288 dwarf::Attribute Attribute = Value.getAttribute();
290 // Other attribute values use the letter 'A' as the marker, and the value
291 // consists of the form code (encoded as an unsigned LEB128 value) followed by
292 // the encoding of the value according to the form code. To ensure
293 // reproducibility of the signature, the set of forms used in the signature
294 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
295 // DW_FORM_string, and DW_FORM_block.
297 switch (Value.getType()) {
298 case DIEValue::isNone:
299 llvm_unreachable("Expected valid DIEValue");
302 // ... An attribute that refers to another type entry T is processed as
304 case DIEValue::isEntry:
305 hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
307 case DIEValue::isInteger: {
309 addULEB128(Attribute);
310 switch (Value.getForm()) {
311 case dwarf::DW_FORM_data1:
312 case dwarf::DW_FORM_data2:
313 case dwarf::DW_FORM_data4:
314 case dwarf::DW_FORM_data8:
315 case dwarf::DW_FORM_udata:
316 case dwarf::DW_FORM_sdata:
317 addULEB128(dwarf::DW_FORM_sdata);
318 addSLEB128((int64_t)Value.getDIEInteger().getValue());
320 // DW_FORM_flag_present is just flag with a value of one. We still give it a
321 // value so just use the value.
322 case dwarf::DW_FORM_flag_present:
323 case dwarf::DW_FORM_flag:
324 addULEB128(dwarf::DW_FORM_flag);
325 addULEB128((int64_t)Value.getDIEInteger().getValue());
328 llvm_unreachable("Unknown integer form!");
332 case DIEValue::isString:
334 addULEB128(Attribute);
335 addULEB128(dwarf::DW_FORM_string);
336 addString(Value.getDIEString().getString());
338 case DIEValue::isBlock:
339 case DIEValue::isLoc:
340 case DIEValue::isLocList:
342 addULEB128(Attribute);
343 addULEB128(dwarf::DW_FORM_block);
344 if (Value.getType() == DIEValue::isBlock) {
345 addULEB128(Value.getDIEBlock().ComputeSize(AP));
346 hashBlockData(Value.getDIEBlock().getValues());
347 } else if (Value.getType() == DIEValue::isLoc) {
348 addULEB128(Value.getDIELoc().ComputeSize(AP));
349 hashBlockData(Value.getDIELoc().getValues());
351 // We could add the block length, but that would take
352 // a bit of work and not add a lot of uniqueness
353 // to the hash in some way we could test.
354 hashLocList(Value.getDIELocList());
357 // FIXME: It's uncertain whether or not we should handle this at the moment.
358 case DIEValue::isExpr:
359 case DIEValue::isLabel:
360 case DIEValue::isDelta:
361 case DIEValue::isTypeSignature:
362 llvm_unreachable("Add support for additional value types.");
366 // Go through the attributes from \param Attrs in the order specified in 7.27.4
368 void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
369 #define ADD_ATTR(ATTR) \
372 hashAttribute(ATTR, Tag); \
375 ADD_ATTR(Attrs.DW_AT_name);
376 ADD_ATTR(Attrs.DW_AT_accessibility);
377 ADD_ATTR(Attrs.DW_AT_address_class);
378 ADD_ATTR(Attrs.DW_AT_allocated);
379 ADD_ATTR(Attrs.DW_AT_artificial);
380 ADD_ATTR(Attrs.DW_AT_associated);
381 ADD_ATTR(Attrs.DW_AT_binary_scale);
382 ADD_ATTR(Attrs.DW_AT_bit_offset);
383 ADD_ATTR(Attrs.DW_AT_bit_size);
384 ADD_ATTR(Attrs.DW_AT_bit_stride);
385 ADD_ATTR(Attrs.DW_AT_byte_size);
386 ADD_ATTR(Attrs.DW_AT_byte_stride);
387 ADD_ATTR(Attrs.DW_AT_const_expr);
388 ADD_ATTR(Attrs.DW_AT_const_value);
389 ADD_ATTR(Attrs.DW_AT_containing_type);
390 ADD_ATTR(Attrs.DW_AT_count);
391 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
392 ADD_ATTR(Attrs.DW_AT_data_location);
393 ADD_ATTR(Attrs.DW_AT_data_member_location);
394 ADD_ATTR(Attrs.DW_AT_decimal_scale);
395 ADD_ATTR(Attrs.DW_AT_decimal_sign);
396 ADD_ATTR(Attrs.DW_AT_default_value);
397 ADD_ATTR(Attrs.DW_AT_digit_count);
398 ADD_ATTR(Attrs.DW_AT_discr);
399 ADD_ATTR(Attrs.DW_AT_discr_list);
400 ADD_ATTR(Attrs.DW_AT_discr_value);
401 ADD_ATTR(Attrs.DW_AT_encoding);
402 ADD_ATTR(Attrs.DW_AT_enum_class);
403 ADD_ATTR(Attrs.DW_AT_endianity);
404 ADD_ATTR(Attrs.DW_AT_explicit);
405 ADD_ATTR(Attrs.DW_AT_is_optional);
406 ADD_ATTR(Attrs.DW_AT_location);
407 ADD_ATTR(Attrs.DW_AT_lower_bound);
408 ADD_ATTR(Attrs.DW_AT_mutable);
409 ADD_ATTR(Attrs.DW_AT_ordering);
410 ADD_ATTR(Attrs.DW_AT_picture_string);
411 ADD_ATTR(Attrs.DW_AT_prototyped);
412 ADD_ATTR(Attrs.DW_AT_small);
413 ADD_ATTR(Attrs.DW_AT_segment);
414 ADD_ATTR(Attrs.DW_AT_string_length);
415 ADD_ATTR(Attrs.DW_AT_threads_scaled);
416 ADD_ATTR(Attrs.DW_AT_upper_bound);
417 ADD_ATTR(Attrs.DW_AT_use_location);
418 ADD_ATTR(Attrs.DW_AT_use_UTF8);
419 ADD_ATTR(Attrs.DW_AT_variable_parameter);
420 ADD_ATTR(Attrs.DW_AT_virtuality);
421 ADD_ATTR(Attrs.DW_AT_visibility);
422 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
423 ADD_ATTR(Attrs.DW_AT_type);
425 // FIXME: Add the extended attributes.
428 // Add all of the attributes for \param Die to the hash.
429 void DIEHash::addAttributes(const DIE &Die) {
431 collectAttributes(Die, Attrs);
432 hashAttributes(Attrs, Die.getTag());
435 void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
437 // ... append the letter 'S',
441 addULEB128(Die.getTag());
447 // Compute the hash of a DIE. This is based on the type signature computation
448 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
449 // flattened description of the DIE.
450 void DIEHash::computeHash(const DIE &Die) {
451 // Append the letter 'D', followed by the DWARF tag of the DIE.
453 addULEB128(Die.getTag());
455 // Add each of the attributes of the DIE.
458 // Then hash each of the children of the DIE.
459 for (auto &C : Die.getChildren()) {
461 // If C is a nested type entry or a member function entry, ...
462 if (isType(C->getTag()) || C->getTag() == dwarf::DW_TAG_subprogram) {
463 StringRef Name = getDIEStringAttr(*C, dwarf::DW_AT_name);
464 // ... and has a DW_AT_name attribute
466 hashNestedType(*C, Name);
473 // Following the last (or if there are no children), append a zero byte.
474 Hash.update(makeArrayRef((uint8_t)'\0'));
477 /// This is based on the type signature computation given in section 7.27 of the
478 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
479 /// with the exception that we are hashing only the context and the name of the
481 uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
483 // Add the contexts to the hash. We won't be computing the ODR hash for
484 // function local types so it's safe to use the generic context hashing
486 // FIXME: If we figure out how to account for linkage in some way we could
487 // actually do this with a slight modification to the parent hash algorithm.
488 if (const DIE *Parent = Die.getParent())
489 addParentContext(*Parent);
491 // Add the current DIE information.
493 // Add the DWARF tag of the DIE.
494 addULEB128(Die.getTag());
496 // Add the name of the type to the hash.
497 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
499 // Now get the result.
500 MD5::MD5Result Result;
503 // ... take the least significant 8 bytes and return those. Our MD5
504 // implementation always returns its results in little endian, swap bytes
506 return support::endian::read64le(Result + 8);
509 /// This is based on the type signature computation given in section 7.27 of the
510 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
511 /// with the inclusion of the full CU and all top level CU entities.
512 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
513 uint64_t DIEHash::computeCUSignature(const DIE &Die) {
520 // Now return the result.
521 MD5::MD5Result Result;
524 // ... take the least significant 8 bytes and return those. Our MD5
525 // implementation always returns its results in little endian, swap bytes
527 return support::endian::read64le(Result + 8);
530 /// This is based on the type signature computation given in section 7.27 of the
531 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
532 /// with the inclusion of additional forms not specifically called out in the
534 uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
538 if (const DIE *Parent = Die.getParent())
539 addParentContext(*Parent);
544 // Now return the result.
545 MD5::MD5Result Result;
548 // ... take the least significant 8 bytes and return those. Our MD5
549 // implementation always returns its results in little endian, swap bytes
551 return support::endian::read64le(Result + 8);