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 /// \brief Including \p Parent adds the context of Parent to the hash..
72 void DIEHash::addParentContext(DIE *Parent) {
74 DEBUG(dbgs() << "Adding parent context to hash...\n");
76 // [7.27.2] For each surrounding type or namespace beginning with the
77 // outermost such construct...
78 SmallVector<DIE *, 1> Parents;
79 while (Parent->getTag() != dwarf::DW_TAG_compile_unit) {
80 Parents.push_back(Parent);
81 Parent = Parent->getParent();
84 // Reverse iterate over our list to go from the outermost construct to the
86 for (SmallVectorImpl<DIE *>::reverse_iterator I = Parents.rbegin(),
91 // ... Append the letter "C" to the sequence...
94 // ... Followed by the DWARF tag of the construct...
95 addULEB128(Die->getTag());
97 // ... Then the name, taken from the DW_AT_name attribute.
98 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
99 DEBUG(dbgs() << "... adding context: " << Name << "\n");
105 // Collect all of the attributes for a particular DIE in single structure.
106 void DIEHash::collectAttributes(DIE *Die, DIEAttrs &Attrs) {
107 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
108 const DIEAbbrev &Abbrevs = Die->getAbbrev();
110 #define COLLECT_ATTR(NAME) \
111 Attrs.NAME.Val = Values[i]; \
112 Attrs.NAME.Desc = &Abbrevs.getData()[i];
114 for (size_t i = 0, e = Values.size(); i != e; ++i) {
115 DEBUG(dbgs() << "Attribute: "
116 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
118 switch (Abbrevs.getData()[i].getAttribute()) {
119 case dwarf::DW_AT_name:
120 COLLECT_ATTR(DW_AT_name);
122 case dwarf::DW_AT_accessibility:
123 COLLECT_ATTR(DW_AT_accessibility)
125 case dwarf::DW_AT_address_class:
126 COLLECT_ATTR(DW_AT_address_class)
128 case dwarf::DW_AT_allocated:
129 COLLECT_ATTR(DW_AT_allocated)
131 case dwarf::DW_AT_artificial:
132 COLLECT_ATTR(DW_AT_artificial)
134 case dwarf::DW_AT_associated:
135 COLLECT_ATTR(DW_AT_associated)
137 case dwarf::DW_AT_binary_scale:
138 COLLECT_ATTR(DW_AT_binary_scale)
140 case dwarf::DW_AT_bit_offset:
141 COLLECT_ATTR(DW_AT_bit_offset)
143 case dwarf::DW_AT_bit_size:
144 COLLECT_ATTR(DW_AT_bit_size)
146 case dwarf::DW_AT_bit_stride:
147 COLLECT_ATTR(DW_AT_bit_stride)
149 case dwarf::DW_AT_byte_size:
150 COLLECT_ATTR(DW_AT_byte_size)
152 case dwarf::DW_AT_byte_stride:
153 COLLECT_ATTR(DW_AT_byte_stride)
155 case dwarf::DW_AT_const_expr:
156 COLLECT_ATTR(DW_AT_const_expr)
158 case dwarf::DW_AT_const_value:
159 COLLECT_ATTR(DW_AT_const_value)
161 case dwarf::DW_AT_containing_type:
162 COLLECT_ATTR(DW_AT_containing_type)
164 case dwarf::DW_AT_count:
165 COLLECT_ATTR(DW_AT_count)
167 case dwarf::DW_AT_data_bit_offset:
168 COLLECT_ATTR(DW_AT_data_bit_offset)
170 case dwarf::DW_AT_data_location:
171 COLLECT_ATTR(DW_AT_data_location)
173 case dwarf::DW_AT_data_member_location:
174 COLLECT_ATTR(DW_AT_data_member_location)
176 case dwarf::DW_AT_decimal_scale:
177 COLLECT_ATTR(DW_AT_decimal_scale)
179 case dwarf::DW_AT_decimal_sign:
180 COLLECT_ATTR(DW_AT_decimal_sign)
182 case dwarf::DW_AT_default_value:
183 COLLECT_ATTR(DW_AT_default_value)
185 case dwarf::DW_AT_digit_count:
186 COLLECT_ATTR(DW_AT_digit_count)
188 case dwarf::DW_AT_discr:
189 COLLECT_ATTR(DW_AT_discr)
191 case dwarf::DW_AT_discr_list:
192 COLLECT_ATTR(DW_AT_discr_list)
194 case dwarf::DW_AT_discr_value:
195 COLLECT_ATTR(DW_AT_discr_value)
197 case dwarf::DW_AT_encoding:
198 COLLECT_ATTR(DW_AT_encoding)
200 case dwarf::DW_AT_enum_class:
201 COLLECT_ATTR(DW_AT_enum_class)
203 case dwarf::DW_AT_endianity:
204 COLLECT_ATTR(DW_AT_endianity)
206 case dwarf::DW_AT_explicit:
207 COLLECT_ATTR(DW_AT_explicit)
209 case dwarf::DW_AT_is_optional:
210 COLLECT_ATTR(DW_AT_is_optional)
212 case dwarf::DW_AT_location:
213 COLLECT_ATTR(DW_AT_location)
215 case dwarf::DW_AT_lower_bound:
216 COLLECT_ATTR(DW_AT_lower_bound)
218 case dwarf::DW_AT_mutable:
219 COLLECT_ATTR(DW_AT_mutable)
221 case dwarf::DW_AT_ordering:
222 COLLECT_ATTR(DW_AT_ordering)
224 case dwarf::DW_AT_picture_string:
225 COLLECT_ATTR(DW_AT_picture_string)
227 case dwarf::DW_AT_prototyped:
228 COLLECT_ATTR(DW_AT_prototyped)
230 case dwarf::DW_AT_small:
231 COLLECT_ATTR(DW_AT_small)
233 case dwarf::DW_AT_segment:
234 COLLECT_ATTR(DW_AT_segment)
236 case dwarf::DW_AT_string_length:
237 COLLECT_ATTR(DW_AT_string_length)
239 case dwarf::DW_AT_threads_scaled:
240 COLLECT_ATTR(DW_AT_threads_scaled)
242 case dwarf::DW_AT_upper_bound:
243 COLLECT_ATTR(DW_AT_upper_bound)
245 case dwarf::DW_AT_use_location:
246 COLLECT_ATTR(DW_AT_use_location)
248 case dwarf::DW_AT_use_UTF8:
249 COLLECT_ATTR(DW_AT_use_UTF8)
251 case dwarf::DW_AT_variable_parameter:
252 COLLECT_ATTR(DW_AT_variable_parameter)
254 case dwarf::DW_AT_virtuality:
255 COLLECT_ATTR(DW_AT_virtuality)
257 case dwarf::DW_AT_visibility:
258 COLLECT_ATTR(DW_AT_visibility)
260 case dwarf::DW_AT_vtable_elem_location:
261 COLLECT_ATTR(DW_AT_vtable_elem_location)
269 // Hash an individual attribute \param Attr based on the type of attribute and
271 void DIEHash::hashAttribute(AttrEntry Attr) {
272 const DIEValue *Value = Attr.Val;
273 const DIEAbbrevData *Desc = Attr.Desc;
275 // TODO: Add support for types.
277 // Add the letter A to the hash.
280 // Then the attribute code and form.
281 addULEB128(Desc->getAttribute());
282 addULEB128(Desc->getForm());
284 // TODO: Add support for additional forms.
285 switch (Desc->getForm()) {
286 // TODO: We'll want to add DW_FORM_string here if we start emitting them
288 case dwarf::DW_FORM_strp:
289 addString(cast<DIEString>(Value)->getString());
291 case dwarf::DW_FORM_data1:
292 case dwarf::DW_FORM_data2:
293 case dwarf::DW_FORM_data4:
294 case dwarf::DW_FORM_data8:
295 case dwarf::DW_FORM_udata:
296 addULEB128(cast<DIEInteger>(Value)->getValue());
301 // Go through the attributes from \param Attrs in the order specified in 7.27.4
303 void DIEHash::hashAttributes(const DIEAttrs &Attrs) {
304 #define ADD_ATTR(ATTR) \
307 hashAttribute(ATTR); \
310 ADD_ATTR(Attrs.DW_AT_name);
311 ADD_ATTR(Attrs.DW_AT_accessibility);
312 ADD_ATTR(Attrs.DW_AT_address_class);
313 ADD_ATTR(Attrs.DW_AT_allocated);
314 ADD_ATTR(Attrs.DW_AT_artificial);
315 ADD_ATTR(Attrs.DW_AT_associated);
316 ADD_ATTR(Attrs.DW_AT_binary_scale);
317 ADD_ATTR(Attrs.DW_AT_bit_offset);
318 ADD_ATTR(Attrs.DW_AT_bit_size);
319 ADD_ATTR(Attrs.DW_AT_bit_stride);
320 ADD_ATTR(Attrs.DW_AT_byte_size);
321 ADD_ATTR(Attrs.DW_AT_byte_stride);
322 ADD_ATTR(Attrs.DW_AT_const_expr);
323 ADD_ATTR(Attrs.DW_AT_const_value);
324 ADD_ATTR(Attrs.DW_AT_containing_type);
325 ADD_ATTR(Attrs.DW_AT_count);
326 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
327 ADD_ATTR(Attrs.DW_AT_data_location);
328 ADD_ATTR(Attrs.DW_AT_data_member_location);
329 ADD_ATTR(Attrs.DW_AT_decimal_scale);
330 ADD_ATTR(Attrs.DW_AT_decimal_sign);
331 ADD_ATTR(Attrs.DW_AT_default_value);
332 ADD_ATTR(Attrs.DW_AT_digit_count);
333 ADD_ATTR(Attrs.DW_AT_discr);
334 ADD_ATTR(Attrs.DW_AT_discr_list);
335 ADD_ATTR(Attrs.DW_AT_discr_value);
336 ADD_ATTR(Attrs.DW_AT_encoding);
337 ADD_ATTR(Attrs.DW_AT_enum_class);
338 ADD_ATTR(Attrs.DW_AT_endianity);
339 ADD_ATTR(Attrs.DW_AT_explicit);
340 ADD_ATTR(Attrs.DW_AT_is_optional);
341 ADD_ATTR(Attrs.DW_AT_location);
342 ADD_ATTR(Attrs.DW_AT_lower_bound);
343 ADD_ATTR(Attrs.DW_AT_mutable);
344 ADD_ATTR(Attrs.DW_AT_ordering);
345 ADD_ATTR(Attrs.DW_AT_picture_string);
346 ADD_ATTR(Attrs.DW_AT_prototyped);
347 ADD_ATTR(Attrs.DW_AT_small);
348 ADD_ATTR(Attrs.DW_AT_segment);
349 ADD_ATTR(Attrs.DW_AT_string_length);
350 ADD_ATTR(Attrs.DW_AT_threads_scaled);
351 ADD_ATTR(Attrs.DW_AT_upper_bound);
352 ADD_ATTR(Attrs.DW_AT_use_location);
353 ADD_ATTR(Attrs.DW_AT_use_UTF8);
354 ADD_ATTR(Attrs.DW_AT_variable_parameter);
355 ADD_ATTR(Attrs.DW_AT_virtuality);
356 ADD_ATTR(Attrs.DW_AT_visibility);
357 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
359 // FIXME: Add the extended attributes.
362 // Add all of the attributes for \param Die to the hash.
363 void DIEHash::addAttributes(DIE *Die) {
365 memset(&Attrs, 0, sizeof(Attrs));
366 collectAttributes(Die, Attrs);
367 hashAttributes(Attrs);
370 // Compute the hash of a DIE. This is based on the type signature computation
371 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
372 // flattened description of the DIE.
373 void DIEHash::computeHash(DIE *Die) {
375 // Append the letter 'D', followed by the DWARF tag of the DIE.
377 addULEB128(Die->getTag());
379 // Add each of the attributes of the DIE.
382 // Then hash each of the children of the DIE.
383 for (std::vector<DIE *>::const_iterator I = Die->getChildren().begin(),
384 E = Die->getChildren().end();
389 /// This is based on the type signature computation given in section 7.27 of the
390 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
391 /// with the exception that we are hashing only the context and the name of the
393 uint64_t DIEHash::computeDIEODRSignature(DIE *Die) {
395 // Add the contexts to the hash. We won't be computing the ODR hash for
396 // function local types so it's safe to use the generic context hashing
398 // FIXME: If we figure out how to account for linkage in some way we could
399 // actually do this with a slight modification to the parent hash algorithm.
400 DIE *Parent = Die->getParent();
402 addParentContext(Parent);
404 // Add the current DIE information.
406 // Add the DWARF tag of the DIE.
407 addULEB128(Die->getTag());
409 // Add the name of the type to the hash.
410 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
412 // Now get the result.
413 MD5::MD5Result Result;
416 // ... take the least significant 8 bytes and return those. Our MD5
417 // implementation always returns its results in little endian, swap bytes
419 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
422 /// This is based on the type signature computation given in section 7.27 of the
423 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
424 /// with the inclusion of the full CU and all top level CU entities.
425 uint64_t DIEHash::computeCUSignature(DIE *Die) {
430 // Now return the result.
431 MD5::MD5Result Result;
434 // ... take the least significant 8 bytes and return those. Our MD5
435 // implementation always returns its results in little endian, swap bytes
437 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);