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 "DwarfCompileUnit.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.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(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(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<DIE *, 1> Parents;
93 while (Parent->getTag() != dwarf::DW_TAG_compile_unit) {
94 Parents.push_back(Parent);
95 Parent = Parent->getParent();
98 // Reverse iterate over our list to go from the outermost construct to the
100 for (SmallVectorImpl<DIE *>::reverse_iterator I = Parents.rbegin(),
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(DIE *Die, DIEAttrs &Attrs) {
121 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
122 const DIEAbbrev &Abbrevs = Die->getAbbrev();
124 #define COLLECT_ATTR(NAME) \
125 Attrs.NAME.Val = Values[i]; \
126 Attrs.NAME.Desc = &Abbrevs.getData()[i];
128 for (size_t i = 0, e = Values.size(); i != e; ++i) {
129 DEBUG(dbgs() << "Attribute: "
130 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
132 switch (Abbrevs.getData()[i].getAttribute()) {
133 case dwarf::DW_AT_name:
134 COLLECT_ATTR(DW_AT_name);
136 case dwarf::DW_AT_accessibility:
137 COLLECT_ATTR(DW_AT_accessibility)
139 case dwarf::DW_AT_address_class:
140 COLLECT_ATTR(DW_AT_address_class)
142 case dwarf::DW_AT_allocated:
143 COLLECT_ATTR(DW_AT_allocated)
145 case dwarf::DW_AT_artificial:
146 COLLECT_ATTR(DW_AT_artificial)
148 case dwarf::DW_AT_associated:
149 COLLECT_ATTR(DW_AT_associated)
151 case dwarf::DW_AT_binary_scale:
152 COLLECT_ATTR(DW_AT_binary_scale)
154 case dwarf::DW_AT_bit_offset:
155 COLLECT_ATTR(DW_AT_bit_offset)
157 case dwarf::DW_AT_bit_size:
158 COLLECT_ATTR(DW_AT_bit_size)
160 case dwarf::DW_AT_bit_stride:
161 COLLECT_ATTR(DW_AT_bit_stride)
163 case dwarf::DW_AT_byte_size:
164 COLLECT_ATTR(DW_AT_byte_size)
166 case dwarf::DW_AT_byte_stride:
167 COLLECT_ATTR(DW_AT_byte_stride)
169 case dwarf::DW_AT_const_expr:
170 COLLECT_ATTR(DW_AT_const_expr)
172 case dwarf::DW_AT_const_value:
173 COLLECT_ATTR(DW_AT_const_value)
175 case dwarf::DW_AT_containing_type:
176 COLLECT_ATTR(DW_AT_containing_type)
178 case dwarf::DW_AT_count:
179 COLLECT_ATTR(DW_AT_count)
181 case dwarf::DW_AT_data_bit_offset:
182 COLLECT_ATTR(DW_AT_data_bit_offset)
184 case dwarf::DW_AT_data_location:
185 COLLECT_ATTR(DW_AT_data_location)
187 case dwarf::DW_AT_data_member_location:
188 COLLECT_ATTR(DW_AT_data_member_location)
190 case dwarf::DW_AT_decimal_scale:
191 COLLECT_ATTR(DW_AT_decimal_scale)
193 case dwarf::DW_AT_decimal_sign:
194 COLLECT_ATTR(DW_AT_decimal_sign)
196 case dwarf::DW_AT_default_value:
197 COLLECT_ATTR(DW_AT_default_value)
199 case dwarf::DW_AT_digit_count:
200 COLLECT_ATTR(DW_AT_digit_count)
202 case dwarf::DW_AT_discr:
203 COLLECT_ATTR(DW_AT_discr)
205 case dwarf::DW_AT_discr_list:
206 COLLECT_ATTR(DW_AT_discr_list)
208 case dwarf::DW_AT_discr_value:
209 COLLECT_ATTR(DW_AT_discr_value)
211 case dwarf::DW_AT_encoding:
212 COLLECT_ATTR(DW_AT_encoding)
214 case dwarf::DW_AT_enum_class:
215 COLLECT_ATTR(DW_AT_enum_class)
217 case dwarf::DW_AT_endianity:
218 COLLECT_ATTR(DW_AT_endianity)
220 case dwarf::DW_AT_explicit:
221 COLLECT_ATTR(DW_AT_explicit)
223 case dwarf::DW_AT_is_optional:
224 COLLECT_ATTR(DW_AT_is_optional)
226 case dwarf::DW_AT_location:
227 COLLECT_ATTR(DW_AT_location)
229 case dwarf::DW_AT_lower_bound:
230 COLLECT_ATTR(DW_AT_lower_bound)
232 case dwarf::DW_AT_mutable:
233 COLLECT_ATTR(DW_AT_mutable)
235 case dwarf::DW_AT_ordering:
236 COLLECT_ATTR(DW_AT_ordering)
238 case dwarf::DW_AT_picture_string:
239 COLLECT_ATTR(DW_AT_picture_string)
241 case dwarf::DW_AT_prototyped:
242 COLLECT_ATTR(DW_AT_prototyped)
244 case dwarf::DW_AT_small:
245 COLLECT_ATTR(DW_AT_small)
247 case dwarf::DW_AT_segment:
248 COLLECT_ATTR(DW_AT_segment)
250 case dwarf::DW_AT_string_length:
251 COLLECT_ATTR(DW_AT_string_length)
253 case dwarf::DW_AT_threads_scaled:
254 COLLECT_ATTR(DW_AT_threads_scaled)
256 case dwarf::DW_AT_upper_bound:
257 COLLECT_ATTR(DW_AT_upper_bound)
259 case dwarf::DW_AT_use_location:
260 COLLECT_ATTR(DW_AT_use_location)
262 case dwarf::DW_AT_use_UTF8:
263 COLLECT_ATTR(DW_AT_use_UTF8)
265 case dwarf::DW_AT_variable_parameter:
266 COLLECT_ATTR(DW_AT_variable_parameter)
268 case dwarf::DW_AT_virtuality:
269 COLLECT_ATTR(DW_AT_virtuality)
271 case dwarf::DW_AT_visibility:
272 COLLECT_ATTR(DW_AT_visibility)
274 case dwarf::DW_AT_vtable_elem_location:
275 COLLECT_ATTR(DW_AT_vtable_elem_location)
277 case dwarf::DW_AT_type:
278 COLLECT_ATTR(DW_AT_type)
286 // Hash an individual attribute \param Attr based on the type of attribute and
288 void DIEHash::hashAttribute(AttrEntry Attr) {
289 const DIEValue *Value = Attr.Val;
290 const DIEAbbrevData *Desc = Attr.Desc;
293 // ... An attribute that refers to another type entry T is processed as
295 // a) If T is in the list of [previously hashed types], use the letter 'R' as
296 // the marker and use the unsigned LEB128 encoding of [the index of T in the
297 // list] as the attribute value; otherwise,
299 // [TODO: implement clause (a)]
301 if (const DIEEntry *EntryAttr = dyn_cast<DIEEntry>(Value)) {
302 DIE *Entry = EntryAttr->getEntry();
304 // b) use the letter 'T' as a the marker, ...
307 addULEB128(Desc->getAttribute());
309 // ... process the type T recursively by performing Steps 2 through 7, and
310 // use the result as the attribute value.
315 // Other attribute values use the letter 'A' as the marker, ...
318 addULEB128(Desc->getAttribute());
320 // ... and the value consists of the form code (encoded as an unsigned LEB128
321 // value) followed by the encoding of the value according to the form code. To
322 // ensure reproducibility of the signature, the set of forms used in the
323 // signature computation is limited to the following: DW_FORM_sdata,
324 // DW_FORM_flag, DW_FORM_string, and DW_FORM_block.
325 switch (Desc->getForm()) {
326 case dwarf::DW_FORM_string:
328 "Add support for DW_FORM_string if we ever start emitting them again");
329 case dwarf::DW_FORM_strp:
330 addULEB128(dwarf::DW_FORM_string);
331 addString(cast<DIEString>(Value)->getString());
333 case dwarf::DW_FORM_data1:
334 case dwarf::DW_FORM_data2:
335 case dwarf::DW_FORM_data4:
336 case dwarf::DW_FORM_data8:
337 case dwarf::DW_FORM_udata:
338 addULEB128(dwarf::DW_FORM_sdata);
339 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
341 // TODO: Add support for additional forms.
345 // Go through the attributes from \param Attrs in the order specified in 7.27.4
347 void DIEHash::hashAttributes(const DIEAttrs &Attrs) {
348 #define ADD_ATTR(ATTR) \
351 hashAttribute(ATTR); \
354 ADD_ATTR(Attrs.DW_AT_name);
355 ADD_ATTR(Attrs.DW_AT_accessibility);
356 ADD_ATTR(Attrs.DW_AT_address_class);
357 ADD_ATTR(Attrs.DW_AT_allocated);
358 ADD_ATTR(Attrs.DW_AT_artificial);
359 ADD_ATTR(Attrs.DW_AT_associated);
360 ADD_ATTR(Attrs.DW_AT_binary_scale);
361 ADD_ATTR(Attrs.DW_AT_bit_offset);
362 ADD_ATTR(Attrs.DW_AT_bit_size);
363 ADD_ATTR(Attrs.DW_AT_bit_stride);
364 ADD_ATTR(Attrs.DW_AT_byte_size);
365 ADD_ATTR(Attrs.DW_AT_byte_stride);
366 ADD_ATTR(Attrs.DW_AT_const_expr);
367 ADD_ATTR(Attrs.DW_AT_const_value);
368 ADD_ATTR(Attrs.DW_AT_containing_type);
369 ADD_ATTR(Attrs.DW_AT_count);
370 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
371 ADD_ATTR(Attrs.DW_AT_data_location);
372 ADD_ATTR(Attrs.DW_AT_data_member_location);
373 ADD_ATTR(Attrs.DW_AT_decimal_scale);
374 ADD_ATTR(Attrs.DW_AT_decimal_sign);
375 ADD_ATTR(Attrs.DW_AT_default_value);
376 ADD_ATTR(Attrs.DW_AT_digit_count);
377 ADD_ATTR(Attrs.DW_AT_discr);
378 ADD_ATTR(Attrs.DW_AT_discr_list);
379 ADD_ATTR(Attrs.DW_AT_discr_value);
380 ADD_ATTR(Attrs.DW_AT_encoding);
381 ADD_ATTR(Attrs.DW_AT_enum_class);
382 ADD_ATTR(Attrs.DW_AT_endianity);
383 ADD_ATTR(Attrs.DW_AT_explicit);
384 ADD_ATTR(Attrs.DW_AT_is_optional);
385 ADD_ATTR(Attrs.DW_AT_location);
386 ADD_ATTR(Attrs.DW_AT_lower_bound);
387 ADD_ATTR(Attrs.DW_AT_mutable);
388 ADD_ATTR(Attrs.DW_AT_ordering);
389 ADD_ATTR(Attrs.DW_AT_picture_string);
390 ADD_ATTR(Attrs.DW_AT_prototyped);
391 ADD_ATTR(Attrs.DW_AT_small);
392 ADD_ATTR(Attrs.DW_AT_segment);
393 ADD_ATTR(Attrs.DW_AT_string_length);
394 ADD_ATTR(Attrs.DW_AT_threads_scaled);
395 ADD_ATTR(Attrs.DW_AT_upper_bound);
396 ADD_ATTR(Attrs.DW_AT_use_location);
397 ADD_ATTR(Attrs.DW_AT_use_UTF8);
398 ADD_ATTR(Attrs.DW_AT_variable_parameter);
399 ADD_ATTR(Attrs.DW_AT_virtuality);
400 ADD_ATTR(Attrs.DW_AT_visibility);
401 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
402 ADD_ATTR(Attrs.DW_AT_type);
404 // FIXME: Add the extended attributes.
407 // Add all of the attributes for \param Die to the hash.
408 void DIEHash::addAttributes(DIE *Die) {
410 collectAttributes(Die, Attrs);
411 hashAttributes(Attrs);
414 // Compute the hash of a DIE. This is based on the type signature computation
415 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
416 // flattened description of the DIE.
417 void DIEHash::computeHash(DIE *Die) {
419 // Append the letter 'D', followed by the DWARF tag of the DIE.
421 addULEB128(Die->getTag());
423 // Add each of the attributes of the DIE.
426 // Then hash each of the children of the DIE.
427 for (std::vector<DIE *>::const_iterator I = Die->getChildren().begin(),
428 E = Die->getChildren().end();
432 // Following the last (or if there are no children), append a zero byte.
433 Hash.update(makeArrayRef((uint8_t)'\0'));
436 /// This is based on the type signature computation given in section 7.27 of the
437 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
438 /// with the exception that we are hashing only the context and the name of the
440 uint64_t DIEHash::computeDIEODRSignature(DIE *Die) {
442 // Add the contexts to the hash. We won't be computing the ODR hash for
443 // function local types so it's safe to use the generic context hashing
445 // FIXME: If we figure out how to account for linkage in some way we could
446 // actually do this with a slight modification to the parent hash algorithm.
447 DIE *Parent = Die->getParent();
449 addParentContext(Parent);
451 // Add the current DIE information.
453 // Add the DWARF tag of the DIE.
454 addULEB128(Die->getTag());
456 // Add the name of the type to the hash.
457 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
459 // Now get the result.
460 MD5::MD5Result Result;
463 // ... take the least significant 8 bytes and return those. Our MD5
464 // implementation always returns its results in little endian, swap bytes
466 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
469 /// This is based on the type signature computation given in section 7.27 of the
470 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
471 /// with the inclusion of the full CU and all top level CU entities.
472 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
473 uint64_t DIEHash::computeCUSignature(DIE *Die) {
478 // Now return the result.
479 MD5::MD5Result Result;
482 // ... take the least significant 8 bytes and return those. Our MD5
483 // implementation always returns its results in little endian, swap bytes
485 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
488 /// This is based on the type signature computation given in section 7.27 of the
489 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
490 /// with the inclusion of additional forms not specifically called out in the
492 uint64_t DIEHash::computeTypeSignature(DIE *Die) {
494 if (DIE *Parent = Die->getParent())
495 addParentContext(Parent);
500 // Now return the result.
501 MD5::MD5Result Result;
504 // ... take the least significant 8 bytes and return those. Our MD5
505 // implementation always returns its results in little endian, swap bytes
507 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);