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