1 //===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for DWARF4 hashing of DIEs.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dwarfdebug"
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"
29 /// \brief Grabs the string in whichever attribute is passed in and returns
30 /// a reference to it.
31 static StringRef getDIEStringAttr(DIE *Die, uint16_t Attr) {
32 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
33 const DIEAbbrev &Abbrevs = Die->getAbbrev();
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();
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");
53 Hash.update(makeArrayRef((uint8_t)'\0'));
56 // FIXME: The LEB128 routines are copied and only slightly modified out of
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");
63 uint8_t Byte = Value & 0x7f;
66 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
71 void DIEHash::addSLEB128(int64_t Value) {
72 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
75 uint8_t Byte = Value & 0x7f;
77 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
78 ((Value == -1) && ((Byte & 0x40) != 0))));
80 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
85 /// \brief Including \p Parent adds the context of Parent to the hash..
86 void DIEHash::addParentContext(DIE *Parent) {
88 DEBUG(dbgs() << "Adding parent context to hash...\n");
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();
98 // Reverse iterate over our list to go from the outermost construct to the
100 for (SmallVectorImpl<DIE *>::reverse_iterator I = Parents.rbegin(),
105 // ... Append the letter "C" to the sequence...
108 // ... Followed by the DWARF tag of the construct...
109 addULEB128(Die->getTag());
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");
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();
124 #define COLLECT_ATTR(NAME) \
126 Attrs.NAME.Val = Values[i]; \
127 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
130 for (size_t i = 0, e = Values.size(); i != e; ++i) {
131 DEBUG(dbgs() << "Attribute: "
132 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
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);
190 // Hash an individual attribute \param Attr based on the type of attribute and
192 void DIEHash::hashAttribute(AttrEntry Attr) {
193 const DIEValue *Value = Attr.Val;
194 const DIEAbbrevData *Desc = Attr.Desc;
197 // ... An attribute that refers to another type entry T is processed as
199 // a) If T is in the list of [previously hashed types], use the letter 'R' as
200 // the marker and use the unsigned LEB128 encoding of [the index of T in the
201 // list] as the attribute value; otherwise,
203 // [TODO: implement clause (a)]
205 if (const DIEEntry *EntryAttr = dyn_cast<DIEEntry>(Value)) {
206 DIE *Entry = EntryAttr->getEntry();
208 // b) use the letter 'T' as a the marker, ...
211 addULEB128(Desc->getAttribute());
213 // ... process the type T recursively by performing Steps 2 through 7, and
214 // use the result as the attribute value.
219 // Other attribute values use the letter 'A' as the marker, ...
222 addULEB128(Desc->getAttribute());
224 // ... and the value consists of the form code (encoded as an unsigned LEB128
225 // value) followed by the encoding of the value according to the form code. To
226 // ensure reproducibility of the signature, the set of forms used in the
227 // signature computation is limited to the following: DW_FORM_sdata,
228 // DW_FORM_flag, DW_FORM_string, and DW_FORM_block.
229 switch (Desc->getForm()) {
230 case dwarf::DW_FORM_string:
232 "Add support for DW_FORM_string if we ever start emitting them again");
233 case dwarf::DW_FORM_strp:
234 addULEB128(dwarf::DW_FORM_string);
235 addString(cast<DIEString>(Value)->getString());
237 case dwarf::DW_FORM_data1:
238 case dwarf::DW_FORM_data2:
239 case dwarf::DW_FORM_data4:
240 case dwarf::DW_FORM_data8:
241 case dwarf::DW_FORM_udata:
242 addULEB128(dwarf::DW_FORM_sdata);
243 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue());
245 // TODO: Add support for additional forms.
249 // Go through the attributes from \param Attrs in the order specified in 7.27.4
251 void DIEHash::hashAttributes(const DIEAttrs &Attrs) {
252 #define ADD_ATTR(ATTR) \
255 hashAttribute(ATTR); \
258 ADD_ATTR(Attrs.DW_AT_name);
259 ADD_ATTR(Attrs.DW_AT_accessibility);
260 ADD_ATTR(Attrs.DW_AT_address_class);
261 ADD_ATTR(Attrs.DW_AT_allocated);
262 ADD_ATTR(Attrs.DW_AT_artificial);
263 ADD_ATTR(Attrs.DW_AT_associated);
264 ADD_ATTR(Attrs.DW_AT_binary_scale);
265 ADD_ATTR(Attrs.DW_AT_bit_offset);
266 ADD_ATTR(Attrs.DW_AT_bit_size);
267 ADD_ATTR(Attrs.DW_AT_bit_stride);
268 ADD_ATTR(Attrs.DW_AT_byte_size);
269 ADD_ATTR(Attrs.DW_AT_byte_stride);
270 ADD_ATTR(Attrs.DW_AT_const_expr);
271 ADD_ATTR(Attrs.DW_AT_const_value);
272 ADD_ATTR(Attrs.DW_AT_containing_type);
273 ADD_ATTR(Attrs.DW_AT_count);
274 ADD_ATTR(Attrs.DW_AT_data_bit_offset);
275 ADD_ATTR(Attrs.DW_AT_data_location);
276 ADD_ATTR(Attrs.DW_AT_data_member_location);
277 ADD_ATTR(Attrs.DW_AT_decimal_scale);
278 ADD_ATTR(Attrs.DW_AT_decimal_sign);
279 ADD_ATTR(Attrs.DW_AT_default_value);
280 ADD_ATTR(Attrs.DW_AT_digit_count);
281 ADD_ATTR(Attrs.DW_AT_discr);
282 ADD_ATTR(Attrs.DW_AT_discr_list);
283 ADD_ATTR(Attrs.DW_AT_discr_value);
284 ADD_ATTR(Attrs.DW_AT_encoding);
285 ADD_ATTR(Attrs.DW_AT_enum_class);
286 ADD_ATTR(Attrs.DW_AT_endianity);
287 ADD_ATTR(Attrs.DW_AT_explicit);
288 ADD_ATTR(Attrs.DW_AT_is_optional);
289 ADD_ATTR(Attrs.DW_AT_location);
290 ADD_ATTR(Attrs.DW_AT_lower_bound);
291 ADD_ATTR(Attrs.DW_AT_mutable);
292 ADD_ATTR(Attrs.DW_AT_ordering);
293 ADD_ATTR(Attrs.DW_AT_picture_string);
294 ADD_ATTR(Attrs.DW_AT_prototyped);
295 ADD_ATTR(Attrs.DW_AT_small);
296 ADD_ATTR(Attrs.DW_AT_segment);
297 ADD_ATTR(Attrs.DW_AT_string_length);
298 ADD_ATTR(Attrs.DW_AT_threads_scaled);
299 ADD_ATTR(Attrs.DW_AT_upper_bound);
300 ADD_ATTR(Attrs.DW_AT_use_location);
301 ADD_ATTR(Attrs.DW_AT_use_UTF8);
302 ADD_ATTR(Attrs.DW_AT_variable_parameter);
303 ADD_ATTR(Attrs.DW_AT_virtuality);
304 ADD_ATTR(Attrs.DW_AT_visibility);
305 ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
306 ADD_ATTR(Attrs.DW_AT_type);
308 // FIXME: Add the extended attributes.
311 // Add all of the attributes for \param Die to the hash.
312 void DIEHash::addAttributes(DIE *Die) {
314 collectAttributes(Die, Attrs);
315 hashAttributes(Attrs);
318 // Compute the hash of a DIE. This is based on the type signature computation
319 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
320 // flattened description of the DIE.
321 void DIEHash::computeHash(DIE *Die) {
323 // Append the letter 'D', followed by the DWARF tag of the DIE.
325 addULEB128(Die->getTag());
327 // Add each of the attributes of the DIE.
330 // Then hash each of the children of the DIE.
331 for (std::vector<DIE *>::const_iterator I = Die->getChildren().begin(),
332 E = Die->getChildren().end();
336 // Following the last (or if there are no children), append a zero byte.
337 Hash.update(makeArrayRef((uint8_t)'\0'));
340 /// This is based on the type signature computation given in section 7.27 of the
341 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
342 /// with the exception that we are hashing only the context and the name of the
344 uint64_t DIEHash::computeDIEODRSignature(DIE *Die) {
346 // Add the contexts to the hash. We won't be computing the ODR hash for
347 // function local types so it's safe to use the generic context hashing
349 // FIXME: If we figure out how to account for linkage in some way we could
350 // actually do this with a slight modification to the parent hash algorithm.
351 DIE *Parent = Die->getParent();
353 addParentContext(Parent);
355 // Add the current DIE information.
357 // Add the DWARF tag of the DIE.
358 addULEB128(Die->getTag());
360 // Add the name of the type to the hash.
361 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
363 // Now get the result.
364 MD5::MD5Result Result;
367 // ... take the least significant 8 bytes and return those. Our MD5
368 // implementation always returns its results in little endian, swap bytes
370 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
373 /// This is based on the type signature computation given in section 7.27 of the
374 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
375 /// with the inclusion of the full CU and all top level CU entities.
376 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
377 uint64_t DIEHash::computeCUSignature(DIE *Die) {
382 // Now return the result.
383 MD5::MD5Result Result;
386 // ... take the least significant 8 bytes and return those. Our MD5
387 // implementation always returns its results in little endian, swap bytes
389 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
392 /// This is based on the type signature computation given in section 7.27 of the
393 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
394 /// with the inclusion of additional forms not specifically called out in the
396 uint64_t DIEHash::computeTypeSignature(DIE *Die) {
398 if (DIE *Parent = Die->getParent())
399 addParentContext(Parent);
404 // Now return the result.
405 MD5::MD5Result Result;
408 // ... take the least significant 8 bytes and return those. Our MD5
409 // implementation always returns its results in little endian, swap bytes
411 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);