+// Type Signature computation code.
+typedef ArrayRef<uint8_t> HashValue;
+
+/// \brief Grabs the string in whichever attribute is passed in and returns
+/// a reference to it.
+static StringRef getDIEStringAttr(DIE *Die, unsigned Attr) {
+ const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
+ const DIEAbbrev &Abbrevs = Die->getAbbrev();
+
+ // Iterate through all the attributes until we find the one we're
+ // looking for, if we can't find it return an empty string.
+ for (size_t i = 0; i < Values.size(); ++i) {
+ if (Abbrevs.getData()[i].getAttribute() == Attr) {
+ DIEValue *V = Values[i];
+ assert(isa<DIEString>(V) && "String requested. Not a string.");
+ DIEString *S = cast<DIEString>(V);
+ return S->getString();
+ }
+ }
+ return StringRef("");
+}
+
+/// \brief Adds the string in \p Str to the hash in \p Hash. This also hashes
+/// a trailing NULL with the string.
+static void addStringToHash(MD5 &Hash, StringRef Str) {
+ DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
+ HashValue SVal((const uint8_t *)Str.data(), Str.size());
+ const uint8_t NB = '\0';
+ HashValue NBVal((const uint8_t *)&NB, 1);
+ Hash.update(SVal);
+ Hash.update(NBVal);
+}
+
+// FIXME: These are copied and only slightly modified out of LEB128.h.
+
+/// \brief Adds the unsigned in \p N to the hash in \p Hash. This also encodes
+/// the unsigned as a ULEB128.
+static void addULEB128ToHash(MD5 &Hash, uint64_t Value) {
+ DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
+ do {
+ uint8_t Byte = Value & 0x7f;
+ Value >>= 7;
+ if (Value != 0)
+ Byte |= 0x80; // Mark this byte to show that more bytes will follow.
+ Hash.update(Byte);
+ } while (Value != 0);
+}
+
+/// \brief Including \p Parent adds the context of Parent to \p Hash.
+static void addParentContextToHash(MD5 &Hash, DIE *Parent) {
+ unsigned Tag = Parent->getTag();
+
+ DEBUG(dbgs() << "Adding parent context to hash...\n");
+
+ // For each surrounding type or namespace...
+ if (Tag != dwarf::DW_TAG_namespace && Tag != dwarf::DW_TAG_class_type &&
+ Tag != dwarf::DW_TAG_structure_type)
+ return;
+
+ // ... beginning with the outermost such construct...
+ if (Parent->getParent() != NULL)
+ addParentContextToHash(Hash, Parent->getParent());
+
+ // Append the letter "C" to the sequence.
+ addULEB128ToHash(Hash, 'C');
+
+ // Followed by the DWARF tag of the construct.
+ addULEB128ToHash(Hash, Parent->getTag());
+
+ // Then the name, taken from the DW_AT_name attribute.
+ StringRef Name = getDIEStringAttr(Parent, dwarf::DW_AT_name);
+ if (!Name.empty())
+ addStringToHash(Hash, Name);
+}
+
+/// This is based on the type signature computation given in section 7.27 of the
+/// DWARF4 standard. It is the md5 hash of a flattened description of the DIE.
+static void addDIEODRSignature(MD5 &Hash, CompileUnit *CU, DIE *Die) {
+
+ // Add the contexts to the hash.
+ DIE *Parent = Die->getParent();
+ if (Parent)
+ addParentContextToHash(Hash, Parent);
+
+ // Add the current DIE information.
+
+ // Add the DWARF tag of the DIE.
+ addULEB128ToHash(Hash, Die->getTag());
+
+ // Add the name of the type to the hash.
+ addStringToHash(Hash, getDIEStringAttr(Die, dwarf::DW_AT_name));
+
+ // Now get the result.
+ MD5::MD5Result Result;
+ Hash.final(Result);
+
+ // ... take the least significant 8 bytes and store those as the attribute.
+ // Our MD5 implementation always returns its results in little endian, swap
+ // bytes appropriately.
+ uint64_t Signature = *reinterpret_cast<support::ulittle64_t *>(Result + 8);
+
+ // FIXME: This should be added onto the type unit, not the type, but this
+ // works as an intermediate stage.
+ CU->addUInt(Die, dwarf::DW_AT_GNU_odr_signature, dwarf::DW_FORM_data8,
+ Signature);
+}
+
+/// Return true if the current DIE is contained within an anonymous namespace.
+static bool isContainedInAnonNamespace(DIE *Die) {
+ DIE *Parent = Die->getParent();
+
+ while (Parent) {
+ if (Die->getTag() == dwarf::DW_TAG_namespace &&
+ getDIEStringAttr(Die, dwarf::DW_AT_name) == "")
+ return true;
+ Parent = Parent->getParent();
+ }
+
+ return false;
+}
+