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