blob: d76af5380413ba1be52790d4480a198fbf129536 [file] [log] [blame]
Eric Christopher45731982013-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 Christopher45731982013-08-08 23:45:55 +000016#include "DIEHash.h"
David Blaikie9208b5e2013-11-13 18:07:27 +000017
18#include "DIE.h"
Eric Christopher45731982013-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 Blaikieafcb9652013-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 Christopher45731982013-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 Blaikie6316ca42013-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 Christopher45731982013-08-08 23:45:55 +000086/// \brief Including \p Parent adds the context of Parent to the hash..
David Blaikie2aee7be2013-10-24 18:29:03 +000087void DIEHash::addParentContext(const DIE &Parent) {
Eric Christopher45731982013-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 Blaikie2aee7be2013-10-24 18:29:03 +000093 SmallVector<const DIE *, 1> Parents;
94 const DIE *Cur = &Parent;
David Blaikie409dd9c2013-11-19 23:08:21 +000095 while (Cur->getParent()) {
David Blaikie2aee7be2013-10-24 18:29:03 +000096 Parents.push_back(Cur);
97 Cur = Cur->getParent();
Eric Christopher45731982013-08-08 23:45:55 +000098 }
David Blaikie409dd9c2013-11-19 23:08:21 +000099 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
100 Cur->getTag() == dwarf::DW_TAG_type_unit);
Eric Christopher45731982013-08-08 23:45:55 +0000101
102 // Reverse iterate over our list to go from the outermost construct to the
103 // innermost.
David Blaikie2aee7be2013-10-24 18:29:03 +0000104 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
105 E = Parents.rend();
Eric Christopher45731982013-08-08 23:45:55 +0000106 I != E; ++I) {
David Blaikie2aee7be2013-10-24 18:29:03 +0000107 const DIE &Die = **I;
Eric Christopher45731982013-08-08 23:45:55 +0000108
109 // ... Append the letter "C" to the sequence...
110 addULEB128('C');
111
112 // ... Followed by the DWARF tag of the construct...
David Blaikie2aee7be2013-10-24 18:29:03 +0000113 addULEB128(Die.getTag());
Eric Christopher45731982013-08-08 23:45:55 +0000114
115 // ... Then the name, taken from the DW_AT_name attribute.
David Blaikie2aee7be2013-10-24 18:29:03 +0000116 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
Eric Christopher45731982013-08-08 23:45:55 +0000117 DEBUG(dbgs() << "... adding context: " << Name << "\n");
118 if (!Name.empty())
119 addString(Name);
120 }
121}
122
Eric Christopherd29614f2013-08-13 01:21:55 +0000123// Collect all of the attributes for a particular DIE in single structure.
David Blaikie2aee7be2013-10-24 18:29:03 +0000124void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
125 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
126 const DIEAbbrev &Abbrevs = Die.getAbbrev();
Eric Christopherd29614f2013-08-13 01:21:55 +0000127
128#define COLLECT_ATTR(NAME) \
David Blaikie01fae512013-10-17 22:14:08 +0000129 case dwarf::NAME: \
130 Attrs.NAME.Val = Values[i]; \
131 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
132 break
Eric Christopherd29614f2013-08-13 01:21:55 +0000133
134 for (size_t i = 0, e = Values.size(); i != e; ++i) {
135 DEBUG(dbgs() << "Attribute: "
136 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
137 << " added.\n");
138 switch (Abbrevs.getData()[i].getAttribute()) {
David Blaikie01fae512013-10-17 22:14:08 +0000139 COLLECT_ATTR(DW_AT_name);
140 COLLECT_ATTR(DW_AT_accessibility);
141 COLLECT_ATTR(DW_AT_address_class);
142 COLLECT_ATTR(DW_AT_allocated);
143 COLLECT_ATTR(DW_AT_artificial);
144 COLLECT_ATTR(DW_AT_associated);
145 COLLECT_ATTR(DW_AT_binary_scale);
146 COLLECT_ATTR(DW_AT_bit_offset);
147 COLLECT_ATTR(DW_AT_bit_size);
148 COLLECT_ATTR(DW_AT_bit_stride);
149 COLLECT_ATTR(DW_AT_byte_size);
150 COLLECT_ATTR(DW_AT_byte_stride);
151 COLLECT_ATTR(DW_AT_const_expr);
152 COLLECT_ATTR(DW_AT_const_value);
153 COLLECT_ATTR(DW_AT_containing_type);
154 COLLECT_ATTR(DW_AT_count);
155 COLLECT_ATTR(DW_AT_data_bit_offset);
156 COLLECT_ATTR(DW_AT_data_location);
157 COLLECT_ATTR(DW_AT_data_member_location);
158 COLLECT_ATTR(DW_AT_decimal_scale);
159 COLLECT_ATTR(DW_AT_decimal_sign);
160 COLLECT_ATTR(DW_AT_default_value);
161 COLLECT_ATTR(DW_AT_digit_count);
162 COLLECT_ATTR(DW_AT_discr);
163 COLLECT_ATTR(DW_AT_discr_list);
164 COLLECT_ATTR(DW_AT_discr_value);
165 COLLECT_ATTR(DW_AT_encoding);
166 COLLECT_ATTR(DW_AT_enum_class);
167 COLLECT_ATTR(DW_AT_endianity);
168 COLLECT_ATTR(DW_AT_explicit);
169 COLLECT_ATTR(DW_AT_is_optional);
170 COLLECT_ATTR(DW_AT_location);
171 COLLECT_ATTR(DW_AT_lower_bound);
172 COLLECT_ATTR(DW_AT_mutable);
173 COLLECT_ATTR(DW_AT_ordering);
174 COLLECT_ATTR(DW_AT_picture_string);
175 COLLECT_ATTR(DW_AT_prototyped);
176 COLLECT_ATTR(DW_AT_small);
177 COLLECT_ATTR(DW_AT_segment);
178 COLLECT_ATTR(DW_AT_string_length);
179 COLLECT_ATTR(DW_AT_threads_scaled);
180 COLLECT_ATTR(DW_AT_upper_bound);
181 COLLECT_ATTR(DW_AT_use_location);
182 COLLECT_ATTR(DW_AT_use_UTF8);
183 COLLECT_ATTR(DW_AT_variable_parameter);
184 COLLECT_ATTR(DW_AT_virtuality);
185 COLLECT_ATTR(DW_AT_visibility);
186 COLLECT_ATTR(DW_AT_vtable_elem_location);
187 COLLECT_ATTR(DW_AT_type);
Eric Christopherd29614f2013-08-13 01:21:55 +0000188 default:
189 break;
190 }
191 }
192}
193
David Blaikieafcb9652013-10-24 17:51:43 +0000194void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
195 const DIE &Entry, StringRef Name) {
196 // append the letter 'N'
197 addULEB128('N');
198
199 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
200 addULEB128(Attribute);
201
202 // the context of the tag,
David Blaikie2aee7be2013-10-24 18:29:03 +0000203 if (const DIE *Parent = Entry.getParent())
204 addParentContext(*Parent);
David Blaikieafcb9652013-10-24 17:51:43 +0000205
206 // the letter 'E',
207 addULEB128('E');
208
209 // and the name of the type.
210 addString(Name);
211
212 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
213 // here's the relevant spec text to implement:
214 //
215 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
216 // the context is omitted and the name to be used is the ABI-specific name
217 // of the subprogram (e.g., the mangled linker name).
218}
219
220void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
221 unsigned DieNumber) {
222 // a) If T is in the list of [previously hashed types], use the letter
223 // 'R' as the marker
224 addULEB128('R');
225
226 addULEB128(Attribute);
227
228 // and use the unsigned LEB128 encoding of [the index of T in the
229 // list] as the attribute value;
230 addULEB128(DieNumber);
231}
232
233void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
David Blaikie2aee7be2013-10-24 18:29:03 +0000234 const DIE &Entry) {
David Blaikieafcb9652013-10-24 17:51:43 +0000235 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
236 "tags. Add support here when there's "
237 "a use case");
238 // Step 5
239 // If the tag in Step 3 is one of [the below tags]
240 if ((Tag == dwarf::DW_TAG_pointer_type ||
241 Tag == dwarf::DW_TAG_reference_type ||
242 Tag == dwarf::DW_TAG_rvalue_reference_type ||
243 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
244 // and the referenced type (via the [below attributes])
245 // FIXME: This seems overly restrictive, and causes hash mismatches
246 // there's a decl/def difference in the containing type of a
247 // ptr_to_member_type, but it's what DWARF says, for some reason.
248 Attribute == dwarf::DW_AT_type) {
David Blaikie32744412013-10-24 17:53:58 +0000249 // ... has a DW_AT_name attribute,
250 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
251 if (!Name.empty()) {
252 hashShallowTypeReference(Attribute, Entry, Name);
253 return;
254 }
David Blaikieafcb9652013-10-24 17:51:43 +0000255 }
256
257 unsigned &DieNumber = Numbering[&Entry];
258 if (DieNumber) {
259 hashRepeatedTypeReference(Attribute, DieNumber);
260 return;
261 }
262
263 // otherwise, b) use the letter 'T' as a the marker, ...
264 addULEB128('T');
265
266 addULEB128(Attribute);
267
268 // ... process the type T recursively by performing Steps 2 through 7, and
269 // use the result as the attribute value.
270 DieNumber = Numbering.size();
David Blaikie2aee7be2013-10-24 18:29:03 +0000271 computeHash(Entry);
David Blaikieafcb9652013-10-24 17:51:43 +0000272}
273
Eric Christopherd29614f2013-08-13 01:21:55 +0000274// Hash an individual attribute \param Attr based on the type of attribute and
275// the form.
David Blaikie6cf58c82013-10-21 22:36:50 +0000276void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000277 const DIEValue *Value = Attr.Val;
278 const DIEAbbrevData *Desc = Attr.Desc;
David Blaikied70a0552013-10-22 18:14:41 +0000279 dwarf::Attribute Attribute = Desc->getAttribute();
Eric Christopherd29614f2013-08-13 01:21:55 +0000280
David Blaikie6cf58c82013-10-21 22:36:50 +0000281 // 7.27 Step 3
David Blaikieca353be2013-10-17 22:07:09 +0000282 // ... An attribute that refers to another type entry T is processed as
283 // follows:
David Blaikieca353be2013-10-17 22:07:09 +0000284 if (const DIEEntry *EntryAttr = dyn_cast<DIEEntry>(Value)) {
David Blaikieafcb9652013-10-24 17:51:43 +0000285 hashDIEEntry(Attribute, Tag, *EntryAttr->getEntry());
David Blaikieca353be2013-10-17 22:07:09 +0000286 return;
287 }
288
289 // Other attribute values use the letter 'A' as the marker, ...
Eric Christopherd29614f2013-08-13 01:21:55 +0000290 addULEB128('A');
291
David Blaikied70a0552013-10-22 18:14:41 +0000292 addULEB128(Attribute);
David Blaikie6316ca42013-10-16 23:36:20 +0000293
David Blaikieca353be2013-10-17 22:07:09 +0000294 // ... and the value consists of the form code (encoded as an unsigned LEB128
295 // value) followed by the encoding of the value according to the form code. To
296 // ensure reproducibility of the signature, the set of forms used in the
David Blaikie6316ca42013-10-16 23:36:20 +0000297 // signature computation is limited to the following: DW_FORM_sdata,
298 // DW_FORM_flag, DW_FORM_string, and DW_FORM_block.
Eric Christopherd29614f2013-08-13 01:21:55 +0000299 switch (Desc->getForm()) {
David Blaikie6316ca42013-10-16 23:36:20 +0000300 case dwarf::DW_FORM_string:
301 llvm_unreachable(
302 "Add support for DW_FORM_string if we ever start emitting them again");
David Blaikie63bb3e12013-10-21 16:37:22 +0000303 case dwarf::DW_FORM_GNU_str_index:
Eric Christopherd29614f2013-08-13 01:21:55 +0000304 case dwarf::DW_FORM_strp:
David Blaikie6316ca42013-10-16 23:36:20 +0000305 addULEB128(dwarf::DW_FORM_string);
Eric Christopherd29614f2013-08-13 01:21:55 +0000306 addString(cast<DIEString>(Value)->getString());
307 break;
Eric Christopherd033d6f2013-08-28 00:10:38 +0000308 case dwarf::DW_FORM_data1:
309 case dwarf::DW_FORM_data2:
310 case dwarf::DW_FORM_data4:
311 case dwarf::DW_FORM_data8:
312 case dwarf::DW_FORM_udata:
David Blaikie6316ca42013-10-16 23:36:20 +0000313 addULEB128(dwarf::DW_FORM_sdata);
314 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
Eric Christopherd033d6f2013-08-28 00:10:38 +0000315 break;
David Blaikie63bb3e12013-10-21 16:37:22 +0000316 default:
317 llvm_unreachable("Add support for additional forms");
Eric Christopherd29614f2013-08-13 01:21:55 +0000318 }
319}
320
321// Go through the attributes from \param Attrs in the order specified in 7.27.4
322// and hash them.
David Blaikie6cf58c82013-10-21 22:36:50 +0000323void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000324#define ADD_ATTR(ATTR) \
325 { \
326 if (ATTR.Val != 0) \
David Blaikie6cf58c82013-10-21 22:36:50 +0000327 hashAttribute(ATTR, Tag); \
Eric Christopherd29614f2013-08-13 01:21:55 +0000328 }
329
Eric Christopherd29614f2013-08-13 01:21:55 +0000330 ADD_ATTR(Attrs.DW_AT_name);
Eric Christophere020fa72013-09-03 20:00:20 +0000331 ADD_ATTR(Attrs.DW_AT_accessibility);
332 ADD_ATTR(Attrs.DW_AT_address_class);
333 ADD_ATTR(Attrs.DW_AT_allocated);
334 ADD_ATTR(Attrs.DW_AT_artificial);
335 ADD_ATTR(Attrs.DW_AT_associated);
336 ADD_ATTR(Attrs.DW_AT_binary_scale);
337 ADD_ATTR(Attrs.DW_AT_bit_offset);
338 ADD_ATTR(Attrs.DW_AT_bit_size);
339 ADD_ATTR(Attrs.DW_AT_bit_stride);
340 ADD_ATTR(Attrs.DW_AT_byte_size);
341 ADD_ATTR(Attrs.DW_AT_byte_stride);
342 ADD_ATTR(Attrs.DW_AT_const_expr);
343 ADD_ATTR(Attrs.DW_AT_const_value);
344 ADD_ATTR(Attrs.DW_AT_containing_type);
345 ADD_ATTR(Attrs.DW_AT_count);
346 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
347 ADD_ATTR(Attrs.DW_AT_data_location);
348 ADD_ATTR(Attrs.DW_AT_data_member_location);
349 ADD_ATTR(Attrs.DW_AT_decimal_scale);
350 ADD_ATTR(Attrs.DW_AT_decimal_sign);
351 ADD_ATTR(Attrs.DW_AT_default_value);
352 ADD_ATTR(Attrs.DW_AT_digit_count);
353 ADD_ATTR(Attrs.DW_AT_discr);
354 ADD_ATTR(Attrs.DW_AT_discr_list);
355 ADD_ATTR(Attrs.DW_AT_discr_value);
356 ADD_ATTR(Attrs.DW_AT_encoding);
357 ADD_ATTR(Attrs.DW_AT_enum_class);
358 ADD_ATTR(Attrs.DW_AT_endianity);
359 ADD_ATTR(Attrs.DW_AT_explicit);
360 ADD_ATTR(Attrs.DW_AT_is_optional);
361 ADD_ATTR(Attrs.DW_AT_location);
362 ADD_ATTR(Attrs.DW_AT_lower_bound);
363 ADD_ATTR(Attrs.DW_AT_mutable);
364 ADD_ATTR(Attrs.DW_AT_ordering);
365 ADD_ATTR(Attrs.DW_AT_picture_string);
366 ADD_ATTR(Attrs.DW_AT_prototyped);
367 ADD_ATTR(Attrs.DW_AT_small);
368 ADD_ATTR(Attrs.DW_AT_segment);
369 ADD_ATTR(Attrs.DW_AT_string_length);
370 ADD_ATTR(Attrs.DW_AT_threads_scaled);
371 ADD_ATTR(Attrs.DW_AT_upper_bound);
372 ADD_ATTR(Attrs.DW_AT_use_location);
373 ADD_ATTR(Attrs.DW_AT_use_UTF8);
374 ADD_ATTR(Attrs.DW_AT_variable_parameter);
375 ADD_ATTR(Attrs.DW_AT_virtuality);
376 ADD_ATTR(Attrs.DW_AT_visibility);
377 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
David Blaikieca353be2013-10-17 22:07:09 +0000378 ADD_ATTR(Attrs.DW_AT_type);
Eric Christophere020fa72013-09-03 20:00:20 +0000379
380 // FIXME: Add the extended attributes.
Eric Christopherd29614f2013-08-13 01:21:55 +0000381}
382
383// Add all of the attributes for \param Die to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +0000384void DIEHash::addAttributes(const DIE &Die) {
David Blaikie94ded5f2013-10-16 00:47:21 +0000385 DIEAttrs Attrs = {};
David Blaikied0d6fcc2013-08-14 22:23:05 +0000386 collectAttributes(Die, Attrs);
David Blaikie2aee7be2013-10-24 18:29:03 +0000387 hashAttributes(Attrs, Die.getTag());
Eric Christopherd29614f2013-08-13 01:21:55 +0000388}
389
David Blaikie65cc9692013-10-25 18:38:43 +0000390void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
391 // 7.27 Step 7
392 // ... append the letter 'S',
393 addULEB128('S');
394
395 // the tag of C,
396 addULEB128(Die.getTag());
397
398 // and the name.
399 addString(Name);
400}
401
Eric Christopherd29614f2013-08-13 01:21:55 +0000402// Compute the hash of a DIE. This is based on the type signature computation
403// given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
404// flattened description of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000405void DIEHash::computeHash(const DIE &Die) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000406 // Append the letter 'D', followed by the DWARF tag of the DIE.
407 addULEB128('D');
David Blaikie2aee7be2013-10-24 18:29:03 +0000408 addULEB128(Die.getTag());
Eric Christopherd29614f2013-08-13 01:21:55 +0000409
410 // Add each of the attributes of the DIE.
411 addAttributes(Die);
412
413 // Then hash each of the children of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000414 for (std::vector<DIE *>::const_iterator I = Die.getChildren().begin(),
415 E = Die.getChildren().end();
David Blaikie65cc9692013-10-25 18:38:43 +0000416 I != E; ++I) {
417 // 7.27 Step 7
418 // If C is a nested type entry or a member function entry, ...
David Blaikie8bc7db72013-10-25 20:04:25 +0000419 if (isType((*I)->getTag()) || (*I)->getTag() == dwarf::DW_TAG_subprogram) {
David Blaikie65cc9692013-10-25 18:38:43 +0000420 StringRef Name = getDIEStringAttr(**I, dwarf::DW_AT_name);
421 // ... and has a DW_AT_name attribute
422 if (!Name.empty()) {
423 hashNestedType(**I, Name);
424 continue;
425 }
426 }
David Blaikie2aee7be2013-10-24 18:29:03 +0000427 computeHash(**I);
David Blaikie65cc9692013-10-25 18:38:43 +0000428 }
David Blaikie71a0ad62013-10-16 20:29:06 +0000429
430 // Following the last (or if there are no children), append a zero byte.
David Blaikie920bb2a2013-10-16 20:40:46 +0000431 Hash.update(makeArrayRef((uint8_t)'\0'));
Eric Christopherd29614f2013-08-13 01:21:55 +0000432}
433
Eric Christopher45731982013-08-08 23:45:55 +0000434/// This is based on the type signature computation given in section 7.27 of the
435/// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
Eric Christophercede3db2013-08-12 23:59:24 +0000436/// with the exception that we are hashing only the context and the name of the
437/// type.
David Blaikie2aee7be2013-10-24 18:29:03 +0000438uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
Eric Christopher45731982013-08-08 23:45:55 +0000439
440 // Add the contexts to the hash. We won't be computing the ODR hash for
441 // function local types so it's safe to use the generic context hashing
442 // algorithm here.
443 // FIXME: If we figure out how to account for linkage in some way we could
444 // actually do this with a slight modification to the parent hash algorithm.
David Blaikie2aee7be2013-10-24 18:29:03 +0000445 if (const DIE *Parent = Die.getParent())
446 addParentContext(*Parent);
Eric Christopher45731982013-08-08 23:45:55 +0000447
448 // Add the current DIE information.
449
450 // Add the DWARF tag of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000451 addULEB128(Die.getTag());
Eric Christopher45731982013-08-08 23:45:55 +0000452
453 // Add the name of the type to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +0000454 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
Eric Christopher45731982013-08-08 23:45:55 +0000455
456 // Now get the result.
457 MD5::MD5Result Result;
458 Hash.final(Result);
459
460 // ... take the least significant 8 bytes and return those. Our MD5
461 // implementation always returns its results in little endian, swap bytes
462 // appropriately.
463 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
464}
Eric Christopherd29614f2013-08-13 01:21:55 +0000465
466/// This is based on the type signature computation given in section 7.27 of the
467/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
468/// with the inclusion of the full CU and all top level CU entities.
Eric Christopher25b7adc2013-09-03 21:57:57 +0000469// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
David Blaikie2aee7be2013-10-24 18:29:03 +0000470uint64_t DIEHash::computeCUSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000471 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000472 Numbering[&Die] = 1;
Eric Christopherd29614f2013-08-13 01:21:55 +0000473
474 // Hash the DIE.
475 computeHash(Die);
476
477 // Now return the result.
478 MD5::MD5Result Result;
479 Hash.final(Result);
480
481 // ... take the least significant 8 bytes and return those. Our MD5
482 // implementation always returns its results in little endian, swap bytes
483 // appropriately.
484 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
485}
Eric Christopher25b7adc2013-09-03 21:57:57 +0000486
487/// This is based on the type signature computation given in section 7.27 of the
488/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
489/// with the inclusion of additional forms not specifically called out in the
490/// standard.
David Blaikie2aee7be2013-10-24 18:29:03 +0000491uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000492 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000493 Numbering[&Die] = 1;
Eric Christopher25b7adc2013-09-03 21:57:57 +0000494
David Blaikie2aee7be2013-10-24 18:29:03 +0000495 if (const DIE *Parent = Die.getParent())
496 addParentContext(*Parent);
David Blaikie8a142aa2013-10-17 00:10:34 +0000497
Eric Christopher25b7adc2013-09-03 21:57:57 +0000498 // Hash the DIE.
499 computeHash(Die);
500
501 // Now return the result.
502 MD5::MD5Result Result;
503 Hash.final(Result);
504
505 // ... take the least significant 8 bytes and return those. Our MD5
506 // implementation always returns its results in little endian, swap bytes
507 // appropriately.
508 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
509}