blob: d8ecc7ccfb9bf6709ef80908d7bd24b936e895b1 [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
Eric Christopher4f17ee02014-03-08 00:29:41 +000014#include "ByteStreamer.h"
Eric Christopher45731982013-08-08 23:45:55 +000015#include "DIEHash.h"
Eric Christopher4f17ee02014-03-08 00:29:41 +000016#include "DwarfDebug.h"
Eric Christopher45731982013-08-08 23:45:55 +000017#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
Eric Christopher420569b2014-02-20 02:50:45 +000019#include "llvm/CodeGen/AsmPrinter.h"
Frederic Risse541e0b2015-01-05 21:29:41 +000020#include "llvm/CodeGen/DIE.h"
Eric Christopher45731982013-08-08 23:45:55 +000021#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"
26
27using namespace llvm;
28
Chandler Carruth1b9dde02014-04-22 02:02:50 +000029#define DEBUG_TYPE "dwarfdebug"
30
Eric Christopher45731982013-08-08 23:45:55 +000031/// \brief Grabs the string in whichever attribute is passed in and returns
32/// a reference to it.
David Blaikieafcb9652013-10-24 17:51:43 +000033static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
Eric Christopher45731982013-08-08 23:45:55 +000034 // Iterate through all the attributes until we find the one we're
35 // looking for, if we can't find it return an empty string.
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +000036 for (const auto &V : Die.values())
37 if (V.getAttribute() == Attr)
38 return V.getDIEString().getString();
39
Eric Christopher45731982013-08-08 23:45:55 +000040 return StringRef("");
41}
42
43/// \brief Adds the string in \p Str to the hash. This also hashes
44/// a trailing NULL with the string.
45void DIEHash::addString(StringRef Str) {
46 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
47 Hash.update(Str);
48 Hash.update(makeArrayRef((uint8_t)'\0'));
49}
50
51// FIXME: The LEB128 routines are copied and only slightly modified out of
52// LEB128.h.
53
54/// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
55void DIEHash::addULEB128(uint64_t Value) {
56 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
57 do {
58 uint8_t Byte = Value & 0x7f;
59 Value >>= 7;
60 if (Value != 0)
61 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
62 Hash.update(Byte);
63 } while (Value != 0);
64}
65
David Blaikie6316ca42013-10-16 23:36:20 +000066void DIEHash::addSLEB128(int64_t Value) {
67 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
68 bool More;
69 do {
70 uint8_t Byte = Value & 0x7f;
71 Value >>= 7;
Eric Christophera1b87fd2014-02-20 02:40:41 +000072 More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
David Blaikie6316ca42013-10-16 23:36:20 +000073 ((Value == -1) && ((Byte & 0x40) != 0))));
74 if (More)
75 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
76 Hash.update(Byte);
77 } while (More);
78}
79
Eric Christopher45731982013-08-08 23:45:55 +000080/// \brief Including \p Parent adds the context of Parent to the hash..
David Blaikie2aee7be2013-10-24 18:29:03 +000081void DIEHash::addParentContext(const DIE &Parent) {
Eric Christopher45731982013-08-08 23:45:55 +000082
83 DEBUG(dbgs() << "Adding parent context to hash...\n");
84
85 // [7.27.2] For each surrounding type or namespace beginning with the
86 // outermost such construct...
David Blaikie2aee7be2013-10-24 18:29:03 +000087 SmallVector<const DIE *, 1> Parents;
88 const DIE *Cur = &Parent;
David Blaikie409dd9c2013-11-19 23:08:21 +000089 while (Cur->getParent()) {
David Blaikie2aee7be2013-10-24 18:29:03 +000090 Parents.push_back(Cur);
91 Cur = Cur->getParent();
Eric Christopher45731982013-08-08 23:45:55 +000092 }
David Blaikie409dd9c2013-11-19 23:08:21 +000093 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
94 Cur->getTag() == dwarf::DW_TAG_type_unit);
Eric Christopher45731982013-08-08 23:45:55 +000095
96 // Reverse iterate over our list to go from the outermost construct to the
97 // innermost.
David Blaikie2aee7be2013-10-24 18:29:03 +000098 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
99 E = Parents.rend();
Eric Christopher45731982013-08-08 23:45:55 +0000100 I != E; ++I) {
David Blaikie2aee7be2013-10-24 18:29:03 +0000101 const DIE &Die = **I;
Eric Christopher45731982013-08-08 23:45:55 +0000102
103 // ... Append the letter "C" to the sequence...
104 addULEB128('C');
105
106 // ... Followed by the DWARF tag of the construct...
David Blaikie2aee7be2013-10-24 18:29:03 +0000107 addULEB128(Die.getTag());
Eric Christopher45731982013-08-08 23:45:55 +0000108
109 // ... Then the name, taken from the DW_AT_name attribute.
David Blaikie2aee7be2013-10-24 18:29:03 +0000110 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
Eric Christopher45731982013-08-08 23:45:55 +0000111 DEBUG(dbgs() << "... adding context: " << Name << "\n");
112 if (!Name.empty())
113 addString(Name);
114 }
115}
116
Eric Christopherd29614f2013-08-13 01:21:55 +0000117// Collect all of the attributes for a particular DIE in single structure.
David Blaikie2aee7be2013-10-24 18:29:03 +0000118void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000119#define COLLECT_ATTR(NAME) \
David Blaikie01fae512013-10-17 22:14:08 +0000120 case dwarf::NAME: \
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000121 Attrs.NAME = V; \
David Blaikie01fae512013-10-17 22:14:08 +0000122 break
Eric Christopherd29614f2013-08-13 01:21:55 +0000123
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000124 for (const auto &V : Die.values()) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000125 DEBUG(dbgs() << "Attribute: "
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000126 << dwarf::AttributeString(V.getAttribute())
Eric Christopherd29614f2013-08-13 01:21:55 +0000127 << " added.\n");
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000128 switch (V.getAttribute()) {
Eric Christophera1b87fd2014-02-20 02:40:41 +0000129 COLLECT_ATTR(DW_AT_name);
130 COLLECT_ATTR(DW_AT_accessibility);
131 COLLECT_ATTR(DW_AT_address_class);
132 COLLECT_ATTR(DW_AT_allocated);
133 COLLECT_ATTR(DW_AT_artificial);
134 COLLECT_ATTR(DW_AT_associated);
135 COLLECT_ATTR(DW_AT_binary_scale);
136 COLLECT_ATTR(DW_AT_bit_offset);
137 COLLECT_ATTR(DW_AT_bit_size);
138 COLLECT_ATTR(DW_AT_bit_stride);
139 COLLECT_ATTR(DW_AT_byte_size);
140 COLLECT_ATTR(DW_AT_byte_stride);
141 COLLECT_ATTR(DW_AT_const_expr);
142 COLLECT_ATTR(DW_AT_const_value);
143 COLLECT_ATTR(DW_AT_containing_type);
144 COLLECT_ATTR(DW_AT_count);
145 COLLECT_ATTR(DW_AT_data_bit_offset);
146 COLLECT_ATTR(DW_AT_data_location);
147 COLLECT_ATTR(DW_AT_data_member_location);
148 COLLECT_ATTR(DW_AT_decimal_scale);
149 COLLECT_ATTR(DW_AT_decimal_sign);
150 COLLECT_ATTR(DW_AT_default_value);
151 COLLECT_ATTR(DW_AT_digit_count);
152 COLLECT_ATTR(DW_AT_discr);
153 COLLECT_ATTR(DW_AT_discr_list);
154 COLLECT_ATTR(DW_AT_discr_value);
155 COLLECT_ATTR(DW_AT_encoding);
156 COLLECT_ATTR(DW_AT_enum_class);
157 COLLECT_ATTR(DW_AT_endianity);
158 COLLECT_ATTR(DW_AT_explicit);
159 COLLECT_ATTR(DW_AT_is_optional);
160 COLLECT_ATTR(DW_AT_location);
161 COLLECT_ATTR(DW_AT_lower_bound);
162 COLLECT_ATTR(DW_AT_mutable);
163 COLLECT_ATTR(DW_AT_ordering);
164 COLLECT_ATTR(DW_AT_picture_string);
165 COLLECT_ATTR(DW_AT_prototyped);
166 COLLECT_ATTR(DW_AT_small);
167 COLLECT_ATTR(DW_AT_segment);
168 COLLECT_ATTR(DW_AT_string_length);
169 COLLECT_ATTR(DW_AT_threads_scaled);
170 COLLECT_ATTR(DW_AT_upper_bound);
171 COLLECT_ATTR(DW_AT_use_location);
172 COLLECT_ATTR(DW_AT_use_UTF8);
173 COLLECT_ATTR(DW_AT_variable_parameter);
174 COLLECT_ATTR(DW_AT_virtuality);
175 COLLECT_ATTR(DW_AT_visibility);
176 COLLECT_ATTR(DW_AT_vtable_elem_location);
177 COLLECT_ATTR(DW_AT_type);
Eric Christopherd29614f2013-08-13 01:21:55 +0000178 default:
179 break;
180 }
181 }
182}
183
David Blaikieafcb9652013-10-24 17:51:43 +0000184void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
185 const DIE &Entry, StringRef Name) {
186 // append the letter 'N'
187 addULEB128('N');
188
189 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
190 addULEB128(Attribute);
191
192 // the context of the tag,
David Blaikie2aee7be2013-10-24 18:29:03 +0000193 if (const DIE *Parent = Entry.getParent())
194 addParentContext(*Parent);
David Blaikieafcb9652013-10-24 17:51:43 +0000195
196 // the letter 'E',
197 addULEB128('E');
198
199 // and the name of the type.
200 addString(Name);
201
202 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
203 // here's the relevant spec text to implement:
204 //
205 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
206 // the context is omitted and the name to be used is the ABI-specific name
207 // of the subprogram (e.g., the mangled linker name).
208}
209
210void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
211 unsigned DieNumber) {
212 // a) If T is in the list of [previously hashed types], use the letter
213 // 'R' as the marker
214 addULEB128('R');
215
216 addULEB128(Attribute);
217
218 // and use the unsigned LEB128 encoding of [the index of T in the
219 // list] as the attribute value;
220 addULEB128(DieNumber);
221}
222
223void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
David Blaikie2aee7be2013-10-24 18:29:03 +0000224 const DIE &Entry) {
David Blaikieafcb9652013-10-24 17:51:43 +0000225 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
226 "tags. Add support here when there's "
227 "a use case");
228 // Step 5
229 // If the tag in Step 3 is one of [the below tags]
230 if ((Tag == dwarf::DW_TAG_pointer_type ||
231 Tag == dwarf::DW_TAG_reference_type ||
232 Tag == dwarf::DW_TAG_rvalue_reference_type ||
233 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
234 // and the referenced type (via the [below attributes])
235 // FIXME: This seems overly restrictive, and causes hash mismatches
236 // there's a decl/def difference in the containing type of a
237 // ptr_to_member_type, but it's what DWARF says, for some reason.
238 Attribute == dwarf::DW_AT_type) {
David Blaikie32744412013-10-24 17:53:58 +0000239 // ... has a DW_AT_name attribute,
240 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
241 if (!Name.empty()) {
242 hashShallowTypeReference(Attribute, Entry, Name);
243 return;
244 }
David Blaikieafcb9652013-10-24 17:51:43 +0000245 }
246
247 unsigned &DieNumber = Numbering[&Entry];
248 if (DieNumber) {
249 hashRepeatedTypeReference(Attribute, DieNumber);
250 return;
251 }
252
Robin Morisset039781e2014-08-29 21:53:01 +0000253 // otherwise, b) use the letter 'T' as the marker, ...
David Blaikieafcb9652013-10-24 17:51:43 +0000254 addULEB128('T');
255
256 addULEB128(Attribute);
257
258 // ... process the type T recursively by performing Steps 2 through 7, and
259 // use the result as the attribute value.
260 DieNumber = Numbering.size();
David Blaikie2aee7be2013-10-24 18:29:03 +0000261 computeHash(Entry);
David Blaikieafcb9652013-10-24 17:51:43 +0000262}
263
Eric Christopher420569b2014-02-20 02:50:45 +0000264// Hash all of the values in a block like set of values. This assumes that
265// all of the data is going to be added as integers.
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000266void DIEHash::hashBlockData(const DIE::const_value_range &Values) {
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000267 for (const auto &V : Values)
268 Hash.update((uint64_t)V.getDIEInteger().getValue());
Eric Christopher420569b2014-02-20 02:50:45 +0000269}
270
Eric Christopher4f17ee02014-03-08 00:29:41 +0000271// Hash the contents of a loclistptr class.
272void DIEHash::hashLocList(const DIELocList &LocList) {
Eric Christopher4f17ee02014-03-08 00:29:41 +0000273 HashingByteStreamer Streamer(*this);
David Blaikie0a456de2014-04-02 01:43:18 +0000274 DwarfDebug &DD = *AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000275 const DebugLocStream &Locs = DD.getDebugLocs();
276 for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
David Blaikie0a456de2014-04-02 01:43:18 +0000277 DD.emitDebugLocEntry(Streamer, Entry);
Eric Christopher4f17ee02014-03-08 00:29:41 +0000278}
279
Eric Christopherd29614f2013-08-13 01:21:55 +0000280// Hash an individual attribute \param Attr based on the type of attribute and
281// the form.
Benjamin Kramer1afc1de2016-06-17 20:41:14 +0000282void DIEHash::hashAttribute(const DIEValue &Value, dwarf::Tag Tag) {
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000283 dwarf::Attribute Attribute = Value.getAttribute();
Eric Christopherd29614f2013-08-13 01:21:55 +0000284
Eric Christopher4b1cf582014-01-31 20:02:58 +0000285 // Other attribute values use the letter 'A' as the marker, and the value
286 // consists of the form code (encoded as an unsigned LEB128 value) followed by
287 // the encoding of the value according to the form code. To ensure
288 // reproducibility of the signature, the set of forms used in the signature
289 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
290 // DW_FORM_string, and DW_FORM_block.
Eric Christopher19308492014-03-06 00:38:32 +0000291
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000292 switch (Value.getType()) {
293 case DIEValue::isNone:
294 llvm_unreachable("Expected valid DIEValue");
295
Eric Christopher2bed2572014-03-06 18:59:42 +0000296 // 7.27 Step 3
297 // ... An attribute that refers to another type entry T is processed as
298 // follows:
299 case DIEValue::isEntry:
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000300 hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
Eric Christopher2bed2572014-03-06 18:59:42 +0000301 break;
Eric Christopher19308492014-03-06 00:38:32 +0000302 case DIEValue::isInteger: {
303 addULEB128('A');
304 addULEB128(Attribute);
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000305 switch (Value.getForm()) {
Eric Christopher19308492014-03-06 00:38:32 +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:
311 case dwarf::DW_FORM_sdata:
312 addULEB128(dwarf::DW_FORM_sdata);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000313 addSLEB128((int64_t)Value.getDIEInteger().getValue());
Eric Christopher19308492014-03-06 00:38:32 +0000314 break;
315 // DW_FORM_flag_present is just flag with a value of one. We still give it a
316 // value so just use the value.
317 case dwarf::DW_FORM_flag_present:
318 case dwarf::DW_FORM_flag:
319 addULEB128(dwarf::DW_FORM_flag);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000320 addULEB128((int64_t)Value.getDIEInteger().getValue());
Eric Christopher19308492014-03-06 00:38:32 +0000321 break;
322 default:
323 llvm_unreachable("Unknown integer form!");
324 }
325 break;
326 }
327 case DIEValue::isString:
Eric Christopher4b1cf582014-01-31 20:02:58 +0000328 addULEB128('A');
329 addULEB128(Attribute);
David Blaikie6316ca42013-10-16 23:36:20 +0000330 addULEB128(dwarf::DW_FORM_string);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000331 addString(Value.getDIEString().getString());
Eric Christopherd29614f2013-08-13 01:21:55 +0000332 break;
Greg Clayton3462a422016-12-08 01:03:48 +0000333 case DIEValue::isInlineString:
334 addULEB128('A');
335 addULEB128(Attribute);
336 addULEB128(dwarf::DW_FORM_string);
337 addString(Value.getDIEInlineString().getString());
338 break;
Eric Christopher19308492014-03-06 00:38:32 +0000339 case DIEValue::isBlock:
340 case DIEValue::isLoc:
Eric Christopher4f17ee02014-03-08 00:29:41 +0000341 case DIEValue::isLocList:
Eric Christopher420569b2014-02-20 02:50:45 +0000342 addULEB128('A');
343 addULEB128(Attribute);
344 addULEB128(dwarf::DW_FORM_block);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000345 if (Value.getType() == DIEValue::isBlock) {
346 addULEB128(Value.getDIEBlock().ComputeSize(AP));
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000347 hashBlockData(Value.getDIEBlock().values());
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000348 } else if (Value.getType() == DIEValue::isLoc) {
349 addULEB128(Value.getDIELoc().ComputeSize(AP));
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000350 hashBlockData(Value.getDIELoc().values());
Eric Christopher4f17ee02014-03-08 00:29:41 +0000351 } else {
352 // We could add the block length, but that would take
353 // a bit of work and not add a lot of uniqueness
354 // to the hash in some way we could test.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000355 hashLocList(Value.getDIELocList());
Eric Christopher420569b2014-02-20 02:50:45 +0000356 }
357 break;
Eric Christopher6bb07f62014-03-06 01:32:56 +0000358 // FIXME: It's uncertain whether or not we should handle this at the moment.
Eric Christopher19308492014-03-06 00:38:32 +0000359 case DIEValue::isExpr:
360 case DIEValue::isLabel:
361 case DIEValue::isDelta:
Eric Christopher19308492014-03-06 00:38:32 +0000362 llvm_unreachable("Add support for additional value types.");
Eric Christopherd29614f2013-08-13 01:21:55 +0000363 }
364}
365
366// Go through the attributes from \param Attrs in the order specified in 7.27.4
367// and hash them.
David Blaikie6cf58c82013-10-21 22:36:50 +0000368void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000369#define ADD_ATTR(ATTR) \
370 { \
Duncan P. N. Exon Smithf3a6a672015-05-27 22:36:37 +0000371 if (ATTR) \
David Blaikie6cf58c82013-10-21 22:36:50 +0000372 hashAttribute(ATTR, Tag); \
Eric Christopherd29614f2013-08-13 01:21:55 +0000373 }
374
Eric Christopherd29614f2013-08-13 01:21:55 +0000375 ADD_ATTR(Attrs.DW_AT_name);
Eric Christophere020fa72013-09-03 20:00:20 +0000376 ADD_ATTR(Attrs.DW_AT_accessibility);
377 ADD_ATTR(Attrs.DW_AT_address_class);
378 ADD_ATTR(Attrs.DW_AT_allocated);
379 ADD_ATTR(Attrs.DW_AT_artificial);
380 ADD_ATTR(Attrs.DW_AT_associated);
381 ADD_ATTR(Attrs.DW_AT_binary_scale);
382 ADD_ATTR(Attrs.DW_AT_bit_offset);
383 ADD_ATTR(Attrs.DW_AT_bit_size);
384 ADD_ATTR(Attrs.DW_AT_bit_stride);
385 ADD_ATTR(Attrs.DW_AT_byte_size);
386 ADD_ATTR(Attrs.DW_AT_byte_stride);
387 ADD_ATTR(Attrs.DW_AT_const_expr);
388 ADD_ATTR(Attrs.DW_AT_const_value);
389 ADD_ATTR(Attrs.DW_AT_containing_type);
390 ADD_ATTR(Attrs.DW_AT_count);
391 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
392 ADD_ATTR(Attrs.DW_AT_data_location);
393 ADD_ATTR(Attrs.DW_AT_data_member_location);
394 ADD_ATTR(Attrs.DW_AT_decimal_scale);
395 ADD_ATTR(Attrs.DW_AT_decimal_sign);
396 ADD_ATTR(Attrs.DW_AT_default_value);
397 ADD_ATTR(Attrs.DW_AT_digit_count);
398 ADD_ATTR(Attrs.DW_AT_discr);
399 ADD_ATTR(Attrs.DW_AT_discr_list);
400 ADD_ATTR(Attrs.DW_AT_discr_value);
401 ADD_ATTR(Attrs.DW_AT_encoding);
402 ADD_ATTR(Attrs.DW_AT_enum_class);
403 ADD_ATTR(Attrs.DW_AT_endianity);
404 ADD_ATTR(Attrs.DW_AT_explicit);
405 ADD_ATTR(Attrs.DW_AT_is_optional);
406 ADD_ATTR(Attrs.DW_AT_location);
407 ADD_ATTR(Attrs.DW_AT_lower_bound);
408 ADD_ATTR(Attrs.DW_AT_mutable);
409 ADD_ATTR(Attrs.DW_AT_ordering);
410 ADD_ATTR(Attrs.DW_AT_picture_string);
411 ADD_ATTR(Attrs.DW_AT_prototyped);
412 ADD_ATTR(Attrs.DW_AT_small);
413 ADD_ATTR(Attrs.DW_AT_segment);
414 ADD_ATTR(Attrs.DW_AT_string_length);
415 ADD_ATTR(Attrs.DW_AT_threads_scaled);
416 ADD_ATTR(Attrs.DW_AT_upper_bound);
417 ADD_ATTR(Attrs.DW_AT_use_location);
418 ADD_ATTR(Attrs.DW_AT_use_UTF8);
419 ADD_ATTR(Attrs.DW_AT_variable_parameter);
420 ADD_ATTR(Attrs.DW_AT_virtuality);
421 ADD_ATTR(Attrs.DW_AT_visibility);
422 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
David Blaikieca353be2013-10-17 22:07:09 +0000423 ADD_ATTR(Attrs.DW_AT_type);
Eric Christophere020fa72013-09-03 20:00:20 +0000424
425 // FIXME: Add the extended attributes.
Eric Christopherd29614f2013-08-13 01:21:55 +0000426}
427
428// Add all of the attributes for \param Die to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +0000429void DIEHash::addAttributes(const DIE &Die) {
David Blaikie94ded5f2013-10-16 00:47:21 +0000430 DIEAttrs Attrs = {};
David Blaikied0d6fcc2013-08-14 22:23:05 +0000431 collectAttributes(Die, Attrs);
David Blaikie2aee7be2013-10-24 18:29:03 +0000432 hashAttributes(Attrs, Die.getTag());
Eric Christopherd29614f2013-08-13 01:21:55 +0000433}
434
David Blaikie65cc9692013-10-25 18:38:43 +0000435void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
436 // 7.27 Step 7
437 // ... append the letter 'S',
438 addULEB128('S');
439
440 // the tag of C,
441 addULEB128(Die.getTag());
442
443 // and the name.
444 addString(Name);
445}
446
Eric Christopherd29614f2013-08-13 01:21:55 +0000447// Compute the hash of a DIE. This is based on the type signature computation
448// given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
449// flattened description of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000450void DIEHash::computeHash(const DIE &Die) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000451 // Append the letter 'D', followed by the DWARF tag of the DIE.
452 addULEB128('D');
David Blaikie2aee7be2013-10-24 18:29:03 +0000453 addULEB128(Die.getTag());
Eric Christopherd29614f2013-08-13 01:21:55 +0000454
455 // Add each of the attributes of the DIE.
456 addAttributes(Die);
457
458 // Then hash each of the children of the DIE.
Duncan P. N. Exon Smith8d3197f2015-05-28 19:56:34 +0000459 for (auto &C : Die.children()) {
David Blaikie65cc9692013-10-25 18:38:43 +0000460 // 7.27 Step 7
461 // If C is a nested type entry or a member function entry, ...
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000462 if (isType(C.getTag()) || C.getTag() == dwarf::DW_TAG_subprogram) {
463 StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name);
David Blaikie65cc9692013-10-25 18:38:43 +0000464 // ... and has a DW_AT_name attribute
465 if (!Name.empty()) {
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000466 hashNestedType(C, Name);
David Blaikie65cc9692013-10-25 18:38:43 +0000467 continue;
468 }
469 }
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000470 computeHash(C);
David Blaikie65cc9692013-10-25 18:38:43 +0000471 }
David Blaikie71a0ad62013-10-16 20:29:06 +0000472
473 // Following the last (or if there are no children), append a zero byte.
David Blaikie920bb2a2013-10-16 20:40:46 +0000474 Hash.update(makeArrayRef((uint8_t)'\0'));
Eric Christopherd29614f2013-08-13 01:21:55 +0000475}
476
Eric Christopher45731982013-08-08 23:45:55 +0000477/// This is based on the type signature computation given in section 7.27 of the
Eric Christopherd29614f2013-08-13 01:21:55 +0000478/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
479/// with the inclusion of the full CU and all top level CU entities.
Eric Christopher25b7adc2013-09-03 21:57:57 +0000480// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
David Blaikie2aee7be2013-10-24 18:29:03 +0000481uint64_t DIEHash::computeCUSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000482 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000483 Numbering[&Die] = 1;
Eric Christopherd29614f2013-08-13 01:21:55 +0000484
485 // Hash the DIE.
486 computeHash(Die);
487
488 // Now return the result.
489 MD5::MD5Result Result;
490 Hash.final(Result);
491
492 // ... take the least significant 8 bytes and return those. Our MD5
493 // implementation always returns its results in little endian, swap bytes
494 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000495 return support::endian::read64le(Result + 8);
Eric Christopherd29614f2013-08-13 01:21:55 +0000496}
Eric Christopher25b7adc2013-09-03 21:57:57 +0000497
498/// This is based on the type signature computation given in section 7.27 of the
499/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
500/// with the inclusion of additional forms not specifically called out in the
501/// standard.
David Blaikie2aee7be2013-10-24 18:29:03 +0000502uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000503 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000504 Numbering[&Die] = 1;
Eric Christopher25b7adc2013-09-03 21:57:57 +0000505
David Blaikie2aee7be2013-10-24 18:29:03 +0000506 if (const DIE *Parent = Die.getParent())
507 addParentContext(*Parent);
David Blaikie8a142aa2013-10-17 00:10:34 +0000508
Eric Christopher25b7adc2013-09-03 21:57:57 +0000509 // Hash the DIE.
510 computeHash(Die);
511
512 // Now return the result.
513 MD5::MD5Result Result;
514 Hash.final(Result);
515
516 // ... take the least significant 8 bytes and return those. Our MD5
517 // implementation always returns its results in little endian, swap bytes
518 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000519 return support::endian::read64le(Result + 8);
Eric Christopher25b7adc2013-09-03 21:57:57 +0000520}