blob: 8409a7f56c28fa0fffca8e022920723b42e8671f [file] [log] [blame]
Eric Christopher0d27ca12013-08-08 23:45:55 +00001//===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for DWARF4 hashing of DIEs.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
16#include "DIE.h"
17#include "DIEHash.h"
18#include "DwarfCompileUnit.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/StringRef.h"
21#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
29/// \brief Grabs the string in whichever attribute is passed in and returns
30/// a reference to it.
31static StringRef getDIEStringAttr(DIE *Die, uint16_t Attr) {
32 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
33 const DIEAbbrev &Abbrevs = Die->getAbbrev();
34
35 // Iterate through all the attributes until we find the one we're
36 // looking for, if we can't find it return an empty string.
37 for (size_t i = 0; i < Values.size(); ++i) {
38 if (Abbrevs.getData()[i].getAttribute() == Attr) {
39 DIEValue *V = Values[i];
40 assert(isa<DIEString>(V) && "String requested. Not a string.");
41 DIEString *S = cast<DIEString>(V);
42 return S->getString();
43 }
44 }
45 return StringRef("");
46}
47
48/// \brief Adds the string in \p Str to the hash. This also hashes
49/// a trailing NULL with the string.
50void DIEHash::addString(StringRef Str) {
51 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
52 Hash.update(Str);
53 Hash.update(makeArrayRef((uint8_t)'\0'));
54}
55
56// FIXME: The LEB128 routines are copied and only slightly modified out of
57// LEB128.h.
58
59/// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
60void DIEHash::addULEB128(uint64_t Value) {
61 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
62 do {
63 uint8_t Byte = Value & 0x7f;
64 Value >>= 7;
65 if (Value != 0)
66 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
67 Hash.update(Byte);
68 } while (Value != 0);
69}
70
David Blaikiec0987082013-10-16 23:36:20 +000071void DIEHash::addSLEB128(int64_t Value) {
72 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
73 bool More;
74 do {
75 uint8_t Byte = Value & 0x7f;
76 Value >>= 7;
77 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
78 ((Value == -1) && ((Byte & 0x40) != 0))));
79 if (More)
80 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
81 Hash.update(Byte);
82 } while (More);
83}
84
Eric Christopher0d27ca12013-08-08 23:45:55 +000085/// \brief Including \p Parent adds the context of Parent to the hash..
86void DIEHash::addParentContext(DIE *Parent) {
87
88 DEBUG(dbgs() << "Adding parent context to hash...\n");
89
90 // [7.27.2] For each surrounding type or namespace beginning with the
91 // outermost such construct...
92 SmallVector<DIE *, 1> Parents;
93 while (Parent->getTag() != dwarf::DW_TAG_compile_unit) {
94 Parents.push_back(Parent);
95 Parent = Parent->getParent();
96 }
97
98 // Reverse iterate over our list to go from the outermost construct to the
99 // innermost.
100 for (SmallVectorImpl<DIE *>::reverse_iterator I = Parents.rbegin(),
101 E = Parents.rend();
102 I != E; ++I) {
103 DIE *Die = *I;
104
105 // ... Append the letter "C" to the sequence...
106 addULEB128('C');
107
108 // ... Followed by the DWARF tag of the construct...
109 addULEB128(Die->getTag());
110
111 // ... Then the name, taken from the DW_AT_name attribute.
112 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
113 DEBUG(dbgs() << "... adding context: " << Name << "\n");
114 if (!Name.empty())
115 addString(Name);
116 }
117}
118
Eric Christopher0710bfa2013-08-13 01:21:55 +0000119// Collect all of the attributes for a particular DIE in single structure.
David Blaikiecbb5c732013-08-14 22:23:05 +0000120void DIEHash::collectAttributes(DIE *Die, DIEAttrs &Attrs) {
Eric Christopher0710bfa2013-08-13 01:21:55 +0000121 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
122 const DIEAbbrev &Abbrevs = Die->getAbbrev();
123
124#define COLLECT_ATTR(NAME) \
David Blaikiecbb5c732013-08-14 22:23:05 +0000125 Attrs.NAME.Val = Values[i]; \
126 Attrs.NAME.Desc = &Abbrevs.getData()[i];
Eric Christopher0710bfa2013-08-13 01:21:55 +0000127
128 for (size_t i = 0, e = Values.size(); i != e; ++i) {
129 DEBUG(dbgs() << "Attribute: "
130 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
131 << " added.\n");
132 switch (Abbrevs.getData()[i].getAttribute()) {
133 case dwarf::DW_AT_name:
134 COLLECT_ATTR(DW_AT_name);
135 break;
Eric Christopherbd18c8d2013-09-03 20:00:20 +0000136 case dwarf::DW_AT_accessibility:
137 COLLECT_ATTR(DW_AT_accessibility)
138 break;
139 case dwarf::DW_AT_address_class:
140 COLLECT_ATTR(DW_AT_address_class)
141 break;
142 case dwarf::DW_AT_allocated:
143 COLLECT_ATTR(DW_AT_allocated)
144 break;
145 case dwarf::DW_AT_artificial:
146 COLLECT_ATTR(DW_AT_artificial)
147 break;
148 case dwarf::DW_AT_associated:
149 COLLECT_ATTR(DW_AT_associated)
150 break;
151 case dwarf::DW_AT_binary_scale:
152 COLLECT_ATTR(DW_AT_binary_scale)
153 break;
154 case dwarf::DW_AT_bit_offset:
155 COLLECT_ATTR(DW_AT_bit_offset)
156 break;
157 case dwarf::DW_AT_bit_size:
158 COLLECT_ATTR(DW_AT_bit_size)
159 break;
160 case dwarf::DW_AT_bit_stride:
161 COLLECT_ATTR(DW_AT_bit_stride)
162 break;
163 case dwarf::DW_AT_byte_size:
164 COLLECT_ATTR(DW_AT_byte_size)
165 break;
166 case dwarf::DW_AT_byte_stride:
167 COLLECT_ATTR(DW_AT_byte_stride)
168 break;
169 case dwarf::DW_AT_const_expr:
170 COLLECT_ATTR(DW_AT_const_expr)
171 break;
172 case dwarf::DW_AT_const_value:
173 COLLECT_ATTR(DW_AT_const_value)
174 break;
175 case dwarf::DW_AT_containing_type:
176 COLLECT_ATTR(DW_AT_containing_type)
177 break;
178 case dwarf::DW_AT_count:
179 COLLECT_ATTR(DW_AT_count)
180 break;
181 case dwarf::DW_AT_data_bit_offset:
182 COLLECT_ATTR(DW_AT_data_bit_offset)
183 break;
184 case dwarf::DW_AT_data_location:
185 COLLECT_ATTR(DW_AT_data_location)
186 break;
187 case dwarf::DW_AT_data_member_location:
188 COLLECT_ATTR(DW_AT_data_member_location)
189 break;
190 case dwarf::DW_AT_decimal_scale:
191 COLLECT_ATTR(DW_AT_decimal_scale)
192 break;
193 case dwarf::DW_AT_decimal_sign:
194 COLLECT_ATTR(DW_AT_decimal_sign)
195 break;
196 case dwarf::DW_AT_default_value:
197 COLLECT_ATTR(DW_AT_default_value)
198 break;
199 case dwarf::DW_AT_digit_count:
200 COLLECT_ATTR(DW_AT_digit_count)
201 break;
202 case dwarf::DW_AT_discr:
203 COLLECT_ATTR(DW_AT_discr)
204 break;
205 case dwarf::DW_AT_discr_list:
206 COLLECT_ATTR(DW_AT_discr_list)
207 break;
208 case dwarf::DW_AT_discr_value:
209 COLLECT_ATTR(DW_AT_discr_value)
210 break;
211 case dwarf::DW_AT_encoding:
212 COLLECT_ATTR(DW_AT_encoding)
213 break;
214 case dwarf::DW_AT_enum_class:
215 COLLECT_ATTR(DW_AT_enum_class)
216 break;
217 case dwarf::DW_AT_endianity:
218 COLLECT_ATTR(DW_AT_endianity)
219 break;
220 case dwarf::DW_AT_explicit:
221 COLLECT_ATTR(DW_AT_explicit)
222 break;
223 case dwarf::DW_AT_is_optional:
224 COLLECT_ATTR(DW_AT_is_optional)
225 break;
226 case dwarf::DW_AT_location:
227 COLLECT_ATTR(DW_AT_location)
228 break;
229 case dwarf::DW_AT_lower_bound:
230 COLLECT_ATTR(DW_AT_lower_bound)
231 break;
232 case dwarf::DW_AT_mutable:
233 COLLECT_ATTR(DW_AT_mutable)
234 break;
235 case dwarf::DW_AT_ordering:
236 COLLECT_ATTR(DW_AT_ordering)
237 break;
238 case dwarf::DW_AT_picture_string:
239 COLLECT_ATTR(DW_AT_picture_string)
240 break;
241 case dwarf::DW_AT_prototyped:
242 COLLECT_ATTR(DW_AT_prototyped)
243 break;
244 case dwarf::DW_AT_small:
245 COLLECT_ATTR(DW_AT_small)
246 break;
247 case dwarf::DW_AT_segment:
248 COLLECT_ATTR(DW_AT_segment)
249 break;
250 case dwarf::DW_AT_string_length:
251 COLLECT_ATTR(DW_AT_string_length)
252 break;
253 case dwarf::DW_AT_threads_scaled:
254 COLLECT_ATTR(DW_AT_threads_scaled)
255 break;
256 case dwarf::DW_AT_upper_bound:
257 COLLECT_ATTR(DW_AT_upper_bound)
258 break;
259 case dwarf::DW_AT_use_location:
260 COLLECT_ATTR(DW_AT_use_location)
261 break;
262 case dwarf::DW_AT_use_UTF8:
263 COLLECT_ATTR(DW_AT_use_UTF8)
264 break;
265 case dwarf::DW_AT_variable_parameter:
266 COLLECT_ATTR(DW_AT_variable_parameter)
267 break;
268 case dwarf::DW_AT_virtuality:
269 COLLECT_ATTR(DW_AT_virtuality)
270 break;
271 case dwarf::DW_AT_visibility:
272 COLLECT_ATTR(DW_AT_visibility)
273 break;
274 case dwarf::DW_AT_vtable_elem_location:
275 COLLECT_ATTR(DW_AT_vtable_elem_location)
Eric Christopher7ced4fa2013-08-28 00:10:38 +0000276 break;
Eric Christopher0710bfa2013-08-13 01:21:55 +0000277 default:
278 break;
279 }
280 }
281}
282
283// Hash an individual attribute \param Attr based on the type of attribute and
284// the form.
285void DIEHash::hashAttribute(AttrEntry Attr) {
286 const DIEValue *Value = Attr.Val;
287 const DIEAbbrevData *Desc = Attr.Desc;
288
289 // TODO: Add support for types.
290
291 // Add the letter A to the hash.
292 addULEB128('A');
293
David Blaikiec0987082013-10-16 23:36:20 +0000294 // Then the attribute code.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000295 addULEB128(Desc->getAttribute());
David Blaikiec0987082013-10-16 23:36:20 +0000296
297 // To ensure reproducibility of the signature, the set of forms used in the
298 // signature computation is limited to the following: DW_FORM_sdata,
299 // DW_FORM_flag, DW_FORM_string, and DW_FORM_block.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000300
301 // TODO: Add support for additional forms.
302 switch (Desc->getForm()) {
David Blaikiec0987082013-10-16 23:36:20 +0000303 case dwarf::DW_FORM_string:
304 llvm_unreachable(
305 "Add support for DW_FORM_string if we ever start emitting them again");
Eric Christopher0710bfa2013-08-13 01:21:55 +0000306 case dwarf::DW_FORM_strp:
David Blaikiec0987082013-10-16 23:36:20 +0000307 addULEB128(dwarf::DW_FORM_string);
Eric Christopher0710bfa2013-08-13 01:21:55 +0000308 addString(cast<DIEString>(Value)->getString());
309 break;
Eric Christopher7ced4fa2013-08-28 00:10:38 +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:
David Blaikiec0987082013-10-16 23:36:20 +0000315 addULEB128(dwarf::DW_FORM_sdata);
316 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
Eric Christopher7ced4fa2013-08-28 00:10:38 +0000317 break;
Eric Christopher0710bfa2013-08-13 01:21:55 +0000318 }
319}
320
321// Go through the attributes from \param Attrs in the order specified in 7.27.4
322// and hash them.
David Blaikiecbb5c732013-08-14 22:23:05 +0000323void DIEHash::hashAttributes(const DIEAttrs &Attrs) {
Eric Christopher0710bfa2013-08-13 01:21:55 +0000324#define ADD_ATTR(ATTR) \
325 { \
326 if (ATTR.Val != 0) \
327 hashAttribute(ATTR); \
328 }
329
Eric Christopher0710bfa2013-08-13 01:21:55 +0000330 ADD_ATTR(Attrs.DW_AT_name);
Eric Christopherbd18c8d2013-09-03 20:00:20 +0000331 ADD_ATTR(Attrs.DW_AT_accessibility);
332 ADD_ATTR(Attrs.DW_AT_address_class);
333 ADD_ATTR(Attrs.DW_AT_allocated);
334 ADD_ATTR(Attrs.DW_AT_artificial);
335 ADD_ATTR(Attrs.DW_AT_associated);
336 ADD_ATTR(Attrs.DW_AT_binary_scale);
337 ADD_ATTR(Attrs.DW_AT_bit_offset);
338 ADD_ATTR(Attrs.DW_AT_bit_size);
339 ADD_ATTR(Attrs.DW_AT_bit_stride);
340 ADD_ATTR(Attrs.DW_AT_byte_size);
341 ADD_ATTR(Attrs.DW_AT_byte_stride);
342 ADD_ATTR(Attrs.DW_AT_const_expr);
343 ADD_ATTR(Attrs.DW_AT_const_value);
344 ADD_ATTR(Attrs.DW_AT_containing_type);
345 ADD_ATTR(Attrs.DW_AT_count);
346 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
347 ADD_ATTR(Attrs.DW_AT_data_location);
348 ADD_ATTR(Attrs.DW_AT_data_member_location);
349 ADD_ATTR(Attrs.DW_AT_decimal_scale);
350 ADD_ATTR(Attrs.DW_AT_decimal_sign);
351 ADD_ATTR(Attrs.DW_AT_default_value);
352 ADD_ATTR(Attrs.DW_AT_digit_count);
353 ADD_ATTR(Attrs.DW_AT_discr);
354 ADD_ATTR(Attrs.DW_AT_discr_list);
355 ADD_ATTR(Attrs.DW_AT_discr_value);
356 ADD_ATTR(Attrs.DW_AT_encoding);
357 ADD_ATTR(Attrs.DW_AT_enum_class);
358 ADD_ATTR(Attrs.DW_AT_endianity);
359 ADD_ATTR(Attrs.DW_AT_explicit);
360 ADD_ATTR(Attrs.DW_AT_is_optional);
361 ADD_ATTR(Attrs.DW_AT_location);
362 ADD_ATTR(Attrs.DW_AT_lower_bound);
363 ADD_ATTR(Attrs.DW_AT_mutable);
364 ADD_ATTR(Attrs.DW_AT_ordering);
365 ADD_ATTR(Attrs.DW_AT_picture_string);
366 ADD_ATTR(Attrs.DW_AT_prototyped);
367 ADD_ATTR(Attrs.DW_AT_small);
368 ADD_ATTR(Attrs.DW_AT_segment);
369 ADD_ATTR(Attrs.DW_AT_string_length);
370 ADD_ATTR(Attrs.DW_AT_threads_scaled);
371 ADD_ATTR(Attrs.DW_AT_upper_bound);
372 ADD_ATTR(Attrs.DW_AT_use_location);
373 ADD_ATTR(Attrs.DW_AT_use_UTF8);
374 ADD_ATTR(Attrs.DW_AT_variable_parameter);
375 ADD_ATTR(Attrs.DW_AT_virtuality);
376 ADD_ATTR(Attrs.DW_AT_visibility);
377 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
378
379 // FIXME: Add the extended attributes.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000380}
381
382// Add all of the attributes for \param Die to the hash.
383void DIEHash::addAttributes(DIE *Die) {
David Blaikiee8917752013-10-16 00:47:21 +0000384 DIEAttrs Attrs = {};
David Blaikiecbb5c732013-08-14 22:23:05 +0000385 collectAttributes(Die, Attrs);
Eric Christopher0710bfa2013-08-13 01:21:55 +0000386 hashAttributes(Attrs);
387}
388
389// Compute the hash of a DIE. This is based on the type signature computation
390// given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
391// flattened description of the DIE.
392void DIEHash::computeHash(DIE *Die) {
393
394 // Append the letter 'D', followed by the DWARF tag of the DIE.
395 addULEB128('D');
396 addULEB128(Die->getTag());
397
398 // Add each of the attributes of the DIE.
399 addAttributes(Die);
400
401 // Then hash each of the children of the DIE.
402 for (std::vector<DIE *>::const_iterator I = Die->getChildren().begin(),
403 E = Die->getChildren().end();
404 I != E; ++I)
405 computeHash(*I);
David Blaikie75ee0002013-10-16 20:29:06 +0000406
407 // Following the last (or if there are no children), append a zero byte.
David Blaikie700b91f2013-10-16 20:40:46 +0000408 Hash.update(makeArrayRef((uint8_t)'\0'));
Eric Christopher0710bfa2013-08-13 01:21:55 +0000409}
410
Eric Christopher0d27ca12013-08-08 23:45:55 +0000411/// This is based on the type signature computation given in section 7.27 of the
412/// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
Eric Christopherdd0cd3c2013-08-12 23:59:24 +0000413/// with the exception that we are hashing only the context and the name of the
414/// type.
Eric Christopher0d27ca12013-08-08 23:45:55 +0000415uint64_t DIEHash::computeDIEODRSignature(DIE *Die) {
416
417 // Add the contexts to the hash. We won't be computing the ODR hash for
418 // function local types so it's safe to use the generic context hashing
419 // algorithm here.
420 // FIXME: If we figure out how to account for linkage in some way we could
421 // actually do this with a slight modification to the parent hash algorithm.
422 DIE *Parent = Die->getParent();
423 if (Parent)
424 addParentContext(Parent);
425
426 // Add the current DIE information.
427
428 // Add the DWARF tag of the DIE.
429 addULEB128(Die->getTag());
430
431 // Add the name of the type to the hash.
432 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
433
434 // Now get the result.
435 MD5::MD5Result Result;
436 Hash.final(Result);
437
438 // ... take the least significant 8 bytes and return those. Our MD5
439 // implementation always returns its results in little endian, swap bytes
440 // appropriately.
441 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
442}
Eric Christopher0710bfa2013-08-13 01:21:55 +0000443
444/// This is based on the type signature computation given in section 7.27 of the
445/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
446/// with the inclusion of the full CU and all top level CU entities.
Eric Christopher800a8762013-09-03 21:57:57 +0000447// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000448uint64_t DIEHash::computeCUSignature(DIE *Die) {
449
450 // Hash the DIE.
451 computeHash(Die);
452
453 // Now return the result.
454 MD5::MD5Result Result;
455 Hash.final(Result);
456
457 // ... take the least significant 8 bytes and return those. Our MD5
458 // implementation always returns its results in little endian, swap bytes
459 // appropriately.
460 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
461}
Eric Christopher800a8762013-09-03 21:57:57 +0000462
463/// This is based on the type signature computation given in section 7.27 of the
464/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
465/// with the inclusion of additional forms not specifically called out in the
466/// standard.
467uint64_t DIEHash::computeTypeSignature(DIE *Die) {
468
David Blaikie88a68cb2013-10-17 00:10:34 +0000469 if (DIE *Parent = Die->getParent())
470 addParentContext(Parent);
471
Eric Christopher800a8762013-09-03 21:57:57 +0000472 // Hash the DIE.
473 computeHash(Die);
474
475 // Now return the result.
476 MD5::MD5Result Result;
477 Hash.final(Result);
478
479 // ... take the least significant 8 bytes and return those. Our MD5
480 // implementation always returns its results in little endian, swap bytes
481 // appropriately.
482 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
483}