c0e2393f3beaaf3196d4ec40c8fd8d71d2e5bd95
[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/STLExtras.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
19 using namespace llvm::object;
20
21 DebugMapObject::DebugMapObject(StringRef ObjectFilename)
22   : Filename(ObjectFilename) {}
23
24 bool DebugMapObject::addSymbol(StringRef Name, uint64_t ObjectAddress,
25                                uint64_t LinkedAddress) {
26   auto InsertResult = Symbols.insert(std::make_pair(Name,
27                                                     SymbolMapping{ObjectAddress,
28                                                                   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 StringMapEntry<SymbolMapping> MapEntryTy;
37   std::vector<const MapEntryTy *> Entries;
38   Entries.reserve(Symbols.getNumItems());
39   for (auto SymIt = Symbols.begin(), End = Symbols.end(); SymIt != End; ++SymIt)
40     Entries.push_back(&*SymIt);
41   std::sort(Entries.begin(), Entries.end(),
42             [] (const MapEntryTy *LHS, const MapEntryTy *RHS) {
43               return LHS->getKey() < RHS->getKey();
44             });
45   for (const auto *Entry: Entries) {
46     const auto &Sym = Entry->getValue();
47     OS << format("\t%016" PRIx64 " => %016" PRIx64 "\t%s\n",
48                      Sym.ObjectAddress, Sym.BinaryAddress, Entry->getKeyData());
49   }
50   OS << '\n';
51 }
52
53 #ifndef NDEBUG
54 void DebugMapObject::dump() const {
55   print(errs());
56 }
57 #endif
58
59 DebugMapObject& DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
60   Objects.emplace_back(new DebugMapObject(ObjectFilePath));
61   return *Objects.back();
62 }
63
64 const DebugMapObject::SymbolMapping *
65 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
66   StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
67   if (Sym == Symbols.end())
68     return nullptr;
69   return &Sym->getValue();
70 }
71
72 void DebugMap::print(raw_ostream& OS) const {
73   OS << "DEBUG MAP:   object addr =>  executable addr\tsymbol name\n";
74   for (const auto &Obj: objects())
75     Obj->print(OS);
76   OS << "END DEBUG MAP\n";
77 }
78
79 #ifndef NDEBUG
80 void DebugMap::dump() const {
81   print(errs());
82 }
83 #endif
84 }