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"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/CodeGen/AsmPrinter.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 /// \brief Grabs the string in whichever attribute is passed in and returns
30 /// a reference to it.
31 static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
32 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
33 const DIEAbbrev &Abbrevs = Die.getAbbrev();
35 // Iterate through all the attributes until we find the one we're
36 // looking for, if we can't find it return an empty string.
37 for (size_t i = 0; i < Values.size(); ++i) {
38 if (Abbrevs.getData()[i].getAttribute() == Attr) {
39 DIEValue *V = Values[i];
40 assert(isa<DIEString>(V) && "String requested. Not a string.");
41 DIEString *S = cast<DIEString>(V);
42 return S->getString();
48 /// \brief Adds the string in \p Str to the hash. This also hashes
49 /// a trailing NULL with the string.
50 void DIEHash::addString(StringRef Str) {
51 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
53 Hash.update(makeArrayRef((uint8_t)'\0'));
56 // FIXME: The LEB128 routines are copied and only slightly modified out of
59 /// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
60 void DIEHash::addULEB128(uint64_t Value) {
61 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
63 uint8_t Byte = Value & 0x7f;
66 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
71 void DIEHash::addSLEB128(int64_t Value) {
72 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
75 uint8_t Byte = Value & 0x7f;
77 More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
78 ((Value == -1) && ((Byte & 0x40) != 0))));
80 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
85 /// \brief Including \p Parent adds the context of Parent to the hash..
86 void DIEHash::addParentContext(const DIE &Parent) {
88 DEBUG(dbgs() << "Adding parent context to hash...\n");
90 // [7.27.2] For each surrounding type or namespace beginning with the
91 // outermost such construct...
92 SmallVector<const DIE *, 1> Parents;
93 const DIE *Cur = &Parent;
94 while (Cur->getParent()) {
95 Parents.push_back(Cur);
96 Cur = Cur->getParent();
98 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
99 Cur->getTag() == dwarf::DW_TAG_type_unit);
101 // Reverse iterate over our list to go from the outermost construct to the
103 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
106 const DIE &Die = **I;
108 // ... Append the letter "C" to the sequence...
111 // ... Followed by the DWARF tag of the construct...
112 addULEB128(Die.getTag());
114 // ... Then the name, taken from the DW_AT_name attribute.
115 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
116 DEBUG(dbgs() << "... adding context: " << Name << "\n");
122 // Collect all of the attributes for a particular DIE in single structure.
123 void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
124 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
125 const DIEAbbrev &Abbrevs = Die.getAbbrev();
127 #define COLLECT_ATTR(NAME) \
129 Attrs.NAME.Val = Values[i]; \
130 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
133 for (size_t i = 0, e = Values.size(); i != e; ++i) {
134 DEBUG(dbgs() << "Attribute: "
135 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
137 switch (Abbrevs.getData()[i].getAttribute()) {
138 COLLECT_ATTR(DW_AT_name);
139 COLLECT_ATTR(DW_AT_accessibility);
140 COLLECT_ATTR(DW_AT_address_class);
141 COLLECT_ATTR(DW_AT_allocated);
142 COLLECT_ATTR(DW_AT_artificial);
143 COLLECT_ATTR(DW_AT_associated);
144 COLLECT_ATTR(DW_AT_binary_scale);
145 COLLECT_ATTR(DW_AT_bit_offset);
146 COLLECT_ATTR(DW_AT_bit_size);
147 COLLECT_ATTR(DW_AT_bit_stride);
148 COLLECT_ATTR(DW_AT_byte_size);
149 COLLECT_ATTR(DW_AT_byte_stride);
150 COLLECT_ATTR(DW_AT_const_expr);
151 COLLECT_ATTR(DW_AT_const_value);
152 COLLECT_ATTR(DW_AT_containing_type);
153 COLLECT_ATTR(DW_AT_count);
154 COLLECT_ATTR(DW_AT_data_bit_offset);
155 COLLECT_ATTR(DW_AT_data_location);
156 COLLECT_ATTR(DW_AT_data_member_location);
157 COLLECT_ATTR(DW_AT_decimal_scale);
158 COLLECT_ATTR(DW_AT_decimal_sign);
159 COLLECT_ATTR(DW_AT_default_value);
160 COLLECT_ATTR(DW_AT_digit_count);
161 COLLECT_ATTR(DW_AT_discr);
162 COLLECT_ATTR(DW_AT_discr_list);
163 COLLECT_ATTR(DW_AT_discr_value);
164 COLLECT_ATTR(DW_AT_encoding);
165 COLLECT_ATTR(DW_AT_enum_class);
166 COLLECT_ATTR(DW_AT_endianity);
167 COLLECT_ATTR(DW_AT_explicit);
168 COLLECT_ATTR(DW_AT_is_optional);
169 COLLECT_ATTR(DW_AT_location);
170 COLLECT_ATTR(DW_AT_lower_bound);
171 COLLECT_ATTR(DW_AT_mutable);
172 COLLECT_ATTR(DW_AT_ordering);
173 COLLECT_ATTR(DW_AT_picture_string);
174 COLLECT_ATTR(DW_AT_prototyped);
175 COLLECT_ATTR(DW_AT_small);
176 COLLECT_ATTR(DW_AT_segment);
177 COLLECT_ATTR(DW_AT_string_length);
178 COLLECT_ATTR(DW_AT_threads_scaled);
179 COLLECT_ATTR(DW_AT_upper_bound);
180 COLLECT_ATTR(DW_AT_use_location);
181 COLLECT_ATTR(DW_AT_use_UTF8);
182 COLLECT_ATTR(DW_AT_variable_parameter);
183 COLLECT_ATTR(DW_AT_virtuality);
184 COLLECT_ATTR(DW_AT_visibility);
185 COLLECT_ATTR(DW_AT_vtable_elem_location);
186 COLLECT_ATTR(DW_AT_type);
193 void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
194 const DIE &Entry, StringRef Name) {
195 // append the letter 'N'
198 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
199 addULEB128(Attribute);
201 // the context of the tag,
202 if (const DIE *Parent = Entry.getParent())
203 addParentContext(*Parent);
208 // and the name of the type.
211 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
212 // here's the relevant spec text to implement:
214 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
215 // the context is omitted and the name to be used is the ABI-specific name
216 // of the subprogram (e.g., the mangled linker name).
219 void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
220 unsigned DieNumber) {
221 // a) If T is in the list of [previously hashed types], use the letter
225 addULEB128(Attribute);
227 // and use the unsigned LEB128 encoding of [the index of T in the
228 // list] as the attribute value;
229 addULEB128(DieNumber);
232 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
234 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
235 "tags. Add support here when there's "
238 // If the tag in Step 3 is one of [the below tags]
239 if ((Tag == dwarf::DW_TAG_pointer_type ||
240 Tag == dwarf::DW_TAG_reference_type ||
241 Tag == dwarf::DW_TAG_rvalue_reference_type ||
242 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
243 // and the referenced type (via the [below attributes])
244 // FIXME: This seems overly restrictive, and causes hash mismatches
245 // there's a decl/def difference in the containing type of a
246 // ptr_to_member_type, but it's what DWARF says, for some reason.
247 Attribute == dwarf::DW_AT_type) {
248 // ... has a DW_AT_name attribute,
249 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
251 hashShallowTypeReference(Attribute, Entry, Name);
256 unsigned &DieNumber = Numbering[&Entry];
258 hashRepeatedTypeReference(Attribute, DieNumber);
262 // otherwise, b) use the letter 'T' as a the marker, ...
265 addULEB128(Attribute);
267 // ... process the type T recursively by performing Steps 2 through 7, and
268 // use the result as the attribute value.
269 DieNumber = Numbering.size();
273 // Hash all of the values in a block like set of values. This assumes that
274 // all of the data is going to be added as integers.
275 void DIEHash::hashBlockData(const SmallVectorImpl<DIEValue *> &Values) {
276 for (SmallVectorImpl<DIEValue *>::const_iterator I = Values.begin(),
279 Hash.update((uint64_t)cast<DIEInteger>(*I)->getValue());
282 // Hash an individual attribute \param Attr based on the type of attribute and
284 void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
285 const DIEValue *Value = Attr.Val;
286 const DIEAbbrevData *Desc = Attr.Desc;
287 dwarf::Attribute Attribute = Desc->getAttribute();
289 // Other attribute values use the letter 'A' as the marker, and the value
290 // consists of the form code (encoded as an unsigned LEB128 value) followed by
291 // the encoding of the value according to the form code. To ensure
292 // reproducibility of the signature, the set of forms used in the signature
293 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
294 // DW_FORM_string, and DW_FORM_block.
296 switch (Value->getType()) {
298 // ... An attribute that refers to another type entry T is processed as
300 case DIEValue::isEntry:
301 hashDIEEntry(Attribute, Tag, *cast<DIEEntry>(Value)->getEntry());
303 case DIEValue::isInteger: {
305 addULEB128(Attribute);
306 switch (Desc->getForm()) {
307 case dwarf::DW_FORM_data1:
308 case dwarf::DW_FORM_data2:
309 case dwarf::DW_FORM_data4:
310 case dwarf::DW_FORM_data8:
311 case dwarf::DW_FORM_udata:
312 case dwarf::DW_FORM_sdata:
313 addULEB128(dwarf::DW_FORM_sdata);
314 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
316 // DW_FORM_flag_present is just flag with a value of one. We still give it a
317 // value so just use the value.
318 case dwarf::DW_FORM_flag_present:
319 case dwarf::DW_FORM_flag:
320 addULEB128(dwarf::DW_FORM_flag);
321 addULEB128((int64_t)cast<DIEInteger>(Value)->getValue());
324 llvm_unreachable("Unknown integer form!");
328 case DIEValue::isString:
330 addULEB128(Attribute);
331 addULEB128(dwarf::DW_FORM_string);
332 addString(cast<DIEString>(Value)->getString());
334 case DIEValue::isBlock:
335 case DIEValue::isLoc:
337 addULEB128(Attribute);
338 addULEB128(dwarf::DW_FORM_block);
339 if (isa<DIEBlock>(Value)) {
340 addULEB128(cast<DIEBlock>(Value)->ComputeSize(AP));
341 hashBlockData(cast<DIEBlock>(Value)->getValues());
343 addULEB128(cast<DIELoc>(Value)->ComputeSize(AP));
344 hashBlockData(cast<DIELoc>(Value)->getValues());
347 // FIXME: Handle loclistptr.
348 case DIEValue::isLocList:
349 // FIXME: It's uncertain whether or not we should handle this at the moment.
350 case DIEValue::isExpr:
351 case DIEValue::isLabel:
352 case DIEValue::isDelta:
353 case DIEValue::isTypeSignature:
354 llvm_unreachable("Add support for additional value types.");
358 // Go through the attributes from \param Attrs in the order specified in 7.27.4
360 void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
361 #define ADD_ATTR(ATTR) \
364 hashAttribute(ATTR, Tag); \
367 ADD_ATTR(Attrs.DW_AT_name);
368 ADD_ATTR(Attrs.DW_AT_accessibility);
369 ADD_ATTR(Attrs.DW_AT_address_class);
370 ADD_ATTR(Attrs.DW_AT_allocated);
371 ADD_ATTR(Attrs.DW_AT_artificial);
372 ADD_ATTR(Attrs.DW_AT_associated);
373 ADD_ATTR(Attrs.DW_AT_binary_scale);
374 ADD_ATTR(Attrs.DW_AT_bit_offset);
375 ADD_ATTR(Attrs.DW_AT_bit_size);
376 ADD_ATTR(Attrs.DW_AT_bit_stride);
377 ADD_ATTR(Attrs.DW_AT_byte_size);
378 ADD_ATTR(Attrs.DW_AT_byte_stride);
379 ADD_ATTR(Attrs.DW_AT_const_expr);
380 ADD_ATTR(Attrs.DW_AT_const_value);
381 ADD_ATTR(Attrs.DW_AT_containing_type);
382 ADD_ATTR(Attrs.DW_AT_count);
383 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
384 ADD_ATTR(Attrs.DW_AT_data_location);
385 ADD_ATTR(Attrs.DW_AT_data_member_location);
386 ADD_ATTR(Attrs.DW_AT_decimal_scale);
387 ADD_ATTR(Attrs.DW_AT_decimal_sign);
388 ADD_ATTR(Attrs.DW_AT_default_value);
389 ADD_ATTR(Attrs.DW_AT_digit_count);
390 ADD_ATTR(Attrs.DW_AT_discr);
391 ADD_ATTR(Attrs.DW_AT_discr_list);
392 ADD_ATTR(Attrs.DW_AT_discr_value);
393 ADD_ATTR(Attrs.DW_AT_encoding);
394 ADD_ATTR(Attrs.DW_AT_enum_class);
395 ADD_ATTR(Attrs.DW_AT_endianity);
396 ADD_ATTR(Attrs.DW_AT_explicit);
397 ADD_ATTR(Attrs.DW_AT_is_optional);
398 ADD_ATTR(Attrs.DW_AT_location);
399 ADD_ATTR(Attrs.DW_AT_lower_bound);
400 ADD_ATTR(Attrs.DW_AT_mutable);
401 ADD_ATTR(Attrs.DW_AT_ordering);
402 ADD_ATTR(Attrs.DW_AT_picture_string);
403 ADD_ATTR(Attrs.DW_AT_prototyped);
404 ADD_ATTR(Attrs.DW_AT_small);
405 ADD_ATTR(Attrs.DW_AT_segment);
406 ADD_ATTR(Attrs.DW_AT_string_length);
407 ADD_ATTR(Attrs.DW_AT_threads_scaled);
408 ADD_ATTR(Attrs.DW_AT_upper_bound);
409 ADD_ATTR(Attrs.DW_AT_use_location);
410 ADD_ATTR(Attrs.DW_AT_use_UTF8);
411 ADD_ATTR(Attrs.DW_AT_variable_parameter);
412 ADD_ATTR(Attrs.DW_AT_virtuality);
413 ADD_ATTR(Attrs.DW_AT_visibility);
414 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
415 ADD_ATTR(Attrs.DW_AT_type);
417 // FIXME: Add the extended attributes.
420 // Add all of the attributes for \param Die to the hash.
421 void DIEHash::addAttributes(const DIE &Die) {
423 collectAttributes(Die, Attrs);
424 hashAttributes(Attrs, Die.getTag());
427 void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
429 // ... append the letter 'S',
433 addULEB128(Die.getTag());
439 // Compute the hash of a DIE. This is based on the type signature computation
440 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
441 // flattened description of the DIE.
442 void DIEHash::computeHash(const DIE &Die) {
443 // Append the letter 'D', followed by the DWARF tag of the DIE.
445 addULEB128(Die.getTag());
447 // Add each of the attributes of the DIE.
450 // Then hash each of the children of the DIE.
451 for (std::vector<DIE *>::const_iterator I = Die.getChildren().begin(),
452 E = Die.getChildren().end();
455 // If C is a nested type entry or a member function entry, ...
456 if (isType((*I)->getTag()) || (*I)->getTag() == dwarf::DW_TAG_subprogram) {
457 StringRef Name = getDIEStringAttr(**I, dwarf::DW_AT_name);
458 // ... and has a DW_AT_name attribute
460 hashNestedType(**I, Name);
467 // Following the last (or if there are no children), append a zero byte.
468 Hash.update(makeArrayRef((uint8_t)'\0'));
471 /// This is based on the type signature computation given in section 7.27 of the
472 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
473 /// with the exception that we are hashing only the context and the name of the
475 uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
477 // Add the contexts to the hash. We won't be computing the ODR hash for
478 // function local types so it's safe to use the generic context hashing
480 // FIXME: If we figure out how to account for linkage in some way we could
481 // actually do this with a slight modification to the parent hash algorithm.
482 if (const DIE *Parent = Die.getParent())
483 addParentContext(*Parent);
485 // Add the current DIE information.
487 // Add the DWARF tag of the DIE.
488 addULEB128(Die.getTag());
490 // Add the name of the type to the hash.
491 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
493 // Now get the result.
494 MD5::MD5Result Result;
497 // ... take the least significant 8 bytes and return those. Our MD5
498 // implementation always returns its results in little endian, swap bytes
500 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
503 /// This is based on the type signature computation given in section 7.27 of the
504 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
505 /// with the inclusion of the full CU and all top level CU entities.
506 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
507 uint64_t DIEHash::computeCUSignature(const DIE &Die) {
514 // Now return the result.
515 MD5::MD5Result Result;
518 // ... take the least significant 8 bytes and return those. Our MD5
519 // implementation always returns its results in little endian, swap bytes
521 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
524 /// This is based on the type signature computation given in section 7.27 of the
525 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
526 /// with the inclusion of additional forms not specifically called out in the
528 uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
532 if (const DIE *Parent = Die.getParent())
533 addParentContext(*Parent);
538 // Now return the result.
539 MD5::MD5Result Result;
542 // ... take the least significant 8 bytes and return those. Our MD5
543 // implementation always returns its results in little endian, swap bytes
545 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);