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