1 //===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm::object;
22 DebugMapObject::DebugMapObject(StringRef ObjectFilename)
23 : Filename(ObjectFilename) {}
25 bool DebugMapObject::addSymbol(StringRef Name, uint64_t ObjectAddress,
26 uint64_t LinkedAddress, uint32_t Size) {
27 auto InsertResult = Symbols.insert(
28 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
30 if (InsertResult.second)
31 AddressToMapping[ObjectAddress] = &*InsertResult.first;
32 return InsertResult.second;
35 void DebugMapObject::print(raw_ostream &OS) const {
36 OS << getObjectFilename() << ":\n";
37 // Sort the symbols in alphabetical order, like llvm-nm (and to get
38 // deterministic output for testing).
39 typedef std::pair<StringRef, SymbolMapping> Entry;
40 std::vector<Entry> Entries;
41 Entries.reserve(Symbols.getNumItems());
42 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
43 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
45 Entries.begin(), Entries.end(),
46 [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
47 for (const auto &Sym : Entries) {
48 OS << format("\t%016" PRIx64 " => %016" PRIx64 "+0x%x\t%s\n",
49 uint64_t(Sym.second.ObjectAddress),
50 uint64_t(Sym.second.BinaryAddress),
51 uint32_t(Sym.second.Size), Sym.first.data());
57 void DebugMapObject::dump() const { print(errs()); }
60 DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
61 Objects.emplace_back(new DebugMapObject(ObjectFilePath));
62 return *Objects.back();
65 const DebugMapObject::DebugMapEntry *
66 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
67 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
68 if (Sym == Symbols.end())
73 const DebugMapObject::DebugMapEntry *
74 DebugMapObject::lookupObjectAddress(uint64_t Address) const {
75 auto Mapping = AddressToMapping.find(Address);
76 if (Mapping == AddressToMapping.end())
78 return Mapping->getSecond();
81 void DebugMap::print(raw_ostream &OS) const {
82 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
83 yout << const_cast<DebugMap&>(*this);
87 void DebugMap::dump() const { print(errs()); }