blob: 7898160ae6b9e215a13935a87a951bcf84f7ef82 [file] [log] [blame]
Frederic Riss231f7142014-12-12 17:31:24 +00001//===- 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"
Frederic Riss231f7142014-12-12 17:31:24 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000011#include "llvm/ADT/iterator_range.h"
Frederic Riss231f7142014-12-12 17:31:24 +000012#include "llvm/Support/DataTypes.h"
13#include "llvm/Support/Format.h"
14#include "llvm/Support/raw_ostream.h"
15#include <algorithm>
16
17namespace llvm {
18namespace dsymutil {
19
20using namespace llvm::object;
21
22DebugMapObject::DebugMapObject(StringRef ObjectFilename)
23 : Filename(ObjectFilename) {}
24
25bool 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
32void 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
53void DebugMapObject::dump() const { print(errs()); }
54#endif
55
56DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
57 Objects.emplace_back(new DebugMapObject(ObjectFilePath));
58 return *Objects.back();
59}
60
61const DebugMapObject::SymbolMapping *
62DebugMapObject::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
69void DebugMap::print(raw_ostream &OS) const {
70 OS << "DEBUG MAP: object addr => executable addr\tsymbol name\n";
71 for (const auto &Obj : objects())
72 Obj->print(OS);
73 OS << "END DEBUG MAP\n";
74}
75
76#ifndef NDEBUG
77void DebugMap::dump() const { print(errs()); }
78#endif
79}
80}