blob: 53e02fac6e374d5b9eb01c29884a2fd2f9c3b8e5 [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) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +000034 const auto &Values = Die.getValues();
David Blaikieafcb9652013-10-24 17:51:43 +000035 const DIEAbbrev &Abbrevs = Die.getAbbrev();
Eric Christopher45731982013-08-08 23:45:55 +000036
37 // Iterate through all the attributes until we find the one we're
38 // looking for, if we can't find it return an empty string.
39 for (size_t i = 0; i < Values.size(); ++i) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +000040 if (Abbrevs.getData()[i].getAttribute() == Attr)
41 return Values[i].getDIEString().getString();
Eric Christopher45731982013-08-08 23:45:55 +000042 }
43 return StringRef("");
44}
45
46/// \brief Adds the string in \p Str to the hash. This also hashes
47/// a trailing NULL with the string.
48void DIEHash::addString(StringRef Str) {
49 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
50 Hash.update(Str);
51 Hash.update(makeArrayRef((uint8_t)'\0'));
52}
53
54// FIXME: The LEB128 routines are copied and only slightly modified out of
55// LEB128.h.
56
57/// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
58void DIEHash::addULEB128(uint64_t Value) {
59 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
60 do {
61 uint8_t Byte = Value & 0x7f;
62 Value >>= 7;
63 if (Value != 0)
64 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
65 Hash.update(Byte);
66 } while (Value != 0);
67}
68
David Blaikie6316ca42013-10-16 23:36:20 +000069void DIEHash::addSLEB128(int64_t Value) {
70 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
71 bool More;
72 do {
73 uint8_t Byte = Value & 0x7f;
74 Value >>= 7;
Eric Christophera1b87fd2014-02-20 02:40:41 +000075 More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
David Blaikie6316ca42013-10-16 23:36:20 +000076 ((Value == -1) && ((Byte & 0x40) != 0))));
77 if (More)
78 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
79 Hash.update(Byte);
80 } while (More);
81}
82
Eric Christopher45731982013-08-08 23:45:55 +000083/// \brief Including \p Parent adds the context of Parent to the hash..
David Blaikie2aee7be2013-10-24 18:29:03 +000084void DIEHash::addParentContext(const DIE &Parent) {
Eric Christopher45731982013-08-08 23:45:55 +000085
86 DEBUG(dbgs() << "Adding parent context to hash...\n");
87
88 // [7.27.2] For each surrounding type or namespace beginning with the
89 // outermost such construct...
David Blaikie2aee7be2013-10-24 18:29:03 +000090 SmallVector<const DIE *, 1> Parents;
91 const DIE *Cur = &Parent;
David Blaikie409dd9c2013-11-19 23:08:21 +000092 while (Cur->getParent()) {
David Blaikie2aee7be2013-10-24 18:29:03 +000093 Parents.push_back(Cur);
94 Cur = Cur->getParent();
Eric Christopher45731982013-08-08 23:45:55 +000095 }
David Blaikie409dd9c2013-11-19 23:08:21 +000096 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
97 Cur->getTag() == dwarf::DW_TAG_type_unit);
Eric Christopher45731982013-08-08 23:45:55 +000098
99 // Reverse iterate over our list to go from the outermost construct to the
100 // innermost.
David Blaikie2aee7be2013-10-24 18:29:03 +0000101 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
102 E = Parents.rend();
Eric Christopher45731982013-08-08 23:45:55 +0000103 I != E; ++I) {
David Blaikie2aee7be2013-10-24 18:29:03 +0000104 const DIE &Die = **I;
Eric Christopher45731982013-08-08 23:45:55 +0000105
106 // ... Append the letter "C" to the sequence...
107 addULEB128('C');
108
109 // ... Followed by the DWARF tag of the construct...
David Blaikie2aee7be2013-10-24 18:29:03 +0000110 addULEB128(Die.getTag());
Eric Christopher45731982013-08-08 23:45:55 +0000111
112 // ... Then the name, taken from the DW_AT_name attribute.
David Blaikie2aee7be2013-10-24 18:29:03 +0000113 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
Eric Christopher45731982013-08-08 23:45:55 +0000114 DEBUG(dbgs() << "... adding context: " << Name << "\n");
115 if (!Name.empty())
116 addString(Name);
117 }
118}
119
Eric Christopherd29614f2013-08-13 01:21:55 +0000120// Collect all of the attributes for a particular DIE in single structure.
David Blaikie2aee7be2013-10-24 18:29:03 +0000121void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000122 const SmallVectorImpl<DIEValue> &Values = Die.getValues();
David Blaikie2aee7be2013-10-24 18:29:03 +0000123 const DIEAbbrev &Abbrevs = Die.getAbbrev();
Eric Christopherd29614f2013-08-13 01:21:55 +0000124
125#define COLLECT_ATTR(NAME) \
David Blaikie01fae512013-10-17 22:14:08 +0000126 case dwarf::NAME: \
127 Attrs.NAME.Val = Values[i]; \
128 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
129 break
Eric Christopherd29614f2013-08-13 01:21:55 +0000130
131 for (size_t i = 0, e = Values.size(); i != e; ++i) {
132 DEBUG(dbgs() << "Attribute: "
133 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
134 << " added.\n");
135 switch (Abbrevs.getData()[i].getAttribute()) {
Eric Christophera1b87fd2014-02-20 02:40:41 +0000136 COLLECT_ATTR(DW_AT_name);
137 COLLECT_ATTR(DW_AT_accessibility);
138 COLLECT_ATTR(DW_AT_address_class);
139 COLLECT_ATTR(DW_AT_allocated);
140 COLLECT_ATTR(DW_AT_artificial);
141 COLLECT_ATTR(DW_AT_associated);
142 COLLECT_ATTR(DW_AT_binary_scale);
143 COLLECT_ATTR(DW_AT_bit_offset);
144 COLLECT_ATTR(DW_AT_bit_size);
145 COLLECT_ATTR(DW_AT_bit_stride);
146 COLLECT_ATTR(DW_AT_byte_size);
147 COLLECT_ATTR(DW_AT_byte_stride);
148 COLLECT_ATTR(DW_AT_const_expr);
149 COLLECT_ATTR(DW_AT_const_value);
150 COLLECT_ATTR(DW_AT_containing_type);
151 COLLECT_ATTR(DW_AT_count);
152 COLLECT_ATTR(DW_AT_data_bit_offset);
153 COLLECT_ATTR(DW_AT_data_location);
154 COLLECT_ATTR(DW_AT_data_member_location);
155 COLLECT_ATTR(DW_AT_decimal_scale);
156 COLLECT_ATTR(DW_AT_decimal_sign);
157 COLLECT_ATTR(DW_AT_default_value);
158 COLLECT_ATTR(DW_AT_digit_count);
159 COLLECT_ATTR(DW_AT_discr);
160 COLLECT_ATTR(DW_AT_discr_list);
161 COLLECT_ATTR(DW_AT_discr_value);
162 COLLECT_ATTR(DW_AT_encoding);
163 COLLECT_ATTR(DW_AT_enum_class);
164 COLLECT_ATTR(DW_AT_endianity);
165 COLLECT_ATTR(DW_AT_explicit);
166 COLLECT_ATTR(DW_AT_is_optional);
167 COLLECT_ATTR(DW_AT_location);
168 COLLECT_ATTR(DW_AT_lower_bound);
169 COLLECT_ATTR(DW_AT_mutable);
170 COLLECT_ATTR(DW_AT_ordering);
171 COLLECT_ATTR(DW_AT_picture_string);
172 COLLECT_ATTR(DW_AT_prototyped);
173 COLLECT_ATTR(DW_AT_small);
174 COLLECT_ATTR(DW_AT_segment);
175 COLLECT_ATTR(DW_AT_string_length);
176 COLLECT_ATTR(DW_AT_threads_scaled);
177 COLLECT_ATTR(DW_AT_upper_bound);
178 COLLECT_ATTR(DW_AT_use_location);
179 COLLECT_ATTR(DW_AT_use_UTF8);
180 COLLECT_ATTR(DW_AT_variable_parameter);
181 COLLECT_ATTR(DW_AT_virtuality);
182 COLLECT_ATTR(DW_AT_visibility);
183 COLLECT_ATTR(DW_AT_vtable_elem_location);
184 COLLECT_ATTR(DW_AT_type);
Eric Christopherd29614f2013-08-13 01:21:55 +0000185 default:
186 break;
187 }
188 }
189}
190
David Blaikieafcb9652013-10-24 17:51:43 +0000191void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
192 const DIE &Entry, StringRef Name) {
193 // append the letter 'N'
194 addULEB128('N');
195
196 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
197 addULEB128(Attribute);
198
199 // the context of the tag,
David Blaikie2aee7be2013-10-24 18:29:03 +0000200 if (const DIE *Parent = Entry.getParent())
201 addParentContext(*Parent);
David Blaikieafcb9652013-10-24 17:51:43 +0000202
203 // the letter 'E',
204 addULEB128('E');
205
206 // and the name of the type.
207 addString(Name);
208
209 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
210 // here's the relevant spec text to implement:
211 //
212 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
213 // the context is omitted and the name to be used is the ABI-specific name
214 // of the subprogram (e.g., the mangled linker name).
215}
216
217void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
218 unsigned DieNumber) {
219 // a) If T is in the list of [previously hashed types], use the letter
220 // 'R' as the marker
221 addULEB128('R');
222
223 addULEB128(Attribute);
224
225 // and use the unsigned LEB128 encoding of [the index of T in the
226 // list] as the attribute value;
227 addULEB128(DieNumber);
228}
229
230void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
David Blaikie2aee7be2013-10-24 18:29:03 +0000231 const DIE &Entry) {
David Blaikieafcb9652013-10-24 17:51:43 +0000232 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
233 "tags. Add support here when there's "
234 "a use case");
235 // Step 5
236 // If the tag in Step 3 is one of [the below tags]
237 if ((Tag == dwarf::DW_TAG_pointer_type ||
238 Tag == dwarf::DW_TAG_reference_type ||
239 Tag == dwarf::DW_TAG_rvalue_reference_type ||
240 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
241 // and the referenced type (via the [below attributes])
242 // FIXME: This seems overly restrictive, and causes hash mismatches
243 // there's a decl/def difference in the containing type of a
244 // ptr_to_member_type, but it's what DWARF says, for some reason.
245 Attribute == dwarf::DW_AT_type) {
David Blaikie32744412013-10-24 17:53:58 +0000246 // ... has a DW_AT_name attribute,
247 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
248 if (!Name.empty()) {
249 hashShallowTypeReference(Attribute, Entry, Name);
250 return;
251 }
David Blaikieafcb9652013-10-24 17:51:43 +0000252 }
253
254 unsigned &DieNumber = Numbering[&Entry];
255 if (DieNumber) {
256 hashRepeatedTypeReference(Attribute, DieNumber);
257 return;
258 }
259
Robin Morisset039781e2014-08-29 21:53:01 +0000260 // otherwise, b) use the letter 'T' as the marker, ...
David Blaikieafcb9652013-10-24 17:51:43 +0000261 addULEB128('T');
262
263 addULEB128(Attribute);
264
265 // ... process the type T recursively by performing Steps 2 through 7, and
266 // use the result as the attribute value.
267 DieNumber = Numbering.size();
David Blaikie2aee7be2013-10-24 18:29:03 +0000268 computeHash(Entry);
David Blaikieafcb9652013-10-24 17:51:43 +0000269}
270
Eric Christopher420569b2014-02-20 02:50:45 +0000271// Hash all of the values in a block like set of values. This assumes that
272// all of the data is going to be added as integers.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000273void DIEHash::hashBlockData(const SmallVectorImpl<DIEValue> &Values) {
274 for (auto I = Values.begin(), E = Values.end(); I != E; ++I)
275 Hash.update((uint64_t)I->getDIEInteger().getValue());
Eric Christopher420569b2014-02-20 02:50:45 +0000276}
277
Eric Christopher4f17ee02014-03-08 00:29:41 +0000278// Hash the contents of a loclistptr class.
279void DIEHash::hashLocList(const DIELocList &LocList) {
Eric Christopher4f17ee02014-03-08 00:29:41 +0000280 HashingByteStreamer Streamer(*this);
David Blaikie0a456de2014-04-02 01:43:18 +0000281 DwarfDebug &DD = *AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000282 const DebugLocStream &Locs = DD.getDebugLocs();
283 for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
David Blaikie0a456de2014-04-02 01:43:18 +0000284 DD.emitDebugLocEntry(Streamer, Entry);
Eric Christopher4f17ee02014-03-08 00:29:41 +0000285}
286
Eric Christopherd29614f2013-08-13 01:21:55 +0000287// Hash an individual attribute \param Attr based on the type of attribute and
288// the form.
David Blaikie6cf58c82013-10-21 22:36:50 +0000289void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000290 const DIEValue &Value = Attr.Val;
Eric Christopherd29614f2013-08-13 01:21:55 +0000291 const DIEAbbrevData *Desc = Attr.Desc;
David Blaikied70a0552013-10-22 18:14:41 +0000292 dwarf::Attribute Attribute = Desc->getAttribute();
Eric Christopherd29614f2013-08-13 01:21:55 +0000293
Eric Christopher4b1cf582014-01-31 20:02:58 +0000294 // Other attribute values use the letter 'A' as the marker, and the value
295 // consists of the form code (encoded as an unsigned LEB128 value) followed by
296 // the encoding of the value according to the form code. To ensure
297 // reproducibility of the signature, the set of forms used in the signature
298 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
299 // DW_FORM_string, and DW_FORM_block.
Eric Christopher19308492014-03-06 00:38:32 +0000300
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000301 switch (Value.getType()) {
302 case DIEValue::isNone:
303 llvm_unreachable("Expected valid DIEValue");
304
Eric Christopher2bed2572014-03-06 18:59:42 +0000305 // 7.27 Step 3
306 // ... An attribute that refers to another type entry T is processed as
307 // follows:
308 case DIEValue::isEntry:
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000309 hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
Eric Christopher2bed2572014-03-06 18:59:42 +0000310 break;
Eric Christopher19308492014-03-06 00:38:32 +0000311 case DIEValue::isInteger: {
312 addULEB128('A');
313 addULEB128(Attribute);
314 switch (Desc->getForm()) {
315 case dwarf::DW_FORM_data1:
316 case dwarf::DW_FORM_data2:
317 case dwarf::DW_FORM_data4:
318 case dwarf::DW_FORM_data8:
319 case dwarf::DW_FORM_udata:
320 case dwarf::DW_FORM_sdata:
321 addULEB128(dwarf::DW_FORM_sdata);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000322 addSLEB128((int64_t)Value.getDIEInteger().getValue());
Eric Christopher19308492014-03-06 00:38:32 +0000323 break;
324 // DW_FORM_flag_present is just flag with a value of one. We still give it a
325 // value so just use the value.
326 case dwarf::DW_FORM_flag_present:
327 case dwarf::DW_FORM_flag:
328 addULEB128(dwarf::DW_FORM_flag);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000329 addULEB128((int64_t)Value.getDIEInteger().getValue());
Eric Christopher19308492014-03-06 00:38:32 +0000330 break;
331 default:
332 llvm_unreachable("Unknown integer form!");
333 }
334 break;
335 }
336 case DIEValue::isString:
Eric Christopher4b1cf582014-01-31 20:02:58 +0000337 addULEB128('A');
338 addULEB128(Attribute);
David Blaikie6316ca42013-10-16 23:36:20 +0000339 addULEB128(dwarf::DW_FORM_string);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000340 addString(Value.getDIEString().getString());
Eric Christopherd29614f2013-08-13 01:21:55 +0000341 break;
Eric Christopher19308492014-03-06 00:38:32 +0000342 case DIEValue::isBlock:
343 case DIEValue::isLoc:
Eric Christopher4f17ee02014-03-08 00:29:41 +0000344 case DIEValue::isLocList:
Eric Christopher420569b2014-02-20 02:50:45 +0000345 addULEB128('A');
346 addULEB128(Attribute);
347 addULEB128(dwarf::DW_FORM_block);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000348 if (Value.getType() == DIEValue::isBlock) {
349 addULEB128(Value.getDIEBlock().ComputeSize(AP));
350 hashBlockData(Value.getDIEBlock().getValues());
351 } else if (Value.getType() == DIEValue::isLoc) {
352 addULEB128(Value.getDIELoc().ComputeSize(AP));
353 hashBlockData(Value.getDIELoc().getValues());
Eric Christopher4f17ee02014-03-08 00:29:41 +0000354 } else {
355 // We could add the block length, but that would take
356 // a bit of work and not add a lot of uniqueness
357 // to the hash in some way we could test.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000358 hashLocList(Value.getDIELocList());
Eric Christopher420569b2014-02-20 02:50:45 +0000359 }
360 break;
Eric Christopher6bb07f62014-03-06 01:32:56 +0000361 // FIXME: It's uncertain whether or not we should handle this at the moment.
Eric Christopher19308492014-03-06 00:38:32 +0000362 case DIEValue::isExpr:
363 case DIEValue::isLabel:
364 case DIEValue::isDelta:
Eric Christopher19308492014-03-06 00:38:32 +0000365 case DIEValue::isTypeSignature:
Eric Christopher19308492014-03-06 00:38:32 +0000366 llvm_unreachable("Add support for additional value types.");
Eric Christopherd29614f2013-08-13 01:21:55 +0000367 }
368}
369
370// Go through the attributes from \param Attrs in the order specified in 7.27.4
371// and hash them.
David Blaikie6cf58c82013-10-21 22:36:50 +0000372void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000373#define ADD_ATTR(ATTR) \
374 { \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000375 if (ATTR.Val) \
David Blaikie6cf58c82013-10-21 22:36:50 +0000376 hashAttribute(ATTR, Tag); \
Eric Christopherd29614f2013-08-13 01:21:55 +0000377 }
378
Eric Christopherd29614f2013-08-13 01:21:55 +0000379 ADD_ATTR(Attrs.DW_AT_name);
Eric Christophere020fa72013-09-03 20:00:20 +0000380 ADD_ATTR(Attrs.DW_AT_accessibility);
381 ADD_ATTR(Attrs.DW_AT_address_class);
382 ADD_ATTR(Attrs.DW_AT_allocated);
383 ADD_ATTR(Attrs.DW_AT_artificial);
384 ADD_ATTR(Attrs.DW_AT_associated);
385 ADD_ATTR(Attrs.DW_AT_binary_scale);
386 ADD_ATTR(Attrs.DW_AT_bit_offset);
387 ADD_ATTR(Attrs.DW_AT_bit_size);
388 ADD_ATTR(Attrs.DW_AT_bit_stride);
389 ADD_ATTR(Attrs.DW_AT_byte_size);
390 ADD_ATTR(Attrs.DW_AT_byte_stride);
391 ADD_ATTR(Attrs.DW_AT_const_expr);
392 ADD_ATTR(Attrs.DW_AT_const_value);
393 ADD_ATTR(Attrs.DW_AT_containing_type);
394 ADD_ATTR(Attrs.DW_AT_count);
395 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
396 ADD_ATTR(Attrs.DW_AT_data_location);
397 ADD_ATTR(Attrs.DW_AT_data_member_location);
398 ADD_ATTR(Attrs.DW_AT_decimal_scale);
399 ADD_ATTR(Attrs.DW_AT_decimal_sign);
400 ADD_ATTR(Attrs.DW_AT_default_value);
401 ADD_ATTR(Attrs.DW_AT_digit_count);
402 ADD_ATTR(Attrs.DW_AT_discr);
403 ADD_ATTR(Attrs.DW_AT_discr_list);
404 ADD_ATTR(Attrs.DW_AT_discr_value);
405 ADD_ATTR(Attrs.DW_AT_encoding);
406 ADD_ATTR(Attrs.DW_AT_enum_class);
407 ADD_ATTR(Attrs.DW_AT_endianity);
408 ADD_ATTR(Attrs.DW_AT_explicit);
409 ADD_ATTR(Attrs.DW_AT_is_optional);
410 ADD_ATTR(Attrs.DW_AT_location);
411 ADD_ATTR(Attrs.DW_AT_lower_bound);
412 ADD_ATTR(Attrs.DW_AT_mutable);
413 ADD_ATTR(Attrs.DW_AT_ordering);
414 ADD_ATTR(Attrs.DW_AT_picture_string);
415 ADD_ATTR(Attrs.DW_AT_prototyped);
416 ADD_ATTR(Attrs.DW_AT_small);
417 ADD_ATTR(Attrs.DW_AT_segment);
418 ADD_ATTR(Attrs.DW_AT_string_length);
419 ADD_ATTR(Attrs.DW_AT_threads_scaled);
420 ADD_ATTR(Attrs.DW_AT_upper_bound);
421 ADD_ATTR(Attrs.DW_AT_use_location);
422 ADD_ATTR(Attrs.DW_AT_use_UTF8);
423 ADD_ATTR(Attrs.DW_AT_variable_parameter);
424 ADD_ATTR(Attrs.DW_AT_virtuality);
425 ADD_ATTR(Attrs.DW_AT_visibility);
426 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
David Blaikieca353be2013-10-17 22:07:09 +0000427 ADD_ATTR(Attrs.DW_AT_type);
Eric Christophere020fa72013-09-03 20:00:20 +0000428
429 // FIXME: Add the extended attributes.
Eric Christopherd29614f2013-08-13 01:21:55 +0000430}
431
432// Add all of the attributes for \param Die to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +0000433void DIEHash::addAttributes(const DIE &Die) {
David Blaikie94ded5f2013-10-16 00:47:21 +0000434 DIEAttrs Attrs = {};
David Blaikied0d6fcc2013-08-14 22:23:05 +0000435 collectAttributes(Die, Attrs);
David Blaikie2aee7be2013-10-24 18:29:03 +0000436 hashAttributes(Attrs, Die.getTag());
Eric Christopherd29614f2013-08-13 01:21:55 +0000437}
438
David Blaikie65cc9692013-10-25 18:38:43 +0000439void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
440 // 7.27 Step 7
441 // ... append the letter 'S',
442 addULEB128('S');
443
444 // the tag of C,
445 addULEB128(Die.getTag());
446
447 // and the name.
448 addString(Name);
449}
450
Eric Christopherd29614f2013-08-13 01:21:55 +0000451// Compute the hash of a DIE. This is based on the type signature computation
452// given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
453// flattened description of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000454void DIEHash::computeHash(const DIE &Die) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000455 // Append the letter 'D', followed by the DWARF tag of the DIE.
456 addULEB128('D');
David Blaikie2aee7be2013-10-24 18:29:03 +0000457 addULEB128(Die.getTag());
Eric Christopherd29614f2013-08-13 01:21:55 +0000458
459 // Add each of the attributes of the DIE.
460 addAttributes(Die);
461
462 // Then hash each of the children of the DIE.
David Blaikieb8184182014-04-14 22:45:02 +0000463 for (auto &C : Die.getChildren()) {
David Blaikie65cc9692013-10-25 18:38:43 +0000464 // 7.27 Step 7
465 // If C is a nested type entry or a member function entry, ...
David Blaikieb8184182014-04-14 22:45:02 +0000466 if (isType(C->getTag()) || C->getTag() == dwarf::DW_TAG_subprogram) {
467 StringRef Name = getDIEStringAttr(*C, dwarf::DW_AT_name);
David Blaikie65cc9692013-10-25 18:38:43 +0000468 // ... and has a DW_AT_name attribute
469 if (!Name.empty()) {
David Blaikieb8184182014-04-14 22:45:02 +0000470 hashNestedType(*C, Name);
David Blaikie65cc9692013-10-25 18:38:43 +0000471 continue;
472 }
473 }
David Blaikieb8184182014-04-14 22:45:02 +0000474 computeHash(*C);
David Blaikie65cc9692013-10-25 18:38:43 +0000475 }
David Blaikie71a0ad62013-10-16 20:29:06 +0000476
477 // Following the last (or if there are no children), append a zero byte.
David Blaikie920bb2a2013-10-16 20:40:46 +0000478 Hash.update(makeArrayRef((uint8_t)'\0'));
Eric Christopherd29614f2013-08-13 01:21:55 +0000479}
480
Eric Christopher45731982013-08-08 23:45:55 +0000481/// This is based on the type signature computation given in section 7.27 of the
482/// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
Eric Christophercede3db2013-08-12 23:59:24 +0000483/// with the exception that we are hashing only the context and the name of the
484/// type.
David Blaikie2aee7be2013-10-24 18:29:03 +0000485uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
Eric Christopher45731982013-08-08 23:45:55 +0000486
487 // Add the contexts to the hash. We won't be computing the ODR hash for
488 // function local types so it's safe to use the generic context hashing
489 // algorithm here.
490 // FIXME: If we figure out how to account for linkage in some way we could
491 // actually do this with a slight modification to the parent hash algorithm.
David Blaikie2aee7be2013-10-24 18:29:03 +0000492 if (const DIE *Parent = Die.getParent())
493 addParentContext(*Parent);
Eric Christopher45731982013-08-08 23:45:55 +0000494
495 // Add the current DIE information.
496
497 // Add the DWARF tag of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000498 addULEB128(Die.getTag());
Eric Christopher45731982013-08-08 23:45:55 +0000499
500 // Add the name of the type to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +0000501 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
Eric Christopher45731982013-08-08 23:45:55 +0000502
503 // Now get the result.
504 MD5::MD5Result Result;
505 Hash.final(Result);
506
507 // ... take the least significant 8 bytes and return those. Our MD5
508 // implementation always returns its results in little endian, swap bytes
509 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000510 return support::endian::read64le(Result + 8);
Eric Christopher45731982013-08-08 23:45:55 +0000511}
Eric Christopherd29614f2013-08-13 01:21:55 +0000512
513/// This is based on the type signature computation given in section 7.27 of the
514/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
515/// with the inclusion of the full CU and all top level CU entities.
Eric Christopher25b7adc2013-09-03 21:57:57 +0000516// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
David Blaikie2aee7be2013-10-24 18:29:03 +0000517uint64_t DIEHash::computeCUSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000518 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000519 Numbering[&Die] = 1;
Eric Christopherd29614f2013-08-13 01:21:55 +0000520
521 // Hash the DIE.
522 computeHash(Die);
523
524 // Now return the result.
525 MD5::MD5Result Result;
526 Hash.final(Result);
527
528 // ... take the least significant 8 bytes and return those. Our MD5
529 // implementation always returns its results in little endian, swap bytes
530 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000531 return support::endian::read64le(Result + 8);
Eric Christopherd29614f2013-08-13 01:21:55 +0000532}
Eric Christopher25b7adc2013-09-03 21:57:57 +0000533
534/// This is based on the type signature computation given in section 7.27 of the
535/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
536/// with the inclusion of additional forms not specifically called out in the
537/// standard.
David Blaikie2aee7be2013-10-24 18:29:03 +0000538uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000539 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000540 Numbering[&Die] = 1;
Eric Christopher25b7adc2013-09-03 21:57:57 +0000541
David Blaikie2aee7be2013-10-24 18:29:03 +0000542 if (const DIE *Parent = Die.getParent())
543 addParentContext(*Parent);
David Blaikie8a142aa2013-10-17 00:10:34 +0000544
Eric Christopher25b7adc2013-09-03 21:57:57 +0000545 // Hash the DIE.
546 computeHash(Die);
547
548 // Now return the result.
549 MD5::MD5Result Result;
550 Hash.final(Result);
551
552 // ... take the least significant 8 bytes and return those. Our MD5
553 // implementation always returns its results in little endian, swap bytes
554 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000555 return support::endian::read64le(Result + 8);
Eric Christopher25b7adc2013-09-03 21:57:57 +0000556}