blob: 24dedfcb673d4b2dba24ef07c679cacf29ead50c [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 Riss4f5874a2015-06-05 21:12:07 +000010#include "BinaryHolder.h"
Frederic Riss231f7142014-12-12 17:31:24 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000012#include "llvm/ADT/iterator_range.h"
Frederic Riss231f7142014-12-12 17:31:24 +000013#include "llvm/Support/DataTypes.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16#include <algorithm>
17
18namespace llvm {
19namespace dsymutil {
20
21using namespace llvm::object;
22
Frederic Riss9ccfddc2015-07-22 23:24:00 +000023DebugMapObject::DebugMapObject(StringRef ObjectFilename,
24 sys::TimeValue Timestamp)
25 : Filename(ObjectFilename), Timestamp(Timestamp) {}
Frederic Riss231f7142014-12-12 17:31:24 +000026
27bool DebugMapObject::addSymbol(StringRef Name, uint64_t ObjectAddress,
Frederic Riss912d0f12015-03-15 01:29:30 +000028 uint64_t LinkedAddress, uint32_t Size) {
Frederic Riss231f7142014-12-12 17:31:24 +000029 auto InsertResult = Symbols.insert(
Frederic Riss912d0f12015-03-15 01:29:30 +000030 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
Frederic Riss1595c5d2015-02-13 23:18:16 +000031
32 if (InsertResult.second)
33 AddressToMapping[ObjectAddress] = &*InsertResult.first;
Frederic Riss231f7142014-12-12 17:31:24 +000034 return InsertResult.second;
35}
36
37void DebugMapObject::print(raw_ostream &OS) const {
38 OS << getObjectFilename() << ":\n";
39 // Sort the symbols in alphabetical order, like llvm-nm (and to get
40 // deterministic output for testing).
41 typedef std::pair<StringRef, SymbolMapping> Entry;
42 std::vector<Entry> Entries;
43 Entries.reserve(Symbols.getNumItems());
44 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
45 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
46 std::sort(
47 Entries.begin(), Entries.end(),
48 [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
49 for (const auto &Sym : Entries) {
Frederic Riss912d0f12015-03-15 01:29:30 +000050 OS << format("\t%016" PRIx64 " => %016" PRIx64 "+0x%x\t%s\n",
Frederic Riss08462f72015-06-01 21:12:45 +000051 uint64_t(Sym.second.ObjectAddress),
Frederic Rissf37964c2015-06-05 20:27:07 +000052 uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
53 Sym.first.data());
Frederic Riss231f7142014-12-12 17:31:24 +000054 }
55 OS << '\n';
56}
57
58#ifndef NDEBUG
59void DebugMapObject::dump() const { print(errs()); }
60#endif
61
Frederic Riss9ccfddc2015-07-22 23:24:00 +000062DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath,
63 sys::TimeValue Timestamp) {
64 Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp));
Frederic Riss231f7142014-12-12 17:31:24 +000065 return *Objects.back();
66}
67
Frederic Riss1595c5d2015-02-13 23:18:16 +000068const DebugMapObject::DebugMapEntry *
Frederic Riss231f7142014-12-12 17:31:24 +000069DebugMapObject::lookupSymbol(StringRef SymbolName) const {
70 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
71 if (Sym == Symbols.end())
72 return nullptr;
Frederic Riss1595c5d2015-02-13 23:18:16 +000073 return &*Sym;
74}
75
76const DebugMapObject::DebugMapEntry *
77DebugMapObject::lookupObjectAddress(uint64_t Address) const {
78 auto Mapping = AddressToMapping.find(Address);
79 if (Mapping == AddressToMapping.end())
80 return nullptr;
81 return Mapping->getSecond();
Frederic Riss231f7142014-12-12 17:31:24 +000082}
83
84void DebugMap::print(raw_ostream &OS) const {
Frederic Riss08462f72015-06-01 21:12:45 +000085 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
Frederic Rissf37964c2015-06-05 20:27:07 +000086 yout << const_cast<DebugMap &>(*this);
Frederic Riss231f7142014-12-12 17:31:24 +000087}
88
89#ifndef NDEBUG
90void DebugMap::dump() const { print(errs()); }
91#endif
Frederic Riss4d0ba662015-06-05 20:27:04 +000092
Frederic Riss4f5874a2015-06-05 21:12:07 +000093namespace {
94struct YAMLContext {
95 StringRef PrependPath;
Frederic Rissa81e8812015-06-05 21:21:57 +000096 Triple BinaryTriple;
Frederic Riss4f5874a2015-06-05 21:12:07 +000097};
98}
99
Frederic Riss4d0ba662015-06-05 20:27:04 +0000100ErrorOr<std::unique_ptr<DebugMap>>
101DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
102 bool Verbose) {
103 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
104 if (auto Err = ErrOrFile.getError())
105 return Err;
106
Frederic Riss4f5874a2015-06-05 21:12:07 +0000107 YAMLContext Ctxt;
108
109 Ctxt.PrependPath = PrependPath;
110
Frederic Riss4d0ba662015-06-05 20:27:04 +0000111 std::unique_ptr<DebugMap> Res;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000112 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000113 yin >> Res;
114
115 if (auto EC = yin.error())
116 return EC;
117
118 return std::move(Res);
119}
120}
121
122namespace yaml {
123
124// Normalize/Denormalize between YAML and a DebugMapObject.
125struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000126 YamlDMO(IO &io) { Timestamp = 0; }
Frederic Riss4d0ba662015-06-05 20:27:04 +0000127 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
128 dsymutil::DebugMapObject denormalize(IO &IO);
129
130 std::string Filename;
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000131 sys::TimeValue::SecondsType Timestamp;
Frederic Riss4d0ba662015-06-05 20:27:04 +0000132 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
133};
134
135void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
136 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
137 io.mapRequired("sym", s.first);
138 io.mapRequired("objAddr", s.second.ObjectAddress);
139 io.mapRequired("binAddr", s.second.BinaryAddress);
140 io.mapOptional("size", s.second.Size);
141}
142
143void MappingTraits<dsymutil::DebugMapObject>::mapping(
144 IO &io, dsymutil::DebugMapObject &DMO) {
145 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
146 io.mapRequired("filename", Norm->Filename);
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000147 io.mapOptional("timestamp", Norm->Timestamp);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000148 io.mapRequired("symbols", Norm->Entries);
149}
150
151void ScalarTraits<Triple>::output(const Triple &val, void *,
152 llvm::raw_ostream &out) {
153 out << val.str();
154}
155
156StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
157 value = Triple(scalar);
158 return StringRef();
159}
160
161size_t
162SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
163 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
164 return seq.size();
165}
166
167dsymutil::DebugMapObject &
168SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
169 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
170 size_t index) {
171 if (index >= seq.size()) {
172 seq.resize(index + 1);
173 seq[index].reset(new dsymutil::DebugMapObject);
174 }
175 return *seq[index];
176}
177
178void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
179 dsymutil::DebugMap &DM) {
180 io.mapRequired("triple", DM.BinaryTriple);
181 io.mapOptional("objects", DM.Objects);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000182 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000183 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
Frederic Riss4d0ba662015-06-05 20:27:04 +0000184}
185
186void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
187 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
188 if (!DM)
189 DM.reset(new DebugMap());
190 io.mapRequired("triple", DM->BinaryTriple);
191 io.mapOptional("objects", DM->Objects);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000192 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000193 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
Frederic Riss4d0ba662015-06-05 20:27:04 +0000194}
195
196MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
197 IO &io, dsymutil::DebugMapObject &Obj) {
198 Filename = Obj.Filename;
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000199 Timestamp = Obj.getTimestamp().toEpochTime();
Frederic Riss4d0ba662015-06-05 20:27:04 +0000200 Entries.reserve(Obj.Symbols.size());
201 for (auto &Entry : Obj.Symbols)
202 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
203}
204
205dsymutil::DebugMapObject
206MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000207 BinaryHolder BinHolder(/* Verbose =*/false);
208 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
209 SmallString<80> Path(Ctxt.PrependPath);
210 StringMap<uint64_t> SymbolAddresses;
211
Frederic Riss4d0ba662015-06-05 20:27:04 +0000212 sys::path::append(Path, Filename);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000213 auto ErrOrObjectFile = BinHolder.GetObjectFile(Path);
214 if (auto EC = ErrOrObjectFile.getError()) {
215 llvm::errs() << "warning: Unable to open " << Path << " " << EC.message()
216 << '\n';
217 } else {
218 // Rewrite the object file symbol addresses in the debug map. The
219 // YAML input is mainly used to test llvm-dsymutil without
220 // requiring binaries checked-in. If we generate the object files
221 // during the test, we can't hardcode the symbols addresses, so
222 // look them up here and rewrite them.
223 for (const auto &Sym : ErrOrObjectFile->symbols()) {
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000224 uint64_t Address = Sym.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000225 ErrorOr<StringRef> Name = Sym.getName();
226 if (!Name)
227 continue;
228 SymbolAddresses[*Name] = Address;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000229 }
230 }
231
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000232 sys::TimeValue TV;
233 TV.fromEpochTime(Timestamp);
234 dsymutil::DebugMapObject Res(Path, TV);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000235 for (auto &Entry : Entries) {
236 auto &Mapping = Entry.second;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000237 uint64_t ObjAddress = Mapping.ObjectAddress;
238 auto AddressIt = SymbolAddresses.find(Entry.first);
239 if (AddressIt != SymbolAddresses.end())
240 ObjAddress = AddressIt->getValue();
241 Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000242 }
243 return Res;
244}
Frederic Riss231f7142014-12-12 17:31:24 +0000245}
246}