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 "DIEHash.h" 17 #include "DIE.h" 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/Dwarf.h" 22 #include "llvm/Support/Endian.h" 23 #include "llvm/Support/MD5.h" 24 #include "llvm/Support/raw_ostream.h" 25 26 using namespace llvm; 27 28 /// \brief Grabs the string in whichever attribute is passed in and returns 29 /// a reference to it. 30 static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) { 31 const SmallVectorImpl<DIEValue *> &Values = Die.getValues(); 32 const DIEAbbrev &Abbrevs = Die.getAbbrev(); 33 34 // Iterate through all the attributes until we find the one we're 35 // looking for, if we can't find it return an empty string. 36 for (size_t i = 0; i < Values.size(); ++i) { 37 if (Abbrevs.getData()[i].getAttribute() == Attr) { 38 DIEValue *V = Values[i]; 39 assert(isa<DIEString>(V) && "String requested. Not a string."); 40 DIEString *S = cast<DIEString>(V); 41 return S->getString(); 42 } 43 } 44 return StringRef(""); 45 } 46 47 /// \brief Adds the string in \p Str to the hash. This also hashes 48 /// a trailing NULL with the string. 49 void DIEHash::addString(StringRef Str) { 50 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n"); 51 Hash.update(Str); 52 Hash.update(makeArrayRef((uint8_t)'\0')); 53 } 54 55 // FIXME: The LEB128 routines are copied and only slightly modified out of 56 // LEB128.h. 57 58 /// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128. 59 void DIEHash::addULEB128(uint64_t Value) { 60 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n"); 61 do { 62 uint8_t Byte = Value & 0x7f; 63 Value >>= 7; 64 if (Value != 0) 65 Byte |= 0x80; // Mark this byte to show that more bytes will follow. 66 Hash.update(Byte); 67 } while (Value != 0); 68 } 69 70 void DIEHash::addSLEB128(int64_t Value) { 71 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n"); 72 bool More; 73 do { 74 uint8_t Byte = Value & 0x7f; 75 Value >>= 7; 76 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) || 77 ((Value == -1) && ((Byte & 0x40) != 0)))); 78 if (More) 79 Byte |= 0x80; // Mark this byte to show that more bytes will follow. 80 Hash.update(Byte); 81 } while (More); 82 } 83 84 /// \brief Including \p Parent adds the context of Parent to the hash.. 85 void DIEHash::addParentContext(const DIE &Parent) { 86 87 DEBUG(dbgs() << "Adding parent context to hash...\n"); 88 89 // [7.27.2] For each surrounding type or namespace beginning with the 90 // outermost such construct... 91 SmallVector<const DIE *, 1> Parents; 92 const DIE *Cur = &Parent; 93 while (Cur->getParent()) { 94 Parents.push_back(Cur); 95 Cur = Cur->getParent(); 96 } 97 assert(Cur->getTag() == dwarf::DW_TAG_compile_unit || 98 Cur->getTag() == dwarf::DW_TAG_type_unit); 99 100 // Reverse iterate over our list to go from the outermost construct to the 101 // innermost. 102 for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(), 103 E = Parents.rend(); 104 I != E; ++I) { 105 const DIE &Die = **I; 106 107 // ... Append the letter "C" to the sequence... 108 addULEB128('C'); 109 110 // ... Followed by the DWARF tag of the construct... 111 addULEB128(Die.getTag()); 112 113 // ... Then the name, taken from the DW_AT_name attribute. 114 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name); 115 DEBUG(dbgs() << "... adding context: " << Name << "\n"); 116 if (!Name.empty()) 117 addString(Name); 118 } 119 } 120 121 // Collect all of the attributes for a particular DIE in single structure. 122 void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) { 123 const SmallVectorImpl<DIEValue *> &Values = Die.getValues(); 124 const DIEAbbrev &Abbrevs = Die.getAbbrev(); 125 126 #define COLLECT_ATTR(NAME) \ 127 case dwarf::NAME: \ 128 Attrs.NAME.Val = Values[i]; \ 129 Attrs.NAME.Desc = &Abbrevs.getData()[i]; \ 130 break 131 132 for (size_t i = 0, e = Values.size(); i != e; ++i) { 133 DEBUG(dbgs() << "Attribute: " 134 << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute()) 135 << " added.\n"); 136 switch (Abbrevs.getData()[i].getAttribute()) { 137 COLLECT_ATTR(DW_AT_name); 138 COLLECT_ATTR(DW_AT_accessibility); 139 COLLECT_ATTR(DW_AT_address_class); 140 COLLECT_ATTR(DW_AT_allocated); 141 COLLECT_ATTR(DW_AT_artificial); 142 COLLECT_ATTR(DW_AT_associated); 143 COLLECT_ATTR(DW_AT_binary_scale); 144 COLLECT_ATTR(DW_AT_bit_offset); 145 COLLECT_ATTR(DW_AT_bit_size); 146 COLLECT_ATTR(DW_AT_bit_stride); 147 COLLECT_ATTR(DW_AT_byte_size); 148 COLLECT_ATTR(DW_AT_byte_stride); 149 COLLECT_ATTR(DW_AT_const_expr); 150 COLLECT_ATTR(DW_AT_const_value); 151 COLLECT_ATTR(DW_AT_containing_type); 152 COLLECT_ATTR(DW_AT_count); 153 COLLECT_ATTR(DW_AT_data_bit_offset); 154 COLLECT_ATTR(DW_AT_data_location); 155 COLLECT_ATTR(DW_AT_data_member_location); 156 COLLECT_ATTR(DW_AT_decimal_scale); 157 COLLECT_ATTR(DW_AT_decimal_sign); 158 COLLECT_ATTR(DW_AT_default_value); 159 COLLECT_ATTR(DW_AT_digit_count); 160 COLLECT_ATTR(DW_AT_discr); 161 COLLECT_ATTR(DW_AT_discr_list); 162 COLLECT_ATTR(DW_AT_discr_value); 163 COLLECT_ATTR(DW_AT_encoding); 164 COLLECT_ATTR(DW_AT_enum_class); 165 COLLECT_ATTR(DW_AT_endianity); 166 COLLECT_ATTR(DW_AT_explicit); 167 COLLECT_ATTR(DW_AT_is_optional); 168 COLLECT_ATTR(DW_AT_location); 169 COLLECT_ATTR(DW_AT_lower_bound); 170 COLLECT_ATTR(DW_AT_mutable); 171 COLLECT_ATTR(DW_AT_ordering); 172 COLLECT_ATTR(DW_AT_picture_string); 173 COLLECT_ATTR(DW_AT_prototyped); 174 COLLECT_ATTR(DW_AT_small); 175 COLLECT_ATTR(DW_AT_segment); 176 COLLECT_ATTR(DW_AT_string_length); 177 COLLECT_ATTR(DW_AT_threads_scaled); 178 COLLECT_ATTR(DW_AT_upper_bound); 179 COLLECT_ATTR(DW_AT_use_location); 180 COLLECT_ATTR(DW_AT_use_UTF8); 181 COLLECT_ATTR(DW_AT_variable_parameter); 182 COLLECT_ATTR(DW_AT_virtuality); 183 COLLECT_ATTR(DW_AT_visibility); 184 COLLECT_ATTR(DW_AT_vtable_elem_location); 185 COLLECT_ATTR(DW_AT_type); 186 default: 187 break; 188 } 189 } 190 } 191 192 void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute, 193 const DIE &Entry, StringRef Name) { 194 // append the letter 'N' 195 addULEB128('N'); 196 197 // the DWARF attribute code (DW_AT_type or DW_AT_friend), 198 addULEB128(Attribute); 199 200 // the context of the tag, 201 if (const DIE *Parent = Entry.getParent()) 202 addParentContext(*Parent); 203 204 // the letter 'E', 205 addULEB128('E'); 206 207 // and the name of the type. 208 addString(Name); 209 210 // Currently DW_TAG_friends are not used by Clang, but if they do become so, 211 // here's the relevant spec text to implement: 212 // 213 // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram, 214 // the context is omitted and the name to be used is the ABI-specific name 215 // of the subprogram (e.g., the mangled linker name). 216 } 217 218 void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute, 219 unsigned DieNumber) { 220 // a) If T is in the list of [previously hashed types], use the letter 221 // 'R' as the marker 222 addULEB128('R'); 223 224 addULEB128(Attribute); 225 226 // and use the unsigned LEB128 encoding of [the index of T in the 227 // list] as the attribute value; 228 addULEB128(DieNumber); 229 } 230 231 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag, 232 const DIE &Entry) { 233 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend " 234 "tags. Add support here when there's " 235 "a use case"); 236 // Step 5 237 // If the tag in Step 3 is one of [the below tags] 238 if ((Tag == dwarf::DW_TAG_pointer_type || 239 Tag == dwarf::DW_TAG_reference_type || 240 Tag == dwarf::DW_TAG_rvalue_reference_type || 241 Tag == dwarf::DW_TAG_ptr_to_member_type) && 242 // and the referenced type (via the [below attributes]) 243 // FIXME: This seems overly restrictive, and causes hash mismatches 244 // there's a decl/def difference in the containing type of a 245 // ptr_to_member_type, but it's what DWARF says, for some reason. 246 Attribute == dwarf::DW_AT_type) { 247 // ... has a DW_AT_name attribute, 248 StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name); 249 if (!Name.empty()) { 250 hashShallowTypeReference(Attribute, Entry, Name); 251 return; 252 } 253 } 254 255 unsigned &DieNumber = Numbering[&Entry]; 256 if (DieNumber) { 257 hashRepeatedTypeReference(Attribute, DieNumber); 258 return; 259 } 260 261 // otherwise, b) use the letter 'T' as a the marker, ... 262 addULEB128('T'); 263 264 addULEB128(Attribute); 265 266 // ... process the type T recursively by performing Steps 2 through 7, and 267 // use the result as the attribute value. 268 DieNumber = Numbering.size(); 269 computeHash(Entry); 270 } 271 272 // Hash an individual attribute \param Attr based on the type of attribute and 273 // the form. 274 void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) { 275 const DIEValue *Value = Attr.Val; 276 const DIEAbbrevData *Desc = Attr.Desc; 277 dwarf::Attribute Attribute = Desc->getAttribute(); 278 279 // 7.27 Step 3 280 // ... An attribute that refers to another type entry T is processed as 281 // follows: 282 if (const DIEEntry *EntryAttr = dyn_cast<DIEEntry>(Value)) { 283 hashDIEEntry(Attribute, Tag, *EntryAttr->getEntry()); 284 return; 285 } 286 287 // Other attribute values use the letter 'A' as the marker, and the value 288 // consists of the form code (encoded as an unsigned LEB128 value) followed by 289 // the encoding of the value according to the form code. To ensure 290 // reproducibility of the signature, the set of forms used in the signature 291 // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag, 292 // 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('A'); 300 addULEB128(Attribute); 301 addULEB128(dwarf::DW_FORM_string); 302 addString(cast<DIEString>(Value)->getString()); 303 break; 304 case dwarf::DW_FORM_data1: 305 case dwarf::DW_FORM_data2: 306 case dwarf::DW_FORM_data4: 307 case dwarf::DW_FORM_data8: 308 case dwarf::DW_FORM_udata: 309 addULEB128('A'); 310 addULEB128(Attribute); 311 addULEB128(dwarf::DW_FORM_sdata); 312 addSLEB128((int64_t)cast<DIEInteger>(Value)->getValue()); 313 break; 314 // DW_FORM_flag_present is just flag with a value of one. We still give it a 315 // value so just use the value. 316 case dwarf::DW_FORM_flag_present: 317 case dwarf::DW_FORM_flag: 318 addULEB128('A'); 319 addULEB128(Attribute); 320 addULEB128(dwarf::DW_FORM_flag); 321 addULEB128((int64_t)cast<DIEInteger>(Value)->getValue()); 322 break; 323 default: 324 llvm_unreachable("Add support for additional forms"); 325 } 326 } 327 328 // Go through the attributes from \param Attrs in the order specified in 7.27.4 329 // and hash them. 330 void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) { 331 #define ADD_ATTR(ATTR) \ 332 { \ 333 if (ATTR.Val != 0) \ 334 hashAttribute(ATTR, Tag); \ 335 } 336 337 ADD_ATTR(Attrs.DW_AT_name); 338 ADD_ATTR(Attrs.DW_AT_accessibility); 339 ADD_ATTR(Attrs.DW_AT_address_class); 340 ADD_ATTR(Attrs.DW_AT_allocated); 341 ADD_ATTR(Attrs.DW_AT_artificial); 342 ADD_ATTR(Attrs.DW_AT_associated); 343 ADD_ATTR(Attrs.DW_AT_binary_scale); 344 ADD_ATTR(Attrs.DW_AT_bit_offset); 345 ADD_ATTR(Attrs.DW_AT_bit_size); 346 ADD_ATTR(Attrs.DW_AT_bit_stride); 347 ADD_ATTR(Attrs.DW_AT_byte_size); 348 ADD_ATTR(Attrs.DW_AT_byte_stride); 349 ADD_ATTR(Attrs.DW_AT_const_expr); 350 ADD_ATTR(Attrs.DW_AT_const_value); 351 ADD_ATTR(Attrs.DW_AT_containing_type); 352 ADD_ATTR(Attrs.DW_AT_count); 353 ADD_ATTR(Attrs.DW_AT_data_bit_offset); 354 ADD_ATTR(Attrs.DW_AT_data_location); 355 ADD_ATTR(Attrs.DW_AT_data_member_location); 356 ADD_ATTR(Attrs.DW_AT_decimal_scale); 357 ADD_ATTR(Attrs.DW_AT_decimal_sign); 358 ADD_ATTR(Attrs.DW_AT_default_value); 359 ADD_ATTR(Attrs.DW_AT_digit_count); 360 ADD_ATTR(Attrs.DW_AT_discr); 361 ADD_ATTR(Attrs.DW_AT_discr_list); 362 ADD_ATTR(Attrs.DW_AT_discr_value); 363 ADD_ATTR(Attrs.DW_AT_encoding); 364 ADD_ATTR(Attrs.DW_AT_enum_class); 365 ADD_ATTR(Attrs.DW_AT_endianity); 366 ADD_ATTR(Attrs.DW_AT_explicit); 367 ADD_ATTR(Attrs.DW_AT_is_optional); 368 ADD_ATTR(Attrs.DW_AT_location); 369 ADD_ATTR(Attrs.DW_AT_lower_bound); 370 ADD_ATTR(Attrs.DW_AT_mutable); 371 ADD_ATTR(Attrs.DW_AT_ordering); 372 ADD_ATTR(Attrs.DW_AT_picture_string); 373 ADD_ATTR(Attrs.DW_AT_prototyped); 374 ADD_ATTR(Attrs.DW_AT_small); 375 ADD_ATTR(Attrs.DW_AT_segment); 376 ADD_ATTR(Attrs.DW_AT_string_length); 377 ADD_ATTR(Attrs.DW_AT_threads_scaled); 378 ADD_ATTR(Attrs.DW_AT_upper_bound); 379 ADD_ATTR(Attrs.DW_AT_use_location); 380 ADD_ATTR(Attrs.DW_AT_use_UTF8); 381 ADD_ATTR(Attrs.DW_AT_variable_parameter); 382 ADD_ATTR(Attrs.DW_AT_virtuality); 383 ADD_ATTR(Attrs.DW_AT_visibility); 384 ADD_ATTR(Attrs.DW_AT_vtable_elem_location); 385 ADD_ATTR(Attrs.DW_AT_type); 386 387 // FIXME: Add the extended attributes. 388 } 389 390 // Add all of the attributes for \param Die to the hash. 391 void DIEHash::addAttributes(const DIE &Die) { 392 DIEAttrs Attrs = {}; 393 collectAttributes(Die, Attrs); 394 hashAttributes(Attrs, Die.getTag()); 395 } 396 397 void DIEHash::hashNestedType(const DIE &Die, StringRef Name) { 398 // 7.27 Step 7 399 // ... append the letter 'S', 400 addULEB128('S'); 401 402 // the tag of C, 403 addULEB128(Die.getTag()); 404 405 // and the name. 406 addString(Name); 407 } 408 409 // Compute the hash of a DIE. This is based on the type signature computation 410 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a 411 // flattened description of the DIE. 412 void DIEHash::computeHash(const DIE &Die) { 413 // Append the letter 'D', followed by the DWARF tag of the DIE. 414 addULEB128('D'); 415 addULEB128(Die.getTag()); 416 417 // Add each of the attributes of the DIE. 418 addAttributes(Die); 419 420 // Then hash each of the children of the DIE. 421 for (std::vector<DIE *>::const_iterator I = Die.getChildren().begin(), 422 E = Die.getChildren().end(); 423 I != E; ++I) { 424 // 7.27 Step 7 425 // If C is a nested type entry or a member function entry, ... 426 if (isType((*I)->getTag()) || (*I)->getTag() == dwarf::DW_TAG_subprogram) { 427 StringRef Name = getDIEStringAttr(**I, dwarf::DW_AT_name); 428 // ... and has a DW_AT_name attribute 429 if (!Name.empty()) { 430 hashNestedType(**I, Name); 431 continue; 432 } 433 } 434 computeHash(**I); 435 } 436 437 // Following the last (or if there are no children), append a zero byte. 438 Hash.update(makeArrayRef((uint8_t)'\0')); 439 } 440 441 /// This is based on the type signature computation given in section 7.27 of the 442 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE 443 /// with the exception that we are hashing only the context and the name of the 444 /// type. 445 uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) { 446 447 // Add the contexts to the hash. We won't be computing the ODR hash for 448 // function local types so it's safe to use the generic context hashing 449 // algorithm here. 450 // FIXME: If we figure out how to account for linkage in some way we could 451 // actually do this with a slight modification to the parent hash algorithm. 452 if (const DIE *Parent = Die.getParent()) 453 addParentContext(*Parent); 454 455 // Add the current DIE information. 456 457 // Add the DWARF tag of the DIE. 458 addULEB128(Die.getTag()); 459 460 // Add the name of the type to the hash. 461 addString(getDIEStringAttr(Die, dwarf::DW_AT_name)); 462 463 // Now get the result. 464 MD5::MD5Result Result; 465 Hash.final(Result); 466 467 // ... take the least significant 8 bytes and return those. Our MD5 468 // implementation always returns its results in little endian, swap bytes 469 // appropriately. 470 return *reinterpret_cast<support::ulittle64_t *>(Result + 8); 471 } 472 473 /// This is based on the type signature computation given in section 7.27 of the 474 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE 475 /// with the inclusion of the full CU and all top level CU entities. 476 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures. 477 uint64_t DIEHash::computeCUSignature(const DIE &Die) { 478 Numbering.clear(); 479 Numbering[&Die] = 1; 480 481 // Hash the DIE. 482 computeHash(Die); 483 484 // Now return the result. 485 MD5::MD5Result Result; 486 Hash.final(Result); 487 488 // ... take the least significant 8 bytes and return those. Our MD5 489 // implementation always returns its results in little endian, swap bytes 490 // appropriately. 491 return *reinterpret_cast<support::ulittle64_t *>(Result + 8); 492 } 493 494 /// This is based on the type signature computation given in section 7.27 of the 495 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE 496 /// with the inclusion of additional forms not specifically called out in the 497 /// standard. 498 uint64_t DIEHash::computeTypeSignature(const DIE &Die) { 499 Numbering.clear(); 500 Numbering[&Die] = 1; 501 502 if (const DIE *Parent = Die.getParent()) 503 addParentContext(*Parent); 504 505 // Hash the DIE. 506 computeHash(Die); 507 508 // Now return the result. 509 MD5::MD5Result Result; 510 Hash.final(Result); 511 512 // ... take the least significant 8 bytes and return those. Our MD5 513 // implementation always returns its results in little endian, swap bytes 514 // appropriately. 515 return *reinterpret_cast<support::ulittle64_t *>(Result + 8); 516 } 517