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 | |
Eric Christopher | 4f17ee0 | 2014-03-08 00:29:41 +0000 | [diff] [blame] | 14 | #include "ByteStreamer.h" |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 15 | #include "DIEHash.h" |
Eric Christopher | 4f17ee0 | 2014-03-08 00:29:41 +0000 | [diff] [blame] | 16 | #include "DwarfDebug.h" |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Eric Christopher | 420569b | 2014-02-20 02:50:45 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/AsmPrinter.h" |
Frederic Riss | e541e0b | 2015-01-05 21:29:41 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/DIE.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 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "dwarfdebug" |
| 30 | |
Eric Christopher | 4573198 | 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 | afcb965 | 2013-10-24 17:51:43 +0000 | [diff] [blame] | 33 | static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) { |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 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. |
Duncan P. N. Exon Smith | 88a8fc5 | 2015-05-27 22:44:06 +0000 | [diff] [blame] | 36 | for (const auto &V : Die.values()) |
| 37 | if (V.getAttribute() == Attr) |
| 38 | return V.getDIEString().getString(); |
| 39 | |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 40 | return StringRef(""); |
| 41 | } |
| 42 | |
| 43 | /// \brief Adds the string in \p Str to the hash. This also hashes |
| 44 | /// a trailing NULL with the string. |
| 45 | void DIEHash::addString(StringRef Str) { |
| 46 | DEBUG(dbgs() << "Adding string " << Str << " to hash.\n"); |
| 47 | Hash.update(Str); |
| 48 | Hash.update(makeArrayRef((uint8_t)'\0')); |
| 49 | } |
| 50 | |
| 51 | // FIXME: The LEB128 routines are copied and only slightly modified out of |
| 52 | // LEB128.h. |
| 53 | |
| 54 | /// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128. |
| 55 | void DIEHash::addULEB128(uint64_t Value) { |
| 56 | DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n"); |
| 57 | do { |
| 58 | uint8_t Byte = Value & 0x7f; |
| 59 | Value >>= 7; |
| 60 | if (Value != 0) |
| 61 | Byte |= 0x80; // Mark this byte to show that more bytes will follow. |
| 62 | Hash.update(Byte); |
| 63 | } while (Value != 0); |
| 64 | } |
| 65 | |
David Blaikie | 6316ca4 | 2013-10-16 23:36:20 +0000 | [diff] [blame] | 66 | void DIEHash::addSLEB128(int64_t Value) { |
| 67 | DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n"); |
| 68 | bool More; |
| 69 | do { |
| 70 | uint8_t Byte = Value & 0x7f; |
| 71 | Value >>= 7; |
Eric Christopher | a1b87fd | 2014-02-20 02:40:41 +0000 | [diff] [blame] | 72 | More = !((((Value == 0) && ((Byte & 0x40) == 0)) || |
David Blaikie | 6316ca4 | 2013-10-16 23:36:20 +0000 | [diff] [blame] | 73 | ((Value == -1) && ((Byte & 0x40) != 0)))); |
| 74 | if (More) |
| 75 | Byte |= 0x80; // Mark this byte to show that more bytes will follow. |
| 76 | Hash.update(Byte); |
| 77 | } while (More); |
| 78 | } |
| 79 | |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 80 | /// \brief Including \p Parent adds the context of Parent to the hash.. |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 81 | void DIEHash::addParentContext(const DIE &Parent) { |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 82 | |
| 83 | DEBUG(dbgs() << "Adding parent context to hash...\n"); |
| 84 | |
| 85 | // [7.27.2] For each surrounding type or namespace beginning with the |
| 86 | // outermost such construct... |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 87 | SmallVector<const DIE *, 1> Parents; |
| 88 | const DIE *Cur = &Parent; |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 89 | while (Cur->getParent()) { |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 90 | Parents.push_back(Cur); |
| 91 | Cur = Cur->getParent(); |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 92 | } |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 93 | assert(Cur->getTag() == dwarf::DW_TAG_compile_unit || |
| 94 | Cur->getTag() == dwarf::DW_TAG_type_unit); |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 95 | |
| 96 | // Reverse iterate over our list to go from the outermost construct to the |
| 97 | // innermost. |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 98 | for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(), |
| 99 | E = Parents.rend(); |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 100 | I != E; ++I) { |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 101 | const DIE &Die = **I; |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 102 | |
| 103 | // ... Append the letter "C" to the sequence... |
| 104 | addULEB128('C'); |
| 105 | |
| 106 | // ... Followed by the DWARF tag of the construct... |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 107 | addULEB128(Die.getTag()); |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 108 | |
| 109 | // ... Then the name, taken from the DW_AT_name attribute. |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 110 | StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name); |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 111 | DEBUG(dbgs() << "... adding context: " << Name << "\n"); |
| 112 | if (!Name.empty()) |
| 113 | addString(Name); |
| 114 | } |
| 115 | } |
| 116 | |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 117 | // Collect all of the attributes for a particular DIE in single structure. |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 118 | void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) { |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 119 | |
Duncan P. N. Exon Smith | 88a8fc5 | 2015-05-27 22:44:06 +0000 | [diff] [blame] | 120 | for (const auto &V : Die.values()) { |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 121 | DEBUG(dbgs() << "Attribute: " |
Duncan P. N. Exon Smith | 88a8fc5 | 2015-05-27 22:44:06 +0000 | [diff] [blame] | 122 | << dwarf::AttributeString(V.getAttribute()) |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 123 | << " added.\n"); |
Duncan P. N. Exon Smith | 88a8fc5 | 2015-05-27 22:44:06 +0000 | [diff] [blame] | 124 | switch (V.getAttribute()) { |
David Blaikie | 74fa803 | 2017-05-23 18:27:09 +0000 | [diff] [blame] | 125 | #define HANDLE_DIE_HASH_ATTR(NAME) \ |
| 126 | case dwarf::NAME: \ |
| 127 | Attrs.NAME = V; \ |
| 128 | break; |
| 129 | #include "DIEHashAttributes.def" |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
David Blaikie | afcb965 | 2013-10-24 17:51:43 +0000 | [diff] [blame] | 136 | void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute, |
| 137 | const DIE &Entry, StringRef Name) { |
| 138 | // append the letter 'N' |
| 139 | addULEB128('N'); |
| 140 | |
| 141 | // the DWARF attribute code (DW_AT_type or DW_AT_friend), |
| 142 | addULEB128(Attribute); |
| 143 | |
| 144 | // the context of the tag, |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 145 | if (const DIE *Parent = Entry.getParent()) |
| 146 | addParentContext(*Parent); |
David Blaikie | afcb965 | 2013-10-24 17:51:43 +0000 | [diff] [blame] | 147 | |
| 148 | // the letter 'E', |
| 149 | addULEB128('E'); |
| 150 | |
| 151 | // and the name of the type. |
| 152 | addString(Name); |
| 153 | |
| 154 | // Currently DW_TAG_friends are not used by Clang, but if they do become so, |
| 155 | // here's the relevant spec text to implement: |
| 156 | // |
| 157 | // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram, |
| 158 | // the context is omitted and the name to be used is the ABI-specific name |
| 159 | // of the subprogram (e.g., the mangled linker name). |
| 160 | } |
| 161 | |
| 162 | void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute, |
| 163 | unsigned DieNumber) { |
| 164 | // a) If T is in the list of [previously hashed types], use the letter |
| 165 | // 'R' as the marker |
| 166 | addULEB128('R'); |
| 167 | |
| 168 | addULEB128(Attribute); |
| 169 | |
| 170 | // and use the unsigned LEB128 encoding of [the index of T in the |
| 171 | // list] as the attribute value; |
| 172 | addULEB128(DieNumber); |
| 173 | } |
| 174 | |
| 175 | void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag, |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 176 | const DIE &Entry) { |
David Blaikie | afcb965 | 2013-10-24 17:51:43 +0000 | [diff] [blame] | 177 | assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend " |
| 178 | "tags. Add support here when there's " |
| 179 | "a use case"); |
| 180 | // Step 5 |
| 181 | // If the tag in Step 3 is one of [the below tags] |
| 182 | if ((Tag == dwarf::DW_TAG_pointer_type || |
| 183 | Tag == dwarf::DW_TAG_reference_type || |
| 184 | Tag == dwarf::DW_TAG_rvalue_reference_type || |
| 185 | Tag == dwarf::DW_TAG_ptr_to_member_type) && |
| 186 | // and the referenced type (via the [below attributes]) |
| 187 | // FIXME: This seems overly restrictive, and causes hash mismatches |
| 188 | // there's a decl/def difference in the containing type of a |
| 189 | // ptr_to_member_type, but it's what DWARF says, for some reason. |
| 190 | Attribute == dwarf::DW_AT_type) { |
David Blaikie | 3274441 | 2013-10-24 17:53:58 +0000 | [diff] [blame] | 191 | // ... has a DW_AT_name attribute, |
| 192 | StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name); |
| 193 | if (!Name.empty()) { |
| 194 | hashShallowTypeReference(Attribute, Entry, Name); |
| 195 | return; |
| 196 | } |
David Blaikie | afcb965 | 2013-10-24 17:51:43 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | unsigned &DieNumber = Numbering[&Entry]; |
| 200 | if (DieNumber) { |
| 201 | hashRepeatedTypeReference(Attribute, DieNumber); |
| 202 | return; |
| 203 | } |
| 204 | |
Robin Morisset | 039781e | 2014-08-29 21:53:01 +0000 | [diff] [blame] | 205 | // otherwise, b) use the letter 'T' as the marker, ... |
David Blaikie | afcb965 | 2013-10-24 17:51:43 +0000 | [diff] [blame] | 206 | addULEB128('T'); |
| 207 | |
| 208 | addULEB128(Attribute); |
| 209 | |
| 210 | // ... process the type T recursively by performing Steps 2 through 7, and |
| 211 | // use the result as the attribute value. |
| 212 | DieNumber = Numbering.size(); |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 213 | computeHash(Entry); |
David Blaikie | afcb965 | 2013-10-24 17:51:43 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Eric Christopher | 420569b | 2014-02-20 02:50:45 +0000 | [diff] [blame] | 216 | // Hash all of the values in a block like set of values. This assumes that |
| 217 | // all of the data is going to be added as integers. |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 218 | void DIEHash::hashBlockData(const DIE::const_value_range &Values) { |
Duncan P. N. Exon Smith | 88a8fc5 | 2015-05-27 22:44:06 +0000 | [diff] [blame] | 219 | for (const auto &V : Values) |
| 220 | Hash.update((uint64_t)V.getDIEInteger().getValue()); |
Eric Christopher | 420569b | 2014-02-20 02:50:45 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Eric Christopher | 4f17ee0 | 2014-03-08 00:29:41 +0000 | [diff] [blame] | 223 | // Hash the contents of a loclistptr class. |
| 224 | void DIEHash::hashLocList(const DIELocList &LocList) { |
Eric Christopher | 4f17ee0 | 2014-03-08 00:29:41 +0000 | [diff] [blame] | 225 | HashingByteStreamer Streamer(*this); |
David Blaikie | 0a456de | 2014-04-02 01:43:18 +0000 | [diff] [blame] | 226 | DwarfDebug &DD = *AP->getDwarfDebug(); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 227 | const DebugLocStream &Locs = DD.getDebugLocs(); |
| 228 | for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue()))) |
David Blaikie | 0a456de | 2014-04-02 01:43:18 +0000 | [diff] [blame] | 229 | DD.emitDebugLocEntry(Streamer, Entry); |
Eric Christopher | 4f17ee0 | 2014-03-08 00:29:41 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 232 | // Hash an individual attribute \param Attr based on the type of attribute and |
| 233 | // the form. |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 234 | void DIEHash::hashAttribute(const DIEValue &Value, dwarf::Tag Tag) { |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 235 | dwarf::Attribute Attribute = Value.getAttribute(); |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 236 | |
Eric Christopher | 4b1cf58 | 2014-01-31 20:02:58 +0000 | [diff] [blame] | 237 | // Other attribute values use the letter 'A' as the marker, and the value |
| 238 | // consists of the form code (encoded as an unsigned LEB128 value) followed by |
| 239 | // the encoding of the value according to the form code. To ensure |
| 240 | // reproducibility of the signature, the set of forms used in the signature |
| 241 | // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag, |
| 242 | // DW_FORM_string, and DW_FORM_block. |
Eric Christopher | 1930849 | 2014-03-06 00:38:32 +0000 | [diff] [blame] | 243 | |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 244 | switch (Value.getType()) { |
| 245 | case DIEValue::isNone: |
| 246 | llvm_unreachable("Expected valid DIEValue"); |
| 247 | |
Eric Christopher | 2bed257 | 2014-03-06 18:59:42 +0000 | [diff] [blame] | 248 | // 7.27 Step 3 |
| 249 | // ... An attribute that refers to another type entry T is processed as |
| 250 | // follows: |
| 251 | case DIEValue::isEntry: |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 252 | hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry()); |
Eric Christopher | 2bed257 | 2014-03-06 18:59:42 +0000 | [diff] [blame] | 253 | break; |
Eric Christopher | 1930849 | 2014-03-06 00:38:32 +0000 | [diff] [blame] | 254 | case DIEValue::isInteger: { |
| 255 | addULEB128('A'); |
| 256 | addULEB128(Attribute); |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 257 | switch (Value.getForm()) { |
Eric Christopher | 1930849 | 2014-03-06 00:38:32 +0000 | [diff] [blame] | 258 | case dwarf::DW_FORM_data1: |
| 259 | case dwarf::DW_FORM_data2: |
| 260 | case dwarf::DW_FORM_data4: |
| 261 | case dwarf::DW_FORM_data8: |
| 262 | case dwarf::DW_FORM_udata: |
| 263 | case dwarf::DW_FORM_sdata: |
| 264 | addULEB128(dwarf::DW_FORM_sdata); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 265 | addSLEB128((int64_t)Value.getDIEInteger().getValue()); |
Eric Christopher | 1930849 | 2014-03-06 00:38:32 +0000 | [diff] [blame] | 266 | break; |
| 267 | // DW_FORM_flag_present is just flag with a value of one. We still give it a |
| 268 | // value so just use the value. |
| 269 | case dwarf::DW_FORM_flag_present: |
| 270 | case dwarf::DW_FORM_flag: |
| 271 | addULEB128(dwarf::DW_FORM_flag); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 272 | addULEB128((int64_t)Value.getDIEInteger().getValue()); |
Eric Christopher | 1930849 | 2014-03-06 00:38:32 +0000 | [diff] [blame] | 273 | break; |
| 274 | default: |
| 275 | llvm_unreachable("Unknown integer form!"); |
| 276 | } |
| 277 | break; |
| 278 | } |
| 279 | case DIEValue::isString: |
Eric Christopher | 4b1cf58 | 2014-01-31 20:02:58 +0000 | [diff] [blame] | 280 | addULEB128('A'); |
| 281 | addULEB128(Attribute); |
David Blaikie | 6316ca4 | 2013-10-16 23:36:20 +0000 | [diff] [blame] | 282 | addULEB128(dwarf::DW_FORM_string); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 283 | addString(Value.getDIEString().getString()); |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 284 | break; |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 285 | case DIEValue::isInlineString: |
| 286 | addULEB128('A'); |
| 287 | addULEB128(Attribute); |
| 288 | addULEB128(dwarf::DW_FORM_string); |
| 289 | addString(Value.getDIEInlineString().getString()); |
| 290 | break; |
Eric Christopher | 1930849 | 2014-03-06 00:38:32 +0000 | [diff] [blame] | 291 | case DIEValue::isBlock: |
| 292 | case DIEValue::isLoc: |
Eric Christopher | 4f17ee0 | 2014-03-08 00:29:41 +0000 | [diff] [blame] | 293 | case DIEValue::isLocList: |
Eric Christopher | 420569b | 2014-02-20 02:50:45 +0000 | [diff] [blame] | 294 | addULEB128('A'); |
| 295 | addULEB128(Attribute); |
| 296 | addULEB128(dwarf::DW_FORM_block); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 297 | if (Value.getType() == DIEValue::isBlock) { |
| 298 | addULEB128(Value.getDIEBlock().ComputeSize(AP)); |
Duncan P. N. Exon Smith | 88a8fc5 | 2015-05-27 22:44:06 +0000 | [diff] [blame] | 299 | hashBlockData(Value.getDIEBlock().values()); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 300 | } else if (Value.getType() == DIEValue::isLoc) { |
| 301 | addULEB128(Value.getDIELoc().ComputeSize(AP)); |
Duncan P. N. Exon Smith | 88a8fc5 | 2015-05-27 22:44:06 +0000 | [diff] [blame] | 302 | hashBlockData(Value.getDIELoc().values()); |
Eric Christopher | 4f17ee0 | 2014-03-08 00:29:41 +0000 | [diff] [blame] | 303 | } else { |
| 304 | // We could add the block length, but that would take |
| 305 | // a bit of work and not add a lot of uniqueness |
| 306 | // to the hash in some way we could test. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 307 | hashLocList(Value.getDIELocList()); |
Eric Christopher | 420569b | 2014-02-20 02:50:45 +0000 | [diff] [blame] | 308 | } |
| 309 | break; |
Eric Christopher | 6bb07f6 | 2014-03-06 01:32:56 +0000 | [diff] [blame] | 310 | // FIXME: It's uncertain whether or not we should handle this at the moment. |
Eric Christopher | 1930849 | 2014-03-06 00:38:32 +0000 | [diff] [blame] | 311 | case DIEValue::isExpr: |
| 312 | case DIEValue::isLabel: |
| 313 | case DIEValue::isDelta: |
Eric Christopher | 1930849 | 2014-03-06 00:38:32 +0000 | [diff] [blame] | 314 | llvm_unreachable("Add support for additional value types."); |
Eric Christopher | d29614f | 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 | 6cf58c8 | 2013-10-21 22:36:50 +0000 | [diff] [blame] | 320 | void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) { |
David Blaikie | 74fa803 | 2017-05-23 18:27:09 +0000 | [diff] [blame] | 321 | #define HANDLE_DIE_HASH_ATTR(NAME) \ |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 322 | { \ |
David Blaikie | 74fa803 | 2017-05-23 18:27:09 +0000 | [diff] [blame] | 323 | if (Attrs.NAME) \ |
| 324 | hashAttribute(Attrs.NAME, Tag); \ |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 325 | } |
David Blaikie | 74fa803 | 2017-05-23 18:27:09 +0000 | [diff] [blame] | 326 | #include "DIEHashAttributes.def" |
Eric Christopher | e020fa7 | 2013-09-03 20:00:20 +0000 | [diff] [blame] | 327 | // FIXME: Add the extended attributes. |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Add all of the attributes for \param Die to the hash. |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 331 | void DIEHash::addAttributes(const DIE &Die) { |
David Blaikie | 94ded5f | 2013-10-16 00:47:21 +0000 | [diff] [blame] | 332 | DIEAttrs Attrs = {}; |
David Blaikie | d0d6fcc | 2013-08-14 22:23:05 +0000 | [diff] [blame] | 333 | collectAttributes(Die, Attrs); |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 334 | hashAttributes(Attrs, Die.getTag()); |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 335 | } |
| 336 | |
David Blaikie | 65cc969 | 2013-10-25 18:38:43 +0000 | [diff] [blame] | 337 | void DIEHash::hashNestedType(const DIE &Die, StringRef Name) { |
| 338 | // 7.27 Step 7 |
| 339 | // ... append the letter 'S', |
| 340 | addULEB128('S'); |
| 341 | |
| 342 | // the tag of C, |
| 343 | addULEB128(Die.getTag()); |
| 344 | |
| 345 | // and the name. |
| 346 | addString(Name); |
| 347 | } |
| 348 | |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 349 | // Compute the hash of a DIE. This is based on the type signature computation |
| 350 | // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a |
| 351 | // flattened description of the DIE. |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 352 | void DIEHash::computeHash(const DIE &Die) { |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 353 | // Append the letter 'D', followed by the DWARF tag of the DIE. |
| 354 | addULEB128('D'); |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 355 | addULEB128(Die.getTag()); |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 356 | |
| 357 | // Add each of the attributes of the DIE. |
| 358 | addAttributes(Die); |
| 359 | |
| 360 | // Then hash each of the children of the DIE. |
Duncan P. N. Exon Smith | 8d3197f | 2015-05-28 19:56:34 +0000 | [diff] [blame] | 361 | for (auto &C : Die.children()) { |
David Blaikie | 65cc969 | 2013-10-25 18:38:43 +0000 | [diff] [blame] | 362 | // 7.27 Step 7 |
| 363 | // If C is a nested type entry or a member function entry, ... |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 364 | if (isType(C.getTag()) || C.getTag() == dwarf::DW_TAG_subprogram) { |
| 365 | StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name); |
David Blaikie | 65cc969 | 2013-10-25 18:38:43 +0000 | [diff] [blame] | 366 | // ... and has a DW_AT_name attribute |
| 367 | if (!Name.empty()) { |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 368 | hashNestedType(C, Name); |
David Blaikie | 65cc969 | 2013-10-25 18:38:43 +0000 | [diff] [blame] | 369 | continue; |
| 370 | } |
| 371 | } |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 372 | computeHash(C); |
David Blaikie | 65cc969 | 2013-10-25 18:38:43 +0000 | [diff] [blame] | 373 | } |
David Blaikie | 71a0ad6 | 2013-10-16 20:29:06 +0000 | [diff] [blame] | 374 | |
| 375 | // 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] | 376 | Hash.update(makeArrayRef((uint8_t)'\0')); |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 379 | /// This is based on the type signature computation given in section 7.27 of the |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 380 | /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE |
| 381 | /// 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] | 382 | // TODO: Initialize the type chain at 0 instead of 1 for CU signatures. |
Mehdi Amini | 96ab48f | 2017-05-29 06:32:34 +0000 | [diff] [blame^] | 383 | uint64_t DIEHash::computeCUSignature(StringRef DWOName, const DIE &Die) { |
David Blaikie | 980d499 | 2013-10-21 18:59:40 +0000 | [diff] [blame] | 384 | Numbering.clear(); |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 385 | Numbering[&Die] = 1; |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 386 | |
Mehdi Amini | 96ab48f | 2017-05-29 06:32:34 +0000 | [diff] [blame^] | 387 | if (!DWOName.empty()) |
| 388 | Hash.update(DWOName); |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 389 | // Hash the DIE. |
| 390 | computeHash(Die); |
| 391 | |
| 392 | // Now return the result. |
| 393 | MD5::MD5Result Result; |
| 394 | Hash.final(Result); |
| 395 | |
| 396 | // ... take the least significant 8 bytes and return those. Our MD5 |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 397 | // implementation always returns its results in little endian, so we actually |
| 398 | // need the "high" word. |
| 399 | return Result.high(); |
Eric Christopher | d29614f | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 400 | } |
Eric Christopher | 25b7adc | 2013-09-03 21:57:57 +0000 | [diff] [blame] | 401 | |
| 402 | /// This is based on the type signature computation given in section 7.27 of the |
| 403 | /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE |
| 404 | /// with the inclusion of additional forms not specifically called out in the |
| 405 | /// standard. |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 406 | uint64_t DIEHash::computeTypeSignature(const DIE &Die) { |
David Blaikie | 980d499 | 2013-10-21 18:59:40 +0000 | [diff] [blame] | 407 | Numbering.clear(); |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 408 | Numbering[&Die] = 1; |
Eric Christopher | 25b7adc | 2013-09-03 21:57:57 +0000 | [diff] [blame] | 409 | |
David Blaikie | 2aee7be | 2013-10-24 18:29:03 +0000 | [diff] [blame] | 410 | if (const DIE *Parent = Die.getParent()) |
| 411 | addParentContext(*Parent); |
David Blaikie | 8a142aa | 2013-10-17 00:10:34 +0000 | [diff] [blame] | 412 | |
Eric Christopher | 25b7adc | 2013-09-03 21:57:57 +0000 | [diff] [blame] | 413 | // Hash the DIE. |
| 414 | computeHash(Die); |
| 415 | |
| 416 | // Now return the result. |
| 417 | MD5::MD5Result Result; |
| 418 | Hash.final(Result); |
| 419 | |
| 420 | // ... take the least significant 8 bytes and return those. Our MD5 |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 421 | // implementation always returns its results in little endian, so we actually |
| 422 | // need the "high" word. |
| 423 | return Result.high(); |
Eric Christopher | 25b7adc | 2013-09-03 21:57:57 +0000 | [diff] [blame] | 424 | } |