blob: aa54db00461313a8fa9cce22d8ea37e8e8f121ff [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 Blaikie0d87c202013-10-17 22:14:08 +0000125 case dwarf::NAME: \
126 Attrs.NAME.Val = Values[i]; \
127 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
128 break
Eric Christopher0710bfa2013-08-13 01:21:55 +0000129
130 for (size_t i = 0, e = Values.size(); i != e; ++i) {
131 DEBUG(dbgs() << "Attribute: "
132 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
133 << " added.\n");
134 switch (Abbrevs.getData()[i].getAttribute()) {
David Blaikie0d87c202013-10-17 22:14:08 +0000135 COLLECT_ATTR(DW_AT_name);
136 COLLECT_ATTR(DW_AT_accessibility);
137 COLLECT_ATTR(DW_AT_address_class);
138 COLLECT_ATTR(DW_AT_allocated);
139 COLLECT_ATTR(DW_AT_artificial);
140 COLLECT_ATTR(DW_AT_associated);
141 COLLECT_ATTR(DW_AT_binary_scale);
142 COLLECT_ATTR(DW_AT_bit_offset);
143 COLLECT_ATTR(DW_AT_bit_size);
144 COLLECT_ATTR(DW_AT_bit_stride);
145 COLLECT_ATTR(DW_AT_byte_size);
146 COLLECT_ATTR(DW_AT_byte_stride);
147 COLLECT_ATTR(DW_AT_const_expr);
148 COLLECT_ATTR(DW_AT_const_value);
149 COLLECT_ATTR(DW_AT_containing_type);
150 COLLECT_ATTR(DW_AT_count);
151 COLLECT_ATTR(DW_AT_data_bit_offset);
152 COLLECT_ATTR(DW_AT_data_location);
153 COLLECT_ATTR(DW_AT_data_member_location);
154 COLLECT_ATTR(DW_AT_decimal_scale);
155 COLLECT_ATTR(DW_AT_decimal_sign);
156 COLLECT_ATTR(DW_AT_default_value);
157 COLLECT_ATTR(DW_AT_digit_count);
158 COLLECT_ATTR(DW_AT_discr);
159 COLLECT_ATTR(DW_AT_discr_list);
160 COLLECT_ATTR(DW_AT_discr_value);
161 COLLECT_ATTR(DW_AT_encoding);
162 COLLECT_ATTR(DW_AT_enum_class);
163 COLLECT_ATTR(DW_AT_endianity);
164 COLLECT_ATTR(DW_AT_explicit);
165 COLLECT_ATTR(DW_AT_is_optional);
166 COLLECT_ATTR(DW_AT_location);
167 COLLECT_ATTR(DW_AT_lower_bound);
168 COLLECT_ATTR(DW_AT_mutable);
169 COLLECT_ATTR(DW_AT_ordering);
170 COLLECT_ATTR(DW_AT_picture_string);
171 COLLECT_ATTR(DW_AT_prototyped);
172 COLLECT_ATTR(DW_AT_small);
173 COLLECT_ATTR(DW_AT_segment);
174 COLLECT_ATTR(DW_AT_string_length);
175 COLLECT_ATTR(DW_AT_threads_scaled);
176 COLLECT_ATTR(DW_AT_upper_bound);
177 COLLECT_ATTR(DW_AT_use_location);
178 COLLECT_ATTR(DW_AT_use_UTF8);
179 COLLECT_ATTR(DW_AT_variable_parameter);
180 COLLECT_ATTR(DW_AT_virtuality);
181 COLLECT_ATTR(DW_AT_visibility);
182 COLLECT_ATTR(DW_AT_vtable_elem_location);
183 COLLECT_ATTR(DW_AT_type);
Eric Christopher0710bfa2013-08-13 01:21:55 +0000184 default:
185 break;
186 }
187 }
188}
189
190// Hash an individual attribute \param Attr based on the type of attribute and
191// the form.
192void DIEHash::hashAttribute(AttrEntry Attr) {
193 const DIEValue *Value = Attr.Val;
194 const DIEAbbrevData *Desc = Attr.Desc;
195
David Blaikie47f66d52013-10-17 22:07:09 +0000196 // 7.27s3
197 // ... An attribute that refers to another type entry T is processed as
198 // follows:
David Blaikie47f66d52013-10-17 22:07:09 +0000199 if (const DIEEntry *EntryAttr = dyn_cast<DIEEntry>(Value)) {
200 DIE *Entry = EntryAttr->getEntry();
David Blaikie3baa3c32013-10-21 18:59:40 +0000201 unsigned &DieNumber = Numbering[Entry];
202 if (DieNumber) {
203 // a) If T is in the list of [previously hashed types], use the letter
204 // 'R' as the marker
205 addULEB128('R');
David Blaikie47f66d52013-10-17 22:07:09 +0000206
David Blaikie3baa3c32013-10-21 18:59:40 +0000207 addULEB128(Desc->getAttribute());
208
209 // and use the unsigned LEB128 encoding of [the index of T in the
210 // list] as the attribute value;
211 addULEB128(DieNumber);
212 return;
213 }
214
215 // otherwise, b) use the letter 'T' as a the marker, ...
David Blaikie47f66d52013-10-17 22:07:09 +0000216 addULEB128('T');
217
218 addULEB128(Desc->getAttribute());
219
220 // ... process the type T recursively by performing Steps 2 through 7, and
221 // use the result as the attribute value.
David Blaikie3baa3c32013-10-21 18:59:40 +0000222 DieNumber = Numbering.size();
David Blaikie47f66d52013-10-17 22:07:09 +0000223 computeHash(Entry);
224 return;
225 }
226
227 // Other attribute values use the letter 'A' as the marker, ...
Eric Christopher0710bfa2013-08-13 01:21:55 +0000228 addULEB128('A');
229
Eric Christopher0710bfa2013-08-13 01:21:55 +0000230 addULEB128(Desc->getAttribute());
David Blaikiec0987082013-10-16 23:36:20 +0000231
David Blaikie47f66d52013-10-17 22:07:09 +0000232 // ... and the value consists of the form code (encoded as an unsigned LEB128
233 // value) followed by the encoding of the value according to the form code. To
234 // ensure reproducibility of the signature, the set of forms used in the
David Blaikiec0987082013-10-16 23:36:20 +0000235 // signature computation is limited to the following: DW_FORM_sdata,
236 // DW_FORM_flag, DW_FORM_string, and DW_FORM_block.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000237 switch (Desc->getForm()) {
David Blaikiec0987082013-10-16 23:36:20 +0000238 case dwarf::DW_FORM_string:
239 llvm_unreachable(
240 "Add support for DW_FORM_string if we ever start emitting them again");
David Blaikieda39dd32013-10-21 16:37:22 +0000241 case dwarf::DW_FORM_GNU_str_index:
Eric Christopher0710bfa2013-08-13 01:21:55 +0000242 case dwarf::DW_FORM_strp:
David Blaikiec0987082013-10-16 23:36:20 +0000243 addULEB128(dwarf::DW_FORM_string);
Eric Christopher0710bfa2013-08-13 01:21:55 +0000244 addString(cast<DIEString>(Value)->getString());
245 break;
Eric Christopher7ced4fa2013-08-28 00:10:38 +0000246 case dwarf::DW_FORM_data1:
247 case dwarf::DW_FORM_data2:
248 case dwarf::DW_FORM_data4:
249 case dwarf::DW_FORM_data8:
250 case dwarf::DW_FORM_udata:
David Blaikiec0987082013-10-16 23:36:20 +0000251 addULEB128(dwarf::DW_FORM_sdata);
252 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
Eric Christopher7ced4fa2013-08-28 00:10:38 +0000253 break;
David Blaikieda39dd32013-10-21 16:37:22 +0000254 default:
255 llvm_unreachable("Add support for additional forms");
Eric Christopher0710bfa2013-08-13 01:21:55 +0000256 }
257}
258
259// Go through the attributes from \param Attrs in the order specified in 7.27.4
260// and hash them.
David Blaikiecbb5c732013-08-14 22:23:05 +0000261void DIEHash::hashAttributes(const DIEAttrs &Attrs) {
Eric Christopher0710bfa2013-08-13 01:21:55 +0000262#define ADD_ATTR(ATTR) \
263 { \
264 if (ATTR.Val != 0) \
265 hashAttribute(ATTR); \
266 }
267
Eric Christopher0710bfa2013-08-13 01:21:55 +0000268 ADD_ATTR(Attrs.DW_AT_name);
Eric Christopherbd18c8d2013-09-03 20:00:20 +0000269 ADD_ATTR(Attrs.DW_AT_accessibility);
270 ADD_ATTR(Attrs.DW_AT_address_class);
271 ADD_ATTR(Attrs.DW_AT_allocated);
272 ADD_ATTR(Attrs.DW_AT_artificial);
273 ADD_ATTR(Attrs.DW_AT_associated);
274 ADD_ATTR(Attrs.DW_AT_binary_scale);
275 ADD_ATTR(Attrs.DW_AT_bit_offset);
276 ADD_ATTR(Attrs.DW_AT_bit_size);
277 ADD_ATTR(Attrs.DW_AT_bit_stride);
278 ADD_ATTR(Attrs.DW_AT_byte_size);
279 ADD_ATTR(Attrs.DW_AT_byte_stride);
280 ADD_ATTR(Attrs.DW_AT_const_expr);
281 ADD_ATTR(Attrs.DW_AT_const_value);
282 ADD_ATTR(Attrs.DW_AT_containing_type);
283 ADD_ATTR(Attrs.DW_AT_count);
284 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
285 ADD_ATTR(Attrs.DW_AT_data_location);
286 ADD_ATTR(Attrs.DW_AT_data_member_location);
287 ADD_ATTR(Attrs.DW_AT_decimal_scale);
288 ADD_ATTR(Attrs.DW_AT_decimal_sign);
289 ADD_ATTR(Attrs.DW_AT_default_value);
290 ADD_ATTR(Attrs.DW_AT_digit_count);
291 ADD_ATTR(Attrs.DW_AT_discr);
292 ADD_ATTR(Attrs.DW_AT_discr_list);
293 ADD_ATTR(Attrs.DW_AT_discr_value);
294 ADD_ATTR(Attrs.DW_AT_encoding);
295 ADD_ATTR(Attrs.DW_AT_enum_class);
296 ADD_ATTR(Attrs.DW_AT_endianity);
297 ADD_ATTR(Attrs.DW_AT_explicit);
298 ADD_ATTR(Attrs.DW_AT_is_optional);
299 ADD_ATTR(Attrs.DW_AT_location);
300 ADD_ATTR(Attrs.DW_AT_lower_bound);
301 ADD_ATTR(Attrs.DW_AT_mutable);
302 ADD_ATTR(Attrs.DW_AT_ordering);
303 ADD_ATTR(Attrs.DW_AT_picture_string);
304 ADD_ATTR(Attrs.DW_AT_prototyped);
305 ADD_ATTR(Attrs.DW_AT_small);
306 ADD_ATTR(Attrs.DW_AT_segment);
307 ADD_ATTR(Attrs.DW_AT_string_length);
308 ADD_ATTR(Attrs.DW_AT_threads_scaled);
309 ADD_ATTR(Attrs.DW_AT_upper_bound);
310 ADD_ATTR(Attrs.DW_AT_use_location);
311 ADD_ATTR(Attrs.DW_AT_use_UTF8);
312 ADD_ATTR(Attrs.DW_AT_variable_parameter);
313 ADD_ATTR(Attrs.DW_AT_virtuality);
314 ADD_ATTR(Attrs.DW_AT_visibility);
315 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
David Blaikie47f66d52013-10-17 22:07:09 +0000316 ADD_ATTR(Attrs.DW_AT_type);
Eric Christopherbd18c8d2013-09-03 20:00:20 +0000317
318 // FIXME: Add the extended attributes.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000319}
320
321// Add all of the attributes for \param Die to the hash.
322void DIEHash::addAttributes(DIE *Die) {
David Blaikiee8917752013-10-16 00:47:21 +0000323 DIEAttrs Attrs = {};
David Blaikiecbb5c732013-08-14 22:23:05 +0000324 collectAttributes(Die, Attrs);
Eric Christopher0710bfa2013-08-13 01:21:55 +0000325 hashAttributes(Attrs);
326}
327
328// Compute the hash of a DIE. This is based on the type signature computation
329// given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
330// flattened description of the DIE.
331void DIEHash::computeHash(DIE *Die) {
Eric Christopher0710bfa2013-08-13 01:21:55 +0000332 // Append the letter 'D', followed by the DWARF tag of the DIE.
333 addULEB128('D');
334 addULEB128(Die->getTag());
335
336 // Add each of the attributes of the DIE.
337 addAttributes(Die);
338
339 // Then hash each of the children of the DIE.
340 for (std::vector<DIE *>::const_iterator I = Die->getChildren().begin(),
341 E = Die->getChildren().end();
342 I != E; ++I)
343 computeHash(*I);
David Blaikie75ee0002013-10-16 20:29:06 +0000344
345 // Following the last (or if there are no children), append a zero byte.
David Blaikie700b91f2013-10-16 20:40:46 +0000346 Hash.update(makeArrayRef((uint8_t)'\0'));
Eric Christopher0710bfa2013-08-13 01:21:55 +0000347}
348
Eric Christopher0d27ca12013-08-08 23:45:55 +0000349/// This is based on the type signature computation given in section 7.27 of the
350/// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
Eric Christopherdd0cd3c2013-08-12 23:59:24 +0000351/// with the exception that we are hashing only the context and the name of the
352/// type.
Eric Christopher0d27ca12013-08-08 23:45:55 +0000353uint64_t DIEHash::computeDIEODRSignature(DIE *Die) {
354
355 // Add the contexts to the hash. We won't be computing the ODR hash for
356 // function local types so it's safe to use the generic context hashing
357 // algorithm here.
358 // FIXME: If we figure out how to account for linkage in some way we could
359 // actually do this with a slight modification to the parent hash algorithm.
360 DIE *Parent = Die->getParent();
361 if (Parent)
362 addParentContext(Parent);
363
364 // Add the current DIE information.
365
366 // Add the DWARF tag of the DIE.
367 addULEB128(Die->getTag());
368
369 // Add the name of the type to the hash.
370 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
371
372 // Now get the result.
373 MD5::MD5Result Result;
374 Hash.final(Result);
375
376 // ... take the least significant 8 bytes and return those. Our MD5
377 // implementation always returns its results in little endian, swap bytes
378 // appropriately.
379 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
380}
Eric Christopher0710bfa2013-08-13 01:21:55 +0000381
382/// This is based on the type signature computation given in section 7.27 of the
383/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
384/// with the inclusion of the full CU and all top level CU entities.
Eric Christopher800a8762013-09-03 21:57:57 +0000385// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
Eric Christopher0710bfa2013-08-13 01:21:55 +0000386uint64_t DIEHash::computeCUSignature(DIE *Die) {
David Blaikie3baa3c32013-10-21 18:59:40 +0000387 Numbering.clear();
388 Numbering[Die] = 1;
Eric Christopher0710bfa2013-08-13 01:21:55 +0000389
390 // Hash the DIE.
391 computeHash(Die);
392
393 // Now return the result.
394 MD5::MD5Result Result;
395 Hash.final(Result);
396
397 // ... take the least significant 8 bytes and return those. Our MD5
398 // implementation always returns its results in little endian, swap bytes
399 // appropriately.
400 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
401}
Eric Christopher800a8762013-09-03 21:57:57 +0000402
403/// This is based on the type signature computation given in section 7.27 of the
404/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
405/// with the inclusion of additional forms not specifically called out in the
406/// standard.
407uint64_t DIEHash::computeTypeSignature(DIE *Die) {
David Blaikie3baa3c32013-10-21 18:59:40 +0000408 Numbering.clear();
409 Numbering[Die] = 1;
Eric Christopher800a8762013-09-03 21:57:57 +0000410
David Blaikie88a68cb2013-10-17 00:10:34 +0000411 if (DIE *Parent = Die->getParent())
412 addParentContext(Parent);
413
Eric Christopher800a8762013-09-03 21:57:57 +0000414 // Hash the DIE.
415 computeHash(Die);
416
417 // Now return the result.
418 MD5::MD5Result Result;
419 Hash.final(Result);
420
421 // ... take the least significant 8 bytes and return those. Our MD5
422 // implementation always returns its results in little endian, swap bytes
423 // appropriately.
424 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
425}