blob: 636d65836c6dff324afc3ce58870cc609e11001d [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,
Pavel Labath62d72042016-11-09 11:43:52 +000024 sys::TimePoint<std::chrono::seconds> Timestamp)
Frederic Riss9ccfddc2015-07-22 23:24:00 +000025 : Filename(ObjectFilename), Timestamp(Timestamp) {}
Frederic Riss231f7142014-12-12 17:31:24 +000026
Frederic Rissd8c33dc2016-01-31 04:29:22 +000027bool DebugMapObject::addSymbol(StringRef Name, Optional<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
Frederic Rissd8c33dc2016-01-31 04:29:22 +000032 if (ObjectAddress && 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 Rissd8c33dc2016-01-31 04:29:22 +000050 if (Sym.second.ObjectAddress)
51 OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress));
52 else
53 OS << "\t????????????????";
54 OS << format(" => %016" PRIx64 "+0x%x\t%s\n",
Frederic Rissf37964c2015-06-05 20:27:07 +000055 uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
56 Sym.first.data());
Frederic Riss231f7142014-12-12 17:31:24 +000057 }
58 OS << '\n';
59}
60
61#ifndef NDEBUG
62void DebugMapObject::dump() const { print(errs()); }
63#endif
64
Pavel Labath62d72042016-11-09 11:43:52 +000065DebugMapObject &
66DebugMap::addDebugMapObject(StringRef ObjectFilePath,
67 sys::TimePoint<std::chrono::seconds> Timestamp) {
Frederic Riss9ccfddc2015-07-22 23:24:00 +000068 Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp));
Frederic Riss231f7142014-12-12 17:31:24 +000069 return *Objects.back();
70}
71
Frederic Riss1595c5d2015-02-13 23:18:16 +000072const DebugMapObject::DebugMapEntry *
Frederic Riss231f7142014-12-12 17:31:24 +000073DebugMapObject::lookupSymbol(StringRef SymbolName) const {
74 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
75 if (Sym == Symbols.end())
76 return nullptr;
Frederic Riss1595c5d2015-02-13 23:18:16 +000077 return &*Sym;
78}
79
80const DebugMapObject::DebugMapEntry *
81DebugMapObject::lookupObjectAddress(uint64_t Address) const {
82 auto Mapping = AddressToMapping.find(Address);
83 if (Mapping == AddressToMapping.end())
84 return nullptr;
85 return Mapping->getSecond();
Frederic Riss231f7142014-12-12 17:31:24 +000086}
87
88void DebugMap::print(raw_ostream &OS) const {
Frederic Riss08462f72015-06-01 21:12:45 +000089 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
Frederic Rissf37964c2015-06-05 20:27:07 +000090 yout << const_cast<DebugMap &>(*this);
Frederic Riss231f7142014-12-12 17:31:24 +000091}
92
93#ifndef NDEBUG
94void DebugMap::dump() const { print(errs()); }
95#endif
Frederic Riss4d0ba662015-06-05 20:27:04 +000096
Frederic Riss4f5874a2015-06-05 21:12:07 +000097namespace {
98struct YAMLContext {
99 StringRef PrependPath;
Frederic Rissa81e8812015-06-05 21:21:57 +0000100 Triple BinaryTriple;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000101};
102}
103
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000104ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Riss4d0ba662015-06-05 20:27:04 +0000105DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
106 bool Verbose) {
107 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
108 if (auto Err = ErrOrFile.getError())
109 return Err;
110
Frederic Riss4f5874a2015-06-05 21:12:07 +0000111 YAMLContext Ctxt;
112
113 Ctxt.PrependPath = PrependPath;
114
Frederic Riss4d0ba662015-06-05 20:27:04 +0000115 std::unique_ptr<DebugMap> Res;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000116 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000117 yin >> Res;
118
119 if (auto EC = yin.error())
120 return EC;
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000121 std::vector<std::unique_ptr<DebugMap>> Result;
122 Result.push_back(std::move(Res));
123 return std::move(Result);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000124}
125}
126
127namespace yaml {
128
129// Normalize/Denormalize between YAML and a DebugMapObject.
130struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000131 YamlDMO(IO &io) { Timestamp = 0; }
Frederic Riss4d0ba662015-06-05 20:27:04 +0000132 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
133 dsymutil::DebugMapObject denormalize(IO &IO);
134
135 std::string Filename;
Pavel Labath62d72042016-11-09 11:43:52 +0000136 int64_t Timestamp;
Frederic Riss4d0ba662015-06-05 20:27:04 +0000137 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
138};
139
140void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
141 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
142 io.mapRequired("sym", s.first);
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000143 io.mapOptional("objAddr", s.second.ObjectAddress);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000144 io.mapRequired("binAddr", s.second.BinaryAddress);
145 io.mapOptional("size", s.second.Size);
146}
147
148void MappingTraits<dsymutil::DebugMapObject>::mapping(
149 IO &io, dsymutil::DebugMapObject &DMO) {
150 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
151 io.mapRequired("filename", Norm->Filename);
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000152 io.mapOptional("timestamp", Norm->Timestamp);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000153 io.mapRequired("symbols", Norm->Entries);
154}
155
156void ScalarTraits<Triple>::output(const Triple &val, void *,
157 llvm::raw_ostream &out) {
158 out << val.str();
159}
160
161StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
162 value = Triple(scalar);
163 return StringRef();
164}
165
166size_t
167SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
168 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
169 return seq.size();
170}
171
172dsymutil::DebugMapObject &
173SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
174 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
175 size_t index) {
176 if (index >= seq.size()) {
177 seq.resize(index + 1);
178 seq[index].reset(new dsymutil::DebugMapObject);
179 }
180 return *seq[index];
181}
182
183void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
184 dsymutil::DebugMap &DM) {
185 io.mapRequired("triple", DM.BinaryTriple);
Frederic Riss2c69d362015-08-26 05:09:59 +0000186 io.mapOptional("binary-path", DM.BinaryPath);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000187 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000188 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
Frederic Risseb85c8f2015-07-24 06:41:11 +0000189 io.mapOptional("objects", DM.Objects);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000190}
191
192void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
193 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
194 if (!DM)
195 DM.reset(new DebugMap());
196 io.mapRequired("triple", DM->BinaryTriple);
Frederic Riss2c69d362015-08-26 05:09:59 +0000197 io.mapOptional("binary-path", DM->BinaryPath);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000198 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000199 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
Frederic Risseb85c8f2015-07-24 06:41:11 +0000200 io.mapOptional("objects", DM->Objects);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000201}
202
203MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
204 IO &io, dsymutil::DebugMapObject &Obj) {
205 Filename = Obj.Filename;
Pavel Labath62d72042016-11-09 11:43:52 +0000206 Timestamp = sys::toTimeT(Obj.getTimestamp());
Frederic Riss4d0ba662015-06-05 20:27:04 +0000207 Entries.reserve(Obj.Symbols.size());
208 for (auto &Entry : Obj.Symbols)
209 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
210}
211
212dsymutil::DebugMapObject
213MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000214 BinaryHolder BinHolder(/* Verbose =*/false);
215 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
216 SmallString<80> Path(Ctxt.PrependPath);
217 StringMap<uint64_t> SymbolAddresses;
218
Frederic Riss4d0ba662015-06-05 20:27:04 +0000219 sys::path::append(Path, Filename);
Frederic Risseb85c8f2015-07-24 06:41:11 +0000220 auto ErrOrObjectFiles = BinHolder.GetObjectFiles(Path);
221 if (auto EC = ErrOrObjectFiles.getError()) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000222 llvm::errs() << "warning: Unable to open " << Path << " " << EC.message()
223 << '\n';
Frederic Risseb85c8f2015-07-24 06:41:11 +0000224 } else if (auto ErrOrObjectFile = BinHolder.Get(Ctxt.BinaryTriple)) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000225 // Rewrite the object file symbol addresses in the debug map. The
226 // YAML input is mainly used to test llvm-dsymutil without
227 // requiring binaries checked-in. If we generate the object files
228 // during the test, we can't hardcode the symbols addresses, so
229 // look them up here and rewrite them.
230 for (const auto &Sym : ErrOrObjectFile->symbols()) {
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000231 uint64_t Address = Sym.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000232 Expected<StringRef> Name = Sym.getName();
Frederic Riss6c8521a2016-01-31 04:29:34 +0000233 if (!Name ||
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000234 (Sym.getFlags() & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) {
235 // TODO: Actually report errors helpfully.
236 if (!Name)
237 consumeError(Name.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000238 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000239 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000240 SymbolAddresses[*Name] = Address;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000241 }
242 }
243
Pavel Labath62d72042016-11-09 11:43:52 +0000244 dsymutil::DebugMapObject Res(Path, sys::toTimePoint(Timestamp));
Frederic Riss4d0ba662015-06-05 20:27:04 +0000245 for (auto &Entry : Entries) {
246 auto &Mapping = Entry.second;
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000247 Optional<uint64_t> ObjAddress;
248 if (Mapping.ObjectAddress)
249 ObjAddress = *Mapping.ObjectAddress;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000250 auto AddressIt = SymbolAddresses.find(Entry.first);
251 if (AddressIt != SymbolAddresses.end())
252 ObjAddress = AddressIt->getValue();
253 Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000254 }
255 return Res;
256}
Frederic Riss231f7142014-12-12 17:31:24 +0000257}
258}