DIEHash.cpp 15 KB

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