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 #define DEBUG_TYPE "dwarfdebug"
16 #include "ByteStreamer.h"
19 #include "DwarfDebug.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/Dwarf.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/MD5.h"
27 #include "llvm/Support/raw_ostream.h"
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 SmallVectorImpl<DIEValue *> &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 DIEValue *V = Values[i];
42 assert(isa<DIEString>(V) && "String requested. Not a string.");
43 DIEString *S = cast<DIEString>(V);
44 return S->getString();
50 /// \brief Adds the string in \p Str to the hash. This also hashes
51 /// a trailing NULL with the string.
52 void DIEHash::addString(StringRef Str) {
53 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
55 Hash.update(makeArrayRef((uint8_t)'\0'));
58 // FIXME: The LEB128 routines are copied and only slightly modified out of
61 /// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
62 void DIEHash::addULEB128(uint64_t Value) {
63 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
65 uint8_t Byte = Value & 0x7f;
68 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
73 void DIEHash::addSLEB128(int64_t Value) {
74 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
77 uint8_t Byte = Value & 0x7f;
79 More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
80 ((Value == -1) && ((Byte & 0x40) != 0))));
82 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
87 /// \brief Including \p Parent adds the context of Parent to the hash..
88 void DIEHash::addParentContext(const DIE &Parent) {
90 DEBUG(dbgs() << "Adding parent context to hash...\n");
92 // [7.27.2] For each surrounding type or namespace beginning with the
93 // outermost such construct...
94 SmallVector<const DIE *, 1> Parents;
95 const DIE *Cur = &Parent;
96 while (Cur->getParent()) {
97 Parents.push_back(Cur);
98 Cur = Cur->getParent();
100 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
101 Cur->getTag() == dwarf::DW_TAG_type_unit);
103 // Reverse iterate over our list to go from the outermost construct to the
105 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
108 const DIE &Die = **I;
110 // ... Append the letter "C" to the sequence...
113 // ... Followed by the DWARF tag of the construct...
114 addULEB128(Die.getTag());
116 // ... Then the name, taken from the DW_AT_name attribute.
117 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
118 DEBUG(dbgs() << "... adding context: " << Name << "\n");
124 // Collect all of the attributes for a particular DIE in single structure.
125 void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
126 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
127 const DIEAbbrev &Abbrevs = Die.getAbbrev();
129 #define COLLECT_ATTR(NAME) \
131 Attrs.NAME.Val = Values[i]; \
132 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
135 for (size_t i = 0, e = Values.size(); i != e; ++i) {
136 DEBUG(dbgs() << "Attribute: "
137 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
139 switch (Abbrevs.getData()[i].getAttribute()) {
140 COLLECT_ATTR(DW_AT_name);
141 COLLECT_ATTR(DW_AT_accessibility);
142 COLLECT_ATTR(DW_AT_address_class);
143 COLLECT_ATTR(DW_AT_allocated);
144 COLLECT_ATTR(DW_AT_artificial);
145 COLLECT_ATTR(DW_AT_associated);
146 COLLECT_ATTR(DW_AT_binary_scale);
147 COLLECT_ATTR(DW_AT_bit_offset);
148 COLLECT_ATTR(DW_AT_bit_size);
149 COLLECT_ATTR(DW_AT_bit_stride);
150 COLLECT_ATTR(DW_AT_byte_size);
151 COLLECT_ATTR(DW_AT_byte_stride);
152 COLLECT_ATTR(DW_AT_const_expr);
153 COLLECT_ATTR(DW_AT_const_value);
154 COLLECT_ATTR(DW_AT_containing_type);
155 COLLECT_ATTR(DW_AT_count);
156 COLLECT_ATTR(DW_AT_data_bit_offset);
157 COLLECT_ATTR(DW_AT_data_location);
158 COLLECT_ATTR(DW_AT_data_member_location);
159 COLLECT_ATTR(DW_AT_decimal_scale);
160 COLLECT_ATTR(DW_AT_decimal_sign);
161 COLLECT_ATTR(DW_AT_default_value);
162 COLLECT_ATTR(DW_AT_digit_count);
163 COLLECT_ATTR(DW_AT_discr);
164 COLLECT_ATTR(DW_AT_discr_list);
165 COLLECT_ATTR(DW_AT_discr_value);
166 COLLECT_ATTR(DW_AT_encoding);
167 COLLECT_ATTR(DW_AT_enum_class);
168 COLLECT_ATTR(DW_AT_endianity);
169 COLLECT_ATTR(DW_AT_explicit);
170 COLLECT_ATTR(DW_AT_is_optional);
171 COLLECT_ATTR(DW_AT_location);
172 COLLECT_ATTR(DW_AT_lower_bound);
173 COLLECT_ATTR(DW_AT_mutable);
174 COLLECT_ATTR(DW_AT_ordering);
175 COLLECT_ATTR(DW_AT_picture_string);
176 COLLECT_ATTR(DW_AT_prototyped);
177 COLLECT_ATTR(DW_AT_small);
178 COLLECT_ATTR(DW_AT_segment);
179 COLLECT_ATTR(DW_AT_string_length);
180 COLLECT_ATTR(DW_AT_threads_scaled);
181 COLLECT_ATTR(DW_AT_upper_bound);
182 COLLECT_ATTR(DW_AT_use_location);
183 COLLECT_ATTR(DW_AT_use_UTF8);
184 COLLECT_ATTR(DW_AT_variable_parameter);
185 COLLECT_ATTR(DW_AT_virtuality);
186 COLLECT_ATTR(DW_AT_visibility);
187 COLLECT_ATTR(DW_AT_vtable_elem_location);
188 COLLECT_ATTR(DW_AT_type);
195 void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
196 const DIE &Entry, StringRef Name) {
197 // append the letter 'N'
200 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
201 addULEB128(Attribute);
203 // the context of the tag,
204 if (const DIE *Parent = Entry.getParent())
205 addParentContext(*Parent);
210 // and the name of the type.
213 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
214 // here's the relevant spec text to implement:
216 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
217 // the context is omitted and the name to be used is the ABI-specific name
218 // of the subprogram (e.g., the mangled linker name).
221 void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
222 unsigned DieNumber) {
223 // a) If T is in the list of [previously hashed types], use the letter
227 addULEB128(Attribute);
229 // and use the unsigned LEB128 encoding of [the index of T in the
230 // list] as the attribute value;
231 addULEB128(DieNumber);
234 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
236 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
237 "tags. Add support here when there's "
240 // If the tag in Step 3 is one of [the below tags]
241 if ((Tag == dwarf::DW_TAG_pointer_type ||
242 Tag == dwarf::DW_TAG_reference_type ||
243 Tag == dwarf::DW_TAG_rvalue_reference_type ||
244 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
245 // and the referenced type (via the [below attributes])
246 // FIXME: This seems overly restrictive, and causes hash mismatches
247 // there's a decl/def difference in the containing type of a
248 // ptr_to_member_type, but it's what DWARF says, for some reason.
249 Attribute == dwarf::DW_AT_type) {
250 // ... has a DW_AT_name attribute,
251 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
253 hashShallowTypeReference(Attribute, Entry, Name);
258 unsigned &DieNumber = Numbering[&Entry];
260 hashRepeatedTypeReference(Attribute, DieNumber);
264 // otherwise, b) use the letter 'T' as a the marker, ...
267 addULEB128(Attribute);
269 // ... process the type T recursively by performing Steps 2 through 7, and
270 // use the result as the attribute value.
271 DieNumber = Numbering.size();
275 // Hash all of the values in a block like set of values. This assumes that
276 // all of the data is going to be added as integers.
277 void DIEHash::hashBlockData(const SmallVectorImpl<DIEValue *> &Values) {
278 for (SmallVectorImpl<DIEValue *>::const_iterator I = Values.begin(),
281 Hash.update((uint64_t)cast<DIEInteger>(*I)->getValue());
284 // Hash the contents of a loclistptr class.
285 void DIEHash::hashLocList(const DIELocList &LocList) {
286 SmallVectorImpl<DotDebugLocEntry>::const_iterator Start =
287 AP->getDwarfDebug()->getDebugLocEntries().begin();
288 Start += LocList.getValue();
289 HashingByteStreamer Streamer(*this);
290 for (SmallVectorImpl<DotDebugLocEntry>::const_iterator
292 E = AP->getDwarfDebug()->getDebugLocEntries().end();
294 const DotDebugLocEntry &Entry = *I;
295 // Go through the entries until we hit the end of the list,
296 // which is the next empty entry.
299 else if (Entry.isMerged())
302 AP->getDwarfDebug()->emitDebugLocEntry(Streamer, Entry);
306 // Hash an individual attribute \param Attr based on the type of attribute and
308 void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
309 const DIEValue *Value = Attr.Val;
310 const DIEAbbrevData *Desc = Attr.Desc;
311 dwarf::Attribute Attribute = Desc->getAttribute();
313 // Other attribute values use the letter 'A' as the marker, and the value
314 // consists of the form code (encoded as an unsigned LEB128 value) followed by
315 // the encoding of the value according to the form code. To ensure
316 // reproducibility of the signature, the set of forms used in the signature
317 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
318 // DW_FORM_string, and DW_FORM_block.
320 switch (Value->getType()) {
322 // ... An attribute that refers to another type entry T is processed as
324 case DIEValue::isEntry:
325 hashDIEEntry(Attribute, Tag, *cast<DIEEntry>(Value)->getEntry());
327 case DIEValue::isInteger: {
329 addULEB128(Attribute);
330 switch (Desc->getForm()) {
331 case dwarf::DW_FORM_data1:
332 case dwarf::DW_FORM_data2:
333 case dwarf::DW_FORM_data4:
334 case dwarf::DW_FORM_data8:
335 case dwarf::DW_FORM_udata:
336 case dwarf::DW_FORM_sdata:
337 addULEB128(dwarf::DW_FORM_sdata);
338 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
340 // DW_FORM_flag_present is just flag with a value of one. We still give it a
341 // value so just use the value.
342 case dwarf::DW_FORM_flag_present:
343 case dwarf::DW_FORM_flag:
344 addULEB128(dwarf::DW_FORM_flag);
345 addULEB128((int64_t)cast<DIEInteger>(Value)->getValue());
348 llvm_unreachable("Unknown integer form!");
352 case DIEValue::isString:
354 addULEB128(Attribute);
355 addULEB128(dwarf::DW_FORM_string);
356 addString(cast<DIEString>(Value)->getString());
358 case DIEValue::isBlock:
359 case DIEValue::isLoc:
360 case DIEValue::isLocList:
362 addULEB128(Attribute);
363 addULEB128(dwarf::DW_FORM_block);
364 if (isa<DIEBlock>(Value)) {
365 addULEB128(cast<DIEBlock>(Value)->ComputeSize(AP));
366 hashBlockData(cast<DIEBlock>(Value)->getValues());
367 } else if (isa<DIELoc>(Value)) {
368 addULEB128(cast<DIELoc>(Value)->ComputeSize(AP));
369 hashBlockData(cast<DIELoc>(Value)->getValues());
371 // We could add the block length, but that would take
372 // a bit of work and not add a lot of uniqueness
373 // to the hash in some way we could test.
374 hashLocList(*cast<DIELocList>(Value));
377 // FIXME: It's uncertain whether or not we should handle this at the moment.
378 case DIEValue::isExpr:
379 case DIEValue::isLabel:
380 case DIEValue::isDelta:
381 case DIEValue::isTypeSignature:
382 llvm_unreachable("Add support for additional value types.");
386 // Go through the attributes from \param Attrs in the order specified in 7.27.4
388 void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
389 #define ADD_ATTR(ATTR) \
392 hashAttribute(ATTR, Tag); \
395 ADD_ATTR(Attrs.DW_AT_name);
396 ADD_ATTR(Attrs.DW_AT_accessibility);
397 ADD_ATTR(Attrs.DW_AT_address_class);
398 ADD_ATTR(Attrs.DW_AT_allocated);
399 ADD_ATTR(Attrs.DW_AT_artificial);
400 ADD_ATTR(Attrs.DW_AT_associated);
401 ADD_ATTR(Attrs.DW_AT_binary_scale);
402 ADD_ATTR(Attrs.DW_AT_bit_offset);
403 ADD_ATTR(Attrs.DW_AT_bit_size);
404 ADD_ATTR(Attrs.DW_AT_bit_stride);
405 ADD_ATTR(Attrs.DW_AT_byte_size);
406 ADD_ATTR(Attrs.DW_AT_byte_stride);
407 ADD_ATTR(Attrs.DW_AT_const_expr);
408 ADD_ATTR(Attrs.DW_AT_const_value);
409 ADD_ATTR(Attrs.DW_AT_containing_type);
410 ADD_ATTR(Attrs.DW_AT_count);
411 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
412 ADD_ATTR(Attrs.DW_AT_data_location);
413 ADD_ATTR(Attrs.DW_AT_data_member_location);
414 ADD_ATTR(Attrs.DW_AT_decimal_scale);
415 ADD_ATTR(Attrs.DW_AT_decimal_sign);
416 ADD_ATTR(Attrs.DW_AT_default_value);
417 ADD_ATTR(Attrs.DW_AT_digit_count);
418 ADD_ATTR(Attrs.DW_AT_discr);
419 ADD_ATTR(Attrs.DW_AT_discr_list);
420 ADD_ATTR(Attrs.DW_AT_discr_value);
421 ADD_ATTR(Attrs.DW_AT_encoding);
422 ADD_ATTR(Attrs.DW_AT_enum_class);
423 ADD_ATTR(Attrs.DW_AT_endianity);
424 ADD_ATTR(Attrs.DW_AT_explicit);
425 ADD_ATTR(Attrs.DW_AT_is_optional);
426 ADD_ATTR(Attrs.DW_AT_location);
427 ADD_ATTR(Attrs.DW_AT_lower_bound);
428 ADD_ATTR(Attrs.DW_AT_mutable);
429 ADD_ATTR(Attrs.DW_AT_ordering);
430 ADD_ATTR(Attrs.DW_AT_picture_string);
431 ADD_ATTR(Attrs.DW_AT_prototyped);
432 ADD_ATTR(Attrs.DW_AT_small);
433 ADD_ATTR(Attrs.DW_AT_segment);
434 ADD_ATTR(Attrs.DW_AT_string_length);
435 ADD_ATTR(Attrs.DW_AT_threads_scaled);
436 ADD_ATTR(Attrs.DW_AT_upper_bound);
437 ADD_ATTR(Attrs.DW_AT_use_location);
438 ADD_ATTR(Attrs.DW_AT_use_UTF8);
439 ADD_ATTR(Attrs.DW_AT_variable_parameter);
440 ADD_ATTR(Attrs.DW_AT_virtuality);
441 ADD_ATTR(Attrs.DW_AT_visibility);
442 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
443 ADD_ATTR(Attrs.DW_AT_type);
445 // FIXME: Add the extended attributes.
448 // Add all of the attributes for \param Die to the hash.
449 void DIEHash::addAttributes(const DIE &Die) {
451 collectAttributes(Die, Attrs);
452 hashAttributes(Attrs, Die.getTag());
455 void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
457 // ... append the letter 'S',
461 addULEB128(Die.getTag());
467 // Compute the hash of a DIE. This is based on the type signature computation
468 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
469 // flattened description of the DIE.
470 void DIEHash::computeHash(const DIE &Die) {
471 // Append the letter 'D', followed by the DWARF tag of the DIE.
473 addULEB128(Die.getTag());
475 // Add each of the attributes of the DIE.
478 // Then hash each of the children of the DIE.
479 for (std::vector<DIE *>::const_iterator I = Die.getChildren().begin(),
480 E = Die.getChildren().end();
483 // If C is a nested type entry or a member function entry, ...
484 if (isType((*I)->getTag()) || (*I)->getTag() == dwarf::DW_TAG_subprogram) {
485 StringRef Name = getDIEStringAttr(**I, dwarf::DW_AT_name);
486 // ... and has a DW_AT_name attribute
488 hashNestedType(**I, Name);
495 // Following the last (or if there are no children), append a zero byte.
496 Hash.update(makeArrayRef((uint8_t)'\0'));
499 /// This is based on the type signature computation given in section 7.27 of the
500 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
501 /// with the exception that we are hashing only the context and the name of the
503 uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
505 // Add the contexts to the hash. We won't be computing the ODR hash for
506 // function local types so it's safe to use the generic context hashing
508 // FIXME: If we figure out how to account for linkage in some way we could
509 // actually do this with a slight modification to the parent hash algorithm.
510 if (const DIE *Parent = Die.getParent())
511 addParentContext(*Parent);
513 // Add the current DIE information.
515 // Add the DWARF tag of the DIE.
516 addULEB128(Die.getTag());
518 // Add the name of the type to the hash.
519 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
521 // Now get the result.
522 MD5::MD5Result Result;
525 // ... take the least significant 8 bytes and return those. Our MD5
526 // implementation always returns its results in little endian, swap bytes
528 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
531 /// This is based on the type signature computation given in section 7.27 of the
532 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
533 /// with the inclusion of the full CU and all top level CU entities.
534 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
535 uint64_t DIEHash::computeCUSignature(const DIE &Die) {
542 // Now return the result.
543 MD5::MD5Result Result;
546 // ... take the least significant 8 bytes and return those. Our MD5
547 // implementation always returns its results in little endian, swap bytes
549 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
552 /// This is based on the type signature computation given in section 7.27 of the
553 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
554 /// with the inclusion of additional forms not specifically called out in the
556 uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
560 if (const DIE *Parent = Die.getParent())
561 addParentContext(*Parent);
566 // Now return the result.
567 MD5::MD5Result Result;
570 // ... take the least significant 8 bytes and return those. Our MD5
571 // implementation always returns its results in little endian, swap bytes
573 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);