blob: 95eca90ef04639bf99e8375a4d7b88f1656e7a06 [file] [log] [blame]
Eric Christopher0d27ca12013-08-08 23:45:55 +00001//===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for DWARF4 hashing of DIEs.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
Eric Christopher0d27ca12013-08-08 23:45:55 +000016#include "DIEHash.h"
David Blaikiea9a8f0f2013-11-13 18:07:27 +000017
18#include "DIE.h"
Eric Christopher0d27ca12013-08-08 23:45:55 +000019#include "DwarfCompileUnit.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/Dwarf.h"
24#include "llvm/Support/Endian.h"
25#include "llvm/Support/MD5.h"
26#include "llvm/Support/raw_ostream.h"
27
28using namespace llvm;
29
30/// \brief Grabs the string in whichever attribute is passed in and returns
31/// a reference to it.
David Blaikie851aa942013-10-24 17:51:43 +000032static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
33 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
34 const DIEAbbrev &Abbrevs = Die.getAbbrev();
Eric Christopher0d27ca12013-08-08 23:45:55 +000035
36 // Iterate through all the attributes until we find the one we're
37 // looking for, if we can't find it return an empty string.
38 for (size_t i = 0; i < Values.size(); ++i) {
39 if (Abbrevs.getData()[i].getAttribute() == Attr) {
40 DIEValue *V = Values[i];
41 assert(isa<DIEString>(V) && "String requested. Not a string.");
42 DIEString *S = cast<DIEString>(V);
43 return S->getString();
44 }
45 }
46 return StringRef("");
47}
48
49/// \brief Adds the string in \p Str to the hash. This also hashes
50/// a trailing NULL with the string.
51void DIEHash::addString(StringRef Str) {
52 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
53 Hash.update(Str);
54 Hash.update(makeArrayRef((uint8_t)'\0'));
55}
56
57// FIXME: The LEB128 routines are copied and only slightly modified out of
58// LEB128.h.
59
60/// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
61void DIEHash::addULEB128(uint64_t Value) {
62 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
63 do {
64 uint8_t Byte = Value & 0x7f;
65 Value >>= 7;
66 if (Value != 0)
67 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
68 Hash.update(Byte);
69 } while (Value != 0);
70}
71
David Blaikiec0987082013-10-16 23:36:20 +000072void DIEHash::addSLEB128(int64_t Value) {
73 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
74 bool More;
75 do {
76 uint8_t Byte = Value & 0x7f;
77 Value >>= 7;
78 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
79 ((Value == -1) && ((Byte & 0x40) != 0))));
80 if (More)
81 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
82 Hash.update(Byte);
83 } while (More);
84}
85
Eric Christopher0d27ca12013-08-08 23:45:55 +000086/// \brief Including \p Parent adds the context of Parent to the hash..
David Blaikie377e8322013-10-24 18:29:03 +000087void DIEHash::addParentContext(const DIE &Parent) {
Eric Christopher0d27ca12013-08-08 23:45:55 +000088
89 DEBUG(dbgs() << "Adding parent context to hash...\n");
90
91 // [7.27.2] For each surrounding type or namespace beginning with the
92 // outermost such construct...
David Blaikie377e8322013-10-24 18:29:03 +000093 SmallVector<const DIE *, 1> Parents;
94 const DIE *Cur = &Parent;
95 while (Cur->getTag() != dwarf::DW_TAG_compile_unit) {
96 Parents.push_back(Cur);
97 Cur = Cur->getParent();
Eric Christopher0d27ca12013-08-08 23:45:55 +000098 }
99
100 // Reverse iterate over our list to go from the outermost construct to the
101 // innermost.
David Blaikie377e8322013-10-24 18:29:03 +0000102 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
103 E = Parents.rend();
Eric Christopher0d27ca12013-08-08 23:45:55 +0000104 I != E; ++I) {
David Blaikie377e8322013-10-24 18:29:03 +0000105 const DIE &Die = **I;
Eric Christopher0d27ca12013-08-08 23:45:55 +0000106
107 // ... Append the letter "C" to the sequence...
108 addULEB128('C');
109
110 // ... Followed by the DWARF tag of the construct...
David Blaikie377e8322013-10-24 18:29:03 +0000111 addULEB128(Die.getTag());
Eric Christopher0d27ca12013-08-08 23:45:55 +0000112
113 // ... Then the name, taken from the DW_AT_name attribute.
David Blaikie377e8322013-10-24 18:29:03 +0000114 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
Eric Christopher0d27ca12013-08-08 23:45:55 +0000115 DEBUG(dbgs() << "... adding context: " << Name << "\n");
116 if (!Name.empty())
117 addString(Name);
118 }
119}
120
Eric Christopher0710bfa2013-08-13 01:21:55 +0000121// Collect all of the attributes for a particular DIE in single structure.
David Blaikie377e8322013-10-24 18:29:03 +0000122void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
123 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
124 const DIEAbbrev &Abbrevs = Die.getAbbrev();
Eric Christopher0710bfa2013-08-13 01:21:55 +0000125
126#define COLLECT_ATTR(NAME) \
David Blaikie0d87c202013-10-17 22:14:08 +0000127 case dwarf::NAME: \
128 Attrs.NAME.Val = Values[i]; \
129 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
130 break
Eric Christopher0710bfa2013-08-13 01:21:55 +0000131
132 for (size_t i = 0, e = Values.size(); i != e; ++i) {
133 DEBUG(dbgs() << "Attribute: "
134 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
135 << " added.\n");
136 switch (Abbrevs.getData()[i].getAttribute()) {
David Blaikie0d87c202013-10-17 22:14:08 +0000137 COLLECT_ATTR(DW_AT_name);
138 COLLECT_ATTR(DW_AT_accessibility);
139 COLLECT_ATTR(DW_AT_address_class);
140 COLLECT_ATTR(DW_AT_allocated);
141 COLLECT_ATTR(DW_AT_artificial);
142 COLLECT_ATTR(DW_AT_associated);
143 COLLECT_ATTR(DW_AT_binary_scale);
144 COLLECT_ATTR(DW_AT_bit_offset);
145 COLLECT_ATTR(DW_AT_bit_size);
146 COLLECT_ATTR(DW_AT_bit_stride);
147 COLLECT_ATTR(DW_AT_byte_size);
148 COLLECT_ATTR(DW_AT_byte_stride);
149 COLLECT_ATTR(DW_AT_const_expr);
150 COLLECT_ATTR(DW_AT_const_value);
151 COLLECT_ATTR(DW_AT_containing_type);
152 COLLECT_ATTR(DW_AT_count);
153 COLLECT_ATTR(DW_AT_data_bit_offset);
154 COLLECT_ATTR(DW_AT_data_location);
155 COLLECT_ATTR(DW_AT_data_member_location);
156 COLLECT_ATTR(DW_AT_decimal_scale);
157 COLLECT_ATTR(DW_AT_decimal_sign);
158 COLLECT_ATTR(DW_AT_default_value);
159 COLLECT_ATTR(DW_AT_digit_count);
160 COLLECT_ATTR(DW_AT_discr);
161 COLLECT_ATTR(DW_AT_discr_list);
162 COLLECT_ATTR(DW_AT_discr_value);
163 COLLECT_ATTR(DW_AT_encoding);
164 COLLECT_ATTR(DW_AT_enum_class);
165 COLLECT_ATTR(DW_AT_endianity);
166 COLLECT_ATTR(DW_AT_explicit);
167 COLLECT_ATTR(DW_AT_is_optional);
168 COLLECT_ATTR(DW_AT_location);
169 COLLECT_ATTR(DW_AT_lower_bound);
170 COLLECT_ATTR(DW_AT_mutable);
171 COLLECT_ATTR(DW_AT_ordering);
172 COLLECT_ATTR(DW_AT_picture_string);
173 COLLECT_ATTR(DW_AT_prototyped);
174 COLLECT_ATTR(DW_AT_small);
175 COLLECT_ATTR(DW_AT_segment);
176 COLLECT_ATTR(DW_AT_string_length);
177 COLLECT_ATTR(DW_AT_threads_scaled);
178 COLLECT_ATTR(DW_AT_upper_bound);
179 COLLECT_ATTR(DW_AT_use_location);
180 COLLECT_ATTR(DW_AT_use_UTF8);
181 COLLECT_ATTR(DW_AT_variable_parameter);
182 COLLECT_ATTR(DW_AT_virtuality);
183 COLLECT_ATTR(DW_AT_visibility);
184 COLLECT_ATTR(DW_AT_vtable_elem_location);
185 COLLECT_ATTR(DW_AT_type);
Eric Christopher0710bfa2013-08-13 01:21:55 +0000186 default:
187 break;
188 }
189 }
190}
191
David Blaikie851aa942013-10-24 17:51:43 +0000192void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
193 const DIE &Entry, StringRef Name) {
194 // append the letter 'N'
195 addULEB128('N');
196
197 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
198 addULEB128(Attribute);
199
200 // the context of the tag,
David Blaikie377e8322013-10-24 18:29:03 +0000201 if (const DIE *Parent = Entry.getParent())
202 addParentContext(*Parent);
David Blaikie851aa942013-10-24 17:51:43 +0000203
204 // the letter 'E',
205 addULEB128('E');
206
207 // and the name of the type.
208 addString(Name);
209
210 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
211 // here's the relevant spec text to implement:
212 //
213 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
214 // the context is omitted and the name to be used is the ABI-specific name
215 // of the subprogram (e.g., the mangled linker name).
216}
217
218void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
219 unsigned DieNumber) {
220 // a) If T is in the list of [previously hashed types], use the letter
221 // 'R' as the marker
222 addULEB128('R');
223
224 addULEB128(Attribute);
225
226 // and use the unsigned LEB128 encoding of [the index of T in the
227 // list] as the attribute value;
228 addULEB128(DieNumber);
229}
230
231void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
David Blaikie377e8322013-10-24 18:29:03 +0000232 const DIE &Entry) {
David Blaikie851aa942013-10-24 17:51:43 +0000233 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
234 "tags. Add support here when there's "
235 "a use case");
236 // Step 5
237 // If the tag in Step 3 is one of [the below tags]
238 if ((Tag == dwarf::DW_TAG_pointer_type ||
239 Tag == dwarf::DW_TAG_reference_type ||
240 Tag == dwarf::DW_TAG_rvalue_reference_type ||
241 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
242 // and the referenced type (via the [below attributes])
243 // FIXME: This seems overly restrictive, and causes hash mismatches
244 // there's a decl/def difference in the containing type of a
245 // ptr_to_member_type, but it's what DWARF says, for some reason.
246 Attribute == dwarf::DW_AT_type) {
David Blaikie1d7d8da2013-10-24 17:53:58 +0000247 // ... has a DW_AT_name attribute,
248 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
249 if (!Name.empty()) {
250 hashShallowTypeReference(Attribute, Entry, Name);
251 return;
252 }
David Blaikie851aa942013-10-24 17:51:43 +0000253 }
254
255 unsigned &DieNumber = Numbering[&Entry];
256 if (DieNumber) {
257 hashRepeatedTypeReference(Attribute, DieNumber);
258 return;
259 }
260
261 // otherwise, b) use the letter 'T' as a the marker, ...
262 addULEB128('T');
263
264 addULEB128(Attribute);
265
266 // ... process the type T recursively by performing Steps 2 through 7, and
267 // use the result as the attribute value.
268 DieNumber = Numbering.size();
David Blaikie377e8322013-10-24 18:29:03 +0000269 computeHash(Entry);
David Blaikie851aa942013-10-24 17:51:43 +0000270}
271
Eric Christopher0710bfa2013-08-13 01:21:55 +0000272// Hash an individual attribute \param Attr based on the type of attribute and
273// the form.
David Blaikief1545a22013-10-21 22:36:50 +0000274void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
Eric Christopher0710bfa2013-08-13 01:21:55 +0000275 const DIEValue *Value = Attr.Val;
276 const DIEAbbrevData *Desc = Attr.Desc;
David Blaikief1962082013-10-22 18:14:41 +0000277 dwarf::Attribute Attribute = Desc->getAttribute();
Eric Christopher0710bfa2013-08-13 01:21:55 +0000278
David Blaikief1545a22013-10-21 22:36:50 +0000279 // 7.27 Step 3
David Blaikie47f66d52013-10-17 22:07:09 +0000280 // ... An attribute that refers to another type entry T is processed as
281 // follows:
David Blaikie47f66d52013-10-17 22:07:09 +0000282 if (const DIEEntry *EntryAttr = dyn_cast<DIEEntry>(Value)) {
David Blaikie851aa942013-10-24 17:51:43 +0000283 hashDIEEntry(Attribute, Tag, *EntryAttr->getEntry());
David Blaikie47f66d52013-10-17 22:07:09 +0000284 return;
285 }
286
287 // Other attribute values use the letter 'A' as the marker, ...
Eric Christopher0710bfa2013-08-13 01:21:55 +0000288 addULEB128('A');
289
David Blaikief1962082013-10-22 18:14:41 +0000290 addULEB128(Attribute);
David Blaikiec0987082013-10-16 23:36:20 +0000291
David Blaikie47f66d52013-10-17 22:07:09 +0000292 // ... and the value consists of the form code (encoded as an unsigned LEB128
293 // value) followed by the encoding of the value according to the form code. To
294 // ensure reproducibility of the signature, the set of forms used in the
David Blaikiec0987082013-10-16 23:36:20 +0000295 // signature computation is limited to the following: DW_FORM_sdata,
296 // DW_FORM_flag, DW_FORM_string, and DW_FORM_block.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000297 switch (Desc->getForm()) {
David Blaikiec0987082013-10-16 23:36:20 +0000298 case dwarf::DW_FORM_string:
299 llvm_unreachable(
300 "Add support for DW_FORM_string if we ever start emitting them again");
David Blaikieda39dd32013-10-21 16:37:22 +0000301 case dwarf::DW_FORM_GNU_str_index:
Eric Christopher0710bfa2013-08-13 01:21:55 +0000302 case dwarf::DW_FORM_strp:
David Blaikiec0987082013-10-16 23:36:20 +0000303 addULEB128(dwarf::DW_FORM_string);
Eric Christopher0710bfa2013-08-13 01:21:55 +0000304 addString(cast<DIEString>(Value)->getString());
305 break;
Eric Christopher7ced4fa2013-08-28 00:10:38 +0000306 case dwarf::DW_FORM_data1:
307 case dwarf::DW_FORM_data2:
308 case dwarf::DW_FORM_data4:
309 case dwarf::DW_FORM_data8:
310 case dwarf::DW_FORM_udata:
David Blaikiec0987082013-10-16 23:36:20 +0000311 addULEB128(dwarf::DW_FORM_sdata);
312 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
Eric Christopher7ced4fa2013-08-28 00:10:38 +0000313 break;
David Blaikieda39dd32013-10-21 16:37:22 +0000314 default:
315 llvm_unreachable("Add support for additional forms");
Eric Christopher0710bfa2013-08-13 01:21:55 +0000316 }
317}
318
319// Go through the attributes from \param Attrs in the order specified in 7.27.4
320// and hash them.
David Blaikief1545a22013-10-21 22:36:50 +0000321void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
Eric Christopher0710bfa2013-08-13 01:21:55 +0000322#define ADD_ATTR(ATTR) \
323 { \
324 if (ATTR.Val != 0) \
David Blaikief1545a22013-10-21 22:36:50 +0000325 hashAttribute(ATTR, Tag); \
Eric Christopher0710bfa2013-08-13 01:21:55 +0000326 }
327
Eric Christopher0710bfa2013-08-13 01:21:55 +0000328 ADD_ATTR(Attrs.DW_AT_name);
Eric Christopherbd18c8d2013-09-03 20:00:20 +0000329 ADD_ATTR(Attrs.DW_AT_accessibility);
330 ADD_ATTR(Attrs.DW_AT_address_class);
331 ADD_ATTR(Attrs.DW_AT_allocated);
332 ADD_ATTR(Attrs.DW_AT_artificial);
333 ADD_ATTR(Attrs.DW_AT_associated);
334 ADD_ATTR(Attrs.DW_AT_binary_scale);
335 ADD_ATTR(Attrs.DW_AT_bit_offset);
336 ADD_ATTR(Attrs.DW_AT_bit_size);
337 ADD_ATTR(Attrs.DW_AT_bit_stride);
338 ADD_ATTR(Attrs.DW_AT_byte_size);
339 ADD_ATTR(Attrs.DW_AT_byte_stride);
340 ADD_ATTR(Attrs.DW_AT_const_expr);
341 ADD_ATTR(Attrs.DW_AT_const_value);
342 ADD_ATTR(Attrs.DW_AT_containing_type);
343 ADD_ATTR(Attrs.DW_AT_count);
344 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
345 ADD_ATTR(Attrs.DW_AT_data_location);
346 ADD_ATTR(Attrs.DW_AT_data_member_location);
347 ADD_ATTR(Attrs.DW_AT_decimal_scale);
348 ADD_ATTR(Attrs.DW_AT_decimal_sign);
349 ADD_ATTR(Attrs.DW_AT_default_value);
350 ADD_ATTR(Attrs.DW_AT_digit_count);
351 ADD_ATTR(Attrs.DW_AT_discr);
352 ADD_ATTR(Attrs.DW_AT_discr_list);
353 ADD_ATTR(Attrs.DW_AT_discr_value);
354 ADD_ATTR(Attrs.DW_AT_encoding);
355 ADD_ATTR(Attrs.DW_AT_enum_class);
356 ADD_ATTR(Attrs.DW_AT_endianity);
357 ADD_ATTR(Attrs.DW_AT_explicit);
358 ADD_ATTR(Attrs.DW_AT_is_optional);
359 ADD_ATTR(Attrs.DW_AT_location);
360 ADD_ATTR(Attrs.DW_AT_lower_bound);
361 ADD_ATTR(Attrs.DW_AT_mutable);
362 ADD_ATTR(Attrs.DW_AT_ordering);
363 ADD_ATTR(Attrs.DW_AT_picture_string);
364 ADD_ATTR(Attrs.DW_AT_prototyped);
365 ADD_ATTR(Attrs.DW_AT_small);
366 ADD_ATTR(Attrs.DW_AT_segment);
367 ADD_ATTR(Attrs.DW_AT_string_length);
368 ADD_ATTR(Attrs.DW_AT_threads_scaled);
369 ADD_ATTR(Attrs.DW_AT_upper_bound);
370 ADD_ATTR(Attrs.DW_AT_use_location);
371 ADD_ATTR(Attrs.DW_AT_use_UTF8);
372 ADD_ATTR(Attrs.DW_AT_variable_parameter);
373 ADD_ATTR(Attrs.DW_AT_virtuality);
374 ADD_ATTR(Attrs.DW_AT_visibility);
375 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
David Blaikie47f66d52013-10-17 22:07:09 +0000376 ADD_ATTR(Attrs.DW_AT_type);
Eric Christopherbd18c8d2013-09-03 20:00:20 +0000377
378 // FIXME: Add the extended attributes.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000379}
380
381// Add all of the attributes for \param Die to the hash.
David Blaikie377e8322013-10-24 18:29:03 +0000382void DIEHash::addAttributes(const DIE &Die) {
David Blaikiee8917752013-10-16 00:47:21 +0000383 DIEAttrs Attrs = {};
David Blaikiecbb5c732013-08-14 22:23:05 +0000384 collectAttributes(Die, Attrs);
David Blaikie377e8322013-10-24 18:29:03 +0000385 hashAttributes(Attrs, Die.getTag());
Eric Christopher0710bfa2013-08-13 01:21:55 +0000386}
387
David Blaikiea9546182013-10-25 18:38:43 +0000388void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
389 // 7.27 Step 7
390 // ... append the letter 'S',
391 addULEB128('S');
392
393 // the tag of C,
394 addULEB128(Die.getTag());
395
396 // and the name.
397 addString(Name);
398}
399
Eric Christopher0710bfa2013-08-13 01:21:55 +0000400// Compute the hash of a DIE. This is based on the type signature computation
401// given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
402// flattened description of the DIE.
David Blaikie377e8322013-10-24 18:29:03 +0000403void DIEHash::computeHash(const DIE &Die) {
Eric Christopher0710bfa2013-08-13 01:21:55 +0000404 // Append the letter 'D', followed by the DWARF tag of the DIE.
405 addULEB128('D');
David Blaikie377e8322013-10-24 18:29:03 +0000406 addULEB128(Die.getTag());
Eric Christopher0710bfa2013-08-13 01:21:55 +0000407
408 // Add each of the attributes of the DIE.
409 addAttributes(Die);
410
411 // Then hash each of the children of the DIE.
David Blaikie377e8322013-10-24 18:29:03 +0000412 for (std::vector<DIE *>::const_iterator I = Die.getChildren().begin(),
413 E = Die.getChildren().end();
David Blaikiea9546182013-10-25 18:38:43 +0000414 I != E; ++I) {
415 // 7.27 Step 7
416 // If C is a nested type entry or a member function entry, ...
David Blaikie7c0c2e52013-10-25 20:04:25 +0000417 if (isType((*I)->getTag()) || (*I)->getTag() == dwarf::DW_TAG_subprogram) {
David Blaikiea9546182013-10-25 18:38:43 +0000418 StringRef Name = getDIEStringAttr(**I, dwarf::DW_AT_name);
419 // ... and has a DW_AT_name attribute
420 if (!Name.empty()) {
421 hashNestedType(**I, Name);
422 continue;
423 }
424 }
David Blaikie377e8322013-10-24 18:29:03 +0000425 computeHash(**I);
David Blaikiea9546182013-10-25 18:38:43 +0000426 }
David Blaikie75ee0002013-10-16 20:29:06 +0000427
428 // Following the last (or if there are no children), append a zero byte.
David Blaikie700b91f2013-10-16 20:40:46 +0000429 Hash.update(makeArrayRef((uint8_t)'\0'));
Eric Christopher0710bfa2013-08-13 01:21:55 +0000430}
431
Eric Christopher0d27ca12013-08-08 23:45:55 +0000432/// This is based on the type signature computation given in section 7.27 of the
433/// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
Eric Christopherdd0cd3c2013-08-12 23:59:24 +0000434/// with the exception that we are hashing only the context and the name of the
435/// type.
David Blaikie377e8322013-10-24 18:29:03 +0000436uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
Eric Christopher0d27ca12013-08-08 23:45:55 +0000437
438 // Add the contexts to the hash. We won't be computing the ODR hash for
439 // function local types so it's safe to use the generic context hashing
440 // algorithm here.
441 // FIXME: If we figure out how to account for linkage in some way we could
442 // actually do this with a slight modification to the parent hash algorithm.
David Blaikie377e8322013-10-24 18:29:03 +0000443 if (const DIE *Parent = Die.getParent())
444 addParentContext(*Parent);
Eric Christopher0d27ca12013-08-08 23:45:55 +0000445
446 // Add the current DIE information.
447
448 // Add the DWARF tag of the DIE.
David Blaikie377e8322013-10-24 18:29:03 +0000449 addULEB128(Die.getTag());
Eric Christopher0d27ca12013-08-08 23:45:55 +0000450
451 // Add the name of the type to the hash.
David Blaikie377e8322013-10-24 18:29:03 +0000452 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
Eric Christopher0d27ca12013-08-08 23:45:55 +0000453
454 // Now get the result.
455 MD5::MD5Result Result;
456 Hash.final(Result);
457
458 // ... take the least significant 8 bytes and return those. Our MD5
459 // implementation always returns its results in little endian, swap bytes
460 // appropriately.
461 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
462}
Eric Christopher0710bfa2013-08-13 01:21:55 +0000463
464/// This is based on the type signature computation given in section 7.27 of the
465/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
466/// with the inclusion of the full CU and all top level CU entities.
Eric Christopher800a8762013-09-03 21:57:57 +0000467// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
David Blaikie377e8322013-10-24 18:29:03 +0000468uint64_t DIEHash::computeCUSignature(const DIE &Die) {
David Blaikie3baa3c32013-10-21 18:59:40 +0000469 Numbering.clear();
David Blaikie377e8322013-10-24 18:29:03 +0000470 Numbering[&Die] = 1;
Eric Christopher0710bfa2013-08-13 01:21:55 +0000471
472 // Hash the DIE.
473 computeHash(Die);
474
475 // Now return the result.
476 MD5::MD5Result Result;
477 Hash.final(Result);
478
479 // ... take the least significant 8 bytes and return those. Our MD5
480 // implementation always returns its results in little endian, swap bytes
481 // appropriately.
482 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
483}
Eric Christopher800a8762013-09-03 21:57:57 +0000484
485/// This is based on the type signature computation given in section 7.27 of the
486/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
487/// with the inclusion of additional forms not specifically called out in the
488/// standard.
David Blaikie377e8322013-10-24 18:29:03 +0000489uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
David Blaikie3baa3c32013-10-21 18:59:40 +0000490 Numbering.clear();
David Blaikie377e8322013-10-24 18:29:03 +0000491 Numbering[&Die] = 1;
Eric Christopher800a8762013-09-03 21:57:57 +0000492
David Blaikie377e8322013-10-24 18:29:03 +0000493 if (const DIE *Parent = Die.getParent())
494 addParentContext(*Parent);
David Blaikie88a68cb2013-10-17 00:10:34 +0000495
Eric Christopher800a8762013-09-03 21:57:57 +0000496 // Hash the DIE.
497 computeHash(Die);
498
499 // Now return the result.
500 MD5::MD5Result Result;
501 Hash.final(Result);
502
503 // ... take the least significant 8 bytes and return those. Our MD5
504 // implementation always returns its results in little endian, swap bytes
505 // appropriately.
506 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
507}