blob: 4717085f43227716ba26d60813fe486f3e3a4fe8 [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 Riss4dd3e0c2015-08-05 18:27:44 +0000100ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Riss4d0ba662015-06-05 20:27:04 +0000101DebugMap::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;
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000117 std::vector<std::unique_ptr<DebugMap>> Result;
118 Result.push_back(std::move(Res));
119 return std::move(Result);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000120}
121}
122
123namespace yaml {
124
125// Normalize/Denormalize between YAML and a DebugMapObject.
126struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000127 YamlDMO(IO &io) { Timestamp = 0; }
Frederic Riss4d0ba662015-06-05 20:27:04 +0000128 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
129 dsymutil::DebugMapObject denormalize(IO &IO);
130
131 std::string Filename;
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000132 sys::TimeValue::SecondsType Timestamp;
Frederic Riss4d0ba662015-06-05 20:27:04 +0000133 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
134};
135
136void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
137 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
138 io.mapRequired("sym", s.first);
139 io.mapRequired("objAddr", s.second.ObjectAddress);
140 io.mapRequired("binAddr", s.second.BinaryAddress);
141 io.mapOptional("size", s.second.Size);
142}
143
144void MappingTraits<dsymutil::DebugMapObject>::mapping(
145 IO &io, dsymutil::DebugMapObject &DMO) {
146 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
147 io.mapRequired("filename", Norm->Filename);
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000148 io.mapOptional("timestamp", Norm->Timestamp);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000149 io.mapRequired("symbols", Norm->Entries);
150}
151
152void ScalarTraits<Triple>::output(const Triple &val, void *,
153 llvm::raw_ostream &out) {
154 out << val.str();
155}
156
157StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
158 value = Triple(scalar);
159 return StringRef();
160}
161
162size_t
163SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
164 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
165 return seq.size();
166}
167
168dsymutil::DebugMapObject &
169SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
170 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
171 size_t index) {
172 if (index >= seq.size()) {
173 seq.resize(index + 1);
174 seq[index].reset(new dsymutil::DebugMapObject);
175 }
176 return *seq[index];
177}
178
179void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
180 dsymutil::DebugMap &DM) {
181 io.mapRequired("triple", DM.BinaryTriple);
Frederic Riss2c69d362015-08-26 05:09:59 +0000182 io.mapOptional("binary-path", DM.BinaryPath);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000183 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000184 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
Frederic Risseb85c8f2015-07-24 06:41:11 +0000185 io.mapOptional("objects", DM.Objects);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000186}
187
188void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
189 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
190 if (!DM)
191 DM.reset(new DebugMap());
192 io.mapRequired("triple", DM->BinaryTriple);
Frederic Riss2c69d362015-08-26 05:09:59 +0000193 io.mapOptional("binary-path", DM->BinaryPath);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000194 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000195 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
Frederic Risseb85c8f2015-07-24 06:41:11 +0000196 io.mapOptional("objects", DM->Objects);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000197}
198
199MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
200 IO &io, dsymutil::DebugMapObject &Obj) {
201 Filename = Obj.Filename;
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000202 Timestamp = Obj.getTimestamp().toEpochTime();
Frederic Riss4d0ba662015-06-05 20:27:04 +0000203 Entries.reserve(Obj.Symbols.size());
204 for (auto &Entry : Obj.Symbols)
205 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
206}
207
208dsymutil::DebugMapObject
209MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000210 BinaryHolder BinHolder(/* Verbose =*/false);
211 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
212 SmallString<80> Path(Ctxt.PrependPath);
213 StringMap<uint64_t> SymbolAddresses;
214
Frederic Riss4d0ba662015-06-05 20:27:04 +0000215 sys::path::append(Path, Filename);
Frederic Risseb85c8f2015-07-24 06:41:11 +0000216 auto ErrOrObjectFiles = BinHolder.GetObjectFiles(Path);
217 if (auto EC = ErrOrObjectFiles.getError()) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000218 llvm::errs() << "warning: Unable to open " << Path << " " << EC.message()
219 << '\n';
Frederic Risseb85c8f2015-07-24 06:41:11 +0000220 } else if (auto ErrOrObjectFile = BinHolder.Get(Ctxt.BinaryTriple)) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000221 // Rewrite the object file symbol addresses in the debug map. The
222 // YAML input is mainly used to test llvm-dsymutil without
223 // requiring binaries checked-in. If we generate the object files
224 // during the test, we can't hardcode the symbols addresses, so
225 // look them up here and rewrite them.
226 for (const auto &Sym : ErrOrObjectFile->symbols()) {
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000227 uint64_t Address = Sym.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000228 ErrorOr<StringRef> Name = Sym.getName();
229 if (!Name)
230 continue;
231 SymbolAddresses[*Name] = Address;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000232 }
233 }
234
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000235 sys::TimeValue TV;
236 TV.fromEpochTime(Timestamp);
237 dsymutil::DebugMapObject Res(Path, TV);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000238 for (auto &Entry : Entries) {
239 auto &Mapping = Entry.second;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000240 uint64_t ObjAddress = Mapping.ObjectAddress;
241 auto AddressIt = SymbolAddresses.find(Entry.first);
242 if (AddressIt != SymbolAddresses.end())
243 ObjAddress = AddressIt->getValue();
244 Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000245 }
246 return Res;
247}
Frederic Riss231f7142014-12-12 17:31:24 +0000248}
249}