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();
35 const DIEAbbrev &Abbrevs = Die.getAbbrev();
37 // Iterate through all the attributes until we find the one we're
38 // looking for, if we can't find it return an empty string.
39 for (size_t i = 0; i < Values.size(); ++i) {
40 if (Abbrevs.getData()[i].getAttribute() == Attr)
41 return Values[i].getDIEString().getString();
46 /// \brief Adds the string in \p Str to the hash. This also hashes
47 /// a trailing NULL with the string.
48 void DIEHash::addString(StringRef Str) {
49 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
51 Hash.update(makeArrayRef((uint8_t)'\0'));
54 // FIXME: The LEB128 routines are copied and only slightly modified out of
57 /// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
58 void DIEHash::addULEB128(uint64_t Value) {
59 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
61 uint8_t Byte = Value & 0x7f;
64 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
69 void DIEHash::addSLEB128(int64_t Value) {
70 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
73 uint8_t Byte = Value & 0x7f;
75 More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
76 ((Value == -1) && ((Byte & 0x40) != 0))));
78 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
83 /// \brief Including \p Parent adds the context of Parent to the hash..
84 void DIEHash::addParentContext(const DIE &Parent) {
86 DEBUG(dbgs() << "Adding parent context to hash...\n");
88 // [7.27.2] For each surrounding type or namespace beginning with the
89 // outermost such construct...
90 SmallVector<const DIE *, 1> Parents;
91 const DIE *Cur = &Parent;
92 while (Cur->getParent()) {
93 Parents.push_back(Cur);
94 Cur = Cur->getParent();
96 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
97 Cur->getTag() == dwarf::DW_TAG_type_unit);
99 // Reverse iterate over our list to go from the outermost construct to the
101 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
104 const DIE &Die = **I;
106 // ... Append the letter "C" to the sequence...
109 // ... Followed by the DWARF tag of the construct...
110 addULEB128(Die.getTag());
112 // ... Then the name, taken from the DW_AT_name attribute.
113 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
114 DEBUG(dbgs() << "... adding context: " << Name << "\n");
120 // Collect all of the attributes for a particular DIE in single structure.
121 void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
122 const SmallVectorImpl<DIEValue> &Values = Die.getValues();
123 const DIEAbbrev &Abbrevs = Die.getAbbrev();
125 #define COLLECT_ATTR(NAME) \
127 Attrs.NAME.Val = Values[i]; \
128 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
131 for (size_t i = 0, e = Values.size(); i != e; ++i) {
132 DEBUG(dbgs() << "Attribute: "
133 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
135 switch (Abbrevs.getData()[i].getAttribute()) {
136 COLLECT_ATTR(DW_AT_name);
137 COLLECT_ATTR(DW_AT_accessibility);
138 COLLECT_ATTR(DW_AT_address_class);
139 COLLECT_ATTR(DW_AT_allocated);
140 COLLECT_ATTR(DW_AT_artificial);
141 COLLECT_ATTR(DW_AT_associated);
142 COLLECT_ATTR(DW_AT_binary_scale);
143 COLLECT_ATTR(DW_AT_bit_offset);
144 COLLECT_ATTR(DW_AT_bit_size);
145 COLLECT_ATTR(DW_AT_bit_stride);
146 COLLECT_ATTR(DW_AT_byte_size);
147 COLLECT_ATTR(DW_AT_byte_stride);
148 COLLECT_ATTR(DW_AT_const_expr);
149 COLLECT_ATTR(DW_AT_const_value);
150 COLLECT_ATTR(DW_AT_containing_type);
151 COLLECT_ATTR(DW_AT_count);
152 COLLECT_ATTR(DW_AT_data_bit_offset);
153 COLLECT_ATTR(DW_AT_data_location);
154 COLLECT_ATTR(DW_AT_data_member_location);
155 COLLECT_ATTR(DW_AT_decimal_scale);
156 COLLECT_ATTR(DW_AT_decimal_sign);
157 COLLECT_ATTR(DW_AT_default_value);
158 COLLECT_ATTR(DW_AT_digit_count);
159 COLLECT_ATTR(DW_AT_discr);
160 COLLECT_ATTR(DW_AT_discr_list);
161 COLLECT_ATTR(DW_AT_discr_value);
162 COLLECT_ATTR(DW_AT_encoding);
163 COLLECT_ATTR(DW_AT_enum_class);
164 COLLECT_ATTR(DW_AT_endianity);
165 COLLECT_ATTR(DW_AT_explicit);
166 COLLECT_ATTR(DW_AT_is_optional);
167 COLLECT_ATTR(DW_AT_location);
168 COLLECT_ATTR(DW_AT_lower_bound);
169 COLLECT_ATTR(DW_AT_mutable);
170 COLLECT_ATTR(DW_AT_ordering);
171 COLLECT_ATTR(DW_AT_picture_string);
172 COLLECT_ATTR(DW_AT_prototyped);
173 COLLECT_ATTR(DW_AT_small);
174 COLLECT_ATTR(DW_AT_segment);
175 COLLECT_ATTR(DW_AT_string_length);
176 COLLECT_ATTR(DW_AT_threads_scaled);
177 COLLECT_ATTR(DW_AT_upper_bound);
178 COLLECT_ATTR(DW_AT_use_location);
179 COLLECT_ATTR(DW_AT_use_UTF8);
180 COLLECT_ATTR(DW_AT_variable_parameter);
181 COLLECT_ATTR(DW_AT_virtuality);
182 COLLECT_ATTR(DW_AT_visibility);
183 COLLECT_ATTR(DW_AT_vtable_elem_location);
184 COLLECT_ATTR(DW_AT_type);
191 void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
192 const DIE &Entry, StringRef Name) {
193 // append the letter 'N'
196 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
197 addULEB128(Attribute);
199 // the context of the tag,
200 if (const DIE *Parent = Entry.getParent())
201 addParentContext(*Parent);
206 // and the name of the type.
209 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
210 // here's the relevant spec text to implement:
212 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
213 // the context is omitted and the name to be used is the ABI-specific name
214 // of the subprogram (e.g., the mangled linker name).
217 void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
218 unsigned DieNumber) {
219 // a) If T is in the list of [previously hashed types], use the letter
223 addULEB128(Attribute);
225 // and use the unsigned LEB128 encoding of [the index of T in the
226 // list] as the attribute value;
227 addULEB128(DieNumber);
230 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
232 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
233 "tags. Add support here when there's "
236 // If the tag in Step 3 is one of [the below tags]
237 if ((Tag == dwarf::DW_TAG_pointer_type ||
238 Tag == dwarf::DW_TAG_reference_type ||
239 Tag == dwarf::DW_TAG_rvalue_reference_type ||
240 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
241 // and the referenced type (via the [below attributes])
242 // FIXME: This seems overly restrictive, and causes hash mismatches
243 // there's a decl/def difference in the containing type of a
244 // ptr_to_member_type, but it's what DWARF says, for some reason.
245 Attribute == dwarf::DW_AT_type) {
246 // ... has a DW_AT_name attribute,
247 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
249 hashShallowTypeReference(Attribute, Entry, Name);
254 unsigned &DieNumber = Numbering[&Entry];
256 hashRepeatedTypeReference(Attribute, DieNumber);
260 // otherwise, b) use the letter 'T' as the marker, ...
263 addULEB128(Attribute);
265 // ... process the type T recursively by performing Steps 2 through 7, and
266 // use the result as the attribute value.
267 DieNumber = Numbering.size();
271 // Hash all of the values in a block like set of values. This assumes that
272 // all of the data is going to be added as integers.
273 void DIEHash::hashBlockData(const SmallVectorImpl<DIEValue> &Values) {
274 for (auto I = Values.begin(), E = Values.end(); I != E; ++I)
275 Hash.update((uint64_t)I->getDIEInteger().getValue());
278 // Hash the contents of a loclistptr class.
279 void DIEHash::hashLocList(const DIELocList &LocList) {
280 HashingByteStreamer Streamer(*this);
281 DwarfDebug &DD = *AP->getDwarfDebug();
282 const DebugLocStream &Locs = DD.getDebugLocs();
283 for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
284 DD.emitDebugLocEntry(Streamer, Entry);
287 // Hash an individual attribute \param Attr based on the type of attribute and
289 void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
290 const DIEValue &Value = Attr.Val;
291 const DIEAbbrevData *Desc = Attr.Desc;
292 dwarf::Attribute Attribute = Desc->getAttribute();
294 // Other attribute values use the letter 'A' as the marker, and the value
295 // consists of the form code (encoded as an unsigned LEB128 value) followed by
296 // the encoding of the value according to the form code. To ensure
297 // reproducibility of the signature, the set of forms used in the signature
298 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
299 // DW_FORM_string, and DW_FORM_block.
301 switch (Value.getType()) {
302 case DIEValue::isNone:
303 llvm_unreachable("Expected valid DIEValue");
306 // ... An attribute that refers to another type entry T is processed as
308 case DIEValue::isEntry:
309 hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
311 case DIEValue::isInteger: {
313 addULEB128(Attribute);
314 switch (Desc->getForm()) {
315 case dwarf::DW_FORM_data1:
316 case dwarf::DW_FORM_data2:
317 case dwarf::DW_FORM_data4:
318 case dwarf::DW_FORM_data8:
319 case dwarf::DW_FORM_udata:
320 case dwarf::DW_FORM_sdata:
321 addULEB128(dwarf::DW_FORM_sdata);
322 addSLEB128((int64_t)Value.getDIEInteger().getValue());
324 // DW_FORM_flag_present is just flag with a value of one. We still give it a
325 // value so just use the value.
326 case dwarf::DW_FORM_flag_present:
327 case dwarf::DW_FORM_flag:
328 addULEB128(dwarf::DW_FORM_flag);
329 addULEB128((int64_t)Value.getDIEInteger().getValue());
332 llvm_unreachable("Unknown integer form!");
336 case DIEValue::isString:
338 addULEB128(Attribute);
339 addULEB128(dwarf::DW_FORM_string);
340 addString(Value.getDIEString().getString());
342 case DIEValue::isBlock:
343 case DIEValue::isLoc:
344 case DIEValue::isLocList:
346 addULEB128(Attribute);
347 addULEB128(dwarf::DW_FORM_block);
348 if (Value.getType() == DIEValue::isBlock) {
349 addULEB128(Value.getDIEBlock().ComputeSize(AP));
350 hashBlockData(Value.getDIEBlock().getValues());
351 } else if (Value.getType() == DIEValue::isLoc) {
352 addULEB128(Value.getDIELoc().ComputeSize(AP));
353 hashBlockData(Value.getDIELoc().getValues());
355 // We could add the block length, but that would take
356 // a bit of work and not add a lot of uniqueness
357 // to the hash in some way we could test.
358 hashLocList(Value.getDIELocList());
361 // FIXME: It's uncertain whether or not we should handle this at the moment.
362 case DIEValue::isExpr:
363 case DIEValue::isLabel:
364 case DIEValue::isDelta:
365 case DIEValue::isTypeSignature:
366 llvm_unreachable("Add support for additional value types.");
370 // Go through the attributes from \param Attrs in the order specified in 7.27.4
372 void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
373 #define ADD_ATTR(ATTR) \
376 hashAttribute(ATTR, Tag); \
379 ADD_ATTR(Attrs.DW_AT_name);
380 ADD_ATTR(Attrs.DW_AT_accessibility);
381 ADD_ATTR(Attrs.DW_AT_address_class);
382 ADD_ATTR(Attrs.DW_AT_allocated);
383 ADD_ATTR(Attrs.DW_AT_artificial);
384 ADD_ATTR(Attrs.DW_AT_associated);
385 ADD_ATTR(Attrs.DW_AT_binary_scale);
386 ADD_ATTR(Attrs.DW_AT_bit_offset);
387 ADD_ATTR(Attrs.DW_AT_bit_size);
388 ADD_ATTR(Attrs.DW_AT_bit_stride);
389 ADD_ATTR(Attrs.DW_AT_byte_size);
390 ADD_ATTR(Attrs.DW_AT_byte_stride);
391 ADD_ATTR(Attrs.DW_AT_const_expr);
392 ADD_ATTR(Attrs.DW_AT_const_value);
393 ADD_ATTR(Attrs.DW_AT_containing_type);
394 ADD_ATTR(Attrs.DW_AT_count);
395 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
396 ADD_ATTR(Attrs.DW_AT_data_location);
397 ADD_ATTR(Attrs.DW_AT_data_member_location);
398 ADD_ATTR(Attrs.DW_AT_decimal_scale);
399 ADD_ATTR(Attrs.DW_AT_decimal_sign);
400 ADD_ATTR(Attrs.DW_AT_default_value);
401 ADD_ATTR(Attrs.DW_AT_digit_count);
402 ADD_ATTR(Attrs.DW_AT_discr);
403 ADD_ATTR(Attrs.DW_AT_discr_list);
404 ADD_ATTR(Attrs.DW_AT_discr_value);
405 ADD_ATTR(Attrs.DW_AT_encoding);
406 ADD_ATTR(Attrs.DW_AT_enum_class);
407 ADD_ATTR(Attrs.DW_AT_endianity);
408 ADD_ATTR(Attrs.DW_AT_explicit);
409 ADD_ATTR(Attrs.DW_AT_is_optional);
410 ADD_ATTR(Attrs.DW_AT_location);
411 ADD_ATTR(Attrs.DW_AT_lower_bound);
412 ADD_ATTR(Attrs.DW_AT_mutable);
413 ADD_ATTR(Attrs.DW_AT_ordering);
414 ADD_ATTR(Attrs.DW_AT_picture_string);
415 ADD_ATTR(Attrs.DW_AT_prototyped);
416 ADD_ATTR(Attrs.DW_AT_small);
417 ADD_ATTR(Attrs.DW_AT_segment);
418 ADD_ATTR(Attrs.DW_AT_string_length);
419 ADD_ATTR(Attrs.DW_AT_threads_scaled);
420 ADD_ATTR(Attrs.DW_AT_upper_bound);
421 ADD_ATTR(Attrs.DW_AT_use_location);
422 ADD_ATTR(Attrs.DW_AT_use_UTF8);
423 ADD_ATTR(Attrs.DW_AT_variable_parameter);
424 ADD_ATTR(Attrs.DW_AT_virtuality);
425 ADD_ATTR(Attrs.DW_AT_visibility);
426 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
427 ADD_ATTR(Attrs.DW_AT_type);
429 // FIXME: Add the extended attributes.
432 // Add all of the attributes for \param Die to the hash.
433 void DIEHash::addAttributes(const DIE &Die) {
435 collectAttributes(Die, Attrs);
436 hashAttributes(Attrs, Die.getTag());
439 void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
441 // ... append the letter 'S',
445 addULEB128(Die.getTag());
451 // Compute the hash of a DIE. This is based on the type signature computation
452 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
453 // flattened description of the DIE.
454 void DIEHash::computeHash(const DIE &Die) {
455 // Append the letter 'D', followed by the DWARF tag of the DIE.
457 addULEB128(Die.getTag());
459 // Add each of the attributes of the DIE.
462 // Then hash each of the children of the DIE.
463 for (auto &C : Die.getChildren()) {
465 // If C is a nested type entry or a member function entry, ...
466 if (isType(C->getTag()) || C->getTag() == dwarf::DW_TAG_subprogram) {
467 StringRef Name = getDIEStringAttr(*C, dwarf::DW_AT_name);
468 // ... and has a DW_AT_name attribute
470 hashNestedType(*C, Name);
477 // Following the last (or if there are no children), append a zero byte.
478 Hash.update(makeArrayRef((uint8_t)'\0'));
481 /// This is based on the type signature computation given in section 7.27 of the
482 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
483 /// with the exception that we are hashing only the context and the name of the
485 uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
487 // Add the contexts to the hash. We won't be computing the ODR hash for
488 // function local types so it's safe to use the generic context hashing
490 // FIXME: If we figure out how to account for linkage in some way we could
491 // actually do this with a slight modification to the parent hash algorithm.
492 if (const DIE *Parent = Die.getParent())
493 addParentContext(*Parent);
495 // Add the current DIE information.
497 // Add the DWARF tag of the DIE.
498 addULEB128(Die.getTag());
500 // Add the name of the type to the hash.
501 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
503 // Now get the result.
504 MD5::MD5Result Result;
507 // ... take the least significant 8 bytes and return those. Our MD5
508 // implementation always returns its results in little endian, swap bytes
510 return support::endian::read64le(Result + 8);
513 /// This is based on the type signature computation given in section 7.27 of the
514 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
515 /// with the inclusion of the full CU and all top level CU entities.
516 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
517 uint64_t DIEHash::computeCUSignature(const DIE &Die) {
524 // Now return the result.
525 MD5::MD5Result Result;
528 // ... take the least significant 8 bytes and return those. Our MD5
529 // implementation always returns its results in little endian, swap bytes
531 return support::endian::read64le(Result + 8);
534 /// This is based on the type signature computation given in section 7.27 of the
535 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
536 /// with the inclusion of additional forms not specifically called out in the
538 uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
542 if (const DIE *Parent = Die.getParent())
543 addParentContext(*Parent);
548 // Now return the result.
549 MD5::MD5Result Result;
552 // ... take the least significant 8 bytes and return those. Our MD5
553 // implementation always returns its results in little endian, swap bytes
555 return support::endian::read64le(Result + 8);