1//===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DebugMap.h"
10#include "BinaryHolder.h"
11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/StringMap.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/BinaryFormat/MachO.h"
15#include "llvm/Object/ObjectFile.h"
16#include "llvm/Support/Chrono.h"
17#include "llvm/Support/Error.h"
18#include "llvm/Support/Format.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/Path.h"
21#include "llvm/Support/WithColor.h"
22#include "llvm/Support/YAMLTraits.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/TargetParser/Triple.h"
25#include <algorithm>
26#include <cinttypes>
27#include <cstdint>
28#include <memory>
29#include <optional>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace llvm {
35
36namespace dsymutil {
37
38using namespace llvm::object;
39
40DebugMapObject::DebugMapObject(StringRef ObjectFilename,
41 sys::TimePoint<std::chrono::seconds> Timestamp,
42 uint8_t Type)
43 : DebugMapObjectFilter(ObjectFilename), Timestamp(Timestamp), Type(Type) {}
44
45bool DebugMapObject::addSymbol(StringRef Name,
46 std::optional<uint64_t> ObjectAddress,
47 uint64_t LinkedAddress, uint32_t Size) {
48 if (Symbols.count(Key: Name)) {
49 // Symbol was previously added.
50 return true;
51 }
52
53 auto InsertResult = Symbols.insert(
54 KV: std::make_pair(x&: Name, y: SymbolMapping(ObjectAddress, LinkedAddress, Size)));
55
56 if (ObjectAddress && InsertResult.second)
57 AddressToMapping[*ObjectAddress] = &*InsertResult.first;
58 return InsertResult.second;
59}
60
61void DebugMapObject::setRelocationMap(dsymutil::RelocationMap &RM) {
62 RelocMap.emplace(args&: RM);
63}
64
65void DebugMapObject::setInstallName(StringRef IN) { InstallName.emplace(args&: IN); }
66
67void DebugMapObject::print(raw_ostream &OS) const {
68 OS << getObjectFilename() << ":\n";
69 // Sort the symbols in alphabetical order, like llvm-nm (and to get
70 // deterministic output for testing).
71 using Entry = std::pair<StringRef, SymbolMapping>;
72 std::vector<Entry> Entries;
73 Entries.reserve(n: Symbols.getNumItems());
74 for (const auto &Sym : Symbols)
75 Entries.push_back(x: std::make_pair(x: Sym.getKey(), y: Sym.getValue()));
76 llvm::sort(C&: Entries, Comp: llvm::less_first());
77 for (const auto &Sym : Entries) {
78 if (Sym.second.ObjectAddress)
79 OS << format(Fmt: "\t%016" PRIx64, Vals: uint64_t(*Sym.second.ObjectAddress));
80 else
81 OS << "\t????????????????";
82 OS << format(Fmt: " => %016" PRIx64 "+0x%x\t%s\n",
83 Vals: uint64_t(Sym.second.BinaryAddress), Vals: uint32_t(Sym.second.Size),
84 Vals: Sym.first.data());
85 }
86 OS << '\n';
87}
88
89#ifndef NDEBUG
90void DebugMapObject::dump() const { print(errs()); }
91#endif
92
93DebugMapObject &
94DebugMap::addDebugMapObject(StringRef ObjectFilePath,
95 sys::TimePoint<std::chrono::seconds> Timestamp,
96 uint8_t Type) {
97 Objects.emplace_back(args: new DebugMapObject(ObjectFilePath, Timestamp, Type));
98 return *Objects.back();
99}
100
101const DebugMapObject::DebugMapEntry *
102DebugMapObject::lookupSymbol(StringRef SymbolName) const {
103 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(Key: SymbolName);
104 if (Sym == Symbols.end())
105 return nullptr;
106 return &*Sym;
107}
108
109const DebugMapObject::DebugMapEntry *
110DebugMapObject::lookupObjectAddress(uint64_t Address) const {
111 auto Mapping = AddressToMapping.find(Val: Address);
112 if (Mapping == AddressToMapping.end())
113 return nullptr;
114 return Mapping->getSecond();
115}
116
117void DebugMap::print(raw_ostream &OS) const {
118 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
119 yout << const_cast<DebugMap &>(*this);
120}
121
122#ifndef NDEBUG
123void DebugMap::dump() const { print(errs()); }
124#endif
125
126DebugMapObjectFilter::DebugMapObjectFilter(StringRef ObjectFilename)
127 : Filename(std::string(ObjectFilename)) {}
128
129namespace {
130
131struct YAMLContext {
132 YAMLContext(BinaryHolder &BinHolder, StringRef PrependPath)
133 : BinHolder(BinHolder), PrependPath(PrependPath) {}
134 BinaryHolder &BinHolder;
135 StringRef PrependPath;
136 Triple BinaryTriple;
137};
138
139} // end anonymous namespace
140
141DebugMap::DebugMap(const Triple &BinaryTriple, StringRef BinaryPath,
142 ArrayRef<uint8_t> BinaryUUID)
143 : BinaryTriple(BinaryTriple), BinaryPath(std::string(BinaryPath)),
144 BinaryUUID(BinaryUUID.begin(), BinaryUUID.end()) {}
145
146ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
147DebugMap::parseYAMLDebugMap(BinaryHolder &BinHolder, StringRef InputFile,
148 StringRef PrependPath, bool Verbose) {
149 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(Filename: InputFile, /*IsText=*/true);
150 if (auto Err = ErrOrFile.getError())
151 return Err;
152
153 YAMLContext Ctxt(BinHolder, PrependPath);
154
155 std::unique_ptr<DebugMap> Res;
156 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
157 yin >> Res;
158
159 if (auto EC = yin.error())
160 return EC;
161 std::vector<std::unique_ptr<DebugMap>> Result;
162 Result.push_back(x: std::move(Res));
163 return std::move(Result);
164}
165
166} // end namespace dsymutil
167
168namespace yaml {
169
170// Normalize/Denormalize between YAML and a DebugMapObject.
171struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
172 YamlDMO(IO &io) {}
173 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
174 dsymutil::DebugMapObject denormalize(IO &IO);
175
176 std::string Filename;
177 int64_t Timestamp = 0;
178 uint8_t Type = MachO::N_OSO;
179 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
180};
181
182void MappingTraits<std::pair<std::string, SymbolMapping>>::mapping(
183 IO &io, std::pair<std::string, SymbolMapping> &s) {
184 io.mapRequired(Key: "sym", Val&: s.first);
185 io.mapOptional(Key: "objAddr", Val&: s.second.ObjectAddress);
186 io.mapRequired(Key: "binAddr", Val&: s.second.BinaryAddress);
187 io.mapOptional(Key: "size", Val&: s.second.Size);
188}
189
190void MappingTraits<dsymutil::DebugMapObject>::mapping(
191 IO &io, dsymutil::DebugMapObject &DMO) {
192 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
193 io.mapRequired(Key: "filename", Val&: Norm->Filename);
194 io.mapOptional(Key: "timestamp", Val&: Norm->Timestamp);
195 io.mapOptional(Key: "type", Val&: Norm->Type);
196 io.mapRequired(Key: "symbols", Val&: Norm->Entries);
197}
198
199void ScalarTraits<Triple>::output(const Triple &val, void *, raw_ostream &out) {
200 out << val.str();
201}
202
203StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
204 value = Triple(scalar);
205 return StringRef();
206}
207
208size_t
209SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
210 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
211 return seq.size();
212}
213
214dsymutil::DebugMapObject &
215SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
216 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
217 size_t index) {
218 if (index >= seq.size()) {
219 seq.resize(new_size: index + 1);
220 seq[index].reset(p: new dsymutil::DebugMapObject);
221 }
222 return *seq[index];
223}
224
225size_t
226SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObjectFilter>>>::
227 size(IO &io,
228 std::vector<std::unique_ptr<dsymutil::DebugMapObjectFilter>> &seq) {
229 return seq.size();
230}
231
232dsymutil::DebugMapObjectFilter &
233SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObjectFilter>>>::
234 element(IO &io,
235 std::vector<std::unique_ptr<dsymutil::DebugMapObjectFilter>> &seq,
236 size_t index) {
237 if (index >= seq.size()) {
238 seq.resize(new_size: index + 1);
239 seq[index].reset(p: new dsymutil::DebugMapObjectFilter);
240 }
241 return *seq[index];
242}
243
244void MappingTraits<dsymutil::DebugMapObjectFilter>::mapping(
245 IO &io, dsymutil::DebugMapObjectFilter &DMOF) {
246 io.mapRequired(Key: "filename", Val&: DMOF.Filename);
247}
248
249void MappingTraits<dsymutil::DebugMapFilter>::mapping(
250 IO &io, dsymutil::DebugMapFilter &DMF) {
251 io.mapRequired(Key: "objects", Val&: DMF.Objects);
252}
253
254void MappingTraits<std::unique_ptr<dsymutil::DebugMapFilter>>::mapping(
255 IO &io, std::unique_ptr<dsymutil::DebugMapFilter> &DMF) {
256 if (!DMF)
257 DMF.reset(p: new DebugMapFilter());
258 io.mapRequired(Key: "objects", Val&: DMF->Objects);
259}
260
261void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
262 dsymutil::DebugMap &DM) {
263 io.mapRequired(Key: "triple", Val&: DM.BinaryTriple);
264 io.mapOptional(Key: "binary-path", Val&: DM.BinaryPath);
265 if (void *Ctxt = io.getContext())
266 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
267 io.mapOptional(Key: "objects", Val&: DM.Objects);
268}
269
270void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
271 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
272 if (!DM)
273 DM.reset(p: new DebugMap());
274 io.mapRequired(Key: "triple", Val&: DM->BinaryTriple);
275 io.mapOptional(Key: "binary-path", Val&: DM->BinaryPath);
276 if (void *Ctxt = io.getContext())
277 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
278 io.mapOptional(Key: "objects", Val&: DM->Objects);
279}
280
281MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
282 IO &io, dsymutil::DebugMapObject &Obj) {
283 Filename = Obj.Filename;
284 Timestamp = sys::toTimeT(TP: Obj.getTimestamp());
285 Type = Obj.getType();
286 Entries.reserve(n: Obj.Symbols.size());
287 for (auto &Entry : Obj.Symbols)
288 Entries.push_back(
289 x: std::make_pair(x: std::string(Entry.getKey()), y&: Entry.getValue()));
290 llvm::sort(C&: Entries, Comp: llvm::less_first());
291}
292
293dsymutil::DebugMapObject
294MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
295 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
296 SmallString<80> Path(Ctxt.PrependPath);
297 StringMap<uint64_t> SymbolAddresses;
298
299 sys::path::append(path&: Path, a: Filename);
300
301 auto ObjectEntry = Ctxt.BinHolder.getObjectEntry(Filename: Path);
302 if (!ObjectEntry) {
303 auto Err = ObjectEntry.takeError();
304 WithColor::warning() << "Unable to open " << Path << " "
305 << toString(E: std::move(Err)) << '\n';
306 } else {
307 auto Object = ObjectEntry->getObject(T: Ctxt.BinaryTriple);
308 if (!Object) {
309 auto Err = Object.takeError();
310 WithColor::warning() << "Unable to open " << Path << " "
311 << toString(E: std::move(Err)) << '\n';
312 } else {
313 for (const auto &Sym : Object->symbols()) {
314 Expected<uint64_t> AddressOrErr = Sym.getValue();
315 if (!AddressOrErr) {
316 // TODO: Actually report errors helpfully.
317 consumeError(Err: AddressOrErr.takeError());
318 continue;
319 }
320 Expected<StringRef> Name = Sym.getName();
321 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
322 if (!Name || !FlagsOrErr ||
323 (*FlagsOrErr & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) {
324 // TODO: Actually report errors helpfully.
325 if (!FlagsOrErr)
326 consumeError(Err: FlagsOrErr.takeError());
327 if (!Name)
328 consumeError(Err: Name.takeError());
329 continue;
330 }
331 SymbolAddresses[*Name] = *AddressOrErr;
332 }
333 }
334 }
335
336 if (Path.ends_with(Suffix: ".dylib")) {
337 // FIXME: find a more resilient way
338 Type = MachO::N_LIB;
339 }
340 dsymutil::DebugMapObject Res(Path, sys::toTimePoint(T: Timestamp), Type);
341
342 for (auto &Entry : Entries) {
343 auto &Mapping = Entry.second;
344 std::optional<uint64_t> ObjAddress;
345 if (Mapping.ObjectAddress)
346 ObjAddress = *Mapping.ObjectAddress;
347 auto AddressIt = SymbolAddresses.find(Key: Entry.first);
348 if (AddressIt != SymbolAddresses.end())
349 ObjAddress = AddressIt->getValue();
350 Res.addSymbol(Name: Entry.first, ObjectAddress: ObjAddress, LinkedAddress: Mapping.BinaryAddress, Size: Mapping.Size);
351 }
352 return Res;
353}
354
355} // end namespace yaml
356} // end namespace llvm
357