ca7ae80ee8f651654427e909c40ed3137c423732
[oota-llvm.git] / tools / dsymutil / DebugMap.cpp
1 //===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "DebugMap.h"
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"
15 #include <algorithm>
16
17 namespace llvm {
18 namespace dsymutil {
19
20 using namespace llvm::object;
21
22 DebugMapObject::DebugMapObject(StringRef ObjectFilename)
23     : Filename(ObjectFilename) {}
24
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;
30 }
31
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()));
41   std::sort(
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,
47                  Sym.first.data());
48   }
49   OS << '\n';
50 }
51
52 #ifndef NDEBUG
53 void DebugMapObject::dump() const { print(errs()); }
54 #endif
55
56 DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
57   Objects.emplace_back(new DebugMapObject(ObjectFilePath));
58   return *Objects.back();
59 }
60
61 const DebugMapObject::SymbolMapping *
62 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
63   StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
64   if (Sym == Symbols.end())
65     return nullptr;
66   return &Sym->getValue();
67 }
68
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())
73     Obj->print(OS);
74   OS << "END DEBUG MAP\n";
75 }
76
77 #ifndef NDEBUG
78 void DebugMap::dump() const { print(errs()); }
79 #endif
80 }
81 }