blob: 30f38658b34a202ed46d005f541145a85591d296 [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();
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) {
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000039 if (Values[i].getAttribute() == Attr)
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +000040 return Values[i].getDIEString().getString();
Eric Christopher45731982013-08-08 23:45:55 +000041 }
42 return StringRef("");
43}
44
45/// \brief Adds the string in \p Str to the hash. This also hashes
46/// a trailing NULL with the string.
47void DIEHash::addString(StringRef Str) {
48 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
49 Hash.update(Str);
50 Hash.update(makeArrayRef((uint8_t)'\0'));
51}
52
53// FIXME: The LEB128 routines are copied and only slightly modified out of
54// LEB128.h.
55
56/// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
57void DIEHash::addULEB128(uint64_t Value) {
58 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
59 do {
60 uint8_t Byte = Value & 0x7f;
61 Value >>= 7;
62 if (Value != 0)
63 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
64 Hash.update(Byte);
65 } while (Value != 0);
66}
67
David Blaikie6316ca42013-10-16 23:36:20 +000068void DIEHash::addSLEB128(int64_t Value) {
69 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
70 bool More;
71 do {
72 uint8_t Byte = Value & 0x7f;
73 Value >>= 7;
Eric Christophera1b87fd2014-02-20 02:40:41 +000074 More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
David Blaikie6316ca42013-10-16 23:36:20 +000075 ((Value == -1) && ((Byte & 0x40) != 0))));
76 if (More)
77 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
78 Hash.update(Byte);
79 } while (More);
80}
81
Eric Christopher45731982013-08-08 23:45:55 +000082/// \brief Including \p Parent adds the context of Parent to the hash..
David Blaikie2aee7be2013-10-24 18:29:03 +000083void DIEHash::addParentContext(const DIE &Parent) {
Eric Christopher45731982013-08-08 23:45:55 +000084
85 DEBUG(dbgs() << "Adding parent context to hash...\n");
86
87 // [7.27.2] For each surrounding type or namespace beginning with the
88 // outermost such construct...
David Blaikie2aee7be2013-10-24 18:29:03 +000089 SmallVector<const DIE *, 1> Parents;
90 const DIE *Cur = &Parent;
David Blaikie409dd9c2013-11-19 23:08:21 +000091 while (Cur->getParent()) {
David Blaikie2aee7be2013-10-24 18:29:03 +000092 Parents.push_back(Cur);
93 Cur = Cur->getParent();
Eric Christopher45731982013-08-08 23:45:55 +000094 }
David Blaikie409dd9c2013-11-19 23:08:21 +000095 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
96 Cur->getTag() == dwarf::DW_TAG_type_unit);
Eric Christopher45731982013-08-08 23:45:55 +000097
98 // Reverse iterate over our list to go from the outermost construct to the
99 // innermost.
David Blaikie2aee7be2013-10-24 18:29:03 +0000100 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
101 E = Parents.rend();
Eric Christopher45731982013-08-08 23:45:55 +0000102 I != E; ++I) {
David Blaikie2aee7be2013-10-24 18:29:03 +0000103 const DIE &Die = **I;
Eric Christopher45731982013-08-08 23:45:55 +0000104
105 // ... Append the letter "C" to the sequence...
106 addULEB128('C');
107
108 // ... Followed by the DWARF tag of the construct...
David Blaikie2aee7be2013-10-24 18:29:03 +0000109 addULEB128(Die.getTag());
Eric Christopher45731982013-08-08 23:45:55 +0000110
111 // ... Then the name, taken from the DW_AT_name attribute.
David Blaikie2aee7be2013-10-24 18:29:03 +0000112 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
Eric Christopher45731982013-08-08 23:45:55 +0000113 DEBUG(dbgs() << "... adding context: " << Name << "\n");
114 if (!Name.empty())
115 addString(Name);
116 }
117}
118
Eric Christopherd29614f2013-08-13 01:21:55 +0000119// Collect all of the attributes for a particular DIE in single structure.
David Blaikie2aee7be2013-10-24 18:29:03 +0000120void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000121 const SmallVectorImpl<DIEValue> &Values = Die.getValues();
Eric Christopherd29614f2013-08-13 01:21:55 +0000122
123#define COLLECT_ATTR(NAME) \
David Blaikie01fae512013-10-17 22:14:08 +0000124 case dwarf::NAME: \
125 Attrs.NAME.Val = Values[i]; \
David Blaikie01fae512013-10-17 22:14:08 +0000126 break
Eric Christopherd29614f2013-08-13 01:21:55 +0000127
128 for (size_t i = 0, e = Values.size(); i != e; ++i) {
129 DEBUG(dbgs() << "Attribute: "
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000130 << dwarf::AttributeString(Values[i].getAttribute())
Eric Christopherd29614f2013-08-13 01:21:55 +0000131 << " added.\n");
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000132 switch (Values[i].getAttribute()) {
Eric Christophera1b87fd2014-02-20 02:40:41 +0000133 COLLECT_ATTR(DW_AT_name);
134 COLLECT_ATTR(DW_AT_accessibility);
135 COLLECT_ATTR(DW_AT_address_class);
136 COLLECT_ATTR(DW_AT_allocated);
137 COLLECT_ATTR(DW_AT_artificial);
138 COLLECT_ATTR(DW_AT_associated);
139 COLLECT_ATTR(DW_AT_binary_scale);
140 COLLECT_ATTR(DW_AT_bit_offset);
141 COLLECT_ATTR(DW_AT_bit_size);
142 COLLECT_ATTR(DW_AT_bit_stride);
143 COLLECT_ATTR(DW_AT_byte_size);
144 COLLECT_ATTR(DW_AT_byte_stride);
145 COLLECT_ATTR(DW_AT_const_expr);
146 COLLECT_ATTR(DW_AT_const_value);
147 COLLECT_ATTR(DW_AT_containing_type);
148 COLLECT_ATTR(DW_AT_count);
149 COLLECT_ATTR(DW_AT_data_bit_offset);
150 COLLECT_ATTR(DW_AT_data_location);
151 COLLECT_ATTR(DW_AT_data_member_location);
152 COLLECT_ATTR(DW_AT_decimal_scale);
153 COLLECT_ATTR(DW_AT_decimal_sign);
154 COLLECT_ATTR(DW_AT_default_value);
155 COLLECT_ATTR(DW_AT_digit_count);
156 COLLECT_ATTR(DW_AT_discr);
157 COLLECT_ATTR(DW_AT_discr_list);
158 COLLECT_ATTR(DW_AT_discr_value);
159 COLLECT_ATTR(DW_AT_encoding);
160 COLLECT_ATTR(DW_AT_enum_class);
161 COLLECT_ATTR(DW_AT_endianity);
162 COLLECT_ATTR(DW_AT_explicit);
163 COLLECT_ATTR(DW_AT_is_optional);
164 COLLECT_ATTR(DW_AT_location);
165 COLLECT_ATTR(DW_AT_lower_bound);
166 COLLECT_ATTR(DW_AT_mutable);
167 COLLECT_ATTR(DW_AT_ordering);
168 COLLECT_ATTR(DW_AT_picture_string);
169 COLLECT_ATTR(DW_AT_prototyped);
170 COLLECT_ATTR(DW_AT_small);
171 COLLECT_ATTR(DW_AT_segment);
172 COLLECT_ATTR(DW_AT_string_length);
173 COLLECT_ATTR(DW_AT_threads_scaled);
174 COLLECT_ATTR(DW_AT_upper_bound);
175 COLLECT_ATTR(DW_AT_use_location);
176 COLLECT_ATTR(DW_AT_use_UTF8);
177 COLLECT_ATTR(DW_AT_variable_parameter);
178 COLLECT_ATTR(DW_AT_virtuality);
179 COLLECT_ATTR(DW_AT_visibility);
180 COLLECT_ATTR(DW_AT_vtable_elem_location);
181 COLLECT_ATTR(DW_AT_type);
Eric Christopherd29614f2013-08-13 01:21:55 +0000182 default:
183 break;
184 }
185 }
186}
187
David Blaikieafcb9652013-10-24 17:51:43 +0000188void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
189 const DIE &Entry, StringRef Name) {
190 // append the letter 'N'
191 addULEB128('N');
192
193 // the DWARF attribute code (DW_AT_type or DW_AT_friend),
194 addULEB128(Attribute);
195
196 // the context of the tag,
David Blaikie2aee7be2013-10-24 18:29:03 +0000197 if (const DIE *Parent = Entry.getParent())
198 addParentContext(*Parent);
David Blaikieafcb9652013-10-24 17:51:43 +0000199
200 // the letter 'E',
201 addULEB128('E');
202
203 // and the name of the type.
204 addString(Name);
205
206 // Currently DW_TAG_friends are not used by Clang, but if they do become so,
207 // here's the relevant spec text to implement:
208 //
209 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
210 // the context is omitted and the name to be used is the ABI-specific name
211 // of the subprogram (e.g., the mangled linker name).
212}
213
214void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
215 unsigned DieNumber) {
216 // a) If T is in the list of [previously hashed types], use the letter
217 // 'R' as the marker
218 addULEB128('R');
219
220 addULEB128(Attribute);
221
222 // and use the unsigned LEB128 encoding of [the index of T in the
223 // list] as the attribute value;
224 addULEB128(DieNumber);
225}
226
227void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
David Blaikie2aee7be2013-10-24 18:29:03 +0000228 const DIE &Entry) {
David Blaikieafcb9652013-10-24 17:51:43 +0000229 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
230 "tags. Add support here when there's "
231 "a use case");
232 // Step 5
233 // If the tag in Step 3 is one of [the below tags]
234 if ((Tag == dwarf::DW_TAG_pointer_type ||
235 Tag == dwarf::DW_TAG_reference_type ||
236 Tag == dwarf::DW_TAG_rvalue_reference_type ||
237 Tag == dwarf::DW_TAG_ptr_to_member_type) &&
238 // and the referenced type (via the [below attributes])
239 // FIXME: This seems overly restrictive, and causes hash mismatches
240 // there's a decl/def difference in the containing type of a
241 // ptr_to_member_type, but it's what DWARF says, for some reason.
242 Attribute == dwarf::DW_AT_type) {
David Blaikie32744412013-10-24 17:53:58 +0000243 // ... has a DW_AT_name attribute,
244 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
245 if (!Name.empty()) {
246 hashShallowTypeReference(Attribute, Entry, Name);
247 return;
248 }
David Blaikieafcb9652013-10-24 17:51:43 +0000249 }
250
251 unsigned &DieNumber = Numbering[&Entry];
252 if (DieNumber) {
253 hashRepeatedTypeReference(Attribute, DieNumber);
254 return;
255 }
256
Robin Morisset039781e2014-08-29 21:53:01 +0000257 // otherwise, b) use the letter 'T' as the marker, ...
David Blaikieafcb9652013-10-24 17:51:43 +0000258 addULEB128('T');
259
260 addULEB128(Attribute);
261
262 // ... process the type T recursively by performing Steps 2 through 7, and
263 // use the result as the attribute value.
264 DieNumber = Numbering.size();
David Blaikie2aee7be2013-10-24 18:29:03 +0000265 computeHash(Entry);
David Blaikieafcb9652013-10-24 17:51:43 +0000266}
267
Eric Christopher420569b2014-02-20 02:50:45 +0000268// Hash all of the values in a block like set of values. This assumes that
269// all of the data is going to be added as integers.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000270void DIEHash::hashBlockData(const SmallVectorImpl<DIEValue> &Values) {
271 for (auto I = Values.begin(), E = Values.end(); I != E; ++I)
272 Hash.update((uint64_t)I->getDIEInteger().getValue());
Eric Christopher420569b2014-02-20 02:50:45 +0000273}
274
Eric Christopher4f17ee02014-03-08 00:29:41 +0000275// Hash the contents of a loclistptr class.
276void DIEHash::hashLocList(const DIELocList &LocList) {
Eric Christopher4f17ee02014-03-08 00:29:41 +0000277 HashingByteStreamer Streamer(*this);
David Blaikie0a456de2014-04-02 01:43:18 +0000278 DwarfDebug &DD = *AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000279 const DebugLocStream &Locs = DD.getDebugLocs();
280 for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
David Blaikie0a456de2014-04-02 01:43:18 +0000281 DD.emitDebugLocEntry(Streamer, Entry);
Eric Christopher4f17ee02014-03-08 00:29:41 +0000282}
283
Eric Christopherd29614f2013-08-13 01:21:55 +0000284// Hash an individual attribute \param Attr based on the type of attribute and
285// the form.
David Blaikie6cf58c82013-10-21 22:36:50 +0000286void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000287 const DIEValue &Value = Attr.Val;
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000288 dwarf::Attribute Attribute = Value.getAttribute();
Eric Christopherd29614f2013-08-13 01:21:55 +0000289
Eric Christopher4b1cf582014-01-31 20:02:58 +0000290 // Other attribute values use the letter 'A' as the marker, and the value
291 // consists of the form code (encoded as an unsigned LEB128 value) followed by
292 // the encoding of the value according to the form code. To ensure
293 // reproducibility of the signature, the set of forms used in the signature
294 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
295 // DW_FORM_string, and DW_FORM_block.
Eric Christopher19308492014-03-06 00:38:32 +0000296
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000297 switch (Value.getType()) {
298 case DIEValue::isNone:
299 llvm_unreachable("Expected valid DIEValue");
300
Eric Christopher2bed2572014-03-06 18:59:42 +0000301 // 7.27 Step 3
302 // ... An attribute that refers to another type entry T is processed as
303 // follows:
304 case DIEValue::isEntry:
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000305 hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
Eric Christopher2bed2572014-03-06 18:59:42 +0000306 break;
Eric Christopher19308492014-03-06 00:38:32 +0000307 case DIEValue::isInteger: {
308 addULEB128('A');
309 addULEB128(Attribute);
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000310 switch (Value.getForm()) {
Eric Christopher19308492014-03-06 00:38:32 +0000311 case dwarf::DW_FORM_data1:
312 case dwarf::DW_FORM_data2:
313 case dwarf::DW_FORM_data4:
314 case dwarf::DW_FORM_data8:
315 case dwarf::DW_FORM_udata:
316 case dwarf::DW_FORM_sdata:
317 addULEB128(dwarf::DW_FORM_sdata);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000318 addSLEB128((int64_t)Value.getDIEInteger().getValue());
Eric Christopher19308492014-03-06 00:38:32 +0000319 break;
320 // DW_FORM_flag_present is just flag with a value of one. We still give it a
321 // value so just use the value.
322 case dwarf::DW_FORM_flag_present:
323 case dwarf::DW_FORM_flag:
324 addULEB128(dwarf::DW_FORM_flag);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000325 addULEB128((int64_t)Value.getDIEInteger().getValue());
Eric Christopher19308492014-03-06 00:38:32 +0000326 break;
327 default:
328 llvm_unreachable("Unknown integer form!");
329 }
330 break;
331 }
332 case DIEValue::isString:
Eric Christopher4b1cf582014-01-31 20:02:58 +0000333 addULEB128('A');
334 addULEB128(Attribute);
David Blaikie6316ca42013-10-16 23:36:20 +0000335 addULEB128(dwarf::DW_FORM_string);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000336 addString(Value.getDIEString().getString());
Eric Christopherd29614f2013-08-13 01:21:55 +0000337 break;
Eric Christopher19308492014-03-06 00:38:32 +0000338 case DIEValue::isBlock:
339 case DIEValue::isLoc:
Eric Christopher4f17ee02014-03-08 00:29:41 +0000340 case DIEValue::isLocList:
Eric Christopher420569b2014-02-20 02:50:45 +0000341 addULEB128('A');
342 addULEB128(Attribute);
343 addULEB128(dwarf::DW_FORM_block);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000344 if (Value.getType() == DIEValue::isBlock) {
345 addULEB128(Value.getDIEBlock().ComputeSize(AP));
346 hashBlockData(Value.getDIEBlock().getValues());
347 } else if (Value.getType() == DIEValue::isLoc) {
348 addULEB128(Value.getDIELoc().ComputeSize(AP));
349 hashBlockData(Value.getDIELoc().getValues());
Eric Christopher4f17ee02014-03-08 00:29:41 +0000350 } else {
351 // We could add the block length, but that would take
352 // a bit of work and not add a lot of uniqueness
353 // to the hash in some way we could test.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000354 hashLocList(Value.getDIELocList());
Eric Christopher420569b2014-02-20 02:50:45 +0000355 }
356 break;
Eric Christopher6bb07f62014-03-06 01:32:56 +0000357 // FIXME: It's uncertain whether or not we should handle this at the moment.
Eric Christopher19308492014-03-06 00:38:32 +0000358 case DIEValue::isExpr:
359 case DIEValue::isLabel:
360 case DIEValue::isDelta:
Eric Christopher19308492014-03-06 00:38:32 +0000361 case DIEValue::isTypeSignature:
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 Smithe7e1d0c2015-05-27 22:14:58 +0000371 if (ATTR.Val) \
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.
David Blaikieb8184182014-04-14 22:45:02 +0000459 for (auto &C : Die.getChildren()) {
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, ...
David Blaikieb8184182014-04-14 22:45:02 +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()) {
David Blaikieb8184182014-04-14 22:45:02 +0000466 hashNestedType(*C, Name);
David Blaikie65cc9692013-10-25 18:38:43 +0000467 continue;
468 }
469 }
David Blaikieb8184182014-04-14 22:45:02 +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
478/// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
Eric Christophercede3db2013-08-12 23:59:24 +0000479/// with the exception that we are hashing only the context and the name of the
480/// type.
David Blaikie2aee7be2013-10-24 18:29:03 +0000481uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
Eric Christopher45731982013-08-08 23:45:55 +0000482
483 // Add the contexts to the hash. We won't be computing the ODR hash for
484 // function local types so it's safe to use the generic context hashing
485 // algorithm here.
486 // FIXME: If we figure out how to account for linkage in some way we could
487 // actually do this with a slight modification to the parent hash algorithm.
David Blaikie2aee7be2013-10-24 18:29:03 +0000488 if (const DIE *Parent = Die.getParent())
489 addParentContext(*Parent);
Eric Christopher45731982013-08-08 23:45:55 +0000490
491 // Add the current DIE information.
492
493 // Add the DWARF tag of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000494 addULEB128(Die.getTag());
Eric Christopher45731982013-08-08 23:45:55 +0000495
496 // Add the name of the type to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +0000497 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
Eric Christopher45731982013-08-08 23:45:55 +0000498
499 // Now get 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.
Rui Ueyama3206b792015-03-02 21:19:12 +0000506 return support::endian::read64le(Result + 8);
Eric Christopher45731982013-08-08 23:45:55 +0000507}
Eric Christopherd29614f2013-08-13 01:21:55 +0000508
509/// This is based on the type signature computation given in section 7.27 of the
510/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
511/// with the inclusion of the full CU and all top level CU entities.
Eric Christopher25b7adc2013-09-03 21:57:57 +0000512// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
David Blaikie2aee7be2013-10-24 18:29:03 +0000513uint64_t DIEHash::computeCUSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000514 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000515 Numbering[&Die] = 1;
Eric Christopherd29614f2013-08-13 01:21:55 +0000516
517 // Hash the DIE.
518 computeHash(Die);
519
520 // Now return the result.
521 MD5::MD5Result Result;
522 Hash.final(Result);
523
524 // ... take the least significant 8 bytes and return those. Our MD5
525 // implementation always returns its results in little endian, swap bytes
526 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000527 return support::endian::read64le(Result + 8);
Eric Christopherd29614f2013-08-13 01:21:55 +0000528}
Eric Christopher25b7adc2013-09-03 21:57:57 +0000529
530/// This is based on the type signature computation given in section 7.27 of the
531/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
532/// with the inclusion of additional forms not specifically called out in the
533/// standard.
David Blaikie2aee7be2013-10-24 18:29:03 +0000534uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000535 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000536 Numbering[&Die] = 1;
Eric Christopher25b7adc2013-09-03 21:57:57 +0000537
David Blaikie2aee7be2013-10-24 18:29:03 +0000538 if (const DIE *Parent = Die.getParent())
539 addParentContext(*Parent);
David Blaikie8a142aa2013-10-17 00:10:34 +0000540
Eric Christopher25b7adc2013-09-03 21:57:57 +0000541 // Hash the DIE.
542 computeHash(Die);
543
544 // Now return the result.
545 MD5::MD5Result Result;
546 Hash.final(Result);
547
548 // ... take the least significant 8 bytes and return those. Our MD5
549 // implementation always returns its results in little endian, swap bytes
550 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000551 return support::endian::read64le(Result + 8);
Eric Christopher25b7adc2013-09-03 21:57:57 +0000552}