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 //===----------------------------------------------------------------------===//
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/Support/DataTypes.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm::object;
23 DebugMapObject::DebugMapObject(StringRef ObjectFilename)
24 : Filename(ObjectFilename) {}
26 bool DebugMapObject::addSymbol(StringRef Name, uint64_t ObjectAddress,
27 uint64_t LinkedAddress) {
28 auto InsertResult = Symbols.insert(
29 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress)));
30 return InsertResult.second;
33 void DebugMapObject::print(raw_ostream &OS) const {
34 OS << getObjectFilename() << ":\n";
35 // Sort the symbols in alphabetical order, like llvm-nm (and to get
36 // deterministic output for testing).
37 typedef std::pair<StringRef, SymbolMapping> Entry;
38 std::vector<Entry> Entries;
39 Entries.reserve(Symbols.getNumItems());
40 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
41 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
43 Entries.begin(), Entries.end(),
44 [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
45 for (const auto &Sym : Entries) {
46 OS << format("\t%016" PRIx64 " => %016" PRIx64 "\t%s\n",
47 Sym.second.ObjectAddress, Sym.second.BinaryAddress,
54 void DebugMapObject::dump() const { print(errs()); }
57 DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
58 Objects.emplace_back(new DebugMapObject(ObjectFilePath));
59 return *Objects.back();
62 const DebugMapObject::SymbolMapping *
63 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
64 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
65 if (Sym == Symbols.end())
67 return &Sym->getValue();
70 void DebugMap::print(raw_ostream &OS) const {
71 OS << "DEBUG MAP: object 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()); }