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