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) {
27 auto InsertResult = Symbols.insert(
28 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress)));
29 return InsertResult.second;
32 void DebugMapObject::print(raw_ostream &OS) const {
33 OS << getObjectFilename() << ":\n";
34 // Sort the symbols in alphabetical order, like llvm-nm (and to get
35 // deterministic output for testing).
36 typedef std::pair<StringRef, SymbolMapping> Entry;
37 std::vector<Entry> Entries;
38 Entries.reserve(Symbols.getNumItems());
39 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
40 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
42 Entries.begin(), Entries.end(),
43 [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
44 for (const auto &Sym : Entries) {
45 OS << format("\t%016" PRIx64 " => %016" PRIx64 "\t%s\n",
46 Sym.second.ObjectAddress, Sym.second.BinaryAddress,
53 void DebugMapObject::dump() const { print(errs()); }
56 DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
57 Objects.emplace_back(new DebugMapObject(ObjectFilePath));
58 return *Objects.back();
61 const DebugMapObject::SymbolMapping *
62 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
63 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
64 if (Sym == Symbols.end())
66 return &Sym->getValue();
69 void DebugMap::print(raw_ostream &OS) const {
70 OS << "DEBUG MAP: " << BinaryTriple.getTriple()
71 << "\n\tobject addr => executable addr\tsymbol name\n";
72 for (const auto &Obj : objects())
74 OS << "END DEBUG MAP\n";
78 void DebugMap::dump() const { print(errs()); }