Initial dsymutil tool commit.
[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
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"
16 #include <algorithm>
17
18 namespace llvm {
19 namespace dsymutil {
20
21 using namespace llvm::object;
22
23 DebugMapObject::DebugMapObject(StringRef ObjectFilename)
24     : Filename(ObjectFilename) {}
25
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;
31 }
32
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()));
42   std::sort(
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,
48                  Sym.first.data());
49   }
50   OS << '\n';
51 }
52
53 #ifndef NDEBUG
54 void DebugMapObject::dump() const { print(errs()); }
55 #endif
56
57 DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
58   Objects.emplace_back(new DebugMapObject(ObjectFilePath));
59   return *Objects.back();
60 }
61
62 const DebugMapObject::SymbolMapping *
63 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
64   StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
65   if (Sym == Symbols.end())
66     return nullptr;
67   return &Sym->getValue();
68 }
69
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())
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 }