blob: 2a65760ce22296a22d3a1393513736487d739aab [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: \
Duncan P. N. Exon Smithf3a6a672015-05-27 22:36:37 +0000125 Attrs.NAME = 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.
Duncan P. N. Exon Smithf3a6a672015-05-27 22:36:37 +0000286void DIEHash::hashAttribute(DIEValue Value, dwarf::Tag Tag) {
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000287 dwarf::Attribute Attribute = Value.getAttribute();
Eric Christopherd29614f2013-08-13 01:21:55 +0000288
Eric Christopher4b1cf582014-01-31 20:02:58 +0000289 // Other attribute values use the letter 'A' as the marker, and the value
290 // consists of the form code (encoded as an unsigned LEB128 value) followed by
291 // the encoding of the value according to the form code. To ensure
292 // reproducibility of the signature, the set of forms used in the signature
293 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
294 // DW_FORM_string, and DW_FORM_block.
Eric Christopher19308492014-03-06 00:38:32 +0000295
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000296 switch (Value.getType()) {
297 case DIEValue::isNone:
298 llvm_unreachable("Expected valid DIEValue");
299
Eric Christopher2bed2572014-03-06 18:59:42 +0000300 // 7.27 Step 3
301 // ... An attribute that refers to another type entry T is processed as
302 // follows:
303 case DIEValue::isEntry:
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000304 hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
Eric Christopher2bed2572014-03-06 18:59:42 +0000305 break;
Eric Christopher19308492014-03-06 00:38:32 +0000306 case DIEValue::isInteger: {
307 addULEB128('A');
308 addULEB128(Attribute);
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000309 switch (Value.getForm()) {
Eric Christopher19308492014-03-06 00:38:32 +0000310 case dwarf::DW_FORM_data1:
311 case dwarf::DW_FORM_data2:
312 case dwarf::DW_FORM_data4:
313 case dwarf::DW_FORM_data8:
314 case dwarf::DW_FORM_udata:
315 case dwarf::DW_FORM_sdata:
316 addULEB128(dwarf::DW_FORM_sdata);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000317 addSLEB128((int64_t)Value.getDIEInteger().getValue());
Eric Christopher19308492014-03-06 00:38:32 +0000318 break;
319 // DW_FORM_flag_present is just flag with a value of one. We still give it a
320 // value so just use the value.
321 case dwarf::DW_FORM_flag_present:
322 case dwarf::DW_FORM_flag:
323 addULEB128(dwarf::DW_FORM_flag);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000324 addULEB128((int64_t)Value.getDIEInteger().getValue());
Eric Christopher19308492014-03-06 00:38:32 +0000325 break;
326 default:
327 llvm_unreachable("Unknown integer form!");
328 }
329 break;
330 }
331 case DIEValue::isString:
Eric Christopher4b1cf582014-01-31 20:02:58 +0000332 addULEB128('A');
333 addULEB128(Attribute);
David Blaikie6316ca42013-10-16 23:36:20 +0000334 addULEB128(dwarf::DW_FORM_string);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000335 addString(Value.getDIEString().getString());
Eric Christopherd29614f2013-08-13 01:21:55 +0000336 break;
Eric Christopher19308492014-03-06 00:38:32 +0000337 case DIEValue::isBlock:
338 case DIEValue::isLoc:
Eric Christopher4f17ee02014-03-08 00:29:41 +0000339 case DIEValue::isLocList:
Eric Christopher420569b2014-02-20 02:50:45 +0000340 addULEB128('A');
341 addULEB128(Attribute);
342 addULEB128(dwarf::DW_FORM_block);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000343 if (Value.getType() == DIEValue::isBlock) {
344 addULEB128(Value.getDIEBlock().ComputeSize(AP));
345 hashBlockData(Value.getDIEBlock().getValues());
346 } else if (Value.getType() == DIEValue::isLoc) {
347 addULEB128(Value.getDIELoc().ComputeSize(AP));
348 hashBlockData(Value.getDIELoc().getValues());
Eric Christopher4f17ee02014-03-08 00:29:41 +0000349 } else {
350 // We could add the block length, but that would take
351 // a bit of work and not add a lot of uniqueness
352 // to the hash in some way we could test.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000353 hashLocList(Value.getDIELocList());
Eric Christopher420569b2014-02-20 02:50:45 +0000354 }
355 break;
Eric Christopher6bb07f62014-03-06 01:32:56 +0000356 // FIXME: It's uncertain whether or not we should handle this at the moment.
Eric Christopher19308492014-03-06 00:38:32 +0000357 case DIEValue::isExpr:
358 case DIEValue::isLabel:
359 case DIEValue::isDelta:
Eric Christopher19308492014-03-06 00:38:32 +0000360 case DIEValue::isTypeSignature:
Eric Christopher19308492014-03-06 00:38:32 +0000361 llvm_unreachable("Add support for additional value types.");
Eric Christopherd29614f2013-08-13 01:21:55 +0000362 }
363}
364
365// Go through the attributes from \param Attrs in the order specified in 7.27.4
366// and hash them.
David Blaikie6cf58c82013-10-21 22:36:50 +0000367void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000368#define ADD_ATTR(ATTR) \
369 { \
Duncan P. N. Exon Smithf3a6a672015-05-27 22:36:37 +0000370 if (ATTR) \
David Blaikie6cf58c82013-10-21 22:36:50 +0000371 hashAttribute(ATTR, Tag); \
Eric Christopherd29614f2013-08-13 01:21:55 +0000372 }
373
Eric Christopherd29614f2013-08-13 01:21:55 +0000374 ADD_ATTR(Attrs.DW_AT_name);
Eric Christophere020fa72013-09-03 20:00:20 +0000375 ADD_ATTR(Attrs.DW_AT_accessibility);
376 ADD_ATTR(Attrs.DW_AT_address_class);
377 ADD_ATTR(Attrs.DW_AT_allocated);
378 ADD_ATTR(Attrs.DW_AT_artificial);
379 ADD_ATTR(Attrs.DW_AT_associated);
380 ADD_ATTR(Attrs.DW_AT_binary_scale);
381 ADD_ATTR(Attrs.DW_AT_bit_offset);
382 ADD_ATTR(Attrs.DW_AT_bit_size);
383 ADD_ATTR(Attrs.DW_AT_bit_stride);
384 ADD_ATTR(Attrs.DW_AT_byte_size);
385 ADD_ATTR(Attrs.DW_AT_byte_stride);
386 ADD_ATTR(Attrs.DW_AT_const_expr);
387 ADD_ATTR(Attrs.DW_AT_const_value);
388 ADD_ATTR(Attrs.DW_AT_containing_type);
389 ADD_ATTR(Attrs.DW_AT_count);
390 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
391 ADD_ATTR(Attrs.DW_AT_data_location);
392 ADD_ATTR(Attrs.DW_AT_data_member_location);
393 ADD_ATTR(Attrs.DW_AT_decimal_scale);
394 ADD_ATTR(Attrs.DW_AT_decimal_sign);
395 ADD_ATTR(Attrs.DW_AT_default_value);
396 ADD_ATTR(Attrs.DW_AT_digit_count);
397 ADD_ATTR(Attrs.DW_AT_discr);
398 ADD_ATTR(Attrs.DW_AT_discr_list);
399 ADD_ATTR(Attrs.DW_AT_discr_value);
400 ADD_ATTR(Attrs.DW_AT_encoding);
401 ADD_ATTR(Attrs.DW_AT_enum_class);
402 ADD_ATTR(Attrs.DW_AT_endianity);
403 ADD_ATTR(Attrs.DW_AT_explicit);
404 ADD_ATTR(Attrs.DW_AT_is_optional);
405 ADD_ATTR(Attrs.DW_AT_location);
406 ADD_ATTR(Attrs.DW_AT_lower_bound);
407 ADD_ATTR(Attrs.DW_AT_mutable);
408 ADD_ATTR(Attrs.DW_AT_ordering);
409 ADD_ATTR(Attrs.DW_AT_picture_string);
410 ADD_ATTR(Attrs.DW_AT_prototyped);
411 ADD_ATTR(Attrs.DW_AT_small);
412 ADD_ATTR(Attrs.DW_AT_segment);
413 ADD_ATTR(Attrs.DW_AT_string_length);
414 ADD_ATTR(Attrs.DW_AT_threads_scaled);
415 ADD_ATTR(Attrs.DW_AT_upper_bound);
416 ADD_ATTR(Attrs.DW_AT_use_location);
417 ADD_ATTR(Attrs.DW_AT_use_UTF8);
418 ADD_ATTR(Attrs.DW_AT_variable_parameter);
419 ADD_ATTR(Attrs.DW_AT_virtuality);
420 ADD_ATTR(Attrs.DW_AT_visibility);
421 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
David Blaikieca353be2013-10-17 22:07:09 +0000422 ADD_ATTR(Attrs.DW_AT_type);
Eric Christophere020fa72013-09-03 20:00:20 +0000423
424 // FIXME: Add the extended attributes.
Eric Christopherd29614f2013-08-13 01:21:55 +0000425}
426
427// Add all of the attributes for \param Die to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +0000428void DIEHash::addAttributes(const DIE &Die) {
David Blaikie94ded5f2013-10-16 00:47:21 +0000429 DIEAttrs Attrs = {};
David Blaikied0d6fcc2013-08-14 22:23:05 +0000430 collectAttributes(Die, Attrs);
David Blaikie2aee7be2013-10-24 18:29:03 +0000431 hashAttributes(Attrs, Die.getTag());
Eric Christopherd29614f2013-08-13 01:21:55 +0000432}
433
David Blaikie65cc9692013-10-25 18:38:43 +0000434void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
435 // 7.27 Step 7
436 // ... append the letter 'S',
437 addULEB128('S');
438
439 // the tag of C,
440 addULEB128(Die.getTag());
441
442 // and the name.
443 addString(Name);
444}
445
Eric Christopherd29614f2013-08-13 01:21:55 +0000446// Compute the hash of a DIE. This is based on the type signature computation
447// given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
448// flattened description of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000449void DIEHash::computeHash(const DIE &Die) {
Eric Christopherd29614f2013-08-13 01:21:55 +0000450 // Append the letter 'D', followed by the DWARF tag of the DIE.
451 addULEB128('D');
David Blaikie2aee7be2013-10-24 18:29:03 +0000452 addULEB128(Die.getTag());
Eric Christopherd29614f2013-08-13 01:21:55 +0000453
454 // Add each of the attributes of the DIE.
455 addAttributes(Die);
456
457 // Then hash each of the children of the DIE.
David Blaikieb8184182014-04-14 22:45:02 +0000458 for (auto &C : Die.getChildren()) {
David Blaikie65cc9692013-10-25 18:38:43 +0000459 // 7.27 Step 7
460 // If C is a nested type entry or a member function entry, ...
David Blaikieb8184182014-04-14 22:45:02 +0000461 if (isType(C->getTag()) || C->getTag() == dwarf::DW_TAG_subprogram) {
462 StringRef Name = getDIEStringAttr(*C, dwarf::DW_AT_name);
David Blaikie65cc9692013-10-25 18:38:43 +0000463 // ... and has a DW_AT_name attribute
464 if (!Name.empty()) {
David Blaikieb8184182014-04-14 22:45:02 +0000465 hashNestedType(*C, Name);
David Blaikie65cc9692013-10-25 18:38:43 +0000466 continue;
467 }
468 }
David Blaikieb8184182014-04-14 22:45:02 +0000469 computeHash(*C);
David Blaikie65cc9692013-10-25 18:38:43 +0000470 }
David Blaikie71a0ad62013-10-16 20:29:06 +0000471
472 // Following the last (or if there are no children), append a zero byte.
David Blaikie920bb2a2013-10-16 20:40:46 +0000473 Hash.update(makeArrayRef((uint8_t)'\0'));
Eric Christopherd29614f2013-08-13 01:21:55 +0000474}
475
Eric Christopher45731982013-08-08 23:45:55 +0000476/// This is based on the type signature computation given in section 7.27 of the
477/// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
Eric Christophercede3db2013-08-12 23:59:24 +0000478/// with the exception that we are hashing only the context and the name of the
479/// type.
David Blaikie2aee7be2013-10-24 18:29:03 +0000480uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
Eric Christopher45731982013-08-08 23:45:55 +0000481
482 // Add the contexts to the hash. We won't be computing the ODR hash for
483 // function local types so it's safe to use the generic context hashing
484 // algorithm here.
485 // FIXME: If we figure out how to account for linkage in some way we could
486 // actually do this with a slight modification to the parent hash algorithm.
David Blaikie2aee7be2013-10-24 18:29:03 +0000487 if (const DIE *Parent = Die.getParent())
488 addParentContext(*Parent);
Eric Christopher45731982013-08-08 23:45:55 +0000489
490 // Add the current DIE information.
491
492 // Add the DWARF tag of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +0000493 addULEB128(Die.getTag());
Eric Christopher45731982013-08-08 23:45:55 +0000494
495 // Add the name of the type to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +0000496 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
Eric Christopher45731982013-08-08 23:45:55 +0000497
498 // Now get the result.
499 MD5::MD5Result Result;
500 Hash.final(Result);
501
502 // ... take the least significant 8 bytes and return those. Our MD5
503 // implementation always returns its results in little endian, swap bytes
504 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000505 return support::endian::read64le(Result + 8);
Eric Christopher45731982013-08-08 23:45:55 +0000506}
Eric Christopherd29614f2013-08-13 01:21:55 +0000507
508/// This is based on the type signature computation given in section 7.27 of the
509/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
510/// with the inclusion of the full CU and all top level CU entities.
Eric Christopher25b7adc2013-09-03 21:57:57 +0000511// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
David Blaikie2aee7be2013-10-24 18:29:03 +0000512uint64_t DIEHash::computeCUSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000513 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000514 Numbering[&Die] = 1;
Eric Christopherd29614f2013-08-13 01:21:55 +0000515
516 // Hash the DIE.
517 computeHash(Die);
518
519 // Now return the result.
520 MD5::MD5Result Result;
521 Hash.final(Result);
522
523 // ... take the least significant 8 bytes and return those. Our MD5
524 // implementation always returns its results in little endian, swap bytes
525 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000526 return support::endian::read64le(Result + 8);
Eric Christopherd29614f2013-08-13 01:21:55 +0000527}
Eric Christopher25b7adc2013-09-03 21:57:57 +0000528
529/// This is based on the type signature computation given in section 7.27 of the
530/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
531/// with the inclusion of additional forms not specifically called out in the
532/// standard.
David Blaikie2aee7be2013-10-24 18:29:03 +0000533uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
David Blaikie980d4992013-10-21 18:59:40 +0000534 Numbering.clear();
David Blaikie2aee7be2013-10-24 18:29:03 +0000535 Numbering[&Die] = 1;
Eric Christopher25b7adc2013-09-03 21:57:57 +0000536
David Blaikie2aee7be2013-10-24 18:29:03 +0000537 if (const DIE *Parent = Die.getParent())
538 addParentContext(*Parent);
David Blaikie8a142aa2013-10-17 00:10:34 +0000539
Eric Christopher25b7adc2013-09-03 21:57:57 +0000540 // Hash the DIE.
541 computeHash(Die);
542
543 // Now return the result.
544 MD5::MD5Result Result;
545 Hash.final(Result);
546
547 // ... take the least significant 8 bytes and return those. Our MD5
548 // implementation always returns its results in little endian, swap bytes
549 // appropriately.
Rui Ueyama3206b792015-03-02 21:19:12 +0000550 return support::endian::read64le(Result + 8);
Eric Christopher25b7adc2013-09-03 21:57:57 +0000551}