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. |
| 31 | static StringRef getDIEStringAttr(DIE *Die, uint16_t Attr) { |
| 32 | const SmallVectorImpl<DIEValue *> &Values = Die->getValues(); |
| 33 | const DIEAbbrev &Abbrevs = Die->getAbbrev(); |
| 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 | |
| 71 | /// \brief Including \p Parent adds the context of Parent to the hash.. |
| 72 | void DIEHash::addParentContext(DIE *Parent) { |
| 73 | |
| 74 | DEBUG(dbgs() << "Adding parent context to hash...\n"); |
| 75 | |
| 76 | // [7.27.2] For each surrounding type or namespace beginning with the |
| 77 | // outermost such construct... |
| 78 | SmallVector<DIE *, 1> Parents; |
| 79 | while (Parent->getTag() != dwarf::DW_TAG_compile_unit) { |
| 80 | Parents.push_back(Parent); |
| 81 | Parent = Parent->getParent(); |
| 82 | } |
| 83 | |
| 84 | // Reverse iterate over our list to go from the outermost construct to the |
| 85 | // innermost. |
| 86 | for (SmallVectorImpl<DIE *>::reverse_iterator I = Parents.rbegin(), |
| 87 | E = Parents.rend(); |
| 88 | I != E; ++I) { |
| 89 | DIE *Die = *I; |
| 90 | |
| 91 | // ... Append the letter "C" to the sequence... |
| 92 | addULEB128('C'); |
| 93 | |
| 94 | // ... Followed by the DWARF tag of the construct... |
| 95 | addULEB128(Die->getTag()); |
| 96 | |
| 97 | // ... Then the name, taken from the DW_AT_name attribute. |
| 98 | StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name); |
| 99 | DEBUG(dbgs() << "... adding context: " << Name << "\n"); |
| 100 | if (!Name.empty()) |
| 101 | addString(Name); |
| 102 | } |
| 103 | } |
| 104 | |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 105 | // Collect all of the attributes for a particular DIE in single structure. |
David Blaikie | cbb5c73 | 2013-08-14 22:23:05 +0000 | [diff] [blame] | 106 | void DIEHash::collectAttributes(DIE *Die, DIEAttrs &Attrs) { |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 107 | const SmallVectorImpl<DIEValue *> &Values = Die->getValues(); |
| 108 | const DIEAbbrev &Abbrevs = Die->getAbbrev(); |
| 109 | |
| 110 | #define COLLECT_ATTR(NAME) \ |
David Blaikie | cbb5c73 | 2013-08-14 22:23:05 +0000 | [diff] [blame] | 111 | Attrs.NAME.Val = Values[i]; \ |
| 112 | Attrs.NAME.Desc = &Abbrevs.getData()[i]; |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 113 | |
| 114 | for (size_t i = 0, e = Values.size(); i != e; ++i) { |
| 115 | DEBUG(dbgs() << "Attribute: " |
| 116 | << dwarf::AttributeString(Abbrevs.getData()[i].getAttribute()) |
| 117 | << " added.\n"); |
| 118 | switch (Abbrevs.getData()[i].getAttribute()) { |
| 119 | case dwarf::DW_AT_name: |
| 120 | COLLECT_ATTR(DW_AT_name); |
| 121 | break; |
Eric Christopher | 7ced4fa | 2013-08-28 00:10:38 +0000 | [diff] [blame] | 122 | case dwarf::DW_AT_language: |
| 123 | COLLECT_ATTR(DW_AT_language); |
| 124 | break; |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Hash an individual attribute \param Attr based on the type of attribute and |
| 132 | // the form. |
| 133 | void DIEHash::hashAttribute(AttrEntry Attr) { |
| 134 | const DIEValue *Value = Attr.Val; |
| 135 | const DIEAbbrevData *Desc = Attr.Desc; |
| 136 | |
| 137 | // TODO: Add support for types. |
| 138 | |
| 139 | // Add the letter A to the hash. |
| 140 | addULEB128('A'); |
| 141 | |
| 142 | // Then the attribute code and form. |
| 143 | addULEB128(Desc->getAttribute()); |
| 144 | addULEB128(Desc->getForm()); |
| 145 | |
| 146 | // TODO: Add support for additional forms. |
| 147 | switch (Desc->getForm()) { |
Eric Christopher | c3e9457 | 2013-08-28 00:13:08 +0000 | [diff] [blame] | 148 | // TODO: We'll want to add DW_FORM_string here if we start emitting them again. |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 149 | case dwarf::DW_FORM_strp: |
| 150 | addString(cast<DIEString>(Value)->getString()); |
| 151 | break; |
Eric Christopher | 7ced4fa | 2013-08-28 00:10:38 +0000 | [diff] [blame] | 152 | case dwarf::DW_FORM_data1: |
| 153 | case dwarf::DW_FORM_data2: |
| 154 | case dwarf::DW_FORM_data4: |
| 155 | case dwarf::DW_FORM_data8: |
| 156 | case dwarf::DW_FORM_udata: |
| 157 | addULEB128(cast<DIEInteger>(Value)->getValue()); |
| 158 | break; |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | // Go through the attributes from \param Attrs in the order specified in 7.27.4 |
| 163 | // and hash them. |
David Blaikie | cbb5c73 | 2013-08-14 22:23:05 +0000 | [diff] [blame] | 164 | void DIEHash::hashAttributes(const DIEAttrs &Attrs) { |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 165 | #define ADD_ATTR(ATTR) \ |
| 166 | { \ |
| 167 | if (ATTR.Val != 0) \ |
| 168 | hashAttribute(ATTR); \ |
| 169 | } |
| 170 | |
| 171 | // FIXME: Add the rest. |
| 172 | ADD_ATTR(Attrs.DW_AT_name); |
Eric Christopher | 7ced4fa | 2013-08-28 00:10:38 +0000 | [diff] [blame] | 173 | ADD_ATTR(Attrs.DW_AT_language); |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Add all of the attributes for \param Die to the hash. |
| 177 | void DIEHash::addAttributes(DIE *Die) { |
| 178 | DIEAttrs Attrs; |
| 179 | memset(&Attrs, 0, sizeof(Attrs)); |
David Blaikie | cbb5c73 | 2013-08-14 22:23:05 +0000 | [diff] [blame] | 180 | collectAttributes(Die, Attrs); |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 181 | hashAttributes(Attrs); |
| 182 | } |
| 183 | |
| 184 | // Compute the hash of a DIE. This is based on the type signature computation |
| 185 | // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a |
| 186 | // flattened description of the DIE. |
| 187 | void DIEHash::computeHash(DIE *Die) { |
| 188 | |
| 189 | // Append the letter 'D', followed by the DWARF tag of the DIE. |
| 190 | addULEB128('D'); |
| 191 | addULEB128(Die->getTag()); |
| 192 | |
| 193 | // Add each of the attributes of the DIE. |
| 194 | addAttributes(Die); |
| 195 | |
| 196 | // Then hash each of the children of the DIE. |
| 197 | for (std::vector<DIE *>::const_iterator I = Die->getChildren().begin(), |
| 198 | E = Die->getChildren().end(); |
| 199 | I != E; ++I) |
| 200 | computeHash(*I); |
| 201 | } |
| 202 | |
Eric Christopher | 0d27ca1 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 203 | /// This is based on the type signature computation given in section 7.27 of the |
| 204 | /// 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] | 205 | /// with the exception that we are hashing only the context and the name of the |
| 206 | /// type. |
Eric Christopher | 0d27ca1 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 207 | uint64_t DIEHash::computeDIEODRSignature(DIE *Die) { |
| 208 | |
| 209 | // Add the contexts to the hash. We won't be computing the ODR hash for |
| 210 | // function local types so it's safe to use the generic context hashing |
| 211 | // algorithm here. |
| 212 | // FIXME: If we figure out how to account for linkage in some way we could |
| 213 | // actually do this with a slight modification to the parent hash algorithm. |
| 214 | DIE *Parent = Die->getParent(); |
| 215 | if (Parent) |
| 216 | addParentContext(Parent); |
| 217 | |
| 218 | // Add the current DIE information. |
| 219 | |
| 220 | // Add the DWARF tag of the DIE. |
| 221 | addULEB128(Die->getTag()); |
| 222 | |
| 223 | // Add the name of the type to the hash. |
| 224 | addString(getDIEStringAttr(Die, dwarf::DW_AT_name)); |
| 225 | |
| 226 | // Now get the result. |
| 227 | MD5::MD5Result Result; |
| 228 | Hash.final(Result); |
| 229 | |
| 230 | // ... take the least significant 8 bytes and return those. Our MD5 |
| 231 | // implementation always returns its results in little endian, swap bytes |
| 232 | // appropriately. |
| 233 | return *reinterpret_cast<support::ulittle64_t *>(Result + 8); |
| 234 | } |
Eric Christopher | 0710bfa | 2013-08-13 01:21:55 +0000 | [diff] [blame] | 235 | |
| 236 | /// This is based on the type signature computation given in section 7.27 of the |
| 237 | /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE |
| 238 | /// with the inclusion of the full CU and all top level CU entities. |
| 239 | uint64_t DIEHash::computeCUSignature(DIE *Die) { |
| 240 | |
| 241 | // Hash the DIE. |
| 242 | computeHash(Die); |
| 243 | |
| 244 | // Now return the result. |
| 245 | MD5::MD5Result Result; |
| 246 | Hash.final(Result); |
| 247 | |
| 248 | // ... take the least significant 8 bytes and return those. Our MD5 |
| 249 | // implementation always returns its results in little endian, swap bytes |
| 250 | // appropriately. |
| 251 | return *reinterpret_cast<support::ulittle64_t *>(Result + 8); |
| 252 | } |