OSDN Git Service

34d9fffb1ae85cee784e08d1d3b683300fc47d5d
[android-x86/external-llvm.git] / lib / CodeGen / AsmPrinter / DIEHash.cpp
1 //===-- 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
27 using namespace llvm;
28
29 /// \brief Grabs the string in whichever attribute is passed in and returns
30 /// a reference to it.
31 static StringRef getDIEStringAttr(const 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.
50 void 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.
60 void 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
71 void 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
85 /// \brief Including \p Parent adds the context of Parent to the hash..
86 void 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
119 // Collect all of the attributes for a particular DIE in single structure.
120 void DIEHash::collectAttributes(DIE *Die, DIEAttrs &Attrs) {
121   const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
122   const DIEAbbrev &Abbrevs = Die->getAbbrev();
123
124 #define COLLECT_ATTR(NAME)                                                     \
125   case dwarf::NAME:                                                            \
126     Attrs.NAME.Val = Values[i];                                                \
127     Attrs.NAME.Desc = &Abbrevs.getData()[i];                                   \
128     break
129
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()) {
135     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);
184     default:
185       break;
186     }
187   }
188 }
189
190 void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
191                                        const DIE &Entry, StringRef Name) {
192   // append the letter 'N'
193   addULEB128('N');
194
195   // the DWARF attribute code (DW_AT_type or DW_AT_friend),
196   addULEB128(Attribute);
197
198   // the context of the tag,
199   if (DIE *Parent = Entry.getParent())
200     addParentContext(Parent);
201
202   // the letter 'E',
203   addULEB128('E');
204
205   // and the name of the type.
206   addString(Name);
207
208   // Currently DW_TAG_friends are not used by Clang, but if they do become so,
209   // here's the relevant spec text to implement:
210   //
211   // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
212   // the context is omitted and the name to be used is the ABI-specific name
213   // of the subprogram (e.g., the mangled linker name).
214 }
215
216 void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
217                                         unsigned DieNumber) {
218   // a) If T is in the list of [previously hashed types], use the letter
219   // 'R' as the marker
220   addULEB128('R');
221
222   addULEB128(Attribute);
223
224   // and use the unsigned LEB128 encoding of [the index of T in the
225   // list] as the attribute value;
226   addULEB128(DieNumber);
227 }
228
229 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
230                            DIE &Entry) {
231   assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
232                                         "tags. Add support here when there's "
233                                         "a use case");
234   // Step 5
235   // If the tag in Step 3 is one of [the below tags]
236   if ((Tag == dwarf::DW_TAG_pointer_type ||
237        Tag == dwarf::DW_TAG_reference_type ||
238        Tag == dwarf::DW_TAG_rvalue_reference_type ||
239        Tag == dwarf::DW_TAG_ptr_to_member_type) &&
240       // and the referenced type (via the [below attributes])
241       // FIXME: This seems overly restrictive, and causes hash mismatches
242       // there's a decl/def difference in the containing type of a
243       // ptr_to_member_type, but it's what DWARF says, for some reason.
244       Attribute == dwarf::DW_AT_type) {
245     // [FIXME] ... has a DW_AT_name attribute,
246     hashShallowTypeReference(Attribute, Entry,
247                              getDIEStringAttr(Entry, dwarf::DW_AT_name));
248     return;
249   }
250
251   unsigned &DieNumber = Numbering[&Entry];
252   if (DieNumber) {
253     hashRepeatedTypeReference(Attribute, DieNumber);
254     return;
255   }
256
257   // otherwise, b) use the letter 'T' as a the marker, ...
258   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();
265   computeHash(&Entry);
266 }
267
268 // Hash an individual attribute \param Attr based on the type of attribute and
269 // the form.
270 void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
271   const DIEValue *Value = Attr.Val;
272   const DIEAbbrevData *Desc = Attr.Desc;
273   dwarf::Attribute Attribute = Desc->getAttribute();
274
275   // 7.27 Step 3
276   // ... An attribute that refers to another type entry T is processed as
277   // follows:
278   if (const DIEEntry *EntryAttr = dyn_cast<DIEEntry>(Value)) {
279     hashDIEEntry(Attribute, Tag, *EntryAttr->getEntry());
280     return;
281   }
282
283   // Other attribute values use the letter 'A' as the marker, ...
284   addULEB128('A');
285
286   addULEB128(Attribute);
287
288   // ... and the value consists of the form code (encoded as an unsigned LEB128
289   // value) followed by the encoding of the value according to the form code. To
290   // ensure reproducibility of the signature, the set of forms used in the
291   // signature computation is limited to the following: DW_FORM_sdata,
292   // DW_FORM_flag, DW_FORM_string, and DW_FORM_block.
293   switch (Desc->getForm()) {
294   case dwarf::DW_FORM_string:
295     llvm_unreachable(
296         "Add support for DW_FORM_string if we ever start emitting them again");
297   case dwarf::DW_FORM_GNU_str_index:
298   case dwarf::DW_FORM_strp:
299     addULEB128(dwarf::DW_FORM_string);
300     addString(cast<DIEString>(Value)->getString());
301     break;
302   case dwarf::DW_FORM_data1:
303   case dwarf::DW_FORM_data2:
304   case dwarf::DW_FORM_data4:
305   case dwarf::DW_FORM_data8:
306   case dwarf::DW_FORM_udata:
307     addULEB128(dwarf::DW_FORM_sdata);
308     addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
309     break;
310   default:
311     llvm_unreachable("Add support for additional forms");
312   }
313 }
314
315 // Go through the attributes from \param Attrs in the order specified in 7.27.4
316 // and hash them.
317 void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
318 #define ADD_ATTR(ATTR)                                                         \
319   {                                                                            \
320     if (ATTR.Val != 0)                                                         \
321       hashAttribute(ATTR, Tag);                                                \
322   }
323
324   ADD_ATTR(Attrs.DW_AT_name);
325   ADD_ATTR(Attrs.DW_AT_accessibility);
326   ADD_ATTR(Attrs.DW_AT_address_class);
327   ADD_ATTR(Attrs.DW_AT_allocated);
328   ADD_ATTR(Attrs.DW_AT_artificial);
329   ADD_ATTR(Attrs.DW_AT_associated);
330   ADD_ATTR(Attrs.DW_AT_binary_scale);
331   ADD_ATTR(Attrs.DW_AT_bit_offset);
332   ADD_ATTR(Attrs.DW_AT_bit_size);
333   ADD_ATTR(Attrs.DW_AT_bit_stride);
334   ADD_ATTR(Attrs.DW_AT_byte_size);
335   ADD_ATTR(Attrs.DW_AT_byte_stride);
336   ADD_ATTR(Attrs.DW_AT_const_expr);
337   ADD_ATTR(Attrs.DW_AT_const_value);
338   ADD_ATTR(Attrs.DW_AT_containing_type);
339   ADD_ATTR(Attrs.DW_AT_count);
340   ADD_ATTR(Attrs.DW_AT_data_bit_offset);
341   ADD_ATTR(Attrs.DW_AT_data_location);
342   ADD_ATTR(Attrs.DW_AT_data_member_location);
343   ADD_ATTR(Attrs.DW_AT_decimal_scale);
344   ADD_ATTR(Attrs.DW_AT_decimal_sign);
345   ADD_ATTR(Attrs.DW_AT_default_value);
346   ADD_ATTR(Attrs.DW_AT_digit_count);
347   ADD_ATTR(Attrs.DW_AT_discr);
348   ADD_ATTR(Attrs.DW_AT_discr_list);
349   ADD_ATTR(Attrs.DW_AT_discr_value);
350   ADD_ATTR(Attrs.DW_AT_encoding);
351   ADD_ATTR(Attrs.DW_AT_enum_class);
352   ADD_ATTR(Attrs.DW_AT_endianity);
353   ADD_ATTR(Attrs.DW_AT_explicit);
354   ADD_ATTR(Attrs.DW_AT_is_optional);
355   ADD_ATTR(Attrs.DW_AT_location);
356   ADD_ATTR(Attrs.DW_AT_lower_bound);
357   ADD_ATTR(Attrs.DW_AT_mutable);
358   ADD_ATTR(Attrs.DW_AT_ordering);
359   ADD_ATTR(Attrs.DW_AT_picture_string);
360   ADD_ATTR(Attrs.DW_AT_prototyped);
361   ADD_ATTR(Attrs.DW_AT_small);
362   ADD_ATTR(Attrs.DW_AT_segment);
363   ADD_ATTR(Attrs.DW_AT_string_length);
364   ADD_ATTR(Attrs.DW_AT_threads_scaled);
365   ADD_ATTR(Attrs.DW_AT_upper_bound);
366   ADD_ATTR(Attrs.DW_AT_use_location);
367   ADD_ATTR(Attrs.DW_AT_use_UTF8);
368   ADD_ATTR(Attrs.DW_AT_variable_parameter);
369   ADD_ATTR(Attrs.DW_AT_virtuality);
370   ADD_ATTR(Attrs.DW_AT_visibility);
371   ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
372   ADD_ATTR(Attrs.DW_AT_type);
373
374   // FIXME: Add the extended attributes.
375 }
376
377 // Add all of the attributes for \param Die to the hash.
378 void DIEHash::addAttributes(DIE *Die) {
379   DIEAttrs Attrs = {};
380   collectAttributes(Die, Attrs);
381   hashAttributes(Attrs, Die->getTag());
382 }
383
384 // Compute the hash of a DIE. This is based on the type signature computation
385 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
386 // flattened description of the DIE.
387 void DIEHash::computeHash(DIE *Die) {
388   // Append the letter 'D', followed by the DWARF tag of the DIE.
389   addULEB128('D');
390   addULEB128(Die->getTag());
391
392   // Add each of the attributes of the DIE.
393   addAttributes(Die);
394
395   // Then hash each of the children of the DIE.
396   for (std::vector<DIE *>::const_iterator I = Die->getChildren().begin(),
397                                           E = Die->getChildren().end();
398        I != E; ++I)
399     computeHash(*I);
400
401   // Following the last (or if there are no children), append a zero byte.
402   Hash.update(makeArrayRef((uint8_t)'\0'));
403 }
404
405 /// This is based on the type signature computation given in section 7.27 of the
406 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
407 /// with the exception that we are hashing only the context and the name of the
408 /// type.
409 uint64_t DIEHash::computeDIEODRSignature(DIE *Die) {
410
411   // Add the contexts to the hash. We won't be computing the ODR hash for
412   // function local types so it's safe to use the generic context hashing
413   // algorithm here.
414   // FIXME: If we figure out how to account for linkage in some way we could
415   // actually do this with a slight modification to the parent hash algorithm.
416   DIE *Parent = Die->getParent();
417   if (Parent)
418     addParentContext(Parent);
419
420   // Add the current DIE information.
421
422   // Add the DWARF tag of the DIE.
423   addULEB128(Die->getTag());
424
425   // Add the name of the type to the hash.
426   addString(getDIEStringAttr(*Die, dwarf::DW_AT_name));
427
428   // Now get the result.
429   MD5::MD5Result Result;
430   Hash.final(Result);
431
432   // ... take the least significant 8 bytes and return those. Our MD5
433   // implementation always returns its results in little endian, swap bytes
434   // appropriately.
435   return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
436 }
437
438 /// This is based on the type signature computation given in section 7.27 of the
439 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
440 /// with the inclusion of the full CU and all top level CU entities.
441 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
442 uint64_t DIEHash::computeCUSignature(DIE *Die) {
443   Numbering.clear();
444   Numbering[Die] = 1;
445
446   // Hash the DIE.
447   computeHash(Die);
448
449   // Now return the result.
450   MD5::MD5Result Result;
451   Hash.final(Result);
452
453   // ... take the least significant 8 bytes and return those. Our MD5
454   // implementation always returns its results in little endian, swap bytes
455   // appropriately.
456   return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
457 }
458
459 /// This is based on the type signature computation given in section 7.27 of the
460 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
461 /// with the inclusion of additional forms not specifically called out in the
462 /// standard.
463 uint64_t DIEHash::computeTypeSignature(DIE *Die) {
464   Numbering.clear();
465   Numbering[Die] = 1;
466
467   if (DIE *Parent = Die->getParent())
468     addParentContext(Parent);
469
470   // Hash the DIE.
471   computeHash(Die);
472
473   // Now return the result.
474   MD5::MD5Result Result;
475   Hash.final(Result);
476
477   // ... take the least significant 8 bytes and return those. Our MD5
478   // implementation always returns its results in little endian, swap bytes
479   // appropriately.
480   return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
481 }