DIEHash.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains support for DWARF4 hashing of DIEs.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "DIEHash.h"
  13. #include "ByteStreamer.h"
  14. #include "DwarfDebug.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/BinaryFormat/Dwarf.h"
  18. #include "llvm/CodeGen/AsmPrinter.h"
  19. #include "llvm/CodeGen/DIE.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/Endian.h"
  22. #include "llvm/Support/MD5.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "dwarfdebug"
  26. /// Grabs the string in whichever attribute is passed in and returns
  27. /// a reference to it.
  28. static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
  29. // Iterate through all the attributes until we find the one we're
  30. // looking for, if we can't find it return an empty string.
  31. for (const auto &V : Die.values())
  32. if (V.getAttribute() == Attr)
  33. return V.getDIEString().getString();
  34. return StringRef("");
  35. }
  36. /// Adds the string in \p Str to the hash. This also hashes
  37. /// a trailing NULL with the string.
  38. void DIEHash::addString(StringRef Str) {
  39. LLVM_DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
  40. Hash.update(Str);
  41. Hash.update(makeArrayRef((uint8_t)'\0'));
  42. }
  43. // FIXME: The LEB128 routines are copied and only slightly modified out of
  44. // LEB128.h.
  45. /// Adds the unsigned in \p Value to the hash encoded as a ULEB128.
  46. void DIEHash::addULEB128(uint64_t Value) {
  47. LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
  48. do {
  49. uint8_t Byte = Value & 0x7f;
  50. Value >>= 7;
  51. if (Value != 0)
  52. Byte |= 0x80; // Mark this byte to show that more bytes will follow.
  53. Hash.update(Byte);
  54. } while (Value != 0);
  55. }
  56. void DIEHash::addSLEB128(int64_t Value) {
  57. LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
  58. bool More;
  59. do {
  60. uint8_t Byte = Value & 0x7f;
  61. Value >>= 7;
  62. More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
  63. ((Value == -1) && ((Byte & 0x40) != 0))));
  64. if (More)
  65. Byte |= 0x80; // Mark this byte to show that more bytes will follow.
  66. Hash.update(Byte);
  67. } while (More);
  68. }
  69. /// Including \p Parent adds the context of Parent to the hash..
  70. void DIEHash::addParentContext(const DIE &Parent) {
  71. LLVM_DEBUG(dbgs() << "Adding parent context to hash...\n");
  72. // [7.27.2] For each surrounding type or namespace beginning with the
  73. // outermost such construct...
  74. SmallVector<const DIE *, 1> Parents;
  75. const DIE *Cur = &Parent;
  76. while (Cur->getParent()) {
  77. Parents.push_back(Cur);
  78. Cur = Cur->getParent();
  79. }
  80. assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
  81. Cur->getTag() == dwarf::DW_TAG_type_unit);
  82. // Reverse iterate over our list to go from the outermost construct to the
  83. // innermost.
  84. for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
  85. E = Parents.rend();
  86. I != E; ++I) {
  87. const DIE &Die = **I;
  88. // ... Append the letter "C" to the sequence...
  89. addULEB128('C');
  90. // ... Followed by the DWARF tag of the construct...
  91. addULEB128(Die.getTag());
  92. // ... Then the name, taken from the DW_AT_name attribute.
  93. StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
  94. LLVM_DEBUG(dbgs() << "... adding context: " << Name << "\n");
  95. if (!Name.empty())
  96. addString(Name);
  97. }
  98. }
  99. // Collect all of the attributes for a particular DIE in single structure.
  100. void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
  101. for (const auto &V : Die.values()) {
  102. LLVM_DEBUG(dbgs() << "Attribute: "
  103. << dwarf::AttributeString(V.getAttribute())
  104. << " added.\n");
  105. switch (V.getAttribute()) {
  106. #define HANDLE_DIE_HASH_ATTR(NAME) \
  107. case dwarf::NAME: \
  108. Attrs.NAME = V; \
  109. break;
  110. #include "DIEHashAttributes.def"
  111. default:
  112. break;
  113. }
  114. }
  115. }
  116. void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
  117. const DIE &Entry, StringRef Name) {
  118. // append the letter 'N'
  119. addULEB128('N');
  120. // the DWARF attribute code (DW_AT_type or DW_AT_friend),
  121. addULEB128(Attribute);
  122. // the context of the tag,
  123. if (const DIE *Parent = Entry.getParent())
  124. addParentContext(*Parent);
  125. // the letter 'E',
  126. addULEB128('E');
  127. // and the name of the type.
  128. addString(Name);
  129. // Currently DW_TAG_friends are not used by Clang, but if they do become so,
  130. // here's the relevant spec text to implement:
  131. //
  132. // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
  133. // the context is omitted and the name to be used is the ABI-specific name
  134. // of the subprogram (e.g., the mangled linker name).
  135. }
  136. void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
  137. unsigned DieNumber) {
  138. // a) If T is in the list of [previously hashed types], use the letter
  139. // 'R' as the marker
  140. addULEB128('R');
  141. addULEB128(Attribute);
  142. // and use the unsigned LEB128 encoding of [the index of T in the
  143. // list] as the attribute value;
  144. addULEB128(DieNumber);
  145. }
  146. void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
  147. const DIE &Entry) {
  148. assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
  149. "tags. Add support here when there's "
  150. "a use case");
  151. // Step 5
  152. // If the tag in Step 3 is one of [the below tags]
  153. if ((Tag == dwarf::DW_TAG_pointer_type ||
  154. Tag == dwarf::DW_TAG_reference_type ||
  155. Tag == dwarf::DW_TAG_rvalue_reference_type ||
  156. Tag == dwarf::DW_TAG_ptr_to_member_type) &&
  157. // and the referenced type (via the [below attributes])
  158. // FIXME: This seems overly restrictive, and causes hash mismatches
  159. // there's a decl/def difference in the containing type of a
  160. // ptr_to_member_type, but it's what DWARF says, for some reason.
  161. Attribute == dwarf::DW_AT_type) {
  162. // ... has a DW_AT_name attribute,
  163. StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
  164. if (!Name.empty()) {
  165. hashShallowTypeReference(Attribute, Entry, Name);
  166. return;
  167. }
  168. }
  169. unsigned &DieNumber = Numbering[&Entry];
  170. if (DieNumber) {
  171. hashRepeatedTypeReference(Attribute, DieNumber);
  172. return;
  173. }
  174. // otherwise, b) use the letter 'T' as the marker, ...
  175. addULEB128('T');
  176. addULEB128(Attribute);
  177. // ... process the type T recursively by performing Steps 2 through 7, and
  178. // use the result as the attribute value.
  179. DieNumber = Numbering.size();
  180. computeHash(Entry);
  181. }
  182. // Hash all of the values in a block like set of values. This assumes that
  183. // all of the data is going to be added as integers.
  184. void DIEHash::hashBlockData(const DIE::const_value_range &Values) {
  185. for (const auto &V : Values)
  186. Hash.update((uint64_t)V.getDIEInteger().getValue());
  187. }
  188. // Hash the contents of a loclistptr class.
  189. void DIEHash::hashLocList(const DIELocList &LocList) {
  190. HashingByteStreamer Streamer(*this);
  191. DwarfDebug &DD = *AP->getDwarfDebug();
  192. const DebugLocStream &Locs = DD.getDebugLocs();
  193. for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
  194. DD.emitDebugLocEntry(Streamer, Entry, nullptr);
  195. }
  196. // Hash an individual attribute \param Attr based on the type of attribute and
  197. // the form.
  198. void DIEHash::hashAttribute(const DIEValue &Value, dwarf::Tag Tag) {
  199. dwarf::Attribute Attribute = Value.getAttribute();
  200. // Other attribute values use the letter 'A' as the marker, and the value
  201. // consists of the form code (encoded as an unsigned LEB128 value) followed by
  202. // the encoding of the value according to the form code. To ensure
  203. // reproducibility of the signature, the set of forms used in the signature
  204. // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
  205. // DW_FORM_string, and DW_FORM_block.
  206. switch (Value.getType()) {
  207. case DIEValue::isNone:
  208. llvm_unreachable("Expected valid DIEValue");
  209. // 7.27 Step 3
  210. // ... An attribute that refers to another type entry T is processed as
  211. // follows:
  212. case DIEValue::isEntry:
  213. hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
  214. break;
  215. case DIEValue::isInteger: {
  216. addULEB128('A');
  217. addULEB128(Attribute);
  218. switch (Value.getForm()) {
  219. case dwarf::DW_FORM_data1:
  220. case dwarf::DW_FORM_data2:
  221. case dwarf::DW_FORM_data4:
  222. case dwarf::DW_FORM_data8:
  223. case dwarf::DW_FORM_udata:
  224. case dwarf::DW_FORM_sdata:
  225. addULEB128(dwarf::DW_FORM_sdata);
  226. addSLEB128((int64_t)Value.getDIEInteger().getValue());
  227. break;
  228. // DW_FORM_flag_present is just flag with a value of one. We still give it a
  229. // value so just use the value.
  230. case dwarf::DW_FORM_flag_present:
  231. case dwarf::DW_FORM_flag:
  232. addULEB128(dwarf::DW_FORM_flag);
  233. addULEB128((int64_t)Value.getDIEInteger().getValue());
  234. break;
  235. default:
  236. llvm_unreachable("Unknown integer form!");
  237. }
  238. break;
  239. }
  240. case DIEValue::isString:
  241. addULEB128('A');
  242. addULEB128(Attribute);
  243. addULEB128(dwarf::DW_FORM_string);
  244. addString(Value.getDIEString().getString());
  245. break;
  246. case DIEValue::isInlineString:
  247. addULEB128('A');
  248. addULEB128(Attribute);
  249. addULEB128(dwarf::DW_FORM_string);
  250. addString(Value.getDIEInlineString().getString());
  251. break;
  252. case DIEValue::isBlock:
  253. case DIEValue::isLoc:
  254. case DIEValue::isLocList:
  255. addULEB128('A');
  256. addULEB128(Attribute);
  257. addULEB128(dwarf::DW_FORM_block);
  258. if (Value.getType() == DIEValue::isBlock) {
  259. addULEB128(Value.getDIEBlock().ComputeSize(AP));
  260. hashBlockData(Value.getDIEBlock().values());
  261. } else if (Value.getType() == DIEValue::isLoc) {
  262. addULEB128(Value.getDIELoc().ComputeSize(AP));
  263. hashBlockData(Value.getDIELoc().values());
  264. } else {
  265. // We could add the block length, but that would take
  266. // a bit of work and not add a lot of uniqueness
  267. // to the hash in some way we could test.
  268. hashLocList(Value.getDIELocList());
  269. }
  270. break;
  271. // FIXME: It's uncertain whether or not we should handle this at the moment.
  272. case DIEValue::isExpr:
  273. case DIEValue::isLabel:
  274. case DIEValue::isBaseTypeRef:
  275. case DIEValue::isDelta:
  276. llvm_unreachable("Add support for additional value types.");
  277. }
  278. }
  279. // Go through the attributes from \param Attrs in the order specified in 7.27.4
  280. // and hash them.
  281. void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
  282. #define HANDLE_DIE_HASH_ATTR(NAME) \
  283. { \
  284. if (Attrs.NAME) \
  285. hashAttribute(Attrs.NAME, Tag); \
  286. }
  287. #include "DIEHashAttributes.def"
  288. // FIXME: Add the extended attributes.
  289. }
  290. // Add all of the attributes for \param Die to the hash.
  291. void DIEHash::addAttributes(const DIE &Die) {
  292. DIEAttrs Attrs = {};
  293. collectAttributes(Die, Attrs);
  294. hashAttributes(Attrs, Die.getTag());
  295. }
  296. void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
  297. // 7.27 Step 7
  298. // ... append the letter 'S',
  299. addULEB128('S');
  300. // the tag of C,
  301. addULEB128(Die.getTag());
  302. // and the name.
  303. addString(Name);
  304. }
  305. // Compute the hash of a DIE. This is based on the type signature computation
  306. // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
  307. // flattened description of the DIE.
  308. void DIEHash::computeHash(const DIE &Die) {
  309. // Append the letter 'D', followed by the DWARF tag of the DIE.
  310. addULEB128('D');
  311. addULEB128(Die.getTag());
  312. // Add each of the attributes of the DIE.
  313. addAttributes(Die);
  314. // Then hash each of the children of the DIE.
  315. for (auto &C : Die.children()) {
  316. // 7.27 Step 7
  317. // If C is a nested type entry or a member function entry, ...
  318. if (isType(C.getTag()) || C.getTag() == dwarf::DW_TAG_subprogram) {
  319. StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name);
  320. // ... and has a DW_AT_name attribute
  321. if (!Name.empty()) {
  322. hashNestedType(C, Name);
  323. continue;
  324. }
  325. }
  326. computeHash(C);
  327. }
  328. // Following the last (or if there are no children), append a zero byte.
  329. Hash.update(makeArrayRef((uint8_t)'\0'));
  330. }
  331. /// This is based on the type signature computation given in section 7.27 of the
  332. /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
  333. /// with the inclusion of the full CU and all top level CU entities.
  334. // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
  335. uint64_t DIEHash::computeCUSignature(StringRef DWOName, const DIE &Die) {
  336. Numbering.clear();
  337. Numbering[&Die] = 1;
  338. if (!DWOName.empty())
  339. Hash.update(DWOName);
  340. // Hash the DIE.
  341. computeHash(Die);
  342. // Now return the result.
  343. MD5::MD5Result Result;
  344. Hash.final(Result);
  345. // ... take the least significant 8 bytes and return those. Our MD5
  346. // implementation always returns its results in little endian, so we actually
  347. // need the "high" word.
  348. return Result.high();
  349. }
  350. /// This is based on the type signature computation given in section 7.27 of the
  351. /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
  352. /// with the inclusion of additional forms not specifically called out in the
  353. /// standard.
  354. uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
  355. Numbering.clear();
  356. Numbering[&Die] = 1;
  357. if (const DIE *Parent = Die.getParent())
  358. addParentContext(*Parent);
  359. // Hash the DIE.
  360. computeHash(Die);
  361. // Now return the result.
  362. MD5::MD5Result Result;
  363. Hash.final(Result);
  364. // ... take the least significant 8 bytes and return those. Our MD5
  365. // implementation always returns its results in little endian, so we actually
  366. // need the "high" word.
  367. return Result.high();
  368. }